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