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

1486 lines
30 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. */
2009-04-06 23:50:20 +00:00
#include <string.h>
#include <TextControl.h>
2006-08-26 16:21:15 +00:00
#include <AbstractLayoutItem.h>
#include <ControlLook.h>
2006-08-26 16:21:15 +00:00
#include <LayoutUtils.h>
2004-10-18 10:21:46 +00:00
#include <Message.h>
#include <PropertyInfo.h>
#include <Region.h>
2002-07-09 12:24:59 +00:00
#include <Window.h>
2008-10-15 09:29:06 +00:00
#include <binary_compatibility/Interface.h>
#include <binary_compatibility/Support.h>
2008-10-15 09:29:06 +00:00
2002-07-09 12:24:59 +00:00
#include "TextInput.h"
2008-09-15 19:43:18 +00:00
//#define TRACE_TEXT_CONTROL
#ifdef TRACE_TEXT_CONTROL
# include <stdio.h>
2008-09-15 19:43:18 +00:00
# include <FunctionTracer.h>
static int32 sFunctionDepth = -1;
# define CALLED(x...) FunctionTracer _ft("BTextControl", __FUNCTION__, \
2008-09-15 19:43:18 +00:00
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
namespace {
const char* const kFrameField = "BTextControl:layoutitem:frame";
const char* const kTextViewItemField = "BTextControl:textViewItem";
const char* const kLabelItemField = "BMenuField:labelItem";
}
static property_info sPropertyList[] = {
{
"Value",
{ B_GET_PROPERTY, B_SET_PROPERTY },
{ B_DIRECT_SPECIFIER },
NULL, 0,
{ B_STRING_TYPE }
},
{}
};
2006-08-26 16:21:15 +00:00
class BTextControl::LabelLayoutItem : public BAbstractLayoutItem {
public:
LabelLayoutItem(BTextControl* parent);
LabelLayoutItem(BMessage* from);
2006-08-26 16:21:15 +00:00
virtual bool IsVisible();
virtual void SetVisible(bool visible);
virtual BRect Frame();
virtual void SetFrame(BRect frame);
void SetParent(BTextControl* parent);
2006-08-26 16:21:15 +00:00
virtual BView* View();
virtual BSize BaseMinSize();
virtual BSize BaseMaxSize();
virtual BSize BasePreferredSize();
virtual BAlignment BaseAlignment();
virtual status_t Archive(BMessage* into, bool deep = true) const;
static BArchivable* Instantiate(BMessage* from);
2006-08-26 16:21:15 +00:00
private:
BTextControl* fParent;
BRect fFrame;
};
class BTextControl::TextViewLayoutItem : public BAbstractLayoutItem {
public:
TextViewLayoutItem(BTextControl* parent);
TextViewLayoutItem(BMessage* from);
2006-08-26 16:21:15 +00:00
virtual bool IsVisible();
virtual void SetVisible(bool visible);
virtual BRect Frame();
virtual void SetFrame(BRect frame);
void SetParent(BTextControl* parent);
2006-08-26 16:21:15 +00:00
virtual BView* View();
virtual BSize BaseMinSize();
virtual BSize BaseMaxSize();
virtual BSize BasePreferredSize();
virtual BAlignment BaseAlignment();
virtual status_t Archive(BMessage* into, bool deep = true) const;
static BArchivable* Instantiate(BMessage* from);
2006-08-26 16:21:15 +00:00
private:
BTextControl* fParent;
BRect fFrame;
};
struct BTextControl::LayoutData {
LayoutData(float width, float height)
:
label_layout_item(NULL),
text_view_layout_item(NULL),
previous_width(width),
previous_height(height),
valid(false)
{
}
LabelLayoutItem* label_layout_item;
TextViewLayoutItem* text_view_layout_item;
float previous_width; // used in FrameResized() for
float previous_height; // invalidation
font_height font_info;
float label_width;
float label_height;
BSize min;
BSize text_view_min;
bool valid;
};
2006-08-26 16:21:15 +00:00
// #pragma mark -
static const int32 kFrameMargin = 2;
static const int32 kLabelInputSpacing = 3;
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);
_InitText(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)
:
BControl(name, label, message, flags | B_FRAME_EVENTS)
2006-08-26 16:21:15 +00:00
{
_InitData(label);
_InitText(text);
2006-08-26 16:21:15 +00:00
_ValidateLayout();
}
2002-07-09 12:24:59 +00:00
BTextControl::BTextControl(const char* label, const char* text,
BMessage* message)
:
BControl(NULL, label, message,
2008-09-15 19:43:18 +00:00
B_WILL_DRAW | B_NAVIGABLE | B_FRAME_EVENTS)
2006-08-26 16:21:15 +00:00
{
_InitData(label);
_InitText(text);
2006-08-26 16:21:15 +00:00
_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);
2008-09-21 15:29:02 +00:00
delete fLayoutData;
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
BTextControl::BTextControl(BMessage* archive)
:
BControl(BUnarchiver::PrepareArchive(archive))
2002-07-09 12:24:59 +00:00
{
BUnarchiver unarchiver(archive);
2002-07-09 12:24:59 +00:00
_InitData(Label(), archive);
2002-07-09 12:24:59 +00:00
if (!BUnarchiver::IsArchiveManaged(archive))
_InitText(NULL, archive);
2003-06-16 07:27:24 +00:00
status_t err = B_OK;
if (archive->HasFloat("_divide"))
err = archive->FindFloat("_divide", &fDivider);
2003-06-16 07:27:24 +00:00
if (err == B_OK && archive->HasMessage("_mod_msg")) {
BMessage* message = new BMessage;
err = archive->FindMessage("_mod_msg", message);
SetModificationMessage(message);
2002-07-09 12:24:59 +00:00
}
unarchiver.Finish(err);
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
{
BArchiver archiver(data);
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 archiver.Finish(ret);
}
status_t
BTextControl::AllArchived(BMessage* into) const
{
BArchiver archiver(into);
status_t err = B_OK;
if (archiver.IsArchived(fLayoutData->text_view_layout_item)) {
err = archiver.AddArchivable(kTextViewItemField,
fLayoutData->text_view_layout_item);
}
if (err == B_OK && archiver.IsArchived(fLayoutData->label_layout_item)) {
err = archiver.AddArchivable(kLabelItemField,
fLayoutData->label_layout_item);
}
return err;
}
status_t
BTextControl::AllUnarchived(const BMessage* from)
{
status_t err;
if ((err = BControl::AllUnarchived(from)) != B_OK)
return err;
_InitText(NULL, from);
BUnarchiver unarchiver(from);
if (unarchiver.IsInstantiated(kTextViewItemField)) {
err = unarchiver.FindObject(kTextViewItemField,
BUnarchiver::B_DONT_ASSUME_OWNERSHIP,
fLayoutData->text_view_layout_item);
if (err == B_OK)
fLayoutData->text_view_layout_item->SetParent(this);
else
return err;
}
if (unarchiver.IsInstantiated(kLabelItemField)) {
err = unarchiver.FindObject(kLabelItemField,
BUnarchiver::B_DONT_ASSUME_OWNERSHIP,
fLayoutData->label_layout_item);
if (err == B_OK)
fLayoutData->label_layout_item->SetParent(this);
}
return err;
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
2010-10-08 21:05:05 +00:00
if (fText->IsFocus()) {
2003-06-16 07:27:24 +00:00
fText->SetInitialText();
2010-10-08 21:05:05 +00:00
fText->SelectAll();
}
2003-06-16 07:27:24 +00:00
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
{
bool enabled = IsEnabled();
bool active = fText->IsFocus() && Window()->IsActive();
BRect rect = fText->Frame();
rect.InsetBy(-2, -2);
if (be_control_look != NULL) {
rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
uint32 flags = 0;
if (!enabled)
flags |= BControlLook::B_DISABLED;
if (active)
flags |= BControlLook::B_FOCUSED;
be_control_look->DrawTextControlBorder(this, rect, updateRect, base,
flags);
rect = Bounds();
rect.right = fDivider - kLabelInputSpacing;
// rect.right = fText->Frame().left - 2;
// rect.right -= 3;//be_control_look->DefaultLabelSpacing();
be_control_look->DrawLabel(this, Label(), rect, updateRect,
base, flags, BAlignment(fLabelAlign, B_ALIGN_MIDDLE));
return;
}
// outer bevel
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
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()) {
_ValidateLayoutData();
font_height& fontHeight = fLayoutData->font_info;
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:
x = fDivider - labelWidth - kLabelInputSpacing;
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)) {
labelArea.right = fText->Frame().left - kLabelInputSpacing;
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);
if (enabled)
fText->SetFlags(fText->Flags() | B_NAVIGABLE);
else
fText->SetFlags(fText->Flags() & ~B_NAVIGABLE);
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
{
CALLED();
2002-07-09 12:24:59 +00:00
_ValidateLayoutData();
if (_width) {
float minWidth = fLayoutData->min.width;
if (Label() == NULL && !(Flags() & B_SUPPORTS_LAYOUT)) {
// Indeed, only if there is no label! BeOS backwards compatible
// behavior:
minWidth = max_c(minWidth, Bounds().Width());
}
*_width = minWidth;
}
if (_height)
*_height = fLayoutData->min.height;
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
}
// 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 *message)
2002-07-09 12:24:59 +00:00
{
if (message->what == B_GET_PROPERTY || message->what == B_SET_PROPERTY) {
BMessage reply(B_REPLY);
bool handled = false;
BMessage specifier;
int32 index;
int32 form;
const char *property;
if (message->GetCurrentSpecifier(&index, &specifier, &form, &property) == B_OK) {
if (strcmp(property, "Value") == 0) {
if (message->what == B_GET_PROPERTY) {
reply.AddString("result", fText->Text());
handled = true;
} else {
const char *value = NULL;
// B_SET_PROPERTY
if (message->FindString("data", &value) == B_OK) {
fText->SetText(value);
reply.AddInt32("error", B_OK);
handled = true;
}
}
}
}
if (handled) {
message->SendReply(&reply);
return;
}
2002-07-09 12:24:59 +00:00
}
BControl::MessageReceived(message);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
BHandler *
BTextControl::ResolveSpecifier(BMessage *message, int32 index,
BMessage *specifier, int32 what,
2003-06-16 07:27:24 +00:00
const char *property)
2002-07-09 12:24:59 +00:00
{
BPropertyInfo propInfo(sPropertyList);
2003-06-16 07:27:24 +00:00
if (propInfo.FindMatch(message, 0, specifier, what, property) >= B_OK)
2003-06-16 07:27:24 +00:00
return this;
return BControl::ResolveSpecifier(message, index, specifier, what,
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);
// TODO: this causes flickering still...
// changes in width
BRect bounds = Bounds();
if (bounds.Width() > fLayoutData->previous_width) {
// invalidate the region between the old and the new right border
BRect rect = bounds;
rect.left += fLayoutData->previous_width - kFrameMargin;
rect.right--;
Invalidate(rect);
} else if (bounds.Width() < fLayoutData->previous_width) {
// invalidate the region of the new right border
BRect rect = bounds;
rect.left = rect.right - kFrameMargin;
Invalidate(rect);
}
// changes in height
if (bounds.Height() > fLayoutData->previous_height) {
// invalidate the region between the old and the new bottom border
BRect rect = bounds;
rect.top += fLayoutData->previous_height - kFrameMargin;
rect.bottom--;
Invalidate(rect);
// invalidate label area
rect = bounds;
rect.right = fDivider;
Invalidate(rect);
} else if (bounds.Height() < fLayoutData->previous_height) {
// invalidate the region of the new bottom border
BRect rect = bounds;
rect.top = rect.bottom - kFrameMargin;
Invalidate(rect);
// invalidate label area
rect = bounds;
rect.right = fDivider;
Invalidate(rect);
}
fLayoutData->previous_width = bounds.Width();
fLayoutData->previous_height = 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
BSize
BTextControl::MinSize()
2002-07-09 12:24:59 +00:00
{
CALLED();
_ValidateLayoutData();
return BLayoutUtils::ComposeSize(ExplicitMinSize(), fLayoutData->min);
}
BSize
BTextControl::MaxSize()
{
CALLED();
_ValidateLayoutData();
BSize max = fLayoutData->min;
max.width = B_SIZE_UNLIMITED;
return BLayoutUtils::ComposeSize(ExplicitMaxSize(), max);
}
BSize
BTextControl::PreferredSize()
{
CALLED();
_ValidateLayoutData();
return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), fLayoutData->min);
}
void
BTextControl::InvalidateLayout(bool descendants)
{
CALLED();
fLayoutData->valid = false;
BView::InvalidateLayout(descendants);
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
2006-08-26 16:21:15 +00:00
BLayoutItem*
BTextControl::CreateLabelLayoutItem()
{
if (!fLayoutData->label_layout_item)
fLayoutData->label_layout_item = new LabelLayoutItem(this);
return fLayoutData->label_layout_item;
2006-08-26 16:21:15 +00:00
}
BLayoutItem*
BTextControl::CreateTextViewLayoutItem()
{
if (!fLayoutData->text_view_layout_item)
fLayoutData->text_view_layout_item = new TextViewLayoutItem(this);
return fLayoutData->text_view_layout_item;
}
void
BTextControl::DoLayout()
{
// Bail out, if we shan't do layout.
if (!(Flags() & B_SUPPORTS_LAYOUT))
return;
CALLED();
// If the user set a layout, we let the base class version call its
// hook.
if (GetLayout()) {
BView::DoLayout();
return;
}
_ValidateLayoutData();
// validate current size
BSize size(Bounds().Size());
if (size.width < fLayoutData->min.width)
size.width = fLayoutData->min.width;
if (size.height < fLayoutData->min.height)
size.height = fLayoutData->min.height;
// divider
float divider = 0;
if (fLayoutData->label_layout_item && fLayoutData->text_view_layout_item) {
// We have layout items. They define the divider location.
divider = fLayoutData->text_view_layout_item->Frame().left
- fLayoutData->label_layout_item->Frame().left;
} else {
if (fLayoutData->label_width > 0)
divider = fLayoutData->label_width + 5;
}
// text view
BRect dirty(fText->Frame());
BRect textFrame(divider + kFrameMargin, kFrameMargin,
size.width - kFrameMargin, size.height - kFrameMargin);
// place the text view and set the divider
BLayoutUtils::AlignInFrame(fText, textFrame);
fDivider = divider;
// invalidate dirty region
dirty = dirty | fText->Frame();
dirty.InsetBy(-kFrameMargin, -kFrameMargin);
Invalidate(dirty);
}
// #pragma mark -
status_t
2008-10-15 09:29:06 +00:00
BTextControl::Perform(perform_code code, void* _data)
{
switch (code) {
case PERFORM_CODE_MIN_SIZE:
((perform_data_min_size*)_data)->return_value
= BTextControl::MinSize();
return B_OK;
case PERFORM_CODE_MAX_SIZE:
((perform_data_max_size*)_data)->return_value
= BTextControl::MaxSize();
return B_OK;
case PERFORM_CODE_PREFERRED_SIZE:
((perform_data_preferred_size*)_data)->return_value
= BTextControl::PreferredSize();
return B_OK;
case PERFORM_CODE_LAYOUT_ALIGNMENT:
((perform_data_layout_alignment*)_data)->return_value
= BTextControl::LayoutAlignment();
return B_OK;
case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH:
((perform_data_has_height_for_width*)_data)->return_value
= BTextControl::HasHeightForWidth();
return B_OK;
case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH:
{
perform_data_get_height_for_width* data
= (perform_data_get_height_for_width*)_data;
BTextControl::GetHeightForWidth(data->width, &data->min, &data->max,
&data->preferred);
return B_OK;
}
case PERFORM_CODE_SET_LAYOUT:
{
perform_data_set_layout* data = (perform_data_set_layout*)_data;
BTextControl::SetLayout(data->layout);
return B_OK;
}
case PERFORM_CODE_LAYOUT_INVALIDATED:
2008-10-15 09:29:06 +00:00
{
perform_data_layout_invalidated* data
= (perform_data_layout_invalidated*)_data;
BTextControl::LayoutInvalidated(data->descendants);
2008-10-15 09:29:06 +00:00
return B_OK;
}
case PERFORM_CODE_DO_LAYOUT:
{
BTextControl::DoLayout();
return B_OK;
}
case PERFORM_CODE_ALL_UNARCHIVED:
{
perform_data_all_unarchived* data
= (perform_data_all_unarchived*)_data;
data->return_value = BTextControl::AllUnarchived(data->archive);
return B_OK;
}
case PERFORM_CODE_ALL_ARCHIVED:
{
perform_data_all_archived* data
= (perform_data_all_archived*)_data;
data->return_value = BTextControl::AllArchived(data->archive);
return B_OK;
}
2008-10-15 09:29:06 +00:00
}
return BControl::Perform(code, _data);
2006-08-26 16:21:15 +00:00
}
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 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;
fLayoutData = new LayoutData(bounds.Width(), bounds.Height());
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
}
void
BTextControl::_InitText(const char* initialText, const BMessage* archive)
{
if (archive)
2008-02-24 20:39:29 +00:00
fText = static_cast<BPrivate::_BTextInput_*>(FindView("_input_"));
if (fText == NULL) {
BRect bounds(Bounds());
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(kFrameMargin, kFrameMargin);
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,
B_FOLLOW_ALL, B_WILL_DRAW | B_FRAME_EVENTS
| (Flags() & B_NAVIGABLE));
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();
}
// Although this is not strictly initializing the text view,
// it cannot be done while fText is NULL, so it resides here.
if (archive) {
int32 labelAlignment = B_ALIGN_LEFT;
int32 textAlignment = B_ALIGN_LEFT;
status_t err = B_OK;
if (archive->HasInt32("_a_label"))
err = archive->FindInt32("_a_label", &labelAlignment);
if (err == B_OK && archive->HasInt32("_a_text"))
err = archive->FindInt32("_a_text", &textAlignment);
SetAlignment((alignment)labelAlignment, (alignment)textAlignment);
}
uint32 navigableFlags = Flags() & B_NAVIGABLE;
if (navigableFlags != 0)
BView::SetFlags(Flags() & ~B_NAVIGABLE);
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();
_ValidateLayoutData();
2006-08-26 16:21:15 +00:00
ResizeTo(Bounds().Width(), fLayoutData->min.height);
2006-08-26 16:21:15 +00:00
_LayoutTextView();
2006-08-26 16:21:15 +00:00
}
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(kFrameMargin, kFrameMargin);
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()
{
CALLED();
if (fLayoutData->label_layout_item && fLayoutData->text_view_layout_item) {
BRect labelFrame = fLayoutData->label_layout_item->Frame();
BRect textFrame = fLayoutData->text_view_layout_item->Frame();
// update divider
fDivider = textFrame.left - labelFrame.left;
2006-08-26 16:21:15 +00:00
MoveTo(labelFrame.left, labelFrame.top);
BSize oldSize = Bounds().Size();
2006-08-26 16:21:15 +00:00
ResizeTo(textFrame.left + textFrame.Width() - labelFrame.left,
textFrame.top + textFrame.Height() - labelFrame.top);
BSize newSize = Bounds().Size();
// If the size changes, ResizeTo() will trigger a relayout, otherwise
// we need to do that explicitly.
if (newSize != oldSize)
Relayout();
}
}
void
BTextControl::_ValidateLayoutData()
{
CALLED();
if (fLayoutData->valid)
return;
// cache font height
font_height& fh = fLayoutData->font_info;
GetFontHeight(&fh);
if (Label() != NULL) {
fLayoutData->label_width = ceilf(StringWidth(Label()));
fLayoutData->label_height = ceilf(fh.ascent) + ceilf(fh.descent);
} else {
fLayoutData->label_width = 0;
fLayoutData->label_height = 0;
2006-08-26 16:21:15 +00:00
}
// compute the minimal divider
float divider = 0;
if (fLayoutData->label_width > 0)
divider = fLayoutData->label_width + 5;
// If we shan't do real layout, we let the current divider take influence.
if (!(Flags() & B_SUPPORTS_LAYOUT))
divider = max_c(divider, fDivider);
// get the minimal (== preferred) text view size
fLayoutData->text_view_min = fText->MinSize();
TRACE("text view min width: %.2f\n", fLayoutData->text_view_min.width);
// compute our minimal (== preferred) size
BSize min(fLayoutData->text_view_min);
min.width += 2 * kFrameMargin;
min.height += 2 * kFrameMargin;
if (divider > 0)
min.width += divider;
if (fLayoutData->label_height > min.height)
min.height = fLayoutData->label_height;
fLayoutData->min = min;
fLayoutData->valid = true;
2010-04-13 13:36:49 +00:00
ResetLayoutInvalidation();
TRACE("width: %.2f, height: %.2f\n", min.width, min.height);
2006-08-26 16:21:15 +00:00
}
// #pragma mark -
BTextControl::LabelLayoutItem::LabelLayoutItem(BTextControl* parent)
:
fParent(parent),
fFrame()
{
}
BTextControl::LabelLayoutItem::LabelLayoutItem(BMessage* from)
:
BAbstractLayoutItem(from),
fParent(NULL),
fFrame()
2006-08-26 16:21:15 +00:00
{
from->FindRect(kFrameField, &fFrame);
2006-08-26 16:21:15 +00:00
}
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();
}
void
BTextControl::LabelLayoutItem::SetParent(BTextControl* parent)
{
fParent = parent;
}
2006-08-26 16:21:15 +00:00
BView*
BTextControl::LabelLayoutItem::View()
{
return fParent;
}
BSize
BTextControl::LabelLayoutItem::BaseMinSize()
{
fParent->_ValidateLayoutData();
2006-08-26 16:21:15 +00:00
if (!fParent->Label())
return BSize(-1, -1);
2006-08-26 16:21:15 +00:00
return BSize(fParent->fLayoutData->label_width + 5,
fParent->fLayoutData->label_height);
2006-08-26 16:21:15 +00:00
}
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);
}
status_t
BTextControl::LabelLayoutItem::Archive(BMessage* into, bool deep) const
{
BArchiver archiver(into);
status_t err = BAbstractLayoutItem::Archive(into, deep);
if (err == B_OK)
err = into->AddRect(kFrameField, fFrame);
return archiver.Finish(err);
}
BArchivable*
BTextControl::LabelLayoutItem::Instantiate(BMessage* from)
{
if (validate_instantiation(from, "BTextControl::LabelLayoutItem"))
return new LabelLayoutItem(from);
return NULL;
}
2006-08-26 16:21:15 +00:00
// #pragma mark -
BTextControl::TextViewLayoutItem::TextViewLayoutItem(BTextControl* parent)
:
fParent(parent),
fFrame()
2006-08-26 16:21:15 +00:00
{
// by default the part right of the divider shall have an unlimited maximum
// width
SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, B_SIZE_UNSET));
2006-08-26 16:21:15 +00:00
}
BTextControl::TextViewLayoutItem::TextViewLayoutItem(BMessage* from)
:
BAbstractLayoutItem(from),
fParent(NULL),
fFrame()
{
from->FindRect(kFrameField, &fFrame);
}
2006-08-26 16:21:15 +00:00
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();
}
void
BTextControl::TextViewLayoutItem::SetParent(BTextControl* parent)
{
fParent = parent;
}
2006-08-26 16:21:15 +00:00
BView*
BTextControl::TextViewLayoutItem::View()
{
return fParent;
}
BSize
BTextControl::TextViewLayoutItem::BaseMinSize()
{
fParent->_ValidateLayoutData();
2006-08-26 16:21:15 +00:00
BSize size = fParent->fLayoutData->text_view_min;
size.width += 2 * kFrameMargin;
size.height += 2 * kFrameMargin;
2006-08-26 16:21:15 +00:00
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);
}
status_t
BTextControl::TextViewLayoutItem::Archive(BMessage* into, bool deep) const
{
BArchiver archiver(into);
status_t err = BAbstractLayoutItem::Archive(into, deep);
if (err == B_OK)
err = into->AddRect(kFrameField, fFrame);
return archiver.Finish(err);
}
BArchivable*
BTextControl::TextViewLayoutItem::Instantiate(BMessage* from)
{
if (validate_instantiation(from, "BTextControl::TextViewLayoutItem"))
return new TextViewLayoutItem(from);
return NULL;
}