From 8dcd4fc8befaf9b0efb5be47aeff55e73080d124 Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Mon, 8 Jan 2024 09:31:07 +0100 Subject: [PATCH] WIP: tree successfully created. Now, let's display it in the window --- include/ui.h | 2 +- src/ui/tree.c | 60 ++++++++++++++-------------- src/ui/window.c | 102 +++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 120 insertions(+), 44 deletions(-) diff --git a/include/ui.h b/include/ui.h index 4770240..fdeaaf1 100644 --- a/include/ui.h +++ b/include/ui.h @@ -124,5 +124,5 @@ void ui_toggle_sidebar (); void print_test_in_tree_dot_c(void); -// GtkWidget *create_a_button_to_click_on(GemGraphClientWindow *window); +GtkWidget *create_a_button(GemGraphClientWindow *window); diff --git a/src/ui/tree.c b/src/ui/tree.c index 8a9b7b9..3cb0f38 100644 --- a/src/ui/tree.c +++ b/src/ui/tree.c @@ -300,33 +300,35 @@ struct _TreeItem -/* static void */ -/* print_hello (GtkWidget *widget, */ -/* gpointer data) */ -/* { */ -/* static int nb; */ -/* nb++; */ -/* printf("Button clicked (n = %d)\n", nb); */ -/* } */ - -/* GtkWidget *create_a_button_to_click_on(GemGraphClientWindow *my_window) */ -/* { */ -/* GtkWidget *button; */ -/* const char *text = "Hello ! I'm the new button. Click me !"; */ - -/* button = gtk_button_new_with_label(text); */ -/* gtk_box_append(GTK_BOX(my_window->runlib_objects), button); */ -/* gtk_widget_show(button); */ - -/* g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); */ - -/* printf("tree.c > print test() \ */ -/* ------------------------------------\nClick the 'new button' in the GtkBox.\n"); */ - -/* return button; */ -/* } */ - -void print_test_in_tree_dot_c() { - printf("tree.c > print test() \ -------------------------------------\nClick the 'new button' in the GtkBox.\n"); +static void +print_hello (GtkWidget *widget, + gpointer data) +{ + static int nb; + nb++; + printf("From 'tree.c', > button clicked (n = %d)\n", nb); } + +GtkWidget *create_a_button(GemGraphClientWindow *my_window) +{ + GtkWidget *button; + const char *text = "Hello ! I'm the new button. Click me !"; + + button = gtk_button_new_with_label(text); + /* gtk_box_append(GTK_BOX(my_window->runlib_objects), button); */ + /* src/ui/tree.c:318:37: error: invalid use of incomplete typedef 'GemGraphClientWindow' */ + /* {aka 'struct _GemGraphClientWindow'} */ + gtk_widget_show(button); + + g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + + printf("--------------------------------------------------------- tree.c line 325\n\ +Current obstacle : invalid use of incomplete typedef 'GemGraphClientWindow'\n\ +How to enable the 'tree.c' code (line 318) to create widgets in the main window ?\n\ + (In the meanwhile, you can click the 'new button' in the GtkBox.)\n\ +NB The speed makefile bug has been fixed in branch [devel]\n\ + (but not here, in [origin/dev/ui-simple-tree])\n"); + + return button; +} + diff --git a/src/ui/window.c b/src/ui/window.c index 6f4f55a..be4e7fe 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -22,10 +22,14 @@ * along with this program. If not, see . */ +#include +#include #include #include #include +G_GNUC_BEGIN_IGNORE_DEPRECATIONS + #include "../../include/base.h" #include "../../include/ui.h" @@ -50,7 +54,7 @@ float rotation_angles[N_AXIS] = { 0.0 }; // Rotation angles on each axis GtkWidget *gl_area = NULL; -static GemGraphClientWindow *window; +GemGraphClientWindow *window; /* -------------------------------------------------------------------------- */ @@ -70,6 +74,7 @@ struct _GemGraphClientWindow GtkLabel *toast_text; GtkGLArea *run_glarea; GtkBox *run_controls; + // GtkButton *my_button; /* Stack objects */ GtkStack *runlib_stack; @@ -108,6 +113,7 @@ static void gem_graph_client_window_class_init(GemGraphClientWindowClass *klass) gtk_widget_class_bind_template_child(widget_class, GemGraphClientWindow, run_controls); gtk_widget_class_bind_template_child(widget_class, GemGraphClientWindow, runlib_stack); gtk_widget_class_bind_template_child(widget_class, GemGraphClientWindow, runlib_objects); +// gtk_widget_class_bind_template_child(widget_class, GemGraphClientWindow, my_button); } static void gem_graph_client_window_init(GemGraphClientWindow *self) @@ -124,7 +130,8 @@ static void gem_graph_client_window_init(GemGraphClientWindow *self) /* -------------------------------------------------------------------------- */ -GtkWidget *create_a_button_to_click_on(GemGraphClientWindow *my_window); +static void create_my_list_model(); +GtkWidget *create_a_button_in_(GemGraphClientWindow *my_window); void ui_set_stack(const char *mode) { @@ -141,10 +148,9 @@ void ui_set_stack(const char *mode) break; case 'r': gtk_menu_button_set_icon_name(window->main_button_mode, "system-run-symbolic"); - create_a_button_to_click_on(window); - // let_s_scroll_it_guys(); - print_test_in_tree_dot_c(); - + create_my_list_model(); + create_a_button_in_(window); + create_a_button(window); // <<< This function is in tree.c break; case 'p': gtk_menu_button_set_icon_name(window->main_button_mode, "x-office-presentation-symbolic"); @@ -262,20 +268,88 @@ print_hello (GtkWidget *widget, { static int nb; nb++; - printf("Button clicked (n = %d)\n", nb); + printf("From 'window.c', > button clicked (n = %d)\n", nb); } -GtkWidget *create_a_button_to_click_on(GemGraphClientWindow *my_window) +GtkWidget *create_a_button_in_(GemGraphClientWindow *my_window) { - GtkWidget *button; + GtkWidget *my_button; const char *text = "Hello ! I'm the new button. Click me !"; - button = gtk_button_new_with_label(text); - gtk_box_append(GTK_BOX(my_window->runlib_objects), button); - gtk_widget_show(button); + my_button = gtk_button_new_with_label(text); + gtk_box_append(GTK_BOX(my_window->runlib_objects), my_button); + gtk_widget_show(my_button); - g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL); + g_signal_connect (my_button, "clicked", G_CALLBACK (print_hello), NULL); - return button; + return my_button; +} + + + + +enum +{ + STRING_COLUMN, + NUM_COLUMNS +}; + + +/* TreeItem structure */ +typedef struct _TreeItem TreeItem; +struct _TreeItem +{ + const char *label; + TreeItem *children; +}; + +// tree data +static TreeItem E[] = {NULL}, F[] = {NULL}, G[] = {NULL}, H[] = {NULL}; +static TreeItem I[] = {NULL}, K[] = {NULL}, N[] = {NULL}, M[] = {NULL}; +static TreeItem L[] = {{"M", M}, {"N", N}, {NULL }}, J[] = {{"L", L}, {NULL}}; +static TreeItem D[] = {{"I", I}, {"J", J}, {"K", K}, {NULL}}; +static TreeItem C[] = {{"F", F}, {"G", G}, {"H", H}, {NULL}}; +static TreeItem A[] = {{"D", D}, {NULL}}, B[] = {{"E", E}, {NULL}}; +static TreeItem R[] = {{"A", A}, {"B", B}, {"C", C}, {NULL}}; +static TreeItem O[] = {{"ROOT", R}, {NULL}}; // Artefact added for symmetry + + +static GListStore *create_node_recursive (GListModel *model, // GListStore* g_list_store_new (GType item_type) + TreeItem *current_item, + GtkTreeIter *iter_parent, + int depth) +{ + if (model == NULL) + printf("tree.c > print test() in : create_node_recursive()"); + + GtkTreeIter iter; + + if (model == NULL) + model = gtk_tree_store_new (NUM_COLUMNS, G_TYPE_STRING); + + while (current_item->label) { + if (1) printf("[%d] %s, ", depth, current_item->label); + gtk_tree_store_append (model, &iter, iter_parent); + gtk_tree_store_set (model, &iter, STRING_COLUMN, current_item->label, -1); + + if (current_item->children) + create_node_recursive (model, current_item->children, &iter, depth + 1); + else + break; + + current_item++; + } + + if (depth == 0) + return G_LIST_MODEL(model); // can cast to GListModel or to GtkTreeStore ? + else + return NULL; +} + +static void create_my_list_model() +{ + GListStore *my_list_model = NULL; + my_list_model = create_node_recursive (my_list_model, O, NULL, 0); + printf(" << tree is OK\n"); }