From c284ea6baf08936353a65c6a820708fcd566c42a Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Mon, 19 Dec 2022 19:48:22 +0100 Subject: [PATCH] feat: add gtk3 examples --- gtk3/example-0/exampleapp.c | 38 +++++++++++++++++++ gtk3/example-0/makefile | 23 ++++++++++++ gtk3/example-0/readme.md | 5 +++ gtk3/example-1/exampleapp.c | 58 ++++++++++++++++++++++++++++ gtk3/example-1/makefile | 23 ++++++++++++ gtk3/example-1/readme.md | 5 +++ gtk3/example-2/exampleapp.c | 75 +++++++++++++++++++++++++++++++++++++ gtk3/example-2/makefile | 23 ++++++++++++ gtk3/example-2/readme.md | 5 +++ gtk3/example-3/builder.ui | 45 ++++++++++++++++++++++ gtk3/example-3/exampleapp.c | 46 +++++++++++++++++++++++ gtk3/example-3/makefile | 23 ++++++++++++ gtk3/example-3/readme.md | 7 ++++ 13 files changed, 376 insertions(+) create mode 100644 gtk3/example-0/exampleapp.c create mode 100644 gtk3/example-0/makefile create mode 100644 gtk3/example-0/readme.md create mode 100644 gtk3/example-1/exampleapp.c create mode 100644 gtk3/example-1/makefile create mode 100644 gtk3/example-1/readme.md create mode 100644 gtk3/example-2/exampleapp.c create mode 100644 gtk3/example-2/makefile create mode 100644 gtk3/example-2/readme.md create mode 100644 gtk3/example-3/builder.ui create mode 100644 gtk3/example-3/exampleapp.c create mode 100644 gtk3/example-3/makefile create mode 100644 gtk3/example-3/readme.md diff --git a/gtk3/example-0/exampleapp.c b/gtk3/example-0/exampleapp.c new file mode 100644 index 0000000..5aa358f --- /dev/null +++ b/gtk3/example-0/exampleapp.c @@ -0,0 +1,38 @@ +#include + +static void activate (GtkApplication* app, gpointer user_data) +{ + GtkWidget *window; + + /*create new object and store it at the pointer*/ + window = gtk_application_window_new (app); + + /*set window title*/ + /*cast pointer using the macro GTK_WINDOW() instead of C cast*/ + gtk_window_set_title (GTK_WINDOW (window), "Window Title"); + + /*set window size*/ + gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); + + /*make window visible*/ + gtk_widget_show_all (window); +} + +/*create a GtkApplication object and run it*/ +int main (int argc, char **argv) +{ + GtkApplication *app; + int status; + + /*initialise pointer with app id (name) and without flags*/ + app = gtk_application_new ("de.swingbe.gtk.example0", G_APPLICATION_FLAGS_NONE); + + /*connect the active signal with the active function defined above*/ + g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); + + /*launch app and pass through arguments*/ + status = g_application_run (G_APPLICATION (app), argc, argv); + g_object_unref (app); + + return status; +} diff --git a/gtk3/example-0/makefile b/gtk3/example-0/makefile new file mode 100644 index 0000000..7990516 --- /dev/null +++ b/gtk3/example-0/makefile @@ -0,0 +1,23 @@ +CC ?= gcc +PKGCONFIG = $(shell which pkg-config) +CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0) +LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0) + +SRC = exampleapp.c + +OBJS = $(SRC:.c=.o) + +all: exampleapp + +%.o: %.c + + $(CC) -c -o $(@F) $(CFLAGS) $< + +exampleapp: $(OBJS) + + $(CC) -o $(@F) $(OBJS) $(LIBS) + +clean: + + rm -f $(OBJS) + rm -f exampleapp diff --git a/gtk3/example-0/readme.md b/gtk3/example-0/readme.md new file mode 100644 index 0000000..676dfba --- /dev/null +++ b/gtk3/example-0/readme.md @@ -0,0 +1,5 @@ +* build + +``` +gcc `pkg-config --cflags gtk+-3.0` -o example-1 example-1.c `pkg-config --libs gtk+-3.0` +``` \ No newline at end of file diff --git a/gtk3/example-1/exampleapp.c b/gtk3/example-1/exampleapp.c new file mode 100644 index 0000000..87061cf --- /dev/null +++ b/gtk3/example-1/exampleapp.c @@ -0,0 +1,58 @@ +#include + +static void print_hello (GtkWidget *widget, + gpointer data) +{ + /*print in a terminal*/ + g_print ("Clicked\n"); +} + +static void +activate (GtkApplication *app, + gpointer user_data) +{ + GtkWidget *window; + GtkWidget *button; + + /*controlling button size and layout by storing it inside a box*/ + GtkWidget *button_box; + + window = gtk_application_window_new (app); + gtk_window_set_title (GTK_WINDOW (window), "Title"); + gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); + + /*create and assigen box with orientation*/ + button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL); + + /*add button box to window*/ + gtk_container_add (GTK_CONTAINER (window), button_box); + + /*initialise button*/ + button = gtk_button_new_with_label ("Button"); + + /*connect button with callback function with NULL as input*/ + g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + + /*connect button with destroy callback to destroy the window instead of the button*/ + g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window); + + /*add button to window*/ + gtk_container_add (GTK_CONTAINER (button_box), button); + + gtk_widget_show_all (window); +} + +int +main (int argc, + char **argv) +{ + GtkApplication *app; + int status; + + app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE); + g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); + status = g_application_run (G_APPLICATION (app), argc, argv); + g_object_unref (app); + + return status; +} diff --git a/gtk3/example-1/makefile b/gtk3/example-1/makefile new file mode 100644 index 0000000..7990516 --- /dev/null +++ b/gtk3/example-1/makefile @@ -0,0 +1,23 @@ +CC ?= gcc +PKGCONFIG = $(shell which pkg-config) +CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0) +LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0) + +SRC = exampleapp.c + +OBJS = $(SRC:.c=.o) + +all: exampleapp + +%.o: %.c + + $(CC) -c -o $(@F) $(CFLAGS) $< + +exampleapp: $(OBJS) + + $(CC) -o $(@F) $(OBJS) $(LIBS) + +clean: + + rm -f $(OBJS) + rm -f exampleapp diff --git a/gtk3/example-1/readme.md b/gtk3/example-1/readme.md new file mode 100644 index 0000000..676dfba --- /dev/null +++ b/gtk3/example-1/readme.md @@ -0,0 +1,5 @@ +* build + +``` +gcc `pkg-config --cflags gtk+-3.0` -o example-1 example-1.c `pkg-config --libs gtk+-3.0` +``` \ No newline at end of file diff --git a/gtk3/example-2/exampleapp.c b/gtk3/example-2/exampleapp.c new file mode 100644 index 0000000..2d9db4f --- /dev/null +++ b/gtk3/example-2/exampleapp.c @@ -0,0 +1,75 @@ +#include + +static void +print_hello (GtkWidget *widget, + gpointer data) +{ + g_print ("Hello World\n"); +} + +static void +activate (GtkApplication *app, + gpointer user_data) +{ + GtkWidget *window; + GtkWidget *grid; + GtkWidget *button; + + /* create a new window, and set its title */ + window = gtk_application_window_new (app); + gtk_window_set_title (GTK_WINDOW (window), "Window"); + gtk_container_set_border_width (GTK_CONTAINER (window), 10); + + /* Here we construct the container that is going pack our buttons */ + grid = gtk_grid_new (); + + /* Pack the container in the window */ + gtk_container_add (GTK_CONTAINER (window), grid); + + button = gtk_button_new_with_label ("Button 1"); + g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + + /* Place the first button in the grid cell (0, 0), and make it fill + * just 1 cell horizontally and vertically (ie no spanning) + */ + gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1); + + button = gtk_button_new_with_label ("Button 2"); + g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + + /* Place the second button in the grid cell (1, 0), and make it fill + * just 1 cell horizontally and vertically (ie no spanning) + */ + gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1); + + button = gtk_button_new_with_label ("Quit"); + g_signal_connect_swapped (button, "clicked", G_CALLBACK (gtk_widget_destroy), window); + + /* Place the Quit button in the grid cell (0, 1), and make it + * span 2 columns. + */ + gtk_grid_attach (GTK_GRID (grid), button, 0, 1, 2, 1); + + /* Now that we are done packing our widgets, we show them all + * in one go, by calling gtk_widget_show_all() on the window. + * This call recursively calls gtk_widget_show() on all widgets + * that are contained in the window, directly or indirectly. + */ + gtk_widget_show_all (window); + +} + +int +main (int argc, + char **argv) +{ + GtkApplication *app; + int status; + + app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE); + g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); + status = g_application_run (G_APPLICATION (app), argc, argv); + g_object_unref (app); + + return status; +} diff --git a/gtk3/example-2/makefile b/gtk3/example-2/makefile new file mode 100644 index 0000000..7990516 --- /dev/null +++ b/gtk3/example-2/makefile @@ -0,0 +1,23 @@ +CC ?= gcc +PKGCONFIG = $(shell which pkg-config) +CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0) +LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0) + +SRC = exampleapp.c + +OBJS = $(SRC:.c=.o) + +all: exampleapp + +%.o: %.c + + $(CC) -c -o $(@F) $(CFLAGS) $< + +exampleapp: $(OBJS) + + $(CC) -o $(@F) $(OBJS) $(LIBS) + +clean: + + rm -f $(OBJS) + rm -f exampleapp diff --git a/gtk3/example-2/readme.md b/gtk3/example-2/readme.md new file mode 100644 index 0000000..f29ce20 --- /dev/null +++ b/gtk3/example-2/readme.md @@ -0,0 +1,5 @@ +* build + +``` +gcc `pkg-config --cflags gtk+-3.0` -o example-2 example-2.c `pkg-config --libs gtk+-3.0` +``` \ No newline at end of file diff --git a/gtk3/example-3/builder.ui b/gtk3/example-3/builder.ui new file mode 100644 index 0000000..6321c93 --- /dev/null +++ b/gtk3/example-3/builder.ui @@ -0,0 +1,45 @@ + + + True + Grid + 10 + + + True + + + True + Button 1 + + + 0 + 0 + + + + + True + Button 2 + + + 1 + 0 + + + + + True + Quit + + + 0 + 1 + 2 + + + + + + + + diff --git a/gtk3/example-3/exampleapp.c b/gtk3/example-3/exampleapp.c new file mode 100644 index 0000000..3525a03 --- /dev/null +++ b/gtk3/example-3/exampleapp.c @@ -0,0 +1,46 @@ +#include + +static void +print_hello (GtkWidget *widget, + gpointer data) +{ + g_print ("Hello World\n"); +} + +int +main (int argc, + char *argv[]) +{ + GtkBuilder *builder; + GObject *window; + GObject *button; + GError *error = NULL; + + gtk_init (&argc, &argv); + + /* Construct a GtkBuilder instance and load our UI description */ + builder = gtk_builder_new (); + if (gtk_builder_add_from_file (builder, "builder.ui", &error) == 0) + { + g_printerr ("Error loading file: %s\n", error->message); + g_clear_error (&error); + return 1; + } + + /* Connect signal handlers to the constructed widgets. */ + window = gtk_builder_get_object (builder, "window"); + g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL); + + button = gtk_builder_get_object (builder, "button1"); + g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + + button = gtk_builder_get_object (builder, "button2"); + g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + + button = gtk_builder_get_object (builder, "quit"); + g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL); + + gtk_main (); + + return 0; +} diff --git a/gtk3/example-3/makefile b/gtk3/example-3/makefile new file mode 100644 index 0000000..7990516 --- /dev/null +++ b/gtk3/example-3/makefile @@ -0,0 +1,23 @@ +CC ?= gcc +PKGCONFIG = $(shell which pkg-config) +CFLAGS = $(shell $(PKGCONFIG) --cflags gtk+-3.0) +LIBS = $(shell $(PKGCONFIG) --libs gtk+-3.0) + +SRC = exampleapp.c + +OBJS = $(SRC:.c=.o) + +all: exampleapp + +%.o: %.c + + $(CC) -c -o $(@F) $(CFLAGS) $< + +exampleapp: $(OBJS) + + $(CC) -o $(@F) $(OBJS) $(LIBS) + +clean: + + rm -f $(OBJS) + rm -f exampleapp diff --git a/gtk3/example-3/readme.md b/gtk3/example-3/readme.md new file mode 100644 index 0000000..e70d476 --- /dev/null +++ b/gtk3/example-3/readme.md @@ -0,0 +1,7 @@ +* build manually + +``` +gcc `pkg-config --cflags gtk+-3.0` -o example-3 example-3.c `pkg-config --libs gtk+-3.0` +``` + +* or build using ```make``` instruction \ No newline at end of file