1 /* 2 * Copyright 2013-2015, Stephan Aßmus <superstippi@gmx.de>. 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5 #ifndef TEXT_DOCUMENT_VIEW_H 6 #define TEXT_DOCUMENT_VIEW_H 7 8 #include <Invoker.h> 9 #include <String.h> 10 #include <View.h> 11 12 #include "TextDocument.h" 13 #include "TextDocumentLayout.h" 14 #include "TextEditor.h" 15 16 17 class BClipboard; 18 class BMessageRunner; 19 20 21 class TextDocumentView : public BView, public BInvoker { 22 public: 23 TextDocumentView(const char* name = NULL); 24 virtual ~TextDocumentView(); 25 26 // BView implementation 27 virtual void MessageReceived(BMessage* message); 28 29 virtual void Draw(BRect updateRect); 30 31 virtual void AttachedToWindow(); 32 virtual void FrameResized(float width, float height); 33 virtual void WindowActivated(bool active); 34 virtual void MakeFocus(bool focus = true); 35 36 virtual void MouseDown(BPoint where); 37 virtual void MouseUp(BPoint where); 38 virtual void MouseMoved(BPoint where, uint32 transit, 39 const BMessage* dragMessage); 40 41 virtual void KeyDown(const char* bytes, int32 numBytes); 42 virtual void KeyUp(const char* bytes, int32 numBytes); 43 44 virtual BSize MinSize(); 45 virtual BSize MaxSize(); 46 virtual BSize PreferredSize(); 47 48 virtual bool HasHeightForWidth(); 49 virtual void GetHeightForWidth(float width, float* min, 50 float* max, float* preferred); 51 52 virtual void Relayout(); 53 54 // TextDocumentView interface 55 void SetTextDocument( 56 const TextDocumentRef& document); 57 58 void SetEditingEnabled(bool enabled); 59 void SetTextEditor( 60 const TextEditorRef& editor); 61 62 void SetInsets(float inset); 63 void SetInsets(float horizontal, float vertical); 64 void SetInsets(float left, float top, float right, 65 float bottom); 66 67 void SetSelectionEnabled(bool enabled); 68 void SetCaret(BPoint where, bool extendSelection); 69 70 void SelectAll(); 71 bool HasSelection() const; 72 void GetSelection(int32& start, int32& end) const; 73 74 void Copy(BClipboard* clipboard); 75 void Paste(BClipboard* clipboard); 76 77 private: 78 float _TextLayoutWidth(float viewWidth) const; 79 80 void _UpdateScrollBars(); 81 82 void _ShowCaret(bool show); 83 void _BlinkCaret(); 84 void _DrawCaret(int32 textOffset); 85 void _DrawSelection(); 86 void _GetSelectionShape(BShape& shape, 87 int32 start, int32 end); 88 89 status_t _PastePossiblyDisallowedChars(const char* str, int32 maxLength); 90 void _PasteAllowedChars(const char* str, int32 maxLength); 91 static bool _IsAllowedChar(char c); 92 static bool _AreCharsAllowed(const char* str, int32 maxLength); 93 94 private: 95 TextDocumentRef fTextDocument; 96 TextDocumentLayout fTextDocumentLayout; 97 TextEditorRef fTextEditor; 98 99 float fInsetLeft; 100 float fInsetTop; 101 float fInsetRight; 102 float fInsetBottom; 103 104 BRect fCaretBounds; 105 BMessageRunner* fCaretBlinker; 106 int32 fCaretBlinkToken; 107 bool fSelectionEnabled; 108 bool fShowCaret; 109 bool fMouseDown; 110 }; 111 112 #endif // TEXT_DOCUMENT_VIEW_H 113