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