[latexila] Templates: create template dialog in a new class



commit 39bdac9a3626b8072575528a2c2fa5dcfc29f782
Author: SÃbastien Wilmet <swilmet src gnome org>
Date:   Sat Mar 10 04:36:59 2012 +0100

    Templates: create template dialog in a new class

 src/main_window.vala       |    4 ++-
 src/templates.vala         |   82 ++++++++-----------------------------------
 src/templates_dialogs.vala |   68 ++++++++++++++++++++++++++++++++++++
 3 files changed, 87 insertions(+), 67 deletions(-)
---
diff --git a/src/main_window.vala b/src/main_window.vala
index 5a021cf..0a7279f 100644
--- a/src/main_window.vala
+++ b/src/main_window.vala
@@ -1562,7 +1562,9 @@ public class MainWindow : Window
     public void on_create_template ()
     {
         return_if_fail (active_tab != null);
-        Templates.get_default ().show_dialog_create (this);
+
+        CreateTemplateDialog dialog = new CreateTemplateDialog (this);
+        dialog.destroy ();
     }
 
     public void on_delete_template ()
diff --git a/src/templates.vala b/src/templates.vala
index aab73de..78a155b 100644
--- a/src/templates.vala
+++ b/src/templates.vala
@@ -324,71 +324,6 @@ public class Templates : GLib.Object
         dialog.destroy ();
     }
 
-    // Dialog: create a new template
-    public void show_dialog_create (MainWindow parent)
-    {
-        return_if_fail (parent.active_tab != null);
-
-        Dialog dialog = new Dialog.with_buttons (_("New Template..."), parent, 0,
-            Stock.OK, ResponseType.ACCEPT,
-            Stock.CANCEL, ResponseType.REJECT,
-            null);
-
-        dialog.set_default_size (420, 370);
-
-        Box content_area = dialog.get_content_area () as Box;
-        content_area.homogeneous = false;
-
-        /* name */
-        Entry entry = new Entry ();
-        Widget component = Utils.get_dialog_component (_("Name of the new template"),
-            entry);
-        content_area.pack_start (component, false);
-
-        /* icon */
-        // we take the default store because it contains all the icons
-        IconView icon_view = create_icon_view (_default_store);
-        Widget scrollbar = Utils.add_scrollbar (icon_view);
-        component = Utils.get_dialog_component (_("Choose an icon"), scrollbar);
-        content_area.pack_start (component);
-
-        content_area.show_all ();
-
-        while (dialog.run () == ResponseType.ACCEPT)
-        {
-            // if no name specified
-            if (entry.text_length == 0)
-                continue;
-
-            List<TreePath> selected_items = icon_view.get_selected_items ();
-
-            // if no icon selected
-            if (selected_items.length () == 0)
-                continue;
-
-            _nb_personal_templates++;
-
-            // get the contents
-            TextIter start, end;
-            parent.active_document.get_bounds (out start, out end);
-            string contents = parent.active_document.get_text (start, end, false);
-
-            // get the icon id
-            TreeModel model = (TreeModel) _default_store;
-            TreePath path = selected_items.nth_data (0);
-            TreeIter iter;
-            string icon_id;
-            model.get_iter (out iter, path);
-            model.get (iter, TemplateColumn.ICON_ID, out icon_id, -1);
-
-            add_template_from_string (_personal_store, entry.text, icon_id, contents);
-            add_personal_template (contents);
-            break;
-        }
-
-        dialog.destroy ();
-    }
-
     public IconView create_icon_view_default_templates ()
     {
         return create_icon_view (_default_store);
@@ -459,8 +394,11 @@ public class Templates : GLib.Object
         _nb_personal_templates--;
     }
 
-    private void add_personal_template (string contents)
+    public void create_personal_template (string name, string icon_id, string contents)
     {
+        add_template_from_string (_personal_store, name, icon_id, contents);
+        _nb_personal_templates++;
+
         save_rc_file ();
 
         File file = get_personal_template_file (_nb_personal_templates - 1);
@@ -481,6 +419,18 @@ public class Templates : GLib.Object
         }
     }
 
+    public string get_icon_id (TreePath default_template_path)
+    {
+        TreeModel model = (TreeModel) _default_store;
+        TreeIter iter;
+        model.get_iter (out iter, default_template_path);
+
+        string icon_id;
+        model.get (iter, TemplateColumn.ICON_ID, out icon_id);
+
+        return icon_id;
+    }
+
     public void save_rc_file ()
     {
         if (_nb_personal_templates == 0)
diff --git a/src/templates_dialogs.vala b/src/templates_dialogs.vala
index 3787129..686523a 100644
--- a/src/templates_dialogs.vala
+++ b/src/templates_dialogs.vala
@@ -19,6 +19,74 @@
 
 using Gtk;
 
+public class CreateTemplateDialog : Dialog
+{
+    public CreateTemplateDialog (MainWindow parent)
+    {
+        return_if_fail (parent.active_tab != null);
+
+        title = _("New Template...");
+        set_transient_for (parent);
+        add_button (Stock.OK, ResponseType.ACCEPT);
+        add_button (Stock.CANCEL, ResponseType.REJECT);
+
+        set_default_size (420, 370);
+
+        Box content_area = get_content_area () as Box;
+        content_area.homogeneous = false;
+
+        /* name */
+        Entry entry = new Entry ();
+        Widget component = Utils.get_dialog_component (_("Name of the new template"),
+            entry);
+        content_area.pack_start (component, false);
+
+        /* icon */
+        Templates templates = Templates.get_default ();
+
+        // Take the default store because it contains all the icons.
+        IconView icon_view = templates.create_icon_view_default_templates ();
+
+        Widget scrollbar = Utils.add_scrollbar (icon_view);
+        component = Utils.get_dialog_component (_("Choose an icon"), scrollbar);
+        content_area.pack_start (component);
+
+        content_area.show_all ();
+
+        run_me (parent, entry, icon_view);
+    }
+
+    private void run_me (MainWindow parent, Entry entry, IconView icon_view)
+    {
+        Templates templates = Templates.get_default ();
+
+        while (run () == ResponseType.ACCEPT)
+        {
+            // if no name specified
+            if (entry.text_length == 0)
+                continue;
+
+            List<TreePath> selected_items = icon_view.get_selected_items ();
+
+            // if no icon selected
+            if (selected_items.length () == 0)
+                continue;
+
+            // get the contents
+            TextIter start, end;
+            parent.active_document.get_bounds (out start, out end);
+            string contents = parent.active_document.get_text (start, end, false);
+
+            // get the icon id
+            TreePath path = selected_items.nth_data (0);
+            string icon_id = templates.get_icon_id (path);
+
+            templates.create_personal_template (entry.text, icon_id, contents);
+            break;
+        }
+    }
+}
+
 public class DeleteTemplateDialog : Dialog
 {
     public DeleteTemplateDialog (MainWindow parent)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]