2 * Copyright 2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * John Scipione, jscipione@gmail.com
10 #include "ColorMenuItem.h"
18 #include <MenuField.h>
19 #include <MenuPrivate.h>
26 #define M_PHI 1.61803398874989484820
29 // #pragma - ColorMenuItem
32 BColorMenuItem::BColorMenuItem(const char* label
, BMessage
* message
,
33 rgb_color color
, char shortcut
, uint32 modifiers
)
35 BMenuItem(label
, message
, shortcut
, modifiers
),
41 BColorMenuItem::BColorMenuItem(BMenu
* menu
, rgb_color color
,
44 BMenuItem(menu
, message
),
50 BColorMenuItem::BColorMenuItem(BMessage
* data
)
54 if (data
->HasColor("_color")) {
57 color
= data
->GetColor("_color", (rgb_color
){ 0, 0, 0 });
60 fColor
= (rgb_color
){ 0, 0, 0 };
65 BColorMenuItem::Instantiate(BMessage
* data
)
67 if (validate_instantiation(data
, "BColorMenuItem"))
68 return new BColorMenuItem(data
);
75 BColorMenuItem::Archive(BMessage
* data
, bool deep
) const
77 status_t result
= BMenuItem::Archive(data
, deep
);
80 result
= data
->AddColor("_color", fColor
);
87 BColorMenuItem::DrawContent()
89 float leftMargin
= _LeftMargin();
90 float padding
= _Padding();
91 float colorRectWidth
= _ColorRectWidth();
93 BRect
colorRect(Frame().InsetByCopy(2.0f
, 2.0f
));
94 colorRect
.left
= colorRect
.right
= leftMargin
;
95 colorRect
.right
+= colorRectWidth
;
97 rgb_color highColor
= Menu()->HighColor();
99 Menu()->SetDrawingMode(B_OP_OVER
);
101 Menu()->SetHighColor(fColor
);
102 Menu()->FillRect(colorRect
);
104 Menu()->SetHighColor(ui_color(B_CONTROL_BORDER_COLOR
));
105 Menu()->StrokeRect(colorRect
);
107 float width
= colorRectWidth
+ padding
;
109 Menu()->MovePenBy(width
, 0.0f
);
110 Menu()->SetHighColor(highColor
);
112 BMenuItem::DrawContent();
117 BColorMenuItem::GetContentSize(float* _width
, float* _height
)
121 BMenuItem::GetContentSize(&labelWidth
, &height
);
124 *_width
= _LeftMargin() + _ColorRectWidth() + _Padding() + labelWidth
;
132 BColorMenuItem::SetMarked(bool mark
)
134 BMenuItem::SetMarked(mark
);
139 // we are marking the item
141 BMenu
* menu
= Menu();
145 // we have a parent menu
148 while ((_menu
= _menu
->Supermenu()) != NULL
)
151 // went up the hierarchy to found the topmost menu
153 if (menu
== NULL
|| menu
->Parent() == NULL
)
156 // our topmost menu has a parent
158 if (dynamic_cast<BMenuField
*>(menu
->Parent()) == NULL
)
161 // our topmost menu's parent is a BMenuField
163 BMenuItem
* topLevelItem
= menu
->ItemAt((int32
)0);
165 if (topLevelItem
== NULL
)
168 // our topmost menu has a menu item
170 BColorMenuItem
* topLevelColorMenuItem
171 = dynamic_cast<BColorMenuItem
*>(topLevelItem
);
172 if (topLevelColorMenuItem
== NULL
)
175 // our topmost menu's item is a BColorMenuItem
178 topLevelColorMenuItem
->SetColor(fColor
);
183 // #pragma mark - BColorMenuItem private methods
187 BColorMenuItem::_LeftMargin()
189 BPrivate::MenuPrivate
menuPrivate(Menu());
191 menuPrivate
.GetItemMargins(&leftMargin
, NULL
, NULL
, NULL
);
197 BColorMenuItem::_Padding()
199 return floorf(std::max(14.0f
, be_plain_font
->Size() + 2) / 2);
204 BColorMenuItem::_ColorRectWidth()
206 return floorf(std::max(14.0f
, be_plain_font
->Size() + 2) * M_PHI
);
211 // #pragma mark - BColorMenuItem reserved virtual methods
214 void BColorMenuItem::_ReservedColorMenuItem1() {}
215 void BColorMenuItem::_ReservedColorMenuItem2() {}
216 void BColorMenuItem::_ReservedColorMenuItem3() {}
217 void BColorMenuItem::_ReservedColorMenuItem4() {}
218 void BColorMenuItem::_ReservedColorMenuItem5() {}
219 void BColorMenuItem::_ReservedColorMenuItem6() {}
220 void BColorMenuItem::_ReservedColorMenuItem7() {}
221 void BColorMenuItem::_ReservedColorMenuItem8() {}
222 void BColorMenuItem::_ReservedColorMenuItem9() {}
223 void BColorMenuItem::_ReservedColorMenuItem10() {}
224 void BColorMenuItem::_ReservedColorMenuItem11() {}
225 void BColorMenuItem::_ReservedColorMenuItem12() {}
226 void BColorMenuItem::_ReservedColorMenuItem13() {}
227 void BColorMenuItem::_ReservedColorMenuItem14() {}
228 void BColorMenuItem::_ReservedColorMenuItem15() {}
229 void BColorMenuItem::_ReservedColorMenuItem16() {}
230 void BColorMenuItem::_ReservedColorMenuItem17() {}
231 void BColorMenuItem::_ReservedColorMenuItem18() {}
232 void BColorMenuItem::_ReservedColorMenuItem19() {}
233 void BColorMenuItem::_ReservedColorMenuItem20() {}