btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / deskbar / TeamMenuItem.cpp
blob6fb53218a39be4feee0b0b5114e8ab82a0e92e49
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
37 #include "TeamMenuItem.h"
39 #include <string.h>
40 #include <stdio.h>
41 #include <stdlib.h>
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>
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"
62 const float kHPad = 8.0f;
63 const float kVPad = 2.0f;
64 const float kLabelOffset = 8.0f;
65 const float kSwitchWidth = 12.0f;
68 // #pragma mark - TTeamMenuItem
71 TTeamMenuItem::TTeamMenuItem(BList* team, BBitmap* icon, char* name,
72 char* signature, float width, float height)
74 TTruncatableMenuItem(new TWindowMenu(team, signature))
76 _Init(team, icon, name, signature, width, height);
80 TTeamMenuItem::TTeamMenuItem(float width, float height)
82 TTruncatableMenuItem("", NULL)
84 _Init(NULL, NULL, strdup(""), strdup(""), width, height);
88 TTeamMenuItem::~TTeamMenuItem()
90 delete fTeam;
91 delete fIcon;
92 free(fSignature);
96 status_t
97 TTeamMenuItem::Invoke(BMessage* message)
99 if (fBarView->InvokeItem(Signature())) {
100 // handles drop on application
101 return B_OK;
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();
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());
117 return BMenuItem::Invoke(message);
121 void
122 TTeamMenuItem::SetOverrideSelected(bool selected)
124 fOverriddenSelected = selected;
125 Highlight(selected);
129 void
130 TTeamMenuItem::SetIcon(BBitmap* icon) {
131 delete fIcon;
132 fIcon = icon;
136 void
137 TTeamMenuItem::GetContentSize(float* width, float* height)
139 BRect iconBounds;
141 if (fIcon != NULL)
142 iconBounds = fIcon->Bounds();
143 else
144 iconBounds = BRect(0, 0, kMinimumIconSize - 1, kMinimumIconSize - 1);
146 BMenuItem::GetContentSize(width, height);
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;
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;
167 } else {
168 *height = iconBounds.Height() + kVPad * 2;
171 *height += 2;
175 void
176 TTeamMenuItem::Draw()
178 BRect frame = Frame();
179 BMenu* menu = Menu();
181 menu->PushState();
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;
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--;
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++;
209 be_control_look->DrawButtonBackground(menu, frame, frame,
210 menuColor, flags, borders);
213 menu->MovePenTo(ContentLocation());
214 DrawContent();
216 menu->PopState();
220 void
221 TTeamMenuItem::DrawContent()
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);
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);
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;
244 updateRect.OffsetTo(BPoint(offsetx, offsety));
245 menu->DrawBitmapAsync(fIcon, updateRect);
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;
254 updateRect.OffsetTo(BPoint(offsetx, offsety));
255 menu->DrawBitmapAsync(fIcon, updateRect);
257 float labelHeight = fLabelAscent + fLabelDescent;
258 drawLocation.x += iconBounds.Width() + kLabelOffset;
259 drawLocation.y = frame.top + ((frame.Height() - labelHeight) / 2)
260 + extra;
263 menu->MovePenTo(drawLocation);
266 menu->SetDrawingMode(B_OP_OVER);
267 menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
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);
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));
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));
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));
294 if (fBarView->Vertical()
295 && static_cast<TBarApp*>(be_app)->Settings()->superExpando
296 && fBarView->ExpandoState()) {
297 DrawExpanderArrow();
302 void
303 TTeamMenuItem::DrawExpanderArrow()
305 BMenu* menu = Menu();
306 BRect frame = Frame();
307 BRect rect(0.0f, 0.0f, kSwitchWidth, kHPad + 2.0f);
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);
317 void
318 TTeamMenuItem::ToggleExpandState(bool resizeWindow)
320 fExpanded = !fExpanded;
321 fArrowDirection = fExpanded ? BControlLook::B_DOWN_ARROW
322 : BControlLook::B_RIGHT_ARROW;
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();
335 if (sub->CountItems() > 1) {
336 TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
337 int myindex = parent->IndexOf(this) + 1;
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);
349 sub->SetExpanded(true, myindex + childIndex);
351 if (resizeWindow)
352 parent->SizeWindow(-1);
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());
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);
370 sub->SetExpanded(false, 0);
372 if (resizeWindow)
373 parent->SizeWindow(1);
379 TWindowMenuItem*
380 TTeamMenuItem::ExpandedWindowItem(int32 id)
382 if (!fExpanded) {
383 // Paranoia
384 return NULL;
387 TExpandoMenuBar* parent = static_cast<TExpandoMenuBar*>(Menu());
388 int childIndex = parent->IndexOf(this) + 1;
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;
397 childIndex++;
399 return NULL;
403 BRect
404 TTeamMenuItem::ExpanderBounds() const
406 BRect bounds(Frame());
407 bounds.left = bounds.right - kSwitchWidth;
408 return bounds;
412 // #pragma mark - Private methods
415 void
416 TTeamMenuItem::_Init(BList* team, BBitmap* icon, char* name, char* signature,
417 float width, float height)
419 fTeam = team;
420 fIcon = icon;
421 fSignature = signature;
423 if (name == NULL) {
424 char temp[32];
425 snprintf(temp, sizeof(temp), "team %ld", (addr_t)team->ItemAt(0));
426 name = strdup(temp);
429 SetLabel(name);
431 fOverrideWidth = width;
432 fOverrideHeight = height;
434 fBarView = static_cast<TBarApp*>(be_app)->BarView();
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);
443 fOverriddenSelected = false;
445 fExpanded = false;
446 fArrowDirection = BControlLook::B_RIGHT_ARROW;
450 bool
451 TTeamMenuItem::_IsSelected() const
453 return IsSelected() || fOverriddenSelected;