diff libgui/src/m-editor/octave-qscintilla.cc @ 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 ff9bd559799b
line wrap: on
line diff
--- 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)
 {