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