]>
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_CHAR(wxTextCtrl::OnChar
)
38 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
39 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
44 wxTextCtrl::wxTextCtrl()
45 #ifndef NO_TEXT_WINDOW_STREAM
52 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
53 const wxString
& value
,
55 const wxSize
& size
, long style
,
56 const wxValidator
& validator
,
61 SetValidator(validator
);
62 if (parent
) parent
->AddChild(this);
64 m_windowStyle
= style
;
67 m_windowId
= (int)NewControlId();
74 wxString
wxTextCtrl::GetValue() const
80 void wxTextCtrl::SetValue(const wxString
& value
)
85 void wxTextCtrl::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
90 // Clipboard operations
91 void wxTextCtrl::Copy()
96 void wxTextCtrl::Cut()
101 void wxTextCtrl::Paste()
106 void wxTextCtrl::SetEditable(bool editable
)
111 void wxTextCtrl::SetInsertionPoint(long pos
)
116 void wxTextCtrl::SetInsertionPointEnd()
118 long pos
= GetLastPosition();
119 SetInsertionPoint(pos
);
122 long wxTextCtrl::GetInsertionPoint() const
128 long wxTextCtrl::GetLastPosition() const
134 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
139 void wxTextCtrl::Remove(long from
, long to
)
144 void wxTextCtrl::SetSelection(long from
, long to
)
149 bool wxTextCtrl::LoadFile(const wxString
& file
)
151 if (!wxFileExists(file
))
158 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
162 struct stat stat_buf
;
163 if (stat(file
, &stat_buf
) < 0)
165 // This may need to be a bigger buffer than the file size suggests,
166 // if it's a UNIX file. Give it an extra 1000 just in case.
167 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
170 while (!input
.eof() && input
.peek() != EOF
)
172 input
.getline(wxBuffer
, 500);
173 int len
= strlen(wxBuffer
);
175 wxBuffer
[len
+1] = 10;
177 strcpy(tmp_buffer
+pos
, wxBuffer
);
178 pos
+= strlen(wxBuffer
);
191 // If file is null, try saved file name first
192 // Returns TRUE if succeeds.
193 bool wxTextCtrl::SaveFile(const wxString
& file
)
195 wxString
theFile(file
);
197 theFile
= m_fileName
;
200 m_fileName
= theFile
;
202 ofstream
output((char*) (const char*) theFile
);
206 // TODO get and save text
211 void wxTextCtrl::WriteText(const wxString
& text
)
213 // TODO write text to control
216 void wxTextCtrl::Clear()
221 bool wxTextCtrl::IsModified() const
227 // Makes 'unmodified'
228 void wxTextCtrl::DiscardEdits()
233 int wxTextCtrl::GetNumberOfLines() const
239 long wxTextCtrl::XYToPosition(long x
, long y
) const
245 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
250 void wxTextCtrl::ShowPosition(long pos
)
255 int wxTextCtrl::GetLineLength(long lineNo
) const
261 wxString
wxTextCtrl::GetLineText(long lineNo
) const
271 void wxTextCtrl::Command(wxCommandEvent
& event
)
273 SetValue (event
.GetString());
274 ProcessCommand (event
);
277 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
279 // By default, load the first file into the text window.
280 if (event
.GetNumberOfFiles() > 0)
282 LoadFile(event
.GetFiles()[0]);
286 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
287 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
289 //=========================================================================
290 // Called then the buffer is full (gcc 2.6.3)
291 // or when "endl" is output (Borland 4.5)
292 //=========================================================================
293 // Class declaration using multiple inheritance doesn't work properly for
294 // Borland. See note in wb_text.h.
295 #ifndef NO_TEXT_WINDOW_STREAM
296 int wxTextCtrl::overflow(int c
)
298 // Make sure there is a holding area
299 if ( allocate()==EOF
)
301 wxError("Streambuf allocation failed","Internal error");
305 // Verify that there are no characters in get area
306 if ( gptr() && gptr() < egptr() )
308 wxError("Who's trespassing my get area?","Internal error");
315 // Make sure there is a put area
318 /* This doesn't seem to be fatal so comment out error message */
319 // wxError("Put area not opened","Internal error");
320 setp( base(), base() );
323 // Determine how many characters have been inserted but no consumed
324 int plen
= pptr() - pbase();
326 // Now Jerry relies on the fact that the buffer is at least 2 chars
327 // long, but the holding area "may be as small as 1" ???
328 // And we need an additional \0, so let's keep this inefficient but
331 // If c!=EOF, it is a character that must also be comsumed
332 int xtra
= c
==EOF
? 0 : 1;
334 // Write temporary C-string to wxTextWindow
336 char *txt
= new char[plen
+xtra
+1];
337 memcpy(txt
, pbase(), plen
);
338 txt
[plen
] = (char)c
; // append c
339 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
340 // If the put area already contained \0, output will be truncated there
346 setp(pbase(), epptr());
348 #if defined(__WATCOMC__)
350 #elif defined(zapeof) // HP-UX (all cfront based?)
353 return c
!=EOF
? c
: 0; // this should make everybody happy
357 //=========================================================================
358 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
359 //=========================================================================
360 int wxTextCtrl::sync()
362 // Verify that there are no characters in get area
363 if ( gptr() && gptr() < egptr() )
365 wxError("Who's trespassing my get area?","Internal error");
369 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
373 int len = pptr() - pbase();
374 char *txt = new char[len+1];
375 strncpy(txt, pbase(), len);
378 setp(pbase(), epptr());
384 //=========================================================================
385 // Should not be called by a "ostream". Used by a "istream"
386 //=========================================================================
387 int wxTextCtrl::underflow()
393 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
399 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
402 str
.Printf("%.2f", f
);
407 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
410 str
.Printf("%.2f", d
);
415 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
423 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
426 str
.Printf("%ld", i
);
431 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)