xref: /haiku/src/apps/deskbar/WindowMenu.cpp (revision 0d452c8f34013b611a54c746a71c05e28796eae2)
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 #include <malloc.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <Catalog.h>
40 #include <Locale.h>
41 #include <Window.h>
42 
43 #include "BarApp.h"
44 #include "BarView.h"
45 #include "ExpandoMenuBar.h"
46 #include "ShowHideMenuItem.h"
47 #include "TeamMenu.h"
48 #include "TeamMenuItem.h"
49 #include "WindowMenuItem.h"
50 #include "WindowMenu.h"
51 
52 #include "tracker_private.h"
53 
54 
55 const int32 kDesktopWindow = 1024;
56 const int32 kMenuWindow	= 1025;
57 const uint32 kWindowScreen = 1026;
58 const uint32 kNormalWindow = 0;
59 const int32 kTeamFloater = 4;
60 const int32 kListFloater = 5;
61 const int32 kSystemFloater = 6;
62 
63 
64 #undef B_TRANSLATE_CONTEXT
65 #define B_TRANSLATE_CONTEXT "WindowMenu"
66 
67 bool
68 TWindowMenu::WindowShouldBeListed(uint32 behavior)
69 {
70 	if (behavior == kNormalWindow || behavior == kWindowScreen)
71 		return true;
72 
73 	return false;
74 }
75 
76 
77 TWindowMenu::TWindowMenu(const BList* team, const char* signature)
78 	: BMenu("Deskbar Team Menu"),
79 	fTeam(team),
80 	fApplicationSignature(signature),
81 	fExpanded(false),
82 	fExpandedIndex(0)
83 {
84 	SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
85 }
86 
87 
88 void
89 TWindowMenu::AttachedToWindow()
90 {
91 	SetFont(be_plain_font);
92 	RemoveItems(0, CountItems(), true);
93 	int32 miniCount = 0;
94 
95 	bool dragging = false;
96 	TBarView* barview =(static_cast<TBarApp*>(be_app))->BarView();
97 	if (barview && barview->LockLooper()) {
98 		// 'dragging' mode set in BarView::CacheDragData
99 		// invoke in MouseEnter in ExpandoMenuBar
100 		dragging = barview->Dragging();
101 		if (dragging) {
102 			// We don't want to show the menu when dragging, but it's not
103 			// possible to remove a submenu once it exists, so we simply hide it
104 			// Don't call BMenu::Hide(), it causes the menu to pop up every now
105 			// and then.
106 			Window()->Hide();
107 			// if in expando (horizontal or vertical)
108 			if (barview->Expando()) {
109 				SetTrackingHook(barview->MenuTrackingHook,
110 					barview->GetTrackingHookData());
111 			}
112 			barview->DragStart();
113 		}
114 		barview->UnlockLooper();
115 	}
116 
117 	int32 parentMenuItems = 0;
118 
119 	int32 numTeams = fTeam->CountItems();
120 	for (int32 i = 0; i < numTeams; i++) {
121 		team_id	theTeam = (team_id)fTeam->ItemAt(i);
122 		int32 count = 0;
123 		int32* tokens = get_token_list(theTeam, &count);
124 
125 		for (int32 j = 0; j < count; j++) {
126 			client_window_info* wInfo = get_window_info(tokens[j]);
127 			if (wInfo == NULL)
128 				continue;
129 
130 			if (WindowShouldBeListed(wInfo->feel)
131 				&& (wInfo->show_hide_level <= 0 || wInfo->is_mini)) {
132 				// Don't add new items if we're expanded. We've already done
133 				// this, they've just been moved.
134 				int32 numItems = CountItems();
135 				int32 addIndex = 0;
136 				for (; addIndex < numItems; addIndex++)
137 					if (strcasecmp(ItemAt(addIndex)->Label(), wInfo->name) > 0)
138 						break;
139 
140 				if (!fExpanded) {
141 					TWindowMenuItem* item = new TWindowMenuItem(wInfo->name,
142 						wInfo->server_token, wInfo->is_mini,
143 						((1 << current_workspace()) & wInfo->workspaces) != 0,
144 						dragging);
145 
146 					// disable app's window dropping for now
147 					if (dragging)
148 						item->SetEnabled(false);
149 
150 					AddItem(item,
151 						TWindowMenuItem::InsertIndexFor(this, 0, item));
152 				} else {
153 					TTeamMenuItem* parentItem
154 						= static_cast<TTeamMenuItem*>(Superitem());
155 					if (parentItem->ExpandedWindowItem(wInfo->server_token)) {
156 						TWindowMenuItem* item = parentItem->ExpandedWindowItem(
157 							wInfo->server_token);
158 						if (item == NULL)
159 							continue;
160 
161 						item->SetTo(wInfo->name, wInfo->server_token,
162 							wInfo->is_mini,
163 							((1 << current_workspace()) & wInfo->workspaces)
164 								!= 0, dragging);
165 						parentMenuItems++;
166 					}
167 				}
168 
169 				if (wInfo->is_mini)
170 					miniCount++;
171 			}
172 			free(wInfo);
173 		}
174 		free(tokens);
175 	}
176 
177 	int32 itemCount = CountItems() + parentMenuItems;
178 	if (itemCount < 1) {
179 		TWindowMenuItem* noWindowsItem
180 			= new TWindowMenuItem(B_TRANSLATE("No windows"), -1, false, false);
181 
182 		noWindowsItem->SetEnabled(false);
183 
184 		AddItem(noWindowsItem);
185 
186 		// if an application has no windows, this feature makes it easy to quit
187 		// it. (but we only add this option if the application is not Tracker.)
188 		if (fApplicationSignature.ICompare(kTrackerSignature) != 0) {
189 			AddSeparatorItem();
190 			AddItem(new TShowHideMenuItem(B_TRANSLATE("Quit application"),
191 				fTeam, B_QUIT_REQUESTED));
192 		}
193 	} else {
194 		// if we are in drag mode, then don't add the window controls
195 		// to the menu
196 		if (!dragging) {
197 			TShowHideMenuItem* hide
198 				= new TShowHideMenuItem(B_TRANSLATE("Hide all"), fTeam,
199 					B_MINIMIZE_WINDOW);
200 			TShowHideMenuItem* show
201 				= new TShowHideMenuItem(B_TRANSLATE("Show all"), fTeam,
202 					B_BRING_TO_FRONT);
203 			TShowHideMenuItem* close
204 				= new TShowHideMenuItem(B_TRANSLATE("Close all"), fTeam,
205 					B_QUIT_REQUESTED);
206 
207 			if (miniCount == itemCount)
208 				hide->SetEnabled(false);
209 			else if (miniCount == 0)
210 				show->SetEnabled(false);
211 
212 			if (!parentMenuItems)
213 				AddSeparatorItem();
214 
215 			AddItem(hide);
216 			AddItem(show);
217 			AddItem(close);
218 		}
219 	}
220 
221 	BMenu::AttachedToWindow();
222 }
223 
224 
225 void
226 TWindowMenu::DetachedFromWindow()
227 {
228 	// in expando mode the teammenu will not call DragStop, thus, it needs to
229 	// be called from here
230 	TBarView* barview = (dynamic_cast<TBarApp*>(be_app))->BarView();
231 	if (barview && barview->Expando() && barview->Dragging()
232 		&& barview->LockLooper()) {
233 		// We changed the show level in AttachedToWindow(). Undo it.
234 		Window()->Show();
235 		barview->DragStop();
236 		barview->UnlockLooper();
237 	}
238 
239 	BMenu::DetachedFromWindow();
240 }
241 
242 
243 BPoint
244 TWindowMenu::ScreenLocation()
245 {
246 	return BMenu::ScreenLocation();
247 }
248 
249 
250 void
251 TWindowMenu::SetExpanded(bool status, int lastIndex)
252 {
253 	fExpanded = status;
254 	fExpandedIndex = lastIndex;
255 }
256 
257