# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1410625180 18000
# Node ID 4caf38b1150ff162cd90b9757a9650873279d4e7
# Parent  ec1e295402fa3e3b6eda5b99c13ca106043bdf48
For GUI undo, send Cntrl-U to Readline rather than callback command_editor.

* libgui/qterminal/libqterminal/unix/TerminalView.cpp:
  (TerminalView::handle_undo_request): Added.  Post a Cntrl-U to the
application core for this TerminalView to process and destined for Readline.

* libgui/qterminal/libqterminal/unix/TerminalView.h: Add a public slot called
handle_undo_request().

* libgui/src/main-window.cc
  (main_window::handle_undo_request): Removed.
  (main_window::command_window_undo_callback): Removed.
  (main_window::construct): Make a connection between the application's signal
focusChanged() and this window's slot onAppFocusChanged().
  (main_window::onApplicationFocusChanged): Added.  Disconnect the previously
focused window's handle_undo_request() slot and connect the newly focused
window's handle_undo_request() slot.
  (main_window::construct_tool_bar): Remove the connection between _undo_action
and this window's handle_undo_request() slot.
  (main_window::command_window_undo_callback): Removed.

* libgui/src/main-window.h: Remove public slot handle_undo_requestion() and
function command_window_undo_callback().  Add private slot onAppFocusChanged().

diff --git a/libgui/qterminal/libqterminal/unix/TerminalView.cpp b/libgui/qterminal/libqterminal/unix/TerminalView.cpp
--- a/libgui/qterminal/libqterminal/unix/TerminalView.cpp
+++ b/libgui/qterminal/libqterminal/unix/TerminalView.cpp
@@ -2391,6 +2391,21 @@
     }
 }
 
+void
+TerminalView::handle_undo_request( void )
+{
+  // Replicate a Cntrl-U keyboard press to this terminal window.
+  QCoreApplication::postEvent (this,
+                               new QKeyEvent(QEvent::KeyPress,
+                                             Qt::Key_Control,
+                                             Qt::NoModifier));
+  QCoreApplication::postEvent (this,
+                               new QKeyEvent(QEvent::KeyPress,
+                                             Qt::Key_U,
+                                             Qt::ControlModifier,
+                                             QString("\025")));
+}
+
 void TerminalView::inputMethodEvent( QInputMethodEvent* event )
 {
   QKeyEvent keyEvent(QEvent::KeyPress,0,Qt::NoModifier,event->commitString());
@@ -2401,6 +2416,7 @@
 
   event->accept();
 }
+
 QVariant TerminalView::inputMethodQuery( Qt::InputMethodQuery query ) const
 {
   const QPoint cursorPos = _screenWindow ? _screenWindow->cursorPosition() : QPoint(0,0);
diff --git a/libgui/qterminal/libqterminal/unix/TerminalView.h b/libgui/qterminal/libqterminal/unix/TerminalView.h
--- a/libgui/qterminal/libqterminal/unix/TerminalView.h
+++ b/libgui/qterminal/libqterminal/unix/TerminalView.h
@@ -450,6 +450,9 @@
     /** See setUsesMouse() */
     bool usesMouse() const;
 
+    /** Dynamically connected with undo action according to focus */
+    void handle_undo_request (void);
+
 signals:
 
     void interrupt_signal (void);
diff --git a/libgui/src/main-window.cc b/libgui/src/main-window.cc
--- a/libgui/src/main-window.cc
+++ b/libgui/src/main-window.cc
@@ -232,12 +232,6 @@
 }
 
 void
-main_window::handle_undo_request (void)
-{
-  octave_link::post_event (this, &main_window::command_window_undo_callback);
-}
-
-void
 main_window::handle_clear_command_window_request (void)
 {
   octave_link::post_event (this, &main_window::clear_command_window_callback);
@@ -1299,8 +1293,30 @@
   Fregister_graphics_toolkit (ovl ("qt"));
 
   configure_shortcuts ();
+
+  connect (qApp, SIGNAL (focusChanged (QWidget *, QWidget *)),
+           this, SLOT (onAppFocusChanged (QWidget *, QWidget *)));
 }
 
+void
+main_window::onAppFocusChanged (QWidget *old, QWidget *now)
+{
+  // Disconnect the old focus from the undo action and connect the
+  // new focus to the undo action checking for existence of undo handler.
+  disconnect (_undo_action, SIGNAL (triggered ()),
+              old, SLOT (handle_undo_request ()));
+  if (connect (_undo_action, SIGNAL (triggered ()),
+               now, SLOT (handle_undo_request ())))
+    {
+      // Successful connection, make icon appear active.
+      _undo_action->setEnabled (true);
+    }
+  else
+    {
+      // Unsuccessful connection, make icon appear inactive.
+      _undo_action->setEnabled (false);
+    }
+}
 
 void
 main_window::handle_octave_ready ()
@@ -2044,9 +2060,6 @@
 
   connect (current_dir_up, SIGNAL (triggered ()),
            this, SLOT (change_directory_up ()));
-
-  connect (_undo_action, SIGNAL (triggered ()),
-           this, SLOT (handle_undo_request ()));
 }
 
 void
@@ -2082,13 +2095,6 @@
 }
 
 void
-main_window::command_window_undo_callback (void)
-{
-  command_editor::undo ();
-  command_editor::redisplay ();
-}
-
-void
 main_window::clear_command_window_callback (void)
 {
   Fclc ();
diff --git a/libgui/src/main-window.h b/libgui/src/main-window.h
--- a/libgui/src/main-window.h
+++ b/libgui/src/main-window.h
@@ -104,7 +104,6 @@
   void handle_clear_history_request (void);
   void handle_rename_variable_request (const QString& old_name,
                                        const QString& new_name);
-  void handle_undo_request (void);
   void new_file (const QString& commands = QString ());
   void open_file (const QString& file_name = QString ());
   void open_online_documentation_page (void);
@@ -204,6 +203,9 @@
   QList<octave_dock_widget *> get_dock_widget_list ()
     { return dock_widget_list (); }
 
+private slots:
+  void onAppFocusChanged(QWidget* old, QWidget* now);
+
 protected:
   void closeEvent (QCloseEvent * closeEvent);
 
@@ -246,8 +248,6 @@
 
   void rename_variable_callback (const name_pair& names);
 
-  void command_window_undo_callback (void);
-
   void clear_command_window_callback (void);
 
   void resize_command_window_callback (void);
exporting patch:
<fdopen>
