xref: /haiku/src/apps/deskbar/WindowMenu.cpp (revision 4466b89c65970de4c7236ac87faa2bee4589f413)
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 	for (int32 i = 0; i < fTeam->CountItems(); i++) {
120 		team_id theTeam = (team_id)fTeam->ItemAt(i);
121 		int32 tokenCount = 0;
122 		int32* tokens = get_token_list(theTeam, &tokenCount);
123 
124 		for (int32 j = 0; j < tokenCount; j++) {
125 			client_window_info* wInfo = get_window_info(tokens[j]);
126 			if (wInfo == NULL)
127 				continue;
128 
129 			if (WindowShouldBeListed(wInfo->feel)
130 				&& (wInfo->show_hide_level <= 0 || wInfo->is_mini)) {
131 				// Don't add new items if we're expanded. We've already done
132 				// this, they've just been moved.
133 				for (int32 addIndex = 0; addIndex < CountItems(); addIndex++) {
134 					TWindowMenuItem* item
135 						= static_cast<TWindowMenuItem*>(ItemAt(addIndex));
136 					if (item != NULL
137 						&& strcasecmp(item->FullTitle(), wInfo->name) > 0)
138 						break;
139 				}
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 		AddItem(noWindowsItem);
185 
186 		// Add a 'Quit application' item if no windows are open
187 		// unless the application is 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 		// Only add the window controls to the menu if we are not in drag mode
195 		if (!dragging) {
196 			TShowHideMenuItem* hide
197 				= new TShowHideMenuItem(B_TRANSLATE("Hide all"), fTeam,
198 					B_MINIMIZE_WINDOW);
199 			TShowHideMenuItem* show
200 				= new TShowHideMenuItem(B_TRANSLATE("Show all"), fTeam,
201 					B_BRING_TO_FRONT);
202 			TShowHideMenuItem* close
203 				= new TShowHideMenuItem(B_TRANSLATE("Close all"), fTeam,
204 					B_QUIT_REQUESTED);
205 
206 			if (miniCount == itemCount)
207 				hide->SetEnabled(false);
208 			else if (miniCount == 0)
209 				show->SetEnabled(false);
210 
211 			if (!parentMenuItems)
212 				AddSeparatorItem();
213 
214 			AddItem(hide);
215 			AddItem(show);
216 			AddItem(close);
217 		}
218 	}
219 
220 	BMenu::AttachedToWindow();
221 }
222 
223 
224 void
225 TWindowMenu::DetachedFromWindow()
226 {
227 	// in expando mode the teammenu will not call DragStop, thus, it needs to
228 	// be called from here
229 	TBarView* barview = (dynamic_cast<TBarApp*>(be_app))->BarView();
230 	if (barview && barview->Expando() && barview->Dragging()
231 		&& barview->LockLooper()) {
232 		// We changed the show level in AttachedToWindow(). Undo it.
233 		Window()->Show();
234 		barview->DragStop();
235 		barview->UnlockLooper();
236 	}
237 
238 	BMenu::DetachedFromWindow();
239 }
240 
241 
242 BPoint
243 TWindowMenu::ScreenLocation()
244 {
245 	return BMenu::ScreenLocation();
246 }
247 
248 
249 void
250 TWindowMenu::SetExpanded(bool status, int lastIndex)
251 {
252 	fExpanded = status;
253 	fExpandedIndex = lastIndex;
254 }
255 
256