xref: /haiku/src/tests/kits/interface/flatten_picture/TestResultItem.cpp (revision ae09eb30c504a822368fc8a57c3020aefa674034)
1 /*
2  * Copyright 2007, Haiku. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Michael Pfeiffer
7  */
8 
9 #include "TestResultItem.h"
10 
11 const float kDistance = 5;
12 
TestResultItem(const char * name,BRect bitmapSize)13 TestResultItem::TestResultItem(const char* name, BRect bitmapSize)
14 	: fName(name)
15 	, fBitmapSize(bitmapSize)
16 	, fOk(true)
17 	, fDirectBitmap(NULL)
18 	, fOriginalBitmap(NULL)
19 	, fArchivedBitmap(NULL)
20 {
21 }
22 
~TestResultItem()23 TestResultItem::~TestResultItem()
24 {
25 	delete fDirectBitmap;
26 	fDirectBitmap = NULL;
27 	delete fOriginalBitmap;
28 	fOriginalBitmap = NULL;
29 	delete fArchivedBitmap;
30 	fArchivedBitmap = NULL;
31 }
32 
33 void
DrawItem(BView * owner,BRect itemRect,bool drawEverthing)34 TestResultItem::DrawItem(BView *owner, BRect itemRect, bool drawEverthing)
35 {
36 	owner->SetDrawingMode(B_OP_COPY);
37 
38 	owner->PushState();
39 	if (IsSelected()) {
40 		rgb_color lowColor = owner->LowColor();
41 		owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT));
42 	} else if (fOk) {
43 		// green background color on success
44 		owner->SetHighColor(200, 255, 200);
45 	} else {
46 		// red background color on failure
47 		owner->SetHighColor(255, 200, 200);
48 	}
49 	owner->FillRect(itemRect);
50 	owner->PopState();
51 
52 	itemRect.InsetBy(1, 1);
53 
54 	owner->MovePenTo(itemRect.left+1, itemRect.top+1);
55 	if (fDirectBitmap != NULL) {
56 		owner->DrawBitmap(fDirectBitmap);
57 	}
58 	owner->MovePenBy(fBitmapSize.Width() + kDistance, 0);
59 
60 	if (fOriginalBitmap != NULL) {
61 		owner->DrawBitmap(fOriginalBitmap);
62 	}
63 	owner->MovePenBy(fBitmapSize.Width() + kDistance, 0);
64 
65 	if (fArchivedBitmap != NULL) {
66 		owner->DrawBitmap(fArchivedBitmap);
67 	}
68 	owner->MovePenBy(fBitmapSize.Width() + kDistance, 0);
69 
70 	owner->DrawBitmap(fDirectBitmap);
71 	owner->SetDrawingMode(B_OP_SUBTRACT);
72 	owner->DrawBitmap(fOriginalBitmap);
73 
74 	owner->MovePenBy(fBitmapSize.Width() + kDistance, 0);
75 
76 	owner->SetDrawingMode(B_OP_OVER);
77 	BFont font;
78 	owner->GetFont(&font);
79 	// vertically center text
80 	float baseLine = itemRect.top + (itemRect.IntegerHeight() / 2 + font.Size() / 2);
81 	owner->MovePenTo(owner->PenLocation().x, baseLine);
82 	owner->DrawString(fName.String());
83 
84 	if (fErrorMessage.Length() == 0)
85 		return;
86 
87 	owner->PushState();
88 	font.SetFace(B_ITALIC_FACE);
89 	owner->SetFont(&font);
90 	owner->SetHighColor(255, 0, 0);
91 	owner->MovePenBy(kDistance, 0);
92 	owner->DrawString(fErrorMessage.String());
93 	owner->PopState();
94 }
95 
96 
97 void
Update(BView * owner,const BFont * font)98 TestResultItem::Update(BView *owner, const BFont *font)
99 {
100 	BListItem::Update(owner, font);
101 	float width = 0.0;
102 	float height = 0.0;
103 
104 	width += font->StringWidth(fName.String());
105 	width += kDistance;
106 	width += font->StringWidth(fErrorMessage.String());
107 
108 	width += 3 * kDistance;
109 	width += 3 * fBitmapSize.Width();
110 
111 	height = fBitmapSize.Height();
112 
113 	// border of two pixels
114 	width += 4;
115 	height += 4;
116 
117 	if (width > Width())
118 		SetWidth(width);
119 
120 	if (height > Height())
121 		SetHeight(height);
122 }
123 
124 
125 // HeaderListItem
HeaderListItem(const char * label1,const char * label2,const char * label3,const char * label4,const char * label5,const char * label6,BRect rect)126 HeaderListItem::HeaderListItem(const char* label1, const char* label2,
127 					const char* label3, const char* label4, const char* label5,
128 					const char* label6, BRect rect)
129 	:
130 	fRect(rect)
131 {
132 	fLabels[0] = label1;
133 	fLabels[1] = label2;
134 	fLabels[2] = label3;
135 	fLabels[3] = label4;
136 	fLabels[4] = label5;
137 	fLabels[5] = label6;
138 }
139 
140 
141 /* virtual */
142 void
DrawItem(BView * owner,BRect itemRect,bool drawEverthing)143 HeaderListItem::DrawItem(BView *owner, BRect itemRect, bool drawEverthing)
144 {
145 	owner->SetDrawingMode(B_OP_COPY);
146 
147 	owner->PushState();
148 	if (IsSelected()) {
149 		rgb_color lowColor = owner->LowColor();
150 		owner->SetHighColor(tint_color(lowColor, B_DARKEN_2_TINT));
151 		owner->FillRect(itemRect);
152 	}
153 
154 	owner->PopState();
155 
156 	itemRect.InsetBy(0, 1);
157 	owner->StrokeRect(itemRect);
158 
159 	itemRect.InsetBy(1, 0);
160 
161 	owner->SetDrawingMode(B_OP_OVER);
162 
163 	BFont font;
164 	owner->GetFont(&font);
165 	float baseLine = itemRect.top + (itemRect.IntegerHeight() / 2 + font.Size() / 2);
166 
167 	for (int32 c = 0; c < sizeof(fLabels) / sizeof(fLabels[0]); c++) {
168 		owner->MovePenTo(itemRect.left + 1 + (fRect.Width() + kDistance) * c, baseLine);
169 		owner->DrawString(fLabels[c]);
170 	}
171 }
172 
173 
174 /* virtual */
175 void
Update(BView * owner,const BFont * font)176 HeaderListItem::Update(BView *owner, const BFont *font)
177 {
178 	BListItem::Update(owner, font);
179 	float width = 0.0;
180 	float height = 0.0;
181 
182 	for (int32 c = 0; c < sizeof(fLabels) / sizeof(fLabels[0]); c++) {
183 		width += font->StringWidth(fLabels[c].String());
184 	}
185 
186 	width += kDistance * (sizeof(fLabels) / sizeof(fLabels[0]) - 1);
187 
188 	height = fRect.Height();
189 
190 	// border of two pixels
191 	width += 4;
192 	height += 4;
193 
194 	if (width > Width())
195 		SetWidth(width);
196 	if (height > Height())
197 		SetHeight(height);
198 }
199