1/* 2 * Copyright 2005-2007, 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 <MessageRunner.h> 12#include <Window.h> 13#include <View.h> 14 15#include <stdio.h> 16 17 18static const uint32 kMsgUpdate = 'updt'; 19 20 21class View : public BView { 22 public: 23 View(BRect rect); 24 virtual ~View(); 25 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); 33 34 private: 35 void _Update(); 36 37 BMessageRunner* fRunner; 38 char fLastKey; 39 bool fPressed; 40 uint8 fLastColor; 41}; 42 43class Window : public BWindow { 44 public: 45 Window(int32 offset = 0); 46 virtual ~Window(); 47 48 virtual bool QuitRequested(); 49}; 50 51class Application : public BApplication { 52 public: 53 Application(); 54 55 virtual void ReadyToRun(); 56}; 57 58 59View::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) 65{ 66} 67 68 69View::~View() 70{ 71} 72 73 74void 75View::AttachedToWindow() 76{ 77 MakeFocus(this); 78 79 BMessage update(kMsgUpdate); 80 fRunner = new BMessageRunner(this, &update, 16667); 81 82 BFont font; 83 font.SetSize(72); 84 SetFont(&font); 85} 86 87 88void 89View::DetachedFromWindow() 90{ 91 delete fRunner; 92 fRunner = NULL; 93} 94 95 96void 97View::MouseDown(BPoint where) 98{ 99 SetMouseEventMask(0, B_LOCK_WINDOW_FOCUS | B_SUSPEND_VIEW_FOCUS 100 | B_NO_POINTER_HISTORY); 101 102 ::Window* window = new ::Window(100); 103 window->Show(); 104} 105 106 107void 108View::MessageReceived(BMessage* message) 109{ 110 switch (message->what) { 111 case kMsgUpdate: 112 _Update(); 113 break; 114 default: 115 BView::MessageReceived(message); 116 break; 117 } 118} 119 120 121void 122View::_Update() 123{ 124 if (fPressed) 125 return; 126 127 if (fLastColor < 255) { 128 fLastColor += 15; 129 Invalidate(); 130 } 131} 132 133 134void 135View::KeyUp(const char* bytes, int32 numBytes) 136{ 137 fPressed = false; 138} 139 140 141void 142View::KeyDown(const char* bytes, int32 numBytes) 143{ 144 fLastKey = bytes[0]; 145 fLastColor = 0; 146 fPressed = true; 147 Invalidate(); 148} 149 150 151void 152View::Draw(BRect updateRect) 153{ 154 SetHighColor(fLastColor, fLastColor, fLastColor); 155 DrawString(&fLastKey, 1, BPoint(20, 70)); 156} 157 158 159// #pragma mark - 160 161 162Window::Window(int32 offset) 163 : BWindow(BRect(100 + offset, 100 + offset, 400 + offset, 400 + offset), 164 "LockFocus-Test", B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS) 165{ 166 BView *view = new View(Bounds()); 167 AddChild(view); 168} 169 170 171Window::~Window() 172{ 173} 174 175 176bool 177Window::QuitRequested() 178{ 179 be_app->PostMessage(B_QUIT_REQUESTED); 180 return true; 181} 182 183 184// #pragma mark - 185 186 187Application::Application() 188 : BApplication("application/x-vnd.haiku-lock_focus") 189{ 190} 191 192 193void 194Application::ReadyToRun(void) 195{ 196 Window* window = new Window(); 197 window->Show(); 198} 199 200 201// #pragma mark - 202 203 204int 205main(int argc, char** argv) 206{ 207 Application app; 208 209 app.Run(); 210 return 0; 211} 212