textctrl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/msw/textctrl.h
  3. // Purpose: wxTextCtrl class
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 01/02/97
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_TEXTCTRL_H_
  11. #define _WX_TEXTCTRL_H_
  12. class WXDLLIMPEXP_CORE wxTextCtrl : public wxTextCtrlBase
  13. {
  14. public:
  15. // creation
  16. // --------
  17. wxTextCtrl() { Init(); }
  18. wxTextCtrl(wxWindow *parent, wxWindowID id,
  19. const wxString& value = wxEmptyString,
  20. const wxPoint& pos = wxDefaultPosition,
  21. const wxSize& size = wxDefaultSize,
  22. long style = 0,
  23. const wxValidator& validator = wxDefaultValidator,
  24. const wxString& name = wxTextCtrlNameStr)
  25. {
  26. Init();
  27. Create(parent, id, value, pos, size, style, validator, name);
  28. }
  29. virtual ~wxTextCtrl();
  30. bool Create(wxWindow *parent, wxWindowID id,
  31. const wxString& value = wxEmptyString,
  32. const wxPoint& pos = wxDefaultPosition,
  33. const wxSize& size = wxDefaultSize,
  34. long style = 0,
  35. const wxValidator& validator = wxDefaultValidator,
  36. const wxString& name = wxTextCtrlNameStr);
  37. // overridden wxTextEntry methods
  38. // ------------------------------
  39. virtual wxString GetValue() const;
  40. virtual wxString GetRange(long from, long to) const;
  41. virtual bool IsEmpty() const;
  42. virtual void WriteText(const wxString& text);
  43. virtual void AppendText(const wxString& text);
  44. virtual void Clear();
  45. virtual int GetLineLength(long lineNo) const;
  46. virtual wxString GetLineText(long lineNo) const;
  47. virtual int GetNumberOfLines() const;
  48. virtual void SetMaxLength(unsigned long len);
  49. virtual void GetSelection(long *from, long *to) const;
  50. virtual void Redo();
  51. virtual bool CanRedo() const;
  52. virtual void SetInsertionPointEnd();
  53. virtual long GetInsertionPoint() const;
  54. virtual wxTextPos GetLastPosition() const;
  55. // implement base class pure virtuals
  56. // ----------------------------------
  57. virtual bool IsModified() const;
  58. virtual void MarkDirty();
  59. virtual void DiscardEdits();
  60. #ifdef __WIN32__
  61. virtual bool EmulateKeyPress(const wxKeyEvent& event);
  62. #endif // __WIN32__
  63. #if wxUSE_RICHEDIT
  64. // apply text attribute to the range of text (only works with richedit
  65. // controls)
  66. virtual bool SetStyle(long start, long end, const wxTextAttr& style);
  67. virtual bool SetDefaultStyle(const wxTextAttr& style);
  68. virtual bool GetStyle(long position, wxTextAttr& style);
  69. #endif // wxUSE_RICHEDIT
  70. // translate between the position (which is just an index in the text ctrl
  71. // considering all its contents as a single strings) and (x, y) coordinates
  72. // which represent column and line.
  73. virtual long XYToPosition(long x, long y) const;
  74. virtual bool PositionToXY(long pos, long *x, long *y) const;
  75. virtual void ShowPosition(long pos);
  76. virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const;
  77. virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt,
  78. wxTextCoord *col,
  79. wxTextCoord *row) const
  80. {
  81. return wxTextCtrlBase::HitTest(pt, col, row);
  82. }
  83. // Caret handling (Windows only)
  84. bool ShowNativeCaret(bool show = true);
  85. bool HideNativeCaret() { return ShowNativeCaret(false); }
  86. // Implementation from now on
  87. // --------------------------
  88. #if wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT
  89. virtual void SetDropTarget(wxDropTarget *dropTarget);
  90. #endif // wxUSE_DRAG_AND_DROP && wxUSE_RICHEDIT
  91. virtual void SetWindowStyleFlag(long style);
  92. virtual void Command(wxCommandEvent& event);
  93. virtual bool MSWCommand(WXUINT param, WXWORD id);
  94. virtual WXHBRUSH MSWControlColor(WXHDC hDC, WXHWND hWnd);
  95. #if wxUSE_RICHEDIT
  96. virtual bool MSWOnNotify(int idCtrl, WXLPARAM lParam, WXLPARAM *result);
  97. int GetRichVersion() const { return m_verRichEdit; }
  98. bool IsRich() const { return m_verRichEdit != 0; }
  99. // rich edit controls are not compatible with normal ones and we must set
  100. // the colours and font for them otherwise
  101. virtual bool SetBackgroundColour(const wxColour& colour);
  102. virtual bool SetForegroundColour(const wxColour& colour);
  103. virtual bool SetFont(const wxFont& font);
  104. #else
  105. bool IsRich() const { return false; }
  106. #endif // wxUSE_RICHEDIT
  107. #if wxUSE_INKEDIT && wxUSE_RICHEDIT
  108. bool IsInkEdit() const { return m_isInkEdit != 0; }
  109. #else
  110. bool IsInkEdit() const { return false; }
  111. #endif
  112. virtual void AdoptAttributesFromHWND();
  113. virtual bool AcceptsFocusFromKeyboard() const;
  114. // returns true if the platform should explicitly apply a theme border
  115. virtual bool CanApplyThemeBorder() const;
  116. // callbacks
  117. void OnDropFiles(wxDropFilesEvent& event);
  118. void OnChar(wxKeyEvent& event); // Process 'enter' if required
  119. void OnCut(wxCommandEvent& event);
  120. void OnCopy(wxCommandEvent& event);
  121. void OnPaste(wxCommandEvent& event);
  122. void OnUndo(wxCommandEvent& event);
  123. void OnRedo(wxCommandEvent& event);
  124. void OnDelete(wxCommandEvent& event);
  125. void OnSelectAll(wxCommandEvent& event);
  126. void OnUpdateCut(wxUpdateUIEvent& event);
  127. void OnUpdateCopy(wxUpdateUIEvent& event);
  128. void OnUpdatePaste(wxUpdateUIEvent& event);
  129. void OnUpdateUndo(wxUpdateUIEvent& event);
  130. void OnUpdateRedo(wxUpdateUIEvent& event);
  131. void OnUpdateDelete(wxUpdateUIEvent& event);
  132. void OnUpdateSelectAll(wxUpdateUIEvent& event);
  133. // Show a context menu for Rich Edit controls (the standard
  134. // EDIT control has one already)
  135. void OnContextMenu(wxContextMenuEvent& event);
  136. #if wxABI_VERSION >= 30002
  137. // Create context menu for RICHEDIT controls. This may be called once during
  138. // the control's lifetime or every time the menu is shown, depending on
  139. // implementation.
  140. wxMenu *MSWCreateContextMenu();
  141. #endif
  142. // be sure the caret remains invisible if the user
  143. // called HideNativeCaret() before
  144. void OnSetFocus(wxFocusEvent& event);
  145. // intercept WM_GETDLGCODE
  146. virtual WXLRESULT MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam);
  147. virtual bool MSWShouldPreProcessMessage(WXMSG* pMsg);
  148. virtual WXDWORD MSWGetStyle(long style, WXDWORD *exstyle) const;
  149. protected:
  150. // common part of all ctors
  151. void Init();
  152. virtual bool DoLoadFile(const wxString& file, int fileType);
  153. // creates the control of appropriate class (plain or rich edit) with the
  154. // styles corresponding to m_windowStyle
  155. //
  156. // this is used by ctor/Create() and when we need to recreate the control
  157. // later
  158. bool MSWCreateText(const wxString& value,
  159. const wxPoint& pos,
  160. const wxSize& size);
  161. virtual void DoSetValue(const wxString &value, int flags = 0);
  162. virtual wxPoint DoPositionToCoords(long pos) const;
  163. // return true if this control has a user-set limit on amount of text (i.e.
  164. // the limit is due to a previous call to SetMaxLength() and not built in)
  165. bool HasSpaceLimit(unsigned int *len) const;
  166. // Used by EN_MAXTEXT handler to increase the size limit (will do nothing
  167. // if the current limit is big enough). Should never be called directly.
  168. //
  169. // Returns true if we increased the limit to allow entering more text,
  170. // false if we hit the limit set by SetMaxLength() and so didn't change it.
  171. bool AdjustSpaceLimit();
  172. #if wxUSE_RICHEDIT && (!wxUSE_UNICODE || wxUSE_UNICODE_MSLU)
  173. // replace the selection or the entire control contents with the given text
  174. // in the specified encoding
  175. bool StreamIn(const wxString& value, wxFontEncoding encoding, bool selOnly);
  176. // get the contents of the control out as text in the given encoding
  177. wxString StreamOut(wxFontEncoding encoding, bool selOnly = false) const;
  178. #endif // wxUSE_RICHEDIT
  179. // replace the contents of the selection or of the entire control with the
  180. // given text
  181. void DoWriteText(const wxString& text,
  182. int flags = SetValue_SendEvent | SetValue_SelectionOnly);
  183. // set the selection (possibly without scrolling the caret into view)
  184. void DoSetSelection(long from, long to, int flags);
  185. // get the length of the line containing the character at the given
  186. // position
  187. long GetLengthOfLineContainingPos(long pos) const;
  188. // send TEXT_UPDATED event, return true if it was handled, false otherwise
  189. bool SendUpdateEvent();
  190. virtual wxSize DoGetBestSize() const;
  191. virtual wxSize DoGetSizeFromTextSize(int xlen, int ylen = -1) const;
  192. #if wxUSE_RICHEDIT
  193. // Apply the character-related parts of wxTextAttr to the given selection
  194. // or the entire control if start == end == -1.
  195. //
  196. // This function is private and should only be called for rich edit
  197. // controls and with (from, to) already in correct order, i.e. from <= to.
  198. bool MSWSetCharFormat(const wxTextAttr& attr, long from = -1, long to = -1);
  199. // Same as above for paragraph-related parts of wxTextAttr. Note that this
  200. // can only be applied to the selection as RichEdit doesn't support setting
  201. // the paragraph styles globally.
  202. bool MSWSetParaFormat(const wxTextAttr& attr, long from, long to);
  203. // we're using RICHEDIT (and not simple EDIT) control if this field is not
  204. // 0, it also gives the version of the RICHEDIT control being used
  205. // (although not directly: 1 is for 1.0, 2 is for either 2.0 or 3.0 as we
  206. // can't nor really need to distinguish between them and 4 is for 4.1)
  207. int m_verRichEdit;
  208. #endif // wxUSE_RICHEDIT
  209. // number of EN_UPDATE events sent by Windows when we change the controls
  210. // text ourselves: we want this to be exactly 1
  211. int m_updatesCount;
  212. private:
  213. virtual void EnableTextChangedEvents(bool enable)
  214. {
  215. m_updatesCount = enable ? -1 : -2;
  216. }
  217. // implement wxTextEntry pure virtual: it implements all the operations for
  218. // the simple EDIT controls
  219. virtual WXHWND GetEditHWND() const { return m_hWnd; }
  220. void OnKeyDown(wxKeyEvent& event);
  221. DECLARE_EVENT_TABLE()
  222. DECLARE_DYNAMIC_CLASS_NO_COPY(wxTextCtrl)
  223. wxMenu* m_privateContextMenu;
  224. bool m_isNativeCaretShown;
  225. #if wxUSE_INKEDIT && wxUSE_RICHEDIT
  226. int m_isInkEdit;
  227. #endif
  228. };
  229. #endif // _WX_TEXTCTRL_H_