]>
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 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxControl
)
33 BEGIN_EVENT_TABLE(wxTextCtrl
, wxControl
)
34 EVT_CHAR(wxTextCtrl::OnChar
)
35 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
36 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground
)
40 wxTextCtrl::wxTextCtrl()
41 #ifndef NO_TEXT_WINDOW_STREAM
48 bool wxTextCtrl::Create(wxWindow
*parent
, wxWindowID id
,
49 const wxString
& value
,
51 const wxSize
& size
, long style
,
52 const wxValidator
& validator
,
57 SetValidator(validator
);
58 if (parent
) parent
->AddChild(this);
60 m_windowStyle
= style
;
63 m_windowId
= (int)NewControlId();
70 wxString
wxTextCtrl::GetValue() const
76 void wxTextCtrl::SetValue(const wxString
& value
)
81 void wxTextCtrl::SetSize(int x
, int y
, int width
, int height
, int sizeFlags
)
86 // Clipboard operations
87 void wxTextCtrl::Copy()
92 void wxTextCtrl::Cut()
97 void wxTextCtrl::Paste()
102 void wxTextCtrl::SetEditable(bool editable
)
107 void wxTextCtrl::SetInsertionPoint(long pos
)
112 void wxTextCtrl::SetInsertionPointEnd()
114 long pos
= GetLastPosition();
115 SetInsertionPoint(pos
);
118 long wxTextCtrl::GetInsertionPoint() const
124 long wxTextCtrl::GetLastPosition() const
130 void wxTextCtrl::Replace(long from
, long to
, const wxString
& value
)
136 void wxTextCtrl::Remove(long from
, long to
)
141 void wxTextCtrl::SetSelection(long from
, long to
)
146 bool wxTextCtrl::LoadFile(const wxString
& file
)
148 if (!wxFileExists(file
))
155 ifstream
input((char*) (const char*) file
, ios::nocreate
| ios::in
);
159 struct stat stat_buf
;
160 if (stat(file
, &stat_buf
) < 0)
162 // This may need to be a bigger buffer than the file size suggests,
163 // if it's a UNIX file. Give it an extra 1000 just in case.
164 char *tmp_buffer
= (char*)malloc((size_t)(stat_buf
.st_size
+1+1000));
167 while (!input
.eof() && input
.peek() != EOF
)
169 input
.getline(wxBuffer
, 500);
170 int len
= strlen(wxBuffer
);
172 wxBuffer
[len
+1] = 10;
174 strcpy(tmp_buffer
+pos
, wxBuffer
);
175 pos
+= strlen(wxBuffer
);
188 // If file is null, try saved file name first
189 // Returns TRUE if succeeds.
190 bool wxTextCtrl::SaveFile(const wxString
& file
)
192 wxString
theFile(file
);
194 theFile
= m_fileName
;
197 m_fileName
= theFile
;
199 ofstream
output((char*) (const char*) theFile
);
203 // TODO get and save text
208 void wxTextCtrl::WriteText(const wxString
& text
)
210 // TODO write text to control
213 void wxTextCtrl::AppendText(const wxString
& text
)
215 // TODO append text to control
218 void wxTextCtrl::Clear()
223 bool wxTextCtrl::IsModified() const
229 // Makes 'unmodified'
230 void wxTextCtrl::DiscardEdits()
235 int wxTextCtrl::GetNumberOfLines() const
241 long wxTextCtrl::XYToPosition(long x
, long y
) const
247 void wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
252 void wxTextCtrl::ShowPosition(long pos
)
257 int wxTextCtrl::GetLineLength(long lineNo
) const
263 wxString
wxTextCtrl::GetLineText(long lineNo
) const
273 void wxTextCtrl::Command(wxCommandEvent
& event
)
275 SetValue (event
.GetString());
276 ProcessCommand (event
);
279 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
281 // By default, load the first file into the text window.
282 if (event
.GetNumberOfFiles() > 0)
284 LoadFile(event
.GetFiles()[0]);
288 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
289 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
291 //=========================================================================
292 // Called then the buffer is full (gcc 2.6.3)
293 // or when "endl" is output (Borland 4.5)
294 //=========================================================================
295 // Class declaration using multiple inheritance doesn't work properly for
296 // Borland. See note in wb_text.h.
297 #ifndef NO_TEXT_WINDOW_STREAM
298 int wxTextCtrl::overflow(int c
)
300 // Make sure there is a holding area
301 if ( allocate()==EOF
)
303 wxError("Streambuf allocation failed","Internal error");
307 // Verify that there are no characters in get area
308 if ( gptr() && gptr() < egptr() )
310 wxError("Who's trespassing my get area?","Internal error");
317 // Make sure there is a put area
320 /* This doesn't seem to be fatal so comment out error message */
321 // wxError("Put area not opened","Internal error");
322 setp( base(), base() );
325 // Determine how many characters have been inserted but no consumed
326 int plen
= pptr() - pbase();
328 // Now Jerry relies on the fact that the buffer is at least 2 chars
329 // long, but the holding area "may be as small as 1" ???
330 // And we need an additional \0, so let's keep this inefficient but
333 // If c!=EOF, it is a character that must also be comsumed
334 int xtra
= c
==EOF
? 0 : 1;
336 // Write temporary C-string to wxTextWindow
338 char *txt
= new char[plen
+xtra
+1];
339 memcpy(txt
, pbase(), plen
);
340 txt
[plen
] = (char)c
; // append c
341 txt
[plen
+xtra
] = '\0'; // append '\0' or overwrite c
342 // If the put area already contained \0, output will be truncated there
348 setp(pbase(), epptr());
350 #if defined(__WATCOMC__)
352 #elif defined(zapeof) // HP-UX (all cfront based?)
355 return c
!=EOF
? c
: 0; // this should make everybody happy
359 //=========================================================================
360 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
361 //=========================================================================
362 int wxTextCtrl::sync()
364 // Verify that there are no characters in get area
365 if ( gptr() && gptr() < egptr() )
367 wxError("Who's trespassing my get area?","Internal error");
371 if ( pptr() && pptr() > pbase() ) return overflow(EOF
);
375 int len = pptr() - pbase();
376 char *txt = new char[len+1];
377 strncpy(txt, pbase(), len);
380 setp(pbase(), epptr());
386 //=========================================================================
387 // Should not be called by a "ostream". Used by a "istream"
388 //=========================================================================
389 int wxTextCtrl::underflow()
395 wxTextCtrl
& wxTextCtrl::operator<<(const wxString
& s
)
401 wxTextCtrl
& wxTextCtrl::operator<<(float f
)
404 str
.Printf("%.2f", f
);
409 wxTextCtrl
& wxTextCtrl::operator<<(double d
)
412 str
.Printf("%.2f", d
);
417 wxTextCtrl
& wxTextCtrl::operator<<(int i
)
425 wxTextCtrl
& wxTextCtrl::operator<<(long i
)
428 str
.Printf("%ld", i
);
433 wxTextCtrl
& wxTextCtrl::operator<<(const char c
)