improved look and applied style guide, fixed redrawing in BWindows with synchronous controls, it wouldn't have mattered if Haiku didn't have anti-aliasing, override B_RETURN on keydown to disallow turning the control off

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14715 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2005-11-05 16:46:36 +00:00
parent e1ab68c612
commit 5ac57aebfb
2 changed files with 236 additions and 229 deletions
+2
View File
@@ -100,6 +100,8 @@ virtual void _ReservedRadioButton2();
BRadioButton &operator=(const BRadioButton &); BRadioButton &operator=(const BRadioButton &);
BRect _KnobFrame() const; BRect _KnobFrame() const;
// for use in "synchronous" BWindows
void _Redraw();
static BBitmap *sBitmaps[2][3]; static BBitmap *sBitmaps[2][3];
+234 -229
View File
@@ -1,33 +1,24 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2005, Haiku, Inc. * Copyright 2001-2005, Haiku, Inc.
// * Distributed under the terms of the MIT license.
// Distributed under the terms of the MIT license. *
// * Authors:
// File Name: RadioButton.cpp * Marc Flerackers ([email protected])
// Authors: Marc Flerackers ([email protected]) * Stephan Aßmus <[email protected]>
// Stephan Aßmus <[email protected]> *
// Description: BRadioButton represents a single on/off button. All * Description:
// sibling BRadioButton objects comprise a single * BRadioButton represents a single on/off button.
// "multiple choice" control. * All sibling BRadioButton objects comprise a single
//------------------------------------------------------------------------------ * "multiple choice" control.
*/
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
#include <RadioButton.h>
#include <Errors.h>
#include <Box.h> #include <Box.h>
#include <Errors.h>
#include <Window.h> #include <Window.h>
// Project Includes ------------------------------------------------------------ #include <RadioButton.h>
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
//------------------------------------------------------------------------------
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), : BControl(frame, name, label, message, resizMask, flags),
@@ -40,48 +31,52 @@ BRadioButton::BRadioButton(BRect frame, const char *name, const char *label,
if (Bounds().Height() < minHeight) if (Bounds().Height() < minHeight)
ResizeTo(Bounds().Width(), minHeight); ResizeTo(Bounds().Width(), minHeight);
} }
//------------------------------------------------------------------------------
BRadioButton::BRadioButton(BMessage *archive) BRadioButton::BRadioButton(BMessage *archive)
: BControl(archive), : BControl(archive),
fOutlined(false) fOutlined(false)
{ {
} }
//------------------------------------------------------------------------------
BRadioButton::~BRadioButton() BRadioButton::~BRadioButton()
{ {
} }
//------------------------------------------------------------------------------
BArchivable *BRadioButton::Instantiate(BMessage *archive)
BArchivable*
BRadioButton::Instantiate(BMessage *archive)
{ {
if (validate_instantiation(archive, "BRadioButton")) if (validate_instantiation(archive, "BRadioButton"))
return new BRadioButton(archive); return new BRadioButton(archive);
else else
return NULL; return NULL;
} }
//------------------------------------------------------------------------------
status_t BRadioButton::Archive(BMessage *archive, bool deep) const
status_t
BRadioButton::Archive(BMessage *archive, bool deep) const
{ {
return BControl::Archive(archive, deep); return BControl::Archive(archive, deep);
} }
//------------------------------------------------------------------------------
void BRadioButton::Draw(BRect updateRect)
void
BRadioButton::Draw(BRect updateRect)
{ {
// NOTE: the commented out StrokeLine() calls appearently tweak the
// rendering on R5, but they are unnecessary under Haiku
font_height fh;
GetFontHeight(&fh);
// layout the rect for the dot // layout the rect for the dot
BRect rect = _KnobFrame(); BRect rect = _KnobFrame();
// its size depends on the text height // its size depends on the text height
font_height fh;
GetFontHeight(&fh);
float textHeight = floorf(fh.ascent + fh.descent + 0.5); float textHeight = floorf(fh.ascent + fh.descent + 0.5);
BPoint labelPos(rect.right + floorf(textHeight / 2.0), BPoint labelPos(rect.right + floorf(textHeight / 2.0),
floorf((rect.top + rect.bottom + textHeight) / 2.0 - fh.descent + 0.5) + 1.0); floorf((rect.top + rect.bottom + textHeight) / 2.0 - fh.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()) {
if (IsFocus()) if (IsFocus())
SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR)); SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
@@ -95,133 +90,106 @@ void BRadioButton::Draw(BRect updateRect)
return; return;
} }
// Placeholder until sBitmaps is filled in // colors
rgb_color no_tint = ui_color(B_PANEL_BACKGROUND_COLOR), rgb_color bg = ui_color(B_PANEL_BACKGROUND_COLOR);
lighten1 = tint_color(no_tint, B_LIGHTEN_1_TINT), rgb_color lightenmax;
lightenmax = tint_color(no_tint, B_LIGHTEN_MAX_TINT), rgb_color lighten1;
darken1 = tint_color(no_tint, B_DARKEN_1_TINT), rgb_color darken1;
darken2 = tint_color(no_tint, B_DARKEN_2_TINT), rgb_color darken2;
darken3 = tint_color(no_tint, B_DARKEN_3_TINT), rgb_color darken3;
//darken4 = tint_color(no_tint, B_DARKEN_4_TINT), rgb_color darkenmax;
darkenmax = tint_color(no_tint, B_DARKEN_MAX_TINT);
rgb_color naviColor = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
rgb_color knob;
rgb_color knobDark;
rgb_color knobLight;
if (IsEnabled()) {
lightenmax = tint_color(bg, B_LIGHTEN_MAX_TINT);
lighten1 = tint_color(bg, B_LIGHTEN_1_TINT);
darken1 = tint_color(bg, B_DARKEN_1_TINT);
darken2 = tint_color(bg, B_DARKEN_2_TINT);
darken3 = tint_color(bg, B_DARKEN_3_TINT);
darkenmax = tint_color(bg, B_DARKEN_MAX_TINT);
knob = naviColor;
knobDark = tint_color(naviColor, B_DARKEN_3_TINT);
knobLight = tint_color(naviColor, 0.15);
} else {
lightenmax = tint_color(bg, B_LIGHTEN_2_TINT);
lighten1 = bg;
darken1 = bg;
darken2 = tint_color(bg, B_DARKEN_1_TINT);
darken3 = tint_color(bg, B_DARKEN_2_TINT);
darkenmax = tint_color(bg, B_DISABLED_LABEL_TINT);
knob = tint_color(naviColor, B_LIGHTEN_2_TINT);
knobDark = tint_color(naviColor, B_LIGHTEN_1_TINT);
knobLight = tint_color(naviColor, (B_LIGHTEN_2_TINT + B_LIGHTEN_MAX_TINT) / 2.0);
}
// NOTE: this improves drawing a lot because of // NOTE: this improves drawing a lot because of
// anti-aliased circles and arcs in Haiku // anti-aliased circles and arcs in Haiku
SetDrawingMode(B_OP_OVER); SetDrawingMode(B_OP_OVER);
if (IsEnabled()) { // dot
// Dot if (Value() == B_CONTROL_ON) {
if (Value() == B_CONTROL_ON) { // full
rgb_color kb_color = ui_color(B_KEYBOARD_NAVIGATION_COLOR); SetHighColor(knobDark);
FillEllipse(rect);
SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT)); SetHighColor(knob);
FillEllipse(rect); FillEllipse(BRect(rect.left + 2, rect.top + 2, rect.right - 2, rect.bottom - 2));
SetHighColor(kb_color);
FillEllipse(BRect(rect.left + 3, rect.top + 3, rect.right - 4, rect.bottom - 4));
SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT));
StrokeLine(BPoint(rect.right - 5, rect.bottom - 4),
BPoint(rect.right - 4, rect.bottom - 5));
SetHighColor(tint_color(kb_color, B_LIGHTEN_MAX_TINT));
StrokeLine(BPoint(rect.left + 4, rect.top + 5),
BPoint(rect.left + 5, rect.top + 4));
} else { SetHighColor(knobLight);
SetHighColor(lightenmax); FillEllipse(BRect(rect.left + 3, rect.top + 3, rect.right - 4, rect.bottom - 4));
FillEllipse(rect);
}
rect.InsetBy(-1.0, -1.0);
// Outer circle
if (fOutlined) {
SetHighColor(darken3);
StrokeEllipse(rect);
} else {
SetHighColor(darken1);
StrokeArc(rect, 45.0f, 180.0f);
SetHighColor(lightenmax);
StrokeArc(rect, 45.0f, -180.0f);
}
rect.InsetBy(1, 1);
// Inner circle
SetHighColor(darken3);
StrokeArc(rect, 45.0f, 180.0f);
// StrokeLine(BPoint(rect.left + 1, rect.top + 1),
// BPoint(rect.left + 1, rect.top + 1));
SetHighColor(no_tint);
StrokeArc(rect, 45.0f, -180.0f);
// StrokeLine(BPoint(rect.left + 1, rect.bottom - 1),
// BPoint(rect.left + 1, rect.bottom - 1));
// StrokeLine(BPoint(rect.right - 1, rect.bottom - 1),
// BPoint(rect.right - 1, rect.bottom - 1));
// StrokeLine(BPoint(rect.right - 1, rect.top + 1),
// BPoint(rect.right - 1, rect.top + 1));
// For faster font rendering, we can restore B_OP_COPY
SetDrawingMode(B_OP_COPY);
// Label
SetHighColor(darkenmax);
DrawString(Label(), labelPos);
// Focus
if (IsFocus()) {
SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
BPoint underLine = labelPos;
underLine.y += fh.descent;
StrokeLine(underLine, underLine + BPoint(StringWidth(Label()), 0.0));
}
} else { } else {
// Dot // empty
if (Value() == B_CONTROL_ON) { SetHighColor(lightenmax);
rgb_color kb_color = ui_color(B_KEYBOARD_NAVIGATION_COLOR); FillEllipse(rect);
}
SetHighColor(tint_color(kb_color, B_LIGHTEN_2_TINT)); rect.InsetBy(-1.0, -1.0);
FillEllipse(rect);
SetHighColor(tint_color(kb_color, B_LIGHTEN_MAX_TINT));
StrokeLine(BPoint(rect.left + 4, rect.top + 5),
BPoint(rect.left + 5, rect.top + 4));
SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT));
StrokeArc(BRect(rect.left + 2, rect.top + 2, rect.right - 2, rect.bottom - 2),
45.0f, -180.0f);
} else {
SetHighColor(lighten1);
FillEllipse(rect);
}
rect.InsetBy(-1.0, -1.0); // outer circle
if (fOutlined) {
// Outer circle // indicating "about to change value"
SetHighColor(no_tint); SetHighColor(darken3);
StrokeEllipse(rect);
} else {
SetHighColor(darken1);
StrokeArc(rect, 45.0f, 180.0f); StrokeArc(rect, 45.0f, 180.0f);
SetHighColor(lighten1); SetHighColor(lightenmax);
StrokeArc(rect, 45.0f, -180.0f); StrokeArc(rect, 45.0f, -180.0f);
}
rect.InsetBy(1, 1); rect.InsetBy(1, 1);
// Inner circle
SetHighColor(darken2);
StrokeArc(rect, 45.0f, 180.0f);
// StrokeLine(BPoint(rect.left + 1, rect.top + 1),
// BPoint(rect.left + 1, rect.top + 1));
SetHighColor(no_tint);
StrokeArc(rect, 45.0f, -180.0f);
// StrokeLine(BPoint(rect.left + 1, rect.bottom - 1),
// BPoint(rect.left + 1, rect.bottom - 1));
// StrokeLine(BPoint(rect.right - 1, rect.bottom - 1),
// BPoint(rect.right - 1, rect.bottom - 1));
// StrokeLine(BPoint(rect.right - 1, rect.top + 1),
// BPoint(rect.right - 1, rect.top + 1));
// Label // inner circle
SetHighColor(tint_color(no_tint, B_DISABLED_LABEL_TINT)); SetHighColor(darken3);
DrawString(Label(), labelPos); StrokeArc(rect, 45.0f, 180.0f);
SetHighColor(bg);
StrokeArc(rect, 45.0f, -180.0f);
// for faster font rendering, we can restore B_OP_COPY
SetDrawingMode(B_OP_COPY);
// label
SetHighColor(darkenmax);
DrawString(Label(), labelPos);
// underline label if focused
if (IsFocus()) {
SetHighColor(naviColor);
BPoint underLine = labelPos;
underLine.y += fh.descent;
StrokeLine(underLine, underLine + BPoint(StringWidth(Label()), 0.0));
} }
} }
//------------------------------------------------------------------------------
void BRadioButton::MouseDown(BPoint point)
void
BRadioButton::MouseDown(BPoint point)
{ {
if (!IsEnabled()) if (!IsEnabled())
return; return;
@@ -233,8 +201,7 @@ void BRadioButton::MouseDown(BPoint point)
SetTracking(true); SetTracking(true);
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
} else { } else {
Draw(Bounds()); _Redraw();
Flush();
BRect bounds = Bounds(); BRect bounds = Bounds();
uint32 buttons; uint32 buttons;
@@ -248,40 +215,40 @@ void BRadioButton::MouseDown(BPoint point)
if (fOutlined != inside) { if (fOutlined != inside) {
fOutlined = inside; fOutlined = inside;
Draw(Bounds()); _Redraw();
Flush();
} }
} while (buttons != 0); } while (buttons != 0);
if (fOutlined) { if (fOutlined) {
fOutlined = false; fOutlined = false;
Draw(Bounds()); _Redraw();
Flush();
SetValue(B_CONTROL_ON); SetValue(B_CONTROL_ON);
Invoke(); Invoke();
} else { } else {
Draw(Bounds()); _Redraw();
Flush();
} }
} }
} }
//------------------------------------------------------------------------------
void BRadioButton::AttachedToWindow()
void
BRadioButton::AttachedToWindow()
{ {
BControl::AttachedToWindow(); BControl::AttachedToWindow();
} }
//------------------------------------------------------------------------------
void BRadioButton::KeyDown(const char *bytes, int32 numBytes)
void
BRadioButton::KeyDown(const char *bytes, int32 numBytes)
{ {
// TODO add select_next_button functionality // TODO add select_next_button functionality
switch (bytes[0]) switch (bytes[0]) {
{ case B_RETURN:
case ' ': // override B_RETURN, which BControl would use to toggle the value
{ // but we don't allow setting the control to "off", only "on"
if (IsEnabled() && !Value()) case B_SPACE: {
{ if (IsEnabled() && !Value()) {
SetValue(B_CONTROL_ON); SetValue(B_CONTROL_ON);
Invoke(); Invoke();
} }
@@ -292,8 +259,10 @@ void BRadioButton::KeyDown(const char *bytes, int32 numBytes)
BControl::KeyDown(bytes, numBytes); BControl::KeyDown(bytes, numBytes);
} }
} }
//------------------------------------------------------------------------------
void BRadioButton::SetValue(int32 value)
void
BRadioButton::SetValue(int32 value)
{ {
if (value != Value()) { if (value != Value()) {
BControl::SetValue(value); BControl::SetValue(value);
@@ -306,16 +275,14 @@ void BRadioButton::SetValue(int32 value)
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
@@ -323,26 +290,21 @@ void BRadioButton::SetValue(int32 value)
child = parent->ChildAt(1); child = parent->ChildAt(1);
else else
child = parent->ChildAt(0); child = parent->ChildAt(0);
} } else
else
child = Window()->ChildAt(0); child = Window()->ChildAt(0);
} } else if (Window())
else if (Window())
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 (child != this && radio) if (child != this && radio)
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());
if (radio) if (radio)
@@ -353,8 +315,10 @@ void BRadioButton::SetValue(int32 value)
child = child->NextSibling(); child = child->NextSibling();
} }
} }
//------------------------------------------------------------------------------
void BRadioButton::GetPreferredSize(float *width, float *height)
void
BRadioButton::GetPreferredSize(float *width, float *height)
{ {
font_height fh; font_height fh;
GetFontHeight(&fh); GetFontHeight(&fh);
@@ -367,28 +331,38 @@ void BRadioButton::GetPreferredSize(float *width, float *height)
*width = (float)ceil(*width); *width = (float)ceil(*width);
} }
//------------------------------------------------------------------------------
void BRadioButton::ResizeToPreferred()
void
BRadioButton::ResizeToPreferred()
{ {
BControl::ResizeToPreferred(); BControl::ResizeToPreferred();
} }
//------------------------------------------------------------------------------
status_t BRadioButton::Invoke(BMessage *message)
status_t
BRadioButton::Invoke(BMessage *message)
{ {
return BControl::Invoke(message); return BControl::Invoke(message);
} }
//------------------------------------------------------------------------------
void BRadioButton::MessageReceived(BMessage *message)
void
BRadioButton::MessageReceived(BMessage *message)
{ {
BControl::MessageReceived(message); BControl::MessageReceived(message);
} }
//------------------------------------------------------------------------------
void BRadioButton::WindowActivated(bool active)
void
BRadioButton::WindowActivated(bool active)
{ {
BControl::WindowActivated(active); BControl::WindowActivated(active);
} }
//------------------------------------------------------------------------------
void BRadioButton::MouseUp(BPoint point)
void
BRadioButton::MouseUp(BPoint point)
{ {
if (!IsTracking()) if (!IsTracking())
return; return;
@@ -403,8 +377,10 @@ void BRadioButton::MouseUp(BPoint point)
SetTracking(false); SetTracking(false);
} }
//------------------------------------------------------------------------------
void BRadioButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
void
BRadioButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
{ {
if (!IsTracking()) if (!IsTracking())
return; return;
@@ -416,63 +392,85 @@ void BRadioButton::MouseMoved(BPoint point, uint32 transit, const BMessage *mess
Invalidate(); Invalidate();
} }
} }
//------------------------------------------------------------------------------
void BRadioButton::DetachedFromWindow()
void
BRadioButton::DetachedFromWindow()
{ {
BControl::DetachedFromWindow(); BControl::DetachedFromWindow();
} }
//------------------------------------------------------------------------------
void BRadioButton::FrameMoved(BPoint newLocation)
void
BRadioButton::FrameMoved(BPoint newLocation)
{ {
BControl::FrameMoved(newLocation); BControl::FrameMoved(newLocation);
} }
//------------------------------------------------------------------------------
void BRadioButton::FrameResized(float width, float height)
void
BRadioButton::FrameResized(float width, float height)
{ {
BControl::FrameResized(width, height); BControl::FrameResized(width, height);
} }
//------------------------------------------------------------------------------
BHandler *BRadioButton::ResolveSpecifier(BMessage *message, int32 index,
BMessage *specifier, int32 what, BHandler*
const char *property) BRadioButton::ResolveSpecifier(BMessage *message, int32 index,
BMessage *specifier, int32 what,
const char *property)
{ {
return BControl::ResolveSpecifier(message, index, specifier, what, return BControl::ResolveSpecifier(message, index, specifier, what,
property); property);
} }
//------------------------------------------------------------------------------
void BRadioButton::MakeFocus(bool focused)
void
BRadioButton::MakeFocus(bool focused)
{ {
BControl::MakeFocus(focused); BControl::MakeFocus(focused);
} }
//------------------------------------------------------------------------------
void BRadioButton::AllAttached()
void
BRadioButton::AllAttached()
{ {
BControl::AllAttached(); BControl::AllAttached();
} }
//------------------------------------------------------------------------------
void BRadioButton::AllDetached()
void
BRadioButton::AllDetached()
{ {
BControl::AllDetached(); BControl::AllDetached();
} }
//------------------------------------------------------------------------------
status_t BRadioButton::GetSupportedSuites(BMessage *message)
status_t
BRadioButton::GetSupportedSuites(BMessage *message)
{ {
return BControl::GetSupportedSuites(message); return BControl::GetSupportedSuites(message);
} }
//------------------------------------------------------------------------------
status_t BRadioButton::Perform(perform_code d, void *arg)
status_t
BRadioButton::Perform(perform_code d, void *arg)
{ {
return BControl::Perform(d, arg); return BControl::Perform(d, arg);
} }
//------------------------------------------------------------------------------
void BRadioButton::_ReservedRadioButton1() {} void BRadioButton::_ReservedRadioButton1() {}
void BRadioButton::_ReservedRadioButton2() {} void BRadioButton::_ReservedRadioButton2() {}
//------------------------------------------------------------------------------
BRadioButton &BRadioButton::operator=(const BRadioButton &)
BRadioButton&
BRadioButton::operator=(const BRadioButton &)
{ {
return *this; return *this;
} }
//------------------------------------------------------------------------------
BRect BRect
BRadioButton::_KnobFrame() const BRadioButton::_KnobFrame() const
{ {
@@ -496,9 +494,16 @@ BRadioButton::_KnobFrame() const
} }
/* void
* $Log $ BRadioButton::_Redraw()
* {
* $Id $ BRect b(Bounds());
* // fill background with ViewColor()
*/ rgb_color highColor = HighColor();
SetHighColor(ViewColor());
FillRect(b);
// restore previous HighColor()
SetHighColor(highColor);
Draw(b);
Flush();
}