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