xref: /haiku/src/apps/deskbar/WindowMenuItem.cpp (revision e81a954787e50e56a7f06f72705b7859b6ab06d1)
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, kVPad);
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 		menu->MovePenTo(contentLocation);
170 		menu->DrawBitmapAsync(fBitmap);
171 
172 		if (width > 16)
173 			contentLocation.x += 8;
174 
175 		contentLocation.x += kIconRect.Width() + kLabelOffset;
176 	}
177 	contentLocation.y += fLabelAscent;
178 
179 	menu->SetDrawingMode(B_OP_COPY);
180 	menu->MovePenTo(contentLocation);
181 
182 	if (IsSelected())
183 		menu->SetHighColor(ui_color(B_MENU_SELECTED_ITEM_TEXT_COLOR));
184 	else
185 		menu->SetHighColor(ui_color(B_MENU_ITEM_TEXT_COLOR));
186 
187 	float labelWidth = menu->StringWidth(Label());
188 	BPoint penLocation = menu->PenLocation();
189 	float offset = penLocation.x - Frame().left;
190 
191 	menu->DrawString(Label(labelWidth + offset));
192 }
193 
194 
195 status_t
196 TWindowMenuItem::Invoke(BMessage* /*message*/)
197 {
198 	if (!fDragging) {
199 		if (fID >= 0) {
200 			int32 action = (modifiers() & B_CONTROL_KEY) != 0
201 				? B_MINIMIZE_WINDOW : B_BRING_TO_FRONT;
202 
203 			bool doZoom = false;
204 			BRect zoomRect(0.0f, 0.0f, 0.0f, 0.0f);
205 			BMenuItem* item;
206 			if (!fExpanded)
207 				item = Menu()->Superitem();
208 			else
209 				item = this;
210 
211 			if (item->Menu()->Window() != NULL) {
212 				zoomRect = item->Menu()->ConvertToScreen(item->Frame());
213 				doZoom = (fMini && action == B_BRING_TO_FRONT)
214 					|| (!fMini && action == B_MINIMIZE_WINDOW);
215 			}
216 
217 			do_window_action(fID, action, zoomRect, doZoom);
218 		}
219 	}
220 	return B_OK;
221 }
222 
223 
224 void
225 TWindowMenuItem::SetTo(const char* name, int32 id, bool mini,
226 	bool currentWorkspace, bool dragging)
227 {
228 	fModified = fCurrentWorkSpace != currentWorkspace || fMini != mini;
229 
230 	fID = id;
231 	fMini = mini;
232 	fCurrentWorkSpace = currentWorkspace;
233 	fDragging = dragging;
234 	fRequireUpdate = false;
235 
236 	_Init(name);
237 }
238 
239 
240 /*static*/ int32
241 TWindowMenuItem::InsertIndexFor(BMenu* menu, int32 startIndex,
242 	TWindowMenuItem* newItem)
243 {
244 	for (int32 index = startIndex;; index++) {
245 		TWindowMenuItem* item
246 			= dynamic_cast<TWindowMenuItem*>(menu->ItemAt(index));
247 		if (item == NULL
248 			|| NaturalCompare(item->Label(), newItem->Label()) > 0) {
249 			return index;
250 		}
251 	}
252 }
253 
254 
255 //	#pragma mark - private methods
256 
257 
258 void
259 TWindowMenuItem::_Init(const char* name)
260 {
261 	if (fMini) {
262 		fBitmap = fCurrentWorkSpace
263 			? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenIcon)
264 			: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowHiddenSwitchIcon);
265 	} else {
266 		fBitmap = fCurrentWorkSpace
267 			? AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownIcon)
268 			: AppResSet()->FindBitmap(B_MESSAGE_TYPE, R_WindowShownSwitchIcon);
269 	}
270 
271 	BFont font(be_plain_font);
272 	fLabelWidth = ceilf(font.StringWidth(name));
273 	font_height fontHeight;
274 	font.GetHeight(&fontHeight);
275 	fLabelAscent = ceilf(fontHeight.ascent);
276 	fLabelDescent = ceilf(fontHeight.descent + fontHeight.leading);
277 
278 	SetLabel(name);
279 }
280