|
| 1 | +/* |
| 2 | + * This file is part of Notepad Next. |
| 3 | + * Copyright 2025 Justin Dailey |
| 4 | + * |
| 5 | + * Notepad Next is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Notepad Next is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with Notepad Next. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "MarkerAppDecorator.h" |
| 20 | +#include "EditorManager.h" |
| 21 | +#include "ScintillaNext.h" |
| 22 | + |
| 23 | + |
| 24 | +static QList<QColor> marker_colors = { |
| 25 | + QColor(0x00, 0xFF, 0xFF), |
| 26 | + QColor(0xFF, 0x80, 0x00), |
| 27 | + QColor(0xFF, 0xFF, 0x00) |
| 28 | +}; |
| 29 | + |
| 30 | +static int QColorToScintillaColour(QColor c) |
| 31 | +{ |
| 32 | + return c.red() | (c.green() << 8) | (c.blue() << 16); |
| 33 | +} |
| 34 | + |
| 35 | +MarkerAppDecorator::MarkerAppDecorator(NotepadNextApplication *app) |
| 36 | + : ApplicationDecorator(app) |
| 37 | +{ |
| 38 | + // Any time an editor is created go ahead and allocate/set the required indicators |
| 39 | + connect(app->getEditorManager(), &EditorManager::editorCreated, this, [](ScintillaNext *editor) { |
| 40 | + for (int i = 0; i < marker_colors.size(); i++) { |
| 41 | + int indicator = editor->allocateIndicator(QString("marker_%1").arg(i)); |
| 42 | + editor->indicSetFore(indicator, QColorToScintillaColour(marker_colors[i])); |
| 43 | + editor->indicSetStyle(indicator, INDIC_ROUNDBOX); |
| 44 | + editor->indicSetOutlineAlpha(indicator, 150); |
| 45 | + editor->indicSetAlpha(indicator, 100); |
| 46 | + editor->indicSetUnder(indicator, true); |
| 47 | + } |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +QColor MarkerAppDecorator::markerColor(int i) const |
| 52 | +{ |
| 53 | + return marker_colors[i]; |
| 54 | +} |
| 55 | + |
| 56 | +void MarkerAppDecorator::mark(ScintillaNext *editor, int i) |
| 57 | +{ |
| 58 | + //const int mainSelection = editor->mainSelection(); |
| 59 | + //const int selectionStart = editor->selectionNStart(mainSelection); |
| 60 | + //const int selectionEnd = editor->selectionNEnd(mainSelection); |
| 61 | + |
| 62 | + const int curPos = editor->currentPos(); |
| 63 | + const int wordStart = editor->wordStartPosition(curPos, true); |
| 64 | + const int wordEnd = editor->wordEndPosition(wordStart, true); |
| 65 | + |
| 66 | + // Make sure the selection is on word boundaries |
| 67 | + if (wordStart == wordEnd) { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + int indicator = editor->allocateIndicator(QString("marker_%1").arg(i)); |
| 72 | + editor->setIndicatorCurrent(indicator); |
| 73 | + |
| 74 | + const QByteArray selText = editor->get_text_range(wordStart, wordEnd); |
| 75 | + Sci_TextToFind ttf {{0, (Sci_PositionCR)editor->length()}, selText.constData(), {-1, -1}}; |
| 76 | + const int flags = SCFIND_WHOLEWORD; |
| 77 | + |
| 78 | + while (editor->send(SCI_FINDTEXT, flags, (sptr_t)&ttf) != -1) { |
| 79 | + editor->indicatorFillRange(ttf.chrgText.cpMin, ttf.chrgText.cpMax - ttf.chrgText.cpMin); |
| 80 | + ttf.chrg.cpMin = ttf.chrgText.cpMax; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +void MarkerAppDecorator::clear(ScintillaNext *editor, int i) |
| 85 | +{ |
| 86 | + int indicator = editor->allocateIndicator(QString("marker_%1").arg(i)); |
| 87 | + editor->setIndicatorCurrent(indicator); |
| 88 | + editor->indicatorClearRange(0, editor->length()); |
| 89 | +} |
| 90 | + |
| 91 | +void MarkerAppDecorator::clearAll(ScintillaNext *editor) |
| 92 | +{ |
| 93 | + for (int i = 0; i < marker_colors.size(); i++) { |
| 94 | + clear(editor, i); |
| 95 | + } |
| 96 | +} |
0 commit comments