xref: /haiku/src/apps/deskbar/BarMenuBar.cpp (revision 893988af824e65e49e55f517b157db8386e8002b)
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 <Debug.h>
36 
37 #include "BarMenuBar.h"
38 
39 #include <string.h>
40 
41 #include <Bitmap.h>
42 #include <NodeInfo.h>
43 
44 #include "icons.h"
45 #include "icons_logo.h"
46 #include "BarWindow.h"
47 #include "BeMenu.h"
48 #include "DeskBarUtils.h"
49 #include "ResourceSet.h"
50 #include "TeamMenu.h"
51 
52 
53 TBarMenuBar::TBarMenuBar(TBarView *bar, BRect frame, const char *name)
54 	: BMenuBar(frame, name, B_FOLLOW_NONE, B_ITEMS_IN_ROW, false),
55 	fBarView(bar),
56 	fAppListMenuItem(NULL)
57 {
58 	SetItemMargins(0.0f, 0.0f, 0.0f, 0.0f);
59 
60 	TBeMenu *beMenu = new TBeMenu(bar);
61 	TBarWindow::SetBeMenu(beMenu);
62 
63 	fBeMenuItem = new TBarMenuTitle(frame.Width(), frame.Height(),
64 		AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_BeLogoIcon), beMenu);
65 	AddItem(fBeMenuItem);
66 }
67 
68 
69 TBarMenuBar::~TBarMenuBar()
70 {
71 }
72 
73 
74 void
75 TBarMenuBar::SmartResize(float width, float height)
76 {
77 	if (width == -1.0f && height == -1.0f) {
78 		BRect frame = Frame();
79 		width = frame.Width();
80 		height = frame.Height();
81 	} else
82 		ResizeTo(width, height);
83 
84 	width -= 1;
85 
86 	int32 count = CountItems();
87 	if (fBeMenuItem)
88 		fBeMenuItem->SetWidthHeight(width / count, height);
89 	if (fAppListMenuItem)
90 		fAppListMenuItem->SetWidthHeight(width / count, height);
91 
92 	InvalidateLayout();
93 }
94 
95 
96 void
97 TBarMenuBar::AddTeamMenu()
98 {
99 	if (CountItems() > 1)
100 		return;
101 
102 	BRect frame(Frame());
103 	delete fAppListMenuItem;
104 
105 	fAppListMenuItem = new TBarMenuTitle(0.0f, 0.0f,
106 		AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_TeamIcon), new TTeamMenu());
107 	AddItem(fAppListMenuItem);
108 	SmartResize(frame.Width() - 1.0f, frame.Height());
109 }
110 
111 
112 void
113 TBarMenuBar::RemoveTeamMenu()
114 {
115 	if (CountItems() < 2)
116 		return;
117 
118 	if (fAppListMenuItem) {
119 		RemoveItem((BMenuItem *)fAppListMenuItem);
120 		delete fAppListMenuItem;
121 		fAppListMenuItem = NULL;
122 	}
123 
124 	BRect frame = Frame();
125 	SmartResize(frame.Width(), frame.Height());
126 }
127 
128 
129 void
130 TBarMenuBar::Draw(BRect rect)
131 {
132 	// want to skip the fancy BMenuBar drawing code.
133 	BMenu::Draw(rect);
134 }
135 
136 
137 void
138 TBarMenuBar::DrawBackground(BRect rect)
139 {
140 	BMenu::DrawBackground(rect);
141 }
142 
143 
144 void
145 TBarMenuBar::MouseMoved(BPoint where, uint32 code, const BMessage *message)
146 {
147 	// the following code parallels that in ExpandoMenuBar for DnD tracking
148 
149 	if (!message) {
150 		// force a cleanup
151 		fBarView->DragStop(true);
152 		BMenuBar::MouseMoved(where, code, message);
153 		return;
154 	}
155 
156 	switch (code) {
157 		case B_ENTERED_VIEW:
158 		{
159 			BPoint loc;
160 			uint32 buttons;
161 			GetMouse(&loc, &buttons);
162 			// attempt to start DnD tracking
163 			if (message && buttons != 0) {
164 				fBarView->CacheDragData(const_cast<BMessage *>(message));
165 				MouseDown(loc);
166 			}
167 			break;
168 		}
169 	}
170 	BMenuBar::MouseMoved(where, code, message);
171 }
172 
173 
174 static void
175 init_tracking_hook(BMenuItem *item, bool (*hookFunction)(BMenu *, void *),
176 	void *state)
177 {
178 	if (!item)
179 		return;
180 
181 	BMenu *windowMenu = item->Submenu();
182 	if (windowMenu)
183 		//	have a menu, set the tracking hook
184 		windowMenu->SetTrackingHook(hookFunction, state);
185 }
186 
187 
188 void
189 TBarMenuBar::InitTrackingHook(bool (*hookFunction)(BMenu *, void *),
190 	void *state, bool both)
191 {
192 	BPoint loc;
193 	uint32 buttons;
194 	GetMouse(&loc, &buttons);
195 	//	set the hook functions for the two menus
196 	//	will always have the be menu
197 	//	may have the app menu as well (mini mode)
198 	if (fBeMenuItem->Frame().Contains(loc) || both)
199 		init_tracking_hook(fBeMenuItem, hookFunction, state);
200 
201 	if (fAppListMenuItem && (fAppListMenuItem->Frame().Contains(loc) || both))
202 		init_tracking_hook(fAppListMenuItem, hookFunction, state);
203 }
204