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