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