WIP: trying to copy : https://github.com/kriptolix/gtk4_python3_exemples/blob/main/ListView/tree_view.py try if(1) line 65
This commit is contained in:
parent
ef3e64dbc3
commit
ad8ff7d711
|
@ -0,0 +1,189 @@
|
||||||
|
/*
|
||||||
|
* 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 <time.h>
|
||||||
|
#include <gtk-4.0/gtk/gtk.h>
|
||||||
|
#include <gdk/gdk.h>
|
||||||
|
#include <glib-2.0/glib.h>
|
||||||
|
#include "../../include/base.h"
|
||||||
|
#include "../../include/ui.h"
|
||||||
|
|
||||||
|
static void and_now_let_s_climb_that_tree (GtkWidget *in_that_box);
|
||||||
|
static void on_destroy (GtkWidget *widget, gpointer data) {if (data) g_print (data); exit(0);}
|
||||||
|
void on_activate_window_creation (GtkApplication *app, gpointer data)
|
||||||
|
{
|
||||||
|
GtkWidget *that_window = gtk_application_window_new (gtk_application_new ("org.gtk.test", 0));
|
||||||
|
g_signal_connect (G_OBJECT (that_window), "destroy", G_CALLBACK(on_destroy),
|
||||||
|
"sand_box.c / on_activate_window_creation() > exit(0) closes window.\n\n\
|
||||||
|
GtkTreeListRow is used by GtkTreeListModel to represent items.\
|
||||||
|
It allows navigating the model as a tree and modify the state of rows.\n\
|
||||||
|
https://docs.gtk.org/gtk4/class.TreeListRow.html\n\
|
||||||
|
assertion 'GTK_IS_TREE_LIST_MODEL (self)' failed (ligne 69)\n\n\
|
||||||
|
https://github.com/kriptolix/gtk4_python3_exemples/blob/main/ListView/tree_view.py\n");
|
||||||
|
gtk_window_set_title (GTK_WINDOW(that_window), "Ignition !");
|
||||||
|
gtk_window_set_default_size (GTK_WINDOW(that_window), 180, 140);
|
||||||
|
GtkWidget *my_scrolling_thing = gtk_scrolled_window_new ();
|
||||||
|
gtk_window_set_child (GTK_WINDOW (that_window), my_scrolling_thing);
|
||||||
|
gtk_widget_show (that_window);
|
||||||
|
and_now_let_s_climb_that_tree (my_scrolling_thing);
|
||||||
|
if (data) {g_print (data); g_print (" and is displayed by : sand_box.c / on_activate_window_creation()\n");}
|
||||||
|
}
|
||||||
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
static void on_setup_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data)
|
||||||
|
/* trying to copy : https://github.com/kriptolix/gtk4_python3_exemples/blob/main/ListView/tree_view.py */
|
||||||
|
/* def setup(widget, item): */
|
||||||
|
/* """Setup the widget to show in the Gtk.Listview""" */
|
||||||
|
/* label = Gtk.Label() */
|
||||||
|
/* expander = Gtk.TreeExpander.new() */
|
||||||
|
/* expander.set_child(label) */
|
||||||
|
/* item.set_child(expander) */
|
||||||
|
{
|
||||||
|
GtkWidget *my_label = gtk_label_new ("here am I"); // <<< compare with : gtk_label_editable_new("..."); !!
|
||||||
|
if (1) // some condition on parent and child, I presume.
|
||||||
|
gtk_list_item_set_child (my_list_item, my_label); // releasing (unref) isn't necessary for my_label.
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_bind_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data)
|
||||||
|
/* trying to copy : https://github.com/kriptolix/gtk4_python3_exemples/blob/main/ListView/tree_view.py */
|
||||||
|
/* def bind(widget, item): """bind data from the store object to the widget""" */
|
||||||
|
/* expander = item.get_child() */
|
||||||
|
/* label = expander.get_child() */
|
||||||
|
/* row = item.get_item() */
|
||||||
|
/* expander.set_list_row(row) */
|
||||||
|
/* obj = row.get_item() */
|
||||||
|
/* label.set_label(obj.data) */
|
||||||
|
{
|
||||||
|
GtkWidget *my_expander = gtk_list_item_get_child (my_list_item);
|
||||||
|
GtkWidget *my_label = gtk_list_item_get_child (my_list_item);
|
||||||
|
// GtkWidget *my_label = gtk_list_item_get_child (my_expander);
|
||||||
|
// GtkTreeListRow *my_row = gtk_tree_list_model_get_row (my_list_item, 0);
|
||||||
|
/* my_string_object is owned by the instance. Caller mustn't change or destroy it. */
|
||||||
|
GtkStringObject *my_string_object = gtk_list_item_get_item (my_list_item);
|
||||||
|
/* The string returned by gtk_string_object_get_string is owned by the instance. */
|
||||||
|
gtk_label_set_text (GTK_LABEL (my_label), gtk_string_object_get_string (my_string_object));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void on_selection_change (GtkStringList *my_selection_model, guint position, guint n_items, gpointer user_data)
|
||||||
|
{
|
||||||
|
// GtkTreeListRow *my_row = gtk_tree_list_model_get_row (my_selection_model, position); << assertion 'GTK_IS_TREE_LIST_MODEL (self)' failed
|
||||||
|
// printf("(line 65) gtk_tree_list_row_get_position (GtkTreeListRow* self) = %d\n", gtk_tree_list_row_get_position (my_selection_model));
|
||||||
|
printf("list size = %d item type = %ld\
|
||||||
|
position (premier élement ayant changé d'état) = %2d %7s\
|
||||||
|
nombre d'éléments ayant changé d'état = %2d\n",
|
||||||
|
g_list_model_get_n_items (G_LIST_MODEL (my_selection_model)),
|
||||||
|
g_list_model_get_item_type(G_LIST_MODEL (my_selection_model)),
|
||||||
|
position,
|
||||||
|
gtk_string_object_get_string (g_list_model_get_item (G_LIST_MODEL (my_selection_model), position)),
|
||||||
|
n_items);
|
||||||
|
}
|
||||||
|
|
||||||
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
void on_activating_this_expander (GtkExpander *my_expander, gpointer my_data) {
|
||||||
|
printf("on_activating exp. > %s\n", gtk_string_object_get_string (GTK_STRING_OBJECT(my_expander)));
|
||||||
|
}
|
||||||
|
void gtk_expander_set_child (GtkExpander* expander, GtkWidget* child) {
|
||||||
|
printf("gtk_expander_set_child () %s\n", gtk_string_object_get_string (GTK_STRING_OBJECT(child)));
|
||||||
|
}
|
||||||
|
|
||||||
|
GListModel *my_item_create_func (gpointer item, gpointer user_data)
|
||||||
|
{
|
||||||
|
gchar *my_child_array[] = {"101", "202", "303", NULL};
|
||||||
|
GtkStringList *my_child_string_list = gtk_string_list_new ((const char * const *) my_child_array);
|
||||||
|
|
||||||
|
if (strcmp(gtk_string_object_get_string(item), "two")) {
|
||||||
|
GtkWidget *my_expander = gtk_expander_new (NULL);
|
||||||
|
// gtk_tree_expander_set_child (GTK_EXPANDER(my_expander), NULL); // g_list_model_get_item (G_LIST_MODEL (my_child_string_list), 1));
|
||||||
|
gtk_expander_set_child (GTK_EXPANDER(my_expander), NULL);
|
||||||
|
on_activating_this_expander (GTK_EXPANDER(my_expander), NULL);
|
||||||
|
printf("%s ", gtk_string_object_get_string(item));
|
||||||
|
return G_LIST_MODEL (my_child_string_list);
|
||||||
|
}
|
||||||
|
else return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void and_now_let_s_climb_that_tree (GtkWidget *in_that_box)
|
||||||
|
{
|
||||||
|
const gchar *my_array[] = {"zero", "one", "two", "three", "four", NULL};
|
||||||
|
GtkStringList *my_string_list = gtk_string_list_new ((const char * const *) my_array);
|
||||||
|
gtk_string_list_append (my_string_list, "five");
|
||||||
|
GtkMultiSelection *my_selection_model = gtk_multi_selection_new (G_LIST_MODEL (my_string_list));
|
||||||
|
|
||||||
|
/* if (gtk_single_selection_get_selected_item (my_selection_model)) */
|
||||||
|
/* gtk_selection_model_selection_changed (my_selection_model, 0, 1); */
|
||||||
|
|
||||||
|
GtkListItemFactory *my_list_item_factory = gtk_signal_list_item_factory_new ();
|
||||||
|
g_signal_connect (my_list_item_factory, "setup", G_CALLBACK (on_setup_cb), NULL);
|
||||||
|
g_signal_connect (my_list_item_factory, "bind", G_CALLBACK (on_bind_cb), NULL);
|
||||||
|
if (1) { g_signal_connect (my_selection_model, "selection-changed", G_CALLBACK (on_selection_change), NULL);}
|
||||||
|
|
||||||
|
// https://docs.gtk.org/gtk4/callback.TreeListModelCreateModelFunc.html
|
||||||
|
// https://docs.gtk.org/gtk4/method.TreeListRow.set_expanded.html
|
||||||
|
|
||||||
|
srand(time(NULL)); int rank = rand() % g_list_model_get_n_items (G_LIST_MODEL (my_selection_model));
|
||||||
|
GtkTreeListModel *my_tree_list_model =
|
||||||
|
gtk_tree_list_model_new (G_LIST_MODEL (my_string_list), 0, 0, my_item_create_func, NULL, NULL);
|
||||||
|
printf("in : GListModel *item_create_func (gpointer item,...) (line 80) (item n°%d is %s)\n",
|
||||||
|
rank, gtk_string_list_get_string (my_string_list, rank));
|
||||||
|
|
||||||
|
GtkWidget *my_list_view = gtk_list_view_new (GTK_SELECTION_MODEL (my_selection_model), my_list_item_factory);
|
||||||
|
// GtkWidget *my_tree_view = gtk_tree_view_new (); next line : use my_tree_view instead of my_list_view ?
|
||||||
|
gtk_scrolled_window_set_child (GTK_SCROLLED_WINDOW (in_that_box), my_list_view);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//----------------------------------------------------------------| mes notes. |---------------------------------------------
|
||||||
|
|
||||||
|
// guix shell -m manifest.scm && pkg-config --libs gtk4 make clean && clear && time make run
|
||||||
|
//
|
||||||
|
// my local widgets examples are in user/jean/01/GTK4/tree (learning)/The_Gnome_way/demos/gtk-demo
|
||||||
|
// tout ce qui commence par gtk... est un widget ex: "my_selection_model" est un widget ??
|
||||||
|
// factory = GtkSignalListItemFactory(setup_cb, bind_cb) <<<
|
||||||
|
// gtk_widget_set_visible(...) <<<
|
||||||
|
//
|
||||||
|
// GtkColumnView
|
||||||
|
//
|
||||||
|
// https://toshiocp.github.io/Gtk4-tutorial/sec29.html
|
||||||
|
// https://docs.gtk.org/gtk4/section-list-widget.html <<< Les listes (deprec // not deprec)
|
||||||
|
//
|
||||||
|
// >>> https://docs.gtk.org/gtk4/ https://docs.gtk.org/glib/ https://docs.gtk.org/gio/ <<<
|
||||||
|
//
|
||||||
|
// https://docs.gtk.org/#user-interface
|
||||||
|
// https://www.typeerror.org/docs/gtk~4.0/
|
||||||
|
// https://www.typeerror.org/docs/gtk~4.0/gtktreemodel
|
||||||
|
//
|
||||||
|
//
|
||||||
|
/* À trier (pour : on_setup_cb())
|
||||||
|
{
|
||||||
|
GtkWidget *my_label = gtk_editable_label_new ("here am I");
|
||||||
|
GtkStringObject *my_item_string = gtk_string_object_get_string (g_list_model_get_item (G_LIST_MODEL (my_list_item), 1));
|
||||||
|
if (strcmp(gtk_string_object_get_string (my_item_string), "two"))
|
||||||
|
gtk_list_item_set_child (my_list_item, my_item_string);
|
||||||
|
// Because gtk_list_item_set_child sunk the floating reference of my_label, releasing (unref) isn't necessary for my_label.
|
||||||
|
}
|
||||||
|
// guint xx = gtk_tree_list_row_get_position (GtkTreeListRow* self);
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
|
@ -51,10 +51,20 @@ void on_activate_window_creation (GtkApplication *app, gpointer data)
|
||||||
//-------------------------------------------------------------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
static void on_setup_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data)
|
static void on_setup_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data)
|
||||||
|
/* trying to copy : https://github.com/kriptolix/gtk4_python3_exemples/blob/main/ListView/tree_view.py */
|
||||||
|
/* def setup(widget, item): */
|
||||||
|
/* """Setup the widget to show in the Gtk.Listview""" */
|
||||||
|
/* label = Gtk.Label() */
|
||||||
|
/* expander = Gtk.TreeExpander.new() */
|
||||||
|
/* expander.set_child(label) */
|
||||||
|
/* item.set_child(expander) */
|
||||||
{
|
{
|
||||||
GtkWidget *my_label = gtk_label_new ("here am I"); // <<< compare with : gtk_label_editable_new("..."); !!
|
GtkWidget *my_label = gtk_label_new ("here am I"); // <<< compare with : gtk_label_editable_new("..."); !!
|
||||||
if (1) // some condition on parent and child, I presume.
|
GtkWidget *my_expander = gtk_list_item_get_child (my_list_item);
|
||||||
gtk_list_item_set_child (my_list_item, my_label); // releasing (unref) isn't necessary for my_label.
|
gtk_list_item_set_child (my_expander, my_label);
|
||||||
|
if (0) // some condition on parent and child, I presume.
|
||||||
|
gtk_list_item_set_child (my_list_item, my_expander);
|
||||||
|
else gtk_list_item_set_child (my_list_item, my_label); // I hoped I would see an expander there...
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_bind_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data)
|
static void on_bind_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_item, gpointer user_data)
|
||||||
|
@ -68,8 +78,8 @@ static void on_bind_cb (GtkSignalListItemFactory *self, GtkListItem *my_list_ite
|
||||||
/* label.set_label(obj.data) */
|
/* label.set_label(obj.data) */
|
||||||
{
|
{
|
||||||
GtkWidget *my_expander = gtk_list_item_get_child (my_list_item);
|
GtkWidget *my_expander = gtk_list_item_get_child (my_list_item);
|
||||||
// GtkWidget *my_label = gtk_list_item_get_child (my_list_item);
|
GtkWidget *my_label = gtk_list_item_get_child (my_list_item);
|
||||||
GtkWidget *my_label = gtk_list_item_get_child (my_expander);
|
// GtkWidget *my_label = gtk_list_item_get_child (my_expander);
|
||||||
// GtkTreeListRow *my_row = gtk_tree_list_model_get_row (my_list_item, 0);
|
// GtkTreeListRow *my_row = gtk_tree_list_model_get_row (my_list_item, 0);
|
||||||
/* my_string_object is owned by the instance. Caller mustn't change or destroy it. */
|
/* my_string_object is owned by the instance. Caller mustn't change or destroy it. */
|
||||||
GtkStringObject *my_string_object = gtk_list_item_get_item (my_list_item);
|
GtkStringObject *my_string_object = gtk_list_item_get_item (my_list_item);
|
||||||
|
|
Loading…
Reference in New Issue