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