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