From faa7ddc14cc2ecbda87378c22a991f7c620adc44 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Sun, 11 Feb 2024 09:33:45 +0100 Subject: [PATCH] =?UTF-8?q?WIP:=20premier=20'arbre'=20obtenu=20=C3=A0=20pa?= =?UTF-8?q?rtir=20du=20code=20de=20Christian=20Hegert=20(to=20clean)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/ui.h | 3 +- src/main.c | 61 +++++++++++++----------- src/ui/sand_box.c | 115 ++-------------------------------------------- 3 files changed, 41 insertions(+), 138 deletions(-) diff --git a/include/ui.h b/include/ui.h index 676d131..fe1ccd3 100644 --- a/include/ui.h +++ b/include/ui.h @@ -122,4 +122,5 @@ void ui_toggle_sidebar (); // Tree primitives -void on_activate_window_creation (GtkApplication *app, gpointer data); +//void on_activate_window_creation (GtkApplication *app, gpointer data); +void on_activate_window_creation (GApplication* app, gpointer user_data); diff --git a/src/main.c b/src/main.c index 8c9edd6..181c3a8 100644 --- a/src/main.c +++ b/src/main.c @@ -56,32 +56,41 @@ * - base.h (définitions 'transversales'/(communes) utilisables par plusieurs ressources */ -int main(int argc, char **argv) -{ - g_autoptr(GemGraphClientApplication) app = NULL; - int res; +int main (int argc, char** argv) { + GtkApplication* app = gtk_application_new ("org.example.gtk4tree", G_APPLICATION_FLAGS_NONE); + g_signal_connect (app, "activate", G_CALLBACK (on_activate_window_creation), NULL); + int status = g_application_run (G_APPLICATION (app), argc, argv); + g_object_unref (app); // use : on_destroy (GtkWidget *widget, gpointer data) {...} ? - app = gem_graph_client_application_new("org.alec.gemgraph", - G_APPLICATION_DEFAULT_FLAGS); - - - - - - - - // Jean 2024-01-20 Je désactive la visualisation de la fenêtre principale - // dans : application.c > gem_graph_client_application_activate() - // pour ne travailler que dans la fenêtre de sand_box.c - - g_signal_connect (app, "activate", G_CALLBACK (on_activate_window_creation), - "This signal comes from main.c"); - - - - - - res = g_application_run(G_APPLICATION(app), argc, argv); - return res; + return status; } +/* int main(int argc, char **argv) */ +/* { */ +/* g_autoptr(GemGraphClientApplication) app = NULL; */ +/* int res; */ + +/* app = gem_graph_client_application_new("org.alec.gemgraph", */ +/* G_APPLICATION_DEFAULT_FLAGS); */ + + + + + + + +/* // Jean 2024-01-20 Je désactive la visualisation de la fenêtre principale */ +/* // dans : application.c > gem_graph_client_application_activate() */ +/* // pour ne travailler que dans la fenêtre de sand_box.c */ + +/* g_signal_connect (app, "activate", G_CALLBACK (on_activate_window_creation), */ +/* "This signal comes from main.c"); */ + + + + + +/* res = g_application_run(G_APPLICATION(app), argc, argv); */ +/* return res; */ +/* } */ + diff --git a/src/ui/sand_box.c b/src/ui/sand_box.c index c26a5b5..a83c9a3 100644 --- a/src/ui/sand_box.c +++ b/src/ui/sand_box.c @@ -57,7 +57,7 @@ static void on_bind_list_item_cb (GtkListItemFactory* factory, GtkListItem* list // Determine if this item should have an expander (i.e., if it has children) GListModel* child_model = create_child_model (text); - if (child_model) gtk_tree_expander_set_expanded (GTK_TREE_EXPANDER (expander), TRUE); + if (child_model) gtk_expander_set_expanded (GTK_TREE_EXPANDER (expander), TRUE); g_object_unref (child_model); // Cleanup } @@ -84,7 +84,7 @@ static GtkTreeListModel* create_tree_list_model () { return tree_model; } -static void on_app_activate (GApplication* app, gpointer user_data) { +void on_activate_window_creation (GApplication* app, gpointer user_data) { GtkWidget* window = gtk_application_window_new (GTK_APPLICATION (app)); gtk_window_set_title (GTK_WINDOW (window), "GTK4 Tree Example"); gtk_window_set_default_size (GTK_WINDOW (window), 600, 400); @@ -99,121 +99,14 @@ static void on_app_activate (GApplication* app, gpointer user_data) { GtkWidget* list_view = gtk_list_view_new (selection_model, factory); gtk_window_set_child (GTK_WINDOW (window), list_view); + gtk_widget_show (window); g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (on_destroy), "sand_box.c Christian Hegert / on_activate_window_creation () > exit (0) closes window\n"); } -static int not_yet_main (int argc, char** argv) { - GtkApplication* app = gtk_application_new ("org.example.gtk4tree", G_APPLICATION_FLAGS_NONE); - g_signal_connect (app, "activate", G_CALLBACK (on_app_activate), NULL); - int status = g_application_run (G_APPLICATION (app), argc, argv); - g_object_unref (app); // use : on_destroy (GtkWidget *widget, gpointer data) {...} ? - return status; -} - -void on_activate_window_creation (GtkApplication *app, gpointer data) -{ - GtkWidget *that_window = gtk_application_window_new (gtk_application_new ("org.gtk.test", 0)); - g_signal_connect (G_OBJECT (that_window), "destroy", G_CALLBACK (on_destroy), - "sand_box.c / on_activate_window_creation () > exit (0) closes window\n"); - gtk_window_set_title (GTK_WINDOW (that_window), "In the lab "); - gtk_window_set_default_size (GTK_WINDOW (that_window), 180, 140); - GtkWidget *my_scrolling_thing = gtk_scrolled_window_new (); - gtk_window_set_child (GTK_WINDOW (that_window), my_scrolling_thing); - gtk_widget_show (that_window); - and_now_let_s_climb_that_tree (my_scrolling_thing); - if (data) {g_print (data); g_print (" and is displayed by : sand_box.c / on_activate_window_creation ()\n");} -} -//------------------------------------------------------------------------------------------------------------------------------------------- - -//void on_activating_this_expander (GtkExpander *my_expander, gpointer my_data) {} -//void gtk_expander_set_child (GtkExpander* expander, GtkWidget* child) {} - -GListModel *my_item_create_func (gpointer item, gpointer user_data) {return NULL;} - -static void on_setup_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data) -/* Setup the widget to show in the Gtk.Listview << adapted from the kriptolix example */ -{ -// printf ("...\n", gtk_string_list_get_string (GTK_STRING_LIST (user_data), 0)); - GtkWidget *my_label = gtk_label_new (NULL), *my_child_label = gtk_label_new (NULL); - GtkWidget *my_expander = gtk_expander_new (NULL); // ? = gtk_expander_new (GTK_LABEL (my_list_item)); - gtk_list_item_set_child (my_list_item, my_label); - gtk_expander_set_child (GTK_EXPANDER (my_expander), my_label); - gtk_list_item_set_child (my_list_item, my_child_label); // ? gtk_list_item_set_child (my_list_item, my_expander); -} - -static void on_bind_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data) -/* Bind data from the store object to the widget << adapting from the kriptolix example */ -{ - GtkWidget *my_expander = gtk_expander_new (NULL); - gtk_expander_set_child (GTK_EXPANDER (my_expander), gtk_list_item_get_child (my_list_item)); - GtkWidget *my_label = gtk_list_item_get_child (my_list_item); // GtkWidget *my_label = gtk_expander_get_child (my_expander); - -// >>> row = item.get_item () <<< GtkTreeListRow instances are created by a GtkTreeListModel -------------------------- -// https://docs.gtk.org/gtk4/class.TreeListRow.html GtkTreeListRow is used by GtkTreeListModel to represent items. -// It allows navigating the model as a tree and modify the state of rows. -// GtkTreeListRow instances are created by a GtkTreeListModel -// The GtkTreeExpander widget can make use of GtkTreeListRow objects -// https://docs.gtk.org/gtk4/method.TreeExpander.set_list_row.html -// >>> void gtk_tree_expander_set_list_row (GtkTreeExpander *self, GtkTreeListRow *list_row); -// https://docs.gtk.org/gtk4/callback.TreeListModelCreateModelFunc.html - -// in add_tree_node (item){} >>> if (type (item) == Gtk.TreeListRow) item = item.get_item (); <<< à 'traduire' +++ -// --------------------------------------------------------------------------------------------------------------------------- - -// GObject *my_object = gtk_tree_list_row_get_item (GTK_TREE_LIST_ROW (self)); // GListModel *gtk_tree_list_row_get_children (GtkTreeListRow *self) -// GtkStringObject *my_string_object = gtk_list_item_get_item (my_list_item); -// gtk_tree_expander_set_list_row (GTK_TREE_LIST_ROW (self), my_string_object); - - /* my_string_object is owned by the instance. Caller mustn't change or destroy it. */ - GtkStringObject *my_string_object = gtk_list_item_get_item (my_list_item); - /* The string returned by gtk_string_object_get_string is owned by the instance. */ - gtk_label_set_text (GTK_LABEL (my_label), gtk_string_object_get_string (my_string_object)); -} - -static void on_selection_change (GtkStringList *my_selection_model, guint position, guint n_items, gpointer user_data) -{ - printf ("list size = %d item type = %ld\ - position (premier élement ayant changé d'état) = %2d %7s\ - nombre d'éléments ayant changé d'état = %2d\n", - g_list_model_get_n_items (G_LIST_MODEL (my_selection_model)), - g_list_model_get_item_type (G_LIST_MODEL (my_selection_model)), - position, - gtk_string_object_get_string (g_list_model_get_item (G_LIST_MODEL (my_selection_model), position)), - n_items); -} - -//------------------------------------------------------------------------------------------------------------------------------------------- - -static void and_now_let_s_climb_that_tree (GtkWidget *in_that_box) -{ - const gchar *my_array[] = {"zero", "one", "two", "three", "four", NULL}; - GtkStringList *my_string_list = gtk_string_list_new ( (const char * const *) my_array); - gtk_string_list_append (my_string_list, "five"); - - // GType *my_item_type = g_list_model_get_item_type (G_LIST_MODEL (my_string_list)); - GListStore *my_list_store = g_list_store_new (80); // = g_list_store_new (my_item_type); - g_list_store_append (my_list_store, my_string_list); // Et maintenant ? - // use my_list_store to implement my_tree_list_model ?... WTF ! - - GtkMultiSelection *my_selection_model = gtk_multi_selection_new (G_LIST_MODEL (my_string_list)); - GtkListItemFactory *my_list_item_factory = gtk_signal_list_item_factory_new (); - GtkTreeListModel *my_tree_list_model = - gtk_tree_list_model_new (G_LIST_MODEL (my_string_list), 0, 0, my_item_create_func, NULL, NULL); - g_signal_connect (my_list_item_factory, "setup", G_CALLBACK (on_setup_cb), my_tree_list_model); - g_signal_connect (my_list_item_factory, "bind", G_CALLBACK (on_bind_cb), my_tree_list_model); - g_signal_connect (my_selection_model, "selection-changed", G_CALLBACK (on_selection_change), NULL); - GtkWidget *my_list_view = gtk_list_view_new (GTK_SELECTION_MODEL (my_selection_model), my_list_item_factory); - gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (in_that_box), my_list_view); - - srand (time (NULL)); int rank = rand () % g_list_model_get_n_items (G_LIST_MODEL (my_selection_model)); - printf ("in : GListModel *item_create_func (gpointer item,...) (line 80) (item n°%d is %s)\n", - rank, gtk_string_list_get_string (my_string_list, rank)); -} - -//-----------------------------------------| mes notes. |------------------------------------------ +//-----------------------------------------| notes. |------------------------------------------ /* On 07/02/2024 04:48, Christian Hegert wrote: *