* Disallowed chars now work correctly and no longer prevent the BTextView from

being edited.
* A non editable BTextView is now still navigable.
* Made cursor one pixel shorter (at least it looked a bit too long to me...).


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@14862 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler
2005-11-12 13:04:29 +00:00
parent 723de034c6
commit ee694ac85f
+63 -59
View File
@@ -1,30 +1,14 @@
//------------------------------------------------------------------------------ /*
// Copyright (c) 2001-2005, Haiku, Inc. * Copyright 2001-2005, Haiku Inc.
// * 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 * Hiroshi Lockheimer (BTextView is based on his STEEngine)
// the rights to use, copy, modify, merge, publish, distribute, sublicense, * Marc Flerackers ([email protected])
// and/or sell copies of the Software, and to permit persons to whom the * Stefano Ceccherini ([email protected])
// Software is furnished to do so, subject to the following conditions: */
//
// The above copyright notice and this permission notice shall be included in /** BTextView displays and manages styled text. */
// 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: TextView.cpp
// Authors: Hiroshi Lockheimer (BTextView is based on his STEEngine)
// Marc Flerackers ([email protected])
// Stefano Ceccherini ([email protected])
// Description: BTextView displays and manages styled text.
//------------------------------------------------------------------------------
// TODOs: // TODOs:
// - Finish documenting this class // - Finish documenting this class
@@ -37,8 +21,8 @@
// Known Bugs: // Known Bugs:
// - Double buffering doesn't work well (disabled by default) // - Double buffering doesn't work well (disabled by default)
#include <cstdlib> #include <stdio.h>
#include <cstdio> #include <stdlib.h>
#include <Application.h> #include <Application.h>
#include <Beep.h> #include <Beep.h>
@@ -691,18 +675,31 @@ BTextView::WindowActivated(bool state)
void void
BTextView::KeyDown(const char *bytes, int32 numBytes) BTextView::KeyDown(const char *bytes, int32 numBytes)
{ {
// TODO: Remove this and move it to specific key handlers ?
// moreover, we should check if ARROW keys work in case the object
// isn't editable
if (!fEditable)
return;
const char keyPressed = bytes[0]; const char keyPressed = bytes[0];
// if the character is not allowed, bail out. if (!fEditable) {
if (fDisallowedChars // only arrow and page keys are allowed
&& fDisallowedChars->HasItem(reinterpret_cast<void *>((uint32)keyPressed))) { // (no need to hide the cursor)
beep(); switch (keyPressed) {
case B_LEFT_ARROW:
case B_RIGHT_ARROW:
case B_UP_ARROW:
case B_DOWN_ARROW:
HandleArrowKey(keyPressed);
break;
case B_HOME:
case B_END:
case B_PAGE_UP:
case B_PAGE_DOWN:
HandlePageKey(keyPressed);
break;
default:
BView::KeyDown(bytes, numBytes);
break;
}
return; return;
} }
@@ -710,42 +707,49 @@ BTextView::KeyDown(const char *bytes, int32 numBytes)
be_app->ObscureCursor(); be_app->ObscureCursor();
if (fCaretVisible) if (fCaretVisible)
InvertCaret(); InvertCaret();
switch (keyPressed) { switch (keyPressed) {
case B_BACKSPACE: case B_BACKSPACE:
HandleBackspace(); HandleBackspace();
break; break;
case B_LEFT_ARROW: case B_LEFT_ARROW:
case B_RIGHT_ARROW: case B_RIGHT_ARROW:
case B_UP_ARROW: case B_UP_ARROW:
case B_DOWN_ARROW: case B_DOWN_ARROW:
HandleArrowKey(keyPressed); HandleArrowKey(keyPressed);
break; break;
case B_DELETE: case B_DELETE:
HandleDelete(); HandleDelete();
break; break;
case B_HOME: case B_HOME:
case B_END: case B_END:
case B_PAGE_UP: case B_PAGE_UP:
case B_PAGE_DOWN: case B_PAGE_DOWN:
HandlePageKey(keyPressed); HandlePageKey(keyPressed);
break; break;
case B_ESCAPE: case B_ESCAPE:
case B_INSERT: case B_INSERT:
case B_FUNCTION_KEY: case B_FUNCTION_KEY:
// ignore, pass it up to superclass // ignore, pass it up to superclass
BView::KeyDown(bytes, numBytes); BView::KeyDown(bytes, numBytes);
break; break;
default: default:
// if the character is not allowed, bail out.
if (fDisallowedChars
&& fDisallowedChars->HasItem(reinterpret_cast<void *>((uint32)keyPressed))) {
beep();
return;
}
HandleAlphaKey(bytes, numBytes); HandleAlphaKey(bytes, numBytes);
break; break;
} }
// draw the caret // draw the caret
if (fSelStart == fSelEnd) { if (fSelStart == fSelEnd) {
if (!fCaretVisible) if (!fCaretVisible)
@@ -3613,7 +3617,7 @@ BTextView::DoInsertText(const char *inText, int32 inLength, int32 inOffset,
void void
BTextView::DoDeleteText(int32 fromOffset, int32 toOffset, BTextView::DoDeleteText(int32 fromOffset, int32 toOffset,
_BTextChangeResult_ *outResult) _BTextChangeResult_ *outResult)
{ {
CALLED(); CALLED();
} }
@@ -3621,12 +3625,12 @@ BTextView::DoDeleteText(int32 fromOffset, int32 toOffset,
void void
BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset, BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset,
bool erase) bool erase)
{ {
// clip the text // clip the text
BRect clipRect = Bounds() & fTextRect; BRect clipRect = Bounds() & fTextRect;
clipRect.InsetBy(-1, -1); clipRect.InsetBy(-1, -1);
BView *view = this; BView *view = this;
if (fOffscreen && fOffscreen->Lock()) { if (fOffscreen && fOffscreen->Lock()) {
view = fOffscreen->ChildAt(0); view = fOffscreen->ChildAt(0);
@@ -3635,7 +3639,7 @@ BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset,
newClip.Set(clipRect); newClip.Set(clipRect);
ConstrainClippingRegion(&newClip); ConstrainClippingRegion(&newClip);
} }
// set the low color to the view color so that // set the low color to the view color so that
// drawing to a non-white background will work // drawing to a non-white background will work
view->SetLowColor(view->ViewColor()); view->SetLowColor(view->ViewColor());
@@ -3663,20 +3667,20 @@ BTextView::DrawLines(int32 startLine, int32 endLine, int32 startOffset,
if (startErase > line->offset) if (startErase > line->offset)
startErase--; startErase--;
} }
eraseRect.left = PointAt(startErase).x; eraseRect.left = PointAt(startErase).x;
eraseRect.top = line->origin + fTextRect.top; eraseRect.top = line->origin + fTextRect.top;
eraseRect.bottom = (line + 1)->origin + fTextRect.top; eraseRect.bottom = (line + 1)->origin + fTextRect.top;
view->FillRect(eraseRect, B_SOLID_LOW); view->FillRect(eraseRect, B_SOLID_LOW);
eraseRect = clipRect; eraseRect = clipRect;
} }
BRegion inputRegion; BRegion inputRegion;
if (fInline != NULL && fInline->IsActive()) if (fInline != NULL && fInline->IsActive())
GetTextRegion(fInline->Offset(), fInline->Offset() + fInline->Length(), &inputRegion); GetTextRegion(fInline->Offset(), fInline->Offset() + fInline->Length(), &inputRegion);
float startLeft = fTextRect.left; float startLeft = fTextRect.left;
for (long i = startLine; i <= endLine; i++) { for (long i = startLine; i <= endLine; i++) {
long length = (line + 1)->offset - line->offset; long length = (line + 1)->offset - line->offset;
@@ -3691,7 +3695,7 @@ BTextView::DrawLines(int32 startLine, int32 endLine, 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 && i >= startEraseLine) { if (erase && i >= startEraseLine) {
@@ -3803,12 +3807,12 @@ BTextView::DrawCaret(int32 offset)
float lineHeight; float lineHeight;
BPoint caretPoint = PointAt(offset, &lineHeight); BPoint caretPoint = PointAt(offset, &lineHeight);
caretPoint.x = min_c(caretPoint.x, fTextRect.right); caretPoint.x = min_c(caretPoint.x, fTextRect.right);
BRect caretRect; BRect caretRect;
caretRect.left = caretRect.right = caretPoint.x; caretRect.left = caretRect.right = caretPoint.x;
caretRect.top = caretPoint.y; caretRect.top = caretPoint.y;
caretRect.bottom = caretPoint.y + lineHeight; caretRect.bottom = caretPoint.y + lineHeight - 1;
InvertRect(caretRect); InvertRect(caretRect);
} }