Fixed memory leak in textcmn.cpp
[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 #ifdef __GNUG__
16 #pragma implementation "textctrlbase.h"
17 #endif
18
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #ifdef __BORLANDC__
23 #pragma hdrstop
24 #endif
25
26 #ifndef WX_PRECOMP
27 #include "wx/intl.h"
28 #include "wx/log.h"
29 #include "wx/textctrl.h"
30 #endif // WX_PRECOMP
31
32 #include "wx/ffile.h"
33
34 // ----------------------------------------------------------------------------
35 // macros
36 // ----------------------------------------------------------------------------
37
38 // we don't have any objects of type wxTextCtrlBase in the program, only
39 // wxTextCtrl, so this cast is safe
40 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
41
42 // ============================================================================
43 // implementation
44 // ============================================================================
45
46 // ----------------------------------------------------------------------------
47 // ctor
48 // ----------------------------------------------------------------------------
49
50 wxTextCtrlBase::wxTextCtrlBase()
51 {
52 #ifndef NO_TEXT_WINDOW_STREAM
53 #if wxUSE_IOSTREAMH
54 if (allocate())
55 setp(base(),ebuf());
56 #else
57 m_streambuf=new char[64];
58 setp(m_streambuf,m_streambuf+64);
59 #endif //wxUSE_IOSTREAMH
60 #endif // NO_TEXT_WINDOW_STREAM
61 }
62
63 wxTextCtrlBase::~wxTextCtrlBase()
64 {
65 #ifndef NO_TEXT_WINDOW_STREAM
66 #if !wxUSE_IOSTREAMH
67 delete[] m_streambuf;
68 #endif
69 #endif
70 }
71
72 // ----------------------------------------------------------------------------
73 // style functions - not implemented here
74 // ----------------------------------------------------------------------------
75
76 // apply styling to text range
77 bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
78 const wxTextAttr& WXUNUSED(style))
79 {
80 // to be implemented in derived TextCtrl classes
81 return FALSE;
82 }
83
84 // change default text attributes
85 bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr &style)
86 {
87 m_defaultStyle = style;
88 return TRUE;
89 }
90
91 // get default text attributes
92 const wxTextAttr& wxTextCtrlBase::GetDefaultStyle() const
93 {
94 return m_defaultStyle;
95 }
96
97 // ----------------------------------------------------------------------------
98 // file IO functions
99 // ----------------------------------------------------------------------------
100
101 bool wxTextCtrlBase::LoadFile(const wxString& filename)
102 {
103 wxFFile file(filename);
104 if ( file.IsOpened() )
105 {
106 wxString text;
107 if ( file.ReadAll(&text) )
108 {
109 SetValue(text);
110
111 DiscardEdits();
112
113 m_filename = filename;
114
115 return TRUE;
116 }
117 }
118
119 wxLogError(_("File couldn't be loaded."));
120
121 return FALSE;
122 }
123
124 bool wxTextCtrlBase::SaveFile(const wxString& filename)
125 {
126 wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
127 if ( !filenameToUse )
128 {
129 // what kind of message to give? is it an error or a program bug?
130 wxLogDebug(wxT("Can't save textctrl to file without filename."));
131
132 return FALSE;
133 }
134
135 wxFFile file(filename, "w");
136 if ( file.IsOpened() && file.Write(GetValue()) )
137 {
138 // it's not modified any longer
139 DiscardEdits();
140
141 m_filename = filename;
142
143 return TRUE;
144 }
145
146 wxLogError(_("The text couldn't be saved."));
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 WXUNUSED(c) )
205 {
206 int len = pptr() - pbase();
207 char *txt = new char[len+1];
208 strncpy(txt, pbase(), len);
209 txt[len] = '\0';
210 (*this) << txt;
211 setp(pbase(), epptr());
212 delete[] txt;
213 return EOF;
214 }
215
216 int wxTextCtrlBase::sync()
217 {
218 int len = pptr() - pbase();
219 char *txt = new char[len+1];
220 strncpy(txt, pbase(), len);
221 txt[len] = '\0';
222 (*this) << txt;
223 setp(pbase(), epptr());
224 delete[] txt;
225 return 0;
226 }
227
228 int wxTextCtrlBase::underflow()
229 {
230 return EOF;
231 }
232
233 #endif // NO_TEXT_WINDOW_STREAM
234