1/*
2 * Copyright 2010-2013 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Stephan A��mus, superstippi@gmx.de
7 *		Adrien Destugues, pulkomandy@pulkomandy.ath.cx
8 *		Axel D��rfler, axeld@pinc-software.de
9 *		John Scipione, jscipione@gmail.com
10 *		Oliver Tappe, zooey@hirschkaefer.de
11 */
12
13
14#include "TimeZoneListItem.h"
15
16#include <new>
17
18#include <Bitmap.h>
19#include <Country.h>
20#include <ControlLook.h>
21#include <String.h>
22#include <TimeZone.h>
23#include <Window.h>
24
25
26static const BString skDefaultString;
27
28
29TimeZoneListItem::TimeZoneListItem(const char* text, BCountry* country,
30	BTimeZone* timeZone)
31	:
32	BStringItem(text, 0, false),
33	fCountry(country),
34	fTimeZone(timeZone),
35	fIcon(NULL)
36{
37}
38
39
40TimeZoneListItem::~TimeZoneListItem()
41{
42	delete fCountry;
43	delete fTimeZone;
44	delete fIcon;
45}
46
47
48void
49TimeZoneListItem::DrawItem(BView* owner, BRect frame, bool complete)
50{
51	if (fIcon != NULL && fIcon->IsValid()) {
52		float iconSize = fIcon->Bounds().Width();
53		_DrawItemWithTextOffset(owner, frame, complete,
54			iconSize + be_control_look->DefaultLabelSpacing());
55
56		BRect iconFrame(frame.left + be_control_look->DefaultLabelSpacing(),
57			frame.top,
58			frame.left + iconSize - 1 + be_control_look->DefaultLabelSpacing(),
59			frame.top + iconSize - 1);
60		owner->SetDrawingMode(B_OP_OVER);
61		owner->DrawBitmap(fIcon, iconFrame);
62		owner->SetDrawingMode(B_OP_COPY);
63	} else
64		_DrawItemWithTextOffset(owner, frame, complete, 0);
65}
66
67
68void
69TimeZoneListItem::Update(BView* owner, const BFont* font)
70{
71	float oldIconSize = Height();
72	BStringItem::Update(owner, font);
73	if (!HasCountry())
74		return;
75
76	float iconSize = Height();
77	if (iconSize == oldIconSize && fIcon != NULL)
78		return;
79
80	SetWidth(Width() + iconSize + be_control_look->DefaultLabelSpacing());
81
82	delete fIcon;
83	fIcon = new(std::nothrow) BBitmap(BRect(0, 0, iconSize - 1, iconSize - 1),
84		B_RGBA32);
85	if (fIcon != NULL && fCountry->GetIcon(fIcon) != B_OK) {
86		delete fIcon;
87		fIcon = NULL;
88	}
89}
90
91
92void
93TimeZoneListItem::SetCountry(BCountry* country)
94{
95	delete fCountry;
96	fCountry = country;
97}
98
99
100void
101TimeZoneListItem::SetTimeZone(BTimeZone* timeZone)
102{
103	delete fTimeZone;
104	fTimeZone = timeZone;
105}
106
107
108const BString&
109TimeZoneListItem::ID() const
110{
111	if (!HasTimeZone())
112		return skDefaultString;
113
114	return fTimeZone->ID();
115}
116
117
118const BString&
119TimeZoneListItem::Name() const
120{
121	if (!HasTimeZone())
122		return skDefaultString;
123
124	return fTimeZone->Name();
125}
126
127
128int
129TimeZoneListItem::OffsetFromGMT() const
130{
131	if (!HasTimeZone())
132		return 0;
133
134	return fTimeZone->OffsetFromGMT();
135}
136
137
138void
139TimeZoneListItem::_DrawItemWithTextOffset(BView* owner, BRect frame,
140	bool complete, float textOffset)
141{
142	rgb_color highColor = owner->HighColor();
143	rgb_color lowColor = owner->LowColor();
144
145	if (IsSelected() || complete) {
146		rgb_color color;
147		if (IsSelected())
148			color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
149		else
150			color = owner->ViewColor();
151
152		owner->SetHighColor(color);
153		owner->SetLowColor(color);
154		owner->FillRect(frame);
155	} else
156		owner->SetLowColor(owner->ViewColor());
157
158	if (!IsEnabled()) {
159		rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
160		if (textColor.red + textColor.green + textColor.blue > 128 * 3)
161			owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
162		else
163			owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
164	} else {
165		if (IsSelected())
166			owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
167		else
168			owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
169	}
170
171	owner->MovePenTo(
172		frame.left + be_control_look->DefaultLabelSpacing() + textOffset,
173		frame.top + BaselineOffset());
174	owner->DrawString(Text());
175
176	owner->SetHighColor(highColor);
177	owner->SetLowColor(lowColor);
178}
179