1 /* 2 * Copyright 2009, 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 #include <stdlib.h> 17 18 19 class ShowInvalidationView : public BView { 20 public: 21 ShowInvalidationView(BRect rect); 22 virtual ~ShowInvalidationView(); 23 24 virtual void Draw(BRect updateRect); 25 }; 26 27 28 class ShowingWindow : public BWindow { 29 public: 30 ShowingWindow(); 31 }; 32 33 34 class ChangingWindow : public BWindow { 35 public: 36 ChangingWindow(); 37 virtual ~ChangingWindow(); 38 39 virtual void MessageReceived(BMessage* message); 40 41 private: 42 BMessageRunner* fRunner; 43 }; 44 45 46 class Application : public BApplication { 47 public: 48 Application(); 49 50 virtual void ReadyToRun(); 51 }; 52 53 54 ShowInvalidationView::ShowInvalidationView(BRect rect) 55 : 56 BView(rect, "show invalidation", B_FOLLOW_ALL, B_WILL_DRAW) 57 { 58 SetViewColor(B_TRANSPARENT_COLOR); 59 } 60 61 62 ShowInvalidationView::~ShowInvalidationView() 63 { 64 } 65 66 67 void 68 ShowInvalidationView::Draw(BRect updateRect) 69 { 70 SetHighColor(rand() % 256, rand() % 256, rand() % 256); 71 FillRect(updateRect); 72 } 73 74 75 // #pragma mark - 76 77 78 ShowingWindow::ShowingWindow() 79 : 80 BWindow(BRect(150, 150, 450, 450), "WindowInvalidation-Test", 81 B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) 82 { 83 BView* view = new ShowInvalidationView(Bounds()); 84 AddChild(view); 85 } 86 87 88 // #pragma mark - 89 90 91 ChangingWindow::ChangingWindow() 92 : 93 BWindow(BRect(150, 150, 400, 400), "WindowInvalidation-Test", 94 B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE) 95 { 96 BMessage message('actn'); 97 fRunner = new BMessageRunner(this, &message, 25000); 98 } 99 100 101 ChangingWindow::~ChangingWindow() 102 { 103 delete fRunner; 104 } 105 106 107 void 108 ChangingWindow::MessageReceived(BMessage* message) 109 { 110 if (message->what == 'actn') { 111 switch (rand() % 3) { 112 case 0: 113 { 114 // resize window 115 BRect bounds; 116 do { 117 bounds = Bounds(); 118 bounds.right += rand() % 21 - 10; 119 bounds.bottom += rand() % 21 - 10; 120 } while (bounds.Width() > 400 || bounds.Height() > 400 121 || bounds.Width() < 50 || bounds.Height() < 50); 122 123 ResizeTo(bounds.Width() + 1, bounds.Height() + 1); 124 break; 125 } 126 127 case 1: 128 { 129 // move window 130 BPoint leftTop; 131 do { 132 leftTop = Frame().LeftTop(); 133 leftTop.x += rand() % 21 - 10; 134 leftTop.y += rand() % 21 - 10; 135 } while (!BRect(100, 100, 200, 200).Contains(leftTop)); 136 137 MoveTo(leftTop); 138 break; 139 } 140 141 case 2: 142 { 143 // set title 144 static const char* kChoices[] 145 = {"Window", "Invalidation", "Test", "Hooray"}; 146 147 SetTitle(kChoices[rand() % (sizeof(kChoices) / sizeof(char*))]); 148 break; 149 } 150 } 151 } else 152 BWindow::MessageReceived(message); 153 } 154 155 156 // #pragma mark - 157 158 159 Application::Application() 160 : 161 BApplication("application/x-vnd.haiku-view_state") 162 { 163 } 164 165 166 void 167 Application::ReadyToRun() 168 { 169 BWindow* window = new ChangingWindow(); 170 window->Show(); 171 172 window = new ShowingWindow(); 173 window->Show(); 174 } 175 176 177 // #pragma mark - 178 179 180 int 181 main(int argc, char** argv) 182 { 183 Application app; 184 app.Run(); 185 186 return 0; 187 } 188