btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / kits / interface / RadioButton.cpp
blob046983340a01a0f61d23d2d9045436d897122b5d
1 /*
2 * Copyright 2001-2008, Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT license.
5 * Authors:
6 * Marc Flerackers (mflerackers@androme.be)
7 * Stephan Aßmus <superstippi@gmx.de>
8 */
11 #include <RadioButton.h>
13 #include <algorithm>
15 #include <Box.h>
16 #include <ControlLook.h>
17 #include <Debug.h>
18 #include <LayoutUtils.h>
19 #include <Window.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),
28 fOutlined(false)
30 // Resize to minimum height if needed for BeOS compatibility
31 float minHeight;
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),
42 fOutlined(false)
47 BRadioButton::BRadioButton(const char* label, BMessage* message)
49 BControl(NULL, label, message, B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS),
50 fOutlined(false)
55 BRadioButton::BRadioButton(BMessage* data)
57 BControl(data),
58 fOutlined(false)
63 BRadioButton::~BRadioButton()
68 BArchivable*
69 BRadioButton::Instantiate(BMessage* data)
71 if (validate_instantiation(data, "BRadioButton"))
72 return new BRadioButton(data);
74 return NULL;
78 status_t
79 BRadioButton::Archive(BMessage* data, bool deep) const
81 return BControl::Archive(data, deep);
85 void
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);
95 if (fOutlined)
96 flags |= BControlLook::B_CLICKED;
98 BRect knobRect(_KnobFrame(fontHeight));
99 BRect rect(knobRect);
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,
114 base, flags);
118 void
119 BRadioButton::MouseDown(BPoint where)
121 if (!IsEnabled())
122 return;
124 fOutlined = true;
126 if ((Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) != 0) {
127 Invalidate();
128 SetTracking(true);
129 SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
130 } else {
131 _Redraw();
133 BRect bounds = Bounds();
134 uint32 buttons;
136 do {
137 snooze(40000);
138 GetMouse(&where, &buttons, true);
139 bool inside = bounds.Contains(where);
141 if (fOutlined != inside) {
142 fOutlined = inside;
143 _Redraw();
145 } while (buttons != 0);
147 if (fOutlined) {
148 fOutlined = false;
149 _Redraw();
150 SetValue(B_CONTROL_ON);
151 Invoke();
152 } else
153 _Redraw();
158 void
159 BRadioButton::AttachedToWindow()
161 BControl::AttachedToWindow();
165 void
166 BRadioButton::KeyDown(const char* bytes, int32 numBytes)
168 // TODO: Add selecting the next button functionality (navigating radio
169 // buttons with the cursor keys)!
171 switch (bytes[0]) {
172 case B_RETURN:
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"
175 case B_SPACE: {
176 if (IsEnabled() && !Value()) {
177 SetValue(B_CONTROL_ON);
178 Invoke();
180 break;
183 default:
184 BControl::KeyDown(bytes, numBytes);
189 void
190 BRadioButton::SetValue(int32 value)
192 if (value != Value()) {
193 BControl::SetValueNoUpdate(value);
194 Invalidate(_KnobFrame());
197 if (value == 0)
198 return;
200 BView* parent = Parent();
201 BView* child = NULL;
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);
216 else
217 child = parent->ChildAt(0);
218 } else
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);
228 else {
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);
247 void
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);
258 if (icon != NULL) {
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));
271 if (_width != NULL)
272 *_width = width;
274 if (_height != NULL)
275 *_height = height;
279 void
280 BRadioButton::ResizeToPreferred()
282 BControl::ResizeToPreferred();
286 status_t
287 BRadioButton::Invoke(BMessage* message)
289 return BControl::Invoke(message);
293 void
294 BRadioButton::MessageReceived(BMessage* message)
296 BControl::MessageReceived(message);
300 void
301 BRadioButton::WindowActivated(bool active)
303 BControl::WindowActivated(active);
307 void
308 BRadioButton::MouseUp(BPoint where)
310 if (!IsTracking())
311 return;
313 fOutlined = Bounds().Contains(where);
314 if (fOutlined) {
315 fOutlined = false;
316 if (Value() != B_CONTROL_ON) {
317 SetValue(B_CONTROL_ON);
318 Invoke();
321 Invalidate();
323 SetTracking(false);
327 void
328 BRadioButton::MouseMoved(BPoint where, uint32 code,
329 const BMessage* dragMessage)
331 if (!IsTracking())
332 return;
334 bool inside = Bounds().Contains(where);
336 if (fOutlined != inside) {
337 fOutlined = inside;
338 Invalidate();
343 void
344 BRadioButton::DetachedFromWindow()
346 BControl::DetachedFromWindow();
350 void
351 BRadioButton::FrameMoved(BPoint newPosition)
353 BControl::FrameMoved(newPosition);
357 void
358 BRadioButton::FrameResized(float newWidth, float newHeight)
360 Invalidate();
361 BControl::FrameResized(newWidth, newHeight);
365 BHandler*
366 BRadioButton::ResolveSpecifier(BMessage* message, int32 index,
367 BMessage* specifier, int32 what, const char* property)
369 return BControl::ResolveSpecifier(message, index, specifier, what,
370 property);
374 void
375 BRadioButton::MakeFocus(bool focus)
377 BControl::MakeFocus(focus);
381 void
382 BRadioButton::AllAttached()
384 BControl::AllAttached();
388 void
389 BRadioButton::AllDetached()
391 BControl::AllDetached();
395 status_t
396 BRadioButton::GetSupportedSuites(BMessage* message)
398 return BControl::GetSupportedSuites(message);
402 status_t
403 BRadioButton::Perform(perform_code code, void* _data)
405 switch (code) {
406 case PERFORM_CODE_MIN_SIZE:
407 ((perform_data_min_size*)_data)->return_value
408 = BRadioButton::MinSize();
409 return B_OK;
411 case PERFORM_CODE_MAX_SIZE:
412 ((perform_data_max_size*)_data)->return_value
413 = BRadioButton::MaxSize();
414 return B_OK;
416 case PERFORM_CODE_PREFERRED_SIZE:
417 ((perform_data_preferred_size*)_data)->return_value
418 = BRadioButton::PreferredSize();
419 return B_OK;
421 case PERFORM_CODE_LAYOUT_ALIGNMENT:
422 ((perform_data_layout_alignment*)_data)->return_value
423 = BRadioButton::LayoutAlignment();
424 return B_OK;
426 case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH:
427 ((perform_data_has_height_for_width*)_data)->return_value
428 = BRadioButton::HasHeightForWidth();
429 return B_OK;
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,
436 &data->preferred);
437 return B_OK;
440 case PERFORM_CODE_SET_LAYOUT:
442 perform_data_set_layout* data = (perform_data_set_layout*)_data;
443 BRadioButton::SetLayout(data->layout);
444 return B_OK;
447 case PERFORM_CODE_LAYOUT_INVALIDATED:
449 perform_data_layout_invalidated* data
450 = (perform_data_layout_invalidated*)_data;
451 BRadioButton::LayoutInvalidated(data->descendants);
452 return B_OK;
455 case PERFORM_CODE_DO_LAYOUT:
457 BRadioButton::DoLayout();
458 return B_OK;
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);
472 BSize
473 BRadioButton::MaxSize()
475 float width, height;
476 GetPreferredSize(&width, &height);
478 return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
479 BSize(width, height));
483 BAlignment
484 BRadioButton::LayoutAlignment()
486 return BLayoutUtils::ComposeAlignment(ExplicitAlignment(),
487 BAlignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_UNSET));
491 status_t
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() {}
502 BRadioButton&
503 BRadioButton::operator=(const BRadioButton &)
505 return *this;
509 BRect
510 BRadioButton::_KnobFrame() const
512 font_height fontHeight;
513 GetFontHeight(&fontHeight);
514 return _KnobFrame(fontHeight);
518 BRect
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));
527 void
528 BRadioButton::_Redraw()
530 BRect bounds(Bounds());
532 // fill background with ViewColor()
533 rgb_color highColor = HighColor();
534 SetHighColor(ViewColor());
535 FillRect(bounds);
537 // restore previous HighColor()
538 SetHighColor(highColor);
539 Draw(bounds);
540 Flush();