implemented text styles for GTK+
[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) wxWindows 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 #ifdef __GNUG__
20 #pragma interface "textctrlbase.h"
21 #endif
22
23 #include "wx/defs.h"
24 #include "wx/control.h" // the base class
25
26 // 16-bit Borland 4.0 doesn't seem to allow multiple inheritance with wxWindow
27 // and streambuf: it complains about deriving a huge class from the huge class
28 // streambuf. !! Also, can't use streambuf if making or using a DLL :-(
29
30 #if (defined(__BORLANDC__)) || defined(__MWERKS__) || defined(_WINDLL) || defined(WXUSINGDLL) || defined(WXMAKINGDLL)
31 #define NO_TEXT_WINDOW_STREAM
32 #endif
33
34 // the streambuf which is used in the declaration of wxTextCtrlBase below is not compatible
35 // with the standard-conforming implementation found in newer egcs versions
36 // (that is, the libstdc++ v3 that is shipped with it)
37 #if defined(__GNUC__)&&( (__GNUC__>2) ||( (__GNUC__==2)&&(__GNUC_MINOR__>97) ) )
38 #define NO_TEXT_WINDOW_STREAM
39 #endif
40
41 #ifndef NO_TEXT_WINDOW_STREAM
42 #if wxUSE_STD_IOSTREAM
43 #include "wx/ioswrap.h" // for iostream classes if we need them
44 #else // !wxUSE_STD_IOSTREAM
45 // can't compile this feature in if we don't use streams at all
46 #define NO_TEXT_WINDOW_STREAM
47 #endif // wxUSE_STD_IOSTREAM/!wxUSE_STD_IOSTREAM
48 #endif
49
50 class WXDLLEXPORT wxTextCtrl;
51
52 // ----------------------------------------------------------------------------
53 // constants
54 // ----------------------------------------------------------------------------
55
56 WXDLLEXPORT_DATA(extern const wxChar*) wxTextCtrlNameStr;
57 WXDLLEXPORT_DATA(extern const wxChar*) wxEmptyString;
58
59 // ----------------------------------------------------------------------------
60 // wxTextAttr: a structure containing the visual attributes of a text
61 // ----------------------------------------------------------------------------
62
63 class WXDLLEXPORT wxTextAttr
64 {
65 public:
66 // ctors
67 wxTextAttr() { }
68 wxTextAttr(const wxColour& colText,
69 const wxColour& colBack = wxNullColour,
70 const wxFont& font = wxNullFont)
71 : m_colText(colText), m_colBack(colBack), m_font(font) { }
72
73 // setters
74 void SetTextColour(const wxColour& colText) { m_colText = colText; }
75 void SetBackgroundColour(const wxColour& colBack) { m_colBack = colBack; }
76 void SetFont(const wxFont& font) { m_font = font; }
77
78 // accessors
79 bool HasTextColour() const { return m_colText.Ok(); }
80 bool HasBackgroundColour() const { return m_colBack.Ok(); }
81 bool HasFont() const { return m_font.Ok(); }
82
83 // setters
84 const wxColour& GetTextColour() const { return m_colText; }
85 const wxColour& GetBackgroundColour() const { return m_colBack; }
86 const wxFont& GetFont() const { return m_font; }
87
88 // returns false if we have any attributes set, true otherwise
89 bool IsDefault() const
90 {
91 return !HasTextColour() && !HasBackgroundColour() && !HasFont();
92 }
93
94 private:
95 wxColour m_colText,
96 m_colBack;
97 wxFont m_font;
98 };
99
100 // ----------------------------------------------------------------------------
101 // wxTextCtrl: a single or multiple line text zone where user can enter and
102 // edit text
103 // ----------------------------------------------------------------------------
104
105 class WXDLLEXPORT wxTextCtrlBase : public wxControl
106 #ifndef NO_TEXT_WINDOW_STREAM
107 , public streambuf
108 #endif
109
110 {
111 public:
112 // creation
113 // --------
114
115 wxTextCtrlBase();
116 ~wxTextCtrlBase();
117
118 // accessors
119 // ---------
120
121 virtual wxString GetValue() const = 0;
122 virtual void SetValue(const wxString& value) = 0;
123
124 virtual int GetLineLength(long lineNo) const = 0;
125 virtual wxString GetLineText(long lineNo) const = 0;
126 virtual int GetNumberOfLines() const = 0;
127
128 virtual bool IsModified() const = 0;
129 virtual bool IsEditable() const = 0;
130
131 // If the return values from and to are the same, there is no selection.
132 virtual void GetSelection(long* from, long* to) const = 0;
133
134 // operations
135 // ----------
136
137 // editing
138 virtual void Clear() = 0;
139 virtual void Replace(long from, long to, const wxString& value) = 0;
140 virtual void Remove(long from, long to) = 0;
141
142 // load/save the controls contents from/to the file
143 virtual bool LoadFile(const wxString& file);
144 virtual bool SaveFile(const wxString& file = wxEmptyString);
145
146 // clears the dirty flag
147 virtual void DiscardEdits() = 0;
148
149 // writing text inserts it at the current position, appending always
150 // inserts it at the end
151 virtual void WriteText(const wxString& text) = 0;
152 virtual void AppendText(const wxString& text) = 0;
153
154 // text control under some platforms supports the text styles: these
155 // methods allow to apply the given text style to the given selection or to
156 // set/get the style which will be used for all appended text
157 virtual bool SetStyle(long start, long end, const wxTextAttr& style);
158 virtual bool SetDefaultStyle(const wxTextAttr& style);
159 virtual const wxTextAttr& GetDefaultStyle() const;
160
161 // translate between the position (which is just an index in the text ctrl
162 // considering all its contents as a single strings) and (x, y) coordinates
163 // which represent column and line.
164 virtual long XYToPosition(long x, long y) const = 0;
165 virtual bool PositionToXY(long pos, long *x, long *y) const = 0;
166
167 virtual void ShowPosition(long pos) = 0;
168
169 // Clipboard operations
170 virtual void Copy() = 0;
171 virtual void Cut() = 0;
172 virtual void Paste() = 0;
173
174 virtual bool CanCopy() const = 0;
175 virtual bool CanCut() const = 0;
176 virtual bool CanPaste() const = 0;
177
178 // Undo/redo
179 virtual void Undo() = 0;
180 virtual void Redo() = 0;
181
182 virtual bool CanUndo() const = 0;
183 virtual bool CanRedo() const = 0;
184
185 // Insertion point
186 virtual void SetInsertionPoint(long pos) = 0;
187 virtual void SetInsertionPointEnd() = 0;
188 virtual long GetInsertionPoint() const = 0;
189 virtual long GetLastPosition() const = 0;
190
191 virtual void SetSelection(long from, long to) = 0;
192 virtual void SetEditable(bool editable) = 0;
193
194 // streambuf methods
195 #ifndef NO_TEXT_WINDOW_STREAM
196 int overflow(int i);
197 int sync();
198 int underflow();
199 #endif // NO_TEXT_WINDOW_STREAM
200
201 // stream-like insertion operators: these are always available, whether we
202 // were, or not, compiled with streambuf support
203 wxTextCtrl& operator<<(const wxString& s);
204 wxTextCtrl& operator<<(int i);
205 wxTextCtrl& operator<<(long i);
206 wxTextCtrl& operator<<(float f);
207 wxTextCtrl& operator<<(double d);
208 wxTextCtrl& operator<<(const wxChar c);
209
210 // obsolete functions
211 #if WXWIN_COMPATIBILITY
212 bool Modified() const { return IsModified(); }
213 #endif
214
215 protected:
216 // the name of the last file loaded with LoadFile() which will be used by
217 // SaveFile() by default
218 wxString m_filename;
219
220 // the text style which will be used for any new text added to the control
221 wxTextAttr m_defaultStyle;
222
223 private:
224 #ifndef NO_TEXT_WINDOW_STREAM
225 #if !wxUSE_IOSTREAMH
226 char *m_streambuf;
227 #endif
228 #endif
229 };
230
231 // ----------------------------------------------------------------------------
232 // include the platform-dependent class definition
233 // ----------------------------------------------------------------------------
234
235 #if defined(__WXMSW__)
236 #include "wx/msw/textctrl.h"
237 #elif defined(__WXMOTIF__)
238 #include "wx/motif/textctrl.h"
239 #elif defined(__WXGTK__)
240 #include "wx/gtk/textctrl.h"
241 #elif defined(__WXQT__)
242 #include "wx/qt/textctrl.h"
243 #elif defined(__WXMAC__)
244 #include "wx/mac/textctrl.h"
245 #elif defined(__WXPM__)
246 #include "wx/os2/textctrl.h"
247 #elif defined(__WXSTUBS__)
248 #include "wx/stubs/textctrl.h"
249 #endif
250
251 #endif
252 // _WX_TEXTCTRL_H_BASE_