before completing my first experimental GTK4 tree

This commit is contained in:
Jean Sirmai 2024-05-17 09:05:46 +02:00
parent 2af51060e7
commit 2bd8febffc
Signed by: jean
GPG Key ID: FB3115C340E057E3
5 changed files with 40 additions and 52 deletions

64
hot.c
View File

@ -15,6 +15,26 @@
static struct TreeNode_t {gchar *text; struct TreeNode_t *child, *next;}; static struct TreeNode_t {gchar *text; struct TreeNode_t *child, *next;};
static struct TreeNode_t *create_tree_node (const gchar* text){
struct TreeNode_t *node = g_malloc0 (sizeof(struct TreeNode_t));
node->text = g_strdup(text);
node->child = NULL; // printf("create_tree_node %s\n", text);
return node;
}
/*static void on_experimental_expander_toggled(GtkExpander *expander, gpointer user_data){
gtk_expander_set_expanded (expander, gtk_expander_get_expanded (expander));
printf("> %s\n", gtk_expander_get_label (user_data));
}*/
static void add_child_node (struct TreeNode_t *parent, struct TreeNode_t *child){
if (parent->child) {
struct TreeNode_t *cur = parent->child;
while (cur && cur->next) {cur = cur->next;}
cur->next = child;
} else parent->child = child;
}
static GListModel* ui_experimental_tree_get_children_model (struct TreeNode_t *parent){ static GListModel* ui_experimental_tree_get_children_model (struct TreeNode_t *parent){
GtkStringList *list = NULL; GtkStringList *list = NULL;
if (parent) { if (parent) {
@ -41,7 +61,7 @@ static GListModel* ui_experimental_tree_create_model_func (GObject *item, gpoint
if (cur == NULL) {cur = parent->child; parent = cur;} if (cur == NULL) {cur = parent->child; parent = cur;}
} }
printf("[ui_experimental_tree_create_model_func] looked for %s in %s item\n", cur->text, string); printf("[ui_experimental_tree_create_model_func] looked for %s in %s item\n", cur->text, string);
return ui_experimental_tree_get_children_model(cur); return ui_experimental_tree_get_children_model (cur);
} }
static void on_experimental_tree_bind_factory (GtkSignalListItemFactory *factory, GObject* object, gpointer user_data){ static void on_experimental_tree_bind_factory (GtkSignalListItemFactory *factory, GObject* object, gpointer user_data){
@ -55,49 +75,25 @@ static void on_experimental_tree_bind_factory (GtkSignalListItemFactory *factory
// g_signal_connect(expander, "activate", G_CALLBACK(on_tree_expander_toggled), row); // g_signal_connect(expander, "activate", G_CALLBACK(on_tree_expander_toggled), row);
gtk_widget_set_margin_start(expander, gtk_tree_list_row_get_depth(row)*20); gtk_widget_set_margin_start(expander, gtk_tree_list_row_get_depth(row)*20);
gboolean is_expanded = gtk_tree_list_row_get_expanded(row); gboolean is_expanded = gtk_tree_list_row_get_expanded(row);
printf("[on_experimental_tree_bind_factory] row != NULL here is %s content and expander is %d\n", text, is_expanded); printf("[on_experimental_tree_bind_factory] row content is [%s] and expander is [%d]\n", text, is_expanded);
} else printf("[on_experimental_tree_bind_factory] row == NULL\n"); } else printf("[on_experimental_tree_bind_factory] row == NULL\n");
} }
static void on_experimental_expander_toggled(GtkExpander *expander, gpointer user_data){
gtk_expander_set_expanded (expander, gtk_expander_get_expanded (expander));
printf("> %s\n", gtk_expander_get_label (user_data));
}
void on_experimental_tree_setup_factory (GtkSignalListItemFactory *factory, void on_experimental_tree_setup_factory (GtkSignalListItemFactory *factory,
GObject* object, GObject* object, gpointer user_data){
gpointer user_data)
{
GtkWidget* expander = gtk_expander_new (NULL); GtkWidget* expander = gtk_expander_new (NULL);
gtk_list_item_set_child (GTK_LIST_ITEM(object), expander); gtk_list_item_set_child (GTK_LIST_ITEM(object), expander);
printf("[on_experimental_tree_setup_factory] here is an expander\n"); printf("[on_experimental_tree_setup_factory] here is an expander\n");
} }
static struct TreeNode_t *create_tree_node (const gchar* text){
struct TreeNode_t *node = g_malloc0 (sizeof(struct TreeNode_t));
node->text = g_strdup(text);
node->child = NULL;
return node;
}
static void add_child_node (struct TreeNode_t *parent, struct TreeNode_t *child){
if (parent->child) {
struct TreeNode_t *cur = parent->child;
while (cur && cur->next) {
cur = cur->next;
}
cur->next = child;
} else parent->child = child;
}
void create_experimental_tree (GtkBox *experimental_box){ void create_experimental_tree (GtkBox *experimental_box){
struct TreeNode_t *tree_root = create_tree_node("hello"); struct TreeNode_t *tree_root = create_tree_node("hello");
struct TreeNode_t *child = create_tree_node("it's me"); struct TreeNode_t *it_s_me = create_tree_node("it's me"); add_child_node(tree_root, it_s_me);
add_child_node(tree_root, child); struct TreeNode_t *remember = create_tree_node("remember"); add_child_node(it_s_me, remember);
GtkStringList *model = gtk_string_list_new(NULL); GtkStringList *model = gtk_string_list_new(NULL);
gtk_string_list_append (model, tree_root->text); gtk_string_list_append (model, tree_root->text);
GtkSignalListItemFactory *factory = GTK_SIGNAL_LIST_ITEM_FACTORY (gtk_signal_list_item_factory_new ()); GtkSignalListItemFactory *factory = GTK_SIGNAL_LIST_ITEM_FACTORY (gtk_signal_list_item_factory_new());
g_signal_connect (factory, "setup", G_CALLBACK(on_experimental_tree_setup_factory), NULL); g_signal_connect (factory, "setup", G_CALLBACK(on_experimental_tree_setup_factory), NULL);
g_signal_connect (factory, "bind", G_CALLBACK(on_experimental_tree_bind_factory), NULL); g_signal_connect (factory, "bind", G_CALLBACK(on_experimental_tree_bind_factory), NULL);
@ -109,15 +105,17 @@ void create_experimental_tree (GtkBox *experimental_box){
tree_root, tree_root,
NULL // (GDestroyNotify) free_tree_node NULL // (GDestroyNotify) free_tree_node
); );
GtkSingleSelection *selection_model = gtk_single_selection_new (G_LIST_MODEL (tree_model)); GtkSingleSelection *selection_model = gtk_single_selection_new (G_LIST_MODEL (tree_model));
gtk_single_selection_set_autoselect (selection_model, FALSE); gtk_single_selection_set_autoselect (selection_model, FALSE);
gtk_single_selection_set_can_unselect (selection_model, TRUE); gtk_single_selection_set_can_unselect (selection_model, TRUE);
GtkWidget *list_view = gtk_list_view_new (GTK_SELECTION_MODEL (selection_model), GtkWidget *list_view = gtk_list_view_new (GTK_SELECTION_MODEL (selection_model),
GTK_LIST_ITEM_FACTORY (factory)); GTK_LIST_ITEM_FACTORY (factory));
gtk_widget_set_visible(GTK_WIDGET(experimental_box), TRUE); gtk_box_append(experimental_box, list_view);
gtk_widget_set_visible(GTK_WIDGET(list_view), TRUE);
// gtk_widget_set_visible(GTK_WIDGET(experimental_box), TRUE);
// gtk_widget_set_visible(GTK_WIDGET(list_view), TRUE);
} }

BIN
hot.o

Binary file not shown.

BIN
myprogram

Binary file not shown.

28
tree.c
View File

@ -199,25 +199,15 @@ void ui_create_tree (GtkWidget *target_widget)
assert(target_widget); assert(target_widget);
// AD HOC XXX & no free() struct TreeNode_t *tree_root = create_tree_node("Root"); // AD HOC XXX & no free()
struct TreeNode_t *tree_root = create_tree_node("Root"); struct TreeNode_t *ATP = create_tree_node("ATP"); add_child_node(tree_root, ATP);
struct TreeNode_t *ATP = create_tree_node("ATP"); struct TreeNode_t *ADP = create_tree_node("ADP"); add_child_node(tree_root, ADP);
struct TreeNode_t *ADP = create_tree_node("ADP"); struct TreeNode_t *AMP = create_tree_node("AMP"); add_child_node(tree_root, AMP);
struct TreeNode_t *AMP = create_tree_node("AMP"); struct TreeNode_t *adenine = create_tree_node("adénine"); add_child_node(ATP, adenine);
struct TreeNode_t *adenine = create_tree_node("adénine"); struct TreeNode_t *ribose = create_tree_node("ribose"); add_child_node(adenine, ribose);
struct TreeNode_t *ribose = create_tree_node("ribose"); struct TreeNode_t *PO4H3_1 = create_tree_node("phosphate 1"); add_child_node(ribose, PO4H3_1);
struct TreeNode_t *PO4H3_1 = create_tree_node("phosphate 1"); struct TreeNode_t *PO4H3_2 = create_tree_node("phosphate 2"); add_child_node(PO4H3_1, PO4H3_2);
struct TreeNode_t *PO4H3_2 = create_tree_node("phosphate 2"); struct TreeNode_t *PO4H3_3 = create_tree_node("phosphate 3"); add_child_node(PO4H3_2, PO4H3_3);
struct TreeNode_t *PO4H3_3 = create_tree_node("phosphate 3");
add_child_node(tree_root, ATP);
add_child_node(tree_root, ADP);
add_child_node(tree_root, AMP);
add_child_node(ATP, adenine);
add_child_node(adenine, ribose);
add_child_node(ribose, PO4H3_1);
add_child_node(PO4H3_1, PO4H3_2);
add_child_node(PO4H3_3, PO4H3_3);
model = gtk_string_list_new(NULL); model = gtk_string_list_new(NULL);
gtk_string_list_append(model, tree_root->text); gtk_string_list_append(model, tree_root->text);

BIN
tree.o

Binary file not shown.