1 /*
2 * A simple dialog for previewing icon representation.
3 *
4 * Authors:
5 * Jon A. Cruz
6 * Bob Jamison
7 * Other dudes from The Inkscape Organization
8 *
9 * Copyright (C) 2004 Bob Jamison
10 * Copyright (C) 2005 Jon A. Cruz
11 *
12 * Released under GNU GPL, read the file 'COPYING' for more information
13 */
14 #ifdef HAVE_CONFIG_H
15 # include <config.h>
16 #endif
18 #include "iconpreview.h"
20 #include <gtk/gtk.h>
22 #include <glib/gmem.h>
23 #include <gtk/gtkdialog.h> //for GTK_RESPONSE* types
24 #include <glibmm/i18n.h>
25 #include <gtkmm/buttonbox.h>
26 #include <gtkmm/stock.h>
28 #include "prefs-utils.h"
29 #include "inkscape.h"
30 #include "document.h"
31 #include "desktop-handles.h"
32 #include "selection.h"
33 #include "desktop.h"
34 #include "display/nr-arena.h"
35 #include "sp-root.h"
36 #include "xml/repr.h"
38 extern "C" {
39 // takes doc, root, icon, and icon name to produce pixels
40 guchar *
41 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
42 const gchar *name, unsigned int psize );
43 }
45 namespace Inkscape {
46 namespace UI {
47 namespace Dialogs {
50 IconPreviewPanel&
51 IconPreviewPanel::getInstance()
52 {
53 IconPreviewPanel &instance = *new IconPreviewPanel();
55 instance.refreshPreview();
57 return instance;
58 }
60 //#########################################################################
61 //## E V E N T S
62 //#########################################################################
64 void IconPreviewPanel::on_button_clicked(int which)
65 {
66 if ( hot != which ) {
67 buttons[hot]->set_active( false );
69 hot = which;
70 updateMagnify();
71 _getContents()->queue_draw();
72 }
73 }
78 //#########################################################################
79 //## C O N S T R U C T O R / D E S T R U C T O R
80 //#########################################################################
81 /**
82 * Constructor
83 */
84 IconPreviewPanel::IconPreviewPanel() :
85 UI::Widget::Panel("", "dialogs.iconpreview", SP_VERB_VIEW_ICON_PREVIEW),
86 hot(1),
87 refreshButton(0),
88 selectionButton(0)
89 {
90 numEntries = 0;
91 Inkscape::XML::Node *things = inkscape_get_repr(INKSCAPE, "iconpreview.sizes.default");
92 if (things) {
93 std::vector<int> rawSizes;
94 for ( Inkscape::XML::Node *child = things->firstChild(); child; child = child->next() )
95 {
96 gchar const *id = child->attribute("id");
97 if ( id )
98 {
99 std::string path("iconpreview.sizes.default.");
100 path += id;
101 gint show = prefs_get_int_attribute_limited( path.c_str(), "show", 1, 0, 1 );
102 gint sizeVal = prefs_get_int_attribute( path.c_str(), "value", -1 );
103 if ( show && (sizeVal > 0) )
104 {
105 rawSizes.push_back( sizeVal );
106 }
107 }
108 }
110 if ( !rawSizes.empty() )
111 {
112 numEntries = rawSizes.size();
113 sizes = new int[numEntries];
114 int i = 0;
115 for ( std::vector<int>::iterator it = rawSizes.begin(); it != rawSizes.end(); ++it, ++i ) {
116 sizes[i] = *it;
117 }
118 }
119 }
121 if ( numEntries < 1 )
122 {
123 numEntries = 5;
124 sizes = new int[numEntries];
125 sizes[0] = 16;
126 sizes[1] = 24;
127 sizes[2] = 32;
128 sizes[3] = 48;
129 sizes[4] = 128;
130 }
132 pixMem = new guchar*[numEntries];
133 images = new Gtk::Image*[numEntries];
134 labels = new Glib::ustring*[numEntries];
135 buttons = new Gtk::ToggleToolButton*[numEntries];
138 for ( int i = 0; i < numEntries; i++ ) {
139 char *label = g_strdup_printf(_("%d x %d"), sizes[i], sizes[i]);
140 labels[i] = new Glib::ustring(label);
141 g_free(label);
142 pixMem[i] = 0;
143 images[i] = 0;
144 }
147 magLabel.set_label( *labels[hot] );
149 Gtk::VBox* magBox = new Gtk::VBox();
151 magBox->pack_start( magnified );
152 magBox->pack_start( magLabel, Gtk::PACK_SHRINK );
155 Gtk::VBox * verts = new Gtk::VBox();
156 for ( int i = 0; i < numEntries; i++ ) {
157 pixMem[i] = new guchar[4 * sizes[i] * sizes[i]];
158 memset( pixMem[i], 0x00, 4 * sizes[i] * sizes[i] );
160 GdkPixbuf *pb = gdk_pixbuf_new_from_data( pixMem[i], GDK_COLORSPACE_RGB, TRUE, 8, sizes[i], sizes[i], sizes[i] * 4, /*(GdkPixbufDestroyNotify)g_free*/NULL, NULL );
161 GtkImage* img = GTK_IMAGE( gtk_image_new_from_pixbuf( pb ) );
162 images[i] = Glib::wrap(img);
163 Glib::ustring label(*labels[i]);
164 buttons[i] = new Gtk::ToggleToolButton(label);
165 buttons[i]->set_active( i == hot );
166 buttons[i]->set_icon_widget(*images[i]);
168 tips.set_tip((*buttons[i]), label);
170 buttons[i]->signal_clicked().connect( sigc::bind<int>( sigc::mem_fun(*this, &IconPreviewPanel::on_button_clicked), i) );
173 verts->add(*buttons[i]);
174 }
176 iconBox.pack_start(splitter);
177 splitter.pack1( *magBox, true, true );
178 splitter.pack2( *verts, false, false );
181 //## The Refresh button
184 Gtk::HButtonBox* holder = new Gtk::HButtonBox( Gtk::BUTTONBOX_END );
185 _getContents()->pack_end(*holder, false, false);
187 selectionButton = new Gtk::ToggleButton(_("Selection")); // , GTK_RESPONSE_APPLY
188 holder->pack_start( *selectionButton, false, false );
189 tips.set_tip((*selectionButton), _("Selection only or whole document"));
190 selectionButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::modeToggled) );
192 gint val = prefs_get_int_attribute_limited( "iconpreview", "selectionOnly", 0, 0, 1 );
193 selectionButton->set_active( val != 0 );
195 refreshButton = new Gtk::Button(Gtk::Stock::REFRESH); // , GTK_RESPONSE_APPLY
196 holder->pack_end( *refreshButton, false, false );
197 tips.set_tip((*refreshButton), _("Refresh the icons"));
198 refreshButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::refreshPreview) );
201 _getContents()->pack_start(iconBox, Gtk::PACK_EXPAND_WIDGET);
203 show_all_children();
204 }
206 //#########################################################################
207 //## M E T H O D S
208 //#########################################################################
211 void IconPreviewPanel::refreshPreview()
212 {
213 SPDesktop *desktop = getDesktop();
214 if ( desktop ) {
216 if ( selectionButton && selectionButton->get_active() )
217 {
218 Inkscape::Selection * sel = sp_desktop_selection(desktop);
219 if ( sel ) {
220 //g_message("found a selection to play with");
222 GSList const *items = sel->itemList();
223 SPObject *target = 0;
224 while ( items && !target ) {
225 SPItem* item = SP_ITEM( items->data );
226 SPObject * obj = SP_OBJECT(item);
227 gchar const *id = SP_OBJECT_ID( obj );
228 if ( id ) {
229 target = obj;
230 }
232 items = g_slist_next(items);
233 }
234 if ( target ) {
235 renderPreview(target);
236 }
237 }
238 }
239 else
240 {
241 SPObject *target = desktop->currentRoot();
242 if ( target ) {
243 renderPreview(target);
244 }
245 }
246 }
247 }
249 void IconPreviewPanel::modeToggled()
250 {
251 prefs_set_int_attribute( "iconpreview", "selectionOnly", (selectionButton && selectionButton->get_active()) ? 1 : 0 );
253 refreshPreview();
254 }
256 void IconPreviewPanel::renderPreview( SPObject* obj )
257 {
258 SPDocument * doc = SP_OBJECT_DOCUMENT(obj);
259 gchar * id = SP_OBJECT_ID(obj);
261 // g_message(" setting up to render '%s' as the icon", id );
263 NRArenaItem *root = NULL;
265 /* Create new arena */
266 NRArena *arena = NRArena::create();
268 /* Create ArenaItem and set transform */
269 unsigned int visionkey = sp_item_display_key_new(1);
271 /* fixme: Memory manage root if needed (Lauris) */
272 root = sp_item_invoke_show ( SP_ITEM( SP_DOCUMENT_ROOT(doc) ),
273 arena, visionkey, SP_ITEM_SHOW_DISPLAY );
275 for ( int i = 0; i < numEntries; i++ ) {
276 guchar * px = sp_icon_doc_icon( doc, root, id, sizes[i] );
277 // g_message( " size %d %s", sizes[i], (px ? "worked" : "failed") );
278 if ( px ) {
279 memcpy( pixMem[i], px, sizes[i] * sizes[i] * 4 );
280 g_free( px );
281 px = 0;
282 } else {
283 memset( pixMem[i], 0, sizes[i] * sizes[i] * 4 );
284 }
285 images[i]->queue_draw();
286 }
287 updateMagnify();
288 }
290 void IconPreviewPanel::updateMagnify()
291 {
292 Glib::RefPtr<Gdk::Pixbuf> buf = images[hot]->get_pixbuf()->scale_simple( 128, 128, Gdk::INTERP_NEAREST );
293 magLabel.set_label( *labels[hot] );
294 magnified.set( buf );
295 magnified.queue_draw();
296 magnified.get_parent()->queue_draw();
297 }
300 } //namespace Dialogs
301 } //namespace UI
302 } //namespace Inkscape
304 /*
305 Local Variables:
306 mode:c++
307 c-file-style:"stroustrup"
308 c-file-offsets:((innamespace . 0)(inline-open . 0))
309 indent-tabs-mode:nil
310 fill-column:99
311 End:
312 */
313 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
315 //#########################################################################
316 //## E N D O F F I L E
317 //#########################################################################