summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/imagetoggler.cpp
blob: 7a0c29528592142b8308df745789c633f1720414 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Jon A. Cruz
 *   Johan B. C. Engelen
 *
 * Copyright (C) 2006-2008 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */

#include <gtkmm/iconinfo.h>

#include "ui/widget/imagetoggler.h"

#include "ui/icon-loader.h"
#include "ui/icon-names.h"

namespace Inkscape {
namespace UI {
namespace Widget {

ImageToggler::ImageToggler( char const* on, char const* off) :
    Glib::ObjectBase(typeid(ImageToggler)),
    Gtk::CellRenderer(),
    _pixOnName(on),
    _pixOffName(off),
    _property_active(*this, "active", false),
    _property_activatable(*this, "activatable", true),
    _property_gossamer(*this, "gossamer", false),
    _property_pixbuf_on(*this, "pixbuf_on", Glib::RefPtr<Gdk::Pixbuf>(nullptr)),
    _property_pixbuf_off(*this, "pixbuf_off", Glib::RefPtr<Gdk::Pixbuf>(nullptr))
{
    property_mode() = Gtk::CELL_RENDERER_MODE_ACTIVATABLE;
    Gtk::IconSize::lookup(Gtk::ICON_SIZE_MENU, _size, _size);
}

void ImageToggler::get_preferred_height_vfunc(Gtk::Widget& widget, int& min_h, int& nat_h) const
{
    min_h = _size + 6;
    nat_h = _size + 8;
}

void ImageToggler::get_preferred_width_vfunc(Gtk::Widget& widget, int& min_w, int& nat_w) const
{
    min_w = _size + 12;
    nat_w = _size + 16;
}

void ImageToggler::render_vfunc( const Cairo::RefPtr<Cairo::Context>& cr,
                                 Gtk::Widget& widget,
                                 const Gdk::Rectangle& background_area,
                                 const Gdk::Rectangle& cell_area,
                                 Gtk::CellRendererState flags )
{
    // Lazy/late pixbuf rendering to get access to scale factor from widget.
    if(!_property_pixbuf_on.get_value()) {
        int scale = widget.get_scale_factor();
        _property_pixbuf_on = sp_get_icon_pixbuf(_pixOnName, _size * scale);
        _property_pixbuf_off = sp_get_icon_pixbuf(_pixOffName, _size * scale);
    }

    // Hide when not being used.
    double alpha = 1.0;
    bool visible = _property_activatable.get_value()
                || _property_active.get_value();
    if (!visible) {
        // XXX There is conflict about this value, some users want 0.2, others want 0.0
        alpha = 0.0;
    }
    if (_property_gossamer.get_value()) {
        alpha += 0.2;
    }
    if (alpha <= 0.0) {
        return;
    }

    Glib::RefPtr<Gdk::Pixbuf> pixbuf;
    if(_property_active.get_value()) {
        pixbuf = _property_pixbuf_on.get_value();
    } else {
        pixbuf = _property_pixbuf_off.get_value();
    }

    cairo_surface_t *surface = gdk_cairo_surface_create_from_pixbuf(
            pixbuf->gobj(), 0, widget.get_window()->gobj());
    g_return_if_fail(surface);

    // Center the icon in the cell area
    int x = cell_area.get_x() + int((cell_area.get_width() - _size) * 0.5);
    int y = cell_area.get_y() + int((cell_area.get_height() - _size) * 0.5);

    cairo_set_source_surface(cr->cobj(), surface, x, y);
    cr->set_operator(Cairo::OPERATOR_ATOP);
    cr->rectangle(x, y, _size, _size);
    if (alpha < 1.0) {
        cr->clip();
        cr->paint_with_alpha(alpha);
    } else {
        cr->fill();
    }
    cairo_surface_destroy(surface); // free!
}

bool
ImageToggler::activate_vfunc(GdkEvent* event,
                            Gtk::Widget& /*widget*/,
                            const Glib::ustring& path,
                            const Gdk::Rectangle& /*background_area*/,
                            const Gdk::Rectangle& /*cell_area*/,
                            Gtk::CellRendererState /*flags*/)
{
    _signal_pre_toggle.emit(event);
    _signal_toggled.emit(path);

    return false;
}


} // namespace Widget
} // namespace UI
} // namespace Inkscape

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :