xref: /haiku/src/preferences/time/TimeZoneListItem.cpp (revision 5b013eab68a65aa83891cba81c681cdcf034533d)
14ae5a452SAdrien Destugues /*
2*5b013eabSJohn Scipione  * Copyright 2010-2013 Haiku, Inc. All rights reserved.
34ae5a452SAdrien Destugues  * Distributed under the terms of the MIT License.
44ae5a452SAdrien Destugues  *
54ae5a452SAdrien Destugues  * Authors:
6*5b013eabSJohn Scipione  *		Stephan Aßmus, superstippi@gmx.de
7*5b013eabSJohn Scipione  *		Adrien Destugues, pulkomandy@pulkomandy.ath.cx
84ae5a452SAdrien Destugues  *		Axel Dörfler, axeld@pinc-software.de
9*5b013eabSJohn Scipione  *		John Scipione, jscipione@gmail.com
10*5b013eabSJohn Scipione  *		Oliver Tappe, zooey@hirschkaefer.de
114ae5a452SAdrien Destugues  */
124ae5a452SAdrien Destugues 
134ae5a452SAdrien Destugues 
144ae5a452SAdrien Destugues #include "TimeZoneListItem.h"
154ae5a452SAdrien Destugues 
164ae5a452SAdrien Destugues #include <new>
174ae5a452SAdrien Destugues 
184ae5a452SAdrien Destugues #include <Bitmap.h>
194ae5a452SAdrien Destugues #include <Country.h>
20*5b013eabSJohn Scipione #include <ControlLook.h>
214ae5a452SAdrien Destugues #include <String.h>
224ae5a452SAdrien Destugues #include <TimeZone.h>
234ae5a452SAdrien Destugues #include <Window.h>
244ae5a452SAdrien Destugues 
254ae5a452SAdrien Destugues 
2699d2aa98SOliver Tappe static const BString skDefaultString;
2799d2aa98SOliver Tappe 
2899d2aa98SOliver Tappe 
TimeZoneListItem(const char * text,BCountry * country,BTimeZone * timeZone)294ae5a452SAdrien Destugues TimeZoneListItem::TimeZoneListItem(const char* text, BCountry* country,
304ae5a452SAdrien Destugues 	BTimeZone* timeZone)
314ae5a452SAdrien Destugues 	:
323740f52dSOliver Tappe 	BStringItem(text, 0, false),
33*5b013eabSJohn Scipione 	fCountry(country),
34*5b013eabSJohn Scipione 	fTimeZone(timeZone),
35*5b013eabSJohn Scipione 	fIcon(NULL)
364ae5a452SAdrien Destugues {
374ae5a452SAdrien Destugues }
384ae5a452SAdrien Destugues 
394ae5a452SAdrien Destugues 
~TimeZoneListItem()404ae5a452SAdrien Destugues TimeZoneListItem::~TimeZoneListItem()
414ae5a452SAdrien Destugues {
42*5b013eabSJohn Scipione 	delete fCountry;
434ae5a452SAdrien Destugues 	delete fTimeZone;
44*5b013eabSJohn Scipione 	delete fIcon;
454ae5a452SAdrien Destugues }
464ae5a452SAdrien Destugues 
474ae5a452SAdrien Destugues 
484ae5a452SAdrien Destugues void
DrawItem(BView * owner,BRect frame,bool complete)494ae5a452SAdrien Destugues TimeZoneListItem::DrawItem(BView* owner, BRect frame, bool complete)
504ae5a452SAdrien Destugues {
514ae5a452SAdrien Destugues 	if (fIcon != NULL && fIcon->IsValid()) {
52*5b013eabSJohn Scipione 		float iconSize = fIcon->Bounds().Width();
53*5b013eabSJohn Scipione 		_DrawItemWithTextOffset(owner, frame, complete,
54*5b013eabSJohn Scipione 			iconSize + be_control_look->DefaultLabelSpacing());
55*5b013eabSJohn Scipione 
56*5b013eabSJohn Scipione 		BRect iconFrame(frame.left + be_control_look->DefaultLabelSpacing(),
57*5b013eabSJohn Scipione 			frame.top,
58*5b013eabSJohn Scipione 			frame.left + iconSize - 1 + be_control_look->DefaultLabelSpacing(),
59*5b013eabSJohn Scipione 			frame.top + iconSize - 1);
604ae5a452SAdrien Destugues 		owner->SetDrawingMode(B_OP_OVER);
614ae5a452SAdrien Destugues 		owner->DrawBitmap(fIcon, iconFrame);
624ae5a452SAdrien Destugues 		owner->SetDrawingMode(B_OP_COPY);
63*5b013eabSJohn Scipione 	} else
64*5b013eabSJohn Scipione 		_DrawItemWithTextOffset(owner, frame, complete, 0);
654ae5a452SAdrien Destugues }
664ae5a452SAdrien Destugues 
674ae5a452SAdrien Destugues 
68*5b013eabSJohn Scipione void
Update(BView * owner,const BFont * font)69*5b013eabSJohn Scipione TimeZoneListItem::Update(BView* owner, const BFont* font)
7038ac8defSOliver Tappe {
71*5b013eabSJohn Scipione 	float oldIconSize = Height();
72*5b013eabSJohn Scipione 	BStringItem::Update(owner, font);
73*5b013eabSJohn Scipione 	if (!HasCountry())
74*5b013eabSJohn Scipione 		return;
75*5b013eabSJohn Scipione 
76*5b013eabSJohn Scipione 	float iconSize = Height();
77*5b013eabSJohn Scipione 	if (iconSize == oldIconSize && fIcon != NULL)
78*5b013eabSJohn Scipione 		return;
79*5b013eabSJohn Scipione 
80*5b013eabSJohn Scipione 	SetWidth(Width() + iconSize + be_control_look->DefaultLabelSpacing());
81*5b013eabSJohn Scipione 
82*5b013eabSJohn Scipione 	delete fIcon;
83*5b013eabSJohn Scipione 	fIcon = new(std::nothrow) BBitmap(BRect(0, 0, iconSize - 1, iconSize - 1),
84*5b013eabSJohn Scipione 		B_RGBA32);
85*5b013eabSJohn Scipione 	if (fIcon != NULL && fCountry->GetIcon(fIcon) != B_OK) {
86*5b013eabSJohn Scipione 		delete fIcon;
87*5b013eabSJohn Scipione 		fIcon = NULL;
88*5b013eabSJohn Scipione 	}
8938ac8defSOliver Tappe }
9038ac8defSOliver Tappe 
9138ac8defSOliver Tappe 
92*5b013eabSJohn Scipione void
SetCountry(BCountry * country)93*5b013eabSJohn Scipione TimeZoneListItem::SetCountry(BCountry* country)
9438ac8defSOliver Tappe {
95*5b013eabSJohn Scipione 	delete fCountry;
96*5b013eabSJohn Scipione 	fCountry = country;
97*5b013eabSJohn Scipione }
98*5b013eabSJohn Scipione 
99*5b013eabSJohn Scipione 
100*5b013eabSJohn Scipione void
SetTimeZone(BTimeZone * timeZone)101*5b013eabSJohn Scipione TimeZoneListItem::SetTimeZone(BTimeZone* timeZone)
102*5b013eabSJohn Scipione {
103*5b013eabSJohn Scipione 	delete fTimeZone;
104*5b013eabSJohn Scipione 	fTimeZone = timeZone;
10538ac8defSOliver Tappe }
10638ac8defSOliver Tappe 
10738ac8defSOliver Tappe 
10899d2aa98SOliver Tappe const BString&
ID() const109750e57b8SOliver Tappe TimeZoneListItem::ID() const
1104ae5a452SAdrien Destugues {
111*5b013eabSJohn Scipione 	if (!HasTimeZone())
11299d2aa98SOliver Tappe 		return skDefaultString;
11399d2aa98SOliver Tappe 
114750e57b8SOliver Tappe 	return fTimeZone->ID();
1154ae5a452SAdrien Destugues }
1164ae5a452SAdrien Destugues 
11799d2aa98SOliver Tappe 
11899d2aa98SOliver Tappe const BString&
Name() const11999d2aa98SOliver Tappe TimeZoneListItem::Name() const
12099d2aa98SOliver Tappe {
121*5b013eabSJohn Scipione 	if (!HasTimeZone())
12299d2aa98SOliver Tappe 		return skDefaultString;
12399d2aa98SOliver Tappe 
12499d2aa98SOliver Tappe 	return fTimeZone->Name();
12599d2aa98SOliver Tappe }
12699d2aa98SOliver Tappe 
12799d2aa98SOliver Tappe 
12899d2aa98SOliver Tappe int
OffsetFromGMT() const12999d2aa98SOliver Tappe TimeZoneListItem::OffsetFromGMT() const
13099d2aa98SOliver Tappe {
131*5b013eabSJohn Scipione 	if (!HasTimeZone())
13299d2aa98SOliver Tappe 		return 0;
13399d2aa98SOliver Tappe 
13499d2aa98SOliver Tappe 	return fTimeZone->OffsetFromGMT();
1354ae5a452SAdrien Destugues }
136*5b013eabSJohn Scipione 
137*5b013eabSJohn Scipione 
138*5b013eabSJohn Scipione void
_DrawItemWithTextOffset(BView * owner,BRect frame,bool complete,float textOffset)139*5b013eabSJohn Scipione TimeZoneListItem::_DrawItemWithTextOffset(BView* owner, BRect frame,
140*5b013eabSJohn Scipione 	bool complete, float textOffset)
141*5b013eabSJohn Scipione {
142*5b013eabSJohn Scipione 	rgb_color highColor = owner->HighColor();
143*5b013eabSJohn Scipione 	rgb_color lowColor = owner->LowColor();
144*5b013eabSJohn Scipione 
145*5b013eabSJohn Scipione 	if (IsSelected() || complete) {
146*5b013eabSJohn Scipione 		rgb_color color;
147*5b013eabSJohn Scipione 		if (IsSelected())
148*5b013eabSJohn Scipione 			color = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
149*5b013eabSJohn Scipione 		else
150*5b013eabSJohn Scipione 			color = owner->ViewColor();
151*5b013eabSJohn Scipione 
152*5b013eabSJohn Scipione 		owner->SetHighColor(color);
153*5b013eabSJohn Scipione 		owner->SetLowColor(color);
154*5b013eabSJohn Scipione 		owner->FillRect(frame);
155*5b013eabSJohn Scipione 	} else
156*5b013eabSJohn Scipione 		owner->SetLowColor(owner->ViewColor());
157*5b013eabSJohn Scipione 
158*5b013eabSJohn Scipione 	if (!IsEnabled()) {
159*5b013eabSJohn Scipione 		rgb_color textColor = ui_color(B_LIST_ITEM_TEXT_COLOR);
160*5b013eabSJohn Scipione 		if (textColor.red + textColor.green + textColor.blue > 128 * 3)
161*5b013eabSJohn Scipione 			owner->SetHighColor(tint_color(textColor, B_DARKEN_2_TINT));
162*5b013eabSJohn Scipione 		else
163*5b013eabSJohn Scipione 			owner->SetHighColor(tint_color(textColor, B_LIGHTEN_2_TINT));
164*5b013eabSJohn Scipione 	} else {
165*5b013eabSJohn Scipione 		if (IsSelected())
166*5b013eabSJohn Scipione 			owner->SetHighColor(ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR));
167*5b013eabSJohn Scipione 		else
168*5b013eabSJohn Scipione 			owner->SetHighColor(ui_color(B_LIST_ITEM_TEXT_COLOR));
169*5b013eabSJohn Scipione 	}
170*5b013eabSJohn Scipione 
171*5b013eabSJohn Scipione 	owner->MovePenTo(
172*5b013eabSJohn Scipione 		frame.left + be_control_look->DefaultLabelSpacing() + textOffset,
173*5b013eabSJohn Scipione 		frame.top + BaselineOffset());
174*5b013eabSJohn Scipione 	owner->DrawString(Text());
175*5b013eabSJohn Scipione 
176*5b013eabSJohn Scipione 	owner->SetHighColor(highColor);
177*5b013eabSJohn Scipione 	owner->SetLowColor(lowColor);
178*5b013eabSJohn Scipione }
179