From ef0519440e63722a4cc8b364341203b029d5ef8b Mon Sep 17 00:00:00 2001 From: Jean Sirmai Date: Wed, 29 Nov 2023 15:36:09 +0100 Subject: [PATCH] WIP: A button is now in the GtkBox *runlib_objects of windows.c How to associate an action to it ? --- include/ui.h | 1 + src/ui/tree.c | 177 +++++------------------------------------------- src/ui/window.c | 22 ++++-- 3 files changed, 35 insertions(+), 165 deletions(-) diff --git a/include/ui.h b/include/ui.h index 2eb1f25..f9824b3 100644 --- a/include/ui.h +++ b/include/ui.h @@ -129,4 +129,5 @@ void onTreeViewRowActivated (GtkTreeView *view, GtkTreePath *path, void tree_c_test(void); GtkWidget *create_tree_label(void); +GtkWidget *create_tree_button(void); diff --git a/src/ui/tree.c b/src/ui/tree.c index b3968d1..d8bae9c 100644 --- a/src/ui/tree.c +++ b/src/ui/tree.c @@ -162,12 +162,12 @@ tree_c_test(void) } else g_error("Oops, we should have a first row in the tree store!\n"); - GtkTreeViewColumn *col; - GtkCellRenderer *renderer; - GtkWidget *view; - GtkTreeModel *model; +// GtkTreeViewColumn *col; +// GtkCellRenderer *renderer; +// GtkWidget *view; +// GtkTreeModel *model; - GtkWidget *window; +// GtkWidget *window; tree_c_printing_test(); @@ -175,165 +175,20 @@ tree_c_test(void) static void tree_c_printing_test(void) { - printf("tree_c_printing_test() \ ------------------------------------\n\ -Changement de cap : comment mettre un label (puis un arbre)\n\ - dans la GtkBox *runlib_objects; de windows.c (ligne 61)\n\ - ?\n\ + printf("tree.c tree_c_printing_test() \ +------------------------------------\n\ +A button is now in the GtkBox *runlib_objects of windows.c (line 61)\n\ + How to associate an action to it\n\ + ?\n\ "); } -// 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://docs.gtk.org/gobject/#functions +https://docs.gtk.org/gtk4/class.Label.html?q= +https://docs.gtk.org/gtk4/ctor.Button.new_with_label.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 - - -using System; -using System.Collections; -using Gtk; - -public class Actress -{ - public string Name; - public string Place; - public int Year; - - public Actress(string name, string place, int year) - { - Name = name; - Place = place; - Year = year; - } -} - -public class SharpApp : Window -{ - ListStore store; - Statusbar statusbar; - - enum Column - { - Name, - Place, - Year - } - - Actress[] actresses = - { - new Actress("Jessica Alba", "Pomona", 1981), - new Actress("Sigourney Weaver", "New York", 1949), - new Actress("Angelina Jolie", "Los Angeles", 1975), - new Actress("Natalie Portman", "Jerusalem", 1981), - new Actress("Rachel Weissz", "London", 1971), - new Actress("Scarlett Johansson", "New York", 1984) - }; - - public SharpApp() : base ("ListView") - { - BorderWidth = 8; - - SetDefaultSize(350, 250); - SetPosition(WindowPosition.Center); - DeleteEvent += delegate { Application.Quit(); }; - - VBox vbox = new VBox(false, 8); - - - ScrolledWindow sw = new ScrolledWindow(); - sw.ShadowType = ShadowType.EtchedIn; - sw.SetPolicy(PolicyType.Automatic, PolicyType.Automatic); - vbox.PackStart(sw, true, true, 0); - - store = CreateModel(); - - TreeView treeView = new TreeView(store); - treeView.RulesHint = true; - treeView.RowActivated += OnRowActivated; - sw.Add(treeView); - - AddColumns(treeView); - - statusbar = new Statusbar(); - - vbox.PackStart(statusbar, false, false, 0); - - Add(vbox); - ShowAll(); - } - - void OnRowActivated (object sender, RowActivatedArgs args) { - - TreeIter iter; - TreeView view = (TreeView) sender; - - if (view.Model.GetIter(out iter, args.Path)) { - string row = (string) view.Model.GetValue(iter, (int) Column.Name ); - row += ", " + (string) view.Model.GetValue(iter, (int) Column.Place ); - row += ", " + view.Model.GetValue(iter, (int) Column.Year ); - statusbar.Push(0, row); - } - // *** if I can dump treeview to the console here I'll be happy *** - // *** I'd prefer a foreach or do/while *** - } - - void AddColumns(TreeView treeView) - { - CellRendererText rendererText = new CellRendererText(); - TreeViewColumn column = new TreeViewColumn("Name", rendererText, - "text", Column.Name); - column.SortColumnId = (int) Column.Name; - treeView.AppendColumn(column); - - rendererText = new CellRendererText(); - column = new TreeViewColumn("Place", rendererText, - "text", Column.Place); - column.SortColumnId = (int) Column.Place; - treeView.AppendColumn(column); - - rendererText = new CellRendererText(); - column = new TreeViewColumn("Year", rendererText, - "text", Column.Year); - column.SortColumnId = (int) Column.Year; - treeView.AppendColumn(column); - } - - - ListStore CreateModel() - { - ListStore store = new ListStore( typeof(string), - typeof(string), typeof(int) ); - - foreach (Actress act in actresses) { - store.AppendValues(act.Name, act.Place, act.Year ); - } - - return store; - } - - public static void Main() - { - Application.Init(); - new SharpApp(); - Application.Run(); - } -} -*/ - - - -/* -(https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Columns_and_Renderer)\n\ -Trying to adapt the first example of this tuto to display a simple tree view in our window.\n\ -Can't use :\n\ - - gtk_init(&argc, &argv);\n\ - - window = gtk_tree_view_new(GTK_WINDOW_TOPLEVEL);\n\ - - g_signal_connect(window, 'delete_event', gtk_main_quit, NULL);\n\ - - tree_c_test(); (or : create_view_and_model() or some equivalent tree description)\n\ - - gtk_container_add(GTK_CONTAINER(window), view);\n\ - - gtk_widget_show_all(window);\n\ - - gtk_main();\n\ -(see main.c lines 35-44)\n\ +https://en.wikibooks.org/wiki/GTK%2B_By_Example/Tree_View/Columns_and_Renderer */ diff --git a/src/ui/window.c b/src/ui/window.c index 25f1539..f93d2c6 100644 --- a/src/ui/window.c +++ b/src/ui/window.c @@ -124,7 +124,8 @@ void ui_set_stack(const char *mode) break; case 'r': gtk_menu_button_set_icon_name(window->main_button_mode, "system-run-symbolic"); - create_tree_label(); +// create_tree_label(); + create_tree_button(); break; case 'p': gtk_menu_button_set_icon_name(window->main_button_mode, "x-office-presentation-symbolic"); @@ -232,9 +233,8 @@ void ui_toggle_sidebar(void) GtkWidget *create_tree_label(void) { - GtkWidget *label, *slider; - GtkAdjustment *adj; - const char *text = "HELLO !"; + GtkWidget *label; + const char *text = "Hello ! I'm the label"; label = gtk_label_new(text); gtk_box_append(GTK_BOX(window->runlib_objects), label); @@ -243,3 +243,17 @@ GtkWidget *create_tree_label(void) return label; } +GtkWidget *create_tree_button(void) +{ + GtkWidget *button; + const char *text = "Hello ! I'm the button"; + + button = gtk_button_new_with_label(text); + gtk_box_append(GTK_BOX(window->runlib_objects), button); + gtk_widget_show(button); + + tree_c_test(); + + return button; +} +