xref: /haiku/src/kits/tracker/MiniMenuField.cpp (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
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 <PopUpMenu.h>
36 #include <Window.h>
37 
38 #include "MiniMenuField.h"
39 #include "Utilities.h"
40 
41 MiniMenuField::MiniMenuField(BRect frame, const char *name, BPopUpMenu *menu,
42 	uint32 resizeFlags, uint32 flags)
43 	:	BView(frame, name, resizeFlags, flags),
44 		fMenu(menu)
45 {
46 	SetFont(be_plain_font, B_FONT_FAMILY_AND_STYLE | B_FONT_SIZE);
47 }
48 
49 MiniMenuField::~MiniMenuField()
50 {
51 	delete fMenu;
52 }
53 
54 void
55 MiniMenuField::AttachedToWindow()
56 {
57 	if (Parent()) {
58 		SetViewColor(Parent()->ViewColor());
59 		SetLowColor(Parent()->ViewColor());
60 	}
61 	SetHighColor(0, 0, 0);
62 }
63 
64 void
65 MiniMenuField::MakeFocus(bool on)
66 {
67 	Invalidate();
68 	BView::MakeFocus(on);
69 }
70 
71 void
72 MiniMenuField::KeyDown(const char *bytes, int32 numBytes)
73 {
74 	switch (bytes[0]) {
75 		case B_SPACE:
76 		case B_DOWN_ARROW:
77 		case B_RIGHT_ARROW:
78 			// invoke from keyboard
79 			fMenu->Go(ConvertToScreen(BPoint(4, 4)), true, true);
80 			break;
81 
82 		default:
83 			BView::KeyDown(bytes, numBytes);
84 			break;
85 	}
86 }
87 
88 void
89 MiniMenuField::Draw(BRect)
90 {
91 	BRect bounds(Bounds());
92 	bounds.InsetBy(2, 2);
93 	BRect rect(bounds);
94 	rect.right--;
95 	rect.bottom--;
96 
97 	rgb_color darkest = tint_color(kBlack, 0.6f);
98 	rgb_color dark = tint_color(kBlack, 0.4f);
99 	rgb_color medium = dark;
100 	rgb_color light = tint_color(kBlack, 0.03f);
101 
102 	SetHighColor(medium);
103 
104 	// draw frame and shadow
105 	BeginLineArray(10);
106 	AddLine(rect.RightTop(), rect.RightBottom(), darkest);
107 	AddLine(rect.RightBottom(), rect.LeftBottom(), darkest);
108 	AddLine(rect.LeftBottom(), rect.LeftTop(), medium);
109 	AddLine(rect.LeftTop(), rect.RightTop(), medium);
110 	AddLine(bounds.LeftBottom() + BPoint(2, 0), bounds.RightBottom(), dark);
111 	AddLine(bounds.RightTop() + BPoint(0, 1), bounds.RightBottom(), dark);
112 	rect.InsetBy(1, 1);
113 	AddLine(rect.RightTop(), rect.RightBottom(), medium);
114 	AddLine(rect.RightBottom(), rect.LeftBottom(), medium);
115 	AddLine(rect.LeftBottom(), rect.LeftTop(), light);
116 	AddLine(rect.LeftTop(), rect.RightTop(), light);
117 
118 	EndLineArray();
119 
120 	// draw triangle
121 	rect = BRect(5, 5, 15, 15);
122 	const rgb_color outlineColor = kBlack;
123 	const rgb_color middleColor = {150, 150, 150, 255};
124 
125 	BeginLineArray(5);
126 	AddLine(BPoint(rect.left + 3, rect.top + 1),
127 		BPoint(rect.left + 3, rect.top + 7), outlineColor);
128 	AddLine(BPoint(rect.left + 3, rect.top + 1),
129 		BPoint(rect.left + 6, rect.top + 4), outlineColor);
130 	AddLine(BPoint(rect.left + 6, rect.top + 4),
131 		BPoint(rect.left + 3, rect.top + 7), outlineColor);
132 
133 	AddLine(BPoint(rect.left + 4, rect.top + 3),
134 		BPoint(rect.left + 4, rect.top + 5), middleColor);
135 	AddLine(BPoint(rect.left + 5, rect.top + 4),
136 		BPoint(rect.left + 5, rect.top + 4), middleColor);
137 	EndLineArray();
138 
139 	// draw focus if focused, else erase focus
140 	bounds = Bounds();
141 	bool focused = IsFocus() && Window()->IsActive();
142 	rgb_color markColor = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
143 	rgb_color viewColor = ViewColor();
144 	BeginLineArray(4);
145 	AddLine(BPoint(bounds.left, bounds.top),
146 		BPoint(bounds.right, bounds.top), focused ? markColor : viewColor);
147 	AddLine(BPoint(bounds.right, bounds.top),
148 		BPoint(bounds.right, bounds.bottom), focused ? markColor : viewColor);
149 	AddLine(BPoint(bounds.right, bounds.bottom),
150 		BPoint(bounds.left, bounds.bottom), focused ? markColor : viewColor);
151 	AddLine(BPoint(bounds.left, bounds.bottom),
152 		BPoint(bounds.left, bounds.top), focused ? markColor : viewColor);
153 	EndLineArray();
154 
155 }
156 
157 void
158 MiniMenuField::MouseDown(BPoint)
159 {
160 	fMenu->Go(ConvertToScreen(BPoint(4, 4)), true);
161 }
162 
163