exporting patch:
# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1526082073 18000
#      Fri May 11 18:41:13 2018 -0500
# Node ID bdb409c060fdd516b1430c78d4d657591b96e616
# Parent  bc5f225bc5783240d02acc81fb59738edfa578a8
Force focus of GUI tab bar click on current index tab (bug #53836)

* main-window.cc (handle_tab_bar_clicked): Added.  New slot function that
  examines the index of the QTabBar that emitted the signal, and if it matches
  the current index temporarily change the index and re-establish.

* main-window.h (handle_tab_bar_clicked): Added.  Declaration of slot.

* octave-dock-widget.cc (octave_dock_widget::octave_dock_widget): Connect the
  QDockWidget dockLocationChanged signal to new slot
  handle_dock_location_changed().
  (handle_dock_location_changed): Added.  Slot that makes sure any tab stack
  that may have arisen has the QTabBar connected to the main_window slot
  handle_tab_bar_clicked().

* octave_dock_widget.h (handle_dock_location_changed): Added.  Declaration of
  slot.

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
@@ -280,6 +280,25 @@ namespace octave
     m_command_window->focus ();
   }
 
+  void
+  main_window::handle_tab_bar_clicked (int index)
+  {
+    // Clicking the active tab won't bring QDockWidget in focus
+    // so inactivate the index so that it is brought back into focus.
+    QList<QTabBar *> tabbarlist = findChildren<QTabBar *> (QString(), Qt::FindDirectChildrenOnly);
+    QObject *snd = QObject::sender();
+    foreach (QTabBar *tbar, tabbarlist)
+      if (tbar == snd)
+        {
+          if (tbar->count () > 0 && tbar->currentIndex () == index)
+            {
+              tbar->setCurrentIndex ((index + 1) % tbar->count ());
+              tbar->setCurrentIndex (index);
+            }
+          break;
+        }
+  }
+
   // catch focus changes and determine the active dock widget
   void main_window::focus_changed (QWidget *, QWidget *new_widget)
   {
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
@@ -139,6 +139,7 @@ namespace octave
 
   public slots:
 
+    void handle_tab_bar_clicked (int index);
     void focus_changed (QWidget *w_old, QWidget *w_new);
     void request_reload_settings (void);
 
diff --git a/libgui/src/octave-dock-widget.cc b/libgui/src/octave-dock-widget.cc
--- a/libgui/src/octave-dock-widget.cc
+++ b/libgui/src/octave-dock-widget.cc
@@ -185,6 +185,8 @@ namespace octave
              this, SLOT (toplevel_change (bool)));
     connect (this, SIGNAL (visibilityChanged (bool)),
              this, SLOT (handle_visibility_changed (bool)));
+    connect (this, SIGNAL (dockLocationChanged (Qt::DockWidgetArea)),
+             this, SLOT (handle_dock_location_changed (Qt::DockWidgetArea)));
 
     connect (p, SIGNAL (settings_changed (const QSettings*)),
              this, SLOT (handle_settings (const QSettings*)));
@@ -534,6 +536,26 @@ namespace octave
     QDockWidget::resizeEvent (event);
   }
 
+  void
+  octave_dock_widget::handle_dock_location_changed (Qt::DockWidgetArea area)
+  {
+    if (parentWidget ())
+      {
+        QList<QTabBar *> tabbarlist = m_parent->findChildren<QTabBar *>
+                                      (QString(), Qt::FindDirectChildrenOnly);
+        foreach (QTabBar *tbar, tabbarlist)
+          if (tbar-> count () > 0)
+            {
+              // Connect a slot to the clicked signal to recognize that
+              // a tab has been pressed, not just activated.
+              disconnect (tbar, SIGNAL (tabBarClicked (int)),
+                          m_parent, SLOT (handle_tab_bar_clicked (int)));
+              connect (tbar, SIGNAL (tabBarClicked (int)),
+                       m_parent, SLOT (handle_tab_bar_clicked (int)));
+            }
+      }
+  }
+
   // slot for hiding the widget
   void
   octave_dock_widget::change_visibility (bool)
diff --git a/libgui/src/octave-dock-widget.h b/libgui/src/octave-dock-widget.h
--- a/libgui/src/octave-dock-widget.h
+++ b/libgui/src/octave-dock-widget.h
@@ -154,6 +154,8 @@ namespace octave
         emit active_changed (true);
     }
 
+    void handle_dock_location_changed (Qt::DockWidgetArea area);
+
     //! Event filter for double clicks into the window decoration elements.
 
     bool eventFilter (QObject *obj, QEvent *e);
