summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/shapeicon.cpp
blob: 7a451ab617503175e42a149bf34c2ecbf0489c1a (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
136
137
138
139
140
141
142
143
144
145
146
147
148
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Authors:
 *   Martin Owens
 *
 * Copyright (C) 2020 Authors
 *
 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
 */


#include <gtkmm/cellrenderer.h>
#include <gtkmm/enums.h>
#include "color.h"
#include "ui/util.h"
#include "ui/widget/shapeicon.h"
#include "ui/icon-loader.h"

namespace Inkscape {
namespace UI {
namespace Widget {

/*
 * This is a type of CellRenderer which you might expect to inherit from the
 * pixbuf CellRenderer, but we actually need to write a Cairo surface directly
 * in order to maintain HiDPI sharpness in icons. Upstream Gtk have made it clear
 * that CellRenderers are going away in Gtk4 so they aren't interested in fixing
 * rendering problems like the one in CellRendererPixbuf.
 *
 * See: https://gitlab.gnome.org/GNOME/gtk/-/issues/613
 */

void CellRendererItemIcon::render_vfunc(const Cairo::RefPtr<Cairo::Context>& cr, 
                                      Gtk::Widget& widget,
                                      const Gdk::Rectangle& background_area,
                                      const Gdk::Rectangle& cell_area,
                                      Gtk::CellRendererState flags)
{
    property_mode() = Gtk::CELL_RENDERER_MODE_ACTIVATABLE;

    std::string shape_type = _property_shape_type.get_value();
    if (shape_type == "-") return; // "-" is an explicit request not to draw any icon

    std::string highlight;
    auto color = _property_color.get_value();
    if (color == 0) {
        auto style_context = widget.get_style_context();
        Gdk::RGBA fg = style_context->get_color(cell_flags_to_state_flags(flags));
        highlight = fg.to_string();
    }
    else {
        highlight = SPColor(color).toString();
    }
    std::string cache_id = shape_type + "-" + highlight;

    // if the icon isn't cached, render it to a pixbuf
    int scale = widget.get_scale_factor();
    if ( !_icon_cache[cache_id] ) { 
        _icon_cache[cache_id] = sp_get_shape_icon(shape_type, Gdk::RGBA(highlight), _size, scale);
    }
    g_return_if_fail(_icon_cache[cache_id]);
  
    // 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);

    // Paint the pixbuf to a cairo surface to get HiDPI support
    paint_icon(cr, widget, _icon_cache[cache_id], x, y);

    // Create an overlay icon
    int clipmask = _property_clipmask.get_value();
    if (clipmask > 0) {
        if (!_clip_overlay) {
            _clip_overlay = sp_get_icon_pixbuf("overlay-clip", Gtk::ICON_SIZE_MENU, scale);
        }
        if (!_mask_overlay) {
            _mask_overlay = sp_get_icon_pixbuf("overlay-mask", Gtk::ICON_SIZE_MENU, scale);
        }
        if (!_both_overlay) {
            _both_overlay = sp_get_icon_pixbuf("overlay-clipmask", Gtk::ICON_SIZE_MENU, scale);
        }

        if (clipmask == OVERLAY_CLIP && _clip_overlay) {
            paint_icon(cr, widget, _clip_overlay, x, y);
        }
        if (clipmask == OVERLAY_MASK && _mask_overlay) {
            paint_icon(cr, widget, _mask_overlay, x, y);
        }
        if (clipmask == OVERLAY_BOTH && _both_overlay) {
            paint_icon(cr, widget, _both_overlay, x, y);
        }
    }

}

void CellRendererItemIcon::paint_icon(const Cairo::RefPtr<Cairo::Context>& cr,
                                      Gtk::Widget& widget,
                                      Glib::RefPtr<Gdk::Pixbuf> pixbuf,
                                      int x, int y)
{
    cairo_surface_t *surface = gdk_cairo_surface_create_from_pixbuf(
            pixbuf->gobj(), 0, widget.get_window()->gobj());
    if (!surface) return;
    cairo_set_source_surface(cr->cobj(), surface, x, y);
    cr->set_operator(Cairo::OPERATOR_ATOP);
    cr->rectangle(x, y, _size, _size);
    cr->fill();
    cairo_surface_destroy(surface); // free!
}

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

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

bool CellRendererItemIcon::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_activated.emit(path);
    return true;
}

} // 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 :