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 void _ResetTextRect(); 296 297 void _HandleBackspace(); 298 void _HandleArrowKey(uint32 inArrowKey); 299 void _HandleDelete(); 300 void _HandlePageKey(uint32 inPageKey); 301 void _HandleAlphaKey(const char* bytes, 302 int32 numBytes); 303 304 void _Refresh(int32 fromOffset, int32 toOffset, 305 bool scroll); 306 void _RecalculateLineBreaks(int32* startLine, 307 int32* endLine); 308 int32 _FindLineBreak(int32 fromOffset, 309 float* outAscent, float* outDescent, 310 float* inOutWidth); 311 312 float _StyledWidth(int32 fromOffset, int32 length, 313 float* outAscent = NULL, 314 float* outDescent = NULL) const; 315 float _TabExpandedStyledWidth(int32 offset, 316 int32 length, float* outAscent = NULL, 317 float* outDescent = NULL) const; 318 319 float _ActualTabWidth(float location) const; 320 321 void _DoInsertText(const char* inText, 322 int32 inLength, int32 inOffset, 323 const text_run_array* inRuns); 324 325 void _DoDeleteText(int32 fromOffset, 326 int32 toOffset); 327 328 void _DrawLine(BView* view, const int32 &startLine, 329 const int32& startOffset, 330 const bool& erase, BRect& eraseRect, 331 BRegion& inputRegion); 332 333 void _DrawLines(int32 startLine, int32 endLine, 334 int32 startOffset = -1, 335 bool erase = false); 336 void _RequestDrawLines(int32 startLine, 337 int32 endLine); 338 339 void _DrawCaret(int32 offset, bool visible); 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 int32 _PreviousWordStart(int32 offset); 388 int32 _NextWordEnd(int32 offset); 389 390 bool _GetProperty(BMessage* specifier, int32 form, 391 const char* property, BMessage* reply); 392 bool _SetProperty(BMessage* specifier, int32 form, 393 const char* property, BMessage* reply); 394 bool _CountProperties(BMessage* specifier, 395 int32 form, const char* property, 396 BMessage* reply); 397 398 void _HandleInputMethodChanged(BMessage* message); 399 void _HandleInputMethodLocationRequest(); 400 void _CancelInputMethod(); 401 402 int32 _LineAt(int32 offset) const; 403 int32 _LineAt(const BPoint& point) const; 404 bool _IsOnEmptyLastLine(int32 offset) const; 405 406 float _NullStyleHeight() const; 407 408 void _ScrollBy(float horizontalStep, 409 float verticalStep); 410 void _ScrollTo(float x, float y); 411 412 private: 413 BPrivate::TextGapBuffer* fText; 414 LineBuffer* fLines; 415 StyleBuffer* fStyles; 416 BRect fTextRect; 417 int32 fSelStart; 418 int32 fSelEnd; 419 bool fCaretVisible; 420 bigtime_t fCaretTime; 421 int32 fCaretOffset; 422 int32 fClickCount; 423 bigtime_t fClickTime; 424 int32 fDragOffset; 425 uint8 fCursor; 426 bool fActive; 427 bool fStylable; 428 float fTabWidth; 429 bool fSelectable; 430 bool fEditable; 431 bool fWrap; 432 int32 fMaxBytes; 433 BList* fDisallowedChars; 434 alignment fAlignment; 435 bool fAutoindent; 436 BBitmap* fOffscreen; 437 color_space fColorSpace; 438 bool fResizable; 439 BView* fContainerView; 440 UndoBuffer* fUndo; 441 InlineInput* fInline; 442 BMessageRunner* fDragRunner; 443 BMessageRunner* fClickRunner; 444 BPoint fWhere; 445 TextTrackState* fTrackingMouse; 446 447 float fMinTextRectWidth; 448 LayoutData* fLayoutData; 449 int32 fLastClickOffset; 450 451 uint32 _reserved[7]; 452 }; 453 454 #endif // _TEXTVIEW_H 455