]>
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 #if !USE_SHARED_LIBRARY
34 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
36 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
37 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
42 wxTextCtrl::wxTextCtrl()
43 #ifndef NO_TEXT_WINDOW_STREAM
50 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
51 const wxString
& value
,
53 const wxSize
& size
, long style
,
54 const wxValidator
& validator
,
59 SetValidator(validator
);
60 if (parent
) parent
->AddChild(this);
62 m_windowStyle
= style
;
65 m_windowId
= (int)NewControlId();
72 wxString
wxTextCtrl::GetValue() const
78 void wxTextCtrl::SetValue(const wxString
& value
)
83 void wxTextCtrl::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
88 // Clipboard operations
89 void wxTextCtrl::Copy()
94 void wxTextCtrl::Cut()
99 void wxTextCtrl::Paste()
104 void wxTextCtrl::SetEditable(bool editable
)
109 void wxTextCtrl::SetInsertionPoint(long pos
)
114 void wxTextCtrl::SetInsertionPointEnd()
116 long pos
= GetLastPosition();
117 SetInsertionPoint(pos
);
120 long wxTextCtrl::GetInsertionPoint() const
126 long wxTextCtrl::GetLastPosition() const
132 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
137 void wxTextCtrl::Remove(long from
, long to
)
142 void wxTextCtrl::SetSelection(long from
, long to
)
147 bool wxTextCtrl::LoadFile(const wxString
& file
)
149 if (!wxFileExists(file
))
156 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
160 struct stat stat_buf
;
161 if (stat(file
, &stat_buf
) < 0)
163 // This may need to be a bigger buffer than the file size suggests,
164 // if it's a UNIX file. Give it an extra 1000 just in case.
165 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
168 while (!input
.eof() && input
.peek() != EOF
)
170 input
.getline(wxBuffer
, 500);
171 int len
= strlen(wxBuffer
);
173 wxBuffer
[len
+1] = 10;
175 strcpy(tmp_buffer
+pos
, wxBuffer
);
176 pos
+= strlen(wxBuffer
);
189 // If file is null, try saved file name first
190 // Returns TRUE if succeeds.
191 bool wxTextCtrl::SaveFile(const wxString
& file
)
193 wxString
theFile(file
);
195 theFile
= m_fileName
;
198 m_fileName
= theFile
;
200 ofstream
output((char*) (const char*) theFile
);
204 // TODO get and save text
209 void wxTextCtrl::WriteText(const wxString
& text
)
211 // TODO write text to control
214 void wxTextCtrl::AppendText(const wxString
& text
)
216 // TODO append text to control
219 void wxTextCtrl::Clear()
224 bool wxTextCtrl::IsModified() const
230 // Makes 'unmodified'
231 void wxTextCtrl::DiscardEdits()
236 int wxTextCtrl::GetNumberOfLines() const
242 long wxTextCtrl::XYToPosition(long x
, long y
) const
248 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
253 void wxTextCtrl::ShowPosition(long pos
)
258 int wxTextCtrl::GetLineLength(long lineNo
) const
264 wxString
wxTextCtrl::GetLineText(long lineNo
) const
270 bool wxTextCtrl::CanCopy() const
272 // Can copy if there's a selection
274 GetSelection(& from
, & to
);
275 return (from
!= to
) ;
278 bool wxTextCtrl::CanCut() const
280 // Can cut if there's a selection
282 GetSelection(& from
, & to
);
283 return (from
!= to
) ;
286 bool wxTextCtrl::CanPaste() const
288 return IsEditable() ;
292 void wxTextCtrl::Undo()
297 void wxTextCtrl::Redo()
302 bool wxTextCtrl::CanUndo() const
308 bool wxTextCtrl::CanRedo() const
314 // If the return values from and to are the same, there is no
316 void wxTextCtrl::GetSelection(long* from
, long* to
) const
323 bool wxTextCtrl::IsEditable() const
329 void wxTextCtrl::Command(wxCommandEvent
& event
)
331 SetValue (event
.GetString());
332 ProcessCommand (event
);
335 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
337 // By default, load the first file into the text window.
338 if (event
.GetNumberOfFiles() > 0)
340 LoadFile(event
.GetFiles()[0]);
344 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
345 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
347 //=========================================================================
348 // Called then the buffer is full (gcc 2.6.3)
349 // or when "endl" is output (Borland 4.5)
350 //=========================================================================
351 // Class declaration using multiple inheritance doesn't work properly for
352 // Borland. See note in wb_text.h.
353 #ifndef NO_TEXT_WINDOW_STREAM
354 int wxTextCtrl::overflow(int c
)
356 // Make sure there is a holding area
357 if ( allocate()==EOF
)
359 wxError("Streambuf allocation failed","Internal error");
363 // Verify that there are no characters in get area
364 if ( gptr() && gptr() < egptr() )
366 wxError("Who's trespassing my get area?","Internal error");
373 // Make sure there is a put area
376 /* This doesn't seem to be fatal so comment out error message */
377 // wxError("Put area not opened","Internal error");
378 setp( base(), base() );
381 // Determine how many characters have been inserted but no consumed
382 int plen
= pptr() - pbase();
384 // Now Jerry relies on the fact that the buffer is at least 2 chars
385 // long, but the holding area "may be as small as 1" ???
386 // And we need an additional \0, so let's keep this inefficient but
389 // If c!=EOF, it is a character that must also be comsumed
390 int xtra
= c
==EOF
? 0 : 1;
392 // Write temporary C-string to wxTextWindow
394 char *txt
= new char[plen
+xtra
+1];
395 memcpy(txt
, pbase(), plen
);
396 txt
[plen
] = (char)c
; // append c
397 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
398 // If the put area already contained \0, output will be truncated there
404 setp(pbase(), epptr());
406 #if defined(__WATCOMC__)
408 #elif defined(zapeof) // HP-UX (all cfront based?)
411 return c
!=EOF
? c
: 0; // this should make everybody happy
415 //=========================================================================
416 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
417 //=========================================================================
418 int wxTextCtrl::sync()
420 // Verify that there are no characters in get area
421 if ( gptr() && gptr() < egptr() )
423 wxError("Who's trespassing my get area?","Internal error");
427 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
431 int len = pptr() - pbase();
432 char *txt = new char[len+1];
433 strncpy(txt, pbase(), len);
436 setp(pbase(), epptr());
442 //=========================================================================
443 // Should not be called by a "ostream". Used by a "istream"
444 //=========================================================================
445 int wxTextCtrl::underflow()
451 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
457 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
460 str
.Printf("%.2f", f
);
465 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
468 str
.Printf("%.2f", d
);
473 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
481 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
484 str
.Printf("%ld", i
);
489 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)