Learning GTK4 Trees _ initial commit (+ simple & light loop)

This commit is contained in:
Jean Sirmai 2023-12-12 19:02:08 +01:00
parent 1a02d93f20
commit 375d658cc8
Signed by: jean
GPG Key ID: FB3115C340E057E3
1 changed files with 207 additions and 205 deletions

View File

@ -32,9 +32,9 @@ static GtkWidget *source_view;
static char *current_file = NULL; static char *current_file = NULL;
static GtkWidget *notebook; static GtkWidget *notebook;
static GtkSingleSelection *selection; // static GtkSingleSelection *selection;
static GtkWidget *toplevel; // static GtkWidget *toplevel;
static char **search_needle; // static char **search_needle;
typedef struct _GtkDemo GtkDemo; typedef struct _GtkDemo GtkDemo;
struct _GtkDemo struct _GtkDemo
@ -142,26 +142,26 @@ struct _CallbackData
GtkTreePath *path; GtkTreePath *path;
}; };
static gboolean /* static gboolean */
gtk_demo_run (GtkDemo *self, /* gtk_demo_run (GtkDemo *self, */
GtkWidget *window) /* GtkWidget *window) */
{ /* { */
GtkWidget *result; /* GtkWidget *result; */
if (!self->func) /* if (!self->func) */
return FALSE; /* return FALSE; */
result = self->func (window); /* result = self->func (window); */
if (result == NULL) /* if (result == NULL) */
return FALSE; /* return FALSE; */
if (GTK_IS_WINDOW (result)) /* if (GTK_IS_WINDOW (result)) */
{ /* { */
gtk_window_set_transient_for (GTK_WINDOW (result), GTK_WINDOW (window)); /* gtk_window_set_transient_for (GTK_WINDOW (result), GTK_WINDOW (window)); */
gtk_window_set_modal (GTK_WINDOW (result), TRUE); /* gtk_window_set_modal (GTK_WINDOW (result), TRUE); */
} /* } */
return TRUE; /* return TRUE; */
} /* } */
static void static void
activate_about (GSimpleAction *action, activate_about (GSimpleAction *action,
@ -255,16 +255,16 @@ activate_inspector (GSimpleAction *action,
gtk_window_set_interactive_debugging (TRUE); gtk_window_set_interactive_debugging (TRUE);
} }
static void /* static void */
activate_run (GSimpleAction *action, /* activate_run (GSimpleAction *action, */
GVariant *parameter, /* GVariant *parameter, */
gpointer window) /* gpointer window) */
{ /* { */
GtkTreeListRow *row = gtk_single_selection_get_selected_item (selection); /* GtkTreeListRow *row = gtk_single_selection_get_selected_item (selection); */
GtkDemo *demo = gtk_tree_list_row_get_item (row); /* GtkDemo *demo = gtk_tree_list_row_get_item (row); */
gtk_demo_run (demo, window); /* gtk_demo_run (demo, window); */
} /* } */
static GtkWidget * static GtkWidget *
display_image (const char *format, display_image (const char *format,
@ -681,238 +681,238 @@ load_file (const char *demoname,
g_object_unref (info_buffer); g_object_unref (info_buffer);
} }
static void /* static void */
activate_cb (GtkWidget *widget, /* activate_cb (GtkWidget *widget, */
guint position, /* guint position, */
gpointer window) /* gpointer window) */
{ /* { */
GListModel *model = G_LIST_MODEL (gtk_list_view_get_model (GTK_LIST_VIEW (widget))); /* GListModel *model = G_LIST_MODEL (gtk_list_view_get_model (GTK_LIST_VIEW (widget))); */
GtkTreeListRow *row = g_list_model_get_item (model, position); /* GtkTreeListRow *row = g_list_model_get_item (model, position); */
GtkDemo *demo = gtk_tree_list_row_get_item (row); /* 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 /* static void */
selection_cb (GtkSingleSelection *sel, /* selection_cb (GtkSingleSelection *sel, */
GParamSpec *pspec, /* GParamSpec *pspec, */
gpointer user_data) /* gpointer user_data) */
{ /* { */
GtkTreeListRow *row = gtk_single_selection_get_selected_item (sel); /* GtkTreeListRow *row = gtk_single_selection_get_selected_item (sel); */
GtkDemo *demo; /* GtkDemo *demo; */
GAction *action; /* GAction *action; */
gtk_widget_set_sensitive (GTK_WIDGET (notebook), !!row); /* gtk_widget_set_sensitive (GTK_WIDGET (notebook), !!row); */
if (!row) /* if (!row) */
{ /* { */
gtk_window_set_title (GTK_WINDOW (toplevel), "No match"); /* 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) /* if (demo->filename) */
load_file (demo->name, demo->filename); /* load_file (demo->name, demo->filename); */
action = g_action_map_lookup_action (G_ACTION_MAP (toplevel), "run"); /* action = g_action_map_lookup_action (G_ACTION_MAP (toplevel), "run"); */
g_simple_action_set_enabled (G_SIMPLE_ACTION (action), demo->func != NULL); /* 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 /* static gboolean */
filter_demo (GtkDemo *demo) /* filter_demo (GtkDemo *demo) */
{ /* { */
int i; /* int i; */
/* Show only if the name matches every needle */ /* Show only if the name matches every needle */
for (i = 0; search_needle[i]; i++) /* for (i = 0; search_needle[i]; i++) */
{ /* { */
if (!demo->title) /* if (!demo->title) */
return FALSE; /* return FALSE; */
if (g_str_match_string (search_needle[i], demo->title, TRUE)) /* if (g_str_match_string (search_needle[i], demo->title, TRUE)) */
continue; /* continue; */
if (demo->keywords) /* if (demo->keywords) */
{ /* { */
int j; /* int j; */
gboolean found = FALSE; /* gboolean found = FALSE; */
for (j = 0; !found && demo->keywords[j]; j++) /* for (j = 0; !found && demo->keywords[j]; j++) */
{ /* { */
if (strstr (demo->keywords[j], search_needle[i])) /* if (strstr (demo->keywords[j], search_needle[i])) */
found = TRUE; /* found = TRUE; */
} /* } */
if (found) /* if (found) */
continue; /* continue; */
} /* } */
return FALSE; /* return FALSE; */
} /* } */
return TRUE; /* return TRUE; */
} /* } */
static gboolean /* static gboolean */
demo_filter_by_name (gpointer item, /* demo_filter_by_name (gpointer item, */
gpointer user_data) /* gpointer user_data) */
{ /* { */
GtkTreeListRow *row = item; /* GtkTreeListRow *row = item; */
GListModel *children; /* GListModel *children; */
GtkDemo *demo; /* GtkDemo *demo; */
guint i, n; /* guint i, n; */
GtkTreeListRow *parent; /* GtkTreeListRow *parent; */
/* Show all items if search is empty */ /* Show all items if search is empty */
if (!search_needle || !search_needle[0] || !*search_needle[0]) /* if (!search_needle || !search_needle[0] || !*search_needle[0]) */
return TRUE; /* return TRUE; */
g_assert (GTK_IS_TREE_LIST_ROW (row)); /* g_assert (GTK_IS_TREE_LIST_ROW (row)); */
g_assert (GTK_IS_FILTER_LIST_MODEL (user_data)); /* g_assert (GTK_IS_FILTER_LIST_MODEL (user_data)); */
/* Show a row if itself of any parent matches */ /* Show a row if itself of any parent matches */
for (parent = row; parent; parent = gtk_tree_list_row_get_parent (parent)) /* for (parent = row; parent; parent = gtk_tree_list_row_get_parent (parent)) */
{ /* { */
demo = gtk_tree_list_row_get_item (parent); /* demo = gtk_tree_list_row_get_item (parent); */
g_assert (GTK_IS_DEMO (demo)); /* g_assert (GTK_IS_DEMO (demo)); */
if (filter_demo (demo)) /* if (filter_demo (demo)) */
return TRUE; /* return TRUE; */
} /* } */
/* Show a row if any child matches */ /* Show a row if any child matches */
children = gtk_tree_list_row_get_children (row); /* children = gtk_tree_list_row_get_children (row); */
if (children) /* if (children) */
{ /* { */
n = g_list_model_get_n_items (children); /* n = g_list_model_get_n_items (children); */
for (i = 0; i < n; i++) /* for (i = 0; i < n; i++) */
{ /* { */
demo = g_list_model_get_item (children, i); /* demo = g_list_model_get_item (children, i); */
g_assert (GTK_IS_DEMO (demo)); /* g_assert (GTK_IS_DEMO (demo)); */
if (filter_demo (demo)) /* if (filter_demo (demo)) */
{ /* { */
g_object_unref (demo); /* g_object_unref (demo); */
return TRUE; /* return TRUE; */
} /* } */
g_object_unref (demo); /* g_object_unref (demo); */
} /* } */
} /* } */
return FALSE; /* return FALSE; */
} /* } */
static void /* static void */
demo_search_changed_cb (GtkSearchEntry *entry, /* demo_search_changed_cb (GtkSearchEntry *entry, */
GtkFilter *filter) /* GtkFilter *filter) */
{ /* { */
const char *text; /* const char *text; */
g_assert (GTK_IS_SEARCH_ENTRY (entry)); /* g_assert (GTK_IS_SEARCH_ENTRY (entry)); */
g_assert (GTK_IS_FILTER (filter)); /* 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) /* if (text && *text) */
search_needle = g_str_tokenize_and_fold (text, NULL, NULL); /* 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 * /* static GListModel * */
create_demo_model (void) /* create_demo_model (void) */
{ /* { */
GListStore *store = g_list_store_new (GTK_TYPE_DEMO); /* GListStore *store = g_list_store_new (GTK_TYPE_DEMO); */
DemoData *demo = gtk_demos; /* DemoData *demo = gtk_demos; */
GtkDemo *d; /* GtkDemo *d; */
d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); /* d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); */
d->name = "main"; /* d->name = "main"; */
d->title = "GTK Demo"; /* d->title = "GTK Demo"; */
d->keywords = NULL; /* d->keywords = NULL; */
d->filename = "main.c"; /* d->filename = "main.c"; */
d->func = NULL; /* d->func = NULL; */
g_list_store_append (store, d); /* g_list_store_append (store, d); */
while (demo->title) /* while (demo->title) */
{ /* { */
d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); /* d = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); */
DemoData *children = demo->children; /* DemoData *children = demo->children; */
d->name = demo->name; /* d->name = demo->name; */
d->title = demo->title; /* d->title = demo->title; */
d->keywords = demo->keywords; /* d->keywords = demo->keywords; */
d->filename = demo->filename; /* d->filename = demo->filename; */
d->func = demo->func; /* d->func = demo->func; */
g_list_store_append (store, d); /* g_list_store_append (store, d); */
if (children) /* if (children) */
{ /* { */
d->children_model = G_LIST_MODEL (g_list_store_new (GTK_TYPE_DEMO)); /* d->children_model = G_LIST_MODEL (g_list_store_new (GTK_TYPE_DEMO)); */
while (children->title) /* while (children->title) */
{ /* { */
GtkDemo *child = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); /* GtkDemo *child = GTK_DEMO (g_object_new (GTK_TYPE_DEMO, NULL)); */
child->name = children->name; /* child->name = children->name; */
child->title = children->title; /* child->title = children->title; */
child->keywords = children->keywords; /* child->keywords = children->keywords; */
child->filename = children->filename; /* child->filename = children->filename; */
child->func = children->func; /* child->func = children->func; */
g_list_store_append (G_LIST_STORE (d->children_model), child); /* g_list_store_append (G_LIST_STORE (d->children_model), child); */
children++; /* children++; */
} /* } */
} /* } */
demo++; /* demo++; */
} /* } */
return G_LIST_MODEL (store); /* return G_LIST_MODEL (store); */
} /* } */
static GListModel * /* static GListModel * */
get_child_model (gpointer item, /* get_child_model (gpointer item, */
gpointer user_data) /* gpointer user_data) */
{ /* { */
GtkDemo *demo = item; /* GtkDemo *demo = item; */
if (demo->children_model) /* if (demo->children_model) */
return g_object_ref (G_LIST_MODEL (demo->children_model)); /* return g_object_ref (G_LIST_MODEL (demo->children_model)); */
return NULL; /* return NULL; */
} /* } */
static void /* static void */
clear_search (GtkSearchBar *bar) /* clear_search (GtkSearchBar *bar) */
{ /* { */
if (!gtk_search_bar_get_search_mode (bar)) /* if (!gtk_search_bar_get_search_mode (bar)) */
{ /* { */
GtkWidget *entry = gtk_search_bar_get_child (GTK_SEARCH_BAR (bar)); /* GtkWidget *entry = gtk_search_bar_get_child (GTK_SEARCH_BAR (bar)); */
gtk_editable_set_text (GTK_EDITABLE (entry), ""); /* gtk_editable_set_text (GTK_EDITABLE (entry), ""); */
} /* } */
} /* } */
static void static void
activate (GApplication *app) activate (GApplication *app)
{ {
GtkBuilder *builder; // GtkBuilder *builder;
GListModel *listmodel; // GListModel *listmodel;
GtkTreeListModel *treemodel; // GtkTreeListModel *treemodel;
GtkWidget *window, *listview, *search_entry, *search_bar; GtkWidget *window;//, *listview, *search_entry, *search_bar;
GtkFilterListModel *filter_model; // GtkFilterListModel *filter_model;
GtkFilter *filter; // GtkFilter *filter;
GSimpleAction *action; // GSimpleAction *action;
window= do_tree_store(NULL); window= do_tree_store(NULL);
gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (window)); gtk_application_add_window (GTK_APPLICATION (app), GTK_WINDOW (window));
@ -1129,5 +1129,7 @@ main (int argc, char **argv)
g_application_run (G_APPLICATION (app), argc, argv); 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; return 0;
} }