Inkscape
Vector Graphics Editor
imagetoggler.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * Jon A. Cruz
5 * Johan B. C. Engelen
6 *
7 * Copyright (C) 2006-2008 Authors
8 *
9 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
10 */
11
13
14#include <gtkmm/snapshot.h>
15#include <sigc++/functors/mem_fun.h>
16
17namespace Inkscape::UI::Widget {
18
19ImageToggler::ImageToggler(char const *on, char const *off) :
20 Glib::ObjectBase(typeid(ImageToggler)),
21 Gtk::CellRendererPixbuf{},
22 _pixOnName(on),
23 _pixOffName(off),
24 _property_active(*this, "active", false),
25 _property_activatable(*this, "activatable", true),
26 _property_gossamer(*this, "gossamer", false),
27 _property_active_icon(*this, "active_icon", "")
28{
29 property_mode() = Gtk::CellRendererMode::ACTIVATABLE;
30 property_icon_size().set_value(Gtk::IconSize::NORMAL); // Previously ICON_SIZE_MENU
31 set_padding(6, 3);
32
33 auto const set_icon = sigc::mem_fun(*this, &ImageToggler::set_icon_name);
34 property_active ().signal_changed().connect(set_icon);
35 property_active_icon().signal_changed().connect(set_icon);
36}
37
38void ImageToggler::set_force_visible(bool const force_visible)
39{
40 _force_visible = force_visible;
41}
42
44{
45 Glib::ustring icon_name;
46 if (_property_active.get_value()) {
47 icon_name = _property_active_icon.get_value();
48 if (icon_name.empty()) icon_name = _pixOnName;
49 } else {
50 icon_name = _pixOffName;
51 }
52 property_icon_name().set_value(icon_name);
53}
54
55void ImageToggler::snapshot_vfunc(Glib::RefPtr<Gtk::Snapshot> const &snapshot,
56 Gtk::Widget &widget,
57 const Gdk::Rectangle &background_area,
58 const Gdk::Rectangle &cell_area,
59 Gtk::CellRendererState flags)
60{
61 // Hide when not being used.
62 double alpha = 1.0;
63 bool visible = _property_activatable.get_value()
64 || _property_active.get_value()
66 if (!visible) {
67 // XXX There is conflict about this value, some users want 0.2, others want 0.0
68 alpha = 0.0;
69 }
70 if (!visible && _property_gossamer.get_value()) {
71 alpha += 0.2;
72 }
73 if (alpha <= 0.0) {
74 return;
75 }
76
77 // Apply alpha to output of Gtk::CellRendererPixbuf, plus x offset to replicate prev behaviour.
78 snapshot->push_opacity(alpha);
79 auto const point = GRAPHENE_POINT_INIT(-0.5f * property_xpad().get_value(), 0);
80 gtk_snapshot_translate(snapshot->gobj(), &point);
81 Gtk::CellRendererPixbuf::snapshot_vfunc(snapshot, widget, background_area, cell_area, flags);
82 snapshot->pop();
83}
84
85bool
86ImageToggler::activate_vfunc(Glib::RefPtr<Gdk::Event const> const &/*event*/,
87 Gtk::Widget &/*widget*/,
88 const Glib::ustring &path,
89 const Gdk::Rectangle &/*background_area*/,
90 const Gdk::Rectangle &/*cell_area*/,
91 Gtk::CellRendererState /*flags*/)
92{
93 _signal_toggled.emit(path);
94 return false;
95}
96
97} // namespace Inkscape::UI::Widget
98
99/*
100 Local Variables:
101 mode:c++
102 c-file-style:"stroustrup"
103 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
104 indent-tabs-mode:nil
105 fill-column:99
106 End:
107*/
108// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Geom::IntRect visible
Definition: canvas.cpp:154
Glib::Property< bool > _property_gossamer
Definition: imagetoggler.h:58
Glib::PropertyProxy< bool > property_active()
Definition: imagetoggler.h:29
sigc::signal< void(const Glib::ustring &)> _signal_toggled
Definition: imagetoggler.h:61
void set_force_visible(bool force_visible)
Sets whether to force visible icons in ALL cells of the column, EVEN IF their activatable & property_...
Glib::Property< std::string > _property_active_icon
Definition: imagetoggler.h:59
ImageToggler(char const *on, char const *off)
Glib::Property< bool > _property_active
Definition: imagetoggler.h:56
Glib::Property< bool > _property_activatable
Definition: imagetoggler.h:57
bool activate_vfunc(Glib::RefPtr< Gdk::Event const > const &event, Gtk::Widget &widget, const Glib::ustring &path, const Gdk::Rectangle &background_area, const Gdk::Rectangle &cell_area, Gtk::CellRendererState flags) override
Glib::PropertyProxy< std::string > property_active_icon()
Definition: imagetoggler.h:32
void snapshot_vfunc(Glib::RefPtr< Gtk::Snapshot > const &snapshot, Gtk::Widget &widget, const Gdk::Rectangle &background_area, const Gdk::Rectangle &cell_area, Gtk::CellRendererState flags) override
Definition: desktop.h:51
Custom widgets.
Definition: desktop.h:127
void set_icon(Gtk::Button &btn, char const *pixmap)
static void set_padding(Gtk::Widget &widget, Gtk::Orientation const orientation, int const margin_start, int const margin_end)
Definition: pack.cpp:78
int off