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