]> git.saurik.com Git - wxWidgets.git/blob - src/motif/textctrl.cpp
bug with object being passed through ... corrected
[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 XtVaSetValues ((Widget) m_mainWidget,
154 XmNeditable, ((style & wxTE_READONLY) ? False : True),
155 NULL);
156
157 // TODO: Is this relevant? What does it do?
158 int noCols = 2;
159 if (!value.IsNull() && (value.Length() > (unsigned int) noCols))
160 noCols = value.Length();
161 XtVaSetValues((Widget) m_mainWidget,
162 XmNcolumns, noCols,
163 NULL);
164 }
165
166 // remove border if asked for
167 if ( style & wxNO_BORDER )
168 {
169 XtVaSetValues((Widget)m_mainWidget,
170 XmNshadowThickness, 0,
171 NULL);
172 }
173
174 if ( !!value )
175 {
176 #if 0
177 // don't do this because it is just linking the text to a source
178 // string which is unsafe. MB
179 //
180 XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str());
181 #else
182 // do this instead... MB
183 //
184 XtVaSetValues( (Widget) m_mainWidget,
185 XmNvalue, (char *)value.c_str(),
186 NULL);
187 #endif
188 }
189
190 // install callbacks
191 XtAddCallback((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc)wxTextWindowChangedProc, (XtPointer)this);
192
193 XtAddCallback((Widget) m_mainWidget, XmNmodifyVerifyCallback, (XtCallbackProc)wxTextWindowModifyProc, (XtPointer)this);
194
195 XtAddCallback((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc)wxTextWindowActivateProc, (XtPointer)this);
196
197 XtAddCallback((Widget) m_mainWidget, XmNfocusCallback, (XtCallbackProc)wxTextWindowGainFocusProc, (XtPointer)this);
198
199 XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this);
200
201 // font
202 m_font = parent->GetFont();
203 ChangeFont(FALSE);
204
205 SetCanAddEventHandler(TRUE);
206 AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y);
207
208 ChangeBackgroundColour();
209
210 return TRUE;
211 }
212
213 WXWidget wxTextCtrl::GetTopWidget() const
214 {
215 return ((m_windowStyle & wxTE_MULTILINE) ? (WXWidget) XtParent((Widget) m_mainWidget) : m_mainWidget);
216 }
217
218 wxString wxTextCtrl::GetValue() const
219 {
220 wxString str; // result
221
222 if (m_windowStyle & wxTE_PASSWORD)
223 {
224 // the value is stored always in m_value because it can't be retrieved
225 // from the text control
226 str = m_value;
227 }
228 else
229 {
230 // just get the string from Motif
231 char *s = XmTextGetString ((Widget) m_mainWidget);
232 if ( s )
233 {
234 str = s;
235 XtFree (s);
236 }
237 //else: return empty string
238
239 if ( m_tempCallbackStruct )
240 {
241 // the string in the control isn't yet updated, can't use it as is
242 MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *)
243 m_tempCallbackStruct);
244 }
245 }
246
247 return str;
248 }
249
250 void wxTextCtrl::SetValue(const wxString& value)
251 {
252 m_inSetValue = TRUE;
253
254 #if 0
255 // don't do this because it is just linking the text to a source
256 // string which is unsafe. MB
257 //
258 XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str());
259 #else
260 // do this instead... MB
261 //
262 XtVaSetValues( (Widget) m_mainWidget,
263 XmNvalue, (char *)value.c_str(),
264 NULL);
265 #endif
266
267 m_inSetValue = FALSE;
268 }
269
270 // Clipboard operations
271 void wxTextCtrl::Copy()
272 {
273 XmTextCopy((Widget) m_mainWidget, CurrentTime);
274 }
275
276 void wxTextCtrl::Cut()
277 {
278 XmTextCut((Widget) m_mainWidget, CurrentTime);
279 }
280
281 void wxTextCtrl::Paste()
282 {
283 XmTextPaste((Widget) m_mainWidget);
284 }
285
286 bool wxTextCtrl::CanCopy() const
287 {
288 // Can copy if there's a selection
289 long from, to;
290 GetSelection(& from, & to);
291 return (from != to) ;
292 }
293
294 bool wxTextCtrl::CanCut() const
295 {
296 // Can cut if there's a selection
297 long from, to;
298 GetSelection(& from, & to);
299 return (from != to) ;
300 }
301
302 bool wxTextCtrl::CanPaste() const
303 {
304 return IsEditable() ;
305 }
306
307 // Undo/redo
308 void wxTextCtrl::Undo()
309 {
310 // Not possible in Motif
311 }
312
313 void wxTextCtrl::Redo()
314 {
315 // Not possible in Motif
316 }
317
318 bool wxTextCtrl::CanUndo() const
319 {
320 // No Undo in Motif
321 return FALSE;
322 }
323
324 bool wxTextCtrl::CanRedo() const
325 {
326 // No Redo in Motif
327 return FALSE;
328 }
329
330 // If the return values from and to are the same, there is no
331 // selection.
332 void wxTextCtrl::GetSelection(long* from, long* to) const
333 {
334 XmTextPosition left, right;
335
336 XmTextGetSelectionPosition((Widget) m_mainWidget, & left, & right);
337
338 *from = (long) left;
339 *to = (long) right;
340 }
341
342 bool wxTextCtrl::IsEditable() const
343 {
344 return (XmTextGetEditable((Widget) m_mainWidget) != 0);
345 }
346
347 void wxTextCtrl::SetEditable(bool editable)
348 {
349 XmTextSetEditable((Widget) m_mainWidget, (Boolean) editable);
350 }
351
352 void wxTextCtrl::SetInsertionPoint(long pos)
353 {
354 XmTextSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
355 }
356
357 void wxTextCtrl::SetInsertionPointEnd()
358 {
359 long pos = GetLastPosition();
360 SetInsertionPoint(pos);
361 }
362
363 long wxTextCtrl::GetInsertionPoint() const
364 {
365 return (long) XmTextGetInsertionPosition ((Widget) m_mainWidget);
366 }
367
368 long wxTextCtrl::GetLastPosition() const
369 {
370 return (long) XmTextGetLastPosition ((Widget) m_mainWidget);
371 }
372
373 void wxTextCtrl::Replace(long from, long to, const wxString& value)
374 {
375 XmTextReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
376 (char*) (const char*) value);
377 }
378
379 void wxTextCtrl::Remove(long from, long to)
380 {
381 XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
382 (Time) 0);
383 XmTextRemove ((Widget) m_mainWidget);
384 }
385
386 void wxTextCtrl::SetSelection(long from, long to)
387 {
388 XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to,
389 (Time) 0);
390 }
391
392 bool wxTextCtrl::LoadFile(const wxString& file)
393 {
394 if (!wxFileExists(file))
395 return FALSE;
396
397 m_fileName = file;
398
399 Clear();
400
401 Widget textWidget = (Widget) m_mainWidget;
402 FILE *fp = 0;
403
404 struct stat statb;
405 if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG ||
406 !(fp = fopen ((char*) (const char*) file, "r")))
407 {
408 return FALSE;
409 }
410 else
411 {
412 long len = statb.st_size;
413 char *text;
414 if (!(text = XtMalloc ((unsigned) (len + 1))))
415 {
416 fclose (fp);
417 return FALSE;
418 }
419 if (fread (text, sizeof (char), len, fp) != (size_t) len)
420 {
421 }
422 fclose (fp);
423
424 text[len] = 0;
425 XmTextSetString (textWidget, text);
426 // m_textPosition = len;
427 XtFree (text);
428 m_modified = FALSE;
429 return TRUE;
430 }
431 }
432
433 // If file is null, try saved file name first
434 // Returns TRUE if succeeds.
435 bool wxTextCtrl::SaveFile(const wxString& file)
436 {
437 wxString theFile(file);
438 if (theFile == "")
439 theFile = m_fileName;
440 if (theFile == "")
441 return FALSE;
442 m_fileName = theFile;
443
444 Widget textWidget = (Widget) m_mainWidget;
445 FILE *fp;
446
447 if (!(fp = fopen ((char*) (const char*) theFile, "w")))
448 {
449 return FALSE;
450 }
451 else
452 {
453 char *text = XmTextGetString (textWidget);
454 long len = XmTextGetLastPosition (textWidget);
455
456 if (fwrite (text, sizeof (char), len, fp) != (size_t) len)
457 {
458 // Did not write whole file
459 }
460 // Make sure newline terminates the file
461 if (text[len - 1] != '\n')
462 fputc ('\n', fp);
463
464 fclose (fp);
465 XtFree (text);
466 m_modified = FALSE;
467 return TRUE;
468 }
469 }
470
471 void wxTextCtrl::WriteText(const wxString& text)
472 {
473 long textPosition = GetInsertionPoint() + strlen (text);
474 XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), (char*) (const char*) text);
475 XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL);
476 SetInsertionPoint(textPosition);
477 XmTextShowPosition ((Widget) m_mainWidget, textPosition);
478 m_modified = TRUE;
479 }
480
481 void wxTextCtrl::AppendText(const wxString& text)
482 {
483 long textPosition = GetLastPosition() + strlen(text);
484 XmTextInsert ((Widget) m_mainWidget, GetLastPosition(), (char*) (const char*) text);
485 XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL);
486 SetInsertionPoint(textPosition);
487 XmTextShowPosition ((Widget) m_mainWidget, textPosition);
488 m_modified = TRUE;
489 }
490
491 void wxTextCtrl::Clear()
492 {
493 XmTextSetString ((Widget) m_mainWidget, "");
494 m_modified = FALSE;
495 }
496
497 bool wxTextCtrl::IsModified() const
498 {
499 return m_modified;
500 }
501
502 // Makes 'unmodified'
503 void wxTextCtrl::DiscardEdits()
504 {
505 m_modified = FALSE;
506 }
507
508 int wxTextCtrl::GetNumberOfLines() const
509 {
510 // HIDEOUSLY inefficient, but we have no choice.
511 char *s = XmTextGetString ((Widget) m_mainWidget);
512 if (s)
513 {
514 long i = 0;
515 int currentLine = 0;
516 bool finished = FALSE;
517 while (!finished)
518 {
519 int ch = s[i];
520 if (ch == '\n')
521 {
522 currentLine++;
523 i++;
524 }
525 else if (ch == 0)
526 {
527 finished = TRUE;
528 }
529 else
530 i++;
531 }
532
533 XtFree (s);
534 return currentLine;
535 }
536 return 0;
537 }
538
539 long wxTextCtrl::XYToPosition(long x, long y) const
540 {
541 /* It seems, that there is a bug in some versions of the Motif library,
542 so the original wxWin-Code doesn't work. */
543 /*
544 Widget textWidget = (Widget) handle;
545 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
546 */
547 /* Now a little workaround: */
548 long r=0;
549 for (int i=0; i<y; i++) r+=(GetLineLength(i)+1);
550 return r+x;
551 }
552
553 bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
554 {
555 Position xx, yy;
556 XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy);
557 if ( x )
558 *x = xx;
559 if ( y )
560 *y = yy;
561
562 return TRUE;
563 }
564
565 void wxTextCtrl::ShowPosition(long pos)
566 {
567 XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos);
568 }
569
570 int wxTextCtrl::GetLineLength(long lineNo) const
571 {
572 wxString str = GetLineText (lineNo);
573 return (int) str.Length();
574 }
575
576 wxString wxTextCtrl::GetLineText(long lineNo) const
577 {
578 // HIDEOUSLY inefficient, but we have no choice.
579 char *s = XmTextGetString ((Widget) m_mainWidget);
580
581 if (s)
582 {
583 wxString buf("");
584 long i;
585 int currentLine = 0;
586 for (i = 0; currentLine != lineNo && s[i]; i++ )
587 if (s[i] == '\n')
588 currentLine++;
589 // Now get the text
590 int j;
591 for (j = 0; s[i] && s[i] != '\n'; i++, j++ )
592 buf += s[i];
593
594 XtFree(s);
595 return buf;
596 }
597 else
598 return wxEmptyString;
599 }
600
601 /*
602 * Text item
603 */
604
605 void wxTextCtrl::Command(wxCommandEvent & event)
606 {
607 SetValue (event.GetString());
608 ProcessCommand (event);
609 }
610
611 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
612 {
613 // By default, load the first file into the text window.
614 if (event.GetNumberOfFiles() > 0)
615 {
616 LoadFile(event.GetFiles()[0]);
617 }
618 }
619
620 void wxTextCtrl::OnChar(wxKeyEvent& event)
621 {
622 // Indicates that we should generate a normal command, because
623 // we're letting default behaviour happen (otherwise it's vetoed
624 // by virtue of overriding OnChar)
625 m_processedDefault = TRUE;
626
627 if (m_tempCallbackStruct)
628 {
629 XmTextVerifyCallbackStruct *textStruct =
630 (XmTextVerifyCallbackStruct *) m_tempCallbackStruct;
631 textStruct->doit = True;
632 if (isascii(event.m_keyCode) && (textStruct->text->length == 1))
633 {
634 textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode);
635 }
636 }
637 }
638
639 void wxTextCtrl::ChangeFont(bool keepOriginalSize)
640 {
641 wxWindow::ChangeFont(keepOriginalSize);
642 }
643
644 void wxTextCtrl::ChangeBackgroundColour()
645 {
646 wxWindow::ChangeBackgroundColour();
647
648 /* TODO: should scrollbars be affected? Should probably have separate
649 * function to change them (by default, taken from wxSystemSettings)
650 */
651 if (m_windowStyle & wxTE_MULTILINE)
652 {
653 Widget parent = XtParent ((Widget) m_mainWidget);
654 Widget hsb, vsb;
655
656 XtVaGetValues (parent,
657 XmNhorizontalScrollBar, &hsb,
658 XmNverticalScrollBar, &vsb,
659 NULL);
660 wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE);
661 if (hsb)
662 DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE);
663 if (vsb)
664 DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE);
665
666 DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE);
667 }
668 }
669
670 void wxTextCtrl::ChangeForegroundColour()
671 {
672 wxWindow::ChangeForegroundColour();
673
674 if (m_windowStyle & wxTE_MULTILINE)
675 {
676 Widget parent = XtParent ((Widget) m_mainWidget);
677 Widget hsb, vsb;
678
679 XtVaGetValues (parent,
680 XmNhorizontalScrollBar, &hsb,
681 XmNverticalScrollBar, &vsb,
682 NULL);
683
684 /* TODO: should scrollbars be affected? Should probably have separate
685 * function to change them (by default, taken from wxSystemSettings)
686 if (hsb)
687 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
688 if (vsb)
689 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
690 */
691 DoChangeForegroundColour((WXWidget) parent, m_foregroundColour);
692 }
693 }
694
695 void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode)
696 {
697 // we're in process of updating the text control
698 m_tempCallbackStruct = wxcbs;
699
700 XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs;
701
702 wxKeyEvent event (wxEVT_CHAR);
703 event.SetId(GetId());
704 event.m_keyCode = keycode;
705 event.SetEventObject(this);
706
707 // Only if wxTextCtrl::OnChar is called will this be set to True (and
708 // the character passed through)
709 cbs->doit = False;
710
711 GetEventHandler()->ProcessEvent(event);
712
713 if ( !InSetValue() && m_processedDefault )
714 {
715 // Can generate a command
716 wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId());
717 commandEvent.SetEventObject(this);
718 ProcessCommand(commandEvent);
719 }
720
721 // do it after the (user) event handlers processed the events because
722 // otherwise GetValue() would return incorrect (not yet updated value)
723 m_tempCallbackStruct = NULL;
724 }
725
726 // ----------------------------------------------------------------------------
727 // helpers and Motif callbacks
728 // ----------------------------------------------------------------------------
729
730 static void MergeChangesIntoString(wxString& value,
731 XmTextVerifyCallbackStruct *cbs)
732 {
733 /* _sm_
734 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
735 * every event as a replace event. cbs->text->ptr gives the replacement
736 * text, cbs->startPos gives the index of the first char affected by the
737 * replace, and cbs->endPos gives the index one more than the last char
738 * affected by the replace (startPos == endPos implies an empty range).
739 * Hence, a deletion is represented by replacing all input text with a
740 * blank string ("", *not* NULL!). A simple insertion that does not
741 * overwrite any text has startPos == endPos.
742 */
743
744 if ( !value )
745 {
746 // easy case: the ol value was empty
747 value = cbs->text->ptr;
748 }
749 else
750 {
751 // merge the changes into the value
752 const char * const passwd = value;
753 int len = value.length();
754
755 len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL
756 len -= cbs->endPos - cbs->startPos; // - text from affected region.
757
758 char * newS = new char [len];
759 char * dest = newS,
760 * insert = cbs->text->ptr;
761
762 // Copy (old) text from passwd, up to the start posn of the change.
763 int i;
764 const char * p = passwd;
765 for (i = 0; i < cbs->startPos; ++i)
766 *dest++ = *p++;
767
768 // Copy the text to be inserted).
769 while (*insert)
770 *dest++ = *insert++;
771
772 // Finally, copy into newS any remaining text from passwd[endPos] on.
773 for (p = passwd + cbs->endPos; *p; )
774 *dest++ = *p++;
775 *dest = 0;
776
777 value = newS;
778
779 delete[] newS;
780 }
781 }
782
783 static void
784 wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr))
785 {
786 if (!wxGetWindowFromTable(w))
787 // Widget has been deleted!
788 return;
789
790 wxTextCtrl *tw = (wxTextCtrl *) clientData;
791 tw->SetModified(TRUE);
792 }
793
794 static void
795 wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs)
796 {
797 wxTextCtrl *tw = (wxTextCtrl *) clientData;
798 tw->m_processedDefault = FALSE;
799
800 // First, do some stuff if it's a password control: in this case, we need
801 // to store the string inside the class because GetValue() can't retrieve
802 // it from the text ctrl. We do *not* do it in other circumstances because
803 // it would double the amount of memory needed.
804
805 if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD )
806 {
807 MergeChangesIntoString(tw->m_value, cbs);
808
809 if ( cbs->text->length > 0 )
810 {
811 int i;
812 for (i = 0; i < cbs->text->length; ++i)
813 cbs->text->ptr[i] = '*';
814 cbs->text->ptr[i] = '\0';
815 }
816 }
817
818 // If we're already within an OnChar, return: probably a programmatic
819 // insertion.
820 if (tw->m_tempCallbackStruct)
821 return;
822
823 // Check for a backspace
824 if (cbs->startPos == (cbs->currInsert - 1))
825 {
826 tw->DoSendEvents((void *)cbs, WXK_DELETE);
827
828 return;
829 }
830
831 // Pasting operation: let it through without calling OnChar
832 if (cbs->text->length > 1)
833 return;
834
835 // Something other than text
836 if (cbs->text->ptr == NULL)
837 return;
838
839 // normal key press
840 char ch = cbs->text->ptr[0];
841 tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch);
842 }
843
844 static void
845 wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs))
846 {
847 if (!wxGetWindowFromTable(w))
848 return;
849
850 wxTextCtrl *tw = (wxTextCtrl *) clientData;
851 wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId());
852 event.SetEventObject(tw);
853 tw->GetEventHandler()->ProcessEvent(event);
854 }
855
856 static void
857 wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs))
858 {
859 if (!wxGetWindowFromTable(w))
860 return;
861
862 wxTextCtrl *tw = (wxTextCtrl *) clientData;
863 wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId());
864 event.SetEventObject(tw);
865 tw->GetEventHandler()->ProcessEvent(event);
866 }
867
868 static void wxTextWindowActivateProc(Widget w, XtPointer clientData,
869 XmAnyCallbackStruct *WXUNUSED(ptr))
870 {
871 if (!wxGetWindowFromTable(w))
872 return;
873
874 wxTextCtrl *tw = (wxTextCtrl *) clientData;
875
876 if (tw->InSetValue())
877 return;
878
879 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER);
880 event.SetId(tw->GetId());
881 event.SetEventObject(tw);
882 tw->ProcessCommand(event);
883 }
884
885 void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event))
886 {
887 Cut();
888 }
889
890 void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event))
891 {
892 Copy();
893 }
894
895 void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event))
896 {
897 Paste();
898 }
899
900 void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event))
901 {
902 Undo();
903 }
904
905 void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event))
906 {
907 Redo();
908 }
909
910 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event)
911 {
912 event.Enable( CanCut() );
913 }
914
915 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event)
916 {
917 event.Enable( CanCopy() );
918 }
919
920 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event)
921 {
922 event.Enable( CanPaste() );
923 }
924
925 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event)
926 {
927 event.Enable( CanUndo() );
928 }
929
930 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event)
931 {
932 event.Enable( CanRedo() );
933 }