* the text rect of the inner text view is now the same size as its bounds;
that's simplifies resizing and moving a lot (it's now working correctly). * the inner text view is now only navigable if the text control should have been navigable. * default text view inset is now (3, 3) - but this might be adapted by the total size of the control. * the height of the control now takes the label into account - it may differ from the height of the text line. * instead of computing the size again, the BTextControl constructor now just uses its GetPreferredSize(). * the label position now depends on the position of the text view. * the inner text view is now centered vertically inside the BTextControl. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14865 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
@@ -1,28 +1,15 @@
|
|||||||
//------------------------------------------------------------------------------
|
/*
|
||||||
// Copyright (c) 2001-2004, Haiku, Inc.
|
* Copyright 2001-2005, Haiku Inc.
|
||||||
//
|
* Distributed under the terms of the MIT License.
|
||||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
*
|
||||||
// copy of this software and associated documentation files (the "Software"),
|
* Authors:
|
||||||
// to deal in the Software without restriction, including without limitation
|
* Frans van Nispen ([email protected])
|
||||||
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
* Stephan Aßmus <[email protected]>
|
||||||
// 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:
|
|
||||||
//
|
/** BTextControl displays text that can act like a control. */
|
||||||
// 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: TextControl.cpp
|
|
||||||
// Author: Frans van Nispen ([email protected])
|
|
||||||
// Description: BTextControl displays text that can act like a control.
|
|
||||||
//------------------------------------------------------------------------------
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
#include <Message.h>
|
#include <Message.h>
|
||||||
@@ -35,16 +22,18 @@
|
|||||||
BTextControl::BTextControl(BRect frame, const char *name, const char *label,
|
BTextControl::BTextControl(BRect frame, const char *name, const char *label,
|
||||||
const char *text, BMessage *message, uint32 mask,
|
const char *text, BMessage *message, uint32 mask,
|
||||||
uint32 flags)
|
uint32 flags)
|
||||||
: BControl(frame, name, label, message, mask, flags | B_FRAME_EVENTS)
|
: BControl(frame, name, label, message, mask, flags | B_FRAME_EVENTS)
|
||||||
{
|
{
|
||||||
InitData(label, text);
|
InitData(label, text);
|
||||||
|
|
||||||
|
float height;
|
||||||
|
GetPreferredSize(NULL, &height);
|
||||||
|
|
||||||
|
ResizeTo(Bounds().Width(), height);
|
||||||
|
|
||||||
float lineHeight = fText->LineHeight(0);
|
float lineHeight = fText->LineHeight(0);
|
||||||
|
fText->ResizeTo(fText->Bounds().Width(), lineHeight);
|
||||||
ResizeTo(Bounds().Width(), lineHeight + 8);
|
fText->MoveTo(fText->Frame().left, (height - lineHeight) / 2);
|
||||||
|
|
||||||
fText->ResizeTo(fText->Bounds().Width(), lineHeight + 4);
|
|
||||||
fText->MoveTo(fText->Frame().left, 2.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -221,15 +210,15 @@ BTextControl::Divider() const
|
|||||||
void
|
void
|
||||||
BTextControl::Draw(BRect updateRect)
|
BTextControl::Draw(BRect updateRect)
|
||||||
{
|
{
|
||||||
rgb_color no_tint = ui_color(B_PANEL_BACKGROUND_COLOR),
|
rgb_color noTint = ui_color(B_PANEL_BACKGROUND_COLOR),
|
||||||
lighten1 = tint_color(no_tint, B_LIGHTEN_1_TINT),
|
lighten1 = tint_color(noTint, B_LIGHTEN_1_TINT),
|
||||||
lighten2 = tint_color(no_tint, B_LIGHTEN_2_TINT),
|
lighten2 = tint_color(noTint, B_LIGHTEN_2_TINT),
|
||||||
lightenmax = tint_color(no_tint, B_LIGHTEN_MAX_TINT),
|
lightenMax = tint_color(noTint, B_LIGHTEN_MAX_TINT),
|
||||||
darken1 = tint_color(no_tint, B_DARKEN_1_TINT),
|
darken1 = tint_color(noTint, B_DARKEN_1_TINT),
|
||||||
darken2 = tint_color(no_tint, B_DARKEN_2_TINT),
|
darken2 = tint_color(noTint, B_DARKEN_2_TINT),
|
||||||
darken4 = tint_color(no_tint, B_DARKEN_4_TINT),
|
darken4 = tint_color(noTint, B_DARKEN_4_TINT),
|
||||||
darkenmax = tint_color(no_tint, B_DARKEN_MAX_TINT),
|
darkenMax = tint_color(noTint, B_DARKEN_MAX_TINT),
|
||||||
nav = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
|
navigationColor = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
|
||||||
|
|
||||||
bool enabled = IsEnabled();
|
bool enabled = IsEnabled();
|
||||||
bool active = false;
|
bool active = false;
|
||||||
@@ -241,9 +230,8 @@ BTextControl::Draw(BRect updateRect)
|
|||||||
rect.InsetBy(-1.0f, -1.0f);
|
rect.InsetBy(-1.0f, -1.0f);
|
||||||
|
|
||||||
if (active) {
|
if (active) {
|
||||||
SetHighColor(nav);
|
SetHighColor(navigationColor);
|
||||||
StrokeRect(rect);
|
StrokeRect(rect);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if (enabled)
|
if (enabled)
|
||||||
SetHighColor(darken4);
|
SetHighColor(darken4);
|
||||||
@@ -253,7 +241,7 @@ BTextControl::Draw(BRect updateRect)
|
|||||||
StrokeLine(rect.LeftTop(), rect.LeftBottom());
|
StrokeLine(rect.LeftTop(), rect.LeftBottom());
|
||||||
StrokeLine(rect.LeftTop(), rect.RightTop());
|
StrokeLine(rect.LeftTop(), rect.RightTop());
|
||||||
|
|
||||||
SetHighColor(no_tint);
|
SetHighColor(noTint);
|
||||||
StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
|
StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
|
||||||
StrokeLine(BPoint(rect.right, rect.top + 1.0f));
|
StrokeLine(BPoint(rect.right, rect.top + 1.0f));
|
||||||
}
|
}
|
||||||
@@ -263,7 +251,7 @@ BTextControl::Draw(BRect updateRect)
|
|||||||
if (enabled)
|
if (enabled)
|
||||||
SetHighColor(darken1);
|
SetHighColor(darken1);
|
||||||
else
|
else
|
||||||
SetHighColor(no_tint);
|
SetHighColor(noTint);
|
||||||
|
|
||||||
StrokeLine(rect.LeftBottom(), rect.LeftTop());
|
StrokeLine(rect.LeftBottom(), rect.LeftTop());
|
||||||
StrokeLine(rect.RightTop());
|
StrokeLine(rect.RightTop());
|
||||||
@@ -277,10 +265,10 @@ BTextControl::Draw(BRect updateRect)
|
|||||||
StrokeLine(BPoint(rect.right, rect.top + 1.0f), rect.RightBottom());
|
StrokeLine(BPoint(rect.right, rect.top + 1.0f), rect.RightBottom());
|
||||||
|
|
||||||
if (Label()) {
|
if (Label()) {
|
||||||
font_height fh;
|
font_height fontHeight;
|
||||||
GetFontHeight(&fh);
|
GetFontHeight(&fontHeight);
|
||||||
|
|
||||||
float y = (float)ceil(fh.ascent + fh.descent + fh.leading) + 2.0f;
|
float y = fontHeight.ascent + fText->Frame().top + 1;
|
||||||
float x;
|
float x;
|
||||||
|
|
||||||
switch (fLabelAlign) {
|
switch (fLabelAlign) {
|
||||||
@@ -296,9 +284,9 @@ BTextControl::Draw(BRect updateRect)
|
|||||||
x = 3.0f;
|
x = 3.0f;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
|
SetHighColor(IsEnabled() ? darkenMax
|
||||||
IsEnabled() ? B_DARKEN_MAX_TINT : B_DISABLED_LABEL_TINT));
|
: tint_color(ui_color(B_PANEL_BACKGROUND_COLOR), B_DISABLED_LABEL_TINT));
|
||||||
DrawString(Label(), BPoint(x, y));
|
DrawString(Label(), BPoint(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -401,23 +389,34 @@ BTextControl::SetEnabled(bool state)
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
BTextControl::GetPreferredSize(float *width, float *height)
|
BTextControl::GetPreferredSize(float *_width, float *_height)
|
||||||
{
|
{
|
||||||
if (height)
|
if (_height) {
|
||||||
*height = fText->LineHeight(0) + 8.0f;
|
// we need enough space for the label and the child text view
|
||||||
|
font_height fontHeight;
|
||||||
|
GetFontHeight(&fontHeight);
|
||||||
|
float labelHeight = ceil(fontHeight.ascent + fontHeight.descent
|
||||||
|
+ fontHeight.leading);
|
||||||
|
float textHeight = fText->LineHeight(0) + 4.0;
|
||||||
|
|
||||||
// TODO: this one I need to find out
|
*_height = max_c(labelHeight, textHeight);
|
||||||
if (width)
|
}
|
||||||
*width = 4.0f + ceilf(StringWidth(Label())) * 2.0f;
|
|
||||||
|
if (_width) {
|
||||||
|
// TODO: this one I need to find out
|
||||||
|
float width = 20.0f + ceilf(StringWidth(Label()));
|
||||||
|
if (width < Bounds().Width())
|
||||||
|
width = Bounds().Width();
|
||||||
|
*_width = width;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
BTextControl::ResizeToPreferred()
|
BTextControl::ResizeToPreferred()
|
||||||
{
|
{
|
||||||
float width, height;
|
// TODO: change divider?
|
||||||
GetPreferredSize(&width, &height);
|
BView::ResizeToPreferred();
|
||||||
BView::ResizeTo(width, height);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -601,7 +600,8 @@ BTextControl::InitData(const char *label, const char *initial_text,
|
|||||||
if (label)
|
if (label)
|
||||||
fDivider = floorf(bounds.Width() / 2.0f);
|
fDivider = floorf(bounds.Width() / 2.0f);
|
||||||
|
|
||||||
if (Flags() & B_NAVIGABLE) {
|
uint32 navigableFlags = Flags() & B_NAVIGABLE;
|
||||||
|
if (navigableFlags != 0) {
|
||||||
fSkipSetFlags = true;
|
fSkipSetFlags = true;
|
||||||
SetFlags(Flags() & ~B_NAVIGABLE);
|
SetFlags(Flags() & ~B_NAVIGABLE);
|
||||||
fSkipSetFlags = false;
|
fSkipSetFlags = false;
|
||||||
@@ -612,19 +612,16 @@ BTextControl::InitData(const char *label, const char *initial_text,
|
|||||||
else {
|
else {
|
||||||
BRect frame(fDivider, bounds.top,
|
BRect frame(fDivider, bounds.top,
|
||||||
bounds.right, bounds.bottom);
|
bounds.right, bounds.bottom);
|
||||||
// we are stroking the frame arround the text view, which
|
// we are stroking the frame around the text view, which
|
||||||
// is 2 pixels wide
|
// is 2 pixels wide
|
||||||
frame.InsetBy(2.0, 2.0);
|
frame.InsetBy(3.0, 3.0);
|
||||||
|
|
||||||
BRect textRect(frame.OffsetToCopy(0.0f, 0.0f));
|
BRect textRect(frame.OffsetToCopy(0.0f, 0.0f));
|
||||||
textRect.InsetBy(2.0, 2.0);
|
|
||||||
|
|
||||||
fText = new _BTextInput_(frame, textRect,
|
|
||||||
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP,
|
|
||||||
B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE);
|
|
||||||
|
|
||||||
|
fText = new _BTextInput_(frame, textRect,
|
||||||
|
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP,
|
||||||
|
B_WILL_DRAW | B_FRAME_EVENTS | navigableFlags);
|
||||||
AddChild(fText);
|
AddChild(fText);
|
||||||
|
|
||||||
SetText(initial_text);
|
SetText(initial_text);
|
||||||
fText->SetAlignment(B_ALIGN_LEFT);
|
fText->SetAlignment(B_ALIGN_LEFT);
|
||||||
fText->AlignTextRect();
|
fText->AlignTextRect();
|
||||||
|
|||||||
@@ -84,12 +84,8 @@ void
|
|||||||
_BTextInput_::FrameResized(float width, float height)
|
_BTextInput_::FrameResized(float width, float height)
|
||||||
{
|
{
|
||||||
BTextView::FrameResized(width, height);
|
BTextView::FrameResized(width, height);
|
||||||
|
|
||||||
AlignTextRect();
|
AlignTextRect();
|
||||||
// TODO: just to get something working, it wouldn't be correct for
|
|
||||||
// scrolled views
|
|
||||||
BRect textRect(Bounds());
|
|
||||||
textRect.InsetBy(2.0, 2.0);
|
|
||||||
SetTextRect(textRect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -174,7 +170,10 @@ _BTextInput_::MakeFocus(bool state)
|
|||||||
void
|
void
|
||||||
_BTextInput_::AlignTextRect()
|
_BTextInput_::AlignTextRect()
|
||||||
{
|
{
|
||||||
|
// TODO: just to get something working, it wouldn't be correct for
|
||||||
|
// scrolled views
|
||||||
|
BRect textRect(Bounds());
|
||||||
|
SetTextRect(textRect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user