]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/common/textcmn.cpp
merged MapBitmap() from 2.2 branch
[wxWidgets.git] / src / common / textcmn.cpp
... / ...
CommitLineData
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#ifdef __GNUG__
17 #pragma implementation "textctrlbase.h"
18#endif
19
20// for compilers that support precompilation, includes "wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
27#if wxUSE_TEXTCTRL
28
29#ifndef WX_PRECOMP
30 #include "wx/intl.h"
31 #include "wx/log.h"
32 #include "wx/textctrl.h"
33#endif // WX_PRECOMP
34
35#include "wx/ffile.h"
36
37// ----------------------------------------------------------------------------
38// macros
39// ----------------------------------------------------------------------------
40
41// we don't have any objects of type wxTextCtrlBase in the program, only
42// wxTextCtrl, so this cast is safe
43#define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
44
45// ============================================================================
46// implementation
47// ============================================================================
48
49// ----------------------------------------------------------------------------
50// ctor
51// ----------------------------------------------------------------------------
52
53wxTextCtrlBase::wxTextCtrlBase()
54{
55#ifndef NO_TEXT_WINDOW_STREAM
56 #if wxUSE_IOSTREAMH
57 if (allocate())
58 setp(base(),ebuf());
59 #else
60 m_streambuf = new char[64];
61 setp(m_streambuf, m_streambuf + 64);
62 #endif //wxUSE_IOSTREAMH
63#endif // NO_TEXT_WINDOW_STREAM
64}
65
66wxTextCtrlBase::~wxTextCtrlBase()
67{
68#ifndef NO_TEXT_WINDOW_STREAM
69#if !wxUSE_IOSTREAMH
70 delete[] m_streambuf;
71#endif
72#endif
73}
74
75// ----------------------------------------------------------------------------
76// style functions - not implemented here
77// ----------------------------------------------------------------------------
78
79// apply styling to text range
80bool wxTextCtrlBase::SetStyle(long WXUNUSED(start), long WXUNUSED(end),
81 const wxTextAttr& WXUNUSED(style))
82{
83 // to be implemented in derived TextCtrl classes
84 return FALSE;
85}
86
87// change default text attributes
88bool wxTextCtrlBase::SetDefaultStyle(const wxTextAttr &style)
89{
90 m_defaultStyle = style;
91 return TRUE;
92}
93
94// get default text attributes
95const wxTextAttr& wxTextCtrlBase::GetDefaultStyle() const
96{
97 return m_defaultStyle;
98}
99
100// ----------------------------------------------------------------------------
101// file IO functions
102// ----------------------------------------------------------------------------
103
104bool wxTextCtrlBase::LoadFile(const wxString& filename)
105{
106#if wxUSE_FFILE
107 wxFFile file(filename);
108 if ( file.IsOpened() )
109 {
110 wxString text;
111 if ( file.ReadAll(&text) )
112 {
113 SetValue(text);
114
115 DiscardEdits();
116
117 m_filename = filename;
118
119 return TRUE;
120 }
121 }
122
123 wxLogError(_("File couldn't be loaded."));
124#endif // wxUSE_FFILE
125
126 return FALSE;
127}
128
129bool wxTextCtrlBase::SaveFile(const wxString& filename)
130{
131 wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
132 if ( !filenameToUse )
133 {
134 // what kind of message to give? is it an error or a program bug?
135 wxLogDebug(wxT("Can't save textctrl to file without filename."));
136
137 return FALSE;
138 }
139
140#if wxUSE_FFILE
141 wxFFile file(filename, "w");
142 if ( file.IsOpened() && file.Write(GetValue()) )
143 {
144 // it's not modified any longer
145 DiscardEdits();
146
147 m_filename = filename;
148
149 return TRUE;
150 }
151
152 wxLogError(_("The text couldn't be saved."));
153#endif // wxUSE_FFILE
154
155 return FALSE;
156}
157
158// ----------------------------------------------------------------------------
159// stream-like insertion operator
160// ----------------------------------------------------------------------------
161
162wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
163{
164 AppendText(s);
165 return *TEXTCTRL(this);
166}
167
168wxTextCtrl& wxTextCtrlBase::operator<<(float f)
169{
170 wxString str;
171 str.Printf(wxT("%.2f"), f);
172 AppendText(str);
173 return *TEXTCTRL(this);
174}
175
176wxTextCtrl& wxTextCtrlBase::operator<<(double d)
177{
178 wxString str;
179 str.Printf(wxT("%.2f"), d);
180 AppendText(str);
181 return *TEXTCTRL(this);
182}
183
184wxTextCtrl& wxTextCtrlBase::operator<<(int i)
185{
186 wxString str;
187 str.Printf(wxT("%d"), i);
188 AppendText(str);
189 return *TEXTCTRL(this);
190}
191
192wxTextCtrl& wxTextCtrlBase::operator<<(long i)
193{
194 wxString str;
195 str.Printf(wxT("%ld"), i);
196 AppendText(str);
197 return *TEXTCTRL(this);
198}
199
200wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
201{
202 return operator<<(wxString(c));
203}
204
205// ----------------------------------------------------------------------------
206// streambuf methods implementation
207// ----------------------------------------------------------------------------
208
209#ifndef NO_TEXT_WINDOW_STREAM
210
211int wxTextCtrlBase::overflow( int WXUNUSED(c) )
212{
213 int len = pptr() - pbase();
214 char *txt = new char[len+1];
215 strncpy(txt, pbase(), len);
216 txt[len] = '\0';
217 (*this) << txt;
218 setp(pbase(), epptr());
219 delete[] txt;
220 return EOF;
221}
222
223int wxTextCtrlBase::sync()
224{
225 int len = pptr() - pbase();
226 char *txt = new char[len+1];
227 strncpy(txt, pbase(), len);
228 txt[len] = '\0';
229 (*this) << txt;
230 setp(pbase(), epptr());
231 delete[] txt;
232 return 0;
233}
234
235int wxTextCtrlBase::underflow()
236{
237 return EOF;
238}
239
240#endif // NO_TEXT_WINDOW_STREAM
241
242// ----------------------------------------------------------------------------
243// clipboard stuff
244// ----------------------------------------------------------------------------
245
246bool wxTextCtrlBase::CanCopy() const
247{
248 // can copy if there's a selection
249 long from, to;
250 GetSelection(&from, &to);
251 return from != to;
252}
253
254bool wxTextCtrlBase::CanCut() const
255{
256 // can cut if there's a selection and if we're not read only
257 return CanCopy() && IsEditable();
258}
259
260bool wxTextCtrlBase::CanPaste() const
261{
262 // can paste if we are not read only
263 return IsEditable();
264}
265
266// ----------------------------------------------------------------------------
267// misc
268// ----------------------------------------------------------------------------
269
270void wxTextCtrlBase::SelectAll()
271{
272 SetSelection(0, GetLastPosition());
273}
274
275#endif // wxUSE_TEXTCTRL
276