1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
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__)
33 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
36 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
37 EVT_DROP_FILES(wxTextCtrl
::OnDropFiles
)
38 EVT_MENU(wxID_CUT
, wxTextCtrl
::OnCut
)
39 EVT_MENU(wxID_COPY
, wxTextCtrl
::OnCopy
)
40 EVT_MENU(wxID_PASTE
, wxTextCtrl
::OnPaste
)
41 EVT_MENU(wxID_UNDO
, wxTextCtrl
::OnUndo
)
42 EVT_MENU(wxID_REDO
, wxTextCtrl
::OnRedo
)
44 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl
::OnUpdateCut
)
45 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl
::OnUpdateCopy
)
46 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl
::OnUpdatePaste
)
47 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl
::OnUpdateUndo
)
48 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl
::OnUpdateRedo
)
53 wxTextCtrl
::wxTextCtrl()
54 #ifndef NO_TEXT_WINDOW_STREAM
61 bool wxTextCtrl
::Create(wxWindow
*parent
, wxWindowID id
,
62 const wxString
& value
,
64 const wxSize
& size
, long style
,
65 const wxValidator
& validator
,
70 SetValidator(validator
);
71 if (parent
) parent
->AddChild(this);
73 m_windowStyle
= style
;
76 m_windowId
= (int)NewControlId();
83 wxString wxTextCtrl
::GetValue() const
89 void wxTextCtrl
::SetValue(const wxString
& value
)
94 void wxTextCtrl
::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
99 // Clipboard operations
100 void wxTextCtrl
::Copy()
105 void wxTextCtrl
::Cut()
110 void wxTextCtrl
::Paste()
115 void wxTextCtrl
::SetEditable(bool editable
)
120 void wxTextCtrl
::SetInsertionPoint(long pos
)
125 void wxTextCtrl
::SetInsertionPointEnd()
127 long pos
= GetLastPosition();
128 SetInsertionPoint(pos
);
131 long wxTextCtrl
::GetInsertionPoint() const
137 long wxTextCtrl
::GetLastPosition() const
143 void wxTextCtrl
::Replace(long from
, long to
, const wxString
& value
)
148 void wxTextCtrl
::Remove(long from
, long to
)
153 void wxTextCtrl
::SetSelection(long from
, long to
)
158 bool wxTextCtrl
::LoadFile(const wxString
& file
)
160 if (!wxFileExists(file
))
167 ifstream
input((char*) (const char*) file
, ios
::nocreate
| ios
::in
);
171 struct stat stat_buf
;
172 if (stat(file
, &stat_buf
) < 0)
174 // This may need to be a bigger buffer than the file size suggests,
175 // if it's a UNIX file. Give it an extra 1000 just in case.
176 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
179 while (!input
.eof() && input
.peek() != EOF
)
181 input
.getline(wxBuffer
, 500);
182 int len
= strlen(wxBuffer
);
184 wxBuffer
[len
+1] = 10;
186 strcpy(tmp_buffer
+pos
, wxBuffer
);
187 pos
+= strlen(wxBuffer
);
200 // If file is null, try saved file name first
201 // Returns TRUE if succeeds.
202 bool wxTextCtrl
::SaveFile(const wxString
& file
)
204 wxString
theFile(file
);
206 theFile
= m_fileName
;
209 m_fileName
= theFile
;
211 ofstream
output((char*) (const char*) theFile
);
215 // TODO get and save text
220 void wxTextCtrl
::WriteText(const wxString
& text
)
222 // TODO write text to control
225 void wxTextCtrl
::AppendText(const wxString
& text
)
227 // TODO append text to control
230 void wxTextCtrl
::Clear()
235 bool wxTextCtrl
::IsModified() const
241 // Makes 'unmodified'
242 void wxTextCtrl
::DiscardEdits()
247 int wxTextCtrl
::GetNumberOfLines() const
253 long wxTextCtrl
::XYToPosition(long x
, long y
) const
259 void wxTextCtrl
::PositionToXY(long pos
, long *x
, long *y
) const
264 void wxTextCtrl
::ShowPosition(long pos
)
269 int wxTextCtrl
::GetLineLength(long lineNo
) const
275 wxString wxTextCtrl
::GetLineText(long lineNo
) const
281 bool wxTextCtrl
::CanCopy() const
283 // Can copy if there's a selection
285 GetSelection(& from
, & to
);
286 return (from
!= to
) ;
289 bool wxTextCtrl
::CanCut() const
291 // Can cut if there's a selection
293 GetSelection(& from
, & to
);
294 return (from
!= to
) ;
297 bool wxTextCtrl
::CanPaste() const
299 return IsEditable() ;
303 void wxTextCtrl
::Undo()
308 void wxTextCtrl
::Redo()
313 bool wxTextCtrl
::CanUndo() const
319 bool wxTextCtrl
::CanRedo() const
325 // If the return values from and to are the same, there is no
327 void wxTextCtrl
::GetSelection(long* from
, long* to
) const
334 bool wxTextCtrl
::IsEditable() const
340 void wxTextCtrl
::Command(wxCommandEvent
& event
)
342 SetValue (event
.GetString());
343 ProcessCommand (event
);
346 void wxTextCtrl
::OnDropFiles(wxDropFilesEvent
& event
)
348 // By default, load the first file into the text window.
349 if (event
.GetNumberOfFiles() > 0)
351 LoadFile(event
.GetFiles()[0]);
355 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
356 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
358 //=========================================================================
359 // Called then the buffer is full (gcc 2.6.3)
360 // or when "endl" is output (Borland 4.5)
361 //=========================================================================
362 // Class declaration using multiple inheritance doesn't work properly for
363 // Borland. See note in wb_text.h.
364 #ifndef NO_TEXT_WINDOW_STREAM
365 int wxTextCtrl
::overflow(int c
)
367 // Make sure there is a holding area
368 if ( allocate()==EOF
)
370 wxError("Streambuf allocation failed","Internal error");
374 // Verify that there are no characters in get area
375 if ( gptr() && gptr() < egptr() )
377 wxError("Who's trespassing my get area?","Internal error");
384 // Make sure there is a put area
387 /* This doesn't seem to be fatal so comment out error message */
388 // wxError("Put area not opened","Internal error");
389 setp( base(), base() );
392 // Determine how many characters have been inserted but no consumed
393 int plen
= pptr() - pbase();
395 // Now Jerry relies on the fact that the buffer is at least 2 chars
396 // long, but the holding area "may be as small as 1" ???
397 // And we need an additional \0, so let's keep this inefficient but
400 // If c!=EOF, it is a character that must also be comsumed
401 int xtra
= c
==EOF?
0 : 1;
403 // Write temporary C-string to wxTextWindow
405 char *txt
= new char[plen
+xtra
+1];
406 memcpy(txt
, pbase(), plen
);
407 txt
[plen
] = (char)c
; // append c
408 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
409 // If the put area already contained \0, output will be truncated there
415 setp(pbase(), epptr());
417 #if defined(__WATCOMC__)
419 #elif defined(zapeof) // HP-UX (all cfront based?)
422 return c
!=EOF ? c
: 0; // this should make everybody happy
426 //=========================================================================
427 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
428 //=========================================================================
429 int wxTextCtrl
::sync()
431 // Verify that there are no characters in get area
432 if ( gptr() && gptr() < egptr() )
434 wxError("Who's trespassing my get area?","Internal error");
438 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
442 int len = pptr() - pbase();
443 char *txt = new char[len+1];
444 strncpy(txt, pbase(), len);
447 setp(pbase(), epptr());
453 //=========================================================================
454 // Should not be called by a "ostream". Used by a "istream"
455 //=========================================================================
456 int wxTextCtrl
::underflow()
462 wxTextCtrl
& wxTextCtrl
::operator<<(const wxString
& s
)
468 wxTextCtrl
& wxTextCtrl
::operator<<(float f
)
471 str
.Printf("%.2f", f
);
476 wxTextCtrl
& wxTextCtrl
::operator<<(double d
)
479 str
.Printf("%.2f", d
);
484 wxTextCtrl
& wxTextCtrl
::operator<<(int i
)
492 wxTextCtrl
& wxTextCtrl
::operator<<(long i
)
495 str
.Printf("%ld", i
);
500 wxTextCtrl
& wxTextCtrl
::operator<<(const char c
)
510 void wxTextCtrl
::OnCut(wxCommandEvent
& event
)
515 void wxTextCtrl
::OnCopy(wxCommandEvent
& event
)
520 void wxTextCtrl
::OnPaste(wxCommandEvent
& event
)
525 void wxTextCtrl
::OnUndo(wxCommandEvent
& event
)
530 void wxTextCtrl
::OnRedo(wxCommandEvent
& event
)
535 void wxTextCtrl
::OnUpdateCut(wxUpdateUIEvent
& event
)
537 event
.Enable( CanCut() );
540 void wxTextCtrl
::OnUpdateCopy(wxUpdateUIEvent
& event
)
542 event
.Enable( CanCopy() );
545 void wxTextCtrl
::OnUpdatePaste(wxUpdateUIEvent
& event
)
547 event
.Enable( CanPaste() );
550 void wxTextCtrl
::OnUpdateUndo(wxUpdateUIEvent
& event
)
552 event
.Enable( CanUndo() );
555 void wxTextCtrl
::OnUpdateRedo(wxUpdateUIEvent
& event
)
557 event
.Enable( CanRedo() );