]>
git.saurik.com Git - wxWidgets.git/blob - src/mac/carbon/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::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
283 void wxTextCtrl::Command(wxCommandEvent
& event
)
285 SetValue (event
.GetString());
286 ProcessCommand (event
);
289 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
291 // By default, load the first file into the text window.
292 if (event
.GetNumberOfFiles() > 0)
294 LoadFile(event
.GetFiles()[0]);
298 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
299 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
301 //=========================================================================
302 // Called then the buffer is full (gcc 2.6.3)
303 // or when "endl" is output (Borland 4.5)
304 //=========================================================================
305 // Class declaration using multiple inheritance doesn't work properly for
306 // Borland. See note in wb_text.h.
307 #ifndef NO_TEXT_WINDOW_STREAM
308 int wxTextCtrl::overflow(int c
)
310 // Make sure there is a holding area
311 if ( allocate()==EOF
)
313 wxError("Streambuf allocation failed","Internal error");
317 // Verify that there are no characters in get area
318 if ( gptr() && gptr() < egptr() )
320 wxError("Who's trespassing my get area?","Internal error");
327 // Make sure there is a put area
330 /* This doesn't seem to be fatal so comment out error message */
331 // wxError("Put area not opened","Internal error");
332 setp( base(), base() );
335 // Determine how many characters have been inserted but no consumed
336 int plen
= pptr() - pbase();
338 // Now Jerry relies on the fact that the buffer is at least 2 chars
339 // long, but the holding area "may be as small as 1" ???
340 // And we need an additional \0, so let's keep this inefficient but
343 // If c!=EOF, it is a character that must also be comsumed
344 int xtra
= c
==EOF
? 0 : 1;
346 // Write temporary C-string to wxTextWindow
348 char *txt
= new char[plen
+xtra
+1];
349 memcpy(txt
, pbase(), plen
);
350 txt
[plen
] = (char)c
; // append c
351 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
352 // If the put area already contained \0, output will be truncated there
358 setp(pbase(), epptr());
360 #if defined(__WATCOMC__)
362 #elif defined(zapeof) // HP-UX (all cfront based?)
365 return c
!=EOF
? c
: 0; // this should make everybody happy
369 //=========================================================================
370 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
371 //=========================================================================
372 int wxTextCtrl::sync()
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");
381 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
385 int len = pptr() - pbase();
386 char *txt = new char[len+1];
387 strncpy(txt, pbase(), len);
390 setp(pbase(), epptr());
396 //=========================================================================
397 // Should not be called by a "ostream". Used by a "istream"
398 //=========================================================================
399 int wxTextCtrl::underflow()
405 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
411 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
414 str
.Printf("%.2f", f
);
419 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
422 str
.Printf("%.2f", d
);
427 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
435 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
438 str
.Printf("%ld", i
);
443 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)