1 /******************************************************************************* 2 / 3 / File: TextView.h 4 / 5 / Description: BTextView displays and manages styled text. 6 / 7 / Copyright 1993-98, Be Incorporated, All Rights Reserved 8 / 9 *******************************************************************************/ 10 11 #ifndef _TEXTVIEW_H 12 #define _TEXTVIEW_H 13 14 #include <BeBuild.h> 15 #include <View.h> 16 17 /*----------------------------------------------------------------*/ 18 /*----- BTextView structures and definitions ---------------------*/ 19 20 struct text_run { 21 int32 offset; /* byte offset of first character of run*/ 22 BFont font; /* font of run*/ 23 rgb_color color; /* color of run*/ 24 }; 25 26 struct text_run_array { 27 int32 count; /* number of text runs*/ 28 text_run runs[1]; /* array of count number of runs*/ 29 }; 30 31 enum undo_state { 32 B_UNDO_UNAVAILABLE, 33 B_UNDO_TYPING, 34 B_UNDO_CUT, 35 B_UNDO_PASTE, 36 B_UNDO_CLEAR, 37 B_UNDO_DROP 38 }; 39 40 41 #if _PR2_COMPATIBLE_ 42 extern "C" void _ReservedTextView2__9BTextViewFv(BTextView *object, 43 BMessage *drag, 44 BBitmap **bitmap, 45 BPoint *point, 46 BHandler **handler); 47 #endif 48 49 50 class BBitmap; 51 class BClipboard; 52 class BFile; 53 class BList; 54 class _BTextGapBuffer_; 55 class _BLineBuffer_; 56 class _BStyleBuffer_; 57 class _BWidthBuffer_; 58 class _BUndoBuffer_; 59 class _BInlineInput_; 60 class _BTextTrackState_; 61 class _BTextChangeResult_; 62 63 extern "C" status_t _init_interface_kit_(); 64 65 /*----------------------------------------------------------------*/ 66 /*----- BTextView class ------------------------------------------*/ 67 68 class BTextView : public BView { 69 public: 70 BTextView(BRect frame, 71 const char *name, 72 BRect textRect, 73 uint32 resizeMask, 74 uint32 flags = B_WILL_DRAW | B_PULSE_NEEDED); 75 BTextView(BRect frame, 76 const char *name, 77 BRect textRect, 78 const BFont *initialFont, 79 const rgb_color *initialColor, 80 uint32 resizeMask, 81 uint32 flags); 82 BTextView(BMessage *data); 83 virtual ~BTextView(); 84 85 static BArchivable* Instantiate(BMessage *data); 86 virtual status_t Archive(BMessage *data, bool deep = true) const; 87 88 virtual void AttachedToWindow(); 89 virtual void DetachedFromWindow(); 90 virtual void Draw(BRect inRect); 91 virtual void MouseDown(BPoint where); 92 virtual void MouseUp(BPoint where); 93 virtual void MouseMoved(BPoint where, 94 uint32 code, 95 const BMessage *message); 96 virtual void WindowActivated(bool state); 97 virtual void KeyDown(const char *bytes, int32 numBytes); 98 virtual void Pulse(); 99 virtual void FrameResized(float width, float height); 100 virtual void MakeFocus(bool focusState = true); 101 virtual void MessageReceived(BMessage *message); 102 virtual BHandler* ResolveSpecifier(BMessage *message, 103 int32 index, 104 BMessage *specifier, 105 int32 form, 106 const char *property); 107 virtual status_t GetSupportedSuites(BMessage *data); 108 virtual status_t Perform(perform_code d, void *arg); 109 110 void SetText(const char *inText, 111 const text_run_array *inRuns = NULL); 112 void SetText(const char *inText, 113 int32 inLength, 114 const text_run_array *inRuns = NULL); 115 void SetText(BFile *inFile, 116 int32 startOffset, 117 int32 inLength, 118 const text_run_array *inRuns = NULL); 119 120 void Insert(const char *inText, 121 const text_run_array *inRuns = NULL); 122 void Insert(const char *inText, 123 int32 inLength, 124 const text_run_array *inRuns = NULL); 125 void Insert(int32 startOffset, 126 const char *inText, 127 int32 inLength, 128 const text_run_array *inRuns = NULL); 129 130 void Delete(); 131 void Delete(int32 startOffset, int32 endOffset); 132 133 const char* Text() const; 134 int32 TextLength() const; 135 void GetText(int32 offset, 136 int32 length, 137 char *buffer) const; 138 uchar ByteAt(int32 offset) const; 139 140 int32 CountLines() const; 141 int32 CurrentLine() const; 142 void GoToLine(int32 lineNum); 143 144 virtual void Cut(BClipboard *clipboard); 145 virtual void Copy(BClipboard *clipboard); 146 virtual void Paste(BClipboard *clipboard); 147 void Clear(); 148 149 virtual bool AcceptsPaste(BClipboard *clipboard); 150 virtual bool AcceptsDrop(const BMessage *inMessage); 151 152 virtual void Select(int32 startOffset, int32 endOffset); 153 void SelectAll(); 154 void GetSelection(int32 *outStart, int32 *outEnd) const; 155 156 void SetFontAndColor(const BFont *inFont, 157 uint32 inMode = B_FONT_ALL, 158 const rgb_color *inColor = NULL); 159 void SetFontAndColor(int32 startOffset, 160 int32 endOffset, 161 const BFont *inFont, 162 uint32 inMode = B_FONT_ALL, 163 const rgb_color *inColor = NULL); 164 165 void GetFontAndColor(int32 inOffset, 166 BFont *outFont, 167 rgb_color *outColor = NULL) const; 168 void GetFontAndColor(BFont *outFont, 169 uint32 *outMode, 170 rgb_color *outColor = NULL, 171 bool *outEqColor = NULL) const; 172 173 void SetRunArray(int32 startOffset, 174 int32 endOffset, 175 const text_run_array *inRuns); 176 text_run_array* RunArray(int32 startOffset, 177 int32 endOffset, 178 int32 *outSize = NULL) const; 179 180 int32 LineAt(int32 offset) const; 181 int32 LineAt(BPoint point) const; 182 BPoint PointAt(int32 inOffset, float *outHeight = NULL) const; 183 int32 OffsetAt(BPoint point) const; 184 int32 OffsetAt(int32 line) const; 185 186 virtual void FindWord(int32 inOffset, 187 int32 *outFromOffset, 188 int32 *outToOffset); 189 190 virtual bool CanEndLine(int32 offset); 191 192 float LineWidth(int32 lineNum = 0) const; 193 float LineHeight(int32 lineNum = 0) const; 194 float TextHeight(int32 startLine, int32 endLine) const; 195 196 void GetTextRegion(int32 startOffset, 197 int32 endOffset, 198 BRegion *outRegion) const; 199 200 virtual void ScrollToOffset(int32 inOffset); 201 void ScrollToSelection(); 202 203 void Highlight(int32 startOffset, int32 endOffset); 204 205 void SetTextRect(BRect rect); 206 BRect TextRect() const; 207 void SetStylable(bool stylable); 208 bool IsStylable() const; 209 void SetTabWidth(float width); 210 float TabWidth() const; 211 void MakeSelectable(bool selectable = true); 212 bool IsSelectable() const; 213 void MakeEditable(bool editable = true); 214 bool IsEditable() const; 215 void SetWordWrap(bool wrap); 216 bool DoesWordWrap() const; 217 void SetMaxBytes(int32 max); 218 int32 MaxBytes() const; 219 void DisallowChar(uint32 aChar); 220 void AllowChar(uint32 aChar); 221 void SetAlignment(alignment flag); 222 alignment Alignment() const; 223 void SetAutoindent(bool state); 224 bool DoesAutoindent() const; 225 void SetColorSpace(color_space colors); 226 color_space ColorSpace() const; 227 void MakeResizable(bool resize, BView *resizeView = NULL); 228 bool IsResizable() const; 229 void SetDoesUndo(bool undo); 230 bool DoesUndo() const; 231 void HideTyping(bool enabled); 232 bool IsTypingHidden(void) const; 233 234 virtual void ResizeToPreferred(); 235 virtual void GetPreferredSize(float *width, float *height); 236 virtual void AllAttached(); 237 virtual void AllDetached(); 238 239 240 static text_run_array* AllocRunArray(int32 entryCount, int32 *outSize = NULL); 241 static text_run_array* CopyRunArray(const text_run_array *orig, int32 countDelta = 0); 242 static void FreeRunArray(text_run_array *array); 243 static void * FlattenRunArray(const text_run_array *inArray, int32 *outSize = NULL); 244 static text_run_array* UnflattenRunArray(const void *data, int32 *outSize = NULL); 245 246 protected: 247 virtual void InsertText(const char *inText, 248 int32 inLength, 249 int32 inOffset, 250 const text_run_array *inRuns); 251 virtual void DeleteText(int32 fromOffset, int32 toOffset); 252 253 public: 254 virtual void Undo(BClipboard *clipboard); 255 undo_state UndoState(bool *isRedo) const; 256 257 protected: 258 virtual void GetDragParameters(BMessage *drag, 259 BBitmap **bitmap, 260 BPoint *point, 261 BHandler **handler); 262 263 /*----- Private or reserved -----------------------------------------*/ 264 private: 265 friend status_t _init_interface_kit_(); 266 friend class _BTextTrackState_; 267 268 #if _PR2_COMPATIBLE_ 269 friend void _ReservedTextView2__9BTextViewFv(BTextView *object, 270 BMessage *drag, 271 BBitmap **bitmap, 272 BPoint *point, 273 BHandler **handler); 274 #endif 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 283 #if !_PR3_COMPATIBLE_ 284 virtual void _ReservedTextView9(); 285 virtual void _ReservedTextView10(); 286 virtual void _ReservedTextView11(); 287 virtual void _ReservedTextView12(); 288 #endif 289 290 void InitObject(BRect textRect, 291 const BFont *initialFont, 292 const rgb_color *initialColor); 293 294 void HandleBackspace(); 295 void HandleArrowKey(uint32 inArrowKey); 296 void HandleDelete(); 297 void HandlePageKey(uint32 inPageKey); 298 void HandleAlphaKey(const char *bytes, int32 numBytes); 299 300 void Refresh(int32 fromOffset, 301 int32 toOffset, 302 bool erase, 303 bool scroll); 304 void RecalculateLineBreaks(int32 *startLine, int32 *endLine); 305 int32 FindLineBreak(int32 fromOffset, 306 float *outAscent, 307 float *outDescent, 308 float *ioWidth); 309 310 float StyledWidth(int32 fromOffset, 311 int32 length, 312 float *outAscent = NULL, 313 float *outDescent = NULL) const; 314 float StyledWidthUTF8Safe(int32 fromOffset, 315 int32 numChars, 316 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, 323 int32 inOffset, 324 const text_run_array *inRuns, 325 _BTextChangeResult_ *outResult); 326 void DoDeleteText(int32 fromOffset, 327 int32 toOffset, 328 _BTextChangeResult_ *outResult); 329 330 void _DrawLine(BView *view, const int32 &startLine, 331 const int32 &startOffset, 332 const bool &erase, 333 BRect &eraseRect, 334 BRegion &inputRegion); 335 void DrawLines(int32 startLine, 336 int32 endLine, 337 int32 startOffset = -1, 338 bool erase = false); 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, 348 uint32 code); 349 350 void TrackMouse(BPoint where, const BMessage *message, 351 bool force = false); 352 353 void TrackDrag(BPoint where); 354 void InitiateDrag(); 355 bool MessageDropped(BMessage *inMessage, 356 BPoint where, 357 BPoint offset); 358 359 void PerformAutoScrolling(); 360 void UpdateScrollbars(); 361 void AutoResize(bool doredraw=true); 362 363 void NewOffscreen(float padding = 0.0F); 364 void DeleteOffscreen(); 365 366 void Activate(); 367 void Deactivate(); 368 369 void NormalizeFont(BFont *font); 370 371 uint32 CharClassification(int32 offset) const; 372 int32 NextInitialByte(int32 offset) const; 373 int32 PreviousInitialByte(int32 offset) const; 374 375 bool GetProperty(BMessage *specifier, 376 int32 form, 377 const char *property, 378 BMessage *reply); 379 bool SetProperty(BMessage *specifier, 380 int32 form, 381 const char *property, 382 BMessage *reply); 383 bool CountProperties(BMessage *specifier, 384 int32 form, 385 const char *property, 386 BMessage *reply); 387 388 void HandleInputMethodChanged(BMessage *message); 389 void HandleInputMethodLocationRequest(); 390 void CancelInputMethod(); 391 392 static void LockWidthBuffer(); 393 static void UnlockWidthBuffer(); 394 395 _BTextGapBuffer_* fText; 396 _BLineBuffer_* fLines; 397 _BStyleBuffer_* fStyles; 398 BRect fTextRect; 399 int32 fSelStart; 400 int32 fSelEnd; 401 bool fCaretVisible; 402 bigtime_t fCaretTime; 403 int32 fClickOffset; 404 int32 fClickCount; 405 bigtime_t fClickTime; 406 int32 fDragOffset; 407 uint8 fCursor; 408 bool fActive; 409 bool fStylable; 410 float fTabWidth; 411 bool fSelectable; 412 bool fEditable; 413 bool fWrap; 414 int32 fMaxBytes; 415 BList* fDisallowedChars; 416 alignment fAlignment; 417 bool fAutoindent; 418 BBitmap* fOffscreen; 419 color_space fColorSpace; 420 bool fResizable; 421 BView* fContainerView; 422 _BUndoBuffer_* fUndo; /* was _reserved[0] */ 423 _BInlineInput_* fInline; /* was _reserved[1] */ 424 BMessageRunner * fDragRunner; /* was _reserved[2] */ 425 BMessageRunner * fClickRunner; /* was _reserved[3] */ 426 BPoint fWhere; 427 _BTextTrackState_* fTrackingMouse; /* was _reserved[6] */ 428 _BTextChangeResult_* fTextChange; /* was _reserved[7] */ 429 uint32 _reserved[1]; /* was 8 */ 430 #if !_PR3_COMPATIBLE_ 431 uint32 _more_reserved[8]; 432 #endif 433 434 static _BWidthBuffer_* sWidths; 435 static sem_id sWidthSem; 436 static int32 sWidthAtom; 437 }; 438 439 /*-------------------------------------------------------------*/ 440 /*-------------------------------------------------------------*/ 441 442 #endif /* _TEXT_VIEW_H */ 443