wxStreamToTextRedirector fixes, define wxHAS_TEXT_WINDOW_STREAM.
[wxWidgets.git] / include / wx / textctrl.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.h
3 // Purpose: wxTextCtrlBase class - the interface of wxTextCtrl
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 13.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWidgets team
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_TEXTCTRL_H_BASE_
13 #define _WX_TEXTCTRL_H_BASE_
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
20 #pragma interface "textctrlbase.h"
21 #endif
22
23 #include "wx/defs.h"
24
25 #if wxUSE_TEXTCTRL
26
27 #include "wx/control.h" // the base class
28 #include "wx/dynarray.h" // wxArrayInt
29 #include "wx/gdicmn.h" // wxPoint
30
31 // Open Watcom 1.3 does allow only ios::rdbuf() while
32 // we want something with streambuf parameter
33 // Also, can't use streambuf if making or using a DLL :-(
34
35 #if defined(__WATCOMC__) || \
36 defined(__MWERKS__) || \
37 (defined(__WINDOWS__) && (defined(WXUSINGDLL) || defined(WXMAKINGDLL)))
38 #define wxHAS_TEXT_WINDOW_STREAM 0
39 #elif wxUSE_STD_IOSTREAM
40 #include "wx/ioswrap.h"
41 #define wxHAS_TEXT_WINDOW_STREAM 1
42 #else
43 #define wxHAS_TEXT_WINDOW_STREAM 0
44 #endif
45
46 #if WXWIN_COMPATIBILITY_2_4 && !wxHAS_TEXT_WINDOW_STREAM
47 // define old flag if one could use it somewhere
48 #define NO_TEXT_WINDOW_STREAM
49 #endif
50
51 class WXDLLEXPORT wxTextCtrl;
52 class WXDLLEXPORT wxTextCtrlBase;
53
54 // ----------------------------------------------------------------------------
55 // wxTextCtrl types
56 // ----------------------------------------------------------------------------
57
58 // wxTextPos is the position in the text
59 typedef long wxTextPos;
60
61 // wxTextCoord is the line or row number (which should have been unsigned but
62 // is long for backwards compatibility)
63 typedef long wxTextCoord;
64
65 // ----------------------------------------------------------------------------
66 // constants
67 // ----------------------------------------------------------------------------
68
69 extern WXDLLEXPORT_DATA(const wxChar*) wxTextCtrlNameStr;
70
71 // this is intentionally not enum to avoid warning fixes with
72 // typecasting from enum type to wxTextCoord
73 const wxTextCoord wxOutOfRangeTextCoord = -1;
74 const wxTextCoord wxInvalidTextCoord = -2;
75
76 // ----------------------------------------------------------------------------
77 // wxTextCtrl style flags
78 // ----------------------------------------------------------------------------
79
80 // the flag bit 0x0001 s free but should be used only for the things which
81 // don't make sense for a text control used by wxTextEntryDialog because they
82 // would otherwise conflict with wxOK, wxCANCEL, wxCENTRE
83
84 #define wxTE_NO_VSCROLL 0x0002
85 #define wxTE_AUTO_SCROLL 0x0008
86
87 #define wxTE_READONLY 0x0010
88 #define wxTE_MULTILINE 0x0020
89 #define wxTE_PROCESS_TAB 0x0040
90
91 // alignment flags
92 #define wxTE_LEFT 0x0000 // 0x0000
93 #define wxTE_CENTER wxALIGN_CENTER_HORIZONTAL // 0x0100
94 #define wxTE_RIGHT wxALIGN_RIGHT // 0x0200
95 #define wxTE_CENTRE wxTE_CENTER
96
97 // this style means to use RICHEDIT control and does something only under wxMSW
98 // and Win32 and is silently ignored under all other platforms
99 #define wxTE_RICH 0x0080
100
101 #define wxTE_PROCESS_ENTER 0x0400
102 #define wxTE_PASSWORD 0x0800
103
104 // automatically detect the URLs and generate the events when mouse is
105 // moved/clicked over an URL
106 //
107 // this is for Win32 richedit and wxGTK2 multiline controls only so far
108 #define wxTE_AUTO_URL 0x1000
109
110 // by default, the Windows text control doesn't show the selection when it
111 // doesn't have focus - use this style to force it to always show it
112 #define wxTE_NOHIDESEL 0x2000
113
114 // use wxHSCROLL to not wrap text at all, wxTE_CHARWRAP to wrap it at any
115 // position and wxTE_WORDWRAP to wrap at words boundary
116 //
117 // if no wrapping style is given at all, the control wraps at word boundary
118 #define wxTE_DONTWRAP wxHSCROLL
119 #define wxTE_CHARWRAP 0x4000 // wrap at any position
120 #define wxTE_WORDWRAP 0x0001 // wrap only at words boundaries
121 #define wxTE_BESTWRAP 0x0000 // this is the default
122
123 // obsolete synonym
124 #define wxTE_LINEWRAP wxTE_CHARWRAP
125
126 // force using RichEdit version 2.0 or 3.0 instead of 1.0 (default) for
127 // wxTE_RICH controls - can be used together with or instead of wxTE_RICH
128 #define wxTE_RICH2 0x8000
129
130 // reuse wxTE_RICH2's value for CAPEDIT control on Windows CE
131 #if defined(__SMARTPHONE__) || defined(__POCKETPC__)
132 #define wxTE_CAPITALIZE wxTE_RICH2
133 #else
134 #define wxTE_CAPITALIZE 0
135 #endif
136
137 // ----------------------------------------------------------------------------
138 // wxTextCtrl::HitTest return values
139 // ----------------------------------------------------------------------------
140
141 // the point asked is ...
142 enum wxTextCtrlHitTestResult
143 {
144 wxTE_HT_UNKNOWN = -2, // this means HitTest() is simply not implemented
145 wxTE_HT_BEFORE, // either to the left or upper
146 wxTE_HT_ON_TEXT, // directly on
147 wxTE_HT_BELOW, // below [the last line]
148 wxTE_HT_BEYOND // after [the end of line]
149 };
150 // ... the character returned
151
152 // ----------------------------------------------------------------------------
153 // Types for wxTextAttr
154 // ----------------------------------------------------------------------------
155
156 // Alignment
157
158 enum wxTextAttrAlignment
159 {
160 wxTEXT_ALIGNMENT_DEFAULT,
161 wxTEXT_ALIGNMENT_LEFT,
162 wxTEXT_ALIGNMENT_CENTRE,
163 wxTEXT_ALIGNMENT_CENTER = wxTEXT_ALIGNMENT_CENTRE,
164 wxTEXT_ALIGNMENT_RIGHT,
165 wxTEXT_ALIGNMENT_JUSTIFIED
166 };
167
168 // Flags to indicate which attributes are being applied
169
170 #define wxTEXT_ATTR_TEXT_COLOUR 0x0001
171 #define wxTEXT_ATTR_BACKGROUND_COLOUR 0x0002
172 #define wxTEXT_ATTR_FONT_FACE 0x0004
173 #define wxTEXT_ATTR_FONT_SIZE 0x0008
174 #define wxTEXT_ATTR_FONT_WEIGHT 0x0010
175 #define wxTEXT_ATTR_FONT_ITALIC 0x0020
176 #define wxTEXT_ATTR_FONT_UNDERLINE 0x0040
177 #define wxTEXT_ATTR_FONT \
178 wxTEXT_ATTR_FONT_FACE | wxTEXT_ATTR_FONT_SIZE | wxTEXT_ATTR_FONT_WEIGHT | wxTEXT_ATTR_FONT_ITALIC | wxTEXT_ATTR_FONT_UNDERLINE
179 #define wxTEXT_ATTR_ALIGNMENT 0x0080
180 #define wxTEXT_ATTR_LEFT_INDENT 0x0100
181 #define wxTEXT_ATTR_RIGHT_INDENT 0x0200
182 #define wxTEXT_ATTR_TABS 0x0400
183
184 // ----------------------------------------------------------------------------
185 // wxTextAttr: a structure containing the visual attributes of a text
186 // ----------------------------------------------------------------------------
187
188 class WXDLLEXPORT wxTextAttr
189 {
190 public:
191 // ctors
192 wxTextAttr() { Init(); }
193 wxTextAttr(const wxColour& colText,
194 const wxColour& colBack = wxNullColour,
195 const wxFont& font = wxNullFont,
196 wxTextAttrAlignment alignment = wxTEXT_ALIGNMENT_DEFAULT);
197
198 // operations
199 void Init();
200
201 // operators
202 void operator= (const wxTextAttr& attr);
203
204 // setters
205 void SetTextColour(const wxColour& colText) { m_colText = colText; m_flags |= wxTEXT_ATTR_TEXT_COLOUR; }
206 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; m_flags |= wxTEXT_ATTR_BACKGROUND_COLOUR; }
207 void SetFont(const wxFont& font, long flags = wxTEXT_ATTR_FONT) { m_font = font; m_flags |= flags; }
208 void SetAlignment(wxTextAttrAlignment alignment) { m_textAlignment = alignment; m_flags |= wxTEXT_ATTR_ALIGNMENT; }
209 void SetTabs(const wxArrayInt& tabs) { m_tabs = tabs; m_flags |= wxTEXT_ATTR_TABS; }
210 void SetLeftIndent(int indent, int subIndent = 0) { m_leftIndent = indent; m_leftSubIndent = subIndent; m_flags |= wxTEXT_ATTR_LEFT_INDENT; }
211 void SetRightIndent(int indent) { m_rightIndent = indent; m_flags |= wxTEXT_ATTR_RIGHT_INDENT; }
212 void SetFlags(long flags) { m_flags = flags; }
213
214 // accessors
215 bool HasTextColour() const { return m_colText.Ok() && HasFlag(wxTEXT_ATTR_TEXT_COLOUR) ; }
216 bool HasBackgroundColour() const { return m_colBack.Ok() && HasFlag(wxTEXT_ATTR_BACKGROUND_COLOUR) ; }
217 bool HasFont() const { return m_font.Ok() && HasFlag(wxTEXT_ATTR_FONT) ; }
218 bool HasAlignment() const { return (m_textAlignment != wxTEXT_ALIGNMENT_DEFAULT) || ((m_flags & wxTEXT_ATTR_ALIGNMENT) != 0) ; }
219 bool HasTabs() const { return (m_flags & wxTEXT_ATTR_TABS) != 0 ; }
220 bool HasLeftIndent() const { return (m_flags & wxTEXT_ATTR_LEFT_INDENT) != 0 ; }
221 bool HasRightIndent() const { return (m_flags & wxTEXT_ATTR_RIGHT_INDENT) != 0 ; }
222 bool HasFlag(long flag) const { return (m_flags & flag) != 0; }
223
224 const wxColour& GetTextColour() const { return m_colText; }
225 const wxColour& GetBackgroundColour() const { return m_colBack; }
226 const wxFont& GetFont() const { return m_font; }
227 wxTextAttrAlignment GetAlignment() const { return m_textAlignment; }
228 const wxArrayInt& GetTabs() const { return m_tabs; }
229 long GetLeftIndent() const { return m_leftIndent; }
230 long GetLeftSubIndent() const { return m_leftSubIndent; }
231 long GetRightIndent() const { return m_rightIndent; }
232 long GetFlags() const { return m_flags; }
233
234 // returns false if we have any attributes set, true otherwise
235 bool IsDefault() const
236 {
237 return !HasTextColour() && !HasBackgroundColour() && !HasFont() && !HasAlignment() &&
238 !HasTabs() && !HasLeftIndent() && !HasRightIndent() ;
239 }
240
241 // return the attribute having the valid font and colours: it uses the
242 // attributes set in attr and falls back first to attrDefault and then to
243 // the text control font/colours for those attributes which are not set
244 static wxTextAttr Combine(const wxTextAttr& attr,
245 const wxTextAttr& attrDef,
246 const wxTextCtrlBase *text);
247
248 private:
249 long m_flags;
250 wxColour m_colText,
251 m_colBack;
252 wxFont m_font;
253 wxTextAttrAlignment m_textAlignment;
254 wxArrayInt m_tabs; // array of int: tab stops in 1/10 mm
255 int m_leftIndent; // left indent in 1/10 mm
256 int m_leftSubIndent; // left indent for all but the first
257 // line in a paragraph relative to the
258 // first line, in 1/10 mm
259 int m_rightIndent; // right indent in 1/10 mm
260 };
261
262 // ----------------------------------------------------------------------------
263 // wxTextCtrl: a single or multiple line text zone where user can enter and
264 // edit text
265 // ----------------------------------------------------------------------------
266
267 class WXDLLEXPORT wxTextCtrlBase : public wxControl
268 #if wxHAS_TEXT_WINDOW_STREAM
269 , public wxSTD streambuf
270 #endif
271
272 {
273 public:
274 // creation
275 // --------
276
277 wxTextCtrlBase(){}
278 ~wxTextCtrlBase(){}
279
280 // accessors
281 // ---------
282
283 virtual wxString GetValue() const = 0;
284 virtual void SetValue(const wxString& value) = 0;
285
286 virtual wxString GetRange(long from, long to) const;
287
288 virtual int GetLineLength(long lineNo) const = 0;
289 virtual wxString GetLineText(long lineNo) const = 0;
290 virtual int GetNumberOfLines() const = 0;
291
292 virtual bool IsModified() const = 0;
293 virtual bool IsEditable() const = 0;
294
295 // more readable flag testing methods
296 bool IsSingleLine() const { return !HasFlag(wxTE_MULTILINE); }
297 bool IsMultiLine() const { return !IsSingleLine(); }
298
299 // If the return values from and to are the same, there is no selection.
300 virtual void GetSelection(long* from, long* to) const = 0;
301
302 virtual wxString GetStringSelection() const;
303
304 // operations
305 // ----------
306
307 // editing
308 virtual void Clear() = 0;
309 virtual void Replace(long from, long to, const wxString& value) = 0;
310 virtual void Remove(long from, long to) = 0;
311
312 // load/save the controls contents from/to the file
313 virtual bool LoadFile(const wxString& file);
314 virtual bool SaveFile(const wxString& file = wxEmptyString);
315
316 // sets/clears the dirty flag
317 virtual void MarkDirty() = 0;
318 virtual void DiscardEdits() = 0;
319
320 // set the max number of characters which may be entered in a single line
321 // text control
322 virtual void SetMaxLength(unsigned long WXUNUSED(len)) { }
323
324 // writing text inserts it at the current position, appending always
325 // inserts it at the end
326 virtual void WriteText(const wxString& text) = 0;
327 virtual void AppendText(const wxString& text) = 0;
328
329 // insert the character which would have resulted from this key event,
330 // return true if anything has been inserted
331 virtual bool EmulateKeyPress(const wxKeyEvent& event);
332
333 // text control under some platforms supports the text styles: these
334 // methods allow to apply the given text style to the given selection or to
335 // set/get the style which will be used for all appended text
336 virtual bool SetStyle(long start, long end, const wxTextAttr& style);
337 virtual bool GetStyle(long position, wxTextAttr& style);
338 virtual bool SetDefaultStyle(const wxTextAttr& style);
339 virtual const wxTextAttr& GetDefaultStyle() const;
340
341 // translate between the position (which is just an index in the text ctrl
342 // considering all its contents as a single strings) and (x, y) coordinates
343 // which represent column and line.
344 virtual long XYToPosition(long x, long y) const = 0;
345 virtual bool PositionToXY(long pos, long *x, long *y) const = 0;
346
347 virtual void ShowPosition(long pos) = 0;
348
349 // find the character at position given in pixels
350 //
351 // NB: pt is in device coords (not adjusted for the client area origin nor
352 // scrolling)
353 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt, long *pos) const;
354 virtual wxTextCtrlHitTestResult HitTest(const wxPoint& pt,
355 wxTextCoord *col,
356 wxTextCoord *row) const;
357
358 // Clipboard operations
359 virtual void Copy() = 0;
360 virtual void Cut() = 0;
361 virtual void Paste() = 0;
362
363 virtual bool CanCopy() const;
364 virtual bool CanCut() const;
365 virtual bool CanPaste() const;
366
367 // Undo/redo
368 virtual void Undo() = 0;
369 virtual void Redo() = 0;
370
371 virtual bool CanUndo() const = 0;
372 virtual bool CanRedo() const = 0;
373
374 // Insertion point
375 virtual void SetInsertionPoint(long pos) = 0;
376 virtual void SetInsertionPointEnd() = 0;
377 virtual long GetInsertionPoint() const = 0;
378 virtual wxTextPos GetLastPosition() const = 0;
379
380 virtual void SetSelection(long from, long to) = 0;
381 virtual void SelectAll();
382 virtual void SetEditable(bool editable) = 0;
383
384 // override streambuf method
385 #if wxHAS_TEXT_WINDOW_STREAM
386 int overflow(int i);
387 #endif // wxHAS_TEXT_WINDOW_STREAM
388
389 // stream-like insertion operators: these are always available, whether we
390 // were, or not, compiled with streambuf support
391 wxTextCtrl& operator<<(const wxString& s);
392 wxTextCtrl& operator<<(int i);
393 wxTextCtrl& operator<<(long i);
394 wxTextCtrl& operator<<(float f);
395 wxTextCtrl& operator<<(double d);
396 wxTextCtrl& operator<<(const wxChar c);
397
398 // do the window-specific processing after processing the update event
399 virtual void DoUpdateWindowUI(wxUpdateUIEvent& event);
400
401 virtual bool ShouldInheritColours() const { return false; }
402
403 protected:
404 // the name of the last file loaded with LoadFile() which will be used by
405 // SaveFile() by default
406 wxString m_filename;
407
408 // the text style which will be used for any new text added to the control
409 wxTextAttr m_defaultStyle;
410
411 DECLARE_NO_COPY_CLASS(wxTextCtrlBase)
412 };
413
414 // ----------------------------------------------------------------------------
415 // include the platform-dependent class definition
416 // ----------------------------------------------------------------------------
417
418 #if defined(__WXX11__)
419 #include "wx/x11/textctrl.h"
420 #elif defined(__WXUNIVERSAL__)
421 #include "wx/univ/textctrl.h"
422 #elif defined(__SMARTPHONE__) && defined(__WXWINCE__)
423 #include "wx/msw/wince/textctrlce.h"
424 #elif defined(__WXMSW__)
425 #include "wx/msw/textctrl.h"
426 #elif defined(__WXMOTIF__)
427 #include "wx/motif/textctrl.h"
428 #elif defined(__WXGTK__)
429 #include "wx/gtk/textctrl.h"
430 #elif defined(__WXMAC__)
431 #include "wx/mac/textctrl.h"
432 #elif defined(__WXCOCOA__)
433 #include "wx/cocoa/textctrl.h"
434 #elif defined(__WXPM__)
435 #include "wx/os2/textctrl.h"
436 #endif
437
438 // ----------------------------------------------------------------------------
439 // wxTextCtrl events
440 // ----------------------------------------------------------------------------
441
442 #if !WXWIN_COMPATIBILITY_EVENT_TYPES
443
444 BEGIN_DECLARE_EVENT_TYPES()
445 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED, 7)
446 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER, 8)
447 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL, 13)
448 DECLARE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN, 14)
449 END_DECLARE_EVENT_TYPES()
450
451 #endif // !WXWIN_COMPATIBILITY_EVENT_TYPES
452
453 class WXDLLEXPORT wxTextUrlEvent : public wxCommandEvent
454 {
455 public:
456 wxTextUrlEvent(int winid, const wxMouseEvent& evtMouse,
457 long start, long end)
458 : wxCommandEvent(wxEVT_COMMAND_TEXT_URL, winid)
459 , m_evtMouse(evtMouse), m_start(start), m_end(end)
460 { }
461
462 // get the mouse event which happend over the URL
463 const wxMouseEvent& GetMouseEvent() const { return m_evtMouse; }
464
465 // get the start of the URL
466 long GetURLStart() const { return m_start; }
467
468 // get the end of the URL
469 long GetURLEnd() const { return m_end; }
470
471 protected:
472 // the corresponding mouse event
473 wxMouseEvent m_evtMouse;
474
475 // the start and end indices of the URL in the text control
476 long m_start,
477 m_end;
478
479 private:
480 DECLARE_DYNAMIC_CLASS_NO_COPY(wxTextUrlEvent)
481
482 public:
483 // for wxWin RTTI only, don't use
484 wxTextUrlEvent() : m_evtMouse(), m_start(0), m_end(0) { }
485 };
486
487 typedef void (wxEvtHandler::*wxTextUrlEventFunction)(wxTextUrlEvent&);
488
489 #define wxTextEventHandler(func) wxCommandEventHandler(func)
490 #define wxTextUrlEventHandler(func) \
491 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxTextUrlEventFunction, &func)
492
493 #define wx__DECLARE_TEXTEVT(evt, id, fn) \
494 wx__DECLARE_EVT1(wxEVT_COMMAND_TEXT_ ## evt, id, wxTextEventHandler(fn))
495
496 #define wx__DECLARE_TEXTURLEVT(evt, id, fn) \
497 wx__DECLARE_EVT1(wxEVT_COMMAND_TEXT_ ## evt, id, wxTextUrlEventHandler(fn))
498
499 #define EVT_TEXT(id, fn) wx__DECLARE_TEXTEVT(UPDATED, id, fn)
500 #define EVT_TEXT_ENTER(id, fn) wx__DECLARE_TEXTEVT(ENTER, id, fn)
501 #define EVT_TEXT_URL(id, fn) wx__DECLARE_TEXTURLEVT(URL, id, fn)
502 #define EVT_TEXT_MAXLEN(id, fn) wx__DECLARE_TEXTEVT(MAXLEN, id, fn)
503
504 #if wxHAS_TEXT_WINDOW_STREAM
505
506 // ----------------------------------------------------------------------------
507 // wxStreamToTextRedirector: this class redirects all data sent to the given
508 // C++ stream to the wxTextCtrl given to its ctor during its lifetime.
509 // ----------------------------------------------------------------------------
510
511 class WXDLLEXPORT wxStreamToTextRedirector
512 {
513 private:
514 void Init(wxTextCtrl *text)
515 {
516 m_sbufOld = m_ostr.rdbuf();
517 m_ostr.rdbuf(text);
518 }
519
520 public:
521 wxStreamToTextRedirector(wxTextCtrl *text)
522 : m_ostr(wxSTD cout)
523 {
524 Init(text);
525 }
526
527 wxStreamToTextRedirector(wxTextCtrl *text, wxSTD ostream *ostr)
528 : m_ostr(*ostr)
529 {
530 Init(text);
531 }
532
533 ~wxStreamToTextRedirector()
534 {
535 m_ostr.rdbuf(m_sbufOld);
536 }
537
538 private:
539 // the stream we're redirecting
540 wxSTD ostream& m_ostr;
541
542 // the old streambuf (before we changed it)
543 wxSTD streambuf *m_sbufOld;
544 };
545
546 #endif // wxHAS_TEXT_WINDOW_STREAM
547
548 #endif // wxUSE_TEXTCTRL
549
550 #endif
551 // _WX_TEXTCTRL_H_BASE_