]> git.saurik.com Git - wxWidgets.git/blob - src/common/textcmn.cpp
applied patch 475879 (Can't use subclassed GridCellEditor)
[wxWidgets.git] / src / common / textcmn.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: common/textcmn.cpp
3 // Purpose: implementation of platform-independent functions of wxTextCtrl
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 13.07.99
7 // RCS-ID: $Id$
8 // Copyright: (c) wxWindows team
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 #ifdef __GNUG__
17 #pragma implementation "textctrlbase.h"
18 #endif
19
20 // for compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_TEXTCTRL
28
29 #ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #include "wx/textctrl.h"
33 #endif // WX_PRECOMP
34
35 #include "wx/ffile.h"
36
37 // ----------------------------------------------------------------------------
38 // macros
39 // ----------------------------------------------------------------------------
40
41 // we don't have any objects of type wxTextCtrlBase in the program, only
42 // wxTextCtrl, so this cast is safe
43 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
44
45 // ============================================================================
46 // implementation
47 // ============================================================================
48
49 IMPLEMENT_DYNAMIC_CLASS(wxTextUrlEvent, wxCommandEvent)
50
51 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
52 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_ENTER)
53 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_URL)
54 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_MAXLEN)
55
56 // ----------------------------------------------------------------------------
57 // ctor
58 // ----------------------------------------------------------------------------
59
60 wxTextCtrlBase::wxTextCtrlBase()
61 {
62 }
63
64 wxTextCtrlBase::~wxTextCtrlBase()
65 {
66 }
67
68 // ----------------------------------------------------------------------------
69 // style functions - not implemented here
70 // ----------------------------------------------------------------------------
71
72 // apply styling to text range
73 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
74 const wxTextAttr& WXUNUSED(style))
75 {
76 // to be implemented in derived TextCtrl classes
77 return FALSE;
78 }
79
80 // change default text attributes
81 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr &style)
82 {
83 m_defaultStyle = style;
84 return TRUE;
85 }
86
87 // get default text attributes
88 const wxTextAttr& wxTextCtrlBase::GetDefaultStyle() const
89 {
90 return m_defaultStyle;
91 }
92
93 // ----------------------------------------------------------------------------
94 // file IO functions
95 // ----------------------------------------------------------------------------
96
97 bool wxTextCtrlBase::LoadFile(const wxString& filename)
98 {
99 #if wxUSE_FFILE
100 wxFFile file(filename);
101 if ( file.IsOpened() )
102 {
103 wxString text;
104 if ( file.ReadAll(&text) )
105 {
106 SetValue(text);
107
108 DiscardEdits();
109
110 m_filename = filename;
111
112 return TRUE;
113 }
114 }
115
116 wxLogError(_("File couldn't be loaded."));
117 #endif // wxUSE_FFILE
118
119 return FALSE;
120 }
121
122 bool wxTextCtrlBase::SaveFile(const wxString& filename)
123 {
124 wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
125 if ( !filenameToUse )
126 {
127 // what kind of message to give? is it an error or a program bug?
128 wxLogDebug(wxT("Can't save textctrl to file without filename."));
129
130 return FALSE;
131 }
132
133 #if wxUSE_FFILE
134 wxFFile file(filename, "w");
135 if ( file.IsOpened() && file.Write(GetValue()) )
136 {
137 // it's not modified any longer
138 DiscardEdits();
139
140 m_filename = filename;
141
142 return TRUE;
143 }
144
145 wxLogError(_("The text couldn't be saved."));
146 #endif // wxUSE_FFILE
147
148 return FALSE;
149 }
150
151 // ----------------------------------------------------------------------------
152 // stream-like insertion operator
153 // ----------------------------------------------------------------------------
154
155 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
156 {
157 AppendText(s);
158 return *TEXTCTRL(this);
159 }
160
161 wxTextCtrl& wxTextCtrlBase::operator<<(float f)
162 {
163 wxString str;
164 str.Printf(wxT("%.2f"), f);
165 AppendText(str);
166 return *TEXTCTRL(this);
167 }
168
169 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
170 {
171 wxString str;
172 str.Printf(wxT("%.2f"), d);
173 AppendText(str);
174 return *TEXTCTRL(this);
175 }
176
177 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
178 {
179 wxString str;
180 str.Printf(wxT("%d"), i);
181 AppendText(str);
182 return *TEXTCTRL(this);
183 }
184
185 wxTextCtrl& wxTextCtrlBase::operator<<(long i)
186 {
187 wxString str;
188 str.Printf(wxT("%ld"), i);
189 AppendText(str);
190 return *TEXTCTRL(this);
191 }
192
193 wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
194 {
195 return operator<<(wxString(c));
196 }
197
198 // ----------------------------------------------------------------------------
199 // streambuf methods implementation
200 // ----------------------------------------------------------------------------
201
202 #ifndef NO_TEXT_WINDOW_STREAM
203
204 int wxTextCtrlBase::overflow(int c)
205 {
206 AppendText((wxChar)c);
207
208 // return something different from EOF
209 return 0;
210 }
211
212 #endif // NO_TEXT_WINDOW_STREAM
213
214 // ----------------------------------------------------------------------------
215 // clipboard stuff
216 // ----------------------------------------------------------------------------
217
218 bool wxTextCtrlBase::CanCopy() const
219 {
220 // can copy if there's a selection
221 long from, to;
222 GetSelection(&from, &to);
223 return from != to;
224 }
225
226 bool wxTextCtrlBase::CanCut() const
227 {
228 // can cut if there's a selection and if we're not read only
229 return CanCopy() && IsEditable();
230 }
231
232 bool wxTextCtrlBase::CanPaste() const
233 {
234 // can paste if we are not read only
235 return IsEditable();
236 }
237
238 // ----------------------------------------------------------------------------
239 // misc
240 // ----------------------------------------------------------------------------
241
242 void wxTextCtrlBase::SelectAll()
243 {
244 SetSelection(0, GetLastPosition());
245 }
246
247 wxString wxTextCtrlBase::GetStringSelection() const
248 {
249 long from, to;
250 GetSelection(&from, &to);
251
252 wxString sel;
253 if ( from < to )
254 {
255 sel = GetValue().Mid(from, to - from);
256 }
257
258 return sel;
259 }
260
261 #else // !wxUSE_TEXTCTRL
262
263 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
264 // controls (wxComboBox and wxSpinCtrl)
265 #include "wx/event.h"
266
267 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
268
269 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL
270