Created
March 9, 2023 07:32
-
-
Save angstyloop/75b553ebc112097de931d5c0bfb943c7 to your computer and use it in GitHub Desktop.
Official GTK4 example 4 (https://docs.gtk.org/gtk4/getting_started.html)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<interface> | |
<object id="window" class="GtkWindow"> | |
<property name="title">Grid</property> | |
<child> | |
<object id="grid" class="GtkGrid"> | |
<child> | |
<object id="button1" class="GtkButton"> | |
<property name="label">Button1</property> | |
<layout> | |
<property name="column">0</property> | |
<property name="row">0</property> | |
</layout> | |
</object> | |
</child> | |
<child> | |
<object id="button2" class="GtkButton"> | |
<property name="label">Button2</property> | |
<layout> | |
<property name="column">1</property> | |
<property name="row">0</property> | |
</layout> | |
</object> | |
</child> | |
<child> | |
<object id="quit" class="GtkButton"> | |
<property name="label">Quit</property> | |
<layout> | |
<property name="column">0</property> | |
<property name="row">1</property> | |
<property name="column-span">2</property> | |
</layout> | |
</object> | |
</child> | |
</object> | |
</child> | |
</object> | |
</interface> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** example-4.c | |
* | |
* COMPILE | |
* | |
* gcc `pkg-config --cflags gtk4` -o example-4 example-4.c `pkg-config --libs gtk4` | |
* | |
* EXAMPLE | |
* | |
* ./example-4 | |
* | |
* SOURCE | |
* | |
* https://docs.gtk.org/gtk4/getting_started.html | |
*/ | |
#include <gtk/gtk.h> | |
#include <glib/gstdio.h> | |
#if GLIB_CHECK_VERSION(2, 74, 0) | |
static int app_flags = G_APPLICATION_DEFAULT_FLAGS; | |
#else | |
static int app_flags = G_APPLICATION_FLAGS_NONE; | |
#endif | |
static void | |
print_hello (GtkWidget *widget, | |
gpointer data) | |
{ | |
g_print ("Hello World\n"); | |
} | |
static void | |
quit_cb (GtkWindow *window) | |
{ | |
gtk_window_close (window); | |
} | |
static void | |
activate (GtkApplication *app, | |
gpointer user_data) | |
{ | |
GtkBuilder *builder = gtk_builder_new (); | |
gtk_builder_add_from_file (builder, "builder.ui", NULL); | |
GObject *window = gtk_builder_get_object (builder, "window"); | |
gtk_window_set_application (GTK_WINDOW (window), app); | |
GObject *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_swapped (button, "clicked", G_CALLBACK (quit_cb), window); | |
gtk_widget_show (GTK_WIDGET(window)); | |
g_object_unref (builder); | |
} | |
int | |
main (int argc, | |
char *argv[]) | |
{ | |
#ifdef GTK_SRCDIR | |
g_chdir (GTK_SRCDIR); | |
#endif | |
GtkApplication *app = gtk_application_new ("org.gtk.example", app_flags); | |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); | |
int status = g_application_run (G_APPLICATION (app), argc, argv); | |
g_object_unref (app); | |
return status; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment