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
11const float distance = 5;
12
13TestResultItem::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
23TestResultItem::~TestResultItem()
24{
25	delete fDirectBitmap;
26	fDirectBitmap = NULL;
27	delete fOriginalBitmap;
28	fOriginalBitmap = NULL;
29	delete fArchivedBitmap;
30	fArchivedBitmap = NULL;
31}
32
33void
34TestResultItem::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	}
43	else if (fOk) {
44		// green background color on success
45		owner->SetHighColor(200, 255, 200);
46	}
47	else {
48		// red background color on failure
49		owner->SetHighColor(255, 200, 200);
50	}
51	owner->FillRect(itemRect);
52	owner->PopState();
53
54	itemRect.InsetBy(1, 1);
55
56	owner->MovePenTo(itemRect.left+1, itemRect.top+1);
57	if (fDirectBitmap != NULL) {
58		owner->DrawBitmap(fDirectBitmap);
59	}
60	owner->MovePenBy(fBitmapSize.Width() + distance, 0);
61
62	if (fOriginalBitmap != NULL) {
63		owner->DrawBitmap(fOriginalBitmap);
64	}
65	owner->MovePenBy(fBitmapSize.Width() + distance, 0);
66
67	if (fArchivedBitmap != NULL) {
68		owner->DrawBitmap(fArchivedBitmap);
69	}
70	owner->MovePenBy(fBitmapSize.Width() + distance, 0);
71
72	owner->DrawBitmap(fDirectBitmap);
73	owner->SetDrawingMode(B_OP_SUBTRACT);
74	owner->DrawBitmap(fOriginalBitmap);
75
76	owner->MovePenBy(fBitmapSize.Width() + distance, 0);
77
78	owner->SetDrawingMode(B_OP_OVER);
79	BFont font;
80	owner->GetFont(&font);
81	// vertically center text
82	float baseLine = itemRect.top + (itemRect.IntegerHeight() / 2 + font.Size() / 2);
83	owner->MovePenTo(owner->PenLocation().x, baseLine);
84	owner->DrawString(fName.String());
85
86	if (fErrorMessage.Length() == 0)
87		return;
88
89	owner->PushState();
90	font.SetFace(B_ITALIC_FACE);
91	owner->SetFont(&font);
92	owner->SetHighColor(255, 0, 0);
93	owner->MovePenBy(distance, 0);
94	owner->DrawString(fErrorMessage.String());
95	owner->PopState();
96}
97
98void
99TestResultItem::Update(BView *owner, const BFont *font)
100{
101	BListItem::Update(owner, font);
102	float width = 0.0;
103	float height = 0.0;
104
105	width += font->StringWidth(fName.String());
106	width += distance;
107	width += font->StringWidth(fErrorMessage.String());
108
109	width += 3 * distance;
110	width += 3 * fBitmapSize.Width();
111
112	height = fBitmapSize.Height();
113
114	// border of two pixels
115	width += 4;
116	height += 4;
117
118	if (width > Width())
119		SetWidth(width);
120
121	if (height > Height())
122		SetHeight(height);
123}
124
125