From 375d658cc8a64feb5e5ed5f219c37bc6d2527336 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Tue, 12 Dec 2023 19:02:08 +0100 Subject: [PATCH] Learning GTK4 Trees _ initial commit (+ simple & light loop) --- demos/gtk-demo/main.c | 412 +++++++++++++++++++++--------------------- 1 file changed, 207 insertions(+), 205 deletions(-) diff --git a/demos/gtk-demo/main.c b/demos/gtk-demo/main.c index a5d52ec..4bfe34b 100644 --- a/demos/gtk-demo/main.c +++ b/demos/gtk-demo/main.c @@ -32,9 +32,9 @@ static GtkWidget *source_view; static char *current_file = NULL; static GtkWidget *notebook; -static GtkSingleSelection *selection; -static GtkWidget *toplevel; -static char **search_needle; +// static GtkSingleSelection *selection; +// static GtkWidget *toplevel; +// static char **search_needle; typedef struct _GtkDemo GtkDemo; struct _GtkDemo @@ -142,26 +142,26 @@ struct _CallbackData GtkTreePath *path; }; -static gboolean -gtk_demo_run (GtkDemo *self, - GtkWidget *window) -{ - GtkWidget *result; +/* static gboolean */ +/* gtk_demo_run (GtkDemo *self, */ +/* GtkWidget *window) */ +/* { */ +/* GtkWidget *result; */ - if (!self->func) - return FALSE; +/* if (!self->func) */ +/* return FALSE; */ - result = self->func (window); - if (result == NULL) - return FALSE; +/* result = self->func (window); */ +/* if (result == NULL) */ +/* return FALSE; */ - if (GTK_IS_WINDOW (result)) - { - gtk_window_set_transient_for (GTK_WINDOW (result), GTK_WINDOW (window)); - gtk_window_set_modal (GTK_WINDOW (result), TRUE); - } - return TRUE; -} +/* if (GTK_IS_WINDOW (result)) */ +/* { */ +/* gtk_window_set_transient_for (GTK_WINDOW (result), GTK_WINDOW (window)); */ +/* gtk_window_set_modal (GTK_WINDOW (result), TRUE); */ +/* } */ +/* return TRUE; */ +/* } */ static void activate_about (GSimpleAction *action, @@ -255,16 +255,16 @@ activate_inspector (GSimpleAction *action, gtk_window_set_interactive_debugging (TRUE); } -static void -activate_run (GSimpleAction *action, - GVariant *parameter, - gpointer window) -{ - GtkTreeListRow *row = gtk_single_selection_get_selected_item (selection); - GtkDemo *demo = gtk_tree_list_row_get_item (row); +/* static void */ +/* activate_run (GSimpleAction *action, */ +/* GVariant *parameter, */ +/* gpointer window) */ +/* { */ +/* GtkTreeListRow *row = gtk_single_selection_get_selected_item (selection); */ +/* GtkDemo *demo = gtk_tree_list_row_get_item (row); */ - gtk_demo_run (demo, window); -} +/* gtk_demo_run (demo, window); */ +/* } */ static GtkWidget * display_image (const char *format, @@ -681,238 +681,238 @@ load_file (const char *demoname, g_object_unref (info_buffer); } -static void -activate_cb (GtkWidget *widget, - guint position, - gpointer window) -{ - GListModel *model = G_LIST_MODEL (gtk_list_view_get_model (GTK_LIST_VIEW (widget))); - GtkTreeListRow *row = g_list_model_get_item (model, position); - GtkDemo *demo = gtk_tree_list_row_get_item (row); +/* static void */ +/* activate_cb (GtkWidget *widget, */ +/* guint position, */ +/* gpointer window) */ +/* { */ +/* GListModel *model = G_LIST_MODEL (gtk_list_view_get_model (GTK_LIST_VIEW (widget))); */ +/* GtkTreeListRow *row = g_list_model_get_item (model, position); */ +/* GtkDemo *demo = gtk_tree_list_row_get_item (row); */ - gtk_demo_run (demo, window); +/* gtk_demo_run (demo, window); */ - g_object_unref (row); -} +/* g_object_unref (row); */ +/* } */ -static void -selection_cb (GtkSingleSelection *sel, - GParamSpec *pspec, - gpointer user_data) -{ - GtkTreeListRow *row = gtk_single_selection_get_selected_item (sel); - GtkDemo *demo; - GAction *action; +/* static void */ +/* selection_cb (GtkSingleSelection *sel, */ +/* GParamSpec *pspec, */ +/* gpointer user_data) */ +/* { */ +/* GtkTreeListRow *row = gtk_single_selection_get_selected_item (sel); */ +/* GtkDemo *demo; */ +/* GAction *action; */ - gtk_widget_set_sensitive (GTK_WIDGET (notebook), !!row); +/* gtk_widget_set_sensitive (GTK_WIDGET (notebook), !!row); */ - if (!row) - { - gtk_window_set_title (GTK_WINDOW (toplevel), "No match"); +/* if (!row) */ +/* { */ +/* gtk_window_set_title (GTK_WINDOW (toplevel), "No match"); */ - return; - } +/* return; */ +/* } */ - demo = gtk_tree_list_row_get_item (row); +/* demo = gtk_tree_list_row_get_item (row); */ - if (demo->filename) - load_file (demo->name, demo->filename); +/* if (demo->filename) */ +/* load_file (demo->name, demo->filename); */ - action = g_action_map_lookup_action (G_ACTION_MAP (toplevel), "run"); - g_simple_action_set_enabled (G_SIMPLE_ACTION (action), demo->func != NULL); +/* action = g_action_map_lookup_action (G_ACTION_MAP (toplevel), "run"); */ +/* g_simple_action_set_enabled (G_SIMPLE_ACTION (action), demo->func != NULL); */ - gtk_window_set_title (GTK_WINDOW (toplevel), demo->title); -} +/* gtk_window_set_title (GTK_WINDOW (toplevel), demo->title); */ +/* } */ -static gboolean -filter_demo (GtkDemo *demo) -{ - int i; +/* static gboolean */ +/* filter_demo (GtkDemo *demo) */ +/* { */ +/* int i; */ /* Show only if the name matches every needle */ - for (i = 0; search_needle[i]; i++) - { - if (!demo->title) - return FALSE; +/* for (i = 0; search_needle[i]; i++) */ +/* { */ +/* if (!demo->title) */ +/* return FALSE; */ - if (g_str_match_string (search_needle[i], demo->title, TRUE)) - continue; +/* if (g_str_match_string (search_needle[i], demo->title, TRUE)) */ +/* continue; */ - if (demo->keywords) - { - int j; - gboolean found = FALSE; +/* if (demo->keywords) */ +/* { */ +/* int j; */ +/* gboolean found = FALSE; */ - for (j = 0; !found && demo->keywords[j]; j++) - { - if (strstr (demo->keywords[j], search_needle[i])) - found = TRUE; - } +/* for (j = 0; !found && demo->keywords[j]; j++) */ +/* { */ +/* if (strstr (demo->keywords[j], search_needle[i])) */ +/* found = TRUE; */ +/* } */ - if (found) - continue; - } +/* if (found) */ +/* continue; */ +/* } */ - return FALSE; - } +/* return FALSE; */ +/* } */ - return TRUE; -} +/* return TRUE; */ +/* } */ -static gboolean -demo_filter_by_name (gpointer item, - gpointer user_data) -{ - GtkTreeListRow *row = item; - GListModel *children; - GtkDemo *demo; - guint i, n; - GtkTreeListRow *parent; +/* static gboolean */ +/* demo_filter_by_name (gpointer item, */ +/* gpointer user_data) */ +/* { */ +/* GtkTreeListRow *row = item; */ +/* GListModel *children; */ +/* GtkDemo *demo; */ +/* guint i, n; */ +/* GtkTreeListRow *parent; */ /* Show all items if search is empty */ - if (!search_needle || !search_needle[0] || !*search_needle[0]) - return TRUE; +/* if (!search_needle || !search_needle[0] || !*search_needle[0]) */ +/* return TRUE; */ - g_assert (GTK_IS_TREE_LIST_ROW (row)); - g_assert (GTK_IS_FILTER_LIST_MODEL (user_data)); +/* g_assert (GTK_IS_TREE_LIST_ROW (row)); */ +/* g_assert (GTK_IS_FILTER_LIST_MODEL (user_data)); */ /* Show a row if itself of any parent matches */ - for (parent = row; parent; parent = gtk_tree_list_row_get_parent (parent)) - { - demo = gtk_tree_list_row_get_item (parent); - g_assert (GTK_IS_DEMO (demo)); +/* for (parent = row; parent; parent = gtk_tree_list_row_get_parent (parent)) */ +/* { */ +/* demo = gtk_tree_list_row_get_item (parent); */ +/* g_assert (GTK_IS_DEMO (demo)); */ - if (filter_demo (demo)) - return TRUE; - } +/* if (filter_demo (demo)) */ +/* return TRUE; */ +/* } */ /* Show a row if any child matches */ - children = gtk_tree_list_row_get_children (row); - if (children) - { - n = g_list_model_get_n_items (children); - for (i = 0; i < n; i++) - { - demo = g_list_model_get_item (children, i); - g_assert (GTK_IS_DEMO (demo)); +/* children = gtk_tree_list_row_get_children (row); */ +/* if (children) */ +/* { */ +/* n = g_list_model_get_n_items (children); */ +/* for (i = 0; i < n; i++) */ +/* { */ +/* demo = g_list_model_get_item (children, i); */ +/* g_assert (GTK_IS_DEMO (demo)); */ - if (filter_demo (demo)) - { - g_object_unref (demo); - return TRUE; - } - g_object_unref (demo); - } - } +/* if (filter_demo (demo)) */ +/* { */ +/* g_object_unref (demo); */ +/* return TRUE; */ +/* } */ +/* g_object_unref (demo); */ +/* } */ +/* } */ - return FALSE; -} +/* return FALSE; */ +/* } */ -static void -demo_search_changed_cb (GtkSearchEntry *entry, - GtkFilter *filter) -{ - const char *text; +/* static void */ +/* demo_search_changed_cb (GtkSearchEntry *entry, */ +/* GtkFilter *filter) */ +/* { */ +/* const char *text; */ - g_assert (GTK_IS_SEARCH_ENTRY (entry)); - g_assert (GTK_IS_FILTER (filter)); +/* g_assert (GTK_IS_SEARCH_ENTRY (entry)); */ +/* g_assert (GTK_IS_FILTER (filter)); */ - text = gtk_editable_get_text (GTK_EDITABLE (entry)); +/* text = gtk_editable_get_text (GTK_EDITABLE (entry)); */ - g_clear_pointer (&search_needle, g_strfreev); +/* g_clear_pointer (&search_needle, g_strfreev); */ - if (text && *text) - search_needle = g_str_tokenize_and_fold (text, NULL, NULL); +/* if (text && *text) */ +/* search_needle = g_str_tokenize_and_fold (text, NULL, NULL); */ - gtk_filter_changed (filter, GTK_FILTER_CHANGE_DIFFERENT); -} +/* gtk_filter_changed (filter, GTK_FILTER_CHANGE_DIFFERENT); */ +/* } */ -static GListModel * -create_demo_model (void) -{ - GListStore *store = g_list_store_new (GTK_TYPE_DEMO); - DemoData *demo = gtk_demos; - GtkDemo *d; +/* static GListModel * */ +/* create_demo_model (void) */ +/* { */ +/* GListStore *store = g_list_store_new (GTK_TYPE_DEMO); */ +/* DemoData *demo = gtk_demos; */ +/* GtkDemo *d; */ - d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); - d->name = "main"; - d->title = "GTK Demo"; - d->keywords = NULL; - d->filename = "main.c"; - d->func = NULL; +/* d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); */ +/* d->name = "main"; */ +/* d->title = "GTK Demo"; */ +/* d->keywords = NULL; */ +/* d->filename = "main.c"; */ +/* d->func = NULL; */ - g_list_store_append (store, d); +/* g_list_store_append (store, d); */ - while (demo->title) - { - d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); - DemoData *children = demo->children; +/* while (demo->title) */ +/* { */ +/* d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); */ +/* DemoData *children = demo->children; */ - d->name = demo->name; - d->title = demo->title; - d->keywords = demo->keywords; - d->filename = demo->filename; - d->func = demo->func; +/* d->name = demo->name; */ +/* d->title = demo->title; */ +/* d->keywords = demo->keywords; */ +/* d->filename = demo->filename; */ +/* d->func = demo->func; */ - g_list_store_append (store, d); +/* g_list_store_append (store, d); */ - if (children) - { - d->children_model = G_LIST_MODEL (g_list_store_new (GTK_TYPE_DEMO)); +/* if (children) */ +/* { */ +/* d->children_model = G_LIST_MODEL (g_list_store_new (GTK_TYPE_DEMO)); */ - while (children->title) - { - GtkDemo *child = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); +/* while (children->title) */ +/* { */ +/* GtkDemo *child = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); */ - child->name = children->name; - child->title = children->title; - child->keywords = children->keywords; - child->filename = children->filename; - child->func = children->func; +/* child->name = children->name; */ +/* child->title = children->title; */ +/* child->keywords = children->keywords; */ +/* child->filename = children->filename; */ +/* child->func = children->func; */ - g_list_store_append (G_LIST_STORE (d->children_model), child); - children++; - } - } +/* g_list_store_append (G_LIST_STORE (d->children_model), child); */ +/* children++; */ +/* } */ +/* } */ - demo++; - } +/* demo++; */ +/* } */ - return G_LIST_MODEL (store); -} +/* return G_LIST_MODEL (store); */ +/* } */ -static GListModel * -get_child_model (gpointer item, - gpointer user_data) -{ - GtkDemo *demo = item; +/* static GListModel * */ +/* get_child_model (gpointer item, */ +/* gpointer user_data) */ +/* { */ +/* GtkDemo *demo = item; */ - if (demo->children_model) - return g_object_ref (G_LIST_MODEL (demo->children_model)); +/* if (demo->children_model) */ +/* return g_object_ref (G_LIST_MODEL (demo->children_model)); */ - return NULL; -} +/* return NULL; */ +/* } */ -static void -clear_search (GtkSearchBar *bar) -{ - if (!gtk_search_bar_get_search_mode (bar)) - { - GtkWidget *entry = gtk_search_bar_get_child (GTK_SEARCH_BAR (bar)); - gtk_editable_set_text (GTK_EDITABLE (entry), ""); - } -} +/* static void */ +/* clear_search (GtkSearchBar *bar) */ +/* { */ +/* if (!gtk_search_bar_get_search_mode (bar)) */ +/* { */ +/* GtkWidget *entry = gtk_search_bar_get_child (GTK_SEARCH_BAR (bar)); */ +/* gtk_editable_set_text (GTK_EDITABLE (entry), ""); */ +/* } */ +/* } */ static void activate (GApplication *app) { - GtkBuilder *builder; - GListModel *listmodel; - GtkTreeListModel *treemodel; - GtkWidget *window, *listview, *search_entry, *search_bar; - GtkFilterListModel *filter_model; - GtkFilter *filter; - GSimpleAction *action; +// GtkBuilder *builder; +// GListModel *listmodel; +// GtkTreeListModel *treemodel; + GtkWidget *window;//, *listview, *search_entry, *search_bar; +// GtkFilterListModel *filter_model; +// GtkFilter *filter; +// GSimpleAction *action; window= do_tree_store(NULL); gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (window)); @@ -1128,6 +1128,8 @@ main (int argc, char **argv) g_signal_connect (app, "handle-local-options", G_CALLBACK (local_options), NULL); g_application_run (G_APPLICATION (app), argc, argv); + + printf(" (line 1132 in demos/gtk-demo/main.c) >> [env] $ clear && meson compile && demos/gtk-demo/gtk4-demo\n"); return 0; }