Make the dot a dynamic size and improve the rendering for anti-aliasing under Haiku. Confirmed to look good with different font sizes.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@12711 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2005-05-18 12:18:11 +00:00
parent 3eaf632305
commit c7694b4ac7
+84 -108
View File
@@ -1,26 +1,11 @@
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
// Copyright (c) 2001-2002, OpenBeOS // Copyright (c) 2001-2005, Haiku, Inc.
// //
// Permission is hereby granted, free of charge, to any person obtaining a // Distributed under the terms of the MIT license.
// 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 // File Name: RadioButton.cpp
// Author: Marc Flerackers ([email protected]) // Authors: Marc Flerackers ([email protected])
// Stephan Aßmus <[email protected]>
// Description: BRadioButton represents a single on/off button. All // Description: BRadioButton represents a single on/off button. All
// sibling BRadioButton objects comprise a single // sibling BRadioButton objects comprise a single
// "multiple choice" control. // "multiple choice" control.
@@ -81,20 +66,38 @@ status_t BRadioButton::Archive(BMessage *archive, bool deep) const
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
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; font_height fh;
GetFontHeight(&fh); GetFontHeight(&fh);
// If the focus is changing, just redraw the focus indicator // layout the rect for the dot
if (IsFocusChanging()) BRect rect(Bounds());
{
float h = 8.0f + (float)ceil(fh.ascent / 2.0f) + 2.0f;
// its size depends on the text height
float textHeight = floorf(fh.ascent + fh.descent + 0.5);
float inset = -floorf(textHeight / 2 - 2);
rect.left -= (inset - 1);
rect.top = floorf((rect.top + rect.bottom) / 2.0);
rect.bottom = rect.top;
rect.right = rect.left;
rect.InsetBy(inset, inset);
BPoint labelPos(rect.right + floorf(textHeight / 2.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 (IsFocusChanging()) {
if (IsFocus()) if (IsFocus())
SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR)); SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
else else
SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR)); SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
StrokeLine(BPoint(20.0f, h), BPoint(20.0f + StringWidth(Label()), h)); BPoint underLine = labelPos;
underLine.y += fh.descent;
StrokeLine(underLine, underLine + BPoint(StringWidth(Label()), 0.0));
return; return;
} }
@@ -109,13 +112,13 @@ void BRadioButton::Draw(BRect updateRect)
//darken4 = tint_color(no_tint, B_DARKEN_4_TINT), //darken4 = tint_color(no_tint, B_DARKEN_4_TINT),
darkenmax = tint_color(no_tint, B_DARKEN_MAX_TINT); darkenmax = tint_color(no_tint, B_DARKEN_MAX_TINT);
BRect rect(1.0, 3.0, 13.0, 15.0); // NOTE: this improves drawing a lot because of
// anti-aliased circles and arcs in Haiku
SetDrawingMode(B_OP_OVER);
if (IsEnabled()) if (IsEnabled()) {
{
// Dot // Dot
if (Value() == B_CONTROL_ON) if (Value() == B_CONTROL_ON) {
{
rgb_color kb_color = ui_color(B_KEYBOARD_NAVIGATION_COLOR); rgb_color kb_color = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT)); SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT));
@@ -124,26 +127,23 @@ void BRadioButton::Draw(BRect updateRect)
FillEllipse(BRect(rect.left + 3, rect.top + 3, rect.right - 4, rect.bottom - 4)); FillEllipse(BRect(rect.left + 3, rect.top + 3, rect.right - 4, rect.bottom - 4));
SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT)); SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT));
StrokeLine(BPoint(rect.right - 5, rect.bottom - 4), StrokeLine(BPoint(rect.right - 5, rect.bottom - 4),
BPoint(rect.right - 4, rect.bottom - 5)); BPoint(rect.right - 4, rect.bottom - 5));
SetHighColor(tint_color(kb_color, B_LIGHTEN_MAX_TINT)); SetHighColor(tint_color(kb_color, B_LIGHTEN_MAX_TINT));
StrokeLine(BPoint(rect.left + 4, rect.top + 5), StrokeLine(BPoint(rect.left + 4, rect.top + 5),
BPoint(rect.left + 5, rect.top + 4)); BPoint(rect.left + 5, rect.top + 4));
} } else {
else
{
SetHighColor(lightenmax); SetHighColor(lightenmax);
FillEllipse(rect); FillEllipse(rect);
} }
rect.InsetBy(-1.0, -1.0);
// Outer circle // Outer circle
if (fOutlined) if (fOutlined) {
{
SetHighColor(darken3); SetHighColor(darken3);
StrokeEllipse(rect); StrokeEllipse(rect);
} } else {
else
{
SetHighColor(darken1); SetHighColor(darken1);
StrokeArc(rect, 45.0f, 180.0f); StrokeArc(rect, 45.0f, 180.0f);
SetHighColor(lightenmax); SetHighColor(lightenmax);
@@ -155,35 +155,34 @@ void BRadioButton::Draw(BRect updateRect)
// Inner circle // Inner circle
SetHighColor(darken3); SetHighColor(darken3);
StrokeArc(rect, 45.0f, 180.0f); StrokeArc(rect, 45.0f, 180.0f);
StrokeLine(BPoint(rect.left + 1, rect.top + 1), // StrokeLine(BPoint(rect.left + 1, rect.top + 1),
BPoint(rect.left + 1, rect.top + 1)); // BPoint(rect.left + 1, rect.top + 1));
SetHighColor(no_tint); SetHighColor(no_tint);
StrokeArc(rect, 45.0f, -180.0f); StrokeArc(rect, 45.0f, -180.0f);
StrokeLine(BPoint(rect.left + 1, rect.bottom - 1), // StrokeLine(BPoint(rect.left + 1, rect.bottom - 1),
BPoint(rect.left + 1, rect.bottom - 1)); // BPoint(rect.left + 1, rect.bottom - 1));
StrokeLine(BPoint(rect.right - 1, rect.bottom - 1), // StrokeLine(BPoint(rect.right - 1, rect.bottom - 1),
BPoint(rect.right - 1, rect.bottom - 1)); // BPoint(rect.right - 1, rect.bottom - 1));
StrokeLine(BPoint(rect.right - 1, rect.top + 1), // StrokeLine(BPoint(rect.right - 1, rect.top + 1),
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 // Label
SetHighColor(darkenmax); SetHighColor(darkenmax);
DrawString(Label(), BPoint(20.0f, 8.0f + (float)ceil(fh.ascent / 2.0f))); DrawString(Label(), labelPos);
// Focus // Focus
if (IsFocus()) if (IsFocus()) {
{
float h = 8.0f + (float)ceil(fh.ascent / 2.0f) + 2.0f;
SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR)); SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR));
StrokeLine(BPoint(20.0f, h), BPoint(20.0f + StringWidth(Label()), h)); BPoint underLine = labelPos;
underLine.y += fh.descent;
StrokeLine(underLine, underLine + BPoint(StringWidth(Label()), 0.0));
} }
} } else {
else
{
// Dot // Dot
if (Value() == B_CONTROL_ON) if (Value() == B_CONTROL_ON) {
{
rgb_color kb_color = ui_color(B_KEYBOARD_NAVIGATION_COLOR); rgb_color kb_color = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
SetHighColor(tint_color(kb_color, B_LIGHTEN_2_TINT)); SetHighColor(tint_color(kb_color, B_LIGHTEN_2_TINT));
@@ -194,13 +193,13 @@ void BRadioButton::Draw(BRect updateRect)
SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT)); SetHighColor(tint_color(kb_color, B_DARKEN_3_TINT));
StrokeArc(BRect(rect.left + 2, rect.top + 2, rect.right - 2, rect.bottom - 2), StrokeArc(BRect(rect.left + 2, rect.top + 2, rect.right - 2, rect.bottom - 2),
45.0f, -180.0f); 45.0f, -180.0f);
} } else {
else
{
SetHighColor(lighten1); SetHighColor(lighten1);
FillEllipse(rect); FillEllipse(rect);
} }
rect.InsetBy(-1.0, -1.0);
// Outer circle // Outer circle
SetHighColor(no_tint); SetHighColor(no_tint);
StrokeArc(rect, 45.0f, 180.0f); StrokeArc(rect, 45.0f, 180.0f);
@@ -212,20 +211,20 @@ void BRadioButton::Draw(BRect updateRect)
// Inner circle // Inner circle
SetHighColor(darken2); SetHighColor(darken2);
StrokeArc(rect, 45.0f, 180.0f); StrokeArc(rect, 45.0f, 180.0f);
StrokeLine(BPoint(rect.left + 1, rect.top + 1), // StrokeLine(BPoint(rect.left + 1, rect.top + 1),
BPoint(rect.left + 1, rect.top + 1)); // BPoint(rect.left + 1, rect.top + 1));
SetHighColor(no_tint); SetHighColor(no_tint);
StrokeArc(rect, 45.0f, -180.0f); StrokeArc(rect, 45.0f, -180.0f);
StrokeLine(BPoint(rect.left + 1, rect.bottom - 1), // StrokeLine(BPoint(rect.left + 1, rect.bottom - 1),
BPoint(rect.left + 1, rect.bottom - 1)); // BPoint(rect.left + 1, rect.bottom - 1));
StrokeLine(BPoint(rect.right - 1, rect.bottom - 1), // StrokeLine(BPoint(rect.right - 1, rect.bottom - 1),
BPoint(rect.right - 1, rect.bottom - 1)); // BPoint(rect.right - 1, rect.bottom - 1));
StrokeLine(BPoint(rect.right - 1, rect.top + 1), // StrokeLine(BPoint(rect.right - 1, rect.top + 1),
BPoint(rect.right - 1, rect.top + 1)); // BPoint(rect.right - 1, rect.top + 1));
// Label // Label
SetHighColor(tint_color(no_tint, B_DISABLED_LABEL_TINT)); SetHighColor(tint_color(no_tint, B_DISABLED_LABEL_TINT));
DrawString(Label(), BPoint(20.0f, 8.0f + (float)ceil(fh.ascent / 2.0f))); DrawString(Label(), labelPos);
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
@@ -235,45 +234,39 @@ void BRadioButton::MouseDown(BPoint point)
return; return;
fOutlined = true; fOutlined = true;
Draw(Bounds());
Flush();
if (Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) if (Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) {
{ Invalidate();
SetTracking(true); SetTracking(true);
SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
} } else {
else Draw(Bounds());
{ Flush();
BRect bounds = Bounds(); BRect bounds = Bounds();
uint32 buttons; uint32 buttons;
do do {
{
snooze(40000); snooze(40000);
GetMouse(&point, &buttons, true); GetMouse(&point, &buttons, true);
bool inside = bounds.Contains(point); bool inside = bounds.Contains(point);
if (fOutlined != inside) if (fOutlined != inside) {
{
fOutlined = inside; fOutlined = inside;
Draw(Bounds()); Draw(Bounds());
Flush(); Flush();
} }
} while (buttons != 0); } while (buttons != 0);
if (fOutlined) if (fOutlined) {
{
fOutlined = false; fOutlined = false;
Draw(Bounds()); Draw(Bounds());
Flush(); Flush();
SetValue(B_CONTROL_ON); SetValue(B_CONTROL_ON);
Invoke(); Invoke();
} } else {
else
{
Draw(Bounds()); Draw(Bounds());
Flush(); Flush();
} }
@@ -404,28 +397,13 @@ void BRadioButton::MouseUp(BPoint point)
if (!IsTracking()) if (!IsTracking())
return; return;
bool inside = Bounds().Contains(point); fOutlined = Bounds().Contains(point);
if (fOutlined) {
if (fOutlined != inside)
{
fOutlined = inside;
Draw(Bounds());
Flush();
}
if (fOutlined)
{
fOutlined = false; fOutlined = false;
Draw(Bounds());
Flush();
SetValue(B_CONTROL_ON); SetValue(B_CONTROL_ON);
Invoke(); Invoke();
} }
else Invalidate();
{
Draw(Bounds());
Flush();
}
SetTracking(false); SetTracking(false);
} }
@@ -437,11 +415,9 @@ void BRadioButton::MouseMoved(BPoint point, uint32 transit, const BMessage *mess
bool inside = Bounds().Contains(point); bool inside = Bounds().Contains(point);
if (fOutlined != inside) if (fOutlined != inside) {
{
fOutlined = inside; fOutlined = inside;
Draw(Bounds()); Invalidate();
Flush();
} }
} }
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------