2 * Copyright 2002-2009, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
11 #include "Constants.h"
12 #include "ReplaceWindow.h"
17 #include <GroupLayoutBuilder.h>
18 #include <GridLayoutBuilder.h>
21 #include <LayoutBuilder.h>
23 #include <Messenger.h>
25 #include <TextControl.h>
28 #undef B_TRANSLATION_CONTEXT
29 #define B_TRANSLATION_CONTEXT "FindandReplaceWindow"
31 ReplaceWindow::ReplaceWindow(BRect frame
, BHandler
* _handler
,
32 BString
* searchString
, BString
* replaceString
,
33 bool caseState
, bool wrapState
, bool backState
)
34 : BWindow(frame
, B_TRANSLATE("Replace"), B_FLOATING_WINDOW
,
35 B_NOT_RESIZABLE
| B_NOT_ZOOMABLE
| B_ASYNCHRONOUS_CONTROLS
36 | B_AUTO_UPDATE_SIZE_LIMITS
, B_CURRENT_WORKSPACE
)
38 AddShortcut('W', B_COMMAND_KEY
, new BMessage(MSG_HIDE_WINDOW
));
40 fSearchString
= new BTextControl("", B_TRANSLATE("Find:"), NULL
, NULL
);
41 fReplaceString
= new BTextControl("", B_TRANSLATE("Replace with:"),
43 fCaseSensBox
= new BCheckBox("", B_TRANSLATE("Case-sensitive"), NULL
);
44 fWrapBox
= new BCheckBox("", B_TRANSLATE("Wrap-around search"), NULL
);
45 fBackSearchBox
= new BCheckBox("", B_TRANSLATE("Search backwards"), NULL
);
46 fAllWindowsBox
= new BCheckBox("", B_TRANSLATE("Replace in all windows"),
47 new BMessage(CHANGE_WINDOW
));
50 fReplaceAllButton
= new BButton("", B_TRANSLATE("Replace all"),
51 new BMessage(MSG_REPLACE_ALL
));
52 fCancelButton
= new BButton("", B_TRANSLATE("Cancel"),
53 new BMessage(MSG_HIDE_WINDOW
));
54 fReplaceButton
= new BButton("", B_TRANSLATE("Replace"),
55 new BMessage(MSG_REPLACE
));
57 SetLayout(new BGroupLayout(B_HORIZONTAL
));
58 AddChild(BGroupLayoutBuilder(B_VERTICAL
, 4)
59 .Add(BGridLayoutBuilder(6, 2)
60 .Add(fSearchString
->CreateLabelLayoutItem(), 0, 0)
61 .Add(fSearchString
->CreateTextViewLayoutItem(), 1, 0)
62 .Add(fReplaceString
->CreateLabelLayoutItem(), 0, 1)
63 .Add(fReplaceString
->CreateTextViewLayoutItem(), 1, 1)
64 .Add(fCaseSensBox
, 1, 2)
66 .Add(fBackSearchBox
, 1, 4)
67 .Add(fAllWindowsBox
, 1, 5)
69 .AddGroup(B_HORIZONTAL
, 10)
70 .Add(fReplaceAllButton
)
75 .SetInsets(10, 10, 10, 10)
78 fReplaceButton
->MakeDefault(true);
82 const char* searchtext
= searchString
->String();
83 const char* replacetext
= replaceString
->String();
85 fSearchString
->SetText(searchtext
);
86 fReplaceString
->SetText(replacetext
);
87 fSearchString
->MakeFocus(true);
89 fCaseSensBox
->SetValue(caseState
? B_CONTROL_ON
: B_CONTROL_OFF
);
90 fWrapBox
->SetValue(wrapState
? B_CONTROL_ON
: B_CONTROL_OFF
);
91 fBackSearchBox
->SetValue(backState
? B_CONTROL_ON
: B_CONTROL_OFF
);
96 ReplaceWindow::MessageReceived(BMessage
* msg
)
100 _SendMessage(MSG_REPLACE
);
105 case MSG_REPLACE_ALL
:
106 _SendMessage(MSG_REPLACE_ALL
);
109 case MSG_HIDE_WINDOW
:
114 BWindow::MessageReceived(msg
);
121 ReplaceWindow::_ChangeUI()
123 fWrapBox
->SetEnabled(fUIchange
);
124 fWrapBox
->SetValue(fUIchange
? B_CONTROL_OFF
: B_CONTROL_ON
);
126 fBackSearchBox
->SetEnabled(fUIchange
);
128 fReplaceButton
->SetEnabled(fUIchange
);
130 fReplaceButton
->MakeDefault(true);
132 fReplaceAllButton
->MakeDefault(true);
134 fUIchange
= !fUIchange
;
139 ReplaceWindow::DispatchMessage(BMessage
* message
, BHandler
* handler
)
141 if (message
->what
== B_KEY_DOWN
) {
143 if (message
->FindInt8("byte", 0, &key
) == B_OK
) {
144 if (key
== B_ESCAPE
) {
145 message
->MakeEmpty();
146 message
->what
= MSG_HIDE_WINDOW
;
148 // This is a hack, but it actually does what is expected,
149 // unlike the hack above. This kind of key filtering probably
150 // ought to be handled by a BMessageFilter, though.
151 BMessenger (this).SendMessage(MSG_HIDE_WINDOW
);
156 BWindow::DispatchMessage(message
, handler
);
161 ReplaceWindow::QuitRequested()
163 BMessenger
messenger(fHandler
);
164 messenger
.SendMessage(MSG_REPLACE_WINDOW_QUIT
);
171 ReplaceWindow::Show()
173 fSearchString
->TextView()->SelectAll();
179 ReplaceWindow::_SendMessage(uint32 what
)
181 BMessage
message(what
);
184 message
.AddString("FindText", fSearchString
->Text());
185 message
.AddString("ReplaceText", fReplaceString
->Text());
187 // Add searchparameters from checkboxes
188 message
.AddBool("casesens", fCaseSensBox
->Value() == B_CONTROL_ON
);
189 message
.AddBool("wrap", fWrapBox
->Value() == B_CONTROL_ON
);
190 message
.AddBool("backsearch", fBackSearchBox
->Value() == B_CONTROL_ON
);
191 message
.AddBool("allwindows", fAllWindowsBox
->Value() == B_CONTROL_ON
);
193 fHandler
->Looper()->PostMessage(&message
, fHandler
);
195 PostMessage(MSG_HIDE_WINDOW
);