xref: /haiku/src/apps/deskbar/WindowMenu.cpp (revision 2f470aec1c92ce6917b8a903e343795dc77af41f)
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 	RemoveItems(0, CountItems(), true);
85 
86 	int32 miniCount = 0;
87 
88 	bool dragging = false;
89 	TBarView *barview =(static_cast<TBarApp *>(be_app))->BarView();
90 	if (barview && 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->Expando()) {
102 				SetTrackingHook(barview->MenuTrackingHook,
103 					barview->GetTrackingHookData());
104 			}
105 			barview->DragStart();
106 		}
107 		barview->UnlockLooper();
108 	}
109 
110 	int32 parentMenuItems = 0;
111 
112 	int32 numTeams = fTeam->CountItems();
113 	for (int32 i = 0; i < numTeams; i++) {
114 		team_id	theTeam = (team_id)fTeam->ItemAt(i);
115 		int32 count = 0;
116 		int32 *tokens = get_token_list(theTeam, &count);
117 
118 		for (int32 j = 0; j < count; j++) {
119 			window_info *wInfo = get_window_info(tokens[j]);
120 			if (wInfo == NULL)
121 				continue;
122 
123 			if (WindowShouldBeListed(wInfo->w_type)
124 				&& (wInfo->show_hide_level <= 0 || wInfo->is_mini)) {
125 				// Don't add new items if we're expanded. We've already done this,
126 				// they've just been moved.
127 				int32 numItems = CountItems();
128 				int32 addIndex = 0;
129 				for (; addIndex < numItems; addIndex++)
130 					if (strcasecmp(ItemAt(addIndex)->Label(), wInfo->name) > 0)
131 						break;
132 
133 				if (!fExpanded) {
134 					TWindowMenuItem *item = new TWindowMenuItem(wInfo->name, wInfo->id,
135 						wInfo->is_mini, ((1 << current_workspace()) & wInfo->workspaces) != 0,
136 						dragging);
137 
138 					// disable app's window dropping for now
139 					if (dragging)
140 						item->SetEnabled(false);
141 
142 					AddItem(item, addIndex);
143 				} else {
144 					TTeamMenuItem *parentItem = static_cast<TTeamMenuItem *>(Superitem());
145 					if (parentItem->ExpandedWindowItem(wInfo->id)) {
146 						TWindowMenuItem *item = parentItem->ExpandedWindowItem(wInfo->id);
147 						if (item == NULL)
148 							continue;
149 
150 						item->SetTo(wInfo->name, wInfo->id, wInfo->is_mini,
151 							((1 << current_workspace()) & wInfo->workspaces) != 0, dragging);
152 						parentMenuItems++;
153 					}
154 				}
155 
156 				if (wInfo->is_mini)
157 					miniCount++;
158 			}
159 			free(wInfo);
160 		}
161 		free(tokens);
162 	}
163 
164 	int32 itemCount = CountItems() + parentMenuItems;
165 	if (itemCount < 1) {
166 		TWindowMenuItem *noWindowsItem =
167  			new TWindowMenuItem("No Windows", -1, false, false);
168 
169 		noWindowsItem->SetEnabled(false);
170 
171 		AddItem(noWindowsItem);
172 
173 		// if an application has no windows, this feature makes it easy to quit it.
174  		// (but we only add this option if the application is not Tracker.)
175  		if (fApplicationSignature.ICompare(kTrackerSignature) != 0) {
176 			AddSeparatorItem();
177 			AddItem(new TShowHideMenuItem("Quit Application", fTeam, B_QUIT_REQUESTED));
178  		}
179 	} else {
180 		//	if we are in drag mode, then don't add the window controls
181 		//	to the menu
182 		if (!dragging) {
183 			TShowHideMenuItem *hide =
184 				new TShowHideMenuItem("Hide All", fTeam, B_MINIMIZE_WINDOW);
185 			TShowHideMenuItem *show =
186 				new TShowHideMenuItem("Show All", fTeam, B_BRING_TO_FRONT);
187 			TShowHideMenuItem* close =
188 				new TShowHideMenuItem("Close All", fTeam, B_QUIT_REQUESTED);
189 
190 			if (miniCount == itemCount)
191 				hide->SetEnabled(false);
192 			else if (miniCount == 0)
193 				show->SetEnabled(false);
194 
195 			if (!parentMenuItems)
196 				AddSeparatorItem();
197 			AddItem(hide);
198 			AddItem(show);
199 			AddItem(close);
200 		}
201 	}
202 
203 	BMenu::AttachedToWindow();
204 }
205 
206 
207 void
208 TWindowMenu::DetachedFromWindow()
209 {
210 	// in expando mode the teammenu will not call DragStop,
211 	// thus, it needs to be called from here
212 	TBarView *barview = (dynamic_cast<TBarApp*>(be_app))->BarView();
213 	if (barview && barview->Expando() && barview->Dragging() && barview->LockLooper()) {
214 		// We changed the show level in AttachedToWindow().  Undo it.
215 		Window()->Show();
216 		barview->DragStop();
217 		barview->UnlockLooper();
218 	}
219 
220 	BMenu::DetachedFromWindow();
221 }
222 
223 
224 BPoint
225 TWindowMenu::ScreenLocation()
226 {
227 	return BMenu::ScreenLocation();
228 }
229 
230 
231 void
232 TWindowMenu::SetExpanded(bool status, int lastIndex)
233 {
234 	fExpanded = status;
235 	fExpandedIndex = lastIndex;
236 }
237 
238