Code

Move app-specific logic and file operations up from the lower level.
[inkscape.git] / src / dialogs / xml-tree.cpp
index 6da0096e206346eed254a0580e4ad90fe77e94b8..934045b33119029a72eee8d3c5555e7c1c4624b1 100644 (file)
@@ -1,36 +1,18 @@
-#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
  *
  * 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 <gtk/gtk.h>
 #include <glibmm/i18n.h>
 #include "helper/window.h"
 #include "macros.h"
@@ -54,7 +36,7 @@
 #include "widgets/icon.h"
 
 #include "dialog-events.h"
-#include "../prefs-utils.h"
+#include "../preferences.h"
 #include "../verbs.h"
 #include "../interface.h"
 
@@ -78,7 +60,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 *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 +79,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);
@@ -212,13 +195,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;
@@ -611,10 +595,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 +633,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)
 {
@@ -772,7 +760,7 @@ void propagate_tree_select(Inkscape::XML::Node *repr)
         sp_xmlview_attr_list_set_repr(attributes, NULL);
     }
 
-    if (repr && ( repr->type() == Inkscape::XML::TEXT_NODE || repr->type() == Inkscape::XML::COMMENT_NODE ) ) {
+    if (repr && ( repr->type() == Inkscape::XML::TEXT_NODE || repr->type() == Inkscape::XML::COMMENT_NODE || repr->type() == Inkscape::XML::PI_NODE ) ) {
         sp_xmlview_content_set_repr(content, repr);
     } else {
         sp_xmlview_content_set_repr(content, NULL);
@@ -924,10 +912,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
 }
@@ -986,7 +975,7 @@ void on_tree_select_row_show_if_text(GtkCTree *tree, GtkCTreeNode *node,
 {
     Inkscape::XML::Node *repr = sp_xmlview_tree_node_get_repr(SP_XMLVIEW_TREE(tree), node);
 
-    if ( repr->type() == Inkscape::XML::TEXT_NODE || repr->type() == Inkscape::XML::COMMENT_NODE ) {
+    if ( repr->type() == Inkscape::XML::TEXT_NODE || repr->type() == Inkscape::XML::COMMENT_NODE || repr->type() == Inkscape::XML::PI_NODE ) {
         gtk_widget_show(GTK_WIDGET(data));
     } else {
         gtk_widget_hide(GTK_WIDGET(data));
@@ -1157,7 +1146,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;
@@ -1304,7 +1293,16 @@ void on_clicked_get_editable_text(GtkWidget */*widget*/, gpointer data)
     dest->text = gtk_editable_get_chars(dest->editable, 0, -1);
 }
 
-
+gboolean
+quit_on_esc (GtkWidget *w, GdkEventKey *event, GObject */*tbl*/)
+{
+    switch (get_group0_keyval (event)) {
+        case GDK_Escape: // defocus
+            gtk_widget_destroy(w);
+            return TRUE;
+    }
+    return FALSE;
+}
 
 void cmd_new_element_node(GtkObject */*object*/, gpointer /*data*/)
 {
@@ -1321,6 +1319,7 @@ void cmd_new_element_node(GtkObject */*object*/, gpointer /*data*/)
     gtk_window_set_transient_for(GTK_WINDOW(window), GTK_WINDOW(dlg));
     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
     gtk_signal_connect(GTK_OBJECT(window), "destroy", gtk_main_quit, NULL);
+    gtk_signal_connect(GTK_OBJECT(window), "key-press-event", G_CALLBACK(quit_on_esc), window);
 
     vbox = gtk_vbox_new(FALSE, 4);
     gtk_container_add(GTK_CONTAINER(window), vbox);
@@ -1337,6 +1336,8 @@ void cmd_new_element_node(GtkObject */*object*/, gpointer /*data*/)
     gtk_box_pack_start(GTK_BOX(vbox), bbox, FALSE, TRUE, 0);
 
     cancel = gtk_button_new_with_label(_("Cancel"));
+    GTK_WIDGET_SET_FLAGS( GTK_WIDGET(cancel),
+                           GTK_CAN_DEFAULT );
     gtk_signal_connect_object( GTK_OBJECT(cancel), "clicked",
                                 G_CALLBACK(gtk_widget_destroy),
                                 GTK_OBJECT(window) );
@@ -1365,12 +1366,11 @@ void cmd_new_element_node(GtkObject */*object*/, gpointer /*data*/)
 
     gtk_main();
 
-    g_assert(selected_repr != NULL);
-
-    if (name.text) {
+    if (selected_repr != NULL && name.text) {
         Inkscape::XML::Document *xml_doc = sp_document_repr_doc(current_document);
         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);
@@ -1463,11 +1463,7 @@ void cmd_set_attr(GtkObject */*object*/, gpointer /*data*/)
     gchar *value = gtk_text_buffer_get_text( gtk_text_view_get_buffer(attr_value),
                                        &start, &end, TRUE );
 
-    if (!sp_repr_set_attr(selected_repr, name, value)) {
-        gchar *message = g_strdup_printf(_("Cannot set <b>%s</b>: Another element with value <b>%s</b> already exists!"), name, value);
-        _message_stack->flash(Inkscape::WARNING_MESSAGE, message);
-        g_free(message);
-    }
+    selected_repr->setAttribute(name, value, false);
 
     g_free(name);
     g_free(value);
@@ -1549,7 +1545,7 @@ 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);