]> git.saurik.com Git - wxWidgets.git/blob - src/mac/textctrl.cpp
Added (untested) support for sub-locales.
[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::Clear()
224 {
225 // TODO
226 }
227
228 bool wxTextCtrl::IsModified() const
229 {
230 // TODO
231 return FALSE;
232 }
233
234 // Makes 'unmodified'
235 void wxTextCtrl::DiscardEdits()
236 {
237 // TODO
238 }
239
240 int wxTextCtrl::GetNumberOfLines() const
241 {
242 // TODO
243 return 0;
244 }
245
246 long wxTextCtrl::XYToPosition(long x, long y) const
247 {
248 // TODO
249 return 0;
250 }
251
252 void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const
253 {
254 // TODO
255 }
256
257 void wxTextCtrl::ShowPosition(long pos)
258 {
259 // TODO
260 }
261
262 int wxTextCtrl::GetLineLength(long lineNo) const
263 {
264 // TODO
265 return 0;
266 }
267
268 wxString wxTextCtrl::GetLineText(long lineNo) const
269 {
270 // TODO
271 return wxString("");
272 }
273
274 /*
275 * Text item
276 */
277
278 void wxTextCtrl::Command(wxCommandEvent & event)
279 {
280 SetValue (event.GetString());
281 ProcessCommand (event);
282 }
283
284 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
285 {
286 // By default, load the first file into the text window.
287 if (event.GetNumberOfFiles() > 0)
288 {
289 LoadFile(event.GetFiles()[0]);
290 }
291 }
292
293 // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of
294 // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers
295
296 //=========================================================================
297 // Called then the buffer is full (gcc 2.6.3)
298 // or when "endl" is output (Borland 4.5)
299 //=========================================================================
300 // Class declaration using multiple inheritance doesn't work properly for
301 // Borland. See note in wb_text.h.
302 #ifndef NO_TEXT_WINDOW_STREAM
303 int wxTextCtrl::overflow(int c)
304 {
305 // Make sure there is a holding area
306 if ( allocate()==EOF )
307 {
308 wxError("Streambuf allocation failed","Internal error");
309 return EOF;
310 }
311
312 // Verify that there are no characters in get area
313 if ( gptr() && gptr() < egptr() )
314 {
315 wxError("Who's trespassing my get area?","Internal error");
316 return EOF;
317 }
318
319 // Reset get area
320 setg(0,0,0);
321
322 // Make sure there is a put area
323 if ( ! pptr() )
324 {
325 /* This doesn't seem to be fatal so comment out error message */
326 // wxError("Put area not opened","Internal error");
327 setp( base(), base() );
328 }
329
330 // Determine how many characters have been inserted but no consumed
331 int plen = pptr() - pbase();
332
333 // Now Jerry relies on the fact that the buffer is at least 2 chars
334 // long, but the holding area "may be as small as 1" ???
335 // And we need an additional \0, so let's keep this inefficient but
336 // safe copy.
337
338 // If c!=EOF, it is a character that must also be comsumed
339 int xtra = c==EOF? 0 : 1;
340
341 // Write temporary C-string to wxTextWindow
342 {
343 char *txt = new char[plen+xtra+1];
344 memcpy(txt, pbase(), plen);
345 txt[plen] = (char)c; // append c
346 txt[plen+xtra] = '\0'; // append '\0' or overwrite c
347 // If the put area already contained \0, output will be truncated there
348 WriteText(txt);
349 delete[] txt;
350 }
351
352 // Reset put area
353 setp(pbase(), epptr());
354
355 #if defined(__WATCOMC__)
356 return __NOT_EOF;
357 #elif defined(zapeof) // HP-UX (all cfront based?)
358 return zapeof(c);
359 #else
360 return c!=EOF ? c : 0; // this should make everybody happy
361 #endif
362 }
363
364 //=========================================================================
365 // called then "endl" is output (gcc) or then explicit sync is done (Borland)
366 //=========================================================================
367 int wxTextCtrl::sync()
368 {
369 // Verify that there are no characters in get area
370 if ( gptr() && gptr() < egptr() )
371 {
372 wxError("Who's trespassing my get area?","Internal error");
373 return EOF;
374 }
375
376 if ( pptr() && pptr() > pbase() ) return overflow(EOF);
377
378 return 0;
379 /* OLD CODE
380 int len = pptr() - pbase();
381 char *txt = new char[len+1];
382 strncpy(txt, pbase(), len);
383 txt[len] = '\0';
384 (*this) << txt;
385 setp(pbase(), epptr());
386 delete[] txt;
387 return 0;
388 */
389 }
390
391 //=========================================================================
392 // Should not be called by a "ostream". Used by a "istream"
393 //=========================================================================
394 int wxTextCtrl::underflow()
395 {
396 return EOF;
397 }
398 #endif
399
400 wxTextCtrl& wxTextCtrl::operator<<(const wxString& s)
401 {
402 WriteText(s);
403 return *this;
404 }
405
406 wxTextCtrl& wxTextCtrl::operator<<(float f)
407 {
408 wxString str;
409 str.Printf("%.2f", f);
410 WriteText(str);
411 return *this;
412 }
413
414 wxTextCtrl& wxTextCtrl::operator<<(double d)
415 {
416 wxString str;
417 str.Printf("%.2f", d);
418 WriteText(str);
419 return *this;
420 }
421
422 wxTextCtrl& wxTextCtrl::operator<<(int i)
423 {
424 wxString str;
425 str.Printf("%d", i);
426 WriteText(str);
427 return *this;
428 }
429
430 wxTextCtrl& wxTextCtrl::operator<<(long i)
431 {
432 wxString str;
433 str.Printf("%ld", i);
434 WriteText(str);
435 return *this;
436 }
437
438 wxTextCtrl& wxTextCtrl::operator<<(const char c)
439 {
440 char buf[2];
441
442 buf[0] = c;
443 buf[1] = 0;
444 WriteText(buf);
445 return *this;
446 }
447