Added a status bar to track line and column number Added a goto line dialog
-- v11: notepad: Track and display line number and column.
From: Jacob Czekalla jacobczekalla@gmail.com
--- programs/notepad/dialog.c | 1 + programs/notepad/main.c | 42 ++++++++++++++++++++++++++++++++-- programs/notepad/main.h | 3 +++ programs/notepad/notepad.rc | 4 ++++ programs/notepad/notepad_res.h | 1 + 5 files changed, 49 insertions(+), 2 deletions(-)
diff --git a/programs/notepad/dialog.c b/programs/notepad/dialog.c index 94301b82bd3..2227a08cec7 100644 --- a/programs/notepad/dialog.c +++ b/programs/notepad/dialog.c @@ -1107,6 +1107,7 @@ VOID DIALOG_EditWrap(VOID) Globals.bWrapLongLines = !Globals.bWrapLongLines; CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP, MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED)); + updateWindowSize(rc.right, rc.bottom); }
VOID DIALOG_SelectFont(VOID) diff --git a/programs/notepad/main.c b/programs/notepad/main.c index b1cdad8156d..3d266a322e8 100644 --- a/programs/notepad/main.c +++ b/programs/notepad/main.c @@ -63,6 +63,7 @@ static const WCHAR value_iMarginLeft[] = {'i','M','a','r','g','i','n','L',' static const WCHAR value_iMarginRight[] = {'i','M','a','r','g','i','n','R','i','g','h','t','\0'}; static const WCHAR value_szHeader[] = {'s','z','H','e','a','d','e','r','\0'}; static const WCHAR value_szFooter[] = {'s','z','T','r','a','i','l','e','r','\0'}; +static const WCHAR value_bStatusBar[] = {'b','S','t','a','t','u','s','B','a','r','\0'};
/*********************************************************************** * @@ -107,6 +108,34 @@ DWORD get_dpi(void) return dpi; }
+static void ToggleStatusBar(void) +{ + RECT rc; + + Globals.bStatusBar = !Globals.bStatusBar; + CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_SBAR, + MF_BYCOMMAND | (Globals.bStatusBar ? MF_CHECKED : MF_UNCHECKED)); + GetClientRect(Globals.hMainWnd, &rc); + ShowWindow(Globals.hStatusBar, Globals.bStatusBar); + updateWindowSize(rc.right, rc.bottom); +} + +void updateWindowSize(int width, int height) +{ + int StatusBarHeight = 0; + + if(Globals.bStatusBar) + { + RECT SBarRect; + + SendMessageW(Globals.hStatusBar, WM_SIZE, 0, 0); + GetWindowRect(Globals.hStatusBar, &SBarRect); + StatusBarHeight = (SBarRect.bottom - SBarRect.top); + } + SetWindowPos(Globals.hEdit, NULL, 0, 0, width, height - StatusBarHeight, + SWP_NOOWNERZORDER | SWP_NOZORDER); +} + /*********************************************************************** * * NOTEPAD_SaveSettingToRegistry @@ -149,6 +178,7 @@ static VOID NOTEPAD_SaveSettingToRegistry(void) SET_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom); SET_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft); SET_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight); + SET_NOTEPAD_REG(hkey, value_bStatusBar, Globals.bStatusBar); #undef SET_NOTEPAD_REG
/* Store the current value as 10 * twips */ @@ -192,6 +222,7 @@ static VOID NOTEPAD_LoadSettingFromRegistry(void) Globals.iMarginBottom = 2500; Globals.iMarginLeft = 2000; Globals.iMarginRight = 2000; + Globals.bStatusBar = TRUE;
Globals.lfFont.lfHeight = -12; Globals.lfFont.lfWidth = 0; @@ -240,6 +271,7 @@ static VOID NOTEPAD_LoadSettingFromRegistry(void) QUERY_NOTEPAD_REG(hkey, value_iMarginBottom, Globals.iMarginBottom); QUERY_NOTEPAD_REG(hkey, value_iMarginLeft, Globals.iMarginLeft); QUERY_NOTEPAD_REG(hkey, value_iMarginRight, Globals.iMarginRight); + QUERY_NOTEPAD_REG(hkey, value_bStatusBar, Globals.bStatusBar); #undef QUERY_NOTEPAD_REG
main_rect.right = main_rect.left + dx; @@ -302,6 +334,7 @@ static int NOTEPAD_MenuCommand(WPARAM wParam)
case CMD_WRAP: DIALOG_EditWrap(); break; case CMD_FONT: DIALOG_SelectFont(); break; + case CMD_SBAR: ToggleStatusBar(); break;
case CMD_HELP_CONTENTS: DIALOG_HelpContents(); break; case CMD_HELP_ABOUT_NOTEPAD: DIALOG_HelpAboutNotepad(); break; @@ -335,6 +368,9 @@ static VOID NOTEPAD_InitData(VOID)
CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP, MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED)); + CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_SBAR, + MF_BYCOMMAND | (Globals.bStatusBar ? MF_CHECKED : MF_UNCHECKED)); + ShowWindow(Globals.hStatusBar, Globals.bStatusBar); }
/*********************************************************************** @@ -542,6 +578,9 @@ static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam, Globals.hFont = CreateFontIndirectW(&Globals.lfFont); SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE); SendMessageW(Globals.hEdit, EM_LIMITTEXT, 0, 0); + Globals.hStatusBar = CreateWindowExW(0, STATUSCLASSNAMEW, NULL, + WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, NULL, + Globals.hInstance, NULL); break; }
@@ -572,8 +611,7 @@ static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam, break;
case WM_SIZE: - SetWindowPos(Globals.hEdit, NULL, 0, 0, LOWORD(lParam), HIWORD(lParam), - SWP_NOOWNERZORDER | SWP_NOZORDER); + updateWindowSize(LOWORD(lParam), HIWORD(lParam)); break;
case WM_SETFOCUS: diff --git a/programs/notepad/main.h b/programs/notepad/main.h index e0efef0c080..7f417789001 100644 --- a/programs/notepad/main.h +++ b/programs/notepad/main.h @@ -43,6 +43,8 @@ typedef struct HWND hFindReplaceDlg; HWND hEdit; HFONT hFont; /* Font used by the edit control */ + HWND hStatusBar; + INT bStatusBar; LOGFONTW lfFont; BOOL bWrapLongLines; WCHAR szFindText[MAX_PATH]; @@ -71,3 +73,4 @@ extern NOTEPAD_GLOBALS Globals; VOID SetFileNameAndEncoding(LPCWSTR szFileName, ENCODING enc); void NOTEPAD_DoFind(FINDREPLACEW *fr); DWORD get_dpi(void); +void updateWindowSize(int width, int height); diff --git a/programs/notepad/notepad.rc b/programs/notepad/notepad.rc index 6fc12dcf5cd..60cd03d9c0e 100644 --- a/programs/notepad/notepad.rc +++ b/programs/notepad/notepad.rc @@ -56,6 +56,10 @@ POPUP "&Search" { MENUITEM "&Search next\tF3", CMD_SEARCH_NEXT MENUITEM "&Replace...\tCtrl+H", CMD_REPLACE } +POPUP "&View" +{ + MENUITEM "&Status Bar", CMD_SBAR +} POPUP "&Help" { MENUITEM "&Contents\tF1", CMD_HELP_CONTENTS MENUITEM "&About Notepad", CMD_HELP_ABOUT_NOTEPAD diff --git a/programs/notepad/notepad_res.h b/programs/notepad/notepad_res.h index 4fed2f8a3cf..bd994d9eede 100644 --- a/programs/notepad/notepad_res.h +++ b/programs/notepad/notepad_res.h @@ -52,6 +52,7 @@
#define CMD_WRAP 0x119 #define CMD_FONT 0x140 +#define CMD_SBAR 0x205
#define CMD_HELP_CONTENTS 0x130 #define CMD_HELP_ABOUT_NOTEPAD 0x134
From: Jacob Czekalla jacobczekalla@gmail.com
--- programs/notepad/dialog.c | 2 ++ programs/notepad/main.c | 58 ++++++++++++++++++++++++++++++++++ programs/notepad/main.h | 6 ++++ programs/notepad/notepad.rc | 2 ++ programs/notepad/notepad_res.h | 2 ++ 5 files changed, 70 insertions(+)
diff --git a/programs/notepad/dialog.c b/programs/notepad/dialog.c index 2227a08cec7..7091d8f995a 100644 --- a/programs/notepad/dialog.c +++ b/programs/notepad/dialog.c @@ -27,6 +27,7 @@ #include <windows.h> #include <shellapi.h> #include <commdlg.h> +#include <commctrl.h> #include <shlwapi.h> #include <winternl.h>
@@ -1107,6 +1108,7 @@ VOID DIALOG_EditWrap(VOID) Globals.bWrapLongLines = !Globals.bWrapLongLines; CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP, MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED)); + SetWindowSubclass(Globals.hEdit, (SUBCLASSPROC)EDIT_CallBackProc, 0, 0); updateWindowSize(rc.right, rc.bottom); }
diff --git a/programs/notepad/main.c b/programs/notepad/main.c index 3d266a322e8..8939b029b4b 100644 --- a/programs/notepad/main.c +++ b/programs/notepad/main.c @@ -108,6 +108,33 @@ DWORD get_dpi(void) return dpi; }
+void UpdateStatusBar(void) +{ + int currentLine; + int currentCol = -1; + WCHAR statusTxt[256]; + int lineIndex; + DWORD selStart; + DWORD selEnd; + + SendMessageW(Globals.hEdit, EM_GETSEL, (WPARAM)&selStart, (LPARAM)&selEnd); + if(selStart == selEnd) + Globals.trackedSel = selStart; + if(selStart < Globals.trackedSel) + currentCol = selStart; + else + currentCol = selEnd; + currentLine = SendMessageW(Globals.hEdit, EM_LINEFROMCHAR, currentCol, 0); + lineIndex = SendMessageW(Globals.hEdit, EM_LINEINDEX, currentLine, 0); + if(Globals.lastLn != currentLine || Globals.lastCol != currentCol) + { + swprintf(statusTxt, ARRAY_SIZE(statusTxt), Globals.szStatusString, currentLine + 1, (currentCol - lineIndex) + 1); + SendMessageW(Globals.hStatusBar, SB_SETTEXTW, 0, (LPARAM)statusTxt); + Globals.lastLn = currentLine; + Globals.lastCol = currentCol; + } +} + static void ToggleStatusBar(void) { RECT rc; @@ -118,6 +145,7 @@ static void ToggleStatusBar(void) GetClientRect(Globals.hMainWnd, &rc); ShowWindow(Globals.hStatusBar, Globals.bStatusBar); updateWindowSize(rc.right, rc.bottom); + UpdateStatusBar(); }
void updateWindowSize(int width, int height) @@ -528,6 +556,31 @@ static void NOTEPAD_DoReplaceAll(FINDREPLACEW *fr) } }
+LRESULT CALLBACK EDIT_CallBackProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, + UINT_PTR uIdSubclass, DWORD_PTR dwRefData) +{ + switch (msg) + { + case WM_KEYDOWN: + case WM_KEYUP: + case WM_MBUTTONDOWN: + case WM_MBUTTONUP: + case WM_LBUTTONDOWN: + case WM_LBUTTONUP: + UpdateStatusBar(); + break; + case WM_MOUSEMOVE: + if(wParam == MK_LBUTTON) + UpdateStatusBar(); + break; + + default: + break; + } + return DefSubclassProc(hWnd, msg, wParam, lParam); +} + + /*********************************************************************** * * NOTEPAD_WndProc @@ -575,12 +628,17 @@ static LRESULT WINAPI NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam, dwStyle, 0, 0, rc.right, rc.bottom, hWnd, NULL, Globals.hInstance, NULL);
+ SetWindowSubclass(Globals.hEdit, (SUBCLASSPROC)EDIT_CallBackProc, 0, 0); Globals.hFont = CreateFontIndirectW(&Globals.lfFont); SendMessageW(Globals.hEdit, WM_SETFONT, (WPARAM)Globals.hFont, FALSE); SendMessageW(Globals.hEdit, EM_LIMITTEXT, 0, 0); Globals.hStatusBar = CreateWindowExW(0, STATUSCLASSNAMEW, NULL, WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hWnd, NULL, Globals.hInstance, NULL); + LoadStringW(Globals.hInstance, STRING_STATUSBAR, (LPWSTR)&Globals.szStatusString, 0); + Globals.lastLn = -1; + Globals.lastCol = -1; + UpdateStatusBar(); break; }
diff --git a/programs/notepad/main.h b/programs/notepad/main.h index 7f417789001..7d25ee967e8 100644 --- a/programs/notepad/main.h +++ b/programs/notepad/main.h @@ -45,6 +45,7 @@ typedef struct HFONT hFont; /* Font used by the edit control */ HWND hStatusBar; INT bStatusBar; + WCHAR* szStatusString; LOGFONTW lfFont; BOOL bWrapLongLines; WCHAR szFindText[MAX_PATH]; @@ -61,6 +62,9 @@ typedef struct INT iMarginRight; WCHAR szHeader[MAX_PATH]; WCHAR szFooter[MAX_PATH]; + INT trackedSel; + INT lastLn; + INT lastCol;
FINDREPLACEW find; FINDREPLACEW lastFind; @@ -73,4 +77,6 @@ extern NOTEPAD_GLOBALS Globals; VOID SetFileNameAndEncoding(LPCWSTR szFileName, ENCODING enc); void NOTEPAD_DoFind(FINDREPLACEW *fr); DWORD get_dpi(void); +void UpdateStatusBar(void); void updateWindowSize(int width, int height); +LRESULT CALLBACK EDIT_CallBackProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData); diff --git a/programs/notepad/notepad.rc b/programs/notepad/notepad.rc index 60cd03d9c0e..7371dba46f5 100644 --- a/programs/notepad/notepad.rc +++ b/programs/notepad/notepad.rc @@ -85,6 +85,8 @@ STRING_NOTSAVED, "File '%s' has been modified.\n\ Would you like to save the changes?" STRING_NOTFOUND, "'%s' could not be found."
+STRING_STATUSBAR, "Ln %ld, Col %ld" + STRING_UNICODE_LE, "Unicode (UTF-16)" STRING_UNICODE_BE, "Unicode (UTF-16 big-endian)" STRING_UTF8, "Unicode (UTF-8)" diff --git a/programs/notepad/notepad_res.h b/programs/notepad/notepad_res.h index bd994d9eede..416d6d775fd 100644 --- a/programs/notepad/notepad_res.h +++ b/programs/notepad/notepad_res.h @@ -79,6 +79,8 @@
#define STRING_NOTFOUND 0x17B
+#define STRING_STATUSBAR 0x206 + #define STRING_UNICODE_LE 0x180 #define STRING_UNICODE_BE 0x181 #define STRING_UTF8 0x182
I believe the new line is now there, but git doesn't seem to highlight it as a change. If I open the file in GitLab's built-in IDE I can see it.
Huw Davies (@huw) commented about programs/notepad/dialog.c:
Globals.bWrapLongLines = !Globals.bWrapLongLines; CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP, MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
- SetWindowSubclass(Globals.hEdit, (SUBCLASSPROC)EDIT_CallBackProc, 0, 0);
As mentioned last time, you should be able to remove the `(SUBCLASSPROC)` cast both here and in the other call to `SetWindowSubclass()`.