gem-graph-client/src/widget/tree.c

122 lines
5.9 KiB
C

/* * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Gem-graph client *
* *
* Tree *
* *
* Copyright © 2021 Libre en Communs <contact@a-lec.org> *
* Copyright © 2021 Adrien Bourmault <neox@a-lec.org> *
* Copyright © 2021 Jean Sirmai <jean@a-lec.org> *
* *
* This file is part of Gem-graph. *
* *
* This program is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Affero General Public License *
* as published by the Free Software Foundation, *
* either version 3 of the License, *
* or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; *
* without even the implied warranty of MERCHANTABILITY *
* or FITNESS FOR A PARTICULAR PURPOSE. *
* See the GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "../../include/signal.h"
#include "../../include/widget.h"
void widget_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;
}
struct TreeNode_t *widget_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 GListModel *get_user_tree_model_child (struct TreeNode_t *parent)
{
GtkStringList *list = NULL;
if (parent) {
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;
}
}
if (0) printf("\n");
return G_LIST_MODEL(list);
}
static 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
return get_user_tree_model_child (cur);
}
void *widget_get_user_rules_tree ()
{
struct TreeNode_t *tree_root = widget_create_user_tree_node("We, the people,");
widget_let_us_create_a_complex_useless_and_expensive_tree (tree_root);
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);
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,
tree_root,
NULL // (GDestroyNotify) free_user_tree_node
);
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));
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);
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);
gtk_widget_set_size_request (GTK_WIDGET (scrolled_window), W_IMAGE_LOCAL * 3, 0);
return scrolled_window;
}