Add list management for gtk156

This commit is contained in:
daleclack 2024-01-04 17:41:12 +08:00
parent bba476162b
commit bf75d5f884
2 changed files with 57 additions and 6 deletions

View File

@ -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<Gtk::ListItem> &item)
{
// Set label for item
item->set_child(*Gtk::make_managed<Gtk::Label>("", Gtk::Align::START));
}
void MainWin::bind_label(const Glib::RefPtr<Gtk::ListItem> &item)
{
// Get position
auto pos = item->get_position();
if (pos == GTK_INVALID_LIST_POSITION)
{
return;
}
// Get label
auto label = dynamic_cast<Gtk::Label*>(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);
}

View File

@ -16,6 +16,12 @@ public:
private:
// Use StringList for dropdown and json file
Glib::RefPtr<Gtk::StringList> dropdown_list;
Glib::RefPtr<Gtk::SingleSelection> selection;
// Factory to renderer
Glib::RefPtr<Gtk::SignalListItemFactory> factory;
void setup_label(const Glib::RefPtr<Gtk::ListItem> &item);
void bind_label(const Glib::RefPtr<Gtk::ListItem> &item);
// Child widgets
Gtk::DropDown dropdown;