[gtranslator] Fix undo/redo behavior



commit fea799196b899306691c60b4061804ecc2312f36
Author: Daniel GarcĂ­a Moreno <danigm wadobo com>
Date:   Tue Sep 18 20:24:08 2018 +0200

    Fix undo/redo behavior
    
    Fix #4

 src/gtr-actions-edit.c | 26 ++++++++++++++++++++------
 src/gtr-notebook.c     | 24 ++++++++++++++++++++++++
 src/gtr-notebook.h     |  2 ++
 src/gtr-window.c       | 22 ++++++++++++++++++++++
 4 files changed, 68 insertions(+), 6 deletions(-)
---
diff --git a/src/gtr-actions-edit.c b/src/gtr-actions-edit.c
index 8b62bbc8..c58aa209 100644
--- a/src/gtr-actions-edit.c
+++ b/src/gtr-actions-edit.c
@@ -37,19 +37,26 @@ gtr_actions_edit_undo (GtkAction * action, GtrWindow * window)
 {
   GtrView *active_view;
   GtkSourceBuffer *active_document;
+  GtrTab *current;
+  GList *msg;
+  GtrPo *po;
 
+  current = gtr_window_get_active_tab (window);
+  po = gtr_tab_get_po (current);
+  msg = gtr_po_get_current_message (po);
   active_view = gtr_window_get_active_view (window);
+
   g_return_if_fail (active_view);
 
   active_document =
     GTK_SOURCE_BUFFER (gtk_text_view_get_buffer
                        (GTK_TEXT_VIEW (active_view)));
 
-  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (active_document));
-  gtk_source_buffer_undo (active_document);
-  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (active_document));
+  if (gtk_source_buffer_can_undo (active_document))
+    gtk_source_buffer_undo (active_document);
 
   gtk_widget_grab_focus (GTK_WIDGET (active_view));
+  g_signal_emit_by_name (current, "message_changed", msg->data);
 }
 
 void
@@ -57,6 +64,13 @@ gtr_actions_edit_redo (GtkAction * action, GtrWindow * window)
 {
   GtrView *active_view;
   GtkSourceBuffer *active_document;
+  GtrTab *current;
+  GList *msg;
+  GtrPo *po;
+
+  current = gtr_window_get_active_tab (window);
+  po = gtr_tab_get_po (current);
+  msg = gtr_po_get_current_message (po);
 
   active_view = gtr_window_get_active_view (window);
   g_return_if_fail (active_view);
@@ -65,11 +79,11 @@ gtr_actions_edit_redo (GtkAction * action, GtrWindow * window)
     GTK_SOURCE_BUFFER (gtk_text_view_get_buffer
                        (GTK_TEXT_VIEW (active_view)));
 
-  gtk_text_buffer_begin_user_action (GTK_TEXT_BUFFER (active_document));
-  gtk_source_buffer_redo (active_document);
-  gtk_text_buffer_end_user_action (GTK_TEXT_BUFFER (active_document));
+  if (gtk_source_buffer_can_redo (active_document))
+    gtk_source_buffer_redo (active_document);
 
   gtk_widget_grab_focus (GTK_WIDGET (active_view));
+  g_signal_emit_by_name (current, "message_changed", msg->data);
 }
 
 void
diff --git a/src/gtr-notebook.c b/src/gtr-notebook.c
index b855d251..9f8efd85 100644
--- a/src/gtr-notebook.c
+++ b/src/gtr-notebook.c
@@ -41,6 +41,9 @@ typedef struct
   GtkWidget *progress_untrans;
   GtkWidget *save;
   GtrProgress *progress;
+
+  GtkWidget *undo;
+  GtkWidget *redo;
 } GtrNotebookPrivate;
 
 G_DEFINE_TYPE_WITH_PRIVATE (GtrNotebook, gtr_notebook, GTK_TYPE_NOTEBOOK)
@@ -153,6 +156,8 @@ gtr_notebook_class_init (GtrNotebookClass * klass)
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, progress_fuzzy);
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, progress_untrans);
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, progress_percentage);
+  gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, undo);
+  gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, redo);
   gtk_widget_class_bind_template_child_private (widget_class, GtrNotebook, save);
 }
 
@@ -300,3 +305,22 @@ gtr_notebook_enable_save (GtrNotebook *notebook,
   gtk_widget_set_sensitive (priv->save, enable);
 }
 
+void
+gtr_notebook_update_undo_buttons (GtrNotebook *notebook,
+                                  GtrView     *view)
+{
+  GtkSourceBuffer *active_document;
+  GtrNotebookPrivate *priv = gtr_notebook_get_instance_private (notebook);
+  gboolean can_undo, can_redo;
+  g_return_if_fail (view);
+
+  active_document =
+    GTK_SOURCE_BUFFER (gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)));
+
+  can_undo = gtk_source_buffer_can_undo (active_document);
+  can_redo = gtk_source_buffer_can_redo (active_document);
+
+  gtk_widget_set_sensitive (priv->undo, can_undo);
+  gtk_widget_set_sensitive (priv->redo, can_redo);
+}
+
diff --git a/src/gtr-notebook.h b/src/gtr-notebook.h
index 53ff6d41..164e9ab0 100644
--- a/src/gtr-notebook.h
+++ b/src/gtr-notebook.h
@@ -79,5 +79,7 @@ void gtr_notebook_set_progress (GtrNotebook *notebook, gint trans, gint untrans,
 
 void gtr_notebook_enable_save (GtrNotebook *notebook, gboolean enable);
 
+void gtr_notebook_update_undo_buttons (GtrNotebook *notebook, GtrView *view);
+
 G_END_DECLS
 #endif /* __NOTEBOOK_H__ */
diff --git a/src/gtr-window.c b/src/gtr-window.c
index 0846bc1a..709c8628 100644
--- a/src/gtr-window.c
+++ b/src/gtr-window.c
@@ -103,6 +103,16 @@ static void          profile_combo_changed            (GtrStatusComboBox *combo,
 
 static void update_saved_state (GtrPo *po, GParamSpec *param, gpointer window);
 
+static void
+update_undo_state (GtrTab     *tab,
+                   GtrMsg     *msg,
+                   GtrWindow  *window)
+{
+  GtrWindowPrivate *priv = gtr_window_get_instance_private(window);
+  GtrView *active_view = gtr_window_get_active_view (window);
+  gtr_notebook_update_undo_buttons (priv->notebook, active_view);
+}
+
 /*
  * gtr_window_update_statusbar_message_count:
  * 
@@ -447,6 +457,18 @@ notebook_tab_added (GtkNotebook * notebook,
                           G_CALLBACK
                           (gtr_window_update_statusbar_message_count),
                           window);
+
+  g_signal_connect_after (child,
+                          "message_changed",
+                          G_CALLBACK (update_undo_state),
+                          window);
+
+  g_signal_connect_after (child,
+                          "showed-message",
+                          G_CALLBACK (update_undo_state),
+                          window);
+
+  update_undo_state (NULL, NULL, window);
 }
 
 static void


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