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