1. new wxFFile class - as wxFile but uses fopen/fread/fseek... instead of
[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 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/textctrl.h"
25 #endif // WX_PRECOMP
26
27 #include "wx/ffile.h"
28
29 // ----------------------------------------------------------------------------
30 // macros
31 // ----------------------------------------------------------------------------
32
33 // we don't have any objects of type wxTextCtrlBase in the program, only
34 // wxTextCtrl, so this cast is safe
35 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
36
37 // ============================================================================
38 // implementation
39 // ============================================================================
40
41 // ----------------------------------------------------------------------------
42 // ctor
43 // ----------------------------------------------------------------------------
44
45 wxTextCtrlBase::wxTextCtrlBase()
46 {
47 }
48
49 // ----------------------------------------------------------------------------
50 // file IO functions
51 // ----------------------------------------------------------------------------
52
53 bool wxTextCtrlBase::LoadFile(const wxString& filename)
54 {
55 wxFFile file(filename);
56 if ( file.IsOpened() )
57 {
58 wxString text;
59 if ( file.ReadAll(&text) )
60 {
61 SetValue(text);
62
63 DiscardEdits();
64
65 m_filename = filename;
66
67 return TRUE;
68 }
69 }
70
71 wxLogError(_("File couldn't be loaded."));
72
73 return FALSE;
74 }
75
76 bool wxTextCtrlBase::SaveFile(const wxString& filename)
77 {
78 wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
79 if ( !filenameToUse )
80 {
81 // what kind of message to give? is it an error or a program bug?
82 wxLogDebug(_T("Can't save textctrl to file without filename."));
83
84 return FALSE;
85 }
86
87 wxFFile file(filename, "w");
88 if ( file.IsOpened() && file.Write(GetValue()) )
89 {
90 // it's not modified any longer
91 DiscardEdits();
92
93 m_filename = filename;
94
95 return TRUE;
96 }
97
98 wxLogError(_("The text couldn't be saved."));
99
100 return FALSE;
101 }
102
103 // ----------------------------------------------------------------------------
104 // stream-like insertion operator
105 // ----------------------------------------------------------------------------
106
107 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
108 {
109 AppendText(s);
110 return *TEXTCTRL(this);
111 }
112
113 wxTextCtrl& wxTextCtrlBase::operator<<(float f)
114 {
115 wxString str;
116 str.Printf(_T("%.2f"), f);
117 AppendText(str);
118 return *TEXTCTRL(this);
119 }
120
121 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
122 {
123 wxString str;
124 str.Printf(_T("%.2f"), d);
125 AppendText(str);
126 return *TEXTCTRL(this);
127 }
128
129 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
130 {
131 wxString str;
132 str.Printf(_T("%d"), i);
133 AppendText(str);
134 return *TEXTCTRL(this);
135 }
136
137 wxTextCtrl& wxTextCtrlBase::operator<<(long i)
138 {
139 wxString str;
140 str.Printf(_T("%ld"), i);
141 AppendText(str);
142 return *TEXTCTRL(this);
143 }
144
145 wxTextCtrl& wxTextCtrlBase::operator<<(const char c)
146 {
147 return operator<<(wxString(c));
148 }
149
150 // ----------------------------------------------------------------------------
151 // streambuf methods implementation
152 // ----------------------------------------------------------------------------
153
154 #ifndef NO_TEXT_WINDOW_STREAM
155
156 int wxTextCtrlBase::overflow( int WXUNUSED(c) )
157 {
158 int len = pptr() - pbase();
159 char *txt = new char[len+1];
160 strncpy(txt, pbase(), len);
161 txt[len] = '\0';
162 (*this) << txt;
163 setp(pbase(), epptr());
164 delete[] txt;
165 return EOF;
166 }
167
168 int wxTextCtrlBase::sync()
169 {
170 int len = pptr() - pbase();
171 char *txt = new char[len+1];
172 strncpy(txt, pbase(), len);
173 txt[len] = '\0';
174 (*this) << txt;
175 setp(pbase(), epptr());
176 delete[] txt;
177 return 0;
178 }
179
180 int wxTextCtrlBase::underflow()
181 {
182 return EOF;
183 }
184
185 #endif // NO_TEXT_WINDOW_STREAM
186