xref: /haiku/src/preferences/bluetooth/DeviceListItem.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2009, Oliver Ruiz Dorantes, <oliver.ruiz.dorantes_at_gmail.com>
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 
6 #include <Bitmap.h>
7 #include <View.h>
8 
9 #include <bluetooth/bdaddrUtils.h>
10 #include <bluetooth/BluetoothDevice.h>
11 /*#include "../media/iconfile.h"*/
12 
13 #include "DeviceListItem.h"
14 
15 #define INSETS  5
16 #define TEXT_ROWS  2
17 
18 namespace Bluetooth {
19 
20 DeviceListItem::DeviceListItem(BluetoothDevice* bDevice)
21 	:
22 	BListItem(),
23 	fDevice(bDevice),
24 	fName("unknown")
25 {
26 	fAddress = bDevice->GetBluetoothAddress();
27 	fClass = bDevice->GetDeviceClass();
28 }
29 
30 
31 void
32 DeviceListItem::SetDevice(BluetoothDevice* bDevice)
33 {
34 	fAddress = bDevice->GetBluetoothAddress();
35 	fClass = bDevice->GetDeviceClass();
36 	fName = bDevice->GetFriendlyName();
37 	// AKAIR rssi we can just have it @ inquiry time...
38 }
39 
40 
41 DeviceListItem::~DeviceListItem()
42 {
43 
44 }
45 
46 
47 void
48 DeviceListItem::DrawItem(BView* owner, BRect itemRect, bool	complete)
49 {
50 	rgb_color	kBlack = { 0, 0, 0, 0 };
51 	rgb_color	kHighlight = { 156, 154, 156, 0 };
52 
53 	if (IsSelected() || complete) {
54 		rgb_color	color;
55 		if (IsSelected())
56 			color = kHighlight;
57 		else
58 			color = owner->ViewColor();
59 
60 		owner->SetHighColor(color);
61 		owner->SetLowColor(color);
62 		owner->FillRect(itemRect);
63 		owner->SetHighColor(kBlack);
64 
65 	} else {
66 		owner->SetLowColor(owner->ViewColor());
67 	}
68 
69 	font_height	finfo;
70 	be_plain_font->GetHeight(&finfo);
71 
72 	BPoint point = BPoint(itemRect.left	+ DeviceClass::PixelsForIcon
73 		+ 2 * INSETS, itemRect.bottom - finfo.descent + 1);
74 	owner->SetFont(be_fixed_font);
75 	owner->SetHighColor(kBlack);
76 	owner->MovePenTo(point);
77 
78 	BString secondLine;
79 
80 	secondLine << bdaddrUtils::ToString(fAddress) << "   ";
81 	fClass.GetMajorDeviceClass(secondLine);
82 	secondLine << " / ";
83 	fClass.GetMinorDeviceClass(secondLine);
84 
85 	owner->DrawString(secondLine.String());
86 
87 	point -= BPoint(0, (finfo.ascent + finfo.descent + finfo.leading) + INSETS);
88 
89 	owner->SetFont(be_plain_font);
90 	owner->MovePenTo(point);
91 	owner->DrawString(fName.String());
92 
93 	fClass.Draw(owner, BPoint(itemRect.left, itemRect.top));
94 
95 #if 0
96 	switch (fClass.GetMajorDeviceClass()) {
97 		case 1:
98 		{
99 			BRect iconRect(0, 0, 15, 15);
100 			BBitmap* icon  = new BBitmap(iconRect, B_CMAP8);
101 			icon->SetBits(kTVBits, kTVWidth * kTVHeight, 0, kTVColorSpace);
102 			owner->DrawBitmap(icon, iconRect, BRect(itemRect.left + INSETS,
103 				itemRect.top + INSETS, itemRect.left + INSETS + PIXELS_FOR_ICON,
104 				itemRect.top + INSETS + PIXELS_FOR_ICON));
105 			break;
106 		}
107 		case 4:
108 		{
109 			BRect iconRect(0, 0, 15, 15);
110 			BBitmap* icon = new BBitmap(iconRect, B_CMAP8);
111 			icon->SetBits(kMixerBits, kMixerWidth * kMixerHeight, 0, kMixerColorSpace);
112 			owner->DrawBitmap(icon, iconRect, BRect(itemRect.left + INSETS,
113 				itemRect.top + INSETS, itemRect.left + INSETS + PIXELS_FOR_ICON,
114 				itemRect.top + INSETS + PIXELS_FOR_ICON));
115 			break;
116 		}
117 	}
118 #endif
119 
120 	owner->SetHighColor(kBlack);
121 
122 }
123 
124 
125 void
126 DeviceListItem::Update(BView* owner, const BFont* font)
127 {
128 	BListItem::Update(owner, font);
129 
130 	font_height height;
131 	font->GetHeight(&height);
132 	SetHeight(MAX((height.ascent + height.descent + height.leading) * TEXT_ROWS
133 		+ (TEXT_ROWS + 1)*INSETS, DeviceClass::PixelsForIcon + 2 * INSETS));
134 
135 }
136 
137 
138 int
139 DeviceListItem::Compare(const void	*firstArg, const void	*secondArg)
140 {
141 	const DeviceListItem* item1 = *static_cast<const DeviceListItem* const *>
142 		(firstArg);
143 	const DeviceListItem* item2 = *static_cast<const DeviceListItem* const *>
144 		(secondArg);
145 
146 	return (int)bdaddrUtils::Compare(item1->fAddress, item2->fAddress);
147 }
148 
149 
150 BluetoothDevice*
151 DeviceListItem::Device() const
152 {
153 	return fDevice;
154 }
155 
156 
157 }
158