xref: /haiku/src/apps/terminal/FindWindow.cpp (revision 56eb8e78cc702792e3b032e3f5f45da9e5dbea9e)
1 /*
2  * Copyright 2007, Haiku, Inc.
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 
9 #include "FindWindow.h"
10 
11 #include <Box.h>
12 #include <Button.h>
13 #include <CheckBox.h>
14 #include <RadioButton.h>
15 #include <String.h>
16 #include <TextControl.h>
17 
18 
19 const uint32 MSG_FIND_HIDE = 'Fhid';
20 
21 
22 FindWindow::FindWindow (BRect frame, BMessenger messenger , BString &str,
23 	bool findSelection, bool matchWord, bool matchCase, bool forwardSearch)
24 	: BWindow(frame, "Find", B_FLOATING_WINDOW, B_NOT_RESIZABLE|B_NOT_ZOOMABLE),
25 	fFindDlgMessenger(messenger)
26 {
27 	AddShortcut((ulong)'W', (ulong)B_COMMAND_KEY, new BMessage(MSG_FIND_HIDE));
28 
29 	//Build up view
30 	fFindView = new BView(Bounds(), "FindView", B_FOLLOW_ALL, B_WILL_DRAW);
31 	fFindView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
32 	AddChild(fFindView);
33 
34 	font_height height;
35 	fFindView->GetFontHeight(&height);
36 	float lineHeight = height.ascent + height.descent + height.leading;
37 
38 	//These labels are from the bottom up
39 	float buttonsTop = frame.Height() - 19 - lineHeight;
40 	float matchWordBottom = buttonsTop - 4;
41 	float matchWordTop = matchWordBottom - lineHeight - 8;
42 	float matchCaseBottom = matchWordTop - 4;
43 	float matchCaseTop = matchCaseBottom - lineHeight - 8;
44 	float forwardSearchBottom = matchCaseTop - 4;
45 	float forwardSearchTop = forwardSearchBottom - lineHeight - 8;
46 
47 	//These things are calculated from the top
48 	float textRadioTop = 12;
49 	float textRadioBottom = textRadioTop + 2 + lineHeight + 2 + 1;
50 	float textRadioRight = fFindView->StringWidth("Use Text: ") + 30;
51 	float selectionRadioTop = textRadioBottom + 4;
52 	float selectionRadioBottom = selectionRadioTop + lineHeight + 8;
53 
54 	//Divider
55 	float dividerHeight = (selectionRadioBottom + forwardSearchTop) / 2;
56 
57 	//Button Coordinates
58 	float searchButtonLeft = (frame.Width() - fFindView->StringWidth("Find") - 60) / 2;
59 	float searchButtonRight = searchButtonLeft + fFindView->StringWidth("Find") + 60;
60 
61 	//Build the Views
62 	fTextRadio = new BRadioButton(BRect(14, textRadioTop, textRadioRight, textRadioBottom),
63 		"fTextRadio", "Use Text: ", NULL);
64 	fFindView->AddChild(fTextRadio);
65 
66 	fFindLabel = new BTextControl(BRect(textRadioRight + 4, textRadioTop, frame.Width() - 14, textRadioBottom),
67 		"fFindLabel", "", "", NULL);
68 	fFindLabel->SetDivider(0);
69 	fFindView->AddChild(fFindLabel);
70 	if (!findSelection)
71 		fFindLabel->SetText(str.String());
72 	fFindLabel->MakeFocus(true);
73 
74 	fSelectionRadio = new BRadioButton(BRect(14, selectionRadioTop, frame.Width() - 14, selectionRadioBottom),
75 		"fSelectionRadio", "Use Selection", NULL);
76 	fFindView->AddChild(fSelectionRadio);
77 
78 	if (findSelection)
79 		fSelectionRadio->SetValue(B_CONTROL_ON);
80 	else
81 		fTextRadio->SetValue(B_CONTROL_ON);
82 
83 	fSeparator = new BBox(BRect(6, dividerHeight, frame.Width() - 6, dividerHeight + 1));
84 	fFindView->AddChild(fSeparator);
85 
86 	fForwardSearchBox = new BCheckBox(BRect(14, forwardSearchTop, frame.Width() - 14, forwardSearchBottom),
87 		"fForwardSearchBox", "Search Forward", NULL);
88 	fFindView->AddChild(fForwardSearchBox);
89 	if (forwardSearch)
90 		fForwardSearchBox->SetValue(B_CONTROL_ON);
91 
92 	fMatchCaseBox = new BCheckBox(BRect(14, matchCaseTop, frame.Width() - 14, matchCaseBottom),
93 		"fMatchCaseBox", "Match Case", NULL);
94 	fFindView->AddChild(fMatchCaseBox);
95 	if (matchCase)
96 		fMatchCaseBox->SetValue(B_CONTROL_ON);
97 
98 	fMatchWordBox = new BCheckBox(BRect(14, matchWordTop, frame.Width() - 14, matchWordBottom),
99 		"fMatchWordBox", "Match Word", NULL);
100 	fFindView->AddChild(fMatchWordBox);
101 	if (matchWord)
102 		fMatchWordBox->SetValue(B_CONTROL_ON);
103 
104 	fFindButton = new BButton(BRect(searchButtonLeft, buttonsTop, searchButtonRight, frame.Height() - 14),
105 		"fFindButton", "Find", new BMessage(MSG_FIND));
106 	fFindButton->MakeDefault(true);
107 	fFindView->AddChild(fFindButton);
108 
109 	Show();
110 }
111 
112 
113 FindWindow::~FindWindow()
114 {
115 }
116 
117 
118 void
119 FindWindow::MessageReceived(BMessage *msg)
120 {
121 	switch (msg->what) {
122 		case B_QUIT_REQUESTED:
123 			Quit();
124 			break;
125 
126 		case MSG_FIND:
127 			_SendFindMessage();
128 			break;
129 
130 		case MSG_FIND_HIDE:
131 			Quit();
132 			break;
133 
134 		default:
135 			BWindow::MessageReceived(msg);
136 			break;
137 	}
138 }
139 
140 
141 void
142 FindWindow::Quit()
143 {
144 	fFindDlgMessenger.SendMessage(MSG_FIND_CLOSED);
145 	BWindow::Quit();
146 }
147 
148 
149 void
150 FindWindow::_SendFindMessage()
151 {
152 	BMessage message(MSG_FIND);
153 
154 	if (fTextRadio->Value() == B_CONTROL_ON) {
155 		message.AddString("findstring", fFindLabel->Text());
156 		message.AddBool("findselection", false);
157 	} else
158 		message.AddBool("findselection", true);
159 
160 	//Add the other parameters
161 	message.AddBool("usetext", fTextRadio->Value() == B_CONTROL_ON);
162 	message.AddBool("forwardsearch", fForwardSearchBox->Value() == B_CONTROL_ON);
163 	message.AddBool("matchcase", fMatchCaseBox->Value() == B_CONTROL_ON);
164 	message.AddBool("matchword", fMatchWordBox->Value() == B_CONTROL_ON);
165 
166 	fFindDlgMessenger.SendMessage(&message);
167 }
168