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