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 // ----------------------------------------------------------------------------
43 static void wxTextWindowChangedProc(Widget w
, XtPointer clientData
, XtPointer ptr
);
44 static void wxTextWindowModifyProc(Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
);
45 static void wxTextWindowGainFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
46 static void wxTextWindowLoseFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
47 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*ptr
);
49 #if !USE_SHARED_LIBRARY
50 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
52 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
53 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
54 EVT_CHAR(wxTextCtrl::OnChar
)
58 // ============================================================================
60 // ============================================================================
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
67 wxTextCtrl::wxTextCtrl()
68 #ifndef NO_TEXT_WINDOW_STREAM
72 m_tempCallbackStruct
= (void*) NULL
;
74 m_processedDefault
= FALSE
;
77 bool wxTextCtrl::Create(wxWindow
*parent
,
79 const wxString
& value
,
83 const wxValidator
& validator
,
86 m_tempCallbackStruct
= (void*) NULL
;
88 m_processedDefault
= FALSE
;
89 // m_backgroundColour = parent->GetBackgroundColour();
90 m_backgroundColour
= * wxWHITE
;
91 m_foregroundColour
= parent
->GetForegroundColour();
94 SetValidator(validator
);
96 parent
->AddChild(this);
98 m_windowStyle
= style
;
101 m_windowId
= (int)NewControlId();
105 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
107 bool wantHorizScrolling
= ((m_windowStyle
& wxHSCROLL
) != 0);
109 // If we don't have horizontal scrollbars, we want word wrap.
110 bool wantWordWrap
= !wantHorizScrolling
;
112 if (m_windowStyle
& wxTE_MULTILINE
)
115 XtSetArg (args
[0], XmNscrollHorizontal
, wantHorizScrolling
? True
: False
);
116 XtSetArg (args
[1], XmNwordWrap
, wantWordWrap
? True
: False
);
118 m_mainWidget
= (WXWidget
) XmCreateScrolledText(parentWidget
,
122 XtVaSetValues ((Widget
) m_mainWidget
,
123 XmNeditable
, ((style
& wxTE_READONLY
) ? False
: True
),
124 XmNeditMode
, XmMULTI_LINE_EDIT
,
126 XtManageChild ((Widget
) m_mainWidget
);
130 m_mainWidget
= (WXWidget
)XtVaCreateManagedWidget
138 // TODO: Is this relevant? What does it do?
140 if (!value
.IsNull() && (value
.Length() > (unsigned int) noCols
))
141 noCols
= value
.Length();
142 XtVaSetValues((Widget
) m_mainWidget
,
147 // remove border if asked for
148 if ( style
& wxNO_BORDER
)
150 XtVaSetValues((Widget
)m_mainWidget
,
151 XmNshadowThickness
, 0,
156 XmTextSetString ((Widget
) m_mainWidget
, (char*)value
.c_str());
159 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
161 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
163 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
165 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
167 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
170 m_windowFont
= parent
->GetFont();
173 SetCanAddEventHandler(TRUE
);
174 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
176 ChangeBackgroundColour();
181 WXWidget
wxTextCtrl::GetTopWidget() const
183 return ((m_windowStyle
& wxTE_MULTILINE
) ? (WXWidget
) XtParent((Widget
) m_mainWidget
) : m_mainWidget
);
186 wxString
wxTextCtrl::GetValue() const
188 if (m_windowStyle
& wxTE_PASSWORD
)
192 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
201 return wxEmptyString
;
206 void wxTextCtrl::SetValue(const wxString
& value
)
210 XmTextSetString ((Widget
) m_mainWidget
, (char*)value
.c_str());
212 m_inSetValue
= FALSE
;
215 // Clipboard operations
216 void wxTextCtrl::Copy()
218 XmTextCopy((Widget
) m_mainWidget
, CurrentTime
);
221 void wxTextCtrl::Cut()
223 XmTextCut((Widget
) m_mainWidget
, CurrentTime
);
226 void wxTextCtrl::Paste()
228 XmTextPaste((Widget
) m_mainWidget
);
231 void wxTextCtrl::SetEditable(bool editable
)
233 XmTextSetEditable((Widget
) m_mainWidget
, (Boolean
) editable
);
236 void wxTextCtrl::SetInsertionPoint(long pos
)
238 XmTextSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
241 void wxTextCtrl::SetInsertionPointEnd()
243 long pos
= GetLastPosition();
244 SetInsertionPoint(pos
);
247 long wxTextCtrl::GetInsertionPoint() const
249 return (long) XmTextGetInsertionPosition ((Widget
) m_mainWidget
);
252 long wxTextCtrl::GetLastPosition() const
254 return (long) XmTextGetLastPosition ((Widget
) m_mainWidget
);
257 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
259 XmTextReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
260 (char*) (const char*) value
);
263 void wxTextCtrl::Remove(long from
, long to
)
265 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
267 XmTextRemove ((Widget
) m_mainWidget
);
270 void wxTextCtrl::SetSelection(long from
, long to
)
272 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
276 bool wxTextCtrl::LoadFile(const wxString
& file
)
278 if (!wxFileExists(file
))
285 Widget textWidget
= (Widget
) m_mainWidget
;
289 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
290 !(fp
= fopen ((char*) (const char*) file
, "r")))
296 long len
= statb
.st_size
;
298 if (!(text
= XtMalloc ((unsigned) (len
+ 1))))
303 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
309 XmTextSetString (textWidget
, text
);
310 // m_textPosition = len;
317 // If file is null, try saved file name first
318 // Returns TRUE if succeeds.
319 bool wxTextCtrl::SaveFile(const wxString
& file
)
321 wxString
theFile(file
);
323 theFile
= m_fileName
;
326 m_fileName
= theFile
;
328 Widget textWidget
= (Widget
) m_mainWidget
;
331 if (!(fp
= fopen ((char*) (const char*) theFile
, "w")))
337 char *text
= XmTextGetString (textWidget
);
338 long len
= XmTextGetLastPosition (textWidget
);
340 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
342 // Did not write whole file
344 // Make sure newline terminates the file
345 if (text
[len
- 1] != '\n')
355 void wxTextCtrl::WriteText(const wxString
& text
)
357 long textPosition
= GetInsertionPoint() + strlen (text
);
358 XmTextInsert ((Widget
) m_mainWidget
, GetInsertionPoint(), (char*) (const char*) text
);
359 XtVaSetValues ((Widget
) m_mainWidget
, XmNcursorPosition
, textPosition
, NULL
);
360 SetInsertionPoint(textPosition
);
361 XmTextShowPosition ((Widget
) m_mainWidget
, textPosition
);
365 void wxTextCtrl::AppendText(const wxString
& text
)
367 long textPosition
= GetLastPosition() + strlen(text
);
368 XmTextInsert ((Widget
) m_mainWidget
, GetLastPosition(), (char*) (const char*) text
);
369 XtVaSetValues ((Widget
) m_mainWidget
, XmNcursorPosition
, textPosition
, NULL
);
370 SetInsertionPoint(textPosition
);
371 XmTextShowPosition ((Widget
) m_mainWidget
, textPosition
);
375 void wxTextCtrl::Clear()
377 XmTextSetString ((Widget
) m_mainWidget
, "");
381 bool wxTextCtrl::IsModified() const
386 // Makes 'unmodified'
387 void wxTextCtrl::DiscardEdits()
389 XmTextSetString ((Widget
) m_mainWidget
, "");
393 int wxTextCtrl::GetNumberOfLines() const
395 // HIDEOUSLY inefficient, but we have no choice.
396 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
401 bool finished
= FALSE
;
424 long wxTextCtrl::XYToPosition(long x
, long y
) const
426 /* It seems, that there is a bug in some versions of the Motif library,
427 so the original wxWin-Code doesn't work. */
429 Widget textWidget = (Widget) handle;
430 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
432 /* Now a little workaround: */
434 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
438 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
441 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
445 void wxTextCtrl::ShowPosition(long pos
)
447 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
450 int wxTextCtrl::GetLineLength(long lineNo
) const
452 wxString str
= GetLineText (lineNo
);
453 return (int) str
.Length();
456 wxString
wxTextCtrl::GetLineText(long lineNo
) const
458 // HIDEOUSLY inefficient, but we have no choice.
459 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
466 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
471 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
478 return wxEmptyString
;
485 void wxTextCtrl::Command(wxCommandEvent
& event
)
487 SetValue (event
.GetString());
488 ProcessCommand (event
);
491 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
493 // By default, load the first file into the text window.
494 if (event
.GetNumberOfFiles() > 0)
496 LoadFile(event
.GetFiles()[0]);
500 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
501 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
503 //=========================================================================
504 // Called then the buffer is full (gcc 2.6.3)
505 // or when "endl" is output (Borland 4.5)
506 //=========================================================================
507 // Class declaration using multiple inheritance doesn't work properly for
508 // Borland. See note in wb_text.h.
509 #ifndef NO_TEXT_WINDOW_STREAM
510 int wxTextCtrl::overflow(int c
)
512 // Make sure there is a holding area
513 if ( allocate()==EOF
)
515 wxError("Streambuf allocation failed","Internal error");
519 // Verify that there are no characters in get area
520 if ( gptr() && gptr() < egptr() )
522 wxError("wxTextCtrl::overflow: Who's trespassing my get area?","Internal error");
529 // Make sure there is a put area
532 /* This doesn't seem to be fatal so comment out error message */
533 // wxError("Put area not opened","Internal error");
534 setp( base(), base() );
537 // Determine how many characters have been inserted but no consumed
538 int plen
= pptr() - pbase();
540 // Now Jerry relies on the fact that the buffer is at least 2 chars
541 // long, but the holding area "may be as small as 1" ???
542 // And we need an additional \0, so let's keep this inefficient but
545 // If c!=EOF, it is a character that must also be comsumed
546 int xtra
= c
==EOF
? 0 : 1;
548 // Write temporary C-string to wxTextWindow
550 char *txt
= new char[plen
+xtra
+1];
551 memcpy(txt
, pbase(), plen
);
552 txt
[plen
] = (char)c
; // append c
553 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
554 // If the put area already contained \0, output will be truncated there
560 setp(pbase(), epptr());
562 #if defined(__WATCOMC__)
564 #elif defined(zapeof) // HP-UX (all cfront based?)
567 return c
!=EOF
? c
: 0; // this should make everybody happy
571 //=========================================================================
572 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
573 //=========================================================================
574 int wxTextCtrl::sync()
576 // Verify that there are no characters in get area
577 if ( gptr() && gptr() < egptr() )
579 wxError("Who's trespassing my get area?","Internal error");
583 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
587 int len = pptr() - pbase();
588 char *txt = new char[len+1];
589 strncpy(txt, pbase(), len);
592 setp(pbase(), epptr());
598 //=========================================================================
599 // Should not be called by a "ostream". Used by a "istream"
600 //=========================================================================
601 int wxTextCtrl::underflow()
607 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
613 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
616 str
.Printf("%.2f", f
);
621 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
624 str
.Printf("%.2f", d
);
629 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
637 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
640 str
.Printf("%ld", i
);
645 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
655 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
657 // Indicates that we should generate a normal command, because
658 // we're letting default behaviour happen (otherwise it's vetoed
659 // by virtue of overriding OnChar)
660 m_processedDefault
= TRUE
;
662 if (m_tempCallbackStruct
)
664 XmTextVerifyCallbackStruct
*textStruct
=
665 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
666 textStruct
->doit
= True
;
667 if (isascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
669 textStruct
->text
->ptr
[0] = ((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
674 void wxTextCtrl::ChangeFont(bool keepOriginalSize
)
676 wxWindow::ChangeFont(keepOriginalSize
);
679 void wxTextCtrl::ChangeBackgroundColour()
681 wxWindow::ChangeBackgroundColour();
683 /* TODO: should scrollbars be affected? Should probably have separate
684 * function to change them (by default, taken from wxSystemSettings)
686 if (m_windowStyle
& wxTE_MULTILINE
)
688 Widget parent
= XtParent ((Widget
) m_mainWidget
);
691 XtVaGetValues (parent
,
692 XmNhorizontalScrollBar
, &hsb
,
693 XmNverticalScrollBar
, &vsb
,
695 wxColour backgroundColour
= wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE
);
697 DoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, TRUE
);
699 DoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, TRUE
);
701 DoChangeBackgroundColour((WXWidget
) parent
, m_backgroundColour
, TRUE
);
705 void wxTextCtrl::ChangeForegroundColour()
707 wxWindow::ChangeForegroundColour();
709 if (m_windowStyle
& wxTE_MULTILINE
)
711 Widget parent
= XtParent ((Widget
) m_mainWidget
);
714 XtVaGetValues (parent
,
715 XmNhorizontalScrollBar
, &hsb
,
716 XmNverticalScrollBar
, &vsb
,
719 /* TODO: should scrollbars be affected? Should probably have separate
720 * function to change them (by default, taken from wxSystemSettings)
722 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
724 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
726 DoChangeForegroundColour((WXWidget
) parent
, m_foregroundColour
);
730 static void wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer ptr
)
732 if (!wxGetWindowFromTable(w
))
733 // Widget has been deleted!
736 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
737 tw
->SetModified(TRUE
);
741 wxTextWindowModifyProc (Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
743 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
744 tw
->m_processedDefault
= FALSE
;
746 // First, do some stuff if it's a password control.
747 // (What does this do exactly?)
749 if (tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
752 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
753 * every event as a replace event. cbs->text->ptr gives the replacement
754 * text, cbs->startPos gives the index of the first char affected by the
755 * replace, and cbs->endPos gives the index one more than the last char
756 * affected by the replace (startPos == endPos implies an empty range).
757 * Hence, a deletion is represented by replacing all input text with a
758 * blank string ("", *not* NULL!). A simple insertion that does not
759 * overwrite any text has startPos == endPos.
762 if (tw
->m_value
.IsNull())
764 tw
->m_value
= cbs
->text
->ptr
;
768 char * passwd
= (char*) (const char*) tw
->m_value
; // Set up a more convenient alias.
770 int len
= passwd
? strlen(passwd
) : 0; // Enough room for old text
771 len
+= strlen(cbs
->text
->ptr
) + 1; // + new text (if any) + NUL
772 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
774 char * newS
= new char [len
];
775 char * p
= passwd
, * dest
= newS
, * insert
= cbs
->text
->ptr
;
777 // Copy (old) text from passwd, up to the start posn of the change.
779 for (i
= 0; i
< cbs
->startPos
; ++i
)
782 // Copy the text to be inserted).
786 // Finally, copy into newS any remaining text from passwd[endPos] on.
787 for (p
= passwd
+ cbs
->endPos
; *p
; )
796 if (cbs
->text
->length
>0)
799 for (i
= 0; i
< cbs
->text
->length
; ++i
)
800 cbs
->text
->ptr
[i
] = '*';
801 cbs
->text
->ptr
[i
] = 0;
805 // If we're already within an OnChar, return: probably
806 // a programmatic insertion.
807 if (tw
->m_tempCallbackStruct
)
810 // Check for a backspace
811 if (cbs
->startPos
== (cbs
->currInsert
- 1))
813 tw
->m_tempCallbackStruct
= (void*) cbs
;
815 wxKeyEvent
event (wxEVT_CHAR
);
816 event
.SetId(tw
->GetId());
817 event
.m_keyCode
= WXK_DELETE
;
818 event
.SetEventObject(tw
);
820 // Only if wxTextCtrl::OnChar is called
821 // will this be set to True (and the character
825 tw
->GetEventHandler()->ProcessEvent(event
);
827 tw
->m_tempCallbackStruct
= NULL
;
829 if (tw
->InSetValue())
832 if (tw
->m_processedDefault
)
834 // Can generate a command
835 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
836 commandEvent
.SetEventObject(tw
);
837 tw
->ProcessCommand(commandEvent
);
843 // Pasting operation: let it through without
845 if (cbs
->text
->length
> 1)
848 // Something other than text
849 if (cbs
->text
->ptr
== NULL
)
852 tw
->m_tempCallbackStruct
= (void*) cbs
;
854 wxKeyEvent
event (wxEVT_CHAR
);
855 event
.SetId(tw
->GetId());
856 event
.SetEventObject(tw
);
857 event
.m_keyCode
= (cbs
->text
->ptr
[0] == 10 ? 13 : cbs
->text
->ptr
[0]);
859 // Only if wxTextCtrl::OnChar is called
860 // will this be set to True (and the character
864 tw
->GetEventHandler()->ProcessEvent(event
);
866 tw
->m_tempCallbackStruct
= NULL
;
868 if (tw
->InSetValue())
871 if (tw
->m_processedDefault
)
873 // Can generate a command
874 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
875 commandEvent
.SetEventObject(tw
);
876 tw
->ProcessCommand(commandEvent
);
880 // ----------------------------------------------------------------------------
882 // ----------------------------------------------------------------------------
885 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
887 if (!wxGetWindowFromTable(w
))
890 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
891 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
892 event
.SetEventObject(tw
);
893 tw
->GetEventHandler()->ProcessEvent(event
);
897 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
899 if (!wxGetWindowFromTable(w
))
902 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
903 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
904 event
.SetEventObject(tw
);
905 tw
->GetEventHandler()->ProcessEvent(event
);
908 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
909 XmAnyCallbackStruct
*ptr
)
911 if (!wxGetWindowFromTable(w
))
914 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
916 if (tw
->InSetValue())
919 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
);
920 event
.SetId(tw
->GetId());
921 event
.SetEventObject(tw
);
922 tw
->ProcessCommand(event
);