xref: /haiku/src/apps/deskbar/BarMenuTitle.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
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 
37 #include "BarMenuTitle.h"
38 
39 #include <Bitmap.h>
40 #include <ControlLook.h>
41 
42 #include "BarApp.h"
43 #include "BarView.h"
44 #include "BarWindow.h"
45 #include "ExpandoMenuBar.h"
46 
47 
48 TBarMenuTitle::TBarMenuTitle(float width, float height, const BBitmap* icon,
49 	BMenu* menu, bool inexpando)
50 	:	BMenuItem(menu, new BMessage(B_REFS_RECEIVED)),
51 		fWidth(width),
52 		fHeight(height),
53 		fInExpando(inexpando),
54 		fIcon(icon)
55 {
56 }
57 
58 
59 TBarMenuTitle::~TBarMenuTitle()
60 {
61 }
62 
63 
64 void
65 TBarMenuTitle::SetWidthHeight(float width, float height)
66 {
67 	fWidth = width;
68 	fHeight = height;
69 }
70 
71 
72 void
73 TBarMenuTitle::GetContentSize(float* width, float* height)
74 {
75 	*width = fWidth;
76 	*height = fHeight;
77 }
78 
79 
80 void
81 TBarMenuTitle::Draw()
82 {
83 	if (be_control_look == NULL) {
84 		BMenuItem::Draw();
85 		return;
86 	}
87 
88 	// fill background if selected
89 	rgb_color base = Menu()->LowColor();
90 	BRect rect = Frame();
91 
92 	BRect windowBounds = Menu()->Window()->Bounds();
93 	if (rect.right > windowBounds.right)
94 		rect.right = windowBounds.right;
95 
96 	if (IsSelected()) {
97 		be_control_look->DrawMenuItemBackground(Menu(), rect, rect, base,
98 			BControlLook::B_ACTIVATED);
99 	} else {
100 		be_control_look->DrawButtonBackground(Menu(), rect, rect, base);
101 	}
102 
103 	// draw content
104 	DrawContent();
105 
106 	// make sure we restore state
107 	Menu()->SetLowColor(base);
108 }
109 
110 
111 void
112 TBarMenuTitle::DrawContent()
113 {
114 	BMenu* menu = Menu();
115 	BRect frame(Frame());
116 
117 	if (be_control_look != NULL) {
118 		menu->SetDrawingMode(B_OP_ALPHA);
119 
120 		if (fIcon != NULL) {
121 			BRect dstRect(fIcon->Bounds());
122 			dstRect.OffsetTo(frame.LeftTop());
123 			dstRect.OffsetBy(rintf(((frame.Width() - dstRect.Width()) / 2)
124 				- 1.0f), rintf(((frame.Height() - dstRect.Height()) / 2)
125 				- 0.0f));
126 
127 			menu->DrawBitmapAsync(fIcon, dstRect);
128 		}
129 		return;
130 	}
131 
132 	rgb_color menuColor = menu->ViewColor();
133 	rgb_color dark = tint_color(menuColor, B_DARKEN_1_TINT);
134 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
135 	rgb_color black = {0, 0, 0, 255};
136 
137 	bool inExpandoMode = dynamic_cast<TExpandoMenuBar*>(menu) != NULL;
138 
139 	BRect bounds(menu->Window()->Bounds());
140 	if (bounds.right < frame.right)
141 		frame.right = bounds.right;
142 
143 	menu->SetDrawingMode(B_OP_COPY);
144 
145 	if (!IsSelected() && !menu->IsRedrawAfterSticky()) {
146 		menu->BeginLineArray(8);
147 		menu->AddLine(frame.RightTop(), frame.LeftTop(), light);
148 		menu->AddLine(frame.LeftBottom(), frame.RightBottom(), dark);
149 		menu->AddLine(frame.LeftTop(),
150 			frame.LeftBottom()+BPoint(0, inExpandoMode ? 0 : -1), light);
151 		menu->AddLine(frame.RightBottom(), frame.RightTop(), dark);
152 		if (inExpandoMode) {
153 			frame.top += 1;
154 			menu->AddLine(frame.LeftTop(), frame.RightTop() + BPoint(-1, 0),
155 				light);
156 		}
157 
158 		menu->EndLineArray();
159 
160 		frame.InsetBy(1, 1);
161 		menu->SetHighColor(menuColor);
162 		menu->FillRect(frame);
163 		menu->SetHighColor(black);
164 		frame.InsetBy(-1, -1);
165 		if (inExpandoMode)
166 			frame.top -= 1;
167 	}
168 
169 	ASSERT(IsEnabled());
170 	if (IsSelected() && !menu->IsRedrawAfterSticky()) {
171 		menu->SetHighColor(tint_color(menuColor, B_HIGHLIGHT_BACKGROUND_TINT));
172 		menu->FillRect(frame);
173 
174 		if (menu->IndexOf(this) > 0) {
175 			menu->SetHighColor(tint_color(menuColor, B_DARKEN_4_TINT));
176 			menu->StrokeLine(frame.LeftTop(), frame.LeftBottom());
177 		}
178 
179 		menu->SetHighColor(black);
180 	}
181 
182 	menu->SetDrawingMode(B_OP_ALPHA);
183 
184 	if (fIcon != NULL) {
185 		BRect dstRect(fIcon->Bounds());
186 		dstRect.OffsetTo(frame.LeftTop());
187 		dstRect.OffsetBy(rintf(((frame.Width() - dstRect.Width()) / 2) - 1.0f),
188 			rintf(((frame.Height() - dstRect.Height()) / 2) - 0.0f));
189 
190 		menu->DrawBitmapAsync(fIcon, dstRect);
191 	}
192 }
193 
194 
195 status_t
196 TBarMenuTitle::Invoke(BMessage* message)
197 {
198 	TBarView* barview = dynamic_cast<TBarApp*>(be_app)->BarView();
199 	if (barview) {
200 		BLooper* looper = barview->Looper();
201 		if (looper->Lock()) {
202 			// tell barview to add the refs to the be menu
203 			barview->HandleBeMenu(NULL);
204 			looper->Unlock();
205 		}
206 	}
207 
208 	return BMenuItem::Invoke(message);
209 }
210 
211