From 579c4d6e5f3bcb12e1fd9d7a5c1808602b5f3a58 Mon Sep 17 00:00:00 2001 From: Ezo Date: Tue, 26 Nov 2013 18:43:56 +0000 Subject: [PATCH] StyleEdit: non-modal Find/Replace windows implementation * Switch to non-modal floating windows for Find/Replace dialogs, preserve parameters of the previous search in this editor session; * Fixes #10053. - GCI 2013 --- src/apps/stylededit/Constants.h | 1 + src/apps/stylededit/FindWindow.cpp | 15 ++++---- src/apps/stylededit/FindWindow.h | 4 +-- src/apps/stylededit/ReplaceWindow.cpp | 16 +++++---- src/apps/stylededit/ReplaceWindow.h | 6 ++-- src/apps/stylededit/StyledEditWindow.cpp | 44 ++++++++++++++++++------ src/apps/stylededit/StyledEditWindow.h | 3 ++ 7 files changed, 60 insertions(+), 29 deletions(-) diff --git a/src/apps/stylededit/Constants.h b/src/apps/stylededit/Constants.h index 4c955b6bac..7cf1133795 100644 --- a/src/apps/stylededit/Constants.h +++ b/src/apps/stylededit/Constants.h @@ -42,6 +42,7 @@ const uint32 MENU_REPLACE_SAME = 'MErs'; const uint32 MSG_SEARCH = 'msea'; const uint32 MSG_REPLACE = 'msre'; const uint32 MSG_REPLACE_ALL = 'mrea'; +const uint32 MSG_HIDE_WINDOW = 'mhdw'; // "Font"-menu const uint32 FONT_SIZE = 'FMsi'; diff --git a/src/apps/stylededit/FindWindow.cpp b/src/apps/stylededit/FindWindow.cpp index 20187d7b3a..368d146d25 100644 --- a/src/apps/stylededit/FindWindow.cpp +++ b/src/apps/stylededit/FindWindow.cpp @@ -28,18 +28,18 @@ FindWindow::FindWindow(BRect frame, BHandler* _handler, BString* searchString, bool caseState, bool wrapState, bool backState) - : BWindow(frame, "FindWindow", B_MODAL_WINDOW, + : BWindow(frame, B_TRANSLATE("Find"), B_FLOATING_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS, B_CURRENT_WORKSPACE) { - AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); + AddShortcut('W', B_COMMAND_KEY, new BMessage(MSG_HIDE_WINDOW)); fSearchString = new BTextControl("", B_TRANSLATE("Find:"), NULL, NULL); fCaseSensBox = new BCheckBox("", B_TRANSLATE("Case-sensitive"), NULL); fWrapBox = new BCheckBox("", B_TRANSLATE("Wrap-around search"), NULL); fBackSearchBox = new BCheckBox("", B_TRANSLATE("Search backwards"), NULL); fCancelButton = new BButton("", B_TRANSLATE("Cancel"), - new BMessage(B_QUIT_REQUESTED)); + new BMessage(MSG_HIDE_WINDOW)); fSearchButton = new BButton("", B_TRANSLATE("Find"), new BMessage(MSG_SEARCH)); @@ -78,8 +78,9 @@ void FindWindow::MessageReceived(BMessage* msg) { switch (msg->what) { - case B_QUIT_REQUESTED: - Quit(); + case MSG_HIDE_WINDOW: + if (!IsHidden()) + Hide(); break; case MSG_SEARCH: _SendMessage(); @@ -100,7 +101,7 @@ FindWindow::DispatchMessage(BMessage* message, BHandler* handler) if (message->FindInt8("byte", 0, &key) == B_OK) { if (key == B_ESCAPE) { message->MakeEmpty(); - message->what = B_QUIT_REQUESTED; + message->what = MSG_HIDE_WINDOW; } } } @@ -124,5 +125,5 @@ FindWindow::_SendMessage() fHandler->Looper()->PostMessage(&message, fHandler); - PostMessage(B_QUIT_REQUESTED); + PostMessage(MSG_HIDE_WINDOW); } diff --git a/src/apps/stylededit/FindWindow.h b/src/apps/stylededit/FindWindow.h index e8720c8434..d734d0f6ee 100644 --- a/src/apps/stylededit/FindWindow.h +++ b/src/apps/stylededit/FindWindow.h @@ -6,8 +6,8 @@ * Mattias Sundblad * Andrew Bachmann */ -#ifndef FIND_WINDOW_H -#define FIND_WINDOW_H +#ifndef FIND_WINDOW_H +#define FIND_WINDOW_H #include diff --git a/src/apps/stylededit/ReplaceWindow.cpp b/src/apps/stylededit/ReplaceWindow.cpp index 9b63c5be4c..40af5ecdc1 100644 --- a/src/apps/stylededit/ReplaceWindow.cpp +++ b/src/apps/stylededit/ReplaceWindow.cpp @@ -31,11 +31,11 @@ ReplaceWindow::ReplaceWindow(BRect frame, BHandler* _handler, BString* searchString, BString* replaceString, bool caseState, bool wrapState, bool backState) - : BWindow(frame, "ReplaceWindow", B_MODAL_WINDOW, + : BWindow(frame, B_TRANSLATE("Replace"), B_FLOATING_WINDOW, B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS, B_CURRENT_WORKSPACE) { - AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED)); + AddShortcut('W', B_COMMAND_KEY, new BMessage(MSG_HIDE_WINDOW)); fSearchString = new BTextControl("", B_TRANSLATE("Find:"), NULL, NULL); fReplaceString = new BTextControl("", B_TRANSLATE("Replace with:"), @@ -50,7 +50,7 @@ ReplaceWindow::ReplaceWindow(BRect frame, BHandler* _handler, fReplaceAllButton = new BButton("", B_TRANSLATE("Replace all"), new BMessage(MSG_REPLACE_ALL)); fCancelButton = new BButton("", B_TRANSLATE("Cancel"), - new BMessage(B_QUIT_REQUESTED)); + new BMessage(MSG_HIDE_WINDOW)); fReplaceButton = new BButton("", B_TRANSLATE("Replace"), new BMessage(MSG_REPLACE)); @@ -106,6 +106,10 @@ ReplaceWindow::MessageReceived(BMessage* msg) _SendMessage(MSG_REPLACE_ALL); break; + case MSG_HIDE_WINDOW: + if (!IsHidden()) + Hide(); + break; default: BWindow::MessageReceived(msg); break; @@ -139,12 +143,12 @@ ReplaceWindow::DispatchMessage(BMessage* message, BHandler* handler) if (message->FindInt8("byte", 0, &key) == B_OK) { if (key == B_ESCAPE) { message->MakeEmpty(); - message->what = B_QUIT_REQUESTED; + message->what = MSG_HIDE_WINDOW; // This is a hack, but it actually does what is expected, // unlike the hack above. This kind of key filtering probably // ought to be handled by a BMessageFilter, though. - BMessenger (this).SendMessage(B_QUIT_REQUESTED); + BMessenger (this).SendMessage(MSG_HIDE_WINDOW); } } } @@ -170,6 +174,6 @@ ReplaceWindow::_SendMessage(uint32 what) fHandler->Looper()->PostMessage(&message, fHandler); - PostMessage(B_QUIT_REQUESTED); + PostMessage(MSG_HIDE_WINDOW); } diff --git a/src/apps/stylededit/ReplaceWindow.h b/src/apps/stylededit/ReplaceWindow.h index ddcff4c23c..64f0034e2f 100644 --- a/src/apps/stylededit/ReplaceWindow.h +++ b/src/apps/stylededit/ReplaceWindow.h @@ -6,8 +6,8 @@ * Mattias Sundblad * Andrew Bachmann */ -#ifndef REPLACE_WINDOW_H -#define REPLACE_WINDOW_H +#ifndef REPLACE_WINDOW_H +#define REPLACE_WINDOW_H #include @@ -32,7 +32,7 @@ class ReplaceWindow : public BWindow { private: void _SendMessage(uint32 what); - void _ChangeUI(); + void _ChangeUI(); BTextControl *fSearchString; BTextControl *fReplaceString; diff --git a/src/apps/stylededit/StyledEditWindow.cpp b/src/apps/stylededit/StyledEditWindow.cpp index e0b8e069f8..892702ced4 100644 --- a/src/apps/stylededit/StyledEditWindow.cpp +++ b/src/apps/stylededit/StyledEditWindow.cpp @@ -82,7 +82,8 @@ bs_printf(BString* string, const char* format, ...) StyledEditWindow::StyledEditWindow(BRect frame, int32 id, uint32 encoding) - : BWindow(frame, "untitled", B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS) + : BWindow(frame, "untitled", B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS), + fFindWindow(NULL), fReplaceWindow(NULL) { _InitWindow(encoding); BString unTitled(B_TRANSLATE("Untitled ")); @@ -95,7 +96,8 @@ StyledEditWindow::StyledEditWindow(BRect frame, int32 id, uint32 encoding) StyledEditWindow::StyledEditWindow(BRect frame, entry_ref* ref, uint32 encoding) - : BWindow(frame, "untitled", B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS) + : BWindow(frame, "untitled", B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS), + fFindWindow(NULL), fReplaceWindow(NULL) { _InitWindow(encoding); OpenFile(ref); @@ -236,10 +238,20 @@ StyledEditWindow::MessageReceived(BMessage* message) break; case MENU_FIND: { - BRect findWindowFrame(100, 100, 400, 235); - BWindow* window = new FindWindow(findWindowFrame, this, - &fStringToFind, fCaseSensitive, fWrapAround, fBackSearch); - window->Show(); + if (fFindWindow == NULL) { + BRect findWindowFrame(Frame()); + findWindowFrame.InsetBy( + (findWindowFrame.Width() - 400) / 2, + (findWindowFrame.Height() - 235) / 2); + + fFindWindow = new FindWindow(findWindowFrame, this, + &fStringToFind, fCaseSensitive, fWrapAround, fBackSearch); + fFindWindow->Show(); + + } else if (fFindWindow->IsHidden()) + fFindWindow->Show(); + else + fFindWindow->Activate(); break; } case MSG_SEARCH: @@ -259,11 +271,21 @@ StyledEditWindow::MessageReceived(BMessage* message) break; case MENU_REPLACE: { - BRect replaceWindowFrame(100, 100, 400, 284); - BWindow* window = new ReplaceWindow(replaceWindowFrame, this, - &fStringToFind, &fReplaceString, fCaseSensitive, fWrapAround, - fBackSearch); - window->Show(); + if (fReplaceWindow == NULL) { + BRect replaceWindowFrame(Frame()); + replaceWindowFrame.InsetBy( + (replaceWindowFrame.Width() - 400) / 2, + (replaceWindowFrame.Height() - 284) / 2); + + fReplaceWindow = new ReplaceWindow(replaceWindowFrame, this, + &fStringToFind, &fReplaceString, fCaseSensitive, + fWrapAround, fBackSearch); + fReplaceWindow->Show(); + + } else if (fReplaceWindow->IsHidden()) + fReplaceWindow->Show(); + else + fReplaceWindow->Activate(); break; } case MSG_REPLACE: diff --git a/src/apps/stylededit/StyledEditWindow.h b/src/apps/stylededit/StyledEditWindow.h index 2ab80935c8..95e88b6b44 100644 --- a/src/apps/stylededit/StyledEditWindow.h +++ b/src/apps/stylededit/StyledEditWindow.h @@ -171,6 +171,9 @@ private: node_ref fNodeRef; node_ref fFolderNodeRef; bool fNagOnNodeChange; + + BWindow* fFindWindow; + BWindow* fReplaceWindow; };