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