From af3a5a7e5420ed5587c1d9ebd4f80123fcff0bdc Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Tue, 12 Dec 2017 11:16:17 +0100 Subject: [PATCH 1/2] + refactoring: cleanup trigger points for URL Hotspot styling --- src/Edit.c | 18 ++++++------ src/Edit.h | 2 +- src/Notepad3.c | 80 ++++++++++++++++++++++++-------------------------- src/Notepad3.h | 1 + src/Styles.c | 37 +++++++++++------------ src/Styles.h | 2 +- 6 files changed, 67 insertions(+), 73 deletions(-) diff --git a/src/Edit.c b/src/Edit.c index d9b1a9c10..82fe0e3ab 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -5793,10 +5793,8 @@ void EditCompleteWord(HWND hwnd, BOOL autoInsert) { // EditUpdateUrlHotspots() // Find and mark all URL hot-spots // -void EditUpdateUrlHotspots(HWND hwnd, int startPos, int endPos) +void EditUpdateUrlHotspots(HWND hwnd, int startPos, int endPos, BOOL bActiveHotspot) { - if (!bHyperlinkHotspot) return; - const char* pszUrlRegEx = "\\b(?:(?:https?|ftp|file)://|www\\.|ftp\\.)" "(?:\\([-A-Z0-9+&@#/%=~_|$?!:,.]*\\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*" "(?:\\([-A-Z0-9+&@#/%=~_|$?!:,.]*\\)|[A-Z0-9+&@#/%=~_|$])"; @@ -5817,8 +5815,7 @@ void EditUpdateUrlHotspots(HWND hwnd, int startPos, int endPos) int start = startPos; int end = endPos; - while (TRUE) - { + do { int iPos = EditFindInTarget(hwnd, pszUrlRegEx, iRegExLen, SCFIND_NP3_REGEX, &start, &end, FALSE); if (iPos < 0) @@ -5826,14 +5823,18 @@ void EditUpdateUrlHotspots(HWND hwnd, int startPos, int endPos) // mark this match SciCall_StartStyling(iPos); - SciCall_SetStyling((end - start), Style_GetHotspotID(hwnd)); + SciCall_SetStyling((end - start), Style_GetHotspotID()); // next occurrence start = end; end = endPos; - if (start >= end) - break; } + while (start < end); + + if (bActiveHotspot) + UpdateEditWndUI(); + else + SendMessage(hwnd, SCI_COLOURISE, 0, (LPARAM)-1); } @@ -5885,7 +5886,6 @@ void EditMatchBrace(HWND hwnd) int iLine = SciCall_LineFromPosition(iEndStyled); iEndStyled = SciCall_PositionFromLine(iLine); SendMessage(hwnd, SCI_COLOURISE, iEndStyled, -1); - //~EditUpdateUrlHotspots(hwnd, iEndStyled, SciCall_GetLineEndPosition(iLine)); } int iPos = SciCall_GetCurrentPos(); diff --git a/src/Edit.h b/src/Edit.h index 322b1f48e..fac50b463 100644 --- a/src/Edit.h +++ b/src/Edit.h @@ -125,7 +125,7 @@ void EditPrintInit(); void EditMatchBrace(HWND); void EditClearAllMarks(HWND); void EditMarkAll(HWND,char*,int,BOOL,BOOL); -void EditUpdateUrlHotspots(HWND, int, int); +void EditUpdateUrlHotspots(HWND, int, int, BOOL); void EditSetAccelWordNav(HWND,BOOL); void EditCompleteWord(HWND,BOOL); void EditGetBookmarkList(HWND,LPWSTR,int); diff --git a/src/Notepad3.c b/src/Notepad3.c index 73eb39f55..c49c3cc49 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -1031,7 +1031,7 @@ HWND InitInstance(HINSTANCE hInstance,LPSTR pszCmdLine,int nCmdShow) UpdateToolbar(); UpdateStatusbar(); UpdateLineNumberWidth(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); + EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength(), bHyperlinkHotspot); // print file immediately and quit if (flagPrintFileAndLeave) @@ -1175,7 +1175,6 @@ LRESULT CALLBACK MainWndProc(HWND hwnd,UINT umsg,WPARAM wParam,LPARAM lParam) //UpdateToolbar(); //UpdateStatusbar(); //UpdateLineNumberWidth(); - //EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); //if (bPendingChangeNotify) // PostMessage(hwnd,WM_CHANGENOTIFY,0,0); break; @@ -1752,7 +1751,6 @@ void MsgThemeChanged(HWND hwnd,WPARAM wParam,LPARAM lParam) UpdateToolbar(); UpdateStatusbar(); UpdateLineNumberWidth(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); UNUSED(lParam); UNUSED(wParam); @@ -1833,7 +1831,6 @@ void MsgSize(HWND hwnd,WPARAM wParam,LPARAM lParam) UpdateStatusbar(); UpdateLineNumberWidth(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); UNUSED(hwnd); UNUSED(lParam); @@ -1925,7 +1922,6 @@ static DWORD DropFilesProc(CLIPFORMAT cf, HGLOBAL hData, HWND hWnd, DWORD dwKeyS } - //============================================================================= // // MsgCopyData() - Handles WM_COPYDATA @@ -2030,7 +2026,6 @@ LRESULT MsgCopyData(HWND hwnd, WPARAM wParam, LPARAM lParam) UpdateToolbar(); UpdateStatusbar(); UpdateLineNumberWidth(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); } @@ -2533,7 +2528,6 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) //SendMessage(hwndEdit,SCI_SETREADONLY,bReadOnly,0); //UpdateToolbar(); //UpdateStatusbar(); - //EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); if (StringCchLenW(szCurFile,COUNTOF(szCurFile))) { @@ -2889,7 +2883,6 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) UpdateToolbar(); UpdateStatusbar(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); } } break; @@ -2933,7 +2926,6 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) EditFixPositions(hwndEdit); UpdateToolbar(); UpdateStatusbar(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); } break; @@ -3056,7 +3048,6 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) LocalFree(pClip); UpdateToolbar(); UpdateStatusbar(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); } break; @@ -4109,8 +4100,8 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) } else SendMessage(hwndEdit,SCI_SETEDGEMODE,EDGE_NONE,0); + UpdateStatusbar(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); break; @@ -4122,7 +4113,6 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) iLongLinesLimit = max(min(iLongLinesLimit,4096),0); SendMessage(hwndEdit,SCI_SETEDGECOLUMN,iLongLinesLimit,0); UpdateStatusbar(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); iLongLinesLimitG = iLongLinesLimit; } break; @@ -4278,14 +4268,8 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) case IDM_VIEW_MATCHBRACES: bMatchBraces = (bMatchBraces) ? FALSE : TRUE; - if (bMatchBraces) { - struct SCNotification scn; - scn.nmhdr.hwndFrom = hwndEdit; - scn.nmhdr.idFrom = IDC_EDIT; - scn.nmhdr.code = SCN_UPDATEUI; - scn.updated = SC_UPDATE_CONTENT; - SendMessage(hwnd,WM_NOTIFY,IDC_EDIT,(LPARAM)&scn); - } + if (bMatchBraces) + UpdateEditWndUI(); else SendMessage(hwndEdit,SCI_BRACEHIGHLIGHT,(WPARAM)-1,(LPARAM)-1); break; @@ -4304,9 +4288,7 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) case IDM_VIEW_HYPERLINKHOTSPOTS: bHyperlinkHotspot = (bHyperlinkHotspot) ? FALSE : TRUE; Style_SetUrlHotSpot(hwndEdit, bHyperlinkHotspot); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); - if (!bHyperlinkHotspot) - SendMessage(hwndEdit, SCI_COLOURISE, 0, (LPARAM)-1); + EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength(), bHyperlinkHotspot); break; case IDM_VIEW_ZOOMIN: @@ -5372,7 +5354,7 @@ LRESULT MsgCommand(HWND hwnd,WPARAM wParam,LPARAM lParam) //============================================================================= // -// OpenHotSpotURL() - Handles WM_NOTIFY +// OpenHotSpotURL() // // void OpenHotSpotURL(int position, BOOL bForceBrowser) @@ -5497,6 +5479,13 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) } break; + case SCN_STYLENEEDED: // this event needs SCI_SETLEXER(SCLEX_CONTAINER) + { + int lineNumber = SciCall_LineFromPosition(SciCall_GetEndStyled()); + EditUpdateUrlHotspots(hwndEdit, SciCall_PositionFromLine(lineNumber), (int)scn->position, bHyperlinkHotspot); + } + break; + case SCN_UPDATEUI: if (scn->updated & ~(SC_UPDATE_V_SCROLL | SC_UPDATE_H_SCROLL)) { @@ -5506,7 +5495,6 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) if (iMarkOccurrences != 0) { EditMarkAll(hwndEdit, NULL, 0, bMarkOccurrencesMatchCase, bMarkOccurrencesMatchWords); } - // Brace Match if (bMatchBraces) { EditMatchBrace(hwndEdit); @@ -5514,17 +5502,8 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) UpdateToolbar(); UpdateStatusbar(); } - break; // fall-through -> bad responsive UI !!! - - case SCN_STYLENEEDED: // this event needs SCI_SETLEXER(SCLEX_CONTAINER) - { - if (bHyperlinkHotspot) { - int lineNumber = SciCall_LineFromPosition(SciCall_GetEndStyled()); - EditUpdateUrlHotspots(hwndEdit, SciCall_PositionFromLine(lineNumber), (int)scn->position); - } - } break; - + case SCN_CHARADDED: // Auto indent if (bAutoIndent && (scn->ch == '\x0D' || scn->ch == '\x0A')) @@ -5675,11 +5654,11 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) } } else if (scn->modificationType & SC_MOD_CHANGESTYLE) { - EditUpdateUrlHotspots(hwndEdit, (int)scn->position, (int)(scn->position + scn->length)); + EditUpdateUrlHotspots(hwndEdit, (int)scn->position, (int)(scn->position + scn->length), bHyperlinkHotspot); } if (scn->linesAdded != 0) { UpdateLineNumberWidth(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); + EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength(), bHyperlinkHotspot); } bModified = TRUE; break; @@ -5691,7 +5670,7 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) case SCN_SAVEPOINTREACHED: bModified = FALSE; UpdateToolbar(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); + //EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); break; case SCN_MARGINCLICK: @@ -5707,7 +5686,7 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) case SCN_SAVEPOINTLEFT: bModified = TRUE; UpdateToolbar(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); + //EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); break; } break; @@ -7005,11 +6984,28 @@ int CreateIniFileEx(LPCWSTR lpszIniFile) { } + +//============================================================================= +// +// UpdateEditWndUI() +// +void UpdateEditWndUI() +{ + struct SCNotification scn; + scn.nmhdr.hwndFrom = hwndEdit; + scn.nmhdr.idFrom = IDC_EDIT; + scn.nmhdr.code = SCN_UPDATEUI; + scn.updated = SC_UPDATE_CONTENT; + //SendMessage(hwndMain, WM_NOTIFY, IDC_EDIT, (LPARAM)&scn); + PostMessage(hwndMain, WM_NOTIFY, IDC_EDIT, (LPARAM)&scn); +} + + + //============================================================================= // // UpdateToolbar() // -// #define EnableTool(id,b) SendMessage(hwndToolbar,TB_ENABLEBUTTON,id, \ MAKELONG(((b) ? 1 : 0), 0)) @@ -7645,12 +7641,13 @@ BOOL FileLoad(BOOL bDontSave,BOOL bNew,BOOL bReload,BOOL bNoEncDetect,LPCWSTR lp UpdateToolbar(); UpdateStatusbar(); UpdateLineNumberWidth(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); // consistent settings file handling (if loaded in editor) bEnableSaveSettings = (StringCchCompareINW(szCurFile, COUNTOF(szCurFile), szIniFile, COUNTOF(szIniFile)) == 0) ? FALSE : TRUE; UpdateSettingsCmds(); + EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength(), bHyperlinkHotspot); + // Show warning: Unicode file loaded as ANSI if (bUnicodeErr) MsgBox(MBWARN,IDS_ERR_UNICODE); @@ -7813,7 +7810,6 @@ BOOL FileSave(BOOL bSaveAlways,BOOL bAsk,BOOL bSaveAs,BOOL bSaveCopy) Style_SetLexerFromFile(hwndEdit,szCurFile); UpdateStatusbar(); UpdateLineNumberWidth(); - EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); } else { StringCchCopy(tchLastSaveCopyDir,COUNTOF(tchLastSaveCopyDir),tchFile); diff --git a/src/Notepad3.h b/src/Notepad3.h index 9c363e66d..e558ba944 100644 --- a/src/Notepad3.h +++ b/src/Notepad3.h @@ -138,6 +138,7 @@ int CreateIniFile(); int CreateIniFileEx(LPCWSTR); +void UpdateEditWndUI(); void UpdateStatusbar(); void UpdateToolbar(); void UpdateLineNumberWidth(); diff --git a/src/Styles.c b/src/Styles.c index a4fe8e7a5..9553e9ee9 100644 --- a/src/Styles.c +++ b/src/Styles.c @@ -3558,10 +3558,8 @@ void Style_SetLexer(HWND hwnd, PEDITLEXER pLexNew) { SendMessage(hwnd, SCI_COLOURISE, 0, (LPARAM)-1); // override hyperlink hotspot style - if (bHyperlinkHotspot) { - Style_SetUrlHotSpot(hwnd, bHyperlinkHotspot); - EditUpdateUrlHotspots(hwnd, 0, SciCall_GetTextLength()); - } + Style_SetUrlHotSpot(hwnd, bHyperlinkHotspot); + EditUpdateUrlHotspots(hwnd, 0, SciCall_GetTextLength(), bHyperlinkHotspot); // Save current lexer pLexCurrent = pLexNew; @@ -3569,6 +3567,19 @@ void Style_SetLexer(HWND hwnd, PEDITLEXER pLexNew) { } +//============================================================================= +// +// Style_GetHotspotID() +// +int Style_GetHotspotID() +{ + if (bHyperlinkHotspot) { + return (bUse2ndDefaultStyle ? (STYLE_LASTPREDEFINED + STY_URL_HOTSPOT + STY_CNT_LAST) : (STYLE_LASTPREDEFINED + STY_URL_HOTSPOT)); + } + return (bUse2ndDefaultStyle ? (STY_DEFAULT + STY_CNT_LAST) : STY_DEFAULT); +} + + //============================================================================= // // Style_SetUrlHotSpot() @@ -3579,11 +3590,11 @@ void Style_SetUrlHotSpot(HWND hwnd, BOOL bHotSpot) int iIdx = (bUse2ndDefaultStyle) ? STY_CNT_LAST : 0; // Hot Spot settings - const int iStyleHotSpot = (STY_URL_HOTSPOT + iIdx); + const int iStyleHotSpot = Style_GetHotspotID(); if (bHotSpot) { - const WCHAR* lpszStyleHotSpot = lexDefault.Styles[iStyleHotSpot].szValue; + const WCHAR* lpszStyleHotSpot = lexDefault.Styles[STY_URL_HOTSPOT + iIdx].szValue; SendMessage(hwnd, SCI_STYLESETHOTSPOT, iStyleHotSpot, (LPARAM)TRUE); SendMessage(hwnd, SCI_SETHOTSPOTSINGLELINE, TRUE, 0); @@ -5634,18 +5645,4 @@ void Style_SelectLexerDlg(HWND hwnd) Style_SetLexer(hwnd,pLexCurrent); } - -//============================================================================= -// -// Style_GetHotspotID() -// -int Style_GetHotspotID(HWND hwnd) -{ - UNUSED(hwnd); - if (bHyperlinkHotspot) { - return (bUse2ndDefaultStyle ? (STY_URL_HOTSPOT + STY_CNT_LAST) : STY_URL_HOTSPOT); - } - return (bUse2ndDefaultStyle ? (STY_DEFAULT + STY_CNT_LAST) : STY_DEFAULT); -} - // End of Styles.c diff --git a/src/Styles.h b/src/Styles.h index fd580e430..9982fa25f 100644 --- a/src/Styles.h +++ b/src/Styles.h @@ -102,7 +102,7 @@ INT_PTR CALLBACK Styles_ConfigDlgProc(HWND,UINT,WPARAM,LPARAM); void Style_ConfigDlg(HWND); INT_PTR CALLBACK Style_SelectLexerDlgProc(HWND,UINT,WPARAM,LPARAM); void Style_SelectLexerDlg(HWND); -int Style_GetHotspotID(HWND); +int Style_GetHotspotID(); #endif //_NP3_STYLES_H_ From 65b340d7adcfa82eb45155fe274e564dfe55adbc Mon Sep 17 00:00:00 2001 From: Rainer Kottenhoff Date: Tue, 12 Dec 2017 11:28:00 +0100 Subject: [PATCH 2/2] + fix: last hyperlink was restyled to lexer's style (not desired hotspot style) --- src/Edit.c | 2 ++ src/Notepad3.c | 4 ++-- src/Styles.c | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Edit.c b/src/Edit.c index 82fe0e3ab..9bb16ddd3 100644 --- a/src/Edit.c +++ b/src/Edit.c @@ -5831,6 +5831,8 @@ void EditUpdateUrlHotspots(HWND hwnd, int startPos, int endPos, BOOL bActiveHots } while (start < end); + SciCall_StartStyling(endPos); + if (bActiveHotspot) UpdateEditWndUI(); else diff --git a/src/Notepad3.c b/src/Notepad3.c index c49c3cc49..4edb8ffa4 100644 --- a/src/Notepad3.c +++ b/src/Notepad3.c @@ -5670,7 +5670,7 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) case SCN_SAVEPOINTREACHED: bModified = FALSE; UpdateToolbar(); - //EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); + EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength(), bHyperlinkHotspot); break; case SCN_MARGINCLICK: @@ -5686,7 +5686,7 @@ LRESULT MsgNotify(HWND hwnd,WPARAM wParam,LPARAM lParam) case SCN_SAVEPOINTLEFT: bModified = TRUE; UpdateToolbar(); - //EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength()); + EditUpdateUrlHotspots(hwndEdit, 0, SciCall_GetTextLength(), bHyperlinkHotspot); break; } break; diff --git a/src/Styles.c b/src/Styles.c index 9553e9ee9..0a201871e 100644 --- a/src/Styles.c +++ b/src/Styles.c @@ -3557,7 +3557,7 @@ void Style_SetLexer(HWND hwnd, PEDITLEXER pLexNew) { // apply lexer styles SendMessage(hwnd, SCI_COLOURISE, 0, (LPARAM)-1); - // override hyperlink hotspot style + // override lexer style by hyperlink hotspot style Style_SetUrlHotSpot(hwnd, bHyperlinkHotspot); EditUpdateUrlHotspots(hwnd, 0, SciCall_GetTextLength(), bHyperlinkHotspot);