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