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 <Catalog.h>
16#include <CheckBox.h>
17#include <GroupLayoutBuilder.h>
18#include <GridLayoutBuilder.h>
19#include <Locale.h>
20#include <LayoutBuilder.h>
21#include <String.h>
22#include <TextControl.h>
23
24
25#undef B_TRANSLATION_CONTEXT
26#define B_TRANSLATION_CONTEXT "FindandReplaceWindow"
27
28FindWindow::FindWindow(BRect frame, BHandler* _handler, BString* searchString,
29	bool caseState, bool wrapState, bool backState)
30	: BWindow(frame, "FindWindow", B_MODAL_WINDOW,
31		B_NOT_RESIZABLE | B_ASYNCHRONOUS_CONTROLS | B_AUTO_UPDATE_SIZE_LIMITS,
32		B_CURRENT_WORKSPACE)
33{
34	AddShortcut('W', B_COMMAND_KEY, new BMessage(B_QUIT_REQUESTED));
35
36	fSearchString = new BTextControl("", B_TRANSLATE("Find:"), NULL, NULL);
37	fCaseSensBox = new BCheckBox("", B_TRANSLATE("Case-sensitive"), NULL);
38	fWrapBox = new BCheckBox("", B_TRANSLATE("Wrap-around search"), NULL);
39	fBackSearchBox = new BCheckBox("", B_TRANSLATE("Search backwards"), NULL);
40	fCancelButton = new BButton("", B_TRANSLATE("Cancel"),
41		new BMessage(B_QUIT_REQUESTED));
42	fSearchButton = new BButton("", B_TRANSLATE("Find"),
43		new BMessage(MSG_SEARCH));
44
45	SetLayout(new BGroupLayout(B_HORIZONTAL));
46	AddChild(BGroupLayoutBuilder(B_VERTICAL, 4)
47		.Add(BGridLayoutBuilder(6, 2)
48				.Add(fSearchString->CreateLabelLayoutItem(), 0, 0)
49				.Add(fSearchString->CreateTextViewLayoutItem(), 1, 0)
50				.Add(fCaseSensBox, 1, 1)
51				.Add(fWrapBox, 1, 2)
52				.Add(fBackSearchBox, 1, 3)
53				)
54		.AddGroup(B_HORIZONTAL, 10)
55			.AddGlue()
56			.Add(fCancelButton)
57			.Add(fSearchButton)
58		.End()
59		.SetInsets(10, 10, 10, 10)
60	);
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
76void
77FindWindow::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
94void
95FindWindow::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
111void
112FindWindow::_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