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