summaryrefslogtreecommitdiffstats
path: root/src/ui/widget/icon-widget.cpp
blob: c456778b62537cdfc2f5ca1b930a29e87cf82ddd (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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
 * \brief Icon Widget
 *
 * Author:
 *   Bryce Harrington <bryce@bryceharrington.org>
 *
 * Copyright (C) 2004 Bryce Harrington
 *    based on work by Lauris Kaplinski in 2002 released under GPL
 *
 * Released under GNU GPL.  Read the file 'COPYING' for more information
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "icon-widget.h"

namespace Inkscape {
namespace UI {
namespace Widget {

/**
 *    General purpose icon widget, supporting SVG, etc. icon loading
 *
 *    \param ...
 *
 *    An icon widget is a ...
 */
  
IconWidget::IconWidget() 
{
    _pb = NULL;
    _size = 0;
}

IconWidget::IconWidget(int unsigned size, int unsigned scale, gchar const *name) 
{
    _size = std::max((int unsigned)128, std::min(size, (int unsigned)1));

    char c[256];
    g_snprintf (c, 256, "%d:%d:%s", _size, scale, name);
    guchar *pixels = image_load_gtk(name, _size, scale);

    if (pixels == NULL) {
        g_warning("Couldn't find matching icon for %s - has this application been installed?", name);
        _pb = NULL;
    } else {
        /* TODO
        _pb = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB, 
                                       TRUE, 8, _size, _size, _size * 4, 
                                       (GdkPixbufDestroyNotify)nr_free, NULL);
        */
    }
}

IconWidget::IconWidget(int unsigned size, guchar const *px)
{
    _size = std::max((int unsigned)128, std::min(size, (int unsigned)1));

    /*
    _pb = gdk_pixbuf_new_from_data((guchar *)px, GDK_COLORSPACE_RGB, 
                                   TRUE, 8, _size, _size, _size * 4, NULL, NULL);
    */
}

IconWidget::~IconWidget() {
}

void IconWidget::size_request(Gtk::Requisition &requisition)
{
    requisition.width  = _size;
    requisition.height = _size;
}

void IconWidget::size_allocate(Gtk::Allocation const &allocation)
{
    Gtk::Widget::size_allocate(allocation);

    if (this->is_drawable()) {
        this->queue_draw();
    }
}

int IconWidget::expose(GdkEventExpose *event)
{
    if (this->is_drawable()) {
        paint(&(event->area));
    }
    return true;
}

void IconWidget::paint(GdkRectangle const *area)
{
    Gtk::Allocation allocation = get_allocation();
    int const x_pad = std::max(0, (allocation.get_width() - _size)/2);
    int const y_pad = std::max(0, (allocation.get_height() - _size)/2);

    int const x_max = std::max(area->x, allocation.get_x() + x_pad);
    int const y_max = std::max(area->y, allocation.get_y() + y_pad);
    int const x_min = std::min(area->x + area->width,
                               allocation.get_x() + x_pad + _size);
    int const y_min = std::min(area->y + area->height,
                               allocation.get_y() + y_pad + _size);

    if (_pb) {
        gdk_draw_pixbuf(this->get_window()->gobj(), NULL, _pb,
                        x_max - allocation.get_x() - x_pad,
                        y_max - allocation.get_y() - y_pad,
                        x_max, y_max,
                        x_min - x_max, y_min - y_max,
                        GDK_RGB_DITHER_NORMAL, x_max, y_max);
    }
}

guchar* IconWidget::image_load(gchar const *name, int unsigned size, int unsigned scale)
{
    guchar *px;

    if (_do_bitmap_icons) {
        px = image_load_pixmap(name, size, scale);
        if (!px) {
            px = image_load_svg(name, size, scale);
        }
    } else {
        px = image_load_svg(name, size, scale);
        if (!px) {
            px = image_load_pixmap(name, size, scale);
        }
    }
    return px;
}

guchar* IconWidget::image_load_pixmap(gchar const *name, int unsigned size, int unsigned scale)
{
    // TODO
    return NULL;
}

guchar* IconWidget::image_load_svg(gchar const *name, int unsigned size, int unsigned scale)
{
    // TODO
    return NULL;
}

guchar* IconWidget::image_load_gtk(gchar const *name, int unsigned size, int unsigned scale)
{
    // TODO
    return NULL;
}

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

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