libroot_debug: Merge guarded heap into libroot_debug.
[haiku.git] / src / tests / servers / app / event_mask / EventMask.cpp
blobe1da8b076a5b23e7b7ac62c802b5d46bfdec716c
1 /*
2 * Copyright 2005, Haiku Inc.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
10 #include <Application.h>
11 #include <Box.h>
12 #include <CheckBox.h>
13 #include <String.h>
14 #include <TextControl.h>
15 #include <Window.h>
17 #include <stdio.h>
20 static const uint32 kMsgPointerEvents = 'pevt';
21 static const rgb_color kPermanentColor = { 130, 130, 220};
22 static const rgb_color kPressedColor = { 220, 120, 120};
25 class PositionView : public BBox {
26 public:
27 PositionView(BRect rect, uint32 options);
28 virtual ~PositionView();
30 void SetPermanent(bool permanent);
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);
37 private:
38 bool fPermanent;
39 uint32 fOptions;
40 BPoint fPosition;
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)
53 PositionView::~PositionView()
58 void
59 PositionView::SetPermanent(bool permanent)
61 if (permanent) {
62 SetEventMask(B_POINTER_EVENTS, 0);
63 SetViewColor(kPermanentColor);
64 } else {
65 SetEventMask(0, 0);
66 SetViewColor(Parent()->ViewColor());
69 fPermanent = permanent;
71 SetLowColor(ViewColor());
72 Invalidate();
76 void
77 PositionView::Draw(BRect updateRect)
79 BBox::Draw(updateRect);
81 BString type;
82 if (fOptions & B_SUSPEND_VIEW_FOCUS) {
83 type << "suspend";
84 if (fOptions & ~B_SUSPEND_VIEW_FOCUS)
85 type << " & ";
87 if (fOptions & B_LOCK_WINDOW_FOCUS)
88 type << "lock";
89 if (fOptions == 0)
90 type << "none";
92 DrawString(type.String(), BPoint(6, 14));
94 BString position;
95 if (fPosition.x >= 0)
96 position << (int32)fPosition.x;
97 else
98 position << "-";
100 position << " : ";
102 if (fPosition.y >= 0)
103 position << (int32)fPosition.y;
104 else
105 position << "-";
107 DrawString(position.String(), BPoint(6, 28));
111 void
112 PositionView::MouseDown(BPoint where)
114 if (fOptions != 0)
115 SetMouseEventMask(B_POINTER_EVENTS, fOptions);
117 SetViewColor(kPressedColor);
118 SetLowColor(ViewColor());
119 fPosition = where;
120 Invalidate();
124 void
125 PositionView::MouseUp(BPoint where)
127 if (fPermanent) {
128 SetViewColor(kPermanentColor);
129 } else {
130 SetViewColor(Parent()->ViewColor());
133 SetLowColor(ViewColor());
134 fPosition = where;
135 Invalidate();
139 void
140 PositionView::MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage)
142 fPosition = where;
144 BRect rect = Bounds().InsetByCopy(5, 5);
145 rect.bottom = 33;
146 Invalidate(rect);
150 // #pragma mark -
153 class Window : public BWindow {
154 public:
155 Window();
156 virtual ~Window();
158 virtual bool QuitRequested();
159 virtual void MessageReceived(BMessage* message);
161 private:
162 PositionView* fViews[4];
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)
170 BView* view = new BView(Bounds(), NULL, B_FOLLOW_ALL, B_WILL_DRAW);
171 view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
172 AddChild(view);
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);
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);
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;
200 fViews[i] = new PositionView(rect, options);
201 view->AddChild(fViews[i]);
203 rect.OffsetBy(120, 0);
206 BCheckBox* checkBox = new BCheckBox(BRect(10, 130, 200, 160), "permanent", "Get all pointer events", new BMessage(kMsgPointerEvents));
207 view->AddChild(checkBox);
211 Window::~Window()
216 bool
217 Window::QuitRequested()
219 be_app->PostMessage(B_QUIT_REQUESTED);
220 return true;
224 void
225 Window::MessageReceived(BMessage* message)
227 switch (message->what) {
228 case kMsgPointerEvents:
230 bool selected = message->FindInt32("be:value") != 0;
232 for (int32 i = 0; i < 4; i++) {
233 fViews[i]->SetPermanent(selected);
235 break;
238 default:
239 BWindow::MessageReceived(message);
240 break;
245 // #pragma mark -
248 class Application : public BApplication {
249 public:
250 Application();
252 virtual void ReadyToRun(void);
256 Application::Application()
257 : BApplication("application/x-vnd.haiku-view_state")
262 void
263 Application::ReadyToRun(void)
265 Window *window = new Window();
266 window->Show();
270 // #pragma mark -
273 int
274 main(int argc, char **argv)
276 Application app;// app;
278 app.Run();
279 return 0;