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