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 <string.h>
40
41#include <Application.h>
42#include <Debug.h>
43#include <Roster.h>
44
45#include "BarApp.h"
46#include "BarMenuBar.h"
47#include "DeskbarUtils.h"
48#include "TeamMenuItem.h"
49
50
51TTeamMenu::TTeamMenu()
52	: BMenu("Team Menu")
53{
54	SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
55	SetFont(be_plain_font);
56}
57
58
59int
60TTeamMenu::CompareByName(const void* first, const void* second)
61{
62	return strcasecmp((*(static_cast<BarTeamInfo* const*>(first)))->name,
63		(*(static_cast<BarTeamInfo* const*>(second)))->name);
64}
65
66
67void
68TTeamMenu::AttachedToWindow()
69{
70	RemoveItems(0, CountItems(), true);
71
72	BMessenger self(this);
73	BList teamList;
74	TBarApp::Subscribe(self, &teamList);
75
76	TBarView* barview = (dynamic_cast<TBarApp*>(be_app))->BarView();
77	bool dragging = barview && barview->Dragging();
78
79	desk_settings* settings = ((TBarApp*)be_app)->Settings();
80
81	if (settings->sortRunningApps)
82		teamList.SortItems(CompareByName);
83
84	int32 count = teamList.CountItems();
85	for (int32 i = 0; i < count; i++) {
86		BarTeamInfo* barInfo = (BarTeamInfo*)teamList.ItemAt(i);
87
88		if (((barInfo->flags & B_BACKGROUND_APP) == 0)
89			&& (strcasecmp(barInfo->sig, kDeskbarSignature) != 0)) {
90			TTeamMenuItem* item = new TTeamMenuItem(barInfo->teams,
91				barInfo->icon, barInfo->name, barInfo->sig, -1, -1,
92				!settings->hideLabels, true);
93
94			if ((settings->trackerAlwaysFirst)
95				&& (strcmp(barInfo->sig, kTrackerSignature) == 0))
96				AddItem(item, 0);
97			else
98				AddItem(item);
99
100			if (dragging && item) {
101				bool canhandle = (dynamic_cast<TBarApp*>(be_app))->BarView()->
102					AppCanHandleTypes(item->Signature());
103				if (item->IsEnabled() != canhandle)
104					item->SetEnabled(canhandle);
105
106				BMenu* menu = item->Submenu();
107				if (menu)
108					menu->SetTrackingHook(barview->MenuTrackingHook,
109						barview->GetTrackingHookData());
110			}
111
112			barInfo->teams = NULL;
113			barInfo->icon = NULL;
114			barInfo->name = NULL;
115			barInfo->sig = NULL;
116		}
117		delete barInfo;
118	}
119
120	if (CountItems() == 0) {
121		BMenuItem* item = new BMenuItem("no application running", NULL);
122		item->SetEnabled(false);
123		AddItem(item);
124	}
125
126	if (dragging && barview->LockLooper()) {
127		SetTrackingHook(barview->MenuTrackingHook,
128			barview->GetTrackingHookData());
129		barview->DragStart();
130		barview->UnlockLooper();
131	}
132
133	BMenu::AttachedToWindow();
134}
135
136
137void
138TTeamMenu::DetachedFromWindow()
139{
140	TBarView* barView = (dynamic_cast<TBarApp*>(be_app))->BarView();
141	if (barView) {
142		BLooper* looper = barView->Looper();
143		if (looper->Lock()) {
144			barView->DragStop();
145			looper->Unlock();
146		}
147	}
148
149	BMenu::DetachedFromWindow();
150
151	BMessenger self(this);
152	TBarApp::Unsubscribe(self);
153}
154
155
156void
157TTeamMenu::DrawBackground(BRect)
158{
159}
160