From: Scott Worley Date: Thu, 23 Feb 2023 05:44:03 +0000 (-0800) Subject: Start on chart GTK widget X-Git-Url: http://git.scottworley.com/batteryviewer/commitdiff_plain/5a9453723d439ed0e58681d7c0955c9f2e348b4d Start on chart GTK widget --- diff --git a/Makefile b/Makefile index 8bc7dbb..ffbff75 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,8 @@ bindir = $(exec_prefix)/bin INSTALL = install -D INSTALL_PROGRAM = $(INSTALL) -batteryviewer: batteryviewer.c - gcc $(shell pkg-config --cflags gtk+-3.0) -o $@ $(CFLAGS) $< $(shell pkg-config --libs gtk+-3.0) +batteryviewer: batteryviewer.c chart.c + gcc $(shell pkg-config --cflags gtk+-3.0) -o $@ $(CFLAGS) $^ $(shell pkg-config --libs gtk+-3.0) all: batteryviewer diff --git a/batteryviewer.c b/batteryviewer.c index 3b5add9..abc5106 100644 --- a/batteryviewer.c +++ b/batteryviewer.c @@ -1,10 +1,6 @@ +#include "chart.h" #include -static void print_hello(GtkWidget *widget __attribute__((unused)), - gpointer data __attribute__((unused))) { - g_print("Hello World\n"); -} - static void activate(GtkApplication *app, gpointer user_data __attribute__((unused))) { @@ -12,14 +8,11 @@ static void activate(GtkApplication *app, gtk_window_set_title(GTK_WINDOW(window), "Window"); gtk_window_set_default_size(GTK_WINDOW(window), 200, 200); - GtkWidget *button_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL); - gtk_container_add(GTK_CONTAINER(window), button_box); + GtkWidget *chart_box = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL); + gtk_container_add(GTK_CONTAINER(window), chart_box); - GtkWidget *button = gtk_button_new_with_label("Hello World"); - g_signal_connect(button, "clicked", G_CALLBACK(print_hello), NULL); - g_signal_connect_swapped(button, "clicked", G_CALLBACK(gtk_widget_destroy), - window); - gtk_container_add(GTK_CONTAINER(button_box), button); + GtkWidget *chart = bv_chart_new(); + gtk_container_add(GTK_CONTAINER(chart_box), chart); gtk_widget_show_all(window); } diff --git a/chart.c b/chart.c new file mode 100644 index 0000000..3efd454 --- /dev/null +++ b/chart.c @@ -0,0 +1,29 @@ +/* + * h/t https://ptomato.name/advanced-gtk-techniques/html/custom-container.html + * for examples of how to make GTK widgets + */ + +#include "chart.h" +#include + +#define BV_CHART_PRIVATE(obj) \ + (G_TYPE_INSTANCE_GET_PRIVATE((obj), BV_CHART_TYPE, BVChartPrivate)) + +typedef struct _BVChartPrivate BVChartPrivate; + +struct _BVChartPrivate { + int temp; +}; + +G_DEFINE_TYPE_WITH_CODE(BVChart, bv_chart, GTK_TYPE_DRAWING_AREA, + G_ADD_PRIVATE(BVChart)) + +static void bv_chart_class_init(BVChartClass *klass __attribute__((unused))) {} + +static void bv_chart_init(BVChart *chart) { + gtk_widget_set_has_window(GTK_WIDGET(chart), FALSE); +} + +GtkWidget *bv_chart_new() { + return GTK_WIDGET(g_object_new(bv_chart_get_type(), NULL)); +} diff --git a/chart.h b/chart.h new file mode 100644 index 0000000..7cda505 --- /dev/null +++ b/chart.h @@ -0,0 +1,39 @@ +#ifndef BV_CHART_H +#define BV_CHART_H + +/* + * h/t https://ptomato.name/advanced-gtk-techniques/html/custom-container.html + * for examples of how to make GTK widgets + */ + +#include +#include + +G_BEGIN_DECLS + +#define BV_CHART_TYPE (bv_chart_get_type()) +#define BV_CHART(obj) \ + (G_TYPE_CHECK_INSTANCE_CAST((obj), BV_CHART_TYPE, BVChart)) +#define BV_CHART_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_CAST((klass), BV_CHART_TYPE, BVChartClass)) +#define BV_IS_CHART(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), BV_CHART_TYPE)) +#define BV_IS_CHART_CLASS(klass) \ + (G_TYPE_CHECK_CLASS_TYPE((klass), BV_CHART_TYPE)) + +typedef struct _BVChart BVChart; +typedef struct _BVChartClass BVChartClass; + +struct _BVChart { + GtkDrawingArea parent_instance; +}; + +struct _BVChartClass { + GtkDrawingAreaClass parent_class; +}; + +GType bv_chart_get_type(void) G_GNUC_CONST; +GtkWidget *bv_chart_new(void); + +G_END_DECLS + +#endif /* BV_CHART_H */