xref: /haiku/src/preferences/time/TimeZoneListItem.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2010, Haiku Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Adrien Destugues <pulkomandy@pulkomandy.ath.cx>
7  *		Stephan Aßmus <superstippi@gmx.de>
8  *		Axel Dörfler, axeld@pinc-software.de
9  *		Oliver Tappe <zooey@hirschkaefer.de>
10 */
11 
12 
13 #include "TimeZoneListItem.h"
14 
15 #include <new>
16 
17 #include <Bitmap.h>
18 #include <Country.h>
19 #include <String.h>
20 #include <TimeZone.h>
21 #include <Window.h>
22 
23 
24 static const BString skDefaultString;
25 
26 
27 TimeZoneListItem::TimeZoneListItem(const char* text, BCountry* country,
28 	BTimeZone* timeZone)
29 	:
30 	BStringItem(text, 0, false),
31 	fIcon(NULL),
32 	fTimeZone(timeZone)
33 {
34 	if (country != NULL) {
35 		fIcon = new(std::nothrow) BBitmap(BRect(0, 0, 15, 15), B_RGBA32);
36 		if (fIcon != NULL && country->GetIcon(fIcon) != B_OK) {
37 			delete fIcon;
38 			fIcon = NULL;
39 		}
40 	}
41 }
42 
43 
44 TimeZoneListItem::~TimeZoneListItem()
45 {
46 	delete fIcon;
47 	delete fTimeZone;
48 }
49 
50 
51 void
52 TimeZoneListItem::DrawItem(BView* owner, BRect frame, bool complete)
53 {
54 	rgb_color kHighlight = {140, 140, 140, 0};
55 	rgb_color kBlack = {0, 0, 0, 0};
56 
57 	if (IsSelected() || complete) {
58 		rgb_color color;
59 		if (IsSelected())
60 			color = kHighlight;
61 		else
62 			color = owner->ViewColor();
63 		owner->SetHighColor(color);
64 		owner->SetLowColor(color);
65 		owner->FillRect(frame);
66 		owner->SetHighColor(kBlack);
67 	} else
68 		owner->SetLowColor(owner->ViewColor());
69 
70 	// Draw the text
71 	BString text = Text();
72 
73 	BFont font = be_plain_font;
74 	font_height	finfo;
75 	font.GetHeight(&finfo);
76 	owner->SetFont(&font);
77 	// TODO: the position is unnecessarily complicated, and not correct either
78 	owner->MovePenTo(frame.left + 8, frame.top
79 		+ (frame.Height() - (finfo.ascent + finfo.descent + finfo.leading)) / 2
80 		+ (finfo.ascent + finfo.descent) - 1);
81 	owner->DrawString(text.String());
82 
83 	// Draw the icon
84 	frame.left = frame.right - 16;
85 	BRect iconFrame(frame);
86 	iconFrame.Set(iconFrame.left, iconFrame.top + 1, iconFrame.left + 15,
87 		iconFrame.top + 16);
88 
89 	if (fIcon != NULL && fIcon->IsValid()) {
90 		owner->SetDrawingMode(B_OP_OVER);
91 		owner->DrawBitmap(fIcon, iconFrame);
92 		owner->SetDrawingMode(B_OP_COPY);
93 	}
94 }
95 
96 
97 bool
98 TimeZoneListItem::HasTimeZone() const
99 {
100 	return fTimeZone != NULL;
101 }
102 
103 
104 const BTimeZone&
105 TimeZoneListItem::TimeZone() const
106 {
107 	return *fTimeZone;
108 }
109 
110 
111 const BString&
112 TimeZoneListItem::ID() const
113 {
114 	if (fTimeZone == NULL)
115 		return skDefaultString;
116 
117 	return fTimeZone->ID();
118 }
119 
120 
121 const BString&
122 TimeZoneListItem::Name() const
123 {
124 	if (fTimeZone == NULL)
125 		return skDefaultString;
126 
127 	return fTimeZone->Name();
128 }
129 
130 
131 int
132 TimeZoneListItem::OffsetFromGMT() const
133 {
134 	if (fTimeZone == NULL)
135 		return 0;
136 
137 	return fTimeZone->OffsetFromGMT();
138 }
139