xref: /haiku/src/tests/servers/app/newClipping/main.cpp (revision f51806635b24d7b95ef5ccadc07a958ecd5d3ca5)
1 #include <OS.h>
2 #include <Application.h>
3 #include <Window.h>
4 #include <View.h>
5 #include <Region.h>
6 #include <Rect.h>
7 #include <String.h>
8 
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 #include "MyView.h"
13 #include "Layer.h"
14 #include "WinBorder.h"
15 
16 #define ApplicationSignature "application/x-vnd.generic-SkeletonApplication"
17 
18 BWindow *wind = NULL;
19 
20 class clsApp
21 :
22 	public BApplication
23 {
24 public:
25 	clsApp();
26 	~clsApp();
27 	virtual void ReadyToRun();
28 };
29 
30 class clsMainWindow
31 :
32 	public BWindow
33 {
34 public:
35 						clsMainWindow(const char *uWindowTitle);
36 						~clsMainWindow();
37 
38 	virtual bool		QuitRequested();
39 			void		AddWindow(BRect frame, const char* name);
40 			void		test2();
41 			void		test1();
42 private:
43 			MyView		*fView;
44 };
45 
clsApp()46 clsApp::clsApp() : BApplication(ApplicationSignature)
47 {
48 	srand(real_time_clock_usecs());
49 }
50 
~clsApp()51 clsApp::~clsApp()
52 {
53 }
54 
ReadyToRun()55 void clsApp::ReadyToRun()
56 {
57 }
58 
59 
clsMainWindow(const char * uWindowTitle)60 clsMainWindow::clsMainWindow(const char *uWindowTitle)
61 :
62 	BWindow(
63 		BRect(50, 50, 800, 650),
64 		uWindowTitle,
65 		B_TITLED_WINDOW_LOOK,
66 		B_NORMAL_WINDOW_FEEL,
67 		0	)
68 {
69 	wind = this;
70 	fView = new MyView(Bounds(), "emu", B_FOLLOW_ALL, B_WILL_DRAW);
71 	AddChild(fView);
72 	fView->MakeFocus(true);
73 }
74 
~clsMainWindow()75 clsMainWindow::~clsMainWindow()
76 {
77 }
78 
QuitRequested()79 bool clsMainWindow::QuitRequested()
80 {
81 	be_app->PostMessage(B_QUIT_REQUESTED);
82 	return BWindow::QuitRequested();
83 }
84 
85 
86 // AddWindow
87 void
AddWindow(BRect frame,const char * name)88 clsMainWindow::AddWindow(BRect frame, const char* name)
89 {
90 	Layer* topLayer = fView->topLayer;
91 
92 	WinBorder* window = new WinBorder(frame, name, B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE,
93 									  (rgb_color){ 255, 203, 0, 255 });
94 	topLayer->AddLayer(window);
95 
96 	// add a coupld children
97 	frame.OffsetTo(B_ORIGIN);
98 	frame.InsetBy(5.0, 5.0);
99 	if (frame.IsValid()) {
100 		Layer* layer1 = new Layer(frame, "View 1", B_FOLLOW_ALL,
101 								  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 180, 180, 180, 255 });
102 
103 		frame.OffsetTo(B_ORIGIN);
104 		frame.InsetBy(15.0, 15.0);
105 		if (frame.IsValid()) {
106 
107 			BRect f = frame;
108 			f.bottom = f.top + f.Height() / 3 - 3;
109 			f.right = f.left + f.Width() / 3 - 3;
110 
111 			// top row of views
112 			Layer* layer;
113 			layer = new Layer(f, "View", B_FOLLOW_LEFT | B_FOLLOW_TOP,
114 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
115 			layer1->AddLayer(layer);
116 
117 			f.OffsetBy(f.Width() + 6, 0);
118 
119 			layer = new Layer(f, "View", B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP,
120 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
121 			layer1->AddLayer(layer);
122 
123 			f.OffsetBy(f.Width() + 6, 0);
124 
125 			layer = new Layer(f, "View", B_FOLLOW_RIGHT | B_FOLLOW_TOP,
126 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
127 			layer1->AddLayer(layer);
128 
129 			// middle row of views
130 			f = frame;
131 			f.bottom = f.top + f.Height() / 3 - 3;
132 			f.right = f.left + f.Width() / 3 - 3;
133 			f.OffsetBy(0, f.Height() + 6);
134 
135 			layer = new Layer(f, "View", B_FOLLOW_LEFT | B_FOLLOW_TOP_BOTTOM,
136 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
137 			layer1->AddLayer(layer);
138 
139 			f.OffsetBy(f.Width() + 6, 0);
140 
141 			layer = new Layer(f, "View", B_FOLLOW_ALL,
142 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
143 			layer1->AddLayer(layer);
144 
145 			f.OffsetBy(f.Width() + 6, 0);
146 
147 			layer = new Layer(f, "View", B_FOLLOW_RIGHT | B_FOLLOW_TOP_BOTTOM,
148 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
149 			layer1->AddLayer(layer);
150 
151 			// bottom row of views
152 			f = frame;
153 			f.bottom = f.top + f.Height() / 3 - 3;
154 			f.right = f.left + f.Width() / 3 - 3;
155 			f.OffsetBy(0, 2 * f.Height() + 12);
156 
157 			layer = new Layer(f, "View", B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
158 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
159 			layer1->AddLayer(layer);
160 
161 			f.OffsetBy(f.Width() + 6, 0);
162 
163 			layer = new Layer(f, "View", B_FOLLOW_LEFT_RIGHT | B_FOLLOW_BOTTOM,
164 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
165 			layer1->AddLayer(layer);
166 
167 			f.OffsetBy(f.Width() + 6, 0);
168 
169 			layer = new Layer(f, "View", B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM,
170 							  B_FULL_UPDATE_ON_RESIZE, (rgb_color){ 120, 120, 120, 255 });
171 			layer1->AddLayer(layer);
172 		}
173 
174 		window->AddLayer(layer1);
175 	}
176 
177 	BRegion		temp;
178 	window->GetWantedRegion(temp);
179 	topLayer->RebuildVisibleRegions(temp, window);
180 }
181 
182 // test2
183 void
test2()184 clsMainWindow::test2()
185 {
186 	BRect frame(20, 20, 330, 230);
187 	for (int32 i = 0; i < 20; i++) {
188 		BString name("Window ");
189 		frame.OffsetBy(20, 15);
190 		name << i + 1;
191 		AddWindow(frame, name.String());
192 	}
193 /*
194 	frame.Set(10, 80, 320, 290);
195 	for (int32 i = 20; i < 40; i++) {
196 		BString name("Window ");
197 		frame.OffsetBy(20, 15);
198 		name << i + 1;
199 		AddWindow(frame, name.String());
200 	}
201 
202 	frame.Set(20, 140, 330, 230);
203 	for (int32 i = 40; i < 60; i++) {
204 		BString name("Window ");
205 		frame.OffsetBy(20, 15);
206 		name << i + 1;
207 		AddWindow(frame, name.String());
208 	}
209 
210 	frame.Set(20, 200, 330, 230);
211 	for (int32 i = 60; i < 80; i++) {
212 		BString name("Window ");
213 		frame.OffsetBy(20, 15);
214 		name << i + 1;
215 		AddWindow(frame, name.String());
216 	}
217 
218 	frame.Set(20, 260, 330, 230);
219 // 99 windows are ok, the 100th looper does not run anymore,
220 // I guess I'm hitting a BeOS limit (max loopers per app?)
221 	for (int32 i = 80; i < 99; i++) {
222 		BString name("Window ");
223 		frame.OffsetBy(20, 15);
224 		name << i + 1;
225 		AddWindow(frame, name.String());
226 	}*/
227 
228 	fView->RequestRedraw();
229 }
230 
test1()231 void clsMainWindow::test1()
232 {
233 	Layer		*topLayer = fView->topLayer;
234 
235 	rgb_color	c;
236 
237 	c.red = rand()/256;
238 	c.green = rand()/256;
239 	c.blue = rand()/256;
240 //	Layer	*lay1
241 //		= new Layer(BRect(20,20,300,220), "lay1", B_FOLLOW_NONE, 0, c);
242 //		= new WinBorder(BRect(20,20,300,220), "lay1", B_FOLLOW_NONE, 0, c);
243 //	topLayer->AddLayer(lay1);
244 // ------
245 	WinBorder	*wb1 = new WinBorder(BRect(20,20,300,220), "wb1", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
246 	topLayer->AddLayer(wb1);
247 		// same size as wb1
248 	Layer	*lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
249 	wb1->AddLayer(lay1);
250 // ------
251 	c.red = rand()/256;
252 	c.green = rand()/256;
253 	c.blue = rand()/256;
254 	Layer	*lay2 = new Layer(BRect(20,20,150,150), "lay2",
255 			B_FOLLOW_NONE,
256 			B_FULL_UPDATE_ON_RESIZE, c);
257 	lay1->AddLayer(lay2);
258 
259 	c.red = rand()/256;
260 	c.green = rand()/256;
261 	c.blue = rand()/256;
262 	Layer	*lay3 = new Layer(BRect(20,20,100,100), "lay3",
263 			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
264 			0, c);
265 	lay2->AddLayer(lay3);
266 
267 	c.red = rand()/256;
268 	c.green = rand()/256;
269 	c.blue = rand()/256;
270 	Layer	*lay12 = new Layer(BRect(170,20,290,150), "lay12",
271 			B_FOLLOW_LEFT | B_FOLLOW_TOP,
272 //			B_FOLLOW_NONE,
273 			0, c);
274 	lay1->AddLayer(lay12);
275 
276 	c.red = rand()/256;
277 	c.green = rand()/256;
278 	c.blue = rand()/256;
279 	Layer	*lay13 = new Layer(BRect(20,20,100,100), "lay13",
280 //			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
281 			B_FOLLOW_RIGHT | B_FOLLOW_TOP,
282 //			B_FOLLOW_LEFT | B_FOLLOW_V_CENTER,
283 //			B_FOLLOW_H_CENTER | B_FOLLOW_TOP,
284 			0, c);
285 	lay12->AddLayer(lay13);
286 
287 	c.red = rand()/256;
288 	c.green = rand()/256;
289 	c.blue = rand()/256;
290 	Layer	*lay102 = new Layer(BRect(200,20,420,250), "lay102",
291 //			B_FOLLOW_NONE,
292 			B_FOLLOW_TOP_BOTTOM,
293 //			B_FULL_UPDATE_ON_RESIZE, c);
294 			0, c);
295 	lay1->AddLayer(lay102);
296 
297 	c.red = rand()/256;
298 	c.green = rand()/256;
299 	c.blue = rand()/256;
300 	Layer	*lay103 = new Layer(BRect(0,0,100,50), "lay103",
301 			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
302 //			B_FOLLOW_LEFT | B_FOLLOW_TOP,
303 			0, c);
304 	lay102->AddLayer(lay103);
305 
306 	c.red = rand()/256;
307 	c.green = rand()/256;
308 	c.blue = rand()/256;
309 	Layer	*lay104 = new Layer(BRect(0,51,100,130), "lay104",
310 //			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
311 			B_FOLLOW_RIGHT | B_FOLLOW_BOTTOM,
312 			0, c);
313 	lay102->AddLayer(lay104);
314 
315 	c.red = rand()/256;
316 	c.green = rand()/256;
317 	c.blue = rand()/256;
318 	Layer	*lay106 = new Layer(BRect(0,140,100, 200), "lay104",
319 //			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
320 			B_FOLLOW_RIGHT | B_FOLLOW_TOP,
321 			0, c);
322 	lay102->AddLayer(lay106);
323 
324 	c.red = rand()/256;
325 	c.green = rand()/256;
326 	c.blue = rand()/256;
327 	Layer	*lay105 = new Layer(BRect(105,51,150,130), "lay104",
328 //			B_FOLLOW_LEFT | B_FOLLOW_BOTTOM,
329 			B_FOLLOW_H_CENTER | B_FOLLOW_BOTTOM,
330 			0, c);
331 	lay102->AddLayer(lay105);
332 
333 //---------
334 	c.red = rand()/256;
335 	c.green = rand()/256;
336 	c.blue = rand()/256;
337 	WinBorder	*wb2 = new WinBorder(BRect(280,120,600,420), "wb2", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
338 	topLayer->AddLayer(wb2);
339 	Layer	*lay21 = new Layer(wb2->Bounds().OffsetToCopy(0,0), "lay21", B_FOLLOW_NONE, 0, c);
340 	wb2->AddLayer(lay21);
341 
342 	c.red = rand()/256;
343 	c.green = rand()/256;
344 	c.blue = rand()/256;
345 	Layer	*lay22 = new Layer(BRect(20,20,150,150), "lay22",
346 			B_FOLLOW_NONE,
347 			0, c);
348 	lay21->AddLayer(lay22);
349 
350 
351 	WinBorder	*wb3 = new WinBorder(BRect(20,20,300,220), "wb3", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
352 	topLayer->AddLayer(wb3);
353 		// same size as wb1
354 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
355 	wb3->AddLayer(lay1);
356 
357 	WinBorder	*wb4 = new WinBorder(BRect(20,20,300,220), "wb4", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
358 	topLayer->AddLayer(wb4);
359 		// same size as wb1
360 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
361 	wb4->AddLayer(lay1);
362 
363 	WinBorder	*wb5 = new WinBorder(BRect(20,20,300,220), "wb5", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
364 	topLayer->AddLayer(wb5);
365 		// same size as wb1
366 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
367 	wb5->AddLayer(lay1);
368 
369 	WinBorder	*wb6 = new WinBorder(BRect(20,20,300,220), "wb6", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
370 	topLayer->AddLayer(wb6);
371 		// same size as wb1
372 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
373 	wb6->AddLayer(lay1);
374 
375 	WinBorder	*wb7 = new WinBorder(BRect(20,20,300,220), "wb7", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
376 	topLayer->AddLayer(wb7);
377 		// same size as wb1
378 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
379 	wb7->AddLayer(lay1);
380 
381 	WinBorder	*wb8 = new WinBorder(BRect(20,20,300,220), "wb8", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
382 	topLayer->AddLayer(wb8);
383 		// same size as wb1
384 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
385 	wb8->AddLayer(lay1);
386 
387 	WinBorder	*wb9 = new WinBorder(BRect(20,20,300,220), "wb9", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
388 	topLayer->AddLayer(wb9);
389 		// same size as wb1
390 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
391 	wb9->AddLayer(lay1);
392 
393 	WinBorder	*wb10 = new WinBorder(BRect(20,20,300,220), "wb10", B_FOLLOW_NONE, B_FULL_UPDATE_ON_RESIZE, c);
394 	topLayer->AddLayer(wb10);
395 		// same size as wb1
396 	lay1 = new Layer(BRect(0,0,280,200), "lay1", B_FOLLOW_ALL, 0, c);
397 	wb10->AddLayer(lay1);
398 
399 //---------
400 
401 	BRegion		temp;
402 	wb1->GetWantedRegion(temp);
403 	topLayer->RebuildVisibleRegions(temp, wb1);
404 
405 	wb2->GetWantedRegion(temp);
406 	topLayer->RebuildVisibleRegions(temp, wb2);
407 
408 	wb3->GetWantedRegion(temp);
409 	topLayer->RebuildVisibleRegions(temp, wb3);
410 
411 	wb4->GetWantedRegion(temp);
412 	topLayer->RebuildVisibleRegions(temp, wb4);
413 
414 	wb5->GetWantedRegion(temp);
415 	topLayer->RebuildVisibleRegions(temp, wb5);
416 
417 	wb6->GetWantedRegion(temp);
418 	topLayer->RebuildVisibleRegions(temp, wb6);
419 
420 	wb7->GetWantedRegion(temp);
421 	topLayer->RebuildVisibleRegions(temp, wb7);
422 
423 	wb8->GetWantedRegion(temp);
424 	topLayer->RebuildVisibleRegions(temp, wb8);
425 
426 	wb9->GetWantedRegion(temp);
427 	topLayer->RebuildVisibleRegions(temp, wb9);
428 
429 	wb10->GetWantedRegion(temp);
430 	topLayer->RebuildVisibleRegions(temp, wb10);
431 
432 
433 	fView->RequestRedraw();
434 
435 	snooze(1000000);
436 
437 	wb1->Hide();
438 
439 	snooze(1000000);
440 
441 	wb1->Show();
442 
443 /*
444 
445 	snooze(2000000);
446 
447 	lay2->MoveBy(25,35);
448 
449 	snooze(2000000);
450 
451 	lay2->ResizeBy(45,55);
452 
453 	snooze(2000000);
454 
455 	lay2->ResizeBy(-45, -55);
456 
457 
458 	snooze(2000000);
459 
460 	lay1->ScrollBy(0,50);
461 
462 	snooze(2000000);
463 
464 	lay2->Hide();
465 
466 	snooze(2000000);
467 
468 	lay2->Show();
469 
470 	snooze(2000000);
471 
472 	lay1->Invalidate(BRect(0,0,500,500));
473 */
474 }
475 
main()476 int main()
477 {
478 	new clsApp();
479 	clsMainWindow	*win = new clsMainWindow("clipping");
480 	win->Show();
481 
482 	win->test2();
483 
484 	be_app->Run();
485 	delete be_app;
486 	return 0;
487 }
488