1 /*!******************************************************************** 2 * 3 Audacity: A Digital Audio Editor 4 5 TextEditHelper.h 6 7 Vitaly Sverchinsky 8 9 **********************************************************************/ 10 11 #pragma once 12 13 #include <wx/string.h> 14 #include <wx/font.h> 15 16 #include "UIHandle.h" 17 #include <wx/colour.h> 18 19 class wxDC; 20 class wxMouseEvent; 21 22 class AudacityProject; 23 class TextEditDelegate; 24 class Track; 25 26 //Used as a delegate for TextEditHelper 27 class TextEditDelegate 28 { 29 public: 30 31 virtual ~TextEditDelegate(); 32 33 virtual void OnTextEditFinished(AudacityProject* project, const wxString& text) = 0; 34 virtual void OnTextEditCancelled(AudacityProject* project) = 0; 35 virtual void OnTextModified(AudacityProject* project, const wxString& text) = 0; 36 virtual void OnTextContextMenu(AudacityProject* project, const wxPoint& position) = 0; 37 38 }; 39 40 41 //Used as a helper object for drawing, editing 42 //and text navigation in TrackPanel 43 class TextEditHelper 44 { 45 wxString mText; 46 wxFont mFont; 47 wxRect mBBox; 48 49 wxColor mTextSelectionColor; 50 wxColor mTextColor; 51 52 bool mRightDragging{ false }; 53 //Index of the symbol from which text drawing should start, 54 //in cases where the whole text cannot fit into provided "view" 55 //rectangle. Used both in hit testing and drawing. 56 int mOffset{ 0 }; 57 int mInitialCursorPos{ 0 }; 58 int mCurrentCursorPos{ 0 }; 59 60 // TextEditHelper will send messages about changes to the object 61 // that implements TextEditDelegate, if present 62 std::weak_ptr<TextEditDelegate> mDelegate; 63 64 public: 65 static bool IsGoodEditKeyCode(int keyCode); 66 67 TextEditHelper(const std::weak_ptr<TextEditDelegate>& delegate, const wxString& text, const wxFont& font); 68 ~TextEditHelper()69 ~TextEditHelper() 70 { 71 } 72 73 void SetTextColor(const wxColor& textColor); 74 void SetTextSelectionColor(const wxColor& textSelectionColor); 75 76 void Cancel(AudacityProject* project); 77 void Finish(AudacityProject* project); 78 79 std::pair<int, int> GetSelection() const; 80 void SetSelection(int from, int to); 81 void SelectAll(); 82 bool IsSelectionEmpty(); 83 84 bool CaptureKey(int keyCode, int mods); 85 bool OnKeyDown(int keyCode, int mods, AudacityProject* project); 86 bool OnChar(int charCode, AudacityProject* project); 87 88 bool OnClick(const wxMouseEvent& event, AudacityProject* project); 89 bool OnDrag(const wxMouseEvent& event, AudacityProject* project); 90 bool OnRelease(const wxMouseEvent& event, AudacityProject* project); 91 92 void Draw(wxDC& dc, const wxRect& rect); 93 94 bool CutSelectedText(AudacityProject& project); 95 bool CopySelectedText(AudacityProject& project); 96 bool PasteSelectedText(AudacityProject& project); 97 98 bool GetCharPositionX(int index, int* outX); 99 100 const wxRect& GetBBox() const; 101 102 protected: 103 104 bool HandleDragRelease(const wxMouseEvent& event, AudacityProject* project); 105 106 void RemoveSelectedText(AudacityProject* project); 107 int FindCursorIndex(const wxPoint& point); 108 109 }; 110