From bf75d5f884aa7aabb105b346d4f014044ffaf5a9 Mon Sep 17 00:00:00 2001 From: daleclack Date: Thu, 4 Jan 2024 17:41:12 +0800 Subject: [PATCH] Add list management for gtk156 --- Gtkmm4/gtk156_dyncombo/src/MainWin.cc | 57 ++++++++++++++++++++++++--- Gtkmm4/gtk156_dyncombo/src/MainWin.hh | 6 +++ 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/Gtkmm4/gtk156_dyncombo/src/MainWin.cc b/Gtkmm4/gtk156_dyncombo/src/MainWin.cc index 792c9fe..3b1d5f8 100644 --- a/Gtkmm4/gtk156_dyncombo/src/MainWin.cc +++ b/Gtkmm4/gtk156_dyncombo/src/MainWin.cc @@ -2,11 +2,11 @@ MainWin::MainWin() : main_box(Gtk::Orientation::HORIZONTAL, 5), - lists_box(Gtk::Orientation::VERTICAL, 5), - drop_frame("Dropdown widget"), - list_frame("Items List"), - btn_add("Add Item"), - btn_remove("Remove Item") + lists_box(Gtk::Orientation::VERTICAL, 5), + drop_frame("Dropdown widget"), + list_frame("Items List"), + btn_add("Add Item"), + btn_remove("Remove Item") { // Initalize window set_icon_name("org.gtk.daleclack"); @@ -19,7 +19,21 @@ MainWin::MainWin() drop_overlay.add_overlay(dropdown); drop_frame.set_child(drop_overlay); - + // Create string list + dropdown_list = Gtk::StringList::create({"Longterm", "Stable", "Develop"}); + selection = Gtk::SingleSelection::create(dropdown_list); + dropdown.set_model(dropdown_list); + main_list_view.set_model(selection); + + // Add a factory to the list view + factory = Gtk::SignalListItemFactory::create(); + factory->signal_bind().connect(sigc::mem_fun(*this, &MainWin::bind_label)); + factory->signal_setup().connect(sigc::mem_fun(*this, &MainWin::setup_label)); + main_list_view.set_factory(factory); + + // Link Signal for buttons + btn_add.signal_clicked().connect(sigc::mem_fun(*this, &MainWin::btnadd_clicked)); + btn_remove.signal_clicked().connect(sigc::mem_fun(*this, &MainWin::btnremove_clicked)); // Add List widget // Scrolled window for listview widget @@ -44,6 +58,32 @@ MainWin::MainWin() set_child(main_box); } +void MainWin::setup_label(const Glib::RefPtr &item) +{ + // Set label for item + item->set_child(*Gtk::make_managed("", Gtk::Align::START)); +} + +void MainWin::bind_label(const Glib::RefPtr &item) +{ + // Get position + auto pos = item->get_position(); + if (pos == GTK_INVALID_LIST_POSITION) + { + return; + } + + // Get label + auto label = dynamic_cast(item->get_child()); + if (!label) + { + return; + } + + // Set text + label->set_text(dropdown_list->get_string(pos)); +} + void MainWin::btnadd_clicked() { Glib::ustring entry_str; @@ -60,4 +100,9 @@ void MainWin::btnadd_clicked() void MainWin::btnremove_clicked() { + // Get Position of item + auto pos = selection->get_selected(); + + // Remove item + dropdown_list->remove(pos); } \ No newline at end of file diff --git a/Gtkmm4/gtk156_dyncombo/src/MainWin.hh b/Gtkmm4/gtk156_dyncombo/src/MainWin.hh index b8ef262..c9b9c5a 100644 --- a/Gtkmm4/gtk156_dyncombo/src/MainWin.hh +++ b/Gtkmm4/gtk156_dyncombo/src/MainWin.hh @@ -16,6 +16,12 @@ public: private: // Use StringList for dropdown and json file Glib::RefPtr dropdown_list; + Glib::RefPtr selection; + + // Factory to renderer + Glib::RefPtr factory; + void setup_label(const Glib::RefPtr &item); + void bind_label(const Glib::RefPtr &item); // Child widgets Gtk::DropDown dropdown;