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