[NOTEPAD] Fix and simplify Status Bar handling (#5076)

- Rename DoCreateStatusBar as DoShowHideStatusBar.
- Simplify WM_SIZE handler.
- Enable/disable CMD_STATUSBAR menu item correctly.
- Make DoShowHideStatusBar independent from DoCreateEditWindow.
This commit is contained in:
Katayama Hirofumi MZ 2023-02-17 09:23:25 +09:00 committed by GitHub
parent d04e148d1c
commit 621439d6f2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 141 deletions

View file

@ -911,17 +911,13 @@ VOID DIALOG_EditTimeDate(VOID)
SendMessage(Globals.hEdit, EM_REPLACESEL, TRUE, (LPARAM)szText);
}
VOID DoCreateStatusBar(VOID)
VOID DoShowHideStatusBar(VOID)
{
RECT rc;
RECT rcstatus;
BOOL bStatusBarVisible;
/* Check if status bar object already exists. */
if (Globals.hStatusBar == NULL)
if (Globals.bShowStatusBar && Globals.hStatusBar == NULL)
{
/* Try to create the status bar */
Globals.hStatusBar = CreateStatusWindow(WS_CHILD | WS_VISIBLE | CCS_BOTTOM | SBARS_SIZEGRIP,
Globals.hStatusBar = CreateStatusWindow(WS_CHILD | CCS_BOTTOM | SBARS_SIZEGRIP,
NULL,
Globals.hMainWnd,
CMD_STATUSBAR_WND_ID);
@ -936,59 +932,17 @@ VOID DoCreateStatusBar(VOID)
LoadString(Globals.hInstance, STRING_LINE_COLUMN, Globals.szStatusBarLineCol, MAX_PATH - 1);
}
/* Set status bar visiblity according to the settings. */
if ((Globals.bWrapLongLines != FALSE) || (Globals.bShowStatusBar == FALSE))
{
bStatusBarVisible = FALSE;
ShowWindow(Globals.hStatusBar, SW_HIDE);
}
else
{
bStatusBarVisible = TRUE;
ShowWindow(Globals.hStatusBar, SW_SHOW);
SendMessage(Globals.hStatusBar, WM_SIZE, 0, 0);
}
/* Update layout of controls */
PostMessageW(Globals.hMainWnd, WM_SIZE, 0, 0);
/* Set check state in show status bar item. */
if (bStatusBarVisible)
{
CheckMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_CHECKED);
}
else
{
CheckMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_UNCHECKED);
}
if (Globals.hStatusBar == NULL)
return;
/* Update menu mar with the previous changes */
DrawMenuBar(Globals.hMainWnd);
/* Update visibility of status bar */
ShowWindow(Globals.hStatusBar, (Globals.bShowStatusBar ? SW_SHOWNOACTIVATE : SW_HIDE));
/* Sefety test is edit control exists */
if (Globals.hEdit != NULL)
{
/* Retrieve the sizes of the controls */
GetClientRect(Globals.hMainWnd, &rc);
GetClientRect(Globals.hStatusBar, &rcstatus);
/* If status bar is currently visible, update dimensions of edit control */
if (bStatusBarVisible)
rc.bottom -= (rcstatus.bottom - rcstatus.top);
/* Resize edit control to right size. */
MoveWindow(Globals.hEdit,
rc.left,
rc.top,
rc.right - rc.left,
rc.bottom - rc.top,
TRUE);
}
/* Set the status bar for multiple-text output */
DIALOG_StatusBarAlignParts();
/* Update content with current row/column text */
/* Update status bar contents */
DIALOG_StatusBarUpdateCaretPos();
/* Update line endings and encoding on the status bar */
DIALOG_StatusBarUpdateLineEndings();
DIALOG_StatusBarUpdateEncoding();
}
@ -1032,17 +986,7 @@ VOID DoCreateEditWindow(VOID)
}
/* Update wrap status into the main menu and recover style flags */
if (Globals.bWrapLongLines)
{
dwStyle = EDIT_STYLE_WRAP;
EnableMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
} else {
dwStyle = EDIT_STYLE;
EnableMenuItem(Globals.hMenu, CMD_STATUSBAR, MF_BYCOMMAND | MF_ENABLED);
}
/* Update previous changes */
DrawMenuBar(Globals.hMainWnd);
dwStyle = (Globals.bWrapLongLines ? EDIT_STYLE_WRAP : EDIT_STYLE);
/* Create the new edit control */
Globals.hEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
@ -1057,7 +1001,6 @@ VOID DoCreateEditWindow(VOID)
NULL,
Globals.hInstance,
NULL);
if (Globals.hEdit == NULL)
{
if (pTemp)
@ -1087,28 +1030,22 @@ VOID DoCreateEditWindow(VOID)
GWLP_WNDPROC,
(LONG_PTR)EDIT_WndProc);
/* Create/update status bar */
DoCreateStatusBar();
/* Finally shows new edit control and set focus into it. */
ShowWindow(Globals.hEdit, SW_SHOW);
SetFocus(Globals.hEdit);
/* Re-arrange controls */
PostMessageW(Globals.hMainWnd, WM_SIZE, 0, 0);
}
VOID DIALOG_EditWrap(VOID)
{
Globals.bWrapLongLines = !Globals.bWrapLongLines;
if (Globals.bWrapLongLines)
{
EnableMenuItem(Globals.hMenu, CMD_GOTO, MF_BYCOMMAND | MF_DISABLED | MF_GRAYED);
}
else
{
EnableMenuItem(Globals.hMenu, CMD_GOTO, MF_BYCOMMAND | MF_ENABLED);
}
EnableMenuItem(Globals.hMenu, CMD_GOTO, (Globals.bWrapLongLines ? MF_GRAYED : MF_ENABLED));
DoCreateEditWindow();
DoShowHideStatusBar();
}
VOID DIALOG_SelectFont(VOID)
@ -1274,8 +1211,7 @@ VOID DIALOG_StatusBarUpdateCaretPos(VOID)
VOID DIALOG_ViewStatusBar(VOID)
{
Globals.bShowStatusBar = !Globals.bShowStatusBar;
DoCreateStatusBar();
DoShowHideStatusBar();
}
VOID DIALOG_HelpContents(VOID)

View file

@ -67,5 +67,5 @@ BOOL FileExists(LPCTSTR szFilename);
BOOL HasFileExtension(LPCTSTR szFilename);
BOOL DoCloseFile(VOID);
VOID DoOpenFile(LPCTSTR szFileName);
VOID DoCreateStatusBar(VOID);
VOID DoShowHideStatusBar(VOID);
VOID DoCreateEditWindow(VOID);

View file

@ -285,13 +285,8 @@ static VOID NOTEPAD_InitMenuPopup(HMENU menu, LPARAM index)
UNREFERENCED_PARAMETER(index);
CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_WRAP,
MF_BYCOMMAND | (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
if (!Globals.bWrapLongLines)
{
CheckMenuItem(GetMenu(Globals.hMainWnd), CMD_STATUSBAR,
MF_BYCOMMAND | (Globals.bShowStatusBar ? MF_CHECKED : MF_UNCHECKED));
}
CheckMenuItem(menu, CMD_WRAP, (Globals.bWrapLongLines ? MF_CHECKED : MF_UNCHECKED));
CheckMenuItem(menu, CMD_STATUSBAR, (Globals.bShowStatusBar ? MF_CHECKED : MF_UNCHECKED));
EnableMenuItem(menu, CMD_UNDO,
SendMessage(Globals.hEdit, EM_CANUNDO, 0, 0) ? MF_ENABLED : MF_GRAYED);
EnableMenuItem(menu, CMD_PASTE,
@ -304,7 +299,6 @@ static VOID NOTEPAD_InitMenuPopup(HMENU menu, LPARAM index)
EnableMenuItem(menu, CMD_SELECT_ALL,
GetWindowTextLength(Globals.hEdit) ? MF_ENABLED : MF_GRAYED);
DrawMenuBar(Globals.hMainWnd);
}
LRESULT CALLBACK EDIT_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
@ -394,56 +388,24 @@ NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
case WM_SIZE:
{
if ((Globals.bShowStatusBar != FALSE) && (Globals.bWrapLongLines == FALSE))
RECT rc;
GetClientRect(hWnd, &rc);
if (Globals.bShowStatusBar)
{
RECT rcStatusBar;
HDWP hdwp;
RECT rcStatus;
SendMessageW(Globals.hStatusBar, WM_SIZE, 0, 0);
GetWindowRect(Globals.hStatusBar, &rcStatus);
rc.bottom -= rcStatus.bottom - rcStatus.top;
}
if (!GetWindowRect(Globals.hStatusBar, &rcStatusBar))
break;
hdwp = BeginDeferWindowPos(2);
if (hdwp == NULL)
break;
hdwp = DeferWindowPos(hdwp,
Globals.hEdit,
NULL,
0,
0,
LOWORD(lParam),
HIWORD(lParam) - (rcStatusBar.bottom - rcStatusBar.top),
SWP_NOZORDER | SWP_NOMOVE);
if (hdwp == NULL)
break;
hdwp = DeferWindowPos(hdwp,
Globals.hStatusBar,
NULL,
0,
0,
LOWORD(lParam),
LOWORD(wParam),
SWP_NOZORDER);
if (hdwp == NULL)
break;
EndDeferWindowPos(hdwp);
MoveWindow(Globals.hEdit, 0, 0, rc.right, rc.bottom, TRUE);
if (Globals.bShowStatusBar)
{
/* Align status bar parts, only if the status bar resize operation succeeds */
DIALOG_StatusBarAlignParts();
}
else
SetWindowPos(Globals.hEdit,
NULL,
0,
0,
LOWORD(lParam),
HIWORD(lParam),
SWP_NOZORDER | SWP_NOMOVE);
break;
}
@ -467,10 +429,11 @@ NOTEPAD_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
DoOpenFile(szFileName);
break;
}
case WM_CHAR:
case WM_INITMENUPOPUP:
NOTEPAD_InitMenuPopup((HMENU)wParam, lParam);
break;
default:
if (msg == aFINDMSGSTRING)
{
@ -669,6 +632,7 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE prev, LPTSTR cmdline, int sh
}
DoCreateEditWindow();
DoShowHideStatusBar();
NOTEPAD_InitData();
DIALOG_FileNew();
@ -677,8 +641,6 @@ int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE prev, LPTSTR cmdline, int sh
UpdateWindow(Globals.hMainWnd);
DragAcceptFiles(Globals.hMainWnd, TRUE);
DIALOG_ViewStatusBar();
if (!HandleCommandLine(cmdline))
{
return 0;

View file

@ -157,9 +157,6 @@ void NOTEPAD_LoadSettingsFromRegistry(void)
Globals.main_rect.right = Globals.main_rect.left + dx;
Globals.main_rect.bottom = Globals.main_rect.top + dy;
/* invert value because DIALOG_ViewStatusBar will be called to show it */
Globals.bShowStatusBar = !Globals.bShowStatusBar;
if (dwPointSize != 0)
Globals.lfFont.lfHeight = HeightFromPointSize(dwPointSize);
else
@ -170,7 +167,7 @@ void NOTEPAD_LoadSettingsFromRegistry(void)
else
{
/* If no settings are found in the registry, then use default values */
Globals.bShowStatusBar = FALSE;
Globals.bShowStatusBar = TRUE;
Globals.bWrapLongLines = FALSE;
SetRect(&Globals.lMargins, 750, 1000, 750, 1000);