xref: /haiku/src/apps/powerstatus/ExtendedInfoWindow.cpp (revision 981f1b1135291a4fca290fbdf69910dc2f24abdd)
1 /*
2  * Copyright 2006, Haiku, Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Clemens Zeidler, haiku@Clemens-Zeidler.de
7  */
8 
9 #include "ExtendedInfoWindow.h"
10 
11 #include <Box.h>
12 #include <GroupLayout.h>
13 #include <GroupView.h>
14 #include <SpaceLayoutItem.h>
15 #include <String.h>
16 
17 
18 BatteryInfoView::BatteryInfoView(BRect frame, int32 resizingMode)
19 	:
20 	BView(frame, "battery info view", resizingMode, B_WILL_DRAW |
21 		B_FULL_UPDATE_ON_RESIZE)
22 {
23 	SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
24 }
25 
26 
27 void
28 BatteryInfoView::Update(battery_info& info, acpi_extended_battery_info& extInfo)
29 {
30 	fBatteryInfo = info;
31 	fBatteryExtendedInfo = extInfo;
32 }
33 
34 
35 void
36 BatteryInfoView::Draw(BRect updateRect)
37 {
38 	SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
39 
40 	BString powerUnit;
41 	BString rateUnit;
42 	switch (fBatteryExtendedInfo.power_unit) {
43 		case 0:
44 			powerUnit = " mWh";
45 			rateUnit = " mW";
46 			break;
47 
48 		case 1:
49 			powerUnit = " mAh";
50 			rateUnit = " mA";
51 			break;
52 	}
53 
54 	BString text;
55 	if (fBatteryInfo.state & BATTERY_CHARGING)
56 		text = "Battery charging";
57 	else if (fBatteryInfo.state & BATTERY_DISCHARGING)
58 		text = "Battery discharging";
59 	else if (fBatteryInfo.state & BATTERY_CRITICAL_STATE)
60 		text = "Empty Battery Slot";
61 	else
62 		text = "Battery unused";
63 	BPoint point(10, 10);
64 	int textHeight = 15;
65 	int space = textHeight + 5;
66 	DrawString(text.String(), point);
67 	point.y += space;
68 
69 	text = "Capacity: ";
70 	text << fBatteryInfo.capacity;
71 	text << powerUnit;
72 	DrawString(text.String(), point);
73 	point.y += space;
74 
75 	text = "Last full Charge: ";
76 	text << fBatteryInfo.full_capacity;
77 	text << powerUnit;
78 	DrawString(text.String(), point);
79 	point.y += space;
80 
81 	text = "Current Rate: ";
82 	text << fBatteryInfo.current_rate;
83 	text << rateUnit;
84 	DrawString(text.String(), point);
85 	point.y += space;
86 
87 	point.y += space;
88 
89 	text = "Design Capacity: ";
90 	text << fBatteryExtendedInfo.design_capacity;
91 	text << powerUnit;
92 	DrawString(text.String(), point);
93 	point.y += space;
94 
95 	text = "Technology: ";
96 	text << fBatteryExtendedInfo.technology;
97 	DrawString(text.String(), point);
98 	point.y += space;
99 
100 	text = "Design Voltage: ";
101 	text << fBatteryExtendedInfo.design_voltage;
102 	text << " mV";
103 	DrawString(text.String(), point);
104 	point.y += space;
105 
106 	text = "Design Capacity Warning: ";
107 	text << fBatteryExtendedInfo.design_capacity_warning;
108 	text << powerUnit;
109 	DrawString(text.String(), point);
110 	point.y += space;
111 
112 	text = "Design Capacity low Warning: ";
113 	text << fBatteryExtendedInfo.design_capacity_low;
114 	text << powerUnit;
115 	DrawString(text.String(), point);
116 	point.y += space;
117 
118 	text = "Capacity Granularity 1: ";
119 	text << fBatteryExtendedInfo.capacity_granularity_1;
120 	DrawString(text.String(), point);
121 	point.y += space;
122 
123 	text = "Capacity Granularity 2: ";
124 	text << fBatteryExtendedInfo.capacity_granularity_2;
125 	DrawString(text.String(), point);
126 	point.y += space;
127 
128 	text = "Model Number: ";
129 	text << fBatteryExtendedInfo.model_number;
130 	DrawString(text.String(), point);
131 	point.y += space;
132 
133 	text = "Serial number: ";
134 	text << fBatteryExtendedInfo.serial_number;
135 	DrawString(text.String(), point);
136 	point.y += space;
137 
138 	text = "Type: ";
139 	text += fBatteryExtendedInfo.type;
140 	DrawString(text.String(), point);
141 	point.y += space;
142 
143 	text = "OEM Info: ";
144 	text += fBatteryExtendedInfo.oem_info;
145 	DrawString(text.String(), point);
146 	point.y += space;
147 
148 }
149 
150 
151 ExtPowerStatusView::ExtPowerStatusView(PowerStatusDriverInterface* interface,
152 			BRect frame, int32 resizingMode, int batteryId,
153 			ExtendedInfoWindow* window)
154 	:
155 	PowerStatusView(interface, frame, resizingMode, batteryId),
156 	fExtendedInfoWindow(window),
157 	fBatteryInfoView(window->GetExtendedBatteryInfoView()),
158 	fSelected(false)
159 {
160 
161 }
162 
163 
164 void
165 ExtPowerStatusView::Draw(BRect updateRect)
166 {
167 	if (fSelected) {
168 		SetLowColor(102, 152, 203);
169 		SetHighColor(102, 152, 203);
170 		FillRect(updateRect);
171 	}
172 	else {
173 		SetLowColor(ui_color(B_PANEL_BACKGROUND_COLOR));
174 		SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
175 		FillRect(updateRect);
176 	}
177 
178 	PowerStatusView::Draw(updateRect);
179 }
180 
181 
182 void
183 ExtPowerStatusView::MouseDown(BPoint where)
184 {
185 	if (!fSelected) {
186 		fSelected = true;
187 		_Update(true);
188 		fExtendedInfoWindow->BatterySelected(this);
189 	}
190 }
191 
192 
193 void
194 ExtPowerStatusView::Select(bool select)
195 {
196 	fSelected = select;
197 	_Update(true);
198 }
199 
200 
201 bool
202 ExtPowerStatusView::IsValid()
203 {
204 	if (fBatteryInfo.state & BATTERY_CRITICAL_STATE)
205 		return false;
206 	return true;
207 }
208 
209 
210 void
211 ExtPowerStatusView::_Update(bool force)
212 {
213 	PowerStatusView::_Update(force);
214 	if (!fSelected)
215 		return;
216 
217 	acpi_extended_battery_info extInfo;
218 	fDriverInterface->GetExtendedBatteryInfo(&extInfo, fBatteryId);
219 
220 	fBatteryInfoView->Update(fBatteryInfo, extInfo);
221 	fBatteryInfoView->Invalidate();
222 }
223 
224 
225 ExtendedInfoWindow::ExtendedInfoWindow(PowerStatusDriverInterface* interface)
226 	:
227 	BWindow(BRect(100, 150, 500, 500), "Extended Battery Info", B_TITLED_WINDOW,
228 		B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS),
229 	fDriverInterface(interface),
230 	fSelectedView(NULL)
231 {
232 	BView *view = new BView(Bounds(), "view", B_FOLLOW_ALL, 0);
233 	view->SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
234 	AddChild(view);
235 
236 	BGroupLayout* mainLayout = new BGroupLayout(B_VERTICAL);
237 	mainLayout->SetSpacing(10);
238 	mainLayout->SetInsets(10, 10, 10, 10);
239 	view->SetLayout(mainLayout);
240 
241 	BRect rect = Bounds();
242 	rect.InsetBy(5, 5);
243 	BBox *infoBox = new BBox(rect, "Power Status Box");
244 	infoBox->SetLabel("Battery Info");
245 	BGroupLayout* infoLayout = new BGroupLayout(B_HORIZONTAL);
246 	infoLayout->SetInsets(10, infoBox->TopBorderOffset() * 2 + 10, 10, 10);
247 	infoLayout->SetSpacing(10);
248 	infoBox->SetLayout(infoLayout);
249 	mainLayout->AddView(infoBox);
250 
251 	BGroupView* batteryView = new BGroupView(B_VERTICAL);
252 	batteryView->GroupLayout()->SetSpacing(10);
253 	infoLayout->AddView(batteryView);
254 
255 	fBatteryInfoView = new BatteryInfoView(BRect(0, 0, 270, 310), B_FOLLOW_ALL);
256 
257 	BGroupLayout* batteryLayout = batteryView->GroupLayout();
258 	BRect batteryRect(0, 0, 50, 30);
259 	for (int i = 0; i < interface->GetBatteryCount(); i++) {
260 		ExtPowerStatusView* view = new ExtPowerStatusView(interface,
261 			batteryRect, B_FOLLOW_ALL, i, this);
262 		batteryLayout->AddView(view);
263 		fBatteryViewList.AddItem(view);
264 		fDriverInterface->StartWatching(view);
265 		if (view->IsValid())
266 			fSelectedView = view;
267 	}
268 
269 	batteryLayout->AddItem(BSpaceLayoutItem::CreateGlue());
270 
271 	infoLayout->AddView(fBatteryInfoView, 20);
272 
273 	if (!fSelectedView && fBatteryViewList.CountItems() > 0)
274 		fSelectedView = fBatteryViewList.ItemAt(0);
275 	fSelectedView->Select();
276 
277 	BSize size = mainLayout->PreferredSize();
278 	ResizeTo(size.width, size.height);
279 }
280 
281 
282 ExtendedInfoWindow::~ExtendedInfoWindow()
283 {
284 	for (int i = 0; i < fBatteryViewList.CountItems(); i++) {
285 		fDriverInterface->StopWatching(fBatteryViewList.ItemAt(i));
286 	}
287 }
288 
289 
290 BatteryInfoView*
291 ExtendedInfoWindow::GetExtendedBatteryInfoView()
292 {
293 	return fBatteryInfoView;
294 }
295 
296 
297 void
298 ExtendedInfoWindow::BatterySelected(ExtPowerStatusView* view)
299 {
300 	if (fSelectedView) {
301 		fSelectedView->Select(false);
302 		fSelectedView->Invalidate();
303 	}
304 
305 	fSelectedView = view;
306 }
307