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