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