Files
haiku-beta6/src/kits/interface/TextControl.cpp
T

992 lines
18 KiB
C++
Raw Normal View History

/*
* Copyright 2001-2008, Haiku Inc.
* Distributed under the terms of the MIT License.
*
* Authors:
* Frans van Nispen ([email protected])
* Stephan Aßmus <[email protected]>
2006-08-26 16:21:15 +00:00
* Ingo Weinhold <[email protected]>
*/
/*! BTextControl displays text that can act like a control. */
2002-07-09 12:24:59 +00:00
#include <stdio.h>
2006-08-26 16:21:15 +00:00
#include <AbstractLayoutItem.h>
#include <LayoutUtils.h>
2004-10-18 10:21:46 +00:00
#include <Message.h>
#include <Region.h>
2002-07-09 12:24:59 +00:00
#include <TextControl.h>
#include <Window.h>
#include "TextInput.h"
2008-09-15 19:43:18 +00:00
//#define TRACE_TEXT_CONTROL
#ifdef TRACE_TEXT_CONTROL
# include <FunctionTracer.h>
static int32 sFunctionDepth = -1;
# define CALLED(x...) FunctionTracer _ft("BMenuField", __FUNCTION__, \
sFunctionDepth)
# define TRACE(x...) { BString _to; \
_to.Append(' ', (sFunctionDepth + 1) * 2); \
printf("%s", _to.String()); printf(x); }
#else
# define CALLED(x...)
# define TRACE(x...)
#endif
2006-08-26 16:21:15 +00:00
class BTextControl::LabelLayoutItem : public BAbstractLayoutItem {
public:
LabelLayoutItem(BTextControl* parent);
virtual bool IsVisible();
virtual void SetVisible(bool visible);
virtual BRect Frame();
virtual void SetFrame(BRect frame);
virtual BView* View();
virtual BSize BaseMinSize();
virtual BSize BaseMaxSize();
virtual BSize BasePreferredSize();
virtual BAlignment BaseAlignment();
private:
BTextControl* fParent;
BRect fFrame;
};
class BTextControl::TextViewLayoutItem : public BAbstractLayoutItem {
public:
TextViewLayoutItem(BTextControl* parent);
virtual bool IsVisible();
virtual void SetVisible(bool visible);
virtual BRect Frame();
virtual void SetFrame(BRect frame);
virtual BView* View();
virtual BSize BaseMinSize();
virtual BSize BaseMaxSize();
virtual BSize BasePreferredSize();
virtual BAlignment BaseAlignment();
private:
BTextControl* fParent;
BRect fFrame;
};
// #pragma mark -
BTextControl::BTextControl(BRect frame, const char* name, const char* label,
const char* text, BMessage* message, uint32 mask, uint32 flags)
: BControl(frame, name, label, message, mask, flags | B_FRAME_EVENTS)
2002-07-09 12:24:59 +00:00
{
_InitData(label, text);
2006-08-26 16:21:15 +00:00
_ValidateLayout();
}
2002-07-09 12:24:59 +00:00
BTextControl::BTextControl(const char* name, const char* label,
const char* text, BMessage* message, uint32 flags)
2008-09-15 19:43:18 +00:00
: BControl(name, label, message, flags | B_FRAME_EVENTS)
2006-08-26 16:21:15 +00:00
{
_InitData(label, text);
_ValidateLayout();
}
2002-07-09 12:24:59 +00:00
BTextControl::BTextControl(const char* label, const char* text,
BMessage* message)
2008-09-15 19:43:18 +00:00
: BControl(NULL, label, message,
B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS)
2006-08-26 16:21:15 +00:00
{
_InitData(label, text);
_ValidateLayout();
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
2002-07-09 12:24:59 +00:00
BTextControl::~BTextControl()
{
2003-06-16 07:27:24 +00:00
SetModificationMessage(NULL);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
BTextControl::BTextControl(BMessage* archive)
: BControl(archive)
2002-07-09 12:24:59 +00:00
{
_InitData(Label(), NULL, archive);
2002-07-09 12:24:59 +00:00
int32 labelAlignment = B_ALIGN_LEFT;
int32 textAlignment = B_ALIGN_LEFT;
2002-07-09 12:24:59 +00:00
if (archive->HasInt32("_a_label"))
archive->FindInt32("_a_label", &labelAlignment);
2002-07-09 12:24:59 +00:00
if (archive->HasInt32("_a_text"))
archive->FindInt32("_a_text", &textAlignment);
SetAlignment((alignment)labelAlignment, (alignment)textAlignment);
2003-06-16 07:27:24 +00:00
if (archive->HasFloat("_divide"))
archive->FindFloat("_divide", &fDivider);
2003-06-16 07:27:24 +00:00
if (archive->HasMessage("_mod_msg")) {
BMessage* message = new BMessage;
archive->FindMessage("_mod_msg", message);
SetModificationMessage(message);
2002-07-09 12:24:59 +00:00
}
}
2004-10-18 10:21:46 +00:00
BArchivable*
BTextControl::Instantiate(BMessage* archive)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
if (validate_instantiation(archive, "BTextControl"))
return new BTextControl(archive);
return NULL;
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
status_t
BTextControl::Archive(BMessage *data, bool deep) const
2002-07-09 12:24:59 +00:00
{
2006-07-08 13:26:55 +00:00
status_t ret = BControl::Archive(data, deep);
2004-10-18 10:21:46 +00:00
alignment labelAlignment, textAlignment;
2003-06-16 07:27:24 +00:00
2004-10-18 10:21:46 +00:00
GetAlignment(&labelAlignment, &textAlignment);
2002-07-09 12:24:59 +00:00
if (ret == B_OK)
ret = data->AddInt32("_a_label", labelAlignment);
if (ret == B_OK)
ret = data->AddInt32("_a_text", textAlignment);
if (ret == B_OK)
ret = data->AddFloat("_divide", Divider());
2003-06-16 07:27:24 +00:00
if (ModificationMessage() && (ret == B_OK))
ret = data->AddMessage("_mod_msg", ModificationMessage());
2003-06-16 07:27:24 +00:00
return ret;
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetText(const char *text)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
if (InvokeKind() != B_CONTROL_INVOKED)
return;
2008-09-15 19:43:18 +00:00
CALLED();
2002-07-09 12:24:59 +00:00
fText->SetText(text);
2003-06-16 07:27:24 +00:00
if (IsFocus())
fText->SetInitialText();
fText->Invalidate();
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
const char *
BTextControl::Text() const
2002-07-09 12:24:59 +00:00
{
return fText->Text();
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetValue(int32 value)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
BControl::SetValue(value);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
status_t
BTextControl::Invoke(BMessage *message)
2002-07-09 12:24:59 +00:00
{
2004-10-18 10:21:46 +00:00
return BControl::Invoke(message);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
BTextView *
BTextControl::TextView() const
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
return fText;
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetModificationMessage(BMessage *message)
2002-07-09 12:24:59 +00:00
{
2005-04-27 17:25:29 +00:00
delete fModificationMessage;
2002-07-09 12:24:59 +00:00
fModificationMessage = message;
}
2004-10-18 10:21:46 +00:00
BMessage *
BTextControl::ModificationMessage() const
2002-07-09 12:24:59 +00:00
{
return fModificationMessage;
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetAlignment(alignment labelAlignment, alignment textAlignment)
2002-07-09 12:24:59 +00:00
{
2004-10-18 10:21:46 +00:00
fText->SetAlignment(textAlignment);
2003-06-16 07:27:24 +00:00
fText->AlignTextRect();
2004-10-18 10:21:46 +00:00
if (fLabelAlign != labelAlignment) {
fLabelAlign = labelAlignment;
2003-06-16 07:27:24 +00:00
Invalidate();
}
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::GetAlignment(alignment* _label, alignment* _text) const
2002-07-09 12:24:59 +00:00
{
if (_label)
*_label = fLabelAlign;
if (_text)
*_text = fText->Alignment();
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetDivider(float dividingLine)
2002-07-09 12:24:59 +00:00
{
fDivider = floorf(dividingLine + 0.5);
_LayoutTextView();
2003-06-16 07:27:24 +00:00
2004-10-18 10:21:46 +00:00
if (Window()) {
2003-06-16 07:27:24 +00:00
fText->Invalidate();
Invalidate();
2002-07-09 12:24:59 +00:00
}
}
2004-10-18 10:21:46 +00:00
float
BTextControl::Divider() const
2002-07-09 12:24:59 +00:00
{
return fDivider;
}
2004-10-18 10:21:46 +00:00
void
BTextControl::Draw(BRect updateRect)
2002-07-09 12:24:59 +00:00
{
rgb_color noTint = ui_color(B_PANEL_BACKGROUND_COLOR);
rgb_color lighten1 = tint_color(noTint, B_LIGHTEN_1_TINT);
rgb_color lighten2 = tint_color(noTint, B_LIGHTEN_2_TINT);
rgb_color lightenMax = tint_color(noTint, B_LIGHTEN_MAX_TINT);
rgb_color darken1 = tint_color(noTint, B_DARKEN_1_TINT);
rgb_color darken2 = tint_color(noTint, B_DARKEN_2_TINT);
rgb_color darken4 = tint_color(noTint, B_DARKEN_4_TINT);
rgb_color navigationColor = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
2003-06-16 07:27:24 +00:00
bool enabled = IsEnabled();
bool active = false;
if (fText->IsFocus() && Window()->IsActive())
active = true;
// outer bevel
BRect rect = fText->Frame();
rect.InsetBy(-2, -2);
if (enabled)
SetHighColor(darken1);
else
SetHighColor(noTint);
StrokeLine(rect.LeftBottom(), rect.LeftTop());
StrokeLine(rect.RightTop());
if (enabled)
SetHighColor(lighten2);
else
SetHighColor(lighten1);
StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
StrokeLine(BPoint(rect.right, rect.top + 1.0f), rect.RightBottom());
// inner bevel
rect.InsetBy(1.0f, 1.0f);
2003-06-16 07:27:24 +00:00
2004-10-18 10:21:46 +00:00
if (active) {
SetHighColor(navigationColor);
2003-06-16 07:27:24 +00:00
StrokeRect(rect);
2004-10-18 10:21:46 +00:00
} else {
2003-06-16 07:27:24 +00:00
if (enabled)
SetHighColor(darken4);
else
SetHighColor(darken2);
StrokeLine(rect.LeftTop(), rect.LeftBottom());
StrokeLine(rect.LeftTop(), rect.RightTop());
SetHighColor(noTint);
2003-06-16 07:27:24 +00:00
StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
StrokeLine(BPoint(rect.right, rect.top + 1.0f));
}
// label
2004-10-18 10:21:46 +00:00
if (Label()) {
2008-02-24 22:35:30 +00:00
font_height fontHeight;
GetFontHeight(&fontHeight);
2003-06-16 07:27:24 +00:00
2008-02-24 22:35:30 +00:00
float y = Bounds().top + (Bounds().Height() + 1 - fontHeight.ascent
- fontHeight.descent) / 2 + fontHeight.ascent;
2002-07-09 12:24:59 +00:00
float x;
2003-06-16 07:27:24 +00:00
float labelWidth = StringWidth(Label());
2004-10-18 10:21:46 +00:00
switch (fLabelAlign) {
2002-07-09 12:24:59 +00:00
case B_ALIGN_RIGHT:
2008-02-24 20:39:29 +00:00
x = fDivider - labelWidth - 3.0;
2002-07-09 12:24:59 +00:00
break;
case B_ALIGN_CENTER:
2008-02-24 20:39:29 +00:00
x = fDivider - labelWidth / 2.0;
2002-07-09 12:24:59 +00:00
break;
default:
2008-02-24 20:39:29 +00:00
x = 0.0;
2002-07-09 12:24:59 +00:00
break;
}
2007-09-01 13:33:51 +00:00
BRect labelArea(x, Bounds().top, x + labelWidth, Bounds().bottom);
if (x < fDivider && updateRect.Intersects(labelArea)) {
2007-09-01 13:33:51 +00:00
labelArea.right = fText->Frame().left - 3;
BRegion clipRegion(labelArea);
ConstrainClippingRegion(&clipRegion);
SetHighColor(IsEnabled() ? ui_color(B_CONTROL_TEXT_COLOR)
: tint_color(noTint, B_DISABLED_LABEL_TINT));
DrawString(Label(), BPoint(x, y));
}
2002-07-09 12:24:59 +00:00
}
}
2004-10-18 10:21:46 +00:00
void
BTextControl::MouseDown(BPoint where)
2002-07-09 12:24:59 +00:00
{
2004-10-18 10:21:46 +00:00
if (!fText->IsFocus()) {
2003-06-16 07:27:24 +00:00
fText->MakeFocus(true);
2002-07-09 12:24:59 +00:00
}
}
2004-10-18 10:21:46 +00:00
void
BTextControl::AttachedToWindow()
2002-07-09 12:24:59 +00:00
{
BControl::AttachedToWindow();
2003-06-16 07:27:24 +00:00
_UpdateTextViewColors(IsEnabled());
fText->MakeEditable(IsEnabled());
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::MakeFocus(bool state)
2002-07-09 12:24:59 +00:00
{
if (state != fText->IsFocus()) {
fText->MakeFocus(state);
2003-06-16 07:27:24 +00:00
if (state)
fText->SelectAll();
}
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetEnabled(bool enabled)
2002-07-09 12:24:59 +00:00
{
if (IsEnabled() == enabled)
2003-06-16 07:27:24 +00:00
return;
2004-10-18 10:21:46 +00:00
if (Window()) {
fText->MakeEditable(enabled);
2003-06-16 07:27:24 +00:00
_UpdateTextViewColors(enabled);
2003-06-16 07:27:24 +00:00
fText->Invalidate();
Window()->UpdateIfNeeded();
}
BControl::SetEnabled(enabled);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::GetPreferredSize(float *_width, float *_height)
2002-07-09 12:24:59 +00:00
{
if (_height) {
// 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);
2008-02-24 20:39:29 +00:00
float textHeight = ceilf(fText->LineHeight(0) + 1.0) + 4.0;
2002-07-09 12:24:59 +00:00
*_height = max_c(labelHeight, textHeight);
}
if (_width) {
*_width = Bounds().Width();
const char* label = Label();
if (label) {
float width = ceilf(StringWidth(label));
*_width = (width * 1.3) + width + 4.0;
}
}
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::ResizeToPreferred()
2002-07-09 12:24:59 +00:00
{
BView::ResizeToPreferred();
fDivider = 0.0;
const char* label = Label();
if (label)
fDivider = ceil(StringWidth(label)) + 2.0;
_LayoutTextView();
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetFlags(uint32 flags)
2002-07-09 12:24:59 +00:00
{
2008-09-15 19:43:18 +00:00
// If the textview is navigable, set it to not navigable if needed
// Else if it is not navigable, set it to navigable if needed
if (fText->Flags() & B_NAVIGABLE) {
if (!(flags & B_NAVIGABLE))
fText->SetFlags(fText->Flags() & ~B_NAVIGABLE);
2004-10-18 10:21:46 +00:00
2008-09-15 19:43:18 +00:00
} else {
if (flags & B_NAVIGABLE)
fText->SetFlags(fText->Flags() | B_NAVIGABLE);
2003-06-16 07:27:24 +00:00
}
2008-09-15 19:43:18 +00:00
// Don't make this one navigable
flags &= ~B_NAVIGABLE;
2002-07-09 12:24:59 +00:00
BView::SetFlags(flags);
}
2004-10-18 10:21:46 +00:00
void
BTextControl::MessageReceived(BMessage *msg)
2002-07-09 12:24:59 +00:00
{
2004-10-18 10:21:46 +00:00
switch(msg->what) {
2003-06-16 07:27:24 +00:00
case B_SET_PROPERTY:
case B_GET_PROPERTY:
// TODO
2002-07-09 12:24:59 +00:00
break;
default:
BControl::MessageReceived(msg);
break;
}
}
2004-10-18 10:21:46 +00:00
BHandler *
BTextControl::ResolveSpecifier(BMessage *msg, int32 index,
2003-06-16 07:27:24 +00:00
BMessage *specifier, int32 form,
const char *property)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
/*
BPropertyInfo propInfo(prop_list);
BHandler *target = NULL;
if (propInfo.FindMatch(message, 0, specifier, what, property) < B_OK)
return BControl::ResolveSpecifier(message, index, specifier, what,
property);
else
return this;
*/
return BControl::ResolveSpecifier(msg, index, specifier, form, property);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
status_t
BTextControl::GetSupportedSuites(BMessage *data)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
return BControl::GetSupportedSuites(data);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::MouseUp(BPoint pt)
2002-07-09 12:24:59 +00:00
{
BControl::MouseUp(pt);
}
2004-10-18 10:21:46 +00:00
void
BTextControl::MouseMoved(BPoint pt, uint32 code, const BMessage *msg)
2002-07-09 12:24:59 +00:00
{
BControl::MouseMoved(pt, code, msg);
}
2004-10-18 10:21:46 +00:00
void
BTextControl::DetachedFromWindow()
2002-07-09 12:24:59 +00:00
{
BControl::DetachedFromWindow();
}
2004-10-18 10:21:46 +00:00
void
BTextControl::AllAttached()
2002-07-09 12:24:59 +00:00
{
BControl::AllAttached();
}
2004-10-18 10:21:46 +00:00
void
BTextControl::AllDetached()
2002-07-09 12:24:59 +00:00
{
BControl::AllDetached();
}
2004-10-18 10:21:46 +00:00
void
BTextControl::FrameMoved(BPoint newPosition)
2002-07-09 12:24:59 +00:00
{
BControl::FrameMoved(newPosition);
}
2004-10-18 10:21:46 +00:00
void
BTextControl::FrameResized(float width, float height)
2002-07-09 12:24:59 +00:00
{
2008-09-15 19:43:18 +00:00
CALLED();
BControl::FrameResized(width, height);
// changes in width
BRect bounds = Bounds();
const float border = 2.0f;
if (bounds.Width() > fPreviousWidth) {
// invalidate the region between the old and the new right border
BRect rect = bounds;
rect.left += fPreviousWidth - border;
rect.right--;
Invalidate(rect);
} else if (bounds.Width() < fPreviousWidth) {
// invalidate the region of the new right border
BRect rect = bounds;
rect.left = rect.right - border;
Invalidate(rect);
}
// changes in height
if (bounds.Height() > fPreviousHeight) {
// invalidate the region between the old and the new bottom border
BRect rect = bounds;
rect.top += fPreviousHeight - border;
rect.bottom--;
Invalidate(rect);
} else if (bounds.Height() < fPreviousHeight) {
// invalidate the region of the new bottom border
BRect rect = bounds;
rect.top = rect.bottom - border;
Invalidate(rect);
}
fPreviousWidth = uint16(bounds.Width());
fPreviousHeight = uint16(bounds.Height());
2008-09-15 19:43:18 +00:00
TRACE("width: %.2f, height: %.2f\n", bounds.Width(), bounds.Height());
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::WindowActivated(bool active)
2002-07-09 12:24:59 +00:00
{
if (fText->IsFocus()) {
// invalidate to remove/show focus indication
2007-09-01 13:33:51 +00:00
BRect rect = fText->Frame();
rect.InsetBy(-1, -1);
Invalidate(rect);
// help out embedded text view which doesn't
// get notified of this
fText->Invalidate();
}
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
status_t
BTextControl::Perform(perform_code d, void *arg)
2002-07-09 12:24:59 +00:00
{
return BControl::Perform(d, arg);
}
2004-10-18 10:21:46 +00:00
2006-08-26 16:21:15 +00:00
BLayoutItem*
BTextControl::CreateLabelLayoutItem()
{
if (!fLabelLayoutItem)
fLabelLayoutItem = new LabelLayoutItem(this);
return fLabelLayoutItem;
}
BLayoutItem*
BTextControl::CreateTextViewLayoutItem()
{
if (!fTextViewLayoutItem)
fTextViewLayoutItem = new TextViewLayoutItem(this);
return fTextViewLayoutItem;
}
2003-06-16 07:27:24 +00:00
void BTextControl::_ReservedTextControl1() {}
void BTextControl::_ReservedTextControl2() {}
void BTextControl::_ReservedTextControl3() {}
void BTextControl::_ReservedTextControl4() {}
2004-10-18 10:21:46 +00:00
BTextControl &
BTextControl::operator=(const BTextControl&)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
return *this;
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::_UpdateTextViewColors(bool enabled)
{
rgb_color textColor;
rgb_color color;
BFont font;
fText->GetFontAndColor(0, &font);
if (enabled)
textColor = ui_color(B_DOCUMENT_TEXT_COLOR);
else {
textColor = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_DISABLED_LABEL_TINT);
}
fText->SetFontAndColor(&font, B_FONT_ALL, &textColor);
if (enabled) {
color = ui_color(B_DOCUMENT_BACKGROUND_COLOR);
} else {
color = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_LIGHTEN_2_TINT);
}
fText->SetViewColor(color);
fText->SetLowColor(color);
}
2004-10-18 10:21:46 +00:00
void
BTextControl::_CommitValue()
2002-07-09 12:24:59 +00:00
{
}
2004-10-18 10:21:46 +00:00
void
BTextControl::_InitData(const char* label, const char* initialText,
BMessage* archive)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
BRect bounds(Bounds());
fText = NULL;
fModificationMessage = NULL;
fLabelAlign = B_ALIGN_LEFT;
fDivider = 0.0f;
fPreviousWidth = bounds.Width();
fPreviousHeight = bounds.Height();
2006-08-26 16:21:15 +00:00
fLabelLayoutItem = NULL;
fTextViewLayoutItem = NULL;
2003-06-16 07:27:24 +00:00
int32 flags = 0;
BFont font(be_plain_font);
2003-06-16 07:27:24 +00:00
if (!archive || !archive->HasString("_fname"))
2004-10-18 10:21:46 +00:00
flags |= B_FONT_FAMILY_AND_STYLE;
2003-06-16 07:27:24 +00:00
if (!archive || !archive->HasFloat("_fflt"))
2004-10-18 10:21:46 +00:00
flags |= B_FONT_SIZE;
2003-06-16 07:27:24 +00:00
if (flags != 0)
SetFont(&font, flags);
if (label)
fDivider = floorf(bounds.Width() / 2.0f);
2003-06-16 07:27:24 +00:00
uint32 navigableFlags = Flags() & B_NAVIGABLE;
2008-09-15 19:43:18 +00:00
if (navigableFlags != 0)
BView::SetFlags(Flags() & ~B_NAVIGABLE);
2003-06-16 07:27:24 +00:00
if (archive)
2008-02-24 20:39:29 +00:00
fText = static_cast<BPrivate::_BTextInput_*>(FindView("_input_"));
if (fText == NULL) {
2008-07-15 13:22:29 +00:00
BRect frame(fDivider, bounds.top, bounds.right, bounds.bottom);
// we are stroking the frame around the text view, which
// is 2 pixels wide
frame.InsetBy(2.0, 3.0);
BRect textRect(frame.OffsetToCopy(B_ORIGIN));
2003-06-16 07:27:24 +00:00
2008-02-24 20:39:29 +00:00
fText = new BPrivate::_BTextInput_(frame, textRect,
2008-09-15 19:43:18 +00:00
B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS | navigableFlags);
2003-06-16 07:27:24 +00:00
AddChild(fText);
SetText(initialText);
2003-06-16 07:27:24 +00:00
fText->SetAlignment(B_ALIGN_LEFT);
fText->AlignTextRect();
}
2002-07-09 12:24:59 +00:00
}
2006-08-26 16:21:15 +00:00
void
BTextControl::_ValidateLayout()
{
2008-09-15 19:43:18 +00:00
CALLED();
float height;
BTextControl::GetPreferredSize(NULL, &height);
2006-08-26 16:21:15 +00:00
ResizeTo(Bounds().Width(), height);
2006-08-26 16:21:15 +00:00
_LayoutTextView();
2006-08-26 16:21:15 +00:00
fPreviousHeight = Bounds().Height();
}
void
BTextControl::_LayoutTextView()
{
2008-09-15 19:43:18 +00:00
CALLED();
BRect frame = Bounds();
frame.left = fDivider;
// we are stroking the frame around the text view, which
// is 2 pixels wide
frame.InsetBy(2.0, 2.0);
fText->MoveTo(frame.left, frame.top);
fText->ResizeTo(frame.Width(), frame.Height());
2008-02-24 20:39:29 +00:00
fText->AlignTextRect();
2008-09-15 19:43:18 +00:00
TRACE("width: %.2f, height: %.2f\n", Frame().Width(), Frame().Height());
TRACE("fDivider: %.2f\n", fDivider);
TRACE("fText frame: (%.2f, %.2f, %.2f, %.2f)\n",
frame.left, frame.top, frame.right, frame.bottom);
}
2006-08-26 16:21:15 +00:00
void
BTextControl::_UpdateFrame()
{
if (fLabelLayoutItem && fTextViewLayoutItem) {
BRect labelFrame = fLabelLayoutItem->Frame();
BRect textFrame = fTextViewLayoutItem->Frame();
MoveTo(labelFrame.left, labelFrame.top);
ResizeTo(textFrame.left + textFrame.Width() - labelFrame.left,
textFrame.top + textFrame.Height() - labelFrame.top);
SetDivider(textFrame.left - labelFrame.left);
}
}
// #pragma mark -
BTextControl::LabelLayoutItem::LabelLayoutItem(BTextControl* parent)
: fParent(parent),
fFrame()
{
}
bool
BTextControl::LabelLayoutItem::IsVisible()
{
return !fParent->IsHidden(fParent);
}
void
BTextControl::LabelLayoutItem::SetVisible(bool visible)
{
// not allowed
}
BRect
BTextControl::LabelLayoutItem::Frame()
{
return fFrame;
}
void
BTextControl::LabelLayoutItem::SetFrame(BRect frame)
{
fFrame = frame;
fParent->_UpdateFrame();
}
BView*
BTextControl::LabelLayoutItem::View()
{
return fParent;
}
BSize
BTextControl::LabelLayoutItem::BaseMinSize()
{
// TODO: Cache the info. Might be too expensive for this call.
const char* label = fParent->Label();
if (!label)
return BSize(-1, -1);
BSize size;
fParent->GetPreferredSize(NULL, &size.height);
size.width = fParent->StringWidth(label) + 2 * 3 - 1;
return size;
}
BSize
BTextControl::LabelLayoutItem::BaseMaxSize()
{
return BaseMinSize();
}
BSize
BTextControl::LabelLayoutItem::BasePreferredSize()
{
return BaseMinSize();
}
BAlignment
BTextControl::LabelLayoutItem::BaseAlignment()
{
return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT);
}
// #pragma mark -
BTextControl::TextViewLayoutItem::TextViewLayoutItem(BTextControl* parent)
: fParent(parent),
fFrame()
{
}
bool
BTextControl::TextViewLayoutItem::IsVisible()
{
return !fParent->IsHidden(fParent);
}
void
BTextControl::TextViewLayoutItem::SetVisible(bool visible)
{
// not allowed
}
BRect
BTextControl::TextViewLayoutItem::Frame()
{
return fFrame;
}
void
BTextControl::TextViewLayoutItem::SetFrame(BRect frame)
{
fFrame = frame;
fParent->_UpdateFrame();
}
BView*
BTextControl::TextViewLayoutItem::View()
{
return fParent;
}
BSize
BTextControl::TextViewLayoutItem::BaseMinSize()
{
// TODO: Cache the info. Might be too expensive for this call.
BSize size;
fParent->GetPreferredSize(NULL, &size.height);
// mmh, some arbitrary minimal width
size.width = 30;
return size;
}
BSize
BTextControl::TextViewLayoutItem::BaseMaxSize()
{
BSize size(BaseMinSize());
size.width = B_SIZE_UNLIMITED;
return size;
}
BSize
BTextControl::TextViewLayoutItem::BasePreferredSize()
{
BSize size(BaseMinSize());
// puh, no idea...
size.width = 100;
return size;
}
BAlignment
BTextControl::TextViewLayoutItem::BaseAlignment()
{
return BAlignment(B_ALIGN_USE_FULL_WIDTH, B_ALIGN_USE_FULL_HEIGHT);
}