[gnome-latex: 136/205] New file: select a template



commit 4f1fc206a5a809f59dc0c41bb26399a45a7e9d11
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Mon Dec 14 01:54:11 2009 +0100

    New file: select a template

 TODO               |   4 +-
 src/CMakeLists.txt |   3 +-
 src/callbacks.c    |  24 ++------
 src/callbacks.h    |   3 +-
 src/main.c         |   4 ++
 src/templates.c    | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/templates.h    |  34 +++++++++++
 src/ui.c           |   1 +
 8 files changed, 226 insertions(+), 21 deletions(-)
---
diff --git a/TODO b/TODO
index 5838909..0658fe1 100644
--- a/TODO
+++ b/TODO
@@ -2,7 +2,7 @@ TODO LaTeXila
 
 [-] Templates
        x create a few default templates
-       - on the action "new file", possibility to select a template
+       x on the action "new file", possibility to select a template
        - possibility to create new templates
        - possibility to delete templates (not the defaults)
 
@@ -11,3 +11,5 @@ TODO LaTeXila
 [-] toolbars: possibility to put the edit toolbar on the right of the first toolbar instead of bottom
 
 [-] comment/uncomment selected lines
+
+[-] replace the notebook in the side pane by a combo box with a close button
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 3d3f22a..dbcd2bd 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -7,7 +7,8 @@ SET(latexila_src main.c                 main.h
                 prefs.c                prefs.h
                 ui.c                   ui.h
                 tool_menu_action.c     tool_menu_action.h
-                file_browser.c         file_browser.h)
+                file_browser.c         file_browser.h
+                templates.c            templates.h)
 ADD_EXECUTABLE(latexila ${latexila_src})
 TARGET_LINK_LIBRARIES(latexila ${GTK2_LIBRARIES})
 
diff --git a/src/callbacks.c b/src/callbacks.c
index c97acdf..c9ab996 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -40,8 +40,6 @@
 #include "prefs.h"
 #include "file_browser.h"
 
-static void create_document_in_new_tab (const gchar *path, const gchar *text,
-               const gchar *title);
 static void close_document (gint index);
 static void save_as_dialog (void);
 static void file_save (void);
@@ -59,16 +57,6 @@ static void set_entry_background (GtkWidget *entry, gboolean error);
 
 static gboolean save_list_opened_docs = FALSE;
 
-void
-cb_new (void)
-{
-       char *default_text = "\\documentclass[a4paper,11pt]{article}\n"
-               "\\begin{document}\n"
-               "\\end{document}";
-
-       create_document_in_new_tab (NULL, default_text, _("New document"));
-}
-
 void
 cb_open (void)
 {
@@ -1002,12 +990,7 @@ change_font_source_view (void)
        }
 }
 
-
-/******************************************************************************
- * local functions
- *****************************************************************************/
-
-static void
+void
 create_document_in_new_tab (const gchar *path, const gchar *text,
                const gchar *title)
 {
@@ -1131,6 +1114,11 @@ create_document_in_new_tab (const gchar *path, const gchar *text,
        update_cursor_position_statusbar ();
 }
 
+
+/******************************************************************************
+ * local functions
+ *****************************************************************************/
+
 static void
 close_document (gint index)
 {
diff --git a/src/callbacks.h b/src/callbacks.h
index 43036eb..6f50722 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -20,7 +20,6 @@
 #ifndef CALLBACKS_H
 #define CALLBACKS_H
 
-void cb_new (void);
 void cb_open (void);
 void cb_save (void);
 void cb_save_as (void);
@@ -80,5 +79,7 @@ void cb_show_edit_toolbar (GtkToggleAction *toggle_action, gpointer user_data);
 void open_new_document_without_uri (const gchar *filename);
 void open_new_document (const gchar *filename, const gchar *uri);
 void change_font_source_view (void);
+void create_document_in_new_tab (const gchar *path, const gchar *text,
+               const gchar *title);
 
 #endif /* CALLBACKS_H */
diff --git a/src/main.c b/src/main.c
index 10510b0..c40b2d2 100644
--- a/src/main.c
+++ b/src/main.c
@@ -34,6 +34,7 @@
 #include "print.h"
 #include "prefs.h"
 #include "ui.h"
+#include "templates.h"
 
 static gboolean option_version (const gchar *option_name, const gchar *value,
                gpointer data, GError **error);
@@ -529,6 +530,9 @@ main (int argc, char *argv[])
        if (option_new_document)
                cb_new ();
 
+       /* templates */
+       init_templates ();
+
 
        gtk_main ();
 
diff --git a/src/templates.c b/src/templates.c
new file mode 100644
index 0000000..47ed465
--- /dev/null
+++ b/src/templates.c
@@ -0,0 +1,174 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright © 2009 Sébastien Wilmet
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <locale.h>
+#include <libintl.h>
+#include <gtk/gtk.h>
+#include <gtksourceview/gtksourceview.h>
+
+#include "main.h"
+#include "config.h"
+#include "print.h"
+#include "callbacks.h"
+#include "templates.h"
+
+static void add_template_from_string (GtkListStore *store, gchar *name,
+               gchar *icon, gchar *contents);
+static void add_template_from_file (GtkListStore *store, gchar *name,
+               gchar *icon, gchar *filename);
+
+static GtkListStore *store;
+
+void
+cb_new (void)
+{
+       GtkWidget *dialog = gtk_dialog_new_with_buttons (_("New File..."),
+                       latexila.main_window,
+                       GTK_DIALOG_NO_SEPARATOR,
+                       GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
+                       GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
+                       NULL);
+
+       gtk_window_set_default_size (GTK_WINDOW (dialog), 400, 200);
+       
+       GtkWidget *content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
+
+       GtkWidget *icon_view = gtk_icon_view_new_with_model (
+                       GTK_TREE_MODEL (store));
+       gtk_icon_view_set_selection_mode (GTK_ICON_VIEW (icon_view),
+                       GTK_SELECTION_SINGLE);
+       gtk_icon_view_set_columns (GTK_ICON_VIEW (icon_view), -1);
+       gtk_icon_view_set_text_column (GTK_ICON_VIEW (icon_view),
+                       COLUMN_TEMPLATE_NAME);
+       gtk_icon_view_set_pixbuf_column (GTK_ICON_VIEW (icon_view),
+                       COLUMN_TEMPLATE_PIXBUF);
+
+       // with a scrollbar (without that there is a problem for resizing the
+       // dialog, we can make it bigger but not smaller...)
+       GtkWidget *scrollbar = gtk_scrolled_window_new (NULL, NULL);
+       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrollbar),
+                       GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
+       gtk_container_add (GTK_CONTAINER (scrollbar), icon_view);
+
+       gtk_box_pack_start (GTK_BOX (content_area), scrollbar, TRUE, TRUE, 0);
+       gtk_widget_show_all (content_area);
+
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+       {
+               GList *selected_items = gtk_icon_view_get_selected_items (
+                               GTK_ICON_VIEW (icon_view));
+               GtkTreePath *path = g_list_nth_data (selected_items, 0);
+               GtkTreeModel *model = GTK_TREE_MODEL (store);
+               GtkTreeIter iter;
+
+               gchar *contents = NULL;
+               if (path != NULL && gtk_tree_model_get_iter (model, &iter, path))
+               {
+                       gtk_tree_model_get (model, &iter,
+                                       COLUMN_TEMPLATE_CONTENTS, &contents,
+                                       -1);
+               }
+               else
+                       contents = g_strdup ("");
+
+               create_document_in_new_tab (NULL, contents, _("New document"));
+
+               g_free (contents);
+
+               // free the GList
+               g_list_foreach (selected_items, (GFunc) gtk_tree_path_free, NULL);
+               g_list_free (selected_items);
+       }
+
+       gtk_widget_destroy (dialog);
+}
+
+void
+init_templates (void)
+{
+       store = gtk_list_store_new (N_COLUMNS_TEMPLATE,
+                       GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING);
+
+       add_template_from_string (store, _("Empty"),
+                       DATA_DIR "/images/templates/empty.png", "");
+       add_template_from_file (store, _("Article"),
+                       DATA_DIR "/images/templates/article.png", _("article-en.tex"));
+       add_template_from_file (store, _("Report"),
+                       DATA_DIR "/images/templates/report.png", _("report-en.tex"));
+       add_template_from_file (store, _("Book"),
+                       DATA_DIR "/images/templates/book.png", _("book-en.tex"));
+       add_template_from_file (store, _("Letter"),
+                       DATA_DIR "/images/templates/letter.png", _("letter-en.tex"));
+
+}
+
+static void
+add_template_from_string (GtkListStore *store, gchar *name, gchar *icon,
+               gchar *contents)
+{
+       if (contents == NULL)
+               return;
+
+       GError *error = NULL;
+       GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file (icon, &error);
+       if (error != NULL)
+       {
+               print_warning ("impossible to load the icon of the template: %s",
+                               error->message);
+               g_error_free (error);
+               return;
+       }
+
+       GtkTreeIter iter;
+       gtk_list_store_append (store, &iter);
+       gtk_list_store_set (store, &iter,
+                       COLUMN_TEMPLATE_PIXBUF, pixbuf,
+                       COLUMN_TEMPLATE_NAME, name,
+                       COLUMN_TEMPLATE_CONTENTS, contents,
+                       -1);
+
+       g_object_unref (pixbuf);
+}
+
+static void
+add_template_from_file (GtkListStore *store, gchar *name, gchar *icon,
+               gchar *filename)
+{
+       gchar *contents = NULL;
+       GError *error = NULL;
+       gchar *path = g_strdup_printf (DATA_DIR "/templates/%s", filename);
+       g_file_get_contents (path, &contents, NULL, &error);
+       g_free (path);
+
+       if (error != NULL)
+       {
+               print_warning ("impossible to load the template \"%s\": %s", name,
+                               error->message);
+               g_error_free (error);
+               return;
+       }
+
+       gchar *text_utf8 = g_locale_to_utf8 (contents, -1, NULL, NULL, NULL);
+       add_template_from_string (store, name, icon, text_utf8);
+
+       g_free (text_utf8);
+       g_free (contents);
+}
diff --git a/src/templates.h b/src/templates.h
new file mode 100644
index 0000000..59a7027
--- /dev/null
+++ b/src/templates.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of LaTeXila.
+ *
+ * Copyright © 2009 Sébastien Wilmet
+ *
+ * LaTeXila is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * LaTeXila is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef TEMPLATES_H
+#define TEMPLATES_H
+
+void cb_new (void);
+void init_templates (void);
+
+enum templates
+{
+       COLUMN_TEMPLATE_PIXBUF,
+       COLUMN_TEMPLATE_NAME,
+       COLUMN_TEMPLATE_CONTENTS,
+       N_COLUMNS_TEMPLATE
+};
+
+#endif /* TEMPLATES_H */
diff --git a/src/ui.c b/src/ui.c
index d403517..cb29d6f 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -31,6 +31,7 @@
 #include "cb_latex.h"
 #include "tool_menu_action.h"
 #include "prefs.h"
+#include "templates.h"
 
 static void register_my_stock_icons (void);
 


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