]> git.saurik.com Git - wxWidgets.git/blob - src/qt/textctrl.cpp
added wxTextCtrl::AppendText, used by TextCtrl operator <<
[wxWidgets.git] / src / qt / textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: textctrl.cpp
3 // Purpose: wxTextCtrl
4 // Author: AUTHOR
5 // Modified by:
6 // Created: ??/??/98
7 // RCS-ID: $Id$
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifdef __GNUG__
13 #pragma implementation "textctrl.h"
14 #endif
15
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <fstream.h>
19
20 #include "wx/textctrl.h"
21 #include "wx/settings.h"
22
23 #if defined(__BORLANDC__) && !defined(__WIN32__)
24 #include <alloc.h>
25 #else
26 #ifndef __GNUWIN32__
27 #include <malloc.h>
28 #endif
29 #endif
30
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
33
34 BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
35 EVT_CHAR(wxTextCtrl::OnChar)
36 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
37 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
38 END_EVENT_TABLE()
39 #endif
40
41 // Text item
42 wxTextCtrl::wxTextCtrl()
43 #ifndef NO_TEXT_WINDOW_STREAM
44 :streambuf()
45 #endif
46 {
47 m_fileName = "";
48 }
49
50 bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
51 const wxString& value,
52 const wxPoint& pos,
53 const wxSize& size, long style,
54 const wxValidator& validator,
55 const wxString& name)
56 {
57 m_fileName = "";
58 SetName(name);
59 SetValidator(validator);
60 if (parent) parent->AddChild(this);
61
62 m_windowStyle = style;
63
64 if ( id == -1 )
65 m_windowId = (int)NewControlId();
66 else
67 m_windowId = id;
68
69 return TRUE;
70 }
71
72 wxString wxTextCtrl::GetValue() const
73 {
74 // TODO
75 return wxString("");
76 }
77
78 void wxTextCtrl::SetValue(const wxString& value)
79 {
80 // TODO
81 }
82
83 void wxTextCtrl::SetSize(int x, int y, int width, int height, int sizeFlags)
84 {
85 // TODO
86 }
87
88 // Clipboard operations
89 void wxTextCtrl::Copy()
90 {
91 // TODO
92 }
93
94 void wxTextCtrl::Cut()
95 {
96 // TODO
97 }
98
99 void wxTextCtrl::Paste()
100 {
101 // TODO
102 }
103
104 void wxTextCtrl::SetEditable(bool editable)
105 {
106 // TODO
107 }
108
109 void wxTextCtrl::SetInsertionPoint(long pos)
110 {
111 // TODO
112 }
113
114 void wxTextCtrl::SetInsertionPointEnd()
115 {
116 long pos = GetLastPosition();
117 SetInsertionPoint(pos);
118 }
119
120 long wxTextCtrl::GetInsertionPoint() const
121 {
122 // TODO
123 return 0;
124 }
125
126 long wxTextCtrl::GetLastPosition() const
127 {
128 // TODO
129 return 0;
130 }
131
132 void wxTextCtrl::Replace(long from, long to, const wxString& value)
133 {
134 // TODO
135 return 0;
136 }
137
138 void wxTextCtrl::Remove(long from, long to)
139 {
140 // TODO
141 }
142
143 void wxTextCtrl::SetSelection(long from, long to)
144 {
145 // TODO
146 }
147
148 bool wxTextCtrl::LoadFile(const wxString& file)
149 {
150 if (!wxFileExists(file))
151 return FALSE;
152
153 m_fileName = file;
154
155 Clear();
156
157 ifstream input((char*) (const char*) file, ios::nocreate | ios::in);
158
159 if (!input.bad())
160 {
161 struct stat stat_buf;
162 if (stat(file, &stat_buf) < 0)
163 return FALSE;
164 // This may need to be a bigger buffer than the file size suggests,
165 // if it's a UNIX file. Give it an extra 1000 just in case.
166 char *tmp_buffer = (char*)malloc((size_t)(stat_buf.st_size+1+1000));
167 long no_lines = 0;
168 long pos = 0;
169 while (!input.eof() && input.peek() != EOF)
170 {
171 input.getline(wxBuffer, 500);
172 int len = strlen(wxBuffer);
173 wxBuffer[len] = 13;
174 wxBuffer[len+1] = 10;
175 wxBuffer[len+2] = 0;
176 strcpy(tmp_buffer+pos, wxBuffer);
177 pos += strlen(wxBuffer);
178 no_lines++;
179 }
180
181 // TODO add line
182
183 free(tmp_buffer);
184
185 return TRUE;
186 }
187 return FALSE;
188 }
189
190 // If file is null, try saved file name first
191 // Returns TRUE if succeeds.
192 bool wxTextCtrl::SaveFile(const wxString& file)
193 {
194 wxString theFile(file);
195 if (theFile == "")
196 theFile = m_fileName;
197 if (theFile == "")
198 return FALSE;
199 m_fileName = theFile;
200
201 ofstream output((char*) (const char*) theFile);
202 if (output.bad())
203 return FALSE;
204
205 // TODO get and save text
206
207 return FALSE;
208 }
209
210 void wxTextCtrl::WriteText(const wxString& text)
211 {
212 // TODO write text to control
213 }
214
215 void wxTextCtrl::AppendText(const wxString& text)
216 {
217 // TODO append text to control
218 }
219
220 void wxTextCtrl::Clear()
221 {
222 // TODO
223 }
224
225 bool wxTextCtrl::IsModified() const
226 {
227 // TODO
228 return FALSE;
229 }
230
231 // Makes 'unmodified'
232 void wxTextCtrl::DiscardEdits()
233 {
234 // TODO
235 }
236
237 int wxTextCtrl::GetNumberOfLines() const
238 {
239 // TODO
240 return 0;
241 }
242
243 long wxTextCtrl::XYToPosition(long x, long y) const
244 {
245 // TODO
246 return 0;
247 }
248
249 void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
250 {
251 // TODO
252 }
253
254 void wxTextCtrl::ShowPosition(long pos)
255 {
256 // TODO
257 }
258
259 int wxTextCtrl::GetLineLength(long lineNo) const
260 {
261 // TODO
262 return 0;
263 }
264
265 wxString wxTextCtrl::GetLineText(long lineNo) const
266 {
267 // TODO
268 return wxString("");
269 }
270
271 /*
272 * Text item
273 */
274
275 void wxTextCtrl::Command(wxCommandEvent & event)
276 {
277 SetValue (event.GetString());
278 ProcessCommand (event);
279 }
280
281 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
282 {
283 // By default, load the first file into the text window.
284 if (event.GetNumberOfFiles() > 0)
285 {
286 LoadFile(event.GetFiles()[0]);
287 }
288 }
289
290 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
291 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
292
293 //=========================================================================
294 // Called then the buffer is full (gcc 2.6.3)
295 // or when "endl" is output (Borland 4.5)
296 //=========================================================================
297 // Class declaration using multiple inheritance doesn't work properly for
298 // Borland. See note in wb_text.h.
299 #ifndef NO_TEXT_WINDOW_STREAM
300 int wxTextCtrl::overflow(int c)
301 {
302 // Make sure there is a holding area
303 if ( allocate()==EOF )
304 {
305 wxError("Streambuf allocation failed","Internal error");
306 return EOF;
307 }
308
309 // Verify that there are no characters in get area
310 if ( gptr() && gptr() < egptr() )
311 {
312 wxError("Who's trespassing my get area?","Internal error");
313 return EOF;
314 }
315
316 // Reset get area
317 setg(0,0,0);
318
319 // Make sure there is a put area
320 if ( ! pptr() )
321 {
322 /* This doesn't seem to be fatal so comment out error message */
323 // wxError("Put area not opened","Internal error");
324 setp( base(), base() );
325 }
326
327 // Determine how many characters have been inserted but no consumed
328 int plen = pptr() - pbase();
329
330 // Now Jerry relies on the fact that the buffer is at least 2 chars
331 // long, but the holding area "may be as small as 1" ???
332 // And we need an additional \0, so let's keep this inefficient but
333 // safe copy.
334
335 // If c!=EOF, it is a character that must also be comsumed
336 int xtra = c==EOF? 0 : 1;
337
338 // Write temporary C-string to wxTextWindow
339 {
340 char *txt = new char[plen+xtra+1];
341 memcpy(txt, pbase(), plen);
342 txt[plen] = (char)c; // append c
343 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
344 // If the put area already contained \0, output will be truncated there
345 AppendText(txt);
346 delete[] txt;
347 }
348
349 // Reset put area
350 setp(pbase(), epptr());
351
352 #if defined(__WATCOMC__)
353 return __NOT_EOF;
354 #elif defined(zapeof) // HP-UX (all cfront based?)
355 return zapeof(c);
356 #else
357 return c!=EOF ? c : 0; // this should make everybody happy
358 #endif
359 }
360
361 //=========================================================================
362 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
363 //=========================================================================
364 int wxTextCtrl::sync()
365 {
366 // Verify that there are no characters in get area
367 if ( gptr() && gptr() < egptr() )
368 {
369 wxError("Who's trespassing my get area?","Internal error");
370 return EOF;
371 }
372
373 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
374
375 return 0;
376 /* OLD CODE
377 int len = pptr() - pbase();
378 char *txt = new char[len+1];
379 strncpy(txt, pbase(), len);
380 txt[len] = '\0';
381 (*this) << txt;
382 setp(pbase(), epptr());
383 delete[] txt;
384 return 0;
385 */
386 }
387
388 //=========================================================================
389 // Should not be called by a "ostream". Used by a "istream"
390 //=========================================================================
391 int wxTextCtrl::underflow()
392 {
393 return EOF;
394 }
395 #endif
396
397 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
398 {
399 AppendText(s);
400 return *this;
401 }
402
403 wxTextCtrl& wxTextCtrl::operator<<(float f)
404 {
405 wxString str;
406 str.Printf("%.2f", f);
407 AppendText(str);
408 return *this;
409 }
410
411 wxTextCtrl& wxTextCtrl::operator<<(double d)
412 {
413 wxString str;
414 str.Printf("%.2f", d);
415 AppendText(str);
416 return *this;
417 }
418
419 wxTextCtrl& wxTextCtrl::operator<<(int i)
420 {
421 wxString str;
422 str.Printf("%d", i);
423 AppendText(str);
424 return *this;
425 }
426
427 wxTextCtrl& wxTextCtrl::operator<<(long i)
428 {
429 wxString str;
430 str.Printf("%ld", i);
431 AppendText(str);
432 return *this;
433 }
434
435 wxTextCtrl& wxTextCtrl::operator<<(const char c)
436 {
437 char buf[2];
438
439 buf[0] = c;
440 buf[1] = 0;
441 AppendText(buf);
442 return *this;
443 }
444