changeset 24172:90903d915625

improve handling of selection markers in editor Clear selection markers if cursor moves away from the current selection. This also means that the selection markers will be cleared if the user clicks the margin to set a breakpoint because that also moves the cursor. * marker.h (marker::selection): New value for editor_markers enum. * file-editor-tab.h, file-editor-tab.cc (file_editor_tab::_indicator_highlight_all, file_editor_tab::_marker_highlight_all): Delete data members and all uses. (file_editor_tab::update_lexer_settings): Call octave_qscintilla::set_selection_marker_color. (file_editor_tab::handle_double_click): Clear selection markers and word selection here. Call show_selection_markers for _edit_area to show new selection. Set word selection to new selected word. * octave-qscintilla.h, octave-qscintilla.cc (octave_qscintilla::m_selection, octave_qscintilla::m_selection_line, octave_qscintilla::m_selection_col, octave_qscintilla::m_indicator_id): New data members. (octave_qscintilla::set_selection_marker_color): New function. (octave_qscintilla::set_word_selection): New function. (octave_qscintilla::show_selection_markers): New function. (octave_qscintilla::cursor_position_changed): New slot. (octave_qscintilla::octave_qscintilla): Initialize them. Initialize selection marker. Connect cursor_position_changed to cursorPositionChanged signal. (octave_qscintalla::clear_selection_markers): Eliminate arguments. Change all uses.
author John W. Eaton <jwe@octave.org>
date Mon, 23 Oct 2017 18:21:12 -0400
parents 1680d425bb38
children 730227072acb
files libgui/src/m-editor/file-editor-tab.cc libgui/src/m-editor/file-editor-tab.h libgui/src/m-editor/marker.h libgui/src/m-editor/octave-qscintilla.cc libgui/src/m-editor/octave-qscintilla.h
diffstat 5 files changed, 95 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
--- a/libgui/src/m-editor/file-editor-tab.cc	Mon Oct 23 20:35:17 2017 -0400
+++ b/libgui/src/m-editor/file-editor-tab.cc	Mon Oct 23 18:21:12 2017 -0400
@@ -235,18 +235,6 @@
   _enc_indicator->setText (_encoding);
   // no changes in encoding yet
   _new_encoding = _encoding;
-
-  // indicators
-  _indicator_highlight_all
-        = _edit_area->indicatorDefine (QsciScintilla::StraightBoxIndicator);
-  if (_indicator_highlight_all == -1)
-    _indicator_highlight_all = 1;
-
-  _edit_area->setIndicatorDrawUnder (true, _indicator_highlight_all);
-
-  _marker_highlight_all = _edit_area->markerDefine (QsciScintilla::Minus);
-  if (_marker_highlight_all == -1)
-    _marker_highlight_all = 1;
 }
 
 file_editor_tab::~file_editor_tab (void)
@@ -803,12 +791,7 @@
   // color indicator for highlighting all occurrences:
   // applications highlight color with more transparency
   QColor hg = QApplication::palette ().color (QPalette::Highlight);
-  hg.setAlphaF (0.25);
-  _edit_area->setIndicatorForegroundColor (hg, _indicator_highlight_all);
-  _edit_area->setIndicatorOutlineColor (hg, _indicator_highlight_all);
-
-  _edit_area->setMarkerBackgroundColor (hg, _marker_highlight_all);
-  _edit_area->setMarkerForegroundColor (hg, _marker_highlight_all);
+  _edit_area->set_selection_marker_color (hg);
 
   // fix line number width with respect to the font size of the lexer and
   // set the line numbers font depending on the lexers font
@@ -2856,11 +2839,13 @@
     {
       // double clicks without modifier
       // clear any existing indicators of this type
-      _edit_area->clear_indicator (_indicator_highlight_all,
-                                   _marker_highlight_all);
+      _edit_area->clear_selection_markers ();
 
       if (_highlight_all_occurrences)
         {
+          // Clear any previous selection.
+          _edit_area->set_word_selection ();
+
           // highlighting of all occurrences of the clicked word is enabled
 
           // get the resulting cursor position
@@ -2902,12 +2887,8 @@
                 {
                   // get cursor position after having found an occurrence
                   _edit_area->getCursorPosition (&oline, &ocol);
-                  // set the indicator
-                  _edit_area->fillIndicatorRange (oline, ocol - wlen,
-                                                  oline, ocol,
-                                                  _indicator_highlight_all);
-
-                  _edit_area->markerAdd (oline, _marker_highlight_all);
+                  // mark the selection
+                  _edit_area->show_selection_markers (oline, ocol, wlen);
 
                   // find next occurrence
                   find_result_available = _edit_area->findNext ();
@@ -2918,6 +2899,7 @@
               _edit_area->setFirstVisibleLine (first_line);
               _edit_area->setCursorPosition (line, col);
               _edit_area->setSelection (line, col - wlen, line, col);
+              _edit_area->set_word_selection (word);
             }
         }
     }
--- a/libgui/src/m-editor/file-editor-tab.h	Mon Oct 23 20:35:17 2017 -0400
+++ b/libgui/src/m-editor/file-editor-tab.h	Mon Oct 23 18:21:12 2017 -0400
@@ -306,8 +306,6 @@
   int _col;
   bool _lines_changed;
   bool _highlight_all_occurrences;
-  int _indicator_highlight_all;
-  int _marker_highlight_all;
   int m_bp_restore_count;
 
 };
--- a/libgui/src/m-editor/marker.h	Mon Oct 23 20:35:17 2017 -0400
+++ b/libgui/src/m-editor/marker.h	Mon Oct 23 18:21:12 2017 -0400
@@ -51,7 +51,8 @@
     unsure_breakpoint,
     bookmark,
     debugger_position,
-    unsure_debugger_position
+    unsure_debugger_position,
+    selection
   };
 
   marker (QsciScintilla *edit_area, int original_linenr,
--- a/libgui/src/m-editor/octave-qscintilla.cc	Mon Oct 23 20:35:17 2017 -0400
+++ b/libgui/src/m-editor/octave-qscintilla.cc	Mon Oct 23 18:21:12 2017 -0400
@@ -44,8 +44,11 @@
 #include <Qsci/qscilexerdiff.h>
 
 #include <Qsci/qscicommandset.h>
+
 #include <QShortcut>
-#include <QMessageBox>
+
+// FIXME: hardwired marker numbers?
+#include "marker.h"
 
 #include "octave-qscintilla.h"
 #include "file-editor-tab.h"
@@ -94,10 +97,14 @@
 }
 
 octave_qscintilla::octave_qscintilla (QWidget *p)
-  : QsciScintilla (p)
+  : QsciScintilla (p), m_word_at_cursor (), m_selection (),
+    m_selection_line (-1), m_selection_col (-1), m_indicator_id (1)
 {
   connect (this, SIGNAL (textChanged (void)), this, SLOT (text_changed (void)));
 
+  connect (this, SIGNAL (cursorPositionChanged (int, int)),
+           this, SLOT (cursor_position_changed (int, int)));
+
   // clear scintilla edit shortcuts that are handled by the editor
   QsciCommandSet *cmd_set = standardCommands ();
 
@@ -186,10 +193,32 @@
     }
 #endif
 
+  // selection markers
+
+  m_indicator_id = indicatorDefine (QsciScintilla::StraightBoxIndicator);
+  if (m_indicator_id == -1)
+    m_indicator_id = 1;
+
+  setIndicatorDrawUnder (true, m_indicator_id);
+
+  markerDefine (QsciScintilla::Minus, marker::selection);
+
   // init state of undo/redo action for this tab
   emit status_update (isUndoAvailable (), isRedoAvailable ());
 }
 
+void
+octave_qscintilla::set_selection_marker_color (const QColor& c)
+{
+  QColor ic = c;
+  ic.setAlphaF (0.25);
+  setIndicatorForegroundColor (ic, m_indicator_id);
+  setIndicatorOutlineColor (ic, m_indicator_id);
+
+  setMarkerForegroundColor (c, marker::selection);
+  setMarkerBackgroundColor (c, marker::selection);
+}
+
 // context menu requested
 void
 octave_qscintilla::contextMenuEvent (QContextMenuEvent *e)
@@ -322,14 +351,14 @@
 
 // helper function for clearing all indicators of a specific style
 void
-octave_qscintilla::clear_indicator (int indicator_style, int marker_style)
+octave_qscintilla::clear_selection_markers (void)
 {
   int end_pos = text ().length ();
   int end_line, end_col;
   lineIndexFromPosition (end_pos, &end_line, &end_col);
-  clearIndicatorRange (0, 0, end_line, end_col, indicator_style);
+  clearIndicatorRange (0, 0, end_line, end_col, m_indicator_id);
 
-  markerDeleteAll (marker_style);
+  markerDeleteAll (marker::selection);
 }
 
 // Function returning the true cursor position where the tab length
@@ -528,6 +557,30 @@
 }
 
 void
+octave_qscintilla::set_word_selection (const QString& word)
+{
+  m_selection = word;
+
+  if (word.isEmpty ())
+    {
+      m_selection_line = -1;
+      m_selection_col = -1;
+
+      clear_selection_markers ();
+    }
+  else
+    getCursorPosition (&m_selection_line, &m_selection_col);
+}
+
+void
+octave_qscintilla::show_selection_markers (int line, int col, int len)
+{
+  fillIndicatorRange (line, col - len, line, col, m_indicator_id);
+
+  markerAdd (line, marker::selection);
+}
+
+void
 octave_qscintilla::contextmenu_help (bool)
 {
   contextmenu_help_doc (false);
@@ -593,6 +646,19 @@
   emit status_update (isUndoAvailable (), isRedoAvailable ());
 }
 
+void
+octave_qscintilla::cursor_position_changed (int line, int col)
+{
+  // Clear the selection if we move away from it.  We have to check the
+  // position, because we allow entering text at the point of the
+  // selection to trigger a search and replace that does not clear the
+  // selection until it is complete.
+
+  if (! m_selection.isEmpty ()
+      && (line != m_selection_line || col != m_selection_col))
+    set_word_selection ();
+}
+
 // when edit area gets focus update information on undo/redo actions
 void octave_qscintilla::focusInEvent (QFocusEvent *focusEvent)
 {
--- a/libgui/src/m-editor/octave-qscintilla.h	Mon Oct 23 20:35:17 2017 -0400
+++ b/libgui/src/m-editor/octave-qscintilla.h	Mon Oct 23 18:21:12 2017 -0400
@@ -26,6 +26,7 @@
 #define octave_octave_qscintilla_h 1
 
 #include <Qsci/qsciscintilla.h>
+
 #include <QMenu>
 #include <QContextMenuEvent>
 
@@ -53,13 +54,19 @@
   void context_run (void);
   void get_global_textcursor_pos (QPoint *global_pos, QPoint *local_pos);
   bool get_actual_word (void);
-  void clear_indicator (int indicator_style, int marker_style);
+  void clear_selection_markers (void);
   void get_current_position (int *pos, int *line, int *col);
   QString comment_string (void);
   int get_style (int pos = -1);
   int is_style_comment (int pos = -1);
   void smart_indent (bool do_smart_indent, int do_auto_close, int line);
 
+  void set_word_selection (const QString& word = QString ());
+
+  void show_selection_markers (int line, int col, int len);
+
+  void set_selection_marker_color (const QColor& c);
+
 signals:
 
   void execute_command_in_terminal_signal (const QString&);
@@ -83,6 +90,7 @@
   void contextmenu_break_once (const QPoint&);
 
   void text_changed (void);
+  void cursor_position_changed (int, int);
 
 protected:
 
@@ -94,6 +102,11 @@
                    const QString& line, QString& first_word);
 
   QString m_word_at_cursor;
+
+  QString m_selection;
+  int m_selection_line;
+  int m_selection_col;
+  int m_indicator_id;
 };
 
 #endif