GTK4_GG_hack/hot.c

162 lines
8.8 KiB
C
Raw Normal View History

2024-04-27 16:16:21 +02:00
#include <stdio.h>
#include <gtk-4.0/gtk/gtk.h>
2024-05-08 07:50:04 +02:00
#include <assert.h>
2024-05-10 06:19:20 +02:00
#include "contain.h"
#include "texts.h"
2024-04-29 23:43:04 +02:00
2024-04-30 18:59:44 +02:00
// https://docs.gtk.org/gtk4/visual_index.html < widgets gallery
// https://docs.gtk.org/gtk4/section-text-widget.html
// https://docs.gtk.org/gtk4/class.Widget.html#height-for-width-geometry-management
// GTK_ORIENTATION_VERTICAL GTK_ORIENTATION_HORIZONTAL
static int run_edit_mode_val = 0;
void set_run_edit_mode(int prescribed_mode) {run_edit_mode_val = prescribed_mode;}
int get_run_edit_mode () {return run_edit_mode_val;}
struct TreeNode_t {gchar *text; struct TreeNode_t *child, *next;};
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 struct TreeNode_t *create_user_tree_node (const gchar* text){
struct TreeNode_t *node = g_malloc0 (sizeof(struct TreeNode_t));
node->text = g_strdup (text);
node->child = NULL; // if (0) printf("create_user_tree_node %s\n", text);
return node;
}
static void let_us_create_a_complex_useless_and_expensive_tree (struct TreeNode_t *tree_root) {
struct TreeNode_t *a = create_user_tree_node("We, the people");add_child_node(tree_root, a);
struct TreeNode_t *b = create_user_tree_node("in Order to"); add_child_node(tree_root, b);
struct TreeNode_t *c = create_user_tree_node("do establish"); add_child_node(tree_root, c);
struct TreeNode_t *aa = create_user_tree_node("aware of"); add_child_node(a, aa);
struct TreeNode_t *aaa = create_user_tree_node("our rights"); add_child_node(aa, aaa);
struct TreeNode_t *aab = create_user_tree_node("our duties"); add_child_node(aa, aab);
struct TreeNode_t *aaaa = create_user_tree_node("read"); add_child_node(aaa, aaaa);
struct TreeNode_t *aaab = create_user_tree_node("write"); add_child_node(aaa, aaab);
struct TreeNode_t *aaac = create_user_tree_node("copy"); add_child_node(aaa, aaac);
struct TreeNode_t *aaad = create_user_tree_node("edit"); add_child_node(aaa, aaad);
struct TreeNode_t *aaaba= create_user_tree_node("code"); add_child_node(aaab, aaaba);
struct TreeNode_t *aaabb= create_user_tree_node("language"); add_child_node(aaab, aaabb);
struct TreeNode_t *aaabc= create_user_tree_node("any art..."); add_child_node(aaab, aaabc);
struct TreeNode_t *aaadb= create_user_tree_node("publish"); add_child_node(aaac, aaadb);
struct TreeNode_t *aaada= create_user_tree_node("create"); add_child_node(aaad, aaada);
struct TreeNode_t *aaba = create_user_tree_node("learn"); add_child_node(aab, aaba);
struct TreeNode_t *aabb = create_user_tree_node("help"); add_child_node(aab, aabb);
struct TreeNode_t *ba = create_user_tree_node("promote"); add_child_node(b, ba);
struct TreeNode_t *bb = create_user_tree_node("individual"); add_child_node(b, bb);
struct TreeNode_t *bc = create_user_tree_node("and common"); add_child_node(b, bc);
struct TreeNode_t *bca = create_user_tree_node("education"); add_child_node(bc, bca);
struct TreeNode_t *bcb = create_user_tree_node("mutual"); add_child_node(bc, bcb);
struct TreeNode_t *bcc = create_user_tree_node("support"); add_child_node(bc, bcc);
struct TreeNode_t *bcd = create_user_tree_node("health"); add_child_node(bc, bcd);
struct TreeNode_t *bcda = create_user_tree_node("mental"); add_child_node(bcd, bcda);
struct TreeNode_t *bcdb = create_user_tree_node("physical"); add_child_node(bcd, bcdb);
struct TreeNode_t *ca = create_user_tree_node("free"); add_child_node(c, ca);
struct TreeNode_t *cb = create_user_tree_node("code"); add_child_node(c, cb);
struct TreeNode_t *cc = create_user_tree_node("access"); add_child_node(c, cc);
}
2024-05-22 09:32:17 +02:00
void on_user_tree_expander_toggled(GtkExpander *expander, gpointer user_data)
{
// This is a conceptual callback for when an expander is toggled
GtkTreeListRow *row = GTK_TREE_LIST_ROW (user_data);
gboolean is_expanded = gtk_tree_list_row_get_expanded(row);
gtk_tree_list_row_set_expanded (row, !is_expanded);
}
static void on_bind_user_tree_factory (GtkSignalListItemFactory *factory, GObject* object, gpointer user_data){
GtkListItem *list_item = GTK_LIST_ITEM (object); assert (list_item);
GtkTreeListRow *row = gtk_list_item_get_item (list_item); assert (row);
// if (row != NULL) {
const gchar *text = gtk_string_object_get_string (GTK_STRING_OBJECT (gtk_tree_list_row_get_item (row)));
GtkWidget *expander = gtk_list_item_get_child (list_item);
gtk_expander_set_label (GTK_EXPANDER (expander), text);
g_signal_handlers_disconnect_by_func(expander, G_CALLBACK (on_user_tree_expander_toggled), row);
g_signal_connect(expander, "activate", G_CALLBACK (on_user_tree_expander_toggled), row);
gtk_widget_set_margin_start(expander, gtk_tree_list_row_get_depth(row)*20);
gboolean is_expanded = gtk_tree_list_row_get_expanded(row);
if (0) printf("[on_bind_user_tree_factory] row content is [%s] and expander is [%d]\n", text, is_expanded);
// } else if (0) printf("[on_bind_user_tree_factory] row == NULL\n");
}
void on_setup_user_tree_factory (GtkSignalListItemFactory *factory,
GObject* object, gpointer user_data){
GtkWidget* expander = gtk_expander_new (NULL);
gtk_list_item_set_child (GTK_LIST_ITEM (object), expander);
if (0) printf("[on_setup_user_tree_factory] here is an expander\n");
}
2024-05-22 09:32:17 +02:00
static GListModel* get_user_tree_model_child (struct TreeNode_t *parent){
GtkStringList *list = NULL;
if (parent) {
2024-05-22 09:32:17 +02:00
if (0) printf("[get_user_tree_model_child] here is %s content : ", parent->text);
struct TreeNode_t *child = parent->child;
if (child) {list = gtk_string_list_new(NULL);}
while(child) {
gtk_string_list_append(list, child->text);
if (0) printf("%s ", child->text);
child = child->next;
}
2024-05-22 22:01:08 +02:00
} // else printf("hot.c GListModel* get_user_tree_model_child (struct TreeNode_t *parent) child = %d \n", parent);
if (0) printf("\n");
return G_LIST_MODEL(list);
}
GListModel* get_user_tree_model (GObject *item, gpointer root){
struct TreeNode_t *cur = (struct TreeNode_t *)root;
struct TreeNode_t *parent = root;
const gchar *string = gtk_string_object_get_string (GTK_STRING_OBJECT (item));
while (cur) {
if (strcmp(string, cur->text) == 0) break;
cur = cur->next;
if (cur == NULL) {cur = parent->child; parent = cur;}
}
if (0) printf("[get_user_user_tree_model] looked for %s in %s item\n", cur->text, string);
// ! WARNING ! TODO CUR EST L'ENFANT, MAINTENANT DONC, SI CUR EST UNE FEUILLE, JE N'ATTEINDRAI PAS SON ENFANT
2024-05-22 09:32:17 +02:00
return get_user_tree_model_child (cur);
}
GtkScrolledWindow *get_user_rules_tree (){
2024-05-22 09:32:17 +02:00
struct TreeNode_t *tree_root = create_user_tree_node("root");
let_us_create_a_complex_useless_and_expensive_tree (tree_root);
2024-05-16 17:29:11 +02:00
GtkStringList *model = gtk_string_list_new(NULL);
gtk_string_list_append (model, tree_root->text);
GtkSignalListItemFactory *factory = GTK_SIGNAL_LIST_ITEM_FACTORY (gtk_signal_list_item_factory_new());
g_signal_connect (factory, "setup", G_CALLBACK(on_setup_user_tree_factory), NULL);
g_signal_connect (factory, "bind", G_CALLBACK(on_bind_user_tree_factory), NULL);
2024-05-16 17:29:11 +02:00
GtkTreeListModel *tree_model = gtk_tree_list_model_new(
G_LIST_MODEL (model),
FALSE, // Passthrough - False in actual usage with dynamic children retrieval
TRUE, // FALSE, // autoexpand
(GtkTreeListModelCreateModelFunc) &get_user_tree_model,
2024-05-16 17:29:11 +02:00
tree_root,
NULL // (GDestroyNotify) free_user_tree_node
2024-05-16 17:29:11 +02:00
);
2024-05-16 17:29:11 +02:00
GtkSingleSelection *selection_model = gtk_single_selection_new (G_LIST_MODEL (tree_model));
gtk_single_selection_set_autoselect (selection_model, FALSE);
gtk_single_selection_set_can_unselect (selection_model, TRUE);
GtkWidget *list_view = gtk_list_view_new (GTK_SELECTION_MODEL (selection_model), GTK_LIST_ITEM_FACTORY (factory));
2024-05-22 09:32:17 +02:00
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (gtk_scrolled_window_new());
// Allocation height too small. Tried to allocate 1922x1030, but GtkNotebook 0x25cd4c0 needs at least 1922x1064.
// even if I remove (comment) the next line :
gtk_scrolled_window_set_child (scrolled_window, list_view);
2024-05-22 09:32:17 +02:00
gtk_scrolled_window_set_policy (scrolled_window, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_widget_set_vexpand (GTK_WIDGET (scrolled_window), TRUE);
gtk_widget_set_visible (GTK_WIDGET (scrolled_window), TRUE);
gtk_widget_set_visible (GTK_WIDGET (list_view), TRUE);
2024-05-22 09:32:17 +02:00
return scrolled_window;
}