xref: /haiku/src/apps/stylededit/FindWindow.cpp (revision 9ecf9d1c1d4888d341a6eac72112c72d1ae3a4cb)
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 	AddShortcut('W',B_COMMAND_KEY,new BMessage(B_QUIT_REQUESTED));
27 
28 	fFindView = new BView(Bounds(), "FindView", B_FOLLOW_ALL, B_WILL_DRAW);
29 	fFindView->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
30 	AddChild(fFindView);
31 
32 	font_height height;
33 	fFindView->GetFontHeight(&height);
34 	float lineHeight = height.ascent+height.descent + height.leading;
35 
36 	float findWidth = fFindView->StringWidth("Find:") + 6;
37 
38 	float searchBottom = 12 + 2 + lineHeight + 2 + 1;
39 	float buttonTop = frame.Height() - 19 - lineHeight;
40 	float wrapBoxTop = (buttonTop + searchBottom - lineHeight) / 2;
41 	float wrapBoxBottom = (buttonTop + searchBottom + lineHeight) / 2;
42 	float caseBoxTop = (searchBottom + wrapBoxTop - lineHeight) / 2;
43 	float caseBoxBottom = (searchBottom + wrapBoxTop + lineHeight) / 2;
44 	float backBoxTop = (buttonTop + wrapBoxBottom - lineHeight) / 2;
45 	float backBoxBottom = (buttonTop + wrapBoxBottom + lineHeight) / 2;
46 
47 	fFindView->AddChild(fSearchString = new BTextControl(BRect(14, 12,
48 		frame.Width() - 10, searchBottom), "", "Find:", NULL, NULL));
49 	fSearchString->SetDivider(findWidth);
50 
51 	fFindView->AddChild(fCaseSensBox = new BCheckBox(BRect(16 + findWidth, caseBoxTop,
52 		frame.Width() - 12, caseBoxBottom), "", "Case-sensitive", NULL));
53 	fFindView->AddChild(fWrapBox = new BCheckBox(BRect(16 + findWidth, wrapBoxTop,
54 		frame.Width() - 12, wrapBoxBottom), "", "Wrap-around search", NULL));
55 	fFindView->AddChild(fBackSearchBox = new BCheckBox(BRect(16 + findWidth,
56 		backBoxTop, frame.Width() - 12, backBoxBottom), "", "Search backwards", NULL));
57 
58 	fFindView->AddChild(fCancelButton = new BButton(BRect(142, buttonTop, 212,
59 		frame.Height() - 7), "", "Cancel", new BMessage(B_QUIT_REQUESTED)));
60 	fFindView->AddChild(fSearchButton = new BButton(BRect(221, buttonTop, 291,
61 		frame.Height() - 7), "", "Find", new BMessage(MSG_SEARCH)));
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 
130 
131