exporting patch:
# HG changeset patch
# User Daniel J Sebald <daniel.sebald@ieee.org>
# Date 1518583472 21600
#      Tue Feb 13 22:44:32 2018 -0600
# Node ID 10546e92694af8755d2bf410f90f4966fac879e7
# Parent  981990aaa93191849f64a811853d6cca529cc6db
Make the variable editor multi-panel via a QMainWindow layout (bug #53046)

* main-window.h: Remove <QMdiArea> and <QMdiSubWindow> from header list.

* octave-dock-widget.h (label_dock_widget): Added.  New intermediate class
  that contains the features related only to the thin header for dock/undock
  and close.  Namely m_icon_size, m_title_widget, m_dock_button,
  m_close_button, m_dock_action and m_close_action and member function
  set_title().
  (octave_dock_widget): Removed some elements for label_dock_widget class and
  inherit that object.

* octave-dock-widget.cc (label_dock_widget::label_dock_widget): Constructor
  of new class selecting a few initialization from octave_dock_widget related
  to thin header and no settings code and no connections at this level.  Set
  the style sheet of the m_dock_button and m_close_button to transparent.
  (label_dock_widget::set_title): Moved from octave_dock_widget without
  modification.
  (octave_dock_widget::octave_dock_widget): Select code moved to
  label_dock_widget constructor.
  (octave_dock_widget::set_title): Moved to label_dock_widget class.

* variable-editor-model.h (variable_editor_model::range_selected): New method
  that provides a list of ranges selected by the QAbstractTable that are
  useful for copy/paste, etc.
  (variable_editor_model::selected_to_octave): Moved here from variable_editor
  to make more modular.
  (variable_editor_model::edit_variable_signal): New signal for a model to
  launch a new variable_dock_widget if a sub-element needs it.
  (variable_editor_model::update_data): Changed from private to public.
  (variable_editor_model::update_data_cache): Ditto.
  (variable_editor_model::double_click): Moved here from variable_editor to
  make more modular.
  (variable_editor_model::up): Ditto.

* variable-editor-model.cc (variable_editor_model::setData): Change the test
  on input variable for QString to be anything that can be converted to a
  QString, e.g., numbers.
  (variable_editor_model::update_data): Remove obsolete emitted signal
  maybe_resize_columns_signal().
  (variable_editor_model::range_selected): New method that provides a list of
  range values for the table.
  (variable_editor_model::double_click): Moved from variable_editor.  Remove
  the indirect reference to table then model.
  (variable_editor_model::double_click): Ditto.
  (idx_to_expr): Ditto.
  (variable_editor_model::selected_to_octave): Ditto.  Simplify by calling
  range_selected().

* variable-editor.h (var_editor_tab): Remove.  A tab widget is no longer used.
  (variable_dock_widget): Added.  A new class inherting label_dock_widget that
  holds the table and data model, comprised of several member functions from
  variable_editor class, as slots, and a few signals to access those slots.
  (HoverToolButton): Very short class derived from QToolButton whose added
  feature is to capture the focused variable_dock_widget when the mouse is
  hovered over the button.
  (ReturnFocusToolButton): Similarly short class inherting HoverToolButton that
  adds the feature of restoring the focus before activating its associated
  action.
  (ReturnFocusMenu): Analogous to ReturnFocusToolButton, but based on QMenu.
  This is placed on a HoverToolButton.
  (variable_editor): Added QStrings m_current_focus_vname and
  m_hovered_focus_vname to keep track of variable that most recently focused.
  Removed m_close_action, m_close_others_action and m_close_all_action QActions
  since tab widget no longer used.  Some for m_tab_widget, m_tab_bar.  Change
  QWidget pointer m_container to QMainWindow pointer m_main.
  (variable_editor::make_edit_view): Change "page" to a variable_dock_widget
  pointer.
  (variable_editor::make_disp_view): Removed.
  (variable_editor::has_focus): Removed.
  (variable_editor::edit_variable): Change from private to public.
  (variable_editor::variable_destroyed): New slot.  Connect to
  variable_dock_widget to keep track of when to check if toolbar still valid.
  (variable_editor::variable_focuse): New slot.  Connect to
  variable_dock_widget to keep track of most recent focused variable window.
  (variable_editor::record_hovered_focus_variable): New slot.  Connect to
  variable_dock_widget toolbar's ReturnFocusToolButton and ReturnFocusMenu for
  when mouse cursor hovers.
  (variable_editor::restore_hovered_focus_variable): New slot.  Connect to
  variable_dock_widget toolbar's ReturnFocusToolButton and ReturnFocusMenu for
  prior to acting in order to return to active window/focus when hovered.
  (variable_editor::request_close_tab): Removed.  No longer needed.
  (variable_editor::request_close_other_tabs): Ditto.
  (variable_editor::request_close_all_tabs): Ditto.
  (variable_editor::close_tab): Ditto.
  (variable_editor::contextmenu_requested): Moved to variable_dock_widget
  class.
  (variable_editor::columnmenu_requested): Ditto.
  (variable_editor::rowmenu_requested): Ditto.
  (variable_editor::double_click): Moved to variable_editor_model class.
  (variable_editor::up): Ditto.
  (variable_editor::clearContent): Moved to variable_dock_widget class.
  (variable_editor::transposeContent): Ditto.
  (variable_editor::delete_selected): Ditto.
  (variable_editor::relay_command): Renamed to relay_selected_command() for
  clarity.
  (variable_editor::clear_content_signal): Added.  New signal to broadcast
  to the variable_dock_widgets' associated slot.
  (variable_editor::copy_clipboard_signal): Ditto.
  (variable_editor::paste_clipboard_signal): Ditto.
  (variable_editor::paste_table_clipboard_signal): Ditto.
  (variable_editor::context_menu_signal): Ditto.
  (variable_editor::column_menu_signal): Ditto.
  (variable_editor::row_menu_signal): Ditto.
  (variable_editor::save_signal): Ditto.
  (variable_editor::delete_selected_signal): Ditto.
  (variable_editor::selected_command_requested_signal): Ditto.
  (variable_editor::enable_actions): Removed.  Too short of function otherwise.
  (variable_editor::octave_to_coords): Removed, no longer needed.
  (variable_editor::real_var_name): Removed, no more KDE problem in tab names
  because tab widget no longer used.  Names stored in variable_dock_widget
  objectName.
  (variable_editor::selected_to_octave): Move to variable_editor_model class.
  (variable_editor::add_tool_bar_button): Convenience function for repetitive
  tool bar button construction.

* variable-editor.cc: Add <QMdiArea> and <QScreen> to list of headers.  Remove
  <QStackedWidget> from list.
  (idx_to_expr): Move to file variable-editor-model.cc.
  (variable_dock_widget::variable_dock_widget): Added.  Constructor that
  sets a couple attributes and connects the header dock actions inherited from
  label_dock_widget to appropriate slots.
  (variable_dock_widget::change_floating): Added.  New slot one line long.
  (variable_dock_widget::change_existence): Added.  New slot that calls close()
  which is configurable by Qt::WA_DeleteOnClose parameter.  For now, "close"
  means "hide".
  (variable_dock_widget::toplevel_change): Added.  Changes the dock/undock
  icon, but also its tooltip.  Also changes the focus to the new window.  First
  time enlarged is very big to simulate "maximize", i.e., better temporary view
  of this variable.
  (variable_dock_widget::focusInEvent): Etc. (see variable-editor.h for list).
  Many functions are moved from variable_editor, often using findChild() or
  findChildren() for convenience rather than m_widget_stack or m_table_widget.
  (variable_editor::variable_editor): Call initializer QMainWindow() and
  QToolBar() similar to what it was once like.  Also initialize
  m_current_focus_vname and m_hovered_focus_vname to "".  Add toolbar to m_main.
  Use an MDI area for central widget of m_main.  Remove all tab bar layout.
  (variable_editor::edit_variable): Use findChild() to look for
  variable_dock_widget of requested name.  If so, bring into focus and return.
  If continue, create new variable_dock_widget "page" and place in m_main.
  Connect all new signals/slots.  Remove unneeded layers of layout containers.
  After calling make_edit_view, set the created widget as the
  variable_dock_widget's main widget.  Supply a title to the label_dock_widget
  and set the text of that title to the variable's name.  Also connect a
  signal/slot so that the text and background can be updated for highlighting
  which variable has focus.  Move other things around a bit, but still pretty
  much the same.  Use findChildren to enable the toolbar on the first variable,
  similar to the way it was previous, but could always enable rather than test.
  (variable_editor::make_edit_view): Set the table focus proxy to the container
  so that focusing one is like focusing the other.  Make it strong focus as
  well.
  (variable_editor::has_focus): Removed.  Not needed, setting focus proxy
  appears to obviate this.
  (variable_editor::make_disp_view): Removed.  Not needed.
  (variable_editor::notice_settings): Took out shortcut_manager shortcuts
  because these tab actions no longer needed.  Also, these shortcuts can
  possibly be handled via the QAction shortcut key.
  (variable_editor::add_tool_bar_button): Added.  Mimic the
  m_tool_bar->addAction() construct used in construct_tool_bar().
  (variable_editor::construct_tool_bar): Utilize add_tool_bar_button() to place
  ReturnFocusToolButton objects in the QToolbar.  Give an example of adding a
  short-cut key to the Save QAction.  Use a HoverToolButton with a
  ReturnFocusMenu object similar to existing construction.  At the end, utilize
  findChildren() to loop through all the objects needing connections to slots
  rather than program individually.

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
@@ -28,11 +28,9 @@ along with Octave; see the file COPYING.
 #include <QMainWindow>
 #include <QThread>
 #include <QTabWidget>
-#include <QMdiArea>
 #include <QStatusBar>
 #include <QToolBar>
 #include <QQueue>
-#include <QMdiSubWindow>
 #include <QCloseEvent>
 #include <QToolButton>
 #include <QComboBox>
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
@@ -37,37 +37,16 @@ along with Octave; see the file COPYING.
 #include "octave-dock-widget.h"
 
 
-octave_dock_widget::octave_dock_widget (QWidget *p)
+label_dock_widget::label_dock_widget (QWidget *p)
   : QDockWidget (p)
 {
-  m_parent = static_cast<QMainWindow *> (p);     // store main window
-  m_floating = false;
-  m_predecessor_widget = nullptr;
-
-  connect (this, SIGNAL (visibilityChanged (bool)),
-           this, SLOT (handle_visibility_changed (bool)));
-
-  connect (p, SIGNAL (settings_changed (const QSettings*)),
-           this, SLOT (handle_settings (const QSettings*)));
-
-  connect (p, SIGNAL (active_dock_changed (octave_dock_widget*,
-                                           octave_dock_widget*)),
-           this, SLOT (handle_active_dock_changed (octave_dock_widget*,
-                                                   octave_dock_widget*)));
-
   QStyle *st = style ();
   m_icon_size = 0.75*st->pixelMetric (QStyle::PM_SmallIconSize);
 
-  // add an extra title bar that persists when floating
-
-  setFeatures (QDockWidget::DockWidgetMovable); // not floatable or closeable
-
   // the custom (extra) title bar of the widget
   m_dock_action = new QAction
                    (QIcon (":/actions/icons/widget-undock.png"), "", this);
-  m_dock_action-> setToolTip (tr ("Undock widget"));
-  connect (m_dock_action, SIGNAL (triggered (bool)),
-           this, SLOT (change_floating (bool)));
+  m_dock_action->setToolTip (tr ("Undock widget"));
   m_dock_button = new QToolButton (this);
   m_dock_button->setDefaultAction (m_dock_action);
   m_dock_button->setFocusPolicy (Qt::NoFocus);
@@ -75,16 +54,15 @@ octave_dock_widget::octave_dock_widget (
 
   m_close_action = new QAction
                    (QIcon (":/actions/icons/widget-close.png"), "", this);
-  m_close_action-> setToolTip (tr ("Hide widget"));
-  connect (m_close_action, SIGNAL (triggered (bool)),
-           this, SLOT (change_visibility (bool)));
+  m_close_action->setToolTip (tr ("Close widget"));
   m_close_button = new QToolButton (this);
   m_close_button->setDefaultAction (m_close_action);
   m_close_button->setFocusPolicy (Qt::NoFocus);
   m_close_button->setIconSize (QSize (m_icon_size,m_icon_size));
 
-  m_icon_color = "";
-  m_title_3d = 50;
+  QString css_button = QString ("QToolButton {background: transparent; border: 0px;}");
+  m_dock_button->setStyleSheet (css_button);
+  m_close_button->setStyleSheet (css_button);
 
   QHBoxLayout *h_layout = new QHBoxLayout ();
   h_layout->addStretch (100);
@@ -106,6 +84,50 @@ octave_dock_widget::octave_dock_widget (
            this, SLOT (selectAll ()));
   // undo handling
   connect (p, SIGNAL (undo_signal ()), this, SLOT (do_undo ()));
+}
+
+// set the title in the dockwidgets title bar
+void
+label_dock_widget::set_title (const QString& title)
+{
+  QHBoxLayout *h_layout
+    = static_cast<QHBoxLayout *> (titleBarWidget ()->layout ());
+  QLabel *label = new QLabel (title);
+  label->setStyleSheet ("background: transparent;");
+  h_layout->insertWidget (0,label);
+  setWindowTitle (title);
+}
+
+
+octave_dock_widget::octave_dock_widget (QWidget *p)
+  : label_dock_widget (p)
+{
+  m_parent = static_cast<QMainWindow *> (p);     // store main window
+  m_floating = false;
+  m_predecessor_widget = nullptr;
+
+  connect (this, SIGNAL (visibilityChanged (bool)),
+           this, SLOT (handle_visibility_changed (bool)));
+
+  connect (p, SIGNAL (settings_changed (const QSettings*)),
+           this, SLOT (handle_settings (const QSettings*)));
+
+  connect (p, SIGNAL (active_dock_changed (octave_dock_widget*,
+                                           octave_dock_widget*)),
+           this, SLOT (handle_active_dock_changed (octave_dock_widget*,
+                                                   octave_dock_widget*)));
+
+  setFeatures (QDockWidget::DockWidgetMovable); // not floatable or closeable
+
+  connect (m_dock_action, SIGNAL (triggered (bool)),
+           this, SLOT (change_floating (bool)));
+  connect (m_close_action, SIGNAL (triggered (bool)),
+           this, SLOT (change_visibility (bool)));
+
+  m_close_action->setToolTip (tr ("Hide widget"));
+
+  m_icon_color = "";
+  m_title_3d = 50;
 
   installEventFilter (this);
 
@@ -192,18 +214,6 @@ octave_dock_widget::make_widget (bool do
   m_floating = false;
 }
 
-// set the title in the dockwidgets title bar
-void
-octave_dock_widget::set_title (const QString& title)
-{
-  QHBoxLayout *h_layout
-    = static_cast<QHBoxLayout *> (titleBarWidget ()->layout ());
-  QLabel *label = new QLabel (title);
-  label->setStyleSheet ("background: transparent;");
-  h_layout->insertWidget (0,label);
-  setWindowTitle (title);
-}
-
 // set the widget which previously had focus when tabified
 void
 octave_dock_widget::set_predecessor_widget (octave_dock_widget *prev_widget)
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
@@ -30,7 +30,41 @@ along with Octave; see the file COPYING.
 #include <QToolButton>
 #include <QMouseEvent>
 
-class octave_dock_widget : public QDockWidget
+// The few decoration items common to both main window and variable editor.
+class label_dock_widget : public QDockWidget
+{
+  Q_OBJECT
+
+public:
+
+  label_dock_widget (QWidget *p = nullptr);
+
+  void set_title (const QString&);
+
+protected slots:
+
+  //! Slots to handle copy & paste.
+  //!@{
+  virtual void copyClipboard (void) {  }
+  virtual void pasteClipboard (void) {  }
+  virtual void selectAll (void) {  }
+  //!@}
+
+  //! Slot to handle undo.
+
+  virtual void do_undo (void) {  }
+
+protected:
+
+  int m_icon_size;
+  QWidget *m_title_widget;
+  QToolButton *m_dock_button;
+  QToolButton *m_close_button;
+  QAction *m_dock_action;
+  QAction *m_close_action;
+};
+
+class octave_dock_widget : public label_dock_widget
 {
   Q_OBJECT
 
@@ -44,7 +78,6 @@ public:
 
   void make_window (void);
   void make_widget (bool dock=true);
-  void set_title (const QString&);
   void set_predecessor_widget (octave_dock_widget *prev_widget);
 
 signals:
@@ -98,17 +131,6 @@ protected slots:
       emit active_changed (true);
   }
 
-  //! Slots to handle copy & paste.
-  //!@{
-  virtual void copyClipboard (void) {  }
-  virtual void pasteClipboard (void) {  }
-  virtual void selectAll (void) {  }
-  //!@}
-
-  //! Slot to handle undo.
-
-  virtual void do_undo (void) {  }
-
   //! Event filter for double clicks into the window decoration elements.
 
   bool eventFilter (QObject *obj, QEvent *e);
@@ -130,7 +152,6 @@ private:
   bool m_floating;
   bool m_custom_style;
   int m_title_3d;
-  int m_icon_size;
   QColor m_bg_color;
   QColor m_bg_color_active;
   QColor m_fg_color;
@@ -139,11 +160,6 @@ private:
   QString m_icon_color_active;
   octave_dock_widget *m_predecessor_widget;
 
-  QWidget *m_title_widget;
-  QToolButton *m_dock_button;
-  QToolButton *m_close_button;
-  QAction *m_dock_action;
-  QAction *m_close_action;
 };
 
 #endif
diff --git a/libgui/src/variable-editor-model.cc b/libgui/src/variable-editor-model.cc
--- a/libgui/src/variable-editor-model.cc
+++ b/libgui/src/variable-editor-model.cc
@@ -33,6 +33,7 @@ along with Octave; see the file COPYING.
 #include <QMap>
 #include <QMessageBox>
 #include <QString>
+#include <QTableView>
 
 #include "octave-qt-link.h"
 #include "variable-editor-model.h"
@@ -901,7 +902,7 @@ bool
 variable_editor_model::setData (const QModelIndex& idx,
                                 const QVariant& v_user_input, int role)
 {
-  if (role != Qt::EditRole || v_user_input.type () != QVariant::String
+  if (role != Qt::EditRole || ! v_user_input.canConvert (QVariant::String)
       || ! idx.isValid ())
     return false;
 
@@ -1206,8 +1207,6 @@ variable_editor_model::update_data (cons
 
   emit dataChanged (QAbstractTableModel::index (0, 0),
                     QAbstractTableModel::index (new_display_rows-1, new_display_cols-1));
-
-  emit maybe_resize_columns_signal ();
 }
 
 void
@@ -1248,3 +1247,90 @@ variable_editor_model::update_descriptio
   emit description_changed (description.isEmpty ()
                             ? make_description_text () : description);
 }
+
+void
+variable_editor_model::double_click (const QModelIndex& idx)
+{
+  if (requires_sub_editor (idx))
+    {
+      QString name = QString::fromStdString(rep->name ());
+      emit edit_variable_signal (name + subscript_expression (idx),
+                                 value_at (idx));
+    }
+}
+
+void
+variable_editor_model::up (void)
+{
+  QString name = QString::fromStdString(rep->name ());
+
+  // FIXME: Is there a better way?
+
+  if (name.endsWith (')') || name.endsWith ('}'))
+    {
+      name.remove (QRegExp ("(\\(|\\{)[^({]*(\\)|\\})$"));
+      emit edit_variable_signal (name, octave_value ());
+    }
+}
+
+static QString
+idx_to_expr (int32_t from, int32_t to)
+{
+  return (from == to
+          ? QString ("%1").arg (from)
+          : QString ("%1:%2").arg (from).arg (to));
+}
+
+QList<int>
+variable_editor_model::range_selected (void)
+{
+  QList<QTableView *> viewlist = parent ()->findChildren<QTableView *> ();
+  if (viewlist.size () != 1)
+    return QList<int> ();
+
+  QItemSelectionModel *sel = viewlist.at (0)->selectionModel ();
+
+  // Return early if nothing selected.
+  if (! sel->hasSelection ())
+    return QList<int> ();
+
+  QList<QModelIndex> indices = sel->selectedIndexes ();
+
+  // FIXME: Shouldn't this be keyed to octave_idx_type?
+
+  int32_t from_row = std::numeric_limits<int32_t>::max ();
+  int32_t to_row = 0;
+  int32_t from_col = std::numeric_limits<int32_t>::max ();
+  int32_t to_col = 0;
+
+  for (const auto& idx : indices)
+    {
+      from_row = std::min (from_row, idx.row ());
+      to_row = std::max (to_row, idx.row ());
+      from_col = std::min (from_col, idx.column ());
+      to_col = std::max (to_col, idx.column ());
+    }
+
+  QVector<int> vect;
+  vect << from_row + 1 << to_row + 1 << from_col + 1 << to_col + 1;
+  QList<int> range = QList<int>::fromVector(vect);
+
+  return range;
+}
+
+QString
+variable_editor_model::selected_to_octave (void)
+{
+  QString name = QString::fromStdString(rep->name ());
+
+  QList<int> range = range_selected ();
+  if (range.isEmpty ())
+    return name;
+
+  QString rows = idx_to_expr (range.at (0), range.at (1));
+  QString cols = idx_to_expr (range.at (2), range.at (3));
+
+  // FIXME: Does cell need separate handling?  Maybe use '{.,.}'?
+
+  return QString ("%1(%2, %3)").arg (name).arg (rows).arg (cols);
+}
diff --git a/libgui/src/variable-editor-model.h b/libgui/src/variable-editor-model.h
--- a/libgui/src/variable-editor-model.h
+++ b/libgui/src/variable-editor-model.h
@@ -253,6 +253,11 @@ public:
     return rep->subscript_expression (idx);
   }
 
+  QList<int> range_selected (void);
+
+  // Convert selection to an Octave expression.
+  QString selected_to_octave (void);
+
 signals: // private
 
   void update_data_signal (const octave_value& val);
@@ -267,16 +272,24 @@ signals: // private
 
   void description_changed (const QString& description);
 
+  void edit_variable_signal (const QString& name, const octave_value& val);
+
+public slots:
+
+  void update_data (const octave_value& val);
+
+  void update_data_cache (void);
+
+  void double_click (const QModelIndex& idx);
+
+  void up (void);
+
 private slots:
 
   void data_error (const QString& msg);
 
   void user_error (const QString& title, const QString& msg);
 
-  void update_data_cache (void);
-
-  void update_data (const octave_value& val);
-
 private:
 
   base_ve_model *rep;
diff --git a/libgui/src/variable-editor.cc b/libgui/src/variable-editor.cc
--- a/libgui/src/variable-editor.cc
+++ b/libgui/src/variable-editor.cc
@@ -33,10 +33,11 @@ along with Octave; see the file COPYING.
 #include <QFileDialog>
 #include <QHeaderView>
 #include <QLabel>
+#include <QMdiArea>
 #include <QMenu>
 #include <QPalette>
+#include <QScreen>
 #include <QSignalMapper>
-#include <QStackedWidget>
 #include <QTableView>
 #include <QTextEdit>
 #include <QTabWidget>
@@ -49,129 +50,749 @@ along with Octave; see the file COPYING.
 #include "variable-editor.h"
 #include "variable-editor-model.h"
 
-static QString
-idx_to_expr (int32_t from, int32_t to)
+
+// Variable dock widget
+
+variable_dock_widget::variable_dock_widget (QWidget *p)
+  : label_dock_widget (p), m_initial_float (true)
 {
-  return (from == to
-          ? QString ("%1").arg (from + 1)
-          : QString ("%1:%2").arg (from + 1).arg (to + 1));
+  setFocusPolicy (Qt::StrongFocus);
+  // This controls whether the variable_dock_widgets are deleted
+  // or hidden when clicking the upper right X icon.  (Look for
+  // the checkmark of the variable_editor's context menu.)
+  // Could make this an option, or perhaps add a drop-down menu
+  // in place of X "close" icon.
+//  setAttribute (Qt::WA_DeleteOnClose);
+
+  connect (m_dock_action, SIGNAL (triggered (bool)),
+           this, SLOT (change_floating (bool)));
+  connect (m_close_action, SIGNAL (triggered (bool)),
+           this, SLOT (change_existence (bool)));
+  connect (this, SIGNAL (topLevelChanged(bool)),
+           this, SLOT (toplevel_change (bool)));
 }
 
-QTableView *
-var_editor_tab::get_edit_view (void) const
+// slot for (un)dock action
+void
+variable_dock_widget::change_floating (bool)
 {
-  return (m_edit_view_idx == m_widget_stack->currentIndex ()
-          ? qobject_cast<QTableView *> (m_widget_stack->widget (m_edit_view_idx))
-          : nullptr);
+  setFloating (! isFloating ());
 }
 
-QTextEdit *
-var_editor_tab::get_disp_view (void) const
+// slot for hiding the widget
+void
+variable_dock_widget::change_existence (bool)
 {
-  return (m_disp_view_idx == m_widget_stack->currentIndex ()
-          ? qobject_cast<QTextEdit *> (m_widget_stack->widget (m_disp_view_idx))
-          : nullptr);
+  close ();
 }
 
 void
-var_editor_tab::set_edit_view (QTableView *edit_view)
+variable_dock_widget::toplevel_change (bool toplevel)
 {
-  m_edit_view_idx = m_widget_stack->addWidget (edit_view);
+  if (toplevel)
+    {
+      m_dock_action->setIcon (QIcon (":/actions/icons/widget-dock.png"));
+      m_dock_action->setToolTip (tr ("Dock widget"));
+
+      // Make initial size expanded very large for "magnified" viewing
+      if (m_initial_float)
+        {
+          QScreen *pscreen = QGuiApplication::primaryScreen ();
+          QRect rect (0, 0, 0, 0);
+          rect = pscreen->availableGeometry ();
+          rect = QRect (rect.x () + 5, rect.y () + 5,
+                        rect.width () - 10, rect.height () - 10);
+          setGeometry (rect);
+          m_initial_float = false;
+        }
+
+      setFocus (Qt::OtherFocusReason);
+      activateWindow();
+    }
+  else
+    {
+      m_dock_action->setIcon (QIcon (":/actions/icons/widget-undock.png"));
+      m_dock_action->setToolTip (tr ("Undock widget"));
+
+      setFocus (Qt::OtherFocusReason);
+    }
 }
 
 void
-var_editor_tab::set_disp_view (QTextEdit *disp_view)
+variable_dock_widget::focusInEvent(QFocusEvent *)
 {
-  m_disp_view_idx = m_widget_stack->addWidget (disp_view);
-}
+  QLabel *label = titleBarWidget ()->findChild<QLabel *> ();
+  if (label != nullptr)
+    label->setStyleSheet ("QLabel {background: skyblue;}");
 
-
-bool
-var_editor_tab::has_focus (void) const
-{
-  QTableView *edit_view = get_edit_view ();
-  QTextEdit *disp_view = get_disp_view ();
-
-  return ((disp_view && disp_view->hasFocus ())
-          || (edit_view && edit_view->hasFocus ()));
+  emit variable_focused_signal (objectName ());
 }
 
 void
-var_editor_tab::set_editable (bool editable)
+variable_dock_widget::focusOutEvent(QFocusEvent *)
 {
-  int idx = (editable ? m_edit_view_idx : m_disp_view_idx);
+  QLabel *label = titleBarWidget ()->findChild<QLabel *> ();
+  if (label != NULL)
+    label->setStyleSheet ("QLabel {background: transparent;}");
+}
 
-  m_widget_stack->setCurrentIndex (idx);
+void
+variable_dock_widget::closeEvent (QCloseEvent *e)
+{
+  QDockWidget::closeEvent (e);
+}
 
-  if (! editable)
+void
+variable_dock_widget::clearContent (void)
+{
+  if (! hasFocus ())
+    return;
+
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (viewlist.size () != 1 || modellist.size () != 1)
+    return;
+
+  QTableView *view = viewlist.at (0);
+  QItemSelectionModel *sel = view->selectionModel ();
+  QList<QModelIndex> indices = sel->selectedIndexes ();
+
+  // FIXME: Use [] for empty cells?
+
+  for (const auto& idx : indices)
+    modellist.at (0)->clear_content (idx);
+}
+
+void
+variable_dock_widget::copyClipboard (void)
+{
+  if (! hasFocus ())
+    return;
+
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  if (viewlist.size () != 1)
+    return;
+
+  QTableView *view = viewlist.at (0);
+  QAbstractItemModel *model = view->model ();
+  QItemSelectionModel *sel = view->selectionModel ();
+  QList<QModelIndex> indices = sel->selectedIndexes ();
+
+  qSort (indices);
+  if (indices.isEmpty ())
+    return;
+
+  // Convert selected items into TSV format and copy that.
+  // Spreadsheet tools should understand that.
+
+  QModelIndex previous = indices.first ();
+  QString copy = model->data (previous).toString ();
+  indices.removeFirst ();
+  foreach (QModelIndex idx, indices)
     {
-      QTextEdit *viewer = get_disp_view ();
+      copy.push_back (previous.row () != idx.row () ? '\n' : '\t');
+      copy.append (model->data (idx).toString ());
+      previous = idx;
+    }
 
-      QVariant v_data = m_model->data ();
+  QClipboard *clipboard = QApplication::clipboard ();
+  clipboard->setText (copy);
+}
 
-      QString str = v_data.toString ();
+void
+variable_dock_widget::pasteClipboard (void)
+{
+  if (! hasFocus ())
+    return;
 
-      viewer->setPlainText (str);
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (viewlist.size () != 1 || modellist.size () != 1)
+    return;
+
+  QTableView *view = viewlist.at (0);
+  QItemSelectionModel *sel = view->selectionModel ();
+  QList<QModelIndex> indices = sel->selectedIndexes ();
+  variable_editor_model *model = modellist.at (0);
+
+  QClipboard *clipboard = QApplication::clipboard ();
+  QString text = clipboard->text ();
+
+  if (indices.isEmpty ())
+    {
+      if (view->size () == QSize (1,1))
+        model->setData (view->model ()->index (0,0), text.toDouble ());
+      else if (view->size () == QSize (0,0))
+        {
+          model->insertColumn (0);
+          model->insertRow (0);
+          model->setData (view->model ()->index (0,0), text.toDouble ());
+        }
+    }
+  else
+    {
+      QStringList cells = text.split(QRegExp("\n|\r\n|\r"));
+      int clen = cells.size ();
+      for (int i = 0; i < indices.size (); i++)
+        model->setData (indices[i], cells.at (i % clen).toDouble ());
     }
 }
 
-// Functions for reimplemented tab widget.
+void variable_dock_widget::pasteTableClipboard (void)
+{
+  if (! hasFocus ())
+    return;
 
-var_editor_tab_widget::var_editor_tab_widget (QWidget *p)
-  : QTabWidget (p)
-{
-  tab_bar *bar = new tab_bar (this);
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (viewlist.size () != 1 || modellist.size () != 1)
+    return;
 
-  connect (bar, SIGNAL (close_current_tab_signal (bool)),
-           p->parent (), SLOT (request_close_tab (bool)));
+  QTableView *view = viewlist.at (0);
+  QItemSelectionModel *sel = view->selectionModel ();
+  QList<QModelIndex> indices = sel->selectedIndexes ();
+  variable_editor_model *model = modellist.at (0);
 
-  this->setTabBar (bar);
+  QClipboard *clipboard = QApplication::clipboard ();
+  QString text = clipboard->text ();
 
-  setTabsClosable (true);
-  setMovable (true);
+  QPoint start, end;
+
+  QPoint tabsize = QPoint (model->rowCount (), model->columnCount ());
+
+  if (indices.isEmpty ())
+    {
+      start = QPoint (0,0);
+      end = tabsize;
+    }
+  else if (indices.size () == 1)
+    {
+      start = QPoint (indices[0].row (), indices[0].column ());
+      end = tabsize;
+    }
+  else
+    {
+      end = QPoint (0,0);
+      start = tabsize;
+
+      for (int i = 0; i < indices.size (); i++)
+        {
+          if (indices[i].column () < start.y ())
+            start.setY (indices[i].column ());
+
+          if (indices[i].column () > end.y ())
+            end.setY (indices[i].column ());
+
+          if (indices[i].row () < start.x ())
+            start.setX (indices[i].column ());
+
+          if (indices[i].row () > end.x ())
+            end.setX (indices[i].column ());
+        }
+    }
+
+  int rownum = 0;
+  int colnum = 0;
+
+  QStringList rows = text.split ('\n');
+  for (const auto& row : rows)
+    {
+      if (rownum > end.x () - start.x ())
+        continue;
+
+      QStringList cols = row.split ('\t');
+      if (cols.isEmpty ())
+        continue;
+
+      for (const auto& col : cols)
+        {
+          if (col.isEmpty ())
+            continue;
+          if (colnum > end.y () - start.y () )
+            continue;
+
+          model->setData (model->index (rownum + start.x (),
+                                        colnum + start.y ()),
+                          QVariant (col));
+
+          colnum++;
+        }
+
+      colnum = 0;
+      rownum++;
+    }
 }
 
-tab_bar *
-var_editor_tab_widget::get_tab_bar (void) const
+void
+variable_dock_widget::transposeContent (void)
 {
-  return qobject_cast<tab_bar *> (tabBar ());
+  if (! hasFocus ())
+    return;
+
+  QString name = objectName ();
+  emit command_requested (QString ("%1 = %1';").arg (name));
 }
 
-bool
-var_editor_tab_widget::current_tab_has_focus (void) const
+void
+variable_dock_widget::createContextMenu (const QPoint& qpos)
 {
-  var_editor_tab *tab
-    = qobject_cast<var_editor_tab *> (widget (currentIndex ()));
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (viewlist.size () != 1 || modellist.size () != 1)
+    return;
 
-  return tab->has_focus ();
+  QTableView *view = viewlist.at (0);
+  QModelIndex index = view->indexAt (qpos);
+
+  if (index.isValid ())
+    {
+      QMenu *menu = new QMenu (this);
+
+      menu->addAction (resource_manager::icon ("edit-cut"),
+                       tr ("Cut"), this, SLOT (cutClipboard ()));
+
+      menu->addAction (resource_manager::icon ("edit-copy"),
+                       tr ("Copy"), this, SLOT (copyClipboard ()));
+
+      menu->addAction (resource_manager::icon ("edit-paste"),
+                       tr ("Paste"), this, SLOT (pasteClipboard ()));
+
+      // FIXME: Need different icon for paste table separate from paste?
+
+      menu->addAction (resource_manager::icon ("edit-paste"),
+                       tr ("Paste Table"), this,
+                       SLOT (pasteTableClipboard ()));
+
+      menu->addSeparator ();
+
+      menu->addAction (resource_manager::icon ("edit-delete"),
+                       tr ("Clear"), this, SLOT (clearContent ()));
+
+      menu->addAction (resource_manager::icon ("document-new"),
+                       tr ("Variable from Selection"), this,
+                       SLOT (createVariable ()));
+
+      // FIXME: addAction for sort?
+      // FIXME: Add icon for transpose.
+
+      menu->addAction (tr ("Transpose"), this, SLOT (transposeContent ()));
+
+      QItemSelectionModel *sel = view->selectionModel ();
+
+      QList<QModelIndex> indices = sel->selectedIndexes ();
+
+      if (! indices.isEmpty ())
+        {
+          menu->addSeparator ();
+
+          QSignalMapper *plot_mapper = new QSignalMapper (menu);
+
+          plot_mapper->setMapping
+            (menu->addAction ("plot", plot_mapper, SLOT (map ())),
+             "figure (); plot (%1);");
+
+          plot_mapper->setMapping
+            (menu->addAction ("bar", plot_mapper, SLOT (map ())),
+             "figure (); bar (%1);");
+
+          plot_mapper->setMapping
+            (menu->addAction ("stem", plot_mapper, SLOT (map ())),
+             "figure (); stem (%1);");
+
+          plot_mapper->setMapping
+            (menu->addAction ("stairs", plot_mapper, SLOT (map ())),
+             "figure (); stairs (%1);");
+
+          plot_mapper->setMapping
+            (menu->addAction ("area", plot_mapper, SLOT (map ())),
+             "figure (); area (%1);");
+
+          plot_mapper->setMapping
+            (menu->addAction ("pie", plot_mapper, SLOT (map ())),
+             "figure (); pie (%1);");
+
+          plot_mapper->setMapping
+            (menu->addAction ("hist", plot_mapper, SLOT (map ())),
+             "figure (); hist (%1);");
+
+          connect (plot_mapper, SIGNAL (mapped (const QString&)),
+                   this, SLOT (relay_selected_command (const QString&)));
+        }
+
+      menu->exec (view->mapToGlobal (qpos));
+    }
 }
 
-QTableView *
-var_editor_tab_widget::get_edit_view (void) const
+void
+variable_dock_widget::createColumnMenu (const QPoint& pt)
 {
-  var_editor_tab *tab
-    = qobject_cast<var_editor_tab *> (widget (currentIndex ()));
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (viewlist.size () != 1 || modellist.size() != 1)
+    return;
 
-  return tab->get_edit_view ();
+  QTableView *view = viewlist.at (0);
+  variable_editor_model *model = modellist.at (0);
+
+  int index = view->horizontalHeader ()->logicalIndexAt (pt);
+
+  if (index < 0 || index > view->model ()->columnCount ())
+    return;
+
+  QList<int> coords = model->range_selected ();
+
+  bool nothingSelected = coords.isEmpty ();
+
+  bool whole_columns_selected
+    =  (nothingSelected
+        ? false
+        : (coords[0] == 1 && coords[1] == view->model ()->rowCount ()));
+
+  bool current_column_selected
+    = nothingSelected ? false : (coords[2] <= index+1 && coords[3] > index);
+
+  int column_selection_count
+    = nothingSelected ? 0 : (coords[3] - coords[2] + 1);
+
+  if (! whole_columns_selected || ! current_column_selected)
+    {
+      view->selectColumn (index);
+      column_selection_count = 1;
+      current_column_selected = true;
+      whole_columns_selected = true;
+    }
+
+  QString column_string
+    = tr (column_selection_count > 1 ? " columns" : " column");
+
+  QMenu *menu = new QMenu (this);
+
+  menu->addAction (resource_manager::icon ("edit-cut"),
+                   tr ("Cut") + column_string,
+                   this, SLOT (cutClipboard ()));
+
+  menu->addAction (resource_manager::icon ("edit-copy"),
+                   tr ("Copy") + column_string,
+                   this, SLOT (copyClipboard ()));
+
+  menu->addAction (resource_manager::icon ("edit-paste"),
+                   tr ("Paste"),
+                   this, SLOT (pasteClipboard ()));
+
+  // FIXME: Different icon for Paste Table?
+
+  menu->addAction (resource_manager::icon ("edit-paste"),
+                   tr ("Paste Table"),
+                   this, SLOT (pasteTableClipboard ()));
+
+  menu->addSeparator ();
+
+  menu->addAction (resource_manager::icon ("edit-delete"),
+                   tr ("Clear") + column_string,
+                   this, SLOT (clearContent ()));
+
+  menu->addAction (resource_manager::icon ("edit-delete"),
+                   tr ("Delete") + column_string,
+                   this, SLOT (delete_selected ()));
+
+  menu->addAction (resource_manager::icon ("document-new"),
+                   tr ("Variable from Selection"),
+                   this, SLOT (createVariable ()));
+
+  menu->addSeparator ();
+
+  QSignalMapper *plot_mapper = new QSignalMapper (menu);
+
+  plot_mapper->setMapping
+    (menu->addAction ("plot", plot_mapper, SLOT (map ())),
+     "figure (); plot (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("bar", plot_mapper, SLOT (map ())),
+     "figure (); bar (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("stem", plot_mapper, SLOT (map ())),
+     "figure (); stem (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("stairs", plot_mapper, SLOT (map ())),
+     "figure (); stairs (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("area", plot_mapper, SLOT (map ())),
+     "figure (); area (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("pie", plot_mapper, SLOT (map ())),
+     "figure (); pie (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("hist", plot_mapper, SLOT (map ())),
+     "figure (); hist (%1);");
+
+  connect (plot_mapper, SIGNAL (mapped (const QString&)),
+           this, SLOT (relay_selected_command (const QString&)));
+
+  QPoint menupos = pt;
+  menupos.setY (view->horizontalHeader ()->height ());
+
+  menu->exec (view->mapToGlobal (menupos));
 }
 
-QTextEdit *
-var_editor_tab_widget::get_disp_view (void) const
+void
+variable_dock_widget::createRowMenu (const QPoint& pt)
 {
-  var_editor_tab *tab
-    = qobject_cast<var_editor_tab *> (widget (currentIndex ()));
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (viewlist.size () != 1 || modellist.size() != 1)
+    return;
 
-  return tab->get_disp_view ();
+  QTableView *view = viewlist.at (0);
+  variable_editor_model *model = modellist.at (0);
+
+  int index = view->verticalHeader ()->logicalIndexAt (pt);
+
+  if (index < 0 || index > view->model ()->columnCount ())
+    return;
+
+  QList<int> coords = model->range_selected ();
+
+  bool nothingSelected = coords.isEmpty ();
+
+  bool whole_rows_selected
+    = (nothingSelected
+       ? false
+       : (coords[2] == 1 && coords[3] == view->model ()->columnCount ()));
+
+  bool current_row_selected
+    = (nothingSelected ? false : (coords[0] <= index+1 && coords[1] > index));
+
+  int rowselection_count = nothingSelected ? 0 : (coords[3] - coords[2] + 1);
+
+  if (! whole_rows_selected || ! current_row_selected)
+    {
+      view->selectRow (index);
+      rowselection_count = 1;
+      current_row_selected = true;
+      whole_rows_selected = true;
+    }
+
+  QString row_string = tr (rowselection_count > 1 ? " rows" : " row");
+
+  QMenu *menu = new QMenu (this);
+
+  menu->addAction (resource_manager::icon ("edit-cut"),
+                   tr ("Cut") + row_string,
+                   this, SLOT (cutClipboard ()));
+
+  menu->addAction (resource_manager::icon ("edit-copy"),
+                   tr ("Copy") + row_string,
+                   this, SLOT (copyClipboard ()));
+
+  menu->addAction (resource_manager::icon ("edit-paste"),
+                   tr ("Paste"),
+                   this, SLOT (pasteClipboard ()));
+
+  // FIXME: Better icon for Paste Table?
+
+  menu->addAction (resource_manager::icon ("edit-paste"),
+                   tr ("Paste Table"),
+                   this, SLOT (pasteTableClipboard ()));
+
+  menu->addSeparator ();
+
+  menu->addAction (resource_manager::icon ("edit-delete"),
+                   tr ("Clear") + row_string,
+                   this, SLOT (clearContent ()));
+
+  menu->addAction (resource_manager::icon ("edit-delete"),
+                   tr ("Delete") + row_string,
+                   this, SLOT (delete_selected ()));
+
+  menu->addAction (resource_manager::icon ("document-new"),
+                   tr ("Variable from Selection"),
+                   this, SLOT (createVariable ()));
+
+  menu->addSeparator ();
+
+  QSignalMapper *plot_mapper = new QSignalMapper (menu);
+
+  plot_mapper->setMapping
+    (menu->addAction ("plot", plot_mapper, SLOT (map ())),
+     "figure (); plot (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("bar", plot_mapper, SLOT (map ())),
+     "figure (); bar (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("stem", plot_mapper, SLOT (map ())),
+     "figure (); stem (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("stairs", plot_mapper, SLOT (map ())),
+     "figure (); stairs (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("area", plot_mapper, SLOT (map ())),
+     "figure (); area (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("pie", plot_mapper, SLOT (map ())),
+     "figure (); pie (%1);");
+
+  plot_mapper->setMapping
+    (menu->addAction ("hist", plot_mapper, SLOT (map ())),
+     "figure (); hist (%1);");
+
+  connect (plot_mapper, SIGNAL (mapped (const QString&)),
+           this, SLOT (relay_selected_command (const QString&)));
+
+  QPoint menupos = pt;
+  menupos.setX (view->verticalHeader ()->width ());
+
+  // FIXME: What was the intent here?
+  // setY (view->verticalHeader ()->sectionPosition (index+1) +
+  //             view->verticalHeader ()->sectionSize (index));
+
+  menu->exec (view->mapToGlobal (menupos));
+}
+
+void
+variable_dock_widget::save (void)
+{
+  if (! hasFocus ())
+    return;
+
+  QString name = objectName ();
+  QString file
+    = QFileDialog::getSaveFileName (this,
+                                    tr ("Save Variable %1 As").arg (name),
+                                     /* Should determine extension from save_default_options */
+                                    tr ("./%1.txt").arg (name),
+                                    0, 0,
+                                    QFileDialog::DontUseNativeDialog);
+
+  // FIXME: Type? binary, float-binary, ascii, text, hdf5, matlab format?
+  // FIXME: Call octave_value::save_* directly?
+
+  if (! file.isEmpty ())
+    emit command_requested (QString ("save (\"%1\", \"%2\");")
+                            .arg (file)
+                            .arg (name));
+}
+
+void
+variable_dock_widget::delete_selected (void)
+{
+  if (! hasFocus ())
+    return;
+
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (modellist.size() != 1)
+    return;
+
+  variable_editor_model *model = modellist.at (0);
+
+  QList<int> coords = model->range_selected ();
+
+  if (coords.isEmpty ())
+    return;
+
+  bool whole_columns_selected
+    = coords[0] == 1 && coords[1] == model->rowCount ();
+
+  bool whole_rows_selected
+    = coords[2] == 1 && coords[3] == model->columnCount ();
+
+  // Must be deleting whole columns or whole rows, and not the whole thing.
+
+  if (whole_columns_selected == whole_rows_selected)
+    return;
+
+  if (whole_rows_selected)
+    model->removeRows (coords[0], coords[1] - coords[0]);
+
+  if (whole_columns_selected)
+    model->removeColumns (coords[2], coords[3] - coords[2]);
+}
+
+void
+variable_dock_widget::selected_command_requested (const QString& cmd)
+{
+  if (! hasFocus ())
+    return;
+
+  QList<variable_editor_model *> modellist = findChildren<variable_editor_model *> ();
+  if (modellist.size() != 1)
+    return;
+
+  variable_editor_model *model = modellist.at (0);
+  emit command_requested (cmd.arg (model->selected_to_octave ()));
+}
+
+
+HoverToolButton::HoverToolButton (QWidget *parent)
+  : QToolButton (parent)
+{
+  installEventFilter (this);
+}
+
+bool HoverToolButton::eventFilter (QObject *obj, QEvent *ev)
+{
+  if (ev->type () == QEvent::HoverEnter)
+    emit hovered_signal ();
+
+  return QToolButton::eventFilter (obj, ev);
+}
+
+ReturnFocusToolButton::ReturnFocusToolButton (QWidget *parent)
+  : HoverToolButton (parent)
+{
+  installEventFilter (this);
+}
+
+bool ReturnFocusToolButton::eventFilter (QObject *obj, QEvent *ev)
+{
+
+  if (ev->type () == QEvent::MouseButtonRelease && isDown ())
+    {
+      emit about_to_activate ();
+
+      setDown (false);
+      QAction *action = defaultAction ();
+      if (action != nullptr)
+        action->activate (QAction::Trigger);
+
+      return true;
+    }
+
+  return HoverToolButton::eventFilter (obj, ev);
+}
+
+ReturnFocusMenu::ReturnFocusMenu (QWidget *parent)
+  : QMenu (parent)
+{
+  installEventFilter (this);
+}
+
+bool ReturnFocusMenu::eventFilter (QObject *obj, QEvent *ev)
+{
+  if (ev->type () == QEvent::MouseButtonRelease && underMouse ())
+    {
+      emit about_to_activate ();
+    }
+
+  return QMenu::eventFilter (obj, ev);
 }
 
 // Variable editor.
 
 variable_editor::variable_editor (QWidget *p)
   : octave_dock_widget (p),
-    m_container (new QWidget (this)),
-    m_tool_bar (new QToolBar ("", m_container)),
-    m_tab_widget (new var_editor_tab_widget (m_container)),
-    m_tab_bar (m_tab_widget->get_tab_bar ()),
+    m_main (new QMainWindow ()),
+    m_tool_bar (new QToolBar (m_main)),
     m_default_width (30),
     m_default_height (100),
     m_add_font_height (0),
@@ -181,61 +802,40 @@ variable_editor::variable_editor (QWidge
     m_font (),
     m_sel_font (),
     m_table_colors (),
-    m_close_action (nullptr),
-    m_close_others_action (nullptr),
-    m_close_all_action (nullptr)
+    m_current_focus_vname (""),
+    m_hovered_focus_vname ("")
 {
   setObjectName ("variable_editor");
   set_title (tr ("Variable Editor"));
   setStatusTip (tr ("Edit variables."));
   setWindowIcon (QIcon (":/actions/icons/logo.png"));
+  setFocusPolicy (Qt::NoFocus);
+  setAttribute (Qt::WA_AlwaysShowToolTips);
+
+  // Tool Bar.
 
   construct_tool_bar ();
+  m_main->addToolBar (m_tool_bar);
+  m_main->setFocusPolicy (Qt::NoFocus);
+  setFocusPolicy (Qt::NoFocus);
 
   // Colors.
 
   for (int i = 0; i < resource_manager::varedit_color_chars ().length (); i++)
     m_table_colors.append (QColor (Qt::white));
 
-  // Tab Widget.
+  // Use an MDI area that is shrunk to nothing as the central widget.
+  // Future feature might be to switch to MDI mode in which the dock
+  // area is shrunk to nothing and the widgets live in the MDI window.
 
-  connect (m_tab_widget, SIGNAL (tabCloseRequested (int)),
-           this, SLOT (closeTab (int)));
+  QMdiArea *central_mdiarea = new QMdiArea (m_main);
+  central_mdiarea->setMinimumSize (QSize (0, 0));
+  central_mdiarea->setMaximumSize (QSize (0, 0));
+  central_mdiarea->resize (QSize (0, 0));
+  m_main->setCentralWidget (central_mdiarea);
 
-  // Tab bar.
-
-  m_close_action
-    = add_action (m_tab_bar->get_context_menu (),
-                  resource_manager::icon ("window-close",false),
-                  tr ("&Close"),
-                  SLOT (request_close_tab (bool)));
-
-  m_close_others_action
-    = add_action (m_tab_bar->get_context_menu (),
-                  resource_manager::icon ("window-close",false),
-                  tr ("Close &Other Tabs"),
-                  SLOT (request_close_other_tabs (bool)));
-
-  m_close_all_action
-    = add_action (m_tab_bar->get_context_menu (),
-                  resource_manager::icon ("window-close",false),
-                  tr ("Close &All Tabs"),
-                  SLOT (request_close_all_tabs (bool)));
-
-  enable_actions ();
-
-  // Layout the widgets vertically with the toolbar on top.
-
-  QVBoxLayout *vbox_layout = new QVBoxLayout ();
-
-  vbox_layout->setSpacing (0);
-  vbox_layout->addWidget (m_tool_bar);
-  vbox_layout->addWidget (m_tab_widget);
-  vbox_layout->setMargin (1);
-
-  m_container->setLayout (vbox_layout);
-
-  setWidget (m_container);
+  m_main->setParent (this);
+  setWidget (m_main);
 
   connect (this, SIGNAL (command_requested (const QString&)),
            p, SLOT (execute_command_in_terminal (const QString&)));
@@ -263,48 +863,6 @@ variable_editor::add_action (QMenu *menu
   return a;
 }
 
-// Slot for the close tab action.
-
-void
-variable_editor::request_close_tab (bool)
-{
-  closeTab (m_tab_bar->currentIndex ());
-}
-
-// Slot for the close other tabs action.
-
-void
-variable_editor::request_close_other_tabs (bool)
-{
-  int current = m_tab_bar->currentIndex ();
-
-  for (int index = m_tab_bar->count ()-1; index >= 0; index--)
-    {
-      if (current != index)
-        closeTab (index);
-    }
-}
-
-// Slot for closing all tabs.
-
-void
-variable_editor::request_close_all_tabs (bool)
-{
-  for (int index = m_tab_bar->count ()-1; index >= 0; index--)
-    closeTab (index);
-}
-
-void
-variable_editor::enable_actions (void)
-{
-  const int count = m_tab_widget->count ();
-
-  m_tool_bar->setEnabled (count > 0);
-  m_close_action->setEnabled (count > 0);
-  m_close_all_action->setEnabled (count > 0);
-  m_close_others_action->setEnabled (count > 1);
-}
-
 void
 variable_editor::edit_variable (const QString& name, const octave_value& val)
 {
@@ -314,65 +872,92 @@ variable_editor::edit_variable (const QS
       notice_settings (settings);
     }
 
-  const int tab_count = m_tab_widget->count ();
-  for (int i = 0; i < tab_count; ++i)
+  QDockWidget *existing_qdw = m_main->findChild<QDockWidget *> (name);
+  if (existing_qdw != NULL)
     {
-      if (real_var_name (i) == name)
-        {
-          // Already open.
+      // Already open.
 
-          m_tab_widget->setCurrentIndex (i);
-          return;
-        }
+      // Put current focused variable out of focus
+      QFocusEvent event (QEvent::FocusOut, Qt::OtherFocusReason);
+      QApplication::sendEvent (m_main->focusWidget (), &event);
+
+      // Put existing variable in focus and raise
+      event = QFocusEvent (QEvent::FocusIn, Qt::OtherFocusReason);
+      QApplication::sendEvent (existing_qdw, &event);
+      existing_qdw->show ();
+      existing_qdw->raise ();
+      existing_qdw->setFocus ();
+
+      return;
     }
 
-  // Do not set parent.
+  variable_dock_widget *page = new variable_dock_widget (this);
+  page->setObjectName (name);
+  m_main->addDockWidget (Qt::TopDockWidgetArea, page);
 
-  QStackedWidget *widget_stack = new QStackedWidget ();
+  connect (page, SIGNAL (command_requested (const QString&)),
+           this, SIGNAL (command_requested (const QString&)));
+  connect (page, SIGNAL (destroyed (QObject *)),
+           this, SLOT (variable_destroyed (QObject *)));
+  connect (page, SIGNAL (variable_focused_signal (const QString&)),
+           this, SLOT (variable_focused (const QString&)));
 
-  var_editor_tab *page = new var_editor_tab (widget_stack);
+  connect (this, SIGNAL (clear_content_signal ()),
+           page, SLOT (clearContent ()));
+  connect (this, SIGNAL (copy_clipboard_signal ()),
+           page, SLOT (copyClipboard ()));
+  connect (this, SIGNAL (paste_clipboard_signal ()),
+           page, SLOT (pasteClipboard ()));
+  connect (this, SIGNAL (paste_table_clipboard_signal ()),
+           page, SLOT (pasteTableClipboard ()));
+  connect (this, SIGNAL (context_menu_signal (const QPoint&)),
+           page, SLOT (createContextMenu (const QPoint&)));
+  connect (this, SIGNAL (column_menu_signal (const QPoint&)),
+           page, SLOT (createColumnMenu (const QPoint&)));
+  connect (this, SIGNAL (row_menu_signal (const QPoint&)),
+           page, SLOT (createRowMenu (const QPoint&)));
+  connect (this, SIGNAL (save_signal ()),
+           page, SLOT (save ()));
+  connect (this, SIGNAL (delete_selected_signal ()),
+           page, SLOT (delete_selected ()));
+  connect (this, SIGNAL (selected_command_requested_signal (const QString&)),
+           page, SLOT (selected_command_requested (const QString&)));
 
-  QVBoxLayout *vbox = new QVBoxLayout (page);
-  page->setLayout (vbox);
-
-  QLabel *label = new QLabel (page);
-  label->setTextFormat (Qt::PlainText);
-  vbox->addWidget (label, 0, Qt::AlignTop);
-
-  variable_editor_model *model = new variable_editor_model (name, val);
+  variable_editor_model *model =
+    new variable_editor_model (name, val, page);
 
   QTableView *edit_view = make_edit_view (page, model);
 
-  QTextEdit *disp_view = make_disp_view (page, model);
+  page->setWidget (edit_view);
 
-  page->set_model (model);
+  // Must supply a title for a QLabel to be created.  Calling set_title()
+  // more than once will add more QLabels.  Could change octave_dock_widget
+  // to always supply a QLabl (initially empty) and then simply update its
+  // contents.
+  page->set_title (name);
+  QLabel *existing_ql = page->titleBarWidget ()->findChild<QLabel *> ();
+  connect (model, SIGNAL (update_label_signal (const QString&)),
+           existing_ql, SLOT (setText (const QString&)));
 
-  page->set_edit_view (edit_view);
-  page->set_disp_view (disp_view);
-
-  vbox->addWidget (widget_stack);
-
-  int tab_idx = m_tab_widget->addTab (page, name);
-
-  m_tab_widget->setCurrentIndex (tab_idx);
-
-  connect (model, SIGNAL (description_changed (const QString&)),
-           label, SLOT (setText (const QString&)));
+  connect (this, SIGNAL (refresh_signal ()),
+           model, SLOT (update_data_cache ()));
+  connect (this, SIGNAL (edit_variable_signal (const QString&, const octave_value&)),
+           model, SLOT (edit_variable (const QString&, const octave_value&)));
 
   connect (model, SIGNAL (dataChanged (const QModelIndex&, const QModelIndex&)),
            this, SLOT (callUpdate (const QModelIndex&, const QModelIndex&)));
-
-  connect (this, SIGNAL (refresh_signal (void)),
-           model, SLOT (update_data_cache (void)));
-
   connect (model, SIGNAL (set_editable_signal (bool)),
            page, SLOT (set_editable (bool)));
 
-  enable_actions ();
+  model->update_data (val);
+
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  if (viewlist.size () == 1)
+    m_tool_bar->setEnabled (true);
 }
 
 QTableView *
-variable_editor::make_edit_view (var_editor_tab *page,
+variable_editor::make_edit_view (variable_dock_widget *page,
                                   variable_editor_model *model)
 {
   QTableView *table = new QTableView (page);
@@ -380,6 +965,8 @@ variable_editor::make_edit_view (var_edi
   model->setParent (table);
 
   table->setModel (model);
+  table->setFocusProxy (page);
+  table->setFocusPolicy (Qt::StrongFocus);
   table->setWordWrap (false);
   table->setContextMenuPolicy (Qt::CustomContextMenu);
   table->setSelectionMode (QAbstractItemView::ContiguousSelection);
@@ -389,17 +976,17 @@ variable_editor::make_edit_view (var_edi
 
   connect (table->horizontalHeader (),
            SIGNAL (customContextMenuRequested (const QPoint&)),
-           this, SLOT (columnmenu_requested (const QPoint&)));
+           page, SLOT (createColumnMenu (const QPoint&)));
 
   connect (table->verticalHeader (),
            SIGNAL (customContextMenuRequested (const QPoint&)),
-           this, SLOT (rowmenu_requested (const QPoint&)));
+           page, SLOT (createRowMenu (const QPoint&)));
 
   connect (table, SIGNAL (customContextMenuRequested (const QPoint&)),
-           this, SLOT (contextmenu_requested (const QPoint&)));
+           page, SLOT (createContextMenu (const QPoint&)));
 
   connect (table, SIGNAL (doubleClicked (const QModelIndex&)),
-           this, SLOT (double_click (const QModelIndex&)));
+           model, SLOT (double_click (const QModelIndex&)));
 
   table->setFont (m_font);
   table->setStyleSheet (m_stylesheet);
@@ -433,50 +1020,12 @@ variable_editor::make_edit_view (var_edi
   return table;
 }
 
-QTextEdit *
-variable_editor::make_disp_view (var_editor_tab *page,
-                                 variable_editor_model *model)
-{
-  QTextEdit *viewer = new QTextEdit (page);
-
-  model->setParent (viewer);
-
-  QVariant v_data = model->data ();
-
-  QString str = v_data.toString ();
-
-  viewer->setPlainText (str);
-
-  return viewer;
-}
-
 void
 variable_editor::refresh (void)
 {
   emit refresh_signal ();
 }
 
-bool
-variable_editor::has_focus (void)
-{
-  // FIXME: This only generates exceptions in certain circumstances.
-  // Get a definitive list and eliminate the need to handle exceptions?
-
-  if (m_tab_widget->currentIndex () == -1)
-    return false;  // No tabs.
-
-  try
-    {
-      return m_tab_widget->current_tab_has_focus ();
-    }
-  catch (...)
-    {
-      return false;
-    }
-
-  return false;
-}
-
 QList<QColor>
 variable_editor::default_colors (void)
 {
@@ -582,12 +1131,6 @@ variable_editor::notice_settings (const 
     icon_size = st->pixelMetric (QStyle::PM_SmallIconSize);
 
   m_tool_bar->setIconSize (QSize (icon_size, icon_size));
-
-  // Shortcuts.
-
-  shortcut_manager::set_shortcut (m_close_action, "editor_file:close");
-  shortcut_manager::set_shortcut (m_close_all_action, "editor_file:close_all");
-  shortcut_manager::set_shortcut (m_close_others_action, "editor_file:close_other");
 }
 
 void
@@ -599,571 +1142,73 @@ variable_editor::closeEvent (QCloseEvent
 }
 
 void
-variable_editor::closeTab (int idx)
+variable_editor::variable_destroyed (QObject *)
 {
-  if (idx < 0 || idx > m_tab_widget->count ())
-    return;
-
-  QWidget *wdgt = m_tab_widget->widget (idx);
-
-  m_tab_widget->removeTab (idx);
-
-  delete wdgt;
-
-  enable_actions ();
+  QList<variable_dock_widget *> vdwlist = findChildren<variable_dock_widget *> ();
+  if (vdwlist.isEmpty ())
+    m_tool_bar->setEnabled (false);
 }
 
 void
-variable_editor::contextmenu_requested (const QPoint& qpos)
+variable_editor::variable_focused (const QString &name)
 {
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  QModelIndex index = view->indexAt (qpos);
-
-  if (index.isValid ())
-    {
-      QMenu *menu = new QMenu (this);
-
-      menu->addAction (resource_manager::icon ("edit-cut"),
-                       tr ("Cut"), this, SLOT (cutClipboard ()));
-
-      menu->addAction (resource_manager::icon ("edit-copy"),
-                       tr ("Copy"), this, SLOT (copyClipboard ()));
-
-      menu->addAction (resource_manager::icon ("edit-paste"),
-                       tr ("Paste"), this, SLOT (pasteClipboard ()));
-
-      // FIXME: Need different icon for paste table separate from paste?
-
-      menu->addAction (resource_manager::icon ("edit-paste"),
-                       tr ("Paste Table"), this,
-                       SLOT (pasteTableClipboard ()));
-
-      menu->addSeparator ();
-
-      menu->addAction (resource_manager::icon ("edit-delete"),
-                       tr ("Clear"), this, SLOT (clearContent ()));
-
-      menu->addAction (resource_manager::icon ("document-new"),
-                       tr ("Variable from Selection"), this,
-                       SLOT (createVariable ()));
-
-      // FIXME: addAction for sort?
-      // FIXME: Add icon for transpose.
-
-      menu->addAction (tr ("Transpose"), this, SLOT (transposeContent ()));
-
-      QItemSelectionModel *sel = view->selectionModel ();
-
-      QList<QModelIndex> indices = sel->selectedIndexes ();
-
-      if (! indices.isEmpty ())
-        {
-          menu->addSeparator ();
-
-          QSignalMapper *plot_mapper = new QSignalMapper (menu);
-
-          plot_mapper->setMapping
-            (menu->addAction ("plot", plot_mapper, SLOT (map ())),
-             "figure (); plot (%1);");
-
-          plot_mapper->setMapping
-            (menu->addAction ("bar", plot_mapper, SLOT (map ())),
-             "figure (); bar (%1);");
-
-          plot_mapper->setMapping
-            (menu->addAction ("stem", plot_mapper, SLOT (map ())),
-             "figure (); stem (%1);");
-
-          plot_mapper->setMapping
-            (menu->addAction ("stairs", plot_mapper, SLOT (map ())),
-             "figure (); stairs (%1);");
-
-          plot_mapper->setMapping
-            (menu->addAction ("area", plot_mapper, SLOT (map ())),
-             "figure (); area (%1);");
-
-          plot_mapper->setMapping
-            (menu->addAction ("pie", plot_mapper, SLOT (map ())),
-             "figure (); pie (%1);");
-
-          plot_mapper->setMapping
-            (menu->addAction ("hist", plot_mapper, SLOT (map ())),
-             "figure (); hist (%1);");
-
-          connect (plot_mapper, SIGNAL (mapped (const QString&)),
-                   this, SLOT (relay_command (const QString&)));
-        }
-
-      menu->exec (view->mapToGlobal (qpos));
-    }
+  m_current_focus_vname = name;
 }
 
 void
-variable_editor::columnmenu_requested (const QPoint& pt)
+variable_editor::record_hovered_focus_variable (void)
 {
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  int index = view->horizontalHeader ()->logicalIndexAt (pt);
-
-  if (index < 0 || index > view->model ()->columnCount ())
-    return;
-
-  QString selection = selected_to_octave ();
-
-  QList<int> coords = octave_to_coords (selection);
-
-  bool nothingSelected = coords.isEmpty ();
-
-  bool whole_columns_selected
-    =  (nothingSelected
-        ? false
-        : (coords[0] == 1 && coords[1] == view->model ()->rowCount ()));
-
-  bool current_column_selected
-    = nothingSelected ? false : (coords[2] <= index+1 && coords[3] > index);
-
-  int column_selection_count
-    = nothingSelected ? 0 : (coords[3] - coords[2] + 1);
-
-  if (! whole_columns_selected || ! current_column_selected)
-    {
-      view->selectColumn (index);
-      column_selection_count = 1;
-      current_column_selected = true;
-      whole_columns_selected = true;
-    }
-
-  QString column_string
-    = tr (column_selection_count > 1 ? " columns" : " column");
-
-  QMenu *menu = new QMenu (this);
-
-  menu->addAction (resource_manager::icon ("edit-cut"),
-                   tr ("Cut") + column_string,
-                   this, SLOT (cutClipboard ()));
-
-  menu->addAction (resource_manager::icon ("edit-copy"),
-                   tr ("Copy") + column_string,
-                   this, SLOT (copyClipboard ()));
-
-  menu->addAction (resource_manager::icon ("edit-paste"),
-                   tr ("Paste"),
-                   this, SLOT (pasteClipboard ()));
-
-  // FIXME: Different icon for Paste Table?
-
-  menu->addAction (resource_manager::icon ("edit-paste"),
-                   tr ("Paste Table"),
-                   this, SLOT (pasteTableClipboard ()));
-
-  menu->addSeparator ();
-
-  menu->addAction (resource_manager::icon ("edit-delete"),
-                   tr ("Clear") + column_string,
-                   this, SLOT (clearContent ()));
-
-  menu->addAction (resource_manager::icon ("edit-delete"),
-                   tr ("Delete") + column_string,
-                   this, SLOT (delete_selected ()));
-
-  menu->addAction (resource_manager::icon ("document-new"),
-                   tr ("Variable from Selection"),
-                   this, SLOT (createVariable ()));
-
-  menu->addSeparator ();
-
-  QSignalMapper *plot_mapper = new QSignalMapper (menu);
-
-  plot_mapper->setMapping
-    (menu->addAction ("plot", plot_mapper, SLOT (map ())),
-     "figure (); plot (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("bar", plot_mapper, SLOT (map ())),
-     "figure (); bar (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("stem", plot_mapper, SLOT (map ())),
-     "figure (); stem (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("stairs", plot_mapper, SLOT (map ())),
-     "figure (); stairs (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("area", plot_mapper, SLOT (map ())),
-     "figure (); area (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("pie", plot_mapper, SLOT (map ())),
-     "figure (); pie (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("hist", plot_mapper, SLOT (map ())),
-     "figure (); hist (%1);");
-
-  connect (plot_mapper, SIGNAL (mapped (const QString&)),
-           this, SLOT (relay_command (const QString&)));
-
-  QPoint menupos = pt;
-  menupos.setY (view->horizontalHeader ()->height ());
-
-  menu->exec (view->mapToGlobal (menupos));
+  m_hovered_focus_vname = m_current_focus_vname;
 }
 
 void
-variable_editor::rowmenu_requested (const QPoint& pt)
+variable_editor::restore_hovered_focus_variable (void)
 {
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  int index = view->verticalHeader ()->logicalIndexAt (pt);
-
-  if (index < 0 || index > view->model ()->columnCount ())
-    return;
-
-  QString selection = selected_to_octave ();
-
-  QList<int> coords = octave_to_coords (selection);
-
-  bool nothingSelected = coords.isEmpty ();
-
-  bool whole_rows_selected
-    = (nothingSelected
-       ? false
-       : (coords[2] == 1 && coords[3] == view->model ()->columnCount ()));
-
-  bool current_row_selected
-    = (nothingSelected ? false : (coords[0] <= index+1 && coords[1] > index));
-
-  int rowselection_count = nothingSelected ? 0 : (coords[3] - coords[2] + 1);
-
-  if (! whole_rows_selected || ! current_row_selected)
+  variable_dock_widget *tofocus = findChild<variable_dock_widget *> (m_hovered_focus_vname);
+  if (tofocus != nullptr)
     {
-      view->selectRow (index);
-      rowselection_count = 1;
-      current_row_selected = true;
-      whole_rows_selected = true;
+      // Note that this may be platform and window system dependent.
+      // On a particular Linux system, activateWindow() alone didn't
+      // immediately set the active window and there was a race
+      // between the window focus and action signal.  Setting the
+      // active window via the QApplication route did work.
+      QApplication::setActiveWindow(tofocus->window());
+      tofocus->activateWindow ();
+      tofocus->setFocus (Qt::OtherFocusReason);
     }
-
-  QString row_string = tr (rowselection_count > 1 ? " rows" : " row");
-
-  QMenu *menu = new QMenu (this);
-
-  menu->addAction (resource_manager::icon ("edit-cut"),
-                   tr ("Cut") + row_string,
-                   this, SLOT (cutClipboard ()));
-
-  menu->addAction (resource_manager::icon ("edit-copy"),
-                   tr ("Copy") + row_string,
-                   this, SLOT (copyClipboard ()));
-
-  menu->addAction (resource_manager::icon ("edit-paste"),
-                   tr ("Paste"),
-                   this, SLOT (pasteClipboard ()));
-
-  // FIXME: Better icon for Paste Table?
-
-  menu->addAction (resource_manager::icon ("edit-paste"),
-                   tr ("Paste Table"),
-                   this, SLOT (pasteTableClipboard ()));
-
-  menu->addSeparator ();
-
-  menu->addAction (resource_manager::icon ("edit-delete"),
-                   tr ("Clear") + row_string,
-                   this, SLOT (clearContent ()));
-
-  menu->addAction (resource_manager::icon ("edit-delete"),
-                   tr ("Delete") + row_string,
-                   this, SLOT (delete_selected ()));
-
-  menu->addAction (resource_manager::icon ("document-new"),
-                   tr ("Variable from Selection"),
-                   this, SLOT (createVariable ()));
-
-  menu->addSeparator ();
-
-  QSignalMapper *plot_mapper = new QSignalMapper (menu);
-
-  plot_mapper->setMapping
-    (menu->addAction ("plot", plot_mapper, SLOT (map ())),
-     "figure (); plot (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("bar", plot_mapper, SLOT (map ())),
-     "figure (); bar (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("stem", plot_mapper, SLOT (map ())),
-     "figure (); stem (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("stairs", plot_mapper, SLOT (map ())),
-     "figure (); stairs (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("area", plot_mapper, SLOT (map ())),
-     "figure (); area (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("pie", plot_mapper, SLOT (map ())),
-     "figure (); pie (%1);");
-
-  plot_mapper->setMapping
-    (menu->addAction ("hist", plot_mapper, SLOT (map ())),
-     "figure (); hist (%1);");
-
-  connect (plot_mapper, SIGNAL (mapped (const QString&)),
-           this, SLOT (relay_command (const QString&)));
-
-  QPoint menupos = pt;
-  menupos.setX (view->verticalHeader ()->width ());
-
-  // FIXME: What was the intent here?
-  // setY (view->verticalHeader ()->sectionPosition (index+1) +
-  //             view->verticalHeader ()->sectionSize (index));
-
-  menu->exec (view->mapToGlobal (menupos));
-}
-
-void
-variable_editor::double_click (const QModelIndex& idx)
-{
-  QString name = real_var_name (m_tab_widget->currentIndex ());
-
-  QTableView *table = m_tab_widget->get_edit_view ();
-
-  if (! table)
-    return;
-
-  variable_editor_model *model
-    = qobject_cast<variable_editor_model *> (table->model ());
-
-  if (model->requires_sub_editor (idx))
-    edit_variable (name + model->subscript_expression (idx),
-                   model->value_at (idx));
 }
 
 void
 variable_editor::save (void)
 {
-  QString name = real_var_name (m_tab_widget->currentIndex ());
-  QString file
-    = QFileDialog::getSaveFileName (this,
-                                    tr ("Save Variable %1 As").arg (name),
-                                    ".", 0, 0,
-                                    QFileDialog::DontUseNativeDialog);
-
-  // FIXME: Type? binary, float-binary, ascii, text, hdf5, matlab format?
-  // FIXME: Call octave_value::save_* directly?
-
-  if (! file.isEmpty ())
-    emit command_requested (QString ("save (\"%1\", \"%2\");")
-                            .arg (file)
-                            .arg (name));
-}
-
-void
-variable_editor::clearContent (void)
-{
-  // FIXME: Shift?
-
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  QAbstractItemModel *model = view->model ();
-  QItemSelectionModel *sel = view->selectionModel ();
-  QList<QModelIndex> indices = sel->selectedIndexes ();
-
-  // FIXME: Use [] for empty cells?
-
-  for (const auto& idx : indices)
-    qobject_cast<variable_editor_model *> (model)->clear_content (idx);
+  emit save_signal ();
 }
 
 void
 variable_editor::cutClipboard (void)
 {
-  if (! has_focus ())
-    return;
-
   copyClipboard ();
 
-  clearContent ();
+  emit clear_content_signal ();
 }
 
 void
 variable_editor::copyClipboard (void)
 {
-  if (! has_focus ())
-    return;
-
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  QAbstractItemModel *model = view->model ();
-  QItemSelectionModel *sel = view->selectionModel ();
-  QList<QModelIndex> indices = sel->selectedIndexes ();
-  qSort (indices);
-  if (indices.isEmpty ())
-    return;
-
-  // Convert selected items into TSV format and copy that.
-  // Spreadsheet tools should understand that.
-
-  QModelIndex previous = indices.first ();
-  QString copy = model->data (previous).toString ();
-  indices.removeFirst ();
-  foreach (QModelIndex idx, indices)
-    {
-      copy.push_back (previous.row () != idx.row () ? '\n' : '\t');
-      copy.append (model->data (idx).toString ());
-      previous = idx;
-    }
-  copy.push_back ('\n');
-
-  QClipboard *clipboard = QApplication::clipboard ();
-  clipboard->setText (copy);
+  emit copy_clipboard_signal ();
 }
 
 void
 variable_editor::pasteClipboard (void)
 {
-  if (! has_focus ())
-    return;
-
-  QClipboard *clipboard = QApplication::clipboard ();
-  QString text = clipboard->text ();
-
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  QItemSelectionModel *sel = view->selectionModel ();
-  QList<QModelIndex> indices = sel->selectedIndexes ();
-
-  variable_editor_model *model
-    = static_cast<variable_editor_model *> (view->model ());
-
-  if (indices.isEmpty ())
-    {
-      if (view->size () == QSize (1,1))
-        model->setData (view->model ()->index (0,0), text.toDouble ());
-      else if (view->size () == QSize (0,0))
-        {
-          model->insertColumn (0);
-          model->insertRow (0);
-          model->setData (view->model ()->index (0,0), text.toDouble ());
-        }
-    }
-  else
-    {
-      for (int i = 0; i < indices.size (); i++)
-        view->model ()->setData (indices[i], text.toDouble ());
-    }
+  emit paste_clipboard_signal ();
 
   emit updated ();
 }
 
 void variable_editor::pasteTableClipboard (void)
 {
-  if (! has_focus ())
-    return;
-
-  QClipboard *clipboard = QApplication::clipboard ();
-  QString text = clipboard->text ();
-
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  QItemSelectionModel *sel = view->selectionModel ();
-  QList<QModelIndex> indices = sel->selectedIndexes ();
-
-  variable_editor_model *model
-    = static_cast<variable_editor_model *> (view->model ());
-
-  QPoint start, end;
-
-  QPoint tabsize = QPoint (model->rowCount (), model->columnCount ());
-
-  if (indices.isEmpty ())
-    {
-      start = QPoint (0,0);
-      end = tabsize;
-    }
-  else if (indices.size () == 1)
-    {
-      start = QPoint (indices[0].row (), indices[0].column ());
-      end = tabsize;
-    }
-  else
-    {
-      end = QPoint (0,0);
-      start = tabsize;
-
-      for (int i = 0; i < indices.size (); i++)
-        {
-          if (indices[i].column () < start.y ())
-            start.setY (indices[i].column ());
-
-          if (indices[i].column () > end.y ())
-            end.setY (indices[i].column ());
-
-          if (indices[i].row () < start.x ())
-            start.setX (indices[i].column ());
-
-          if (indices[i].row () > end.x ())
-            end.setX (indices[i].column ());
-        }
-    }
-
-  int rownum = 0;
-  int colnum = 0;
-
-  QStringList rows = text.split ('\n');
-  for (const auto& row : rows)
-    {
-      if (rownum > end.x () - start.x ())
-        continue;
-
-      QStringList cols = row.split ('\t');
-      if (cols.isEmpty ())
-        continue;
-
-      for (const auto& col : cols)
-        {
-          if (col.isEmpty ())
-            continue;
-          if (colnum > end.y () - start.y () )
-            continue;
-
-          model->setData (model->index (rownum + start.x (),
-                                        colnum + start.y ()),
-                          QVariant (col));
-
-          colnum++;
-        }
-
-      colnum = 0;
-      rownum++;
-    }
+  emit paste_table_clipboard_signal ();
 
   emit updated ();
 }
@@ -1173,185 +1218,13 @@ variable_editor::createVariable (void)
 {
   // FIXME: Create unnamed1..n if exist ('unnamed', 'var') is true.
 
-  relay_command ("unnamed = %1");
+  relay_selected_command ("unnamed = %1");
 }
 
 void
-variable_editor::transposeContent (void)
+variable_editor::relay_selected_command (const QString& cmd)
 {
-  QString name = real_var_name (m_tab_widget->currentIndex ());
-
-  emit command_requested (QString ("%1 = %1';").arg (name));
-
-  emit updated ();
-}
-
-void
-variable_editor::up (void)
-{
-  QString name = real_var_name (m_tab_widget->currentIndex ());
-
-  // FIXME: Is there a better way?
-
-  if (name.endsWith (')') || name.endsWith ('}'))
-    {
-      name.remove (QRegExp ("(\\(|\\{)[^({]*(\\)|\\})$"));
-      edit_variable (name, octave_value ());
-    }
-}
-
-void
-variable_editor::delete_selected (void)
-{
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return;
-
-  QString selection = selected_to_octave ();
-  QList<int> coords = octave_to_coords (selection);
-
-  if (coords.isEmpty ())
-    return;
-
-  bool whole_columns_selected
-    = coords[0] == 1 && coords[1] == view->model ()->rowCount ();
-
-  bool whole_rows_selected
-    = coords[2] == 1 && coords[3] == view->model ()->columnCount ();
-
-  // Must be deleting whole columns or whole rows, and not the whole thing.
-
-  if (whole_columns_selected == whole_rows_selected)
-    return;
-
-  if (whole_rows_selected)
-    view->model ()->removeRows (coords[0], coords[1] - coords[0]);
-
-  if (whole_columns_selected)
-    view->model ()->removeColumns (coords[2], coords[3] - coords[2]);
-
-  emit updated ();
-}
-
-void
-variable_editor::relay_command (const QString& cmd)
-{
-  emit command_requested (cmd.arg (selected_to_octave ()));
-}
-
-QList<int>
-variable_editor::octave_to_coords (QString& selection)
-{
-  // FIXME: Is this necessary or would it be quicker to clone the function
-  // that gives us the QString?
-
-  // Sanity check.
-
-  if (selection.count (",") != 1)
-    return QList<int> ();
-
-  // FIXME: Why clear if object has just been created?
-
-  QList<int> output;
-  output.clear ();
-
-  // Remove braces.
-
-  int firstbracket = std::max (selection.indexOf ("("),
-                               selection.indexOf ("{"));
-
-  selection = selection.mid (firstbracket + 1,
-                             selection.length () - (firstbracket + 2));
-
-  QString rows = selection.left (selection.indexOf (","));
-  if (! rows.contains (":"))
-    {
-      // Only one row.
-
-      output.push_back (rows.toInt ());
-      output.push_back (output.last ());
-    }
-  else
-    {
-      output.push_back (rows.left (rows.indexOf (":")).toInt ());
-      output.push_back (rows.right (rows.length () - (rows.indexOf (":") + 1))
-                        .toInt ());
-    }
-
-  QString cols;
-  cols = selection.right (selection.length () - (selection.indexOf (",") + 1));
-  if (cols.left (1) == " ")
-    cols = cols.right (cols.length () - 1);
-
-  if (! cols.contains (":"))
-    {
-      // Only one row.
-
-      output.push_back (cols.toInt ());
-      output.push_back (output.last ());
-    }
-  else
-    {
-      output.push_back (cols.left (cols.indexOf (":")).toInt ());
-      output.push_back (cols.right (cols.length () - (cols.indexOf (":") + 1))
-                        .toInt ());
-    }
-
-  return output;
-}
-
-// Return the real variable name from the tab addressed by 'index',
-// cleaned of any '&' possibly inserted by KDE.
-
-QString
-variable_editor::real_var_name (int index)
-{
-  QString var_name = m_tab_widget->tabText (index);
-  var_name.remove (QChar ('&'));
-  return var_name;
-}
-
-QString
-variable_editor::selected_to_octave (void)
-{
-  QString name = real_var_name (m_tab_widget->currentIndex ());
-
-  QTableView *view = m_tab_widget->get_edit_view ();
-
-  if (! view)
-    return name;
-
-  QItemSelectionModel *sel = view->selectionModel ();
-
-  // Return early if nothing selected.
-
-  if (! sel->hasSelection ())
-    return name;
-
-  QList<QModelIndex> indices = sel->selectedIndexes ();
-
-  // FIXME: Shouldn't this be keyed to octave_idx_type?
-
-  int32_t from_row = std::numeric_limits<int32_t>::max ();
-  int32_t to_row = 0;
-  int32_t from_col = std::numeric_limits<int32_t>::max ();
-  int32_t to_col = 0;
-
-  for (const auto& idx : indices)
-    {
-      from_row = std::min (from_row, idx.row ());
-      to_row = std::max (to_row, idx.row ());
-      from_col = std::min (from_col, idx.column ());
-      to_col = std::max (to_col, idx.column ());
-    }
-
-  QString rows = idx_to_expr (from_row, to_row);
-  QString cols = idx_to_expr (from_col, to_col);
-
-  // FIXME: Does cell need separate handling?  Maybe use '{.,.}'?
-
-  return QString ("%1(%2, %3)").arg (name).arg (rows).arg (cols);
+  emit selected_command_requested_signal (cmd);
 }
 
 // Also updates the font.
@@ -1383,12 +1256,10 @@ void variable_editor::update_colors (voi
         + m_table_colors[3].name () +" }";
     }
 
-  if (m_tab_widget->count () < 1)
-    return;
-
-  for (int i = 0; i < m_tab_widget->count (); i++)
+  QList<QTableView *> viewlist = findChildren<QTableView *> ();
+  for (int i = 0; i < viewlist.size (); i++)
     {
-      QTableView *view = m_tab_widget->get_edit_view ();
+      QTableView *view = viewlist.at (i);
 
       if (! view)
         continue;
@@ -1400,36 +1271,59 @@ void variable_editor::update_colors (voi
 
 }
 
+QAction *
+variable_editor::add_tool_bar_button (const QIcon &icon,
+                                      const QString &text,
+                                      const QObject *receiver,
+                                      const char *member)
+{
+  QAction *action = new QAction (icon, text, this);
+  connect(action, SIGNAL (triggered ()), receiver, member);
+  QToolButton *button = new ReturnFocusToolButton (m_tool_bar);
+  button->setDefaultAction (action);
+  button->setText (text);
+  button->setToolTip (text);
+  button->setIcon (icon);
+  m_tool_bar->addWidget (button);
+
+  return action;
+}
+
 void
 variable_editor::construct_tool_bar (void)
 {
   m_tool_bar->setAllowedAreas (Qt::TopToolBarArea);
 
-  m_tool_bar->setMovable (false);
-
   m_tool_bar->setObjectName ("VariableEditorToolBar");
 
   m_tool_bar->setWindowTitle (tr ("Variable Editor Toolbar"));
 
-  m_tool_bar->addAction (resource_manager::icon ("document-save"),
-                         tr ("Save"), this, SLOT (save ()));
+  QAction *action;
+  action = add_tool_bar_button (resource_manager::icon ("document-save"),
+                                tr ("Save"), this, SLOT (save ()));
+  action->setShortcuts (QKeySequence::Save);
+  action->setStatusTip(tr("Save variable to a file"));
 
   m_tool_bar->addSeparator ();
 
-  m_tool_bar->addAction (resource_manager::icon ("edit-cut"),
-                         tr ("Cut"), this, SLOT (cutClipboard ()));
+  action = add_tool_bar_button (resource_manager::icon ("edit-cut"),
+                                tr ("Cut"), this, SLOT (cutClipboard ()));
+  action->setStatusTip(tr("Cut data to clipboard"));
 
-  m_tool_bar->addAction (resource_manager::icon ("edit-copy"),
-                         tr ("Copy"), this, SLOT (copyClipboard ()));
+  action = add_tool_bar_button (resource_manager::icon ("edit-copy"),
+                                tr ("Copy"), this, SLOT (copyClipboard ()));
+  action->setStatusTip(tr("Copy data to clipboard"));
 
-  m_tool_bar->addAction (resource_manager::icon ("edit-paste"),
-                         tr ("Paste"), this, SLOT (pasteClipboard ()));
+  action = add_tool_bar_button (resource_manager::icon ("edit-paste"),
+                                tr ("Paste"), this, SLOT (pasteClipboard ()));
+  action->setStatusTip(tr("Paste clipboard into variable data"));
 
   // FIXME: Different icon for Paste Table?
 
-  m_tool_bar->addAction (resource_manager::icon ("edit-paste"),
-                         tr ("Paste Table"),
-                         this, SLOT (pasteTableClipboard ()));
+  action = add_tool_bar_button (resource_manager::icon ("edit-paste"),
+                                tr ("Paste Table"),
+                                this, SLOT (pasteTableClipboard ()));
+  action->setStatusTip(tr("Another paste clipboard into variable data"));
 
   m_tool_bar->addSeparator ();
 
@@ -1437,16 +1331,20 @@ variable_editor::construct_tool_bar (voi
   // QAction *print_action; /icons/fileprint.png
   // m_tool_bar->addSeparator ();
 
-  QToolButton *plot_tool_button = new QToolButton (m_tool_bar);
+  action = new QAction (resource_manager::icon ("plot-xy-curve"),
+                        tr ("Plot"), m_tool_bar);
+  action->setToolTip (tr ("Plot Selected Data"));
+  QToolButton *plot_tool_button = new HoverToolButton (m_tool_bar);
+  plot_tool_button->setDefaultAction (action);
 
   plot_tool_button->setText (tr ("Plot"));
-  plot_tool_button->setToolTip (tr ("Plot Selected Data"));
+  plot_tool_button->setToolTip (tr ("Plot selected data"));
   plot_tool_button->setIcon (resource_manager::icon ("plot-xy-curve"));
 
   plot_tool_button->setPopupMode (QToolButton::InstantPopup);
 
-  QMenu *plot_menu = new QMenu (tr ("Plot"), plot_tool_button);
-
+  QMenu *plot_menu = new ReturnFocusMenu (plot_tool_button);
+  plot_menu->setTitle (tr ("Plot"));
   plot_menu->setSeparatorsCollapsible (false);
 
   QSignalMapper *plot_mapper = new QSignalMapper (plot_menu);
@@ -1480,16 +1378,38 @@ variable_editor::construct_tool_bar (voi
      "figure (); hist (%1);");
 
   connect (plot_mapper, SIGNAL (mapped (const QString&)),
-           this, SLOT (relay_command (const QString&)));
+           this, SLOT (relay_selected_command (const QString&)));
 
   plot_tool_button->setMenu (plot_menu);
 
   m_tool_bar->addWidget (plot_tool_button);
 
-  m_tool_bar->addSeparator ();
+  // The QToolButton mouse-clicks change active window, so connect all
+  // HoverToolButton and RuternFocusToolButton objects to the mechanism
+  // that restores active window and focus before acting.
+  QList<HoverToolButton *> hbuttonlist = m_tool_bar->findChildren<HoverToolButton *> (QString (), Qt::FindDirectChildrenOnly);
+  for (int i = 0; i < hbuttonlist.size (); i++)
+    {
+      connect (hbuttonlist.at (i), SIGNAL (hovered_signal ()),
+               this, SLOT (record_hovered_focus_variable ()));
+    }
+  QList<ReturnFocusToolButton *> rfbuttonlist = m_tool_bar->findChildren<ReturnFocusToolButton *> (QString (), Qt::FindDirectChildrenOnly);
+  for (int i = 0; i < rfbuttonlist.size (); i++)
+    {
+      connect (rfbuttonlist.at (i), SIGNAL (about_to_activate ()),
+               this, SLOT (restore_hovered_focus_variable ()));
+    }
 
-  m_tool_bar->addAction (QIcon (resource_manager::icon ("go-up")), tr ("Up"),
-                         this, SLOT (up ()));
+  // Same for QMenu
+  QList<ReturnFocusMenu *> menulist = m_tool_bar->findChildren<ReturnFocusMenu *> (QString ());
+  for (int i = 0; i < menulist.size (); i++)
+    {
+      connect (menulist.at (i), SIGNAL (about_to_activate ()),
+               this, SLOT (restore_hovered_focus_variable ()));
+    }
+
+  m_tool_bar->setAttribute(Qt::WA_ShowWithoutActivating);
+  m_tool_bar->setFocusPolicy (Qt::NoFocus);
 
   // Disabled when no tab is present.
 
diff --git a/libgui/src/variable-editor.h b/libgui/src/variable-editor.h
--- a/libgui/src/variable-editor.h
+++ b/libgui/src/variable-editor.h
@@ -42,77 +42,121 @@ class QToolBar;
 
 class variable_editor_model;
 
-class var_editor_tab : public QWidget
+// The individual variable subwindow class
+
+class variable_dock_widget : public label_dock_widget
 {
   Q_OBJECT
 
 public:
 
-  var_editor_tab (QStackedWidget *widget_stack, QWidget *p = nullptr)
-    : QWidget (p), m_widget_stack (widget_stack)
-  { }
+  variable_dock_widget (QWidget *p = nullptr);
 
-  ~var_editor_tab (void) = default;
+signals:
 
-  // No copying!
+  void command_requested (const QString& cmd);
 
-  var_editor_tab (const var_editor_tab&) = delete;
+  void variable_focused_signal (const QString& name);
 
-  var_editor_tab& operator = (const var_editor_tab&) = delete;
+protected:
 
-  QTableView * get_edit_view (void) const;
-  QTextEdit * get_disp_view (void) const;
+  void focusInEvent(QFocusEvent *event);
 
-  void set_edit_view (QTableView *);
-  void set_disp_view (QTextEdit *);
+  void focusOutEvent(QFocusEvent *event);
 
-  void set_model (variable_editor_model *model)
-  {
-    m_model = model;
-  }
-
-  bool has_focus (void) const;
+  virtual void closeEvent (QCloseEvent *e);
 
 public slots:
 
-  void set_editable (bool);
+  void clearContent (void);
 
-private:
+  void copyClipboard (void);
 
-  variable_editor_model *m_model;
+  void pasteClipboard (void);
 
-  QStackedWidget *m_widget_stack;
+  void pasteTableClipboard (void);
 
-  int m_edit_view_idx;
-  int m_disp_view_idx;
+  void transposeContent (void);
+
+  void createContextMenu (const QPoint& pt);
+
+  void createColumnMenu (const QPoint& pt);
+
+  void createRowMenu (const QPoint& pt);
+
+  void save (void);
+
+  void delete_selected (void);
+
+  void selected_command_requested (const QString& cmd);
+
+private slots:
+
+  void change_floating (bool);
+
+  void change_existence (bool);
+  
+  void toplevel_change (bool);
+
+protected:
+
+  bool m_initial_float;
 };
 
-// Subclassed QTabWidget for using custom tabbar
+// Gadgets to keep track and restore what variable window
+// was in focus just prior to selecting something on the
+// menu bar
 
-class var_editor_tab_widget : public QTabWidget
+class HoverToolButton : public QToolButton
 {
   Q_OBJECT
 
 public:
 
-  var_editor_tab_widget (QWidget *p);
+  HoverToolButton (QWidget *parent = nullptr);
 
-  ~var_editor_tab_widget (void) = default;
+signals:
 
-  // No copying!
+  void hovered_signal (void);
 
-  var_editor_tab_widget (const var_editor_tab_widget&) = delete;
+protected:
 
-  var_editor_tab_widget& operator = (const var_editor_tab_widget&) = delete;
-
-  tab_bar * get_tab_bar (void) const;
-
-  bool current_tab_has_focus (void) const;
-
-  QTextEdit *get_disp_view (void) const;
-  QTableView *get_edit_view (void) const;
+  bool eventFilter (QObject *obj, QEvent *ev);
 };
 
+class ReturnFocusToolButton : public HoverToolButton
+{
+  Q_OBJECT
+
+public:
+
+  ReturnFocusToolButton (QWidget *parent = nullptr);
+
+signals:
+
+  void about_to_activate (void);
+
+protected:
+
+  bool eventFilter (QObject *obj, QEvent *ev);
+};
+
+class ReturnFocusMenu : public QMenu
+{
+  Q_OBJECT
+
+public:
+
+  ReturnFocusMenu (QWidget *parent = nullptr);
+
+signals:
+
+  void about_to_activate (void);
+
+protected:
+
+  bool eventFilter (QObject *obj, QEvent *ev);
+};
 
 // The variable editor class
 
@@ -132,50 +176,37 @@ public:
 
   variable_editor& operator = (const variable_editor&) = delete;
 
-  void edit_variable (const QString& name, const octave_value& val);
-
-  QTableView *make_edit_view (var_editor_tab *page,
+  QTableView *make_edit_view (variable_dock_widget *page,
                               variable_editor_model *model);
 
-  QTextEdit *make_disp_view (var_editor_tab *page,
-                             variable_editor_model *model);
-
   void refresh (void);
 
-  bool has_focus (void);
-
   static QList<QColor> default_colors (void);
 
   static QStringList color_names (void);
 
 public slots:
 
-  void callUpdate (const QModelIndex&,const QModelIndex&);
+  void callUpdate (const QModelIndex&, const QModelIndex&);
 
   void notice_settings (const QSettings *);
 
+  void edit_variable (const QString& name, const octave_value& val);
+
+  void variable_destroyed (QObject *obj);
+
+  void variable_focused (const QString& name);
+
+  void record_hovered_focus_variable (void);
+
+  void restore_hovered_focus_variable (void);
+
 protected slots:
 
-  void request_close_tab (bool);
-  void request_close_other_tabs (bool);
-  void request_close_all_tabs (bool);
-
   void closeEvent (QCloseEvent *);
 
-  void closeTab (int idx);
-
-  void contextmenu_requested (const QPoint& pt);
-
-  void columnmenu_requested (const QPoint& pt);
-
-  void rowmenu_requested (const QPoint& pt);
-
-  void double_click (const QModelIndex& idx);
-
   void save (void);
 
-  void clearContent (void);
-
   void cutClipboard (void);
 
   void copyClipboard (void);
@@ -186,15 +217,9 @@ protected slots:
 
   void createVariable (void);
 
-  void transposeContent (void);
-
-  void up (void);
-
-  void delete_selected (void);
-
   // Send command to Octave interpreter.
   // %1 in CMD is replaced with the value of selected_to_octave.
-  void relay_command (const QString& cmd);
+  void relay_selected_command (const QString& cmd);
 
 signals:
 
@@ -206,21 +231,35 @@ signals:
 
   void refresh_signal (void);
 
+  void clear_content_signal (void);
+
+  void copy_clipboard_signal (void);
+
+  void paste_clipboard_signal (void);
+
+  void paste_table_clipboard_signal (void);
+
+  void context_menu_signal (const QPoint& pt);
+
+  void column_menu_signal (const QPoint& pt);
+
+  void row_menu_signal (const QPoint& pt);
+
+  void save_signal (void);
+
+  void delete_selected_signal (void);
+
+  void selected_command_requested_signal (const QString& cmd);
+
 private:
 
   QAction * add_action (QMenu *menu, const QIcon& icon, const QString& text,
                         const char *member);
 
-  void enable_actions (void);
-
-  QWidget *m_container;
+  QMainWindow *m_main;
 
   QToolBar *m_tool_bar;
 
-  var_editor_tab_widget *m_tab_widget;
-
-  tab_bar *m_tab_bar;
-
   int m_default_width;
 
   int m_default_height;
@@ -241,21 +280,16 @@ private:
 
   QList<QColor> m_table_colors;
 
-  QAction *m_close_action;
-  QAction *m_close_others_action;
-  QAction *m_close_all_action;
-
-  QList<int> octave_to_coords (QString&);
-
-  // Get the real variable name from the tab text
-  QString real_var_name (int index);
-
-  // Convert selection to an Octave expression.
-  QString selected_to_octave (void);
-
   void update_colors (void);
 
+  QAction *add_tool_bar_button (const QIcon &icon, const QString &text,
+                                const QObject *receiver, const char *member);
+
   void construct_tool_bar (void);
+
+  QString m_current_focus_vname;
+
+  QString m_hovered_focus_vname;
 };
 
 #endif
