xref: /haiku/src/apps/deskbar/BarMenuTitle.cpp (revision 579f1dbca962a2a03df54f69fdc6e9423f91f20e)
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 
43 #include "BarApp.h"
44 #include "BarView.h"
45 #include "BarWindow.h"
46 #include "ExpandoMenuBar.h"
47 
48 
49 TBarMenuTitle::TBarMenuTitle(float width, float height, const BBitmap* icon,
50 	BMenu* menu, bool expando)
51 	:
52 	BMenuItem(menu, new BMessage(B_REFS_RECEIVED)),
53 	fWidth(width),
54 	fHeight(height),
55 	fInExpando(expando),
56 	fIcon(icon)
57 {
58 }
59 
60 
61 TBarMenuTitle::~TBarMenuTitle()
62 {
63 }
64 
65 
66 void
67 TBarMenuTitle::SetContentSize(float width, float height)
68 {
69 	fWidth = width;
70 	fHeight = height;
71 }
72 
73 
74 void
75 TBarMenuTitle::GetContentSize(float* width, float* height)
76 {
77 	*width = fWidth;
78 	*height = fHeight;
79 }
80 
81 
82 void
83 TBarMenuTitle::Draw()
84 {
85 	BMenu* menu = Menu();
86 	if (menu == NULL)
87 		return;
88 
89 	BRect frame(Frame());
90 	rgb_color base = menu->LowColor();
91 
92 	menu->PushState();
93 
94 	BRect windowBounds = menu->Window()->Bounds();
95 	if (frame.right > windowBounds.right)
96 		frame.right = windowBounds.right;
97 
98 	// fill in background
99 	if (IsSelected()) {
100 		be_control_look->DrawMenuItemBackground(menu, frame, frame, base,
101 			BControlLook::B_ACTIVATED);
102 	} else
103 		be_control_look->DrawButtonBackground(menu, frame, frame, base);
104 
105 	menu->MovePenTo(ContentLocation());
106 	DrawContent();
107 
108 	menu->PopState();
109 }
110 
111 
112 void
113 TBarMenuTitle::DrawContent()
114 {
115 	if (fIcon == NULL)
116 		return;
117 
118 	BMenu* menu = Menu();
119 	BRect frame(Frame());
120 	BRect iconRect(fIcon->Bounds());
121 
122 	menu->SetDrawingMode(B_OP_ALPHA);
123 	iconRect.OffsetTo(frame.LeftTop());
124 
125 	float widthOffset = rintf((frame.Width() - iconRect.Width()) / 2);
126 	float heightOffset = rintf((frame.Height() - iconRect.Height()) / 2);
127 	iconRect.OffsetBy(widthOffset - 1.0f, heightOffset + 2.0f);
128 
129 	menu->DrawBitmapAsync(fIcon, iconRect);
130 }
131 
132 
133 status_t
134 TBarMenuTitle::Invoke(BMessage* message)
135 {
136 	TBarView* barview = dynamic_cast<TBarApp*>(be_app)->BarView();
137 	if (barview) {
138 		BLooper* looper = barview->Looper();
139 		if (looper->Lock()) {
140 			// tell barview to add the refs to the deskbar menu
141 			barview->HandleDeskbarMenu(NULL);
142 			looper->Unlock();
143 		}
144 	}
145 
146 	return BMenuItem::Invoke(message);
147 }
148