Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
drop-down-list.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#include "drop-down-list.h"
4#include <gtkmm/dropdown.h>
5#include <gtkmm/label.h>
6#include <gtkmm/stringobject.h>
7
8namespace Inkscape::UI::Widget {
9
13
14DropDownList::DropDownList(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder):
15 Gtk::DropDown(cobject) {
16
17 //TODO: verify if we want/need to set model too
18 _init();
19}
20
22 set_name("DropDownList");
23
24 _factory->signal_setup().connect([this](const Glib::RefPtr<Gtk::ListItem>& list_item) {
25 auto label = Gtk::make_managed<Gtk::Label>();
26 label->set_xalign(0);
27 label->set_valign(Gtk::Align::CENTER);
28 list_item->set_child(*label);
29 });
30
31 _factory->signal_bind().connect([this](const Glib::RefPtr<Gtk::ListItem>& list_item) {
32 auto obj = list_item->get_item();
33 auto& label = dynamic_cast<Gtk::Label&>(*list_item->get_child());
35 auto pos = list_item->get_position();
36 if (_separator_callback(pos)) {
37 label.get_parent()->add_css_class("top-separator");
38 }
39 }
40 auto item = std::dynamic_pointer_cast<Gtk::StringObject>(obj);
41 label.set_label(item->get_string());
42 });
43
44 set_list_factory(_factory);
45 set_model(_model);
46}
47
48unsigned int DropDownList::append(const Glib::ustring& item) {
49 auto n = _model->get_n_items();
50 _model->append(item);
51 return n;
52}
53
54void DropDownList::enable_search(bool enable) {
55 if (enable) {
56 auto expression = Gtk::ClosureExpression<Glib::ustring>::create([this](auto& item){
57 return std::dynamic_pointer_cast<Gtk::StringObject>(item)->get_string();
58 });
59 set_expression(expression);
60 }
61 set_enable_search(enable);
62 // search or expression reset item factory; restore it
63 set_list_factory(_factory);
64}
65
66void DropDownList::set_row_separator_func(std::function<bool (unsigned int)> callback) {
67 _separator_callback = callback;
68}
69
70} // namespace
unsigned int append(const Glib::ustring &item)
Glib::RefPtr< Gtk::SignalListItemFactory > _factory
Glib::RefPtr< Gtk::StringList > _model
std::function< bool(unsigned int)> _separator_callback
void set_row_separator_func(std::function< bool(unsigned int)> callback)
void enable_search(bool enable=true)
Simple DropDown widget presenting popup list of string items to choose from.
SPItem * item
Glib::ustring label
Definition desktop.h:50
Custom widgets.
Definition desktop.h:126
Glib::RefPtr< Gtk::Builder > builder