Add Shivar's patch for using iostream
[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 #if !wxUSE_IOSTREAMH
52 #ifndef NO_TEXT_WINDOW_STREAM
53 :streambuf()
54 #endif //NO_TEXT_WINDOW_STREAM
55 #endif //!wxUSE_IOSTREAMH
56 {
57 #if wxUSE_IOSTREAMH
58 #ifndef NO_TEXT_WINDOW_STREAM
59 if (allocate())
60 setp(base(),ebuf());
61 #endif // NO_TEXT_WINDOW_STREAM
62 #endif //wxUSE_IOSTREAMH
63 }
64
65 // ----------------------------------------------------------------------------
66 // file IO functions
67 // ----------------------------------------------------------------------------
68
69 bool wxTextCtrlBase::LoadFile(const wxString& filename)
70 {
71 wxFFile file(filename);
72 if ( file.IsOpened() )
73 {
74 wxString text;
75 if ( file.ReadAll(&text) )
76 {
77 SetValue(text);
78
79 DiscardEdits();
80
81 m_filename = filename;
82
83 return TRUE;
84 }
85 }
86
87 wxLogError(_("File couldn't be loaded."));
88
89 return FALSE;
90 }
91
92 bool wxTextCtrlBase::SaveFile(const wxString& filename)
93 {
94 wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
95 if ( !filenameToUse )
96 {
97 // what kind of message to give? is it an error or a program bug?
98 wxLogDebug(wxT("Can't save textctrl to file without filename."));
99
100 return FALSE;
101 }
102
103 wxFFile file(filename, "w");
104 if ( file.IsOpened() && file.Write(GetValue()) )
105 {
106 // it's not modified any longer
107 DiscardEdits();
108
109 m_filename = filename;
110
111 return TRUE;
112 }
113
114 wxLogError(_("The text couldn't be saved."));
115
116 return FALSE;
117 }
118
119 // ----------------------------------------------------------------------------
120 // stream-like insertion operator
121 // ----------------------------------------------------------------------------
122
123 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
124 {
125 AppendText(s);
126 return *TEXTCTRL(this);
127 }
128
129 wxTextCtrl& wxTextCtrlBase::operator<<(float f)
130 {
131 wxString str;
132 str.Printf(wxT("%.2f"), f);
133 AppendText(str);
134 return *TEXTCTRL(this);
135 }
136
137 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
138 {
139 wxString str;
140 str.Printf(wxT("%.2f"), d);
141 AppendText(str);
142 return *TEXTCTRL(this);
143 }
144
145 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
146 {
147 wxString str;
148 str.Printf(wxT("%d"), i);
149 AppendText(str);
150 return *TEXTCTRL(this);
151 }
152
153 wxTextCtrl& wxTextCtrlBase::operator<<(long i)
154 {
155 wxString str;
156 str.Printf(wxT("%ld"), i);
157 AppendText(str);
158 return *TEXTCTRL(this);
159 }
160
161 wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
162 {
163 return operator<<(wxString(c));
164 }
165
166 // ----------------------------------------------------------------------------
167 // streambuf methods implementation
168 // ----------------------------------------------------------------------------
169
170 #ifndef NO_TEXT_WINDOW_STREAM
171
172 int wxTextCtrlBase::overflow( int WXUNUSED(c) )
173 {
174 int len = pptr() - pbase();
175 char *txt = new char[len+1];
176 strncpy(txt, pbase(), len);
177 txt[len] = '\0';
178 (*this) << txt;
179 setp(pbase(), epptr());
180 delete[] txt;
181 return EOF;
182 }
183
184 int wxTextCtrlBase::sync()
185 {
186 int len = pptr() - pbase();
187 char *txt = new char[len+1];
188 strncpy(txt, pbase(), len);
189 txt[len] = '\0';
190 (*this) << txt;
191 setp(pbase(), epptr());
192 delete[] txt;
193 return 0;
194 }
195
196 int wxTextCtrlBase::underflow()
197 {
198 return EOF;
199 }
200
201 #endif // NO_TEXT_WINDOW_STREAM
202