xref: /haiku/src/apps/terminal/FindWindow.cpp (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
1 /*
2  * Copyright 2007-2009, Haiku, Inc. All rights reserved.
3  * Copyright 2003-2004 Kian Duffy, myob@users.sourceforge.net
4  * Parts Copyright 1998-1999 Kazuho Okui and Takashi Murai.
5  * All rights reserved. Distributed under the terms of the MIT license.
6  */
7 
8 #include "FindWindow.h"
9 
10 
11 #include <Box.h>
12 #include <Button.h>
13 #include <CheckBox.h>
14 #include <ControlLook.h>
15 #include <GridLayoutBuilder.h>
16 #include <GroupLayout.h>
17 #include <GroupLayoutBuilder.h>
18 #include <RadioButton.h>
19 #include <String.h>
20 #include <TextControl.h>
21 
22 
23 const uint32 MSG_FIND_HIDE = 'Fhid';
24 const uint32 TOGGLE_FIND_CONTROL = 'MTFG';
25 const BRect kWindowFrame(10, 30, 250, 200);
26 
27 
28 FindWindow::FindWindow(BMessenger messenger, const BString& str,
29 		bool findSelection, bool matchWord, bool matchCase, bool forwardSearch)
30 	:
31 	BWindow(kWindowFrame, "Find", B_FLOATING_WINDOW,
32 		B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_CLOSE_ON_ESCAPE
33 		| B_AUTO_UPDATE_SIZE_LIMITS),
34 	fFindDlgMessenger(messenger)
35 {
36 	SetLayout(new BGroupLayout(B_VERTICAL));
37 
38 	BBox *separator = new BBox("separator");
39 	separator->SetExplicitMinSize(BSize(250.0, B_SIZE_UNSET));
40 	separator->SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 1.0));
41 
42 	BRadioButton* useSelection = NULL;
43 	const float spacing = be_control_look->DefaultItemSpacing();
44 	BView* layoutView = BGroupLayoutBuilder(B_VERTICAL, 5.0)
45 		.SetInsets(spacing, spacing, spacing, spacing)
46 		.Add(BGridLayoutBuilder()
47 			.Add(fTextRadio = new BRadioButton("Use text:",
48 				new BMessage(TOGGLE_FIND_CONTROL)), 0, 0)
49 			.Add(fFindLabel = new BTextControl(NULL, NULL, NULL), 1, 0)
50 			.Add(useSelection = new BRadioButton("Use selection",
51 				new BMessage(TOGGLE_FIND_CONTROL)), 0, 1))
52 		.Add(separator)
53 		.Add(fForwardSearchBox = new BCheckBox("Search forward"))
54 		.Add(fMatchCaseBox = new BCheckBox("Match case"))
55 		.Add(fMatchWordBox = new BCheckBox("Match word"))
56 		.Add(fFindButton = new BButton("Find", new BMessage(MSG_FIND)))
57 		.End();
58 	AddChild(layoutView);
59 
60 	layoutView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
61 
62 	fFindLabel->SetDivider(0.0);
63 
64 	if (!findSelection) {
65 		fFindLabel->SetText(str.String());
66 		fFindLabel->MakeFocus(true);
67 	} else {
68 		fFindLabel->SetEnabled(false);
69 	}
70 
71 	if (findSelection)
72 		useSelection->SetValue(B_CONTROL_ON);
73 	else
74 		fTextRadio->SetValue(B_CONTROL_ON);
75 
76 	if (forwardSearch)
77 		fForwardSearchBox->SetValue(B_CONTROL_ON);
78 
79 	if (matchCase)
80 		fMatchCaseBox->SetValue(B_CONTROL_ON);
81 
82 	if (matchWord)
83 		fMatchWordBox->SetValue(B_CONTROL_ON);
84 
85 	fFindButton->MakeDefault(true);
86 
87 	AddShortcut((uint32)'W', B_COMMAND_KEY, new BMessage(MSG_FIND_HIDE));
88 
89 	Show();
90 }
91 
92 
93 FindWindow::~FindWindow()
94 {
95 }
96 
97 
98 void
99 FindWindow::MessageReceived(BMessage *msg)
100 {
101 	switch (msg->what) {
102 		case B_QUIT_REQUESTED:
103 			Quit();
104 			break;
105 
106 		case MSG_FIND:
107 			_SendFindMessage();
108 			break;
109 
110 		case MSG_FIND_HIDE:
111 			Quit();
112 			break;
113 
114 		case TOGGLE_FIND_CONTROL:
115 			fFindLabel->SetEnabled(fTextRadio->Value() == B_CONTROL_ON);
116 			break;
117 
118 		default:
119 			BWindow::MessageReceived(msg);
120 			break;
121 	}
122 }
123 
124 
125 void
126 FindWindow::Quit()
127 {
128 	fFindDlgMessenger.SendMessage(MSG_FIND_CLOSED);
129 	BWindow::Quit();
130 }
131 
132 
133 void
134 FindWindow::_SendFindMessage()
135 {
136 	BMessage message(MSG_FIND);
137 
138 	if (fTextRadio->Value() == B_CONTROL_ON) {
139 		message.AddString("findstring", fFindLabel->Text());
140 		message.AddBool("findselection", false);
141 	} else
142 		message.AddBool("findselection", true);
143 
144 	//Add the other parameters
145 	// TODO: "usetext" is never checked for elsewhere and seems
146 	// redundant with "findselection", why is it here?
147 	message.AddBool("usetext", fTextRadio->Value() == B_CONTROL_ON);
148 	message.AddBool("forwardsearch", fForwardSearchBox->Value() == B_CONTROL_ON);
149 	message.AddBool("matchcase", fMatchCaseBox->Value() == B_CONTROL_ON);
150 	message.AddBool("matchword", fMatchWordBox->Value() == B_CONTROL_ON);
151 
152 	fFindDlgMessenger.SendMessage(&message);
153 }
154