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