xref: /haiku/src/apps/stylededit/FindWindow.cpp (revision 579c4d6e5f3bcb12e1fd9d7a5c1808602b5f3a58)
1 /*
2  * Copyright 2002-2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Mattias Sundblad
7  *		Andrew Bachmann
8  */
9 
10 
11 #include "Constants.h"
12 #include "FindWindow.h"
13 
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <CheckBox.h>
17 #include <GroupLayoutBuilder.h>
18 #include <GridLayoutBuilder.h>
19 #include <Locale.h>
20 #include <LayoutBuilder.h>
21 #include <String.h>
22 #include <TextControl.h>
23 
24 
25 #undef B_TRANSLATION_CONTEXT
26 #define B_TRANSLATION_CONTEXT "FindandReplaceWindow"
27 
28 
29 FindWindow::FindWindow(BRect frame, BHandler* _handler, BString* searchString,
30 	bool caseState, bool wrapState, bool backState)
31 	: BWindow(frame, B_TRANSLATE("Find"), B_FLOATING_WINDOW,
32 		B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS,
33 		B_CURRENT_WORKSPACE)
34 {
35 	AddShortcut('W', B_COMMAND_KEY, new BMessage(MSG_HIDE_WINDOW));
36 
37 	fSearchString = new BTextControl("", B_TRANSLATE("Find:"), NULL, NULL);
38 	fCaseSensBox = new BCheckBox("", B_TRANSLATE("Case-sensitive"), NULL);
39 	fWrapBox = new BCheckBox("", B_TRANSLATE("Wrap-around search"), NULL);
40 	fBackSearchBox = new BCheckBox("", B_TRANSLATE("Search backwards"), NULL);
41 	fCancelButton = new BButton("", B_TRANSLATE("Cancel"),
42 		new BMessage(MSG_HIDE_WINDOW));
43 	fSearchButton = new BButton("", B_TRANSLATE("Find"),
44 		new BMessage(MSG_SEARCH));
45 
46 	SetLayout(new BGroupLayout(B_HORIZONTAL));
47 	AddChild(BGroupLayoutBuilder(B_VERTICAL, 4)
48 		.Add(BGridLayoutBuilder(6, 2)
49 				.Add(fSearchString->CreateLabelLayoutItem(), 0, 0)
50 				.Add(fSearchString->CreateTextViewLayoutItem(), 1, 0)
51 				.Add(fCaseSensBox, 1, 1)
52 				.Add(fWrapBox, 1, 2)
53 				.Add(fBackSearchBox, 1, 3)
54 				)
55 		.AddGroup(B_HORIZONTAL, 10)
56 			.AddGlue()
57 			.Add(fCancelButton)
58 			.Add(fSearchButton)
59 		.End()
60 		.SetInsets(10, 10, 10, 10)
61 	);
62 
63 	fSearchButton->MakeDefault(true);
64 	fHandler = _handler;
65 
66 	const char* text = searchString->String();
67 
68 	fSearchString->SetText(text);
69 	fSearchString->MakeFocus(true);
70 
71 	fCaseSensBox->SetValue(caseState ? B_CONTROL_ON : B_CONTROL_OFF);
72 	fWrapBox->SetValue(wrapState ? B_CONTROL_ON : B_CONTROL_OFF);
73 	fBackSearchBox->SetValue(backState ? B_CONTROL_ON : B_CONTROL_OFF);
74 }
75 
76 
77 void
78 FindWindow::MessageReceived(BMessage* msg)
79 {
80 	switch (msg->what) {
81 		case MSG_HIDE_WINDOW:
82 			if (!IsHidden())
83 				Hide();
84 			break;
85 		case MSG_SEARCH:
86 			_SendMessage();
87 			break;
88 
89 		default:
90 			BWindow::MessageReceived(msg);
91 			break;
92 	}
93 }
94 
95 
96 void
97 FindWindow::DispatchMessage(BMessage* message, BHandler* handler)
98 {
99 	if (message->what == B_KEY_DOWN) {
100 		int8 key;
101 		if (message->FindInt8("byte", 0, &key) == B_OK) {
102 			if (key == B_ESCAPE) {
103 				message->MakeEmpty();
104 				message->what = MSG_HIDE_WINDOW;
105 			}
106 		}
107 	}
108 
109 	BWindow::DispatchMessage(message, handler);
110 }
111 
112 
113 void
114 FindWindow::_SendMessage()
115 {
116 	BMessage message(MSG_SEARCH);
117 
118 	// Add the string
119 	message.AddString("findtext", fSearchString->Text());
120 
121 	// Add searchparameters from checkboxes
122 	message.AddBool("casesens", fCaseSensBox->Value() == B_CONTROL_ON);
123 	message.AddBool("wrap", fWrapBox->Value() == B_CONTROL_ON);
124 	message.AddBool("backsearch", fBackSearchBox->Value() == B_CONTROL_ON);
125 
126 	fHandler->Looper()->PostMessage(&message, fHandler);
127 
128 	PostMessage(MSG_HIDE_WINDOW);
129 }
130