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