xref: /haiku/src/apps/cortex/support/TextControlFloater.cpp (revision 4b3b81da9e459443d75329cfd08bc9a57ad02653)
1 // TextControlFloater.cpp
2 
3 #include "TextControlFloater.h"
4 
5 #include <Debug.h>
6 #include <TextControl.h>
7 
8 __USE_CORTEX_NAMESPACE
9 
10 // ---------------------------------------------------------------- //
11 
12 class MomentaryTextControl :
13 	public	BTextControl {
14 	typedef	BTextControl _inherited;
15 
16 public:
17 	MomentaryTextControl(
18 		BRect											frame,
19 		const char*								name,
20 		const char*								label,
21 		const char*								text,
22 		BMessage*									message,
23 		uint32										resizingMode=B_FOLLOW_LEFT|B_FOLLOW_TOP,
24 		uint32										flags=B_WILL_DRAW|B_NAVIGABLE) :
25 		BTextControl(frame,  name, label, text, message, resizingMode, flags) {
26 
27 		SetEventMask(B_POINTER_EVENTS|B_KEYBOARD_EVENTS);
28 	}
29 
30 public:
31 	virtual void AllAttached() {
32 		TextView()->SelectAll();
33 
34 		// size parent to fit me
35 		Window()->ResizeTo(
36 			Bounds().Width(),
37 			Bounds().Height());
38 
39 		Window()->Show();
40 	}
41 
42 	virtual void MouseDown(
43 		BPoint										point) {
44 
45 		if(Bounds().Contains(point))
46 			_inherited::MouseDown(point);
47 
48 		Invoke();
49 //		// +++++ shouldn't an out-of-bounds click send the changed value?
50 //		BMessenger(Window()).SendMessage(B_QUIT_REQUESTED);
51 	}
52 
53 	virtual void KeyDown(
54 		const char*								bytes,
55 		int32											numBytes) {
56 
57 		if(numBytes == 1 && *bytes == B_ESCAPE) {
58 			BWindow* w = Window(); // +++++ maui/2 workaround
59 			BMessenger(w).SendMessage(B_QUIT_REQUESTED);
60 			return;
61 		}
62 	}
63 };
64 
65 // ---------------------------------------------------------------- //
66 
67 // ---------------------------------------------------------------- //
68 // dtor/ctors
69 // ---------------------------------------------------------------- //
70 
71 TextControlFloater::~TextControlFloater() {
72 	if(m_cancelMessage)
73 		delete m_cancelMessage;
74 }
75 
76 TextControlFloater::TextControlFloater(
77 	BRect											frame,
78 	alignment									align,
79 	const BFont*							font,
80 	const char*								text,
81 	const BMessenger&					target,
82 	BMessage*									message,
83 	BMessage*									cancelMessage) :
84 	BWindow(
85 		frame, //.InsetBySelf(-3.0,-3.0), // expand frame to counteract control border
86 		"TextControlFloater",
87 		B_NO_BORDER_WINDOW_LOOK,
88 		B_FLOATING_APP_WINDOW_FEEL,
89 		0),
90 	m_target(target),
91 	m_message(message),
92 	m_cancelMessage(cancelMessage),
93 	m_sentUpdate(false) {
94 
95 	m_control = new MomentaryTextControl(
96 		Bounds(),
97 		"textControl",
98 		0,
99 		text,
100 		message,
101 		B_FOLLOW_ALL_SIDES);
102 
103 	Run();
104 	Lock();
105 
106 	m_control->TextView()->SetFontAndColor(font);
107 	m_control->TextView()->SetAlignment(align);
108 	m_control->SetDivider(0.0);
109 
110 	m_control->SetViewColor(B_TRANSPARENT_COLOR);
111 	m_control->TextView()->SelectAll();
112 	AddChild(m_control);
113 	m_control->MakeFocus();
114 
115 	Unlock();
116 }
117 
118 // ---------------------------------------------------------------- //
119 // BWindow
120 // ---------------------------------------------------------------- //
121 
122 void TextControlFloater::WindowActivated(
123 	bool											activated) {
124 
125 	if(!activated)
126 		// +++++ will the message get sent first?
127 		Quit();
128 }
129 
130 // ---------------------------------------------------------------- //
131 // BHandler
132 // ---------------------------------------------------------------- //
133 
134 void TextControlFloater::MessageReceived(
135 	BMessage*									message) {
136 
137 	if(message->what == m_message->what) {
138 		// done; relay message to final target
139 		message->AddString("_value", m_control->TextView()->Text());
140 		m_target.SendMessage(message);
141 		m_sentUpdate = true;
142 		Quit();
143 		return;
144 	}
145 
146 	switch(message->what) {
147 		default:
148 			_inherited::MessageReceived(message);
149 	}
150 }
151 
152 bool TextControlFloater::QuitRequested() {
153 	if(!m_sentUpdate && m_cancelMessage)
154 		m_target.SendMessage(m_cancelMessage);
155 
156 	return true;
157 }
158 
159 // END -- TextControlFloater.cpp --
160