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