Created
April 13, 2024 16:27
-
-
Save ollie314/db92cfa125867070484e7b47ab2ac4c8 to your computer and use it in GitHub Desktop.
Sample Gtk application
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
/* Sample using gtk3 */ | |
/* Compiling: gcc `pkg-config --cflags gtk+-3.0` -o main main.c `pkg-config --libs gtk+-3.0`*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <gtk/gtk.h> | |
void on_window_destroy(GtkWidget *widget, gpointer data) | |
{ | |
gtk_main_quit(); | |
} | |
void on_button_clicked(GtkWidget *widget, gpointer data) | |
{ | |
GtkWidget *entry = GTK_WIDGET(data); | |
const gchar *input_text = gtk_entry_get_text(GTK_ENTRY(entry)); | |
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(gtk_widget_get_toplevel(widget)), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "We will store: %s", input_text); | |
gtk_dialog_run(GTK_DIALOG(dialog)); | |
gtk_widget_destroy(dialog); | |
} | |
int main(int argc, char** argv) | |
{ | |
gtk_init(&argc, &argv); | |
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
gtk_window_set_title(GTK_WINDOW(window), "Archibald"); | |
gtk_container_set_border_width(GTK_CONTAINER(window), 10); | |
gtk_widget_set_size_request(window, 200, 100); | |
g_signal_connect(window, "destroy", G_CALLBACK(on_window_destroy), NULL); | |
GtkWidget *label = gtk_label_new("This is a test from a newbie"); | |
GtkWidget *input_text = gtk_entry_new(); | |
gtk_entry_set_placeholder_text(GTK_ENTRY(input_text), "Enter the value"); | |
GtkWidget *button = gtk_button_new_with_label("Save"); | |
g_signal_connect(button, "clicked", G_CALLBACK(on_button_clicked), input_text); | |
GtkWidget *box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); | |
gtk_box_pack_start(GTK_BOX(box), label, FALSE, FALSE, 0); | |
gtk_box_pack_start(GTK_BOX(box), input_text, FALSE, FALSE, 0); | |
gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); | |
gtk_container_add(GTK_CONTAINER(window), box); | |
gtk_widget_show_all(window); | |
gtk_main(); | |
return EXIT_SUCCESS; | |
} |
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
/* Sample using gtk4 */ | |
/* Compiling: gcc $(pkg-config --cflags gtk4) -o main main.c $(pkg-config --libs gtk4) */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <gtk/gtk.h> | |
static void print_hello(GtkWidget *widget, gpointer data) | |
{ | |
g_print("test\n"); | |
} | |
static void activate(GtkApplication *app, gpointer user_data) | |
{ | |
GtkWidget *window; | |
GtkWidget *button; | |
window = gtk_application_window_new(app); | |
gtk_window_set_title(GTK_WINDOW (window), "Anibal"); | |
gtk_window_set_default_size(GTK_WINDOW (window), 200, 200); | |
button = gtk_button_new_with_label("Check"); | |
g_signal_connect(button, "clicked", G_CALLBACK (print_hello), NULL); | |
gtk_window_set_child(GTK_WINDOW (window), button); | |
gtk_window_present(GTK_WINDOW(window)); | |
} | |
int main(int argc, char **argv) | |
{ | |
GtkApplication *app; | |
int status; | |
/* To use the following enum, we must update glibc */ | |
/*app = gtk_application_new("io.digitalean.anibal", G_APPLICATION_DEFAULT_FLAGS);*/ | |
app = gtk_application_new("io.digitalean.anibal", 0); | |
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