xref: /haiku/src/apps/stylededit/FindWindow.cpp (revision 204dee708a999d5a71d0cb9497650ee7cef85d0a)
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, "FindWindow", B_MODAL_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(B_QUIT_REQUESTED));
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(B_QUIT_REQUESTED));
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 B_QUIT_REQUESTED:
82 			Quit();
83 			break;
84 		case MSG_SEARCH:
85 			_SendMessage();
86 			break;
87 
88 		default:
89 			BWindow::MessageReceived(msg);
90 			break;
91 	}
92 }
93 
94 
95 void
96 FindWindow::DispatchMessage(BMessage* message, BHandler* handler)
97 {
98 	if (message->what == B_KEY_DOWN) {
99 		int8 key;
100 		if (message->FindInt8("byte", 0, &key) == B_OK) {
101 			if (key == B_ESCAPE) {
102 				message->MakeEmpty();
103 				message->what = B_QUIT_REQUESTED;
104 			}
105 		}
106 	}
107 
108 	BWindow::DispatchMessage(message, handler);
109 }
110 
111 
112 void
113 FindWindow::_SendMessage()
114 {
115 	BMessage message(MSG_SEARCH);
116 
117 	// Add the string
118 	message.AddString("findtext", fSearchString->Text());
119 
120 	// Add searchparameters from checkboxes
121 	message.AddBool("casesens", fCaseSensBox->Value() == B_CONTROL_ON);
122 	message.AddBool("wrap", fWrapBox->Value() == B_CONTROL_ON);
123 	message.AddBool("backsearch", fBackSearchBox->Value() == B_CONTROL_ON);
124 
125 	fHandler->Looper()->PostMessage(&message, fHandler);
126 
127 	PostMessage(B_QUIT_REQUESTED);
128 }
129