]>
git.saurik.com Git - wxWidgets.git/blob - src/qt/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"
23 #if defined(__BORLANDC__) && !defined(__WIN32__)
31 #if !USE_SHARED_LIBRARY
32 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
34 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
35 EVT_CHAR(wxTextCtrl::OnChar
)
36 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
37 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
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
)
138 void wxTextCtrl::Remove(long from
, long to
)
143 void wxTextCtrl::SetSelection(long from
, long to
)
148 bool wxTextCtrl::LoadFile(const wxString
& file
)
150 if (!wxFileExists(file
))
157 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
161 struct stat stat_buf
;
162 if (stat(file
, &stat_buf
) < 0)
164 // This may need to be a bigger buffer than the file size suggests,
165 // if it's a UNIX file. Give it an extra 1000 just in case.
166 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
169 while (!input
.eof() && input
.peek() != EOF
)
171 input
.getline(wxBuffer
, 500);
172 int len
= strlen(wxBuffer
);
174 wxBuffer
[len
+1] = 10;
176 strcpy(tmp_buffer
+pos
, wxBuffer
);
177 pos
+= strlen(wxBuffer
);
190 // If file is null, try saved file name first
191 // Returns TRUE if succeeds.
192 bool wxTextCtrl::SaveFile(const wxString
& file
)
194 wxString
theFile(file
);
196 theFile
= m_fileName
;
199 m_fileName
= theFile
;
201 ofstream
output((char*) (const char*) theFile
);
205 // TODO get and save text
210 void wxTextCtrl::WriteText(const wxString
& text
)
212 // TODO write text to control
215 void wxTextCtrl::Clear()
220 bool wxTextCtrl::IsModified() const
226 // Makes 'unmodified'
227 void wxTextCtrl::DiscardEdits()
232 int wxTextCtrl::GetNumberOfLines() const
238 long wxTextCtrl::XYToPosition(long x
, long y
) const
244 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
249 void wxTextCtrl::ShowPosition(long pos
)
254 int wxTextCtrl::GetLineLength(long lineNo
) const
260 wxString
wxTextCtrl::GetLineText(long lineNo
) const
270 void wxTextCtrl::Command(wxCommandEvent
& event
)
272 SetValue (event
.GetString());
273 ProcessCommand (event
);
276 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
278 // By default, load the first file into the text window.
279 if (event
.GetNumberOfFiles() > 0)
281 LoadFile(event
.GetFiles()[0]);
285 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
286 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
288 //=========================================================================
289 // Called then the buffer is full (gcc 2.6.3)
290 // or when "endl" is output (Borland 4.5)
291 //=========================================================================
292 // Class declaration using multiple inheritance doesn't work properly for
293 // Borland. See note in wb_text.h.
294 #ifndef NO_TEXT_WINDOW_STREAM
295 int wxTextCtrl::overflow(int c
)
297 // Make sure there is a holding area
298 if ( allocate()==EOF
)
300 wxError("Streambuf allocation failed","Internal error");
304 // Verify that there are no characters in get area
305 if ( gptr() && gptr() < egptr() )
307 wxError("Who's trespassing my get area?","Internal error");
314 // Make sure there is a put area
317 /* This doesn't seem to be fatal so comment out error message */
318 // wxError("Put area not opened","Internal error");
319 setp( base(), base() );
322 // Determine how many characters have been inserted but no consumed
323 int plen
= pptr() - pbase();
325 // Now Jerry relies on the fact that the buffer is at least 2 chars
326 // long, but the holding area "may be as small as 1" ???
327 // And we need an additional \0, so let's keep this inefficient but
330 // If c!=EOF, it is a character that must also be comsumed
331 int xtra
= c
==EOF
? 0 : 1;
333 // Write temporary C-string to wxTextWindow
335 char *txt
= new char[plen
+xtra
+1];
336 memcpy(txt
, pbase(), plen
);
337 txt
[plen
] = (char)c
; // append c
338 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
339 // If the put area already contained \0, output will be truncated there
345 setp(pbase(), epptr());
347 #if defined(__WATCOMC__)
349 #elif defined(zapeof) // HP-UX (all cfront based?)
352 return c
!=EOF
? c
: 0; // this should make everybody happy
356 //=========================================================================
357 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
358 //=========================================================================
359 int wxTextCtrl::sync()
361 // Verify that there are no characters in get area
362 if ( gptr() && gptr() < egptr() )
364 wxError("Who's trespassing my get area?","Internal error");
368 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
372 int len = pptr() - pbase();
373 char *txt = new char[len+1];
374 strncpy(txt, pbase(), len);
377 setp(pbase(), epptr());
383 //=========================================================================
384 // Should not be called by a "ostream". Used by a "istream"
385 //=========================================================================
386 int wxTextCtrl::underflow()
392 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
398 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
401 str
.Printf("%.2f", f
);
406 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
409 str
.Printf("%.2f", d
);
414 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
422 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
425 str
.Printf("%ld", i
);
430 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)