textctrl.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/x11/textctrl.h
  3. // Purpose:
  4. // Author: Robert Roebling
  5. // Created: 01/02/97
  6. // Copyright: (c) 1998 Robert Roebling
  7. // Licence: wxWindows licence
  8. /////////////////////////////////////////////////////////////////////////////
  9. #ifndef __X11TEXTCTRLH__
  10. #define __X11TEXTCTRLH__
  11. // Set to 1 to use wxUniv's implementation, 0
  12. // to use wxX11's.
  13. #define wxUSE_UNIV_TEXTCTRL 1
  14. #if wxUSE_UNIV_TEXTCTRL
  15. #include "wx/univ/textctrl.h"
  16. #else
  17. #include "wx/scrolwin.h"
  18. #include "wx/arrstr.h"
  19. //-----------------------------------------------------------------------------
  20. // classes
  21. //-----------------------------------------------------------------------------
  22. class WXDLLIMPEXP_FWD_CORE wxTextCtrl;
  23. //-----------------------------------------------------------------------------
  24. // helpers
  25. //-----------------------------------------------------------------------------
  26. enum wxSourceUndo
  27. {
  28. wxSOURCE_UNDO_LINE,
  29. wxSOURCE_UNDO_ENTER,
  30. wxSOURCE_UNDO_BACK,
  31. wxSOURCE_UNDO_INSERT_LINE,
  32. wxSOURCE_UNDO_DELETE,
  33. wxSOURCE_UNDO_PASTE
  34. };
  35. class wxSourceUndoStep: public wxObject
  36. {
  37. public:
  38. wxSourceUndoStep( wxSourceUndo type, int y1, int y2, wxTextCtrl *owner );
  39. void Undo();
  40. wxSourceUndo m_type;
  41. int m_y1;
  42. int m_y2;
  43. int m_cursorX;
  44. int m_cursorY;
  45. wxTextCtrl *m_owner;
  46. wxString m_text;
  47. wxArrayString m_lines;
  48. };
  49. class wxSourceLine
  50. {
  51. public:
  52. wxSourceLine( const wxString &text = wxEmptyString )
  53. {
  54. m_text = text;
  55. }
  56. wxString m_text;
  57. };
  58. WX_DECLARE_OBJARRAY(wxSourceLine, wxSourceLineArray);
  59. enum wxSourceLanguage
  60. {
  61. wxSOURCE_LANG_NONE,
  62. wxSOURCE_LANG_CPP,
  63. wxSOURCE_LANG_PERL,
  64. wxSOURCE_LANG_PYTHON
  65. };
  66. //-----------------------------------------------------------------------------
  67. // wxTextCtrl
  68. //-----------------------------------------------------------------------------
  69. class WXDLLIMPEXP_CORE wxTextCtrl: public wxTextCtrlBase, public wxScrollHelper
  70. {
  71. public:
  72. wxTextCtrl() { Init(); }
  73. wxTextCtrl(wxWindow *parent,
  74. wxWindowID id,
  75. const wxString &value = wxEmptyString,
  76. const wxPoint &pos = wxDefaultPosition,
  77. const wxSize &size = wxDefaultSize,
  78. long style = 0,
  79. const wxValidator& validator = wxDefaultValidator,
  80. const wxString &name = wxTextCtrlNameStr);
  81. virtual ~wxTextCtrl();
  82. bool Create(wxWindow *parent,
  83. wxWindowID id,
  84. const wxString &value = wxEmptyString,
  85. const wxPoint &pos = wxDefaultPosition,
  86. const wxSize &size = wxDefaultSize,
  87. long style = 0,
  88. const wxValidator& validator = wxDefaultValidator,
  89. const wxString &name = wxTextCtrlNameStr);
  90. // required for scrolling with wxScrollHelper
  91. // ------------------------------------------
  92. virtual void PrepareDC(wxDC& dc) { DoPrepareDC(dc); }
  93. // implement base class pure virtuals
  94. // ----------------------------------
  95. virtual void ChangeValue(const wxString &value);
  96. virtual int GetLineLength(long lineNo) const;
  97. virtual wxString GetLineText(long lineNo) const;
  98. virtual int GetNumberOfLines() const;
  99. virtual bool IsModified() const;
  100. virtual bool IsEditable() const;
  101. // more readable flag testing methods
  102. // ----------------------------------
  103. bool IsPassword() const { return (GetWindowStyle() & wxTE_PASSWORD) != 0; }
  104. bool WrapLines() const { return false; }
  105. // If the return values from and to are the same, there is no selection.
  106. virtual void GetSelection(long* from, long* to) const;
  107. // operations
  108. // ----------
  109. // editing
  110. virtual void Clear();
  111. virtual void Replace(long from, long to, const wxString& value);
  112. virtual void Remove(long from, long to);
  113. // clears the dirty flag
  114. virtual void DiscardEdits();
  115. virtual void SetMaxLength(unsigned long len);
  116. // writing text inserts it at the current position, appending always
  117. // inserts it at the end
  118. virtual void WriteText(const wxString& text);
  119. virtual void AppendText(const wxString& text);
  120. // apply text attribute to the range of text (only works with richedit
  121. // controls)
  122. virtual bool SetStyle(long start, long end, const wxTextAttr& style);
  123. // translate between the position (which is just an index in the text ctrl
  124. // considering all its contents as a single strings) and (x, y) coordinates
  125. // which represent column and line.
  126. virtual long XYToPosition(long x, long y) const;
  127. virtual bool PositionToXY(long pos, long *x, long *y) const;
  128. virtual void ShowPosition(long pos);
  129. // Clipboard operations
  130. virtual void Copy();
  131. virtual void Cut();
  132. virtual void Paste();
  133. // Undo/redo
  134. virtual void Undo();
  135. virtual void Redo() {}
  136. virtual bool CanUndo() const { return (m_undos.GetCount() > 0); }
  137. virtual bool CanRedo() const { return false; }
  138. // Insertion point
  139. virtual void SetInsertionPoint(long pos);
  140. virtual void SetInsertionPointEnd();
  141. virtual long GetInsertionPoint() const;
  142. virtual wxTextPos GetLastPosition() const;
  143. virtual void SetSelection(long from, long to);
  144. virtual void SetEditable(bool editable);
  145. virtual bool Enable( bool enable = true );
  146. void OnCut(wxCommandEvent& event);
  147. void OnCopy(wxCommandEvent& event);
  148. void OnPaste(wxCommandEvent& event);
  149. void OnUndo(wxCommandEvent& event);
  150. void OnRedo(wxCommandEvent& event);
  151. void OnUpdateCut(wxUpdateUIEvent& event);
  152. void OnUpdateCopy(wxUpdateUIEvent& event);
  153. void OnUpdatePaste(wxUpdateUIEvent& event);
  154. void OnUpdateUndo(wxUpdateUIEvent& event);
  155. void OnUpdateRedo(wxUpdateUIEvent& event);
  156. bool SetFont(const wxFont& font);
  157. bool SetForegroundColour(const wxColour& colour);
  158. bool SetBackgroundColour(const wxColour& colour);
  159. void SetModified() { m_modified = true; }
  160. // textctrl specific scrolling
  161. virtual bool ScrollLines(int lines);
  162. virtual bool ScrollPages(int pages);
  163. // not part of the wxTextCtrl API from now on..
  164. void SetLanguage( wxSourceLanguage lang = wxSOURCE_LANG_NONE );
  165. void Delete();
  166. void DeleteLine();
  167. void Indent();
  168. void Unindent();
  169. bool HasSelection();
  170. void ClearSelection();
  171. int GetCursorX() { return m_cursorX; }
  172. int GetCursorY() { return m_cursorY; }
  173. bool IsModified() { return m_modified; }
  174. bool OverwriteMode() { return m_overwrite; }
  175. // implementation from now on...
  176. int PosToPixel( int line, int pos );
  177. int PixelToPos( int line, int pixel );
  178. void SearchForBrackets();
  179. void DoChar( char c );
  180. void DoBack();
  181. void DoDelete();
  182. void DoReturn();
  183. void DoDClick();
  184. wxString GetNextToken( wxString &line, size_t &pos );
  185. void DrawLinePart( wxDC &dc, int x, int y, const wxString &toDraw, const wxString &origin, const wxColour &colour);
  186. void DrawLine( wxDC &dc, int x, int y, const wxString &line, int lineNum );
  187. void OnPaint( wxPaintEvent &event );
  188. void OnEraseBackground( wxEraseEvent &event );
  189. void OnMouse( wxMouseEvent &event );
  190. void OnChar( wxKeyEvent &event );
  191. void OnSetFocus( wxFocusEvent& event );
  192. void OnKillFocus( wxFocusEvent& event );
  193. void OnInternalIdle();
  194. void RefreshLine( int n );
  195. void RefreshDown( int n );
  196. void MoveCursor( int new_x, int new_y, bool shift = false, bool centre = false );
  197. void MyAdjustScrollbars();
  198. protected:
  199. // common part of all ctors
  200. void Init();
  201. virtual wxSize DoGetBestSize() const;
  202. virtual void DoSetValue(const wxString& value, int flags = 0);
  203. friend class wxSourceUndoStep;
  204. wxSourceLineArray m_lines;
  205. wxFont m_sourceFont;
  206. wxColour m_sourceColour;
  207. wxColour m_commentColour;
  208. wxColour m_stringColour;
  209. int m_cursorX;
  210. int m_cursorY;
  211. int m_selStartX,m_selStartY;
  212. int m_selEndX,m_selEndY;
  213. int m_lineHeight;
  214. int m_charWidth;
  215. int m_longestLine;
  216. bool m_overwrite;
  217. bool m_modified;
  218. bool m_editable;
  219. bool m_ignoreInput;
  220. wxArrayString m_keywords;
  221. wxColour m_keywordColour;
  222. wxArrayString m_defines;
  223. wxColour m_defineColour;
  224. wxArrayString m_variables;
  225. wxColour m_variableColour;
  226. wxSourceLanguage m_lang;
  227. wxList m_undos;
  228. bool m_capturing;
  229. int m_bracketX;
  230. int m_bracketY;
  231. private:
  232. DECLARE_EVENT_TABLE()
  233. DECLARE_DYNAMIC_CLASS(wxTextCtrl);
  234. };
  235. //-----------------------------------------------------------------------------
  236. // this is superfluous here but helps to compile
  237. //-----------------------------------------------------------------------------
  238. // cursor movement and also selection and delete operations
  239. #define wxACTION_TEXT_GOTO wxT("goto") // to pos in numArg
  240. #define wxACTION_TEXT_FIRST wxT("first") // go to pos 0
  241. #define wxACTION_TEXT_LAST wxT("last") // go to last pos
  242. #define wxACTION_TEXT_HOME wxT("home")
  243. #define wxACTION_TEXT_END wxT("end")
  244. #define wxACTION_TEXT_LEFT wxT("left")
  245. #define wxACTION_TEXT_RIGHT wxT("right")
  246. #define wxACTION_TEXT_UP wxT("up")
  247. #define wxACTION_TEXT_DOWN wxT("down")
  248. #define wxACTION_TEXT_WORD_LEFT wxT("wordleft")
  249. #define wxACTION_TEXT_WORD_RIGHT wxT("wordright")
  250. #define wxACTION_TEXT_PAGE_UP wxT("pageup")
  251. #define wxACTION_TEXT_PAGE_DOWN wxT("pagedown")
  252. // clipboard operations
  253. #define wxACTION_TEXT_COPY wxT("copy")
  254. #define wxACTION_TEXT_CUT wxT("cut")
  255. #define wxACTION_TEXT_PASTE wxT("paste")
  256. // insert text at the cursor position: the text is in strArg of PerformAction
  257. #define wxACTION_TEXT_INSERT wxT("insert")
  258. // if the action starts with either of these prefixes and the rest of the
  259. // string is one of the movement commands, it means to select/delete text from
  260. // the current cursor position to the new one
  261. #define wxACTION_TEXT_PREFIX_SEL wxT("sel")
  262. #define wxACTION_TEXT_PREFIX_DEL wxT("del")
  263. // mouse selection
  264. #define wxACTION_TEXT_ANCHOR_SEL wxT("anchorsel")
  265. #define wxACTION_TEXT_EXTEND_SEL wxT("extendsel")
  266. #define wxACTION_TEXT_SEL_WORD wxT("wordsel")
  267. #define wxACTION_TEXT_SEL_LINE wxT("linesel")
  268. // undo or redo
  269. #define wxACTION_TEXT_UNDO wxT("undo")
  270. #define wxACTION_TEXT_REDO wxT("redo")
  271. // ----------------------------------------------------------------------------
  272. // wxTextCtrl types
  273. // ----------------------------------------------------------------------------
  274. class WXDLLIMPEXP_CORE wxStdTextCtrlInputHandler : public wxStdInputHandler
  275. {
  276. public:
  277. wxStdTextCtrlInputHandler(wxInputHandler *inphand) : wxStdInputHandler(inphand) {}
  278. virtual bool HandleKey(wxInputConsumer *consumer,
  279. const wxKeyEvent& event,
  280. bool pressed) { return false; }
  281. virtual bool HandleMouse(wxInputConsumer *consumer, const wxMouseEvent& event) { return false; }
  282. virtual bool HandleMouseMove(wxInputConsumer *consumer, const wxMouseEvent& event) { return false; }
  283. virtual bool HandleFocus(wxInputConsumer *consumer, const wxFocusEvent& event) { return false; }
  284. protected:
  285. // get the position of the mouse click
  286. static wxTextPos HitTest(const wxTextCtrl *text, const wxPoint& pos) { return 0; }
  287. // capture data
  288. wxTextCtrl *m_winCapture;
  289. };
  290. #endif
  291. // wxUSE_UNIV_TEXTCTRL
  292. #endif // __X11TEXTCTRLH__