1/*
2 * Copyright (c) 2005-2010, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
4 *
5 * Author:
6 *		DarkWyrm <darkwyrm@gmail.com>
7 */
8#include "InlineEditor.h"
9
10#include <Handler.h>
11#include <MessageFilter.h>
12
13class EditFilter : public BMessageFilter
14{
15public:
16	EditFilter(BTextControl *textbox)
17		: BMessageFilter(B_PROGRAMMED_DELIVERY, B_ANY_SOURCE, B_KEY_DOWN)
18	{
19		fTextBox = textbox;
20	}
21
22	~EditFilter(void)
23	{
24	}
25
26	filter_result Filter(BMessage *msg, BHandler **target)
27	{
28		int32 rawchar;
29		msg->FindInt32("raw_char", &rawchar);
30
31		if (rawchar == B_ESCAPE) {
32			BLooper *loop = (*target)->Looper();
33			if (loop) {
34				BMessenger msgr(loop);
35				msgr.SendMessage(B_QUIT_REQUESTED);
36				return B_SKIP_MESSAGE;
37			}
38		} else if (rawchar == B_ENTER) {
39			fTextBox->Invoke();
40			return B_SKIP_MESSAGE;
41		}
42		return B_DISPATCH_MESSAGE;
43	}
44
45private:
46	BTextControl 	*fTextBox;
47
48};
49
50InlineEditor::InlineEditor(BMessenger target, const BRect &frame,
51							const char *text)
52 :	BWindow(frame, "InlineEditor", B_NO_BORDER_WINDOW_LOOK, B_NORMAL_WINDOW_FEEL,
53 			B_ASYNCHRONOUS_CONTROLS),
54 	fMessenger(target),
55 	fCommand(M_INLINE_TEXT)
56{
57	fTextBox = new BTextControl(BRect(0, 0, 1, 1), "inlinebox", NULL, text,
58								new BMessage(fCommand), B_FOLLOW_ALL, B_WILL_DRAW);
59	AddChild(fTextBox);
60	fTextBox->SetDivider(0);
61	fTextBox->MakeFocus(true);
62
63	fTextBox->ResizeToPreferred();
64	fTextBox->ResizeTo(Bounds().Width(), fTextBox->Bounds().Height());
65	ResizeTo(Bounds().Width(), fTextBox->Bounds().Height());
66
67	AddCommonFilter(new EditFilter(fTextBox));
68}
69
70
71bool
72InlineEditor::QuitRequested(void)
73{
74	return true;
75}
76
77
78void
79InlineEditor::SetMessage(BMessage *msg)
80{
81	fCommand = msg ? msg->what : 0;
82	fTextBox->SetMessage(msg);
83}
84
85
86void
87InlineEditor::MessageReceived(BMessage *msg)
88{
89	if (msg->what == fCommand) {
90		fMessenger.SendMessage(msg);
91		PostMessage(B_QUIT_REQUESTED);
92	}
93}
94
95void
96InlineEditor::WindowActivated(bool active)
97{
98	if (!active)
99		PostMessage(B_QUIT_REQUESTED);
100}
101
102