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