Code

Merge and cleanup of GSoC C++-ification project.
[inkscape.git] / src / dialogs / xml-tree.cpp
index 33ac02a9b0df915c5dd9347bc771dd1a8ce5002c..c90cde490ba02dfd0812b1c57a19ddadfbe2934f 100644 (file)
@@ -1,68 +1,48 @@
-#define __SP_XMLVIEW_TREE_C__
-
-/**
- * \brief  XML View
- *
- * Authors:
+/** @file
+ * @brief XML editor
+ */
+/* Authors:
  *   Lauris Kaplinski <lauris@kaplinski.com>
  *   MenTaLguY <mental@rydia.net>
  *   bulia byak <buliabyak@users.sf.net>
  *   Johan Engelen <goejendaagh@zonnet.nl>
+ *   David Turner
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 1999-2006 Authors
- * Copyright (C) 2004 David Turner
- *
  * Released under GNU GPL, read the file 'COPYING' for more information
  */
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#endif
-
-#include <gtk/gtkmain.h>
-#include <gtk/gtkhbox.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtkhpaned.h>
-#include <gtk/gtkvpaned.h>
-#include <gtk/gtkhseparator.h>
-#include <gtk/gtkhbbox.h>
-#include <gtk/gtktoolbar.h>
-#include <gtk/gtkscrolledwindow.h>
-#include <gtk/gtkentry.h>
-#include <gtk/gtkarrow.h>
-
+#include <gdk/gdkkeysyms.h>
 #include <glibmm/i18n.h>
+#include <gtk/gtk.h>
+
+#include "desktop.h"
+#include "../desktop-handles.h"
+#include "dialog-events.h"
+#include "../document.h"
+#include "../event-context.h"
 #include "helper/window.h"
-#include "macros.h"
 #include "../inkscape.h"
-#include "../document.h"
-#include "../desktop-handles.h"
-#include "desktop.h"
+#include "../interface.h"
+#include "macros.h"
+#include "message-context.h"
+#include "message-stack.h"
+#include "../preferences.h"
 #include "../selection.h"
+#include "shortcuts.h"
+#include "../sp-root.h"
 #include "../sp-string.h"
 #include "../sp-tspan.h"
-#include "../sp-root.h"
-#include "../event-context.h"
-#include "in-dt-coordsys.h"
-
-
-#include "../widgets/sp-xmlview-tree.h"
-#include "../widgets/sp-xmlview-content.h"
-#include "../widgets/sp-xmlview-attr-list.h"
-
-#include "../inkscape-stock.h"
-#include "widgets/icon.h"
-
-#include "dialog-events.h"
-#include "../prefs-utils.h"
+#include "ui/icon-names.h"
 #include "../verbs.h"
-#include "../interface.h"
-
-#include "shortcuts.h"
-#include <gdk/gdkkeysyms.h>
+#include "widgets/icon.h"
+#include "../widgets/sp-xmlview-attr-list.h"
+#include "../widgets/sp-xmlview-content.h"
+#include "../widgets/sp-xmlview-tree.h"
 
-#include "message-stack.h"
-#include "message-context.h"
+using Inkscape::DocumentUndo;
 
 #define MIN_ONSCREEN_DISTANCE 50
 
@@ -78,7 +58,7 @@ static sigc::connection document_replaced_connection;
 static win_data wd;
 // impossible original values to make sure they are read from prefs
 static gint x = -1000, y = -1000, w = 0, h = 0;
-static gchar const *const prefs_path = "dialogs.xml";
+static Glib::ustring const prefs_path = "/dialogs/xml/";
 static GtkWidget *status = NULL;
 static Inkscape::MessageStack *_message_stack = NULL;
 static Inkscape::MessageContext *_message_context = NULL;
@@ -97,7 +77,8 @@ static SPDocument *current_document = NULL;
 static gint selected_attr = 0;
 static Inkscape::XML::Node *selected_repr = NULL;
 
-static void sp_xmltree_desktop_change( Inkscape::Application *inkscape,  SPDesktop *desktop, GtkWidget *dialog );
+static void sp_xmltree_desktop_activate( Inkscape::Application *inkscape,  SPDesktop *desktop, GtkWidget *dialog );
+static void sp_xmltree_desktop_deactivate( Inkscape::Application *inkscape,  SPDesktop *desktop, GtkWidget *dialog );
 
 static void set_tree_desktop(SPDesktop *desktop);
 static void set_tree_document(SPDocument *document);
@@ -164,6 +145,7 @@ static void cmd_set_attr(GtkObject *object, gpointer data);
 
 static gboolean sp_xml_tree_key_press(GtkWidget *widget, GdkEventKey *event);
 
+static bool in_dt_coordsys(SPObject const &item);
 
 /*
  * \brief Sets the XML status bar when the tree is selected.
@@ -212,13 +194,14 @@ void sp_xml_tree_dialog()
         gtk_tooltips_enable(tooltips);
 
         dlg = sp_window_new("", TRUE);
+        Inkscape::Preferences *prefs = Inkscape::Preferences::get();
         if (x == -1000 || y == -1000) {
-            x = prefs_get_int_attribute(prefs_path, "x", -1000);
-            y = prefs_get_int_attribute(prefs_path, "y", -1000);
+            x = prefs->getInt(prefs_path + "x", -1000);
+            y = prefs->getInt(prefs_path + "y", -1000);
         }
         if (w ==0 || h == 0) {
-            w = prefs_get_int_attribute(prefs_path, "w", 0);
-            h = prefs_get_int_attribute(prefs_path, "h", 0);
+            w = prefs->getInt(prefs_path + "w", 0);
+            h = prefs->getInt(prefs_path + "h", 0);
         }
 
 //        if (x<0) x=0;
@@ -304,7 +287,7 @@ void sp_xml_tree_dialog()
                 _("New element node"),
                 NULL,
                 sp_icon_new( Inkscape::ICON_SIZE_LARGE_TOOLBAR,
-                                    INKSCAPE_STOCK_ADD_XML_ELEMENT_NODE ),
+                                    INKSCAPE_ICON_XML_ELEMENT_NEW ),
                 G_CALLBACK(cmd_new_element_node),
                 NULL);
 
@@ -325,7 +308,7 @@ void sp_xml_tree_dialog()
         button = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
                 NULL, _("New text node"), NULL,
                 sp_icon_new( Inkscape::ICON_SIZE_LARGE_TOOLBAR,
-                             INKSCAPE_STOCK_ADD_XML_TEXT_NODE ),
+                             INKSCAPE_ICON_XML_TEXT_NEW ),
                 G_CALLBACK(cmd_new_text_node),
                 NULL);
 
@@ -346,7 +329,7 @@ void sp_xml_tree_dialog()
         button = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
                 NULL, _("Duplicate node"), NULL,
                 sp_icon_new( Inkscape::ICON_SIZE_LARGE_TOOLBAR,
-                             INKSCAPE_STOCK_DUPLICATE_XML_NODE ),
+                             INKSCAPE_ICON_XML_NODE_DUPLICATE ),
                 G_CALLBACK(cmd_duplicate_node),
                 NULL);
 
@@ -365,9 +348,9 @@ void sp_xml_tree_dialog()
         gtk_toolbar_append_space(GTK_TOOLBAR(toolbar));
 
         button = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
-                NULL, _("Delete node"), NULL,
+                NULL, Q_("nodeAsInXMLdialogTooltip|Delete node"), NULL,
                 sp_icon_new( Inkscape::ICON_SIZE_LARGE_TOOLBAR,
-                             INKSCAPE_STOCK_DELETE_XML_NODE ),
+                             INKSCAPE_ICON_XML_NODE_DELETE ),
                                            G_CALLBACK(cmd_delete_node), NULL );
 
         gtk_signal_connect_while_alive(GTK_OBJECT(tree), "tree_select_row",
@@ -467,7 +450,7 @@ void sp_xml_tree_dialog()
         button = gtk_toolbar_append_item(GTK_TOOLBAR(toolbar),
                 NULL, _("Delete attribute"), NULL,
                 sp_icon_new( Inkscape::ICON_SIZE_LARGE_TOOLBAR,
-                             INKSCAPE_STOCK_DELETE_XML_ATTRIBUTE ),
+                             INKSCAPE_ICON_XML_ATTRIBUTE_DELETE ),
                (GCallback) cmd_delete_attr, NULL);
 
         gtk_signal_connect_while_alive(GTK_OBJECT(attributes), "select_row",
@@ -611,10 +594,10 @@ void sp_xml_tree_dialog()
         gtk_widget_hide(text_container);
 
         g_signal_connect( G_OBJECT(INKSCAPE), "activate_desktop",
-                           G_CALLBACK(sp_xmltree_desktop_change), dlg);
+                           G_CALLBACK(sp_xmltree_desktop_activate), dlg);
 
         g_signal_connect( G_OBJECT(INKSCAPE), "deactivate_desktop",
-                           G_CALLBACK(sp_xmltree_desktop_change), dlg);
+                           G_CALLBACK(sp_xmltree_desktop_deactivate), dlg);
 
         g_signal_connect((GObject *) dlg, "key_press_event", (GCallback) sp_xml_tree_key_press, NULL);
 
@@ -649,16 +632,20 @@ static gboolean sp_xml_tree_key_press(GtkWidget */*widget*/, GdkEventKey *event)
 }
 
 
-static void sp_xmltree_desktop_change(Inkscape::Application */*inkscape*/,
+static void sp_xmltree_desktop_activate(Inkscape::Application */*inkscape*/,
                                       SPDesktop *desktop,
                                       GtkWidget */*dialog*/ )
 {
-    if (!desktop) {
-        return;
-    }
     set_tree_desktop(desktop);
 }
 
+static void sp_xmltree_desktop_deactivate(Inkscape::Application */*inkscape*/,
+                                      SPDesktop */*desktop*/,
+                                      GtkWidget */*dialog*/ )
+{
+    set_tree_desktop(NULL);
+}
+
 
 void set_tree_desktop(SPDesktop *desktop)
 {
@@ -696,9 +683,8 @@ void set_tree_document(SPDocument *document)
     if (current_document) {
 
         document_uri_set_connection = current_document->connectURISet(sigc::bind(sigc::ptr_fun(&on_document_uri_set), current_document));
-        on_document_uri_set(SP_DOCUMENT_URI(current_document), current_document);
-        set_tree_repr(sp_document_repr_root(current_document));
-
+        on_document_uri_set( current_document->getURI(), current_document );
+        set_tree_repr(current_document->getReprRoot());
     } else {
         set_tree_repr(NULL);
     }
@@ -887,10 +873,10 @@ void after_tree_move(GtkCTree */*tree*/,
     if (GTK_CTREE_ROW(node)->parent  == new_parent &&
         GTK_CTREE_ROW(node)->sibling == new_sibling)
     {
-        sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                         _("Drag XML subtree"));
+        DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                           _("Drag XML subtree"));
     } else {
-        sp_document_cancel(current_document);
+        DocumentUndo::cancel(current_document);
     }
 }
 
@@ -924,10 +910,11 @@ static gboolean on_delete(GtkObject */*object*/, GdkEvent */*event*/, gpointer /
     if (x<0) x=0;
     if (y<0) y=0;
 
-    prefs_set_int_attribute(prefs_path, "x", x);
-    prefs_set_int_attribute(prefs_path, "y", y);
-    prefs_set_int_attribute(prefs_path, "w", w);
-    prefs_set_int_attribute(prefs_path, "h", h);
+    Inkscape::Preferences *prefs = Inkscape::Preferences::get();
+    prefs->setInt(prefs_path + "x", x);
+    prefs->setInt(prefs_path + "y", y);
+    prefs->setInt(prefs_path + "w", w);
+    prefs->setInt(prefs_path + "h", h);
 
     return FALSE; // which means, go ahead and destroy it
 }
@@ -1157,7 +1144,7 @@ void on_tree_select_row_enable_if_indentable(GtkCTree *tree, GtkCTreeNode *node,
             // skip to the child just before the current repr
             for ( prev = parent->firstChild() ;
                   prev && prev->next() != repr ;
-                  prev = prev->next() );
+                  prev = prev->next() ){};
 
             if (prev && prev->type() == Inkscape::XML::ELEMENT_NODE) {
                 indentable = TRUE;
@@ -1291,7 +1278,7 @@ void on_document_uri_set(gchar const */*uri*/, SPDocument *document)
 {
     gchar title[500];
     sp_ui_dialog_title_string(Inkscape::Verb::get(SP_VERB_DIALOG_XML_EDITOR), title);
-    gchar *t = g_strdup_printf("%s: %s", SP_DOCUMENT_NAME(document), title);
+    gchar *t = g_strdup_printf("%s: %s", document->getName(), title);
     gtk_window_set_title(GTK_WINDOW(dlg), t);
     g_free(t);
 }
@@ -1378,16 +1365,17 @@ void cmd_new_element_node(GtkObject */*object*/, gpointer /*data*/)
     gtk_main();
 
     if (selected_repr != NULL && name.text) {
-        Inkscape::XML::Document *xml_doc = sp_document_repr_doc(current_document);
+        Inkscape::XML::Document *xml_doc = current_document->getReprDoc();
         Inkscape::XML::Node *new_repr;
         new_repr = xml_doc->createElement(name.text);
+        Inkscape::GC::release(new_repr);
         g_free(name.text);
         selected_repr->appendChild(new_repr);
         set_tree_select(new_repr);
         set_dt_select(new_repr);
 
-        sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Create new element node"));
+        DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                           _("Create new element node"));
     }
 
 } // end of cmd_new_element_node()
@@ -1398,12 +1386,12 @@ void cmd_new_text_node(GtkObject */*object*/, gpointer /*data*/)
 {
     g_assert(selected_repr != NULL);
 
-    Inkscape::XML::Document *xml_doc = sp_document_repr_doc(current_document);
+    Inkscape::XML::Document *xml_doc = current_document->getReprDoc();
     Inkscape::XML::Node *text = xml_doc->createTextNode("");
     selected_repr->appendChild(text);
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Create new text node"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Create new text node"));
 
     set_tree_select(text);
     set_dt_select(text);
@@ -1420,8 +1408,8 @@ void cmd_duplicate_node(GtkObject */*object*/, gpointer /*data*/)
     Inkscape::XML::Node *dup = selected_repr->duplicate(parent->document());
     parent->addChild(dup, selected_repr);
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Duplicate node"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Duplicate node"));
 
     GtkCTreeNode *node = sp_xmlview_tree_get_repr_node(SP_XMLVIEW_TREE(tree), dup);
 
@@ -1437,8 +1425,8 @@ void cmd_delete_node(GtkObject */*object*/, gpointer /*data*/)
     g_assert(selected_repr != NULL);
     sp_repr_unparent(selected_repr);
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Delete node"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       Q_("nodeAsInXMLinHistoryDialog|Delete node"));
 }
 
 
@@ -1455,8 +1443,8 @@ void cmd_delete_attr(GtkObject */*object*/, gpointer /*data*/)
         updated->updateRepr();
     }
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Delete attribute"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Delete attribute"));
 }
 
 
@@ -1484,8 +1472,8 @@ void cmd_set_attr(GtkObject */*object*/, gpointer /*data*/)
         updated->updateRepr();
     }
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Change attribute"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Change attribute"));
 
     /* TODO: actually, the row won't have been created yet.  why? */
     gint row = sp_xmlview_attr_list_find_row_from_key(GTK_CLIST(attributes),
@@ -1514,8 +1502,8 @@ void cmd_raise_node(GtkObject */*object*/, gpointer /*data*/)
 
     parent->changeOrder(selected_repr, ref);
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Raise node"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Raise node"));
 
     set_tree_select(selected_repr);
     set_dt_select(selected_repr);
@@ -1531,8 +1519,8 @@ void cmd_lower_node(GtkObject */*object*/, gpointer /*data*/)
 
     parent->changeOrder(selected_repr, selected_repr->next());
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Lower node"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Lower node"));
 
     set_tree_select(selected_repr);
     set_dt_select(selected_repr);
@@ -1555,14 +1543,14 @@ void cmd_indent_node(GtkObject */*object*/, gpointer /*data*/)
 
     Inkscape::XML::Node* ref = NULL;
     if (prev->firstChild()) {
-        for( ref = prev->firstChild() ; ref->next() ; ref = ref->next() );
+        for( ref = prev->firstChild() ; ref->next() ; ref = ref->next() ){};
     }
 
     parent->removeChild(repr);
     prev->addChild(repr, ref);
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Indent node"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Indent node"));
     set_tree_select(repr);
     set_dt_select(repr);
 
@@ -1582,13 +1570,38 @@ void cmd_unindent_node(GtkObject */*object*/, gpointer /*data*/)
     parent->removeChild(repr);
     grandparent->addChild(repr, parent);
 
-    sp_document_done(current_document, SP_VERB_DIALOG_XML_EDITOR,
-                     _("Unindent node"));
+    DocumentUndo::done(current_document, SP_VERB_DIALOG_XML_EDITOR,
+                       _("Unindent node"));
     set_tree_select(repr);
     set_dt_select(repr);
 
 } // end of cmd_unindent_node()
 
+/** Returns true iff \a item is suitable to be included in the selection, in particular
+    whether it has a bounding box in the desktop coordinate system for rendering resize handles.
+
+    Descendents of <defs> nodes (markers etc.) return false, for example.
+*/
+bool in_dt_coordsys(SPObject const &item)
+{
+    /* Definition based on sp_item_i2doc_affine. */
+    SPObject const *child = &item;
+    g_return_val_if_fail(child != NULL, false);
+    for(;;) {
+        if (!SP_IS_ITEM(child)) {
+            return false;
+        }
+        SPObject const * const parent = SP_OBJECT_PARENT(child);
+        if (parent == NULL) {
+            break;
+        }
+        child = parent;
+    }
+    g_assert(SP_IS_ROOT(child));
+    /* Relevance: Otherwise, I'm not sure whether to return true or false. */
+    return true;
+}
+
 
 /*
   Local Variables:
@@ -1599,4 +1612,4 @@ void cmd_unindent_node(GtkObject */*object*/, gpointer /*data*/)
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :