xref: /haiku/src/apps/deskbar/TeamMenuItem.cpp (revision 15fb7d88e971c4d6c787c6a3a5c159afb1ebf77b)
1 /*
2 Open Tracker License
3 
4 Terms and Conditions
5 
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7 
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
14 
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
17 
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
28 
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
30 trademarks of Be Incorporated in the United States and other countries. Other
31 brand product names are registered trademarks or trademarks of their respective
32 holders.
33 All rights reserved.
34 */
35 
36 
37 #include "TeamMenuItem.h"
38 
39 #include <string.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 
43 #include <Bitmap.h>
44 #include <ControlLook.h>
45 #include <Debug.h>
46 #include <Font.h>
47 #include <Region.h>
48 #include <Roster.h>
49 #include <Resources.h>
50 
51 #include "BarApp.h"
52 #include "BarMenuBar.h"
53 #include "BarView.h"
54 #include "ExpandoMenuBar.h"
55 #include "ResourceSet.h"
56 #include "ShowHideMenuItem.h"
57 #include "TeamMenu.h"
58 #include "WindowMenu.h"
59 #include "WindowMenuItem.h"
60 
61 
62 const float kHPad = 8.0f;
63 const float kVPad = 2.0f;
64 const float kLabelOffset = 8.0f;
65 const float kSwitchWidth = 12.0f;
66 
67 
68 //	#pragma mark - TTeamMenuItem
69 
70 
71 TTeamMenuItem::TTeamMenuItem(BList* team, BBitmap* icon, char* name,
72 	char* signature, float width, float height)
73 	:
74 	TTruncatableMenuItem(new TWindowMenu(team, signature))
75 {
76 	_Init(team, icon, name, signature, width, height);
77 }
78 
79 
80 TTeamMenuItem::TTeamMenuItem(float width, float height)
81 	:
82 	TTruncatableMenuItem("", NULL)
83 {
84 	_Init(NULL, NULL, strdup(""), strdup(""), width, height);
85 }
86 
87 
88 TTeamMenuItem::~TTeamMenuItem()
89 {
90 	delete fTeam;
91 	delete fIcon;
92 	free(fSignature);
93 }
94 
95 
96 status_t
97 TTeamMenuItem::Invoke(BMessage* message)
98 {
99 	if (fBarView->InvokeItem(Signature())) {
100 		// handles drop on application
101 		return B_OK;
102 	}
103 
104 	// if the app could not handle the drag message
105 	// and we were dragging, then kill the drag
106 	// should never get here, disabled item will not invoke
107 	if (fBarView != NULL && fBarView->Dragging())
108 		fBarView->DragStop();
109 
110 	// bring to front or minimize shortcuts
111 	uint32 mods = modifiers();
112 	if (mods & B_CONTROL_KEY) {
113 		TShowHideMenuItem::TeamShowHideCommon((mods & B_SHIFT_KEY)
114 			? B_MINIMIZE_WINDOW : B_BRING_TO_FRONT, Teams());
115 	}
116 
117 	return BMenuItem::Invoke(message);
118 }
119 
120 
121 void
122 TTeamMenuItem::SetOverrideSelected(bool selected)
123 {
124 	fOverriddenSelected = selected;
125 	Highlight(selected);
126 }
127 
128 
129 void
130 TTeamMenuItem::SetIcon(BBitmap* icon) {
131 	delete fIcon;
132 	fIcon = icon;
133 }
134 
135 
136 void
137 TTeamMenuItem::GetContentSize(float* width, float* height)
138 {
139 	BRect iconBounds;
140 
141 	if (fIcon != NULL)
142 		iconBounds = fIcon->Bounds();
143 	else
144 		iconBounds = BRect(0, 0, kMinimumIconSize - 1, kMinimumIconSize - 1);
145 
146 	BMenuItem::GetContentSize(width, height);
147 
148 	if (fOverrideWidth != -1.0f)
149 		*width = fOverrideWidth;
150 	else {
151 		*width = kHPad + iconBounds.Width() + kHPad;
152 		if (iconBounds.Width() <= 32
153 			&& !static_cast<TBarApp*>(be_app)->Settings()->hideLabels) {
154 			*width += LabelWidth() + kHPad;
155 		}
156 	}
157 
158 	if (fOverrideHeight != -1.0f)
159 		*height = fOverrideHeight;
160 	else {
161 		if (fBarView->Vertical()) {
162 			*height = iconBounds.Height() + kVPad * 2;
163 			if (!static_cast<TBarApp*>(be_app)->Settings()->hideLabels
164 				&& iconBounds.Width() > 32) {
165 				*height += fLabelAscent + fLabelDescent;
166 			}
167 		} else {
168 			*height = iconBounds.Height() + kVPad * 2;
169 		}
170 	}
171 	*height += 2;
172 }
173 
174 
175 void
176 TTeamMenuItem::Draw()
177 {
178 	BRect frame = Frame();
179 	BMenu* menu = Menu();
180 
181 	menu->PushState();
182 
183 	rgb_color menuColor = ui_color(B_MENU_BACKGROUND_COLOR);
184 	bool canHandle = !fBarView->Dragging()
185 		|| fBarView->AppCanHandleTypes(Signature());
186 	uint32 flags = 0;
187 	if (_IsSelected() && canHandle)
188 		flags |= BControlLook::B_ACTIVATED;
189 
190 	uint32 borders = BControlLook::B_TOP_BORDER;
191 	if (fBarView->Vertical()) {
192 		menu->SetHighColor(tint_color(menuColor, B_DARKEN_1_TINT));
193 		borders |= BControlLook::B_LEFT_BORDER
194 			| BControlLook::B_RIGHT_BORDER;
195 		menu->StrokeLine(frame.LeftBottom(), frame.RightBottom());
196 		frame.bottom--;
197 
198 		be_control_look->DrawMenuBarBackground(menu, frame, frame,
199 			menuColor, flags, borders);
200 	} else {
201 		if (flags & BControlLook::B_ACTIVATED)
202 			menu->SetHighColor(tint_color(menuColor, B_DARKEN_3_TINT));
203 		else
204 			menu->SetHighColor(tint_color(menuColor, 1.22));
205 		borders |= BControlLook::B_BOTTOM_BORDER;
206 		menu->StrokeLine(frame.LeftTop(), frame.LeftBottom());
207 		frame.left++;
208 
209 		be_control_look->DrawButtonBackground(menu, frame, frame,
210 			menuColor, flags, borders);
211 	}
212 
213 	menu->MovePenTo(ContentLocation());
214 	DrawContent();
215 
216 	menu->PopState();
217 }
218 
219 
220 void
221 TTeamMenuItem::DrawContent()
222 {
223 	BMenu* menu = Menu();
224 	if (fIcon != NULL) {
225 		if (fIcon->ColorSpace() == B_RGBA32) {
226 			menu->SetDrawingMode(B_OP_ALPHA);
227 			menu->SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_OVERLAY);
228 		} else
229 			menu->SetDrawingMode(B_OP_OVER);
230 
231 		BRect frame = Frame();
232 		BRect iconBounds = fIcon->Bounds();
233 		BRect updateRect = iconBounds;
234 		float extra = fBarView->Vertical() ? 0.0f : -1.0f;
235 		BPoint contentLocation = ContentLocation();
236 		BPoint drawLocation = contentLocation + BPoint(kHPad, kVPad);
237 
238 		if (static_cast<TBarApp*>(be_app)->Settings()->hideLabels
239 			|| (fBarView->Vertical() && iconBounds.Width() > 32)) {
240 			float offsetx = contentLocation.x
241 				+ ((frame.Width() - iconBounds.Width()) / 2) + extra;
242 			float offsety = contentLocation.y + 3.0f + extra;
243 
244 			updateRect.OffsetTo(BPoint(offsetx, offsety));
245 			menu->DrawBitmapAsync(fIcon, updateRect);
246 
247 			drawLocation.x = ((frame.Width() - LabelWidth()) / 2);
248 			drawLocation.y = frame.top + iconBounds.Height() + kVPad * 2;
249 		} else {
250 			float offsetx = contentLocation.x + kHPad;
251 			float offsety = contentLocation.y +
252 				((frame.Height() - iconBounds.Height()) / 2) + extra;
253 
254 			updateRect.OffsetTo(BPoint(offsetx, offsety));
255 			menu->DrawBitmapAsync(fIcon, updateRect);
256 
257 			float labelHeight = fLabelAscent + fLabelDescent;
258 			drawLocation.x += iconBounds.Width() + kLabelOffset;
259 			drawLocation.y = frame.top + ((frame.Height() - labelHeight) / 2)
260 				+ extra;
261 		}
262 
263 		menu->MovePenTo(drawLocation);
264 	}
265 
266 	menu->SetDrawingMode(B_OP_OVER);
267 	menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
268 
269 	// override the drawing of the content when the item is disabled
270 	// the wrong lowcolor is used when the item is disabled since the
271 	// text color does not change
272 	menu->MovePenBy(0, fLabelAscent);
273 
274 	bool canHandle = !fBarView->Dragging()
275 		|| fBarView->AppCanHandleTypes(Signature());
276 	if (_IsSelected() && IsEnabled() && canHandle)
277 		menu->SetLowColor(tint_color(ui_color(B_MENU_BACKGROUND_COLOR),
278 			B_HIGHLIGHT_BACKGROUND_TINT));
279 	else
280 		menu->SetLowColor(ui_color(B_MENU_BACKGROUND_COLOR));
281 
282 	if (IsSelected())
283 		menu->SetHighColor(ui_color(B_MENU_SELECTED_ITEM_TEXT_COLOR));
284 	else
285 		menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
286 
287 	if (!static_cast<TBarApp*>(be_app)->Settings()->hideLabels) {
288 		float labelWidth = menu->StringWidth(Label());
289 		BPoint penLocation = menu->PenLocation();
290 		float offset = penLocation.x - Frame().left;
291 		menu->DrawString(Label(labelWidth + offset));
292 	}
293 
294 	if (fBarView->Vertical()
295 		&& static_cast<TBarApp*>(be_app)->Settings()->superExpando
296 		&& fBarView->ExpandoState()) {
297 		DrawExpanderArrow();
298 	}
299 }
300 
301 
302 void
303 TTeamMenuItem::DrawExpanderArrow()
304 {
305 	BMenu* menu = Menu();
306 	BRect frame = Frame();
307 	BRect rect(0.0f, 0.0f, kSwitchWidth, kHPad + 2.0f);
308 
309 	rect.OffsetTo(BPoint(frame.right - rect.Width(),
310 		ContentLocation().y + ((frame.Height() - rect.Height()) / 2)));
311 
312 	float colorTint = B_DARKEN_3_TINT;
313 
314 	rgb_color bgColor = ui_color(B_MENU_BACKGROUND_COLOR);
315 	if (bgColor.red + bgColor.green + bgColor.blue <= 128 * 3) {
316 		colorTint = B_LIGHTEN_2_TINT;
317 	}
318 
319 	be_control_look->DrawArrowShape(menu, rect, rect,
320 		bgColor, fArrowDirection, 0,
321 		colorTint);
322 }
323 
324 
325 void
326 TTeamMenuItem::ToggleExpandState(bool resizeWindow)
327 {
328 	fExpanded = !fExpanded;
329 	fArrowDirection = fExpanded ? BControlLook::B_DOWN_ARROW
330 		: BControlLook::B_RIGHT_ARROW;
331 
332 	if (fExpanded) {
333 		// Populate Menu() with the stuff from SubMenu().
334 		TWindowMenu* sub = (static_cast<TWindowMenu*>(Submenu()));
335 		if (sub != NULL) {
336 			// force the menu to update it's contents.
337 			bool locked = sub->LockLooper();
338 				// if locking the looper failed, the menu is just not visible
339 			sub->AttachedToWindow();
340 			if (locked)
341 				sub->UnlockLooper();
342 
343 			if (sub->CountItems() > 1) {
344 				TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
345 				int myindex = parent->IndexOf(this) + 1;
346 
347 				TWindowMenuItem* windowItem = NULL;
348 				int32 childIndex = 0;
349 				int32 totalChildren = sub->CountItems() - 4;
350 					// hide, show, close, separator.
351 				for (; childIndex < totalChildren; childIndex++) {
352 					windowItem = static_cast<TWindowMenuItem*>
353 						(sub->RemoveItem((int32)0));
354 					parent->AddItem(windowItem, myindex + childIndex);
355 					windowItem->SetExpanded(true);
356 				}
357 				sub->SetExpanded(true, myindex + childIndex);
358 
359 				if (resizeWindow)
360 					parent->SizeWindow(-1);
361 			}
362 		}
363 	} else {
364 		// Remove the goodies from the Menu() that should be in the SubMenu();
365 		TWindowMenu* sub = static_cast<TWindowMenu*>(Submenu());
366 		if (sub != NULL) {
367 			TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
368 
369 			TWindowMenuItem* windowItem = NULL;
370 			int32 childIndex = parent->IndexOf(this) + 1;
371 			while (parent->SubmenuAt(childIndex) == NULL
372 				&& childIndex < parent->CountItems()) {
373 				windowItem
374 					= static_cast<TWindowMenuItem*>(parent->RemoveItem(childIndex));
375 				sub->AddItem(windowItem, 0);
376 				windowItem->SetExpanded(false);
377 			}
378 			sub->SetExpanded(false, 0);
379 
380 			if (resizeWindow)
381 				parent->SizeWindow(1);
382 		}
383 	}
384 }
385 
386 
387 TWindowMenuItem*
388 TTeamMenuItem::ExpandedWindowItem(int32 id)
389 {
390 	if (!fExpanded) {
391 		// Paranoia
392 		return NULL;
393 	}
394 
395 	TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
396 	int childIndex = parent->IndexOf(this) + 1;
397 
398 	while (!parent->SubmenuAt(childIndex)
399 		&& childIndex < parent->CountItems()) {
400 		TWindowMenuItem* item
401 			= static_cast<TWindowMenuItem*>(parent->ItemAt(childIndex));
402 		if (item->ID() == id)
403 			return item;
404 
405 		childIndex++;
406 	}
407 	return NULL;
408 }
409 
410 
411 BRect
412 TTeamMenuItem::ExpanderBounds() const
413 {
414 	BRect bounds(Frame());
415 	bounds.left = bounds.right - kSwitchWidth;
416 	return bounds;
417 }
418 
419 
420 //	#pragma mark - Private methods
421 
422 
423 void
424 TTeamMenuItem::_Init(BList* team, BBitmap* icon, char* name, char* signature,
425 	float width, float height)
426 {
427 	fTeam = team;
428 	fIcon = icon;
429 	fSignature = signature;
430 
431 	if (name == NULL) {
432 		char temp[32];
433 		snprintf(temp, sizeof(temp), "team %ld", (addr_t)team->ItemAt(0));
434 		name = strdup(temp);
435 	}
436 
437 	SetLabel(name);
438 
439 	fOverrideWidth = width;
440 	fOverrideHeight = height;
441 
442 	fBarView = static_cast<TBarApp*>(be_app)->BarView();
443 
444 	BFont font(be_plain_font);
445 	fLabelWidth = ceilf(font.StringWidth(name));
446 	font_height fontHeight;
447 	font.GetHeight(&fontHeight);
448 	fLabelAscent = ceilf(fontHeight.ascent);
449 	fLabelDescent = ceilf(fontHeight.descent + fontHeight.leading);
450 
451 	fOverriddenSelected = false;
452 
453 	fExpanded = false;
454 	fArrowDirection = BControlLook::B_RIGHT_ARROW;
455 }
456 
457 
458 bool
459 TTeamMenuItem::_IsSelected() const
460 {
461 	return IsSelected() || fOverriddenSelected;
462 }
463