]>
git.saurik.com Git - wxWidgets.git/blob - src/stubs/textctrl.cpp
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 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
35 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
36 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
37 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
38 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
39 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
40 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
41 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
43 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
44 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
45 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
46 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
47 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
51 wxTextCtrl::wxTextCtrl()
52 #ifndef NO_TEXT_WINDOW_STREAM
59 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
60 const wxString
& value
,
62 const wxSize
& size
, long style
,
63 const wxValidator
& validator
,
68 SetValidator(validator
);
69 if (parent
) parent
->AddChild(this);
71 m_windowStyle
= style
;
74 m_windowId
= (int)NewControlId();
81 wxString
wxTextCtrl::GetValue() const
87 void wxTextCtrl::SetValue(const wxString
& value
)
92 void wxTextCtrl::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
97 // Clipboard operations
98 void wxTextCtrl::Copy()
103 void wxTextCtrl::Cut()
108 void wxTextCtrl::Paste()
113 void wxTextCtrl::SetEditable(bool editable
)
118 void wxTextCtrl::SetInsertionPoint(long pos
)
123 void wxTextCtrl::SetInsertionPointEnd()
125 long pos
= GetLastPosition();
126 SetInsertionPoint(pos
);
129 long wxTextCtrl::GetInsertionPoint() const
135 long wxTextCtrl::GetLastPosition() const
141 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
146 void wxTextCtrl::Remove(long from
, long to
)
151 void wxTextCtrl::SetSelection(long from
, long to
)
156 bool wxTextCtrl::LoadFile(const wxString
& file
)
158 if (!wxFileExists(file
))
165 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
169 struct stat stat_buf
;
170 if (stat(file
, &stat_buf
) < 0)
172 // This may need to be a bigger buffer than the file size suggests,
173 // if it's a UNIX file. Give it an extra 1000 just in case.
174 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
177 while (!input
.eof() && input
.peek() != EOF
)
179 input
.getline(wxBuffer
, 500);
180 int len
= strlen(wxBuffer
);
182 wxBuffer
[len
+1] = 10;
184 strcpy(tmp_buffer
+pos
, wxBuffer
);
185 pos
+= strlen(wxBuffer
);
198 // If file is null, try saved file name first
199 // Returns TRUE if succeeds.
200 bool wxTextCtrl::SaveFile(const wxString
& file
)
202 wxString
theFile(file
);
204 theFile
= m_fileName
;
207 m_fileName
= theFile
;
209 ofstream
output((char*) (const char*) theFile
);
213 // TODO get and save text
218 void wxTextCtrl::WriteText(const wxString
& text
)
220 // TODO write text to control
223 void wxTextCtrl::AppendText(const wxString
& text
)
225 // TODO append text to control
228 void wxTextCtrl::Clear()
233 bool wxTextCtrl::IsModified() const
239 // Makes 'unmodified'
240 void wxTextCtrl::DiscardEdits()
245 int wxTextCtrl::GetNumberOfLines() const
251 long wxTextCtrl::XYToPosition(long x
, long y
) const
257 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
262 void wxTextCtrl::ShowPosition(long pos
)
267 int wxTextCtrl::GetLineLength(long lineNo
) const
273 wxString
wxTextCtrl::GetLineText(long lineNo
) const
279 bool wxTextCtrl::CanCopy() const
281 // Can copy if there's a selection
283 GetSelection(& from
, & to
);
284 return (from
!= to
) ;
287 bool wxTextCtrl::CanCut() const
289 // Can cut if there's a selection
291 GetSelection(& from
, & to
);
292 return (from
!= to
) ;
295 bool wxTextCtrl::CanPaste() const
297 return IsEditable() ;
301 void wxTextCtrl::Undo()
306 void wxTextCtrl::Redo()
311 bool wxTextCtrl::CanUndo() const
317 bool wxTextCtrl::CanRedo() const
323 // If the return values from and to are the same, there is no
325 void wxTextCtrl::GetSelection(long* from
, long* to
) const
332 bool wxTextCtrl::IsEditable() const
338 void wxTextCtrl::Command(wxCommandEvent
& event
)
340 SetValue (event
.GetString());
341 ProcessCommand (event
);
344 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
346 // By default, load the first file into the text window.
347 if (event
.GetNumberOfFiles() > 0)
349 LoadFile(event
.GetFiles()[0]);
353 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
354 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
356 //=========================================================================
357 // Called then the buffer is full (gcc 2.6.3)
358 // or when "endl" is output (Borland 4.5)
359 //=========================================================================
360 // Class declaration using multiple inheritance doesn't work properly for
361 // Borland. See note in wb_text.h.
362 #ifndef NO_TEXT_WINDOW_STREAM
363 int wxTextCtrl::overflow(int c
)
365 // Make sure there is a holding area
366 if ( allocate()==EOF
)
368 wxError("Streambuf allocation failed","Internal error");
372 // Verify that there are no characters in get area
373 if ( gptr() && gptr() < egptr() )
375 wxError("Who's trespassing my get area?","Internal error");
382 // Make sure there is a put area
385 /* This doesn't seem to be fatal so comment out error message */
386 // wxError("Put area not opened","Internal error");
387 setp( base(), base() );
390 // Determine how many characters have been inserted but no consumed
391 int plen
= pptr() - pbase();
393 // Now Jerry relies on the fact that the buffer is at least 2 chars
394 // long, but the holding area "may be as small as 1" ???
395 // And we need an additional \0, so let's keep this inefficient but
398 // If c!=EOF, it is a character that must also be comsumed
399 int xtra
= c
==EOF
? 0 : 1;
401 // Write temporary C-string to wxTextWindow
403 char *txt
= new char[plen
+xtra
+1];
404 memcpy(txt
, pbase(), plen
);
405 txt
[plen
] = (char)c
; // append c
406 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
407 // If the put area already contained \0, output will be truncated there
413 setp(pbase(), epptr());
415 #if defined(__WATCOMC__)
417 #elif defined(zapeof) // HP-UX (all cfront based?)
420 return c
!=EOF
? c
: 0; // this should make everybody happy
424 //=========================================================================
425 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
426 //=========================================================================
427 int wxTextCtrl::sync()
429 // Verify that there are no characters in get area
430 if ( gptr() && gptr() < egptr() )
432 wxError("Who's trespassing my get area?","Internal error");
436 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
440 int len = pptr() - pbase();
441 char *txt = new char[len+1];
442 strncpy(txt, pbase(), len);
445 setp(pbase(), epptr());
451 //=========================================================================
452 // Should not be called by a "ostream". Used by a "istream"
453 //=========================================================================
454 int wxTextCtrl::underflow()
460 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
466 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
469 str
.Printf("%.2f", f
);
474 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
477 str
.Printf("%.2f", d
);
482 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
490 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
493 str
.Printf("%ld", i
);
498 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)
508 void wxTextCtrl::OnCut(wxCommandEvent
& event
)
513 void wxTextCtrl::OnCopy(wxCommandEvent
& event
)
518 void wxTextCtrl::OnPaste(wxCommandEvent
& event
)
523 void wxTextCtrl::OnUndo(wxCommandEvent
& event
)
528 void wxTextCtrl::OnRedo(wxCommandEvent
& event
)
533 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
535 event
.Enable( CanCut() );
538 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
540 event
.Enable( CanCopy() );
543 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
545 event
.Enable( CanPaste() );
548 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
550 event
.Enable( CanUndo() );
553 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
555 event
.Enable( CanRedo() );