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
;
83 m_backgroundColour
= parent
->GetBackgroundColour();
84 m_foregroundColour
= parent
->GetForegroundColour();
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 m_windowFont
= parent
->GetFont();
149 SetCanAddEventHandler(TRUE
);
150 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
, pos
.x
, pos
.y
, size
.x
, size
.y
);
152 ChangeBackgroundColour();
157 WXWidget
wxTextCtrl::GetTopWidget() const
159 return ((m_windowStyle
& wxTE_MULTILINE
) ? (WXWidget
) XtParent((Widget
) m_mainWidget
) : m_mainWidget
);
162 wxString
wxTextCtrl::GetValue() const
164 if (m_windowStyle
& wxTE_PASSWORD
)
168 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
177 return wxEmptyString
;
182 void wxTextCtrl::SetValue(const wxString
& value
)
184 wxASSERT_MSG( (!value
.IsNull()), "Must not pass a null string to wxTextCtrl::SetValue." ) ;
187 XmTextSetString ((Widget
) m_mainWidget
, (char*) (const char*) value
);
189 m_inSetValue
= FALSE
;
192 // Clipboard operations
193 void wxTextCtrl::Copy()
195 XmTextCopy((Widget
) m_mainWidget
, CurrentTime
);
198 void wxTextCtrl::Cut()
200 XmTextCut((Widget
) m_mainWidget
, CurrentTime
);
203 void wxTextCtrl::Paste()
205 XmTextPaste((Widget
) m_mainWidget
);
208 void wxTextCtrl::SetEditable(bool editable
)
210 XmTextSetEditable((Widget
) m_mainWidget
, (Boolean
) editable
);
213 void wxTextCtrl::SetInsertionPoint(long pos
)
215 XmTextSetInsertionPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
218 void wxTextCtrl::SetInsertionPointEnd()
220 long pos
= GetLastPosition();
221 SetInsertionPoint(pos
);
224 long wxTextCtrl::GetInsertionPoint() const
226 return (long) XmTextGetInsertionPosition ((Widget
) m_mainWidget
);
229 long wxTextCtrl::GetLastPosition() const
231 return (long) XmTextGetLastPosition ((Widget
) m_mainWidget
);
234 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
236 XmTextReplace ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
237 (char*) (const char*) value
);
240 void wxTextCtrl::Remove(long from
, long to
)
242 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
244 XmTextRemove ((Widget
) m_mainWidget
);
247 void wxTextCtrl::SetSelection(long from
, long to
)
249 XmTextSetSelection ((Widget
) m_mainWidget
, (XmTextPosition
) from
, (XmTextPosition
) to
,
253 bool wxTextCtrl::LoadFile(const wxString
& file
)
255 if (!wxFileExists(file
))
262 Widget textWidget
= (Widget
) m_mainWidget
;
266 if ((stat ((char*) (const char*) file
, &statb
) == -1) || (statb
.st_mode
& S_IFMT
) != S_IFREG
||
267 !(fp
= fopen ((char*) (const char*) file
, "r")))
273 long len
= statb
.st_size
;
275 if (!(text
= XtMalloc ((unsigned) (len
+ 1))))
280 if (fread (text
, sizeof (char), len
, fp
) != (size_t) len
)
286 XmTextSetString (textWidget
, text
);
287 // m_textPosition = len;
294 // If file is null, try saved file name first
295 // Returns TRUE if succeeds.
296 bool wxTextCtrl::SaveFile(const wxString
& file
)
298 wxString
theFile(file
);
300 theFile
= m_fileName
;
303 m_fileName
= theFile
;
305 Widget textWidget
= (Widget
) m_mainWidget
;
308 if (!(fp
= fopen ((char*) (const char*) theFile
, "w")))
314 char *text
= XmTextGetString (textWidget
);
315 long len
= XmTextGetLastPosition (textWidget
);
317 if (fwrite (text
, sizeof (char), len
, fp
) != (size_t) len
)
319 // Did not write whole file
321 // Make sure newline terminates the file
322 if (text
[len
- 1] != '\n')
332 void wxTextCtrl::WriteText(const wxString
& text
)
334 long textPosition
= GetInsertionPoint() + strlen (text
);
335 XmTextInsert ((Widget
) m_mainWidget
, GetInsertionPoint(), (char*) (const char*) text
);
336 XtVaSetValues ((Widget
) m_mainWidget
, XmNcursorPosition
, textPosition
, NULL
);
337 SetInsertionPoint(textPosition
);
338 XmTextShowPosition ((Widget
) m_mainWidget
, textPosition
);
342 void wxTextCtrl::Clear()
344 XmTextSetString ((Widget
) m_mainWidget
, "");
348 bool wxTextCtrl::IsModified() const
353 // Makes 'unmodified'
354 void wxTextCtrl::DiscardEdits()
356 XmTextSetString ((Widget
) m_mainWidget
, "");
360 int wxTextCtrl::GetNumberOfLines() const
362 // HIDEOUSLY inefficient, but we have no choice.
363 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
368 bool finished
= FALSE
;
391 long wxTextCtrl::XYToPosition(long x
, long y
) const
393 /* It seems, that there is a bug in some versions of the Motif library,
394 so the original wxWin-Code doesn't work. */
396 Widget textWidget = (Widget) handle;
397 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
399 /* Now a little workaround: */
401 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
405 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
408 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
412 void wxTextCtrl::ShowPosition(long pos
)
414 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
417 int wxTextCtrl::GetLineLength(long lineNo
) const
419 wxString str
= GetLineText (lineNo
);
420 return (int) str
.Length();
423 wxString
wxTextCtrl::GetLineText(long lineNo
) const
425 // HIDEOUSLY inefficient, but we have no choice.
426 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
433 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
438 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
445 return wxEmptyString
;
452 void wxTextCtrl::Command(wxCommandEvent
& event
)
454 SetValue (event
.GetString());
455 ProcessCommand (event
);
458 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
460 // By default, load the first file into the text window.
461 if (event
.GetNumberOfFiles() > 0)
463 LoadFile(event
.GetFiles()[0]);
467 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
468 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
470 //=========================================================================
471 // Called then the buffer is full (gcc 2.6.3)
472 // or when "endl" is output (Borland 4.5)
473 //=========================================================================
474 // Class declaration using multiple inheritance doesn't work properly for
475 // Borland. See note in wb_text.h.
476 #ifndef NO_TEXT_WINDOW_STREAM
477 int wxTextCtrl::overflow(int c
)
479 // Make sure there is a holding area
480 if ( allocate()==EOF
)
482 wxError("Streambuf allocation failed","Internal error");
486 // Verify that there are no characters in get area
487 if ( gptr() && gptr() < egptr() )
489 wxError("Who's trespassing my get area?","Internal error");
496 // Make sure there is a put area
499 /* This doesn't seem to be fatal so comment out error message */
500 // wxError("Put area not opened","Internal error");
501 setp( base(), base() );
504 // Determine how many characters have been inserted but no consumed
505 int plen
= pptr() - pbase();
507 // Now Jerry relies on the fact that the buffer is at least 2 chars
508 // long, but the holding area "may be as small as 1" ???
509 // And we need an additional \0, so let's keep this inefficient but
512 // If c!=EOF, it is a character that must also be comsumed
513 int xtra
= c
==EOF
? 0 : 1;
515 // Write temporary C-string to wxTextWindow
517 char *txt
= new char[plen
+xtra
+1];
518 memcpy(txt
, pbase(), plen
);
519 txt
[plen
] = (char)c
; // append c
520 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
521 // If the put area already contained \0, output will be truncated there
527 setp(pbase(), epptr());
529 #if defined(__WATCOMC__)
531 #elif defined(zapeof) // HP-UX (all cfront based?)
534 return c
!=EOF
? c
: 0; // this should make everybody happy
538 //=========================================================================
539 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
540 //=========================================================================
541 int wxTextCtrl::sync()
543 // Verify that there are no characters in get area
544 if ( gptr() && gptr() < egptr() )
546 wxError("Who's trespassing my get area?","Internal error");
550 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
554 int len = pptr() - pbase();
555 char *txt = new char[len+1];
556 strncpy(txt, pbase(), len);
559 setp(pbase(), epptr());
565 //=========================================================================
566 // Should not be called by a "ostream". Used by a "istream"
567 //=========================================================================
568 int wxTextCtrl::underflow()
574 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
580 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
583 str
.Printf("%.2f", f
);
588 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
591 str
.Printf("%.2f", d
);
596 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
604 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
607 str
.Printf("%ld", i
);
612 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
622 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
624 // Indicates that we should generate a normal command, because
625 // we're letting default behaviour happen (otherwise it's vetoed
626 // by virtue of overriding OnChar)
627 m_processedDefault
= TRUE
;
629 if (m_tempCallbackStruct
)
631 XmTextVerifyCallbackStruct
*textStruct
=
632 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
633 textStruct
->doit
= True
;
634 if (isascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
636 textStruct
->text
->ptr
[0] = ((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
641 void wxTextCtrl::ChangeFont(bool keepOriginalSize
)
643 wxWindow::ChangeFont(keepOriginalSize
);
646 void wxTextCtrl::ChangeBackgroundColour()
648 wxWindow::ChangeBackgroundColour();
650 Widget parent
= XtParent ((Widget
) m_mainWidget
);
653 XtVaGetValues (parent
,
654 XmNhorizontalScrollBar
, &hsb
,
655 XmNverticalScrollBar
, &vsb
,
658 /* TODO: should scrollbars be affected? Should probably have separate
659 * function to change them (by default, taken from wxSystemSettings)
661 DoChangeBackgroundColour((WXWidget) hsb, m_backgroundColour, TRUE);
663 DoChangeBackgroundColour((WXWidget) vsb, m_backgroundColour, TRUE);
666 DoChangeBackgroundColour((WXWidget
) parent
, m_backgroundColour
, TRUE
);
669 void wxTextCtrl::ChangeForegroundColour()
671 wxWindow::ChangeForegroundColour();
674 Widget parent
= XtParent ((Widget
) m_mainWidget
);
677 XtVaGetValues (parent
,
678 XmNhorizontalScrollBar
, &hsb
,
679 XmNverticalScrollBar
, &vsb
,
682 /* TODO: should scrollbars be affected? Should probably have separate
683 * function to change them (by default, taken from wxSystemSettings)
685 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
687 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
689 DoChangeForegroundColour((WXWidget
) parent
, m_foregroundColour
);
692 static void wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer ptr
)
694 if (!wxGetWindowFromTable(w
))
695 // Widget has been deleted!
698 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
699 tw
->SetModified(TRUE
);
703 wxTextWindowModifyProc (Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
705 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
706 tw
->m_processedDefault
= FALSE
;
708 // First, do some stuff if it's a password control.
709 // (What does this do exactly?)
711 if (tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
714 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
715 * every event as a replace event. cbs->text->ptr gives the replacement
716 * text, cbs->startPos gives the index of the first char affected by the
717 * replace, and cbs->endPos gives the index one more than the last char
718 * affected by the replace (startPos == endPos implies an empty range).
719 * Hence, a deletion is represented by replacing all input text with a
720 * blank string ("", *not* NULL!). A simple insertion that does not
721 * overwrite any text has startPos == endPos.
724 if (tw
->m_value
.IsNull())
726 tw
->m_value
= cbs
->text
->ptr
;
730 char * passwd
= (char*) (const char*) tw
->m_value
; // Set up a more convenient alias.
732 int len
= passwd
? strlen(passwd
) : 0; // Enough room for old text
733 len
+= strlen(cbs
->text
->ptr
) + 1; // + new text (if any) + NUL
734 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
736 char * newS
= new char [len
];
737 char * p
= passwd
, * dest
= newS
, * insert
= cbs
->text
->ptr
;
739 // Copy (old) text from passwd, up to the start posn of the change.
741 for (i
= 0; i
< cbs
->startPos
; ++i
)
744 // Copy the text to be inserted).
748 // Finally, copy into newS any remaining text from passwd[endPos] on.
749 for (p
= passwd
+ cbs
->endPos
; *p
; )
758 if (cbs
->text
->length
>0)
761 for (i
= 0; i
< cbs
->text
->length
; ++i
)
762 cbs
->text
->ptr
[i
] = '*';
763 cbs
->text
->ptr
[i
] = 0;
767 // If we're already within an OnChar, return: probably
768 // a programmatic insertion.
769 if (tw
->m_tempCallbackStruct
)
772 // Check for a backspace
773 if (cbs
->startPos
== (cbs
->currInsert
- 1))
775 tw
->m_tempCallbackStruct
= (void*) cbs
;
777 wxKeyEvent
event (wxEVT_CHAR
);
778 event
.SetId(tw
->GetId());
779 event
.m_keyCode
= WXK_DELETE
;
780 event
.SetEventObject(tw
);
782 // Only if wxTextCtrl::OnChar is called
783 // will this be set to True (and the character
787 tw
->GetEventHandler()->ProcessEvent(event
);
789 tw
->m_tempCallbackStruct
= NULL
;
791 if (tw
->InSetValue())
794 if (tw
->m_processedDefault
)
796 // Can generate a command
797 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
798 commandEvent
.SetEventObject(tw
);
799 tw
->ProcessCommand(commandEvent
);
805 // Pasting operation: let it through without
807 if (cbs
->text
->length
> 1)
810 // Something other than text
811 if (cbs
->text
->ptr
== NULL
)
814 tw
->m_tempCallbackStruct
= (void*) cbs
;
816 wxKeyEvent
event (wxEVT_CHAR
);
817 event
.SetId(tw
->GetId());
818 event
.SetEventObject(tw
);
819 event
.m_keyCode
= (cbs
->text
->ptr
[0] == 10 ? 13 : cbs
->text
->ptr
[0]);
821 // Only if wxTextCtrl::OnChar is called
822 // will this be set to True (and the character
826 tw
->GetEventHandler()->ProcessEvent(event
);
828 tw
->m_tempCallbackStruct
= NULL
;
830 if (tw
->InSetValue())
833 if (tw
->m_processedDefault
)
835 // Can generate a command
836 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, tw
->GetId());
837 commandEvent
.SetEventObject(tw
);
838 tw
->ProcessCommand(commandEvent
);
843 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
845 if (!wxGetWindowFromTable(w
))
848 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
849 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
850 event
.SetEventObject(tw
);
851 tw
->GetEventHandler()->ProcessEvent(event
);
855 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
)
857 if (!wxGetWindowFromTable(w
))
860 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
861 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
862 event
.SetEventObject(tw
);
863 tw
->GetEventHandler()->ProcessEvent(event
);
866 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
867 XmAnyCallbackStruct
*ptr
)
869 if (!wxGetWindowFromTable(w
))
872 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
875 type_event = wxEVENT_TYPE_TEXT_ENTER_COMMAND ;
878 type_event = wxEVENT_TYPE_TEXT_COMMAND ;
883 if (tw
->InSetValue())
886 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
);
887 event
.SetId(tw
->GetId());
888 event
.SetEventObject(tw
);
889 tw
->ProcessCommand(event
);