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