1/*
2 * Copyright (c) 1999-2000, Eric Moon.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions, and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions, and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * 3. The name of the author may not be used to endorse or promote products
17 *    derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31
32// TextControlFloater.cpp
33
34#include "TextControlFloater.h"
35
36#include <Debug.h>
37#include <TextControl.h>
38
39__USE_CORTEX_NAMESPACE
40
41// ---------------------------------------------------------------- //
42
43class MomentaryTextControl :
44	public	BTextControl {
45	typedef	BTextControl _inherited;
46
47public:
48	MomentaryTextControl(
49		BRect											frame,
50		const char*								name,
51		const char*								label,
52		const char*								text,
53		BMessage*									message,
54		uint32										resizingMode=B_FOLLOW_LEFT|B_FOLLOW_TOP,
55		uint32										flags=B_WILL_DRAW|B_NAVIGABLE) :
56		BTextControl(frame,  name, label, text, message, resizingMode, flags) {
57
58		SetEventMask(B_POINTER_EVENTS|B_KEYBOARD_EVENTS);
59	}
60
61public:
62	virtual void AllAttached() {
63		TextView()->SelectAll();
64
65		// size parent to fit me
66		Window()->ResizeTo(
67			Bounds().Width(),
68			Bounds().Height());
69
70		Window()->Show();
71	}
72
73	virtual void MouseDown(
74		BPoint										point) {
75
76		if(Bounds().Contains(point))
77			_inherited::MouseDown(point);
78
79		Invoke();
80//		// +++++ shouldn't an out-of-bounds click send the changed value?
81//		BMessenger(Window()).SendMessage(B_QUIT_REQUESTED);
82	}
83
84	virtual void KeyDown(
85		const char*								bytes,
86		int32											numBytes) {
87
88		if(numBytes == 1 && *bytes == B_ESCAPE) {
89			BWindow* w = Window(); // +++++ maui/2 workaround
90			BMessenger(w).SendMessage(B_QUIT_REQUESTED);
91			return;
92		}
93	}
94};
95
96// ---------------------------------------------------------------- //
97
98// ---------------------------------------------------------------- //
99// dtor/ctors
100// ---------------------------------------------------------------- //
101
102TextControlFloater::~TextControlFloater() {
103	if(m_cancelMessage)
104		delete m_cancelMessage;
105}
106
107TextControlFloater::TextControlFloater(
108	BRect											frame,
109	alignment									align,
110	const BFont*							font,
111	const char*								text,
112	const BMessenger&					target,
113	BMessage*									message,
114	BMessage*									cancelMessage) :
115	BWindow(
116		frame, //.InsetBySelf(-3.0,-3.0), // expand frame to counteract control border
117		"TextControlFloater",
118		B_NO_BORDER_WINDOW_LOOK,
119		B_FLOATING_APP_WINDOW_FEEL,
120		0),
121	m_target(target),
122	m_message(message),
123	m_cancelMessage(cancelMessage),
124	m_sentUpdate(false) {
125
126	m_control = new MomentaryTextControl(
127		Bounds(),
128		"textControl",
129		0,
130		text,
131		message,
132		B_FOLLOW_ALL_SIDES);
133
134	Run();
135	Lock();
136
137	m_control->TextView()->SetFontAndColor(font);
138	m_control->TextView()->SetAlignment(align);
139	m_control->SetDivider(0.0);
140
141	m_control->SetViewColor(B_TRANSPARENT_COLOR);
142	m_control->TextView()->SelectAll();
143	AddChild(m_control);
144	m_control->MakeFocus();
145
146	Unlock();
147}
148
149// ---------------------------------------------------------------- //
150// BWindow
151// ---------------------------------------------------------------- //
152
153void TextControlFloater::WindowActivated(
154	bool											activated) {
155
156	if(!activated)
157		// +++++ will the message get sent first?
158		Quit();
159}
160
161// ---------------------------------------------------------------- //
162// BHandler
163// ---------------------------------------------------------------- //
164
165void TextControlFloater::MessageReceived(
166	BMessage*									message) {
167
168	if(message->what == m_message->what) {
169		// done; relay message to final target
170		message->AddString("_value", m_control->TextView()->Text());
171		m_target.SendMessage(message);
172		m_sentUpdate = true;
173		Quit();
174		return;
175	}
176
177	switch(message->what) {
178		default:
179			_inherited::MessageReceived(message);
180	}
181}
182
183bool TextControlFloater::QuitRequested() {
184	if(!m_sentUpdate && m_cancelMessage)
185		m_target.SendMessage(m_cancelMessage);
186
187	return true;
188}
189
190// END -- TextControlFloater.cpp --
191