[latexila/wip/templates-revamp: 2/2] LatexilaTemplatesDialogs: dialog to create a new template (wip)



commit e9e8b4ad64d84f3e891e5fd6fc20086dfa82e763
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Sat Apr 18 15:07:24 2015 +0200

    LatexilaTemplatesDialogs: dialog to create a new template (wip)

 src/liblatexila/latexila-templates-dialogs.c  |  106 +++++++++++++++++++++++++
 src/liblatexila/latexila-templates-dialogs.h  |    5 +-
 src/liblatexila/latexila-templates-personal.c |   29 +++++++
 src/liblatexila/latexila-templates-personal.h |    5 +
 src/templates_dialogs.vala                    |   49 -----------
 5 files changed, 144 insertions(+), 50 deletions(-)
---
diff --git a/src/liblatexila/latexila-templates-dialogs.c b/src/liblatexila/latexila-templates-dialogs.c
index 062a684..e51968f 100644
--- a/src/liblatexila/latexila-templates-dialogs.c
+++ b/src/liblatexila/latexila-templates-dialogs.c
@@ -201,3 +201,109 @@ latexila_templates_dialogs_open (GtkWindow *parent_window)
   gtk_widget_destroy (GTK_WIDGET (dialog));
   return contents;
 }
+
+/**
+ * latexila_templates_dialogs_create_template:
+ * @parent_window: transient parent window of the dialog.
+ * @template_contents: the template's contents
+ *
+ * Runs a #GtkDialog to create a new template. The template's contents is given.
+ * The #GtkDialog asks the template's name and icon.
+ */
+void
+latexila_templates_dialogs_create_template (GtkWindow   *parent_window,
+                                            const gchar *template_contents)
+{
+  GtkDialog *dialog;
+  GtkBox *content_area;
+  GtkEntry *entry;
+  GtkWidget *component;
+  LatexilaTemplatesDefault *default_store;
+  GtkTreeView *default_view;
+  GtkWidget *scrolled_window;
+
+  dialog = g_object_new (GTK_TYPE_DIALOG,
+                         "use-header-bar", TRUE,
+                         "title", _("New Template..."),
+                         "destroy-with-parent", TRUE,
+                         "transient-for", parent_window,
+                         NULL);
+
+  gtk_dialog_add_buttons (dialog,
+                          _("_Cancel"), GTK_RESPONSE_CANCEL,
+                          _("Crea_te"), GTK_RESPONSE_OK,
+                          NULL);
+
+  gtk_dialog_set_default_response (dialog, GTK_RESPONSE_OK);
+
+  content_area = GTK_BOX (gtk_dialog_get_content_area (dialog));
+
+  /* FIXME needed? */
+  /*gtk_box_set_homogeneous (content_area, FALSE);*/
+
+  /* Name */
+  entry = GTK_ENTRY (gtk_entry_new ());
+  gtk_widget_set_hexpand (GTK_WIDGET (entry), TRUE);
+  component = latexila_utils_get_dialog_component (_("Name of the new template"),
+                                                   GTK_WIDGET (entry));
+  gtk_box_pack_start (content_area, component, FALSE, TRUE, 0);
+
+  /* Icon.
+   * Take the default store because it contains all the icons.
+   */
+  default_store = latexila_templates_default_get_instance ();
+  default_view = latexila_templates_get_view (GTK_LIST_STORE (default_store));
+
+  scrolled_window = gtk_scrolled_window_new (NULL, NULL);
+  gtk_widget_set_size_request (scrolled_window, 250, 200);
+  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_window),
+                                       GTK_SHADOW_IN);
+
+  gtk_container_add (GTK_CONTAINER (scrolled_window),
+                     GTK_WIDGET (default_view));
+
+  component = latexila_utils_get_dialog_component (_("Choose an icon"), scrolled_window);
+  gtk_box_pack_start (content_area, component, TRUE, TRUE, 0);
+
+  gtk_widget_show_all (GTK_WIDGET (content_area));
+
+  while (gtk_dialog_run (dialog) == GTK_RESPONSE_OK)
+    {
+      GtkTreeSelection *selection;
+      GList *selected_rows;
+      GtkTreePath *path;
+      GtkTreeIter iter;
+      gchar *config_icon_name = NULL;
+
+      /* If no name specified. */
+      if (gtk_entry_get_text_length (entry) == 0)
+        continue;
+
+      selection = gtk_tree_view_get_selection (default_view);
+
+      /* If no icons selected. */
+      if (gtk_tree_selection_count_selected_rows (selection) == 0)
+        continue;
+
+      /* Get config icon name. */
+      selected_rows = gtk_tree_selection_get_selected_rows (selection, NULL);
+      g_assert (g_list_length (selected_rows) == 1);
+
+      path = selected_rows->data;
+
+      if (!gtk_tree_model_get_iter (GTK_TREE_MODEL (default_store), &iter, path))
+        {
+          g_warning ("Create template dialog: invalid path");
+          break;
+        }
+
+      gtk_tree_model_get (GTK_TREE_MODEL (default_store), &iter,
+                          LATEXILA_TEMPLATES_COLUMN_CONFIG_ICON_NAME, &config_icon_name,
+                          -1);
+
+      g_list_free_full (selected_rows, (GDestroyNotify) gtk_tree_path_free);
+      g_free (config_icon_name);
+    }
+
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+}
diff --git a/src/liblatexila/latexila-templates-dialogs.h b/src/liblatexila/latexila-templates-dialogs.h
index 950b218..9dc0102 100644
--- a/src/liblatexila/latexila-templates-dialogs.h
+++ b/src/liblatexila/latexila-templates-dialogs.h
@@ -24,7 +24,10 @@
 
 G_BEGIN_DECLS
 
-gchar *         latexila_templates_dialogs_open             (GtkWindow *parent_window);
+gchar *         latexila_templates_dialogs_open               (GtkWindow *parent_window);
+
+void            latexila_templates_dialogs_create_template    (GtkWindow   *parent_window,
+                                                               const gchar *template_contents);
 
 G_END_DECLS
 
diff --git a/src/liblatexila/latexila-templates-personal.c b/src/liblatexila/latexila-templates-personal.c
index c168c41..17a2d49 100644
--- a/src/liblatexila/latexila-templates-personal.c
+++ b/src/liblatexila/latexila-templates-personal.c
@@ -216,6 +216,7 @@ latexila_templates_personal_get_contents (LatexilaTemplatesPersonal *templates,
   GError *error = NULL;
 
   g_return_val_if_fail (LATEXILA_IS_TEMPLATES_PERSONAL (templates), NULL);
+  g_return_val_if_fail (path != NULL, NULL);
 
   gtk_tree_model_get_iter (GTK_TREE_MODEL (templates),
                            &iter,
@@ -239,3 +240,31 @@ latexila_templates_personal_get_contents (LatexilaTemplatesPersonal *templates,
   g_object_unref (file);
   return contents;
 }
+
+/**
+ * latexila_templates_personal_create:
+ * @templates: the #LatexilaTemplatesPersonal instance.
+ * @name: the template's name.
+ * @config_icon_name: the icon name that will be stored in the config file.
+ * @contents: the template's contents.
+ *
+ * Creates a new personal template. The new template is added at the end of the
+ * list.
+ */
+void
+latexila_templates_personal_create (LatexilaTemplatesPersonal *templates,
+                                    const gchar               *name,
+                                    const gchar               *config_icon_name,
+                                    const gchar               *contents)
+{
+  gint template_num;
+  GFile *template_file;
+
+  g_return_if_fail (LATEXILA_IS_TEMPLATES_PERSONAL (templates));
+  g_return_if_fail (name != NULL && name[0] != '\0');
+  g_return_if_fail (config_icon_name != NULL && config_icon_name[0] != '\0');
+  g_return_if_fail (contents != NULL);
+
+  template_num = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (templates), NULL);
+  template_file = get_personal_template_file (template_num);
+}
diff --git a/src/liblatexila/latexila-templates-personal.h b/src/liblatexila/latexila-templates-personal.h
index 2bc0a51..566b488 100644
--- a/src/liblatexila/latexila-templates-personal.h
+++ b/src/liblatexila/latexila-templates-personal.h
@@ -33,6 +33,11 @@ LatexilaTemplatesPersonal *
 gchar *       latexila_templates_personal_get_contents          (LatexilaTemplatesPersonal *templates,
                                                                  GtkTreePath               *path);
 
+void          latexila_templates_personal_create                (LatexilaTemplatesPersonal *templates,
+                                                                 const gchar               *name,
+                                                                 const gchar               *config_icon_name,
+                                                                 const gchar               *contents);
+
 G_END_DECLS
 
 #endif /* __LATEXILA_TEMPLATES_PERSONAL_H__ */
diff --git a/src/templates_dialogs.vala b/src/templates_dialogs.vala
index 766ef07..d63b673 100644
--- a/src/templates_dialogs.vala
+++ b/src/templates_dialogs.vala
@@ -23,41 +23,7 @@ public class CreateTemplateDialog : Dialog
 {
     public CreateTemplateDialog (MainWindow parent)
     {
-        Object (use_header_bar: 1);
         return_val_if_fail (parent.active_tab != null, null);
-
-        title = _("New Template...");
-        set_transient_for (parent);
-        destroy_with_parent = true;
-        add_button (_("_Cancel"), ResponseType.CANCEL);
-        add_button (_("Crea_te"), ResponseType.OK);
-        set_default_response (ResponseType.OK);
-
-        Box content_area = get_content_area () as Box;
-        content_area.homogeneous = false;
-
-        /* name */
-        Entry entry = new Entry ();
-        entry.hexpand = true;
-        Widget component = Latexila.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.
-        TreeView templates_list = templates.get_default_templates_list ();
-
-        ScrolledWindow scrollbar = Utils.add_scrollbar (templates_list);
-        scrollbar.set_shadow_type (ShadowType.IN);
-        scrollbar.set_size_request (250, 200);
-        component = Latexila.utils_get_dialog_component (_("Choose an icon"), scrollbar);
-        content_area.pack_start (component);
-
-        content_area.show_all ();
-
-        run_me (parent, entry, templates_list);
     }
 
     private void run_me (MainWindow parent, Entry entry, TreeView templates_list)
@@ -66,26 +32,11 @@ public class CreateTemplateDialog : Dialog
 
         while (run () == ResponseType.OK)
         {
-            // if no name specified
-            if (entry.text_length == 0)
-                continue;
-
-            TreeSelection select = templates_list.get_selection ();
-            List<TreePath> selected_items = select.get_selected_rows (null);
-
-            // 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;
         }


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