Last active
March 9, 2023 05:29
-
-
Save angstyloop/2ba51845fae3d403bf64b9d8c7d4fc71 to your computer and use it in GitHub Desktop.
Official GTK4 example 1 (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
/** example-1.c | |
* | |
* COMPILE | |
* | |
* gcc `pkg-config --cflags gtk4` -o example-1 example-1.c `pkg-config --libs gtk4` | |
* | |
* EXAMPLE | |
* | |
* ./example-1 | |
* | |
* SOURCE | |
* | |
* https://docs.gtk.org/gtk4/getting_started.html | |
*/ | |
#include <gtk/gtk.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 activate(GtkApplication *app, gpointer user_data) | |
{ | |
GtkWidget *window, *button, *box; | |
window = gtk_application_window_new (app); | |
gtk_window_set_title (GTK_WINDOW (window), "Window"); | |
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200); | |
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0); | |
gtk_widget_set_halign (box, GTK_ALIGN_CENTER); | |
gtk_widget_set_valign (box, GTK_ALIGN_CENTER); | |
gtk_window_set_child (GTK_WINDOW (window), box); | |
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_window_destroy), window); | |
gtk_box_append (GTK_BOX (box), button); | |
gtk_widget_show(window); | |
} | |
int main (int argc, char **argv) | |
{ | |
GtkApplication *app; | |
int status; | |
app = gtk_application_new ("org.gtk.example", app_flags); | |
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL); | |
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