Added GSocket for Unix (only GTK for the moment)
[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
16 // for compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
18
19 #ifdef __BORLANDC__
20 #pragma hdrstop
21 #endif
22
23 #ifndef WX_PRECOMP
24 #include "wx/intl.h"
25 #include "wx/log.h"
26 #include "wx/textctrl.h"
27 #endif // WX_PRECOMP
28
29 #include "wx/ffile.h"
30
31 // ----------------------------------------------------------------------------
32 // macros
33 // ----------------------------------------------------------------------------
34
35 // we don't have any objects of type wxTextCtrlBase in the program, only
36 // wxTextCtrl, so this cast is safe
37 #define TEXTCTRL(ptr) ((wxTextCtrl *)(ptr))
38
39 // ============================================================================
40 // implementation
41 // ============================================================================
42
43 // ----------------------------------------------------------------------------
44 // ctor
45 // ----------------------------------------------------------------------------
46
47 wxTextCtrlBase::wxTextCtrlBase()
48 {
49 #ifndef NO_TEXT_WINDOW_STREAM
50 if (allocate())
51 setp(base(),ebuf());
52 #endif // NO_TEXT_WINDOW_STREAM
53 }
54
55 // ----------------------------------------------------------------------------
56 // file IO functions
57 // ----------------------------------------------------------------------------
58
59 bool wxTextCtrlBase::LoadFile(const wxString& filename)
60 {
61 wxFFile file(filename);
62 if ( file.IsOpened() )
63 {
64 wxString text;
65 if ( file.ReadAll(&text) )
66 {
67 SetValue(text);
68
69 DiscardEdits();
70
71 m_filename = filename;
72
73 return TRUE;
74 }
75 }
76
77 wxLogError(_("File couldn't be loaded."));
78
79 return FALSE;
80 }
81
82 bool wxTextCtrlBase::SaveFile(const wxString& filename)
83 {
84 wxString filenameToUse = filename.IsEmpty() ? m_filename : filename;
85 if ( !filenameToUse )
86 {
87 // what kind of message to give? is it an error or a program bug?
88 wxLogDebug(_T("Can't save textctrl to file without filename."));
89
90 return FALSE;
91 }
92
93 wxFFile file(filename, "w");
94 if ( file.IsOpened() && file.Write(GetValue()) )
95 {
96 // it's not modified any longer
97 DiscardEdits();
98
99 m_filename = filename;
100
101 return TRUE;
102 }
103
104 wxLogError(_("The text couldn't be saved."));
105
106 return FALSE;
107 }
108
109 // ----------------------------------------------------------------------------
110 // stream-like insertion operator
111 // ----------------------------------------------------------------------------
112
113 wxTextCtrl& wxTextCtrlBase::operator<<(const wxString& s)
114 {
115 AppendText(s);
116 return *TEXTCTRL(this);
117 }
118
119 wxTextCtrl& wxTextCtrlBase::operator<<(float f)
120 {
121 wxString str;
122 str.Printf(_T("%.2f"), f);
123 AppendText(str);
124 return *TEXTCTRL(this);
125 }
126
127 wxTextCtrl& wxTextCtrlBase::operator<<(double d)
128 {
129 wxString str;
130 str.Printf(_T("%.2f"), d);
131 AppendText(str);
132 return *TEXTCTRL(this);
133 }
134
135 wxTextCtrl& wxTextCtrlBase::operator<<(int i)
136 {
137 wxString str;
138 str.Printf(_T("%d"), i);
139 AppendText(str);
140 return *TEXTCTRL(this);
141 }
142
143 wxTextCtrl& wxTextCtrlBase::operator<<(long i)
144 {
145 wxString str;
146 str.Printf(_T("%ld"), i);
147 AppendText(str);
148 return *TEXTCTRL(this);
149 }
150
151 wxTextCtrl& wxTextCtrlBase::operator<<(const wxChar c)
152 {
153 return operator<<(wxString(c));
154 }
155
156 // ----------------------------------------------------------------------------
157 // streambuf methods implementation
158 // ----------------------------------------------------------------------------
159
160 #ifndef NO_TEXT_WINDOW_STREAM
161
162 int wxTextCtrlBase::overflow( int WXUNUSED(c) )
163 {
164 int len = pptr() - pbase();
165 char *txt = new char[len+1];
166 strncpy(txt, pbase(), len);
167 txt[len] = '\0';
168 (*this) << txt;
169 setp(pbase(), epptr());
170 delete[] txt;
171 return EOF;
172 }
173
174 int wxTextCtrlBase::sync()
175 {
176 int len = pptr() - pbase();
177 char *txt = new char[len+1];
178 strncpy(txt, pbase(), len);
179 txt[len] = '\0';
180 (*this) << txt;
181 setp(pbase(), epptr());
182 delete[] txt;
183 return 0;
184 }
185
186 int wxTextCtrlBase::underflow()
187 {
188 return EOF;
189 }
190
191 #endif // NO_TEXT_WINDOW_STREAM
192