Patch by Rene Gollent: TextViews now can scroll even if they aren't

attached to a BScrollView.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@22432 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Stefano Ceccherini 2007-10-04 11:32:06 +00:00
parent 39c99f8644
commit f2476bcc9c
4 changed files with 44 additions and 18 deletions

View File

@ -255,6 +255,7 @@ virtual void Undo(BClipboard *clipboard);
undo_state UndoState(bool *isRedo) const; undo_state UndoState(bool *isRedo) const;
protected: protected:
void _ScrollToOffset(int32 nOffset, bool useHorz, bool useVert);
virtual void GetDragParameters(BMessage *drag, virtual void GetDragParameters(BMessage *drag,
BBitmap **bitmap, BBitmap **bitmap,
BPoint *point, BPoint *point,

View File

@ -100,6 +100,11 @@ _BTextInput_::KeyDown(const char* bytes, int32 numBytes)
} }
} }
void
_BTextInput_::ScrollToOffset(int32 nOffset)
{
_ScrollToOffset(nOffset, true, false);
}
void void
_BTextInput_::MakeFocus(bool state) _BTextInput_::MakeFocus(bool state)

View File

@ -62,6 +62,7 @@ virtual void MakeFocus(bool focusState = true);
void AlignTextRect(); void AlignTextRect();
void SetInitialText(); void SetInitialText();
virtual void ScrollToOffset(int32 nOffset);
virtual void Paste(BClipboard *clipboard); virtual void Paste(BClipboard *clipboard);
protected: protected:

View File

@ -24,7 +24,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <new> #include <new>
using namespace std; using namespace std;
#define DEBUG 1
#include <Application.h> #include <Application.h>
#include <Beep.h> #include <Beep.h>
#include <Bitmap.h> #include <Bitmap.h>
@ -806,6 +806,7 @@ BTextView::MessageReceived(BMessage *message)
switch (opcode) { switch (opcode) {
case B_INPUT_METHOD_STARTED: case B_INPUT_METHOD_STARTED:
{ {
PRINT(("B_INPUT_METHOD_STARTED\n"));
BMessenger messenger; BMessenger messenger;
if (message->FindMessenger("be:reply_to", &messenger) == B_OK) { if (message->FindMessenger("be:reply_to", &messenger) == B_OK) {
ASSERT(fInline == NULL); ASSERT(fInline == NULL);
@ -815,15 +816,20 @@ BTextView::MessageReceived(BMessage *message)
} }
case B_INPUT_METHOD_STOPPED: case B_INPUT_METHOD_STOPPED:
PRINT(("B_INPUT_METHOD_STOPPED\n"));
delete fInline; delete fInline;
fInline = NULL; fInline = NULL;
break; break;
case B_INPUT_METHOD_CHANGED: case B_INPUT_METHOD_CHANGED:
PRINT(("B_INPUT_METHOD_CHANGED\n"));
if (fInline != NULL)
HandleInputMethodChanged(message); HandleInputMethodChanged(message);
break; break;
case B_INPUT_METHOD_LOCATION_REQUEST: case B_INPUT_METHOD_LOCATION_REQUEST:
PRINT(("B_INPUT_METHOD_LOCATION_REQUEST\n"));
if (fInline != NULL)
HandleInputMethodLocationRequest(); HandleInputMethodLocationRequest();
break; break;
@ -1928,25 +1934,40 @@ BTextView::GetTextRegion(int32 startOffset, int32 endOffset, BRegion *outRegion)
*/ */
void void
BTextView::ScrollToOffset(int32 inOffset) BTextView::ScrollToOffset(int32 inOffset)
{
_ScrollToOffset(inOffset, ScrollBar(B_HORIZONTAL) != NULL, ScrollBar(B_VERTICAL) != NULL);
}
void
BTextView::_ScrollToOffset(int32 inOffset, bool useHorz, bool useVert)
{ {
BRect bounds = Bounds(); BRect bounds = Bounds();
float lineHeight = 0.0; float lineHeight = 0.0;
float xdiff = 0.0;
float ydiff = 0.0;
BPoint point = PointAt(inOffset, &lineHeight); BPoint point = PointAt(inOffset, &lineHeight);
// TODO: We should do the following, since otherwise the textview if (useHorz) {
// won't scroll unless it's attached to a scrollview. if (point.x < bounds.left) {
/*if (!bounds.Contains(point)) xdiff = -1 * (bounds.IntegerWidth() / 2);
ScrollTo(point); */ // normalize scroll value to prevent scrolling past left boundary of view
if (bounds.left < fabs(xdiff))
if (ScrollBar(B_HORIZONTAL) != NULL) { xdiff = -1 * bounds.left;
if (point.x < bounds.left || point.x >= bounds.right) } else if (point.x >= bounds.right)
ScrollBar(B_HORIZONTAL)->SetValue(point.x - (bounds.IntegerWidth() / 2)); xdiff = bounds.IntegerWidth() / 2;
} }
if (ScrollBar(B_VERTICAL) != NULL) { if (useVert) {
if (point.y < bounds.top || (point.y + lineHeight) >= bounds.bottom) if (point.y < bounds.top) {
ScrollBar(B_VERTICAL)->SetValue(point.y - (bounds.IntegerHeight() / 2)); ydiff = -1 * (bounds.IntegerHeight() / 2);
// normalize scroll value to prevent scrolling past top of view
if (bounds.top < fabs(ydiff))
ydiff = -1 * bounds.top;
} else if (point.y >= bounds.bottom)
ydiff = bounds.IntegerHeight() / 2;
} }
ScrollBy(xdiff, ydiff);
} }
@ -4398,8 +4419,7 @@ void
BTextView::HandleInputMethodChanged(BMessage *message) BTextView::HandleInputMethodChanged(BMessage *message)
{ {
// TODO: block input if not editable (Andrew) // TODO: block input if not editable (Andrew)
if (!fInline) ASSERT(fInline != NULL);
return;
const char *string = NULL; const char *string = NULL;
if (message->FindString("be:string", &string) < B_OK || string == NULL) if (message->FindString("be:string", &string) < B_OK || string == NULL)
@ -4470,8 +4490,7 @@ BTextView::HandleInputMethodChanged(BMessage *message)
void void
BTextView::HandleInputMethodLocationRequest() BTextView::HandleInputMethodLocationRequest()
{ {
if (!fInline) ASSERT(fInline != NULL);
return;
int32 offset = fInline->Offset(); int32 offset = fInline->Offset();
const int32 limit = offset + fInline->Length(); const int32 limit = offset + fInline->Length();