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 18 static const uint32 kMsgUpdate = 'updt'; 19 20 21 class 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 43 class Window : public BWindow { 44 public: 45 Window(int32 offset = 0); 46 virtual ~Window(); 47 48 virtual bool QuitRequested(); 49 }; 50 51 class Application : public BApplication { 52 public: 53 Application(); 54 55 virtual void ReadyToRun(); 56 }; 57 58 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) 65 { 66 } 67 68 69 View::~View() 70 { 71 } 72 73 74 void 75 View::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 88 void 89 View::DetachedFromWindow() 90 { 91 delete fRunner; 92 fRunner = NULL; 93 } 94 95 96 void 97 View::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 107 void 108 View::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 121 void 122 View::_Update() 123 { 124 if (fPressed) 125 return; 126 127 if (fLastColor < 255) { 128 fLastColor += 15; 129 Invalidate(); 130 } 131 } 132 133 134 void 135 View::KeyUp(const char* bytes, int32 numBytes) 136 { 137 fPressed = false; 138 } 139 140 141 void 142 View::KeyDown(const char* bytes, int32 numBytes) 143 { 144 fLastKey = bytes[0]; 145 fLastColor = 0; 146 fPressed = true; 147 Invalidate(); 148 } 149 150 151 void 152 View::Draw(BRect updateRect) 153 { 154 SetHighColor(fLastColor, fLastColor, fLastColor); 155 DrawString(&fLastKey, 1, BPoint(20, 70)); 156 } 157 158 159 // #pragma mark - 160 161 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) 165 { 166 BView *view = new View(Bounds()); 167 AddChild(view); 168 } 169 170 171 Window::~Window() 172 { 173 } 174 175 176 bool 177 Window::QuitRequested() 178 { 179 be_app->PostMessage(B_QUIT_REQUESTED); 180 return true; 181 } 182 183 184 // #pragma mark - 185 186 187 Application::Application() 188 : BApplication("application/x-vnd.haiku-lock_focus") 189 { 190 } 191 192 193 void 194 Application::ReadyToRun(void) 195 { 196 Window* window = new Window(); 197 window->Show(); 198 } 199 200 201 // #pragma mark - 202 203 204 int 205 main(int argc, char** argv) 206 { 207 Application app; 208 209 app.Run(); 210 return 0; 211 } 212