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