[gnome-latex: 10/205] save and save as



commit 5394e3e2bd14acd9b664f947d1849212fd51e930
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Mon Aug 3 14:41:05 2009 +0200

    save and save as

 TODO            |  14 ++++----
 src/callbacks.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/callbacks.h |   3 ++
 src/main.c      |   8 +++--
 src/main.h      |  13 +++++---
 5 files changed, 127 insertions(+), 13 deletions(-)
---
diff --git a/TODO b/TODO
index 5b83634..299dd21 100644
--- a/TODO
+++ b/TODO
@@ -4,14 +4,16 @@ Jul 31, 2009 to Aug 7, 2009
 
 [x] about dialog
 
+[-] files
+       x open
+       x save 
+       x save as
+       - close
+       - new
+       - tabs
+
 [-] GtkSourceView
        - syntaxic color
        - show/hide line numbers
        - undo/redo
 
-[-] files
-       x open
-       - new
-       - save 
-       - save as
-       - tabs
diff --git a/src/callbacks.c b/src/callbacks.c
index 4e2f11b..e9a8807 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -9,6 +9,8 @@
 #include "error.h"
 
 static void open_file (const gchar *file_name, GtkTextView *text_view);
+static void save_as_dialog (widgets_t *widgets);
+static void file_save (void);
 
 void
 cb_open (GtkAction *action, widgets_t *widgets)
@@ -41,6 +43,44 @@ cb_open (GtkAction *action, widgets_t *widgets)
        gtk_widget_destroy (dialog);
 }
 
+void
+cb_save (GtkAction *action, widgets_t *widgets)
+{
+       if (docs.active)
+       {
+               if (! docs.active->saved)
+               {
+                       if (docs.active->path == NULL)
+                               save_as_dialog (widgets);
+
+                       file_save();
+               }
+       }
+
+       else
+               print_warning ("no document opened");
+}
+
+void
+cb_save_as (GtkAction *action, widgets_t *widgets)
+{
+       if (docs.active)
+       {
+               document_t doc_backup = *docs.active;
+
+               docs.active->path = NULL;
+               docs.active->saved = FALSE;
+               cb_save (action, widgets);
+
+               // if the user click on cancel
+               if (! docs.active->saved)
+                       (*docs.active) = doc_backup;
+       }
+
+       else
+               print_warning ("no document opened");
+}
+
 void
 cb_about_dialog (GtkAction *action, widgets_t *widgets)
 {
@@ -81,9 +121,17 @@ cb_quit (void)
        gtk_main_quit ();
 }
 
+void
+cb_text_changed (GtkWidget *widget, gpointer user_data)
+{
+       if (docs.active)
+               docs.active->saved = FALSE;
+}
+
 static void
 open_file (const gchar *file_name, GtkTextView *text_view)
 {
+       // the arguments must not be NULL
        g_return_if_fail (file_name && text_view);
 
        gchar *contents = NULL;
@@ -100,3 +148,57 @@ open_file (const gchar *file_name, GtkTextView *text_view)
        else
                print_warning ("impossible to open the file \"%s\"", file_name);
 }
+
+static void
+save_as_dialog (widgets_t *widgets)
+{
+       GtkWidget *dialog = gtk_file_chooser_dialog_new (
+                       _("Save File"),
+                       GTK_WINDOW (widgets->window),
+                       GTK_FILE_CHOOSER_ACTION_SAVE,
+                       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
+                       NULL
+       );
+
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+               docs.active->path = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+
+       gtk_widget_destroy (dialog);
+}
+
+static void
+file_save (void)
+{
+       if (docs.active->path != NULL)
+       {
+               print_info ("save current buffer to \"%s\"", docs.active->path);
+
+               FILE* file = fopen (docs.active->path, "w");
+               if (file != NULL)
+               {
+                       GtkTextBuffer *text_buffer = gtk_text_view_get_buffer (docs.active->text_view);
+                       GtkTextIter start;
+                       GtkTextIter end;
+                       gtk_text_buffer_get_bounds (text_buffer, &start, &end);
+                       gchar *contents = gtk_text_buffer_get_text (text_buffer, &start, &end, FALSE);
+                       gchar *locale = g_locale_from_utf8 (contents, -1, NULL, NULL, NULL);
+                       
+                       // write the contents into the file
+                       fprintf (file, "%s\n", locale);
+
+                       fclose (file);
+                       g_free (contents);
+                       g_free (locale);
+
+                       docs.active->saved = TRUE;
+               }
+
+               // error with fopen
+               else
+               {
+                       print_warning ("impossible to save the file \"%s\"",
+                                       docs.active->path);
+               }
+       }
+}
diff --git a/src/callbacks.h b/src/callbacks.h
index 57204ef..f116ef3 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -2,7 +2,10 @@
 #define CALLBACKS_H
 
 void cb_open (GtkAction *action, widgets_t *widgets);
+void cb_save (GtkAction *action, widgets_t *widgets);
+void cb_save_as (GtkAction *action, widgets_t *widgets);
 void cb_about_dialog (GtkAction *action, widgets_t *widgets);
 void cb_quit (void);
+void cb_text_changed (GtkWidget *widget, gpointer user_data);
 
 #endif /* CALLBACKS_H */
diff --git a/src/main.c b/src/main.c
index e6e01fc..16a8fbb 100644
--- a/src/main.c
+++ b/src/main.c
@@ -48,9 +48,9 @@ main (int argc, char *argv[])
                {"FileOpen", GTK_STOCK_OPEN, _("Open..."), "<Control>O",
                        _("Open a file"), G_CALLBACK (cb_open)},
                {"FileSave", GTK_STOCK_SAVE, _("Save..."), "<Control>S",
-                       _("Save the current file"), NULL},
+                       _("Save the current file"), G_CALLBACK (cb_save)},
                {"FileSaveAs", GTK_STOCK_SAVE_AS, _("Save as..."), "<Shift><Control>S",
-                       _("Save the current file with a different name"), NULL},
+                       _("Save the current file with a different name"), G_CALLBACK (cb_save_as)},
                {"FileQuit", GTK_STOCK_QUIT, _("Quit"), "<Control>Q",
                        _("Quit the program"), G_CALLBACK (cb_quit)},
                
@@ -124,6 +124,10 @@ main (int argc, char *argv[])
                "\\end{document}";
        gtk_text_buffer_set_text (source_buffer, source_default_text, -1);
 
+       // when the text is modified, the "saved" field of document_t must be FALSE
+       g_signal_connect (G_OBJECT (source_buffer), "changed",
+                       G_CALLBACK (cb_text_changed), NULL);
+
        // with a scrollbar
        GtkWidget *sw = gtk_scrolled_window_new (NULL, NULL);
        gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
diff --git a/src/main.h b/src/main.h
index e1cff00..84991f2 100644
--- a/src/main.h
+++ b/src/main.h
@@ -6,6 +6,13 @@
 #define PROGRAM_NAME "LaTeXila"
 #define PROGRAM_VERSION "0.0.1"
 
+// used for data callbacks
+typedef struct
+{
+       GtkWidget *window;
+} widgets_t;
+
+// each document opened is represented by a document_t structure
 typedef struct
 {
        gchar *path;
@@ -19,11 +26,7 @@ typedef struct
        document_t *active;
 } docs_t;
 
-typedef struct
-{
-       GtkWidget *window;
-} widgets_t;
-
+// all the documents are accessible by the docs variable
 extern docs_t docs;
 
 #endif /* MAIN_H */


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