BColorControl: Size text rect based on font size

* Also change kMinCellSize from a uint32 to a float so that it can be used
  with std::min() and std::max() instead of min_c() and max_c().
* Set the text controls sizes and margins based on the font size. Also rework
  _TextRectOffset() so that it will get the right spacing from by dividing the
  palette frame by 3.
* Replace bare numbers and refactor with calculation or magic constant.
* Create a private method _TextRectOffset() which calculates and
  returns the vertical text rect offset to use based on the font size.
* Replace 2.0 with new kBevelSpacing constant where appropriate.
* fPaletteFrame calculation in _LayoutView() was refactored but should
  not have changed.
This commit is contained in:
John Scipione
2013-06-10 19:15:07 -04:00
parent 1186916f06
commit 62fec205dd
2 changed files with 46 additions and 34 deletions
+1
View File
@@ -103,6 +103,7 @@ private:
BRect _PaletteSelectorFrame(uint8 colorIndex) const; BRect _PaletteSelectorFrame(uint8 colorIndex) const;
BRect _RampFrame(uint8 rampIndex) const; BRect _RampFrame(uint8 rampIndex) const;
void _SetCellSize(float size); void _SetCellSize(float size);
float _TextRectOffset();
private: private:
BRect fPaletteFrame; BRect fPaletteFrame;
+45 -34
View File
@@ -36,11 +36,13 @@ using BPrivate::gSystemCatalog;
#define B_TRANSLATION_CONTEXT "ColorControl" #define B_TRANSLATION_CONTEXT "ColorControl"
static const uint32 kMsgColorEntered = 'ccol'; static const uint32 kMsgColorEntered = 'ccol';
static const uint32 kMinCellSize = 6; static const float kMinCellSize = 6.0f;
static const float kSelectorPenSize = 2.0f; static const float kSelectorPenSize = 2.0f;
static const float kSelectorSize = 4.0f; static const float kSelectorSize = 4.0f;
static const float kSelectorHSpacing = 2.0f; static const float kSelectorHSpacing = 2.0f;
static const float kTextFieldsHSpacing = 6.0f; static const float kTextFieldsHSpacing = 6.0f;
static const float kDefaultFontSize = 12.0f;
static const float kBevelSpacing = 2.0f;
BColorControl::BColorControl(BPoint leftTop, color_control_layout layout, BColorControl::BColorControl(BPoint leftTop, color_control_layout layout,
@@ -108,14 +110,20 @@ BColorControl::_InitData(color_control_layout layout, float size,
SetValue(value); SetValue(value);
} else { } else {
BRect rect(0.0f, 0.0f, 70.0f, 15.0f); BRect textRect(0.0f, 0.0f, 0.0f, 0.0f);
float labelWidth = std::max(StringWidth(red), float labelWidth = std::max(StringWidth(red),
std::max(StringWidth(green), StringWidth(blue))) + 5; std::max(StringWidth(green), StringWidth(blue)))
rect.right = labelWidth + StringWidth("999") + 20; + kTextFieldsHSpacing;
textRect.right = labelWidth + StringWidth("999999");
// enough room for 3 digits plus 3 digits of padding
font_height fontHeight;
GetFontHeight(&fontHeight);
float labelHeight = fontHeight.ascent + fontHeight.descent;
textRect.bottom = labelHeight;
// red // red
fRedText = new BTextControl(rect, "_red", red, "0", fRedText = new BTextControl(textRect, "_red", red, "0",
new BMessage(kMsgColorEntered), B_FOLLOW_LEFT | B_FOLLOW_TOP, new BMessage(kMsgColorEntered), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE); B_WILL_DRAW | B_NAVIGABLE);
fRedText->SetDivider(labelWidth); fRedText->SetDivider(labelWidth);
@@ -126,12 +134,10 @@ BColorControl::_InitData(color_control_layout layout, float size,
fRedText->TextView()->AllowChar(i); fRedText->TextView()->AllowChar(i);
fRedText->TextView()->SetMaxBytes(3); fRedText->TextView()->SetMaxBytes(3);
float offset = fRedText->Bounds().Height() + 2.0f;
// green // green
rect.OffsetBy(0, offset); textRect.OffsetBy(0, _TextRectOffset());
fGreenText = new BTextControl(rect, "_green", green, "0", fGreenText = new BTextControl(textRect, "_green", green, "0",
new BMessage(kMsgColorEntered), B_FOLLOW_LEFT | B_FOLLOW_TOP, new BMessage(kMsgColorEntered), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE); B_WILL_DRAW | B_NAVIGABLE);
fGreenText->SetDivider(labelWidth); fGreenText->SetDivider(labelWidth);
@@ -144,8 +150,8 @@ BColorControl::_InitData(color_control_layout layout, float size,
// blue // blue
rect.OffsetBy(0, offset); textRect.OffsetBy(0, _TextRectOffset());
fBlueText = new BTextControl(rect, "_blue", blue, "0", fBlueText = new BTextControl(textRect, "_blue", blue, "0",
new BMessage(kMsgColorEntered), B_FOLLOW_LEFT | B_FOLLOW_TOP, new BMessage(kMsgColorEntered), B_FOLLOW_LEFT | B_FOLLOW_TOP,
B_WILL_DRAW | B_NAVIGABLE); B_WILL_DRAW | B_NAVIGABLE);
fBlueText->SetDivider(labelWidth); fBlueText->SetDivider(labelWidth);
@@ -165,7 +171,7 @@ BColorControl::_InitData(color_control_layout layout, float size,
if (useOffscreen) { if (useOffscreen) {
BRect bounds = fPaletteFrame; BRect bounds = fPaletteFrame;
bounds.InsetBy(-2.0f, -2.0f); bounds.InsetBy(-kBevelSpacing, -kBevelSpacing);
fBitmap = new BBitmap(bounds, B_RGB32, true, false); fBitmap = new BBitmap(bounds, B_RGB32, true, false);
fOffscreenView = new BView(bounds, "off_view", 0, 0); fOffscreenView = new BView(bounds, "off_view", 0, 0);
@@ -183,30 +189,24 @@ BColorControl::_InitData(color_control_layout layout, float size,
void void
BColorControl::_LayoutView() BColorControl::_LayoutView()
{ {
if (fPaletteMode) { fPaletteFrame.Set(0, 0, fColumns * fCellSize, fRows * fCellSize);
fPaletteFrame.Set(2.0f, 2.0f, fPaletteFrame.OffsetBy(kBevelSpacing, kBevelSpacing);
float(fColumns) * fCellSize + 2.0, if (!fPaletteMode) {
float(fRows) * fCellSize + 2.0); // Reduce the inner space by 1 pixel so that the frame
} else { // is exactly rows * cellsize pixels in height
fPaletteFrame.Set(2.0f, 2.0f, fPaletteFrame.bottom -= 1;
float(fColumns) * fCellSize + 2.0,
float(fRows) * fCellSize + 2.0 - 1.0);
// 1 pixel adjust so that the inner space
// has exactly rows * cellsize pixels in height
} }
BRect rect = fPaletteFrame.InsetByCopy(-2.0, -2.0); BRect rect = fPaletteFrame.InsetByCopy(-kBevelSpacing, -kBevelSpacing);
// bevel // frame not including bevel
if (rect.Height() < fBlueText->Frame().bottom) { if (rect.Height() < fBlueText->Frame().bottom)
// adjust the height to fit
rect.bottom = fBlueText->Frame().bottom; rect.bottom = fBlueText->Frame().bottom;
}
float offset = floor(rect.bottom / 4); float offset = floorf(rect.bottom / 4);
float y = offset; float y = offset;
if (offset < fRedText->Bounds().Height() + 2) { if (offset < _TextRectOffset()) {
offset = fRedText->Bounds().Height() + 2; offset = _TextRectOffset();
y = 0; y = 0;
} }
@@ -417,7 +417,7 @@ BColorControl::Draw(BRect updateRect)
void void
BColorControl::_DrawColorArea(BView* target, BRect updateRect) BColorControl::_DrawColorArea(BView* target, BRect updateRect)
{ {
BRect bevelRect = fPaletteFrame.InsetByCopy(-2.0, -2.0); BRect bevelRect = fPaletteFrame.InsetByCopy(-kBevelSpacing, -kBevelSpacing);
bool enabled = IsEnabled(); bool enabled = IsEnabled();
rgb_color noTint = ui_color(B_PANEL_BACKGROUND_COLOR); rgb_color noTint = ui_color(B_PANEL_BACKGROUND_COLOR);
@@ -615,7 +615,18 @@ BColorControl::_RampFrame(uint8 rampIndex) const
void void
BColorControl::_SetCellSize(float size) BColorControl::_SetCellSize(float size)
{ {
fCellSize = ceilf(max_c(kMinCellSize, size)); BFont font;
GetFont(&font);
fCellSize = std::max(kMinCellSize,
ceilf(size * font.Size() / kDefaultFontSize));
}
float
BColorControl::_TextRectOffset()
{
return std::max(fRedText->Bounds().Height(),
ceilf(_PaletteFrame().Height() / 3));
} }
@@ -635,7 +646,7 @@ BColorControl::_InitOffscreen()
{ {
if (fBitmap->Lock()) { if (fBitmap->Lock()) {
_DrawColorArea(fOffscreenView, _DrawColorArea(fOffscreenView,
fPaletteFrame.InsetByCopy(-2.0f, -2.0f)); fPaletteFrame.InsetByCopy(-kBevelSpacing, -kBevelSpacing));
fOffscreenView->Sync(); fOffscreenView->Sync();
fBitmap->Unlock(); fBitmap->Unlock();
} }
@@ -841,7 +852,7 @@ BColorControl::DetachedFromWindow()
void void
BColorControl::GetPreferredSize(float* _width, float* _height) BColorControl::GetPreferredSize(float* _width, float* _height)
{ {
BRect rect = fPaletteFrame.InsetByCopy(-2.0, -2.0); BRect rect = fPaletteFrame.InsetByCopy(-kBevelSpacing, -kBevelSpacing);
// bevel // bevel
if (rect.Height() < fBlueText->Frame().bottom) { if (rect.Height() < fBlueText->Frame().bottom) {