xref: /haiku/src/add-ons/input_server/devices/keyboard/TeamListItem.cpp (revision 372b901dfeada686207d00bbcce456f748bbda12)
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 <LocaleRoster.h>
15 #include <NodeInfo.h>
16 #include <Path.h>
17 #include <View.h>
18 
19 
20 static const int32 kItemMargin = 2;
21 
22 
23 bool gLocalizedNamePreferred;
24 
25 
26 TeamListItem::TeamListItem(team_info &teamInfo)
27 	:
28 	fTeamInfo(teamInfo),
29 	fAppInfo(),
30 	fMiniIcon(BRect(0, 0, 15, 15), B_RGBA32),
31 	fLargeIcon(BRect(0, 0, 31, 31), B_RGBA32),
32 	fFound(false),
33 	fRefusingToQuit(false)
34 {
35 	int32 cookie = 0;
36 	image_info info;
37 	if (get_next_image_info(teamInfo.team, &cookie, &info) == B_OK) {
38 		fPath = BPath(info.name);
39 		BNode node(info.name);
40 		BNodeInfo nodeInfo(&node);
41 		nodeInfo.GetTrackerIcon(&fMiniIcon, B_MINI_ICON);
42 		nodeInfo.GetTrackerIcon(&fLargeIcon, B_LARGE_ICON);
43 	}
44 
45 	if (be_roster->GetRunningAppInfo(fTeamInfo.team, &fAppInfo) != B_OK)
46 		fAppInfo.signature[0] = '\0';
47 
48 	CacheLocalizedName();
49 }
50 
51 
52 TeamListItem::~TeamListItem()
53 {
54 }
55 
56 
57 void
58 TeamListItem::CacheLocalizedName()
59 {
60 	if (BLocaleRoster::Default()->GetLocalizedFileName(fLocalizedName,
61 			fAppInfo.ref, true) != B_OK)
62 		fLocalizedName = fPath.Leaf();
63 }
64 
65 
66 void
67 TeamListItem::DrawItem(BView* owner, BRect frame, bool complete)
68 {
69 	rgb_color kHighlight = ui_color(B_LIST_SELECTED_BACKGROUND_COLOR);
70 	rgb_color kHighlightText = ui_color(B_LIST_SELECTED_ITEM_TEXT_COLOR);
71 	rgb_color kText = ui_color(B_LIST_ITEM_TEXT_COLOR);
72 
73 	rgb_color kIdealRed = { 255, 0, 0, 0 };
74 	rgb_color kIdealBlue = { 0, 0, 255, 0 };
75 	rgb_color kRed = mix_color(kIdealRed, kText, 191);
76 	rgb_color kBlue = mix_color(kIdealBlue, kText, 191);
77 	rgb_color kHighlightRed = mix_color(kIdealRed, kHighlightText, 191);
78 	rgb_color kHighlightBlue = mix_color(kIdealBlue, kHighlightText, 191);
79 
80 	BRect r(frame);
81 
82 	if (IsSelected() || complete) {
83 		owner->SetHighColor(kHighlight);
84 		owner->SetLowColor(kHighlight);
85 		owner->FillRect(r);
86 	}
87 
88 	frame.left += 4;
89 	BRect iconFrame(frame);
90 	iconFrame.Set(iconFrame.left, iconFrame.top + 1, iconFrame.left + 15,
91 		iconFrame.top + 16);
92 	owner->SetDrawingMode(B_OP_ALPHA);
93 	owner->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
94 	owner->DrawBitmap(&fMiniIcon, iconFrame);
95 	owner->SetDrawingMode(B_OP_COPY);
96 
97 	frame.left += 16;
98 	if (fRefusingToQuit)
99 		owner->SetHighColor(IsSelected() ? kHighlightRed : kRed);
100 	else {
101 		if (IsSystemServer())
102 			owner->SetHighColor(IsSelected() ? kHighlightBlue : kBlue);
103 		else
104 			owner->SetHighColor(IsSelected() ? kHighlightText : kText);
105 	}
106 	BFont font = be_plain_font;
107 	font_height	finfo;
108 	font.GetHeight(&finfo);
109 	owner->SetFont(&font);
110 	owner->MovePenTo(frame.left + 8, frame.top + ((frame.Height()
111 			- (finfo.ascent + finfo.descent + finfo.leading)) / 2)
112 		+ finfo.ascent);
113 
114 	if (gLocalizedNamePreferred)
115 		owner->DrawString(fLocalizedName.String());
116 	else
117 		owner->DrawString(fPath.Leaf());
118 }
119 
120 
121 /*static*/ int32
122 TeamListItem::MinimalHeight()
123 {
124 	return 16 + kItemMargin;
125 }
126 
127 
128 void
129 TeamListItem::Update(BView* owner, const BFont* font)
130 {
131 	// we need to override the update method so we can make sure
132 	// the list item size doesn't change
133 	BListItem::Update(owner, font);
134 	if (Height() < MinimalHeight())
135 		SetHeight(MinimalHeight());
136 }
137 
138 
139 const team_info*
140 TeamListItem::GetInfo()
141 {
142 	return &fTeamInfo;
143 }
144 
145 
146 bool
147 TeamListItem::IsSystemServer()
148 {
149 	static bool firstCall = true;
150 	static BPath systemServersPath;
151 	static BPath trackerPath;
152 	static BPath deskbarPath;
153 
154 	if (firstCall) {
155 		find_directory(B_SYSTEM_SERVERS_DIRECTORY, &systemServersPath);
156 
157 		find_directory(B_SYSTEM_DIRECTORY, &trackerPath);
158 		trackerPath.Append("Tracker");
159 
160 		find_directory(B_SYSTEM_DIRECTORY, &deskbarPath);
161 		deskbarPath.Append("Deskbar");
162 
163 		firstCall = false;
164 	}
165 
166 	if (strncmp(systemServersPath.Path(), fTeamInfo.args,
167 			strlen(systemServersPath.Path())) == 0)
168 		return true;
169 
170 	if (strncmp(trackerPath.Path(), fTeamInfo.args,
171 			strlen(trackerPath.Path())) == 0)
172 		return true;
173 
174 	if (strncmp(deskbarPath.Path(), fTeamInfo.args,
175 			strlen(deskbarPath.Path())) == 0)
176 		return true;
177 
178 	return false;
179 }
180 
181 
182 bool
183 TeamListItem::IsApplication() const
184 {
185 	return fAppInfo.signature[0] != '\0';
186 }
187 
188 
189 void
190 TeamListItem::SetRefusingToQuit(bool refusing)
191 {
192 	fRefusingToQuit = refusing;
193 }
194 
195 
196 bool
197 TeamListItem::IsRefusingToQuit()
198 {
199 	return fRefusingToQuit;
200 }
201