xref: /haiku/src/apps/deskbar/BarMenuTitle.cpp (revision 16d5c24e533eb14b7b8a99ee9f3ec9ba66335b1e)
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) - 1.0f),
124 				rintf(((frame.Height() - dstRect.Height()) / 2) - 0.0f));
125 
126 			menu->DrawBitmapAsync(fIcon, dstRect);
127 		}
128 		return;
129 	}
130 
131 	rgb_color menuColor = menu->ViewColor();
132 	rgb_color dark = tint_color(menuColor, B_DARKEN_1_TINT);
133 	rgb_color light = tint_color(menuColor, B_LIGHTEN_2_TINT);
134 	rgb_color black = {0, 0, 0, 255};
135 
136 	bool inExpandoMode = dynamic_cast<TExpandoMenuBar*>(menu) != NULL;
137 
138 	BRect bounds(menu->Window()->Bounds());
139 	if (bounds.right < frame.right)
140 		frame.right = bounds.right;
141 
142 	menu->SetDrawingMode(B_OP_COPY);
143 
144 	if (!IsSelected() && !menu->IsRedrawAfterSticky()) {
145 		menu->BeginLineArray(8);
146 		menu->AddLine(frame.RightTop(), frame.LeftTop(), light);
147 		menu->AddLine(frame.LeftBottom(), frame.RightBottom(), dark);
148 		menu->AddLine(frame.LeftTop(),
149 			frame.LeftBottom()+BPoint(0, inExpandoMode ? 0 : -1), light);
150 		menu->AddLine(frame.RightBottom(), frame.RightTop(), dark);
151 		if (inExpandoMode) {
152 			frame.top += 1;
153 			menu->AddLine(frame.LeftTop(), frame.RightTop() + BPoint(-1, 0),
154 				light);
155 		}
156 
157 		menu->EndLineArray();
158 
159 		frame.InsetBy(1, 1);
160 		menu->SetHighColor(menuColor);
161 		menu->FillRect(frame);
162 		menu->SetHighColor(black);
163 		frame.InsetBy(-1, -1);
164 		if (inExpandoMode)
165 			frame.top -= 1;
166 	}
167 
168 	ASSERT(IsEnabled());
169 	if (IsSelected() && !menu->IsRedrawAfterSticky()) {
170 		menu->SetHighColor(tint_color(menuColor, B_HIGHLIGHT_BACKGROUND_TINT));
171 		menu->FillRect(frame);
172 
173 		if (menu->IndexOf(this) > 0) {
174 			menu->SetHighColor(tint_color(menuColor, B_DARKEN_4_TINT));
175 			menu->StrokeLine(frame.LeftTop(), frame.LeftBottom());
176 		}
177 
178 		menu->SetHighColor(black);
179 	}
180 
181 	menu->SetDrawingMode(B_OP_ALPHA);
182 
183 	if (fIcon != NULL) {
184 		BRect dstRect(fIcon->Bounds());
185 		dstRect.OffsetTo(frame.LeftTop());
186 		dstRect.OffsetBy(rintf(((frame.Width() - dstRect.Width()) / 2) - 1.0f),
187 			rintf(((frame.Height() - dstRect.Height()) / 2) - 0.0f));
188 
189 		menu->DrawBitmapAsync(fIcon, dstRect);
190 	}
191 }
192 
193 
194 status_t
195 TBarMenuTitle::Invoke(BMessage *message)
196 {
197 	TBarView *barview = dynamic_cast<TBarApp *>(be_app)->BarView();
198 	if (barview) {
199 		BLooper *looper = barview->Looper();
200 		if (looper->Lock()) {
201 			// tell barview to add the refs to the be menu
202 			barview->HandleBeMenu(NULL);
203 			looper->Unlock();
204 		}
205 	}
206 
207 	return BMenuItem::Invoke(message);
208 }
209