1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "textctrl.h"
24 #include <sys/types.h>
29 #include "wx/textctrl.h"
30 #include "wx/settings.h"
31 #include "wx/filefn.h"
36 #include "wx/motif/private.h"
38 // ----------------------------------------------------------------------------
40 // ----------------------------------------------------------------------------
42 // helper: inserts the new text in the value of the text ctrl and returns the
44 static void MergeChangesIntoString(wxString
& value
,
45 XmTextVerifyCallbackStruct
*textStruct
);
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
);
54 #if !USE_SHARED_LIBRARY
55 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
57 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
58 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
59 EVT_CHAR(wxTextCtrl::OnChar
)
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
)
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
)
76 // ============================================================================
78 // ============================================================================
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
85 wxTextCtrl::wxTextCtrl()
87 m_tempCallbackStruct
= (void*) NULL
;
89 m_processedDefault
= FALSE
;
92 bool wxTextCtrl::Create(wxWindow
*parent
,
94 const wxString
& value
,
98 const wxValidator
& validator
,
101 m_tempCallbackStruct
= (void*) NULL
;
103 m_processedDefault
= FALSE
;
104 // m_backgroundColour = parent->GetBackgroundColour();
105 m_backgroundColour
= * wxWHITE
;
106 m_foregroundColour
= parent
->GetForegroundColour();
109 SetValidator(validator
);
111 parent
->AddChild(this);
113 m_windowStyle
= style
;
116 m_windowId
= (int)NewControlId();
120 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
122 bool wantHorizScrolling
= ((m_windowStyle
& wxHSCROLL
) != 0);
124 // If we don't have horizontal scrollbars, we want word wrap.
125 bool wantWordWrap
= !wantHorizScrolling
;
127 if (m_windowStyle
& wxTE_MULTILINE
)
130 XtSetArg (args
[0], XmNscrollHorizontal
, wantHorizScrolling
? True
: False
);
131 XtSetArg (args
[1], XmNwordWrap
, wantWordWrap
? True
: False
);
133 m_mainWidget
= (WXWidget
) XmCreateScrolledText(parentWidget
,
137 XtVaSetValues ((Widget
) m_mainWidget
,
138 XmNeditable
, ((style
& wxTE_READONLY
) ? False
: True
),
139 XmNeditMode
, XmMULTI_LINE_EDIT
,
141 XtManageChild ((Widget
) m_mainWidget
);
145 m_mainWidget
= (WXWidget
)XtVaCreateManagedWidget
153 // TODO: Is this relevant? What does it do?
155 if (!value
.IsNull() && (value
.Length() > (unsigned int) noCols
))
156 noCols
= value
.Length();
157 XtVaSetValues((Widget
) m_mainWidget
,
162 // remove border if asked for
163 if ( style
& wxNO_BORDER
)
165 XtVaSetValues((Widget
)m_mainWidget
,
166 XmNshadowThickness
, 0,
171 XmTextSetString ((Widget
) m_mainWidget
, (char*)value
.c_str());
174 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
176 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
178 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
180 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
182 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
185 m_font
= parent
->GetFont();
188 SetCanAddEventHandler(TRUE
);
189 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
191 ChangeBackgroundColour();
196 WXWidget
wxTextCtrl::GetTopWidget() const
198 return ((m_windowStyle
& wxTE_MULTILINE
) ? (WXWidget
) XtParent((Widget
) m_mainWidget
) : m_mainWidget
);
201 wxString
wxTextCtrl::GetValue() const
203 wxString str
; // result
205 if (m_windowStyle
& wxTE_PASSWORD
)
207 // the value is stored always in m_value because it can't be retrieved
208 // from the text control
213 // just get the string from Motif
214 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
220 //else: return empty string
222 if ( m_tempCallbackStruct
)
224 // the string in the control isn't yet updated, can't use it as is
225 MergeChangesIntoString(str
, (XmTextVerifyCallbackStruct
*)
226 m_tempCallbackStruct
);
233 void wxTextCtrl::SetValue(const wxString
& value
)
237 XmTextSetString ((Widget
) m_mainWidget
, (char*)value
.c_str());
239 m_inSetValue
= FALSE
;
242 // Clipboard operations
243 void wxTextCtrl::Copy()
245 XmTextCopy((Widget
) m_mainWidget
, CurrentTime
);
248 void wxTextCtrl::Cut()
250 XmTextCut((Widget
) m_mainWidget
, CurrentTime
);
253 void wxTextCtrl::Paste()
255 XmTextPaste((Widget
) m_mainWidget
);
258 bool wxTextCtrl::CanCopy() const
260 // Can copy if there's a selection
262 GetSelection(& from
, & to
);
263 return (from
!= to
) ;
266 bool wxTextCtrl::CanCut() const
268 // Can cut if there's a selection
270 GetSelection(& from
, & to
);
271 return (from
!= to
) ;
274 bool wxTextCtrl::CanPaste() const
276 return IsEditable() ;
280 void wxTextCtrl::Undo()
282 // Not possible in Motif
285 void wxTextCtrl::Redo()
287 // Not possible in Motif
290 bool wxTextCtrl::CanUndo() const
296 bool wxTextCtrl::CanRedo() const
302 // If the return values from and to are the same, there is no
304 void wxTextCtrl::GetSelection(long* from
, long* to
) const
306 XmTextPosition left
, right
;
308 XmTextGetSelectionPosition((Widget
) m_mainWidget
, & left
, & right
);
314 bool wxTextCtrl::IsEditable() const
316 return (XmTextGetEditable((Widget
) m_mainWidget
) != 0);
319 void wxTextCtrl::SetEditable(bool editable
)
321 XmTextSetEditable((Widget
) m_mainWidget
, (Boolean
) editable
);
324 void wxTextCtrl::SetInsertionPoint(long pos
)
326 XmTextSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
329 void wxTextCtrl::SetInsertionPointEnd()
331 long pos
= GetLastPosition();
332 SetInsertionPoint(pos
);
335 long wxTextCtrl::GetInsertionPoint() const
337 return (long) XmTextGetInsertionPosition ((Widget
) m_mainWidget
);
340 long wxTextCtrl::GetLastPosition() const
342 return (long) XmTextGetLastPosition ((Widget
) m_mainWidget
);
345 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
347 XmTextReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
348 (char*) (const char*) value
);
351 void wxTextCtrl::Remove(long from
, long to
)
353 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
355 XmTextRemove ((Widget
) m_mainWidget
);
358 void wxTextCtrl::SetSelection(long from
, long to
)
360 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
364 bool wxTextCtrl::LoadFile(const wxString
& file
)
366 if (!wxFileExists(file
))
373 Widget textWidget
= (Widget
) m_mainWidget
;
377 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
378 !(fp
= fopen ((char*) (const char*) file
, "r")))
384 long len
= statb
.st_size
;
386 if (!(text
= XtMalloc ((unsigned) (len
+ 1))))
391 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
397 XmTextSetString (textWidget
, text
);
398 // m_textPosition = len;
405 // If file is null, try saved file name first
406 // Returns TRUE if succeeds.
407 bool wxTextCtrl::SaveFile(const wxString
& file
)
409 wxString
theFile(file
);
411 theFile
= m_fileName
;
414 m_fileName
= theFile
;
416 Widget textWidget
= (Widget
) m_mainWidget
;
419 if (!(fp
= fopen ((char*) (const char*) theFile
, "w")))
425 char *text
= XmTextGetString (textWidget
);
426 long len
= XmTextGetLastPosition (textWidget
);
428 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
430 // Did not write whole file
432 // Make sure newline terminates the file
433 if (text
[len
- 1] != '\n')
443 void wxTextCtrl::WriteText(const wxString
& text
)
445 long textPosition
= GetInsertionPoint() + strlen (text
);
446 XmTextInsert ((Widget
) m_mainWidget
, GetInsertionPoint(), (char*) (const char*) text
);
447 XtVaSetValues ((Widget
) m_mainWidget
, XmNcursorPosition
, textPosition
, NULL
);
448 SetInsertionPoint(textPosition
);
449 XmTextShowPosition ((Widget
) m_mainWidget
, textPosition
);
453 void wxTextCtrl::AppendText(const wxString
& text
)
455 long textPosition
= GetLastPosition() + strlen(text
);
456 XmTextInsert ((Widget
) m_mainWidget
, GetLastPosition(), (char*) (const char*) text
);
457 XtVaSetValues ((Widget
) m_mainWidget
, XmNcursorPosition
, textPosition
, NULL
);
458 SetInsertionPoint(textPosition
);
459 XmTextShowPosition ((Widget
) m_mainWidget
, textPosition
);
463 void wxTextCtrl::Clear()
465 XmTextSetString ((Widget
) m_mainWidget
, "");
469 bool wxTextCtrl::IsModified() const
474 // Makes 'unmodified'
475 void wxTextCtrl::DiscardEdits()
480 int wxTextCtrl::GetNumberOfLines() const
482 // HIDEOUSLY inefficient, but we have no choice.
483 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
488 bool finished
= FALSE
;
511 long wxTextCtrl::XYToPosition(long x
, long y
) const
513 /* It seems, that there is a bug in some versions of the Motif library,
514 so the original wxWin-Code doesn't work. */
516 Widget textWidget = (Widget) handle;
517 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
519 /* Now a little workaround: */
521 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
525 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
528 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
537 void wxTextCtrl::ShowPosition(long pos
)
539 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
542 int wxTextCtrl::GetLineLength(long lineNo
) const
544 wxString str
= GetLineText (lineNo
);
545 return (int) str
.Length();
548 wxString
wxTextCtrl::GetLineText(long lineNo
) const
550 // HIDEOUSLY inefficient, but we have no choice.
551 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
558 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
563 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
570 return wxEmptyString
;
577 void wxTextCtrl::Command(wxCommandEvent
& event
)
579 SetValue (event
.GetString());
580 ProcessCommand (event
);
583 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
585 // By default, load the first file into the text window.
586 if (event
.GetNumberOfFiles() > 0)
588 LoadFile(event
.GetFiles()[0]);
592 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
594 // Indicates that we should generate a normal command, because
595 // we're letting default behaviour happen (otherwise it's vetoed
596 // by virtue of overriding OnChar)
597 m_processedDefault
= TRUE
;
599 if (m_tempCallbackStruct
)
601 XmTextVerifyCallbackStruct
*textStruct
=
602 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
603 textStruct
->doit
= True
;
604 if (isascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
606 textStruct
->text
->ptr
[0] = ((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
611 void wxTextCtrl::ChangeFont(bool keepOriginalSize
)
613 wxWindow::ChangeFont(keepOriginalSize
);
616 void wxTextCtrl::ChangeBackgroundColour()
618 wxWindow::ChangeBackgroundColour();
620 /* TODO: should scrollbars be affected? Should probably have separate
621 * function to change them (by default, taken from wxSystemSettings)
623 if (m_windowStyle
& wxTE_MULTILINE
)
625 Widget parent
= XtParent ((Widget
) m_mainWidget
);
628 XtVaGetValues (parent
,
629 XmNhorizontalScrollBar
, &hsb
,
630 XmNverticalScrollBar
, &vsb
,
632 wxColour backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
634 DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
636 DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
638 DoChangeBackgroundColour((WXWidget
) parent
, m_backgroundColour
, TRUE
);
642 void wxTextCtrl::ChangeForegroundColour()
644 wxWindow::ChangeForegroundColour();
646 if (m_windowStyle
& wxTE_MULTILINE
)
648 Widget parent
= XtParent ((Widget
) m_mainWidget
);
651 XtVaGetValues (parent
,
652 XmNhorizontalScrollBar
, &hsb
,
653 XmNverticalScrollBar
, &vsb
,
656 /* TODO: should scrollbars be affected? Should probably have separate
657 * function to change them (by default, taken from wxSystemSettings)
659 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
661 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
663 DoChangeForegroundColour((WXWidget
) parent
, m_foregroundColour
);
667 void wxTextCtrl::DoSendEvents(void *wxcbs
, long keycode
)
669 // we're in process of updating the text control
670 m_tempCallbackStruct
= wxcbs
;
672 XmTextVerifyCallbackStruct
*cbs
= (XmTextVerifyCallbackStruct
*)wxcbs
;
674 wxKeyEvent
event (wxEVT_CHAR
);
675 event
.SetId(GetId());
676 event
.m_keyCode
= keycode
;
677 event
.SetEventObject(this);
679 // Only if wxTextCtrl::OnChar is called will this be set to True (and
680 // the character passed through)
683 GetEventHandler()->ProcessEvent(event
);
685 if ( !InSetValue() && m_processedDefault
)
687 // Can generate a command
688 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
689 commandEvent
.SetEventObject(this);
690 ProcessCommand(commandEvent
);
693 // do it after the (user) event handlers processed the events because
694 // otherwise GetValue() would return incorrect (not yet updated value)
695 m_tempCallbackStruct
= NULL
;
698 // ----------------------------------------------------------------------------
699 // helpers and Motif callbacks
700 // ----------------------------------------------------------------------------
702 static void MergeChangesIntoString(wxString
& value
,
703 XmTextVerifyCallbackStruct
*cbs
)
706 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
707 * every event as a replace event. cbs->text->ptr gives the replacement
708 * text, cbs->startPos gives the index of the first char affected by the
709 * replace, and cbs->endPos gives the index one more than the last char
710 * affected by the replace (startPos == endPos implies an empty range).
711 * Hence, a deletion is represented by replacing all input text with a
712 * blank string ("", *not* NULL!). A simple insertion that does not
713 * overwrite any text has startPos == endPos.
718 // easy case: the ol value was empty
719 value
= cbs
->text
->ptr
;
723 // merge the changes into the value
724 const char * const passwd
= value
;
725 int len
= value
.length();
727 len
+= strlen(cbs
->text
->ptr
) + 1; // + new text (if any) + NUL
728 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
730 char * newS
= new char [len
];
732 * insert
= cbs
->text
->ptr
;
734 // Copy (old) text from passwd, up to the start posn of the change.
736 const char * p
= passwd
;
737 for (i
= 0; i
< cbs
->startPos
; ++i
)
740 // Copy the text to be inserted).
744 // Finally, copy into newS any remaining text from passwd[endPos] on.
745 for (p
= passwd
+ cbs
->endPos
; *p
; )
756 wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer ptr
)
758 if (!wxGetWindowFromTable(w
))
759 // Widget has been deleted!
762 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
763 tw
->SetModified(TRUE
);
767 wxTextWindowModifyProc (Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
769 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
770 tw
->m_processedDefault
= FALSE
;
772 // First, do some stuff if it's a password control: in this case, we need
773 // to store the string inside the class because GetValue() can't retrieve
774 // it from the text ctrl. We do *not* do it in other circumstances because
775 // it would double the amount of memory needed.
777 if ( tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
779 MergeChangesIntoString(tw
->m_value
, cbs
);
781 if ( cbs
->text
->length
> 0 )
784 for (i
= 0; i
< cbs
->text
->length
; ++i
)
785 cbs
->text
->ptr
[i
] = '*';
786 cbs
->text
->ptr
[i
] = '\0';
790 // If we're already within an OnChar, return: probably a programmatic
792 if (tw
->m_tempCallbackStruct
)
795 // Check for a backspace
796 if (cbs
->startPos
== (cbs
->currInsert
- 1))
798 tw
->DoSendEvents((void *)cbs
, WXK_DELETE
);
803 // Pasting operation: let it through without calling OnChar
804 if (cbs
->text
->length
> 1)
807 // Something other than text
808 if (cbs
->text
->ptr
== NULL
)
812 char ch
= cbs
->text
->ptr
[0];
813 tw
->DoSendEvents((void *)cbs
, ch
== '\n' ? '\r' : ch
);
817 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
819 if (!wxGetWindowFromTable(w
))
822 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
823 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
824 event
.SetEventObject(tw
);
825 tw
->GetEventHandler()->ProcessEvent(event
);
829 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
831 if (!wxGetWindowFromTable(w
))
834 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
835 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
836 event
.SetEventObject(tw
);
837 tw
->GetEventHandler()->ProcessEvent(event
);
840 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
841 XmAnyCallbackStruct
*ptr
)
843 if (!wxGetWindowFromTable(w
))
846 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
848 if (tw
->InSetValue())
851 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
);
852 event
.SetId(tw
->GetId());
853 event
.SetEventObject(tw
);
854 tw
->ProcessCommand(event
);
857 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
862 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
867 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
872 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
877 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
882 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
884 event
.Enable( CanCut() );
887 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
889 event
.Enable( CanCopy() );
892 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
894 event
.Enable( CanPaste() );
897 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
899 event
.Enable( CanUndo() );
902 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
904 event
.Enable( CanRedo() );