tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / kits / interface / RadioButton.cpp
blob6893ea49357267ff1af74ccddb6c6b0c90c7935d
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 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,
110 base, flags);
114 void
115 BRadioButton::MouseDown(BPoint where)
117 if (!IsEnabled())
118 return;
120 fOutlined = true;
122 if ((Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) != 0) {
123 Invalidate();
124 SetTracking(true);
125 SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
126 } else {
127 _Redraw();
129 BRect bounds = Bounds();
130 uint32 buttons;
132 do {
133 snooze(40000);
134 GetMouse(&where, &buttons, true);
135 bool inside = bounds.Contains(where);
137 if (fOutlined != inside) {
138 fOutlined = inside;
139 _Redraw();
141 } while (buttons != 0);
143 if (fOutlined) {
144 fOutlined = false;
145 _Redraw();
146 SetValue(B_CONTROL_ON);
147 Invoke();
148 } else
149 _Redraw();
154 void
155 BRadioButton::AttachedToWindow()
157 BControl::AttachedToWindow();
161 void
162 BRadioButton::KeyDown(const char* bytes, int32 numBytes)
164 // TODO: Add selecting the next button functionality (navigating radio
165 // buttons with the cursor keys)!
167 switch (bytes[0]) {
168 case B_RETURN:
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"
171 case B_SPACE: {
172 if (IsEnabled() && !Value()) {
173 SetValue(B_CONTROL_ON);
174 Invoke();
176 break;
179 default:
180 BControl::KeyDown(bytes, numBytes);
185 void
186 BRadioButton::SetValue(int32 value)
188 if (value != Value()) {
189 BControl::SetValueNoUpdate(value);
190 Invalidate(_KnobFrame());
193 if (value == 0)
194 return;
196 BView* parent = Parent();
197 BView* child = NULL;
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);
212 else
213 child = parent->ChildAt(0);
214 } else
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);
224 else {
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);
243 void
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);
254 if (icon != NULL) {
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));
267 if (_width != NULL)
268 *_width = width;
270 if (_height != NULL)
271 *_height = height;
275 void
276 BRadioButton::ResizeToPreferred()
278 BControl::ResizeToPreferred();
282 status_t
283 BRadioButton::Invoke(BMessage* message)
285 return BControl::Invoke(message);
289 void
290 BRadioButton::MessageReceived(BMessage* message)
292 BControl::MessageReceived(message);
296 void
297 BRadioButton::WindowActivated(bool active)
299 BControl::WindowActivated(active);
303 void
304 BRadioButton::MouseUp(BPoint where)
306 if (!IsTracking())
307 return;
309 fOutlined = Bounds().Contains(where);
310 if (fOutlined) {
311 fOutlined = false;
312 if (Value() != B_CONTROL_ON) {
313 SetValue(B_CONTROL_ON);
314 Invoke();
317 Invalidate();
319 SetTracking(false);
323 void
324 BRadioButton::MouseMoved(BPoint where, uint32 code,
325 const BMessage* dragMessage)
327 if (!IsTracking())
328 return;
330 bool inside = Bounds().Contains(where);
332 if (fOutlined != inside) {
333 fOutlined = inside;
334 Invalidate();
339 void
340 BRadioButton::DetachedFromWindow()
342 BControl::DetachedFromWindow();
346 void
347 BRadioButton::FrameMoved(BPoint newPosition)
349 BControl::FrameMoved(newPosition);
353 void
354 BRadioButton::FrameResized(float newWidth, float newHeight)
356 Invalidate();
357 BControl::FrameResized(newWidth, newHeight);
361 BHandler*
362 BRadioButton::ResolveSpecifier(BMessage* message, int32 index,
363 BMessage* specifier, int32 what, const char* property)
365 return BControl::ResolveSpecifier(message, index, specifier, what,
366 property);
370 void
371 BRadioButton::MakeFocus(bool focus)
373 BControl::MakeFocus(focus);
377 void
378 BRadioButton::AllAttached()
380 BControl::AllAttached();
384 void
385 BRadioButton::AllDetached()
387 BControl::AllDetached();
391 status_t
392 BRadioButton::GetSupportedSuites(BMessage* message)
394 return BControl::GetSupportedSuites(message);
398 status_t
399 BRadioButton::Perform(perform_code code, void* _data)
401 switch (code) {
402 case PERFORM_CODE_MIN_SIZE:
403 ((perform_data_min_size*)_data)->return_value
404 = BRadioButton::MinSize();
405 return B_OK;
407 case PERFORM_CODE_MAX_SIZE:
408 ((perform_data_max_size*)_data)->return_value
409 = BRadioButton::MaxSize();
410 return B_OK;
412 case PERFORM_CODE_PREFERRED_SIZE:
413 ((perform_data_preferred_size*)_data)->return_value
414 = BRadioButton::PreferredSize();
415 return B_OK;
417 case PERFORM_CODE_LAYOUT_ALIGNMENT:
418 ((perform_data_layout_alignment*)_data)->return_value
419 = BRadioButton::LayoutAlignment();
420 return B_OK;
422 case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH:
423 ((perform_data_has_height_for_width*)_data)->return_value
424 = BRadioButton::HasHeightForWidth();
425 return B_OK;
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,
432 &data->preferred);
433 return B_OK;
436 case PERFORM_CODE_SET_LAYOUT:
438 perform_data_set_layout* data = (perform_data_set_layout*)_data;
439 BRadioButton::SetLayout(data->layout);
440 return B_OK;
443 case PERFORM_CODE_LAYOUT_INVALIDATED:
445 perform_data_layout_invalidated* data
446 = (perform_data_layout_invalidated*)_data;
447 BRadioButton::LayoutInvalidated(data->descendants);
448 return B_OK;
451 case PERFORM_CODE_DO_LAYOUT:
453 BRadioButton::DoLayout();
454 return B_OK;
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);
468 BSize
469 BRadioButton::MaxSize()
471 float width, height;
472 GetPreferredSize(&width, &height);
474 return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
475 BSize(width, height));
479 BAlignment
480 BRadioButton::LayoutAlignment()
482 return BLayoutUtils::ComposeAlignment(ExplicitAlignment(),
483 BAlignment(B_ALIGN_LEFT, B_ALIGN_VERTICAL_UNSET));
487 status_t
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() {}
498 BRadioButton&
499 BRadioButton::operator=(const BRadioButton &)
501 return *this;
505 BRect
506 BRadioButton::_KnobFrame() const
508 font_height fontHeight;
509 GetFontHeight(&fontHeight);
510 return _KnobFrame(fontHeight);
514 BRect
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));
523 void
524 BRadioButton::_Redraw()
526 BRect bounds(Bounds());
528 // fill background with ViewColor()
529 rgb_color highColor = HighColor();
530 SetHighColor(ViewColor());
531 FillRect(bounds);
533 // restore previous HighColor()
534 SetHighColor(highColor);
535 Draw(bounds);
536 Flush();