]> git.saurik.com Git - wxWidgets.git/blob - src/common/textcmn.cpp
Fixed the drawing of the HRules so they don't try to iterate over all
[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
55 // ----------------------------------------------------------------------------
56 // ctor
57 // ----------------------------------------------------------------------------
58
59 wxTextCtrlBase::wxTextCtrlBase()
60 {
61 #ifndef NO_TEXT_WINDOW_STREAM
62 #if wxUSE_IOSTREAMH
63 if (allocate())
64 setp(base(),ebuf());
65 #else
66 m_streambuf = new char[64];
67 setp(m_streambuf, m_streambuf + 64);
68 #endif //wxUSE_IOSTREAMH
69 #endif // NO_TEXT_WINDOW_STREAM
70 }
71
72 wxTextCtrlBase::~wxTextCtrlBase()
73 {
74 #ifndef NO_TEXT_WINDOW_STREAM
75 #if !wxUSE_IOSTREAMH
76 delete[] m_streambuf;
77 #endif
78 #endif
79 }
80
81 // ----------------------------------------------------------------------------
82 // style functions - not implemented here
83 // ----------------------------------------------------------------------------
84
85 // apply styling to text range
86 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
87 const wxTextAttr& WXUNUSED(style))
88 {
89 // to be implemented in derived TextCtrl classes
90 return FALSE;
91 }
92
93 // change default text attributes
94 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr &style)
95 {
96 m_defaultStyle = style;
97 return TRUE;
98 }
99
100 // get default text attributes
101 const wxTextAttr& wxTextCtrlBase::GetDefaultStyle() const
102 {
103 return m_defaultStyle;
104 }
105
106 // ----------------------------------------------------------------------------
107 // file IO functions
108 // ----------------------------------------------------------------------------
109
110 bool wxTextCtrlBase::LoadFile(const wxString& filename)
111 {
112 #if wxUSE_FFILE
113 wxFFile file(filename);
114 if ( file.IsOpened() )
115 {
116 wxString text;
117 if ( file.ReadAll(&text) )
118 {
119 SetValue(text);
120
121 DiscardEdits();
122
123 m_filename = filename;
124
125 return TRUE;
126 }
127 }
128
129 wxLogError(_("File couldn't be loaded."));
130 #endif // wxUSE_FFILE
131
132 return FALSE;
133 }
134
135 bool wxTextCtrlBase::SaveFile(const wxString& filename)
136 {
137 wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
138 if ( !filenameToUse )
139 {
140 // what kind of message to give? is it an error or a program bug?
141 wxLogDebug(wxT("Can't save textctrl to file without filename."));
142
143 return FALSE;
144 }
145
146 #if wxUSE_FFILE
147 wxFFile file(filename, "w");
148 if ( file.IsOpened() && file.Write(GetValue()) )
149 {
150 // it's not modified any longer
151 DiscardEdits();
152
153 m_filename = filename;
154
155 return TRUE;
156 }
157
158 wxLogError(_("The text couldn't be saved."));
159 #endif // wxUSE_FFILE
160
161 return FALSE;
162 }
163
164 // ----------------------------------------------------------------------------
165 // stream-like insertion operator
166 // ----------------------------------------------------------------------------
167
168 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
169 {
170 AppendText(s);
171 return *TEXTCTRL(this);
172 }
173
174 wxTextCtrl& wxTextCtrlBase::operator<<(float f)
175 {
176 wxString str;
177 str.Printf(wxT("%.2f"), f);
178 AppendText(str);
179 return *TEXTCTRL(this);
180 }
181
182 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
183 {
184 wxString str;
185 str.Printf(wxT("%.2f"), d);
186 AppendText(str);
187 return *TEXTCTRL(this);
188 }
189
190 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
191 {
192 wxString str;
193 str.Printf(wxT("%d"), i);
194 AppendText(str);
195 return *TEXTCTRL(this);
196 }
197
198 wxTextCtrl& wxTextCtrlBase::operator<<(long i)
199 {
200 wxString str;
201 str.Printf(wxT("%ld"), i);
202 AppendText(str);
203 return *TEXTCTRL(this);
204 }
205
206 wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
207 {
208 return operator<<(wxString(c));
209 }
210
211 // ----------------------------------------------------------------------------
212 // streambuf methods implementation
213 // ----------------------------------------------------------------------------
214
215 #ifndef NO_TEXT_WINDOW_STREAM
216
217 int wxTextCtrlBase::overflow( int WXUNUSED(c) )
218 {
219 int len = pptr() - pbase();
220 char *txt = new char[len+1];
221 strncpy(txt, pbase(), len);
222 txt[len] = '\0';
223 (*this) << txt;
224 setp(pbase(), epptr());
225 delete[] txt;
226 return EOF;
227 }
228
229 int wxTextCtrlBase::sync()
230 {
231 int len = pptr() - pbase();
232 char *txt = new char[len+1];
233 strncpy(txt, pbase(), len);
234 txt[len] = '\0';
235 (*this) << txt;
236 setp(pbase(), epptr());
237 delete[] txt;
238 return 0;
239 }
240
241 int wxTextCtrlBase::underflow()
242 {
243 return EOF;
244 }
245
246 #endif // NO_TEXT_WINDOW_STREAM
247
248 // ----------------------------------------------------------------------------
249 // clipboard stuff
250 // ----------------------------------------------------------------------------
251
252 bool wxTextCtrlBase::CanCopy() const
253 {
254 // can copy if there's a selection
255 long from, to;
256 GetSelection(&from, &to);
257 return from != to;
258 }
259
260 bool wxTextCtrlBase::CanCut() const
261 {
262 // can cut if there's a selection and if we're not read only
263 return CanCopy() && IsEditable();
264 }
265
266 bool wxTextCtrlBase::CanPaste() const
267 {
268 // can paste if we are not read only
269 return IsEditable();
270 }
271
272 // ----------------------------------------------------------------------------
273 // misc
274 // ----------------------------------------------------------------------------
275
276 void wxTextCtrlBase::SelectAll()
277 {
278 SetSelection(0, GetLastPosition());
279 }
280
281 #else // !wxUSE_TEXTCTRL
282
283 // define this one even if !wxUSE_TEXTCTRL because it is also used by other
284 // controls (wxComboBox and wxSpinCtrl)
285 #include "wx/event.h"
286
287 DEFINE_EVENT_TYPE(wxEVT_COMMAND_TEXT_UPDATED)
288
289 #endif // wxUSE_TEXTCTRL/!wxUSE_TEXTCTRL
290