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