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