Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
combo-tool-item.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
5/*
6 * Authors:
7 * Tavmjong Bah <tavmjong@free.fr>
8 *
9 * Copyright (C) 2017 Tavmjong Bah
10 *
11 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
12 */
13
14#include "combo-tool-item.h"
15
16#include <utility>
17#include <vector>
18#include <sigc++/functors/mem_fun.h>
19#include <gdkmm/pixbuf.h>
20#include <gtkmm/cellrendererpixbuf.h>
21#include <gtkmm/box.h>
22#include <gtkmm/combobox.h>
23#include <gtkmm/label.h>
24#include <gtkmm/liststore.h>
25
26#include "preferences.h"
27#include "ui/pack.h"
28
29namespace Inkscape::UI::Widget {
30
31static void strip_trailing(Glib::ustring &s, char const c)
32{
33 if (!s.empty() && s.raw().back() == c) {
34 s.resize(s.size() - 1);
35 }
36}
37
38ComboToolItem*
39ComboToolItem::create(const Glib::ustring &group_label,
40 const Glib::ustring &tooltip,
41 const Glib::ustring &stock_id,
42 Glib::RefPtr<Gtk::ListStore> store,
43 bool has_entry)
44{
45 return new ComboToolItem(group_label, tooltip, stock_id, std::move(store), has_entry);
46}
47
48ComboToolItem::ComboToolItem(Glib::ustring group_label,
49 Glib::ustring tooltip,
50 Glib::ustring stock_id,
51 Glib::RefPtr<Gtk::ListStore> store,
52 bool has_entry) :
53 _active(-1),
54 _group_label(std::move( group_label )),
55 _tooltip(std::move( tooltip )),
56 _stock_id(std::move( stock_id )),
57 _store (std::move(store)),
58 _use_label (true),
59 _use_icon (false),
60 _use_pixbuf (true),
61 _icon_size ( Gtk::IconSize::LARGE ),
62 _combobox (nullptr),
63 _container(Gtk::make_managed<Gtk::Box>())
64{
66 _container->set_spacing(3);
67
68 // ": " is added to the group label later
69 // If we have them already, we strip them
72
73 _combobox = Gtk::make_managed<Gtk::ComboBox>(has_entry);
74 _combobox->set_model(_store);
75
77
78 _combobox->signal_changed().connect(
79 sigc::mem_fun(*this, &ComboToolItem::on_changed_combobox));
81}
82
84
85void
86ComboToolItem::focus_on_click( bool focus_on_click )
87{
88 _combobox->set_focus_on_click(focus_on_click);
89}
90
91
92void
98
99void
101{
104}
105
106void
108{
111}
112
113void
115{
116 if (use_group_label == (_group_label_widget != nullptr)) {
117 return;
118 }
119
120 if (use_group_label) {
121 _combobox->reference();
122 _container->remove(*_combobox);
123 _group_label_widget = std::make_unique<Gtk::Label>(_group_label + ": ");
126 _combobox->unreference();
127 } else {
129 _group_label_widget.reset();
130 }
131}
132
133void
135{
136 _combobox->clear();
137
138 ComboToolItemColumns columns;
139
140 if (_use_icon) {
142 if (prefs->getBool("/theme/symbolicIcons", false)) {
143 auto children = _store->children();
144 for (auto row : children) {
145 auto const &icon = row.get_value(columns.col_icon);
146 auto const pos = icon.find("-symbolic");
147 if (pos == icon.npos) {
148 row.set_value(columns.col_icon, icon + "-symbolic");
149 }
150 }
151 }
152
153 auto const renderer = Gtk::make_managed<Gtk::CellRendererPixbuf>();
154 renderer->set_property ("stock_size", Gtk::IconSize::LARGE);
155 _combobox->pack_start (*renderer, false);
156 _combobox->add_attribute (*renderer, "icon_name", columns.col_icon );
157 } else if (_use_pixbuf) {
158 auto const renderer = Gtk::make_managed<Gtk::CellRendererPixbuf>();
159 _combobox->pack_start (*renderer, false);
160 _combobox->add_attribute (*renderer, "pixbuf", columns.col_pixbuf );
161 }
162
163 if (_use_label) {
164 _combobox->pack_start(columns.col_label);
165 }
166
167 std::vector<Gtk::CellRenderer*> cells = _combobox->get_cells();
168 for (auto & cell : cells) {
169 _combobox->add_attribute (*cell, "sensitive", columns.col_sensitive);
170 }
171
172 set_tooltip_text(_tooltip);
173 _combobox->set_tooltip_text(_tooltip);
174 _combobox->set_active (_active);
175}
176
177void
179 if (_active == active) return;
180
181 _active = active;
182
183 if (_combobox) {
184 _combobox->set_active (active);
185 }
186}
187
188Glib::ustring
190 Gtk::TreeModel::Row row = _store->children()[_active];
191 ComboToolItemColumns columns;
192 return row.get_value(columns.col_label);
193}
194
195void
197 int row = _combobox->get_active_row_number();
198 set_active( row );
199 _changed.emit (_active);
200 _changed_after.emit (_active);
201}
202
203} // namespace Inkscape::UI::Widget
204
205/*
206 Local Variables:
207 mode:c++
208 c-file-style:"stroustrup"
209 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
210 indent-tabs-mode:nil
211 fill-column:99
212 End:
213*/
214// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Fragment store
Definition canvas.cpp:155
Preference storage class.
Definition preferences.h:61
bool getBool(Glib::ustring const &pref_path, bool def=false)
Retrieve a Boolean value.
static Preferences * get()
Access the singleton Preferences object.
Gtk::TreeModelColumn< Glib::RefPtr< Gdk::Pixbuf > > col_pixbuf
Gtk::TreeModelColumn< Glib::ustring > col_icon
Gtk::TreeModelColumn< bool > col_sensitive
Gtk::TreeModelColumn< Glib::ustring > col_label
void use_group_label(bool use_group_label)
std::unique_ptr< Gtk::Label > _group_label_widget
sigc::signal< void(int)> _changed
static ComboToolItem * create(const Glib::ustring &label, const Glib::ustring &tooltip, const Glib::ustring &stock_id, Glib::RefPtr< Gtk::ListStore > store, bool has_entry=false)
void focus_on_click(bool focus_on_click)
sigc::signal< void(int)> _changed_after
Glib::RefPtr< Gtk::ListStore > _store
ComboToolItem(Glib::ustring group_label, Glib::ustring tooltip, Glib::ustring stock_id, Glib::RefPtr< Gtk::ListStore > store, bool has_entry=false)
A combobox that can be displayed in a toolbar.
double c[8][4]
std::vector< ItemIterator > _active
Definition desktop.h:50
Custom widgets.
Definition desktop.h:126
static void strip_trailing(Glib::ustring &s, char const c)
void pack_start(Gtk::Box &box, Gtk::Widget &child, bool const expand, bool const fill, unsigned const padding)
Adds child to box, packed with reference to the start of box.
Definition pack.cpp:141
static void append(std::vector< T > &target, std::vector< T > &&source)
STL namespace.
Helpers for using Gtk::Boxes, encapsulating large changes between GTK3 & GTK4.
Singleton class to access the preferences file in a convenient way.
std::vector< Edges > cells(cairo_t *, PathVector const &ps)
Definition sanitize.cpp:103