]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: wxTextCtrl | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
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 | ||
02e8b2f9 JS |
33 | #include <Xm/Text.h> |
34 | #include <sys/types.h> | |
35 | #include <sys/stat.h> | |
36 | #include <ctype.h> | |
37 | ||
38 | #include "wx/motif/private.h" | |
39 | ||
40 | static void | |
41 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr); | |
42 | static void | |
43 | wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs); | |
44 | static void | |
45 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
46 | static void | |
47 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
dfc54541 JS |
48 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, |
49 | XmAnyCallbackStruct *ptr); | |
02e8b2f9 | 50 | |
4bb6408c JS |
51 | #if !USE_SHARED_LIBRARY |
52 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) | |
53 | ||
54 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
55 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
02e8b2f9 | 56 | EVT_CHAR(wxTextCtrl::OnChar) |
4bb6408c JS |
57 | END_EVENT_TABLE() |
58 | #endif | |
59 | ||
60 | // Text item | |
61 | wxTextCtrl::wxTextCtrl() | |
62 | #ifndef NO_TEXT_WINDOW_STREAM | |
63 | :streambuf() | |
64 | #endif | |
65 | { | |
66 | m_fileName = ""; | |
02e8b2f9 JS |
67 | m_tempCallbackStruct = (void*) NULL; |
68 | m_modified = FALSE; | |
dfc54541 | 69 | m_processedDefault = FALSE; |
4bb6408c JS |
70 | } |
71 | ||
72 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, | |
73 | const wxString& value, | |
74 | const wxPoint& pos, | |
75 | const wxSize& size, long style, | |
76 | const wxValidator& validator, | |
77 | const wxString& name) | |
78 | { | |
02e8b2f9 JS |
79 | m_tempCallbackStruct = (void*) NULL; |
80 | m_modified = FALSE; | |
dfc54541 | 81 | m_processedDefault = FALSE; |
4bb6408c | 82 | m_fileName = ""; |
02e8b2f9 | 83 | |
4bb6408c JS |
84 | SetName(name); |
85 | SetValidator(validator); | |
86 | if (parent) parent->AddChild(this); | |
87 | ||
88 | m_windowStyle = style; | |
89 | ||
90 | if ( id == -1 ) | |
91 | m_windowId = (int)NewControlId(); | |
92 | else | |
93 | m_windowId = id; | |
94 | ||
02e8b2f9 JS |
95 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
96 | ||
97 | bool wantHorizScrolling = ((m_windowStyle & wxHSCROLL) != 0); | |
98 | ||
99 | // If we don't have horizontal scrollbars, we want word wrap. | |
100 | bool wantWordWrap = !wantHorizScrolling; | |
101 | ||
102 | if (m_windowStyle & wxTE_MULTILINE) | |
103 | { | |
104 | Arg args[2]; | |
105 | XtSetArg (args[0], XmNscrollHorizontal, wantHorizScrolling ? True : False); | |
106 | XtSetArg (args[1], XmNwordWrap, wantWordWrap ? True : False); | |
107 | ||
108 | m_mainWidget = (WXWidget) XmCreateScrolledText (parentWidget, (char*) (const char*) name, args, 2); | |
109 | ||
110 | XtVaSetValues ((Widget) m_mainWidget, | |
111 | XmNeditable, ((style & wxTE_READONLY) ? False : True), | |
112 | XmNeditMode, XmMULTI_LINE_EDIT, | |
113 | NULL); | |
114 | XtManageChild ((Widget) m_mainWidget); | |
115 | } | |
116 | else | |
117 | { | |
118 | m_mainWidget = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) name, | |
119 | xmTextWidgetClass, parentWidget, | |
120 | NULL); | |
121 | ||
122 | // TODO: Is this relevant? What does it do? | |
123 | int noCols = 2; | |
124 | if (!value.IsNull() && (value.Length() > (unsigned int) noCols)) | |
125 | noCols = value.Length(); | |
126 | XtVaSetValues ((Widget) m_mainWidget, | |
127 | XmNcolumns, noCols, | |
128 | NULL); | |
129 | } | |
130 | ||
131 | if (!value.IsNull()) | |
132 | XmTextSetString ((Widget) m_mainWidget, (char*) (const char*) value); | |
133 | ||
134 | XtAddCallback((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc)wxTextWindowChangedProc, (XtPointer)this); | |
135 | ||
136 | XtAddCallback((Widget) m_mainWidget, XmNmodifyVerifyCallback, (XtCallbackProc)wxTextWindowModifyProc, (XtPointer)this); | |
137 | ||
dfc54541 | 138 | XtAddCallback((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc)wxTextWindowActivateProc, (XtPointer)this); |
02e8b2f9 JS |
139 | |
140 | XtAddCallback((Widget) m_mainWidget, XmNfocusCallback, (XtCallbackProc)wxTextWindowGainFocusProc, (XtPointer)this); | |
141 | ||
142 | XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this); | |
143 | ||
144 | SetCanAddEventHandler(TRUE); | |
145 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
146 | ||
147 | SetFont(* parent->GetFont()); | |
148 | ChangeColour(m_mainWidget); | |
149 | ||
4bb6408c JS |
150 | return TRUE; |
151 | } | |
152 | ||
02e8b2f9 | 153 | WXWidget wxTextCtrl::GetTopWidget() const |
4bb6408c | 154 | { |
dfc54541 | 155 | return ((m_windowStyle & wxTE_MULTILINE) ? (WXWidget) XtParent((Widget) m_mainWidget) : m_mainWidget); |
4bb6408c JS |
156 | } |
157 | ||
02e8b2f9 | 158 | wxString wxTextCtrl::GetValue() const |
4bb6408c | 159 | { |
dfc54541 JS |
160 | if (m_windowStyle & wxTE_PASSWORD) |
161 | return m_value; | |
162 | else | |
163 | { | |
164 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
165 | if (s) | |
166 | { | |
167 | wxString str(s); | |
168 | XtFree (s); | |
169 | return str; | |
170 | } | |
171 | else | |
172 | { | |
173 | return wxEmptyString; | |
174 | } | |
175 | } | |
4bb6408c JS |
176 | } |
177 | ||
02e8b2f9 | 178 | void wxTextCtrl::SetValue(const wxString& value) |
4bb6408c | 179 | { |
dfc54541 JS |
180 | wxASSERT_MSG( (!value.IsNull()), "Must not pass a null string to wxTextCtrl::SetValue." ) ; |
181 | m_inSetValue = TRUE; | |
182 | ||
183 | XmTextSetString ((Widget) m_mainWidget, (char*) (const char*) value); | |
184 | ||
185 | m_inSetValue = FALSE; | |
4bb6408c JS |
186 | } |
187 | ||
188 | // Clipboard operations | |
189 | void wxTextCtrl::Copy() | |
190 | { | |
dfc54541 | 191 | XmTextCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
192 | } |
193 | ||
194 | void wxTextCtrl::Cut() | |
195 | { | |
dfc54541 | 196 | XmTextCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
197 | } |
198 | ||
199 | void wxTextCtrl::Paste() | |
200 | { | |
dfc54541 | 201 | XmTextPaste((Widget) m_mainWidget); |
4bb6408c JS |
202 | } |
203 | ||
204 | void wxTextCtrl::SetEditable(bool editable) | |
205 | { | |
dfc54541 | 206 | XmTextSetEditable((Widget) m_mainWidget, (Boolean) editable); |
4bb6408c JS |
207 | } |
208 | ||
209 | void wxTextCtrl::SetInsertionPoint(long pos) | |
210 | { | |
dfc54541 | 211 | XmTextSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
212 | } |
213 | ||
214 | void wxTextCtrl::SetInsertionPointEnd() | |
215 | { | |
216 | long pos = GetLastPosition(); | |
217 | SetInsertionPoint(pos); | |
218 | } | |
219 | ||
220 | long wxTextCtrl::GetInsertionPoint() const | |
221 | { | |
dfc54541 | 222 | return (long) XmTextGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
223 | } |
224 | ||
225 | long wxTextCtrl::GetLastPosition() const | |
226 | { | |
dfc54541 | 227 | return (long) XmTextGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
228 | } |
229 | ||
230 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
231 | { | |
dfc54541 JS |
232 | XmTextReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
233 | (char*) (const char*) value); | |
4bb6408c JS |
234 | } |
235 | ||
236 | void wxTextCtrl::Remove(long from, long to) | |
237 | { | |
dfc54541 JS |
238 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
239 | (Time) 0); | |
240 | XmTextRemove ((Widget) m_mainWidget); | |
4bb6408c JS |
241 | } |
242 | ||
243 | void wxTextCtrl::SetSelection(long from, long to) | |
244 | { | |
dfc54541 JS |
245 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
246 | (Time) 0); | |
4bb6408c JS |
247 | } |
248 | ||
249 | bool wxTextCtrl::LoadFile(const wxString& file) | |
250 | { | |
251 | if (!wxFileExists(file)) | |
252 | return FALSE; | |
253 | ||
254 | m_fileName = file; | |
255 | ||
256 | Clear(); | |
257 | ||
258 | ifstream input((char*) (const char*) file, ios::nocreate | ios::in); | |
259 | ||
260 | if (!input.bad()) | |
261 | { | |
262 | struct stat stat_buf; | |
263 | if (stat(file, &stat_buf) < 0) | |
264 | return FALSE; | |
265 | // This may need to be a bigger buffer than the file size suggests, | |
266 | // if it's a UNIX file. Give it an extra 1000 just in case. | |
267 | char *tmp_buffer = (char*)malloc((size_t)(stat_buf.st_size+1+1000)); | |
268 | long no_lines = 0; | |
269 | long pos = 0; | |
270 | while (!input.eof() && input.peek() != EOF) | |
271 | { | |
272 | input.getline(wxBuffer, 500); | |
273 | int len = strlen(wxBuffer); | |
274 | wxBuffer[len] = 13; | |
275 | wxBuffer[len+1] = 10; | |
276 | wxBuffer[len+2] = 0; | |
277 | strcpy(tmp_buffer+pos, wxBuffer); | |
278 | pos += strlen(wxBuffer); | |
279 | no_lines++; | |
280 | } | |
281 | ||
282 | // TODO add line | |
283 | ||
284 | free(tmp_buffer); | |
285 | ||
286 | return TRUE; | |
287 | } | |
288 | return FALSE; | |
289 | } | |
290 | ||
291 | // If file is null, try saved file name first | |
292 | // Returns TRUE if succeeds. | |
293 | bool wxTextCtrl::SaveFile(const wxString& file) | |
294 | { | |
295 | wxString theFile(file); | |
296 | if (theFile == "") | |
297 | theFile = m_fileName; | |
298 | if (theFile == "") | |
299 | return FALSE; | |
300 | m_fileName = theFile; | |
301 | ||
302 | ofstream output((char*) (const char*) theFile); | |
303 | if (output.bad()) | |
304 | return FALSE; | |
305 | ||
306 | // TODO get and save text | |
307 | ||
308 | return FALSE; | |
309 | } | |
310 | ||
311 | void wxTextCtrl::WriteText(const wxString& text) | |
312 | { | |
dfc54541 JS |
313 | long textPosition = GetInsertionPoint() + strlen (text); |
314 | XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), (char*) (const char*) text); | |
315 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
316 | SetInsertionPoint(textPosition); | |
317 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
318 | m_modified = TRUE; | |
4bb6408c JS |
319 | } |
320 | ||
321 | void wxTextCtrl::Clear() | |
322 | { | |
02e8b2f9 | 323 | XmTextSetString ((Widget) m_mainWidget, ""); |
02e8b2f9 | 324 | m_modified = FALSE; |
4bb6408c JS |
325 | } |
326 | ||
327 | bool wxTextCtrl::IsModified() const | |
328 | { | |
02e8b2f9 | 329 | return m_modified; |
4bb6408c JS |
330 | } |
331 | ||
332 | // Makes 'unmodified' | |
333 | void wxTextCtrl::DiscardEdits() | |
334 | { | |
dfc54541 JS |
335 | XmTextSetString ((Widget) m_mainWidget, ""); |
336 | m_modified = FALSE; | |
4bb6408c JS |
337 | } |
338 | ||
339 | int wxTextCtrl::GetNumberOfLines() const | |
340 | { | |
dfc54541 JS |
341 | // HIDEOUSLY inefficient, but we have no choice. |
342 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
343 | if (s) | |
344 | { | |
345 | long i = 0; | |
346 | int currentLine = 0; | |
347 | bool finished = FALSE; | |
348 | while (!finished) | |
349 | { | |
350 | int ch = s[i]; | |
351 | if (ch == '\n') | |
352 | { | |
353 | currentLine++; | |
354 | i++; | |
355 | } | |
356 | else if (ch == 0) | |
357 | { | |
358 | finished = TRUE; | |
359 | } | |
360 | else | |
361 | i++; | |
362 | } | |
363 | ||
364 | XtFree (s); | |
365 | return currentLine; | |
366 | } | |
4bb6408c JS |
367 | return 0; |
368 | } | |
369 | ||
370 | long wxTextCtrl::XYToPosition(long x, long y) const | |
371 | { | |
dfc54541 JS |
372 | /* It seems, that there is a bug in some versions of the Motif library, |
373 | so the original wxWin-Code doesn't work. */ | |
374 | /* | |
375 | Widget textWidget = (Widget) handle; | |
376 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
377 | */ | |
378 | /* Now a little workaround: */ | |
379 | long r=0; | |
380 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
381 | return r+x; | |
4bb6408c JS |
382 | } |
383 | ||
384 | void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
385 | { | |
dfc54541 JS |
386 | Position xx, yy; |
387 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
388 | *x = xx; *y = yy; | |
4bb6408c JS |
389 | } |
390 | ||
391 | void wxTextCtrl::ShowPosition(long pos) | |
392 | { | |
dfc54541 | 393 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
394 | } |
395 | ||
396 | int wxTextCtrl::GetLineLength(long lineNo) const | |
397 | { | |
dfc54541 JS |
398 | wxString str = GetLineText (lineNo); |
399 | return (int) str.Length(); | |
4bb6408c JS |
400 | } |
401 | ||
402 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
403 | { | |
dfc54541 JS |
404 | // HIDEOUSLY inefficient, but we have no choice. |
405 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
406 | ||
407 | if (s) | |
408 | { | |
409 | wxString buf(""); | |
410 | long i; | |
411 | int currentLine = 0; | |
412 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
413 | if (s[i] == '\n') | |
414 | currentLine++; | |
415 | // Now get the text | |
416 | int j; | |
417 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
418 | buf += s[i]; | |
419 | ||
420 | XtFree(s); | |
421 | return buf; | |
422 | } | |
423 | else | |
424 | return wxEmptyString; | |
4bb6408c JS |
425 | } |
426 | ||
427 | /* | |
428 | * Text item | |
429 | */ | |
430 | ||
431 | void wxTextCtrl::Command(wxCommandEvent & event) | |
432 | { | |
433 | SetValue (event.GetString()); | |
434 | ProcessCommand (event); | |
435 | } | |
436 | ||
437 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
438 | { | |
439 | // By default, load the first file into the text window. | |
440 | if (event.GetNumberOfFiles() > 0) | |
441 | { | |
442 | LoadFile(event.GetFiles()[0]); | |
443 | } | |
444 | } | |
445 | ||
446 | // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of | |
447 | // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers | |
448 | ||
449 | //========================================================================= | |
450 | // Called then the buffer is full (gcc 2.6.3) | |
451 | // or when "endl" is output (Borland 4.5) | |
452 | //========================================================================= | |
453 | // Class declaration using multiple inheritance doesn't work properly for | |
454 | // Borland. See note in wb_text.h. | |
455 | #ifndef NO_TEXT_WINDOW_STREAM | |
456 | int wxTextCtrl::overflow(int c) | |
457 | { | |
458 | // Make sure there is a holding area | |
459 | if ( allocate()==EOF ) | |
460 | { | |
461 | wxError("Streambuf allocation failed","Internal error"); | |
462 | return EOF; | |
463 | } | |
464 | ||
465 | // Verify that there are no characters in get area | |
466 | if ( gptr() && gptr() < egptr() ) | |
467 | { | |
468 | wxError("Who's trespassing my get area?","Internal error"); | |
469 | return EOF; | |
470 | } | |
471 | ||
472 | // Reset get area | |
473 | setg(0,0,0); | |
474 | ||
475 | // Make sure there is a put area | |
476 | if ( ! pptr() ) | |
477 | { | |
478 | /* This doesn't seem to be fatal so comment out error message */ | |
479 | // wxError("Put area not opened","Internal error"); | |
480 | setp( base(), base() ); | |
481 | } | |
482 | ||
483 | // Determine how many characters have been inserted but no consumed | |
484 | int plen = pptr() - pbase(); | |
485 | ||
486 | // Now Jerry relies on the fact that the buffer is at least 2 chars | |
487 | // long, but the holding area "may be as small as 1" ??? | |
488 | // And we need an additional \0, so let's keep this inefficient but | |
489 | // safe copy. | |
490 | ||
491 | // If c!=EOF, it is a character that must also be comsumed | |
492 | int xtra = c==EOF? 0 : 1; | |
493 | ||
494 | // Write temporary C-string to wxTextWindow | |
495 | { | |
496 | char *txt = new char[plen+xtra+1]; | |
497 | memcpy(txt, pbase(), plen); | |
498 | txt[plen] = (char)c; // append c | |
499 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c | |
500 | // If the put area already contained \0, output will be truncated there | |
501 | WriteText(txt); | |
502 | delete[] txt; | |
503 | } | |
504 | ||
505 | // Reset put area | |
506 | setp(pbase(), epptr()); | |
507 | ||
508 | #if defined(__WATCOMC__) | |
509 | return __NOT_EOF; | |
510 | #elif defined(zapeof) // HP-UX (all cfront based?) | |
511 | return zapeof(c); | |
512 | #else | |
513 | return c!=EOF ? c : 0; // this should make everybody happy | |
514 | #endif | |
515 | } | |
516 | ||
517 | //========================================================================= | |
518 | // called then "endl" is output (gcc) or then explicit sync is done (Borland) | |
519 | //========================================================================= | |
520 | int wxTextCtrl::sync() | |
521 | { | |
522 | // Verify that there are no characters in get area | |
523 | if ( gptr() && gptr() < egptr() ) | |
524 | { | |
525 | wxError("Who's trespassing my get area?","Internal error"); | |
526 | return EOF; | |
527 | } | |
528 | ||
529 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); | |
530 | ||
531 | return 0; | |
532 | /* OLD CODE | |
533 | int len = pptr() - pbase(); | |
534 | char *txt = new char[len+1]; | |
535 | strncpy(txt, pbase(), len); | |
536 | txt[len] = '\0'; | |
537 | (*this) << txt; | |
538 | setp(pbase(), epptr()); | |
539 | delete[] txt; | |
540 | return 0; | |
541 | */ | |
542 | } | |
543 | ||
544 | //========================================================================= | |
545 | // Should not be called by a "ostream". Used by a "istream" | |
546 | //========================================================================= | |
547 | int wxTextCtrl::underflow() | |
548 | { | |
549 | return EOF; | |
550 | } | |
551 | #endif | |
552 | ||
553 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
554 | { | |
555 | WriteText(s); | |
556 | return *this; | |
557 | } | |
558 | ||
559 | wxTextCtrl& wxTextCtrl::operator<<(float f) | |
560 | { | |
561 | wxString str; | |
562 | str.Printf("%.2f", f); | |
563 | WriteText(str); | |
564 | return *this; | |
565 | } | |
566 | ||
567 | wxTextCtrl& wxTextCtrl::operator<<(double d) | |
568 | { | |
569 | wxString str; | |
570 | str.Printf("%.2f", d); | |
571 | WriteText(str); | |
572 | return *this; | |
573 | } | |
574 | ||
575 | wxTextCtrl& wxTextCtrl::operator<<(int i) | |
576 | { | |
577 | wxString str; | |
578 | str.Printf("%d", i); | |
579 | WriteText(str); | |
580 | return *this; | |
581 | } | |
582 | ||
583 | wxTextCtrl& wxTextCtrl::operator<<(long i) | |
584 | { | |
585 | wxString str; | |
586 | str.Printf("%ld", i); | |
587 | WriteText(str); | |
588 | return *this; | |
589 | } | |
590 | ||
591 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
592 | { | |
593 | char buf[2]; | |
594 | ||
595 | buf[0] = c; | |
596 | buf[1] = 0; | |
597 | WriteText(buf); | |
598 | return *this; | |
599 | } | |
600 | ||
02e8b2f9 JS |
601 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
602 | { | |
dfc54541 JS |
603 | // Indicates that we should generate a normal command, because |
604 | // we're letting default behaviour happen (otherwise it's vetoed | |
605 | // by virtue of overriding OnChar) | |
606 | m_processedDefault = TRUE; | |
607 | ||
02e8b2f9 JS |
608 | if (m_tempCallbackStruct) |
609 | { | |
610 | XmTextVerifyCallbackStruct *textStruct = | |
611 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
612 | textStruct->doit = True; | |
613 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
614 | { | |
615 | textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); | |
616 | } | |
617 | } | |
618 | } | |
619 | ||
dfc54541 | 620 | static void wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr) |
02e8b2f9 JS |
621 | { |
622 | if (!wxGetWindowFromTable(w)) | |
623 | // Widget has been deleted! | |
624 | return; | |
625 | ||
626 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
627 | tw->SetModified(TRUE); | |
628 | } | |
629 | ||
630 | static void | |
631 | wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs) | |
632 | { | |
dfc54541 JS |
633 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
634 | tw->m_processedDefault = FALSE; | |
02e8b2f9 | 635 | |
dfc54541 JS |
636 | // First, do some stuff if it's a password control. |
637 | // (What does this do exactly?) | |
638 | ||
639 | if (tw->GetWindowStyleFlag() & wxTE_PASSWORD) | |
640 | { | |
641 | /* _sm_ | |
642 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
643 | * every event as a replace event. cbs->text->ptr gives the replacement | |
644 | * text, cbs->startPos gives the index of the first char affected by the | |
645 | * replace, and cbs->endPos gives the index one more than the last char | |
646 | * affected by the replace (startPos == endPos implies an empty range). | |
647 | * Hence, a deletion is represented by replacing all input text with a | |
648 | * blank string ("", *not* NULL!). A simple insertion that does not | |
649 | * overwrite any text has startPos == endPos. | |
650 | */ | |
651 | ||
652 | if (tw->m_value.IsNull()) | |
653 | { | |
654 | tw->m_value = cbs->text->ptr; | |
655 | } | |
656 | else | |
657 | { | |
658 | char * passwd = (char*) (const char*) tw->m_value; // Set up a more convenient alias. | |
659 | ||
660 | int len = passwd ? strlen(passwd) : 0; // Enough room for old text | |
661 | len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL | |
662 | len -= cbs->endPos - cbs->startPos; // - text from affected region. | |
663 | ||
664 | char * newS = new char [len]; | |
665 | char * p = passwd, * dest = newS, * insert = cbs->text->ptr; | |
666 | ||
667 | // Copy (old) text from passwd, up to the start posn of the change. | |
668 | int i; | |
669 | for (i = 0; i < cbs->startPos; ++i) | |
670 | *dest++ = *p++; | |
671 | ||
672 | // Copy the text to be inserted). | |
673 | while (*insert) | |
674 | *dest++ = *insert++; | |
675 | ||
676 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
677 | for (p = passwd + cbs->endPos; *p; ) | |
678 | *dest++ = *p++; | |
679 | *dest = 0; | |
680 | ||
681 | tw->m_value = newS; | |
682 | ||
683 | delete[] newS; | |
684 | } | |
685 | ||
686 | if (cbs->text->length>0) | |
687 | { | |
688 | int i; | |
689 | for (i = 0; i < cbs->text->length; ++i) | |
690 | cbs->text->ptr[i] = '*'; | |
691 | cbs->text->ptr[i] = 0; | |
692 | } | |
693 | } | |
694 | ||
695 | // If we're already within an OnChar, return: probably | |
696 | // a programmatic insertion. | |
697 | if (tw->m_tempCallbackStruct) | |
698 | return; | |
699 | ||
700 | // Check for a backspace | |
701 | if (cbs->startPos == (cbs->currInsert - 1)) | |
702 | { | |
703 | tw->m_tempCallbackStruct = (void*) cbs; | |
704 | ||
705 | wxKeyEvent event (wxEVT_CHAR); | |
706 | event.SetId(tw->GetId()); | |
707 | event.m_keyCode = WXK_DELETE; | |
708 | event.SetEventObject(tw); | |
709 | ||
710 | // Only if wxTextCtrl::OnChar is called | |
711 | // will this be set to True (and the character | |
712 | // passed through) | |
713 | cbs->doit = False; | |
714 | ||
715 | tw->GetEventHandler()->ProcessEvent(event); | |
716 | ||
717 | tw->m_tempCallbackStruct = NULL; | |
718 | ||
a4294b78 | 719 | if (tw->InSetValue()) |
dfc54541 JS |
720 | return; |
721 | ||
722 | if (tw->m_processedDefault) | |
723 | { | |
724 | // Can generate a command | |
725 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, tw->GetId()); | |
726 | commandEvent.SetEventObject(tw); | |
727 | tw->ProcessCommand(commandEvent); | |
728 | } | |
729 | ||
730 | return; | |
731 | } | |
732 | ||
733 | // Pasting operation: let it through without | |
734 | // calling OnChar | |
735 | if (cbs->text->length > 1) | |
736 | return; | |
737 | ||
738 | // Something other than text | |
739 | if (cbs->text->ptr == NULL) | |
740 | return; | |
02e8b2f9 | 741 | |
02e8b2f9 JS |
742 | tw->m_tempCallbackStruct = (void*) cbs; |
743 | ||
744 | wxKeyEvent event (wxEVT_CHAR); | |
745 | event.SetId(tw->GetId()); | |
02e8b2f9 | 746 | event.SetEventObject(tw); |
dfc54541 | 747 | event.m_keyCode = (cbs->text->ptr[0] == 10 ? 13 : cbs->text->ptr[0]); |
02e8b2f9 JS |
748 | |
749 | // Only if wxTextCtrl::OnChar is called | |
750 | // will this be set to True (and the character | |
751 | // passed through) | |
752 | cbs->doit = False; | |
753 | ||
754 | tw->GetEventHandler()->ProcessEvent(event); | |
755 | ||
756 | tw->m_tempCallbackStruct = NULL; | |
757 | ||
a4294b78 | 758 | if (tw->InSetValue()) |
dfc54541 JS |
759 | return; |
760 | ||
761 | if (tw->m_processedDefault) | |
762 | { | |
763 | // Can generate a command | |
764 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, tw->GetId()); | |
765 | commandEvent.SetEventObject(tw); | |
766 | tw->ProcessCommand(commandEvent); | |
767 | } | |
02e8b2f9 JS |
768 | } |
769 | ||
770 | static void | |
771 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs) | |
772 | { | |
773 | if (!wxGetWindowFromTable(w)) | |
774 | return; | |
775 | ||
776 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
777 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
778 | event.SetEventObject(tw); | |
779 | tw->GetEventHandler()->ProcessEvent(event); | |
780 | } | |
781 | ||
782 | static void | |
783 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs) | |
784 | { | |
785 | if (!wxGetWindowFromTable(w)) | |
786 | return; | |
787 | ||
788 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
789 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
790 | event.SetEventObject(tw); | |
791 | tw->GetEventHandler()->ProcessEvent(event); | |
792 | } | |
dfc54541 JS |
793 | |
794 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
795 | XmAnyCallbackStruct *ptr) | |
796 | { | |
797 | if (!wxGetWindowFromTable(w)) | |
798 | return; | |
799 | ||
800 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
801 | /* | |
802 | case XmCR_ACTIVATE: | |
803 | type_event = wxEVENT_TYPE_TEXT_ENTER_COMMAND ; | |
804 | break; | |
805 | default: | |
806 | type_event = wxEVENT_TYPE_TEXT_COMMAND ; | |
807 | break; | |
808 | } | |
809 | */ | |
810 | ||
a4294b78 | 811 | if (tw->InSetValue()) |
dfc54541 JS |
812 | return; |
813 | ||
814 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); | |
815 | event.SetId(tw->GetId()); | |
816 | event.SetEventObject(tw); | |
817 | tw->ProcessCommand(event); | |
818 | } |