2 * Copyright 2001-2008, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
6 * Marc Flerackers (mflerackers@androme.be)
7 * Stephan Aßmus <superstippi@gmx.de>
11 #include <RadioButton.h>
16 #include <ControlLook.h>
18 #include <LayoutUtils.h>
21 #include <binary_compatibility/Interface.h>
24 BRadioButton::BRadioButton(BRect frame
, const char* name
, const char* label
,
25 BMessage
* message
, uint32 resizingMode
, uint32 flags
)
27 BControl(frame
, name
, label
, message
, resizingMode
, flags
| B_FRAME_EVENTS
),
30 // Resize to minimum height if needed for BeOS compatibility
32 GetPreferredSize(NULL
, &minHeight
);
33 if (Bounds().Height() < minHeight
)
34 ResizeTo(Bounds().Width(), minHeight
);
38 BRadioButton::BRadioButton(const char* name
, const char* label
,
39 BMessage
* message
, uint32 flags
)
41 BControl(name
, label
, message
, flags
| B_FRAME_EVENTS
),
47 BRadioButton::BRadioButton(const char* label
, BMessage
* message
)
49 BControl(NULL
, label
, message
, B_WILL_DRAW
| B_NAVIGABLE
| B_FRAME_EVENTS
),
55 BRadioButton::BRadioButton(BMessage
* data
)
63 BRadioButton::~BRadioButton()
69 BRadioButton::Instantiate(BMessage
* data
)
71 if (validate_instantiation(data
, "BRadioButton"))
72 return new BRadioButton(data
);
79 BRadioButton::Archive(BMessage
* data
, bool deep
) const
81 return BControl::Archive(data
, deep
);
86 BRadioButton::Draw(BRect updateRect
)
88 // its size depends on the text height
89 font_height fontHeight
;
90 GetFontHeight(&fontHeight
);
92 rgb_color base
= ui_color(B_PANEL_BACKGROUND_COLOR
);
94 uint32 flags
= be_control_look
->Flags(this);
96 flags
|= BControlLook::B_CLICKED
;
98 BRect
knobRect(_KnobFrame(fontHeight
));
100 be_control_look
->DrawRadioButton(this, rect
, updateRect
, base
, flags
);
102 // erase the is control flag before drawing the label so that the label
103 // will get drawn using B_PANEL_TEXT_COLOR.
104 flags
&= ~BControlLook::B_IS_CONTROL
;
106 BRect
labelRect(Bounds());
107 labelRect
.left
= knobRect
.right
+ 1
108 + be_control_look
->DefaultLabelSpacing();
110 const BBitmap
* icon
= IconBitmap(
111 B_INACTIVE_ICON_BITMAP
| (IsEnabled() ? 0 : B_DISABLED_ICON_BITMAP
));
113 be_control_look
->DrawLabel(this, Label(), icon
, labelRect
, updateRect
,
119 BRadioButton::MouseDown(BPoint where
)
126 if ((Window()->Flags() & B_ASYNCHRONOUS_CONTROLS
) != 0) {
129 SetMouseEventMask(B_POINTER_EVENTS
, B_LOCK_WINDOW_FOCUS
);
133 BRect bounds
= Bounds();
138 GetMouse(&where
, &buttons
, true);
139 bool inside
= bounds
.Contains(where
);
141 if (fOutlined
!= inside
) {
145 } while (buttons
!= 0);
150 SetValue(B_CONTROL_ON
);
159 BRadioButton::AttachedToWindow()
161 BControl::AttachedToWindow();
166 BRadioButton::KeyDown(const char* bytes
, int32 numBytes
)
168 // TODO: Add selecting the next button functionality (navigating radio
169 // buttons with the cursor keys)!
173 // override B_RETURN, which BControl would use to toggle the value
174 // but we don't allow setting the control to "off", only "on"
176 if (IsEnabled() && !Value()) {
177 SetValue(B_CONTROL_ON
);
184 BControl::KeyDown(bytes
, numBytes
);
190 BRadioButton::SetValue(int32 value
)
192 if (value
!= Value()) {
193 BControl::SetValueNoUpdate(value
);
194 Invalidate(_KnobFrame());
200 BView
* parent
= Parent();
203 if (parent
!= NULL
) {
204 // If the parent is a BBox, the group parent is the parent of the BBox
205 BBox
* box
= dynamic_cast<BBox
*>(parent
);
207 if (box
!= NULL
&& box
->LabelView() == this)
208 parent
= box
->Parent();
210 if (parent
!= NULL
) {
211 BBox
* box
= dynamic_cast<BBox
*>(parent
);
213 // If the parent is a BBox, skip the label if there is one
214 if (box
!= NULL
&& box
->LabelView())
215 child
= parent
->ChildAt(1);
217 child
= parent
->ChildAt(0);
219 child
= Window()->ChildAt(0);
220 } else if (Window() != NULL
)
221 child
= Window()->ChildAt(0);
223 while (child
!= NULL
) {
224 BRadioButton
* radio
= dynamic_cast<BRadioButton
*>(child
);
226 if (radio
!= NULL
&& (radio
!= this))
227 radio
->SetValue(B_CONTROL_OFF
);
229 // If the child is a BBox, check if the label is a radiobutton
230 BBox
* box
= dynamic_cast<BBox
*>(child
);
232 if (box
!= NULL
&& box
->LabelView()) {
233 radio
= dynamic_cast<BRadioButton
*>(box
->LabelView());
235 if (radio
!= NULL
&& (radio
!= this))
236 radio
->SetValue(B_CONTROL_OFF
);
240 child
= child
->NextSibling();
243 ASSERT(Value() == B_CONTROL_ON
);
248 BRadioButton::GetPreferredSize(float* _width
, float* _height
)
250 font_height fontHeight
;
251 GetFontHeight(&fontHeight
);
253 BRect
rect(_KnobFrame(fontHeight
));
254 float width
= rect
.right
+ rect
.left
;
255 float height
= rect
.bottom
+ rect
.top
;
257 const BBitmap
* icon
= IconBitmap(B_INACTIVE_ICON_BITMAP
);
259 width
+= be_control_look
->DefaultLabelSpacing()
260 + icon
->Bounds().Width() + 1;
261 height
= std::max(height
, icon
->Bounds().Height());
264 if (const char* label
= Label()) {
265 width
+= be_control_look
->DefaultLabelSpacing()
266 + ceilf(StringWidth(label
));
267 height
= std::max(height
,
268 ceilf(6.0f
+ fontHeight
.ascent
+ fontHeight
.descent
));
280 BRadioButton::ResizeToPreferred()
282 BControl::ResizeToPreferred();
287 BRadioButton::Invoke(BMessage
* message
)
289 return BControl::Invoke(message
);
294 BRadioButton::MessageReceived(BMessage
* message
)
296 BControl::MessageReceived(message
);
301 BRadioButton::WindowActivated(bool active
)
303 BControl::WindowActivated(active
);
308 BRadioButton::MouseUp(BPoint where
)
313 fOutlined
= Bounds().Contains(where
);
316 if (Value() != B_CONTROL_ON
) {
317 SetValue(B_CONTROL_ON
);
328 BRadioButton::MouseMoved(BPoint where
, uint32 code
,
329 const BMessage
* dragMessage
)
334 bool inside
= Bounds().Contains(where
);
336 if (fOutlined
!= inside
) {
344 BRadioButton::DetachedFromWindow()
346 BControl::DetachedFromWindow();
351 BRadioButton::FrameMoved(BPoint newPosition
)
353 BControl::FrameMoved(newPosition
);
358 BRadioButton::FrameResized(float newWidth
, float newHeight
)
361 BControl::FrameResized(newWidth
, newHeight
);
366 BRadioButton::ResolveSpecifier(BMessage
* message
, int32 index
,
367 BMessage
* specifier
, int32 what
, const char* property
)
369 return BControl::ResolveSpecifier(message
, index
, specifier
, what
,
375 BRadioButton::MakeFocus(bool focus
)
377 BControl::MakeFocus(focus
);
382 BRadioButton::AllAttached()
384 BControl::AllAttached();
389 BRadioButton::AllDetached()
391 BControl::AllDetached();
396 BRadioButton::GetSupportedSuites(BMessage
* message
)
398 return BControl::GetSupportedSuites(message
);
403 BRadioButton::Perform(perform_code code
, void* _data
)
406 case PERFORM_CODE_MIN_SIZE
:
407 ((perform_data_min_size
*)_data
)->return_value
408 = BRadioButton::MinSize();
411 case PERFORM_CODE_MAX_SIZE
:
412 ((perform_data_max_size
*)_data
)->return_value
413 = BRadioButton::MaxSize();
416 case PERFORM_CODE_PREFERRED_SIZE
:
417 ((perform_data_preferred_size
*)_data
)->return_value
418 = BRadioButton::PreferredSize();
421 case PERFORM_CODE_LAYOUT_ALIGNMENT
:
422 ((perform_data_layout_alignment
*)_data
)->return_value
423 = BRadioButton::LayoutAlignment();
426 case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH
:
427 ((perform_data_has_height_for_width
*)_data
)->return_value
428 = BRadioButton::HasHeightForWidth();
431 case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH
:
433 perform_data_get_height_for_width
* data
434 = (perform_data_get_height_for_width
*)_data
;
435 BRadioButton::GetHeightForWidth(data
->width
, &data
->min
, &data
->max
,
440 case PERFORM_CODE_SET_LAYOUT
:
442 perform_data_set_layout
* data
= (perform_data_set_layout
*)_data
;
443 BRadioButton::SetLayout(data
->layout
);
447 case PERFORM_CODE_LAYOUT_INVALIDATED
:
449 perform_data_layout_invalidated
* data
450 = (perform_data_layout_invalidated
*)_data
;
451 BRadioButton::LayoutInvalidated(data
->descendants
);
455 case PERFORM_CODE_DO_LAYOUT
:
457 BRadioButton::DoLayout();
461 case PERFORM_CODE_SET_ICON
:
463 perform_data_set_icon
* data
= (perform_data_set_icon
*)_data
;
464 return BRadioButton::SetIcon(data
->icon
, data
->flags
);
468 return BControl::Perform(code
, _data
);
473 BRadioButton::MaxSize()
476 GetPreferredSize(&width
, &height
);
478 return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
479 BSize(width
, height
));
484 BRadioButton::LayoutAlignment()
486 return BLayoutUtils::ComposeAlignment(ExplicitAlignment(),
487 BAlignment(B_ALIGN_LEFT
, B_ALIGN_VERTICAL_UNSET
));
492 BRadioButton::SetIcon(const BBitmap
* icon
, uint32 flags
)
494 return BControl::SetIcon(icon
, flags
| B_CREATE_DISABLED_ICON_BITMAPS
);
498 void BRadioButton::_ReservedRadioButton1() {}
499 void BRadioButton::_ReservedRadioButton2() {}
503 BRadioButton::operator=(const BRadioButton
&)
510 BRadioButton::_KnobFrame() const
512 font_height fontHeight
;
513 GetFontHeight(&fontHeight
);
514 return _KnobFrame(fontHeight
);
519 BRadioButton::_KnobFrame(const font_height
& fontHeight
) const
521 // Same as BCheckBox...
522 return BRect(0.0f
, 2.0f
, ceilf(3.0f
+ fontHeight
.ascent
),
523 ceilf(5.0f
+ fontHeight
.ascent
));
528 BRadioButton::_Redraw()
530 BRect
bounds(Bounds());
532 // fill background with ViewColor()
533 rgb_color highColor
= HighColor();
534 SetHighColor(ViewColor());
537 // restore previous HighColor()
538 SetHighColor(highColor
);