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