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

629 lines
12 KiB
C++
Raw Normal View History

2002-07-09 12:24:59 +00:00
//------------------------------------------------------------------------------
2004-10-18 10:21:46 +00:00
// Copyright (c) 2001-2004, Haiku, Inc.
2002-07-09 12:24:59 +00:00
//
// Permission is hereby granted, free of charge, to any person obtaining a
// 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: TextControl.cpp
// Author: Frans van Nispen ([email protected])
// Description: BTextControl displays text that can act like a control.
//------------------------------------------------------------------------------
#include <stdio.h>
2004-10-18 10:21:46 +00:00
#include <Message.h>
2002-07-09 12:24:59 +00:00
#include <TextControl.h>
#include <Window.h>
#include "TextInput.h"
2003-06-16 07:27:24 +00:00
BTextControl::BTextControl(BRect frame, const char *name, const char *label,
const char *text, BMessage *message, uint32 mask,
2002-07-09 12:24:59 +00:00
uint32 flags)
2003-06-16 07:27:24 +00:00
: BControl(frame, name, label, message, mask, flags | B_FRAME_EVENTS)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
InitData(label, text);
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
float lineHeight = fText->LineHeight(0);
2002-07-09 12:24:59 +00:00
ResizeTo(Bounds().Width(), lineHeight + 8);
2002-07-09 12:24:59 +00:00
fText->ResizeTo(fText->Bounds().Width(), lineHeight + 4);
fText->MoveTo(fText->Frame().left, 2.0f);
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
2003-06-16 07:27:24 +00:00
BTextControl::BTextControl(BMessage *data)
2002-07-09 12:24:59 +00:00
: BControl(data)
{
2003-06-16 07:27:24 +00:00
InitData(Label(), NULL, data);
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
int32 _a_label = B_ALIGN_LEFT;
int32 _a_text = B_ALIGN_LEFT;
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
if (data->HasInt32("_a_label"))
data->FindInt32("_a_label", &_a_label);
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
if (data->HasInt32("_a_text"))
data->FindInt32("_a_text", &_a_text);
SetAlignment((alignment)_a_label, (alignment)_a_text);
if (data->HasFloat("_divide"))
data->FindFloat("_a_text", &fDivider);
2004-10-18 10:21:46 +00:00
if (data->HasMessage("_mod_msg")) {
2003-06-16 07:27:24 +00:00
BMessage *_mod_msg = new BMessage;
data->FindMessage("_mod_msg", _mod_msg);
SetModificationMessage(_mod_msg);
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);
else
2002-07-09 12:24:59 +00:00
return NULL;
}
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
{
2003-06-16 07:27:24 +00:00
BView::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
2004-10-18 10:21:46 +00:00
data->AddInt32("_a_label", labelAlignment);
data->AddInt32("_a_text", textAlignment);
2003-06-16 07:27:24 +00:00
data->AddFloat("_divide", Divider());
if (ModificationMessage())
data->AddMessage("_mod_msg", ModificationMessage());
return B_OK;
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;
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
{
2004-10-18 10:21:46 +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
{
2004-10-18 10:21:46 +00:00
float dx = fDivider - dividingLine;
2003-06-16 07:27:24 +00:00
2004-10-18 10:21:46 +00:00
fDivider = dividingLine;
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
fText->MoveBy(-dx, 0.0f);
fText->ResizeBy(dx, 0.0f);
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
{
2003-06-16 07:27:24 +00:00
rgb_color no_tint = ui_color(B_PANEL_BACKGROUND_COLOR),
lighten1 = tint_color(no_tint, B_LIGHTEN_1_TINT),
lighten2 = tint_color(no_tint, B_LIGHTEN_2_TINT),
lightenmax = tint_color(no_tint, B_LIGHTEN_MAX_TINT),
darken1 = tint_color(no_tint, B_DARKEN_1_TINT),
darken2 = tint_color(no_tint, B_DARKEN_2_TINT),
darken4 = tint_color(no_tint, B_DARKEN_4_TINT),
darkenmax = tint_color(no_tint, B_DARKEN_MAX_TINT),
nav = ui_color(B_KEYBOARD_NAVIGATION_COLOR);
bool enabled = IsEnabled();
bool active = false;
if (fText->IsFocus() && Window()->IsActive())
active = true;
BRect rect(fText->Frame());
rect.InsetBy(-1.0f, -1.0f);
2004-10-18 10:21:46 +00:00
if (active) {
2003-06-16 07:27:24 +00:00
SetHighColor(nav);
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(no_tint);
StrokeLine(BPoint(rect.left + 1.0f, rect.bottom), rect.RightBottom());
StrokeLine(BPoint(rect.right, rect.top + 1.0f));
}
rect.InsetBy(-1.0f, -1.0f);
if (enabled)
SetHighColor(darken1);
else
SetHighColor(no_tint);
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());
2002-07-09 12:24:59 +00:00
2004-10-18 10:21:46 +00:00
if (Label()) {
2003-06-16 07:27:24 +00:00
font_height fh;
GetFontHeight(&fh);
float y = (float)ceil(fh.ascent + fh.descent + fh.leading) + 2.0f;
2002-07-09 12:24:59 +00:00
float x;
2003-06-16 07:27:24 +00:00
2004-10-18 10:21:46 +00:00
switch (fLabelAlign) {
2002-07-09 12:24:59 +00:00
case B_ALIGN_RIGHT:
2003-06-16 07:27:24 +00:00
x = fDivider - StringWidth(Label()) - 3.0f;
2002-07-09 12:24:59 +00:00
break;
case B_ALIGN_CENTER:
2003-06-16 07:27:24 +00:00
x = fDivider - StringWidth(Label()) / 2.0f;
2002-07-09 12:24:59 +00:00
break;
default:
x = 3.0f;
break;
}
SetHighColor(tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
IsEnabled() ? B_DARKEN_MAX_TINT : B_DISABLED_LABEL_TINT));
DrawString(Label(), BPoint(x, y));
}
}
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);
fText->SelectAll();
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
bool enabled = IsEnabled();
rgb_color textColor;
rgb_color color = HighColor();
BFont font;
fText->GetFontAndColor(0, &font, &color);
if (enabled)
textColor = color;
else
textColor = tint_color(color, B_LIGHTEN_2_TINT);
fText->SetFontAndColor(&font, B_FONT_ALL, &textColor);
2004-10-18 10:21:46 +00:00
if (enabled) {
2003-06-16 07:27:24 +00:00
color.red = 255;
color.green = 255;
color.blue = 255;
2004-10-18 10:21:46 +00:00
} else
2003-06-16 07:27:24 +00:00
color = tint_color(color, B_LIGHTEN_2_TINT);
2002-07-09 12:24:59 +00:00
2003-06-16 07:27:24 +00:00
fText->SetViewColor(color);
fText->SetLowColor(color);
fText->MakeEditable(enabled);
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
{
2003-06-16 07:27:24 +00:00
fText->MakeFocus(state);
if (state)
fText->SelectAll();
2002-07-09 12:24:59 +00:00
}
2004-10-18 10:21:46 +00:00
void
BTextControl::SetEnabled(bool state)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
if (IsEnabled() == state)
return;
2004-10-18 10:21:46 +00:00
if (Window()) {
2003-06-16 07:27:24 +00:00
fText->MakeEditable(state);
rgb_color textColor;
rgb_color color = {0, 0, 0, 255};
BFont font;
fText->GetFontAndColor(0, &font, &color);
if (state)
textColor = color;
else
textColor = tint_color(color, B_DISABLED_LABEL_TINT);
fText->SetFontAndColor(&font, B_FONT_ALL, &textColor);
2004-10-18 10:21:46 +00:00
if (state) {
2003-06-16 07:27:24 +00:00
color.red = 255;
color.green = 255;
color.blue = 255;
2004-10-18 10:21:46 +00:00
} else
2003-06-16 07:27:24 +00:00
color = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
B_LIGHTEN_2_TINT);
fText->SetViewColor(color);
fText->SetLowColor(color);
fText->Invalidate();
Window()->UpdateIfNeeded();
}
2002-07-09 12:24:59 +00:00
BControl::SetEnabled(state);
}
2004-10-18 10:21:46 +00:00
void
BTextControl::GetPreferredSize(float *width, float *height)
2002-07-09 12:24:59 +00:00
{
BFont font;
GetFont(&font);
font_height fh;
font.GetHeight(&fh);
2004-10-18 10:21:46 +00:00
if (height)
*height = (float)ceil(fh.ascent + fh.descent + fh.leading) + 7.0f;
2002-07-09 12:24:59 +00:00
// TODO: this one I need to find out
2004-10-18 10:21:46 +00:00
if (width)
*width = 4.0f + (float)ceil(font.StringWidth(Label()))*2.0f;
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
{
2004-10-18 10:21:46 +00:00
float width, height;
GetPreferredSize(&width, &height);
BView::ResizeTo(width, height);
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
{
2004-10-18 10:21:46 +00:00
if (!fSkipSetFlags) {
2003-06-16 07:27:24 +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
2004-10-18 10:21:46 +00:00
if (fText->Flags() & B_NAVIGABLE) {
2003-06-16 07:27:24 +00:00
if (!(flags & B_NAVIGABLE))
fText->SetFlags(fText->Flags() & ~B_NAVIGABLE);
2004-10-18 10:21:46 +00:00
} else {
2003-06-16 07:27:24 +00:00
if (flags & B_NAVIGABLE)
fText->SetFlags(fText->Flags() | B_NAVIGABLE);
}
// 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 newWidth, float newHeight)
2002-07-09 12:24:59 +00:00
{
BControl::FrameResized(newWidth, newHeight);
}
2004-10-18 10:21:46 +00:00
void
BTextControl::WindowActivated(bool active)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
if (fText->IsFocus())
2005-04-27 17:25:29 +00:00
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
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::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 *initial_text,
2003-06-16 07:27:24 +00:00
BMessage *data)
2002-07-09 12:24:59 +00:00
{
2003-06-16 07:27:24 +00:00
BRect bounds(Bounds());
fText = NULL;
//fLabel = NULL;
fModificationMessage = NULL;
fLabelAlign = B_ALIGN_LEFT;
fDivider = 0.0f;
fPrevWidth = 0;
fPrevHeight = 0;
//fClean = true;
fSkipSetFlags = false;
int32 flags = 0;
BFont font(be_plain_font);
2003-06-16 07:27:24 +00:00
if (!data || !data->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 (!data || !data->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 = bounds.Width() / 2.0f;
2004-10-18 10:21:46 +00:00
if (Flags() & B_NAVIGABLE) {
2003-06-16 07:27:24 +00:00
fSkipSetFlags = true;
SetFlags(Flags() & ~B_NAVIGABLE);
fSkipSetFlags = false;
}
if (data)
2004-10-18 10:21:46 +00:00
fText = static_cast<_BTextInput_ *>(FindView("_input_"));
else {
BRect frame(fDivider, bounds.top + 2.0f,
bounds.right - 2.0f, bounds.bottom - 2.0f);
2003-06-16 07:27:24 +00:00
BRect textRect(frame.OffsetToCopy(0.0f, 0.0f));
textRect.InsetBy(2.0, 2.0);
2003-06-16 07:27:24 +00:00
fText = new _BTextInput_(frame, textRect,
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP,
B_WILL_DRAW | B_FRAME_EVENTS | B_NAVIGABLE);
2003-06-16 07:27:24 +00:00
AddChild(fText);
SetText(initial_text);
fText->SetAlignment(B_ALIGN_LEFT);
fText->AlignTextRect();
}
2002-07-09 12:24:59 +00:00
}