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