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