1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
30trademarks of Be Incorporated in the United States and other countries. Other
31brand product names are registered trademarks or trademarks of their respective
32holders.
33All 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
60TTeamMenu::TTeamMenu(TBarView* barView)
61	:
62	BMenu("Team Menu"),
63	fBarView(barView)
64{
65	SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
66}
67
68
69int
70TTeamMenu::CompareByName(const void* first, const void* second)
71{
72	BCollator collator;
73	BLocale::Default()->GetCollator(&collator);
74
75	return collator.Compare(
76		(*(static_cast<BarTeamInfo* const*>(first)))->name,
77		(*(static_cast<BarTeamInfo* const*>(second)))->name);
78}
79
80
81void
82TTeamMenu::AttachedToWindow()
83{
84	RemoveItems(0, CountItems(), true);
85		// remove all items
86
87	BMessenger self(this);
88	BList teamList;
89	TBarApp::Subscribe(self, &teamList);
90
91	bool dragging = fBarView != NULL && fBarView->Dragging();
92	desk_settings* settings = static_cast<TBarApp*>(be_app)->Settings();
93	const int32 iconSize = static_cast<TBarApp*>(be_app)->TeamIconSize();
94	const float iconPadding = be_control_look->ComposeSpacing(kIconPadding);
95	float iconOnlyWidth = iconSize + iconPadding;
96	if (settings->hideLabels)
97		iconOnlyWidth += iconPadding; // add an extra icon padding
98	const int32 large = be_control_look->ComposeIconSize(B_LARGE_ICON)
99		.IntegerWidth() + 1;
100	const int32 min = be_control_look->ComposeIconSize(kMinimumIconSize)
101		.IntegerWidth() + 1;
102
103	// calculate the minimum item width based on font and icon size
104	float minItemWidth = 0;
105	if (settings->hideLabels) {
106		minItemWidth = std::max(floorf(gMinimumWindowWidth / 2),
107			iconOnlyWidth);
108	} else {
109		float labelWidth = gMinimumWindowWidth - iconOnlyWidth
110			+ (be_plain_font->Size() - 12) * 4;
111		if (iconSize <= large) // label wraps after 32x32
112			labelWidth += iconSize - min;
113		minItemWidth = iconOnlyWidth + labelWidth;
114	}
115
116	float maxItemWidth = minItemWidth;
117
118	int32 teamCount = teamList.CountItems();
119	if (!settings->hideLabels) {
120		// go through list and find the widest label
121		for (int32 i = 0; i < teamCount; i++) {
122			BarTeamInfo* barInfo = (BarTeamInfo*)teamList.ItemAt(i);
123			float labelWidth = StringWidth(barInfo->name);
124			// label wraps after 32x32
125			float itemWidth = iconSize > large
126				? std::max(labelWidth, iconOnlyWidth)
127				: labelWidth + iconOnlyWidth + min
128					+ (be_plain_font->Size() - 12) * 4;
129			maxItemWidth = std::max(maxItemWidth, itemWidth);
130		}
131
132		// but not too wide
133		maxItemWidth = std::min(maxItemWidth, gMaximumWindowWidth);
134	}
135
136	SetMaxContentWidth(maxItemWidth);
137
138	if (settings->sortRunningApps)
139		teamList.SortItems(TTeamMenu::CompareByName);
140
141	// go through list and add the items
142	for (int32 i = 0; i < teamCount; i++) {
143		// add items back
144		BarTeamInfo* barInfo = (BarTeamInfo*)teamList.ItemAt(i);
145		TTeamMenuItem* item = new TTeamMenuItem(barInfo->teams,
146			barInfo->icon, barInfo->name, barInfo->sig, maxItemWidth);
147
148		if (settings->trackerAlwaysFirst
149			&& strcasecmp(barInfo->sig, kTrackerSignature) == 0) {
150			AddItem(item, 0);
151		} else
152			AddItem(item);
153
154		if (dragging && item != NULL) {
155			bool canhandle = fBarView->AppCanHandleTypes(item->Signature());
156			if (item->IsEnabled() != canhandle)
157				item->SetEnabled(canhandle);
158
159			BMenu* menu = item->Submenu();
160			if (menu != NULL) {
161				menu->SetTrackingHook(fBarView->MenuTrackingHook,
162					fBarView->GetTrackingHookData());
163			}
164		}
165	}
166
167	if (CountItems() == 0) {
168		BMenuItem* item = new BMenuItem("no application running", NULL);
169		item->SetEnabled(false);
170		AddItem(item);
171	}
172
173	if (dragging && fBarView->LockLooper()) {
174		SetTrackingHook(fBarView->MenuTrackingHook,
175			fBarView->GetTrackingHookData());
176		fBarView->DragStart();
177		fBarView->UnlockLooper();
178	}
179
180	BMenu::AttachedToWindow();
181}
182
183
184void
185TTeamMenu::DetachedFromWindow()
186{
187	if (fBarView != NULL) {
188		BLooper* looper = fBarView->Looper();
189		if (looper != NULL && looper->Lock()) {
190			fBarView->DragStop();
191			looper->Unlock();
192		}
193	}
194
195	BMenu::DetachedFromWindow();
196
197	BMessenger self(this);
198	TBarApp::Unsubscribe(self);
199}
200
201
202void
203TTeamMenu::MessageReceived(BMessage* message)
204{
205	TTeamMenuItem* item = NULL;
206
207	switch (message->what) {
208		case B_SOME_APP_QUIT:
209		case kRemoveTeam:
210		{
211			int32 itemIndex = -1;
212			message->FindInt32("itemIndex", &itemIndex);
213			team_id team = -1;
214			message->FindInt32("team", &team);
215
216			item = dynamic_cast<TTeamMenuItem*>(ItemAt(itemIndex));
217			if (item != NULL && item->Teams()->HasItem((void*)(addr_t)team)) {
218				item->Teams()->RemoveItem(team);
219				RemoveItem(itemIndex);
220				delete item;
221			}
222			break;
223		}
224
225		default:
226			BMenu::MessageReceived(message);
227			break;
228	}
229}
230
231
232void
233TTeamMenu::MouseDown(BPoint where)
234{
235	if (fBarView == NULL || fBarView->Dragging())
236		return BMenu::MouseDown(where);
237
238	BMenuItem* item = ItemAtPoint(where);
239	if (item == NULL)
240		return BMenu::MouseDown(where);
241
242	TTeamMenuItem* teamItem = dynamic_cast<TTeamMenuItem*>(item);
243	if (teamItem == NULL || !teamItem->HandleMouseDown(where))
244		BMenu::MouseDown(where);
245}
246
247
248BMenuItem*
249TTeamMenu::ItemAtPoint(BPoint point)
250{
251	int32 itemCount = CountItems();
252	for (int32 index = 0; index < itemCount; index++) {
253		BMenuItem* item = ItemAt(index);
254		if (item != NULL && item->Frame().Contains(point))
255			return item;
256	}
257
258	// no item found
259	return NULL;
260}
261