Code

From trunk
[inkscape.git] / src / dialogs / iconpreview.cpp
index b3ea55df333085e363c3080482b1912d58027878..f31914e3eb88d8cdabf5de9cb14f18e8d9cb249a 100644 (file)
 
 #include <gtk/gtk.h>
 
+#include <glib/gmem.h>
 #include <gtk/gtkdialog.h> //for GTK_RESPONSE* types
 #include <glibmm/i18n.h>
 #include <gtkmm/buttonbox.h>
 #include <gtkmm/stock.h>
 
-#include "prefs-utils.h"
+#include "preferences.h"
 #include "inkscape.h"
 #include "document.h"
 #include "desktop-handles.h"
@@ -46,18 +47,14 @@ namespace UI {
 namespace Dialogs {
 
 
-IconPreviewPanel* IconPreviewPanel::instance = 0;
-
-
-IconPreviewPanel& IconPreviewPanel::getInstance()
+IconPreviewPanel&
+IconPreviewPanel::getInstance()
 {
-    if ( !instance ) {
-        instance = new IconPreviewPanel();
-    }
+    static IconPreviewPanel &instance = *new IconPreviewPanel();
 
-    instance->refreshPreview();
+    instance.refreshPreview();
 
-    return *instance;
+    return instance;
 }
 
 //#########################################################################
@@ -71,7 +68,7 @@ void IconPreviewPanel::on_button_clicked(int which)
 
         hot = which;
         updateMagnify();
-        queue_draw();
+        _getContents()->queue_draw();
     }
 }
 
@@ -85,39 +82,32 @@ void IconPreviewPanel::on_button_clicked(int which)
  * Constructor
  */
 IconPreviewPanel::IconPreviewPanel() :
-    Panel(),
+    UI::Widget::Panel("", "/dialogs/iconpreview", SP_VERB_VIEW_ICON_PREVIEW),
     hot(1),
     refreshButton(0),
     selectionButton(0)
 {
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     numEntries = 0;
-    Inkscape::XML::Node *things = inkscape_get_repr(INKSCAPE, "iconpreview.sizes.default");
-    if (things) {
-        std::vector<int> rawSizes;
-        for ( Inkscape::XML::Node *child = things->firstChild(); child; child = child->next() )
-        {
-            gchar const *id = child->attribute("id");
-            if ( id )
-            {
-                std::string path("iconpreview.sizes.default.");
-                path += id;
-                gint show = prefs_get_int_attribute_limited( path.c_str(), "show", 1, 0, 1 );
-                gint sizeVal = prefs_get_int_attribute( path.c_str(), "value", -1 );
-                if ( show && (sizeVal > 0) )
-                {
-                    rawSizes.push_back( sizeVal );
-                }
+
+    std::vector<Glib::ustring> pref_sizes = prefs->getAllDirs("/iconpreview/sizes/default");
+    std::vector<int> rawSizes;
+    
+    for (std::vector<Glib::ustring>::iterator i = pref_sizes.begin(); i != pref_sizes.end(); ++i) {
+        if (prefs->getBool(*i + "/show", true)) {
+            int sizeVal = prefs->getInt(*i + "/value", -1);
+            if (sizeVal > 0) {
+                rawSizes.push_back(sizeVal);
             }
         }
+    }
 
-        if ( !rawSizes.empty() )
-        {
-            numEntries = rawSizes.size();
-            sizes = new int[numEntries];
-            int i = 0;
-            for ( std::vector<int>::iterator it = rawSizes.begin(); it != rawSizes.end(); ++it, ++i ) {
-                sizes[i] = *it;
-            }
+    if ( !rawSizes.empty() ) {
+        numEntries = rawSizes.size();
+        sizes = new int[numEntries];
+        int i = 0;
+        for ( std::vector<int>::iterator it = rawSizes.begin(); it != rawSizes.end(); ++it, ++i ) {
+            sizes[i] = *it;
         }
     }
 
@@ -129,7 +119,7 @@ IconPreviewPanel::IconPreviewPanel() :
         sizes[1] = 24;
         sizes[2] = 32;
         sizes[3] = 48;
-        sizes[5] = 128;
+        sizes[4] = 128;
     }
 
     pixMem = new guchar*[numEntries];
@@ -160,7 +150,7 @@ IconPreviewPanel::IconPreviewPanel() :
         pixMem[i] = new guchar[4 * sizes[i] * sizes[i]];
         memset( pixMem[i], 0x00, 4 *  sizes[i] * sizes[i] );
 
-        GdkPixbuf *pb = gdk_pixbuf_new_from_data( pixMem[i], GDK_COLORSPACE_RGB, TRUE, 8, sizes[i], sizes[i], sizes[i] * 4, /*(GdkPixbufDestroyNotify)nr_free*/NULL, NULL );
+        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 );
         GtkImage* img = GTK_IMAGE( gtk_image_new_from_pixbuf( pb ) );
         images[i] = Glib::wrap(img);
         Glib::ustring label(*labels[i]);
@@ -185,14 +175,14 @@ IconPreviewPanel::IconPreviewPanel() :
 
 
     Gtk::HButtonBox* holder = new Gtk::HButtonBox( Gtk::BUTTONBOX_END );
-    pack_end( *holder, false, false );
+    _getContents()->pack_end(*holder, false, false);
 
     selectionButton = new Gtk::ToggleButton(_("Selection")); // , GTK_RESPONSE_APPLY
     holder->pack_start( *selectionButton, false, false );
     tips.set_tip((*selectionButton), _("Selection only or whole document"));
     selectionButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::modeToggled) );
 
-    gint val = prefs_get_int_attribute_limited( "iconpreview", "selectionOnly", 0, 0, 1 );
+    gint val = prefs->getBool("/iconpreview/selectionOnly");
     selectionButton->set_active( val != 0 );
 
     refreshButton = new Gtk::Button(Gtk::Stock::REFRESH); // , GTK_RESPONSE_APPLY
@@ -201,7 +191,7 @@ IconPreviewPanel::IconPreviewPanel() :
     refreshButton->signal_clicked().connect( sigc::mem_fun(*this, &IconPreviewPanel::refreshPreview) );
 
 
-    pack_start(iconBox, Gtk::PACK_EXPAND_WIDGET);
+    _getContents()->pack_start(iconBox, Gtk::PACK_EXPAND_WIDGET);
 
     show_all_children();
 }
@@ -213,12 +203,12 @@ IconPreviewPanel::IconPreviewPanel() :
 
 void IconPreviewPanel::refreshPreview()
 {
-    SPDesktop *desktop = SP_ACTIVE_DESKTOP;
+    SPDesktop *desktop = getDesktop();
     if ( desktop ) {
 
         if ( selectionButton && selectionButton->get_active() )
         {
-            Inkscape::Selection * sel = SP_DT_SELECTION(desktop);
+            Inkscape::Selection * sel = sp_desktop_selection(desktop);
             if ( sel ) {
                 //g_message("found a selection to play with");
 
@@ -251,7 +241,8 @@ void IconPreviewPanel::refreshPreview()
 
 void IconPreviewPanel::modeToggled()
 {
-    prefs_set_int_attribute( "iconpreview", "selectionOnly", (selectionButton && selectionButton->get_active()) ? 1 : 0 );
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setBool("/iconpreview/selectionOnly", (selectionButton && selectionButton->get_active()));
 
     refreshPreview();
 }
@@ -271,7 +262,6 @@ void IconPreviewPanel::renderPreview( SPObject* obj )
     /* Create ArenaItem and set transform */
     unsigned int visionkey = sp_item_display_key_new(1);
 
-    /* fixme: Memory manage root if needed (Lauris) */
     root = sp_item_invoke_show ( SP_ITEM( SP_DOCUMENT_ROOT(doc) ),
                                  arena, visionkey, SP_ITEM_SHOW_DISPLAY );
 
@@ -288,6 +278,9 @@ void IconPreviewPanel::renderPreview( SPObject* obj )
         images[i]->queue_draw();
     }
     updateMagnify();
+
+    sp_item_invoke_hide(SP_ITEM(sp_document_root(doc)), visionkey);
+    nr_object_unref((NRObject *) arena);
 }
 
 void IconPreviewPanel::updateMagnify()
@@ -308,13 +301,9 @@ void IconPreviewPanel::updateMagnify()
   Local Variables:
   mode:c++
   c-file-style:"stroustrup"
-  c-file-offsets:((innamespace . 0)(inline-open . 0))
+  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
   indent-tabs-mode:nil
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
-
-//#########################################################################
-//## E N D    O F    F I L E
-//#########################################################################
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :