xref: /haiku/src/tests/servers/app/event_mask/EventMask.cpp (revision bab64f65bb775dc23060e276f1f1c4498ab7af6c)
1 /*
2  * Copyright 2005, Haiku Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include <Application.h>
11 #include <Box.h>
12 #include <CheckBox.h>
13 #include <String.h>
14 #include <TextControl.h>
15 #include <Window.h>
16 
17 #include <stdio.h>
18 
19 
20 static const uint32 kMsgPointerEvents = 'pevt';
21 static const rgb_color kPermanentColor = { 130, 130, 220};
22 static const rgb_color kPressedColor = { 220, 120, 120};
23 
24 
25 class PositionView : public BBox {
26 	public:
27 		PositionView(BRect rect, uint32 options);
28 		virtual ~PositionView();
29 
30 		void SetPermanent(bool permanent);
31 
32 		virtual void Draw(BRect updateRect);
33 		virtual void MouseDown(BPoint where);
34 		virtual void MouseUp(BPoint where);
35 		virtual void MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage);
36 
37 	private:
38 		bool	fPermanent;
39 		uint32	fOptions;
40 		BPoint	fPosition;
41 };
42 
43 
PositionView(BRect rect,uint32 options)44 PositionView::PositionView(BRect rect, uint32 options)
45 	: BBox(rect, "event mask", B_FOLLOW_ALL, B_WILL_DRAW),
46 	fPermanent(false),
47 	fOptions(options),
48 	fPosition(-1, -1)
49 {
50 }
51 
52 
~PositionView()53 PositionView::~PositionView()
54 {
55 }
56 
57 
58 void
SetPermanent(bool permanent)59 PositionView::SetPermanent(bool permanent)
60 {
61 	if (permanent) {
62 		SetEventMask(B_POINTER_EVENTS, 0);
63 		SetViewColor(kPermanentColor);
64 	} else {
65 		SetEventMask(0, 0);
66 		SetViewColor(Parent()->ViewColor());
67 	}
68 
69 	fPermanent = permanent;
70 
71 	SetLowColor(ViewColor());
72 	Invalidate();
73 }
74 
75 
76 void
Draw(BRect updateRect)77 PositionView::Draw(BRect updateRect)
78 {
79 	BBox::Draw(updateRect);
80 
81 	BString type;
82 	if (fOptions & B_SUSPEND_VIEW_FOCUS) {
83 		type << "suspend";
84 		if (fOptions & ~B_SUSPEND_VIEW_FOCUS)
85 			type << " & ";
86 	}
87 	if (fOptions & B_LOCK_WINDOW_FOCUS)
88 		type << "lock";
89 	if (fOptions == 0)
90 		type << "none";
91 
92 	DrawString(type.String(), BPoint(6, 14));
93 
94 	BString position;
95 	if (fPosition.x >= 0)
96 		position << (int32)fPosition.x;
97 	else
98 		position << "-";
99 
100 	position << " : ";
101 
102 	if (fPosition.y >= 0)
103 		position << (int32)fPosition.y;
104 	else
105 		position << "-";
106 
107 	DrawString(position.String(), BPoint(6, 28));
108 }
109 
110 
111 void
MouseDown(BPoint where)112 PositionView::MouseDown(BPoint where)
113 {
114 	if (fOptions != 0)
115 		SetMouseEventMask(B_POINTER_EVENTS, fOptions);
116 
117 	SetViewColor(kPressedColor);
118 	SetLowColor(ViewColor());
119 	fPosition = where;
120 	Invalidate();
121 }
122 
123 
124 void
MouseUp(BPoint where)125 PositionView::MouseUp(BPoint where)
126 {
127 	if (fPermanent) {
128 		SetViewColor(kPermanentColor);
129 	} else {
130 		SetViewColor(Parent()->ViewColor());
131 	}
132 
133 	SetLowColor(ViewColor());
134 	fPosition = where;
135 	Invalidate();
136 }
137 
138 
139 void
MouseMoved(BPoint where,uint32 transit,const BMessage * dragMessage)140 PositionView::MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage)
141 {
142 	fPosition = where;
143 
144 	BRect rect = Bounds().InsetByCopy(5, 5);
145 	rect.bottom = 33;
146 	Invalidate(rect);
147 }
148 
149 
150 //	#pragma mark -
151 
152 
153 class Window : public BWindow {
154 	public:
155 		Window();
156 		virtual ~Window();
157 
158 		virtual bool QuitRequested();
159 		virtual void MessageReceived(BMessage* message);
160 
161 	private:
162 		PositionView* fViews[4];
163 };
164 
165 
Window()166 Window::Window()
167 	: BWindow(BRect(100, 100, 590, 260), "EventMask-Test",
168 			B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_NOT_RESIZABLE | B_NOT_ZOOMABLE)
169 {
170 	BView* view = new BView(Bounds(), NULL, B_FOLLOW_ALL, B_WILL_DRAW);
171 	view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
172 	AddChild(view);
173 
174 	BTextControl* textControl = new BTextControl(BRect(10, 10, 290, 30),
175 		"text", "Type to test focus suspending:", "", NULL, B_FOLLOW_LEFT_RIGHT);
176 	textControl->SetDivider(textControl->StringWidth(textControl->Label()) + 8);
177 	view->AddChild(textControl);
178 
179 	textControl = new BTextControl(BRect(300, 10, 420, 30),
180 		"all", "All keys:", "", NULL, B_FOLLOW_LEFT_RIGHT);
181 	textControl->SetDivider(textControl->StringWidth(textControl->Label()) + 8);
182 	view->AddChild(textControl);
183 	textControl->TextView()->SetEventMask(B_KEYBOARD_EVENTS, 0);
184 
185 	BRect rect(10, 40, 120, 120);
186 	for (int32 i = 0; i < 4; i++) {
187 		uint32 options = 0;
188 		switch (i) {
189 			case 1:
190 				options = B_SUSPEND_VIEW_FOCUS;
191 				break;
192 			case 2:
193 				options = B_LOCK_WINDOW_FOCUS;
194 				break;
195 			case 3:
196 				options = B_SUSPEND_VIEW_FOCUS | B_LOCK_WINDOW_FOCUS;
197 				break;
198 		}
199 
200 		fViews[i] = new PositionView(rect, options);
201 		view->AddChild(fViews[i]);
202 
203 		rect.OffsetBy(120, 0);
204 	}
205 
206 	BCheckBox* checkBox = new BCheckBox(BRect(10, 130, 200, 160), "permanent", "Get all pointer events", new BMessage(kMsgPointerEvents));
207 	view->AddChild(checkBox);
208 }
209 
210 
~Window()211 Window::~Window()
212 {
213 }
214 
215 
216 bool
QuitRequested()217 Window::QuitRequested()
218 {
219 	be_app->PostMessage(B_QUIT_REQUESTED);
220 	return true;
221 }
222 
223 
224 void
MessageReceived(BMessage * message)225 Window::MessageReceived(BMessage* message)
226 {
227 	switch (message->what) {
228 		case kMsgPointerEvents:
229 		{
230 			bool selected = message->FindInt32("be:value") != 0;
231 
232 			for (int32 i = 0; i < 4; i++) {
233 				fViews[i]->SetPermanent(selected);
234 			}
235 			break;
236 		}
237 
238 		default:
239 			BWindow::MessageReceived(message);
240 			break;
241 	}
242 }
243 
244 
245 //	#pragma mark -
246 
247 
248 class Application : public BApplication {
249 	public:
250 		Application();
251 
252 		virtual void ReadyToRun(void);
253 };
254 
255 
Application()256 Application::Application()
257 	: BApplication("application/x-vnd.haiku-view_state")
258 {
259 }
260 
261 
262 void
ReadyToRun(void)263 Application::ReadyToRun(void)
264 {
265 	Window *window = new Window();
266 	window->Show();
267 }
268 
269 
270 //	#pragma mark -
271 
272 
273 int
main(int argc,char ** argv)274 main(int argc, char **argv)
275 {
276 	Application app;// app;
277 
278 	app.Run();
279 	return 0;
280 }
281