vfs: check userland buffers before reading them.
[haiku.git] / src / tests / servers / app / lock_focus / LockFocusTest.cpp
blob097b8235ad20c8e59d5a1a2293b9ed73982a1399
1 /*
2 * Copyright 2005-2007, 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 <MessageRunner.h>
12 #include <Window.h>
13 #include <View.h>
15 #include <stdio.h>
18 static const uint32 kMsgUpdate = 'updt';
21 class View : public BView {
22 public:
23 View(BRect rect);
24 virtual ~View();
26 virtual void AttachedToWindow();
27 virtual void DetachedFromWindow();
28 virtual void Draw(BRect updateRect);
29 virtual void KeyDown(const char* bytes, int32 numBytes);
30 virtual void KeyUp(const char* bytes, int32 numBytes);
31 virtual void MessageReceived(BMessage* message);
32 virtual void MouseDown(BPoint where);
34 private:
35 void _Update();
37 BMessageRunner* fRunner;
38 char fLastKey;
39 bool fPressed;
40 uint8 fLastColor;
43 class Window : public BWindow {
44 public:
45 Window(int32 offset = 0);
46 virtual ~Window();
48 virtual bool QuitRequested();
51 class Application : public BApplication {
52 public:
53 Application();
55 virtual void ReadyToRun();
59 View::View(BRect rect)
60 : BView(rect, "lock focus", B_FOLLOW_ALL, B_WILL_DRAW),
61 fRunner(NULL),
62 fLastKey('\0'),
63 fPressed(false),
64 fLastColor(255)
69 View::~View()
74 void
75 View::AttachedToWindow()
77 MakeFocus(this);
79 BMessage update(kMsgUpdate);
80 fRunner = new BMessageRunner(this, &update, 16667);
82 BFont font;
83 font.SetSize(72);
84 SetFont(&font);
88 void
89 View::DetachedFromWindow()
91 delete fRunner;
92 fRunner = NULL;
96 void
97 View::MouseDown(BPoint where)
99 SetMouseEventMask(0, B_LOCK_WINDOW_FOCUS | B_SUSPEND_VIEW_FOCUS
100 | B_NO_POINTER_HISTORY);
102 ::Window* window = new ::Window(100);
103 window->Show();
107 void
108 View::MessageReceived(BMessage* message)
110 switch (message->what) {
111 case kMsgUpdate:
112 _Update();
113 break;
114 default:
115 BView::MessageReceived(message);
116 break;
121 void
122 View::_Update()
124 if (fPressed)
125 return;
127 if (fLastColor < 255) {
128 fLastColor += 15;
129 Invalidate();
134 void
135 View::KeyUp(const char* bytes, int32 numBytes)
137 fPressed = false;
141 void
142 View::KeyDown(const char* bytes, int32 numBytes)
144 fLastKey = bytes[0];
145 fLastColor = 0;
146 fPressed = true;
147 Invalidate();
151 void
152 View::Draw(BRect updateRect)
154 SetHighColor(fLastColor, fLastColor, fLastColor);
155 DrawString(&fLastKey, 1, BPoint(20, 70));
159 // #pragma mark -
162 Window::Window(int32 offset)
163 : BWindow(BRect(100 + offset, 100 + offset, 400 + offset, 400 + offset),
164 "LockFocus-Test", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
166 BView *view = new View(Bounds());
167 AddChild(view);
171 Window::~Window()
176 bool
177 Window::QuitRequested()
179 be_app->PostMessage(B_QUIT_REQUESTED);
180 return true;
184 // #pragma mark -
187 Application::Application()
188 : BApplication("application/x-vnd.haiku-lock_focus")
193 void
194 Application::ReadyToRun(void)
196 Window* window = new Window();
197 window->Show();
201 // #pragma mark -
204 int
205 main(int argc, char** argv)
207 Application app;
209 app.Run();
210 return 0;