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