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 BRect
labelRect(Bounds());
103 labelRect
.left
= knobRect
.right
+ 1
104 + be_control_look
->DefaultLabelSpacing();
106 const BBitmap
* icon
= IconBitmap(
107 B_INACTIVE_ICON_BITMAP
| (IsEnabled() ? 0 : B_DISABLED_ICON_BITMAP
));
109 be_control_look
->DrawLabel(this, Label(), icon
, labelRect
, updateRect
,
115 BRadioButton::MouseDown(BPoint where
)
122 if ((Window()->Flags() & B_ASYNCHRONOUS_CONTROLS
) != 0) {
125 SetMouseEventMask(B_POINTER_EVENTS
, B_LOCK_WINDOW_FOCUS
);
129 BRect bounds
= Bounds();
134 GetMouse(&where
, &buttons
, true);
135 bool inside
= bounds
.Contains(where
);
137 if (fOutlined
!= inside
) {
141 } while (buttons
!= 0);
146 SetValue(B_CONTROL_ON
);
155 BRadioButton::AttachedToWindow()
157 BControl::AttachedToWindow();
162 BRadioButton::KeyDown(const char* bytes
, int32 numBytes
)
164 // TODO: Add selecting the next button functionality (navigating radio
165 // buttons with the cursor keys)!
169 // override B_RETURN, which BControl would use to toggle the value
170 // but we don't allow setting the control to "off", only "on"
172 if (IsEnabled() && !Value()) {
173 SetValue(B_CONTROL_ON
);
180 BControl::KeyDown(bytes
, numBytes
);
186 BRadioButton::SetValue(int32 value
)
188 if (value
!= Value()) {
189 BControl::SetValueNoUpdate(value
);
190 Invalidate(_KnobFrame());
196 BView
* parent
= Parent();
199 if (parent
!= NULL
) {
200 // If the parent is a BBox, the group parent is the parent of the BBox
201 BBox
* box
= dynamic_cast<BBox
*>(parent
);
203 if (box
!= NULL
&& box
->LabelView() == this)
204 parent
= box
->Parent();
206 if (parent
!= NULL
) {
207 BBox
* box
= dynamic_cast<BBox
*>(parent
);
209 // If the parent is a BBox, skip the label if there is one
210 if (box
!= NULL
&& box
->LabelView())
211 child
= parent
->ChildAt(1);
213 child
= parent
->ChildAt(0);
215 child
= Window()->ChildAt(0);
216 } else if (Window() != NULL
)
217 child
= Window()->ChildAt(0);
219 while (child
!= NULL
) {
220 BRadioButton
* radio
= dynamic_cast<BRadioButton
*>(child
);
222 if (radio
!= NULL
&& (radio
!= this))
223 radio
->SetValue(B_CONTROL_OFF
);
225 // If the child is a BBox, check if the label is a radiobutton
226 BBox
* box
= dynamic_cast<BBox
*>(child
);
228 if (box
!= NULL
&& box
->LabelView()) {
229 radio
= dynamic_cast<BRadioButton
*>(box
->LabelView());
231 if (radio
!= NULL
&& (radio
!= this))
232 radio
->SetValue(B_CONTROL_OFF
);
236 child
= child
->NextSibling();
239 ASSERT(Value() == B_CONTROL_ON
);
244 BRadioButton::GetPreferredSize(float* _width
, float* _height
)
246 font_height fontHeight
;
247 GetFontHeight(&fontHeight
);
249 BRect
rect(_KnobFrame(fontHeight
));
250 float width
= rect
.right
+ rect
.left
;
251 float height
= rect
.bottom
+ rect
.top
;
253 const BBitmap
* icon
= IconBitmap(B_INACTIVE_ICON_BITMAP
);
255 width
+= be_control_look
->DefaultLabelSpacing()
256 + icon
->Bounds().Width() + 1;
257 height
= std::max(height
, icon
->Bounds().Height());
260 if (const char* label
= Label()) {
261 width
+= be_control_look
->DefaultLabelSpacing()
262 + ceilf(StringWidth(label
));
263 height
= std::max(height
,
264 ceilf(6.0f
+ fontHeight
.ascent
+ fontHeight
.descent
));
276 BRadioButton::ResizeToPreferred()
278 BControl::ResizeToPreferred();
283 BRadioButton::Invoke(BMessage
* message
)
285 return BControl::Invoke(message
);
290 BRadioButton::MessageReceived(BMessage
* message
)
292 BControl::MessageReceived(message
);
297 BRadioButton::WindowActivated(bool active
)
299 BControl::WindowActivated(active
);
304 BRadioButton::MouseUp(BPoint where
)
309 fOutlined
= Bounds().Contains(where
);
312 if (Value() != B_CONTROL_ON
) {
313 SetValue(B_CONTROL_ON
);
324 BRadioButton::MouseMoved(BPoint where
, uint32 code
,
325 const BMessage
* dragMessage
)
330 bool inside
= Bounds().Contains(where
);
332 if (fOutlined
!= inside
) {
340 BRadioButton::DetachedFromWindow()
342 BControl::DetachedFromWindow();
347 BRadioButton::FrameMoved(BPoint newPosition
)
349 BControl::FrameMoved(newPosition
);
354 BRadioButton::FrameResized(float newWidth
, float newHeight
)
357 BControl::FrameResized(newWidth
, newHeight
);
362 BRadioButton::ResolveSpecifier(BMessage
* message
, int32 index
,
363 BMessage
* specifier
, int32 what
, const char* property
)
365 return BControl::ResolveSpecifier(message
, index
, specifier
, what
,
371 BRadioButton::MakeFocus(bool focus
)
373 BControl::MakeFocus(focus
);
378 BRadioButton::AllAttached()
380 BControl::AllAttached();
385 BRadioButton::AllDetached()
387 BControl::AllDetached();
392 BRadioButton::GetSupportedSuites(BMessage
* message
)
394 return BControl::GetSupportedSuites(message
);
399 BRadioButton::Perform(perform_code code
, void* _data
)
402 case PERFORM_CODE_MIN_SIZE
:
403 ((perform_data_min_size
*)_data
)->return_value
404 = BRadioButton::MinSize();
407 case PERFORM_CODE_MAX_SIZE
:
408 ((perform_data_max_size
*)_data
)->return_value
409 = BRadioButton::MaxSize();
412 case PERFORM_CODE_PREFERRED_SIZE
:
413 ((perform_data_preferred_size
*)_data
)->return_value
414 = BRadioButton::PreferredSize();
417 case PERFORM_CODE_LAYOUT_ALIGNMENT
:
418 ((perform_data_layout_alignment
*)_data
)->return_value
419 = BRadioButton::LayoutAlignment();
422 case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH
:
423 ((perform_data_has_height_for_width
*)_data
)->return_value
424 = BRadioButton::HasHeightForWidth();
427 case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH
:
429 perform_data_get_height_for_width
* data
430 = (perform_data_get_height_for_width
*)_data
;
431 BRadioButton::GetHeightForWidth(data
->width
, &data
->min
, &data
->max
,
436 case PERFORM_CODE_SET_LAYOUT
:
438 perform_data_set_layout
* data
= (perform_data_set_layout
*)_data
;
439 BRadioButton::SetLayout(data
->layout
);
443 case PERFORM_CODE_LAYOUT_INVALIDATED
:
445 perform_data_layout_invalidated
* data
446 = (perform_data_layout_invalidated
*)_data
;
447 BRadioButton::LayoutInvalidated(data
->descendants
);
451 case PERFORM_CODE_DO_LAYOUT
:
453 BRadioButton::DoLayout();
457 case PERFORM_CODE_SET_ICON
:
459 perform_data_set_icon
* data
= (perform_data_set_icon
*)_data
;
460 return BRadioButton::SetIcon(data
->icon
, data
->flags
);
464 return BControl::Perform(code
, _data
);
469 BRadioButton::MaxSize()
472 GetPreferredSize(&width
, &height
);
474 return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
475 BSize(width
, height
));
480 BRadioButton::LayoutAlignment()
482 return BLayoutUtils::ComposeAlignment(ExplicitAlignment(),
483 BAlignment(B_ALIGN_LEFT
, B_ALIGN_VERTICAL_UNSET
));
488 BRadioButton::SetIcon(const BBitmap
* icon
, uint32 flags
)
490 return BControl::SetIcon(icon
, flags
| B_CREATE_DISABLED_ICON_BITMAPS
);
494 void BRadioButton::_ReservedRadioButton1() {}
495 void BRadioButton::_ReservedRadioButton2() {}
499 BRadioButton::operator=(const BRadioButton
&)
506 BRadioButton::_KnobFrame() const
508 font_height fontHeight
;
509 GetFontHeight(&fontHeight
);
510 return _KnobFrame(fontHeight
);
515 BRadioButton::_KnobFrame(const font_height
& fontHeight
) const
517 // Same as BCheckBox...
518 return BRect(0.0f
, 2.0f
, ceilf(3.0f
+ fontHeight
.ascent
),
519 ceilf(5.0f
+ fontHeight
.ascent
));
524 BRadioButton::_Redraw()
526 BRect
bounds(Bounds());
528 // fill background with ViewColor()
529 rgb_color highColor
= HighColor();
530 SetHighColor(ViewColor());
533 // restore previous HighColor()
534 SetHighColor(highColor
);