BTextControl:

* Placed _BTextInput_ into BPrivate namespace.
* Made _BTextInput_::AlignTextRect() smarter, it centers the line vertically,
  for the case that the BTextControl has a larger label font. Improved insets
  for asthetics.
* Used _BTextInput_::AlignTextRect() consistently in BTextControl, no more
  custom calls to SetTextRect(). Account for minimum vertical inset of 2
  pixels in GetPreferredSize().
* Consistendly select all text when gaining focus in _BTextInput_.
* Override MouseDown() in case the control did not have focus before, or else
  BTextView::MouseDown() will deselct the text again and place the cursor.
  (in line with BeOS behavior)
* Removed unused fBool member from _BTextInput_ and other cleanup.

BTextView:
* Reimplemented BTextView::_AutoResize() so that it works well with
  BTextControl and autoscrolling when the alignment is not B_ALIGN_LEFT.
  I needed two new members for this, fLeftInset and fRightInset which are
  the original insets from the fTextRects. It might currently be broken
  for renaming things in Tracker, I will have to check. _AutoResize() no
  longer messes up the fTextRect insets.
* Fixed stray carrets sometimes being left over, mostly when auto scrolling,
  but I observed them in other cases as well.
* Prevent negative scrolling offsets when autoscrolling. Fixes weird scrolling
  offsets when navigating to the left.
* Reset scrolling to B_ORIGIN when SetText() is called. Fixes for example
  starting to type in the middle of the control in Vision when entering new
  text and autoscrolling was triggered before.



git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@24101 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stephan Aßmus
2008-02-24 20:39:29 +00:00
parent 35b450cd25
commit b8872c02c1
6 changed files with 115 additions and 140 deletions
+3 -2
View File
@@ -10,8 +10,9 @@
#include <TextView.h> #include <TextView.h>
class BLayoutItem; class BLayoutItem;
namespace BPrivate {
class _BTextInput_; class _BTextInput_;
}
class BTextControl : public BControl { class BTextControl : public BControl {
public: public:
@@ -105,7 +106,7 @@ class BTextControl : public BControl {
void _UpdateFrame(); void _UpdateFrame();
private: private:
_BTextInput_* fText; BPrivate::_BTextInput_* fText;
char* fLabel; char* fLabel;
BMessage* fModificationMessage; BMessage* fModificationMessage;
alignment fLabelAlign; alignment fLabelAlign;
+3 -1
View File
@@ -315,6 +315,8 @@ class BTextView : public BView {
_BLineBuffer_* fLines; _BLineBuffer_* fLines;
_BStyleBuffer_* fStyles; _BStyleBuffer_* fStyles;
BRect fTextRect; BRect fTextRect;
float fLeftInset;
float fRightInset;
int32 fSelStart; int32 fSelStart;
int32 fSelEnd; int32 fSelEnd;
bool fCaretVisible; bool fCaretVisible;
@@ -345,7 +347,7 @@ class BTextView : public BView {
BPoint fWhere; BPoint fWhere;
_BTextTrackState_* fTrackingMouse; _BTextTrackState_* fTrackingMouse;
_BTextChangeResult_* fTextChange; _BTextChangeResult_* fTextChange;
uint32 _reserved[9]; uint32 _reserved[7];
static _BWidthBuffer_* sWidths; static _BWidthBuffer_* sWidths;
static sem_id sWidthSem; static sem_id sWidthSem;
+12 -20
View File
@@ -333,24 +333,26 @@ BTextControl::Draw(BRect updateRect)
// label // label
if (Label()) { if (Label()) {
font_height fontHeight; font_height fh;
GetFontHeight(&fontHeight); GetFontHeight(&fh);
float y = fontHeight.ascent + fText->Frame().top + 1; float y = Bounds().top
+ (Bounds().Height() + 1 - fh.ascent - fh.descent) / 2
+ fh.ascent;
float x; float x;
float labelWidth = StringWidth(Label()); float labelWidth = StringWidth(Label());
switch (fLabelAlign) { switch (fLabelAlign) {
case B_ALIGN_RIGHT: case B_ALIGN_RIGHT:
x = fDivider - labelWidth - 3.0f; x = fDivider - labelWidth - 3.0;
break; break;
case B_ALIGN_CENTER: case B_ALIGN_CENTER:
x = fDivider - labelWidth / 2.0f; x = fDivider - labelWidth / 2.0;
break; break;
default: default:
x = 3.0f; x = 0.0;
break; break;
} }
@@ -373,7 +375,6 @@ BTextControl::MouseDown(BPoint where)
{ {
if (!fText->IsFocus()) { if (!fText->IsFocus()) {
fText->MakeFocus(true); fText->MakeFocus(true);
fText->SelectAll();
} }
} }
@@ -428,7 +429,7 @@ BTextControl::GetPreferredSize(float *_width, float *_height)
GetFontHeight(&fontHeight); GetFontHeight(&fontHeight);
float labelHeight = ceil(fontHeight.ascent + fontHeight.descent float labelHeight = ceil(fontHeight.ascent + fontHeight.descent
+ fontHeight.leading); + fontHeight.leading);
float textHeight = ceilf(fText->LineHeight(0)) + 4.0; float textHeight = ceilf(fText->LineHeight(0) + 1.0) + 4.0;
*_height = max_c(labelHeight, textHeight); *_height = max_c(labelHeight, textHeight);
} }
@@ -730,7 +731,7 @@ BTextControl::_InitData(const char* label, const char* initialText,
} }
if (archive) if (archive)
fText = static_cast<_BTextInput_ *>(FindView("_input_")); fText = static_cast<BPrivate::_BTextInput_*>(FindView("_input_"));
else { else {
BRect frame(fDivider, bounds.top, BRect frame(fDivider, bounds.top,
bounds.right, bounds.bottom); bounds.right, bounds.bottom);
@@ -739,7 +740,7 @@ BTextControl::_InitData(const char* label, const char* initialText,
frame.InsetBy(2.0, 3.0); frame.InsetBy(2.0, 3.0);
BRect textRect(frame.OffsetToCopy(B_ORIGIN)); BRect textRect(frame.OffsetToCopy(B_ORIGIN));
fText = new _BTextInput_(frame, textRect, fText = new BPrivate::_BTextInput_(frame, textRect,
B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP, B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP,
B_WILL_DRAW | B_FRAME_EVENTS | navigableFlags); B_WILL_DRAW | B_FRAME_EVENTS | navigableFlags);
AddChild(fText); AddChild(fText);
@@ -775,16 +776,7 @@ BTextControl::_LayoutTextView()
frame.InsetBy(2.0, 2.0); frame.InsetBy(2.0, 2.0);
fText->MoveTo(frame.left, frame.top); fText->MoveTo(frame.left, frame.top);
fText->ResizeTo(frame.Width(), frame.Height()); fText->ResizeTo(frame.Width(), frame.Height());
fText->AlignTextRect();
BRect textRect(frame.OffsetToCopy(B_ORIGIN));
// the label font could require the control to be higher than
// necessary for the text view, we compensate this by layouting
// the text rect to be in the middle, normally this means there
// is one pixel spacing on each side
float lineHeight = ceilf(fText->LineHeight(0));
textRect.InsetBy(1, floorf((frame.Height() - lineHeight) / 2));
fText->SetTextRect(textRect);
} }
+40 -35
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2007, Haiku Inc. * Copyright 2001-2008, Haiku Inc. All rights reserved.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -20,20 +20,21 @@
#include "TextInput.h" #include "TextInput.h"
namespace BPrivate {
_BTextInput_::_BTextInput_(BRect frame, BRect textRect, uint32 resizeMask, _BTextInput_::_BTextInput_(BRect frame, BRect textRect, uint32 resizeMask,
uint32 flags) uint32 flags)
: BTextView(frame, "_input_", textRect, resizeMask, flags), : BTextView(frame, "_input_", textRect, resizeMask, flags)
fPreviousText(NULL), , fPreviousText(NULL)
fBool(false)
{ {
MakeResizable(true); MakeResizable(true);
} }
_BTextInput_::_BTextInput_(BMessage *archive) _BTextInput_::_BTextInput_(BMessage* archive)
: BTextView(archive), : BTextView(archive)
fPreviousText(NULL), , fPreviousText(NULL)
fBool(false)
{ {
MakeResizable(true); MakeResizable(true);
} }
@@ -62,6 +63,19 @@ _BTextInput_::Archive(BMessage *data, bool deep) const
} }
void
_BTextInput_::MouseDown(BPoint where)
{
if (!IsFocus()) {
MakeFocus(true);
return;
}
// only pass through to base class if we already have focus
BTextView::MouseDown(where);
}
void void
_BTextInput_::FrameResized(float width, float height) _BTextInput_::FrameResized(float width, float height)
{ {
@@ -116,50 +130,39 @@ _BTextInput_::MakeFocus(bool state)
if (state) { if (state) {
SetInitialText(); SetInitialText();
SelectAll();
fBool = true;
if (Window()) {
BMessage *message = Window()->CurrentMessage();
if (message && message->what == B_KEY_DOWN)
SelectAll();
}
} else { } else {
if (strcmp(Text(), fPreviousText) != 0) if (strcmp(Text(), fPreviousText) != 0)
TextControl()->Invoke(); TextControl()->Invoke();
free(fPreviousText); free(fPreviousText);
fPreviousText = NULL; fPreviousText = NULL;
fBool = false;
if (Window()) {
BMessage *message = Window()->CurrentMessage();
if (message && message->what == B_MOUSE_DOWN)
Select(0, 0);
}
} }
if (Window()) { // if (Window()) {
// TODO: why do we have to invalidate here? // TODO: why do we have to invalidate here?
// I'm leaving this in, but it looks suspicious... :-) // I'm leaving this in, but it looks suspicious... :-)
Invalidate(Bounds()); // Invalidate(Bounds());
if (BTextControl* parent = dynamic_cast<BTextControl*>(Parent())) { if (BTextControl* parent = dynamic_cast<BTextControl*>(Parent())) {
BRect frame = Frame(); BRect frame = Frame();
frame.InsetBy(-1.0, -1.0); frame.InsetBy(-1.0, -1.0);
parent->Invalidate(frame); parent->Invalidate(frame);
} }
} // }
} }
void void
_BTextInput_::AlignTextRect() _BTextInput_::AlignTextRect()
{ {
// TODO: just to get something working, it wouldn't be correct for // the label font could require the control to be higher than
// scrolled views // necessary for the text view, we compensate this by layouting
// the text rect to be in the middle, normally this means there
// is one pixel spacing on each side
BRect textRect(Bounds()); BRect textRect(Bounds());
textRect.left = 0.0;
float vInset = max_c(1, floorf((textRect.Height() - LineHeight(0)) / 2.0));
textRect.InsetBy(2, vInset);
SetTextRect(textRect); SetTextRect(textRect);
} }
@@ -167,10 +170,8 @@ _BTextInput_::AlignTextRect()
void void
_BTextInput_::SetInitialText() _BTextInput_::SetInitialText()
{ {
if (fPreviousText) { free(fPreviousText);
free(fPreviousText); fPreviousText = NULL;
fPreviousText = NULL;
}
if (Text()) if (Text())
fPreviousText = strdup(Text()); fPreviousText = strdup(Text());
@@ -237,3 +238,7 @@ _BTextInput_::TextControl()
return textControl; return textControl;
} }
} // namespace BPrivate
+18 -46
View File
@@ -1,60 +1,35 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2002, OpenBeOS * Copyright 2001-2008, Haiku Inc. All rights reserved.
// * 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 (xlr8@tref.nl)
// 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 BTextView derivative owned by an instance of BTextControl.
//
// 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: TextInput.h
// Author: Frans van Nispen ([email protected])
// Description: The BTextView derivative owned by an instance of
// BTextControl.
//------------------------------------------------------------------------------
#ifndef _TEXT_CONTROLI_H #ifndef _TEXT_CONTROLI_H
#define _TEXT_CONTROLI_H #define _TEXT_CONTROLI_H
// Standard Includes -----------------------------------------------------------
// System Includes -------------------------------------------------------------
#include <TextView.h> #include <TextView.h>
// Project Includes ------------------------------------------------------------
// Local Includes --------------------------------------------------------------
// Local Defines ---------------------------------------------------------------
// Globals ---------------------------------------------------------------------
class BTextControl; class BTextControl;
// _BTextInput_ class ---------------------------------------------------------- namespace BPrivate {
class _BTextInput_ : public BTextView { class _BTextInput_ : public BTextView {
public: public:
_BTextInput_(BRect frame, BRect textRect, _BTextInput_(BRect frame, BRect textRect,
uint32 resizeMask, uint32 resizeMask,
uint32 flags = B_WILL_DRAW | B_PULSE_NEEDED); uint32 flags = B_WILL_DRAW | B_PULSE_NEEDED);
_BTextInput_(BMessage *data); _BTextInput_(BMessage *data);
~_BTextInput_(); virtual ~_BTextInput_();
static BArchivable* Instantiate(BMessage *data); static BArchivable* Instantiate(BMessage *data);
virtual status_t Archive(BMessage *data, bool deep = true) const; virtual status_t Archive(BMessage *data, bool deep = true) const;
virtual void MouseDown(BPoint where);
virtual void FrameResized(float width, float height); virtual void FrameResized(float width, float height);
virtual void KeyDown(const char *bytes, int32 numBytes); virtual void KeyDown(const char *bytes, int32 numBytes);
virtual void MakeFocus(bool focusState = true); virtual void MakeFocus(bool focusState = true);
@@ -78,14 +53,11 @@ private:
char *fPreviousText; char *fPreviousText;
bool fBool; bool fBool;
}; };
//------------------------------------------------------------------------------
} // namespace BPrivate
using namespace BPrivate;
#endif // _TEXT_CONTROLI_H #endif // _TEXT_CONTROLI_H
/*
* $Log $
*
* $Id $
*
*/
+39 -36
View File
@@ -400,8 +400,7 @@ BTextView::AttachedToWindow()
fDragOffset = -1; fDragOffset = -1;
fActive = false; fActive = false;
if (fResizable) _AutoResize(true);
_AutoResize(true);
_UpdateScrollbars(); _UpdateScrollbars();
@@ -995,7 +994,7 @@ BTextView::SetText(const char *inText, int32 inLength, const text_run_array *inR
// recalc line breaks and draw the text // recalc line breaks and draw the text
_Refresh(0, inLength, true, false); _Refresh(0, inLength, true, false);
fClickOffset = fSelStart = fSelEnd = 0; fClickOffset = fSelStart = fSelEnd = 0;
ScrollToOffset(fSelStart); ScrollTo(B_ORIGIN);
// draw the caret // draw the caret
if (fActive) if (fActive)
@@ -1917,13 +1916,16 @@ BTextView::_ScrollToOffset(int32 inOffset, bool useHorizontal, bool useVertical)
float lineHeight = 0.0; float lineHeight = 0.0;
float xDiff = 0.0; float xDiff = 0.0;
float yDiff = 0.0; float yDiff = 0.0;
BPoint point = PointAt(inOffset, &lineHeight); BPoint point = PointAt(inOffset, &lineHeight);
if (useHorizontal) { if (useHorizontal) {
if (point.x < bounds.left) if (point.x < bounds.left)
xDiff = point.x - bounds.left - bounds.IntegerWidth() / 2; xDiff = point.x - bounds.left - bounds.IntegerWidth() / 2;
else if (point.x >= bounds.right) else if (point.x >= bounds.right)
xDiff = point.x - bounds.right + bounds.IntegerWidth() / 2; xDiff = point.x - bounds.right + bounds.IntegerWidth() / 2;
// prevent negative scroll offset
if (bounds.left + xDiff < 0.0)
xDiff = -bounds.left;
} }
if (useVertical) { if (useVertical) {
@@ -1931,6 +1933,9 @@ BTextView::_ScrollToOffset(int32 inOffset, bool useHorizontal, bool useVertical)
yDiff = point.y - bounds.top - bounds.IntegerHeight() / 2; yDiff = point.y - bounds.top - bounds.IntegerHeight() / 2;
else if (point.y >= bounds.bottom) else if (point.y >= bounds.bottom)
yDiff = point.y - bounds.bottom + bounds.IntegerHeight() / 2; yDiff = point.y - bounds.bottom + bounds.IntegerHeight() / 2;
// prevent negative scroll offset
if (bounds.top + yDiff < 0.0)
yDiff = -bounds.top;
} }
ScrollBy(xDiff, yDiff); ScrollBy(xDiff, yDiff);
@@ -1978,6 +1983,8 @@ BTextView::SetTextRect(BRect rect)
return; return;
fTextRect = rect; fTextRect = rect;
fLeftInset = fTextRect.left;
fRightInset = Bounds().right - fTextRect.right;
if (Window() != NULL) { if (Window() != NULL) {
Invalidate(); Invalidate();
@@ -2708,6 +2715,8 @@ BTextView::_InitObject(BRect textRect, const BFont *initialFont,
// to have less code duplication, and a single place where to do changes // to have less code duplication, and a single place where to do changes
// if needed., // if needed.,
fTextRect = textRect; fTextRect = textRect;
fLeftInset = fTextRect.left;
fRightInset = Bounds().right - fTextRect.right;
fSelStart = fSelEnd = 0; fSelStart = fSelEnd = 0;
fCaretVisible = false; fCaretVisible = false;
fCaretTime = 0; fCaretTime = 0;
@@ -3117,8 +3126,7 @@ BTextView::_Refresh(int32 fromOffset, int32 toOffset, bool erase, bool scroll)
|| fAlignment != B_ALIGN_LEFT) || fAlignment != B_ALIGN_LEFT)
drawOffset = (*fLines)[fromLine]->offset; drawOffset = (*fLines)[fromLine]->offset;
if (fResizable) _AutoResize(false);
_AutoResize(false);
_DrawLines(fromLine, toLine, drawOffset, erase); _DrawLines(fromLine, toLine, drawOffset, erase);
@@ -3541,7 +3549,7 @@ BTextView::_DrawLine(BView *view, const int32 &lineNum, const int32 &startOffset
startLeft /= 2; startLeft /= 2;
startLeft += fTextRect.left; startLeft += fTextRect.left;
} }
view->MovePenTo(startLeft, line->origin + line->ascent + fTextRect.top + 1); view->MovePenTo(startLeft, line->origin + line->ascent + fTextRect.top + 1);
if (erase) { if (erase) {
@@ -3671,10 +3679,6 @@ BTextView::_DrawLines(int32 startLine, int32 endLine, int32 startOffset,
if (fAlignment != B_ALIGN_LEFT) if (fAlignment != B_ALIGN_LEFT)
erase = true; erase = true;
// Actually hide the caret
if (fCaretVisible)
_DrawCaret(fSelStart);
BRect eraseRect = clipRect; BRect eraseRect = clipRect;
int32 startEraseLine = startLine; int32 startEraseLine = startLine;
STELine* line = (*fLines)[startLine]; STELine* line = (*fLines)[startLine];
@@ -4097,32 +4101,31 @@ BTextView::_UpdateScrollbars()
void void
BTextView::_AutoResize(bool redraw) BTextView::_AutoResize(bool redraw)
{ {
if (fResizable) { if (!fResizable)
float oldWidth = Bounds().Width() + 1; return;
float newWidth = 3;
for (int32 i = 0; i < CountLines(); i++) BRect bounds = Bounds();
newWidth += LineWidth(i); float oldWidth = fTextRect.Width();
float newWidth = max_c(bounds.Width() - (fLeftInset + fRightInset),
BRect newRect(0, 0, ceilf(newWidth), ceilf(LineHeight(0)) + 2); ceilf(LineWidth(0)));
if (fContainerView != NULL) { if (fContainerView != NULL) {
fContainerView->ResizeTo(newRect.Width() + 1, newRect.Height()); fContainerView->ResizeBy(ceilf(newWidth - oldWidth), 0);
if (fAlignment == B_ALIGN_CENTER) if (fAlignment == B_ALIGN_CENTER)
fContainerView->MoveBy(ceilf((oldWidth - (newRect.Width() + 1)) / 2), 0); fContainerView->MoveBy(ceilf((oldWidth - newWidth) / 2), 0);
else if (fAlignment == B_ALIGN_RIGHT) else if (fAlignment == B_ALIGN_RIGHT)
fContainerView->MoveBy(oldWidth - (newRect.Width() + 1), 0); fContainerView->MoveBy(ceilf(oldWidth - newWidth), 0);
fContainerView->Invalidate(); // fContainerView->Invalidate();
}
fTextRect = newRect.InsetBySelf(0, 1);
if (redraw)
_DrawLines(0, 0);
// Erase the old text (TODO: Might not work for alignments different than B_ALIGN_LEFT)
SetLowColor(ViewColor());
FillRect(BRect(fTextRect.right, fTextRect.top, Bounds().right, fTextRect.bottom), B_SOLID_LOW);
} }
fTextRect.right = fTextRect.left + newWidth;
if (redraw)
_DrawLines(0, 0);
// Erase the old text (TODO: Might not work for alignments different than B_ALIGN_LEFT)
SetLowColor(ViewColor());
FillRect(BRect(fTextRect.right, fTextRect.top, Bounds().right, fTextRect.bottom), B_SOLID_LOW);
} }