xref: /haiku/src/kits/tracker/SelectionWindow.cpp (revision 18ee9c8397139a401d33e45d971b2143f8c8f8df)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
32 All rights reserved.
33 */
34 
35 
36 #include <Alert.h>
37 #include <Box.h>
38 #include <Catalog.h>
39 #include <ControlLook.h>
40 #include <LayoutBuilder.h>
41 #include <Locale.h>
42 #include <MenuItem.h>
43 #include <PopUpMenu.h>
44 #include <WindowPrivate.h>
45 
46 #include "AutoLock.h"
47 #include "ContainerWindow.h"
48 #include "Commands.h"
49 #include "Screen.h"
50 #include "SelectionWindow.h"
51 
52 
53 const uint32 kSelectButtonPressed = 'sbpr';
54 
55 
56 //	#pragma mark - SelectionWindow
57 
58 
59 #undef B_TRANSLATION_CONTEXT
60 #define B_TRANSLATION_CONTEXT "SelectionWindow"
61 
62 
SelectionWindow(BContainerWindow * window)63 SelectionWindow::SelectionWindow(BContainerWindow* window)
64 	:
65 	BWindow(BRect(0, 0, 270, 0), B_TRANSLATE("Select"),	B_TITLED_WINDOW,
66 		B_NOT_ZOOMABLE | B_NOT_MINIMIZABLE | B_NOT_V_RESIZABLE
67 			| B_NO_WORKSPACE_ACTIVATION | B_ASYNCHRONOUS_CONTROLS
68 			| B_NOT_ANCHORED_ON_ACTIVATE | B_AUTO_UPDATE_SIZE_LIMITS
69 			| B_CLOSE_ON_ESCAPE),
70 	fParentWindow(window)
71 {
72 	if (window->Feel() & kDesktopWindowFeel) {
73 		// The window will not show up if we have
74 		// B_FLOATING_SUBSET_WINDOW_FEEL and use it with the desktop window
75 		// since it's never in front.
76 		SetFeel(B_NORMAL_WINDOW_FEEL);
77 	}
78 
79 	AddToSubset(fParentWindow);
80 
81 	BMenu* menu = new BPopUpMenu("popup");
82 	menu->AddItem(new BMenuItem(B_TRANSLATE("starts with"),	NULL));
83 	menu->AddItem(new BMenuItem(B_TRANSLATE("ends with"), NULL));
84 	menu->AddItem(new BMenuItem(B_TRANSLATE("contains"), NULL));
85 	menu->AddItem(new BMenuItem(B_TRANSLATE("matches wildcard expression"),
86 		NULL));
87 	menu->AddItem(new BMenuItem(B_TRANSLATE("matches regular expression"),
88 		NULL));
89 
90 	menu->SetLabelFromMarked(true);
91 	menu->ItemAt(3)->SetMarked(true);
92 		// Set wildcard matching to default.
93 
94 	// Set up the menu field
95 	fMatchingTypeMenuField = new BMenuField("name", B_TRANSLATE("Name"), menu);
96 
97 	// Set up the expression text control
98 	fExpressionTextControl = new BTextControl("expression", NULL, "", NULL);
99 
100 	// Set up the Invert checkbox
101 	fInverseCheckBox = new BCheckBox("inverse", B_TRANSLATE("Invert"), NULL);
102 	fInverseCheckBox->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
103 
104 	// Set up the Ignore Case checkbox
105 	fIgnoreCaseCheckBox = new BCheckBox(
106 		"ignore", B_TRANSLATE("Ignore case"), NULL);
107 	fIgnoreCaseCheckBox->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
108 	fIgnoreCaseCheckBox->SetValue(1);
109 
110 	// Set up the Select button
111 	fSelectButton = new BButton("select", B_TRANSLATE("Select"),
112 		new BMessage(kSelectButtonPressed));
113 	fSelectButton->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
114 	fSelectButton->MakeDefault(true);
115 
116 	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_HALF_ITEM_SPACING)
117 		.SetInsets(B_USE_WINDOW_SPACING)
118 		.Add(fMatchingTypeMenuField)
119 		.Add(fExpressionTextControl)
120 		.AddGroup(B_HORIZONTAL)
121 			.Add(fInverseCheckBox)
122 			.Add(fIgnoreCaseCheckBox)
123 			.AddGlue(100.0)
124 			.Add(fSelectButton)
125 			.End()
126 		.End();
127 
128 	Run();
129 
130 	Lock();
131 	MoveCloseToMouse();
132 	fExpressionTextControl->MakeFocus(true);
133 	Unlock();
134 }
135 
136 
137 void
MessageReceived(BMessage * message)138 SelectionWindow::MessageReceived(BMessage* message)
139 {
140 	switch (message->what) {
141 		case kSelectButtonPressed:
142 		{
143 			Hide();
144 				// Order of posting and hiding important
145 				// since we want to activate the target
146 				// window when the message arrives.
147 				// (Hide is synhcronous, while PostMessage is not.)
148 				// See PoseView::SelectMatchingEntries().
149 
150 			BMessage* selectionInfo = new BMessage(kSelectMatchingEntries);
151 			selectionInfo->AddInt32("ExpressionType", ExpressionType());
152 			BString expression;
153 			Expression(expression);
154 			selectionInfo->AddString("Expression", expression.String());
155 			selectionInfo->AddBool("InvertSelection", Invert());
156 			selectionInfo->AddBool("IgnoreCase", IgnoreCase());
157 			fParentWindow->PostMessage(selectionInfo);
158 			break;
159 		}
160 
161 		default:
162 			_inherited::MessageReceived(message);
163 			break;
164 	}
165 }
166 
167 
168 bool
QuitRequested()169 SelectionWindow::QuitRequested()
170 {
171 	Hide();
172 	return false;
173 }
174 
175 
176 void
MoveCloseToMouse()177 SelectionWindow::MoveCloseToMouse()
178 {
179 	uint32 buttons;
180 	BPoint mousePosition;
181 
182 	ChildAt((int32)0)->GetMouse(&mousePosition, &buttons);
183 	ConvertToScreen(&mousePosition);
184 
185 	// Position the window centered around the mouse...
186 	BPoint windowPosition = BPoint(mousePosition.x - Frame().Width() / 2,
187 		mousePosition.y	- Frame().Height() / 2);
188 
189 	// ... unless that's outside of the current screen size:
190 	BScreen screen;
191 	windowPosition.x
192 		= MAX(20, MIN(screen.Frame().right - 20 - Frame().Width(),
193 		windowPosition.x));
194 	windowPosition.y = MAX(20,
195 		MIN(screen.Frame().bottom - 20 - Frame().Height(), windowPosition.y));
196 
197 	MoveTo(windowPosition);
198 	SetWorkspaces(1UL << current_workspace());
199 }
200 
201 
202 TrackerStringExpressionType
ExpressionType() const203 SelectionWindow::ExpressionType() const
204 {
205 	if (!fMatchingTypeMenuField->LockLooper())
206 		return kNone;
207 
208 	BMenuItem* item = fMatchingTypeMenuField->Menu()->FindMarked();
209 	if (item == NULL) {
210 		fMatchingTypeMenuField->UnlockLooper();
211 		return kNone;
212 	}
213 
214 	int32 index = fMatchingTypeMenuField->Menu()->IndexOf(item);
215 
216 	fMatchingTypeMenuField->UnlockLooper();
217 
218 	if (index < kStartsWith || index > kRegexpMatch)
219 		return kNone;
220 
221 	TrackerStringExpressionType typeArray[] = {	kStartsWith, kEndsWith,
222 		kContains, kGlobMatch, kRegexpMatch};
223 
224 	return typeArray[index];
225 }
226 
227 
228 void
Expression(BString & result) const229 SelectionWindow::Expression(BString &result) const
230 {
231 	if (!fExpressionTextControl->LockLooper())
232 		return;
233 
234 	result = fExpressionTextControl->Text();
235 
236 	fExpressionTextControl->UnlockLooper();
237 }
238 
239 
240 bool
IgnoreCase() const241 SelectionWindow::IgnoreCase() const
242 {
243 	if (!fIgnoreCaseCheckBox->LockLooper()) {
244 		// default action
245 		return true;
246 	}
247 
248 	bool ignore = fIgnoreCaseCheckBox->Value() != 0;
249 
250 	fIgnoreCaseCheckBox->UnlockLooper();
251 
252 	return ignore;
253 }
254 
255 
256 bool
Invert() const257 SelectionWindow::Invert() const
258 {
259 	if (!fInverseCheckBox->LockLooper()) {
260 		// default action
261 		return false;
262 	}
263 
264 	bool inverse = fInverseCheckBox->Value() != 0;
265 
266 	fInverseCheckBox->UnlockLooper();
267 
268 	return inverse;
269 }
270