Skip to content
This repository
Browse code

Unfinished version of editable selections.

  • Loading branch information...
commit eb0ce9ff05f1dbd2e15a66dbfa7209c9d51ba121 1 parent 0373ccf
AJ Palkovic ajpalkovic authored

Showing 2 changed files with 237 additions and 0 deletions. Show diff stats Hide diff stats

  1. +172 0 src/styler_selections.cpp
  2. +65 0 src/styler_selections.h
172 src/styler_selections.cpp
... ... @@ -0,0 +1,172 @@
  1 +/*******************************************************************************
  2 + *
  3 + * Copyright (C) 2009, Alexander Stigsen, e-texteditor.com
  4 + *
  5 + * This software is licensed under the Open Company License as described
  6 + * in the file license.txt, which you should have received as part of this
  7 + * distribution. The terms are also available at http://opencompany.org/license.
  8 + *
  9 + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  10 + * KIND, either express or implied.
  11 + *
  12 + ******************************************************************************/
  13 +
  14 +#include "styler_selections.h"
  15 +
  16 +#include "StyleRun.h"
  17 +#include "Lines.h"
  18 +#include "Document.h"
  19 +
  20 +Styler_Selections::Styler_Selections(const DocumentWrapper& rev, const Lines& lines, const tmTheme& theme, EditorCtrl& editorCtrl)
  21 +: m_doc(rev), m_lines(lines),
  22 + m_theme(theme), m_searchHighlightColor(m_theme.searchHighlightColor),
  23 + m_selectionHighlightColor(m_theme.shadowColor) ,
  24 + m_enabled(falsed)
  25 +{
  26 +}
  27 +
  28 +void Styler_Selections::EnableNavigation() {
  29 + m_enabled = !m_enabled;
  30 +
  31 + if(m_enabled) {
  32 + m_nextSelection = -1;
  33 + m_selections = m_editorCtrl.GetSelections();
  34 + NextSelection();
  35 + }
  36 +}
  37 +
  38 +void Styler_Selections::NextSelection() {
  39 + if(!m_enabled) return;
  40 +
  41 + m_nextSelection++;
  42 + if(m_nextSelection >= m_selections.size()) {
  43 + m_nextSelection = 0;
  44 + }
  45 +
  46 + m_editorCtrl.RemoveAllSelections();
  47 + m_editorCtrl.SetPos(m_selections[m_nextSelection].start);
  48 + m_editorCtrlSelect(m_selections[m_nextSelection].start, m_selections[m_nextSelection].end);
  49 + m_editorCtrl.DrawLayout();
  50 +}
  51 +
  52 +void Styler_Selections::PreviousSelection() {
  53 + if(!m_enabled) return;
  54 +
  55 + m_nextSelection-;
  56 + if(m_nextSelection < 0) {
  57 + m_nextSelection = m_selections.size()-1;
  58 + }
  59 +
  60 + m_editorCtrl.RemoveAllSelections();
  61 + m_editorCtrl.SetPos(m_selections[m_nextSelection].start);
  62 + m_editorCtrlSelect(m_selections[m_nextSelection].start, m_selections[m_nextSelection].end);
  63 + m_editorCtrl.DrawLayout();
  64 +}
  65 +
  66 +void Styler_Selections::Style(StyleRun& sr) {
  67 + if(!m_enabled) return;
  68 +
  69 + const unsigned int rstart = sr.GetRunStart();
  70 + const unsigned int rend = sr.GetRunEnd();
  71 +
  72 + for(unsigned int c = 0; c < m_selections.size(); c++) {
  73 + unsigned int start = m_selections[c].start;
  74 + unsigned int end = m_selections[c].end;
  75 +
  76 + if (start > rend) break;
  77 +
  78 + // Check for overlap (or zero-length sel at start-of-line)
  79 + if ((end > rstart && start < rend) || (start == end && end == rstart)) {
  80 + start = wxMax(rstart, start);
  81 + end = wxMin(rend, end);
  82 +
  83 + ApplyStyle(sr, start, end);
  84 + }
  85 + }
  86 +}
  87 +
  88 +void Styler_Selections::ApplyStyle(StyleRun& sr, unsigned int start, unsigned int end) {
  89 + sr.SetBackgroundColor(start, end, m_hlcolor);
  90 + sr.SetShowHidden(start, end, true);
  91 +}
  92 +
  93 +void Styler_Selections::Insert(unsigned int pos, unsigned int length) {
  94 + wxASSERT(0 <= pos && pos < m_doc.GetLength());
  95 + wxASSERT(0 <= length && pos+length <= m_doc.GetLength());
  96 +
  97 + if(!m_enabled) return;
  98 +
  99 + for(unsigned int c = 0; c < m_selections.size(); c++) {
  100 + unsigned int start = m_selections[c].start;
  101 + unsigned int end = m_selections[c].end;
  102 +
  103 + if(start > pos) {
  104 + m_selections[c].start += length;
  105 + m_selections[c].end += length;
  106 + } else if(end > pos) {
  107 + m_selections[c].end += length;
  108 + }
  109 + }
  110 +}
  111 +
  112 +void Styler_Selections::Delete(unsigned int start_pos, unsigned int end_pos) {
  113 + wxASSERT(0 <= start_pos && start_pos <= m_doc.GetLength());
  114 + if(!m_enabled) return;
  115 +
  116 + if (start_pos == end_pos) return;
  117 + wxASSERT(end_pos > start_pos);
  118 +
  119 + // Check if we have deleted the entire document
  120 + if (m_doc.GetLength() == 0) {
  121 + Invalidate();
  122 + return;
  123 + }
  124 +
  125 + // Adjust start & end
  126 + unsigned int length = end_pos - start_pos;
  127 + if (m_search_start >= start_pos) {
  128 + if (m_search_start > end_pos) m_search_start -= length;
  129 + else m_search_start = start_pos;
  130 + }
  131 + if (m_search_end > start_pos) {
  132 + if (m_search_end > end_pos) m_search_end -= length;
  133 + else m_search_end = start_pos;
  134 + }
  135 + else return; // Change after search area, no need to re-search
  136 +
  137 + unsigned int search_start = m_search_start;
  138 +
  139 + if (!m_matches.empty()) {
  140 + if (start_pos >= m_matches.back().end) {
  141 + // Do a new search from end of last match
  142 + DoSearch(search_start, m_search_end, true);
  143 + return;
  144 + }
  145 +
  146 + // Find matches touched by deletion and remove those. Update all following
  147 + bool is_first = true;
  148 + vector<interval>::iterator p = m_matches.begin();
  149 + while (p != m_matches.end()) {
  150 + if (p->end > start_pos) {
  151 + // Remember first valid match before pos
  152 + if (is_first) {
  153 + if (p != m_matches.begin()) search_start = (p-1)->end;
  154 + is_first = false;
  155 + }
  156 +
  157 + if (p->start < end_pos) {
  158 + // pos inside match. Delete and continue
  159 + p = m_matches.erase(p);
  160 + if (p != m_matches.end()) continue; // new iterator
  161 + else break;
  162 + }
  163 + else {
  164 + // Move match to correct position
  165 + p->start -= length;
  166 + p->end -= length;
  167 + }
  168 + }
  169 + ++p;
  170 + }
  171 + }
  172 +}
65 src/styler_selections.h
... ... @@ -0,0 +1,65 @@
  1 +/*******************************************************************************
  2 + *
  3 + * Copyright (C) 2009, Alexander Stigsen, e-texteditor.com
  4 + *
  5 + * This software is licensed under the Open Company License as described
  6 + * in the file license.txt, which you should have received as part of this
  7 + * distribution. The terms are also available at http://opencompany.org/license.
  8 + *
  9 + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  10 + * KIND, either express or implied.
  11 + *
  12 + ******************************************************************************/
  13 +
  14 +#ifndef __STYLER_SELECTIONS_H__
  15 +#define __STYLER_SELECTIONS_H__
  16 +
  17 +#include "wx/wxprec.h"
  18 +#ifndef WX_PRECOMP
  19 + #include <wx/string.h>
  20 + #include <wx/colour.h>
  21 +#endif
  22 +
  23 +#include "Catalyst.h"
  24 +#include "styler.h"
  25 +#include <vector>
  26 +
  27 +
  28 +class EditorCtrl;
  29 +class DocumentWrapper;
  30 +class Document;
  31 +class StyleRun;
  32 +struct tmTheme;
  33 +class Lines;
  34 +
  35 +class Styler_Selections : public Styler {
  36 +public:
  37 +
  38 + Styler_Selections(const DocumentWrapper& rev, const Lines& lines, const tmTheme& theme, EditorCtrl& editorCtrl);
  39 + virtual ~Styler_Selections() {};
  40 +
  41 + void Style(StyleRun& sr);
  42 + // Handle document changes
  43 + void Insert(unsigned int pos, unsigned int length);
  44 + void Delete(unsigned int start_pos, unsigned int end_pos);
  45 +
  46 + void EnableNavigation();
  47 + void NextSelection();
  48 + void PreviousSelection();
  49 +private:
  50 + // Member variables
  51 + const DocumentWrapper& m_doc;
  52 + const Lines& m_lines;
  53 + EditorCtrl& m_editorCtrl;
  54 +
  55 + bool m_enabled;
  56 + int m_nextSelections;
  57 + std::vector<interval> m_selections;
  58 +
  59 + // Theme variables
  60 + const tmTheme& m_theme;
  61 + const wxColour& m_searchHighlightColor;
  62 + const wxColour& m_selectionHighlightColor;
  63 +};
  64 +
  65 +#endif // __STYLER_SELECTIONS_H__

0 comments on commit eb0ce9f

Please sign in to comment.
Something went wrong with that request. Please try again.