xref: /haiku/src/apps/stylededit/FindWindow.cpp (revision 31139022cd3056691fc8d6f64b2d9ffa9a101de3)
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 <CheckBox.h>
16 #include <String.h>
17 #include <TextControl.h>
18 
19 
20 FindWindow::FindWindow(BRect frame, BHandler *_handler, BString *searchString,
21 	bool *caseState, bool *wrapState, bool *backState)
22 	: BWindow(frame, "FindWindow", B_MODAL_WINDOW,
23 		B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS,
24 		B_CURRENT_WORKSPACE)
25 {
26 	fFindView = new BView(Bounds(), "FindView", B_FOLLOW_ALL, B_WILL_DRAW);
27 	fFindView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
28 	AddChild(fFindView);
29 
30 	font_height height;
31 	fFindView->GetFontHeight(&height);
32 	float lineHeight = height.ascent+height.descent + height.leading;
33 
34 	float findWidth = fFindView->StringWidth("Find:") + 6;
35 
36 	float searchBottom = 12 + 2 + lineHeight + 2 + 1;
37 	float buttonTop = frame.Height() - 19 - lineHeight;
38 	float wrapBoxTop = (buttonTop + searchBottom - lineHeight) / 2;
39 	float wrapBoxBottom = (buttonTop + searchBottom + lineHeight) / 2;
40 	float caseBoxTop = (searchBottom + wrapBoxTop - lineHeight) / 2;
41 	float caseBoxBottom = (searchBottom + wrapBoxTop + lineHeight) / 2;
42 	float backBoxTop = (buttonTop + wrapBoxBottom - lineHeight) / 2;
43 	float backBoxBottom = (buttonTop + wrapBoxBottom + lineHeight) / 2;
44 
45 	fFindView->AddChild(fSearchString = new BTextControl(BRect(14, 12,
46 		frame.Width() - 10, searchBottom), "", "Find:", NULL, NULL));
47 	fSearchString->SetDivider(findWidth);
48 
49 	fFindView->AddChild(fCaseSensBox = new BCheckBox(BRect(16 + findWidth, caseBoxTop,
50 		frame.Width() - 12, caseBoxBottom), "", "Case-sensitive", NULL));
51 	fFindView->AddChild(fWrapBox = new BCheckBox(BRect(16 + findWidth, wrapBoxTop,
52 		frame.Width() - 12, wrapBoxBottom), "", "Wrap-around search", NULL));
53 	fFindView->AddChild(fBackSearchBox = new BCheckBox(BRect(16 + findWidth,
54 		backBoxTop, frame.Width() - 12, backBoxBottom), "", "Search backwards", NULL));
55 
56 	fFindView->AddChild(fCancelButton = new BButton(BRect(142, buttonTop, 212,
57 		frame.Height() - 7), "", "Cancel", new BMessage(B_QUIT_REQUESTED)));
58 	fFindView->AddChild(fSearchButton = new BButton(BRect(221, buttonTop, 291,
59 		frame.Height() - 7), "", "Find", new BMessage(MSG_SEARCH)));
60 
61 	fSearchButton->MakeDefault(true);
62 	fHandler = _handler;
63 
64 	const char *text = searchString->String();
65 
66     fSearchString->SetText(text);
67 	fSearchString->MakeFocus(true);
68 
69 	fCaseSensBox->SetValue(*caseState ? B_CONTROL_ON : B_CONTROL_OFF);
70 	fWrapBox->SetValue(*wrapState ? B_CONTROL_ON : B_CONTROL_OFF);
71 	fBackSearchBox->SetValue(*backState ? B_CONTROL_ON : B_CONTROL_OFF);
72 }
73 
74 
75 void
76 FindWindow::MessageReceived(BMessage *msg)
77 {
78 	switch (msg->what) {
79 		case B_QUIT_REQUESTED:
80 			Quit();
81 			break;
82 		case MSG_SEARCH:
83 			_SendMessage();
84 			break;
85 
86 		default:
87 			BWindow::MessageReceived(msg);
88 			break;
89 	}
90 }
91 
92 
93 void
94 FindWindow::DispatchMessage(BMessage *message, BHandler *handler)
95 {
96 	if (message->what == B_KEY_DOWN) {
97 		int8 key;
98 		if (message->FindInt8("byte", 0, &key) == B_OK) {
99 			if (key == B_ESCAPE) {
100 				message->MakeEmpty();
101 				message->what = B_QUIT_REQUESTED;
102 			}
103 		}
104 	}
105 
106 	BWindow::DispatchMessage(message, handler);
107 }
108 
109 
110 void
111 FindWindow::_SendMessage()
112 {
113 	BMessage message(MSG_SEARCH);
114 
115 	// Add the string
116 	message.AddString("findtext", fSearchString->Text());
117 
118 	// Add searchparameters from checkboxes
119 	message.AddBool("casesens", fCaseSensBox->Value() == B_CONTROL_ON);
120 	message.AddBool("wrap", fWrapBox->Value() == B_CONTROL_ON);
121 	message.AddBool("backsearch", fBackSearchBox->Value() == B_CONTROL_ON);
122 
123 	fHandler->Looper()->PostMessage(&message, fHandler);
124 
125 	PostMessage(B_QUIT_REQUESTED);
126 }
127 
128 
129