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
;
73 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
74 const wxString
& value
,
76 const wxSize
& size
, long style
,
77 const wxValidator
& validator
,
80 m_tempCallbackStruct
= (void*) NULL
;
82 m_processedDefault
= FALSE
;
87 SetValidator(validator
);
88 if (parent
) parent
->AddChild(this);
90 m_windowStyle
= style
;
93 m_windowId
= (int)NewControlId();
97 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
99 bool wantHorizScrolling
= ((m_windowStyle
& wxHSCROLL
) != 0);
101 // If we don't have horizontal scrollbars, we want word wrap.
102 bool wantWordWrap
= !wantHorizScrolling
;
104 if (m_windowStyle
& wxTE_MULTILINE
)
107 XtSetArg (args
[0], XmNscrollHorizontal
, wantHorizScrolling
? True
: False
);
108 XtSetArg (args
[1], XmNwordWrap
, wantWordWrap
? True
: False
);
110 m_mainWidget
= (WXWidget
) XmCreateScrolledText (parentWidget
, (char*) (const char*) name
, args
, 2);
112 XtVaSetValues ((Widget
) m_mainWidget
,
113 XmNeditable
, ((style
& wxTE_READONLY
) ? False
: True
),
114 XmNeditMode
, XmMULTI_LINE_EDIT
,
116 XtManageChild ((Widget
) m_mainWidget
);
120 m_mainWidget
= (WXWidget
) XtVaCreateManagedWidget ((char*) (const char*) name
,
121 xmTextWidgetClass
, parentWidget
,
124 // TODO: Is this relevant? What does it do?
126 if (!value
.IsNull() && (value
.Length() > (unsigned int) noCols
))
127 noCols
= value
.Length();
128 XtVaSetValues ((Widget
) m_mainWidget
,
134 XmTextSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
136 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
138 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
140 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
142 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
144 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
146 SetCanAddEventHandler(TRUE
);
147 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
149 SetFont(* parent
->GetFont());
150 ChangeColour(m_mainWidget
);
155 WXWidget
wxTextCtrl::GetTopWidget() const
157 return ((m_windowStyle
& wxTE_MULTILINE
) ? (WXWidget
) XtParent((Widget
) m_mainWidget
) : m_mainWidget
);
160 wxString
wxTextCtrl::GetValue() const
162 if (m_windowStyle
& wxTE_PASSWORD
)
166 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
175 return wxEmptyString
;
180 void wxTextCtrl::SetValue(const wxString
& value
)
182 wxASSERT_MSG( (!value
.IsNull()), "Must not pass a null string to wxTextCtrl::SetValue." ) ;
185 XmTextSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
187 m_inSetValue
= FALSE
;
190 // Clipboard operations
191 void wxTextCtrl::Copy()
193 XmTextCopy((Widget
) m_mainWidget
, CurrentTime
);
196 void wxTextCtrl::Cut()
198 XmTextCut((Widget
) m_mainWidget
, CurrentTime
);
201 void wxTextCtrl::Paste()
203 XmTextPaste((Widget
) m_mainWidget
);
206 void wxTextCtrl::SetEditable(bool editable
)
208 XmTextSetEditable((Widget
) m_mainWidget
, (Boolean
) editable
);
211 void wxTextCtrl::SetInsertionPoint(long pos
)
213 XmTextSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
216 void wxTextCtrl::SetInsertionPointEnd()
218 long pos
= GetLastPosition();
219 SetInsertionPoint(pos
);
222 long wxTextCtrl::GetInsertionPoint() const
224 return (long) XmTextGetInsertionPosition ((Widget
) m_mainWidget
);
227 long wxTextCtrl::GetLastPosition() const
229 return (long) XmTextGetLastPosition ((Widget
) m_mainWidget
);
232 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
234 XmTextReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
235 (char*) (const char*) value
);
238 void wxTextCtrl::Remove(long from
, long to
)
240 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
242 XmTextRemove ((Widget
) m_mainWidget
);
245 void wxTextCtrl::SetSelection(long from
, long to
)
247 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
251 bool wxTextCtrl::LoadFile(const wxString
& file
)
253 if (!wxFileExists(file
))
260 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
264 struct stat stat_buf
;
265 if (stat(file
, &stat_buf
) < 0)
267 // This may need to be a bigger buffer than the file size suggests,
268 // if it's a UNIX file. Give it an extra 1000 just in case.
269 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
272 while (!input
.eof() && input
.peek() != EOF
)
274 input
.getline(wxBuffer
, 500);
275 int len
= strlen(wxBuffer
);
277 wxBuffer
[len
+1] = 10;
279 strcpy(tmp_buffer
+pos
, wxBuffer
);
280 pos
+= strlen(wxBuffer
);
293 // If file is null, try saved file name first
294 // Returns TRUE if succeeds.
295 bool wxTextCtrl::SaveFile(const wxString
& file
)
297 wxString
theFile(file
);
299 theFile
= m_fileName
;
302 m_fileName
= theFile
;
304 ofstream
output((char*) (const char*) theFile
);
308 // TODO get and save text
313 void wxTextCtrl::WriteText(const wxString
& text
)
315 long textPosition
= GetInsertionPoint() + strlen (text
);
316 XmTextInsert ((Widget
) m_mainWidget
, GetInsertionPoint(), (char*) (const char*) text
);
317 XtVaSetValues ((Widget
) m_mainWidget
, XmNcursorPosition
, textPosition
, NULL
);
318 SetInsertionPoint(textPosition
);
319 XmTextShowPosition ((Widget
) m_mainWidget
, textPosition
);
323 void wxTextCtrl::Clear()
325 XmTextSetString ((Widget
) m_mainWidget
, "");
329 bool wxTextCtrl::IsModified() const
334 // Makes 'unmodified'
335 void wxTextCtrl::DiscardEdits()
337 XmTextSetString ((Widget
) m_mainWidget
, "");
341 int wxTextCtrl::GetNumberOfLines() const
343 // HIDEOUSLY inefficient, but we have no choice.
344 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
349 bool finished
= FALSE
;
372 long wxTextCtrl::XYToPosition(long x
, long y
) const
374 /* It seems, that there is a bug in some versions of the Motif library,
375 so the original wxWin-Code doesn't work. */
377 Widget textWidget = (Widget) handle;
378 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
380 /* Now a little workaround: */
382 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
386 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
389 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
393 void wxTextCtrl::ShowPosition(long pos
)
395 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
398 int wxTextCtrl::GetLineLength(long lineNo
) const
400 wxString str
= GetLineText (lineNo
);
401 return (int) str
.Length();
404 wxString
wxTextCtrl::GetLineText(long lineNo
) const
406 // HIDEOUSLY inefficient, but we have no choice.
407 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
414 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
419 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
426 return wxEmptyString
;
433 void wxTextCtrl::Command(wxCommandEvent
& event
)
435 SetValue (event
.GetString());
436 ProcessCommand (event
);
439 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
441 // By default, load the first file into the text window.
442 if (event
.GetNumberOfFiles() > 0)
444 LoadFile(event
.GetFiles()[0]);
448 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
449 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
451 //=========================================================================
452 // Called then the buffer is full (gcc 2.6.3)
453 // or when "endl" is output (Borland 4.5)
454 //=========================================================================
455 // Class declaration using multiple inheritance doesn't work properly for
456 // Borland. See note in wb_text.h.
457 #ifndef NO_TEXT_WINDOW_STREAM
458 int wxTextCtrl::overflow(int c
)
460 // Make sure there is a holding area
461 if ( allocate()==EOF
)
463 wxError("Streambuf allocation failed","Internal error");
467 // Verify that there are no characters in get area
468 if ( gptr() && gptr() < egptr() )
470 wxError("Who's trespassing my get area?","Internal error");
477 // Make sure there is a put area
480 /* This doesn't seem to be fatal so comment out error message */
481 // wxError("Put area not opened","Internal error");
482 setp( base(), base() );
485 // Determine how many characters have been inserted but no consumed
486 int plen
= pptr() - pbase();
488 // Now Jerry relies on the fact that the buffer is at least 2 chars
489 // long, but the holding area "may be as small as 1" ???
490 // And we need an additional \0, so let's keep this inefficient but
493 // If c!=EOF, it is a character that must also be comsumed
494 int xtra
= c
==EOF
? 0 : 1;
496 // Write temporary C-string to wxTextWindow
498 char *txt
= new char[plen
+xtra
+1];
499 memcpy(txt
, pbase(), plen
);
500 txt
[plen
] = (char)c
; // append c
501 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
502 // If the put area already contained \0, output will be truncated there
508 setp(pbase(), epptr());
510 #if defined(__WATCOMC__)
512 #elif defined(zapeof) // HP-UX (all cfront based?)
515 return c
!=EOF
? c
: 0; // this should make everybody happy
519 //=========================================================================
520 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
521 //=========================================================================
522 int wxTextCtrl::sync()
524 // Verify that there are no characters in get area
525 if ( gptr() && gptr() < egptr() )
527 wxError("Who's trespassing my get area?","Internal error");
531 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
535 int len = pptr() - pbase();
536 char *txt = new char[len+1];
537 strncpy(txt, pbase(), len);
540 setp(pbase(), epptr());
546 //=========================================================================
547 // Should not be called by a "ostream". Used by a "istream"
548 //=========================================================================
549 int wxTextCtrl::underflow()
555 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
561 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
564 str
.Printf("%.2f", f
);
569 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
572 str
.Printf("%.2f", d
);
577 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
585 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
588 str
.Printf("%ld", i
);
593 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
603 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
605 // Indicates that we should generate a normal command, because
606 // we're letting default behaviour happen (otherwise it's vetoed
607 // by virtue of overriding OnChar)
608 m_processedDefault
= TRUE
;
610 if (m_tempCallbackStruct
)
612 XmTextVerifyCallbackStruct
*textStruct
=
613 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
614 textStruct
->doit
= True
;
615 if (isascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
617 textStruct
->text
->ptr
[0] = ((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
622 static void wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer ptr
)
624 if (!wxGetWindowFromTable(w
))
625 // Widget has been deleted!
628 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
629 tw
->SetModified(TRUE
);
633 wxTextWindowModifyProc (Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
635 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
636 tw
->m_processedDefault
= FALSE
;
638 // First, do some stuff if it's a password control.
639 // (What does this do exactly?)
641 if (tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
644 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
645 * every event as a replace event. cbs->text->ptr gives the replacement
646 * text, cbs->startPos gives the index of the first char affected by the
647 * replace, and cbs->endPos gives the index one more than the last char
648 * affected by the replace (startPos == endPos implies an empty range).
649 * Hence, a deletion is represented by replacing all input text with a
650 * blank string ("", *not* NULL!). A simple insertion that does not
651 * overwrite any text has startPos == endPos.
654 if (tw
->m_value
.IsNull())
656 tw
->m_value
= cbs
->text
->ptr
;
660 char * passwd
= (char*) (const char*) tw
->m_value
; // Set up a more convenient alias.
662 int len
= passwd
? strlen(passwd
) : 0; // Enough room for old text
663 len
+= strlen(cbs
->text
->ptr
) + 1; // + new text (if any) + NUL
664 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
666 char * newS
= new char [len
];
667 char * p
= passwd
, * dest
= newS
, * insert
= cbs
->text
->ptr
;
669 // Copy (old) text from passwd, up to the start posn of the change.
671 for (i
= 0; i
< cbs
->startPos
; ++i
)
674 // Copy the text to be inserted).
678 // Finally, copy into newS any remaining text from passwd[endPos] on.
679 for (p
= passwd
+ cbs
->endPos
; *p
; )
688 if (cbs
->text
->length
>0)
691 for (i
= 0; i
< cbs
->text
->length
; ++i
)
692 cbs
->text
->ptr
[i
] = '*';
693 cbs
->text
->ptr
[i
] = 0;
697 // If we're already within an OnChar, return: probably
698 // a programmatic insertion.
699 if (tw
->m_tempCallbackStruct
)
702 // Check for a backspace
703 if (cbs
->startPos
== (cbs
->currInsert
- 1))
705 tw
->m_tempCallbackStruct
= (void*) cbs
;
707 wxKeyEvent
event (wxEVT_CHAR
);
708 event
.SetId(tw
->GetId());
709 event
.m_keyCode
= WXK_DELETE
;
710 event
.SetEventObject(tw
);
712 // Only if wxTextCtrl::OnChar is called
713 // will this be set to True (and the character
717 tw
->GetEventHandler()->ProcessEvent(event
);
719 tw
->m_tempCallbackStruct
= NULL
;
721 if (tw
->m_inSetValue
)
724 if (tw
->m_processedDefault
)
726 // Can generate a command
727 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
728 commandEvent
.SetEventObject(tw
);
729 tw
->ProcessCommand(commandEvent
);
735 // Pasting operation: let it through without
737 if (cbs
->text
->length
> 1)
740 // Something other than text
741 if (cbs
->text
->ptr
== NULL
)
744 tw
->m_tempCallbackStruct
= (void*) cbs
;
746 wxKeyEvent
event (wxEVT_CHAR
);
747 event
.SetId(tw
->GetId());
748 event
.SetEventObject(tw
);
749 event
.m_keyCode
= (cbs
->text
->ptr
[0] == 10 ? 13 : cbs
->text
->ptr
[0]);
751 // Only if wxTextCtrl::OnChar is called
752 // will this be set to True (and the character
756 tw
->GetEventHandler()->ProcessEvent(event
);
758 tw
->m_tempCallbackStruct
= NULL
;
760 if (tw
->m_inSetValue
)
763 if (tw
->m_processedDefault
)
765 // Can generate a command
766 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
767 commandEvent
.SetEventObject(tw
);
768 tw
->ProcessCommand(commandEvent
);
773 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
775 if (!wxGetWindowFromTable(w
))
778 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
779 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
780 event
.SetEventObject(tw
);
781 tw
->GetEventHandler()->ProcessEvent(event
);
785 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
787 if (!wxGetWindowFromTable(w
))
790 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
791 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
792 event
.SetEventObject(tw
);
793 tw
->GetEventHandler()->ProcessEvent(event
);
796 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
797 XmAnyCallbackStruct
*ptr
)
799 if (!wxGetWindowFromTable(w
))
802 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
805 type_event = wxEVENT_TYPE_TEXT_ENTER_COMMAND ;
808 type_event = wxEVENT_TYPE_TEXT_COMMAND ;
813 if (tw
->m_inSetValue
)
816 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
);
817 event
.SetId(tw
->GetId());
818 event
.SetEventObject(tw
);
819 tw
->ProcessCommand(event
);