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