* Updated the coding style.

* The layout friendly constructors don't need to mess with the control size.
* The layout friendly constructors can use the respective BControl constructor.
* Refactored some duplicated code.
* Removed duplicated GetFontHeight() calls.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@27585 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2008-09-16 15:08:02 +00:00
parent 349c911ee9
commit 43f8c6143a
2 changed files with 117 additions and 165 deletions
+57 -101
View File
@@ -1,129 +1,85 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright (c) 2001-2008, Haiku Inc. All rights reserved.
// * Distributed under the terms of the MIT/X11 license.
// Permission is hereby granted, free of charge, to any person obtaining a */
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// File Name: RadioButton.cpp
// Author: Marc Flerackers ([email protected])
// Description: BRadioButton represents a single on/off button. All
// sibling BRadioButton objects comprise a single
// "multiple choice" control.
//------------------------------------------------------------------------------
#ifndef _RADIO_BUTTON_H #ifndef _RADIO_BUTTON_H
#define _RADIO_BUTTON_H #define _RADIO_BUTTON_H
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
#include <BeBuild.h> #include <BeBuild.h>
#include <Control.h> #include <Control.h>
#include <Bitmap.h> #include <Bitmap.h>
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
// BRadioButton class ----------------------------------------------------------
class BRadioButton : public BControl { class BRadioButton : public BControl {
public: public:
BRadioButton(BRect frame, BRadioButton(BRect frame, const char* name,
const char *name, const char* label, BMessage* message,
const char *label, uint32 resizMask
BMessage *message, = B_FOLLOW_LEFT | B_FOLLOW_TOP,
uint32 resizMask = B_FOLLOW_LEFT | B_FOLLOW_TOP, uint32 flags = B_WILL_DRAW | B_NAVIGABLE);
uint32 flags = B_WILL_DRAW | B_NAVIGABLE); BRadioButton(const char* name,
BRadioButton(const char *name, const char* label, BMessage* message,
const char *label, uint32 flags = B_WILL_DRAW | B_NAVIGABLE);
BMessage *message, BRadioButton(const char* label,
uint32 flags = B_WILL_DRAW | B_NAVIGABLE); BMessage* message);
BRadioButton(const char *label,
BMessage *message);
BRadioButton(BMessage *archive); BRadioButton(BMessage* archive);
virtual ~BRadioButton(); virtual ~BRadioButton();
static BArchivable *Instantiate(BMessage *archive); static BArchivable* Instantiate(BMessage* archive);
virtual status_t Archive(BMessage *archive, bool deep = true) const; virtual status_t Archive(BMessage* archive,
bool deep = true) const;
virtual void Draw(BRect updateRect); virtual void Draw(BRect updateRect);
virtual void MouseDown(BPoint point); virtual void MouseDown(BPoint point);
virtual void AttachedToWindow(); virtual void AttachedToWindow();
virtual void KeyDown(const char *bytes, int32 numBytes); virtual void KeyDown(const char* bytes, int32 numBytes);
virtual void SetValue(int32 value); virtual void SetValue(int32 value);
virtual void GetPreferredSize(float *width, float *height); virtual void GetPreferredSize(float* _width, float* _height);
virtual void ResizeToPreferred(); virtual void ResizeToPreferred();
virtual status_t Invoke(BMessage *message = NULL); virtual status_t Invoke(BMessage* message = NULL);
virtual void MessageReceived(BMessage *message); virtual void MessageReceived(BMessage* message);
virtual void WindowActivated(bool active); virtual void WindowActivated(bool active);
virtual void MouseUp(BPoint point); virtual void MouseUp(BPoint point);
virtual void MouseMoved(BPoint point, uint32 transit, const BMessage *message); virtual void MouseMoved(BPoint point, uint32 transit,
virtual void DetachedFromWindow(); const BMessage* dragMessage);
virtual void FrameMoved(BPoint newLocation); virtual void DetachedFromWindow();
virtual void FrameResized(float width, float height); virtual void FrameMoved(BPoint newLocation);
virtual void FrameResized(float width, float height);
virtual BHandler *ResolveSpecifier(BMessage *message, virtual BHandler* ResolveSpecifier(BMessage* message,
int32 index, int32 index, BMessage* specifier,
BMessage *specifier, int32 what, const char* property);
int32 what,
const char *property);
virtual void MakeFocus(bool focused = true); virtual void MakeFocus(bool focused = true);
virtual void AllAttached(); virtual void AllAttached();
virtual void AllDetached(); virtual void AllDetached();
virtual status_t GetSupportedSuites(BMessage *message); virtual status_t GetSupportedSuites(BMessage* message);
virtual status_t Perform(perform_code d, void *arg); virtual status_t Perform(perform_code d, void* argument);
virtual BSize MaxSize(); virtual BSize MaxSize();
private: private:
friend status_t _init_interface_kit_(); friend status_t _init_interface_kit_();
virtual void _ReservedRadioButton1(); virtual void _ReservedRadioButton1();
virtual void _ReservedRadioButton2(); virtual void _ReservedRadioButton2();
BRadioButton &operator=(const BRadioButton &); BRadioButton& operator=(const BRadioButton& other);
BRect _KnobFrame() const; BRect _KnobFrame() const;
// for use in "synchronous" BWindows BRect _KnobFrame(const font_height& fontHeight) const;
void _Redraw(); void _Redraw();
// for use in "synchronous" BWindows
static BBitmap *sBitmaps[2][3]; private:
static BBitmap* sBitmaps[2][3];
bool fOutlined; bool fOutlined;
uint32 _reserved[2];
uint32 _reserved[2];
}; };
//------------------------------------------------------------------------------
#endif // _RADIO_BUTTON_H #endif // _RADIO_BUTTON_H
/*
* $Log $
*
* $Id $
*
*/
+60 -64
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2006, Haiku, Inc. * Copyright 2001-2008, Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT license. * Distributed under the terms of the MIT license.
* *
* Authors: * Authors:
@@ -21,53 +21,38 @@
#include <Window.h> #include <Window.h>
BRadioButton::BRadioButton(BRect frame, const char* name, const char* label,
BRadioButton::BRadioButton(BRect frame, const char *name, const char *label, BMessage* message, uint32 resizMask, uint32 flags)
BMessage *message, uint32 resizMask, uint32 flags) : BControl(frame, name, label, message, resizMask, flags | B_FRAME_EVENTS),
: BControl(frame, name, label, message, resizMask, flags), fOutlined(false)
fOutlined(false)
{ {
// Resize to minimum height if needed // Resize to minimum height if needed for BeOS compatibility
font_height fontHeight; float minHeight;
GetFontHeight(&fontHeight); GetPreferredSize(NULL, &minHeight);
float minHeight = ceilf(6.0f + fontHeight.ascent + fontHeight.descent);
if (Bounds().Height() < minHeight) if (Bounds().Height() < minHeight)
ResizeTo(Bounds().Width(), minHeight); ResizeTo(Bounds().Width(), minHeight);
} }
BRadioButton::BRadioButton(const char *name, const char *label, BRadioButton::BRadioButton(const char* name, const char* label,
BMessage *message, uint32 flags) BMessage* message, uint32 flags)
: BControl(BRect(0, 0, -1, -1), name, label, message, B_FOLLOW_NONE, : BControl(name, label, message, flags | B_FRAME_EVENTS),
flags | B_SUPPORTS_LAYOUT), fOutlined(false)
fOutlined(false)
{ {
// Resize to minimum height if needed
font_height fontHeight;
GetFontHeight(&fontHeight);
float minHeight = ceilf(6.0f + fontHeight.ascent + fontHeight.descent);
if (Bounds().Height() < minHeight)
ResizeTo(Bounds().Width(), minHeight);
} }
BRadioButton::BRadioButton(const char *label, BMessage *message) BRadioButton::BRadioButton(const char* label, BMessage* message)
: BControl(BRect(0, 0, -1, -1), NULL, label, message, B_FOLLOW_NONE, : BControl(NULL, label, message,
B_WILL_DRAW | B_NAVIGABLE | B_SUPPORTS_LAYOUT), B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS),
fOutlined(false) fOutlined(false)
{ {
// Resize to minimum height if needed
font_height fontHeight;
GetFontHeight(&fontHeight);
float minHeight = ceilf(6.0f + fontHeight.ascent + fontHeight.descent);
if (Bounds().Height() < minHeight)
ResizeTo(Bounds().Width(), minHeight);
} }
BRadioButton::BRadioButton(BMessage *archive) BRadioButton::BRadioButton(BMessage* archive)
: BControl(archive), : BControl(archive),
fOutlined(false) fOutlined(false)
{ {
} }
@@ -78,7 +63,7 @@ BRadioButton::~BRadioButton()
BArchivable* BArchivable*
BRadioButton::Instantiate(BMessage *archive) BRadioButton::Instantiate(BMessage* archive)
{ {
if (validate_instantiation(archive, "BRadioButton")) if (validate_instantiation(archive, "BRadioButton"))
return new BRadioButton(archive); return new BRadioButton(archive);
@@ -88,7 +73,7 @@ BRadioButton::Instantiate(BMessage *archive)
status_t status_t
BRadioButton::Archive(BMessage *archive, bool deep) const BRadioButton::Archive(BMessage* archive, bool deep) const
{ {
return BControl::Archive(archive, deep); return BControl::Archive(archive, deep);
} }
@@ -97,16 +82,17 @@ BRadioButton::Archive(BMessage *archive, bool deep) const
void void
BRadioButton::Draw(BRect updateRect) BRadioButton::Draw(BRect updateRect)
{ {
// layout the rect for the dot
BRect rect = _KnobFrame();
// its size depends on the text height // its size depends on the text height
font_height fontHeight; font_height fontHeight;
GetFontHeight(&fontHeight); GetFontHeight(&fontHeight);
float textHeight = ceilf(fontHeight.ascent + fontHeight.descent); float textHeight = ceilf(fontHeight.ascent + fontHeight.descent);
// layout the rect for the dot
BRect rect = _KnobFrame(fontHeight);
BPoint labelPos(rect.right + floorf(textHeight / 2.0), BPoint labelPos(rect.right + floorf(textHeight / 2.0),
floorf((rect.top + rect.bottom + textHeight) / 2.0 - fontHeight.descent + 0.5) + 1.0); floorf((rect.top + rect.bottom + textHeight) / 2.0
- fontHeight.descent + 0.5) + 1.0);
// if the focus is changing, just redraw the focus indicator // if the focus is changing, just redraw the focus indicator
if (IsFocusChanging()) { if (IsFocusChanging()) {
@@ -157,7 +143,8 @@ BRadioButton::Draw(BRect updateRect)
knob = tint_color(naviColor, B_LIGHTEN_2_TINT); knob = tint_color(naviColor, B_LIGHTEN_2_TINT);
knobDark = tint_color(naviColor, B_LIGHTEN_1_TINT); knobDark = tint_color(naviColor, B_LIGHTEN_1_TINT);
knobLight = tint_color(naviColor, (B_LIGHTEN_2_TINT + B_LIGHTEN_MAX_TINT) / 2.0); knobLight = tint_color(naviColor, (B_LIGHTEN_2_TINT
+ B_LIGHTEN_MAX_TINT) / 2.0);
} }
// dot // dot
@@ -167,10 +154,12 @@ BRadioButton::Draw(BRect updateRect)
FillEllipse(rect); FillEllipse(rect);
SetHighColor(knob); SetHighColor(knob);
FillEllipse(BRect(rect.left + 2, rect.top + 2, rect.right - 3, rect.bottom - 3)); FillEllipse(BRect(rect.left + 2, rect.top + 2, rect.right - 3,
rect.bottom - 3));
SetHighColor(knobLight); SetHighColor(knobLight);
FillEllipse(BRect(rect.left + 3, rect.top + 3, rect.right - 5, rect.bottom - 5)); FillEllipse(BRect(rect.left + 3, rect.top + 3, rect.right - 5,
rect.bottom - 5));
} else { } else {
// empty // empty
SetHighColor(lightenmax); SetHighColor(lightenmax);
@@ -186,18 +175,18 @@ BRadioButton::Draw(BRect updateRect)
StrokeEllipse(rect); StrokeEllipse(rect);
} else { } else {
SetHighColor(darken1); SetHighColor(darken1);
StrokeArc(rect, 45.0f, 180.0f); StrokeArc(rect, 45.0, 180.0);
SetHighColor(lightenmax); SetHighColor(lightenmax);
StrokeArc(rect, 45.0f, -180.0f); StrokeArc(rect, 45.0, -180.0);
} }
rect.InsetBy(1, 1); rect.InsetBy(1, 1);
// inner circle // inner circle
SetHighColor(darken3); SetHighColor(darken3);
StrokeArc(rect, 45.0f, 180.0f); StrokeArc(rect, 45.0, 180.0);
SetHighColor(bg); SetHighColor(bg);
StrokeArc(rect, 45.0f, -180.0f); StrokeArc(rect, 45.0, -180.0);
// for faster font rendering, we can restore B_OP_COPY // for faster font rendering, we can restore B_OP_COPY
SetDrawingMode(B_OP_COPY); SetDrawingMode(B_OP_COPY);
@@ -267,9 +256,10 @@ BRadioButton::AttachedToWindow()
void void
BRadioButton::KeyDown(const char *bytes, int32 numBytes) BRadioButton::KeyDown(const char* bytes, int32 numBytes)
{ {
// TODO add select_next_button functionality // TODO: Add selecting the next button functionality (navigating radio
// buttons with the cursor keys)!
switch (bytes[0]) { switch (bytes[0]) {
case B_RETURN: case B_RETURN:
@@ -300,18 +290,18 @@ BRadioButton::SetValue(int32 value)
if (!value) if (!value)
return; return;
BView *parent = Parent(); BView* parent = Parent();
BView *child = NULL; BView* child = NULL;
if (parent) { if (parent) {
// If the parent is a BBox, the group parent is the parent of the BBox // If the parent is a BBox, the group parent is the parent of the BBox
BBox *box = dynamic_cast<BBox*>(parent); BBox* box = dynamic_cast<BBox*>(parent);
if (box && box->LabelView() == this) if (box && box->LabelView() == this)
parent = box->Parent(); parent = box->Parent();
if (parent) { if (parent) {
BBox *box = dynamic_cast<BBox*>(parent); BBox* box = dynamic_cast<BBox*>(parent);
// If the parent is a BBox, skip the label if there is one // If the parent is a BBox, skip the label if there is one
if (box && box->LabelView()) if (box && box->LabelView())
@@ -324,13 +314,13 @@ BRadioButton::SetValue(int32 value)
child = Window()->ChildAt(0); child = Window()->ChildAt(0);
while (child) { while (child) {
BRadioButton *radio = dynamic_cast<BRadioButton*>(child); BRadioButton* radio = dynamic_cast<BRadioButton*>(child);
if (radio && (radio != this)) if (radio && (radio != this))
radio->SetValue(B_CONTROL_OFF); radio->SetValue(B_CONTROL_OFF);
else { else {
// If the child is a BBox, check if the label is a radiobutton // If the child is a BBox, check if the label is a radiobutton
BBox *box = dynamic_cast<BBox*>(child); BBox* box = dynamic_cast<BBox*>(child);
if (box && box->LabelView()) { if (box && box->LabelView()) {
radio = dynamic_cast<BRadioButton*>(box->LabelView()); radio = dynamic_cast<BRadioButton*>(box->LabelView());
@@ -354,7 +344,7 @@ BRadioButton::GetPreferredSize(float* _width, float* _height)
GetFontHeight(&fontHeight); GetFontHeight(&fontHeight);
if (_width) { if (_width) {
BRect rect = _KnobFrame(); BRect rect = _KnobFrame(fontHeight);
float width = rect.right + floorf(ceilf(fontHeight.ascent float width = rect.right + floorf(ceilf(fontHeight.ascent
+ fontHeight.descent) / 2.0); + fontHeight.descent) / 2.0);
@@ -365,7 +355,7 @@ BRadioButton::GetPreferredSize(float* _width, float* _height)
} }
if (_height) if (_height)
*_height = ceilf(fontHeight.ascent + fontHeight.descent) + 6.0f; *_height = ceilf(fontHeight.ascent + fontHeight.descent) + 6.0;
} }
@@ -377,14 +367,14 @@ BRadioButton::ResizeToPreferred()
status_t status_t
BRadioButton::Invoke(BMessage *message) BRadioButton::Invoke(BMessage* message)
{ {
return BControl::Invoke(message); return BControl::Invoke(message);
} }
void void
BRadioButton::MessageReceived(BMessage *message) BRadioButton::MessageReceived(BMessage* message)
{ {
BControl::MessageReceived(message); BControl::MessageReceived(message);
} }
@@ -416,7 +406,7 @@ BRadioButton::MouseUp(BPoint point)
void void
BRadioButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message) BRadioButton::MouseMoved(BPoint point, uint32 transit, const BMessage* message)
{ {
if (!IsTracking()) if (!IsTracking())
return; return;
@@ -447,14 +437,14 @@ BRadioButton::FrameMoved(BPoint newLocation)
void void
BRadioButton::FrameResized(float width, float height) BRadioButton::FrameResized(float width, float height)
{ {
Invalidate();
BControl::FrameResized(width, height); BControl::FrameResized(width, height);
} }
BHandler* BHandler*
BRadioButton::ResolveSpecifier(BMessage *message, int32 index, BRadioButton::ResolveSpecifier(BMessage* message, int32 index,
BMessage *specifier, int32 what, BMessage* specifier, int32 what, const char* property)
const char *property)
{ {
return BControl::ResolveSpecifier(message, index, specifier, what, return BControl::ResolveSpecifier(message, index, specifier, what,
property); property);
@@ -483,14 +473,14 @@ BRadioButton::AllDetached()
status_t status_t
BRadioButton::GetSupportedSuites(BMessage *message) BRadioButton::GetSupportedSuites(BMessage* message)
{ {
return BControl::GetSupportedSuites(message); return BControl::GetSupportedSuites(message);
} }
status_t status_t
BRadioButton::Perform(perform_code d, void *arg) BRadioButton::Perform(perform_code d, void* arg)
{ {
return BControl::Perform(d, arg); return BControl::Perform(d, arg);
} }
@@ -525,7 +515,13 @@ BRadioButton::_KnobFrame() const
{ {
font_height fontHeight; font_height fontHeight;
GetFontHeight(&fontHeight); GetFontHeight(&fontHeight);
return _KnobFrame(fontHeight);
}
BRect
BRadioButton::_KnobFrame(const font_height& fontHeight) const
{
// layout the rect for the dot // layout the rect for the dot
BRect rect(Bounds()); BRect rect(Bounds());