]> git.saurik.com Git - wxWidgets.git/blame - src/qt/textctrl.cpp
Removed XPM lib references from makefile
[wxWidgets.git] / src / qt / textctrl.cpp
CommitLineData
7c78e7c7
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: textctrl.cpp
01b2eeec
KB
3// Purpose: wxTextCtrl
4// Author: AUTHOR
5// Modified by:
6// Created: ??/??/98
7// RCS-ID: $Id$
8// Copyright: (c) AUTHOR
9// Licence: wxWindows licence
7c78e7c7
RR
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "textctrl.h"
14#endif
15
01b2eeec
KB
16#include <sys/types.h>
17#include <sys/stat.h>
18#include <fstream.h>
7c78e7c7 19
01b2eeec
KB
20#include "wx/textctrl.h"
21#include "wx/settings.h"
7c78e7c7 22
01b2eeec
KB
23#if defined(__BORLANDC__) && !defined(__WIN32__)
24#include <alloc.h>
25#else
26#ifndef __GNUWIN32__
27#include <malloc.h>
28#endif
29#endif
7c78e7c7 30
01b2eeec 31IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl)
7c78e7c7
RR
32
33BEGIN_EVENT_TABLE(wxTextCtrl, wxControl)
01b2eeec
KB
34 EVT_CHAR(wxTextCtrl::OnChar)
35 EVT_DROP_FILES(wxTextCtrl::OnDropFiles)
36 EVT_ERASE_BACKGROUND(wxTextCtrl::OnEraseBackground)
7c78e7c7
RR
37END_EVENT_TABLE()
38
01b2eeec
KB
39// Text item
40wxTextCtrl::wxTextCtrl()
41#ifndef NO_TEXT_WINDOW_STREAM
42 :streambuf()
43#endif
7c78e7c7 44{
01b2eeec
KB
45 m_fileName = "";
46}
7c78e7c7 47
01b2eeec
KB
48bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id,
49 const wxString& value,
50 const wxPoint& pos,
51 const wxSize& size, long style,
52 const wxValidator& validator,
53 const wxString& name)
7c78e7c7 54{
01b2eeec
KB
55 m_fileName = "";
56 SetName(name);
57 SetValidator(validator);
58 if (parent) parent->AddChild(this);
59
60 m_windowStyle = style;
7c78e7c7 61
01b2eeec
KB
62 if ( id == -1 )
63 m_windowId = (int)NewControlId();
64 else
65 m_windowId = id;
7c78e7c7 66
01b2eeec
KB
67 return TRUE;
68}
69
70wxString wxTextCtrl::GetValue() const
7c78e7c7 71{
01b2eeec
KB
72 // TODO
73 return wxString("");
74}
7c78e7c7 75
01b2eeec 76void wxTextCtrl::SetValue(const wxString& value)
7c78e7c7 77{
01b2eeec
KB
78 // TODO
79}
7c78e7c7 80
01b2eeec 81void wxTextCtrl::SetSize(int x, int y, int width, int height, int sizeFlags)
7c78e7c7 82{
01b2eeec
KB
83 // TODO
84}
7c78e7c7 85
01b2eeec
KB
86// Clipboard operations
87void wxTextCtrl::Copy()
7c78e7c7 88{
01b2eeec
KB
89 // TODO
90}
7c78e7c7 91
01b2eeec 92void wxTextCtrl::Cut()
7c78e7c7 93{
01b2eeec
KB
94 // TODO
95}
7c78e7c7 96
01b2eeec 97void wxTextCtrl::Paste()
7c78e7c7 98{
01b2eeec
KB
99 // TODO
100}
7c78e7c7 101
01b2eeec 102void wxTextCtrl::SetEditable(bool editable)
7c78e7c7 103{
01b2eeec
KB
104 // TODO
105}
7c78e7c7 106
01b2eeec
KB
107void wxTextCtrl::SetInsertionPoint(long pos)
108{
109 // TODO
110}
7c78e7c7 111
01b2eeec 112void wxTextCtrl::SetInsertionPointEnd()
7c78e7c7 113{
01b2eeec
KB
114 long pos = GetLastPosition();
115 SetInsertionPoint(pos);
116}
7c78e7c7 117
01b2eeec 118long wxTextCtrl::GetInsertionPoint() const
7c78e7c7 119{
01b2eeec
KB
120 // TODO
121 return 0;
122}
7c78e7c7 123
01b2eeec 124long wxTextCtrl::GetLastPosition() const
7c78e7c7 125{
01b2eeec
KB
126 // TODO
127 return 0;
128}
7c78e7c7 129
01b2eeec 130void wxTextCtrl::Replace(long from, long to, const wxString& value)
7c78e7c7 131{
01b2eeec
KB
132 // TODO
133 return 0;
134}
7c78e7c7 135
01b2eeec 136void wxTextCtrl::Remove(long from, long to)
7c78e7c7 137{
01b2eeec
KB
138 // TODO
139}
7c78e7c7 140
01b2eeec 141void wxTextCtrl::SetSelection(long from, long to)
7c78e7c7 142{
01b2eeec
KB
143 // TODO
144}
145
146bool wxTextCtrl::LoadFile(const wxString& file)
147{
148 if (!wxFileExists(file))
149 return FALSE;
150
151 m_fileName = file;
152
153 Clear();
154
155 ifstream input((char*) (const char*) file, ios::nocreate | ios::in);
156
157 if (!input.bad())
158 {
159 struct stat stat_buf;
160 if (stat(file, &stat_buf) < 0)
161 return FALSE;
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));
165 long no_lines = 0;
166 long pos = 0;
167 while (!input.eof() && input.peek() != EOF)
168 {
169 input.getline(wxBuffer, 500);
170 int len = strlen(wxBuffer);
171 wxBuffer[len] = 13;
172 wxBuffer[len+1] = 10;
173 wxBuffer[len+2] = 0;
174 strcpy(tmp_buffer+pos, wxBuffer);
175 pos += strlen(wxBuffer);
176 no_lines++;
177 }
178
179 // TODO add line
180
181 free(tmp_buffer);
182
183 return TRUE;
184 }
185 return FALSE;
186}
7c78e7c7 187
01b2eeec
KB
188// If file is null, try saved file name first
189// Returns TRUE if succeeds.
190bool wxTextCtrl::SaveFile(const wxString& file)
7c78e7c7 191{
01b2eeec
KB
192 wxString theFile(file);
193 if (theFile == "")
194 theFile = m_fileName;
195 if (theFile == "")
196 return FALSE;
197 m_fileName = theFile;
198
199 ofstream output((char*) (const char*) theFile);
200 if (output.bad())
201 return FALSE;
202
203 // TODO get and save text
204
205 return FALSE;
206}
7c78e7c7 207
01b2eeec 208void wxTextCtrl::WriteText(const wxString& text)
7c78e7c7 209{
01b2eeec
KB
210 // TODO write text to control
211}
7c78e7c7 212
13c21be5
HH
213void wxTextCtrl::AppendText(const wxString& text)
214{
215 // TODO append text to control
216}
217
01b2eeec 218void wxTextCtrl::Clear()
7c78e7c7 219{
01b2eeec
KB
220 // TODO
221}
7c78e7c7 222
01b2eeec 223bool wxTextCtrl::IsModified() const
7c78e7c7 224{
01b2eeec
KB
225 // TODO
226 return FALSE;
227}
7c78e7c7 228
01b2eeec
KB
229// Makes 'unmodified'
230void wxTextCtrl::DiscardEdits()
7c78e7c7 231{
01b2eeec
KB
232 // TODO
233}
7c78e7c7 234
01b2eeec 235int wxTextCtrl::GetNumberOfLines() const
7c78e7c7 236{
01b2eeec
KB
237 // TODO
238 return 0;
239}
7c78e7c7 240
01b2eeec 241long wxTextCtrl::XYToPosition(long x, long y) const
7c78e7c7 242{
01b2eeec
KB
243 // TODO
244 return 0;
245}
7c78e7c7 246
01b2eeec 247void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
7c78e7c7 248{
01b2eeec
KB
249 // TODO
250}
7c78e7c7 251
01b2eeec 252void wxTextCtrl::ShowPosition(long pos)
7c78e7c7 253{
01b2eeec
KB
254 // TODO
255}
7c78e7c7 256
01b2eeec 257int wxTextCtrl::GetLineLength(long lineNo) const
7c78e7c7 258{
01b2eeec
KB
259 // TODO
260 return 0;
261}
7c78e7c7 262
01b2eeec 263wxString wxTextCtrl::GetLineText(long lineNo) const
7c78e7c7 264{
01b2eeec
KB
265 // TODO
266 return wxString("");
267}
7c78e7c7 268
01b2eeec
KB
269/*
270 * Text item
271 */
272
273void wxTextCtrl::Command(wxCommandEvent & event)
7c78e7c7 274{
01b2eeec
KB
275 SetValue (event.GetString());
276 ProcessCommand (event);
277}
7c78e7c7 278
01b2eeec 279void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
7c78e7c7 280{
01b2eeec
KB
281 // By default, load the first file into the text window.
282 if (event.GetNumberOfFiles() > 0)
283 {
284 LoadFile(event.GetFiles()[0]);
285 }
286}
287
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
290
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
298int wxTextCtrl::overflow(int c)
299{
300 // Make sure there is a holding area
301 if ( allocate()==EOF )
302 {
303 wxError("Streambuf allocation failed","Internal error");
304 return EOF;
305 }
306
307 // Verify that there are no characters in get area
308 if ( gptr() && gptr() < egptr() )
309 {
310 wxError("Who's trespassing my get area?","Internal error");
311 return EOF;
312 }
313
314 // Reset get area
315 setg(0,0,0);
316
317 // Make sure there is a put area
318 if ( ! pptr() )
319 {
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() );
323 }
324
325 // Determine how many characters have been inserted but no consumed
326 int plen = pptr() - pbase();
327
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
331 // safe copy.
332
333 // If c!=EOF, it is a character that must also be comsumed
334 int xtra = c==EOF? 0 : 1;
335
336 // Write temporary C-string to wxTextWindow
337 {
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
13c21be5 343 AppendText(txt);
01b2eeec
KB
344 delete[] txt;
345 }
346
347 // Reset put area
7c78e7c7 348 setp(pbase(), epptr());
7c78e7c7 349
01b2eeec
KB
350#if defined(__WATCOMC__)
351 return __NOT_EOF;
352#elif defined(zapeof) // HP-UX (all cfront based?)
353 return zapeof(c);
354#else
355 return c!=EOF ? c : 0; // this should make everybody happy
356#endif
357}
358
359//=========================================================================
360// called then "endl" is output (gcc) or then explicit sync is done (Borland)
361//=========================================================================
362int wxTextCtrl::sync()
7c78e7c7 363{
01b2eeec
KB
364 // Verify that there are no characters in get area
365 if ( gptr() && gptr() < egptr() )
366 {
367 wxError("Who's trespassing my get area?","Internal error");
368 return EOF;
369 }
370
371 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
372
373 return 0;
374/* OLD CODE
7c78e7c7
RR
375 int len = pptr() - pbase();
376 char *txt = new char[len+1];
377 strncpy(txt, pbase(), len);
378 txt[len] = '\0';
379 (*this) << txt;
380 setp(pbase(), epptr());
381 delete[] txt;
382 return 0;
01b2eeec
KB
383*/
384}
7c78e7c7 385
01b2eeec
KB
386//=========================================================================
387// Should not be called by a "ostream". Used by a "istream"
388//=========================================================================
389int wxTextCtrl::underflow()
7c78e7c7
RR
390{
391 return EOF;
01b2eeec
KB
392}
393#endif
7c78e7c7
RR
394
395wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
396{
13c21be5 397 AppendText(s);
01b2eeec 398 return *this;
7c78e7c7
RR
399}
400
401wxTextCtrl& wxTextCtrl::operator<<(float f)
402{
01b2eeec
KB
403 wxString str;
404 str.Printf("%.2f", f);
13c21be5 405 AppendText(str);
01b2eeec 406 return *this;
7c78e7c7
RR
407}
408
409wxTextCtrl& wxTextCtrl::operator<<(double d)
410{
01b2eeec
KB
411 wxString str;
412 str.Printf("%.2f", d);
13c21be5 413 AppendText(str);
01b2eeec 414 return *this;
7c78e7c7
RR
415}
416
417wxTextCtrl& wxTextCtrl::operator<<(int i)
418{
01b2eeec
KB
419 wxString str;
420 str.Printf("%d", i);
13c21be5 421 AppendText(str);
01b2eeec 422 return *this;
7c78e7c7
RR
423}
424
425wxTextCtrl& wxTextCtrl::operator<<(long i)
426{
01b2eeec
KB
427 wxString str;
428 str.Printf("%ld", i);
13c21be5 429 AppendText(str);
01b2eeec 430 return *this;
7c78e7c7
RR
431}
432
433wxTextCtrl& wxTextCtrl::operator<<(const char c)
434{
01b2eeec 435 char buf[2];
7c78e7c7 436
01b2eeec
KB
437 buf[0] = c;
438 buf[1] = 0;
13c21be5 439 AppendText(buf);
01b2eeec 440 return *this;
7c78e7c7
RR
441}
442