[gnome-latex: 23/205] files recently opened



commit be2f2cc137ae4876efa44b369737baea6bcde1bc
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Thu Aug 20 13:57:16 2009 +0200

    files recently opened

 TODO            |  4 ++-
 src/callbacks.c | 87 ++++++++++++++++++++++++++++++---------------------------
 src/callbacks.h |  1 +
 src/main.c      | 11 ++++++++
 src/ui.xml      |  1 +
 5 files changed, 62 insertions(+), 42 deletions(-)
---
diff --git a/TODO b/TODO
index 96423ea..350e4e4 100644
--- a/TODO
+++ b/TODO
@@ -12,7 +12,7 @@ Tue Aug 18, 2009 to Mon Aug 24, 2009
        x default height and width
        - text in red when it's an error
 
-[-] files recently opened (in the File menu)
+[x] files recently opened (in the File menu)
 
 [-] statusbar
        - function print_statusbar ()
@@ -28,6 +28,8 @@ Tue Aug 25, 2009 to Mon Aug 31, 2009
 
 [-] search and replace
 
+[-] copy/cut/paste
+
 [-] Preferences
        - commands
        - source code font
diff --git a/src/callbacks.c b/src/callbacks.c
index 99749b8..efe786f 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -13,7 +13,9 @@
 #include "callbacks.h"
 #include "print.h"
 
-static void create_document_in_new_tab (gchar *path, gchar *text, gchar *title);
+static void document_get_content (const gchar *filename, const gchar *uri);
+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);
@@ -23,7 +25,6 @@ static void set_undo_redo_sensitivity (void);
 static void run_compilation (gchar *title, gchar *command);
 static void view_document (gchar *title, gchar *doc_extension);
 static void add_action (gchar *title, gchar *command, gchar *command_output);
-//static void free_actions (void);
 
 void
 cb_new (void)
@@ -49,23 +50,15 @@ cb_open (void)
 
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
        {
-               gchar *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
-               print_info ("Open file: \"%s\"", filename);
-               gchar *contents = NULL;
-               if (g_file_get_contents (filename, &contents, NULL, NULL))
-               {
-                       // convert the text to UTF-8
-                       gchar *text_utf8 = g_locale_to_utf8 (contents, -1, NULL, NULL, NULL);
+               gchar *filename = gtk_file_chooser_get_filename (
+                               GTK_FILE_CHOOSER (dialog));
+               gchar *uri = gtk_file_chooser_get_uri (
+                               GTK_FILE_CHOOSER (dialog));
 
-                       create_document_in_new_tab (filename, text_utf8,
-                                       g_path_get_basename (filename));
-                       g_free (contents);
-                       g_free (text_utf8);
-               }
-               else
-                       print_warning ("impossible to open the file \"%s\"", filename);
+               document_get_content (filename, uri);
 
                g_free (filename);
+               g_free (uri);
        }
        gtk_widget_destroy (dialog);
 }
@@ -163,7 +156,6 @@ cb_quit (void)
 {
        if (close_all ())
        {
-               //free_actions ();
                print_info ("Bye bye");
                gtk_main_quit ();
        }
@@ -313,19 +305,53 @@ cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gp
        set_undo_redo_sensitivity ();
 }
 
+void
+cb_recent_item_activated (GtkRecentAction *action, gpointer user_data)
+{
+       GtkRecentChooser *chooser = GTK_RECENT_CHOOSER (action);
+       GtkRecentInfo *info = gtk_recent_chooser_get_current_item (chooser);
+       const gchar *filename = gtk_recent_info_get_uri_display (info);
+       const gchar *uri = gtk_recent_info_get_uri (info);
+
+       document_get_content (filename, uri);
+}
+
 /*****************************
  * local functions
  *****************************/
 
 static void
-create_document_in_new_tab (gchar *path, gchar *text, gchar *title)
+document_get_content (const gchar *filename, const gchar *uri)
+{
+       print_info ("Open file: \"%s\"", filename);
+
+       gchar *contents = NULL;
+       if (g_file_get_contents (filename, &contents, NULL, NULL))
+       {
+               // convert the text to UTF-8
+               gchar *text_utf8 = g_locale_to_utf8 (contents, -1, NULL, NULL, NULL);
+
+               create_document_in_new_tab (filename, text_utf8,
+                               g_path_get_basename (filename));
+
+               GtkRecentManager *manager = gtk_recent_manager_get_default ();
+               gtk_recent_manager_add_item (manager, uri);
+
+               g_free (contents);
+               g_free (text_utf8);
+       }
+       else
+               print_warning ("impossible to open the file \"%s\"", filename);
+}
+
+static void
+create_document_in_new_tab (const gchar *path, const gchar *text, const gchar *title)
 {
        // create a new document_t structure
        // if path = NULL, this is a new document
        // else, the user opened a document
        document_t *new_doc = g_malloc (sizeof (document_t));
        new_doc->path = g_strdup (path);
-       //new_doc->saved = (path == NULL) ? FALSE : TRUE;
        new_doc->saved = TRUE;
        new_doc->source_buffer = gtk_source_buffer_new (NULL);
        new_doc->source_view = gtk_source_view_new_with_buffer (new_doc->source_buffer);
@@ -684,28 +710,6 @@ view_document (gchar *title, gchar *doc_extension)
        }
 }
 
-/*
-void
-free_actions (void)
-{
-       while (latexila.active_action != NULL)
-       {
-               action_t *action = latexila.active_action;
-               latexila.all_actions = g_list_remove (latexila.all_actions, action);
-
-               if (action->title != NULL)
-                       g_free (action->title);
-               if (action->command != NULL)
-                       g_free (action->command);
-               if (action->command_output != NULL)
-                       g_free (action->command_output);
-               g_free (action);
-               
-               latexila.active_action = g_list_nth_data (latexila.all_actions, 0);
-       }
-}
-*/
-
 static void
 add_action (gchar *title, gchar *command, gchar *command_output)
 {
@@ -734,3 +738,4 @@ add_action (gchar *title, gchar *command, gchar *command_output)
        g_free (title2);
 }
 
+
diff --git a/src/callbacks.h b/src/callbacks.h
index fb8803a..36df104 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -23,5 +23,6 @@ void cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page,
 gboolean cb_delete_event (GtkWidget *widget, GdkEvent *event,
                gpointer user_data);
 void cb_line_numbers (GtkToggleAction *action, gpointer user_data);
+void cb_recent_item_activated (GtkRecentAction *action, gpointer user_data);
 
 #endif /* CALLBACKS_H */
diff --git a/src/main.c b/src/main.c
index ee9a970..3df5b0d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -94,10 +94,21 @@ main (int argc, char *argv[])
        guint nb_entries = G_N_ELEMENTS (entries);
        guint nb_toggle_entries = G_N_ELEMENTS (toggle_entries);
 
+       // recent document
+       GtkAction *recent = gtk_recent_action_new ("FileOpenRecent",
+                       _("Open Recent"), _("Open recently used files"), NULL);
+       g_signal_connect (G_OBJECT (recent), "item-activated",
+                       G_CALLBACK (cb_recent_item_activated), NULL);
+
+       GtkRecentFilter *filter = gtk_recent_filter_new ();
+       gtk_recent_filter_add_application (filter, "latexila");
+       gtk_recent_chooser_add_filter (GTK_RECENT_CHOOSER (recent), filter);
+
        // 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_toggle_actions (action_group, toggle_entries, nb_toggle_entries, NULL);
+       gtk_action_group_add_action (action_group, recent);
        GtkUIManager *ui_manager = gtk_ui_manager_new ();
        gtk_ui_manager_insert_action_group (ui_manager, action_group, 0);
 
diff --git a/src/ui.xml b/src/ui.xml
index 71047dd..1c6c8c7 100644
--- a/src/ui.xml
+++ b/src/ui.xml
@@ -8,6 +8,7 @@ In the code, GtkUIManager is used to construct them.
     <menu action="File">
       <menuitem action="FileNew" />
       <menuitem action="FileOpen" />
+      <menuitem action="FileOpenRecent" />
       <separator />
       <menuitem action="FileSave" />
       <menuitem action="FileSaveAs" />


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