/
evo
/
Gtk4-tutorial
Обзор
Документация
Войти
/
evo
/
Gtk4-tutorial
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/misc/list1.c
74 строки
3 KB
M0JXD
Add space for all stat =g_application_run
01 ноя 2025, 03:42
01 ноя 2025, 03:42
4ddec85
Код
Авторство
О чём код?
#include <gtk/gtk.h> static void setup_cb (GtkSignalListItemFactory *self, GtkListItem *listitem, gpointer user_data) { GtkWidget *lb = gtk_label_new (NULL); gtk_list_item_set_child (listitem, lb); /* Because gtk_list_item_set_child sunk the floating reference of lb, releasing (unref) isn't necessary for lb. */ } static void bind_cb (GtkSignalListItemFactory *self, GtkListItem *listitem, gpointer user_data) { GtkWidget *lb = gtk_list_item_get_child (listitem); /* Strobj is owned by the instance. Caller mustn't change or destroy it. */ GtkStringObject *strobj = gtk_list_item_get_item (listitem); /* The string returned by gtk_string_object_get_string is owned by the instance. */ gtk_label_set_text (GTK_LABEL (lb), gtk_string_object_get_string (strobj)); } static void unbind_cb (GtkSignalListItemFactory *self, GtkListItem *listitem, gpointer user_data) { /* There's nothing to do here. */ } static void teardown_cb (GtkSignalListItemFactory *self, GtkListItem *listitem, gpointer user_data) { /* There's nothing to do here. */ /* GtkListItem instance will be destroyed soon. You don't need to set the child to NULL. */ } static void app_activate (GApplication *application) { GtkApplication *app = GTK_APPLICATION (application); GtkWidget *win = gtk_application_window_new (app); gtk_window_set_default_size (GTK_WINDOW (win), 600, 400); GtkWidget *scr = gtk_scrolled_window_new (); gtk_window_set_child (GTK_WINDOW (win), scr); char *array[] = { "one", "two", "three", "four", NULL }; /* sl is owned by ns */ /* ns and factory are owned by lv. */ /* Therefore, you don't need to care about their destruction. */ GtkStringList *sl = gtk_string_list_new ((const char * const *) array); GtkNoSelection *ns = gtk_no_selection_new (G_LIST_MODEL (sl)); GtkListItemFactory *factory = gtk_signal_list_item_factory_new (); g_signal_connect (factory, "setup", G_CALLBACK (setup_cb), NULL); g_signal_connect (factory, "bind", G_CALLBACK (bind_cb), NULL); /* The following two lines can be left out. The handlers do nothing. */ g_signal_connect (factory, "unbind", G_CALLBACK (unbind_cb), NULL); g_signal_connect (factory, "teardown", G_CALLBACK (teardown_cb), NULL); GtkWidget *lv = gtk_list_view_new (GTK_SELECTION_MODEL (ns), factory); gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (scr), lv); gtk_window_present (GTK_WINDOW (win)); } /* ----- main ----- */ #define APPLICATION_ID "com.github.ToshioCP.list1" int main (int argc, char **argv) { GtkApplication *app; int stat; app = gtk_application_new (APPLICATION_ID, G_APPLICATION_DEFAULT_FLAGS); g_signal_connect (app, "activate", G_CALLBACK (app_activate), NULL); stat = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return stat; }