1 /** @file
2 * @brief 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,2010 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 <gtk/gtk.h>
19 #include <glib/gmem.h>
20 #include <glibmm/i18n.h>
21 #include <gtkmm/alignment.h>
22 #include <gtkmm/buttonbox.h>
23 #include <gtkmm/stock.h>
25 #include "desktop.h"
26 #include "desktop-handles.h"
27 #include "display/nr-arena.h"
28 #include "document.h"
29 #include "inkscape.h"
30 #include "preferences.h"
31 #include "selection.h"
32 #include "sp-root.h"
33 #include "xml/repr.h"
35 #include "icon-preview.h"
37 extern "C" {
38 // takes doc, root, icon, and icon name to produce pixels
39 guchar *
40 sp_icon_doc_icon( SPDocument *doc, NRArenaItem *root,
41 const gchar *name, unsigned int psize );
42 }
44 namespace Inkscape {
45 namespace UI {
46 namespace Dialog {
49 IconPreviewPanel &IconPreviewPanel::getInstance()
50 {
51 IconPreviewPanel *instance = new IconPreviewPanel();
53 instance->refreshPreview();
55 return *instance;
56 }
58 //#########################################################################
59 //## E V E N T S
60 //#########################################################################
62 void IconPreviewPanel::on_button_clicked(int which)
63 {
64 if ( hot != which ) {
65 buttons[hot]->set_active( false );
67 hot = which;
68 updateMagnify();
69 _getContents()->queue_draw();
70 }
71 }
76 //#########################################################################
77 //## C O N S T R U C T O R / D E S T R U C T O R
78 //#########################################################################
79 /**
80 * Constructor
81 */
82 IconPreviewPanel::IconPreviewPanel() :
83 UI::Widget::Panel("", "/dialogs/iconpreview", SP_VERB_VIEW_ICON_PREVIEW),
84 deskTrack(),
85 desktop(0),
86 document(0),
87 timer(0),
88 pending(false),
89 hot(1),
90 selectionButton(0),
91 desktopChangeConn(),
92 docReplacedConn(),
93 docModConn(),
94 selChangedConn()
95 {
96 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
97 numEntries = 0;
99 std::vector<Glib::ustring> pref_sizes = prefs->getAllDirs("/iconpreview/sizes/default");
100 std::vector<int> rawSizes;
102 for (std::vector<Glib::ustring>::iterator i = pref_sizes.begin(); i != pref_sizes.end(); ++i) {
103 if (prefs->getBool(*i + "/show", true)) {
104 int sizeVal = prefs->getInt(*i + "/value", -1);
105 if (sizeVal > 0) {
106 rawSizes.push_back(sizeVal);
107 }
108 }
109 }
111 if ( !rawSizes.empty() ) {
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 }
120 if ( numEntries < 1 )
121 {
122 numEntries = 5;
123 sizes = new int[numEntries];
124 sizes[0] = 16;
125 sizes[1] = 24;
126 sizes[2] = 32;
127 sizes[3] = 48;
128 sizes[4] = 128;
129 }
131 pixMem = new guchar*[numEntries];
132 images = new Gtk::Image*[numEntries];
133 labels = new Glib::ustring*[numEntries];
134 buttons = new Gtk::ToggleToolButton*[numEntries];
137 for ( int i = 0; i < numEntries; i++ ) {
138 char *label = g_strdup_printf(_("%d x %d"), sizes[i], sizes[i]);
139 labels[i] = new Glib::ustring(label);
140 g_free(label);
141 pixMem[i] = 0;
142 images[i] = 0;
143 }
146 magLabel.set_label( *labels[hot] );
148 Gtk::VBox* magBox = new Gtk::VBox();
150 Gtk::Frame *magFrame = Gtk::manage(new Gtk::Frame(_("Magnified:")));
151 magFrame->add( magnified );
153 magBox->pack_start( *magFrame, Gtk::PACK_EXPAND_WIDGET );
154 magBox->pack_start( magLabel, Gtk::PACK_SHRINK );
157 Gtk::VBox *verts = new Gtk::VBox();
158 Gtk::HBox *horiz = 0;
159 int previous = 0;
160 int avail = 0;
161 for ( int i = numEntries - 1; i >= 0; --i ) {
162 pixMem[i] = new guchar[4 * sizes[i] * sizes[i]];
163 memset( pixMem[i], 0x00, 4 * sizes[i] * sizes[i] );
165 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 );
166 GtkImage* img = GTK_IMAGE( gtk_image_new_from_pixbuf( pb ) );
167 images[i] = Glib::wrap(img);
168 Glib::ustring label(*labels[i]);
169 buttons[i] = new Gtk::ToggleToolButton(label);
170 buttons[i]->set_active( i == hot );
171 Gtk::Frame *frame = new Gtk::Frame();
172 frame->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
173 frame->add(*images[i]);
174 buttons[i]->set_icon_widget(*Gtk::manage(frame));
176 tips.set_tip((*buttons[i]), label);
178 buttons[i]->signal_clicked().connect( sigc::bind<int>( sigc::mem_fun(*this, &IconPreviewPanel::on_button_clicked), i) );
181 Gtk::Alignment *align = Gtk::manage(new Gtk::Alignment(0.5, 0.5, 0, 0));
182 align->add(*buttons[i]);
184 int pad = 12;
185 if ((avail == 0) && (previous == 0)) {
186 verts->pack_end(*align, Gtk::PACK_SHRINK);
187 previous = sizes[i];
188 avail = sizes[i];
189 } else {
190 if ((avail < pad) || ((sizes[i] > avail) && (sizes[i] < previous))) {
191 horiz = 0;
192 }
193 if ((horiz == 0) && (sizes[i] <= previous)) {
194 avail = previous;
195 }
196 if (sizes[i] <= avail) {
197 if (!horiz) {
198 horiz = Gtk::manage(new Gtk::HBox());
199 avail = previous;
200 verts->pack_end(*horiz, Gtk::PACK_SHRINK);
201 }
202 horiz->pack_start(*align, Gtk::PACK_EXPAND_WIDGET);
203 avail -= sizes[i];
204 avail -= pad; // a little extra for padding
205 } else {
206 horiz = 0;
207 verts->pack_end(*align, Gtk::PACK_SHRINK);
208 }
209 }
210 }
212 iconBox.pack_start(splitter);
213 splitter.pack1( *magBox, true, true );
214 Gtk::Frame *actuals = Gtk::manage(new Gtk::Frame(_("Actual Size:")));
215 actuals->add(*verts);
216 splitter.pack2( *actuals, false, false );
219 selectionButton = new Gtk::CheckButton(_("Selection")); // , GTK_RESPONSE_APPLY
220 magBox->pack_start( *selectionButton, Gtk::PACK_SHRINK );
221 tips.set_tip((*selectionButton), _("Selection only or whole document"));
222 selectionButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::modeToggled) );
224 gint val = prefs->getBool("/iconpreview/selectionOnly");
225 selectionButton->set_active( val != 0 );
228 _getContents()->pack_start(iconBox, Gtk::PACK_SHRINK);
230 show_all_children();
232 // Connect this up last
233 desktopChangeConn = deskTrack.connectDesktopChanged( sigc::mem_fun(*this, &IconPreviewPanel::setDesktop) );
234 deskTrack.connect(GTK_WIDGET(gobj()));
235 }
237 IconPreviewPanel::~IconPreviewPanel()
238 {
239 setDesktop(0);
240 if (timer) {
241 timer->stop();
242 delete timer;
243 timer = 0;
244 }
246 selChangedConn.disconnect();
247 docModConn.disconnect();
248 docReplacedConn.disconnect();
249 desktopChangeConn.disconnect();
250 deskTrack.disconnect();
251 }
253 //#########################################################################
254 //## M E T H O D S
255 //#########################################################################
258 void IconPreviewPanel::setDesktop( SPDesktop* desktop )
259 {
260 Panel::setDesktop(desktop);
262 SPDocument *newDoc = (desktop) ? desktop->doc() : 0;
264 if ( desktop != this->desktop ) {
265 docReplacedConn.disconnect();
266 selChangedConn.disconnect();
268 this->desktop = Panel::getDesktop();
269 if ( this->desktop ) {
270 docReplacedConn = this->desktop->connectDocumentReplaced(sigc::hide<0>(sigc::mem_fun(this, &IconPreviewPanel::setDocument)));
271 if (this->desktop->selection) {
272 selChangedConn = desktop->selection->connectChanged(sigc::hide(sigc::mem_fun(this, &IconPreviewPanel::queueRefresh)));
273 }
274 }
275 }
276 setDocument(newDoc);
277 deskTrack.setBase(desktop);
278 }
280 void IconPreviewPanel::setDocument( SPDocument *document )
281 {
282 if (this->document != document) {
283 docModConn.disconnect();
285 this->document = document;
286 if (this->document) {
287 docModConn = this->document->connectModified(sigc::hide(sigc::mem_fun(this, &IconPreviewPanel::queueRefresh)));
288 queueRefresh();
289 }
290 }
291 }
293 void IconPreviewPanel::refreshPreview()
294 {
295 SPDesktop *desktop = getDesktop();
296 if (!timer) {
297 timer = new Glib::Timer();
298 }
299 if (timer->elapsed() < 0.1) {
300 // Do not refresh too quickly
301 queueRefresh();
302 } else if ( desktop ) {
303 if ( selectionButton && selectionButton->get_active() )
304 {
305 Inkscape::Selection * sel = sp_desktop_selection(desktop);
306 if ( sel ) {
307 //g_message("found a selection to play with");
309 GSList const *items = sel->itemList();
310 SPObject *target = 0;
311 while ( items && !target ) {
312 SPItem* item = SP_ITEM( items->data );
313 SPObject * obj = SP_OBJECT(item);
314 gchar const *id = obj->getId();
315 if ( id ) {
316 target = obj;
317 }
319 items = g_slist_next(items);
320 }
321 if ( target ) {
322 renderPreview(target);
323 }
324 }
325 } else {
326 SPObject *target = desktop->currentRoot();
327 if ( target ) {
328 renderPreview(target);
329 }
330 }
331 timer->reset();
332 }
333 }
335 bool IconPreviewPanel::refreshCB()
336 {
337 bool callAgain = true;
338 if (!timer) {
339 timer = new Glib::Timer();
340 }
341 if ( timer->elapsed() > 0.1 ) {
342 callAgain = false;
343 refreshPreview();
344 pending = false;
345 }
346 return callAgain;
347 }
349 void IconPreviewPanel::queueRefresh()
350 {
351 if (!pending) {
352 pending = true;
353 if (!timer) {
354 timer = new Glib::Timer();
355 }
356 Glib::signal_idle().connect( sigc::mem_fun(this, &IconPreviewPanel::refreshCB), Glib::PRIORITY_DEFAULT_IDLE );
357 }
358 }
360 void IconPreviewPanel::modeToggled()
361 {
362 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
363 prefs->setBool("/iconpreview/selectionOnly", (selectionButton && selectionButton->get_active()));
365 refreshPreview();
366 }
368 void IconPreviewPanel::renderPreview( SPObject* obj )
369 {
370 SPDocument * doc = SP_OBJECT_DOCUMENT(obj);
371 gchar const * id = obj->getId();
373 // g_message(" setting up to render '%s' as the icon", id );
375 NRArenaItem *root = NULL;
377 /* Create new arena */
378 NRArena *arena = NRArena::create();
380 /* Create ArenaItem and set transform */
381 unsigned int visionkey = sp_item_display_key_new(1);
383 root = sp_item_invoke_show ( SP_ITEM( SP_DOCUMENT_ROOT(doc) ),
384 arena, visionkey, SP_ITEM_SHOW_DISPLAY );
386 for ( int i = 0; i < numEntries; i++ ) {
387 guchar * px = sp_icon_doc_icon( doc, root, id, sizes[i] );
388 // g_message( " size %d %s", sizes[i], (px ? "worked" : "failed") );
389 if ( px ) {
390 memcpy( pixMem[i], px, sizes[i] * sizes[i] * 4 );
391 g_free( px );
392 px = 0;
393 } else {
394 memset( pixMem[i], 0, sizes[i] * sizes[i] * 4 );
395 }
396 images[i]->queue_draw();
397 }
398 updateMagnify();
400 sp_item_invoke_hide(SP_ITEM(sp_document_root(doc)), visionkey);
401 nr_object_unref((NRObject *) arena);
402 }
404 void IconPreviewPanel::updateMagnify()
405 {
406 Glib::RefPtr<Gdk::Pixbuf> buf = images[hot]->get_pixbuf()->scale_simple( 128, 128, Gdk::INTERP_NEAREST );
407 magLabel.set_label( *labels[hot] );
408 magnified.set( buf );
409 magnified.queue_draw();
410 magnified.get_parent()->queue_draw();
411 }
413 } //namespace Dialogs
414 } //namespace UI
415 } //namespace Inkscape
417 /*
418 Local Variables:
419 mode:c++
420 c-file-style:"stroustrup"
421 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
422 indent-tabs-mode:nil
423 fill-column:99
424 End:
425 */
426 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :