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