xref: /haiku/src/apps/deskbar/WindowMenuItem.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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 "WindowMenuItem.h"
38 
39 #include <Bitmap.h>
40 #include <Debug.h>
41 #include <NaturalCompare.h>
42 
43 #include "BarApp.h"
44 #include "BarMenuBar.h"
45 #include "BarView.h"
46 #include "ExpandoMenuBar.h"
47 #include "ResourceSet.h"
48 #include "TeamMenu.h"
49 #include "WindowMenu.h"
50 
51 #include "icons.h"
52 
53 
54 const float kHPad = 10.0f;
55 const float kVPad = 2.0f;
56 const float kLabelOffset = 8.0f;
57 
58 const BRect kIconRect(1.0f, 1.0f, 13.0f, 14.0f);
59 
60 
61 //	#pragma mark - TWindowMenuItem
62 
63 
64 TWindowMenuItem::TWindowMenuItem(const char* name, int32 id, bool mini,
65 	bool currentWorkspace, bool dragging)
66 	:
67 	TTruncatableMenuItem(name, NULL),
68 	fID(id),
69 	fMini(mini),
70 	fCurrentWorkSpace(currentWorkspace),
71 	fDragging(dragging),
72 	fExpanded(false),
73 	fRequireUpdate(false),
74 	fModified(false)
75 {
76 	_Init(name);
77 }
78 
79 
80 void
81 TWindowMenuItem::GetContentSize(float* width, float* height)
82 {
83 	if (width != NULL) {
84 		if (!fExpanded) {
85 			*width = kHPad + fLabelWidth + kHPad;
86 			if (fID >= 0)
87 				*width += fBitmap->Bounds().Width() + kLabelOffset;
88 		} else
89 			*width = Frame().Width()/* - kHPad*/;
90 	}
91 
92 	// Note: when the item is in "expanded mode", ie embedded into
93 	// the Deskbar itself, then a truncated label is used in SetLabel()
94 	// The code here is ignorant of this fact, but it doesn't seem to
95 	// hurt anything.
96 
97 	if (height != NULL) {
98 		*height = (fID >= 0) ? fBitmap->Bounds().Height() : 0.0f;
99 		float labelHeight = fLabelAscent + fLabelDescent;
100 		*height = (labelHeight > *height) ? labelHeight : *height;
101 		*height += kVPad * 2;
102 	}
103 }
104 
105 
106 void
107 TWindowMenuItem::Draw()
108 {
109 	if (!fExpanded) {
110 		BMenuItem::Draw();
111 		return;
112 	}
113 
114 	// TODO: Tint this smartly based on the low color, this does
115 	// nothing to black.
116 	rgb_color menuColor = tint_color(ui_color(B_MENU_BACKGROUND_COLOR), 1.07);
117 	BRect frame(Frame());
118 	BMenu* menu = Menu();
119 
120 	menu->PushState();
121 
122 	// if not selected or being tracked on, fill with gray
123 	TBarView* barview = (static_cast<TBarApp*>(be_app))->BarView();
124 	if ((!IsSelected() && !menu->IsRedrawAfterSticky())
125 		|| barview->Dragging() || !IsEnabled()) {
126 
127 		rgb_color shadow = tint_color(menuColor, 1.09);
128 		menu->SetHighColor(shadow);
129 		frame.right = frame.left + kHPad / 2;
130 		menu->FillRect(frame);
131 
132 		menu->SetHighColor(menuColor);
133 		frame.left = frame.right + 1;
134 		frame.right = Frame().right;
135 		menu->FillRect(frame);
136 	}
137 
138 	if (IsEnabled() && IsSelected() && !menu->IsRedrawAfterSticky()) {
139 		// fill
140 		rgb_color backColor = tint_color(menuColor,
141 			B_HIGHLIGHT_BACKGROUND_TINT);
142 		menu->SetLowColor(backColor);
143 		menu->SetHighColor(backColor);
144 		menu->FillRect(frame);
145 	} else {
146 		menu->SetLowColor(menuColor);
147 		menu->SetHighColor(menuColor);
148 	}
149 
150 	DrawContent();
151 
152 	menu->PopState();
153 }
154 
155 
156 void
157 TWindowMenuItem::DrawContent()
158 {
159 	BMenu* menu = Menu();
160 	BPoint contentLocation = ContentLocation() + BPoint(kHPad, 0);
161 
162 	if (fID >= 0) {
163 		menu->SetDrawingMode(B_OP_OVER);
164 
165 		float width = fBitmap->Bounds().Width();
166 		if (width > 16)
167 			contentLocation.x -= 8;
168 
169 		float height;
170 		GetContentSize(NULL, &height);
171 		contentLocation.y += (height - fBitmap->Bounds().Height()) / 2;
172 
173 		menu->MovePenTo(contentLocation);
174 		menu->DrawBitmapAsync(fBitmap);
175 
176 		if (width > 16)
177 			contentLocation.x += 8;
178 
179 		contentLocation.x += kIconRect.Width() + kLabelOffset;
180 	}
181 	contentLocation.y = ContentLocation().y + kVPad + fLabelAscent;
182 
183 	menu->SetDrawingMode(B_OP_COPY);
184 	menu->MovePenTo(contentLocation);
185 
186 	if (IsSelected())
187 		menu->SetHighColor(ui_color(B_MENU_SELECTED_ITEM_TEXT_COLOR));
188 	else
189 		menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
190 
191 	float labelWidth = menu->StringWidth(Label());
192 	BPoint penLocation = menu->PenLocation();
193 	float offset = penLocation.x - Frame().left;
194 
195 	menu->DrawString(Label(labelWidth + offset));
196 }
197 
198 
199 status_t
200 TWindowMenuItem::Invoke(BMessage* /*message*/)
201 {
202 	if (!fDragging) {
203 		if (fID >= 0) {
204 			int32 action = (modifiers() & B_CONTROL_KEY) != 0
205 				? B_MINIMIZE_WINDOW : B_BRING_TO_FRONT;
206 
207 			bool doZoom = false;
208 			BRect zoomRect(0.0f, 0.0f, 0.0f, 0.0f);
209 			BMenuItem* item;
210 			if (!fExpanded)
211 				item = Menu()->Superitem();
212 			else
213 				item = this;
214 
215 			if (item->Menu()->Window() != NULL) {
216 				zoomRect = item->Menu()->ConvertToScreen(item->Frame());
217 				doZoom = (fMini && action == B_BRING_TO_FRONT)
218 					|| (!fMini && action == B_MINIMIZE_WINDOW);
219 			}
220 
221 			do_window_action(fID, action, zoomRect, doZoom);
222 		}
223 	}
224 	return B_OK;
225 }
226 
227 
228 void
229 TWindowMenuItem::SetTo(const char* name, int32 id, bool mini,
230 	bool currentWorkspace, bool dragging)
231 {
232 	fModified = fCurrentWorkSpace != currentWorkspace || fMini != mini;
233 
234 	fID = id;
235 	fMini = mini;
236 	fCurrentWorkSpace = currentWorkspace;
237 	fDragging = dragging;
238 	fRequireUpdate = false;
239 
240 	_Init(name);
241 }
242 
243 
244 /*static*/ int32
245 TWindowMenuItem::InsertIndexFor(BMenu* menu, int32 startIndex,
246 	TWindowMenuItem* newItem)
247 {
248 	for (int32 index = startIndex;; index++) {
249 		TWindowMenuItem* item
250 			= dynamic_cast<TWindowMenuItem*>(menu->ItemAt(index));
251 		if (item == NULL
252 			|| NaturalCompare(item->Label(), newItem->Label()) > 0) {
253 			return index;
254 		}
255 	}
256 }
257 
258 
259 //	#pragma mark - private methods
260 
261 
262 void
263 TWindowMenuItem::_Init(const char* name)
264 {
265 	if (fMini) {
266 		fBitmap = fCurrentWorkSpace
267 			? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenIcon)
268 			: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenSwitchIcon);
269 	} else {
270 		fBitmap = fCurrentWorkSpace
271 			? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownIcon)
272 			: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownSwitchIcon);
273 	}
274 
275 	BFont font(be_plain_font);
276 	fLabelWidth = ceilf(font.StringWidth(name));
277 	font_height fontHeight;
278 	font.GetHeight(&fontHeight);
279 	fLabelAscent = ceilf(fontHeight.ascent);
280 	fLabelDescent = ceilf(fontHeight.descent + fontHeight.leading);
281 
282 	SetLabel(name);
283 }
284