Skip to content

Commit

Permalink
fixed Escape error with opened Multi-Edit Field
Browse files Browse the repository at this point in the history
  • Loading branch information
daddel80 committed Dec 28, 2024
1 parent 6612e17 commit 47e3e32
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 36 deletions.
51 changes: 15 additions & 36 deletions src/MultiReplacePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ void MultiReplace::editTextAt(int itemIndex, ColumnID columnID) {
);

// Create an expand/collapse toggle button next to the edit control
HWND hwndExpandBtnLocal = CreateWindowEx(
hwndExpandBtn = CreateWindowEx(
0,
L"BUTTON",
L"", // Indicator for expand/collapse
Expand All @@ -2099,7 +2099,9 @@ void MultiReplace::editTextAt(int itemIndex, ColumnID columnID) {
);

// Set the _hBoldFont2 to the expand/collapse button
SendMessage(hwndExpandBtnLocal, WM_SETFONT, (WPARAM)_hBoldFont2, TRUE);
if (_hBoldFont2) {
SendMessage(hwndExpandBtn, WM_SETFONT, (WPARAM)_hBoldFont2, TRUE);
}

// Set the initial text of the edit control
wchar_t itemText[MAX_TEXT_LENGTH];
Expand All @@ -2117,11 +2119,8 @@ void MultiReplace::editTextAt(int itemIndex, ColumnID columnID) {
SetFocus(hwndEdit);
SendMessage(hwndEdit, EM_SETSEL, 0, -1);

// Create a context instance to manage the MultiReplace instance and button handle
EditControlContext* ctx = new EditControlContext{ this, hwndExpandBtnLocal };

// Subclass the edit control for custom keyboard handling
SetWindowSubclass(hwndEdit, EditControlSubclassProc, 1, (DWORD_PTR)ctx);
SetWindowSubclass(hwndEdit, EditControlSubclassProc, 1, (DWORD_PTR)this);

// Store the editing state
_editingItemIndex = itemIndex;
Expand All @@ -2135,11 +2134,6 @@ void MultiReplace::closeEditField(bool commitChanges) {
return; // No active edit field present
}

// Retrieve the context associated with hwndEdit
EditControlContext* ctx;
GetWindowSubclass(hwndEdit, EditControlSubclassProc, 1, reinterpret_cast<DWORD_PTR*>(&ctx));
HWND hwndExpandBtn = ctx ? ctx->hwndExpandBtn : nullptr;

if (commitChanges &&
_editingColumnID != ColumnID::INVALID &&
_editingItemIndex >= 0 &&
Expand Down Expand Up @@ -2188,7 +2182,7 @@ void MultiReplace::closeEditField(bool commitChanges) {
DestroyWindow(hwndEdit);
hwndEdit = nullptr;

// destroy the expand button if it exists
// Destroy the expand button if it exists
if (hwndExpandBtn && IsWindow(hwndExpandBtn)) {
DestroyWindow(hwndExpandBtn);
hwndExpandBtn = nullptr;
Expand All @@ -2201,8 +2195,7 @@ void MultiReplace::closeEditField(bool commitChanges) {
_editingColumnID = ColumnID::INVALID;

// Restore hover text only if we suppressed it
if (isHoverTextSuppressed)
{
if (isHoverTextSuppressed) {
isHoverTextSuppressed = false;

// If user has hover text enabled in general, re-add LVS_EX_INFOTIP
Expand All @@ -2215,39 +2208,30 @@ void MultiReplace::closeEditField(bool commitChanges) {
}

LRESULT CALLBACK MultiReplace::EditControlSubclassProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData) {
// Access the instance of MultiReplace
MultiReplace* pThis = reinterpret_cast<MultiReplace*>(dwRefData);
EditControlContext* ctx = reinterpret_cast<EditControlContext*>(dwRefData);

switch (msg) {
case WM_CHAR: {
if (wParam == VK_RETURN) {
// If multiline, let's allow Enter to create a newline:
// do nothing special => DefSubclassProc
// If single-line, we might want to close or commit
// But currently we have ES_MULTILINE always
}
else if (wParam == VK_ESCAPE) {
// Possibly close or revert changes
pThis->closeEditField(false);
case WM_KEYDOWN:
if (wParam == VK_ESCAPE || wParam == VK_TAB) {
pThis->closeEditField(true);
RemoveWindowSubclass(hwnd, EditControlSubclassProc, uIdSubclass);
return 0;
}
break;
}

case WM_KILLFOCUS:
{
HWND newFocus = GetFocus();
if (newFocus == ctx->hwndExpandBtn) {
if (newFocus == hwndExpandBtn) {
return 0;
}

ctx->pThis->closeEditField(true);
pThis->closeEditField(true);
RemoveWindowSubclass(hwnd, EditControlSubclassProc, uIdSubclass);
delete ctx;
return 0;
}


}
return DefSubclassProc(hwnd, msg, wParam, lParam);
}
Expand Down Expand Up @@ -2398,11 +2382,6 @@ LRESULT CALLBACK MultiReplace::ListViewSubclassProc(HWND hwnd, UINT msg, WPARAM

void MultiReplace::toggleEditExpand()
{
// Retrieve context for button handle
EditControlContext* ctx;
GetWindowSubclass(hwndEdit, EditControlSubclassProc, 1, reinterpret_cast<DWORD_PTR*>(&ctx));
HWND hwndExpandBtn = ctx ? ctx->hwndExpandBtn : nullptr;

if (!hwndEdit || !hwndExpandBtn)
return;

Expand Down Expand Up @@ -2434,7 +2413,7 @@ void MultiReplace::toggleEditExpand()

// Update position and size of edit control and button
MoveWindow(hwndEdit, ptLT.x, ptLT.y, curWidth, newHeight, TRUE);
MoveWindow(hwndExpandBtn, ptLT.x + curWidth, ptLT.y - 1 , sx(20), newHeight + 2, TRUE);
MoveWindow(hwndExpandBtn, ptLT.x + curWidth, ptLT.y - 1, sx(20), newHeight + 2, TRUE);

// Bring controls to the top and ensure edit has focus
SetWindowPos(hwndEdit, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
Expand Down
1 change: 1 addition & 0 deletions src/MultiReplacePanel.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ struct EditControlContext
inline std::vector<UndoRedoAction> undoStack;
inline std::vector<UndoRedoAction> redoStack;
inline LuaHashTablesMap hashTablesMap; // Stores hash tables persistently between calls
inline HWND hwndExpandBtn = nullptr;
inline HFONT _hBoldFont2;

inline bool _editIsExpanded = false; // track expand state
Expand Down

0 comments on commit 47e3e32

Please sign in to comment.