xref: /haiku/src/apps/deskbar/TeamMenu.cpp (revision 4a55cc230cf7566cadcbb23b1928eefff8aea9a2)
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 "TeamMenu.h"
38 
39 #include <algorithm>
40 #include <strings.h>
41 
42 #include <Application.h>
43 #include <Collator.h>
44 #include <ControlLook.h>
45 #include <Debug.h>
46 #include <Mime.h>
47 #include <Roster.h>
48 
49 #include "BarApp.h"
50 #include "BarMenuBar.h"
51 #include "BarView.h"
52 #include "DeskbarUtils.h"
53 #include "StatusView.h"
54 #include "TeamMenuItem.h"
55 
56 
57 //	#pragma mark - TTeamMenuItem
58 
59 
60 TTeamMenu::TTeamMenu(TBarView* barView)
61 	:
62 	BMenu("Team Menu"),
63 	fBarView(barView)
64 {
65 	SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
66 	SetFont(be_plain_font);
67 }
68 
69 
70 int
71 TTeamMenu::CompareByName(const void* first, const void* second)
72 {
73 	BCollator collator;
74 	BLocale::Default()->GetCollator(&collator);
75 
76 	return collator.Compare(
77 		(*(static_cast<BarTeamInfo* const*>(first)))->name,
78 		(*(static_cast<BarTeamInfo* const*>(second)))->name);
79 }
80 
81 
82 void
83 TTeamMenu::AttachedToWindow()
84 {
85 	RemoveItems(0, CountItems(), true);
86 		// remove all items
87 
88 	BMessenger self(this);
89 	BList teamList;
90 	TBarApp::Subscribe(self, &teamList);
91 
92 	bool dragging = fBarView != NULL && fBarView->Dragging();
93 	desk_settings* settings = static_cast<TBarApp*>(be_app)->Settings();
94 	int32 iconSize = static_cast<TBarApp*>(be_app)->IconSize();
95 	float iconOnlyWidth = iconSize + be_control_look->ComposeSpacing(kIconPadding);
96 
97 	// calculate the minimum item width based on font and icon size
98 	float minItemWidth = 0;
99 	if (settings->hideLabels) {
100 		minItemWidth = std::max(floorf(gMinimumWindowWidth / 2),
101 			iconOnlyWidth);
102 	} else {
103 		float labelWidth = gMinimumWindowWidth - iconOnlyWidth
104 			+ (be_plain_font->Size() - 12) * 4;
105 		if (iconSize <= B_LARGE_ICON) // label wraps after 32x32
106 			labelWidth += iconSize - kMinimumIconSize;
107 		minItemWidth = iconOnlyWidth + labelWidth;
108 	}
109 
110 	float maxItemWidth = minItemWidth;
111 
112 	int32 itemCount = teamList.CountItems();
113 	if (!settings->hideLabels) {
114 		// go through list and find the widest label
115 		for (int32 i = 0; i < itemCount; i++) {
116 			BarTeamInfo* barInfo = (BarTeamInfo*)teamList.ItemAt(i);
117 			float labelWidth = StringWidth(barInfo->name);
118 			// label wraps after 32x32
119 			float itemWidth = iconSize > B_LARGE_ICON
120 				? std::max(labelWidth, iconOnlyWidth)
121 				: labelWidth + iconOnlyWidth + kMinimumIconSize
122 					+ (be_plain_font->Size() - 12) * 4;
123 			maxItemWidth = std::max(maxItemWidth, itemWidth);
124 		}
125 
126 		// but not too wide
127 		maxItemWidth = std::min(maxItemWidth, gMaximumWindowWidth);
128 	}
129 
130 	SetMaxContentWidth(maxItemWidth);
131 
132 	if (settings->sortRunningApps)
133 		teamList.SortItems(TTeamMenu::CompareByName);
134 
135 	// go through list and add the items
136 	for (int32 i = 0; i < itemCount; i++) {
137 		// add items back
138 		BarTeamInfo* barInfo = (BarTeamInfo*)teamList.ItemAt(i);
139 		TTeamMenuItem* item = new TTeamMenuItem(barInfo->teams,
140 			barInfo->icon, barInfo->name, barInfo->sig, maxItemWidth);
141 
142 		if (settings->trackerAlwaysFirst
143 			&& strcasecmp(barInfo->sig, kTrackerSignature) == 0) {
144 			AddItem(item, 0);
145 		} else
146 			AddItem(item);
147 
148 		if (dragging && item != NULL) {
149 			bool canhandle = fBarView->AppCanHandleTypes(item->Signature());
150 			if (item->IsEnabled() != canhandle)
151 				item->SetEnabled(canhandle);
152 
153 			BMenu* menu = item->Submenu();
154 			if (menu != NULL) {
155 				menu->SetTrackingHook(fBarView->MenuTrackingHook,
156 					fBarView->GetTrackingHookData());
157 			}
158 		}
159 	}
160 
161 	if (CountItems() == 0) {
162 		BMenuItem* item = new BMenuItem("no application running", NULL);
163 		item->SetEnabled(false);
164 		AddItem(item);
165 	}
166 
167 	if (dragging && fBarView->LockLooper()) {
168 		SetTrackingHook(fBarView->MenuTrackingHook,
169 			fBarView->GetTrackingHookData());
170 		fBarView->DragStart();
171 		fBarView->UnlockLooper();
172 	}
173 
174 	BMenu::AttachedToWindow();
175 }
176 
177 
178 void
179 TTeamMenu::DetachedFromWindow()
180 {
181 	if (fBarView != NULL) {
182 		BLooper* looper = fBarView->Looper();
183 		if (looper != NULL && looper->Lock()) {
184 			fBarView->DragStop();
185 			looper->Unlock();
186 		}
187 	}
188 
189 	BMenu::DetachedFromWindow();
190 
191 	BMessenger self(this);
192 	TBarApp::Unsubscribe(self);
193 }
194