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 bool pack = prefs->getBool("/iconpreview/pack", true);
101 std::vector<Glib::ustring> pref_sizes = prefs->getAllDirs("/iconpreview/sizes/default");
102 std::vector<int> rawSizes;
104 for (std::vector<Glib::ustring>::iterator i = pref_sizes.begin(); i != pref_sizes.end(); ++i) {
105 if (prefs->getBool(*i + "/show", true)) {
106 int sizeVal = prefs->getInt(*i + "/value", -1);
107 if (sizeVal > 0) {
108 rawSizes.push_back(sizeVal);
109 }
110 }
111 }
113 if ( !rawSizes.empty() ) {
114 numEntries = rawSizes.size();
115 sizes = new int[numEntries];
116 int i = 0;
117 for ( std::vector<int>::iterator it = rawSizes.begin(); it != rawSizes.end(); ++it, ++i ) {
118 sizes[i] = *it;
119 }
120 }
122 if ( numEntries < 1 )
123 {
124 numEntries = 5;
125 sizes = new int[numEntries];
126 sizes[0] = 16;
127 sizes[1] = 24;
128 sizes[2] = 32;
129 sizes[3] = 48;
130 sizes[4] = 128;
131 }
133 pixMem = new guchar*[numEntries];
134 images = new Gtk::Image*[numEntries];
135 labels = new Glib::ustring*[numEntries];
136 buttons = new Gtk::ToggleToolButton*[numEntries];
139 for ( int i = 0; i < numEntries; i++ ) {
140 char *label = g_strdup_printf(_("%d x %d"), sizes[i], sizes[i]);
141 labels[i] = new Glib::ustring(label);
142 g_free(label);
143 pixMem[i] = 0;
144 images[i] = 0;
145 }
148 magLabel.set_label( *labels[hot] );
150 Gtk::VBox* magBox = new Gtk::VBox();
152 Gtk::Frame *magFrame = Gtk::manage(new Gtk::Frame(_("Magnified:")));
153 magFrame->add( magnified );
155 magBox->pack_start( *magFrame, Gtk::PACK_EXPAND_WIDGET );
156 magBox->pack_start( magLabel, Gtk::PACK_SHRINK );
159 Gtk::VBox *verts = new Gtk::VBox();
160 Gtk::HBox *horiz = 0;
161 int previous = 0;
162 int avail = 0;
163 for ( int i = numEntries - 1; i >= 0; --i ) {
164 pixMem[i] = new guchar[4 * sizes[i] * sizes[i]];
165 memset( pixMem[i], 0x00, 4 * sizes[i] * sizes[i] );
167 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 );
168 GtkImage* img = GTK_IMAGE( gtk_image_new_from_pixbuf( pb ) );
169 images[i] = Glib::wrap(img);
170 Glib::ustring label(*labels[i]);
171 buttons[i] = new Gtk::ToggleToolButton(label);
172 buttons[i]->set_active( i == hot );
173 Gtk::Frame *frame = new Gtk::Frame();
174 frame->set_shadow_type(Gtk::SHADOW_ETCHED_IN);
175 frame->add(*images[i]);
176 buttons[i]->set_icon_widget(*Gtk::manage(frame));
178 tips.set_tip((*buttons[i]), label);
180 buttons[i]->signal_clicked().connect( sigc::bind<int>( sigc::mem_fun(*this, &IconPreviewPanel::on_button_clicked), i) );
183 Gtk::Alignment *align = Gtk::manage(new Gtk::Alignment(0.5, 0.5, 0, 0));
184 align->add(*buttons[i]);
186 int pad = 12;
187 if ( !pack || ( (avail == 0) && (previous == 0) ) ) {
188 verts->pack_end(*align, Gtk::PACK_SHRINK);
189 previous = sizes[i];
190 avail = sizes[i];
191 } else {
192 if ((avail < pad) || ((sizes[i] > avail) && (sizes[i] < previous))) {
193 horiz = 0;
194 }
195 if ((horiz == 0) && (sizes[i] <= previous)) {
196 avail = previous;
197 }
198 if (sizes[i] <= avail) {
199 if (!horiz) {
200 horiz = Gtk::manage(new Gtk::HBox());
201 avail = previous;
202 verts->pack_end(*horiz, Gtk::PACK_SHRINK);
203 }
204 horiz->pack_start(*align, Gtk::PACK_EXPAND_WIDGET);
205 avail -= sizes[i];
206 avail -= pad; // a little extra for padding
207 } else {
208 horiz = 0;
209 verts->pack_end(*align, Gtk::PACK_SHRINK);
210 }
211 }
212 }
214 iconBox.pack_start(splitter);
215 splitter.pack1( *magBox, true, true );
216 Gtk::Frame *actuals = Gtk::manage(new Gtk::Frame(_("Actual Size:")));
217 actuals->add(*verts);
218 splitter.pack2( *actuals, false, false );
221 selectionButton = new Gtk::CheckButton(_("Selection")); // , GTK_RESPONSE_APPLY
222 magBox->pack_start( *selectionButton, Gtk::PACK_SHRINK );
223 tips.set_tip((*selectionButton), _("Selection only or whole document"));
224 selectionButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::modeToggled) );
226 gint val = prefs->getBool("/iconpreview/selectionOnly");
227 selectionButton->set_active( val != 0 );
230 _getContents()->pack_start(iconBox, Gtk::PACK_SHRINK);
232 show_all_children();
234 // Connect this up last
235 desktopChangeConn = deskTrack.connectDesktopChanged( sigc::mem_fun(*this, &IconPreviewPanel::setDesktop) );
236 deskTrack.connect(GTK_WIDGET(gobj()));
237 }
239 IconPreviewPanel::~IconPreviewPanel()
240 {
241 setDesktop(0);
242 if (timer) {
243 timer->stop();
244 delete timer;
245 timer = 0;
246 }
248 selChangedConn.disconnect();
249 docModConn.disconnect();
250 docReplacedConn.disconnect();
251 desktopChangeConn.disconnect();
252 deskTrack.disconnect();
253 }
255 //#########################################################################
256 //## M E T H O D S
257 //#########################################################################
260 void IconPreviewPanel::setDesktop( SPDesktop* desktop )
261 {
262 Panel::setDesktop(desktop);
264 SPDocument *newDoc = (desktop) ? desktop->doc() : 0;
266 if ( desktop != this->desktop ) {
267 docReplacedConn.disconnect();
268 selChangedConn.disconnect();
270 this->desktop = Panel::getDesktop();
271 if ( this->desktop ) {
272 docReplacedConn = this->desktop->connectDocumentReplaced(sigc::hide<0>(sigc::mem_fun(this, &IconPreviewPanel::setDocument)));
273 if (this->desktop->selection) {
274 selChangedConn = desktop->selection->connectChanged(sigc::hide(sigc::mem_fun(this, &IconPreviewPanel::queueRefresh)));
275 }
276 }
277 }
278 setDocument(newDoc);
279 deskTrack.setBase(desktop);
280 }
282 void IconPreviewPanel::setDocument( SPDocument *document )
283 {
284 if (this->document != document) {
285 docModConn.disconnect();
287 this->document = document;
288 if (this->document) {
289 docModConn = this->document->connectModified(sigc::hide(sigc::mem_fun(this, &IconPreviewPanel::queueRefresh)));
290 queueRefresh();
291 }
292 }
293 }
295 void IconPreviewPanel::refreshPreview()
296 {
297 SPDesktop *desktop = getDesktop();
298 if (!timer) {
299 timer = new Glib::Timer();
300 }
301 if (timer->elapsed() < 0.1) {
302 // Do not refresh too quickly
303 queueRefresh();
304 } else if ( desktop ) {
305 if ( selectionButton && selectionButton->get_active() )
306 {
307 Inkscape::Selection * sel = sp_desktop_selection(desktop);
308 if ( sel ) {
309 //g_message("found a selection to play with");
311 GSList const *items = sel->itemList();
312 SPObject *target = 0;
313 while ( items && !target ) {
314 SPItem* item = SP_ITEM( items->data );
315 SPObject * obj = SP_OBJECT(item);
316 gchar const *id = obj->getId();
317 if ( id ) {
318 target = obj;
319 }
321 items = g_slist_next(items);
322 }
323 if ( target ) {
324 renderPreview(target);
325 }
326 }
327 } else {
328 SPObject *target = desktop->currentRoot();
329 if ( target ) {
330 renderPreview(target);
331 }
332 }
333 timer->reset();
334 }
335 }
337 bool IconPreviewPanel::refreshCB()
338 {
339 bool callAgain = true;
340 if (!timer) {
341 timer = new Glib::Timer();
342 }
343 if ( timer->elapsed() > 0.1 ) {
344 callAgain = false;
345 refreshPreview();
346 pending = false;
347 }
348 return callAgain;
349 }
351 void IconPreviewPanel::queueRefresh()
352 {
353 if (!pending) {
354 pending = true;
355 if (!timer) {
356 timer = new Glib::Timer();
357 }
358 Glib::signal_idle().connect( sigc::mem_fun(this, &IconPreviewPanel::refreshCB), Glib::PRIORITY_DEFAULT_IDLE );
359 }
360 }
362 void IconPreviewPanel::modeToggled()
363 {
364 Inkscape::Preferences *prefs = Inkscape::Preferences::get();
365 prefs->setBool("/iconpreview/selectionOnly", (selectionButton && selectionButton->get_active()));
367 refreshPreview();
368 }
370 void IconPreviewPanel::renderPreview( SPObject* obj )
371 {
372 SPDocument * doc = SP_OBJECT_DOCUMENT(obj);
373 gchar const * id = obj->getId();
375 // g_message(" setting up to render '%s' as the icon", id );
377 NRArenaItem *root = NULL;
379 /* Create new arena */
380 NRArena *arena = NRArena::create();
382 /* Create ArenaItem and set transform */
383 unsigned int visionkey = sp_item_display_key_new(1);
385 root = sp_item_invoke_show ( SP_ITEM( SP_DOCUMENT_ROOT(doc) ),
386 arena, visionkey, SP_ITEM_SHOW_DISPLAY );
388 for ( int i = 0; i < numEntries; i++ ) {
389 guchar * px = sp_icon_doc_icon( doc, root, id, sizes[i] );
390 // g_message( " size %d %s", sizes[i], (px ? "worked" : "failed") );
391 if ( px ) {
392 memcpy( pixMem[i], px, sizes[i] * sizes[i] * 4 );
393 g_free( px );
394 px = 0;
395 } else {
396 memset( pixMem[i], 0, sizes[i] * sizes[i] * 4 );
397 }
398 images[i]->queue_draw();
399 }
400 updateMagnify();
402 sp_item_invoke_hide(SP_ITEM(sp_document_root(doc)), visionkey);
403 nr_object_unref((NRObject *) arena);
404 }
406 void IconPreviewPanel::updateMagnify()
407 {
408 Glib::RefPtr<Gdk::Pixbuf> buf = images[hot]->get_pixbuf()->scale_simple( 128, 128, Gdk::INTERP_NEAREST );
409 magLabel.set_label( *labels[hot] );
410 magnified.set( buf );
411 magnified.queue_draw();
412 magnified.get_parent()->queue_draw();
413 }
415 } //namespace Dialogs
416 } //namespace UI
417 } //namespace Inkscape
419 /*
420 Local Variables:
421 mode:c++
422 c-file-style:"stroustrup"
423 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
424 indent-tabs-mode:nil
425 fill-column:99
426 End:
427 */
428 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :