| CODENOTIFIER | HelpYou are not signed inSign in |
Project: Notepad++
Revision: 299
Author: donho
Date: 21 Jul 2008 14:05:05
Changes:[ADD] Add UrlHighlighter class files.
Files:| ... | ...@@ -0,0 +1,269 @@ | |
| 1 | //this file is part of notepad++ | |
| 2 | //Copyright (C)2003 Harry <harrybharry@users.sourceforge.net> | |
| 3 | // | |
| 4 | //This program is free software; you can redistribute it and/or | |
| 5 | //modify it under the terms of the GNU General Public License | |
| 6 | //as published by the Free Software Foundation; either | |
| 7 | //version 2 of the License, or (at your option) any later version. | |
| 8 | // | |
| 9 | //This program is distributed in the hope that it will be useful, | |
| 10 | //but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | //GNU General Public License for more details. | |
| 13 | // | |
| 14 | //You should have received a copy of the GNU General Public License | |
| 15 | //along with this program; if not, write to the Free Software | |
| 16 | //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 17 | ||
| 18 | #include "UrlHighlighter.h" | |
| 19 | #include "Parameters.h" | |
| 20 | ||
| 21 | #define MAXLINEHIGHLIGHT 400 //prevent highlighter from doing too much work when a lot is visible | |
| 22 | ||
| 23 | const char *urlHttpRegExpr = "http://[a-z0-9_\\-\\+.:?&@=/%#]*"; | |
| 24 | const char *urlHttpsRegExpr ="https://[a-z0-9_\\-\\+.:?&@=/%#]*"; | |
| 25 | const char *urlFtpRegExpr = "ftp://[a-z0-9_\\-\\+.:?&@=/%#]*"; | |
| 26 | const char *emailRegExpr = "mailto:[a-z0-9._-]+@[a-z0-9.-]+\\.[a-z]+"; | |
| 27 | ||
| 28 | //Used to map style to style with hotspot properties | |
| 29 | struct HotspotStyle { | |
| 30 | int styleID; | |
| 31 | int hotspotID; | |
| 32 | HotspotStyle(int sid, int hid) : styleID(sid), hotspotID(hid) {}; | |
| 33 | }; | |
| 34 | ||
| 35 | UrlHighlighter::UrlHighlighter(FindReplaceDlg * pFRDlg) | |
| 36 | : _pFRDlg(pFRDlg), _isDrawing(false) | |
| 37 | { | |
| 38 | //Nothing to do | |
| 39 | } | |
| 40 | ||
| 41 | ||
| 42 | void UrlHighlighter::highlightView(ScintillaEditView * pHighlightView) | |
| 43 | { | |
| 44 | if (_isDrawing) | |
| 45 | return; | |
| 46 | _isDrawing = true; | |
| 47 | ||
| 48 | const NppGUI & nppGUI = (NppParameters::getInstance())->getNppGUI(); | |
| 49 | //Test if highlight is needed | |
| 50 | int urlAction = (NppParameters::getInstance())->getNppGUI()._styleURL; | |
| 51 | if ((urlAction != 1) && (urlAction != 2)) | |
| 52 | return; | |
| 53 | ||
| 54 | // Save target locations for other search functions | |
| 55 | int loc = (int)pHighlightView->execute(SCI_GETENDSTYLED); | |
| 56 | int originalStartPos = (int)pHighlightView->execute(SCI_GETTARGETSTART); | |
| 57 | int originalEndPos = (int)pHighlightView->execute(SCI_GETTARGETEND); | |
| 58 | ||
| 59 | // Get the range of text visible and highlight everything in it | |
| 60 | int firstLine = (int)pHighlightView->execute(SCI_GETFIRSTVISIBLELINE); | |
| 61 | int nrLines = min((int)pHighlightView->execute(SCI_LINESONSCREEN), MAXLINEHIGHLIGHT ) + 1; | |
| 62 | int lastLine = (int)pHighlightView->execute(SCI_DOCLINEFROMVISIBLE, firstLine + nrLines); | |
| 63 | const int startPos =(int)pHighlightView->execute(SCI_POSITIONFROMLINE, firstLine); | |
| 64 | int endPos = (int)pHighlightView->execute(SCI_POSITIONFROMLINE, lastLine); | |
| 65 | if (endPos == -1) //past EOF | |
| 66 | endPos = (int)pHighlightView->getCurrentDocLen() - 1; | |
| 67 | ||
| 68 | ||
| 69 | int preEndStyled = (int)pHighlightView->execute(SCI_GETENDSTYLED); | |
| 70 | int nextHotspotStyle = 31; //First style to be used for hotspot | |
| 71 | ||
| 72 | vector< HotspotStyle > hotspotStylers; //current stylers that map normal style to style with hotspot | |
| 73 | ||
| 74 | pHighlightView->execute(SCI_SETSEARCHFLAGS, SCFIND_REGEXP|SCFIND_POSIX); | |
| 75 | ||
| 76 | vector<const char *> urlRegExpressions; | |
| 77 | urlRegExpressions.push_back(urlHttpRegExpr); | |
| 78 | urlRegExpressions.push_back(urlHttpsRegExpr); | |
| 79 | urlRegExpressions.push_back(urlFtpRegExpr); | |
| 80 | urlRegExpressions.push_back(emailRegExpr); | |
| 81 | ||
| 82 | int hotSpotStyle = 31; | |
| 83 | ||
| 84 | pHighlightView->execute(SCI_STYLESETFORE, hotSpotStyle, blue); | |
| 85 | pHighlightView->execute(SCI_STYLESETUNDERLINE, hotSpotStyle, TRUE); | |
| 86 | pHighlightView->execute(SCI_STYLESETHOTSPOT, hotSpotStyle, TRUE); | |
| 87 | ||
| 88 | for (size_t k = 0 ; k < urlRegExpressions.size() ; k++) | |
| 89 | { | |
| 90 | pHighlightView->execute(SCI_SETTARGETSTART, startPos); | |
| 91 | pHighlightView->execute(SCI_SETTARGETEND, endPos); | |
| 92 | ||
| 93 | int targetStart = 0; | |
| 94 | int targetEnd = 0; | |
| 95 | targetStart = pHighlightView->execute(SCI_SEARCHINTARGET, strlen(urlRegExpressions[k]), (LPARAM)urlRegExpressions[k]); | |
| 96 | while (targetStart != -1) | |
| 97 | { | |
| 98 | targetStart = (int)(pHighlightView->execute(SCI_GETTARGETSTART)); | |
| 99 | targetEnd = (int)(pHighlightView->execute(SCI_GETTARGETEND)); | |
| 100 | int foundTextLen = targetEnd - targetStart; | |
| 101 | int idStyle = pHighlightView->execute(SCI_GETSTYLEAT, targetStart); | |
| 102 | ||
| 103 | pHighlightView->execute(SCI_STARTSTYLING, targetStart, 0xFF); | |
| 104 | pHighlightView->execute(SCI_SETSTYLING, foundTextLen, hotSpotStyle); | |
| 105 | ||
| 106 | int startMovingPos = targetStart + foundTextLen; | |
| 107 | ||
| 108 | if (startMovingPos >= endPos) | |
| 109 | { | |
| 110 | targetStart = -1; | |
| 111 | } | |
| 112 | else | |
| 113 | { | |
| 114 | pHighlightView->execute(SCI_SETTARGETSTART, startMovingPos); | |
| 115 | pHighlightView->execute(SCI_SETTARGETEND, endPos); | |
| 116 | targetStart = pHighlightView->execute(SCI_SEARCHINTARGET, strlen(urlRegExpressions[k]), (LPARAM)urlRegExpressions[k]); | |
| 117 | } | |
| 118 | } | |
| 119 | } | |
| 120 | pHighlightView->execute(SCI_STARTSTYLING, preEndStyled, 0xFF); | |
| 121 | ||
| 122 | // restore the original targets to avoid conflicts with the search/replace functions | |
| 123 | pHighlightView->execute(SCI_SETTARGETSTART, originalStartPos); | |
| 124 | pHighlightView->execute(SCI_SETTARGETEND, originalEndPos); | |
| 125 | _isDrawing = false; | |
| 126 | } | |
| 127 | ||
| 128 | ||
| 129 | /* | |
| 130 | void UrlHighlighter::highlightView(ScintillaEditView * pHighlightView) | |
| 131 | { | |
| 132 | if (_isDrawing) | |
| 133 | return; | |
| 134 | _isDrawing = true; | |
| 135 | ||
| 136 | const NppGUI & nppGUI = (NppParameters::getInstance())->getNppGUI(); | |
| 137 | //Test if highlight is needed | |
| 138 | int urlAction = (NppParameters::getInstance())->getNppGUI()._styleURL; | |
| 139 | if ((urlAction != 1) && (urlAction != 2)) | |
| 140 | return; | |
| 141 | ||
| 142 | // Save target locations for other search functions | |
| 143 | int loc = (int)pHighlightView->execute(SCI_GETENDSTYLED); | |
| 144 | int originalStartPos = (int)pHighlightView->execute(SCI_GETTARGETSTART); | |
| 145 | int originalEndPos = (int)pHighlightView->execute(SCI_GETTARGETEND); | |
| 146 | ||
| 147 | // Get the range of text visible and highlight everything in it | |
| 148 | int firstLine = (int)pHighlightView->execute(SCI_GETFIRSTVISIBLELINE); | |
| 149 | int nrLines = min((int)pHighlightView->execute(SCI_LINESONSCREEN), MAXLINEHIGHLIGHT ) + 1; | |
| 150 | int lastLine = (int)pHighlightView->execute(SCI_DOCLINEFROMVISIBLE, firstLine + nrLines); | |
| 151 | const int startPos =(int)pHighlightView->execute(SCI_POSITIONFROMLINE, firstLine); | |
| 152 | int endPos = (int)pHighlightView->execute(SCI_POSITIONFROMLINE, lastLine); | |
| 153 | if (endPos == -1) //past EOF | |
| 154 | endPos = (int)pHighlightView->getCurrentDocLen() - 1; | |
| 155 | ||
| 156 | ||
| 157 | int preEndStyled = (int)pHighlightView->execute(SCI_GETENDSTYLED); | |
| 158 | int nextHotspotStyle = 31; //First style to be used for hotspot | |
| 159 | ||
| 160 | vector< HotspotStyle > hotspotStylers; //current stylers that map normal style to style with hotspot | |
| 161 | ||
| 162 | pHighlightView->execute(SCI_SETSEARCHFLAGS, SCFIND_REGEXP|SCFIND_POSIX); | |
| 163 | ||
| 164 | vector<const char *> urlRegExpressions; | |
| 165 | urlRegExpressions.push_back(urlHttpRegExpr); | |
| 166 | urlRegExpressions.push_back(urlHttpsRegExpr); | |
| 167 | urlRegExpressions.push_back(urlFtpRegExpr); | |
| 168 | urlRegExpressions.push_back(emailRegExpr); | |
| 169 | ||
| 170 | for (size_t k = 0 ; k < urlRegExpressions.size() ; k++) | |
| 171 | { | |
| 172 | pHighlightView->execute(SCI_SETTARGETSTART, startPos); | |
| 173 | pHighlightView->execute(SCI_SETTARGETEND, endPos); | |
| 174 | ||
| 175 | int targetStart = 0; | |
| 176 | int targetEnd = 0; | |
| 177 | targetStart = pHighlightView->execute(SCI_SEARCHINTARGET, strlen(urlRegExpressions[k]), (LPARAM)urlRegExpressions[k]); | |
| 178 | while (targetStart != -1) | |
| 179 | { | |
| 180 | targetStart = (int)(pHighlightView->execute(SCI_GETTARGETSTART)); | |
| 181 | targetEnd = (int)(pHighlightView->execute(SCI_GETTARGETEND)); | |
| 182 | int foundTextLen = targetEnd - targetStart; | |
| 183 | int idStyle = pHighlightView->execute(SCI_GETSTYLEAT, targetStart); | |
| 184 | ||
| 185 | int foundStyle = -1; | |
| 186 | for (size_t i = 0 ; i < hotspotStylers.size() ; i++) | |
| 187 | { | |
| 188 | if (hotspotStylers[i].styleID == idStyle) | |
| 189 | { | |
| 190 | foundStyle = hotspotStylers[i].hotspotID; | |
| 191 | break; | |
| 192 | } | |
| 193 | if (hotspotStylers[i].hotspotID == idStyle) | |
| 194 | { | |
| 195 | foundStyle = hotspotStylers[i].hotspotID; | |
| 196 | break; | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 200 | if (foundStyle == -1) //no hotspot style for this style, create one | |
| 201 | { | |
| 202 | if (nextHotspotStyle == 0) | |
| 203 | { | |
| 204 | //no style slots left | |
| 205 | } | |
| 206 | else | |
| 207 | { | |
| 208 | hotspotStylers.push_back(HotspotStyle(idStyle, nextHotspotStyle)); | |
| 209 | ||
| 210 | char fontName[256]; | |
| 211 | Style hotspotStyle; | |
| 212 | ||
| 213 | hotspotStyle._styleID = nextHotspotStyle; | |
| 214 | pHighlightView->execute(SCI_STYLEGETFONT, idStyle, (LPARAM)fontName); | |
| 215 | hotspotStyle._fontName = &fontName[0]; | |
| 216 | hotspotStyle._fgColor = pHighlightView->execute(SCI_STYLEGETFORE, idStyle); | |
| 217 | hotspotStyle._bgColor = pHighlightView->execute(SCI_STYLEGETBACK, idStyle); | |
| 218 | hotspotStyle._fontSize = pHighlightView->execute(SCI_STYLEGETSIZE, idStyle); | |
| 219 | ||
| 220 | int isBold = pHighlightView->execute(SCI_STYLEGETBOLD, idStyle); | |
| 221 | int isItalic = pHighlightView->execute(SCI_STYLEGETITALIC, idStyle); | |
| 222 | int isUnderline = pHighlightView->execute(SCI_STYLEGETUNDERLINE, idStyle); | |
| 223 | hotspotStyle._fontStyle = (isBold?FONTSTYLE_BOLD:0) | (isItalic?FONTSTYLE_ITALIC:0) | (isUnderline?FONTSTYLE_UNDERLINE:0); | |
| 224 | ||
| 225 | if (urlAction == 2) | |
| 226 | hotspotStyle._fontStyle |= FONTSTYLE_UNDERLINE; //force underline | |
| 227 | ||
| 228 | //Apply style | |
| 229 | pHighlightView->execute(SCI_STYLESETFORE, nextHotspotStyle, hotspotStyle._fgColor); | |
| 230 | pHighlightView->execute(SCI_STYLESETBACK, nextHotspotStyle, hotspotStyle._bgColor); | |
| 231 | pHighlightView->execute(SCI_STYLESETFONT, nextHotspotStyle, (LPARAM)hotspotStyle._fontName); | |
| 232 | pHighlightView->execute(SCI_STYLESETBOLD, nextHotspotStyle, hotspotStyle._fontStyle & FONTSTYLE_BOLD); | |
| 233 | pHighlightView->execute(SCI_STYLESETITALIC, nextHotspotStyle, hotspotStyle._fontStyle & FONTSTYLE_ITALIC); | |
| 234 | pHighlightView->execute(SCI_STYLESETUNDERLINE, nextHotspotStyle, hotspotStyle._fontStyle & FONTSTYLE_UNDERLINE); | |
| 235 | pHighlightView->execute(SCI_STYLESETSIZE, nextHotspotStyle, hotspotStyle._fontSize); | |
| 236 | pHighlightView->execute(SCI_STYLESETHOTSPOT, nextHotspotStyle, TRUE); | |
| 237 | ||
| 238 | foundStyle = nextHotspotStyle; | |
| 239 | nextHotspotStyle--; | |
| 240 | } | |
| 241 | } | |
| 242 | else //if a style could be found, apply it | |
| 243 | { | |
| 244 | pHighlightView->execute(SCI_STARTSTYLING, targetStart, 0xFF); | |
| 245 | pHighlightView->execute(SCI_SETSTYLING, foundTextLen, foundStyle); | |
| 246 | } | |
| 247 | ||
| 248 | int startMovingPos = targetStart + foundTextLen; | |
| 249 | ||
| 250 | if (startMovingPos >= endPos) | |
| 251 | { | |
| 252 | targetStart = -1; | |
| 253 | } | |
| 254 | else | |
| 255 | { | |
| 256 | pHighlightView->execute(SCI_SETTARGETSTART, startMovingPos); | |
| 257 | pHighlightView->execute(SCI_SETTARGETEND, endPos); | |
| 258 | targetStart = pHighlightView->execute(SCI_SEARCHINTARGET, strlen(urlRegExpressions[k]), (LPARAM)urlRegExpressions[k]); | |
| 259 | } | |
| 260 | } | |
| 261 | } | |
| 262 | pHighlightView->execute(SCI_STARTSTYLING, preEndStyled, 0xFF); | |
| 263 | ||
| 264 | // restore the original targets to avoid conflicts with the search/replace functions | |
| 265 | pHighlightView->execute(SCI_SETTARGETSTART, originalStartPos); | |
| 266 | pHighlightView->execute(SCI_SETTARGETEND, originalEndPos); | |
| 267 | _isDrawing = false; | |
| 268 | } | |
| 269 | */ |
| ... | ...@@ -0,0 +1,33 @@ | |
| 1 | //this file is part of notepad++ | |
| 2 | //Copyright (C)2003 Harry <harrybharry@users.sourceforge.net> | |
| 3 | // | |
| 4 | //This program is free software; you can redistribute it and/or | |
| 5 | //modify it under the terms of the GNU General Public License | |
| 6 | //as published by the Free Software Foundation; either | |
| 7 | //version 2 of the License, or (at your option) any later version. | |
| 8 | // | |
| 9 | //This program is distributed in the hope that it will be useful, | |
| 10 | //but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 | //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 | //GNU General Public License for more details. | |
| 13 | // | |
| 14 | //You should have received a copy of the GNU General Public License | |
| 15 | //along with this program; if not, write to the Free Software | |
| 16 | //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
| 17 | ||
| 18 | #ifndef URLHIGHLIGHTER_H | |
| 19 | #define URLHIGHLIGHTER_H | |
| 20 | ||
| 21 | #include "ScintillaEditView.h" | |
| 22 | #include "FindReplaceDlg.h" | |
| 23 | ||
| 24 | class UrlHighlighter { | |
| 25 | public: | |
| 26 | UrlHighlighter(FindReplaceDlg * pFRDlg); | |
| 27 | void highlightView(ScintillaEditView * pHighlightView); | |
| 28 | private: | |
| 29 | FindReplaceDlg * _pFRDlg; | |
| 30 | bool _isDrawing; | |
| 31 | }; | |
| 32 | ||
| 33 | #endif //URLHIGHLIGHTER_H |