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

259 lines
8.2 KiB
C
Raw Normal View History

/*
* Gem-graph OpenGL experiments
*
* Desc: User interface functions
*
* Copyright (C) 2023 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 <unistd.h>
#include <gtk-4.0/gtk/gtk.h>
#include <glib-2.0/glib.h>
#include "../../include/base.h"
#include "../../include/ui.h"
#include <gtk/gtk.h>
/*
2024-01-05 17:46:14 +01:00
* ! WARNING ! Ici règne le désordre le plus complet ! WARNING !
* Attention vous entrez à vos risques et périls dans un dangereux laboratoire.
* Ici, rien n'est balisé. Tout peut arriver. Le code est encombré de déchets inutiles.
* Les commentaires sont inadaptés, incompréhensibles et possiblement nuisibles.
* (en tout cas, inutilisables...)
* Nul ne sait ce qui s'y passe et l'auteur, possiblement frappé d'idiotie,
* ne sait dire lui-même ce qu'il veut, ce qu'il fait ou a fait et ce qu'il fera...
*/
enum
{
COL_FIRST_NAME = 0,
COL_LAST_NAME,
NUM_COLS
} ;
2024-01-05 17:46:14 +01:00
GtkWidget *view;
GtkListStore *list_store;
GtkTreeStore *tree_store;
GtkTreeIter iter, child;
static GtkTreeModel *
create_and_fill_model (void)
{
2024-01-05 17:46:14 +01:00
// GtkTreeStore *tree_store;
GtkTreeIter toplevel;//, child;
2024-01-05 17:46:14 +01:00
tree_store = gtk_tree_store_new(NUM_COLS, G_TYPE_STRING, G_TYPE_STRING);
/* Append a top level row and leave it empty */
2024-01-05 17:46:14 +01:00
gtk_tree_store_append(tree_store, &toplevel, NULL);
gtk_tree_store_set(tree_store, &toplevel,
COL_FIRST_NAME, "RanTanPlan",
2024-01-05 17:46:14 +01:00
COL_LAST_NAME, "(a stupid dog)",
-1);
2024-01-05 17:46:14 +01:00
gtk_tree_store_append(tree_store, &toplevel, NULL);
gtk_tree_store_set(tree_store, &toplevel,
COL_FIRST_NAME, "Ma",
COL_LAST_NAME, "Dalton",
-1);
/* Append a second top level row, and fill it with some data */
2024-01-05 17:46:14 +01:00
gtk_tree_store_append(tree_store, &toplevel, NULL);
gtk_tree_store_set(tree_store, &toplevel,
COL_FIRST_NAME, "Joe",
COL_LAST_NAME, "Dalton",
-1);
2024-01-05 17:46:14 +01:00
gtk_tree_store_append(tree_store, &toplevel, NULL);
gtk_tree_store_set(tree_store, &toplevel,
COL_FIRST_NAME, "Jack",
COL_LAST_NAME, "Dalton",
-1);
2024-01-05 17:46:14 +01:00
gtk_tree_store_append(tree_store, &toplevel, NULL);
gtk_tree_store_set(tree_store, &toplevel,
COL_FIRST_NAME, "William",
COL_LAST_NAME, "Dalton",
-1);
/* Append a child to the second top level row, and fill in some data */
2024-01-05 17:46:14 +01:00
gtk_tree_store_append(tree_store, &child, &toplevel);
gtk_tree_store_set(tree_store, &child,
COL_FIRST_NAME, "Averell",
COL_LAST_NAME, "Dalton",
-1);
2024-01-05 17:46:14 +01:00
return GTK_TREE_MODEL(tree_store);
}
/***********************************************************
* *
* Going through every row in a list store *
* *
***********************************************************/
void
traverse_list_store (GtkListStore *liststore)
{
gboolean valid;
g_return_if_fail ( liststore != NULL );
/* Get first row in list store */
valid = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(liststore), &iter);
while (valid)
{
/* ... do something with that row using the iter ... */
/* (Here column 0 of the list store is of type G_TYPE_STRING) */
gtk_list_store_set(liststore, &iter, 0, "Joe", -1);
/* Make iter point to the next row in the list store */
valid = gtk_tree_model_iter_next(GTK_TREE_MODEL(liststore), &iter);
}
}
/*************************************************************
* *
* Converting a GtkTreePath into a GtkTreeIter *
* *
*************************************************************/
/*************************************************************
* *
* onTreeViewRowActivated: a row has been double-clicked *
* *
*************************************************************/
void
onTreeViewRowActivated (GtkTreeView *view, GtkTreePath *path,
GtkTreeViewColumn *col, gpointer userdata)
{
GtkTreeModel *model;
model = gtk_tree_view_get_model(view);
if (gtk_tree_model_get_iter(model, &iter, path))
{
gchar *name;
gtk_tree_model_get(model, &iter, "ANY_COLUMN_NAME", &name, -1);
g_print ("The row containing the name '%s' has been double-clicked.\n", name);
g_free(name);
}
}
void
2023-11-25 08:14:39 +01:00
tree_c_test(void)
{
2024-01-05 17:46:14 +01:00
GtkTreeViewColumn *col;
GtkCellRenderer *renderer;
GtkWidget *view;
GtkTreeModel *model;
view = gtk_tree_view_new();
col = gtk_tree_view_column_new();
gtk_tree_view_column_set_title(col, "First Name");
list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_UINT);
2024-01-05 17:46:14 +01:00
gtk_tree_view_set_model(GTK_TREE_VIEW(view), GTK_TREE_MODEL(list_store));
// g_object_unref(liststore);
/* Append empty rows to the list store. Iter will point to the new row */
gtk_list_store_append(list_store, &iter);
gtk_list_store_append(list_store, &iter);
gtk_list_store_append(list_store, &iter);
tree_store = gtk_tree_store_new(1, G_TYPE_STRING);
/* Append an empty top-level row to the tree store.
* Iter will point to the new row */
gtk_tree_store_append(tree_store, &iter, NULL);
/* Append another empty top-level row to the tree store.
* Iter will point to the new row */
gtk_tree_store_append(tree_store, &iter, NULL);
/* Append a child to the row we just added.
* Child will point to the new row */
gtk_tree_store_append(tree_store, &child, &iter);
/* Get the first row, and add a child to it as well (could have been done
* right away earlier of course, this is just for demonstration purposes) */
if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(tree_store), &iter))
{
2023-11-25 08:14:39 +01:00
/* Child will point to new row */
gtk_tree_store_append(tree_store, &child, &iter);
}
2023-11-25 08:14:39 +01:00
else g_error("Oops, we should have a first row in the tree store!\n");
}
2024-01-05 17:46:14 +01:00
void print_test_in_tree_dot_c(void)
2023-11-25 08:14:39 +01:00
{
2024-01-05 17:46:14 +01:00
printf("tree.c > print test() \
------------------------------------\n\
A button is now in the GtkBox and an action is associated to it.\n\n");
}
/*
2024-01-05 17:46:14 +01:00
https://mesonbuild.com/Manual.html
https://blog.gtk.org/2020/06/08/more-on-lists-in-gtk-4/
https://docs.gtk.org/gtk4/section-list-widget.html
https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Tree_Models
https://stackoverflow.com/questions/74556059/how-to-build-a-tree-in-gtk4-4-10
https://stackoverflow.com/questions/51002454/how-to-list-all-the-rows-of-a-gtk-treeview
https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Columns_and_Renderer
*/
2024-01-05 17:46:14 +01:00
static GListModel *
create_settings_model (gpointer item,
gpointer unused)
{
GSettings *settings = item;
char **schemas;
GListStore *result;
guint i;
schemas = g_settings_list_children (settings);
if (schemas == NULL || schemas[0] == NULL)
{
g_free (schemas);
return NULL;
}
result = g_list_store_new (G_TYPE_SETTINGS);
for (i = 0; schemas[i] != NULL; i++)
{
GSettings *child = g_settings_get_child (settings, schemas[i]);
g_list_store_append (result, child);
g_object_unref (child);
}
g_strfreev (schemas);
return G_LIST_MODEL (result);
}