Code

now that selection description includes style (filtered, clipped), we need to update...
[inkscape.git] / src / dialogs / iconpreview.cpp
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 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 "preferences.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     static 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     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
91     numEntries = 0;
93     std::vector<Glib::ustring> pref_sizes = prefs->getAllDirs("/iconpreview/sizes/default");
94     std::vector<int> rawSizes;
95     
96     for (std::vector<Glib::ustring>::iterator i = pref_sizes.begin(); i != pref_sizes.end(); ++i) {
97         if (prefs->getBool(*i + "/show", true)) {
98             int sizeVal = prefs->getInt(*i + "/value", -1);
99             if (sizeVal > 0) {
100                 rawSizes.push_back(sizeVal);
101             }
102         }
103     }
105     if ( !rawSizes.empty() ) {
106         numEntries = rawSizes.size();
107         sizes = new int[numEntries];
108         int i = 0;
109         for ( std::vector<int>::iterator it = rawSizes.begin(); it != rawSizes.end(); ++it, ++i ) {
110             sizes[i] = *it;
111         }
112     }
114     if ( numEntries < 1 )
115     {
116         numEntries = 5;
117         sizes = new int[numEntries];
118         sizes[0] = 16;
119         sizes[1] = 24;
120         sizes[2] = 32;
121         sizes[3] = 48;
122         sizes[4] = 128;
123     }
125     pixMem = new guchar*[numEntries];
126     images = new Gtk::Image*[numEntries];
127     labels = new Glib::ustring*[numEntries];
128     buttons = new Gtk::ToggleToolButton*[numEntries];
131     for ( int i = 0; i < numEntries; i++ ) {
132         char *label = g_strdup_printf(_("%d x %d"), sizes[i], sizes[i]);
133         labels[i] = new Glib::ustring(label);
134         g_free(label);
135         pixMem[i] = 0;
136         images[i] = 0;
137     }
140     magLabel.set_label( *labels[hot] );
142     Gtk::VBox* magBox = new Gtk::VBox();
144     magBox->pack_start( magnified );
145     magBox->pack_start( magLabel, Gtk::PACK_SHRINK );
148     Gtk::VBox * verts = new Gtk::VBox();
149     for ( int i = 0; i < numEntries; i++ ) {
150         pixMem[i] = new guchar[4 * sizes[i] * sizes[i]];
151         memset( pixMem[i], 0x00, 4 *  sizes[i] * sizes[i] );
153         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 );
154         GtkImage* img = GTK_IMAGE( gtk_image_new_from_pixbuf( pb ) );
155         images[i] = Glib::wrap(img);
156         Glib::ustring label(*labels[i]);
157         buttons[i] = new Gtk::ToggleToolButton(label);
158         buttons[i]->set_active( i == hot );
159         buttons[i]->set_icon_widget(*images[i]);
161         tips.set_tip((*buttons[i]), label);
163         buttons[i]->signal_clicked().connect( sigc::bind<int>( sigc::mem_fun(*this, &IconPreviewPanel::on_button_clicked), i) );
166         verts->add(*buttons[i]);
167     }
169     iconBox.pack_start(splitter);
170     splitter.pack1( *magBox, true, true );
171     splitter.pack2( *verts, false, false );
174     //## The Refresh button
177     Gtk::HButtonBox* holder = new Gtk::HButtonBox( Gtk::BUTTONBOX_END );
178     _getContents()->pack_end(*holder, false, false);
180     selectionButton = new Gtk::ToggleButton(_("Selection")); // , GTK_RESPONSE_APPLY
181     holder->pack_start( *selectionButton, false, false );
182     tips.set_tip((*selectionButton), _("Selection only or whole document"));
183     selectionButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::modeToggled) );
185     gint val = prefs->getBool("/iconpreview/selectionOnly");
186     selectionButton->set_active( val != 0 );
188     refreshButton = new Gtk::Button(Gtk::Stock::REFRESH); // , GTK_RESPONSE_APPLY
189     holder->pack_end( *refreshButton, false, false );
190     tips.set_tip((*refreshButton), _("Refresh the icons"));
191     refreshButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::refreshPreview) );
194     _getContents()->pack_start(iconBox, Gtk::PACK_EXPAND_WIDGET);
196     show_all_children();
199 //#########################################################################
200 //## M E T H O D S
201 //#########################################################################
204 void IconPreviewPanel::refreshPreview()
206     SPDesktop *desktop = getDesktop();
207     if ( desktop ) {
209         if ( selectionButton && selectionButton->get_active() )
210         {
211             Inkscape::Selection * sel = sp_desktop_selection(desktop);
212             if ( sel ) {
213                 //g_message("found a selection to play with");
215                 GSList const *items = sel->itemList();
216                 SPObject *target = 0;
217                 while ( items && !target ) {
218                     SPItem* item = SP_ITEM( items->data );
219                     SPObject * obj = SP_OBJECT(item);
220                     gchar const *id = SP_OBJECT_ID( obj );
221                     if ( id ) {
222                         target = obj;
223                     }
225                     items = g_slist_next(items);
226                 }
227                 if ( target ) {
228                     renderPreview(target);
229                 }
230             }
231         }
232         else
233         {
234             SPObject *target = desktop->currentRoot();
235             if ( target ) {
236                 renderPreview(target);
237             }
238         }
239     }
242 void IconPreviewPanel::modeToggled()
244     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
245     prefs->setBool("/iconpreview/selectionOnly", (selectionButton && selectionButton->get_active()));
247     refreshPreview();
250 void IconPreviewPanel::renderPreview( SPObject* obj )
252     SPDocument * doc = SP_OBJECT_DOCUMENT(obj);
253     gchar * id = SP_OBJECT_ID(obj);
255 //    g_message(" setting up to render '%s' as the icon", id );
257     NRArenaItem *root = NULL;
259     /* Create new arena */
260     NRArena *arena = NRArena::create();
262     /* Create ArenaItem and set transform */
263     unsigned int visionkey = sp_item_display_key_new(1);
265     root = sp_item_invoke_show ( SP_ITEM( SP_DOCUMENT_ROOT(doc) ),
266                                  arena, visionkey, SP_ITEM_SHOW_DISPLAY );
268     for ( int i = 0; i < numEntries; i++ ) {
269         guchar * px = sp_icon_doc_icon( doc, root, id, sizes[i] );
270 //         g_message( " size %d %s", sizes[i], (px ? "worked" : "failed") );
271         if ( px ) {
272             memcpy( pixMem[i], px, sizes[i] * sizes[i] * 4 );
273             g_free( px );
274             px = 0;
275         } else {
276             memset( pixMem[i], 0, sizes[i] * sizes[i] * 4 );
277         }
278         images[i]->queue_draw();
279     }
280     updateMagnify();
282     sp_item_invoke_hide(SP_ITEM(sp_document_root(doc)), visionkey);
283     nr_object_unref((NRObject *) arena);
286 void IconPreviewPanel::updateMagnify()
288     Glib::RefPtr<Gdk::Pixbuf> buf = images[hot]->get_pixbuf()->scale_simple( 128, 128, Gdk::INTERP_NEAREST );
289     magLabel.set_label( *labels[hot] );
290     magnified.set( buf );
291     magnified.queue_draw();
292     magnified.get_parent()->queue_draw();
296 } //namespace Dialogs
297 } //namespace UI
298 } //namespace Inkscape
300 /*
301   Local Variables:
302   mode:c++
303   c-file-style:"stroustrup"
304   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
305   indent-tabs-mode:nil
306   fill-column:99
307   End:
308 */
309 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :