xref: /haiku/src/apps/deskbar/WindowMenu.cpp (revision f659f955ba5566abe8403a10a816eeaae14ef97b)
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 teamCount = fTeam->CountItems();
120 	for (int32 i = 0; i < teamCount; i++) {
121 		team_id theTeam = (team_id)fTeam->ItemAt(i);
122 		int32 tokenCount = 0;
123 		int32* tokens = get_token_list(theTeam, &tokenCount);
124 
125 		for (int32 j = 0; j < tokenCount; 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 
136 				// Find first item that sorts alphabetically after this window,
137 				// so we know where to put it
138 				for (int32 addIndex = 0; addIndex < numItems; addIndex++) {
139 					TWindowMenuItem* item
140 						= static_cast<TWindowMenuItem*>(ItemAt(addIndex));
141 					if (item != NULL
142 						&& strcasecmp(item->FullTitle(), wInfo->name) > 0)
143 						break;
144 				}
145 
146 				if (!fExpanded) {
147 					TWindowMenuItem* item = new TWindowMenuItem(wInfo->name,
148 						wInfo->server_token, wInfo->is_mini,
149 						((1 << current_workspace()) & wInfo->workspaces) != 0,
150 						dragging);
151 
152 					// disable app's window dropping for now
153 					if (dragging)
154 						item->SetEnabled(false);
155 
156 					AddItem(item,
157 						TWindowMenuItem::InsertIndexFor(this, 0, item));
158 				} else {
159 					TTeamMenuItem* parentItem
160 						= static_cast<TTeamMenuItem*>(Superitem());
161 					if (parentItem->ExpandedWindowItem(wInfo->server_token)) {
162 						TWindowMenuItem* item = parentItem->ExpandedWindowItem(
163 							wInfo->server_token);
164 						if (item == NULL)
165 							continue;
166 
167 						item->SetTo(wInfo->name, wInfo->server_token,
168 							wInfo->is_mini,
169 							((1 << current_workspace()) & wInfo->workspaces)
170 								!= 0, dragging);
171 						parentMenuItems++;
172 					}
173 				}
174 
175 				if (wInfo->is_mini)
176 					miniCount++;
177 			}
178 			free(wInfo);
179 		}
180 		free(tokens);
181 	}
182 
183 	int32 itemCount = CountItems() + parentMenuItems;
184 	if (itemCount < 1) {
185 		TWindowMenuItem* noWindowsItem
186 			= new TWindowMenuItem(B_TRANSLATE("No windows"), -1, false, false);
187 
188 		noWindowsItem->SetEnabled(false);
189 		AddItem(noWindowsItem);
190 
191 		// Add a 'Quit application' item if no windows are open
192 		// unless the application is Tracker
193 		if (fApplicationSignature.ICompare(kTrackerSignature) != 0) {
194 			AddSeparatorItem();
195 			AddItem(new TShowHideMenuItem(B_TRANSLATE("Quit application"),
196 				fTeam, B_QUIT_REQUESTED));
197 		}
198 	} else {
199 		// Only add the window controls to the menu if we are not in drag mode
200 		if (!dragging) {
201 			TShowHideMenuItem* hide
202 				= new TShowHideMenuItem(B_TRANSLATE("Hide all"), fTeam,
203 					B_MINIMIZE_WINDOW);
204 			TShowHideMenuItem* show
205 				= new TShowHideMenuItem(B_TRANSLATE("Show all"), fTeam,
206 					B_BRING_TO_FRONT);
207 			TShowHideMenuItem* close
208 				= new TShowHideMenuItem(B_TRANSLATE("Close all"), fTeam,
209 					B_QUIT_REQUESTED);
210 
211 			if (miniCount == itemCount)
212 				hide->SetEnabled(false);
213 			else if (miniCount == 0)
214 				show->SetEnabled(false);
215 
216 			if (!parentMenuItems)
217 				AddSeparatorItem();
218 
219 			AddItem(hide);
220 			AddItem(show);
221 			AddItem(close);
222 		}
223 	}
224 
225 	BMenu::AttachedToWindow();
226 }
227 
228 
229 void
230 TWindowMenu::DetachedFromWindow()
231 {
232 	// in expando mode the teammenu will not call DragStop, thus, it needs to
233 	// be called from here
234 	TBarView* barview = (dynamic_cast<TBarApp*>(be_app))->BarView();
235 	if (barview && barview->Expando() && barview->Dragging()
236 		&& barview->LockLooper()) {
237 		// We changed the show level in AttachedToWindow(). Undo it.
238 		Window()->Show();
239 		barview->DragStop();
240 		barview->UnlockLooper();
241 	}
242 
243 	BMenu::DetachedFromWindow();
244 }
245 
246 
247 BPoint
248 TWindowMenu::ScreenLocation()
249 {
250 	return BMenu::ScreenLocation();
251 }
252 
253 
254 void
255 TWindowMenu::SetExpanded(bool status, int lastIndex)
256 {
257 	fExpanded = status;
258 	fExpandedIndex = lastIndex;
259 }
260 
261