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