]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/textctrl.cpp
1 /////////////////////////////////////////////////////////////////////////////
8 // Copyright: (c) AUTHOR
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "textctrl.h"
17 #include <sys/types.h>
24 #include "wx/textctrl.h"
25 #include "wx/settings.h"
26 #include "wx/filefn.h"
29 #if defined(__BORLANDC__) && !defined(__WIN32__)
39 #if !USE_SHARED_LIBRARY
40 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
42 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
43 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
48 wxTextCtrl::wxTextCtrl()
49 #ifndef NO_TEXT_WINDOW_STREAM
56 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
57 const wxString
& value
,
59 const wxSize
& size
, long style
,
60 const wxValidator
& validator
,
65 SetValidator(validator
);
66 if (parent
) parent
->AddChild(this);
68 m_windowStyle
= style
;
71 m_windowId
= (int)NewControlId();
78 wxString
wxTextCtrl::GetValue() const
84 void wxTextCtrl::SetValue(const wxString
& value
)
89 void wxTextCtrl::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
94 // Clipboard operations
95 void wxTextCtrl::Copy()
100 void wxTextCtrl::Cut()
105 void wxTextCtrl::Paste()
110 void wxTextCtrl::SetEditable(bool editable
)
115 void wxTextCtrl::SetInsertionPoint(long pos
)
120 void wxTextCtrl::SetInsertionPointEnd()
122 long pos
= GetLastPosition();
123 SetInsertionPoint(pos
);
126 long wxTextCtrl::GetInsertionPoint() const
132 long wxTextCtrl::GetLastPosition() const
138 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
143 void wxTextCtrl::Remove(long from
, long to
)
148 void wxTextCtrl::SetSelection(long from
, long to
)
153 bool wxTextCtrl::LoadFile(const wxString
& file
)
155 if (!wxFileExists(file
))
163 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
165 ifstream
input((char*) (const char*) file
, 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::Clear()
228 bool wxTextCtrl::IsModified() const
234 // Makes 'unmodified'
235 void wxTextCtrl::DiscardEdits()
240 int wxTextCtrl::GetNumberOfLines() const
246 long wxTextCtrl::XYToPosition(long x
, long y
) const
252 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
257 void wxTextCtrl::ShowPosition(long pos
)
262 int wxTextCtrl::GetLineLength(long lineNo
) const
268 wxString
wxTextCtrl::GetLineText(long lineNo
) const
278 void wxTextCtrl::Command(wxCommandEvent
& event
)
280 SetValue (event
.GetString());
281 ProcessCommand (event
);
284 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
286 // By default, load the first file into the text window.
287 if (event
.GetNumberOfFiles() > 0)
289 LoadFile(event
.GetFiles()[0]);
293 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
294 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
296 //=========================================================================
297 // Called then the buffer is full (gcc 2.6.3)
298 // or when "endl" is output (Borland 4.5)
299 //=========================================================================
300 // Class declaration using multiple inheritance doesn't work properly for
301 // Borland. See note in wb_text.h.
302 #ifndef NO_TEXT_WINDOW_STREAM
303 int wxTextCtrl::overflow(int c
)
305 // Make sure there is a holding area
306 if ( allocate()==EOF
)
308 wxError("Streambuf allocation failed","Internal error");
312 // Verify that there are no characters in get area
313 if ( gptr() && gptr() < egptr() )
315 wxError("Who's trespassing my get area?","Internal error");
322 // Make sure there is a put area
325 /* This doesn't seem to be fatal so comment out error message */
326 // wxError("Put area not opened","Internal error");
327 setp( base(), base() );
330 // Determine how many characters have been inserted but no consumed
331 int plen
= pptr() - pbase();
333 // Now Jerry relies on the fact that the buffer is at least 2 chars
334 // long, but the holding area "may be as small as 1" ???
335 // And we need an additional \0, so let's keep this inefficient but
338 // If c!=EOF, it is a character that must also be comsumed
339 int xtra
= c
==EOF
? 0 : 1;
341 // Write temporary C-string to wxTextWindow
343 char *txt
= new char[plen
+xtra
+1];
344 memcpy(txt
, pbase(), plen
);
345 txt
[plen
] = (char)c
; // append c
346 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
347 // If the put area already contained \0, output will be truncated there
353 setp(pbase(), epptr());
355 #if defined(__WATCOMC__)
357 #elif defined(zapeof) // HP-UX (all cfront based?)
360 return c
!=EOF
? c
: 0; // this should make everybody happy
364 //=========================================================================
365 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
366 //=========================================================================
367 int wxTextCtrl::sync()
369 // Verify that there are no characters in get area
370 if ( gptr() && gptr() < egptr() )
372 wxError("Who's trespassing my get area?","Internal error");
376 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
380 int len = pptr() - pbase();
381 char *txt = new char[len+1];
382 strncpy(txt, pbase(), len);
385 setp(pbase(), epptr());
391 //=========================================================================
392 // Should not be called by a "ostream". Used by a "istream"
393 //=========================================================================
394 int wxTextCtrl::underflow()
400 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
406 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
409 str
.Printf("%.2f", f
);
414 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
417 str
.Printf("%.2f", d
);
422 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
430 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
433 str
.Printf("%ld", i
);
438 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)