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