1 /**
2 * \brief Icon Widget
3 *
4 * Author:
5 * Bryce Harrington <bryce@bryceharrington.org>
6 *
7 * Copyright (C) 2004 Bryce Harrington
8 * based on work by Lauris Kaplinski in 2002 released under GPL
9 *
10 * Released under GNU GPL. Read the file 'COPYING' for more information
11 */
13 #ifdef HAVE_CONFIG_H
14 # include <config.h>
15 #endif
17 #include <glib/gmem.h>
18 #include "icon-widget.h"
20 namespace Inkscape {
21 namespace UI {
22 namespace Widget {
24 /**
25 * General purpose icon widget, supporting SVG, etc. icon loading
26 *
27 * \param ...
28 *
29 * An icon widget is a ...
30 */
32 IconWidget::IconWidget()
33 {
34 _pb = NULL;
35 _size = 0;
36 }
38 IconWidget::IconWidget(int unsigned size, int unsigned scale, gchar const *name)
39 {
40 _size = std::max((int unsigned)128, std::min(size, (int unsigned)1));
42 char c[256];
43 g_snprintf (c, 256, "%d:%d:%s", _size, scale, name);
44 guchar *pixels = image_load_gtk(name, _size, scale);
46 if (pixels == NULL) {
47 g_warning("Couldn't find matching icon for %s - has this application been installed?", name);
48 _pb = NULL;
49 } else {
50 /* TODO
51 _pb = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB,
52 TRUE, 8, _size, _size, _size * 4,
53 (GdkPixbufDestroyNotify)g_free, NULL);
54 */
55 }
56 }
58 IconWidget::IconWidget(int unsigned size, guchar const *px)
59 {
60 _size = std::max((int unsigned)128, std::min(size, (int unsigned)1));
62 /*
63 _pb = gdk_pixbuf_new_from_data((guchar *)px, GDK_COLORSPACE_RGB,
64 TRUE, 8, _size, _size, _size * 4, NULL, NULL);
65 */
66 }
68 IconWidget::~IconWidget() {
69 }
71 void IconWidget::size_request(Gtk::Requisition &requisition)
72 {
73 requisition.width = _size;
74 requisition.height = _size;
75 }
77 void IconWidget::size_allocate(Gtk::Allocation const &allocation)
78 {
79 Gtk::Widget::size_allocate(allocation);
81 if (this->is_drawable()) {
82 this->queue_draw();
83 }
84 }
86 int IconWidget::expose(GdkEventExpose *event)
87 {
88 if (this->is_drawable()) {
89 paint(&(event->area));
90 }
91 return true;
92 }
94 void IconWidget::paint(GdkRectangle const *area)
95 {
96 Gtk::Allocation allocation = get_allocation();
97 int const x_pad = std::max(0, (allocation.get_width() - _size)/2);
98 int const y_pad = std::max(0, (allocation.get_height() - _size)/2);
100 int const x_max = std::max(area->x, allocation.get_x() + x_pad);
101 int const y_max = std::max(area->y, allocation.get_y() + y_pad);
102 int const x_min = std::min(area->x + area->width,
103 allocation.get_x() + x_pad + _size);
104 int const y_min = std::min(area->y + area->height,
105 allocation.get_y() + y_pad + _size);
107 if (_pb) {
108 gdk_draw_pixbuf(this->get_window()->gobj(), NULL, _pb,
109 x_max - allocation.get_x() - x_pad,
110 y_max - allocation.get_y() - y_pad,
111 x_max, y_max,
112 x_min - x_max, y_min - y_max,
113 GDK_RGB_DITHER_NORMAL, x_max, y_max);
114 }
115 }
117 guchar* IconWidget::image_load(gchar const *name, int unsigned size, int unsigned scale)
118 {
119 guchar *px;
121 if (_do_bitmap_icons) {
122 px = image_load_pixmap(name, size, scale);
123 if (!px) {
124 px = image_load_svg(name, size, scale);
125 }
126 } else {
127 px = image_load_svg(name, size, scale);
128 if (!px) {
129 px = image_load_pixmap(name, size, scale);
130 }
131 }
132 return px;
133 }
135 guchar* IconWidget::image_load_pixmap(gchar const *name, int unsigned size, int unsigned scale)
136 {
137 // TODO
138 return NULL;
139 }
141 guchar* IconWidget::image_load_svg(gchar const *name, int unsigned size, int unsigned scale)
142 {
143 // TODO
144 return NULL;
145 }
147 guchar* IconWidget::image_load_gtk(gchar const *name, int unsigned size, int unsigned scale)
148 {
149 // TODO
150 return NULL;
151 }
153 } // namespace Widget
154 } // namespace UI
155 } // namespace Inkscape
157 /*
158 Local Variables:
159 mode:c++
160 c-file-style:"stroustrup"
161 c-file-offsets:((innamespace . 0)(inline-open . 0))
162 indent-tabs-mode:nil
163 fill-column:99
164 End:
165 */
166 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :