xref: /haiku/src/tests/kits/interface/flatten_picture/PictureTestCases.cpp (revision 82afdb09c8152910513c44ea2a0801060c31c662)
1*82afdb09SMichael Pfeiffer /*
2*82afdb09SMichael Pfeiffer  * Copyright 2007, Haiku. All rights reserved.
3*82afdb09SMichael Pfeiffer  * Distributed under the terms of the MIT License.
4*82afdb09SMichael Pfeiffer  *
5*82afdb09SMichael Pfeiffer  * Authors:
6*82afdb09SMichael Pfeiffer  *		Michael Pfeiffer
7*82afdb09SMichael Pfeiffer  */
8*82afdb09SMichael Pfeiffer 
9*82afdb09SMichael Pfeiffer #include "PictureTestCases.h"
10*82afdb09SMichael Pfeiffer 
11*82afdb09SMichael Pfeiffer void testEmptyPicture(BView *view, BRect frame)
12*82afdb09SMichael Pfeiffer {
13*82afdb09SMichael Pfeiffer 	// no op
14*82afdb09SMichael Pfeiffer }
15*82afdb09SMichael Pfeiffer 
16*82afdb09SMichael Pfeiffer void testDrawString(BView *view, BRect frame)
17*82afdb09SMichael Pfeiffer {
18*82afdb09SMichael Pfeiffer 	BFont font;
19*82afdb09SMichael Pfeiffer 	view->GetFont(&font);
20*82afdb09SMichael Pfeiffer 	font_height height;
21*82afdb09SMichael Pfeiffer 	font.GetHeight(&height);
22*82afdb09SMichael Pfeiffer 	float baseline = frame.bottom - height.descent;
23*82afdb09SMichael Pfeiffer 	// draw base line
24*82afdb09SMichael Pfeiffer 	view->SetHighColor(0, 255, 0);
25*82afdb09SMichael Pfeiffer 	view->StrokeLine(BPoint(frame.left, baseline - 1), BPoint(frame.right, baseline -1));
26*82afdb09SMichael Pfeiffer 
27*82afdb09SMichael Pfeiffer 	view->SetHighColor(0, 0, 0);
28*82afdb09SMichael Pfeiffer 	view->DrawString("Haiku [ÖÜÄöüä]", BPoint(frame.left, baseline));
29*82afdb09SMichael Pfeiffer }
30*82afdb09SMichael Pfeiffer 
31*82afdb09SMichael Pfeiffer void testFillRed(BView *view, BRect frame)
32*82afdb09SMichael Pfeiffer {
33*82afdb09SMichael Pfeiffer 	frame.InsetBy(2, 2);
34*82afdb09SMichael Pfeiffer 	view->SetHighColor(255, 0, 0);
35*82afdb09SMichael Pfeiffer 	view->FillRect(frame);
36*82afdb09SMichael Pfeiffer }
37*82afdb09SMichael Pfeiffer 
38*82afdb09SMichael Pfeiffer void testStrokeRect(BView *view, BRect frame)
39*82afdb09SMichael Pfeiffer {
40*82afdb09SMichael Pfeiffer 	frame.InsetBy(2, 2);
41*82afdb09SMichael Pfeiffer 	int levels = (int)(frame.Height()/2 + 1);
42*82afdb09SMichael Pfeiffer 	for (int i = 0; i < levels; i ++) {
43*82afdb09SMichael Pfeiffer 		view->SetHighColor(0, 0, 255 * (levels-i) / levels);
44*82afdb09SMichael Pfeiffer 		view->StrokeRect(frame);
45*82afdb09SMichael Pfeiffer 		frame.InsetBy(1, 1);
46*82afdb09SMichael Pfeiffer 	}
47*82afdb09SMichael Pfeiffer }
48*82afdb09SMichael Pfeiffer 
49*82afdb09SMichael Pfeiffer void testVerticalLine(BView *view, BRect frame)
50*82afdb09SMichael Pfeiffer {
51*82afdb09SMichael Pfeiffer 	view->StrokeLine(BPoint(frame.left, frame.top), BPoint(frame.right, frame.bottom));
52*82afdb09SMichael Pfeiffer }
53*82afdb09SMichael Pfeiffer 
54*82afdb09SMichael Pfeiffer void testStrokeScaledRect(BView *view, BRect frame)
55*82afdb09SMichael Pfeiffer {
56*82afdb09SMichael Pfeiffer 	view->SetScale(0.5);
57*82afdb09SMichael Pfeiffer 	view->StrokeRect(frame);
58*82afdb09SMichael Pfeiffer }
59*82afdb09SMichael Pfeiffer 
60*82afdb09SMichael Pfeiffer void testRecordPicture(BView *view, BRect frame)
61*82afdb09SMichael Pfeiffer {
62*82afdb09SMichael Pfeiffer 	BPicture *picture = new BPicture();
63*82afdb09SMichael Pfeiffer 	view->BeginPicture(picture);
64*82afdb09SMichael Pfeiffer 	view->FillRect(frame);
65*82afdb09SMichael Pfeiffer 	view->EndPicture();
66*82afdb09SMichael Pfeiffer 	delete picture;
67*82afdb09SMichael Pfeiffer }
68*82afdb09SMichael Pfeiffer 
69*82afdb09SMichael Pfeiffer void testRecordAndPlayPicture(BView *view, BRect frame)
70*82afdb09SMichael Pfeiffer {
71*82afdb09SMichael Pfeiffer 	BPicture *picture = new BPicture();
72*82afdb09SMichael Pfeiffer 	view->BeginPicture(picture);
73*82afdb09SMichael Pfeiffer 	frame.InsetBy(2, 2);
74*82afdb09SMichael Pfeiffer 	view->FillRect(frame);
75*82afdb09SMichael Pfeiffer 	view->EndPicture();
76*82afdb09SMichael Pfeiffer 	view->DrawPicture(picture);
77*82afdb09SMichael Pfeiffer 	delete picture;
78*82afdb09SMichael Pfeiffer }
79*82afdb09SMichael Pfeiffer 
80*82afdb09SMichael Pfeiffer void testRecordAndPlayPictureWithOffset(BView *view, BRect frame)
81*82afdb09SMichael Pfeiffer {
82*82afdb09SMichael Pfeiffer 	BPicture *picture = new BPicture();
83*82afdb09SMichael Pfeiffer 	view->BeginPicture(picture);
84*82afdb09SMichael Pfeiffer 	frame.InsetBy(frame.Width() / 4, frame.Height() / 4);
85*82afdb09SMichael Pfeiffer 	frame.OffsetTo(0, 0);
86*82afdb09SMichael Pfeiffer 	view->FillRect(frame);
87*82afdb09SMichael Pfeiffer 	view->EndPicture();
88*82afdb09SMichael Pfeiffer 
89*82afdb09SMichael Pfeiffer 	view->DrawPicture(picture, BPoint(10, 10));
90*82afdb09SMichael Pfeiffer 	// color of picture should not change
91*82afdb09SMichael Pfeiffer 	view->SetLowColor(0, 255, 0);
92*82afdb09SMichael Pfeiffer 	view->SetLowColor(255, 0, 0);
93*82afdb09SMichael Pfeiffer 	view->DrawPicture(picture, BPoint(0, 0));
94*82afdb09SMichael Pfeiffer 	delete picture;
95*82afdb09SMichael Pfeiffer }
96*82afdb09SMichael Pfeiffer 
97*82afdb09SMichael Pfeiffer void testBitmap(BView *view, BRect frame) {
98*82afdb09SMichael Pfeiffer 	BBitmap bitmap(frame, B_RGBA32);
99*82afdb09SMichael Pfeiffer 	for (int32 y = 0; y < bitmap.Bounds().IntegerHeight(); y ++) {
100*82afdb09SMichael Pfeiffer 		for (int32 x = 0; x < bitmap.Bounds().IntegerWidth(); x ++) {
101*82afdb09SMichael Pfeiffer 			char *pixel = (char*)bitmap.Bits();
102*82afdb09SMichael Pfeiffer 			pixel += bitmap.BytesPerRow() * y + 4 * x;
103*82afdb09SMichael Pfeiffer 			// fill with blue
104*82afdb09SMichael Pfeiffer 			pixel[0] = 255;
105*82afdb09SMichael Pfeiffer 			pixel[1] = 0;
106*82afdb09SMichael Pfeiffer 			pixel[2] = 0;
107*82afdb09SMichael Pfeiffer 			pixel[3] = 255;
108*82afdb09SMichael Pfeiffer 		}
109*82afdb09SMichael Pfeiffer 	}
110*82afdb09SMichael Pfeiffer 	view->DrawBitmap(&bitmap, BPoint(0, 0));
111*82afdb09SMichael Pfeiffer }
112*82afdb09SMichael Pfeiffer 
113*82afdb09SMichael Pfeiffer TestCase gTestCases[] = {
114*82afdb09SMichael Pfeiffer 	{ "Test Empty Picture", testEmptyPicture },
115*82afdb09SMichael Pfeiffer 	{ "Test Vertical Line", testVerticalLine },
116*82afdb09SMichael Pfeiffer 	{ "Test Stroke Rect", testStrokeRect },
117*82afdb09SMichael Pfeiffer 	{ "Test Draw String", testDrawString },
118*82afdb09SMichael Pfeiffer 	{ "Test Fill Red", testFillRed },
119*82afdb09SMichael Pfeiffer 	{ "Test Stroke Scaled Rect", testStrokeScaledRect },
120*82afdb09SMichael Pfeiffer 	{ "Test Record Picture", testRecordPicture },
121*82afdb09SMichael Pfeiffer 	{ "Test Record And Play Picture", testRecordAndPlayPicture },
122*82afdb09SMichael Pfeiffer 	{ "Test Record And Play Picture With Offset", testRecordAndPlayPictureWithOffset },
123*82afdb09SMichael Pfeiffer 	{ "Test Draw Bitmap", testBitmap },
124*82afdb09SMichael Pfeiffer 	{ NULL, NULL }
125*82afdb09SMichael Pfeiffer };