Code

Merge from trunk.
[inkscape.git] / src / widgets / desktop-widget.cpp
index ab440595f24e0e6a06c12e934cb8aa2e3698c4fa..4620b15328e0137603f11dcf50078cf0426513a5 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_DESKTOP_WIDGET_C__
-
 /** \file
  * Desktop widget implementation
  */
@@ -10,6 +8,8 @@
  *   Ralf Stephan <ralf@ark.in-berlin.de>
  *   John Bintz <jcoswell@coswellproductions.org>
  *   Johan Engelen <j.b.c.engelen@ewi.utwente.nl>
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2007 Johan Engelen
  * Copyright (C) 2006 John Bintz
@@ -41,6 +41,7 @@
 #include <extension/db.h>
 #include "file.h"
 #include "helper/units.h"
+#include "helper/unit-tracker.h"
 #include "inkscape-private.h"
 #include "interface.h"
 #include "macros.h"
@@ -55,6 +56,7 @@
 #include "ui/widget/layer-selector.h"
 #include "ui/widget/selected-style.h"
 #include "ui/uxmanager.h"
+#include "util/ege-appear-time-tracker.h"
 
 // We're in the "widgets" directory, so no need to explicitly prefix these:
 #include "button.h"
 using Inkscape::round;
 #endif
 
-
+using Inkscape::UnitTracker;
 using Inkscape::UI::UXManager;
 using Inkscape::UI::ToolboxFactory;
+using ege::AppearTimeTracker;
 
 #ifdef WITH_INKBOARD
 #endif
@@ -120,6 +123,30 @@ static void sp_dtw_sticky_zoom_toggled (GtkMenuItem *item, gpointer data);
 
 SPViewWidgetClass *dtw_parent_class;
 
+static GTimer *baseTimer = 0;
+static bool timeReported = false;
+
+static void timeGoing(gchar const* id)
+{
+    if ( !baseTimer ) {
+        g_message("Starting time at point [%s]", id);
+        baseTimer = g_timer_new();
+    }
+}
+
+static void checkTime(gchar const* msg)
+{
+    if ( baseTimer && !timeReported ) {
+        timeReported = true;
+        g_timer_stop(baseTimer);
+        gulong msCount = 0;
+        gdouble secs = g_timer_elapsed( baseTimer, &msCount );
+        g_message("Time ended at %2.3f with [%s]", secs, msg);
+    }
+}
+
+
+
 class CMSPrefWatcher {
 public:
     CMSPrefWatcher() :
@@ -242,6 +269,8 @@ SPDesktopWidget::window_get_pointer()
     return Geom::Point(x,y);
 }
 
+static GTimer *overallTimer = 0;
+
 /**
  * Registers SPDesktopWidget class and returns its type number.
  */
@@ -249,6 +278,7 @@ GType SPDesktopWidget::getType(void)
 {
     static GtkType type = 0;
     if (!type) {
+        timeGoing("SPDesktopWidget::getType");
         GTypeInfo info = {
             sizeof(SPDesktopWidgetClass),
             0, // base_init
@@ -262,6 +292,8 @@ GType SPDesktopWidget::getType(void)
             0 // value_table
         };
         type = g_type_register_static(SP_TYPE_VIEW_WIDGET, "SPDesktopWidget", &info, static_cast<GTypeFlags>(0));
+        // Begin a timer to watch for the first desktop to appear on-screen
+        overallTimer = g_timer_new();
     }
     return type;
 }
@@ -272,6 +304,7 @@ GType SPDesktopWidget::getType(void)
 static void
 sp_desktop_widget_class_init (SPDesktopWidgetClass *klass)
 {
+    timeGoing("sp_desktop_widget_class_init");
     dtw_parent_class = (SPViewWidgetClass*)gtk_type_class (SP_TYPE_VIEW_WIDGET);
 
     GtkObjectClass *object_class = (GtkObjectClass *) klass;
@@ -288,6 +321,7 @@ sp_desktop_widget_class_init (SPDesktopWidgetClass *klass)
  */
 void SPDesktopWidget::init( SPDesktopWidget *dtw )
 {
+    timeGoing("SPDesktopWidget::init");
     GtkWidget *widget;
     GtkWidget *tbl;
     GtkWidget *canvas_tbl;
@@ -325,7 +359,7 @@ void SPDesktopWidget::init( SPDesktopWidget *dtw )
     dtw->hbox = gtk_hbox_new(FALSE, 0);
     gtk_box_pack_end( GTK_BOX (dtw->vbox), dtw->hbox, TRUE, TRUE, 0 );
     gtk_widget_show(dtw->hbox);
-
     dtw->aux_toolbox = ToolboxFactory::createAuxToolbox();
     gtk_box_pack_end (GTK_BOX (dtw->vbox), dtw->aux_toolbox, FALSE, TRUE, 0);
 
@@ -563,6 +597,18 @@ void SPDesktopWidget::init( SPDesktopWidget *dtw )
     gtk_widget_show_all (dtw->vbox);
 
     gtk_widget_grab_focus (GTK_WIDGET(dtw->canvas));
+
+    // If this is the first desktop created, report the time it takes to show up
+    if ( overallTimer ) {
+        if ( prefs->getBool("/dialogs/debug/trackAppear", false) ) {
+            // Time tracker takes ownership of the timer.
+            AppearTimeTracker *tracker = new AppearTimeTracker(overallTimer, GTK_WIDGET(dtw), "first SPDesktopWidget");
+            tracker->setAutodelete(true);
+        } else {
+            g_timer_destroy(overallTimer);
+        }
+        overallTimer = 0;
+    }
 }
 
 /**
@@ -708,8 +754,8 @@ sp_desktop_widget_realize (GtkWidget *widget)
     NRRect d;
     d.x0 = 0.0;
     d.y0 = 0.0;
-    d.x1 = sp_document_width (dtw->desktop->doc());
-    d.y1 = sp_document_height (dtw->desktop->doc());
+    d.x1 = (dtw->desktop->doc())->getWidth ();
+    d.y1 = (dtw->desktop->doc())->getHeight ();
 
     if ((fabs (d.x1 - d.x0) < 1.0) || (fabs (d.y1 - d.y0) < 1.0)) return;
 
@@ -730,7 +776,7 @@ void SPDesktopWidget::updateNamedview()
     modified_connection = desktop->namedview->connectModified(sigc::mem_fun(*this, &SPDesktopWidget::namedviewModified));
     namedviewModified(desktop->namedview, SP_OBJECT_MODIFIED_FLAG);
 
-    updateTitle(SP_DOCUMENT_NAME (desktop->doc()));
+    updateTitle( desktop->doc()->getName() );
 }
 
 /**
@@ -857,7 +903,7 @@ SPDesktopWidget::shutdown()
                 GTK_BUTTONS_NONE,
                 _("<span weight=\"bold\" size=\"larger\">Save changes to document \"%s\" before closing?</span>\n\n"
                   "If you close without saving, your changes will be discarded."),
-                SP_DOCUMENT_NAME(doc));
+                doc->getName());
             // fix for bug 1767940:
             GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(GTK_MESSAGE_DIALOG(dialog)->label), GTK_CAN_FOCUS);
 
@@ -879,12 +925,12 @@ SPDesktopWidget::shutdown()
             {
                 Gtk::Window *window = (Gtk::Window*)gtk_object_get_data (GTK_OBJECT(this), "window");
 
-                sp_document_ref(doc);
+                doc->doRef();
                 sp_namedview_document_from_window(desktop);
                 if (sp_file_save_document(*window, doc)) {
-                    sp_document_unref(doc);
+                    doc->doUnref();
                 } else { // save dialog cancelled or save failed
-                    sp_document_unref(doc);
+                    doc->doUnref();
                     return TRUE;
                 }
 
@@ -899,7 +945,7 @@ SPDesktopWidget::shutdown()
         }
         /* Code to check data loss */
         bool allow_data_loss = FALSE;
-        while (sp_document_repr_root(doc)->attribute("inkscape:dataloss") != NULL && allow_data_loss == FALSE) {
+        while (doc->getReprRoot()->attribute("inkscape:dataloss") != NULL && allow_data_loss == FALSE) {
             GtkWidget *dialog;
 
             /** \todo
@@ -913,7 +959,7 @@ SPDesktopWidget::shutdown()
                 GTK_BUTTONS_NONE,
                 _("<span weight=\"bold\" size=\"larger\">The file \"%s\" was saved with a format (%s) that may cause data loss!</span>\n\n"
                   "Do you want to save this file as Inkscape SVG?"),
-                SP_DOCUMENT_NAME(doc)? SP_DOCUMENT_NAME(doc) : "Unnamed",
+                doc->getName() ? doc->getName() : "Unnamed",
                 SP_MODULE_KEY_OUTPUT_SVG_INKSCAPE);
             // fix for bug 1767940:
             GTK_WIDGET_UNSET_FLAGS(GTK_WIDGET(GTK_MESSAGE_DIALOG(dialog)->label), GTK_CAN_FOCUS);
@@ -937,14 +983,14 @@ SPDesktopWidget::shutdown()
             switch (response) {
             case GTK_RESPONSE_YES:
             {
-                sp_document_ref(doc);
+                doc->doRef();
 
                 Gtk::Window *window = (Gtk::Window*)gtk_object_get_data (GTK_OBJECT(this), "window");
 
                 if (sp_file_save_dialog(*window, doc, Inkscape::Extension::FILE_SAVE_METHOD_INKSCAPE_SVG)) {
-                    sp_document_unref(doc);
+                    doc->doUnref();
                 } else { // save dialog cancelled or save failed
-                    sp_document_unref(doc);
+                    doc->doUnref();
                     return TRUE;
                 }
 
@@ -1393,8 +1439,6 @@ SPViewWidget *sp_desktop_widget_new( SPNamedView *namedview )
 {
     SPDesktopWidget* dtw = SPDesktopWidget::createInstance(namedview);
 
-    UXManager::getInstance()->addTrack(dtw);
-
     return SP_VIEW_WIDGET(dtw);
 }
 
@@ -1440,10 +1484,12 @@ SPDesktopWidget* SPDesktopWidget::createInstance(SPNamedView *namedview)
     toolboxes.push_back(dtw->aux_toolbox);
     toolboxes.push_back(dtw->commands_toolbox);
     toolboxes.push_back(dtw->snap_toolbox);
-    UXManager::getInstance()->connectToDesktop( toolboxes, dtw->desktop );
 
     dtw->panels->setDesktop( dtw->desktop );
 
+    UXManager::getInstance()->addTrack(dtw);
+    UXManager::getInstance()->connectToDesktop( toolboxes, dtw->desktop );
+
     return dtw;
 }
 
@@ -1510,6 +1556,40 @@ void SPDesktopWidget::namedviewModified(SPObject *obj, guint flags)
         sp_ruler_set_metric(GTK_RULER (this->vruler), nv->getDefaultMetric());
         sp_ruler_set_metric(GTK_RULER (this->hruler), nv->getDefaultMetric());
 
+        /* This loops through all the grandchildren of aux toolbox,
+         * and for each that it finds, it performs an sp_search_by_data_recursive(),
+         * looking for widgets that hold some "tracker" data (this is used by
+         * all toolboxes to refer to the unit selector). The default document units 
+         * is then selected within these unit selectors.
+         *
+         * Of course it would be nice to be able to refer to the toolbox and the
+         * unit selector directly by name, but I don't yet see a way to do that.
+         * 
+         * This should solve: https://bugs.launchpad.net/inkscape/+bug/362995
+         */
+        if (GTK_IS_CONTAINER(aux_toolbox)) {
+            GList *ch = gtk_container_get_children (GTK_CONTAINER(aux_toolbox));
+            for (GList *i = ch; i != NULL; i = i->next) {
+                if (GTK_IS_CONTAINER(i->data)) {
+                    GList *grch = gtk_container_get_children (GTK_CONTAINER(i->data));
+                    for (GList *j = grch; j != NULL; j = j->next) {                        
+                        if (!GTK_IS_WIDGET(j->data)) // wasn't a widget
+                            continue;
+
+                        gpointer t = sp_search_by_data_recursive(GTK_WIDGET(j->data), (gpointer) "tracker");
+                        if (t == NULL) // didn't find any tracker data
+                            continue;
+
+                        UnitTracker *tracker = reinterpret_cast<UnitTracker*>( t );
+                        if (tracker == NULL) // it's null when inkscape is first opened
+                            continue;
+
+                        tracker->setActiveUnit( nv->doc_units );
+                    } // grandchildren
+                } // if child is a container
+            } // children
+        } // if aux_toolbox is a container
+
         gtk_tooltips_set_tip(this->tt, this->hruler_box, gettext(sp_unit_get_plural (nv->doc_units)), NULL);
         gtk_tooltips_set_tip(this->tt, this->vruler_box, gettext(sp_unit_get_plural (nv->doc_units)), NULL);
 
@@ -1540,7 +1620,7 @@ bool SPDesktopWidget::onFocusInEvent(GdkEventFocus*)
 {
     Inkscape::Preferences *prefs = Inkscape::Preferences::get();
     if (prefs->getBool("/options/bitmapautoreload/value", true)) {
-        GSList const *imageList = sp_document_get_resource_list(desktop->doc(), "image");
+        GSList const *imageList = (desktop->doc())->getResourceList("image");
         for (GSList const *p = imageList; p; p = p->next) {
             SPImage* image = SP_IMAGE(p->data);
             sp_image_refresh_if_outdated( image );
@@ -1802,11 +1882,11 @@ sp_desktop_widget_update_scrollbars (SPDesktopWidget *dtw, double scale)
 
     /* The desktop region we always show unconditionally */
     SPDocument *doc = dtw->desktop->doc();
-    Geom::Rect darea ( Geom::Point(-sp_document_width(doc), -sp_document_height(doc)),
-                     Geom::Point(2 * sp_document_width(doc), 2 * sp_document_height(doc))  );
+    Geom::Rect darea ( Geom::Point(-doc->getWidth(), -doc->getHeight()),
+                     Geom::Point(2 * doc->getWidth(), 2 * doc->getHeight())  );
     SPObject* root = doc->root;
     SPItem* item = SP_ITEM(root);
-    Geom::OptRect deskarea = Geom::unify(darea, sp_item_bbox_desktop(item));
+    Geom::OptRect deskarea = Geom::unify(darea, item->getBboxDesktop());
 
     /* Canvas region we always show unconditionally */
     Geom::Rect carea( Geom::Point(deskarea->min()[Geom::X] * scale - 64, deskarea->max()[Geom::Y] * -scale - 64),