├── .vscode └── settings.json ├── LICENSE ├── README.md ├── banner.png ├── c ├── gtk3 │ ├── action-bar.c │ ├── application-window.c │ ├── application.c │ ├── assistant.c │ ├── box.c │ ├── builder.c.in │ ├── button.c │ ├── combo-box-text.c │ ├── combo-box.c │ ├── dialog-1.c │ ├── dialog-2.c │ ├── entry-completion.c │ ├── entry.c │ ├── expander.c │ ├── file-chooser-button.c │ ├── file-chooser-dialog.c │ ├── fixed.c │ ├── flow-box.c │ ├── glarea.c │ ├── grid-1.c │ ├── grid-2.c │ ├── grid-3.c │ ├── header-bar.c │ ├── image-1.c.in │ ├── image-2.c │ ├── info-bar.c │ ├── label.c │ ├── list-box.c │ ├── meson.build │ ├── message-dialog.c │ ├── stack-1.c │ ├── stack-2.c │ ├── text-view.c │ ├── tree-view.c │ └── window.c └── gtk4 │ ├── application-window.c │ ├── application.c │ ├── box.c │ ├── builder.c.in │ ├── glarea.c │ ├── header-bar.c │ ├── label.c │ ├── meson.build │ └── window.c ├── data ├── builder-gtk3.ui ├── builder-gtk4.ui ├── css-provider.css ├── grid-explained.png ├── resources.xml └── template.ui ├── lua ├── gtk3 │ ├── action-bar.lua │ ├── application-window.lua │ ├── application.lua │ ├── assistant.lua │ ├── box.lua │ ├── builder-1.lua.in │ ├── builder-2.lua.in │ ├── button.lua │ ├── combo-box-text.lua │ ├── combo-box.lua │ ├── css-provider.lua.in │ ├── dialog-1.lua │ ├── dialog-2.lua │ ├── entry-completion.lua │ ├── entry.lua │ ├── expander.lua │ ├── file-chooser-button.lua │ ├── file-chooser-dailog.lua │ ├── fixed.lua │ ├── flow-box.lua │ ├── grid-1.lua │ ├── grid-2.lua │ ├── grid-3.lua │ ├── header-bar.lua │ ├── image-1.lua.in │ ├── image-2.lua │ ├── info-bar.lua │ ├── label-1.lua │ ├── label-2.lua │ ├── list-box.lua │ ├── meson.build │ ├── message-dialog-1.lua │ ├── message-dialog-2.lua │ ├── notebook.lua │ ├── overlay.lua │ ├── paned.lua │ ├── progress-bar.lua │ ├── revealer.lua │ ├── scrolled-window.lua │ ├── spinner.lua │ ├── stack-1.lua │ ├── stack-2.lua │ ├── status-bar.lua │ ├── tree-view.lua │ └── window.lua └── gtk4 │ ├── action-bar.lua │ ├── application-window.lua │ ├── application.lua │ ├── assistant.lua │ ├── box.lua │ ├── builder-1.lua.in │ ├── builder-2.lua.in │ ├── button.lua │ ├── combo-box-text.lua │ ├── combo-box.lua │ ├── css-provider.lua.in │ ├── header-bar.lua │ ├── label.lua │ └── meson.build ├── meson.build └── vala ├── gtk3 ├── action-bar.vala ├── application-window.vala ├── application.vala ├── assistant.vala ├── box.vala ├── builder.vala.in ├── button.vala ├── combo-box-text.vala ├── dialog.vala ├── entry-completion.vala ├── entry.vala ├── fixed.vala ├── flow-box.vala ├── header-bar.vala ├── label.vala ├── list-box.vala ├── meson.build ├── message-dialog.vala ├── template.vala └── window.vala └── gtk4 ├── action-bar.vala ├── application-window.vala ├── application.vala ├── assistant.vala ├── box.vala ├── builder.vala.in ├── css-provider.vala.in ├── fixed.vala ├── header-bar.vala ├── label.vala ├── list-box.vala ├── meson.build └── window.vala /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "mesonbuild.configureOnOpen": true, 3 | "C_Cpp.default.compileCommands": "_BUILD/compile_commands.json", 4 | "C_Cpp.default.configurationProvider": "mesonbuild.mesonbuild" 5 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2021-2025 Josué Martínez 2 | 3 | This software is provided 'as-is', without any express or implied 4 | warranty. In no event will the authors be held liable for any damages 5 | arising from the use of this software. 6 | 7 | Permission is granted to anyone to use this software for any purpose, 8 | including commercial applications, and to alter it and redistribute it 9 | freely, subject to the following restrictions: 10 | 11 | 1. The origin of this software must not be misrepresented; you must not 12 | claim that you wrote the original software. If you use this software 13 | in a product, an acknowledgment in the product documentation would be 14 | appreciated but is not required. 15 | 2. Altered source versions must be plainly marked as such, and must not be 16 | misrepresented as being the original software. 17 | 3. This notice may not be removed or altered from any source distribution. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![License][LicenseBadge]][LicenseURL] 2 | 3 | # GTK Examples 4 | 5 | > [!NOTE] 6 | > _"It's been X days/weeks/months since last commit, is this project still maintained?"_ 7 | > __Yes__. I'm just busy working :) 8 | 9 | A set of various GTK code examples in different programming languages. 10 | 11 | ![GTK Examples Banner](banner.png) 12 | 13 | ## Building and running 14 | 15 | The main requirement to build and run the (C and Vala) examples is to have: 16 | 17 | - Development files for GTK (3 and 4) 18 | - Meson 19 | - C and Vala compilers 20 | 21 | > [!NOTE] 22 | > You might also need the GLEW development files (needed by the glarea example), try building and running first to see if everything it's ok 23 | 24 | Then just run: 25 | 26 | ``` 27 | meson setup _BUILD . 28 | ninja -C _BUILD 29 | ``` 30 | 31 | And that's it! You'll found compiled binaries under the following folders: 32 | 33 | - _BUILD/c/ for the C examples 34 | - _BUILD/vala/ for the Vala examples 35 | 36 | ### Lua 37 | 38 | In the case of the Lua examples, they're supposed to run on any version of Lua, and the only requirement is to have [LGI](https://github.com/lgi-devs/lgi) installed, BUT: I highly recommend you to have the Git version. That's because the Git version supports GTK 4 and libadwaita, so you'll be sure every example will work. 39 | 40 | > [!WARNING] 41 | > To build [LGI](https://github.com/lgi-devs/lgi) you'll need `gobject-introspection`, check out the [LGI repo](https://github.com/lgi-devs/lgi) for more details if you're having trouble. 42 | 43 | ## About this repository 44 | 45 | I decided to merge all my repos with GTK examples here because I feel that having different repos for the same purpose and with (basically) the same code is a mess, so I prefer to have all these examples in one place. 46 | 47 | ## About some examples 48 | 49 | Note that GTK 3 and 4 are considerably different, which means that I'll try to have the same examples for both versions, but not all of them will be present in both versions. That said, here's a list of notes about some of the examples: 50 | 51 | - GtkDialog: GTK 3 only, this class was deprecated in GTK 4.10 52 | - GtkAssistant: in GTK 3 you can set a subtitle in the header bar, but not in GTK 4 because there's no subtitle property in GtkHeaderBar. 53 | - GtkCssProvider: in GTK 4 this class was deprecated in GTK 4.10, the example is still present, but it's slightly different 54 | 55 | [LicenseBadge]: https://img.shields.io/badge/License-Zlib-brightgreen?style=for-the-badge 56 | [LicenseURL]: https://opensource.org/licenses/Zlib -------------------------------------------------------------------------------- /banner.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Miqueas/GTK-Examples/3cad3511fc26ba19a87636bb2eba6253d9733f67/banner.png -------------------------------------------------------------------------------- /c/gtk3/action-bar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.ActionBar"; 7 | const gchar *appTitle = "GtkActionBar"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *actionBar, *actionBarLabel, *actionBarButton, *box, *boxLabel; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | actionBar = gtk_action_bar_new(); 31 | actionBarLabel = gtk_label_new("Something"); 32 | actionBarButton = gtk_button_new_with_label("A button"); 33 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); 34 | boxLabel = gtk_label_new("App content"); 35 | 36 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 37 | gtk_container_add(GTK_CONTAINER(window), box); 38 | 39 | gtk_action_bar_pack_start(GTK_ACTION_BAR(actionBar), actionBarLabel); 40 | gtk_action_bar_pack_end(GTK_ACTION_BAR(actionBar), actionBarButton); 41 | 42 | gtk_box_pack_start(GTK_BOX(box), boxLabel, TRUE, TRUE, 0); 43 | gtk_box_pack_end(GTK_BOX(box), actionBar, FALSE, TRUE, 0); 44 | gtk_widget_show_all(box); 45 | } -------------------------------------------------------------------------------- /c/gtk3/application-window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppStartup(GApplication *self, gpointer data); 4 | void onAppActivate(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.ApplicationWindow"; 7 | const gchar *appTitle = "GtkApplicationWindow"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window = gtk_application_window_new(GTK_APPLICATION(self)); 28 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 29 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 30 | } -------------------------------------------------------------------------------- /c/gtk3/application.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Application"; 7 | 8 | int main(int argc, char **argv) { 9 | GtkApplication *app = gtk_application_new(appID, 0); 10 | 11 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 12 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 13 | 14 | int result = g_application_run(G_APPLICATION(app), argc, argv); 15 | g_object_unref(app); 16 | 17 | return result; 18 | } 19 | 20 | void onAppActivate(GApplication *self, gpointer data) { 21 | g_print("Hello, world!\n"); 22 | } 23 | 24 | void onAppStartup(GApplication *self, gpointer data) { 25 | g_print("Initializing the app... Done!\n"); 26 | } -------------------------------------------------------------------------------- /c/gtk3/box.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onButtonClicked(GtkButton *self, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Box"; 8 | const gchar *appTitle = "GtkBox"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int result = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return result; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *box, *label, *button; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | label = gtk_label_new("Click the button"); 32 | button = gtk_button_new_with_label("🤔"); 33 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 34 | 35 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 36 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 37 | gtk_container_add(GTK_CONTAINER(window), box); 38 | 39 | gtk_widget_set_halign(button, GTK_ALIGN_CENTER); 40 | gtk_widget_set_valign(button, GTK_ALIGN_CENTER); 41 | g_signal_connect(button, "clicked", G_CALLBACK(onButtonClicked), NULL); 42 | 43 | gtk_widget_set_halign(box, GTK_ALIGN_CENTER); 44 | gtk_widget_set_valign(box, GTK_ALIGN_CENTER); 45 | gtk_box_pack_start(GTK_BOX(box), label, FALSE, TRUE, 0); 46 | gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0); 47 | gtk_widget_show_all(box); 48 | } 49 | 50 | void onButtonClicked(GtkButton *self, gpointer data) { 51 | static int count = 0; 52 | g_print("You clicked %d times!\n", ++count); 53 | } -------------------------------------------------------------------------------- /c/gtk3/builder.c.in: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppStartup(GApplication *self, gpointer data); 4 | void onAppActivate(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Builder"; 7 | 8 | int main(int argc, char **argv) { 9 | GtkApplication *app = gtk_application_new(appID, 0); 10 | 11 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 12 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 13 | 14 | int result = g_application_run(G_APPLICATION(app), argc, argv); 15 | g_object_unref(app); 16 | 17 | return result; 18 | } 19 | 20 | void onAppActivate(GApplication *self, gpointer data) { 21 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 22 | gtk_window_present(window); 23 | } 24 | 25 | void onAppStartup(GApplication *self, gpointer data) { 26 | GtkBuilder *builder = gtk_builder_new_from_file("@builderPath@"); 27 | GObject *window = gtk_builder_get_object(builder, "window"); 28 | gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window)); 29 | } -------------------------------------------------------------------------------- /c/gtk3/button.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onButtonClicked(GtkButton *self, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Button"; 8 | const gchar *appTitle = "GtkButton"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int result = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return result; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *button; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | button = gtk_button_new_with_label("Click me"); 32 | 33 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | gtk_container_add(GTK_CONTAINER(window), button); 36 | 37 | gtk_widget_set_visible(button, TRUE); 38 | gtk_widget_set_halign(button, GTK_ALIGN_CENTER); 39 | gtk_widget_set_valign(button, GTK_ALIGN_CENTER); 40 | g_signal_connect(button, "clicked", G_CALLBACK(onButtonClicked), NULL); 41 | } 42 | 43 | void onButtonClicked(GtkButton *self, gpointer data) { 44 | g_print("You clicked me!\n"); 45 | } -------------------------------------------------------------------------------- /c/gtk3/combo-box-text.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onComboBoxChanged(GtkComboBox *self, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.ComboBoxText"; 8 | const gchar *appTitle = "GtkComboBoxText"; 9 | const gchar *comboBoxTextValues[8][2] = { 10 | { "gnome", "GNOME" }, 11 | { "plasma", "KDE Plasma" }, 12 | { "xfce", "XFCE" }, 13 | { "mate", "MATE" }, 14 | { "cinnamon", "Cinnamon" }, 15 | { "pantheon", "Pantheon" }, 16 | { "lxde", "LXDE" }, 17 | { "lxqt", "LXQT" } 18 | }; 19 | 20 | int main(int argc, char **argv) { 21 | GtkApplication *app = gtk_application_new(appID, 0); 22 | 23 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 24 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 25 | 26 | int result = g_application_run(G_APPLICATION(app), argc, argv); 27 | g_object_unref(app); 28 | 29 | return result; 30 | } 31 | 32 | void onAppActivate(GApplication *self, gpointer data) { 33 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 34 | gtk_window_present(window); 35 | } 36 | 37 | void onAppStartup(GApplication *self, gpointer data) { 38 | GtkWidget *window, *hintLabel, *comboBox, *box; 39 | 40 | window = gtk_application_window_new(GTK_APPLICATION(self)); 41 | comboBox = gtk_combo_box_text_new(); 42 | hintLabel = gtk_label_new("Default id: gnome"); 43 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 44 | 45 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 46 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 47 | gtk_container_add(GTK_CONTAINER(window), box); 48 | 49 | for (int i = 0; i < 8; i++) { 50 | const gchar *id = comboBoxTextValues[i][0]; 51 | const gchar *text = comboBoxTextValues[i][1]; 52 | gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(comboBox), id, text); 53 | } 54 | 55 | gtk_combo_box_set_active_id(GTK_COMBO_BOX(comboBox), "gnome"); 56 | 57 | g_object_set(box, "halign", GTK_ALIGN_CENTER, "valign", GTK_ALIGN_CENTER, NULL); 58 | gtk_box_pack_start(GTK_BOX(box), gtk_label_new("Select an option"), FALSE, TRUE, 0); 59 | gtk_box_pack_start(GTK_BOX(box), comboBox, FALSE, TRUE, 0); 60 | gtk_box_pack_start(GTK_BOX(box), hintLabel, FALSE, TRUE, 0); 61 | gtk_widget_show_all(box); 62 | 63 | g_signal_connect(comboBox, "changed", G_CALLBACK(onComboBoxChanged), hintLabel); 64 | } 65 | 66 | void onComboBoxChanged(GtkComboBox *self, gpointer data) { 67 | GtkLabel *label = GTK_LABEL(data); 68 | const gchar *id = gtk_combo_box_get_active_id(GTK_COMBO_BOX(self)); 69 | const gchar *labelText = g_strconcat("Option id: ", id, NULL); 70 | gtk_label_set_label(label, labelText); 71 | } -------------------------------------------------------------------------------- /c/gtk3/combo-box.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onComboBoxChanged(GtkComboBox *self, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.ComboBox"; 8 | const gchar *appTitle = "GtkComboBox"; 9 | const char *items[8] = { 10 | "GNOME", 11 | "KDE Plasma", 12 | "XFCE", 13 | "MATE", 14 | "Cinnamon", 15 | "Pantheon", 16 | "LXDE", 17 | "LXQT" 18 | }; 19 | 20 | int main(int argc, char **argv) { 21 | GtkApplication *app = gtk_application_new(appID, 0); 22 | 23 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 24 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 25 | 26 | int result = g_application_run(G_APPLICATION(app), argc, argv); 27 | g_object_unref(app); 28 | 29 | return result; 30 | } 31 | 32 | void onAppActivate(GApplication *self, gpointer data) { 33 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 34 | gtk_window_present(window); 35 | } 36 | 37 | void onAppStartup(GApplication *self, gpointer data) { 38 | GtkWidget *window, *messageLabel, *hintLabel, *comboBox, *box; 39 | GtkCellRenderer *render; 40 | GtkListStore *model; 41 | GtkTreeIter iter; 42 | 43 | window = gtk_application_window_new(GTK_APPLICATION(self)); 44 | messageLabel = gtk_label_new("Select an option"); 45 | comboBox = gtk_combo_box_new(); 46 | hintLabel = gtk_label_new("Default option: 0"); 47 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 48 | model = gtk_list_store_new(1, G_TYPE_STRING); 49 | render = gtk_cell_renderer_text_new(); 50 | 51 | for (int i = 0; i < 8; i++) { 52 | gtk_list_store_append(model, &iter); 53 | gtk_list_store_set(model, &iter, 0, items[i], -1); 54 | } 55 | 56 | gtk_container_add(GTK_CONTAINER(window), box); 57 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 58 | 59 | gtk_combo_box_set_model(GTK_COMBO_BOX(comboBox), GTK_TREE_MODEL(model)); 60 | gtk_combo_box_set_active(GTK_COMBO_BOX(comboBox), 0); 61 | gtk_cell_layout_pack_start(GTK_CELL_LAYOUT(comboBox), render, TRUE); 62 | gtk_cell_layout_set_attributes(GTK_CELL_LAYOUT(comboBox), render, "text", 0, NULL); 63 | g_signal_connect(comboBox, "changed", G_CALLBACK(onComboBoxChanged), hintLabel); 64 | 65 | gtk_widget_set_halign(box, GTK_ALIGN_CENTER); 66 | gtk_widget_set_valign(box, GTK_ALIGN_CENTER); 67 | gtk_box_pack_start(GTK_BOX(box), messageLabel, FALSE, TRUE, 0); 68 | gtk_box_pack_start(GTK_BOX(box), comboBox, FALSE, TRUE, 0); 69 | gtk_box_pack_start(GTK_BOX(box), hintLabel, FALSE, TRUE, 0); 70 | gtk_widget_show_all(box); 71 | } 72 | 73 | void onComboBoxChanged(GtkComboBox *self, gpointer data) { 74 | int n = gtk_combo_box_get_active(GTK_COMBO_BOX(self)); 75 | char text[128]; 76 | sprintf(text, "Option %d selected (%s)", n, items[n]); 77 | g_object_set(data, "label", text, NULL); 78 | } -------------------------------------------------------------------------------- /c/gtk3/dialog-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Dialog1"; 7 | const gchar *appTitle = "GtkDialog"; 8 | const gchar *titleText = "Universe destruction"; 9 | const gchar *epilogText = "Do you accept?"; 10 | const gchar *summaryText = "Our universe has a lot of problems and the only way to fix\n" 11 | "it is destroying the entire universe and this important decision\nis now in your hands."; 12 | 13 | int main(int argc, char **argv) { 14 | GtkApplication *app = gtk_application_new(appID, 0); 15 | 16 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 17 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 18 | 19 | int result = g_application_run(G_APPLICATION(app), argc, argv); 20 | g_object_unref(app); 21 | 22 | return result; 23 | } 24 | 25 | void onAppActivate(GApplication *self, gpointer data) { 26 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 27 | gint result = gtk_dialog_run(GTK_DIALOG(window)); 28 | gtk_widget_destroy(GTK_WIDGET(window)); 29 | 30 | switch (result) { 31 | case GTK_RESPONSE_OK: { 32 | g_print("Universe destroyed! 💥\n"); 33 | break; 34 | } 35 | 36 | case GTK_RESPONSE_CANCEL: { 37 | g_print("Universe is in peace now! 🙏\n"); 38 | break; 39 | } 40 | 41 | default: { 42 | g_print("Nothing happens! 🤔\n"); 43 | break; 44 | } 45 | } 46 | } 47 | 48 | void onAppStartup(GApplication *self, gpointer data) { 49 | GtkWidget *dialog, *titleLabel, *summaryLabel, *epilogLabel, *contentBox; 50 | 51 | dialog = gtk_dialog_new(); 52 | titleLabel = gtk_label_new(titleText); 53 | summaryLabel = gtk_label_new(summaryText); 54 | epilogLabel = gtk_label_new(epilogText); 55 | contentBox = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); 56 | 57 | gtk_window_set_title(GTK_WINDOW(dialog), appTitle); 58 | gtk_window_set_application(GTK_WINDOW(dialog), GTK_APPLICATION(self)); 59 | gtk_container_set_border_width(GTK_CONTAINER(dialog), 10); 60 | gtk_dialog_add_button(GTK_DIALOG(dialog), "Yes 👍", GTK_RESPONSE_OK); 61 | gtk_dialog_add_button(GTK_DIALOG(dialog), "No 🛑", GTK_RESPONSE_CANCEL); 62 | 63 | gtk_label_set_use_markup(GTK_LABEL(titleLabel), TRUE); 64 | 65 | gtk_label_set_xalign(GTK_LABEL(summaryLabel), 0); 66 | gtk_label_set_line_wrap(GTK_LABEL(summaryLabel), TRUE); 67 | gtk_label_set_line_wrap_mode(GTK_LABEL(summaryLabel), PANGO_WRAP_CHAR); 68 | 69 | gtk_label_set_use_markup(GTK_LABEL(epilogLabel), TRUE); 70 | 71 | gtk_box_set_spacing(GTK_BOX(contentBox), 10); 72 | gtk_box_pack_start(GTK_BOX(contentBox), titleLabel, FALSE, TRUE, 0); 73 | gtk_box_pack_start(GTK_BOX(contentBox), summaryLabel, FALSE, TRUE, 0); 74 | gtk_box_pack_start(GTK_BOX(contentBox), epilogLabel, FALSE, TRUE, 10); 75 | gtk_widget_show_all(contentBox); 76 | } -------------------------------------------------------------------------------- /c/gtk3/dialog-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Dialog2"; 7 | const gchar *appTitle = "GtkDialog"; 8 | const gchar *title_text = "Universe destruction"; 9 | const gchar *epilog_text = "Do you accept?"; 10 | const gchar *summary_text = "Our universe has a lot of problems and the only way to fix\n" 11 | "it is destroying the entire universe and this important decision\nis now in your hands."; 12 | 13 | int main(int argc, char **argv) { 14 | GtkApplication *app = gtk_application_new(appID, 0); 15 | 16 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 17 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 18 | 19 | int result = g_application_run(G_APPLICATION(app), argc, argv); 20 | g_object_unref(app); 21 | 22 | return result; 23 | } 24 | 25 | void onAppActivate(GApplication *self, gpointer data) { 26 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 27 | gint result = gtk_dialog_run(GTK_DIALOG(window)); 28 | gtk_widget_destroy(GTK_WIDGET(window)); 29 | 30 | switch (result) { 31 | case GTK_RESPONSE_OK: { 32 | g_print("Universe destroyed! 💥\n"); 33 | break; 34 | } 35 | 36 | case GTK_RESPONSE_CANCEL: { 37 | g_print("Universe is in peace now! 🙏\n"); 38 | break; 39 | } 40 | 41 | default: { 42 | g_print("Nothing happens! 🤔\n"); 43 | break; 44 | } 45 | } 46 | } 47 | 48 | void onAppStartup(GApplication *self, gpointer data) { 49 | GtkWidget *dialog, *headerBar, *titleLabel, *summaryLabel, *epilogLabel, *contentBox; 50 | 51 | // "use-header-bar" is a Construct Only property 52 | dialog = g_object_new(GTK_TYPE_DIALOG, "use-header-bar", TRUE, NULL); 53 | headerBar = gtk_dialog_get_header_bar(GTK_DIALOG(dialog)); 54 | titleLabel = gtk_label_new(title_text); 55 | summaryLabel = gtk_label_new(summary_text); 56 | epilogLabel = gtk_label_new(epilog_text); 57 | contentBox = gtk_dialog_get_content_area(GTK_DIALOG(dialog)); 58 | 59 | gtk_dialog_add_button(GTK_DIALOG(dialog), "Yes 👍", GTK_RESPONSE_OK); 60 | gtk_dialog_add_button(GTK_DIALOG(dialog), "No 🛑", GTK_RESPONSE_CANCEL); 61 | gtk_container_set_border_width(GTK_CONTAINER(dialog), 10); 62 | gtk_window_set_application(GTK_WINDOW(dialog), GTK_APPLICATION(self)); 63 | 64 | gtk_header_bar_set_title(GTK_HEADER_BAR(headerBar), "GtkDialog"); 65 | gtk_header_bar_set_subtitle(GTK_HEADER_BAR(headerBar), "Example 2"); 66 | 67 | gtk_label_set_use_markup(GTK_LABEL(titleLabel), TRUE); 68 | 69 | gtk_label_set_xalign(GTK_LABEL(summaryLabel), 0); 70 | gtk_label_set_line_wrap(GTK_LABEL(summaryLabel), TRUE); 71 | gtk_label_set_line_wrap_mode(GTK_LABEL(summaryLabel), PANGO_WRAP_CHAR); 72 | 73 | gtk_label_set_use_markup(GTK_LABEL(epilogLabel), TRUE); 74 | 75 | gtk_box_set_spacing(GTK_BOX(contentBox), 10); 76 | gtk_box_pack_start(GTK_BOX(contentBox), titleLabel, FALSE, TRUE, 0); 77 | gtk_box_pack_start(GTK_BOX(contentBox), summaryLabel, FALSE, TRUE, 0); 78 | gtk_box_pack_start(GTK_BOX(contentBox), epilogLabel, FALSE, TRUE, 10); 79 | } 80 | -------------------------------------------------------------------------------- /c/gtk3/entry-completion.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.EntryCompletion"; 7 | const gchar *appTitle = "GtkEntryCompletion"; 8 | const char *items[6] = { "GNOME", "C", "GTK", "Example", "Hello, world!" }; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int res = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return res; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkListStore *model; 29 | GtkTreeIter iter; 30 | GtkWidget *window, *entry, *box, *hintLabel; 31 | GtkEntryCompletion *completion; 32 | 33 | model = gtk_list_store_new(1, G_TYPE_STRING); 34 | window = gtk_application_window_new(GTK_APPLICATION(self)); 35 | entry = gtk_entry_new(); 36 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 37 | hintLabel = gtk_label_new("Try typing \"gnome\" or \"hello\""); 38 | completion = gtk_entry_completion_new(); 39 | 40 | GtkWidget* widgets[3] = { entry, box, hintLabel }; 41 | 42 | for (int i = 0; i < 3; i++) 43 | gtk_widget_set_visible(widgets[i], TRUE); 44 | 45 | for (int i = 0; i < 6; i++) { 46 | gtk_list_store_append(model, &iter); 47 | gtk_list_store_set(model, &iter, 0, items[i], -1); 48 | } 49 | 50 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 51 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 52 | gtk_container_add(GTK_CONTAINER(window), box); 53 | 54 | gtk_entry_set_completion(GTK_ENTRY(entry), completion); 55 | 56 | gtk_widget_set_halign(box, GTK_ALIGN_CENTER); 57 | gtk_widget_set_valign(box, GTK_ALIGN_CENTER); 58 | gtk_box_pack_start(GTK_BOX(box), hintLabel, FALSE, TRUE, 0); 59 | gtk_box_pack_start(GTK_BOX(box), entry, FALSE, TRUE, 0); 60 | 61 | gtk_entry_completion_set_model(completion, GTK_TREE_MODEL(model)); 62 | gtk_entry_completion_set_text_column(completion, 0); 63 | } -------------------------------------------------------------------------------- /c/gtk3/entry.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void on_entry_changed(GtkWidget *self, GdkEvent *ev, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Entry"; 8 | const gchar *appTitle = "GtkEntry"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int res = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return res; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *box, *hintLabel, *entry, *outputLabel; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 32 | hintLabel = gtk_label_new("Enter some text"); 33 | entry = gtk_entry_new(); 34 | outputLabel = gtk_label_new(""); 35 | 36 | gtk_container_add(GTK_CONTAINER(window), box); 37 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 38 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 39 | 40 | gtk_widget_set_halign(box, GTK_ALIGN_CENTER); 41 | gtk_widget_set_valign(box, GTK_ALIGN_CENTER); 42 | 43 | g_signal_connect(entry, "key-release-event", G_CALLBACK(on_entry_changed), outputLabel); 44 | 45 | gtk_box_pack_start(GTK_BOX(box), hintLabel, FALSE, TRUE, 0); 46 | gtk_box_pack_start(GTK_BOX(box), entry, FALSE, TRUE, 0); 47 | gtk_box_pack_start(GTK_BOX(box), outputLabel, FALSE, TRUE, 0); 48 | 49 | gtk_label_set_line_wrap(GTK_LABEL(outputLabel), TRUE); 50 | gtk_label_set_line_wrap_mode(GTK_LABEL(outputLabel), PANGO_WRAP_CHAR); 51 | gtk_label_set_max_width_chars(GTK_LABEL(outputLabel), 18); 52 | } 53 | 54 | void on_entry_changed(GtkWidget *self, GdkEvent *ev, gpointer data) { 55 | char *text; 56 | g_object_get(self, "text", &text, NULL); 57 | g_object_set(data, "label", text, NULL); 58 | } 59 | -------------------------------------------------------------------------------- /c/gtk3/expander.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Expander"; 7 | const gchar *appTitle = "GtkExpander"; 8 | const char *loremIpsumText = "Duis in metus eros. Duis faucibus rutrum eros eu vestibulum.\n" 9 | "Proin et arcu nulla. Etiam at lacinia nibh. Vivamus pellentesque nunc nibh,\n" 10 | "ac dignissim massa lobortis ut. Integer eu felis in elit semper ullamcorper\n" 11 | "at in ipsum. Suspendisse tempus massa vel nibh tristique vestibulum.\n" 12 | "Vestibulum varius eu nunc eu interdum. Curabitur pulvinar velit in purus\n" 13 | "facilisis, et auctor augue consequat. Donec finibus felis ligula, a convallis\n" 14 | "justo tristique a."; 15 | 16 | int main(int argc, char **argv) { 17 | GtkApplication *app = gtk_application_new(appID, 0); 18 | 19 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 20 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 21 | 22 | int res = g_application_run(G_APPLICATION(app), argc, argv); 23 | g_object_unref(app); 24 | 25 | return res; 26 | } 27 | 28 | void onAppActivate(GApplication *self, gpointer data) { 29 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 30 | gtk_window_present(window); 31 | } 32 | 33 | void onAppStartup(GApplication *self, gpointer data) { 34 | GtkWidget *window, *expander, *expanderText; 35 | 36 | window = gtk_application_window_new(GTK_APPLICATION(self)); 37 | expander = gtk_expander_new(" Lorem ipsum "); 38 | expanderText = gtk_label_new(loremIpsumText); 39 | 40 | gtk_container_add(GTK_CONTAINER(window), expander); 41 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 42 | gtk_window_set_resizable(GTK_WINDOW(window), FALSE); 43 | gtk_container_set_border_width(GTK_CONTAINER(window), 10); 44 | 45 | gtk_widget_set_visible(expander, TRUE); 46 | gtk_container_add(GTK_CONTAINER(expander), expanderText); 47 | gtk_expander_set_use_markup(GTK_EXPANDER(expander), TRUE); 48 | gtk_expander_set_resize_toplevel(GTK_EXPANDER(expander), TRUE); 49 | 50 | gtk_widget_set_visible(expanderText, TRUE); 51 | gtk_label_set_line_wrap(GTK_LABEL(expanderText), TRUE); 52 | } -------------------------------------------------------------------------------- /c/gtk3/file-chooser-button.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onFileSet(GtkFileChooserButton *self, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.FileChooserButton"; 8 | const gchar *appTitle = "GtkFileChooserButton"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int res = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return res; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *box, *hintText, *button, *outputText; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 32 | hintText = gtk_label_new("Open a file"); 33 | button = gtk_file_chooser_button_new("", GTK_FILE_CHOOSER_ACTION_OPEN); 34 | outputText = gtk_label_new(""); 35 | 36 | gtk_container_add(GTK_CONTAINER(window), box); 37 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 38 | 39 | gtk_widget_set_halign(box, GTK_ALIGN_CENTER); 40 | gtk_widget_set_valign(box, GTK_ALIGN_CENTER); 41 | gtk_box_pack_start(GTK_BOX(box), hintText, FALSE, TRUE, 0); 42 | gtk_box_pack_start(GTK_BOX(box), button, FALSE, TRUE, 0); 43 | gtk_box_pack_start(GTK_BOX(box), outputText, FALSE, TRUE, 0); 44 | gtk_widget_show_all(box); 45 | 46 | gtk_widget_set_halign(button, GTK_ALIGN_CENTER); 47 | gtk_widget_set_valign(button, GTK_ALIGN_CENTER); 48 | g_signal_connect(button, "file-set", G_CALLBACK(onFileSet), outputText); 49 | 50 | gtk_label_set_line_wrap(GTK_LABEL(outputText), TRUE); 51 | } 52 | 53 | void onFileSet(GtkFileChooserButton *self, gpointer data) { 54 | gchar *filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(self)); 55 | g_object_set(data, "label", g_strconcat("Selected file: ", filename, NULL), NULL); 56 | } -------------------------------------------------------------------------------- /c/gtk3/file-chooser-dialog.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.FileChooserDialog"; 7 | const gchar *appTitle = "GtkFileChooserDialog"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | int result = gtk_dialog_run(GTK_DIALOG(window)); 24 | 25 | switch (result) { 26 | case GTK_RESPONSE_OK: { 27 | gchar *name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(window)); 28 | g_print("You selected: %s\n", name); 29 | break; 30 | } 31 | 32 | case GTK_RESPONSE_CANCEL: { 33 | g_print("Canceled\n"); 34 | break; 35 | } 36 | 37 | default: { 38 | g_print("Something else\n"); 39 | break; 40 | } 41 | } 42 | 43 | gtk_widget_destroy(GTK_WIDGET(window)); 44 | } 45 | 46 | void onAppStartup(GApplication *self, gpointer data) { 47 | GtkWidget *window = gtk_file_chooser_dialog_new(appTitle, NULL, GTK_FILE_CHOOSER_ACTION_OPEN, NULL, NULL); 48 | gtk_dialog_add_button(GTK_DIALOG(window), "Open", GTK_RESPONSE_OK); 49 | gtk_dialog_add_button(GTK_DIALOG(window), "Cancel", GTK_RESPONSE_CANCEL); 50 | gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window)); 51 | } -------------------------------------------------------------------------------- /c/gtk3/fixed.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Fixed"; 7 | const gchar *appTitle = "GtkFixed"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int res = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return res; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *fixed; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | fixed = gtk_fixed_new(); 31 | 32 | gtk_container_add(GTK_CONTAINER(window), fixed); 33 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 34 | 35 | gtk_fixed_put(GTK_FIXED(fixed), gtk_label_new("A"), 10, 20); 36 | gtk_fixed_put(GTK_FIXED(fixed), gtk_label_new("B"), 100, 200); 37 | gtk_fixed_put(GTK_FIXED(fixed), gtk_label_new("C"), 99, 326); 38 | gtk_widget_show_all(fixed); 39 | } -------------------------------------------------------------------------------- /c/gtk3/flow-box.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.FlowBox"; 7 | const gchar *appTitle = "GtkFlowBox"; 8 | const gchar *icons[20] = { 9 | "face-angel", 10 | "face-angry", 11 | "face-surprise", 12 | "face-laugh", 13 | "face-plain", 14 | "face-sad", 15 | "face-cool", 16 | "face-smirk", 17 | "face-sick", 18 | "face-kiss", 19 | "face-heart-broken", 20 | "face-smile", 21 | "face-crying", 22 | "face-devilish", 23 | "face-heart", 24 | "face-sad", 25 | "face-smile-big", 26 | "face-tired", 27 | "face-wink", 28 | "face-worried" 29 | }; 30 | 31 | int main(int argc, char **argv) { 32 | GtkApplication *app = gtk_application_new(appID, 0); 33 | 34 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 35 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 36 | 37 | int res = g_application_run(G_APPLICATION(app), argc, argv); 38 | g_object_unref(app); 39 | 40 | return res; 41 | } 42 | 43 | void onAppActivate(GApplication *self, gpointer data) { 44 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 45 | gtk_window_present(window); 46 | } 47 | 48 | void onAppStartup(GApplication *self, gpointer data) { 49 | GtkWidget *window, *flowBox, *scrolledWindow; 50 | 51 | window = gtk_application_window_new(GTK_APPLICATION(self)); 52 | flowBox = gtk_flow_box_new(); 53 | scrolledWindow = gtk_scrolled_window_new(NULL, NULL); 54 | 55 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 56 | gtk_container_add(GTK_CONTAINER(window), scrolledWindow); 57 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 58 | 59 | gtk_flow_box_set_selection_mode(GTK_FLOW_BOX(flowBox), GTK_SELECTION_NONE); 60 | gtk_flow_box_set_max_children_per_line(GTK_FLOW_BOX(flowBox), 30); 61 | 62 | srand(time(NULL)); 63 | 64 | for (int i = 0; i < 1000; i++) 65 | gtk_flow_box_insert( 66 | GTK_FLOW_BOX(flowBox), 67 | gtk_image_new_from_icon_name(icons[(rand() % 20)], GTK_ICON_SIZE_DIALOG), 68 | i 69 | ); 70 | 71 | gtk_container_add(GTK_CONTAINER(scrolledWindow), flowBox); 72 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledWindow), GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC); 73 | gtk_widget_show_all(scrolledWindow); 74 | } -------------------------------------------------------------------------------- /c/gtk3/grid-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Grid1"; 7 | const gchar *appTitle = "GtkGrid"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *grid; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | grid = gtk_grid_new(); 31 | 32 | gtk_container_add(GTK_CONTAINER(window), grid); 33 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | 36 | for (int i = 0; i < 3; ++i) { 37 | for (int j = 0; j < 3; ++j) { 38 | char str[20]; 39 | sprintf(str, "Top: %d. Left: %d", i, j); 40 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new(str), j, i, 1, 1); 41 | } 42 | } 43 | 44 | gtk_widget_set_halign(grid, GTK_ALIGN_CENTER); 45 | gtk_widget_set_valign(grid, GTK_ALIGN_CENTER); 46 | gtk_grid_set_row_spacing(GTK_GRID(grid), 10); 47 | gtk_grid_set_column_spacing(GTK_GRID(grid), 10); 48 | gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE); 49 | gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); 50 | gtk_widget_show_all(grid); 51 | } -------------------------------------------------------------------------------- /c/gtk3/grid-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Grid2"; 7 | const gchar *appTitle = "GtkGrid"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *grid; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | grid = gtk_grid_new(); 31 | 32 | gtk_container_add(GTK_CONTAINER(window), grid); 33 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | 36 | gtk_widget_set_halign(grid, GTK_ALIGN_CENTER); 37 | gtk_widget_set_valign(grid, GTK_ALIGN_CENTER); 38 | gtk_grid_set_row_spacing(GTK_GRID(grid), 10); 39 | gtk_grid_set_column_spacing(GTK_GRID(grid), 10); 40 | gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE); 41 | gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); 42 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 0. Left: 0"), 0, 0, 1, 1); 43 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 0. Left: 1"), 1, 0, 1, 1); 44 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 0. Left: 2"), 2, 0, 1, 1); 45 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 1. Left: 0"), 0, 1, 1, 1); 46 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 1. Left: 1"), 1, 1, 1, 1); 47 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 1. Left: 2"), 2, 1, 1, 1); 48 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 2. Left: 0"), 0, 2, 1, 1); 49 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 2. Left: 1"), 1, 2, 1, 1); 50 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 2. Left: 2"), 2, 2, 1, 1); 51 | gtk_widget_show_all(grid); 52 | } -------------------------------------------------------------------------------- /c/gtk3/grid-3.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Grid3"; 7 | const gchar *appTitle = "GtkGrid"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *grid; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | grid = gtk_grid_new(); 31 | 32 | gtk_container_add(GTK_CONTAINER(window), grid); 33 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | 36 | gtk_widget_set_halign(grid, GTK_ALIGN_CENTER); 37 | gtk_widget_set_valign(grid, GTK_ALIGN_CENTER); 38 | gtk_grid_set_row_spacing(GTK_GRID(grid), 10); 39 | gtk_grid_set_column_spacing(GTK_GRID(grid), 10); 40 | gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE); 41 | gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); 42 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Columns"), 1, 0, 2, 1); 43 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Rows"), 0, 0, 1, 3); 44 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 1. Left: 1"), 1, 1, 1, 1); 45 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 1. Left: 2"), 2, 1, 1, 1); 46 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 2. Left: 1"), 1, 2, 1, 1); 47 | gtk_grid_attach(GTK_GRID(grid), gtk_label_new("Top: 2. Left: 2"), 2, 2, 1, 1); 48 | gtk_widget_show_all(grid); 49 | } -------------------------------------------------------------------------------- /c/gtk3/header-bar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.HeaderBar"; 7 | const gchar *appTitle = "GtkHeaderBar"; 8 | const gchar *appSubtitle = "App subtitle"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int result = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return result; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *header; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | header = gtk_header_bar_new(); 32 | 33 | gtk_window_set_titlebar(GTK_WINDOW(window), header); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | 36 | gtk_widget_set_visible(header, TRUE); 37 | gtk_header_bar_set_title(GTK_HEADER_BAR(header), appTitle); 38 | gtk_header_bar_set_subtitle(GTK_HEADER_BAR(header), appSubtitle); 39 | gtk_header_bar_set_show_close_button(GTK_HEADER_BAR(header), TRUE); 40 | } -------------------------------------------------------------------------------- /c/gtk3/image-1.c.in: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Image1"; 7 | const gchar *appTitle = "GtkImage"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *image; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | image = gtk_image_new_from_file("@imagePath@"); 31 | 32 | gtk_container_add(GTK_CONTAINER(window), image); 33 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | 36 | gtk_widget_set_visible(image, TRUE); 37 | } -------------------------------------------------------------------------------- /c/gtk3/image-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Image2"; 7 | const gchar *appTitle = "GtkImage"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int res = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return res; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *image; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | image = gtk_image_new_from_icon_name("computer", GTK_ICON_SIZE_INVALID); 31 | 32 | gtk_container_add(GTK_CONTAINER(window), image); 33 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | 36 | gtk_widget_set_visible(image, TRUE); 37 | gtk_image_set_pixel_size(GTK_IMAGE(image), 256); 38 | } -------------------------------------------------------------------------------- /c/gtk3/info-bar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onInfoBarResponse(GtkInfoBar *self, int responseID, gpointer data); 6 | void createInfoBar(GtkButton *self, gpointer data); 7 | GtkMessageType stringToGtkMessageType(const gchar *label); 8 | 9 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.InfoBar"; 10 | const gchar *appTitle = "GtkInfoBar"; 11 | 12 | int main(int argc, char **argv) { 13 | GtkApplication *app = gtk_application_new(appID, 0); 14 | 15 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 16 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 17 | 18 | int result = g_application_run(G_APPLICATION(app), argc, argv); 19 | g_object_unref(app); 20 | 21 | return result; 22 | } 23 | 24 | void onAppActivate(GApplication *self, gpointer data) { 25 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 26 | gtk_window_present(window); 27 | } 28 | 29 | void onAppStartup(GApplication *self, gpointer data) { 30 | GtkWidget *window, *box, *grid, *infoButton, *warningButton, *questionButton, *errorButton, *otherButton; 31 | 32 | window = gtk_application_window_new(GTK_APPLICATION(self)); 33 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); 34 | grid = gtk_grid_new(); 35 | infoButton = gtk_button_new_with_label("Info"); 36 | warningButton = gtk_button_new_with_label("Warning"); 37 | questionButton = gtk_button_new_with_label("Question"); 38 | errorButton = gtk_button_new_with_label("Error"); 39 | otherButton = gtk_button_new_with_label("Other"); 40 | 41 | gtk_container_add(GTK_CONTAINER(window), box); 42 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 43 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 44 | 45 | gtk_box_pack_end(GTK_BOX(box), grid, TRUE, TRUE, 0); 46 | gtk_widget_show_all(box); 47 | 48 | gtk_widget_set_halign(grid, GTK_ALIGN_CENTER); 49 | gtk_widget_set_valign(grid, GTK_ALIGN_CENTER); 50 | gtk_grid_set_row_spacing(GTK_GRID(grid), 10); 51 | gtk_grid_set_column_spacing(GTK_GRID(grid), 10); 52 | gtk_grid_set_row_homogeneous(GTK_GRID(grid), TRUE); 53 | gtk_grid_set_column_homogeneous(GTK_GRID(grid), TRUE); 54 | gtk_container_set_border_width(GTK_CONTAINER(grid), 10); 55 | gtk_grid_attach(GTK_GRID(grid), infoButton, 0, 0, 1, 1); 56 | gtk_grid_attach(GTK_GRID(grid), warningButton, 1, 0, 1, 1); 57 | gtk_grid_attach(GTK_GRID(grid), questionButton, 0, 1, 1, 1); 58 | gtk_grid_attach(GTK_GRID(grid), errorButton, 1, 1, 1, 1); 59 | gtk_grid_attach(GTK_GRID(grid), otherButton, 0, 2, 2, 1); 60 | gtk_widget_show_all(grid); 61 | 62 | g_signal_connect(infoButton, "clicked", G_CALLBACK(createInfoBar), box); 63 | g_signal_connect(warningButton, "clicked", G_CALLBACK(createInfoBar), box); 64 | g_signal_connect(questionButton, "clicked", G_CALLBACK(createInfoBar), box); 65 | g_signal_connect(errorButton, "clicked", G_CALLBACK(createInfoBar), box); 66 | g_signal_connect(otherButton, "clicked", G_CALLBACK(createInfoBar), box); 67 | } 68 | 69 | void createInfoBar(GtkButton *self, gpointer data) { 70 | const gchar *buttonText = gtk_button_get_label(self); 71 | GtkWidget *infoBar, *infoBarContentBox; 72 | 73 | infoBar = gtk_info_bar_new(); 74 | infoBarContentBox = gtk_info_bar_get_content_area(GTK_INFO_BAR(infoBar)); 75 | 76 | gtk_info_bar_set_show_close_button(GTK_INFO_BAR(infoBar), TRUE); 77 | gtk_info_bar_set_message_type(GTK_INFO_BAR(infoBar), stringToGtkMessageType(buttonText)); 78 | g_signal_connect(infoBar, "response", G_CALLBACK(onInfoBarResponse), data); 79 | 80 | gtk_box_pack_start(GTK_BOX(infoBarContentBox), gtk_label_new(buttonText), FALSE, TRUE, 0); 81 | 82 | gtk_box_pack_start(GTK_BOX(data), infoBar, FALSE, TRUE, 0); 83 | gtk_widget_show_all(GTK_WIDGET(data)); 84 | } 85 | 86 | void onInfoBarResponse(GtkInfoBar *self, int responseID, gpointer data) { 87 | gtk_container_remove(GTK_CONTAINER(data), GTK_WIDGET(self)); 88 | gtk_widget_destroy(GTK_WIDGET(self)); 89 | } 90 | 91 | GtkMessageType stringToGtkMessageType(const gchar *label) { 92 | GtkMessageType result; 93 | gchar *string = g_ascii_strup(label, strlen(label)); 94 | 95 | if (g_str_equal("INFO", string)) 96 | result = GTK_MESSAGE_INFO; 97 | else if (g_str_equal("WARNING", string)) 98 | result = GTK_MESSAGE_WARNING; 99 | else if (g_str_equal("QUESTION", string)) 100 | result = GTK_MESSAGE_QUESTION; 101 | else if (g_str_equal("ERROR", string)) 102 | result = GTK_MESSAGE_ERROR; 103 | else if (g_str_equal("OTHER", string)) 104 | result = GTK_MESSAGE_OTHER; 105 | 106 | return result; 107 | } -------------------------------------------------------------------------------- /c/gtk3/label.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Label"; 7 | const gchar *appTitle = "GtkLabel"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window, *label; 28 | 29 | window = gtk_application_window_new(GTK_APPLICATION(self)); 30 | label = gtk_label_new("Hello, world!"); 31 | 32 | gtk_container_add(GTK_CONTAINER(window), label); 33 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 34 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 35 | 36 | gtk_widget_set_visible(label, TRUE); 37 | } -------------------------------------------------------------------------------- /c/gtk3/list-box.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onButtonClicked(GtkButton *self, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.ListBox"; 8 | const gchar *appTitle = "GtkListBox"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int result = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return result; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *box, *listBox, *scrolledWindow, *button; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 32 | listBox = gtk_list_box_new(); 33 | scrolledWindow = gtk_scrolled_window_new(NULL, NULL); 34 | button = gtk_button_new_with_label("Load"); 35 | 36 | gtk_container_add(GTK_CONTAINER(window), box); 37 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 38 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 39 | 40 | gtk_box_pack_start(GTK_BOX(box), scrolledWindow, TRUE, TRUE, 0); 41 | gtk_box_pack_start(GTK_BOX(box), button, FALSE, FALSE, 0); 42 | gtk_widget_show_all(box); 43 | 44 | gtk_container_add(GTK_CONTAINER(scrolledWindow), listBox); 45 | gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledWindow), GTK_SHADOW_NONE); 46 | gtk_scrolled_window_set_propagate_natural_width(GTK_SCROLLED_WINDOW(scrolledWindow), TRUE); 47 | gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolledWindow), TRUE); 48 | gtk_widget_show_all(scrolledWindow); 49 | 50 | gtk_widget_set_halign(button, GTK_ALIGN_CENTER); 51 | gtk_widget_set_valign(button, GTK_ALIGN_CENTER); 52 | gtk_widget_set_margin_start(button, 10); 53 | gtk_widget_set_margin_end(button, 10); 54 | gtk_widget_set_margin_bottom(button, 10); 55 | g_signal_connect(button, "clicked", G_CALLBACK(onButtonClicked), listBox); 56 | } 57 | 58 | void onButtonClicked(GtkButton *self, gpointer data) { 59 | for (int i = 0; i < 100; i++) { 60 | char str[16]; 61 | sprintf(str, "Some text %d", (i + 1)); 62 | GtkWidget *tempLabel = gtk_label_new(str); 63 | gtk_widget_set_margin_start(tempLabel, 10); 64 | gtk_widget_set_margin_end(tempLabel, 10); 65 | gtk_widget_set_margin_top(tempLabel, 10); 66 | gtk_widget_set_margin_bottom(tempLabel, 10); 67 | gtk_list_box_insert(GTK_LIST_BOX(data), tempLabel, i); 68 | } 69 | 70 | gtk_widget_show_all(GTK_WIDGET(data)); 71 | } -------------------------------------------------------------------------------- /c/gtk3/meson.build: -------------------------------------------------------------------------------- 1 | src = [ 2 | 'action-bar', 3 | 'application', 4 | 'application-window', 5 | 'assistant', 6 | 'box', 7 | 'builder', 8 | 'button', 9 | 'combo-box', 10 | 'combo-box-text', 11 | 'dialog-1', 12 | 'dialog-2', 13 | 'entry', 14 | 'entry-completion', 15 | 'expander', 16 | 'file-chooser-button', 17 | 'file-chooser-dialog', 18 | 'fixed', 19 | 'flow-box', 20 | 'grid-1', 21 | 'grid-2', 22 | 'grid-3', 23 | 'header-bar', 24 | 'image-1', 25 | 'image-2', 26 | 'info-bar', 27 | 'label', 28 | 'list-box', 29 | 'message-dialog', 30 | 'stack-1', 31 | 'stack-2', 32 | 'text-view', 33 | 'tree-view', 34 | 'window' 35 | ] 36 | 37 | builder = configure_file( 38 | input: 'builder.c.in', 39 | output: 'builder.c', 40 | configuration: builder_gtk3_ui 41 | ) 42 | 43 | image1 = configure_file( 44 | input: 'image-1.c.in', 45 | output: 'image-1.c', 46 | configuration: image_conf 47 | ) 48 | 49 | if build_machine.system() == 'linux' 50 | executable('glarea', 'glarea.c', dependencies: [ gtk3, glew ]) 51 | endif 52 | 53 | foreach name : src 54 | if name == 'builder' 55 | executable(name, builder, dependencies: gtk3) 56 | elif name == 'image-1' 57 | executable(name, image1, dependencies: gtk3) 58 | else 59 | executable(name, name + '.c', dependencies: gtk3) 60 | endif 61 | endforeach -------------------------------------------------------------------------------- /c/gtk3/message-dialog.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.MessageDialog"; 7 | const gchar *appTitle = "GtkMessageDialog"; 8 | const gchar *titleText = "Universe destruction"; 9 | const gchar *summaryText = "Our universe has a lot of problems and the only way to fix\n" 10 | "it is destroying the entire universe and this important decision\nis now in your hands."; 11 | 12 | int main(int argc, char **argv) { 13 | GtkApplication *app = gtk_application_new(appID, 0); 14 | 15 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 16 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 17 | 18 | int result = g_application_run(G_APPLICATION(app), argc, argv); 19 | g_object_unref(app); 20 | 21 | return result; 22 | } 23 | 24 | void onAppActivate(GApplication *self, gpointer data) { 25 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 26 | gint result = gtk_dialog_run(GTK_DIALOG(window)); 27 | gtk_widget_destroy(GTK_WIDGET(window)); 28 | 29 | switch (result) { 30 | case GTK_RESPONSE_OK: { 31 | g_print("Universe destroyed! 💥\n"); 32 | break; 33 | } 34 | 35 | case GTK_RESPONSE_CANCEL: { 36 | g_print("Universe is in peace now! 🙏\n"); 37 | break; 38 | } 39 | 40 | default: { 41 | g_print("Nothing happens! 🤔\n"); 42 | break; 43 | } 44 | } 45 | } 46 | 47 | void onAppStartup(GApplication *self, gpointer data) { 48 | GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, NULL); 49 | 50 | gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(dialog)); 51 | 52 | g_object_set(dialog, "text", titleText, NULL); 53 | g_object_set(dialog, "use-markup", TRUE, NULL); 54 | g_object_set(dialog, "secondary-text", summaryText, NULL); 55 | gtk_window_set_title(GTK_WINDOW(dialog), appTitle); 56 | gtk_dialog_add_button(GTK_DIALOG(dialog), "Yes 👍", GTK_RESPONSE_OK); 57 | gtk_dialog_add_button(GTK_DIALOG(dialog), "No 🛑", GTK_RESPONSE_CANCEL); 58 | } 59 | -------------------------------------------------------------------------------- /c/gtk3/stack-1.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | GtkWidget* doStack(); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Stack1"; 8 | const gchar *appTitle = "GtkStack"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int res = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return res; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *win = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(win); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *win, *stack, *switcher, *box; 29 | 30 | win = gtk_application_window_new(GTK_APPLICATION(self)); 31 | stack = doStack(); 32 | switcher = gtk_stack_switcher_new(); 33 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 34 | 35 | gtk_container_add(GTK_CONTAINER(win), box); 36 | gtk_window_set_title(GTK_WINDOW(win), appTitle); 37 | gtk_window_set_default_size(GTK_WINDOW(win), 400, 400); 38 | gtk_container_set_border_width(GTK_CONTAINER(win), 10); 39 | 40 | gtk_widget_set_halign(switcher, GTK_ALIGN_CENTER); 41 | gtk_stack_switcher_set_stack(GTK_STACK_SWITCHER(switcher), GTK_STACK(stack)); 42 | 43 | gtk_box_set_homogeneous(GTK_BOX(box), FALSE); 44 | gtk_box_pack_start(GTK_BOX(box), stack, TRUE, TRUE, 0); 45 | gtk_box_pack_start(GTK_BOX(box), switcher, FALSE, TRUE, 0); 46 | gtk_widget_show_all(box); 47 | } 48 | 49 | GtkWidget* doStack() { 50 | GtkWidget *stack = gtk_stack_new(); 51 | gtk_stack_set_transition_type(GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT); 52 | 53 | for (int i = 0; i < 3; i++) { 54 | char label[56], title[7], name[6]; 55 | 56 | sprintf(label, "Page %d", i + 1); 57 | sprintf(title, "Page %d", i + 1); 58 | sprintf(name, "page%d", i + 1); 59 | 60 | GtkWidget *widget = gtk_label_new(label); 61 | gtk_label_set_use_markup(GTK_LABEL(widget), TRUE); 62 | 63 | gtk_stack_add_titled(GTK_STACK(stack), widget, name, title); 64 | } 65 | 66 | return stack; 67 | } -------------------------------------------------------------------------------- /c/gtk3/stack-2.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | GtkWidget* doStack(); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.Stack2"; 8 | const gchar *appTitle = "GtkStack"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int res = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return res; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *win = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(win); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *win, *stack, *sidebar, *box; 29 | 30 | win = gtk_application_window_new(GTK_APPLICATION(self)); 31 | stack = doStack(); 32 | sidebar = gtk_stack_sidebar_new(); 33 | box = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 10); 34 | 35 | gtk_container_add(GTK_CONTAINER(win), box); 36 | gtk_window_set_title(GTK_WINDOW(win), appTitle); 37 | gtk_window_set_default_size(GTK_WINDOW(win), 400, 400); 38 | 39 | gtk_stack_sidebar_set_stack(GTK_STACK_SIDEBAR(sidebar), GTK_STACK(stack)); 40 | 41 | gtk_box_set_homogeneous(GTK_BOX(box), FALSE); 42 | gtk_box_pack_start(GTK_BOX(box), sidebar, FALSE, TRUE, 0); 43 | gtk_box_pack_start(GTK_BOX(box), stack, TRUE, TRUE, 0); 44 | gtk_widget_show_all(box); 45 | } 46 | 47 | GtkWidget* doStack() { 48 | GtkWidget *stack = gtk_stack_new(); 49 | gtk_stack_set_transition_type(GTK_STACK(stack), GTK_STACK_TRANSITION_TYPE_SLIDE_UP_DOWN); 50 | 51 | for (int i = 0; i < 3; i++) { 52 | char label[56], title[7], name[6]; 53 | 54 | sprintf(label, "Page %d", i + 1); 55 | sprintf(title, "Page %d", i + 1); 56 | sprintf(name, "page%d", i + 1); 57 | 58 | GtkWidget *widget = gtk_label_new(label); 59 | gtk_label_set_use_markup(GTK_LABEL(widget), TRUE); 60 | 61 | gtk_stack_add_titled(GTK_STACK(stack), widget, name, title); 62 | } 63 | 64 | return stack; 65 | } -------------------------------------------------------------------------------- /c/gtk3/text-view.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk3.TextView"; 7 | const gchar *appTitle = "GtkTextView"; 8 | const gchar *loremIpsum = 9 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor" 10 | "incididunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad" 11 | "sapientiam perveniri potest, non paranda nobis solum ea, sed fruenda etiam" 12 | "sapientia est; sive hoc difficile est, tamen nec modus est ullus investigandi" 13 | "veri, nisi inveneris, et quaerendi defatigatio turpis est, cum id, quod maxime" 14 | "placeat, facere possimus, omnis voluptas assumenda est, omnis." 15 | "\n\n" 16 | "Ut omittam pericula, labores, dolorem etiam, quem optimus quisque pro patria et" 17 | "pro suis suscipit, ut non plus voluptatum habeat quam dolorum. Nam et ipsa" 18 | "declinatio ad libidinem fingitur -- ait enim declinare atomum sine causa; quo." 19 | "\n\n" 20 | "Credo quibusdam usu venire; ut abhorreant a Latinis, quod inciderint in inculta" 21 | "quaedam et quasi architecto beatae vitae dicta sunt, explicabo. Nemo enim ipsam" 22 | "voluptatem, quia voluptas sit, aspernatur aut odit aut fugit, sed quia maiores" 23 | "consequatur. Eadem fortitudinis ratio reperietur. Nam neque laborum perfunctio" 24 | "neque.\0"; 25 | 26 | int main(int argc, char **argv) { 27 | GtkApplication *app = gtk_application_new(appID, 0); 28 | 29 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 30 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 31 | 32 | int res = g_application_run(G_APPLICATION(app), argc, argv); 33 | g_object_unref(app); 34 | 35 | return res; 36 | } 37 | 38 | void onAppActivate(GApplication *self, gpointer data) { 39 | GtkWindow *win = gtk_application_get_active_window(GTK_APPLICATION(self)); 40 | gtk_window_present(win); 41 | } 42 | 43 | void onAppStartup(GApplication *self, gpointer data) { 44 | GtkWidget *window, *textView; 45 | 46 | window = gtk_application_window_new(GTK_APPLICATION(self)); 47 | textView = gtk_text_view_new(); 48 | 49 | gtk_container_add(GTK_CONTAINER(window), textView); 50 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 51 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 52 | 53 | gtk_text_view_set_editable(GTK_TEXT_VIEW(textView), FALSE); 54 | gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(textView), GTK_WRAP_WORD); 55 | 56 | GtkTextBuffer *textBuffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textView)); 57 | gtk_text_buffer_set_text(GTK_TEXT_BUFFER(textBuffer), loremIpsum, -1); 58 | 59 | gtk_widget_show(textView); 60 | } -------------------------------------------------------------------------------- /c/gtk3/tree-view.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | typedef struct _TreeItem TreeItem; 4 | 5 | struct _TreeItem { 6 | gboolean enabled; 7 | const gchar *label; 8 | TreeItem *child; 9 | }; 10 | 11 | enum { 12 | ENABLED_COLUMN = 0, 13 | LABEL_COLUMN, 14 | NUM_COLUMNS 15 | }; 16 | 17 | TreeItem row1[] = { 18 | { TRUE, "Sublime Text", NULL }, 19 | { TRUE, "VS Code", NULL }, 20 | { TRUE, "Atom", NULL } 21 | }; 22 | 23 | TreeItem row2[] = { 24 | { TRUE, "Spotify", NULL }, 25 | { TRUE, "Deezer", NULL }, 26 | { TRUE, "SoundCloud", NULL } 27 | }; 28 | 29 | TreeItem row3[] = { 30 | { TRUE, "Edge", NULL }, 31 | { TRUE, "Chrome", NULL }, 32 | { TRUE, "Firefox", NULL } 33 | }; 34 | 35 | TreeItem rows[] = { 36 | { TRUE, "Code editors", row1 }, 37 | { TRUE, "Music services", row2 }, 38 | { TRUE, "Web browsers", row3 } 39 | }; 40 | 41 | void app_activate(GApplication *self, gpointer data); 42 | void app_startup(GApplication *self, gpointer data); 43 | GtkWidget* build_tree_view(); 44 | GtkTreeModel* create_model(); 45 | void add_columns(GtkTreeView *treeview); 46 | void cell_toggled(GtkCellRendererToggle *cell, char *path, gpointer data); 47 | 48 | int main(int argc, char **argv) { 49 | const gchar *app_id = "io.github.Miqueas.GTK-Examples.C.Gtk3.TreeView"; 50 | GtkApplication *app = gtk_application_new(app_id, 0); 51 | 52 | g_signal_connect(app, "startup", G_CALLBACK(app_startup), NULL); 53 | g_signal_connect(app, "activate", G_CALLBACK(app_activate), NULL); 54 | 55 | int res = g_application_run(G_APPLICATION(app), argc, argv); 56 | g_object_unref(app); 57 | 58 | return res; 59 | } 60 | 61 | void app_activate(GApplication *self, gpointer data) { 62 | GtkWindow *win = gtk_application_get_active_window(GTK_APPLICATION(self)); 63 | gtk_window_present(win); 64 | } 65 | 66 | void app_startup(GApplication *self, gpointer data) { 67 | GtkWidget *win = g_object_new( 68 | GTK_TYPE_APPLICATION_WINDOW, 69 | "application", GTK_APPLICATION(self), 70 | "default-width", 400, 71 | "default-height", 400, 72 | NULL 73 | ); 74 | 75 | GtkWidget *header = g_object_new( 76 | GTK_TYPE_HEADER_BAR, 77 | "visible", TRUE, 78 | "show-close-button", TRUE, 79 | "title", "GtkGrid", 80 | NULL 81 | ); 82 | 83 | gtk_container_add(GTK_CONTAINER(win), build_tree_view()); 84 | gtk_window_set_titlebar(GTK_WINDOW(win), header); 85 | } 86 | 87 | GtkWidget* build_tree_view() { 88 | GtkWidget *view = g_object_new(GTK_TYPE_TREE_VIEW, "visible", TRUE, NULL); 89 | 90 | GtkTreeModel *model = create_model(); 91 | gtk_tree_view_set_model(GTK_TREE_VIEW(view), model); 92 | 93 | add_columns(GTK_TREE_VIEW(view)); 94 | g_object_unref(model); 95 | g_signal_connect(view, "realize", G_CALLBACK(gtk_tree_view_expand_all), NULL); 96 | 97 | return view; 98 | } 99 | 100 | GtkTreeModel* create_model() { 101 | TreeItem *data = rows; 102 | GtkTreeIter iter; 103 | GtkTreeStore *model = gtk_tree_store_new(NUM_COLUMNS, G_TYPE_BOOLEAN, G_TYPE_STRING); 104 | 105 | while (data->enabled) { 106 | TreeItem *child = data->child; 107 | 108 | gtk_tree_store_append(model, &iter, NULL); 109 | gtk_tree_store_set(model, &iter, 110 | ENABLED_COLUMN, data->enabled, 111 | LABEL_COLUMN, data->label, 112 | -1 113 | ); 114 | 115 | while (child->enabled) { 116 | GtkTreeIter child_iter; 117 | 118 | gtk_tree_store_append(model, &child_iter, &iter); 119 | gtk_tree_store_set(model, &child_iter, 120 | ENABLED_COLUMN, child->enabled, 121 | LABEL_COLUMN, child->label, 122 | -1 123 | ); 124 | 125 | child++; 126 | } 127 | 128 | data++; 129 | } 130 | 131 | return GTK_TREE_MODEL(model); 132 | } 133 | 134 | void add_columns(GtkTreeView *view) { 135 | GtkCellRenderer *cell; 136 | 137 | cell = gtk_cell_renderer_toggle_new(); 138 | gtk_tree_view_insert_column_with_attributes(view, -1, "Enabled", cell, "active", ENABLED_COLUMN, NULL); 139 | g_signal_connect(cell, "toggled", G_CALLBACK(cell_toggled), view); 140 | 141 | cell = gtk_cell_renderer_text_new(); 142 | gtk_tree_view_insert_column_with_attributes(view, -1, "App", cell, "text", LABEL_COLUMN, NULL); 143 | } 144 | 145 | void cell_toggled(GtkCellRendererToggle *self, char *path_str, gpointer data) { 146 | GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(data)); 147 | GtkTreePath *path = gtk_tree_path_new_from_string((const gchar *) path_str); 148 | 149 | GtkTreeIter iter; 150 | gboolean active; 151 | gint *col_pos; 152 | 153 | col_pos = g_object_get_data (G_OBJECT (self), "column"); 154 | 155 | gtk_tree_model_get_iter(model, &iter, path); 156 | gtk_tree_model_get(model, &iter, col_pos, &active, -1); 157 | 158 | active = (active) ? FALSE : TRUE; 159 | gtk_tree_store_set(GTK_TREE_STORE(model), &iter, col_pos, active, -1); 160 | gtk_tree_path_free (path); 161 | } -------------------------------------------------------------------------------- /c/gtk3/window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | const gchar *appTitle = "GtkWindow"; 4 | 5 | int main(int argc, char **argv) { 6 | gtk_init(&argc, &argv); 7 | 8 | GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); 9 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 10 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 11 | g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); 12 | gtk_window_present(GTK_WINDOW(window)); 13 | gtk_main(); 14 | 15 | return 0; 16 | } -------------------------------------------------------------------------------- /c/gtk4/application-window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppStartup(GApplication *self, gpointer data); 4 | void onAppActivate(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk4.ApplicationWindow"; 7 | const gchar *appTitle = "GtkApplicationWindow"; 8 | 9 | int main(int argc, char **argv) { 10 | GtkApplication *app = gtk_application_new(appID, 0); 11 | 12 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 13 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 14 | 15 | int result = g_application_run(G_APPLICATION(app), argc, argv); 16 | g_object_unref(app); 17 | 18 | return result; 19 | } 20 | 21 | void onAppActivate(GApplication *self, gpointer data) { 22 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 23 | gtk_window_present(window); 24 | } 25 | 26 | void onAppStartup(GApplication *self, gpointer data) { 27 | GtkWidget *window = gtk_application_window_new(GTK_APPLICATION(self)); 28 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 29 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 30 | } -------------------------------------------------------------------------------- /c/gtk4/application.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk4.Application"; 7 | 8 | int main(int argc, char **argv) { 9 | GtkApplication *app = gtk_application_new(appID, 0); 10 | 11 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 12 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 13 | 14 | int result = g_application_run(G_APPLICATION(app), argc, argv); 15 | g_object_unref(app); 16 | 17 | return result; 18 | } 19 | 20 | void onAppActivate(GApplication *self, gpointer data) { 21 | g_print("Hello there!\n"); 22 | } 23 | 24 | void onAppStartup(GApplication *self, gpointer data) { 25 | g_print("Initializing the app... Done!\n"); 26 | } -------------------------------------------------------------------------------- /c/gtk4/box.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | void onButtonClicked(GtkButton *self, gpointer data); 6 | 7 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk4.Box"; 8 | const gchar *appTitle = "GtkBox"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int result = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return result; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *box, *hintText, *button; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | box = gtk_box_new(GTK_ORIENTATION_VERTICAL, 10); 32 | hintText = gtk_label_new("Click the button"); 33 | button = gtk_button_new_with_label("🤔"); 34 | 35 | gtk_window_set_child(GTK_WINDOW(window), box); 36 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 37 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 38 | 39 | gtk_box_append(GTK_BOX(box), hintText); 40 | gtk_box_append(GTK_BOX(box), button); 41 | gtk_widget_set_halign(box, GTK_ALIGN_CENTER); 42 | gtk_widget_set_valign(box, GTK_ALIGN_CENTER); 43 | 44 | gtk_widget_set_halign(button, GTK_ALIGN_CENTER); 45 | gtk_widget_set_valign(button, GTK_ALIGN_CENTER); 46 | g_signal_connect(button, "clicked", G_CALLBACK(onButtonClicked), NULL); 47 | } 48 | 49 | void onButtonClicked(GtkButton *self, gpointer data) { 50 | static int count = 0; 51 | g_print("You clicked %d times!\n", ++count); 52 | } -------------------------------------------------------------------------------- /c/gtk4/builder.c.in: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppStartup(GApplication *self, gpointer data); 4 | void onAppActivate(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk4.Builder"; 7 | 8 | int main(int argc, char **argv) { 9 | GtkApplication *app = gtk_application_new(appID, 0); 10 | 11 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 12 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 13 | 14 | int result = g_application_run(G_APPLICATION(app), argc, argv); 15 | g_object_unref(app); 16 | 17 | return result; 18 | } 19 | 20 | void onAppActivate(GApplication *self, gpointer data) { 21 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 22 | gtk_window_present(window); 23 | } 24 | 25 | void onAppStartup(GApplication *self, gpointer data) { 26 | GtkBuilder *builder = gtk_builder_new_from_file("@builderPath@"); 27 | GObject *window = gtk_builder_get_object(builder, "window"); 28 | gtk_application_add_window(GTK_APPLICATION(self), GTK_WINDOW(window)); 29 | } -------------------------------------------------------------------------------- /c/gtk4/header-bar.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk4.HeaderBar"; 7 | const gchar *appTitle = "GtkHeaderBar"; 8 | const gchar *appSubtitle = "App subtitle"; 9 | 10 | int main(int argc, char **argv) { 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int result = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return result; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *titleText, *subtitleText, *titleBox, *headerBar; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | titleText = gtk_label_new(appTitle); 32 | subtitleText = gtk_label_new(appSubtitle); 33 | titleBox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); 34 | headerBar = gtk_header_bar_new(); 35 | 36 | gtk_window_set_titlebar(GTK_WINDOW(window), headerBar); 37 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 38 | 39 | gtk_label_set_use_markup(GTK_LABEL(titleText), TRUE); 40 | gtk_label_set_use_markup(GTK_LABEL(subtitleText), TRUE); 41 | 42 | gtk_widget_set_margin_top(titleBox, 4); 43 | gtk_widget_set_margin_bottom(titleBox, 4); 44 | gtk_widget_set_valign(titleBox, GTK_ALIGN_CENTER); 45 | 46 | gtk_box_append(GTK_BOX(titleBox), titleText); 47 | gtk_box_append(GTK_BOX(titleBox), subtitleText); 48 | 49 | // Since GTK 4, we need to manually add a title widget if we need/want it, 50 | // because there's no longer `title` and `subtitle` properties 51 | gtk_header_bar_set_title_widget(GTK_HEADER_BAR(headerBar), titleBox); 52 | gtk_header_bar_set_show_title_buttons(GTK_HEADER_BAR(headerBar), TRUE); 53 | } -------------------------------------------------------------------------------- /c/gtk4/label.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void onAppActivate(GApplication *self, gpointer data); 4 | void onAppStartup(GApplication *self, gpointer data); 5 | 6 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk4.Label"; 7 | const gchar *appTitle = "GtkLabel"; 8 | 9 | int main(int argc, char **argv) { 10 | const gchar *appID = "io.github.Miqueas.GTK-Examples.C.Gtk4.Label"; 11 | GtkApplication *app = gtk_application_new(appID, 0); 12 | 13 | g_signal_connect(app, "startup", G_CALLBACK(onAppStartup), NULL); 14 | g_signal_connect(app, "activate", G_CALLBACK(onAppActivate), NULL); 15 | 16 | int result = g_application_run(G_APPLICATION(app), argc, argv); 17 | g_object_unref(app); 18 | 19 | return result; 20 | } 21 | 22 | void onAppActivate(GApplication *self, gpointer data) { 23 | GtkWindow *window = gtk_application_get_active_window(GTK_APPLICATION(self)); 24 | gtk_window_present(window); 25 | } 26 | 27 | void onAppStartup(GApplication *self, gpointer data) { 28 | GtkWidget *window, *label; 29 | 30 | window = gtk_application_window_new(GTK_APPLICATION(self)); 31 | label = gtk_label_new("Hi there!"); 32 | 33 | gtk_window_set_child(GTK_WINDOW(window), label); 34 | gtk_window_set_title(GTK_WINDOW(window), appTitle); 35 | gtk_window_set_default_size(GTK_WINDOW(window), 400, 400); 36 | } -------------------------------------------------------------------------------- /c/gtk4/meson.build: -------------------------------------------------------------------------------- 1 | src = [ 2 | 'application', 3 | 'application-window', 4 | 'box', 5 | 'builder', 6 | 'header-bar', 7 | 'label', 8 | 'window', 9 | ] 10 | 11 | builder = configure_file( 12 | input: 'builder.c.in', 13 | output: 'builder.c', 14 | configuration: builder_gtk4_ui 15 | ) 16 | 17 | if build_machine.system() == 'linux' 18 | executable('glarea', 'glarea.c', dependencies: [ gtk4, glew ]) 19 | endif 20 | 21 | foreach name : src 22 | if name == 'builder' 23 | executable(name, builder, dependencies: gtk4) 24 | else 25 | executable(name, name + '.c', dependencies: gtk4) 26 | endif 27 | endforeach -------------------------------------------------------------------------------- /c/gtk4/window.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | int main(void) { 4 | // In GTK 4, this function no longer requires the `argc` and `argv` parameters 5 | gtk_init(); 6 | 7 | GtkWidget *win = gtk_window_new(); 8 | gtk_window_set_default_size(GTK_WINDOW(win), 400, 400); 9 | 10 | // I think this is optional 11 | // g_signal_connect(win, "destroy", G_CALLBACK(gtk_window_close), win); 12 | gtk_window_present(GTK_WINDOW(win)); 13 | 14 | // In GTK 4, all the `gtk_main` API and family was remove 15 | while (g_list_model_get_n_items(gtk_window_get_toplevels()) > 0) 16 | g_main_context_iteration(NULL, TRUE); 17 | 18 | return 0; 19 | } -------------------------------------------------------------------------------- /data/builder-gtk3.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | GtkBuilder 8 | center 9 | 400 10 | 400 11 | center 12 | 13 | 14 | 15 | 16 | True 17 | GtkBuilder 18 | True 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | True 27 | Hello, world! 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /data/builder-gtk4.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | GtkBuilder 8 | 400 9 | 400 10 | 11 | 12 | 13 | 14 | True 15 | 16 | 17 | GtkBuilder 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Hello, world! 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /data/css-provider.css: -------------------------------------------------------------------------------- 1 | .color-red { 2 | color: red; 3 | font-weight: bold; 4 | font-style: italic; 5 | font-size: 32px; 6 | } -------------------------------------------------------------------------------- /data/grid-explained.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Miqueas/GTK-Examples/3cad3511fc26ba19a87636bb2eba6253d9733f67/data/grid-explained.png -------------------------------------------------------------------------------- /data/resources.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | template.ui 6 | 7 | -------------------------------------------------------------------------------- /data/template.ui: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /lua/gtk3/action-bar.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkActionBar: A full width container to add contextual actions 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.ActionBar" 7 | local appTitle = "GtkActionBar" 8 | local app = Gtk.Application { application_id = appID } 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow() 12 | local headerBar = Gtk.HeaderBar() 13 | local actionBar = Gtk.ActionBar() 14 | local mainBox = Gtk.Box() 15 | 16 | self:add_window(window) 17 | 18 | window:set_titlebar(headerBar) 19 | window:set_default_size(400, 400) 20 | window:add(mainBox) 21 | 22 | headerBar.visible = true 23 | headerBar.title = appTitle 24 | headerBar.show_close_button = true 25 | 26 | actionBar:pack_start(Gtk.Label.new("Something" )) 27 | actionBar:pack_end(Gtk.Button.new_with_label("A button")) 28 | 29 | mainBox.orientation = Gtk.Orientation.VERTICAL 30 | mainBox:pack_start(Gtk.Label.new("Here is the content of your app"), true, true, 0) 31 | mainBox:pack_end(actionBar, false, true, 0) 32 | mainBox:show_all() 33 | end 34 | 35 | function app:on_activate() 36 | self.active_window:present() 37 | end 38 | 39 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/application-window.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkApplicationWindow: A GtkWindow with GtkApplication integration 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.ApplicationWindow" 7 | local appTitle = "GtkApplicationWindow" 8 | local app = Gtk.Application { application_id = appID } 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow() 12 | local headerBar = Gtk.HeaderBar() 13 | 14 | self:add_window(window) 15 | 16 | window:set_titlebar(headerBar) 17 | window:set_default_size(400, 400) 18 | 19 | headerBar.visible = true 20 | headerBar.title = appTitle 21 | headerBar.show_close_button = true 22 | end 23 | 24 | function app:on_activate() 25 | self.active_window:present() 26 | end 27 | 28 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/application.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkApplication: Provides an application interface 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Application" 7 | local app = Gtk.Application { application_id = appID } 8 | 9 | -- I recommend to check out this links for more information: 10 | -- https://wiki.gnome.org/HowDoI/GtkApplication 11 | -- https://developer.gnome.org/gio/stable/GApplication.html 12 | -- https://developer.gnome.org/gtk3/stable/GtkApplication.html 13 | function app:on_startup() 14 | -- The ::startup signal is called only once, so it's recommended to create/prepare 15 | -- all the GUI here and then present it on the ::activate signal 16 | print("app::startup called") 17 | end 18 | 19 | function app:on_activate() 20 | -- The ::activate signal can be triggered indefinitely, so please try to not do heavy 21 | -- things here. Check out the doc links above for more details 22 | print("app::activate called") 23 | end 24 | 25 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/assistant.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local Glib = lgi.require("GLib") 4 | 5 | -- GtkAssistant: a widget for guiding the user through step to achieve something 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Assistant" 8 | local appTitle = "GtkAssistant" 9 | local appWelcomeText = "Welcome to the GtkAssistant example" 10 | local app = Gtk.Application { application_id = appID } 11 | 12 | local function doPage2(assistantWindow) 13 | local grid = Gtk.Grid() 14 | local infoLabel = Gtk.Label.new("Fill this form") 15 | local nameLabel = Gtk.Label.new("Full name") 16 | local usernameLabel = Gtk.Label.new("Username") 17 | local passwordLabel = Gtk.Label.new("Password") 18 | local nameEntry = Gtk.Entry() 19 | local usernameEntry = Gtk.Entry() 20 | local passwordEntry = Gtk.Entry { visibility = false, input_purpose = Gtk.InputPurpose.PASSWORD } 21 | local doneButton = Gtk.Button.new_with_label("Done") 22 | 23 | grid.column_spacing = 10 24 | grid.row_spacing = 10 25 | grid.halign = Gtk.Align.CENTER 26 | grid.valign = Gtk.Align.CENTER 27 | grid:attach(infoLabel, 0, 0, 2, 1) 28 | grid:attach(nameLabel, 0, 1, 1, 1) 29 | grid:attach(nameEntry, 1, 1, 1, 1) 30 | grid:attach(usernameLabel, 0, 2, 1, 1) 31 | grid:attach(usernameEntry, 1, 2, 1, 1) 32 | grid:attach(passwordLabel, 0, 3, 1, 1) 33 | grid:attach(passwordEntry, 1, 3, 1, 1) 34 | grid:attach(doneButton, 1, 4, 1, 1) 35 | grid:show_all() 36 | 37 | function doneButton:on_clicked() 38 | assistantWindow:set_page_complete(grid, true) 39 | end 40 | 41 | return grid 42 | end 43 | 44 | local function doPage3(assistantWindow) 45 | local progressBar = Gtk.ProgressBar() 46 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 47 | 48 | box.halign = Gtk.Align.CENTER 49 | box.valign = Gtk.Align.CENTER 50 | box:pack_start(Gtk.Label.new("Installing the system..."), false, false, 0) 51 | box:pack_start(progressBar, false, false, 0) 52 | box:show_all() 53 | 54 | progressBar.width_request = 240 55 | progressBar.pulse_step = 0.2 56 | 57 | function assistantWindow:on_apply() 58 | Glib.timeout_add(200, 200, function () 59 | local fraction = progressBar:get_fraction() 60 | 61 | if fraction == 1 or fraction == 1.0 then 62 | assistantWindow:set_page_complete(box, true) 63 | assistantWindow:commit() 64 | return false 65 | elseif fraction < 0 then 66 | progressBar:pulse() 67 | else 68 | progressBar:set_fraction(fraction + 0.1) 69 | end 70 | 71 | return true 72 | end, 1000) 73 | end 74 | 75 | return box 76 | end 77 | 78 | function app:on_activate() 79 | self.active_window:present() 80 | end 81 | 82 | function app:on_startup() 83 | local window = Gtk.Assistant() 84 | local headerBar = Gtk.HeaderBar() 85 | local page1 = Gtk.Label { visible = true, label = appWelcomeText, use_markup = true } 86 | local page2 = doPage2(window) 87 | local page3 = doPage3(window) 88 | local page4 = Gtk.Label { visible = true, label = "System installed successfully!" } 89 | 90 | self:add_window(window) 91 | 92 | window:set_titlebar(headerBar) 93 | window:set_default_size(800, 400) 94 | window.on_cancel = Gtk.Widget.destroy 95 | window.on_close = Gtk.Widget.destroy 96 | 97 | window:append_page(page1) 98 | window:append_page(page2) 99 | window:append_page(page3) 100 | window:append_page(page4) 101 | 102 | window:set_page_title(page1, "Welcome") 103 | window:set_page_title(page2, "Create account") 104 | window:set_page_title(page3, "Installing system") 105 | window:set_page_title(page4, "Summary") 106 | 107 | window:set_page_type(page1, 1) 108 | window:set_page_type(page2, 2) 109 | window:set_page_type(page3, 4) 110 | window:set_page_type(page4, 3) 111 | 112 | window:set_page_complete(page1, true) 113 | 114 | headerBar.visible = true 115 | headerBar.subtitle = appTitle 116 | headerBar.show_close_button = true 117 | end 118 | 119 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/box.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkBox: An horizontal or vertical layout container 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Box" 7 | local appTitle = "GtkBox" 8 | local app = Gtk.Application { application_id = appID } 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow.new(self) 12 | local headerBar = Gtk.HeaderBar() 13 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 14 | 15 | window:add(box) 16 | window:set_titlebar(headerBar) 17 | window:set_default_size(400, 400) 18 | 19 | headerBar.visible = true 20 | headerBar.title = appTitle 21 | headerBar.show_close_button = true 22 | 23 | box.valign = Gtk.Align.CENTER 24 | box.halign = Gtk.Align.CENTER 25 | box:pack_start(Gtk.Label.new("A label"), false, true, 0) 26 | box:pack_start(Gtk.Button.new_with_label("A button"), false, true, 0) 27 | box:show_all() 28 | end 29 | 30 | function app:on_activate() 31 | self.active_window:present() 32 | end 33 | 34 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/builder-1.lua.in: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkBuilder: Create graphical user interfaces with XML 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Builder1" 7 | local app = Gtk.Application({ application_id = appID }) 8 | local builder = Gtk.Builder.new_from_file("@builderPath@") 9 | 10 | function app:on_startup() 11 | self:add_window(builder:get_object("window")) 12 | end 13 | 14 | function app:on_activate() 15 | self.active_window:present() 16 | end 17 | 18 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/builder-2.lua.in: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- Another way to use GtkBuilder in LGI 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Builder2" 7 | local app = Gtk.Application({ application_id = appID }) 8 | local builder = Gtk.Builder.new_from_file("@builderPath@") 9 | local ui = builder.objects 10 | 11 | function app:on_startup() 12 | self:add_window(ui.window) 13 | end 14 | 15 | function app:on_activate() 16 | self.active_window:present() 17 | end 18 | 19 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/button.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkButton: A button widget 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Button" 8 | local appTitle = "GtkButton" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.DEFAULT_FLAGS) 10 | 11 | function app:on_startup() 12 | local window = Gtk.ApplicationWindow.new(self) 13 | local headerBar = Gtk.HeaderBar() 14 | local button = Gtk.Button.new_with_label("Click me!") 15 | local count = 1 16 | 17 | window.child = button 18 | window.title = appTitle 19 | window:set_titlebar(headerBar) 20 | window:set_default_size(400, 400) 21 | 22 | headerBar.title = appTitle 23 | headerBar.visible = true 24 | headerBar.show_close_button = true 25 | 26 | button.valign = Gtk.Align.CENTER 27 | button.halign = Gtk.Align.CENTER 28 | button.visible = true 29 | 30 | -- The "clicked" signal is emited when the user press the button 31 | function button:on_clicked() 32 | self.label = ("Clicked %d times!"):format(count) 33 | count = count + 1 34 | end 35 | end 36 | 37 | function app:on_activate() 38 | self.active_window:present() 39 | end 40 | 41 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/combo-box-text.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.ComboBoxText" 5 | local appTitle = "GtkComboBoxText" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local window = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 400, 14 | border_width = 10 15 | }) 16 | 17 | -- Label to be updated 18 | local label = Gtk.Label({ visible = true, label = "Default id: gnome" }) 19 | 20 | --[[ GtkComboBoxText: 21 | 22 | Just a text-only GtkComboBox 23 | 24 | ]] 25 | local combo = Gtk.ComboBoxText({ visible = true }) 26 | 27 | local Items = { 28 | "GNOME", 29 | "KDE Plasma", 30 | "XFCE", 31 | "MATE", 32 | "Cinnamon", 33 | "Pantheon", 34 | "LXDE", 35 | "LXQT" 36 | } 37 | 38 | for id, value in pairs(Items) do 39 | -- For GtkComboBoxText, the ID can be either a number or text 40 | combo:append(id, value) 41 | end 42 | 43 | combo.active_id = 1 44 | 45 | -- Changes the 'Label' text when user change the combo box value 46 | function combo:on_changed() 47 | label.label = "Option id: " .. self:get_active_id() 48 | end 49 | 50 | local box = Gtk.Box({ 51 | visible = true, 52 | orientation = Gtk.Orientation.VERTICAL, 53 | spacing = 10, 54 | halign = Gtk.Align.CENTER, 55 | valign = Gtk.Align.CENTER, 56 | 57 | Gtk.Label({ visible = true, label = "Select an option" }), 58 | combo, 59 | label 60 | }) 61 | 62 | window:add(box) 63 | end 64 | 65 | function app:on_activate() 66 | self.active_window:present() 67 | end 68 | 69 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/combo-box.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local GObject = lgi.require("GObject", "2.0") 4 | 5 | -- GtkComboBox: A dropdown menu widget 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.ComboBox" 8 | local appTitle = "GtkComboBox" 9 | local app = Gtk.Application({ application_id = appID }) 10 | 11 | function app:on_startup() 12 | local win = Gtk.ApplicationWindow({ 13 | title = appTitle, 14 | application = self, 15 | default_width = 400, 16 | default_height = 400, 17 | border_width = 10 18 | }) 19 | 20 | -- Model for the combo box 21 | local model = Gtk.ListStore.new({ GObject.Type.STRING }) 22 | local items = { 23 | "GNOME", 24 | "KDE Plasma", 25 | "XFCE", 26 | "MATE", 27 | "Cinnamon", 28 | "Pantheon", 29 | "LXDE", 30 | "LXQT" 31 | } 32 | 33 | -- Add the items to the model 34 | for _, name in ipairs(items) do 35 | model:append({ name }) 36 | end 37 | 38 | -- Label to be updated 39 | local label = Gtk.Label({ visible = true, label = "Default option: 0" }) 40 | 41 | local combo = Gtk.ComboBox({ 42 | visible = true, 43 | model = model, 44 | active = 0, 45 | cells = { 46 | { 47 | Gtk.CellRendererText(), 48 | { text = 1 }, 49 | align = Gtk.Align.START 50 | } 51 | } 52 | }) 53 | 54 | -- Changes the 'label' text when user change the combo box value 55 | function combo:on_changed() 56 | local n = self:get_active() 57 | label.label = "Option "..n.." selected ("..items[n + 1]..")" 58 | end 59 | 60 | local box = Gtk.Box({ 61 | visible = true, 62 | orientation = Gtk.Orientation.VERTICAL, 63 | spacing = 10, 64 | halign = Gtk.Align.CENTER, 65 | valign = Gtk.Align.CENTER, 66 | 67 | Gtk.Label({ visible = true, label = "Select an option" }), 68 | combo, 69 | label 70 | }) 71 | 72 | win:add(box) 73 | end 74 | 75 | function app:on_activate() 76 | self.active_window:present() 77 | end 78 | 79 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/css-provider.lua.in: -------------------------------------------------------------------------------- 1 | --[[ 2 | Original by: Díaz Víctor (https://github.com/diazvictor) 3 | Date: 28.02.2021 02:26:39 -04 4 | Mantainer: Miqueas Martínez (https://github.com/Miqueas) 5 | ]] 6 | 7 | local lgi = require('lgi') 8 | local Gtk = lgi.require('Gtk', '3.0') 9 | local Gio = lgi.Gio 10 | 11 | -- GtkCssProvider: CSS-like styling class for widgets 12 | 13 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.CssProvider" 14 | local appTitle = "GtkCssProvider" 15 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.DEFAULT_FLAGS) 16 | 17 | function app:on_startup() 18 | local window = Gtk.ApplicationWindow.new(self) 19 | window.title = appTitle 20 | window.default_width = 400 21 | window.default_height = 400 22 | window.border_width = 10 23 | 24 | local provider = Gtk.CssProvider() 25 | provider:load_from_path("@cssPath@") 26 | 27 | local screen = window:get_screen() 28 | local styleContext = window:get_style_context() 29 | styleContext.add_provider_for_screen(screen, provider, 600) 30 | 31 | local label = Gtk.Label.new("Hi there!") 32 | local button = Gtk.Button.new_with_label("Toggle label style") 33 | 34 | function button:on_clicked() 35 | local labelStyleContext = label:get_style_context() 36 | 37 | if labelStyleContext:has_class("color-red") then 38 | labelStyleContext:remove_class("color-red") 39 | else 40 | labelStyleContext:add_class("color-red") 41 | end 42 | end 43 | 44 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 45 | box.halign = Gtk.Align.CENTER 46 | box.valign = Gtk.Align.CENTER 47 | box:pack_start(label, false, false, 0) 48 | box:pack_start(button, false, false, 0) 49 | box:show_all() 50 | 51 | window.child = box 52 | end 53 | 54 | function app:on_activate() 55 | self.active_window:present() 56 | end 57 | 58 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/dialog-1.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local Pango = lgi.Pango 4 | local Gio = lgi.Gio 5 | 6 | -- GtkDialog: A generic popup window 7 | 8 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Dialog1" 9 | local appTitle = "GtkDialog" 10 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.DEFAULT_FLAGS) 11 | 12 | function app:on_startup() 13 | local dialog = Gtk.Dialog() 14 | dialog.title = appTitle 15 | dialog.application = self 16 | dialog.border_width = 10 17 | dialog.default_width = 400 18 | 19 | dialog:add_button("Yes 😈👍", Gtk.ResponseType.OK) 20 | dialog:add_button("No 💀🤚", Gtk.ResponseType.CANCEL) 21 | 22 | local titleText = "Universe destruction" 23 | local titleLabel = Gtk.Label.new(titleText) 24 | titleLabel.use_markup = true 25 | 26 | local summaryText = "Our universe has a lot of problems and the only way to fix it is destroying the entire universe and this important decision is now in your hands." 27 | local summaryLabel = Gtk.Label.new(summaryText) 28 | summaryLabel.wrap = true 29 | summaryLabel.halign = 0 30 | summaryLabel.wrap_mode = Pango.WrapMode.CHAR 31 | 32 | local epilogText = "Do you accept?" 33 | local epilogLabel = Gtk.Label.new(epilogText) 34 | epilogLabel.use_markup = true 35 | 36 | local contentBox = dialog:get_content_area() 37 | contentBox.spacing = 10 38 | contentBox:pack_start(titleLabel, false, true, 0) 39 | contentBox:pack_start(summaryLabel, false, true, 0) 40 | contentBox:pack_start(epilogLabel, false, true, 10) 41 | contentBox:show_all() 42 | end 43 | 44 | function app:on_activate() 45 | -- When working with dialogs, use this instead of 'present()' 46 | local response = self.active_window:run() 47 | self.active_window:destroy() 48 | 49 | if response == Gtk.ResponseType.OK then 50 | print("Universe destroyed! 💥") 51 | elseif response == Gtk.ResponseType.CANCEL then 52 | print("Universe is in peace now... 🙏") 53 | else 54 | print("Nothing happens... 🤔") 55 | end 56 | end 57 | 58 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/dialog-2.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local Pango = lgi.require("Pango", "1.0") 4 | 5 | -- GtkDialog: A generic popup window 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Dialog2" 8 | local appTitle = "GtkDialog" 9 | local app = Gtk.Application({ application_id = appID }) 10 | 11 | function app:on_startup() 12 | local dialog = Gtk.Dialog({ 13 | application = self, 14 | use_header_bar = 1, 15 | default_width = 400, 16 | border_width = 10 17 | }) 18 | 19 | dialog:add_button("Yes 😈👍", Gtk.ResponseType.OK) 20 | dialog:add_button("No 💀🛑", Gtk.ResponseType.CANCEL) 21 | dialog:get_header_bar().title = appTitle 22 | 23 | local titleText = [[Universe destruction]] 24 | local titleLabel = Gtk.Label({ visible = true, label = titleText, use_markup = true }) 25 | 26 | local summaryText = "Our universe has a lot of problems and the only way to fix it is destroying the entire universe and this important decision is now in your hands." 27 | local summaryLabel = Gtk.Label({ 28 | visible = true, 29 | label = summaryText, 30 | xalign = 0, 31 | wrap = true, 32 | wrap_mode = Pango.WrapMode.CHAR 33 | }) 34 | 35 | local epilogText = [[Do you accept?]] 36 | local epilogLabel = Gtk.Label({ visible = true, label = epilogText, use_markup = true }) 37 | 38 | local contentBox = dialog:get_content_area() 39 | contentBox.spacing = 10 40 | contentBox:pack_start(titleLabel, false, true, 0) 41 | contentBox:pack_start(summaryLabel, false, true, 0) 42 | contentBox:pack_start(epilogLabel, false, true, 10) 43 | end 44 | 45 | function app:on_activate() 46 | -- When you work with dialogs, use this instead of 'present()' 47 | local response = self.active_window:run() 48 | self.active_window:destroy() 49 | 50 | if response == Gtk.ResponseType.OK then 51 | print("Universe destroyed! 💥") 52 | elseif response == Gtk.ResponseType.CANCEL then 53 | print("Universe is in peace now! 🙏") 54 | else 55 | print("Nothing happens! 🤔") 56 | end 57 | end 58 | 59 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/entry-completion.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local GObject = lgi.require("GObject", "2.0") 4 | 5 | -- GtkEntryCompletion: a helper class for GtkEntry widgets auto-completion 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.EntryCompletion" 8 | local appTitle = "GtkEntryCompletion" 9 | local app = Gtk.Application({ application_id = appID }) 10 | 11 | function app:on_startup() 12 | local win = Gtk.ApplicationWindow({ 13 | title = appTitle, 14 | application = self, 15 | default_width = 400, 16 | default_height = 400, 17 | border_width = 10 18 | }) 19 | 20 | local items = { "GNOME", "Lua", "LGI", "GTK", "Moonsteal", "Example" } 21 | local model = Gtk.ListStore.new({ GObject.Type.STRING }) 22 | 23 | for _, name in ipairs(items) do 24 | model:append({ name }) 25 | end 26 | 27 | local entryCompletion = Gtk.EntryCompletion({ 28 | model = model, 29 | text_column = 0, 30 | popup_completion = true 31 | }) 32 | 33 | local entry = Gtk.Entry({ visible = true, completion = entryCompletion }) 34 | 35 | local box = Gtk.Box({ 36 | visible = true, 37 | orientation = Gtk.Orientation.VERTICAL, 38 | spacing = 10, 39 | halign = Gtk.Align.CENTER, 40 | valign = Gtk.Align.CENTER, 41 | 42 | Gtk.Label({ visible = true, label = "Try typing \"gnome\" or \"gtk\"" }), 43 | entry 44 | }) 45 | 46 | win:add(box) 47 | end 48 | 49 | function app:on_activate() 50 | self.active_window:present() 51 | end 52 | 53 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/entry.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkEntry: a generic input widget 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Entry" 7 | local appTitle = "GtkEntry" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400, 16 | border_width = 10 17 | }) 18 | 19 | local label = Gtk.Label({ visible = true }) 20 | local entry = Gtk.Entry({ visible = true }) 21 | 22 | -- Updates the label text while typing 23 | function entry:on_key_release_event() 24 | label.label = entry.text 25 | end 26 | 27 | local box = Gtk.Box({ 28 | visible = true, 29 | orientation = Gtk.Orientation.VERTICAL, 30 | spacing = 10, 31 | halign = Gtk.Align.CENTER, 32 | valign = Gtk.Align.CENTER, 33 | 34 | Gtk.Label({ visible = true, label = "Enter some text" }), 35 | entry, 36 | label 37 | }) 38 | 39 | win:add(box) 40 | end 41 | 42 | function app:on_activate() 43 | self.active_window:present() 44 | end 45 | 46 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/expander.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkExpander: a "more details" widget 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Expander" 7 | local appTitle = "GtkExpander" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | border_width = 10, 16 | resizable = false 17 | }) 18 | 19 | local loremText = [[Duis in metus eros. Duis faucibus rutrum eros eu vestibulum. 20 | Proin et arcu nulla. Etiam at lacinia nibh. Vivamus pellentesque nunc nibh, 21 | ac dignissim massa lobortis ut. Integer eu felis in elit semper ullamcorper 22 | at in ipsum. Suspendisse tempus massa vel nibh tristique vestibulum. 23 | Vestibulum varius eu nunc eu interdum. Curabitur pulvinar velit in purus 24 | facilisis, et auctor augue consequat. Donec finibus felis ligula, a convallis 25 | justo tristique a.]] 26 | 27 | local expander = Gtk.Expander({ 28 | visible = true, 29 | label = [[ Lorem ipsum ]], 30 | use_markup = true, 31 | resize_toplevel = true, 32 | 33 | Gtk.Label({ visible = true, label = loremText, wrap = true }) 34 | }) 35 | 36 | win:add(expander) 37 | end 38 | 39 | function app:on_activate() 40 | self.active_window:present() 41 | end 42 | 43 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/file-chooser-button.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkFileChooserButton: a button that exclusively opens a file selection dialog 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.FileChooserButton" 7 | local appTitle = "GtkFileChooserButton" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400, 16 | border_width = 10 17 | }) 18 | 19 | local box = Gtk.Box({ 20 | visible = true, 21 | spacing = 10, 22 | orientation = Gtk.Orientation.VERTICAL, 23 | valign = Gtk.Align.CENTER, 24 | halign = Gtk.Align.CENTER, 25 | 26 | Gtk.Label({ visible = true, label = "Open a file" }), 27 | }) 28 | 29 | local label = Gtk.Label({ visible = true, wrap = true }) 30 | 31 | local fileChooserButton = Gtk.FileChooserButton({ 32 | visible = true, 33 | halign = Gtk.Align.CENTER 34 | }) 35 | 36 | function fileChooserButton:on_file_set() 37 | label.label = "Selected file: " .. self:get_filename() 38 | end 39 | 40 | box:pack_start(fileChooserButton, false, true, 0) 41 | box:pack_start(label, false, true, 0) 42 | 43 | win:add(box) 44 | end 45 | 46 | function app:on_activate() 47 | self.active_window:present() 48 | end 49 | 50 | return app:run() -------------------------------------------------------------------------------- /lua/gtk3/file-chooser-dailog.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkFileChooserDialog: a file selection dialog 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.FileChooserDialog" 7 | local appTitle = "GtkFileChooserDialog" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local fileChooserDialog = Gtk.FileChooserDialog({ 12 | title = appTitle, 13 | application = self, 14 | action = Gtk.FileChooserAction.OPEN 15 | }) 16 | 17 | fileChooserDialog:add_button("Open", Gtk.ResponseType.OK) 18 | fileChooserDialog:add_button("Cancel", Gtk.ResponseType.CANCEL) 19 | end 20 | 21 | function app:on_activate() 22 | local response = self.active_window:run() 23 | 24 | if response == Gtk.ResponseType.OK then 25 | local name = self.active_window:get_filename() 26 | print("You selected: " .. name) 27 | elseif response == Gtk.ResponseType.CANCEL then 28 | print("Canceled") 29 | else 30 | print("Something else") 31 | end 32 | 33 | self.active_window:destroy() 34 | end 35 | 36 | return app:run() -------------------------------------------------------------------------------- /lua/gtk3/fixed.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkFixed: a widget that can place widgets at fixed positions and fixed sizes 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Fixed" 7 | local appTitle = "GtkFixed" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400, 16 | border_width = 10 17 | }) 18 | 19 | local fixed = Gtk.Fixed({ 20 | visible = true, 21 | 22 | { Gtk.Label { visible = true, label = "A" }, y = 20, x = 10 }, 23 | { Gtk.Label { visible = true, label = "B" }, y = 200, x = 100 }, 24 | { Gtk.Label { visible = true, label = "C" }, y = 99, x = 326 } 25 | }) 26 | 27 | win:add(fixed) 28 | end 29 | 30 | function app:on_activate() 31 | self.active_window:present() 32 | end 33 | 34 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/flow-box.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.FlowBox" 5 | local appTitle = "GtkFlowBox" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local win = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 400, 14 | border_width = 10 15 | }) 16 | 17 | local flowbox = Gtk.FlowBox({ 18 | visible = true, 19 | valign = Gtk.Align.START, 20 | selection_mode = Gtk.SelectionMode.NONE, 21 | max_children_per_line = 30 22 | }) 23 | 24 | local scroll = Gtk.ScrolledWindow { 25 | visible = true, 26 | hscrollbar_policy = Gtk.PolicyType.NEVER 27 | } 28 | 29 | local icons = { 30 | "face-angry", 31 | "face-surprise", 32 | "face-laugh", 33 | "face-plain", 34 | "face-sad", 35 | "face-cool", 36 | "face-smirk", 37 | "face-sick", 38 | "face-kiss", 39 | "face-smile" 40 | } 41 | 42 | math.randomseed(os.time()) 43 | 44 | for i = 0, 399 do 45 | flowbox:insert(Gtk.Image { 46 | visible = true, 47 | icon_name = icons[math.random(1, 10)], 48 | pixel_size = 32 49 | }, i) 50 | end 51 | 52 | scroll:add(flowbox) 53 | win:add(scroll) 54 | end 55 | 56 | function app:on_activate() 57 | self.active_window:present() 58 | end 59 | 60 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/grid-1.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkGrid: a container that acts like a grid, with columns, rows and cells 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Grid1" 7 | local appTitle = "GtkGrid" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400, 16 | border_width = 10 17 | }) 18 | 19 | local grid = Gtk.Grid({ 20 | visible = true, 21 | -- This makes all columns of the same width 22 | column_homogeneous = true, 23 | -- Same, but for rows 24 | row_homogeneous = true, 25 | -- Sets the spacing between all columns 26 | column_spacing = 10, 27 | -- Same, but for rows 28 | row_spacing = 10, 29 | 30 | halign = Gtk.Align.CENTER, 31 | valign = Gtk.Align.CENTER, 32 | 33 | -- If you can't understand how GtkGrid works, see the GtkGrid-Explained.png file 34 | { Gtk.Label({ visible = true, label = "Top: 0. Left: 0" }), top_attach = 0, left_attach = 0 }, 35 | { Gtk.Label({ visible = true, label = "Top: 0. Left: 1" }), top_attach = 0, left_attach = 1 }, 36 | { Gtk.Label({ visible = true, label = "Top: 0. Left: 2" }), top_attach = 0, left_attach = 2 }, 37 | { Gtk.Label({ visible = true, label = "Top: 1. Left: 0" }), top_attach = 1, left_attach = 0 }, 38 | { Gtk.Label({ visible = true, label = "Top: 1. Left: 1" }), top_attach = 1, left_attach = 1 }, 39 | { Gtk.Label({ visible = true, label = "Top: 1. Left: 2" }), top_attach = 1, left_attach = 2 }, 40 | { Gtk.Label({ visible = true, label = "Top: 2. Left: 0" }), top_attach = 2, left_attach = 0 }, 41 | { Gtk.Label({ visible = true, label = "Top: 2. Left: 1" }), top_attach = 2, left_attach = 1 }, 42 | { Gtk.Label({ visible = true, label = "Top: 2. Left: 2" }), top_attach = 2, left_attach = 2 } 43 | }) 44 | 45 | win:add(grid) 46 | end 47 | 48 | function app:on_activate() 49 | self.active_window:present() 50 | end 51 | 52 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/grid-2.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Grid2" 5 | local appTitle = "GtkGrid" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local win = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 400, 14 | border_width = 10 15 | }) 16 | 17 | local grid = Gtk.Grid({ 18 | visible = true, 19 | column_homogeneous = true, 20 | row_homogeneous = true, 21 | column_spacing = 10, 22 | row_spacing = 10, 23 | 24 | halign = Gtk.Align.CENTER, 25 | valign = Gtk.Align.CENTER, 26 | 27 | { Gtk.Label({ visible = true, label = "Columns" }), top_attach = 0, left_attach = 1, width = 2 }, 28 | { Gtk.Label({ visible = true, label = "Rows" }), top_attach = 0, left_attach = 0, height = 3 }, 29 | { Gtk.Label({ visible = true, label = "Top: 1. Left: 1" }), top_attach = 1, left_attach = 1 }, 30 | { Gtk.Label({ visible = true, label = "Top: 1. Left: 2" }), top_attach = 1, left_attach = 2 }, 31 | { Gtk.Label({ visible = true, label = "Top: 2. Left: 1" }), top_attach = 2, left_attach = 1 }, 32 | { Gtk.Label({ visible = true, label = "Top: 2. Left: 2" }), top_attach = 2, left_attach = 2 } 33 | }) 34 | 35 | win:add(grid) 36 | end 37 | 38 | function app:on_activate() 39 | self.active_window:present() 40 | end 41 | 42 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/grid-3.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Grid3" 5 | local appTitle = "GtkGrid" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local win = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 400, 14 | border_width = 10 15 | }) 16 | 17 | local grid = Gtk.Grid({ 18 | visible = true, 19 | column_homogeneous = true, 20 | row_homogeneous = true, 21 | column_spacing = 10, 22 | row_spacing = 10, 23 | 24 | halign = Gtk.Align.CENTER, 25 | valign = Gtk.Align.CENTER, 26 | }) 27 | 28 | for top = 0, 2 do 29 | for left = 0, 2 do 30 | grid:attach( 31 | Gtk.Label({ 32 | visible = true, 33 | label = ("Top: %d. Left: %d."):format(top, left) 34 | }), 35 | left, top, 1, 1 36 | ) 37 | end 38 | end 39 | 40 | win:add(grid) 41 | end 42 | 43 | function app:on_activate() 44 | self.active_window:present() 45 | end 46 | 47 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/header-bar.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkHeaderBar: a (client-side) title bar decoration widget 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.HeaderBar" 7 | local appTitle = "GtkHeaderBar" 8 | local appSubtitle = "A (client-side) title bar decoration widget" 9 | local app = Gtk.Application({ application_id = appID }) 10 | 11 | function app:on_startup() 12 | local win = Gtk.ApplicationWindow({ 13 | application = self, 14 | default_width = 400, 15 | default_height = 400 16 | }) 17 | 18 | local headerBar = Gtk.HeaderBar({ 19 | visible = true, 20 | -- GtkHeaderBar don't sets the window controls buttons by default 21 | -- this property changes that behavior 22 | show_close_button = true, 23 | title = appTitle, 24 | subtitle = appSubtitle 25 | }) 26 | 27 | win:set_titlebar(headerBar) 28 | end 29 | 30 | function app:on_activate() 31 | self.active_window:present() 32 | end 33 | 34 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/image-1.lua.in: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkImage: a widget that shows an image 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Image1" 7 | local appTitle = "GtkImage" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400 16 | }) 17 | 18 | local image = Gtk.Image({ visible = true, file = "sus.jpg", pixel_size = 256 }) 19 | 20 | win:add(image) 21 | end 22 | 23 | function app:on_activate() 24 | self.active_window:present() 25 | end 26 | 27 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/image-2.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Image2" 5 | local appTitle = "GtkImage" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local win = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 400 14 | }) 15 | 16 | local image = Gtk.Image({ visible = true, icon_name = "computer", pixel_size = 256 }) 17 | 18 | win:add(image) 19 | end 20 | 21 | function app:on_activate() 22 | self.active_window:present() 23 | end 24 | 25 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/info-bar.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkInfoBar: an "in-app notifications" like widget 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.InfoBar" 7 | local appTitle = "GtkInfoBar" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400, 16 | --border_width = 10 17 | }) 18 | 19 | local box = Gtk.Box({ visible = true, orientation = Gtk.Orientation.VERTICAL }) 20 | local infoButton = Gtk.Button({ visible = true, label = "Info" }) 21 | local warningButton = Gtk.Button({ visible = true, label = "Warning" }) 22 | local questionButton = Gtk.Button({ visible = true, label = "Question" }) 23 | local errorButton = Gtk.Button({ visible = true, label = "Error" }) 24 | local otherButton = Gtk.Button({ visible = true, label = "Other" }) 25 | 26 | local function createInfoBar(self, data) 27 | local infoBar = Gtk.InfoBar({ 28 | visible = true, 29 | message_type = Gtk.MessageType[self.label:upper()], 30 | show_close_button = true 31 | }) 32 | 33 | -- For this widget, the 'get_content_area()' method always returns a Gtk.Box 34 | infoBar 35 | :get_content_area() 36 | :pack_start(Gtk.Label({ visible = true, label = self.label }), false, true, 0) 37 | 38 | -- When an action widget is clicked, this signal is emitted 39 | function infoBar:on_response() 40 | -- For this case, just remove it from the Gtk.Box and then destroy it 41 | box:remove(self) 42 | self:destroy() 43 | end 44 | 45 | -- Add the info bar to the Gtk.Box 46 | box:pack_start(infoBar, false, true, 0) 47 | end 48 | 49 | infoButton.on_clicked = createInfoBar 50 | warningButton.on_clicked = createInfoBar 51 | questionButton.on_clicked = createInfoBar 52 | errorButton.on_clicked = createInfoBar 53 | otherButton.on_clicked = createInfoBar 54 | 55 | local grid = Gtk.Grid({ 56 | visible = true, 57 | column_homogeneous = true, 58 | row_homogeneous = true, 59 | column_spacing = 10, 60 | row_spacing = 10, 61 | 62 | halign = Gtk.Align.CENTER, 63 | valign = Gtk.Align.CENTER, 64 | 65 | { infoButton, top_attach = 0, left_attach = 0 }, 66 | { warningButton, top_attach = 0, left_attach = 1 }, 67 | { questionButton, top_attach = 1, left_attach = 0 }, 68 | { errorButton, top_attach = 1, left_attach = 1 }, 69 | { otherButton, top_attach = 2, left_attach = 0, width = 2 }, 70 | }) 71 | 72 | box:pack_end(grid, true, true, 0) 73 | win:add(box) 74 | end 75 | 76 | function app:on_activate() 77 | self.active_window:present() 78 | end 79 | 80 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/label-1.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkLabel: a text widget 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Label1" 7 | local appTitle = "GtkLabel" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400 16 | }) 17 | 18 | local label = Gtk.Label({ 19 | visible = true, 20 | label = "Hello, world!", 21 | valign = Gtk.Align.CENTER, 22 | halign = Gtk.Align.CENTER 23 | }) 24 | 25 | win:add(label) 26 | end 27 | 28 | function app:on_activate() 29 | self.active_window:present() 30 | end 31 | 32 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/label-2.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Label2" 5 | local appTitle = "GtkLabel" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local win = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 400 14 | }) 15 | 16 | local label = Gtk.Label({ 17 | visible = true, 18 | valign = Gtk.Align.CENTER, 19 | halign = Gtk.Align.CENTER, 20 | -- The use_markup property to use an HTML-like 21 | -- basic markup: 22 | use_markup = true, 23 | label = "Hello, world!" 24 | }) 25 | 26 | win:add(label) 27 | end 28 | 29 | function app:on_activate() 30 | self.active_window:present() 31 | end 32 | 33 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/list-box.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkListBox: a list container widget 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.ListBox1" 7 | local appTitle = "GtkListBox" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local win = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400, 16 | border_width = 10 17 | }) 18 | 19 | local listBox = Gtk.ListBox({ visible = true }) 20 | 21 | local scrolledWindow = Gtk.ScrolledWindow({ 22 | visible = true, 23 | shadow_type = Gtk.ShadowType.NONE, 24 | propagate_natural_width = true, 25 | propagate_natural_height = true, 26 | 27 | listBox 28 | }) 29 | 30 | local button = Gtk.Button({ 31 | visible = true, 32 | label = "Load", 33 | halign = Gtk.Align.CENTER, 34 | valign = Gtk.Align.CENTER 35 | }) 36 | 37 | function button:on_clicked() 38 | for i = 0, 100 do 39 | listBox:insert(Gtk.Label({ visible = true, label = "Text " .. i }), i) 40 | end 41 | end 42 | 43 | local box = Gtk.Box({ 44 | visible = true, 45 | orientation = Gtk.Orientation.VERTICAL, 46 | spacing = 10, 47 | 48 | { scrolledWindow, expand = true }, 49 | { button } 50 | }) 51 | 52 | win:add(box) 53 | end 54 | 55 | function app:on_activate() 56 | self.active_window:present() 57 | end 58 | 59 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/meson.build: -------------------------------------------------------------------------------- 1 | message('Run the Lua examples by just using the Lua command from the project root directory') 2 | message('Except for the following Lua examples that needs to be configured:') 3 | 4 | configure_file( 5 | input: 'builder-1.lua.in', 6 | output: 'builder-1.lua', 7 | configuration: builder_gtk3_ui 8 | ) 9 | 10 | configure_file( 11 | input: 'builder-2.lua.in', 12 | output: 'builder-2.lua', 13 | configuration: builder_gtk3_ui 14 | ) 15 | 16 | configure_file( 17 | input: 'css-provider.lua.in', 18 | output: 'css-provider.lua', 19 | configuration: css_conf 20 | ) 21 | 22 | configure_file( 23 | input: 'image-1.lua.in', 24 | output: 'image-1.lua', 25 | configuration: image_conf 26 | ) -------------------------------------------------------------------------------- /lua/gtk3/message-dialog-1.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkMessageDialog: a basic GtkDialog for messages 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.MessageDialog1" 7 | local appTitle = "GtkMessageDialog" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local titleMarkup = [[Universe destruction]] 12 | local messageText = "Our universe has a lot of problems and the only way to fix it is destroying the entire universe and this important decision is now in your hands." 13 | 14 | local messageDialog = Gtk.MessageDialog({ 15 | application = self, 16 | buttons = Gtk.ButtonsType.NONE, 17 | message_type = Gtk.MessageType.QUESTION, 18 | title = appTitle, 19 | text = titleMarkup, 20 | use_markup = true, 21 | secondary_text = messageText 22 | }) 23 | 24 | messageDialog:add_button("Yes 👍", Gtk.ResponseType.OK) 25 | messageDialog:add_button("No 🛑", Gtk.ResponseType.CANCEL) 26 | end 27 | 28 | function app:on_activate() 29 | -- When you work with dialogs, use this instead of 'present()' 30 | local response = self.active_window:run() 31 | 32 | if response == Gtk.ResponseType.OK then 33 | print("Universe destroyed! 💥") 34 | elseif response == Gtk.ResponseType.CANCEL then 35 | print("Universe is in peace now! 🙏") 36 | else 37 | print("Nothing happens! 🤔") 38 | end 39 | 40 | self.active_window:destroy() 41 | end 42 | 43 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/message-dialog-2.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.MessageDialog2" 5 | local appTitle = "GtkMessageDialog" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local titleMarkup = [[Universe destruction]] 10 | local messageText = "Our universe has a lot of problems and the only way to fix it is destroying the entire universe and this important decision is now in your hands." 11 | 12 | local messageDialog = Gtk.MessageDialog({ 13 | title = appTitle, 14 | application = self, 15 | buttons = Gtk.ButtonsType.YES_NO, 16 | message_type = Gtk.MessageType.QUESTION, 17 | text = titleMarkup, 18 | secondary_text = messageText, 19 | use_markup = true 20 | }) 21 | 22 | function messageDialog:on_response(responseID) 23 | if responseID == Gtk.ResponseType.YES then 24 | print("Universe destroyed! 💥") 25 | elseif responseID == Gtk.ResponseType.NO then 26 | print("Universe is in peace now! 🙏") 27 | else 28 | print("Something else...") 29 | print("Response ID: " .. tostring(responseID)) 30 | end 31 | 32 | self:destroy() 33 | end 34 | end 35 | 36 | function app:on_activate() 37 | self.active_window:run() 38 | end 39 | 40 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/notebook.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Notebook" 5 | local appTitle = "GtkNotebook" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local win = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 400 14 | }) 15 | 16 | local newTabButton = Gtk.Button({ 17 | visible = true, 18 | halign = Gtk.Align.CENTER, 19 | margin = 10, 20 | 21 | Gtk.Box({ 22 | visible = true, 23 | spacing = 6, 24 | orientation = Gtk.Orientation.HORIZONTAL, 25 | 26 | Gtk.Label({ visible = true, label = "New tab" }), 27 | Gtk.Image({ visible = true, icon_name = "list-add-symbolic" }) 28 | }) 29 | }) 30 | 31 | local notebook = Gtk.Notebook({ 32 | visible = true, 33 | -- Removes a default border around the widget 34 | show_border = false, 35 | -- If has a lot of tabs, then makes it "scrollable" 36 | scrollable = true 37 | }) 38 | 39 | local count = 1 40 | 41 | function newTabButton:on_clicked() 42 | notebook:append_page( 43 | -- Page content 44 | Gtk.Label({ visible = true, label = "Page " .. count }), 45 | -- Page tab widget 46 | Gtk.Label({ visible = true, label = "Tab " .. count }) 47 | ) 48 | 49 | count = count + 1 50 | end 51 | 52 | local box = Gtk.Box({ 53 | visible = true, 54 | orientation = Gtk.Orientation.VERTICAL 55 | }) 56 | 57 | box:pack_start(notebook, true, true, 0) 58 | box:pack_start(newTabButton, false, false, 0) 59 | 60 | win:add(box) 61 | end 62 | 63 | function app:on_activate() 64 | self.active_window:present() 65 | end 66 | 67 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/overlay.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkOverlay: a widget for pushing widgets on top of other (like an stack) 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Overlay" 7 | local appTitle = "GtkOverlay" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400 16 | }) 17 | 18 | local overlay = Gtk.Overlay({ 19 | visible = true, 20 | 21 | Gtk.Image({ 22 | visible = true, 23 | icon_name = "computer", 24 | pixel_size = 256 25 | }) 26 | }) 27 | 28 | overlay:add_overlay(Gtk.Image({ 29 | visible = true, 30 | icon_name = "computer-symbolic", 31 | pixel_size = 128 32 | })) 33 | 34 | overlay:add_overlay(Gtk.Image({ 35 | visible = true, 36 | icon_name = "input-gaming", 37 | pixel_size = 64 38 | })) 39 | 40 | overlay:add_overlay(Gtk.Image({ 41 | visible = true, 42 | icon_name = "input-gaming-symbolic", 43 | pixel_size = 32 44 | })) 45 | 46 | window:add(overlay) 47 | end 48 | 49 | function app:on_activate() 50 | self.active_window:present() 51 | end 52 | 53 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/paned.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkPaned: a widget with 2 panes and a adjustable division 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Paned" 7 | local appTitle = "GtkPaned" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400 16 | }) 17 | 18 | local paned = Gtk.Paned({ visible = true, orientation = Gtk.Orientation.HORIZONTAL }) 19 | paned:pack1(Gtk.Label({ visible = true, label = "Space 1" }), true, false) 20 | paned:pack2(Gtk.Label({ visible = true, label = "Space 2" }), true, false) 21 | 22 | window:add(paned) 23 | end 24 | 25 | function app:on_activate() 26 | self.active_window:present() 27 | end 28 | 29 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/progress-bar.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.ProgressBar" 5 | local appTitle = "GtkProgressBar" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local window = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 300, 13 | default_height = 300, 14 | border_width = 10 15 | }) 16 | 17 | local progressBar = Gtk.ProgressBar({ show_text = true, pulse_step = 0.2 }) 18 | local pulseButton = Gtk.Button({ label = "Pulse" }) 19 | local fillButton = Gtk.Button({ label = "Fill" }) 20 | local setButton = Gtk.Button({ label = "Set" }) 21 | 22 | function pulseButton:on_clicked() 23 | progressBar:pulse() 24 | end 25 | 26 | function fillButton:on_clicked() 27 | progressBar.fraction = 1.0 28 | end 29 | 30 | function setButton:on_clicked() 31 | progressBar.fraction = progressBar.fraction + 0.01 32 | end 33 | 34 | local box = Gtk.Box({ 35 | spacing = 10, 36 | orientation = Gtk.Orientation.VERTICAL, 37 | valign = Gtk.Align.CENTER, 38 | halign = Gtk.Align.CENTER, 39 | 40 | progressBar, 41 | Gtk.Box({ 42 | spacing = 10, 43 | orientation = Gtk.Orientation.HORIZONTAL, 44 | valign = Gtk.Align.CENTER, 45 | halign = Gtk.Align.CENTER, 46 | 47 | pulseButton, 48 | fillButton, 49 | setButton 50 | }) 51 | }) 52 | 53 | box:show_all() 54 | window:add(box) 55 | end 56 | 57 | function app:on_activate() 58 | self.active_window:present() 59 | end 60 | 61 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/revealer.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkRevealer: a container for show/hide a widget with an animation. 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Revealer" 7 | local appTitle = "GtkRevealer" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 200, 15 | resizable = false, 16 | border_width = 10 17 | }) 18 | 19 | local revealer = Gtk.Revealer({ 20 | visible = true, 21 | reveal_child = true, 22 | 23 | Gtk.Label({ visible = true, label = "Hello there!", margin = 10 }) 24 | }) 25 | 26 | local toggleButton = Gtk.ToggleButton({ visible = true, label = "Toggle message", active = revealer.reveal_child }) 27 | 28 | function toggleButton:on_clicked() 29 | revealer.reveal_child = (not revealer.reveal_child) and true or false 30 | end 31 | 32 | local box = Gtk.Box({ visible = true, orientation = Gtk.Orientation.VERTICAL }) 33 | box:pack_start(revealer, true, true, 0) 34 | box:pack_start(toggleButton, false, false, 0) 35 | 36 | window:add(box) 37 | end 38 | 39 | function app:on_activate() 40 | self.active_window:present() 41 | end 42 | 43 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/scrolled-window.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkScrolledWindow: A container for scrollable content 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.ScrolledWindow" 7 | local appTitle = "GtkScrolledWindow" 8 | local app = Gtk.Application({ application_id = appID }) 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow({ 12 | title = appTitle, 13 | application = self, 14 | default_width = 400, 15 | default_height = 400 16 | }) 17 | 18 | local grid = Gtk.Grid({ 19 | visible = true, 20 | column_homogeneous = true, 21 | row_homogeneous = true, 22 | column_spacing = 10, 23 | row_spacing = 10, 24 | halign = Gtk.Align.START, 25 | valign = Gtk.Align.START 26 | }) 27 | 28 | local scroll = Gtk.ScrolledWindow({ 29 | visible = true, 30 | -- Removes a shadow that is added by default 31 | shadow_type = Gtk.ShadowType.NONE, 32 | -- "Expands" the child width, and makes the child use the real allocated width 33 | propagate_natural_width = true, 34 | -- Same, but for height 35 | propagate_natural_height = true, 36 | 37 | grid 38 | }) 39 | 40 | for top = 1, 100 do 41 | for left = 1, 100 do 42 | grid:attach( 43 | Gtk.Label({ visible = true, label = "Top: "..top..". Left: "..left }), 44 | left - 1, 45 | top - 1, 46 | 1, 1 47 | ) 48 | end 49 | end 50 | 51 | window:add(scroll) 52 | end 53 | 54 | function app:on_activate() 55 | self.active_window:present() 56 | end 57 | 58 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/spinner.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Spinner" 5 | local appTitle = "GtkSpinner" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local window = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 300, 13 | default_height = 300, 14 | border_width = 10 15 | }) 16 | 17 | local spinner = Gtk.Spinner({ visible = true }) 18 | local button = Gtk.Button({ visible = true, label = "Start" }) 19 | 20 | function button:on_clicked() 21 | if spinner.active then 22 | self.label = "Start" 23 | spinner:stop() 24 | else 25 | self.label = "Stop" 26 | spinner:start() 27 | end 28 | end 29 | 30 | local box = Gtk.Box({ 31 | visible = true, 32 | spacing = 10, 33 | orientation = Gtk.Orientation.VERTICAL, 34 | valign = Gtk.Align.CENTER, 35 | halign = Gtk.Align.CENTER, 36 | 37 | spinner, 38 | button 39 | }) 40 | 41 | window:add(box) 42 | end 43 | 44 | function app:on_activate() 45 | self.active_window:present() 46 | end 47 | 48 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/stack-1.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkStack: a container that shows only one child at a time 5 | 6 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Stack1" 7 | local appTitle = "GtkStack" 8 | local app = Gtk.Application({ application_id = appID }) 9 | local labelText = "Page %d" 10 | local labelTitle = "Page %d" 11 | local labelName = "page%d" 12 | 13 | function app:on_startup() 14 | local window = Gtk.ApplicationWindow({ 15 | title = appTitle, 16 | application = self, 17 | default_width = 600, 18 | default_height = 400, 19 | border_width = 10 20 | }) 21 | 22 | local stack = Gtk.Stack({ 23 | transition_type = Gtk.StackTransitionType.SLIDE_LEFT_RIGHT, 24 | -- Increment this value to see the animation more slow 25 | transition_duration = 280 26 | }) 27 | 28 | for i = 1, 3 do 29 | stack:add_titled( 30 | Gtk.Label({ use_markup = true, label = labelText:format(i) }), 31 | labelName:format(i), 32 | labelTitle:format(i) 33 | ) 34 | end 35 | 36 | local box = Gtk.Box({ 37 | orientation = Gtk.Orientation.VERTICAL, 38 | { stack, expand = true }, 39 | Gtk.StackSwitcher({ stack = stack, halign = Gtk.Align.CENTER }) 40 | }) 41 | 42 | box:show_all() 43 | window:add(box) 44 | end 45 | 46 | function app:on_activate() 47 | self.active_window:present() 48 | end 49 | 50 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/stack-2.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Stack2" 5 | local appTitle = "" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local window = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 600, 13 | default_height = 400 14 | }) 15 | 16 | local stack = Gtk.Stack({ 17 | visible = true, 18 | transition_type = Gtk.StackTransitionType.SLIDE_UP_DOWN, 19 | -- Increment this value to see the animation more slow 20 | transition_duration = 280, 21 | 22 | { 23 | Gtk.Label({ 24 | visible = true, 25 | label = "Page 0", 26 | use_markup = true 27 | }), 28 | title = "Page 0", 29 | name = "Page0" 30 | }, 31 | { 32 | Gtk.Label({ 33 | visible = true, 34 | label = "Page 1", 35 | use_markup = true 36 | }), 37 | title = "Page 1", 38 | name = "Page1" 39 | }, 40 | { 41 | Gtk.Label({ 42 | visible = true, 43 | label = "Page 2", 44 | use_markup = true 45 | }), 46 | title = "Page 2", 47 | name = "Page2" 48 | }, 49 | { 50 | Gtk.Label({ 51 | visible = true, 52 | label = "Page 3", 53 | use_markup = true 54 | }), 55 | title = "Page 3", 56 | name = "Page3" 57 | } 58 | }) 59 | 60 | local box = Gtk.Box({ 61 | visible = true, 62 | orientation = Gtk.Orientation.HORIZONTAL, 63 | 64 | Gtk.StackSidebar({ 65 | visible = true, 66 | stack = stack, 67 | halign = Gtk.Align.CENTER 68 | }), 69 | { 70 | stack, 71 | expand = true 72 | } 73 | }) 74 | 75 | window:add(box) 76 | end 77 | 78 | function app:on_activate() 79 | self.active_window:present() 80 | end 81 | 82 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/status-bar.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.Statusbar" 5 | local appTitle = "GtkStatusbar" 6 | local app = Gtk.Application({ application_id = appID }) 7 | 8 | function app:on_startup() 9 | local window = Gtk.ApplicationWindow({ 10 | title = appTitle, 11 | application = self, 12 | default_width = 400, 13 | default_height = 200 14 | }) 15 | 16 | local statusbar = Gtk.Statusbar({ visible = true }) 17 | 18 | local button = Gtk.Button({ 19 | visible = true, 20 | label = "Click me", 21 | halign = Gtk.Align.CENTER, 22 | valign = Gtk.Align.CENTER 23 | }) 24 | 25 | 26 | local c = 0 27 | 28 | function button:on_clicked() 29 | c = c + 1 30 | statusbar:push(c, "You clicked " .. c .. " times") 31 | end 32 | 33 | local box = Gtk.Box({ visible = true, orientation = Gtk.Orientation.VERTICAL }) 34 | box:pack_start(button, true, true, 0) 35 | box:pack_end(statusbar, false, true, 0) 36 | 37 | window:add(box) 38 | end 39 | 40 | function app:on_activate() 41 | --[[ GtkStatusbar: A widget like GtkActionBar for report some info to the user. 42 | 43 | ]] 44 | self.active_window:present() 45 | end 46 | 47 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/tree-view.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local GObject = lgi.require("GObject") 4 | 5 | local Column = { NAME = 1, SELECTED = 2 } 6 | 7 | local items = { 8 | { group = "Code Editors", 9 | child = { 10 | { app = "VS Code", selected = true }, 11 | { app = "Sublime Text", selected = false }, 12 | { app = "Atom", selected = false }, 13 | } 14 | }, 15 | { group = "Music Services", 16 | child = { 17 | { app = "YouTube Music", selected = true }, 18 | { app = "Deezer", selected = true }, 19 | { app = "Spotify", selected = false }, 20 | } 21 | }, 22 | { group = "Web Browsers", 23 | child = { 24 | { app = "Microsoft Edge", selected = true }, 25 | { app = "Google Chrome", selected = true }, 26 | { app = "Mozilla Firefox", selected = false }, 27 | } 28 | }, 29 | } 30 | 31 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk3.TreeView" 32 | local appTitle = "GtkTreeView" 33 | local app = Gtk.Application({ application_id = appID }) 34 | 35 | function app:on_startup() 36 | local window = Gtk.ApplicationWindow.new(self) 37 | local box = Gtk.Box({ orientation = Gtk.Orientation.VERTICAL, spacing = 10 }) 38 | local titleText = Gtk.Label({ label = "Software Selection" }) 39 | local treeView = Gtk.TreeView() 40 | local treeViewModel = Gtk.TreeStore.new({ GObject.Type.STRING, GObject.Type.BOOLEAN }) 41 | 42 | window:add(box) 43 | window.title = appTitle 44 | window.border_width = 10 45 | window:set_default_size(400, 400) 46 | 47 | box:pack_start(titleText, false, false, 0) 48 | box:pack_start(treeView, true, true, 0) 49 | box:show_all() 50 | 51 | treeView.model = treeViewModel 52 | treeView:append_column(Gtk.TreeViewColumn({ title = "App", expand = true, 53 | { Gtk.CellRendererText(), { text = Column.NAME } } 54 | })) 55 | 56 | treeView:append_column(Gtk.TreeViewColumn({ title = "Install", 57 | { Gtk.CellRendererToggle({ on_toggled = function (self, pathStr) 58 | print("TODO") 59 | end }), { active = Column.SELECTED } } 60 | })) 61 | 62 | for _, item in ipairs(items) do 63 | local parentIter = treeViewModel:append() 64 | treeViewModel[parentIter] = { 65 | [Column.NAME] = item.group, 66 | } 67 | 68 | for _, child in ipairs(item.child) do 69 | local childIter = treeViewModel:append(parentIter) 70 | treeViewModel[childIter] = { 71 | [Column.NAME] = child.app, 72 | [Column.SELECTED] = child.selected, 73 | } 74 | end 75 | end 76 | end 77 | 78 | function app:on_activate() 79 | self.active_window:present() 80 | end 81 | 82 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk3/window.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | 4 | -- GtkWindow: a window widget 5 | 6 | local window = Gtk.Window({ 7 | default_width = 400, 8 | default_height = 400 9 | }) 10 | 11 | -- The "destroy" signal is emited when the user clicks the close button 12 | window.on_destroy = Gtk.main_quit 13 | window:present() 14 | 15 | Gtk.main() -------------------------------------------------------------------------------- /lua/gtk4/action-bar.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkActionBar: A full width container to add contextual actions 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.ActionBar" 8 | local appTitle = "GtkActionBar" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 10 | 11 | function app:on_startup() 12 | local window = Gtk.ApplicationWindow.new(self) 13 | local titleLabel = Gtk.Label.new(([[%s]]):format(appTitle)) 14 | local headerBar = Gtk.HeaderBar.new() 15 | local actionBar = Gtk.ActionBar.new() 16 | local content = Gtk.Label.new("Here is the content of your app") 17 | local mainBox = Gtk.Box() 18 | 19 | self:add_window(window) 20 | 21 | window.child = mainBox 22 | window:set_titlebar(headerBar) 23 | window:set_default_size(400, 400) 24 | 25 | titleLabel.use_markup = true 26 | 27 | headerBar.title_widget = titleLabel 28 | headerBar.show_title_buttons = true 29 | 30 | actionBar:pack_start(Gtk.Label.new("Something")) 31 | actionBar:pack_end(Gtk.Button.new_with_label("A button")) 32 | 33 | content.vexpand = true 34 | 35 | mainBox.orientation = Gtk.Orientation.VERTICAL 36 | mainBox:append(content) 37 | mainBox:append(actionBar) 38 | end 39 | 40 | function app:on_activate() 41 | self.active_window:present() 42 | end 43 | 44 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/application-window.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkApplicationWindow: A GtkWindow with GtkApplication integration 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.ApplicationWindow" 8 | local appTitle = "GtkApplicationWindow" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 10 | 11 | function app:on_startup() 12 | local window = Gtk.ApplicationWindow.new(self) 13 | window.title = appTitle 14 | window:set_default_size(400, 400) 15 | end 16 | 17 | function app:on_activate() 18 | self.active_window:present() 19 | end 20 | 21 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/application.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "3.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkApplication: Provides an application interface 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.Application" 8 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 9 | 10 | -- I recommend to check out this links for more information: 11 | -- https://wiki.gnome.org/HowDoI/GtkApplication 12 | -- https://developer.gnome.org/gio/stable/GApplication.html 13 | -- https://developer.gnome.org/gtk3/stable/GtkApplication.html 14 | function app:on_startup() 15 | -- The ::startup signal is called only once, so it's recommended to create/prepare 16 | -- all the GUI here and then present it on the ::activate signal 17 | print("app::startup called") 18 | end 19 | 20 | function app:on_activate() 21 | -- The ::activate signal can be triggered indefinitely, so please try to not do heavy 22 | -- things here. Check out the doc links above for more details 23 | print("app::activate called") 24 | end 25 | 26 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/assistant.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Glib = lgi.require("GLib") 4 | 5 | -- GtkAssistant: a widget for guiding the user through step to achieve something 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.Assistant" 8 | local appTitle = "GtkAssistant" 9 | local appWelcomeText = "Welcome to the GtkAssistant example" 10 | local app = Gtk.Application { application_id = appID } 11 | 12 | local function buildPage2(assistantWindow) 13 | local grid = Gtk.Grid() 14 | local infoLabel = Gtk.Label.new("Fill this form") 15 | local nameLabel = Gtk.Label.new("Full name") 16 | local usernameLabel = Gtk.Label.new("Username") 17 | local passwordLabel = Gtk.Label.new("Password") 18 | local nameEntry = Gtk.Entry() 19 | local usernameEntry = Gtk.Entry() 20 | local passwordEntry = Gtk.Entry { visibility = false, input_purpose = Gtk.InputPurpose.PASSWORD } 21 | local doneButton = Gtk.Button.new_with_label("Done") 22 | 23 | grid.column_spacing = 10 24 | grid.row_spacing = 10 25 | grid.halign = Gtk.Align.CENTER 26 | grid.valign = Gtk.Align.CENTER 27 | grid:attach(infoLabel, 0, 0, 2, 1) 28 | grid:attach(nameLabel, 0, 1, 1, 1) 29 | grid:attach(nameEntry, 1, 1, 1, 1) 30 | grid:attach(usernameLabel, 0, 2, 1, 1) 31 | grid:attach(usernameEntry, 1, 2, 1, 1) 32 | grid:attach(passwordLabel, 0, 3, 1, 1) 33 | grid:attach(passwordEntry, 1, 3, 1, 1) 34 | grid:attach(doneButton, 1, 4, 1, 1) 35 | 36 | function doneButton:on_clicked() 37 | assistantWindow:set_page_complete(grid, true) 38 | end 39 | 40 | return grid 41 | end 42 | 43 | local function buildPage3(assistantWindow) 44 | local progressBar = Gtk.ProgressBar() 45 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 46 | 47 | box.halign = Gtk.Align.CENTER 48 | box.valign = Gtk.Align.CENTER 49 | box:append(Gtk.Label.new("Installing the system...")) 50 | box:append(progressBar) 51 | 52 | progressBar.width_request = 240 53 | progressBar.pulse_step = 0.2 54 | 55 | function assistantWindow:on_apply() 56 | Glib.timeout_add(200, 200, function () 57 | local fraction = progressBar:get_fraction() 58 | 59 | if fraction == 1 or fraction == 1.0 then 60 | assistantWindow:set_page_complete(box, true) 61 | assistantWindow:commit() 62 | return false 63 | elseif fraction < 0 then 64 | progressBar:pulse() 65 | else 66 | progressBar:set_fraction(fraction + 0.1) 67 | end 68 | 69 | return true 70 | end, 1000) 71 | end 72 | 73 | return box 74 | end 75 | 76 | function app:on_activate() 77 | if self.active_window then 78 | self.active_window:present() 79 | end 80 | end 81 | 82 | function app:on_startup() 83 | local window = Gtk.Assistant() 84 | local headerBar = Gtk.HeaderBar() 85 | local page1 = Gtk.Label.new(appWelcomeText) 86 | local page2 = buildPage2(window) 87 | local page3 = buildPage3(window) 88 | local page4 = Gtk.Label.new("System installed successfully!") 89 | 90 | page1.use_markup = true 91 | 92 | self:add_window(window) 93 | 94 | window:set_titlebar(headerBar) 95 | window:set_default_size(800, 400) 96 | window.on_cancel = function() app:quit() end 97 | window.on_close = function() app:quit() end 98 | 99 | window:append_page(page1) 100 | window:append_page(page2) 101 | window:append_page(page3) 102 | window:append_page(page4) 103 | 104 | window:set_page_title(page1, "Welcome") 105 | window:set_page_title(page2, "Create account") 106 | window:set_page_title(page3, "Installing system") 107 | window:set_page_title(page4, "Summary") 108 | 109 | window:set_page_type(page1, 1) 110 | window:set_page_type(page2, 2) 111 | window:set_page_type(page3, 4) 112 | window:set_page_type(page4, 3) 113 | 114 | window:set_page_complete(page1, true) 115 | end 116 | 117 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/box.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkBox: An horizontal or vertical layout container 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.Box" 8 | local app_tile = "GtkBox" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 10 | 11 | function app:on_startup() 12 | local window = Gtk.ApplicationWindow.new(self) 13 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 14 | 15 | window.child = box 16 | window.title = app_tile 17 | window:set_default_size(400, 400) 18 | 19 | box.valign = Gtk.Align.CENTER 20 | box:append(Gtk.Label.new("Label 1")) 21 | box:append(Gtk.Label.new("Label 2")) 22 | end 23 | 24 | function app:on_activate() 25 | self.active_window:present() 26 | end 27 | 28 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/builder-1.lua.in: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkBuilder: Create graphical user interfaces with XML 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.Builder1" 8 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.DEFAULT_FLAGS) 9 | local builder = Gtk.Builder.new_from_file("@builderPath@") 10 | 11 | function app:on_startup() 12 | self:add_window(builder:get_object("window")) 13 | end 14 | 15 | function app:on_activate() 16 | self.active_window:present() 17 | end 18 | 19 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/builder-2.lua.in: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- Another way to use GtkBuilder in LGI 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.Builder2" 8 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.DEFAULT_FLAGS) 9 | local builder = Gtk.Builder.new_from_file("@builderPath@") 10 | local ui = builder.objects 11 | 12 | function app:on_startup() 13 | self:add_window(ui.window) 14 | end 15 | 16 | function app:on_activate() 17 | self.active_window:present() 18 | end 19 | 20 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/button.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkButton: A button widget 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.Button" 8 | local appTitle = "GtkButton" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.DEFAULT_FLAGS) 10 | 11 | function app:on_startup() 12 | local window = Gtk.ApplicationWindow.new(self) 13 | local headerBar = Gtk.HeaderBar.new() 14 | local titleLabel = Gtk.Label.new(appTitle) 15 | local button = Gtk.Button.new_with_label("Click me!") 16 | local count = 1 17 | 18 | window.child = button 19 | window.title = appTitle 20 | window:set_titlebar(headerBar) 21 | window:set_default_size(400, 400) 22 | 23 | headerBar.title_widget = titleLabel 24 | headerBar.show_title_buttons = true 25 | 26 | titleLabel:add_css_class("title") 27 | 28 | button.valign = Gtk.Align.CENTER 29 | button.halign = Gtk.Align.CENTER 30 | 31 | -- The "clicked" signal is emited when the user press the button 32 | function button:on_clicked() 33 | self.label = ("Clicked %d times!"):format(count) 34 | count = count + 1 35 | end 36 | end 37 | 38 | function app:on_activate() 39 | self.active_window:present() 40 | end 41 | 42 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/combo-box-text.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkComboBoxText: Just a text-only GtkComboBox 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.ComboBoxText" 8 | local appTitle = "GtkComboBoxText" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 10 | local items = { "GNOME", "KDE Plasma", "XFCE", "MATE", "Cinnamon", "Pantheon", "LXDE", "LXQT" } 11 | 12 | function app:on_startup() 13 | local window = Gtk.ApplicationWindow.new(self) 14 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 15 | local label = Gtk.Label.new("Default id: 1") 16 | local combo = Gtk.ComboBoxText.new() 17 | 18 | window.child = box 19 | window.title = appTitle 20 | window:set_default_size(400, 400) 21 | 22 | box.halign = Gtk.Align.CENTER 23 | box.valign = Gtk.Align.CENTER 24 | box:append(Gtk.Label.new("Select an option")) 25 | box:append(combo) 26 | box:append(label) 27 | 28 | for id, value in ipairs(items) do 29 | combo:append(id, value) 30 | end 31 | 32 | combo.active_id = 1 33 | 34 | function combo:on_changed() 35 | label.label = "Option id: " .. self:get_active_id() 36 | end 37 | end 38 | 39 | function app:on_activate() 40 | self.active_window:present() 41 | end 42 | 43 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/combo-box.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | local GObject = lgi.GObject 5 | 6 | -- GtkComboBox: A dropdown menu widget 7 | 8 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.ComboBox" 9 | local appTitle = "GtkComboBox" 10 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 11 | local items = { "GNOME", "KDE Plasma", "XFCE", "MATE", "Cinnamon", "Pantheon", "LXDE", "LXQT" } 12 | 13 | function app:on_startup() 14 | local window = Gtk.ApplicationWindow.new(self) 15 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 16 | local model = Gtk.ListStore.new({ GObject.Type.STRING }) 17 | local label = Gtk.Label.new("Default option: 0") 18 | local comboBox = Gtk.ComboBox.new_with_model(model) 19 | 20 | window.child = box 21 | window.title = appTitle 22 | window:set_default_size(400, 400) 23 | 24 | box.halign = Gtk.Align.CENTER 25 | box.valign = Gtk.Align.CENTER 26 | box:append(Gtk.Label.new("Select an option")) 27 | box:append(comboBox) 28 | box:append(label) 29 | 30 | for _, name in ipairs(items) do 31 | model:append({ name }) 32 | end 33 | 34 | comboBox.active = 0 35 | comboBox.cells = { { Gtk.CellRendererText(), { text = 1 }, align = Gtk.Align.START } } 36 | 37 | function comboBox:on_changed() 38 | local n = self:get_active() 39 | label.label = "Option " .. n .. " selected (" .. items[n + 1] .. ")" 40 | end 41 | end 42 | 43 | function app:on_activate() 44 | self.active_window:present() 45 | end 46 | 47 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/css-provider.lua.in: -------------------------------------------------------------------------------- 1 | local lgi = require('lgi') 2 | local Gtk = lgi.require('Gtk', '4.0') 3 | local Gio = lgi.Gio 4 | 5 | -- GtkCssProvider: CSS-like styling class for widgets 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.CssProvider" 8 | local appTitle = "GtkCssProvider" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.DEFAULT_FLAGS) 10 | 11 | function app:on_startup() 12 | local window = Gtk.ApplicationWindow.new(self) 13 | window.title = appTitle 14 | window.default_width = 400 15 | window.default_height = 400 16 | 17 | local provider = Gtk.CssProvider() 18 | provider:load_from_path("@cssPath@") 19 | 20 | local display = window:get_display() 21 | Gtk.StyleContext.add_provider_for_display(display, provider, 600) 22 | 23 | local label = Gtk.Label.new("Hi there!") 24 | local button = Gtk.Button.new_with_label("Toggle label style") 25 | 26 | function button:on_clicked() 27 | if label:has_css_class("color-red") then 28 | label:remove_css_class("color-red") 29 | else 30 | label:add_css_class("color-red") 31 | end 32 | end 33 | 34 | local box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 10) 35 | box.halign = Gtk.Align.CENTER 36 | box.valign = Gtk.Align.CENTER 37 | box:append(label) 38 | box:append(button) 39 | 40 | window.child = box 41 | end 42 | 43 | function app:on_activate() 44 | self.active_window:present() 45 | end 46 | 47 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/header-bar.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.HeaderBar" 6 | local appTitle = "GtkHeaderBar" 7 | local appSubtitle = "App subtitle" 8 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 9 | 10 | function app:on_startup() 11 | local window = Gtk.ApplicationWindow.new(self) 12 | local titleLabel = Gtk.Label.new(appTitle) 13 | local subtitleLabel = Gtk.Label.new(appSubtitle) 14 | local titleBox = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0) 15 | local headerBar = Gtk.HeaderBar() 16 | 17 | window.titlebar = headerBar 18 | window:set_default_size(400, 400) 19 | 20 | titleLabel:add_css_class("title") 21 | subtitleLabel:add_css_class("subtitle") 22 | 23 | titleBox.valign = Gtk.Align.CENTER 24 | titleBox:append(titleLabel) 25 | titleBox:append(subtitleLabel) 26 | 27 | headerBar.title_widget = titleBox 28 | headerBar.show_title_buttons = true 29 | end 30 | 31 | function app:on_activate() 32 | self.active_window:present() 33 | end 34 | 35 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/label.lua: -------------------------------------------------------------------------------- 1 | local lgi = require("lgi") 2 | local Gtk = lgi.require("Gtk", "4.0") 3 | local Gio = lgi.Gio 4 | 5 | -- GtkLabel: A text widget 6 | 7 | local appID = "io.github.Miqueas.GTK-Examples.Lua.Gtk4.Label" 8 | local appTitle = "GtkLabel" 9 | local app = Gtk.Application.new(appID, Gio.ApplicationFlags.FLAGS_NONE) 10 | 11 | function app:on_startup() 12 | local window = Gtk.ApplicationWindow.new(self) 13 | local label = Gtk.Label.new("Hi there!") 14 | 15 | window.child = label 16 | window.title = appTitle 17 | window:set_default_size(400, 400) 18 | 19 | label.valign = Gtk.Align.CENTER 20 | label.halign = Gtk.Align.CENTER 21 | end 22 | 23 | function app:on_activate() 24 | self.active_window:present() 25 | end 26 | 27 | return app:run(arg) -------------------------------------------------------------------------------- /lua/gtk4/meson.build: -------------------------------------------------------------------------------- 1 | configure_file( 2 | input: 'builder-1.lua.in', 3 | output: 'builder-1.lua', 4 | configuration: builder_gtk4_ui 5 | ) 6 | 7 | configure_file( 8 | input: 'builder-2.lua.in', 9 | output: 'builder-2.lua', 10 | configuration: builder_gtk4_ui 11 | ) 12 | 13 | configure_file( 14 | input: 'css-provider.lua.in', 15 | output: 'css-provider.lua', 16 | configuration: css_conf 17 | ) -------------------------------------------------------------------------------- /meson.build: -------------------------------------------------------------------------------- 1 | project('GTK-Examples', [ 'c', 'vala' ], 2 | version: '0.1.0', 3 | license: 'Zlib', 4 | meson_version: '>= 0.56.0' 5 | ) 6 | 7 | # Allows using the 'with' keyword in Vala without warnings 8 | add_project_arguments('--enable-experimental', language: 'vala') 9 | 10 | gtk3 = dependency('gtk+-3.0', required: true) 11 | gtk4 = dependency('gtk4', required: true) 12 | 13 | if build_machine.system() == 'linux' 14 | glew = dependency('glew', required: true) 15 | else 16 | message('Skipping glew dependency on non-linux systems') 17 | endif 18 | 19 | 20 | gnome = import('gnome') 21 | resources = gnome.compile_resources('resources', 'data/resources.xml', source_dir: 'data') 22 | 23 | builder_gtk3_ui = { 'builderPath': meson.project_source_root() / 'data' / 'builder-gtk3.ui' } 24 | builder_gtk4_ui = { 'builderPath': meson.project_source_root() / 'data' / 'builder-gtk4.ui' } 25 | css_conf = { 'cssPath': meson.project_source_root() / 'data' / 'css-provider.css' } 26 | image_conf = { 'imagePath': meson.project_source_root() / 'data' / 'grid-explained.png' } 27 | 28 | subdir('c/gtk3') 29 | subdir('c/gtk4') 30 | subdir('lua/gtk3') 31 | subdir('lua/gtk4') 32 | subdir('vala/gtk3') 33 | subdir('vala/gtk4') -------------------------------------------------------------------------------- /vala/gtk3/action-bar.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.ActionBar"; 2 | const string appTitle = "GtkActionBar"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var headerBar = new Gtk.HeaderBar(); 19 | var actionBar = new Gtk.ActionBar(); 20 | var actionBarLabel = new Gtk.Label("Some information"); 21 | var actionBarButton = new Gtk.Button.with_label("A button"); 22 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); 23 | var boxLabel = new Gtk.Label("App content"); 24 | 25 | with (window) { 26 | set_titlebar(headerBar); 27 | set_default_size(400, 400); 28 | add(box); 29 | } 30 | 31 | with (headerBar) { 32 | visible = true; 33 | title = appTitle; 34 | show_close_button = true; 35 | } 36 | 37 | with (actionBar) { 38 | pack_start(actionBarLabel); 39 | pack_end(actionBarButton); 40 | } 41 | 42 | with (box) { 43 | pack_start(boxLabel, true, true, 0); 44 | pack_end(actionBar, false, true, 0); 45 | show_all(); 46 | } 47 | } -------------------------------------------------------------------------------- /vala/gtk3/application-window.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.ApplicationWindow"; 2 | const string appTitle = "GtkApplicationWindow"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var headerBar = new Gtk.HeaderBar(); 19 | 20 | with (window) { 21 | set_titlebar(headerBar); 22 | set_default_size(400, 400); 23 | } 24 | 25 | with (headerBar) { 26 | visible = true; 27 | title = appTitle; 28 | show_close_button = true; 29 | } 30 | } -------------------------------------------------------------------------------- /vala/gtk3/application.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Application"; 2 | 3 | int main(string[] args) { 4 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 5 | app.startup.connect(onAppStartup); 6 | app.activate.connect(onAppActivate); 7 | return app.run(args); 8 | } 9 | 10 | void onAppActivate() { 11 | print("Hello there!\n"); 12 | } 13 | 14 | void onAppStartup() { 15 | print("Initializing... Done!\n"); 16 | } -------------------------------------------------------------------------------- /vala/gtk3/assistant.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Assistant"; 2 | const string appTitle = "GtkAssistant"; 3 | const string appWelcomeText = "Welcome to the GtkAssistant example"; 4 | 5 | int main(string[] args) { 6 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 7 | app.startup.connect(onAppStartup); 8 | app.activate.connect(onAppActivate); 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.Assistant(); 19 | var headerBar = new Gtk.HeaderBar(); 20 | var page1 = new Gtk.Label(appWelcomeText) { visible = true, use_markup = true }; 21 | var page2 = doPage2(window); 22 | var page3 = doPage3(window); 23 | var page4 = new Gtk.Label("System installed successfully!") { visible = true }; 24 | 25 | (self as Gtk.Application)?.add_window(window); 26 | 27 | with (window) { 28 | set_titlebar(headerBar); 29 | set_default_size(800, 400); 30 | cancel.connect(destroy); 31 | close.connect(destroy); 32 | 33 | append_page(page1); 34 | append_page(page2); 35 | append_page(page3); 36 | append_page(page4); 37 | 38 | set_page_title(page1, "Welcome"); 39 | set_page_title(page2, "Create account"); 40 | set_page_title(page3, "Installing system"); 41 | set_page_title(page4, "Summary"); 42 | 43 | set_page_type(page1, Gtk.AssistantPageType.INTRO); 44 | set_page_type(page2, Gtk.AssistantPageType.CONFIRM); 45 | set_page_type(page3, Gtk.AssistantPageType.PROGRESS); 46 | set_page_type(page4, Gtk.AssistantPageType.SUMMARY); 47 | 48 | set_page_complete(page1, true); 49 | } 50 | 51 | with (headerBar) { 52 | visible = true; 53 | subtitle = appTitle; 54 | show_close_button = true; 55 | } 56 | } 57 | 58 | Gtk.Grid doPage2(Gtk.Assistant assistant) { 59 | var grid = new Gtk.Grid(); 60 | var infoLabel = new Gtk.Label("Fill this form"); 61 | var nameLabel = new Gtk.Label("Full name"); 62 | var usernameLabel = new Gtk.Label("Username"); 63 | var passwordLabel = new Gtk.Label("Password"); 64 | var nameEntry = new Gtk.Entry(); 65 | var usernameEntry = new Gtk.Entry(); 66 | var passwordEntry = new Gtk.Entry(); 67 | var doneButton = new Gtk.Button.with_label("Done"); 68 | 69 | with (grid) { 70 | column_spacing = 10; 71 | row_spacing = 10; 72 | halign = Gtk.Align.CENTER; 73 | valign = Gtk.Align.CENTER; 74 | attach(infoLabel, 0, 0, 2, 1); 75 | attach(nameLabel, 0, 1, 1, 1); 76 | attach(nameEntry, 1, 1, 1, 1); 77 | attach(usernameLabel, 0, 2, 1, 1); 78 | attach(usernameEntry, 1, 2, 1, 1); 79 | attach(passwordLabel, 0, 3, 1, 1); 80 | attach(passwordEntry, 1, 3, 1, 1); 81 | attach(doneButton, 1, 4, 1, 1); 82 | show_all(); 83 | } 84 | 85 | passwordEntry.visibility = false; 86 | passwordEntry.input_purpose = Gtk.InputPurpose.PASSWORD; 87 | 88 | doneButton.clicked.connect(self => assistant.set_page_complete(grid, true)); 89 | 90 | return grid; 91 | } 92 | 93 | Gtk.Box doPage3(Gtk.Assistant assistant) { 94 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 95 | var progressBar = new Gtk.ProgressBar(); 96 | 97 | with (box) { 98 | halign = Gtk.Align.CENTER; 99 | valign = Gtk.Align.CENTER; 100 | pack_start(new Gtk.Label("Installing the system..."), false, false, 0); 101 | pack_start(progressBar, false, false, 0); 102 | show_all(); 103 | } 104 | 105 | with (progressBar) { 106 | width_request = 240; 107 | pulse_step = 0.2; 108 | } 109 | 110 | assistant.apply.connect(self => Timeout.add(200, () => { 111 | var fraction = progressBar.get_fraction(); 112 | 113 | if (fraction == 1 || fraction == 1.0) { 114 | assistant.set_page_complete(box, true); 115 | assistant.commit(); 116 | return Source.REMOVE; 117 | } else if (fraction < 0) { 118 | progressBar.pulse(); 119 | } else { 120 | progressBar.set_fraction(fraction + 0.1); 121 | } 122 | 123 | return Source.CONTINUE; 124 | })); 125 | 126 | return box; 127 | } -------------------------------------------------------------------------------- /vala/gtk3/box.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Box"; 2 | const string appTitle = "GtkBox"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var headerBar = new Gtk.HeaderBar(); 19 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 20 | 21 | with (window) { 22 | add(box); 23 | set_titlebar(headerBar); 24 | set_default_size(400, 400); 25 | } 26 | 27 | with (headerBar) { 28 | visible = true; 29 | title = appTitle; 30 | show_close_button = true; 31 | } 32 | 33 | with (box) { 34 | halign = Gtk.Align.CENTER; 35 | valign = Gtk.Align.CENTER; 36 | pack_start(new Gtk.Label("A label"), false, false, 0); 37 | pack_start(new Gtk.Button.with_label("A button"), false, false, 0); 38 | show_all(); 39 | } 40 | } -------------------------------------------------------------------------------- /vala/gtk3/builder.vala.in: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Builder"; 2 | 3 | int main(string[] args) { 4 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 5 | app.startup.connect(onAppStartup); 6 | app.activate.connect(onAppActivate); 7 | return app.run(args); 8 | } 9 | 10 | void onAppActivate(Application self) { 11 | var window = (self as Gtk.Application)?.get_active_window(); 12 | window?.present(); 13 | } 14 | 15 | void onAppStartup(Application self) { 16 | var builder = new Gtk.Builder.from_file("@builderPath@"); 17 | var window = builder.get_object("window"); 18 | (self as Gtk.Application)?.add_window(window as Gtk.ApplicationWindow); 19 | } -------------------------------------------------------------------------------- /vala/gtk3/button.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Button"; 2 | const string appTitle = "GtkButton"; 3 | static int count = 0; 4 | 5 | int main(string[] args) { 6 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 7 | app.startup.connect(onAppStartup); 8 | app.activate.connect(onAppActivate); 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | var headerBar = new Gtk.HeaderBar(); 20 | var button = new Gtk.Button.with_label("Click me!"); 21 | 22 | with (window) { 23 | add(button); 24 | set_titlebar(headerBar); 25 | set_default_size(400, 400); 26 | } 27 | 28 | with (headerBar) { 29 | visible = true; 30 | title = appTitle; 31 | show_close_button = true; 32 | } 33 | 34 | with (button) { 35 | visible = true; 36 | valign = Gtk.Align.CENTER; 37 | halign = Gtk.Align.CENTER; 38 | clicked.connect(onButtonClicked); 39 | } 40 | } 41 | 42 | void onButtonClicked(Gtk.Button self) { 43 | print("You clicked %d times!\n", ++count); 44 | } -------------------------------------------------------------------------------- /vala/gtk3/combo-box-text.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.ComboBoxText"; 2 | const string appTitle = "GtkActionBar"; 3 | const string[] comboBoxTextValues = { 4 | "GNOME", "Plasma", 5 | "Pantheon", "XFCE", 6 | "MATE", "Cinnamon", 7 | "LXDE", "LXQT", 8 | }; 9 | 10 | int main(string[] args) { 11 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 12 | app.startup.connect(onAppStartup); 13 | app.activate.connect(onAppActivate); 14 | return app.run(args); 15 | } 16 | 17 | void onAppActivate(Application self) { 18 | var window = (self as Gtk.Application)?.get_active_window(); 19 | window?.present(); 20 | } 21 | 22 | void onAppStartup(Application self) { 23 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 24 | var headerBar = new Gtk.HeaderBar(); 25 | var comboBox = new Gtk.ComboBoxText(); 26 | var hintLabel = new Gtk.Label("Default id: gnome"); 27 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 28 | 29 | with (window) { 30 | add(box); 31 | set_titlebar(headerBar); 32 | set_default_size(400, 400); 33 | } 34 | 35 | with (headerBar) { 36 | visible = true; 37 | title = appTitle; 38 | show_close_button = true; 39 | } 40 | 41 | with (comboBox) { 42 | foreach (var name in comboBoxTextValues) append(name.down(), name); 43 | set_active_id("gnome"); 44 | changed.connect(self => hintLabel.label = @"Option id: $(self.get_active_id())"); 45 | } 46 | 47 | with (box) { 48 | halign = Gtk.Align.CENTER; 49 | valign = Gtk.Align.CENTER; 50 | pack_start(new Gtk.Label("Select an option"), false, true, 0); 51 | pack_start(comboBox, false, true, 0); 52 | pack_start(hintLabel, false, true, 0); 53 | show_all(); 54 | } 55 | } -------------------------------------------------------------------------------- /vala/gtk3/dialog.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Dialog"; 2 | const string appTitle = "GtkDialog"; 3 | const string titleText = "Universe destruction"; 4 | const string summaryText = "Our universe has a lot of problems and the only way to fix it is destroying the entire universe and this important decision is now in your hands."; 5 | const string epilogText = "Do you accept?"; 6 | 7 | int main(string[] args) { 8 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 9 | app.startup.connect(onAppStartup); 10 | app.activate.connect(onAppActivate); 11 | return app.run(args); 12 | } 13 | 14 | void onAppActivate(Application self) { 15 | var window = (self as Gtk.Application)?.get_active_window() as Gtk.Dialog; 16 | var response = window?.run(); 17 | window?.destroy(); 18 | 19 | switch (response) { 20 | case Gtk.ResponseType.OK: 21 | print("Universe destroyed! 💥\n"); 22 | break; 23 | case Gtk.ResponseType.CANCEL: 24 | print("Universe is in peace now! 🙏\n"); 25 | break; 26 | default: 27 | print("Nothing happens! 🤔\n"); 28 | break; 29 | } 30 | } 31 | 32 | void onAppStartup(Application self) { 33 | var dialog = new Gtk.Dialog.with_buttons(appTitle, null, Gtk.DialogFlags.MODAL, 34 | "Yes 😈👍", Gtk.ResponseType.OK, 35 | "No 💀🤚", Gtk.ResponseType.CANCEL 36 | ); 37 | 38 | var titleLabel = new Gtk.Label(titleText); 39 | var summaryLabel = new Gtk.Label(summaryText); 40 | var epilogLabel = new Gtk.Label(epilogText); 41 | var contentBox = dialog.get_content_area(); 42 | 43 | with (dialog) { 44 | title = appTitle; 45 | application = self as Gtk.Application; 46 | default_width = 400; 47 | border_width = 10; 48 | } 49 | 50 | titleLabel.use_markup = true; 51 | 52 | with (summaryLabel) { 53 | xalign = 0; 54 | wrap = true; 55 | wrap_mode = Pango.WrapMode.CHAR; 56 | } 57 | 58 | epilogLabel.use_markup = true; 59 | 60 | with (contentBox) { 61 | spacing = 10; 62 | pack_start(titleLabel, false); 63 | pack_start(summaryLabel, false); 64 | pack_start(epilogLabel, false, true, 10); 65 | show_all(); 66 | } 67 | } -------------------------------------------------------------------------------- /vala/gtk3/entry-completion.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.EntryCompletion"; 2 | const string appTitle = "GtkEntry"; 3 | const string[] items = { "GNOME", "Vala", "GTK", "Example", "Entry", "Completion" }; 4 | 5 | int main(string[] args) { 6 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 7 | app.startup.connect(onAppStartup); 8 | app.activate.connect(onAppActivate); 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | var headerBar = new Gtk.HeaderBar(); 20 | var entryCompletionModel = new Gtk.ListStore(1, typeof(string)); 21 | var entryCompletion = new Gtk.EntryCompletion(); 22 | var entry = new Gtk.Entry(); 23 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 24 | Gtk.TreeIter iter; 25 | 26 | with (window) { 27 | add(box); 28 | set_titlebar(headerBar); 29 | set_default_size(400, 400); 30 | } 31 | 32 | with (headerBar) { 33 | visible = true; 34 | title = appTitle; 35 | show_close_button = true; 36 | } 37 | 38 | foreach (var name in items) { 39 | entryCompletionModel.append(out iter); 40 | entryCompletionModel.set(iter, 0, name); 41 | } 42 | 43 | with (entryCompletion) { 44 | model = entryCompletionModel; 45 | text_column = 0; 46 | popup_completion = true; 47 | } 48 | 49 | entry.completion = entryCompletion; 50 | 51 | with (box) { 52 | halign = Gtk.Align.CENTER; 53 | valign = Gtk.Align.CENTER; 54 | pack_start(new Gtk.Label("Try typing \"gnome\" or \"gtk\""), false); 55 | pack_start(entry, false); 56 | show_all(); 57 | } 58 | } -------------------------------------------------------------------------------- /vala/gtk3/entry.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Entry"; 2 | const string appTitle = "GtkEntry"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var headerBar = new Gtk.HeaderBar(); 19 | var label = new Gtk.Label(""); 20 | var entry = new Gtk.Entry(); 21 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 22 | 23 | with (window) { 24 | add(box); 25 | set_titlebar(headerBar); 26 | set_default_size(400, 400); 27 | } 28 | 29 | with (headerBar) { 30 | visible = true; 31 | title = appTitle; 32 | show_close_button = true; 33 | } 34 | 35 | entry.key_release_event.connect(() => { label.label = entry.text; return true; }); 36 | 37 | with (box) { 38 | halign = Gtk.Align.CENTER; 39 | valign = Gtk.Align.CENTER; 40 | pack_start(new Gtk.Label("Enter some text"), false); 41 | pack_start(entry, false); 42 | pack_start(label, false); 43 | show_all(); 44 | } 45 | } -------------------------------------------------------------------------------- /vala/gtk3/fixed.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Fixed"; 2 | const string appTitle = "GtkFixed"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var headerBar = new Gtk.HeaderBar(); 19 | var fixed = new Gtk.Fixed(); 20 | 21 | with (window) { 22 | add(fixed); 23 | set_titlebar(headerBar); 24 | set_default_size(400, 400); 25 | } 26 | 27 | with (headerBar) { 28 | visible = true; 29 | title = appTitle; 30 | show_close_button = true; 31 | } 32 | 33 | with (fixed) { 34 | visible = true; 35 | put(new Gtk.Label("A") { visible = true }, 10, 20); 36 | put(new Gtk.Label("B") { visible = true }, 100, 200); 37 | put(new Gtk.Label("C") { visible = true }, 120, 326); 38 | } 39 | } -------------------------------------------------------------------------------- /vala/gtk3/flow-box.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.FlowBox"; 2 | const string appTitle = "GtkFlowBox"; 3 | const string[] icons = { 4 | "face-angel", 5 | "face-angry", 6 | "face-surprise", 7 | "face-laugh", 8 | "face-plain", 9 | "face-sad", 10 | "face-cool", 11 | "face-smirk", 12 | "face-sick", 13 | "face-kiss", 14 | "face-heart-broken", 15 | "face-smile", 16 | "face-crying", 17 | "face-devilish", 18 | "face-heart", 19 | "face-sad", 20 | "face-smile-big", 21 | "face-tired", 22 | "face-wink", 23 | "face-worried" 24 | }; 25 | 26 | int main(string[] args) { 27 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 28 | app.startup.connect(onAppStartup); 29 | app.activate.connect(onAppActivate); 30 | return app.run(args); 31 | } 32 | 33 | void onAppActivate(Application self) { 34 | var window = (self as Gtk.Application)?.get_active_window(); 35 | window?.present(); 36 | } 37 | 38 | void onAppStartup(Application self) { 39 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 40 | var headerBar = new Gtk.HeaderBar(); 41 | var flowBox = new Gtk.FlowBox(); 42 | var scrolledWindow = new Gtk.ScrolledWindow(null, null); 43 | 44 | with (window) { 45 | add(scrolledWindow); 46 | set_titlebar(headerBar); 47 | set_default_size(400, 400); 48 | } 49 | 50 | with (headerBar) { 51 | visible = true; 52 | title = appTitle; 53 | show_close_button = true; 54 | } 55 | 56 | with (flowBox) { 57 | set_selection_mode(Gtk.SelectionMode.NONE); 58 | set_max_children_per_line(30); 59 | 60 | for (var i = 0; i < 1000; i++) 61 | insert(new Gtk.Image.from_icon_name(icons[Random.int_range(0, 20)], Gtk.IconSize.DIALOG), i); 62 | } 63 | 64 | with (scrolledWindow) { 65 | add(flowBox); 66 | set_policy(Gtk.PolicyType.NEVER, Gtk.PolicyType.AUTOMATIC); 67 | show_all(); 68 | } 69 | } -------------------------------------------------------------------------------- /vala/gtk3/header-bar.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.HeaderBar"; 2 | const string appTitle = "GtkHeaderBar"; 3 | const string appSubtitle = "App subtitle"; 4 | 5 | int main(string[] args) { 6 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 7 | app.startup.connect(onAppStartup); 8 | app.activate.connect(onAppActivate); 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | var headerBar = new Gtk.HeaderBar(); 20 | 21 | with (window) { 22 | set_titlebar(headerBar); 23 | set_default_size(400, 400); 24 | } 25 | 26 | with (headerBar) { 27 | visible = true; 28 | title = appTitle; 29 | subtitle = appSubtitle; 30 | show_close_button = true; 31 | } 32 | } -------------------------------------------------------------------------------- /vala/gtk3/label.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.Label"; 2 | const string appTitle = "GtkLabel"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var headerBar = new Gtk.HeaderBar(); 19 | var label = new Gtk.Label("Hi there!") { visible = true }; 20 | 21 | with (window) { 22 | add(label); 23 | set_titlebar(headerBar); 24 | set_default_size(400, 400); 25 | } 26 | 27 | with (headerBar) { 28 | visible = true; 29 | title = appTitle; 30 | show_close_button = true; 31 | } 32 | } -------------------------------------------------------------------------------- /vala/gtk3/list-box.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.ListBox"; 2 | const string appTitle = "GtkListBox"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var headerBar = new Gtk.HeaderBar(); 19 | var listBox = new Gtk.ListBox(); 20 | var scrolledWindow = new Gtk.ScrolledWindow(null, null); 21 | var button = new Gtk.Button.with_label("Load"); 22 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 23 | 24 | with (window) { 25 | border_width = 10; 26 | add(box); 27 | set_titlebar(headerBar); 28 | set_default_size(400, 400); 29 | } 30 | 31 | with (headerBar) { 32 | visible = true; 33 | title = appTitle; 34 | show_close_button = true; 35 | } 36 | 37 | with (scrolledWindow) { 38 | shadow_type = Gtk.ShadowType.NONE; 39 | propagate_natural_width = true; 40 | propagate_natural_height = true; 41 | add(listBox); 42 | } 43 | 44 | with (button) { 45 | halign = Gtk.Align.CENTER; 46 | valign = Gtk.Align.CENTER; 47 | clicked.connect(self => { 48 | for (int i = 0; i < 100; i++) 49 | listBox.insert(new Gtk.Label(@"Text $(i + 1)") { visible = true }, i); 50 | }); 51 | } 52 | 53 | with (box) { 54 | pack_start(scrolledWindow, true, true, 0); 55 | pack_start(button, false, false, 0); 56 | show_all(); 57 | } 58 | } -------------------------------------------------------------------------------- /vala/gtk3/meson.build: -------------------------------------------------------------------------------- 1 | src = [ 2 | 'action-bar', 3 | 'application', 4 | 'application-window', 5 | 'assistant', 6 | 'box', 7 | 'builder', 8 | 'button', 9 | 'combo-box-text', 10 | 'dialog', 11 | 'entry-completion', 12 | 'entry', 13 | 'header-bar', 14 | 'label', 15 | 'list-box', 16 | 'fixed', 17 | 'flow-box', 18 | 'message-dialog', 19 | 'template', 20 | 'window', 21 | ] 22 | 23 | builder = configure_file( 24 | input: 'builder.vala.in', 25 | output: 'builder.vala', 26 | configuration: builder_gtk3_ui 27 | ) 28 | 29 | foreach name : src 30 | if name == 'builder' 31 | executable(name, builder, dependencies: gtk3) 32 | elif name == 'template' 33 | executable(name, [name + '.vala', resources], dependencies: gtk3) 34 | else 35 | executable(name, name + '.vala', dependencies: gtk3) 36 | endif 37 | endforeach -------------------------------------------------------------------------------- /vala/gtk3/message-dialog.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk3.MessageDialog"; 2 | const string appTitle = "GtkMessageDialog"; 3 | const string titleText = "Universe destruction"; 4 | const string summaryText = "Our universe has a lot of problems and the only way to fix it is destroying the entire universe and this important decision is now in your hands."; 5 | const string epilogText = "Do you accept?"; 6 | 7 | int main(string[] args) { 8 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 9 | app.startup.connect(onAppStartup); 10 | app.activate.connect(onAppActivate); 11 | return app.run(args); 12 | } 13 | 14 | void onAppActivate(Application self) { 15 | var window = (self as Gtk.Application)?.get_active_window() as Gtk.MessageDialog; 16 | var response = window?.run(); 17 | window?.destroy(); 18 | 19 | switch (response) { 20 | case Gtk.ResponseType.OK: 21 | print("Universe destroyed! 💥\n"); 22 | break; 23 | case Gtk.ResponseType.CANCEL: 24 | print("Universe is in peace now! 🙏\n"); 25 | break; 26 | default: 27 | print("Nothing happens! 🤔\n"); 28 | break; 29 | } 30 | } 31 | 32 | void onAppStartup(Application self) { 33 | var messageDialog = new Gtk.MessageDialog( 34 | null, 35 | Gtk.DialogFlags.MODAL, 36 | Gtk.MessageType.QUESTION, 37 | Gtk.ButtonsType.NONE, 38 | null 39 | ); 40 | 41 | with (messageDialog) { 42 | title = appTitle; 43 | text = titleText; 44 | use_markup = true; 45 | secondary_text = summaryText; 46 | application = self as Gtk.Application; 47 | add_button("Yes 👍", Gtk.ResponseType.OK); 48 | add_button("No 🛑", Gtk.ResponseType.CANCEL); 49 | } 50 | } -------------------------------------------------------------------------------- /vala/gtk3/template.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Gtk3.Template"; 2 | const string[] greetings = { "Hello!", "¡Hola!", "Bonjour!", "Ciao!", "こんにちは!" }; 3 | 4 | int main(string[] args) { 5 | return new App().run(args); 6 | } 7 | 8 | class App : Gtk.Application { 9 | public App() { 10 | Object( 11 | application_id: appID, 12 | flags: ApplicationFlags.FLAGS_NONE, 13 | resource_base_path: "/io/github/Miqueas/GTK-Examples" 14 | ); 15 | } 16 | 17 | public override void activate() { 18 | this.active_window.present(); 19 | } 20 | 21 | public override void startup() { 22 | base.startup(); 23 | new Window(this); 24 | } 25 | } 26 | 27 | [GtkTemplate (ui = "/io/github/Miqueas/GTK-Examples/template.ui")] 28 | class Window : Gtk.ApplicationWindow { 29 | [GtkChild] 30 | public unowned Gtk.Label label; 31 | 32 | public Window(App app) { 33 | Object(application: app); 34 | } 35 | 36 | public override void constructed() { 37 | Timeout.add(1000, () => { 38 | this.label.label = greetings[Random.int_range(0, 5)]; 39 | return Source.CONTINUE; 40 | }); 41 | } 42 | } -------------------------------------------------------------------------------- /vala/gtk3/window.vala: -------------------------------------------------------------------------------- 1 | const string appTitle = "GtkWindow"; 2 | 3 | void main(string[] args) { 4 | Gtk.init(ref args); 5 | 6 | var window = new Gtk.Window(); 7 | var headerBar = new Gtk.HeaderBar(); 8 | 9 | with (headerBar) { 10 | visible = true; 11 | title = appTitle; 12 | show_close_button = true; 13 | } 14 | 15 | with (window) { 16 | set_titlebar(headerBar); 17 | set_default_size(400, 400); 18 | destroy.connect(Gtk.main_quit); 19 | present(); 20 | } 21 | 22 | Gtk.main(); 23 | } -------------------------------------------------------------------------------- /vala/gtk4/action-bar.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.ActionBar"; 2 | const string appTitle = "GtkActionBar"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate(Application self) { 12 | var window = (self as Gtk.Application)?.get_active_window(); 13 | window?.present(); 14 | } 15 | 16 | void onAppStartup(Application self) { 17 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 18 | var actionBar = new Gtk.ActionBar(); 19 | var actionBarLabel = new Gtk.Label("Some information"); 20 | var actionBarButton = new Gtk.Button.with_label("A button"); 21 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); 22 | var boxLabel = new Gtk.Label("App content"); 23 | var headerBar = new Gtk.HeaderBar(); 24 | var titleLabel = new Gtk.Label(appTitle); 25 | 26 | with (window) { 27 | child = box; 28 | set_titlebar(headerBar); 29 | set_default_size(400, 400); 30 | } 31 | 32 | with (boxLabel) { 33 | halign = Gtk.Align.CENTER; 34 | valign = Gtk.Align.CENTER; 35 | hexpand = true; 36 | vexpand = true; 37 | } 38 | 39 | with (headerBar) { 40 | title_widget = titleLabel; 41 | show_title_buttons = true; 42 | } 43 | 44 | with (actionBar) { 45 | pack_start(actionBarLabel); 46 | pack_end(actionBarButton); 47 | } 48 | 49 | with (box) { 50 | append(boxLabel); 51 | append(actionBar); 52 | } 53 | } -------------------------------------------------------------------------------- /vala/gtk4/application-window.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.ApplicationWindow"; 2 | const string appTitle = "GtkApplication"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | window.title = appTitle; 20 | window.set_default_size(400, 400); 21 | } -------------------------------------------------------------------------------- /vala/gtk4/application.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.Application"; 2 | 3 | int main(string[] args) { 4 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 5 | app.startup.connect(onAppStartup); 6 | app.activate.connect(onAppActivate); 7 | 8 | return app.run(args); 9 | } 10 | 11 | void onAppActivate() { 12 | print("Hello there!\n"); 13 | } 14 | 15 | void onAppStartup() { 16 | print("Initializing... Done!\n"); 17 | } -------------------------------------------------------------------------------- /vala/gtk4/assistant.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.Assistant"; 2 | const string appTitle = "GtkAssistant"; 3 | const string deprecationTitleText = "This widget is deprecated!"; 4 | const string deprecationMessageText = "Since GTK 4.10, the GtkAssistant widget is deprecated, you should not use it anymore"; 5 | 6 | int main(string[] args) { 7 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 8 | app.startup.connect(onAppStartup); 9 | app.activate.connect(onAppActivate); 10 | return app.run(args); 11 | } 12 | 13 | void onAppActivate(Application self) { 14 | var window = (self as Gtk.Application)?.get_active_window(); 15 | window?.present(); 16 | } 17 | 18 | void onAppStartup(Application self) { 19 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 20 | var titleLabel = new Gtk.Label(appTitle); 21 | var headerBar = new Gtk.HeaderBar(); 22 | var deprecationTitleLabel = new Gtk.Label(deprecationTitleText); 23 | var deprecationMessageLabel = new Gtk.Label(deprecationMessageText); 24 | var mainBox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); 25 | 26 | with (window) { 27 | child = mainBox; 28 | titlebar = headerBar; 29 | set_default_size(320, 160); 30 | } 31 | 32 | titleLabel.add_css_class("title"); 33 | 34 | with (headerBar) { 35 | title_widget = titleLabel; 36 | show_title_buttons = true; 37 | } 38 | 39 | deprecationTitleLabel.use_markup = true; 40 | 41 | with (deprecationMessageLabel) { 42 | max_width_chars = 30; 43 | wrap = true; 44 | wrap_mode = Pango.WrapMode.WORD; 45 | } 46 | 47 | with (mainBox) { 48 | valign = Gtk.Align.CENTER; 49 | halign = Gtk.Align.CENTER; 50 | width_request = 200; 51 | append(deprecationTitleLabel); 52 | append(deprecationMessageLabel); 53 | } 54 | } -------------------------------------------------------------------------------- /vala/gtk4/box.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.Box"; 2 | const string appTitle = "GtkBox"; 3 | static int count = 0; 4 | 5 | int main(string[] args) { 6 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 7 | app.startup.connect(onAppStartup); 8 | app.activate.connect(onAppActivate); 9 | 10 | return app.run(args); 11 | } 12 | 13 | void onAppActivate(Application self) { 14 | var window = (self as Gtk.Application)?.get_active_window(); 15 | window?.present(); 16 | } 17 | 18 | void onAppStartup(Application self) { 19 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 20 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 21 | var label = new Gtk.Label("Click the button"); 22 | var button = new Gtk.Button.with_label("🤔"); 23 | 24 | window.child = box; 25 | window.title = appTitle; 26 | window.set_default_size(400, 400); 27 | 28 | box.halign = Gtk.Align.CENTER; 29 | box.valign = Gtk.Align.CENTER; 30 | box.append(label); 31 | box.append(button); 32 | 33 | button.halign = Gtk.Align.CENTER; 34 | button.valign = Gtk.Align.CENTER; 35 | button.clicked.connect(onButtonClicked); 36 | } 37 | 38 | void onButtonClicked(Gtk.Button self) { 39 | print("You clicked %d times!\n", ++count); 40 | } -------------------------------------------------------------------------------- /vala/gtk4/builder.vala.in: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.Builder"; 2 | 3 | int main(string[] args) { 4 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 5 | app.startup.connect(onAppStartup); 6 | app.activate.connect(onAppActivate); 7 | return app.run(args); 8 | } 9 | 10 | void onAppActivate(Application self) { 11 | var window = (self as Gtk.Application)?.get_active_window(); 12 | window?.present(); 13 | } 14 | 15 | void onAppStartup(Application self) { 16 | var builder = new Gtk.Builder.from_file("@builderPath@"); 17 | var window = builder.get_object("window"); 18 | (self as Gtk.Application)?.add_window(window as Gtk.ApplicationWindow); 19 | } -------------------------------------------------------------------------------- /vala/gtk4/css-provider.vala.in: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.CssProvider"; 2 | const string appTitle = "GtkCssProvider"; 3 | const string appSubtitle = "App subtitle"; 4 | 5 | int main(string[] args) { 6 | var app = new Gtk.Application(appID, ApplicationFlags.DEFAULT_FLAGS); 7 | app.startup.connect(onAppStartup); 8 | app.activate.connect(onAppActivate); 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | window.title = appTitle; 20 | window.default_width = 400; 21 | window.default_height = 400; 22 | 23 | var provider = new Gtk.CssProvider(); 24 | provider.load_from_path("@cssPath@"); 25 | 26 | var display = window.get_display(); 27 | Gtk.StyleContext.add_provider_for_display( 28 | // Gdk.Display.get_default() also works 29 | display, 30 | provider, 31 | Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION 32 | ); 33 | 34 | var label = new Gtk.Label("Hi there!"); 35 | var button = new Gtk.Button.with_label("Toggle label style"); 36 | button.clicked.connect(() => { 37 | if (label.has_css_class("color-red")) { 38 | label.remove_css_class("color-red"); 39 | } else { 40 | label.add_css_class("color-red"); 41 | } 42 | }); 43 | 44 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 45 | box.halign = Gtk.Align.CENTER; 46 | box.valign = Gtk.Align.CENTER; 47 | box.append(label); 48 | box.append(button); 49 | 50 | window.child = box; 51 | } -------------------------------------------------------------------------------- /vala/gtk4/fixed.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.Fixed"; 2 | const string appTitle = "GtkFixed"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | var fixed = new Gtk.Fixed(); 20 | 21 | window.child = fixed; 22 | window.title = appTitle; 23 | window.set_default_size(400, 400); 24 | 25 | fixed.put(new Gtk.Label("A"), 10, 20); 26 | fixed.put(new Gtk.Label("B"), 100, 200); 27 | fixed.put(new Gtk.Label("C"), 99, 326); 28 | } -------------------------------------------------------------------------------- /vala/gtk4/header-bar.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.HeaderBar"; 2 | const string appTitle = "GtkHeaderBar"; 3 | const string appSubtitle = "App subtitle"; 4 | 5 | int main(string[] args) { 6 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 7 | app.startup.connect(onAppStartup); 8 | app.activate.connect(onAppActivate); 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | var titleLabel = new Gtk.Label(appTitle); 20 | var subtitleLabel = new Gtk.Label(appSubtitle); 21 | var titleBox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0); 22 | var header = new Gtk.HeaderBar(); 23 | 24 | window.titlebar = header; 25 | window.set_default_size(400, 400); 26 | 27 | titleLabel.add_css_class("title"); 28 | subtitleLabel.add_css_class("subtitle"); 29 | 30 | titleBox.valign = Gtk.Align.CENTER; 31 | titleBox.append(titleLabel); 32 | titleBox.append(subtitleLabel); 33 | 34 | header.title_widget = titleBox; 35 | header.show_title_buttons = true; 36 | } -------------------------------------------------------------------------------- /vala/gtk4/label.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.Label"; 2 | const string appTitle = "GtkLabel"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | var label = new Gtk.Label("Hi there!"); 20 | 21 | window.child = label; 22 | window.title = appTitle; 23 | window.set_default_size(400, 400); 24 | } -------------------------------------------------------------------------------- /vala/gtk4/list-box.vala: -------------------------------------------------------------------------------- 1 | const string appID = "io.github.Miqueas.GTK-Examples.Vala.Gtk4.ListBox"; 2 | const string appTitle = "GtkListBox"; 3 | 4 | int main(string[] args) { 5 | var app = new Gtk.Application(appID, ApplicationFlags.FLAGS_NONE); 6 | app.startup.connect(onAppStartup); 7 | app.activate.connect(onAppActivate); 8 | 9 | return app.run(args); 10 | } 11 | 12 | void onAppActivate(Application self) { 13 | var window = (self as Gtk.Application)?.get_active_window(); 14 | window?.present(); 15 | } 16 | 17 | void onAppStartup(Application self) { 18 | var window = new Gtk.ApplicationWindow(self as Gtk.Application); 19 | var listBox = new Gtk.ListBox(); 20 | var scrolledWindow = new Gtk.ScrolledWindow(); 21 | var button = new Gtk.Button.with_label("Load"); 22 | var box = new Gtk.Box(Gtk.Orientation.VERTICAL, 10); 23 | 24 | window.child = box; 25 | window.title = appTitle; 26 | window.set_default_size(400, 400); 27 | 28 | scrolledWindow.child = listBox; 29 | scrolledWindow.vexpand = true; 30 | scrolledWindow.propagate_natural_width = true; 31 | scrolledWindow.propagate_natural_height = true; 32 | 33 | button.margin_bottom = 10; 34 | button.halign = Gtk.Align.CENTER; 35 | button.valign = Gtk.Align.CENTER; 36 | button.clicked.connect(self => { 37 | for (int i = 0; i < 100; i++) 38 | listBox.insert(new Gtk.Label(@"Text $(i + 1)"), i); 39 | }); 40 | 41 | box.append(scrolledWindow); 42 | box.append(button); 43 | } -------------------------------------------------------------------------------- /vala/gtk4/meson.build: -------------------------------------------------------------------------------- 1 | src = [ 2 | 'action-bar', 3 | 'application-window', 4 | 'application', 5 | 'assistant', 6 | 'box', 7 | 'builder', 8 | 'css-provider', 9 | 'fixed', 10 | 'header-bar', 11 | 'label', 12 | 'list-box', 13 | 'window', 14 | ] 15 | 16 | builder = configure_file( 17 | input: 'builder.vala.in', 18 | output: 'builder.vala', 19 | configuration: builder_gtk4_ui 20 | ) 21 | 22 | css_provider = configure_file( 23 | input: 'css-provider.vala.in', 24 | output: 'css-provider.vala', 25 | configuration: css_conf 26 | ) 27 | 28 | foreach name : src 29 | if name == 'builder' 30 | executable(name, builder, dependencies: gtk4) 31 | elif name == 'css-provider' 32 | executable(name, css_provider, dependencies: gtk4) 33 | else 34 | executable(name, name + '.vala', dependencies: gtk4) 35 | endif 36 | endforeach -------------------------------------------------------------------------------- /vala/gtk4/window.vala: -------------------------------------------------------------------------------- 1 | const string appTitle = "GtkWindow"; 2 | 3 | void main() { 4 | Gtk.init(); 5 | 6 | var window = new Gtk.Window(); 7 | window.title = appTitle; 8 | window.set_default_size(400, 400); 9 | window.present(); 10 | 11 | while (Gtk.Window.get_toplevels().get_n_items() > 0) 12 | MainContext.get_thread_default().iteration(true); 13 | } --------------------------------------------------------------------------------