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