1 /* 2 * Copyright 2007-2009, Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _TEXTVIEW_H 6 #define _TEXTVIEW_H 7 8 9 #include <Locker.h> 10 #include <View.h> 11 12 13 class BBitmap; 14 class BClipboard; 15 class BFile; 16 class BList; 17 class BMessageRunner; 18 19 struct text_run { 20 int32 offset; 21 BFont font; 22 rgb_color color; 23 }; 24 25 struct text_run_array { 26 int32 count; 27 text_run runs[1]; 28 }; 29 30 enum undo_state { 31 B_UNDO_UNAVAILABLE, 32 B_UNDO_TYPING, 33 B_UNDO_CUT, 34 B_UNDO_PASTE, 35 B_UNDO_CLEAR, 36 B_UNDO_DROP 37 }; 38 39 namespace BPrivate { 40 class TextGapBuffer; 41 } 42 43 44 class BTextView : public BView { 45 public: 46 BTextView(BRect frame, const char* name, 47 BRect textRect, uint32 resizeMask, 48 uint32 flags 49 = B_WILL_DRAW | B_PULSE_NEEDED); 50 BTextView(BRect frame, const char* name, 51 BRect textRect, const BFont* initialFont, 52 const rgb_color* initialColor, 53 uint32 resizeMask, uint32 flags); 54 55 BTextView(const char* name, 56 uint32 flags 57 = B_WILL_DRAW | B_PULSE_NEEDED); 58 BTextView(const char* name, 59 const BFont* initialFont, 60 const rgb_color* initialColor, 61 uint32 flags); 62 63 BTextView(BMessage* archive); 64 65 virtual ~BTextView(); 66 67 static BArchivable* Instantiate(BMessage* archive); 68 virtual status_t Archive(BMessage* archive, 69 bool deep = true) const; 70 71 virtual void AttachedToWindow(); 72 virtual void DetachedFromWindow(); 73 virtual void Draw(BRect updateRect); 74 virtual void MouseDown(BPoint where); 75 virtual void MouseUp(BPoint where); 76 virtual void MouseMoved(BPoint where, uint32 code, 77 const BMessage* dragMessage); 78 virtual void WindowActivated(bool state); 79 virtual void KeyDown(const char* bytes, int32 numBytes); 80 virtual void Pulse(); 81 virtual void FrameResized(float width, float height); 82 virtual void MakeFocus(bool focusState = true); 83 virtual void MessageReceived(BMessage* message); 84 85 virtual BHandler* ResolveSpecifier(BMessage* message, 86 int32 index, BMessage* specifier, 87 int32 form, const char* property); 88 virtual status_t GetSupportedSuites(BMessage* data); 89 90 void SetText(const char* inText, 91 const text_run_array* inRuns = NULL); 92 void SetText(const char* inText, int32 inLength, 93 const text_run_array* inRuns = NULL); 94 void SetText(BFile* inFile, 95 int32 startOffset, int32 inLength, 96 const text_run_array* inRuns = NULL); 97 98 void Insert(const char* inText, 99 const text_run_array* inRuns = NULL); 100 void Insert(const char* inText, int32 inLength, 101 const text_run_array* inRuns = NULL); 102 void Insert(int32 startOffset, 103 const char* inText, int32 inLength, 104 const text_run_array* inRuns = NULL); 105 106 void Delete(); 107 void Delete(int32 startOffset, int32 endOffset); 108 109 const char* Text() const; 110 int32 TextLength() const; 111 void GetText(int32 offset, int32 length, 112 char* buffer) const; 113 uint8 ByteAt(int32 offset) const; 114 115 int32 CountLines() const; 116 int32 CurrentLine() const; 117 void GoToLine(int32 lineIndex); 118 119 virtual void Cut(BClipboard* clipboard); 120 virtual void Copy(BClipboard* clipboard); 121 virtual void Paste(BClipboard* clipboard); 122 void Clear(); 123 124 virtual bool AcceptsPaste(BClipboard* clipboard); 125 virtual bool AcceptsDrop(const BMessage* inMessage); 126 127 virtual void Select(int32 startOffset, int32 endOffset); 128 void SelectAll(); 129 void GetSelection(int32* outStart, 130 int32* outEnd) const; 131 132 void SetFontAndColor(const BFont* inFont, 133 uint32 inMode = B_FONT_ALL, 134 const rgb_color* inColor = NULL); 135 void SetFontAndColor(int32 startOffset, 136 int32 endOffset, const BFont* inFont, 137 uint32 inMode = B_FONT_ALL, 138 const rgb_color* inColor = NULL); 139 140 void GetFontAndColor(int32 inOffset, BFont* outFont, 141 rgb_color* outColor = NULL) const; 142 void GetFontAndColor(BFont* outFont, 143 uint32* sameProperties, 144 rgb_color* outColor = NULL, 145 bool* sameColor = NULL) const; 146 147 void SetRunArray(int32 startOffset, int32 endOffset, 148 const text_run_array* inRuns); 149 text_run_array* RunArray(int32 startOffset, int32 endOffset, 150 int32* outSize = NULL) const; 151 152 int32 LineAt(int32 offset) const; 153 int32 LineAt(BPoint point) const; 154 BPoint PointAt(int32 inOffset, 155 float* outHeight = NULL) const; 156 int32 OffsetAt(BPoint point) const; 157 int32 OffsetAt(int32 line) const; 158 159 virtual void FindWord(int32 inOffset, int32* outFromOffset, 160 int32* outToOffset); 161 162 virtual bool CanEndLine(int32 offset); 163 164 float LineWidth(int32 lineIndex = 0) const; 165 float LineHeight(int32 lineIndex = 0) const; 166 float TextHeight(int32 startLine, 167 int32 endLine) const; 168 169 void GetTextRegion(int32 startOffset, 170 int32 endOffset, BRegion* outRegion) const; 171 172 virtual void ScrollToOffset(int32 inOffset); 173 void ScrollToSelection(); 174 175 void Highlight(int32 startOffset, int32 endOffset); 176 177 void SetTextRect(BRect rect); 178 BRect TextRect() const; 179 void SetInsets(float left, float top, float right, 180 float bottom); 181 void GetInsets(float* _left, float* _top, 182 float* _right, float* _bottom) const; 183 184 void SetStylable(bool stylable); 185 bool IsStylable() const; 186 void SetTabWidth(float width); 187 float TabWidth() const; 188 void MakeSelectable(bool selectable = true); 189 bool IsSelectable() const; 190 void MakeEditable(bool editable = true); 191 bool IsEditable() const; 192 void SetWordWrap(bool wrap); 193 bool DoesWordWrap() const; 194 void SetMaxBytes(int32 max); 195 int32 MaxBytes() const; 196 void DisallowChar(uint32 aChar); 197 void AllowChar(uint32 aChar); 198 void SetAlignment(alignment flag); 199 alignment Alignment() const; 200 void SetAutoindent(bool state); 201 bool DoesAutoindent() const; 202 void SetColorSpace(color_space colors); 203 color_space ColorSpace() const; 204 void MakeResizable(bool resize, 205 BView* resizeView = NULL); 206 bool IsResizable() const; 207 void SetDoesUndo(bool undo); 208 bool DoesUndo() const; 209 void HideTyping(bool enabled); 210 bool IsTypingHidden() const; 211 212 virtual void ResizeToPreferred(); 213 virtual void GetPreferredSize(float* _width, float* _height); 214 215 virtual BSize MinSize(); 216 virtual BSize MaxSize(); 217 virtual BSize PreferredSize(); 218 219 virtual bool HasHeightForWidth(); 220 virtual void GetHeightForWidth(float width, float* min, 221 float* max, float* preferred); 222 223 virtual void InvalidateLayout(bool descendants = false); 224 225 protected: 226 virtual void DoLayout(); 227 228 public: 229 virtual void AllAttached(); 230 virtual void AllDetached(); 231 232 static text_run_array* AllocRunArray(int32 entryCount, 233 int32* outSize = NULL); 234 static text_run_array* CopyRunArray(const text_run_array* orig, 235 int32 countDelta = 0); 236 static void FreeRunArray(text_run_array* array); 237 static void* FlattenRunArray(const text_run_array* inArray, 238 int32* outSize = NULL); 239 static text_run_array* UnflattenRunArray(const void* data, 240 int32* outSize = NULL); 241 242 protected: 243 virtual void InsertText(const char* inText, int32 inLength, 244 int32 inOffset, 245 const text_run_array* inRuns); 246 virtual void DeleteText(int32 fromOffset, int32 toOffset); 247 248 public: 249 virtual void Undo(BClipboard* clipboard); 250 undo_state UndoState(bool* isRedo) const; 251 252 protected: 253 virtual void GetDragParameters(BMessage* drag, 254 BBitmap** _bitmap, BPoint* point, 255 BHandler** _handler); 256 257 // FBC padding and forbidden methods 258 public: 259 virtual status_t Perform(perform_code code, void* data); 260 261 private: 262 virtual void _ReservedTextView3(); 263 virtual void _ReservedTextView4(); 264 virtual void _ReservedTextView5(); 265 virtual void _ReservedTextView6(); 266 virtual void _ReservedTextView7(); 267 virtual void _ReservedTextView8(); 268 virtual void _ReservedTextView9(); 269 virtual void _ReservedTextView10(); 270 virtual void _ReservedTextView11(); 271 virtual void _ReservedTextView12(); 272 273 private: 274 class InlineInput; 275 struct LayoutData; 276 class LineBuffer; 277 class StyleBuffer; 278 class TextTrackState; 279 class UndoBuffer; 280 281 // UndoBuffer derivatives 282 class CutUndoBuffer; 283 class PasteUndoBuffer; 284 class ClearUndoBuffer; 285 class DropUndoBuffer; 286 class TypingUndoBuffer; 287 288 friend class TextTrackState; 289 290 void _InitObject(BRect textRect, 291 const BFont* initialFont, 292 const rgb_color* initialColor); 293 294 void _ValidateLayoutData(); 295 296 void _HandleBackspace(); 297 void _HandleArrowKey(uint32 inArrowKey); 298 void _HandleDelete(); 299 void _HandlePageKey(uint32 inPageKey); 300 void _HandleAlphaKey(const char* bytes, 301 int32 numBytes); 302 303 void _Refresh(int32 fromOffset, int32 toOffset, 304 bool erase, bool scroll); 305 void _RecalculateLineBreaks(int32* startLine, 306 int32* endLine); 307 int32 _FindLineBreak(int32 fromOffset, 308 float* outAscent, float* outDescent, 309 float* ioWidth); 310 311 float _StyledWidth(int32 fromOffset, int32 length, 312 float* outAscent = NULL, 313 float* outDescent = NULL) const; 314 float _StyledWidthUTF8Safe(int32 fromOffset, 315 int32 numChars, float* outAscent = NULL, 316 float* outDescent = NULL) const; 317 318 float _ActualTabWidth(float location) const; 319 320 void _DoInsertText(const char* inText, 321 int32 inLength, int32 inOffset, 322 const text_run_array* inRuns); 323 324 void _DoDeleteText(int32 fromOffset, 325 int32 toOffset); 326 327 void _DrawLine(BView* view, const int32 &startLine, 328 const int32& startOffset, 329 const bool& erase, BRect& eraseRect, 330 BRegion& inputRegion); 331 332 void _DrawLines(int32 startLine, int32 endLine, 333 int32 startOffset = -1, 334 bool erase = false); 335 void _RequestDrawLines(int32 startLine, 336 int32 endLine, int32 startOffset = -1, 337 bool erase = false); 338 339 void _DrawCaret(int32 offset); 340 void _ShowCaret(); 341 void _HideCaret(); 342 void _InvertCaret(); 343 void _DragCaret(int32 offset); 344 345 void _StopMouseTracking(); 346 bool _PerformMouseUp(BPoint where); 347 bool _PerformMouseMoved(BPoint where, uint32 code); 348 349 void _TrackMouse(BPoint where, 350 const BMessage* message, 351 bool force = false); 352 353 void _TrackDrag(BPoint where); 354 void _InitiateDrag(); 355 bool _MessageDropped(BMessage* inMessage, 356 BPoint where, BPoint offset); 357 358 void _PerformAutoScrolling(); 359 void _UpdateScrollbars(); 360 void _AutoResize(bool doRedraw = true); 361 362 void _NewOffscreen(float padding = 0.0); 363 void _DeleteOffscreen(); 364 365 void _Activate(); 366 void _Deactivate(); 367 368 void _NormalizeFont(BFont* font); 369 370 void _SetRunArray(int32 startOffset, int32 endOffset, 371 const text_run_array* inRuns); 372 373 void _ApplyStyleRange(int32 fromOffset, 374 int32 toOffset, 375 uint32 inMode = B_FONT_ALL, 376 const BFont *inFont = NULL, 377 const rgb_color *inColor = NULL, 378 bool syncNullStyle = true); 379 380 uint32 _CharClassification(int32 offset) const; 381 int32 _NextInitialByte(int32 offset) const; 382 int32 _PreviousInitialByte(int32 offset) const; 383 384 int32 _PreviousWordBoundary(int32 offset); 385 int32 _NextWordBoundary(int32 offset); 386 387 bool _GetProperty(BMessage* specifier, int32 form, 388 const char* property, BMessage* reply); 389 bool _SetProperty(BMessage* specifier, int32 form, 390 const char* property, BMessage* reply); 391 bool _CountProperties(BMessage* specifier, 392 int32 form, const char* property, 393 BMessage* reply); 394 395 void _HandleInputMethodChanged(BMessage* message); 396 void _HandleInputMethodLocationRequest(); 397 void _CancelInputMethod(); 398 399 int32 _LineAt(int32 offset) const; 400 int32 _LineAt(const BPoint& point) const; 401 bool _IsOnEmptyLastLine(int32 offset) const; 402 403 float _NullStyleHeight() const; 404 405 void _ScrollBy(float horizontalStep, 406 float verticalStep); 407 void _ScrollTo(float x, float y); 408 409 private: 410 BPrivate::TextGapBuffer* fText; 411 LineBuffer* fLines; 412 StyleBuffer* fStyles; 413 BRect fTextRect; 414 int32 fSelStart; 415 int32 fSelEnd; 416 bool fCaretVisible; 417 bigtime_t fCaretTime; 418 int32 fCaretOffset; 419 int32 fClickCount; 420 bigtime_t fClickTime; 421 int32 fDragOffset; 422 uint8 fCursor; 423 bool fActive; 424 bool fStylable; 425 float fTabWidth; 426 bool fSelectable; 427 bool fEditable; 428 bool fWrap; 429 int32 fMaxBytes; 430 BList* fDisallowedChars; 431 alignment fAlignment; 432 bool fAutoindent; 433 BBitmap* fOffscreen; 434 color_space fColorSpace; 435 bool fResizable; 436 BView* fContainerView; 437 UndoBuffer* fUndo; 438 InlineInput* fInline; 439 BMessageRunner* fDragRunner; 440 BMessageRunner* fClickRunner; 441 BPoint fWhere; 442 TextTrackState* fTrackingMouse; 443 444 float fMinTextRectWidth; 445 LayoutData* fLayoutData; 446 int32 fLastClickOffset; 447 448 uint32 _reserved[7]; 449 }; 450 451 #endif // _TEXTVIEW_H 452