xref: /haiku/src/apps/deskbar/BarMenuTitle.cpp (revision 21258e2674226d6aa732321b6f8494841895af5f)
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 "BarMenuTitle.h"
38 
39 #include <Bitmap.h>
40 #include <ControlLook.h>
41 #include <Debug.h>
42 #include <Region.h>
43 
44 #include "BarApp.h"
45 #include "BarView.h"
46 #include "BarWindow.h"
47 #include "DeskbarMenu.h"
48 
49 
50 TBarMenuTitle::TBarMenuTitle(float width, float height, const BBitmap* icon,
51 	BMenu* menu, TBarView* barView)
52 	:
53 	BMenuItem(menu, new BMessage(B_REFS_RECEIVED)),
54 	fWidth(width),
55 	fHeight(height),
56 	fIcon(icon),
57 	fMenu(menu),
58 	fBarView(barView),
59 	fInitStatus(B_NO_INIT)
60 {
61 	if (fIcon == NULL || fMenu == NULL || fBarView == NULL)
62 		fInitStatus = B_BAD_VALUE;
63 	else
64 		fInitStatus = B_OK;
65 }
66 
67 
68 TBarMenuTitle::~TBarMenuTitle()
69 {
70 }
71 
72 
73 void
74 TBarMenuTitle::SetContentSize(float width, float height)
75 {
76 	fWidth = width;
77 	fHeight = height;
78 }
79 
80 
81 void
82 TBarMenuTitle::GetContentSize(float* width, float* height)
83 {
84 	*width = fWidth;
85 	*height = fHeight;
86 }
87 
88 
89 void
90 TBarMenuTitle::Draw()
91 {
92 	BMenu* menu = Menu();
93 	if (fInitStatus != B_OK || menu == NULL)
94 		return;
95 
96 	BRect frame(Frame());
97 	rgb_color base = ui_color(B_MENU_BACKGROUND_COLOR);
98 
99 	menu->PushState();
100 
101 	BRect windowBounds = menu->Window()->Bounds();
102 	if (frame.right > windowBounds.right)
103 		frame.right = windowBounds.right;
104 
105 	// fill in background
106 	if (IsSelected()) {
107 		be_control_look->DrawMenuItemBackground(menu, frame, frame, base,
108 			BControlLook::B_ACTIVATED);
109 	} else
110 		be_control_look->DrawButtonBackground(menu, frame, frame, base);
111 
112 	menu->MovePenTo(ContentLocation());
113 	DrawContent();
114 
115 	menu->PopState();
116 }
117 
118 
119 void
120 TBarMenuTitle::DrawContent()
121 {
122 	if (fInitStatus != B_OK)
123 		return;
124 
125 	BMenu* menu = Menu();
126 	if (menu == NULL)
127 		return;
128 
129 	menu->SetDrawingMode(B_OP_ALPHA);
130 
131 	const BRect frame(Frame());
132 	BRect iconRect(fIcon->Bounds().OffsetToCopy(frame.LeftTop()));
133 
134 	float widthOffset = rintf((frame.Width() - iconRect.Width()) / 2);
135 	float heightOffset = rintf((frame.Height() - iconRect.Height()) / 2);
136 
137 	// cut-off the leaf
138 	bool isLeafMenu = dynamic_cast<TDeskbarMenu*>(fMenu) != NULL;
139 	if (isLeafMenu)
140 		iconRect.OffsetBy(widthOffset, frame.Height() - iconRect.Height() + 2);
141 	else
142 		iconRect.OffsetBy(widthOffset, heightOffset);
143 
144 	// clip to menu item frame
145 	if (iconRect.Width() > frame.Width()) {
146 		float diff = iconRect.Width() - frame.Width();
147 		BRect mask(iconRect.InsetByCopy(floorf(diff / 2), 0));
148 		BRegion clipping(mask);
149 		menu->ConstrainClippingRegion(&clipping);
150 	}
151 
152 	menu->DrawBitmapAsync(fIcon, iconRect);
153 	menu->ConstrainClippingRegion(NULL);
154 }
155 
156 
157 status_t
158 TBarMenuTitle::Invoke(BMessage* message)
159 {
160 	if (fInitStatus != B_OK || fBarView == NULL)
161 		return fInitStatus;
162 
163 	BLooper* looper = fBarView->Looper();
164 	if (looper->Lock()) {
165 		// tell barview to add the refs to the deskbar menu
166 		fBarView->HandleDeskbarMenu(NULL);
167 		looper->Unlock();
168 	}
169 
170 	return BMenuItem::Invoke(message);
171 }
172