vfs: check userland buffers before reading them.
[haiku.git] / src / apps / deskbar / WindowMenuItem.cpp
blobf9d70bfbdce77c8ca592ce57978e0ad0c51676f8
1 /*
2 Open Tracker License
4 Terms and Conditions
6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
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:
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.
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.
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.
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.
37 #include "WindowMenuItem.h"
39 #include <Bitmap.h>
40 #include <Debug.h>
41 #include <NaturalCompare.h>
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"
51 #include "icons.h"
54 const float kHPad = 10.0f;
55 const float kVPad = 2.0f;
56 const float kLabelOffset = 8.0f;
58 const BRect kIconRect(1.0f, 1.0f, 13.0f, 14.0f);
61 // #pragma mark - TWindowMenuItem
64 TWindowMenuItem::TWindowMenuItem(const char* name, int32 id, bool mini,
65 bool currentWorkspace, bool dragging)
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)
76 _Init(name);
80 void
81 TWindowMenuItem::GetContentSize(float* width, float* height)
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*/;
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.
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;
106 void
107 TWindowMenuItem::Draw()
109 if (!fExpanded) {
110 BMenuItem::Draw();
111 return;
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();
120 menu->PushState();
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()) {
127 rgb_color shadow = tint_color(menuColor, 1.09);
128 menu->SetHighColor(shadow);
129 frame.right = frame.left + kHPad / 2;
130 menu->FillRect(frame);
132 menu->SetHighColor(menuColor);
133 frame.left = frame.right + 1;
134 frame.right = Frame().right;
135 menu->FillRect(frame);
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);
150 DrawContent();
152 menu->PopState();
156 void
157 TWindowMenuItem::DrawContent()
159 BMenu* menu = Menu();
160 BPoint contentLocation = ContentLocation() + BPoint(kHPad, kVPad);
162 if (fID >= 0) {
163 menu->SetDrawingMode(B_OP_OVER);
165 float width = fBitmap->Bounds().Width();
166 if (width > 16)
167 contentLocation.x -= 8;
169 menu->MovePenTo(contentLocation);
170 menu->DrawBitmapAsync(fBitmap);
172 if (width > 16)
173 contentLocation.x += 8;
175 contentLocation.x += kIconRect.Width() + kLabelOffset;
177 contentLocation.y += fLabelAscent;
179 menu->SetDrawingMode(B_OP_COPY);
180 menu->MovePenTo(contentLocation);
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));
187 float labelWidth = menu->StringWidth(Label());
188 BPoint penLocation = menu->PenLocation();
189 float offset = penLocation.x - Frame().left;
191 menu->DrawString(Label(labelWidth + offset));
195 status_t
196 TWindowMenuItem::Invoke(BMessage* /*message*/)
198 if (!fDragging) {
199 if (fID >= 0) {
200 int32 action = (modifiers() & B_CONTROL_KEY) != 0
201 ? B_MINIMIZE_WINDOW : B_BRING_TO_FRONT;
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;
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);
217 do_window_action(fID, action, zoomRect, doZoom);
220 return B_OK;
224 void
225 TWindowMenuItem::SetTo(const char* name, int32 id, bool mini,
226 bool currentWorkspace, bool dragging)
228 fModified = fCurrentWorkSpace != currentWorkspace || fMini != mini;
230 fID = id;
231 fMini = mini;
232 fCurrentWorkSpace = currentWorkspace;
233 fDragging = dragging;
234 fRequireUpdate = false;
236 _Init(name);
240 /*static*/ int32
241 TWindowMenuItem::InsertIndexFor(BMenu* menu, int32 startIndex,
242 TWindowMenuItem* newItem)
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;
255 // #pragma mark - private methods
258 void
259 TWindowMenuItem::_Init(const char* name)
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);
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);
278 SetLabel(name);