xref: /haiku/src/apps/deskbar/TeamMenuItem.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
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 	be_control_look->DrawArrowShape(menu, rect, rect,
312 		ui_color(B_MENU_BACKGROUND_COLOR), fArrowDirection, 0,
313 		B_DARKEN_3_TINT);
314 }
315 
316 
317 void
318 TTeamMenuItem::ToggleExpandState(bool resizeWindow)
319 {
320 	fExpanded = !fExpanded;
321 	fArrowDirection = fExpanded ? BControlLook::B_DOWN_ARROW
322 		: BControlLook::B_RIGHT_ARROW;
323 
324 	if (fExpanded) {
325 		// Populate Menu() with the stuff from SubMenu().
326 		TWindowMenu* sub = (static_cast<TWindowMenu*>(Submenu()));
327 		if (sub != NULL) {
328 			// force the menu to update it's contents.
329 			bool locked = sub->LockLooper();
330 				// if locking the looper failed, the menu is just not visible
331 			sub->AttachedToWindow();
332 			if (locked)
333 				sub->UnlockLooper();
334 
335 			if (sub->CountItems() > 1) {
336 				TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
337 				int myindex = parent->IndexOf(this) + 1;
338 
339 				TWindowMenuItem* windowItem = NULL;
340 				int32 childIndex = 0;
341 				int32 totalChildren = sub->CountItems() - 4;
342 					// hide, show, close, separator.
343 				for (; childIndex < totalChildren; childIndex++) {
344 					windowItem = static_cast<TWindowMenuItem*>
345 						(sub->RemoveItem((int32)0));
346 					parent->AddItem(windowItem, myindex + childIndex);
347 					windowItem->SetExpanded(true);
348 				}
349 				sub->SetExpanded(true, myindex + childIndex);
350 
351 				if (resizeWindow)
352 					parent->SizeWindow(-1);
353 			}
354 		}
355 	} else {
356 		// Remove the goodies from the Menu() that should be in the SubMenu();
357 		TWindowMenu* sub = static_cast<TWindowMenu*>(Submenu());
358 		if (sub != NULL) {
359 			TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
360 
361 			TWindowMenuItem* windowItem = NULL;
362 			int32 childIndex = parent->IndexOf(this) + 1;
363 			while (parent->SubmenuAt(childIndex) == NULL
364 				&& childIndex < parent->CountItems()) {
365 				windowItem
366 					= static_cast<TWindowMenuItem*>(parent->RemoveItem(childIndex));
367 				sub->AddItem(windowItem, 0);
368 				windowItem->SetExpanded(false);
369 			}
370 			sub->SetExpanded(false, 0);
371 
372 			if (resizeWindow)
373 				parent->SizeWindow(1);
374 		}
375 	}
376 }
377 
378 
379 TWindowMenuItem*
380 TTeamMenuItem::ExpandedWindowItem(int32 id)
381 {
382 	if (!fExpanded) {
383 		// Paranoia
384 		return NULL;
385 	}
386 
387 	TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
388 	int childIndex = parent->IndexOf(this) + 1;
389 
390 	while (!parent->SubmenuAt(childIndex)
391 		&& childIndex < parent->CountItems()) {
392 		TWindowMenuItem* item
393 			= static_cast<TWindowMenuItem*>(parent->ItemAt(childIndex));
394 		if (item->ID() == id)
395 			return item;
396 
397 		childIndex++;
398 	}
399 	return NULL;
400 }
401 
402 
403 BRect
404 TTeamMenuItem::ExpanderBounds() const
405 {
406 	BRect bounds(Frame());
407 	bounds.left = bounds.right - kSwitchWidth;
408 	return bounds;
409 }
410 
411 
412 //	#pragma mark - Private methods
413 
414 
415 void
416 TTeamMenuItem::_Init(BList* team, BBitmap* icon, char* name, char* signature,
417 	float width, float height)
418 {
419 	fTeam = team;
420 	fIcon = icon;
421 	fSignature = signature;
422 
423 	if (name == NULL) {
424 		char temp[32];
425 		snprintf(temp, sizeof(temp), "team %ld", (addr_t)team->ItemAt(0));
426 		name = strdup(temp);
427 	}
428 
429 	SetLabel(name);
430 
431 	fOverrideWidth = width;
432 	fOverrideHeight = height;
433 
434 	fBarView = static_cast<TBarApp*>(be_app)->BarView();
435 
436 	BFont font(be_plain_font);
437 	fLabelWidth = ceilf(font.StringWidth(name));
438 	font_height fontHeight;
439 	font.GetHeight(&fontHeight);
440 	fLabelAscent = ceilf(fontHeight.ascent);
441 	fLabelDescent = ceilf(fontHeight.descent + fontHeight.leading);
442 
443 	fOverriddenSelected = false;
444 
445 	fExpanded = false;
446 	fArrowDirection = BControlLook::B_RIGHT_ARROW;
447 }
448 
449 
450 bool
451 TTeamMenuItem::_IsSelected() const
452 {
453 	return IsSelected() || fOverriddenSelected;
454 }
455