]>
Commit | Line | Data |
---|---|---|
93cf77c0 JS |
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 | #include <sys/types.h> | |
17 | #include <sys/stat.h> | |
18 | #include <fstream.h> | |
19 | ||
20 | #include "wx/textctrl.h" | |
21 | #include "wx/settings.h" | |
34138703 JS |
22 | #include "wx/filefn.h" |
23 | #include "wx/utils.h" | |
93cf77c0 JS |
24 | |
25 | #if defined(__BORLANDC__) && !defined(__WIN32__) | |
26 | #include <alloc.h> | |
27 | #else | |
28 | #ifndef __GNUWIN32__ | |
29 | #include <malloc.h> | |
30 | #endif | |
31 | #endif | |
32 | ||
33 | #if !USE_SHARED_LIBRARY | |
34 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) | |
35 | ||
36 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
93cf77c0 | 37 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) |
93cf77c0 JS |
38 | END_EVENT_TABLE() |
39 | #endif | |
40 | ||
41 | // Text item | |
42 | wxTextCtrl::wxTextCtrl() | |
43 | #ifndef NO_TEXT_WINDOW_STREAM | |
44 | :streambuf() | |
45 | #endif | |
46 | { | |
47 | m_fileName = ""; | |
48 | } | |
49 | ||
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) | |
56 | { | |
57 | m_fileName = ""; | |
58 | SetName(name); | |
59 | SetValidator(validator); | |
60 | if (parent) parent->AddChild(this); | |
61 | ||
62 | m_windowStyle = style; | |
63 | ||
64 | if ( id == -1 ) | |
65 | m_windowId = (int)NewControlId(); | |
66 | else | |
67 | m_windowId = id; | |
68 | ||
69 | return TRUE; | |
70 | } | |
71 | ||
72 | wxString wxTextCtrl::GetValue() const | |
73 | { | |
74 | // TODO | |
75 | return wxString(""); | |
76 | } | |
77 | ||
78 | void wxTextCtrl::SetValue(const wxString& value) | |
79 | { | |
80 | // TODO | |
81 | } | |
82 | ||
83 | void wxTextCtrl::SetSize(int x, int y, int width, int height, int sizeFlags) | |
84 | { | |
85 | // TODO | |
86 | } | |
87 | ||
88 | // Clipboard operations | |
89 | void wxTextCtrl::Copy() | |
90 | { | |
91 | // TODO | |
92 | } | |
93 | ||
94 | void wxTextCtrl::Cut() | |
95 | { | |
96 | // TODO | |
97 | } | |
98 | ||
99 | void wxTextCtrl::Paste() | |
100 | { | |
101 | // TODO | |
102 | } | |
103 | ||
104 | void wxTextCtrl::SetEditable(bool editable) | |
105 | { | |
106 | // TODO | |
107 | } | |
108 | ||
109 | void wxTextCtrl::SetInsertionPoint(long pos) | |
110 | { | |
111 | // TODO | |
112 | } | |
113 | ||
114 | void wxTextCtrl::SetInsertionPointEnd() | |
115 | { | |
116 | long pos = GetLastPosition(); | |
117 | SetInsertionPoint(pos); | |
118 | } | |
119 | ||
120 | long wxTextCtrl::GetInsertionPoint() const | |
121 | { | |
122 | // TODO | |
123 | return 0; | |
124 | } | |
125 | ||
126 | long wxTextCtrl::GetLastPosition() const | |
127 | { | |
128 | // TODO | |
129 | return 0; | |
130 | } | |
131 | ||
132 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
133 | { | |
134 | // TODO | |
93cf77c0 JS |
135 | } |
136 | ||
137 | void wxTextCtrl::Remove(long from, long to) | |
138 | { | |
139 | // TODO | |
140 | } | |
141 | ||
142 | void wxTextCtrl::SetSelection(long from, long to) | |
143 | { | |
144 | // TODO | |
145 | } | |
146 | ||
147 | bool wxTextCtrl::LoadFile(const wxString& file) | |
148 | { | |
149 | if (!wxFileExists(file)) | |
150 | return FALSE; | |
151 | ||
152 | m_fileName = file; | |
153 | ||
154 | Clear(); | |
155 | ||
156 | ifstream input((char*) (const char*) file, ios::nocreate | ios::in); | |
157 | ||
158 | if (!input.bad()) | |
159 | { | |
160 | struct stat stat_buf; | |
161 | if (stat(file, &stat_buf) < 0) | |
162 | return FALSE; | |
163 | // This may need to be a bigger buffer than the file size suggests, | |
164 | // if it's a UNIX file. Give it an extra 1000 just in case. | |
165 | char *tmp_buffer = (char*)malloc((size_t)(stat_buf.st_size+1+1000)); | |
166 | long no_lines = 0; | |
167 | long pos = 0; | |
168 | while (!input.eof() && input.peek() != EOF) | |
169 | { | |
170 | input.getline(wxBuffer, 500); | |
171 | int len = strlen(wxBuffer); | |
172 | wxBuffer[len] = 13; | |
173 | wxBuffer[len+1] = 10; | |
174 | wxBuffer[len+2] = 0; | |
175 | strcpy(tmp_buffer+pos, wxBuffer); | |
176 | pos += strlen(wxBuffer); | |
177 | no_lines++; | |
178 | } | |
179 | ||
180 | // TODO add line | |
181 | ||
182 | free(tmp_buffer); | |
183 | ||
184 | return TRUE; | |
185 | } | |
186 | return FALSE; | |
187 | } | |
188 | ||
189 | // If file is null, try saved file name first | |
190 | // Returns TRUE if succeeds. | |
191 | bool wxTextCtrl::SaveFile(const wxString& file) | |
192 | { | |
193 | wxString theFile(file); | |
194 | if (theFile == "") | |
195 | theFile = m_fileName; | |
196 | if (theFile == "") | |
197 | return FALSE; | |
198 | m_fileName = theFile; | |
199 | ||
200 | ofstream output((char*) (const char*) theFile); | |
201 | if (output.bad()) | |
202 | return FALSE; | |
203 | ||
204 | // TODO get and save text | |
205 | ||
206 | return FALSE; | |
207 | } | |
208 | ||
209 | void wxTextCtrl::WriteText(const wxString& text) | |
210 | { | |
211 | // TODO write text to control | |
212 | } | |
213 | ||
13c21be5 HH |
214 | void wxTextCtrl::AppendText(const wxString& text) |
215 | { | |
216 | // TODO append text to control | |
217 | } | |
218 | ||
93cf77c0 JS |
219 | void wxTextCtrl::Clear() |
220 | { | |
221 | // TODO | |
222 | } | |
223 | ||
224 | bool wxTextCtrl::IsModified() const | |
225 | { | |
226 | // TODO | |
227 | return FALSE; | |
228 | } | |
229 | ||
230 | // Makes 'unmodified' | |
231 | void wxTextCtrl::DiscardEdits() | |
232 | { | |
233 | // TODO | |
234 | } | |
235 | ||
236 | int wxTextCtrl::GetNumberOfLines() const | |
237 | { | |
238 | // TODO | |
239 | return 0; | |
240 | } | |
241 | ||
242 | long wxTextCtrl::XYToPosition(long x, long y) const | |
243 | { | |
244 | // TODO | |
245 | return 0; | |
246 | } | |
247 | ||
248 | void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
249 | { | |
250 | // TODO | |
251 | } | |
252 | ||
253 | void wxTextCtrl::ShowPosition(long pos) | |
254 | { | |
255 | // TODO | |
256 | } | |
257 | ||
258 | int wxTextCtrl::GetLineLength(long lineNo) const | |
259 | { | |
260 | // TODO | |
261 | return 0; | |
262 | } | |
263 | ||
264 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
265 | { | |
266 | // TODO | |
267 | return wxString(""); | |
268 | } | |
269 | ||
270 | /* | |
271 | * Text item | |
272 | */ | |
273 | ||
274 | void wxTextCtrl::Command(wxCommandEvent & event) | |
275 | { | |
276 | SetValue (event.GetString()); | |
277 | ProcessCommand (event); | |
278 | } | |
279 | ||
280 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
281 | { | |
282 | // By default, load the first file into the text window. | |
283 | if (event.GetNumberOfFiles() > 0) | |
284 | { | |
285 | LoadFile(event.GetFiles()[0]); | |
286 | } | |
287 | } | |
288 | ||
289 | // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of | |
290 | // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers | |
291 | ||
292 | //========================================================================= | |
293 | // Called then the buffer is full (gcc 2.6.3) | |
294 | // or when "endl" is output (Borland 4.5) | |
295 | //========================================================================= | |
296 | // Class declaration using multiple inheritance doesn't work properly for | |
297 | // Borland. See note in wb_text.h. | |
298 | #ifndef NO_TEXT_WINDOW_STREAM | |
299 | int wxTextCtrl::overflow(int c) | |
300 | { | |
301 | // Make sure there is a holding area | |
302 | if ( allocate()==EOF ) | |
303 | { | |
304 | wxError("Streambuf allocation failed","Internal error"); | |
305 | return EOF; | |
306 | } | |
307 | ||
308 | // Verify that there are no characters in get area | |
309 | if ( gptr() && gptr() < egptr() ) | |
310 | { | |
311 | wxError("Who's trespassing my get area?","Internal error"); | |
312 | return EOF; | |
313 | } | |
314 | ||
315 | // Reset get area | |
316 | setg(0,0,0); | |
317 | ||
318 | // Make sure there is a put area | |
319 | if ( ! pptr() ) | |
320 | { | |
321 | /* This doesn't seem to be fatal so comment out error message */ | |
322 | // wxError("Put area not opened","Internal error"); | |
323 | setp( base(), base() ); | |
324 | } | |
325 | ||
326 | // Determine how many characters have been inserted but no consumed | |
327 | int plen = pptr() - pbase(); | |
328 | ||
329 | // Now Jerry relies on the fact that the buffer is at least 2 chars | |
330 | // long, but the holding area "may be as small as 1" ??? | |
331 | // And we need an additional \0, so let's keep this inefficient but | |
332 | // safe copy. | |
333 | ||
334 | // If c!=EOF, it is a character that must also be comsumed | |
335 | int xtra = c==EOF? 0 : 1; | |
336 | ||
337 | // Write temporary C-string to wxTextWindow | |
338 | { | |
339 | char *txt = new char[plen+xtra+1]; | |
340 | memcpy(txt, pbase(), plen); | |
341 | txt[plen] = (char)c; // append c | |
342 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c | |
343 | // If the put area already contained \0, output will be truncated there | |
13c21be5 | 344 | AppendText(txt); |
93cf77c0 JS |
345 | delete[] txt; |
346 | } | |
347 | ||
348 | // Reset put area | |
349 | setp(pbase(), epptr()); | |
350 | ||
351 | #if defined(__WATCOMC__) | |
352 | return __NOT_EOF; | |
353 | #elif defined(zapeof) // HP-UX (all cfront based?) | |
354 | return zapeof(c); | |
355 | #else | |
356 | return c!=EOF ? c : 0; // this should make everybody happy | |
357 | #endif | |
358 | } | |
359 | ||
360 | //========================================================================= | |
361 | // called then "endl" is output (gcc) or then explicit sync is done (Borland) | |
362 | //========================================================================= | |
363 | int wxTextCtrl::sync() | |
364 | { | |
365 | // Verify that there are no characters in get area | |
366 | if ( gptr() && gptr() < egptr() ) | |
367 | { | |
368 | wxError("Who's trespassing my get area?","Internal error"); | |
369 | return EOF; | |
370 | } | |
371 | ||
372 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); | |
373 | ||
374 | return 0; | |
375 | /* OLD CODE | |
376 | int len = pptr() - pbase(); | |
377 | char *txt = new char[len+1]; | |
378 | strncpy(txt, pbase(), len); | |
379 | txt[len] = '\0'; | |
380 | (*this) << txt; | |
381 | setp(pbase(), epptr()); | |
382 | delete[] txt; | |
383 | return 0; | |
384 | */ | |
385 | } | |
386 | ||
387 | //========================================================================= | |
388 | // Should not be called by a "ostream". Used by a "istream" | |
389 | //========================================================================= | |
390 | int wxTextCtrl::underflow() | |
391 | { | |
392 | return EOF; | |
393 | } | |
394 | #endif | |
395 | ||
396 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
397 | { | |
13c21be5 | 398 | AppendText(s); |
93cf77c0 JS |
399 | return *this; |
400 | } | |
401 | ||
402 | wxTextCtrl& wxTextCtrl::operator<<(float f) | |
403 | { | |
404 | wxString str; | |
405 | str.Printf("%.2f", f); | |
13c21be5 | 406 | AppendText(str); |
93cf77c0 JS |
407 | return *this; |
408 | } | |
409 | ||
410 | wxTextCtrl& wxTextCtrl::operator<<(double d) | |
411 | { | |
412 | wxString str; | |
413 | str.Printf("%.2f", d); | |
13c21be5 | 414 | AppendText(str); |
93cf77c0 JS |
415 | return *this; |
416 | } | |
417 | ||
418 | wxTextCtrl& wxTextCtrl::operator<<(int i) | |
419 | { | |
420 | wxString str; | |
421 | str.Printf("%d", i); | |
13c21be5 | 422 | AppendText(str); |
93cf77c0 JS |
423 | return *this; |
424 | } | |
425 | ||
426 | wxTextCtrl& wxTextCtrl::operator<<(long i) | |
427 | { | |
428 | wxString str; | |
429 | str.Printf("%ld", i); | |
13c21be5 | 430 | AppendText(str); |
93cf77c0 JS |
431 | return *this; |
432 | } | |
433 | ||
434 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
435 | { | |
436 | char buf[2]; | |
437 | ||
438 | buf[0] = c; | |
439 | buf[1] = 0; | |
13c21be5 | 440 | AppendText(buf); |
93cf77c0 JS |
441 | return *this; |
442 | } | |
443 |