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