xref: /haiku/src/add-ons/input_server/devices/keyboard/TeamListItem.cpp (revision 1b80286772b529a3d6de3bbeb0720c62e6a32fed)
1 /*
2  * Copyright 2004-2008, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Jérôme Duval
7  */
8 
9 #include "TeamListItem.h"
10 
11 #include <string.h>
12 
13 #include <FindDirectory.h>
14 #include <NodeInfo.h>
15 #include <Path.h>
16 #include <View.h>
17 
18 
19 static const int32 kItemMargin = 2;
20 
21 
22 TeamListItem::TeamListItem(team_info &tinfo)
23 	:
24 	fInfo(tinfo),
25 	fIcon(BRect(0, 0, 15, 15), B_RGBA32),
26 	fLargeIcon(BRect(0, 0, 31, 31), B_RGBA32)
27 {
28 	int32 cookie = 0;
29 	image_info info;
30 	if (get_next_image_info(tinfo.team, &cookie, &info) == B_OK) {
31 		fPath = BPath(info.name);
32 		BNode node(info.name);
33 		BNodeInfo nodeInfo(&node);
34 		nodeInfo.GetTrackerIcon(&fIcon, B_MINI_ICON);
35 		nodeInfo.GetTrackerIcon(&fLargeIcon, B_LARGE_ICON);
36 	}
37 }
38 
39 
40 TeamListItem::~TeamListItem()
41 {
42 }
43 
44 
45 void
46 TeamListItem::DrawItem(BView *owner, BRect frame, bool complete)
47 {
48 	rgb_color kHighlight = { 140,140,140,0 };
49 	rgb_color kBlack = { 0,0,0,0 };
50 	rgb_color kBlue = { 0,0,255,0 };
51 
52 	BRect r(frame);
53 
54 	if (IsSelected() || complete) {
55 		rgb_color color;
56 		if (IsSelected())
57 			color = kHighlight;
58 		else
59 			color = owner->ViewColor();
60 
61 		owner->SetHighColor(color);
62 		owner->SetLowColor(color);
63 		owner->FillRect(r);
64 		owner->SetHighColor(kBlack);
65 	} else {
66 		owner->SetLowColor(owner->ViewColor());
67 	}
68 
69 	frame.left += 4;
70 	BRect iconFrame(frame);
71 	iconFrame.Set(iconFrame.left, iconFrame.top + 1, iconFrame.left + 15,
72 		iconFrame.top + 16);
73 	owner->SetDrawingMode(B_OP_ALPHA);
74 	owner->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
75 	owner->DrawBitmap(&fIcon, iconFrame);
76 	owner->SetDrawingMode(B_OP_COPY);
77 
78 	frame.left += 16;
79 	owner->SetHighColor(IsSystemServer() ? kBlue : kBlack);
80 
81 	BFont font = be_plain_font;
82 	font_height	finfo;
83 	font.GetHeight(&finfo);
84 	owner->SetFont(&font);
85 	owner->MovePenTo(frame.left + 8, frame.top + ((frame.Height()
86 			- (finfo.ascent + finfo.descent + finfo.leading)) / 2)
87 		+ finfo.ascent);
88 	owner->DrawString(fPath.Leaf());
89 }
90 
91 
92 /*static*/ int32
93 TeamListItem::MinimalHeight()
94 {
95 	return 16 + kItemMargin;
96 }
97 
98 
99 void
100 TeamListItem::Update(BView* owner, const BFont* font)
101 {
102 	// we need to override the update method so we can make sure
103 	// the list item size doesn't change
104 	BListItem::Update(owner, font);
105 	if (Height() < MinimalHeight())
106 		SetHeight(MinimalHeight());
107 }
108 
109 
110 const team_info*
111 TeamListItem::GetInfo()
112 {
113 	return &fInfo;
114 }
115 
116 
117 bool
118 TeamListItem::IsSystemServer()
119 {
120 	static bool firstCall = true;
121 	static BPath systemServersPath;
122 	static BPath trackerPath;
123 	static BPath deskbarPath;
124 
125 	if (firstCall) {
126 		find_directory(B_SYSTEM_SERVERS_DIRECTORY, &systemServersPath);
127 
128 		find_directory(B_SYSTEM_DIRECTORY, &trackerPath);
129 		trackerPath.Append("Tracker");
130 
131 		find_directory(B_SYSTEM_DIRECTORY, &deskbarPath);
132 		deskbarPath.Append("Deskbar");
133 
134 		firstCall = false;
135 	}
136 
137 	if (strncmp(systemServersPath.Path(), fInfo.args, strlen(systemServersPath.Path())) == 0)
138 		return true;
139 
140 	if (strncmp(trackerPath.Path(), fInfo.args, strlen(trackerPath.Path())) == 0)
141 		return true;
142 
143 	if (strncmp(deskbarPath.Path(), fInfo.args, strlen(deskbarPath.Path())) == 0)
144 		return true;
145 
146 	return false;
147 }
148