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