[gnome-latex: 22/205] log zone: view the previous actions The actions are not stored in a GList of action_t structures, th



commit ef320c4f7d7a0eb610e41b4967f2e4f94b29415f
Author: Sébastien Wilmet <sebastien wilmet gmail com>
Date:   Wed Aug 19 15:15:02 2009 +0200

    log zone: view the previous actions
    The actions are not stored in a GList of action_t structures, that was a
    stupid idea...
    The actions are now stored in a GtkListStore (latexila.list_store), but only the
    title is showed in the list (to the left), the command and the command output are
    showed to the right.

 TODO            |  11 ++--
 src/callbacks.c | 152 +++++++++++++++++++++++++++++++++++---------------------
 src/callbacks.h |   8 ++-
 src/main.c      |  17 +++++--
 src/main.h      |  39 +++++++--------
 src/print.c     |  25 +++-------
 src/print.h     |   3 +-
 7 files changed, 150 insertions(+), 105 deletions(-)
---
diff --git a/TODO b/TODO
index 842c542..96423ea 100644
--- a/TODO
+++ b/TODO
@@ -5,11 +5,14 @@ Tue Aug 18, 2009 to Mon Aug 24, 2009
 [x] tabs with close buttons
 
 [-] log zone
-       - horizontal pane
+       x horizontal pane
          left: list with an history of the actions numbered
          right: details of the action selected
        x function print_log ()
        x default height and width
+       - text in red when it's an error
+
+[-] files recently opened (in the File menu)
 
 [-] statusbar
        - function print_statusbar ()
@@ -23,10 +26,12 @@ Tue Aug 18, 2009 to Mon Aug 24, 2009
 
 Tue Aug 25, 2009 to Mon Aug 31, 2009
 
-[-] files recently opened (in the File menu)
-
 [-] search and replace
 
+[-] Preferences
+       - commands
+       - source code font
+
 [-] CMake
 
 [-] French translation
diff --git a/src/callbacks.c b/src/callbacks.c
index 4297e5b..99749b8 100644
--- a/src/callbacks.c
+++ b/src/callbacks.c
@@ -20,9 +20,10 @@ static void file_save (void);
 static gboolean close_all (void);
 static void set_title (void);
 static void set_undo_redo_sensitivity (void);
-static void run_compilation (action_t *action);
-static void view_document (action_t *action, gchar *doc_extension);
-static void free_actions (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)
@@ -162,7 +163,7 @@ cb_quit (void)
 {
        if (close_all ())
        {
-               free_actions ();
+               //free_actions ();
                print_info ("Bye bye");
                gtk_main_quit ();
        }
@@ -187,7 +188,7 @@ cb_redo (void)
 }
 
 gboolean
-cb_delete_event (GtkWidget *widget, GdkEvent *event, gpointer data)
+cb_delete_event (GtkWidget *widget, GdkEvent *event, gpointer user_data)
 {
        cb_quit ();
 
@@ -213,59 +214,52 @@ cb_line_numbers (GtkToggleAction *action, gpointer user_data)
 void
 cb_latex (void)
 {
-       action_t *action = g_malloc (sizeof (action_t));
-       action->title = g_strdup (_("Compile (latex)"));
-       action->command = g_strdup_printf ("latex -interaction=nonstopmode %s",
+       gchar *title = _("Compile (latex)");
+       gchar *command = g_strdup_printf ("latex -interaction=nonstopmode %s",
                        latexila.active_doc->path);
-       action->command_output = NULL;
 
-       latexila.all_actions = g_list_append (latexila.all_actions, action);
-       latexila.active_action = action;
-
-       run_compilation (action);
+       run_compilation (title, command);
+       g_free (command);
 }
 
 void
 cb_pdflatex (void)
 {
-       action_t *action = g_malloc (sizeof (action_t));
-       action->title = g_strdup (_("Compile (pdflatex)"));
-       action->command = g_strdup_printf ("pdflatex -interaction=nonstopmode %s",
+       gchar *title = _("Compile (pdflatex)");
+       gchar *command = g_strdup_printf ("pdflatex -interaction=nonstopmode %s",
                        latexila.active_doc->path);
-       action->command_output = NULL;
-
-       latexila.all_actions = g_list_append (latexila.all_actions, action);
-       latexila.active_action = action;
 
-       run_compilation (action);
+       run_compilation (title, command);
+       g_free (command);
 }
 
 void
 cb_view_dvi (void)
 {
-       action_t *action = g_malloc (sizeof (action_t));
-       action->title = g_strdup (_("View DVI"));
-       action->command = NULL;
-       action->command_output = NULL;
-
-       latexila.all_actions = g_list_append (latexila.all_actions, action);
-       latexila.active_action = action;
-
-       view_document (action, ".dvi");
+       view_document (_("View DVI"), ".dvi");
 }
 
 void
 cb_view_pdf (void)
 {
-       action_t *action = g_malloc (sizeof (action_t));
-       action->title = g_strdup (_("View PDF"));
-       action->command = NULL;
-       action->command_output = NULL;
-
-       latexila.all_actions = g_list_append (latexila.all_actions, action);
-       latexila.active_action = action;
+       view_document (_("View PDF"), ".pdf");
+}
 
-       view_document (action, ".pdf");
+void
+cb_action_list_changed (GtkTreeSelection *selection, gpointer user_data)
+{
+       GtkTreeIter iter;
+       GtkTreeModel *model;
+       if (gtk_tree_selection_get_selected (selection, &model, &iter))
+       {
+               gchar *title, *command, *command_output;
+               gtk_tree_model_get (model, &iter,
+                               COLUMN_ACTION_TITLE, &title,
+                               COLUMN_ACTION_COMMAND, &command,
+                               COLUMN_ACTION_COMMAND_OUTPUT, &command_output,
+                               -1);
+               print_log (latexila.log, title, command, command_output);
+       }
 }
 
 void
@@ -576,17 +570,21 @@ set_undo_redo_sensitivity (void)
 }
 
 static void
-run_compilation (action_t *action)
+run_compilation (gchar *title, gchar *command)
 {
        if (latexila.active_doc != NULL)
        {
+               gchar *command_output;
+
                // the current document is a *.tex file?
                gboolean tex_file = g_regex_match_simple ("\\.tex$", latexila.active_doc->path, 0, 0);
                if (! tex_file)
                {
-                       action->command_output = g_strdup_printf (_("compilation failed: %s is not a *.tex 
file"),
+                       command_output = g_strdup_printf (_("compilation failed: %s is not a *.tex file"),
                                        g_path_get_basename (latexila.active_doc->path));
-                       print_log (action);
+
+                       add_action (title, command, command_output);
+                       g_free (command_output);
                        return;
                }
 
@@ -597,31 +595,34 @@ run_compilation (action_t *action)
                //gchar *full_command = g_strdup_printf ("%s %s", command, latexila.active_doc->path);
                GError *error = NULL;
 
-               print_info ("execution of the command: %s", action->command);
+               print_info ("execution of the command: %s", command);
                
-               g_spawn_command_line_sync (action->command, &(action->command_output), NULL, NULL, &error);
+               g_spawn_command_line_sync (command, &command_output, NULL, NULL, &error);
                g_chdir (dir_backup);
                
                // an error occured
                if (error != NULL)
                {
-                       action->command_output = g_strdup_printf (_("execution failed: %s"),
+                       command_output = g_strdup_printf (_("execution failed: %s"),
                                        error->message);
                        g_error_free (error);
                }
 
-               print_log (action);
+               add_action (title, command, command_output);
 
+               g_free (command_output);
                g_free (dir_backup);
                g_free (dir);
        }
 }
 
 static void
-view_document (action_t *action, gchar *doc_extension)
+view_document (gchar *title, gchar *doc_extension)
 {
        if (latexila.active_doc != NULL)
        {
+               gchar *command, *command_output;
+
                GError *error = NULL;
                GRegex *regex = g_regex_new ("\\.tex$", 0, 0, NULL);
 
@@ -629,16 +630,19 @@ view_document (action_t *action, gchar *doc_extension)
                gchar *doc_path = g_regex_replace_literal (regex, latexila.active_doc->path,
                                -1, 0, doc_extension, 0, NULL);
 
-               action->command = g_strdup_printf ("evince %s", doc_path);
+               command = g_strdup_printf ("evince %s", doc_path);
 
                // the current document is a *.tex file?
                gboolean tex_file = g_regex_match (regex, latexila.active_doc->path, 0, NULL);
                if (! tex_file)
                {
-                       action->command_output = g_strdup_printf (_("failed: %s is not a *.tex file"),
+                       command_output = g_strdup_printf (_("failed: %s is not a *.tex file"),
                                        g_path_get_basename (latexila.active_doc->path));
 
-                       print_log (action);
+                       add_action (title, command, command_output);
+                       g_free (command);
+                       g_free (command_output);
+                       g_free (doc_path);
                        g_regex_unref (regex);
                        return;
                }
@@ -646,36 +650,41 @@ view_document (action_t *action, gchar *doc_extension)
                // the document (PDF, DVI, ...) file exist?
                if (! g_file_test (doc_path, G_FILE_TEST_IS_REGULAR))
                {
-                       action->command_output = g_strdup_printf (
+                       command_output = g_strdup_printf (
                                        _("%s does not exist. If this is not already made, compile the 
document with the right command."),
                                        g_path_get_basename (doc_path));
 
-                       print_log (action);
+                       add_action (title, command, command_output);
+                       g_free (command);
+                       g_free (command_output);
                        g_free (doc_path);
                        g_regex_unref (regex);
                        return;
                }
 
                // run the command
-               print_info ("execution of the command: %s", action->command);
-               g_spawn_command_line_async (action->command, &error);
+               print_info ("execution of the command: %s", command);
+               g_spawn_command_line_async (command, &error);
 
                if (error != NULL)
                {
-                       action->command_output = g_strdup_printf (_("execution failed: %s"),
+                       command_output = g_strdup_printf (_("execution failed: %s"),
                                        error->message);
                        g_error_free (error);
                }
                else
-                       action->command_output = g_strdup ("OK");
+                       command_output = g_strdup ("OK");
 
-               print_log (action);
+               add_action (title, command, command_output);
 
-               g_regex_unref (regex);
+               g_free (command);
+               g_free (command_output);
                g_free (doc_path);
+               g_regex_unref (regex);
        }
 }
 
+/*
 void
 free_actions (void)
 {
@@ -695,4 +704,33 @@ free_actions (void)
                latexila.active_action = g_list_nth_data (latexila.all_actions, 0);
        }
 }
+*/
+
+static void
+add_action (gchar *title, gchar *command, gchar *command_output)
+{
+       static gint num = 1;
+       gchar *title2 = g_strdup_printf ("%d. %s", num, title);
+
+       // append an new entry to the action list
+       GtkTreeIter iter;
+       gtk_list_store_append (latexila.list_store, &iter);
+       gtk_list_store_set (latexila.list_store, &iter,
+                       COLUMN_ACTION_TITLE, title2,
+                       COLUMN_ACTION_COMMAND, command,
+                       COLUMN_ACTION_COMMAND_OUTPUT, command_output,
+                       -1);
+
+       // the new entry is selected
+       // cb_action_list_changed () is called, so the details are showed
+       gtk_tree_selection_select_iter (latexila.list_selection, &iter);
+
+       // scroll to the end
+       GtkTreePath *path = gtk_tree_model_get_path (
+                       GTK_TREE_MODEL (latexila.list_store), &iter);
+       gtk_tree_view_scroll_to_cell (latexila.list_view, path, NULL, FALSE, 0, 0);
+
+       num++;
+       g_free (title2);
+}
 
diff --git a/src/callbacks.h b/src/callbacks.h
index 521f488..fb8803a 100644
--- a/src/callbacks.h
+++ b/src/callbacks.h
@@ -14,10 +14,14 @@ void cb_latex (void);
 void cb_pdflatex (void);
 void cb_view_dvi (void);
 void cb_view_pdf (void);
+void cb_action_list_changed (GtkTreeSelection *selection,
+               gpointer user_data);
 void cb_about_dialog (void);
 void cb_text_changed (GtkWidget *widget, gpointer user_data);
-void cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page, guint page_num, gpointer user_data);
-gboolean cb_delete_event (GtkWidget *widget, GdkEvent *event, gpointer data);
+void cb_page_change (GtkNotebook *notebook, GtkNotebookPage *page,
+               guint page_num, gpointer user_data);
+gboolean cb_delete_event (GtkWidget *widget, GdkEvent *event,
+               gpointer user_data);
 void cb_line_numbers (GtkToggleAction *action, gpointer user_data);
 
 #endif /* CALLBACKS_H */
diff --git a/src/main.c b/src/main.c
index 2eb66d7..ee9a970 100644
--- a/src/main.c
+++ b/src/main.c
@@ -145,15 +145,26 @@ main (int argc, char *argv[])
        gtk_paned_pack2 (GTK_PANED (vpaned), hpaned, TRUE, TRUE);
 
        // actions list
-       GtkListStore *list_store = gtk_list_store_new (N_COLUMNS, G_TYPE_STRING);
+       GtkListStore *list_store = gtk_list_store_new (N_COLUMNS_ACTION,
+                       G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
        latexila.list_store = list_store;
        
        GtkWidget *list_view = gtk_tree_view_new_with_model (
                        GTK_TREE_MODEL (list_store));
+       latexila.list_view = GTK_TREE_VIEW (list_view);
        GtkCellRenderer *renderer = gtk_cell_renderer_text_new ();
        GtkTreeViewColumn *column = gtk_tree_view_column_new_with_attributes (
-                       "Actions", renderer, "text", COLUMN_ACTION_TITLE, NULL);        
-       gtk_tree_view_append_column (GTK_TREE_VIEW (list_view), column);
+                       _("Action history"), renderer, "text", COLUMN_ACTION_TITLE, NULL);      
+       gtk_tree_view_append_column (latexila.list_view, column);
+       
+       // hide "Action history"
+       //gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (list_view), FALSE);
+
+       GtkTreeSelection *select = gtk_tree_view_get_selection (latexila.list_view);
+       latexila.list_selection = select;
+       gtk_tree_selection_set_mode (select, GTK_SELECTION_SINGLE);
+       g_signal_connect (G_OBJECT (select), "changed",
+                       G_CALLBACK (cb_action_list_changed), NULL);
 
        // with a scrollbar
        GtkWidget *sw1 = gtk_scrolled_window_new (NULL, NULL);
diff --git a/src/main.h b/src/main.h
index cfe8b97..30b30eb 100644
--- a/src/main.h
+++ b/src/main.h
@@ -11,32 +11,25 @@
 // each document opened is represented by a document_t structure
 typedef struct
 {
-       gchar           *path;
-       gboolean        saved;
-       GtkWidget       *source_view;
+       gchar                   *path;
+       gboolean                saved;
+       GtkWidget               *source_view;
        GtkSourceBuffer *source_buffer;
-       GtkWidget       *title;
+       GtkWidget               *title;
 } document_t;
 
 typedef struct
 {
-       gchar *title;
-       gchar *command;
-       gchar *command_output;
-} action_t;
-
-typedef struct
-{
-       GList           *all_docs;
-       document_t      *active_doc;
-       GList           *all_actions;
-       action_t        *active_action;
-       GtkWindow       *main_window;
-       GtkNotebook     *notebook;
-       GtkListStore    *list_store;
-       GtkTextView     *log;
-       GtkAction       *undo;
-       GtkAction       *redo;
+       GList                   *all_docs;
+       document_t              *active_doc;
+       GtkWindow               *main_window;
+       GtkNotebook             *notebook;
+       GtkListStore            *list_store;
+       GtkTreeView             *list_view;
+       GtkTreeSelection        *list_selection;
+       GtkTextView             *log;
+       GtkAction               *undo;
+       GtkAction               *redo;
 } latexila_t;
 
 // all the documents are accessible by the docs variable
@@ -46,7 +39,9 @@ extern latexila_t latexila;
 enum
 {
        COLUMN_ACTION_TITLE,
-       N_COLUMNS
+       COLUMN_ACTION_COMMAND,
+       COLUMN_ACTION_COMMAND_OUTPUT,
+       N_COLUMNS_ACTION
 };
 
 #endif /* MAIN_H */
diff --git a/src/print.c b/src/print.c
index 39cd205..2519ca7 100644
--- a/src/print.c
+++ b/src/print.c
@@ -2,21 +2,12 @@
 #include <stdio.h>
 #include <stdarg.h>
 #include <gtk/gtk.h>
-#include <gtksourceview/gtksourceview.h>
-
-#include "main.h"
 
 void
-print_log (action_t *action)
+print_log (GtkTextView *log, gchar *title, gchar *command,
+               gchar *command_output)
 {
-       /* append an entry to the action list */
-       GtkTreeIter iter;
-       gtk_list_store_append (latexila.list_store, &iter);
-       gtk_list_store_set (latexila.list_store, &iter, COLUMN_ACTION_TITLE,
-                       action->title, -1);
-
-       /* show the details */
-       GtkTextBuffer *log_buffer = gtk_text_view_get_buffer (latexila.log);
+       GtkTextBuffer *log_buffer = gtk_text_view_get_buffer (log);
        gtk_text_buffer_set_text (log_buffer, "", -1);
 
        GtkTextIter end;
@@ -24,17 +15,17 @@ print_log (action_t *action)
        // title (in bold)
        gtk_text_buffer_get_end_iter (log_buffer, &end);
        gtk_text_buffer_insert_with_tags_by_name (log_buffer, &end,
-                       action->title, -1, "bold", NULL);
+                       title, -1, "bold", NULL);
 
        // command
        gtk_text_buffer_get_end_iter (log_buffer, &end);
-       gchar *command = g_strdup_printf ("\n$ %s\n", action->command);
-       gtk_text_buffer_insert (log_buffer, &end, command, -1);
-       g_free (command);
+       gchar *command2 = g_strdup_printf ("\n$ %s\n", command);
+       gtk_text_buffer_insert (log_buffer, &end, command2, -1);
+       g_free (command2);
 
        // command output
        gtk_text_buffer_get_end_iter (log_buffer, &end);
-       gtk_text_buffer_insert (log_buffer, &end, action->command_output, -1);
+       gtk_text_buffer_insert (log_buffer, &end, command_output, -1);
 }
 
 void
diff --git a/src/print.h b/src/print.h
index 26e592b..6490c14 100644
--- a/src/print.h
+++ b/src/print.h
@@ -1,7 +1,8 @@
 #ifndef PRINT_H
 #define PRINT_H
 
-void print_log (action_t *action);
+void print_log (GtkTextView *log, gchar *title, gchar *command,
+               gchar *command_output);
 
 void print_info (char *, ...);
 void print_warning (char *, ...);


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