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