1// main.cpp
2
3#include <stdio.h>
4
5#include <Application.h>
6#include <Region.h>
7#include <View.h>
8#include <Window.h>
9
10class TestView1 : public BView {
11
12 public:
13	TestView1(BRect frame, const char* name, uint32 resizeFlags, uint32 flags)
14		: BView(frame, name, resizeFlags, flags)
15	{
16		SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
17		SetHighColor(ViewColor());
18	}
19
20	virtual void Draw(BRect updateRect)
21	{
22		BRegion region;
23		region.Include(BRect(20, 20, 40, 40));
24		region.Include(BRect(30, 30, 80, 80));
25		ConstrainClippingRegion(&region);
26
27		SetHighColor(255, 0, 0, 255);
28		FillRect(BRect(0, 0, 100, 100));
29
30		ConstrainClippingRegion(NULL);
31
32		SetHighColor(0, 0, 0, 255);
33		StrokeLine(BPoint(2, 2), BPoint(80, 80));
34	}
35};
36
37class TestView2 : public BView {
38
39 public:
40	TestView2(BRect frame, const char* name, uint32 resizeFlags, uint32 flags)
41		: BView(frame, name, resizeFlags, flags)
42	{
43		SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
44		SetHighColor(ViewColor());
45	}
46
47	virtual void AttachedToWindow()
48	{
49		SetOrigin(20, 20);
50	}
51
52	virtual void Draw(BRect updateRect)
53	{
54//		SetOrigin(20, 20);
55
56		BRegion region;
57		region.Include(BRect(20, 20, 40, 40));
58		region.Include(BRect(30, 30, 80, 80));
59		ConstrainClippingRegion(&region);
60
61		SetHighColor(255, 128, 0, 255);
62		FillRect(BRect(0, 0, 100, 100));
63
64		ConstrainClippingRegion(NULL);
65
66		SetHighColor(0, 0, 0, 255);
67		StrokeLine(BPoint(2, 2), BPoint(80, 80));
68	}
69};
70
71class TestView3 : public BView {
72
73 public:
74	TestView3(BRect frame, const char* name, uint32 resizeFlags, uint32 flags)
75		: BView(frame, name, resizeFlags, flags)
76	{
77		SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
78		SetHighColor(ViewColor());
79	}
80
81	virtual void AttachedToWindow()
82	{
83		SetOrigin(20, 20);
84	}
85
86	virtual void Draw(BRect updateRect)
87	{
88		BRegion region;
89		region.Include(BRect(20, 20, 40, 40));
90		region.Include(BRect(30, 30, 80, 80));
91		ConstrainClippingRegion(&region);
92
93		SetHighColor(55, 255, 128, 255);
94		FillRect(BRect(0, 0, 100, 100));
95
96		PushState();
97			SetOrigin(15, 15);
98
99			ConstrainClippingRegion(&region);
100
101			SetHighColor(155, 255, 128, 255);
102			FillRect(BRect(0, 0, 100, 100));
103
104//			ConstrainClippingRegion(NULL);
105
106			SetHighColor(0, 0, 0, 255);
107			StrokeLine(BPoint(2, 2), BPoint(80, 80));
108			SetHighColor(255, 0, 0, 255);
109			StrokeLine(BPoint(2, 2), BPoint(4, 2));
110		PopState();
111
112		SetHighColor(0, 0, 0, 255);
113		StrokeLine(BPoint(4, 2), BPoint(82, 80));
114	}
115};
116
117class TestView4 : public BView {
118
119 public:
120	TestView4(BRect frame, const char* name, uint32 resizeFlags, uint32 flags)
121		: BView(frame, name, resizeFlags, flags)
122	{
123		SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
124		SetHighColor(ViewColor());
125	}
126
127	virtual void AttachedToWindow()
128	{
129		SetOrigin(20, 20);
130	}
131
132	virtual void Draw(BRect updateRect)
133	{
134		BRegion region;
135		region.Include(BRect(20, 20, 40, 40));
136		region.Include(BRect(30, 30, 140, 140));
137		ConstrainClippingRegion(&region);
138
139		SetHighColor(55, 255, 128, 255);
140		FillRect(BRect(0, 0, 200, 200));
141
142		// NOTE: This exposes broken behavior of the ZETA
143		// (probably R5 too) app_server. The new origin
144		// is not taken into account
145		PushState();
146
147		ConstrainClippingRegion(&region);
148		SetOrigin(15, 15);
149		SetScale(1.5);
150
151		SetHighColor(155, 255, 128, 255);
152		FillRect(BRect(0, 0, 200, 200));
153
154		ConstrainClippingRegion(NULL);
155
156		SetHighColor(0, 0, 0, 255);
157		SetDrawingMode(B_OP_OVER);
158		DrawString("Text is scaled.", BPoint(20, 30));
159
160		SetScale(1.2);
161		DrawString("Text is scaled.", BPoint(20, 30));
162
163		StrokeLine(BPoint(2, 2), BPoint(80, 80));
164
165		PopState();
166
167		SetHighColor(0, 0, 0, 255);
168		StrokeLine(BPoint(4, 2), BPoint(82, 80));
169	}
170};
171
172class TestView5 : public BView {
173
174 public:
175	TestView5(BRect frame, const char* name, uint32 resizeFlags, uint32 flags)
176		: BView(frame, name, resizeFlags, flags)
177	{
178		SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
179		SetHighColor(ViewColor());
180	}
181
182	virtual void AttachedToWindow()
183	{
184	}
185
186	virtual void Draw(BRect updateRect)
187	{
188		BRegion region;
189		region.Include(BRect(0, 0, 40, 40));
190		region.Include(BRect(30, 30, 140, 140));
191		ConstrainClippingRegion(&region);
192
193		SetHighColor(55, 255, 128, 255);
194		FillRect(BRect(0, 0, 200, 200));
195
196		PushState();
197			ConstrainClippingRegion(&region);
198			SetOrigin(50, 10);
199
200			SetHighColor(155, 55, 128, 255);
201			FillRect(BRect(0, 0, 200, 200));
202		PopState();
203
204		SetHighColor(255, 0, 0);
205		StrokeLine(Bounds().LeftTop(), Bounds().RightBottom());
206	}
207};
208
209
210class TestView6 : public BView {
211
212 public:
213	TestView6(BRect frame, const char* name, uint32 resizeFlags, uint32 flags)
214		: BView(frame, name, resizeFlags, flags)
215	{
216		SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
217		SetHighColor(ViewColor());
218	}
219
220	virtual void AttachedToWindow()
221	{
222	}
223
224	virtual void Draw(BRect updateRect)
225	{
226		BRegion region;
227		region.Include(BRect(0, 0, 40, 40));
228		region.Include(BRect(30, 30, 140, 140));
229		ConstrainClippingRegion(&region);
230
231		SetHighColor(55, 255, 128, 255);
232		FillRect(BRect(0, 0, 200, 200));
233
234		PushState();
235			SetOrigin(50, 10);
236			ConstrainClippingRegion(&region);
237
238			SetHighColor(155, 55, 128, 255);
239			FillRect(BRect(0, 0, 200, 200));
240		PopState();
241
242		SetHighColor(255, 0, 0);
243		StrokeLine(Bounds().LeftTop(), Bounds().RightBottom());
244	}
245};
246
247// main
248int
249main(int argc, char** argv)
250{
251	BApplication* app = new BApplication(
252		"application/x.vnd-Haiku.ClippingRegion");
253
254	BRect frame(50.0, 50.0, 300.0, 250.0);
255	BWindow* window;
256	BView* view;
257
258//	window = new BWindow(frame, "Test1", B_TITLED_WINDOW,
259//		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
260//
261//	view = new TestView1(window->Bounds(), "test1", B_FOLLOW_ALL,
262//		B_WILL_DRAW);
263//	window->AddChild(view);
264//	window->Show();
265//
266//	frame.OffsetBy(20, 20);
267//	window = new BWindow(frame, "Test2", B_TITLED_WINDOW,
268//		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
269//
270//	view = new TestView2(window->Bounds(), "test2", B_FOLLOW_ALL,
271//		B_WILL_DRAW);
272//	window->AddChild(view);
273//	window->Show();
274//
275//	frame.OffsetBy(20, 20);
276//	window = new BWindow(frame, "Test3", B_TITLED_WINDOW,
277//		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
278//
279//	view = new TestView3(window->Bounds(), "test3", B_FOLLOW_ALL,
280//		B_WILL_DRAW);
281//	window->AddChild(view);
282//	window->Show();
283//
284//	frame.OffsetBy(20, 20);
285//	window = new BWindow(frame, "Test4", B_TITLED_WINDOW,
286//		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
287//
288//	view = new TestView4(window->Bounds(), "test4", B_FOLLOW_ALL,
289//		B_WILL_DRAW);
290//	window->AddChild(view);
291//	window->Show();
292
293
294	frame.OffsetBy(20, 20);
295	window = new BWindow(frame, "Test5", B_TITLED_WINDOW,
296		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
297
298	view = new TestView5(window->Bounds(), "test5", B_FOLLOW_ALL,
299		B_WILL_DRAW);
300	window->AddChild(view);
301	window->Show();
302
303	frame.OffsetBy(20, 20);
304	window = new BWindow(frame, "Test6", B_TITLED_WINDOW,
305		B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
306
307	view = new TestView6(window->Bounds(), "test6", B_FOLLOW_ALL,
308		B_WILL_DRAW);
309	window->AddChild(view);
310	window->Show();
311
312	app->Run();
313
314	delete app;
315	return 0;
316}
317