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 "icon-widget.h"
19 namespace Inkscape {
20 namespace UI {
21 namespace Widget {
23 /**
24 * General purpose icon widget, supporting SVG, etc. icon loading
25 *
26 * \param ...
27 *
28 * An icon widget is a ...
29 */
31 IconWidget::IconWidget()
32 {
33 _pb = NULL;
34 _size = 0;
35 }
37 IconWidget::IconWidget(int unsigned size, int unsigned scale, gchar const *name)
38 {
39 _size = std::max((int unsigned)128, std::min(size, (int unsigned)1));
41 char c[256];
42 g_snprintf (c, 256, "%d:%d:%s", _size, scale, name);
43 guchar *pixels = image_load_gtk(name, _size, scale);
45 if (pixels == NULL) {
46 g_warning("Couldn't find matching icon for %s - has this application been installed?", name);
47 _pb = NULL;
48 } else {
49 /* TODO
50 _pb = gdk_pixbuf_new_from_data(pixels, GDK_COLORSPACE_RGB,
51 TRUE, 8, _size, _size, _size * 4,
52 (GdkPixbufDestroyNotify)nr_free, NULL);
53 */
54 }
55 }
57 IconWidget::IconWidget(int unsigned size, guchar const *px)
58 {
59 _size = std::max((int unsigned)128, std::min(size, (int unsigned)1));
61 /*
62 _pb = gdk_pixbuf_new_from_data((guchar *)px, GDK_COLORSPACE_RGB,
63 TRUE, 8, _size, _size, _size * 4, NULL, NULL);
64 */
65 }
67 IconWidget::~IconWidget() {
68 }
70 void IconWidget::size_request(Gtk::Requisition &requisition)
71 {
72 requisition.width = _size;
73 requisition.height = _size;
74 }
76 void IconWidget::size_allocate(Gtk::Allocation const &allocation)
77 {
78 Gtk::Widget::size_allocate(allocation);
80 if (this->is_drawable()) {
81 this->queue_draw();
82 }
83 }
85 int IconWidget::expose(GdkEventExpose *event)
86 {
87 if (this->is_drawable()) {
88 paint(&(event->area));
89 }
90 return true;
91 }
93 void IconWidget::paint(GdkRectangle const *area)
94 {
95 Gtk::Allocation allocation = get_allocation();
96 int const x_pad = std::max(0, (allocation.get_width() - _size)/2);
97 int const y_pad = std::max(0, (allocation.get_height() - _size)/2);
99 int const x_max = std::max(area->x, allocation.get_x() + x_pad);
100 int const y_max = std::max(area->y, allocation.get_y() + y_pad);
101 int const x_min = std::min(area->x + area->width,
102 allocation.get_x() + x_pad + _size);
103 int const y_min = std::min(area->y + area->height,
104 allocation.get_y() + y_pad + _size);
106 if (_pb) {
107 gdk_draw_pixbuf(this->get_window()->gobj(), NULL, _pb,
108 x_max - allocation.get_x() - x_pad,
109 y_max - allocation.get_y() - y_pad,
110 x_max, y_max,
111 x_min - x_max, y_min - y_max,
112 GDK_RGB_DITHER_NORMAL, x_max, y_max);
113 }
114 }
116 guchar* IconWidget::image_load(gchar const *name, int unsigned size, int unsigned scale)
117 {
118 guchar *px;
120 if (_do_bitmap_icons) {
121 px = image_load_pixmap(name, size, scale);
122 if (!px) {
123 px = image_load_svg(name, size, scale);
124 }
125 } else {
126 px = image_load_svg(name, size, scale);
127 if (!px) {
128 px = image_load_pixmap(name, size, scale);
129 }
130 }
131 return px;
132 }
134 guchar* IconWidget::image_load_pixmap(gchar const *name, int unsigned size, int unsigned scale)
135 {
136 // TODO
137 return NULL;
138 }
140 guchar* IconWidget::image_load_svg(gchar const *name, int unsigned size, int unsigned scale)
141 {
142 // TODO
143 return NULL;
144 }
146 guchar* IconWidget::image_load_gtk(gchar const *name, int unsigned size, int unsigned scale)
147 {
148 // TODO
149 return NULL;
150 }
152 } // namespace Widget
153 } // namespace UI
154 } // namespace Inkscape
156 /*
157 Local Variables:
158 mode:c++
159 c-file-style:"stroustrup"
160 c-file-offsets:((innamespace . 0)(inline-open . 0))
161 indent-tabs-mode:nil
162 fill-column:99
163 End:
164 */
165 // vim: filetype=c++:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :