1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "textctrl.h"
16 #include <sys/types.h>
20 #include "wx/textctrl.h"
21 #include "wx/settings.h"
22 #include "wx/filefn.h"
25 #if defined(__BORLANDC__) && !defined(__WIN32__)
34 #include <sys/types.h>
38 #include "wx/motif/private.h"
41 wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer ptr
);
43 wxTextWindowModifyProc (Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
);
45 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
47 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
48 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
49 XmAnyCallbackStruct
*ptr
);
51 #if !USE_SHARED_LIBRARY
52 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
54 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
55 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
56 EVT_CHAR(wxTextCtrl::OnChar
)
61 wxTextCtrl::wxTextCtrl()
62 #ifndef NO_TEXT_WINDOW_STREAM
67 m_tempCallbackStruct
= (void*) NULL
;
69 m_processedDefault
= FALSE
;
72 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
73 const wxString
& value
,
75 const wxSize
& size
, long style
,
76 const wxValidator
& validator
,
79 m_tempCallbackStruct
= (void*) NULL
;
81 m_processedDefault
= FALSE
;
85 SetValidator(validator
);
86 if (parent
) parent
->AddChild(this);
88 m_windowStyle
= style
;
91 m_windowId
= (int)NewControlId();
95 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
97 bool wantHorizScrolling
= ((m_windowStyle
& wxHSCROLL
) != 0);
99 // If we don't have horizontal scrollbars, we want word wrap.
100 bool wantWordWrap
= !wantHorizScrolling
;
102 if (m_windowStyle
& wxTE_MULTILINE
)
105 XtSetArg (args
[0], XmNscrollHorizontal
, wantHorizScrolling
? True
: False
);
106 XtSetArg (args
[1], XmNwordWrap
, wantWordWrap
? True
: False
);
108 m_mainWidget
= (WXWidget
) XmCreateScrolledText (parentWidget
, (char*) (const char*) name
, args
, 2);
110 XtVaSetValues ((Widget
) m_mainWidget
,
111 XmNeditable
, ((style
& wxTE_READONLY
) ? False
: True
),
112 XmNeditMode
, XmMULTI_LINE_EDIT
,
114 XtManageChild ((Widget
) m_mainWidget
);
118 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) name
,
119 xmTextWidgetClass
, parentWidget
,
122 // TODO: Is this relevant? What does it do?
124 if (!value
.IsNull() && (value
.Length() > (unsigned int) noCols
))
125 noCols
= value
.Length();
126 XtVaSetValues ((Widget
) m_mainWidget
,
132 XmTextSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
134 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
136 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
138 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
140 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
142 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
144 SetCanAddEventHandler(TRUE
);
145 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
147 SetFont(* parent
->GetFont());
148 ChangeColour(m_mainWidget
);
153 WXWidget
wxTextCtrl::GetTopWidget() const
155 return ((m_windowStyle
& wxTE_MULTILINE
) ? (WXWidget
) XtParent((Widget
) m_mainWidget
) : m_mainWidget
);
158 wxString
wxTextCtrl::GetValue() const
160 if (m_windowStyle
& wxTE_PASSWORD
)
164 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
173 return wxEmptyString
;
178 void wxTextCtrl::SetValue(const wxString
& value
)
180 wxASSERT_MSG( (!value
.IsNull()), "Must not pass a null string to wxTextCtrl::SetValue." ) ;
183 XmTextSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
185 m_inSetValue
= FALSE
;
188 // Clipboard operations
189 void wxTextCtrl::Copy()
191 XmTextCopy((Widget
) m_mainWidget
, CurrentTime
);
194 void wxTextCtrl::Cut()
196 XmTextCut((Widget
) m_mainWidget
, CurrentTime
);
199 void wxTextCtrl::Paste()
201 XmTextPaste((Widget
) m_mainWidget
);
204 void wxTextCtrl::SetEditable(bool editable
)
206 XmTextSetEditable((Widget
) m_mainWidget
, (Boolean
) editable
);
209 void wxTextCtrl::SetInsertionPoint(long pos
)
211 XmTextSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
214 void wxTextCtrl::SetInsertionPointEnd()
216 long pos
= GetLastPosition();
217 SetInsertionPoint(pos
);
220 long wxTextCtrl::GetInsertionPoint() const
222 return (long) XmTextGetInsertionPosition ((Widget
) m_mainWidget
);
225 long wxTextCtrl::GetLastPosition() const
227 return (long) XmTextGetLastPosition ((Widget
) m_mainWidget
);
230 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
232 XmTextReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
233 (char*) (const char*) value
);
236 void wxTextCtrl::Remove(long from
, long to
)
238 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
240 XmTextRemove ((Widget
) m_mainWidget
);
243 void wxTextCtrl::SetSelection(long from
, long to
)
245 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
249 bool wxTextCtrl::LoadFile(const wxString
& file
)
251 if (!wxFileExists(file
))
258 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
262 struct stat stat_buf
;
263 if (stat(file
, &stat_buf
) < 0)
265 // This may need to be a bigger buffer than the file size suggests,
266 // if it's a UNIX file. Give it an extra 1000 just in case.
267 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
270 while (!input
.eof() && input
.peek() != EOF
)
272 input
.getline(wxBuffer
, 500);
273 int len
= strlen(wxBuffer
);
275 wxBuffer
[len
+1] = 10;
277 strcpy(tmp_buffer
+pos
, wxBuffer
);
278 pos
+= strlen(wxBuffer
);
291 // If file is null, try saved file name first
292 // Returns TRUE if succeeds.
293 bool wxTextCtrl::SaveFile(const wxString
& file
)
295 wxString
theFile(file
);
297 theFile
= m_fileName
;
300 m_fileName
= theFile
;
302 ofstream
output((char*) (const char*) theFile
);
306 // TODO get and save text
311 void wxTextCtrl::WriteText(const wxString
& text
)
313 long textPosition
= GetInsertionPoint() + strlen (text
);
314 XmTextInsert ((Widget
) m_mainWidget
, GetInsertionPoint(), (char*) (const char*) text
);
315 XtVaSetValues ((Widget
) m_mainWidget
, XmNcursorPosition
, textPosition
, NULL
);
316 SetInsertionPoint(textPosition
);
317 XmTextShowPosition ((Widget
) m_mainWidget
, textPosition
);
321 void wxTextCtrl::Clear()
323 XmTextSetString ((Widget
) m_mainWidget
, "");
327 bool wxTextCtrl::IsModified() const
332 // Makes 'unmodified'
333 void wxTextCtrl::DiscardEdits()
335 XmTextSetString ((Widget
) m_mainWidget
, "");
339 int wxTextCtrl::GetNumberOfLines() const
341 // HIDEOUSLY inefficient, but we have no choice.
342 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
347 bool finished
= FALSE
;
370 long wxTextCtrl::XYToPosition(long x
, long y
) const
372 /* It seems, that there is a bug in some versions of the Motif library,
373 so the original wxWin-Code doesn't work. */
375 Widget textWidget = (Widget) handle;
376 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
378 /* Now a little workaround: */
380 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
384 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
387 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
391 void wxTextCtrl::ShowPosition(long pos
)
393 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
396 int wxTextCtrl::GetLineLength(long lineNo
) const
398 wxString str
= GetLineText (lineNo
);
399 return (int) str
.Length();
402 wxString
wxTextCtrl::GetLineText(long lineNo
) const
404 // HIDEOUSLY inefficient, but we have no choice.
405 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
412 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
417 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
424 return wxEmptyString
;
431 void wxTextCtrl::Command(wxCommandEvent
& event
)
433 SetValue (event
.GetString());
434 ProcessCommand (event
);
437 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
439 // By default, load the first file into the text window.
440 if (event
.GetNumberOfFiles() > 0)
442 LoadFile(event
.GetFiles()[0]);
446 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
447 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
449 //=========================================================================
450 // Called then the buffer is full (gcc 2.6.3)
451 // or when "endl" is output (Borland 4.5)
452 //=========================================================================
453 // Class declaration using multiple inheritance doesn't work properly for
454 // Borland. See note in wb_text.h.
455 #ifndef NO_TEXT_WINDOW_STREAM
456 int wxTextCtrl::overflow(int c
)
458 // Make sure there is a holding area
459 if ( allocate()==EOF
)
461 wxError("Streambuf allocation failed","Internal error");
465 // Verify that there are no characters in get area
466 if ( gptr() && gptr() < egptr() )
468 wxError("Who's trespassing my get area?","Internal error");
475 // Make sure there is a put area
478 /* This doesn't seem to be fatal so comment out error message */
479 // wxError("Put area not opened","Internal error");
480 setp( base(), base() );
483 // Determine how many characters have been inserted but no consumed
484 int plen
= pptr() - pbase();
486 // Now Jerry relies on the fact that the buffer is at least 2 chars
487 // long, but the holding area "may be as small as 1" ???
488 // And we need an additional \0, so let's keep this inefficient but
491 // If c!=EOF, it is a character that must also be comsumed
492 int xtra
= c
==EOF
? 0 : 1;
494 // Write temporary C-string to wxTextWindow
496 char *txt
= new char[plen
+xtra
+1];
497 memcpy(txt
, pbase(), plen
);
498 txt
[plen
] = (char)c
; // append c
499 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
500 // If the put area already contained \0, output will be truncated there
506 setp(pbase(), epptr());
508 #if defined(__WATCOMC__)
510 #elif defined(zapeof) // HP-UX (all cfront based?)
513 return c
!=EOF
? c
: 0; // this should make everybody happy
517 //=========================================================================
518 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
519 //=========================================================================
520 int wxTextCtrl::sync()
522 // Verify that there are no characters in get area
523 if ( gptr() && gptr() < egptr() )
525 wxError("Who's trespassing my get area?","Internal error");
529 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
533 int len = pptr() - pbase();
534 char *txt = new char[len+1];
535 strncpy(txt, pbase(), len);
538 setp(pbase(), epptr());
544 //=========================================================================
545 // Should not be called by a "ostream". Used by a "istream"
546 //=========================================================================
547 int wxTextCtrl::underflow()
553 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
559 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
562 str
.Printf("%.2f", f
);
567 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
570 str
.Printf("%.2f", d
);
575 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
583 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
586 str
.Printf("%ld", i
);
591 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
601 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
603 // Indicates that we should generate a normal command, because
604 // we're letting default behaviour happen (otherwise it's vetoed
605 // by virtue of overriding OnChar)
606 m_processedDefault
= TRUE
;
608 if (m_tempCallbackStruct
)
610 XmTextVerifyCallbackStruct
*textStruct
=
611 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
612 textStruct
->doit
= True
;
613 if (isascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
615 textStruct
->text
->ptr
[0] = ((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
620 static void wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer ptr
)
622 if (!wxGetWindowFromTable(w
))
623 // Widget has been deleted!
626 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
627 tw
->SetModified(TRUE
);
631 wxTextWindowModifyProc (Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
633 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
634 tw
->m_processedDefault
= FALSE
;
636 // First, do some stuff if it's a password control.
637 // (What does this do exactly?)
639 if (tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
642 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
643 * every event as a replace event. cbs->text->ptr gives the replacement
644 * text, cbs->startPos gives the index of the first char affected by the
645 * replace, and cbs->endPos gives the index one more than the last char
646 * affected by the replace (startPos == endPos implies an empty range).
647 * Hence, a deletion is represented by replacing all input text with a
648 * blank string ("", *not* NULL!). A simple insertion that does not
649 * overwrite any text has startPos == endPos.
652 if (tw
->m_value
.IsNull())
654 tw
->m_value
= cbs
->text
->ptr
;
658 char * passwd
= (char*) (const char*) tw
->m_value
; // Set up a more convenient alias.
660 int len
= passwd
? strlen(passwd
) : 0; // Enough room for old text
661 len
+= strlen(cbs
->text
->ptr
) + 1; // + new text (if any) + NUL
662 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
664 char * newS
= new char [len
];
665 char * p
= passwd
, * dest
= newS
, * insert
= cbs
->text
->ptr
;
667 // Copy (old) text from passwd, up to the start posn of the change.
669 for (i
= 0; i
< cbs
->startPos
; ++i
)
672 // Copy the text to be inserted).
676 // Finally, copy into newS any remaining text from passwd[endPos] on.
677 for (p
= passwd
+ cbs
->endPos
; *p
; )
686 if (cbs
->text
->length
>0)
689 for (i
= 0; i
< cbs
->text
->length
; ++i
)
690 cbs
->text
->ptr
[i
] = '*';
691 cbs
->text
->ptr
[i
] = 0;
695 // If we're already within an OnChar, return: probably
696 // a programmatic insertion.
697 if (tw
->m_tempCallbackStruct
)
700 // Check for a backspace
701 if (cbs
->startPos
== (cbs
->currInsert
- 1))
703 tw
->m_tempCallbackStruct
= (void*) cbs
;
705 wxKeyEvent
event (wxEVT_CHAR
);
706 event
.SetId(tw
->GetId());
707 event
.m_keyCode
= WXK_DELETE
;
708 event
.SetEventObject(tw
);
710 // Only if wxTextCtrl::OnChar is called
711 // will this be set to True (and the character
715 tw
->GetEventHandler()->ProcessEvent(event
);
717 tw
->m_tempCallbackStruct
= NULL
;
719 if (tw
->InSetValue())
722 if (tw
->m_processedDefault
)
724 // Can generate a command
725 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
726 commandEvent
.SetEventObject(tw
);
727 tw
->ProcessCommand(commandEvent
);
733 // Pasting operation: let it through without
735 if (cbs
->text
->length
> 1)
738 // Something other than text
739 if (cbs
->text
->ptr
== NULL
)
742 tw
->m_tempCallbackStruct
= (void*) cbs
;
744 wxKeyEvent
event (wxEVT_CHAR
);
745 event
.SetId(tw
->GetId());
746 event
.SetEventObject(tw
);
747 event
.m_keyCode
= (cbs
->text
->ptr
[0] == 10 ? 13 : cbs
->text
->ptr
[0]);
749 // Only if wxTextCtrl::OnChar is called
750 // will this be set to True (and the character
754 tw
->GetEventHandler()->ProcessEvent(event
);
756 tw
->m_tempCallbackStruct
= NULL
;
758 if (tw
->InSetValue())
761 if (tw
->m_processedDefault
)
763 // Can generate a command
764 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
765 commandEvent
.SetEventObject(tw
);
766 tw
->ProcessCommand(commandEvent
);
771 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
773 if (!wxGetWindowFromTable(w
))
776 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
777 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
778 event
.SetEventObject(tw
);
779 tw
->GetEventHandler()->ProcessEvent(event
);
783 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
785 if (!wxGetWindowFromTable(w
))
788 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
789 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
790 event
.SetEventObject(tw
);
791 tw
->GetEventHandler()->ProcessEvent(event
);
794 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
795 XmAnyCallbackStruct
*ptr
)
797 if (!wxGetWindowFromTable(w
))
800 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
803 type_event = wxEVENT_TYPE_TEXT_ENTER_COMMAND ;
806 type_event = wxEVENT_TYPE_TEXT_COMMAND ;
811 if (tw
->InSetValue())
814 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
);
815 event
.SetId(tw
->GetId());
816 event
.SetEventObject(tw
);
817 tw
->ProcessCommand(event
);