[gnome-latex: 9/205] Open file (without tabs) each document will be represented by a document_t structure all the documen



commit 9cd973b7c3805cb89cc171f508ccf641408a29df
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Sun Aug 2 22:54:05 2009 +0200

    Open file (without tabs)
    each document will be represented by a document_t structure
    all the documents will be accessible with the extern variable docs

 TODO            |  8 +++++---
 src/callbacks.c | 36 +++++++++++++++++++++++++++++++-----
 src/callbacks.h |  4 ++--
 src/main.c      | 16 +++++++++++++++-
 src/main.h      | 20 ++++++++++++++++++++
 5 files changed, 73 insertions(+), 11 deletions(-)
---
diff --git a/TODO b/TODO
index 394cf05..5b83634 100644
--- a/TODO
+++ b/TODO
@@ -9,7 +9,9 @@ Jul 31, 2009 to Aug 7, 2009
        - show/hide line numbers
        - undo/redo
 
-[-] files (with tabs)
+[-] files
+       x open
        - new
-       - open
-       - save and save as
+       - save 
+       - save as
+       - tabs
diff --git a/src/callbacks.c b/src/callbacks.c
index 1163512..4e2f11b 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -8,12 +8,14 @@
 #include "callbacks.h"
 #include "error.h"
 
+static void open_file (const gchar *file_name, GtkTextView *text_view);
+
 void
-cb_open (void)
+cb_open (GtkAction *action, widgets_t *widgets)
 {
        GtkWidget *dialog = gtk_file_chooser_dialog_new (
                        _("Open File"),
-                       NULL,
+                       GTK_WINDOW (widgets->window),
                        GTK_FILE_CHOOSER_ACTION_OPEN,
                        GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                        GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
@@ -25,10 +27,14 @@ cb_open (void)
                char *filename;
                filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
 
+               docs.active->path = g_strdup (filename);
+               docs.active->saved = TRUE;
+
                //TODO open the file in a new tab
-               //open_file (filename);
+               open_file (filename, docs.active->text_view);
                
                print_info ("Open file: \"%s\"", filename);
+
                g_free (filename);
        }
 
@@ -36,7 +42,7 @@ cb_open (void)
 }
 
 void
-cb_about_dialog (void)
+cb_about_dialog (GtkAction *action, widgets_t *widgets)
 {
        gchar *comments = _(PROGRAM_NAME " is a LaTeX development environment for the GNOME Desktop");
        gchar *copyright = "Copyright © 2009 Sébastien Wilmet";
@@ -54,7 +60,7 @@ cb_about_dialog (void)
        };
 
        gtk_show_about_dialog (
-                       NULL,
+                       GTK_WINDOW (widgets->window),
                        "program-name", PROGRAM_NAME,
                        "authors", authors,
                        "comments", comments,
@@ -74,3 +80,23 @@ cb_quit (void)
        print_info ("Bye bye");
        gtk_main_quit ();
 }
+
+static void
+open_file (const gchar *file_name, GtkTextView *text_view)
+{
+       g_return_if_fail (file_name && text_view);
+
+       gchar *contents = NULL;
+
+       if (g_file_get_contents (file_name, &contents, NULL, NULL))
+       {
+               GtkTextBuffer *text_buffer = gtk_text_view_get_buffer (text_view);
+               gchar *utf8 = g_locale_to_utf8 (contents, -1, NULL, NULL, NULL);
+               gtk_text_buffer_set_text (text_buffer, utf8, -1);
+               g_free (contents);
+               g_free (utf8);
+       }
+
+       else
+               print_warning ("impossible to open the file \"%s\"", file_name);
+}
diff --git a/src/callbacks.h b/src/callbacks.h
index a1e4573..57204ef 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -1,8 +1,8 @@
 #ifndef CALLBACKS_H
 #define CALLBACKS_H
 
-void cb_open (void);
-void cb_about_dialog (void);
+void cb_open (GtkAction *action, widgets_t *widgets);
+void cb_about_dialog (GtkAction *action, widgets_t *widgets);
 void cb_quit (void);
 
 #endif /* CALLBACKS_H */
diff --git a/src/main.c b/src/main.c
index 510a4d8..e6e01fc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -8,6 +8,8 @@
 #include "callbacks.h"
 #include "error.h"
 
+docs_t docs = {NULL, NULL};
+
 int
 main (int argc, char *argv[])
 {
@@ -75,8 +77,16 @@ main (int argc, char *argv[])
 
        guint nb_entries = G_N_ELEMENTS (entries);
 
+       // this structure contain the window and the widgets used in callbacks
+       // functions
+       // so the widgets must be initialized before the call of
+       // gtk_action_group_add_actions ()
+       widgets_t widgets;
+       widgets.window = window;
+
+       // create the action group and the ui manager
        GtkActionGroup *action_group = gtk_action_group_new ("menuActionGroup");
-       gtk_action_group_add_actions (action_group, entries, nb_entries, NULL);
+       gtk_action_group_add_actions (action_group, entries, nb_entries, &widgets);
        GtkUIManager *ui_manager = gtk_ui_manager_new ();
        gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
 
@@ -120,6 +130,10 @@ main (int argc, char *argv[])
                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
        gtk_paned_pack1 (GTK_PANED (vpaned), sw, TRUE, TRUE);
        gtk_container_add (GTK_CONTAINER (sw), source_view);
+
+       // initialize the extern structure docs
+       document_t first_document = {NULL, FALSE, GTK_TEXT_VIEW (source_view)};
+       docs.active = &first_document;
        
        /* log zone */
        GtkWidget *log_view = gtk_text_view_new ();
diff --git a/src/main.h b/src/main.h
index 2c72579..e1cff00 100644
--- a/src/main.h
+++ b/src/main.h
@@ -6,4 +6,24 @@
 #define PROGRAM_NAME "LaTeXila"
 #define PROGRAM_VERSION "0.0.1"
 
+typedef struct
+{
+       gchar *path;
+       gboolean saved;
+       GtkTextView *text_view;
+} document_t;
+
+typedef struct
+{
+       GList *all;
+       document_t *active;
+} docs_t;
+
+typedef struct
+{
+       GtkWidget *window;
+} widgets_t;
+
+extern docs_t docs;
+
 #endif /* MAIN_H */


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