]> git.saurik.com Git - wxWidgets.git/blame - src/motif/textctrl.cpp
Applied #15375 to stop event-sending in generic wxSpinCtrl ctor (eco)
[wxWidgets.git] / src / motif / textctrl.cpp
CommitLineData
4bb6408c 1/////////////////////////////////////////////////////////////////////////////
355b4d3d 2// Name: src/motif/textctrl.cpp
4bb6408c
JS
3// Purpose: wxTextCtrl
4// Author: Julian Smart
5// Modified by:
6// Created: 17/09/98
4bb6408c 7// Copyright: (c) Julian Smart
65571936 8// Licence: wxWindows licence
4bb6408c
JS
9/////////////////////////////////////////////////////////////////////////////
10
66a007fb
VZ
11// ============================================================================
12// declarations
13// ============================================================================
14
15// ----------------------------------------------------------------------------
16// headers
17// ----------------------------------------------------------------------------
18
1248b41f
MB
19// For compilers that support precompilation, includes "wx.h".
20#include "wx/wxprec.h"
21
4bb6408c
JS
22#include <sys/types.h>
23#include <sys/stat.h>
66a007fb 24#include <ctype.h>
4bb6408c
JS
25
26#include "wx/textctrl.h"
de6185e2
WS
27
28#ifndef WX_PRECOMP
29 #include "wx/utils.h"
9eddec69 30 #include "wx/settings.h"
de6185e2
WS
31#endif
32
4bb6408c 33#include "wx/filefn.h"
4bb6408c 34
338dd992
JJ
35#ifdef __VMS__
36#pragma message disable nosimpint
37#endif
02e8b2f9 38#include <Xm/Text.h>
338dd992
JJ
39#ifdef __VMS__
40#pragma message enable nosimpint
41#endif
02e8b2f9
JS
42
43#include "wx/motif/private.h"
44
66a007fb
VZ
45// ----------------------------------------------------------------------------
46// private functions
47// ----------------------------------------------------------------------------
48
c27eab7e
VZ
49// helper: inserts the new text in the value of the text ctrl and returns the
50// result in place
51static void MergeChangesIntoString(wxString& value,
52 XmTextVerifyCallbackStruct *textStruct);
53
66a007fb
VZ
54// callbacks
55static void wxTextWindowChangedProc(Widget w, XtPointer clientData, XtPointer ptr);
56static void wxTextWindowModifyProc(Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs);
57static void wxTextWindowGainFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs);
58static void wxTextWindowLoseFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs);
59static void wxTextWindowActivateProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *ptr);
02e8b2f9 60
9d112688 61 BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase)
66a007fb
VZ
62 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
63 EVT_CHAR(wxTextCtrl::OnChar)
e702ff0f
JS
64
65 EVT_MENU(wxID_CUT, wxTextCtrl::OnCut)
66 EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy)
67 EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste)
68 EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo)
69 EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo)
70
71 EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut)
72 EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy)
73 EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste)
74 EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo)
75 EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo)
76
66a007fb 77 END_EVENT_TABLE()
4bb6408c 78
66a007fb
VZ
79// ============================================================================
80// implementation
81// ============================================================================
82
83// ----------------------------------------------------------------------------
84// wxTextCtrl
85// ----------------------------------------------------------------------------
86
4bb6408c
JS
87// Text item
88wxTextCtrl::wxTextCtrl()
4bb6408c 89{
d3b9f782 90 m_tempCallbackStruct = NULL;
7d8268a1
WS
91 m_modified = false;
92 m_processedDefault = false;
4bb6408c
JS
93}
94
66a007fb
VZ
95bool wxTextCtrl::Create(wxWindow *parent,
96 wxWindowID id,
2d120f83
JS
97 const wxString& value,
98 const wxPoint& pos,
66a007fb
VZ
99 const wxSize& size,
100 long style,
2d120f83
JS
101 const wxValidator& validator,
102 const wxString& name)
4bb6408c 103{
e1aae528
MB
104 if( !CreateControl( parent, id, pos, size, style, validator, name ) )
105 return false;
105fbe1f 106 PreCreation();
e1aae528 107
d3b9f782 108 m_tempCallbackStruct = NULL;
7d8268a1
WS
109 m_modified = false;
110 m_processedDefault = false;
66a007fb 111
02e8b2f9 112 Widget parentWidget = (Widget) parent->GetClientWidget();
66a007fb 113
996994c7 114 Bool wantHorizScroll = (m_windowStyle & wxHSCROLL) != 0 ? True : False;
02e8b2f9 115 // If we don't have horizontal scrollbars, we want word wrap.
996994c7
MB
116 // OpenMotif 2.1 crashes if wantWordWrap is True in Japanese
117 // locale (and probably other multibyte locales). The check might be
118 // more precise
119#if wxCHECK_LESSTIF() || wxCHECK_MOTIF_VERSION( 2, 2 )
120 Bool wantWordWrap = wantHorizScroll == True ? False : True;
121#else
122 Bool wantWordWrap = False;
123#endif
66a007fb 124
02e8b2f9
JS
125 if (m_windowStyle & wxTE_MULTILINE)
126 {
996994c7
MB
127 Arg args[8];
128 int count = 0;
129 XtSetArg (args[count], XmNscrollHorizontal, wantHorizScroll); ++count;
105fbe1f
MB
130 if( m_font.IsOk() )
131 XtSetArg (args[count], (String) wxFont::GetFontTag(),
132 m_font.GetFontType( XtDisplay(parentWidget) ) ); ++count;
996994c7 133 XtSetArg (args[count], XmNwordWrap, wantWordWrap); ++count;
6991087b 134 XtSetArg (args[count], XmNvalue, (const char*)value.mb_str()); ++count;
996994c7
MB
135 XtSetArg (args[count], XmNeditable,
136 style & wxTE_READONLY ? False : True); ++count;
137 XtSetArg (args[count], XmNeditMode, XmMULTI_LINE_EDIT ); ++count;
138
139 m_mainWidget =
140 (WXWidget) XmCreateScrolledText(parentWidget,
6991087b 141 name.char_str(),
996994c7
MB
142 args, count);
143
02e8b2f9
JS
144 XtManageChild ((Widget) m_mainWidget);
145 }
146 else
147 {
66a007fb
VZ
148 m_mainWidget = (WXWidget)XtVaCreateManagedWidget
149 (
6991087b 150 name.mb_str(),
66a007fb
VZ
151 xmTextWidgetClass,
152 parentWidget,
996994c7 153 wxFont::GetFontTag(), m_font.GetFontType( XtDisplay(parentWidget) ),
6991087b 154 XmNvalue, (const char*)value.mb_str(),
996994c7
MB
155 XmNeditable, (style & wxTE_READONLY) ?
156 False : True,
66a007fb
VZ
157 NULL
158 );
159
996994c7 160#if 0
02e8b2f9
JS
161 // TODO: Is this relevant? What does it do?
162 int noCols = 2;
6636ef8d 163 if (!value.empty() && (value.length() > (unsigned int) noCols))
de6185e2 164 noCols = value.length();
66a007fb
VZ
165 XtVaSetValues((Widget) m_mainWidget,
166 XmNcolumns, noCols,
167 NULL);
996994c7 168#endif
02e8b2f9 169 }
66a007fb
VZ
170
171 // remove border if asked for
172 if ( style & wxNO_BORDER )
173 {
174 XtVaSetValues((Widget)m_mainWidget,
175 XmNshadowThickness, 0,
176 NULL);
177 }
178
66a007fb 179 // install callbacks
02e8b2f9 180 XtAddCallback((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc)wxTextWindowChangedProc, (XtPointer)this);
66a007fb 181
02e8b2f9 182 XtAddCallback((Widget) m_mainWidget, XmNmodifyVerifyCallback, (XtCallbackProc)wxTextWindowModifyProc, (XtPointer)this);
66a007fb 183
dfc54541 184 XtAddCallback((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc)wxTextWindowActivateProc, (XtPointer)this);
66a007fb 185
02e8b2f9 186 XtAddCallback((Widget) m_mainWidget, XmNfocusCallback, (XtCallbackProc)wxTextWindowGainFocusProc, (XtPointer)this);
66a007fb 187
02e8b2f9 188 XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this);
66a007fb 189
105fbe1f 190 PostCreation();
e1aae528 191 AttachWidget (parent, m_mainWidget, (WXWidget) NULL,
f9204363 192 pos.x, pos.y, size.x, size.y);
66a007fb 193
7d8268a1 194 return true;
4bb6408c
JS
195}
196
02e8b2f9 197WXWidget wxTextCtrl::GetTopWidget() const
4bb6408c 198{
ccb234b4
MB
199 return IsMultiLine() ? (WXWidget)XtParent((Widget)m_mainWidget)
200 : m_mainWidget;
4bb6408c
JS
201}
202
02e8b2f9 203wxString wxTextCtrl::GetValue() const
4bb6408c 204{
c27eab7e
VZ
205 wxString str; // result
206
dfc54541 207 if (m_windowStyle & wxTE_PASSWORD)
c27eab7e
VZ
208 {
209 // the value is stored always in m_value because it can't be retrieved
210 // from the text control
211 str = m_value;
212 }
dfc54541
JS
213 else
214 {
89954433 215 str = wxTextEntry::GetValue();
c27eab7e
VZ
216
217 if ( m_tempCallbackStruct )
dfc54541 218 {
c27eab7e
VZ
219 // the string in the control isn't yet updated, can't use it as is
220 MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *)
221 m_tempCallbackStruct);
dfc54541
JS
222 }
223 }
c27eab7e
VZ
224
225 return str;
4bb6408c
JS
226}
227
ee2ec18e 228void wxTextCtrl::DoSetValue(const wxString& text, int flags)
4bb6408c 229{
7d8268a1 230 m_inSetValue = true;
66a007fb 231
6991087b 232 XmTextSetString ((Widget) m_mainWidget, text.char_str());
996994c7
MB
233 XtVaSetValues ((Widget) m_mainWidget,
234 XmNcursorPosition, text.length(),
b86de524 235 NULL);
ccb234b4 236
996994c7
MB
237 SetInsertionPoint(text.length());
238 XmTextShowPosition ((Widget) m_mainWidget, text.length());
355b4d3d 239 m_modified = true;
66a007fb 240
7d8268a1 241 m_inSetValue = false;
ee2ec18e
VZ
242
243 if ( flags & SetValue_SendEvent )
244 SendTextUpdatedEvent();
4bb6408c
JS
245}
246
4bb6408c
JS
247bool wxTextCtrl::IsModified() const
248{
02e8b2f9 249 return m_modified;
4bb6408c
JS
250}
251
3a9fa0d6
VZ
252// Makes modified or unmodified
253void wxTextCtrl::MarkDirty()
254{
7d8268a1 255 m_modified = true;
3a9fa0d6
VZ
256}
257
4bb6408c
JS
258void wxTextCtrl::DiscardEdits()
259{
7d8268a1 260 m_modified = false;
4bb6408c
JS
261}
262
263int wxTextCtrl::GetNumberOfLines() const
264{
dfc54541
JS
265 // HIDEOUSLY inefficient, but we have no choice.
266 char *s = XmTextGetString ((Widget) m_mainWidget);
267 if (s)
268 {
2d120f83
JS
269 long i = 0;
270 int currentLine = 0;
7d8268a1 271 bool finished = false;
2d120f83
JS
272 while (!finished)
273 {
274 int ch = s[i];
275 if (ch == '\n')
276 {
277 currentLine++;
278 i++;
279 }
280 else if (ch == 0)
281 {
7d8268a1 282 finished = true;
2d120f83
JS
283 }
284 else
285 i++;
286 }
66a007fb 287
2d120f83
JS
288 XtFree (s);
289 return currentLine;
dfc54541 290 }
4bb6408c
JS
291 return 0;
292}
293
294long wxTextCtrl::XYToPosition(long x, long y) const
295{
dfc54541 296/* It seems, that there is a bug in some versions of the Motif library,
2d120f83
JS
297 so the original wxWin-Code doesn't work. */
298 /*
299 Widget textWidget = (Widget) handle;
300 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
301 */
dfc54541
JS
302 /* Now a little workaround: */
303 long r=0;
304 for (int i=0; i<y; i++) r+=(GetLineLength(i)+1);
66a007fb 305 return r+x;
4bb6408c
JS
306}
307
813c20a6 308bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
4bb6408c 309{
dfc54541
JS
310 Position xx, yy;
311 XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy);
813c20a6
VZ
312 if ( x )
313 *x = xx;
314 if ( y )
315 *y = yy;
316
7d8268a1 317 return true;
4bb6408c
JS
318}
319
320void wxTextCtrl::ShowPosition(long pos)
321{
dfc54541 322 XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
4bb6408c
JS
323}
324
325int wxTextCtrl::GetLineLength(long lineNo) const
326{
dfc54541 327 wxString str = GetLineText (lineNo);
7520f3da 328 return (int) str.length();
4bb6408c
JS
329}
330
331wxString wxTextCtrl::GetLineText(long lineNo) const
332{
dfc54541
JS
333 // HIDEOUSLY inefficient, but we have no choice.
334 char *s = XmTextGetString ((Widget) m_mainWidget);
66a007fb 335
dfc54541
JS
336 if (s)
337 {
355b4d3d 338 wxString buf;
dfc54541
JS
339 long i;
340 int currentLine = 0;
341 for (i = 0; currentLine != lineNo && s[i]; i++ )
2d120f83
JS
342 if (s[i] == '\n')
343 currentLine++;
344 // Now get the text
345 int j;
346 for (j = 0; s[i] && s[i] != '\n'; i++, j++ )
347 buf += s[i];
66a007fb 348
2d120f83
JS
349 XtFree(s);
350 return buf;
351 }
352 else
353 return wxEmptyString;
4bb6408c
JS
354}
355
356/*
2d120f83
JS
357* Text item
358*/
359
4bb6408c
JS
360void wxTextCtrl::Command(wxCommandEvent & event)
361{
362 SetValue (event.GetString());
363 ProcessCommand (event);
364}
365
366void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
367{
368 // By default, load the first file into the text window.
369 if (event.GetNumberOfFiles() > 0)
370 {
371 LoadFile(event.GetFiles()[0]);
372 }
373}
374
02e8b2f9
JS
375void wxTextCtrl::OnChar(wxKeyEvent& event)
376{
2d120f83
JS
377 // Indicates that we should generate a normal command, because
378 // we're letting default behaviour happen (otherwise it's vetoed
379 // by virtue of overriding OnChar)
7d8268a1 380 m_processedDefault = true;
66a007fb 381
2d120f83 382 if (m_tempCallbackStruct)
02e8b2f9 383 {
2d120f83
JS
384 XmTextVerifyCallbackStruct *textStruct =
385 (XmTextVerifyCallbackStruct *) m_tempCallbackStruct;
386 textStruct->doit = True;
7a0079d5 387 if (wxIsascii(event.m_keyCode) && (textStruct->text->length == 1))
2d120f83 388 {
355b4d3d 389 textStruct->text->ptr[0] = (char)((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode);
2d120f83 390 }
02e8b2f9 391 }
02e8b2f9
JS
392}
393
4b5f3fe6 394void wxTextCtrl::ChangeFont(bool keepOriginalSize)
0d57be45 395{
4b5f3fe6 396 wxWindow::ChangeFont(keepOriginalSize);
0d57be45
JS
397}
398
399void wxTextCtrl::ChangeBackgroundColour()
400{
321db4b6 401 wxWindow::ChangeBackgroundColour();
66a007fb 402
94b49b93 403 /* TODO: should scrollbars be affected? Should probably have separate
2d120f83
JS
404 * function to change them (by default, taken from wxSystemSettings)
405 */
94b49b93
JS
406 if (m_windowStyle & wxTE_MULTILINE)
407 {
408 Widget parent = XtParent ((Widget) m_mainWidget);
409 Widget hsb, vsb;
66a007fb 410
94b49b93 411 XtVaGetValues (parent,
2d120f83
JS
412 XmNhorizontalScrollBar, &hsb,
413 XmNverticalScrollBar, &vsb,
414 NULL);
a756f210 415 wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
94b49b93 416 if (hsb)
7d8268a1 417 wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true);
94b49b93 418 if (vsb)
7d8268a1 419 wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true);
66a007fb 420
1119b899 421 // MBN: why change parent background?
7d8268a1 422 // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
94b49b93 423 }
0d57be45
JS
424}
425
426void wxTextCtrl::ChangeForegroundColour()
427{
321db4b6 428 wxWindow::ChangeForegroundColour();
66a007fb 429
94b49b93
JS
430 if (m_windowStyle & wxTE_MULTILINE)
431 {
432 Widget parent = XtParent ((Widget) m_mainWidget);
433 Widget hsb, vsb;
66a007fb 434
94b49b93 435 XtVaGetValues (parent,
2d120f83
JS
436 XmNhorizontalScrollBar, &hsb,
437 XmNverticalScrollBar, &vsb,
438 NULL);
66a007fb 439
2d120f83
JS
440 /* TODO: should scrollbars be affected? Should probably have separate
441 * function to change them (by default, taken from wxSystemSettings)
442 if (hsb)
94b49b93 443 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
2d120f83 444 if (vsb)
94b49b93 445 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
2d120f83 446 */
a8680e3e 447 wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
94b49b93 448 }
0d57be45
JS
449}
450
c27eab7e
VZ
451void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode)
452{
453 // we're in process of updating the text control
454 m_tempCallbackStruct = wxcbs;
455
456 XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs;
457
458 wxKeyEvent event (wxEVT_CHAR);
459 event.SetId(GetId());
460 event.m_keyCode = keycode;
461 event.SetEventObject(this);
462
463 // Only if wxTextCtrl::OnChar is called will this be set to True (and
464 // the character passed through)
465 cbs->doit = False;
466
937013e0 467 HandleWindowEvent(event);
c27eab7e
VZ
468
469 if ( !InSetValue() && m_processedDefault )
470 {
471 // Can generate a command
ce7fe42e 472 wxCommandEvent commandEvent(wxEVT_TEXT, GetId());
c27eab7e
VZ
473 commandEvent.SetEventObject(this);
474 ProcessCommand(commandEvent);
475 }
476
477 // do it after the (user) event handlers processed the events because
478 // otherwise GetValue() would return incorrect (not yet updated value)
479 m_tempCallbackStruct = NULL;
480}
481
e1aae528
MB
482wxSize wxDoGetSingleTextCtrlBestSize( Widget textWidget,
483 const wxWindow* window )
484{
485 Dimension xmargin, ymargin, highlight, shadow;
486 char* value;
487
488 XtVaGetValues( textWidget,
489 XmNmarginWidth, &xmargin,
490 XmNmarginHeight, &ymargin,
491 XmNvalue, &value,
492 XmNhighlightThickness, &highlight,
493 XmNshadowThickness, &shadow,
494 NULL );
1119b899 495
e1aae528 496 if( !value )
da8cf723 497 value = wxMOTIF_STR("|");
e1aae528
MB
498
499 int x, y;
500 window->GetTextExtent( value, &x, &y );
501
105fbe1f
MB
502 if( x < 90 )
503 x = 90;
1119b899 504
e1aae528
MB
505 return wxSize( x + 2 * xmargin + 2 * highlight + 2 * shadow,
506 // MBN: +2 necessary: Lesstif bug or mine?
7d8268a1 507 y + 2 * ymargin + 2 * highlight + 2 * shadow + 2 );
e1aae528
MB
508}
509
510wxSize wxTextCtrl::DoGetBestSize() const
511{
512 if( IsSingleLine() )
f9204363
MB
513 {
514 wxSize best = wxControl::DoGetBestSize();
105fbe1f
MB
515#if wxCHECK_MOTIF_VERSION( 2, 3 )
516 // OpenMotif 2.3 gives way too big X sizes
517 wxSize other_best = wxDoGetSingleTextCtrlBestSize
518 ( (Widget) GetTopWidget(), this );
519 return wxSize( other_best.x, best.y );
520#else
521 if( best.x < 90 ) best.x = 90;
f9204363
MB
522
523 return best;
105fbe1f 524#endif
f9204363 525 }
e1aae528
MB
526 else
527 return wxWindow::DoGetBestSize();
528}
529
c27eab7e
VZ
530// ----------------------------------------------------------------------------
531// helpers and Motif callbacks
532// ----------------------------------------------------------------------------
533
534static void MergeChangesIntoString(wxString& value,
535 XmTextVerifyCallbackStruct *cbs)
536{
537 /* _sm_
538 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
539 * every event as a replace event. cbs->text->ptr gives the replacement
540 * text, cbs->startPos gives the index of the first char affected by the
541 * replace, and cbs->endPos gives the index one more than the last char
542 * affected by the replace (startPos == endPos implies an empty range).
543 * Hence, a deletion is represented by replacing all input text with a
544 * blank string ("", *not* NULL!). A simple insertion that does not
545 * overwrite any text has startPos == endPos.
546 */
547
548 if ( !value )
549 {
550 // easy case: the ol value was empty
551 value = cbs->text->ptr;
552 }
553 else
554 {
555 // merge the changes into the value
556 const char * const passwd = value;
557 int len = value.length();
558
44d130a3
MB
559 len += ( cbs->text->ptr ?
560 strlen(cbs->text->ptr) :
561 0 ) + 1; // + new text (if any) + NUL
c27eab7e
VZ
562 len -= cbs->endPos - cbs->startPos; // - text from affected region.
563
564 char * newS = new char [len];
565 char * dest = newS,
566 * insert = cbs->text->ptr;
567
568 // Copy (old) text from passwd, up to the start posn of the change.
569 int i;
570 const char * p = passwd;
571 for (i = 0; i < cbs->startPos; ++i)
572 *dest++ = *p++;
573
574 // Copy the text to be inserted).
44d130a3
MB
575 if (insert)
576 while (*insert)
577 *dest++ = *insert++;
c27eab7e
VZ
578
579 // Finally, copy into newS any remaining text from passwd[endPos] on.
580 for (p = passwd + cbs->endPos; *p; )
581 *dest++ = *p++;
582 *dest = 0;
583
584 value = newS;
585
586 delete[] newS;
587 }
588}
589
590static void
af111fc3 591wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
02e8b2f9 592{
2d120f83
JS
593 if (!wxGetWindowFromTable(w))
594 // Widget has been deleted!
595 return;
66a007fb 596
2d120f83 597 wxTextCtrl *tw = (wxTextCtrl *) clientData;
7d8268a1 598 tw->SetModified(true);
02e8b2f9
JS
599}
600
66a007fb 601static void
af111fc3 602wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs)
02e8b2f9 603{
dfc54541 604 wxTextCtrl *tw = (wxTextCtrl *) clientData;
7d8268a1 605 tw->m_processedDefault = false;
66a007fb 606
c27eab7e
VZ
607 // First, do some stuff if it's a password control: in this case, we need
608 // to store the string inside the class because GetValue() can't retrieve
609 // it from the text ctrl. We do *not* do it in other circumstances because
610 // it would double the amount of memory needed.
66a007fb 611
c27eab7e 612 if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD )
dfc54541 613 {
c27eab7e 614 MergeChangesIntoString(tw->m_value, cbs);
66a007fb 615
c27eab7e 616 if ( cbs->text->length > 0 )
dfc54541
JS
617 {
618 int i;
619 for (i = 0; i < cbs->text->length; ++i)
620 cbs->text->ptr[i] = '*';
c27eab7e 621 cbs->text->ptr[i] = '\0';
dfc54541
JS
622 }
623 }
66a007fb 624
ccb234b4
MB
625 if(tw->InSetValue())
626 return;
627
c27eab7e
VZ
628 // If we're already within an OnChar, return: probably a programmatic
629 // insertion.
dfc54541
JS
630 if (tw->m_tempCallbackStruct)
631 return;
66a007fb 632
dfc54541
JS
633 // Check for a backspace
634 if (cbs->startPos == (cbs->currInsert - 1))
635 {
c27eab7e 636 tw->DoSendEvents((void *)cbs, WXK_DELETE);
66a007fb 637
dfc54541
JS
638 return;
639 }
66a007fb 640
c27eab7e 641 // Pasting operation: let it through without calling OnChar
dfc54541
JS
642 if (cbs->text->length > 1)
643 return;
66a007fb 644
dfc54541
JS
645 // Something other than text
646 if (cbs->text->ptr == NULL)
647 return;
66a007fb 648
c27eab7e
VZ
649 // normal key press
650 char ch = cbs->text->ptr[0];
651 tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch);
02e8b2f9
JS
652}
653
66a007fb 654static void
af111fc3 655wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs))
02e8b2f9 656{
2d120f83
JS
657 if (!wxGetWindowFromTable(w))
658 return;
66a007fb 659
2d120f83
JS
660 wxTextCtrl *tw = (wxTextCtrl *) clientData;
661 wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId());
662 event.SetEventObject(tw);
937013e0 663 tw->HandleWindowEvent(event);
02e8b2f9
JS
664}
665
66a007fb 666static void
af111fc3 667wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs))
02e8b2f9 668{
2d120f83
JS
669 if (!wxGetWindowFromTable(w))
670 return;
66a007fb 671
2d120f83
JS
672 wxTextCtrl *tw = (wxTextCtrl *) clientData;
673 wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId());
674 event.SetEventObject(tw);
937013e0 675 tw->HandleWindowEvent(event);
02e8b2f9 676}
dfc54541
JS
677
678static void wxTextWindowActivateProc(Widget w, XtPointer clientData,
af111fc3 679 XmAnyCallbackStruct *WXUNUSED(ptr))
dfc54541 680{
2d120f83
JS
681 if (!wxGetWindowFromTable(w))
682 return;
66a007fb 683
2d120f83 684 wxTextCtrl *tw = (wxTextCtrl *) clientData;
66a007fb 685
2d120f83
JS
686 if (tw->InSetValue())
687 return;
66a007fb 688
ce7fe42e 689 wxCommandEvent event(wxEVT_TEXT_ENTER);
2d120f83
JS
690 event.SetId(tw->GetId());
691 event.SetEventObject(tw);
692 tw->ProcessCommand(event);
dfc54541 693}
c27eab7e 694
e702ff0f
JS
695void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
696{
697 Cut();
698}
699
700void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
701{
702 Copy();
703}
704
705void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
706{
707 Paste();
708}
709
710void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
711{
712 Undo();
713}
714
715void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
716{
717 Redo();
718}
719
720void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
721{
722 event.Enable( CanCut() );
723}
724
725void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
726{
727 event.Enable( CanCopy() );
728}
729
730void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
731{
732 event.Enable( CanPaste() );
733}
734
735void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
736{
737 event.Enable( CanUndo() );
738}
739
740void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
741{
742 event.Enable( CanRedo() );
743}