]>
Commit | Line | Data |
---|---|---|
4bb6408c JS |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: textctrl.cpp | |
3 | // Purpose: wxTextCtrl | |
4 | // Author: Julian Smart | |
5 | // Modified by: | |
6 | // Created: 17/09/98 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Julian Smart | |
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" | |
22 | #include "wx/filefn.h" | |
23 | #include "wx/utils.h" | |
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 | ||
02e8b2f9 JS |
33 | #include <Xm/Text.h> |
34 | #include <sys/types.h> | |
35 | #include <sys/stat.h> | |
36 | #include <ctype.h> | |
37 | ||
38 | #include "wx/motif/private.h" | |
39 | ||
40 | static void | |
41 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr); | |
42 | static void | |
43 | wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs); | |
44 | static void | |
45 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
46 | static void | |
47 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
dfc54541 | 48 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, |
2d120f83 | 49 | XmAnyCallbackStruct *ptr); |
02e8b2f9 | 50 | |
4bb6408c JS |
51 | #if !USE_SHARED_LIBRARY |
52 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) | |
53 | ||
54 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) | |
2d120f83 JS |
55 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) |
56 | EVT_CHAR(wxTextCtrl::OnChar) | |
4bb6408c JS |
57 | END_EVENT_TABLE() |
58 | #endif | |
59 | ||
60 | // Text item | |
61 | wxTextCtrl::wxTextCtrl() | |
62 | #ifndef NO_TEXT_WINDOW_STREAM | |
2d120f83 | 63 | :streambuf() |
4bb6408c JS |
64 | #endif |
65 | { | |
66 | m_fileName = ""; | |
02e8b2f9 JS |
67 | m_tempCallbackStruct = (void*) NULL; |
68 | m_modified = FALSE; | |
dfc54541 | 69 | m_processedDefault = FALSE; |
4bb6408c JS |
70 | } |
71 | ||
72 | bool wxTextCtrl::Create(wxWindow *parent, wxWindowID id, | |
2d120f83 JS |
73 | const wxString& value, |
74 | const wxPoint& pos, | |
75 | const wxSize& size, long style, | |
76 | const wxValidator& validator, | |
77 | const wxString& name) | |
4bb6408c | 78 | { |
02e8b2f9 JS |
79 | m_tempCallbackStruct = (void*) NULL; |
80 | m_modified = FALSE; | |
dfc54541 | 81 | m_processedDefault = FALSE; |
4bb6408c | 82 | m_fileName = ""; |
94b49b93 JS |
83 | // m_backgroundColour = parent->GetBackgroundColour(); |
84 | m_backgroundColour = * wxWHITE; | |
0d57be45 | 85 | m_foregroundColour = parent->GetForegroundColour(); |
2d120f83 | 86 | |
4bb6408c JS |
87 | SetName(name); |
88 | SetValidator(validator); | |
89 | if (parent) parent->AddChild(this); | |
2d120f83 | 90 | |
4bb6408c | 91 | m_windowStyle = style; |
2d120f83 | 92 | |
4bb6408c | 93 | if ( id == -1 ) |
2d120f83 | 94 | m_windowId = (int)NewControlId(); |
4bb6408c | 95 | else |
2d120f83 JS |
96 | m_windowId = id; |
97 | ||
02e8b2f9 | 98 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
2d120f83 | 99 | |
02e8b2f9 | 100 | bool wantHorizScrolling = ((m_windowStyle & wxHSCROLL) != 0); |
2d120f83 | 101 | |
02e8b2f9 JS |
102 | // If we don't have horizontal scrollbars, we want word wrap. |
103 | bool wantWordWrap = !wantHorizScrolling; | |
2d120f83 | 104 | |
02e8b2f9 JS |
105 | if (m_windowStyle & wxTE_MULTILINE) |
106 | { | |
107 | Arg args[2]; | |
108 | XtSetArg (args[0], XmNscrollHorizontal, wantHorizScrolling ? True : False); | |
109 | XtSetArg (args[1], XmNwordWrap, wantWordWrap ? True : False); | |
2d120f83 | 110 | |
02e8b2f9 | 111 | m_mainWidget = (WXWidget) XmCreateScrolledText (parentWidget, (char*) (const char*) name, args, 2); |
2d120f83 | 112 | |
02e8b2f9 | 113 | XtVaSetValues ((Widget) m_mainWidget, |
2d120f83 JS |
114 | XmNeditable, ((style & wxTE_READONLY) ? False : True), |
115 | XmNeditMode, XmMULTI_LINE_EDIT, | |
116 | NULL); | |
02e8b2f9 JS |
117 | XtManageChild ((Widget) m_mainWidget); |
118 | } | |
119 | else | |
120 | { | |
121 | m_mainWidget = (WXWidget) XtVaCreateManagedWidget ((char*) (const char*) name, | |
2d120f83 JS |
122 | xmTextWidgetClass, parentWidget, |
123 | NULL); | |
124 | ||
02e8b2f9 JS |
125 | // TODO: Is this relevant? What does it do? |
126 | int noCols = 2; | |
127 | if (!value.IsNull() && (value.Length() > (unsigned int) noCols)) | |
128 | noCols = value.Length(); | |
129 | XtVaSetValues ((Widget) m_mainWidget, | |
2d120f83 JS |
130 | XmNcolumns, noCols, |
131 | NULL); | |
02e8b2f9 | 132 | } |
2d120f83 | 133 | |
02e8b2f9 JS |
134 | if (!value.IsNull()) |
135 | XmTextSetString ((Widget) m_mainWidget, (char*) (const char*) value); | |
2d120f83 | 136 | |
02e8b2f9 | 137 | XtAddCallback((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc)wxTextWindowChangedProc, (XtPointer)this); |
2d120f83 | 138 | |
02e8b2f9 | 139 | XtAddCallback((Widget) m_mainWidget, XmNmodifyVerifyCallback, (XtCallbackProc)wxTextWindowModifyProc, (XtPointer)this); |
2d120f83 | 140 | |
dfc54541 | 141 | XtAddCallback((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc)wxTextWindowActivateProc, (XtPointer)this); |
2d120f83 | 142 | |
02e8b2f9 | 143 | XtAddCallback((Widget) m_mainWidget, XmNfocusCallback, (XtCallbackProc)wxTextWindowGainFocusProc, (XtPointer)this); |
2d120f83 | 144 | |
02e8b2f9 | 145 | XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this); |
2d120f83 | 146 | |
4b5f3fe6 JS |
147 | m_windowFont = parent->GetFont(); |
148 | ChangeFont(FALSE); | |
2d120f83 | 149 | |
02e8b2f9 JS |
150 | SetCanAddEventHandler(TRUE); |
151 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
2d120f83 | 152 | |
0d57be45 | 153 | ChangeBackgroundColour(); |
2d120f83 | 154 | |
4bb6408c JS |
155 | return TRUE; |
156 | } | |
157 | ||
02e8b2f9 | 158 | WXWidget wxTextCtrl::GetTopWidget() const |
4bb6408c | 159 | { |
dfc54541 | 160 | return ((m_windowStyle & wxTE_MULTILINE) ? (WXWidget) XtParent((Widget) m_mainWidget) : m_mainWidget); |
4bb6408c JS |
161 | } |
162 | ||
02e8b2f9 | 163 | wxString wxTextCtrl::GetValue() const |
4bb6408c | 164 | { |
dfc54541 JS |
165 | if (m_windowStyle & wxTE_PASSWORD) |
166 | return m_value; | |
167 | else | |
168 | { | |
169 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
170 | if (s) | |
171 | { | |
2d120f83 | 172 | wxString str(s); |
dfc54541 | 173 | XtFree (s); |
2d120f83 JS |
174 | return str; |
175 | } | |
176 | else | |
dfc54541 JS |
177 | { |
178 | return wxEmptyString; | |
179 | } | |
180 | } | |
4bb6408c JS |
181 | } |
182 | ||
02e8b2f9 | 183 | void wxTextCtrl::SetValue(const wxString& value) |
4bb6408c | 184 | { |
2d120f83 JS |
185 | // This assert is wrong -- means that you can't set an empty |
186 | // string (IsNull == IsEmpty). | |
187 | // wxASSERT_MSG( (!value.IsNull()), "Must not pass a null string to wxTextCtrl::SetValue." ) ; | |
dfc54541 | 188 | m_inSetValue = TRUE; |
2d120f83 | 189 | |
dfc54541 | 190 | XmTextSetString ((Widget) m_mainWidget, (char*) (const char*) value); |
2d120f83 | 191 | |
dfc54541 | 192 | m_inSetValue = FALSE; |
4bb6408c JS |
193 | } |
194 | ||
195 | // Clipboard operations | |
196 | void wxTextCtrl::Copy() | |
197 | { | |
dfc54541 | 198 | XmTextCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
199 | } |
200 | ||
201 | void wxTextCtrl::Cut() | |
202 | { | |
dfc54541 | 203 | XmTextCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
204 | } |
205 | ||
206 | void wxTextCtrl::Paste() | |
207 | { | |
dfc54541 | 208 | XmTextPaste((Widget) m_mainWidget); |
4bb6408c JS |
209 | } |
210 | ||
211 | void wxTextCtrl::SetEditable(bool editable) | |
212 | { | |
dfc54541 | 213 | XmTextSetEditable((Widget) m_mainWidget, (Boolean) editable); |
4bb6408c JS |
214 | } |
215 | ||
216 | void wxTextCtrl::SetInsertionPoint(long pos) | |
217 | { | |
dfc54541 | 218 | XmTextSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
219 | } |
220 | ||
221 | void wxTextCtrl::SetInsertionPointEnd() | |
222 | { | |
223 | long pos = GetLastPosition(); | |
224 | SetInsertionPoint(pos); | |
225 | } | |
226 | ||
227 | long wxTextCtrl::GetInsertionPoint() const | |
228 | { | |
dfc54541 | 229 | return (long) XmTextGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
230 | } |
231 | ||
232 | long wxTextCtrl::GetLastPosition() const | |
233 | { | |
dfc54541 | 234 | return (long) XmTextGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
235 | } |
236 | ||
237 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
238 | { | |
dfc54541 | 239 | XmTextReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
2d120f83 | 240 | (char*) (const char*) value); |
4bb6408c JS |
241 | } |
242 | ||
243 | void wxTextCtrl::Remove(long from, long to) | |
244 | { | |
dfc54541 JS |
245 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
246 | (Time) 0); | |
247 | XmTextRemove ((Widget) m_mainWidget); | |
4bb6408c JS |
248 | } |
249 | ||
250 | void wxTextCtrl::SetSelection(long from, long to) | |
251 | { | |
dfc54541 JS |
252 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
253 | (Time) 0); | |
4bb6408c JS |
254 | } |
255 | ||
256 | bool wxTextCtrl::LoadFile(const wxString& file) | |
257 | { | |
258 | if (!wxFileExists(file)) | |
259 | return FALSE; | |
2d120f83 | 260 | |
4bb6408c | 261 | m_fileName = file; |
2d120f83 | 262 | |
4bb6408c | 263 | Clear(); |
2d120f83 | 264 | |
1a3ac83f JS |
265 | Widget textWidget = (Widget) m_mainWidget; |
266 | FILE *fp; | |
2d120f83 | 267 | |
1a3ac83f JS |
268 | struct stat statb; |
269 | if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || | |
2d120f83 | 270 | !(fp = fopen ((char*) (const char*) file, "r"))) |
4bb6408c | 271 | { |
2d120f83 | 272 | return FALSE; |
1a3ac83f JS |
273 | } |
274 | else | |
275 | { | |
2d120f83 JS |
276 | long len = statb.st_size; |
277 | char *text; | |
278 | if (!(text = XtMalloc ((unsigned) (len + 1)))) | |
279 | { | |
280 | fclose (fp); | |
281 | return FALSE; | |
282 | } | |
283 | if (fread (text, sizeof (char), len, fp) != (size_t) len) | |
284 | { | |
285 | } | |
286 | fclose (fp); | |
287 | ||
288 | text[len] = 0; | |
289 | XmTextSetString (textWidget, text); | |
290 | // m_textPosition = len; | |
291 | XtFree (text); | |
292 | m_modified = FALSE; | |
293 | return TRUE; | |
4bb6408c | 294 | } |
4bb6408c JS |
295 | } |
296 | ||
297 | // If file is null, try saved file name first | |
298 | // Returns TRUE if succeeds. | |
299 | bool wxTextCtrl::SaveFile(const wxString& file) | |
300 | { | |
301 | wxString theFile(file); | |
302 | if (theFile == "") | |
303 | theFile = m_fileName; | |
304 | if (theFile == "") | |
305 | return FALSE; | |
306 | m_fileName = theFile; | |
2d120f83 JS |
307 | |
308 | Widget textWidget = (Widget) m_mainWidget; | |
309 | FILE *fp; | |
310 | ||
311 | if (!(fp = fopen ((char*) (const char*) theFile, "w"))) | |
1a3ac83f | 312 | { |
2d120f83 | 313 | return FALSE; |
1a3ac83f | 314 | } |
2d120f83 | 315 | else |
1a3ac83f | 316 | { |
2d120f83 JS |
317 | char *text = XmTextGetString (textWidget); |
318 | long len = XmTextGetLastPosition (textWidget); | |
319 | ||
320 | if (fwrite (text, sizeof (char), len, fp) != (size_t) len) | |
321 | { | |
322 | // Did not write whole file | |
323 | } | |
324 | // Make sure newline terminates the file | |
325 | if (text[len - 1] != '\n') | |
326 | fputc ('\n', fp); | |
327 | ||
328 | fclose (fp); | |
329 | XtFree (text); | |
330 | m_modified = FALSE; | |
331 | return TRUE; | |
1a3ac83f | 332 | } |
4bb6408c JS |
333 | } |
334 | ||
335 | void wxTextCtrl::WriteText(const wxString& text) | |
336 | { | |
dfc54541 JS |
337 | long textPosition = GetInsertionPoint() + strlen (text); |
338 | XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), (char*) (const char*) text); | |
339 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
340 | SetInsertionPoint(textPosition); | |
341 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
342 | m_modified = TRUE; | |
4bb6408c JS |
343 | } |
344 | ||
4dba84be JS |
345 | void wxTextCtrl::AppendText(const wxString& text) |
346 | { | |
347 | long textPosition = GetLastPosition() + strlen(text); | |
348 | XmTextInsert ((Widget) m_mainWidget, GetLastPosition(), (char*) (const char*) text); | |
349 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
350 | SetInsertionPoint(textPosition); | |
351 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
352 | m_modified = TRUE; | |
353 | } | |
354 | ||
4bb6408c JS |
355 | void wxTextCtrl::Clear() |
356 | { | |
02e8b2f9 | 357 | XmTextSetString ((Widget) m_mainWidget, ""); |
02e8b2f9 | 358 | m_modified = FALSE; |
4bb6408c JS |
359 | } |
360 | ||
361 | bool wxTextCtrl::IsModified() const | |
362 | { | |
02e8b2f9 | 363 | return m_modified; |
4bb6408c JS |
364 | } |
365 | ||
366 | // Makes 'unmodified' | |
367 | void wxTextCtrl::DiscardEdits() | |
368 | { | |
dfc54541 JS |
369 | XmTextSetString ((Widget) m_mainWidget, ""); |
370 | m_modified = FALSE; | |
4bb6408c JS |
371 | } |
372 | ||
373 | int wxTextCtrl::GetNumberOfLines() const | |
374 | { | |
dfc54541 JS |
375 | // HIDEOUSLY inefficient, but we have no choice. |
376 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
377 | if (s) | |
378 | { | |
2d120f83 JS |
379 | long i = 0; |
380 | int currentLine = 0; | |
381 | bool finished = FALSE; | |
382 | while (!finished) | |
383 | { | |
384 | int ch = s[i]; | |
385 | if (ch == '\n') | |
386 | { | |
387 | currentLine++; | |
388 | i++; | |
389 | } | |
390 | else if (ch == 0) | |
391 | { | |
392 | finished = TRUE; | |
393 | } | |
394 | else | |
395 | i++; | |
396 | } | |
397 | ||
398 | XtFree (s); | |
399 | return currentLine; | |
dfc54541 | 400 | } |
4bb6408c JS |
401 | return 0; |
402 | } | |
403 | ||
404 | long wxTextCtrl::XYToPosition(long x, long y) const | |
405 | { | |
dfc54541 | 406 | /* It seems, that there is a bug in some versions of the Motif library, |
2d120f83 JS |
407 | so the original wxWin-Code doesn't work. */ |
408 | /* | |
409 | Widget textWidget = (Widget) handle; | |
410 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
411 | */ | |
dfc54541 JS |
412 | /* Now a little workaround: */ |
413 | long r=0; | |
414 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
415 | return r+x; | |
4bb6408c JS |
416 | } |
417 | ||
418 | void wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
419 | { | |
dfc54541 JS |
420 | Position xx, yy; |
421 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
422 | *x = xx; *y = yy; | |
4bb6408c JS |
423 | } |
424 | ||
425 | void wxTextCtrl::ShowPosition(long pos) | |
426 | { | |
dfc54541 | 427 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
428 | } |
429 | ||
430 | int wxTextCtrl::GetLineLength(long lineNo) const | |
431 | { | |
dfc54541 JS |
432 | wxString str = GetLineText (lineNo); |
433 | return (int) str.Length(); | |
4bb6408c JS |
434 | } |
435 | ||
436 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
437 | { | |
dfc54541 JS |
438 | // HIDEOUSLY inefficient, but we have no choice. |
439 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
2d120f83 | 440 | |
dfc54541 JS |
441 | if (s) |
442 | { | |
443 | wxString buf(""); | |
444 | long i; | |
445 | int currentLine = 0; | |
446 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
2d120f83 JS |
447 | if (s[i] == '\n') |
448 | currentLine++; | |
449 | // Now get the text | |
450 | int j; | |
451 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
452 | buf += s[i]; | |
453 | ||
454 | XtFree(s); | |
455 | return buf; | |
456 | } | |
457 | else | |
458 | return wxEmptyString; | |
4bb6408c JS |
459 | } |
460 | ||
461 | /* | |
2d120f83 JS |
462 | * Text item |
463 | */ | |
464 | ||
4bb6408c JS |
465 | void wxTextCtrl::Command(wxCommandEvent & event) |
466 | { | |
467 | SetValue (event.GetString()); | |
468 | ProcessCommand (event); | |
469 | } | |
470 | ||
471 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
472 | { | |
473 | // By default, load the first file into the text window. | |
474 | if (event.GetNumberOfFiles() > 0) | |
475 | { | |
476 | LoadFile(event.GetFiles()[0]); | |
477 | } | |
478 | } | |
479 | ||
480 | // The streambuf code was partly taken from chapter 3 by Jerry Schwarz of | |
481 | // AT&T's "C++ Lanuage System Release 3.0 Library Manual" - Stein Somers | |
482 | ||
483 | //========================================================================= | |
484 | // Called then the buffer is full (gcc 2.6.3) | |
485 | // or when "endl" is output (Borland 4.5) | |
486 | //========================================================================= | |
487 | // Class declaration using multiple inheritance doesn't work properly for | |
488 | // Borland. See note in wb_text.h. | |
489 | #ifndef NO_TEXT_WINDOW_STREAM | |
490 | int wxTextCtrl::overflow(int c) | |
491 | { | |
2d120f83 JS |
492 | // Make sure there is a holding area |
493 | if ( allocate()==EOF ) | |
494 | { | |
495 | wxError("Streambuf allocation failed","Internal error"); | |
496 | return EOF; | |
497 | } | |
498 | ||
499 | // Verify that there are no characters in get area | |
500 | if ( gptr() && gptr() < egptr() ) | |
501 | { | |
502 | wxError("wxTextCtrl::overflow: Who's trespassing my get area?","Internal error"); | |
503 | return EOF; | |
504 | } | |
505 | ||
506 | // Reset get area | |
507 | setg(0,0,0); | |
508 | ||
509 | // Make sure there is a put area | |
510 | if ( ! pptr() ) | |
511 | { | |
512 | /* This doesn't seem to be fatal so comment out error message */ | |
513 | // wxError("Put area not opened","Internal error"); | |
514 | setp( base(), base() ); | |
515 | } | |
516 | ||
517 | // Determine how many characters have been inserted but no consumed | |
518 | int plen = pptr() - pbase(); | |
519 | ||
520 | // Now Jerry relies on the fact that the buffer is at least 2 chars | |
521 | // long, but the holding area "may be as small as 1" ??? | |
522 | // And we need an additional \0, so let's keep this inefficient but | |
523 | // safe copy. | |
524 | ||
525 | // If c!=EOF, it is a character that must also be comsumed | |
526 | int xtra = c==EOF? 0 : 1; | |
527 | ||
528 | // Write temporary C-string to wxTextWindow | |
529 | { | |
530 | char *txt = new char[plen+xtra+1]; | |
531 | memcpy(txt, pbase(), plen); | |
532 | txt[plen] = (char)c; // append c | |
533 | txt[plen+xtra] = '\0'; // append '\0' or overwrite c | |
534 | // If the put area already contained \0, output will be truncated there | |
535 | WriteText(txt); | |
536 | delete[] txt; | |
537 | } | |
538 | ||
539 | // Reset put area | |
540 | setp(pbase(), epptr()); | |
541 | ||
4bb6408c | 542 | #if defined(__WATCOMC__) |
2d120f83 | 543 | return __NOT_EOF; |
4bb6408c | 544 | #elif defined(zapeof) // HP-UX (all cfront based?) |
2d120f83 | 545 | return zapeof(c); |
4bb6408c | 546 | #else |
2d120f83 | 547 | return c!=EOF ? c : 0; // this should make everybody happy |
4bb6408c JS |
548 | #endif |
549 | } | |
550 | ||
551 | //========================================================================= | |
552 | // called then "endl" is output (gcc) or then explicit sync is done (Borland) | |
553 | //========================================================================= | |
554 | int wxTextCtrl::sync() | |
555 | { | |
2d120f83 JS |
556 | // Verify that there are no characters in get area |
557 | if ( gptr() && gptr() < egptr() ) | |
558 | { | |
559 | wxError("Who's trespassing my get area?","Internal error"); | |
560 | return EOF; | |
561 | } | |
562 | ||
563 | if ( pptr() && pptr() > pbase() ) return overflow(EOF); | |
564 | ||
565 | return 0; | |
566 | /* OLD CODE | |
567 | int len = pptr() - pbase(); | |
568 | char *txt = new char[len+1]; | |
569 | strncpy(txt, pbase(), len); | |
570 | txt[len] = '\0'; | |
571 | (*this) << txt; | |
572 | setp(pbase(), epptr()); | |
573 | delete[] txt; | |
574 | return 0; | |
575 | */ | |
4bb6408c JS |
576 | } |
577 | ||
578 | //========================================================================= | |
579 | // Should not be called by a "ostream". Used by a "istream" | |
580 | //========================================================================= | |
581 | int wxTextCtrl::underflow() | |
582 | { | |
2d120f83 | 583 | return EOF; |
4bb6408c JS |
584 | } |
585 | #endif | |
586 | ||
587 | wxTextCtrl& wxTextCtrl::operator<<(const wxString& s) | |
588 | { | |
4dba84be | 589 | AppendText(s); |
4bb6408c JS |
590 | return *this; |
591 | } | |
592 | ||
593 | wxTextCtrl& wxTextCtrl::operator<<(float f) | |
594 | { | |
595 | wxString str; | |
596 | str.Printf("%.2f", f); | |
4dba84be | 597 | AppendText(str); |
4bb6408c JS |
598 | return *this; |
599 | } | |
600 | ||
601 | wxTextCtrl& wxTextCtrl::operator<<(double d) | |
602 | { | |
603 | wxString str; | |
604 | str.Printf("%.2f", d); | |
4dba84be | 605 | AppendText(str); |
4bb6408c JS |
606 | return *this; |
607 | } | |
608 | ||
609 | wxTextCtrl& wxTextCtrl::operator<<(int i) | |
610 | { | |
611 | wxString str; | |
612 | str.Printf("%d", i); | |
4dba84be | 613 | AppendText(str); |
4bb6408c JS |
614 | return *this; |
615 | } | |
616 | ||
617 | wxTextCtrl& wxTextCtrl::operator<<(long i) | |
618 | { | |
619 | wxString str; | |
620 | str.Printf("%ld", i); | |
4dba84be | 621 | AppendText(str); |
4bb6408c JS |
622 | return *this; |
623 | } | |
624 | ||
625 | wxTextCtrl& wxTextCtrl::operator<<(const char c) | |
626 | { | |
627 | char buf[2]; | |
2d120f83 | 628 | |
4bb6408c JS |
629 | buf[0] = c; |
630 | buf[1] = 0; | |
4dba84be | 631 | AppendText(buf); |
4bb6408c JS |
632 | return *this; |
633 | } | |
634 | ||
02e8b2f9 JS |
635 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
636 | { | |
2d120f83 JS |
637 | // Indicates that we should generate a normal command, because |
638 | // we're letting default behaviour happen (otherwise it's vetoed | |
639 | // by virtue of overriding OnChar) | |
640 | m_processedDefault = TRUE; | |
641 | ||
642 | if (m_tempCallbackStruct) | |
02e8b2f9 | 643 | { |
2d120f83 JS |
644 | XmTextVerifyCallbackStruct *textStruct = |
645 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
646 | textStruct->doit = True; | |
647 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
648 | { | |
649 | textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); | |
650 | } | |
02e8b2f9 | 651 | } |
02e8b2f9 JS |
652 | } |
653 | ||
4b5f3fe6 | 654 | void wxTextCtrl::ChangeFont(bool keepOriginalSize) |
0d57be45 | 655 | { |
4b5f3fe6 | 656 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
657 | } |
658 | ||
659 | void wxTextCtrl::ChangeBackgroundColour() | |
660 | { | |
321db4b6 | 661 | wxWindow::ChangeBackgroundColour(); |
2d120f83 | 662 | |
94b49b93 | 663 | /* TODO: should scrollbars be affected? Should probably have separate |
2d120f83 JS |
664 | * function to change them (by default, taken from wxSystemSettings) |
665 | */ | |
94b49b93 JS |
666 | if (m_windowStyle & wxTE_MULTILINE) |
667 | { | |
668 | Widget parent = XtParent ((Widget) m_mainWidget); | |
669 | Widget hsb, vsb; | |
2d120f83 | 670 | |
94b49b93 | 671 | XtVaGetValues (parent, |
2d120f83 JS |
672 | XmNhorizontalScrollBar, &hsb, |
673 | XmNverticalScrollBar, &vsb, | |
674 | NULL); | |
94b49b93 JS |
675 | wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE); |
676 | if (hsb) | |
677 | DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); | |
678 | if (vsb) | |
679 | DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); | |
2d120f83 | 680 | |
94b49b93 JS |
681 | DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE); |
682 | } | |
0d57be45 JS |
683 | } |
684 | ||
685 | void wxTextCtrl::ChangeForegroundColour() | |
686 | { | |
321db4b6 | 687 | wxWindow::ChangeForegroundColour(); |
2d120f83 | 688 | |
94b49b93 JS |
689 | if (m_windowStyle & wxTE_MULTILINE) |
690 | { | |
691 | Widget parent = XtParent ((Widget) m_mainWidget); | |
692 | Widget hsb, vsb; | |
2d120f83 | 693 | |
94b49b93 | 694 | XtVaGetValues (parent, |
2d120f83 JS |
695 | XmNhorizontalScrollBar, &hsb, |
696 | XmNverticalScrollBar, &vsb, | |
697 | NULL); | |
698 | ||
699 | /* TODO: should scrollbars be affected? Should probably have separate | |
700 | * function to change them (by default, taken from wxSystemSettings) | |
701 | if (hsb) | |
94b49b93 | 702 | DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); |
2d120f83 | 703 | if (vsb) |
94b49b93 | 704 | DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); |
2d120f83 | 705 | */ |
94b49b93 JS |
706 | DoChangeForegroundColour((WXWidget) parent, m_foregroundColour); |
707 | } | |
0d57be45 JS |
708 | } |
709 | ||
dfc54541 | 710 | static void wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer ptr) |
02e8b2f9 | 711 | { |
2d120f83 JS |
712 | if (!wxGetWindowFromTable(w)) |
713 | // Widget has been deleted! | |
714 | return; | |
715 | ||
716 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
717 | tw->SetModified(TRUE); | |
02e8b2f9 JS |
718 | } |
719 | ||
720 | static void | |
721 | wxTextWindowModifyProc (Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs) | |
722 | { | |
dfc54541 JS |
723 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
724 | tw->m_processedDefault = FALSE; | |
2d120f83 | 725 | |
dfc54541 JS |
726 | // First, do some stuff if it's a password control. |
727 | // (What does this do exactly?) | |
2d120f83 | 728 | |
dfc54541 JS |
729 | if (tw->GetWindowStyleFlag() & wxTE_PASSWORD) |
730 | { | |
2d120f83 JS |
731 | /* _sm_ |
732 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
733 | * every event as a replace event. cbs->text->ptr gives the replacement | |
734 | * text, cbs->startPos gives the index of the first char affected by the | |
735 | * replace, and cbs->endPos gives the index one more than the last char | |
736 | * affected by the replace (startPos == endPos implies an empty range). | |
737 | * Hence, a deletion is represented by replacing all input text with a | |
738 | * blank string ("", *not* NULL!). A simple insertion that does not | |
739 | * overwrite any text has startPos == endPos. | |
740 | */ | |
741 | ||
dfc54541 JS |
742 | if (tw->m_value.IsNull()) |
743 | { | |
744 | tw->m_value = cbs->text->ptr; | |
745 | } | |
746 | else | |
747 | { | |
748 | char * passwd = (char*) (const char*) tw->m_value; // Set up a more convenient alias. | |
2d120f83 | 749 | |
dfc54541 JS |
750 | int len = passwd ? strlen(passwd) : 0; // Enough room for old text |
751 | len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL | |
752 | len -= cbs->endPos - cbs->startPos; // - text from affected region. | |
2d120f83 | 753 | |
dfc54541 JS |
754 | char * newS = new char [len]; |
755 | char * p = passwd, * dest = newS, * insert = cbs->text->ptr; | |
2d120f83 | 756 | |
dfc54541 JS |
757 | // Copy (old) text from passwd, up to the start posn of the change. |
758 | int i; | |
759 | for (i = 0; i < cbs->startPos; ++i) | |
760 | *dest++ = *p++; | |
2d120f83 | 761 | |
dfc54541 JS |
762 | // Copy the text to be inserted). |
763 | while (*insert) | |
2d120f83 JS |
764 | *dest++ = *insert++; |
765 | ||
dfc54541 JS |
766 | // Finally, copy into newS any remaining text from passwd[endPos] on. |
767 | for (p = passwd + cbs->endPos; *p; ) | |
768 | *dest++ = *p++; | |
769 | *dest = 0; | |
2d120f83 | 770 | |
dfc54541 | 771 | tw->m_value = newS; |
2d120f83 | 772 | |
dfc54541 JS |
773 | delete[] newS; |
774 | } | |
2d120f83 | 775 | |
dfc54541 JS |
776 | if (cbs->text->length>0) |
777 | { | |
778 | int i; | |
779 | for (i = 0; i < cbs->text->length; ++i) | |
780 | cbs->text->ptr[i] = '*'; | |
781 | cbs->text->ptr[i] = 0; | |
782 | } | |
783 | } | |
2d120f83 | 784 | |
dfc54541 JS |
785 | // If we're already within an OnChar, return: probably |
786 | // a programmatic insertion. | |
787 | if (tw->m_tempCallbackStruct) | |
788 | return; | |
2d120f83 | 789 | |
dfc54541 JS |
790 | // Check for a backspace |
791 | if (cbs->startPos == (cbs->currInsert - 1)) | |
792 | { | |
793 | tw->m_tempCallbackStruct = (void*) cbs; | |
2d120f83 | 794 | |
dfc54541 JS |
795 | wxKeyEvent event (wxEVT_CHAR); |
796 | event.SetId(tw->GetId()); | |
797 | event.m_keyCode = WXK_DELETE; | |
798 | event.SetEventObject(tw); | |
2d120f83 | 799 | |
dfc54541 JS |
800 | // Only if wxTextCtrl::OnChar is called |
801 | // will this be set to True (and the character | |
802 | // passed through) | |
803 | cbs->doit = False; | |
2d120f83 | 804 | |
dfc54541 | 805 | tw->GetEventHandler()->ProcessEvent(event); |
2d120f83 | 806 | |
dfc54541 | 807 | tw->m_tempCallbackStruct = NULL; |
2d120f83 | 808 | |
a4294b78 | 809 | if (tw->InSetValue()) |
dfc54541 | 810 | return; |
2d120f83 | 811 | |
dfc54541 JS |
812 | if (tw->m_processedDefault) |
813 | { | |
814 | // Can generate a command | |
815 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, tw->GetId()); | |
2d120f83 | 816 | commandEvent.SetEventObject(tw); |
dfc54541 | 817 | tw->ProcessCommand(commandEvent); |
2d120f83 JS |
818 | } |
819 | ||
dfc54541 JS |
820 | return; |
821 | } | |
2d120f83 | 822 | |
dfc54541 JS |
823 | // Pasting operation: let it through without |
824 | // calling OnChar | |
825 | if (cbs->text->length > 1) | |
826 | return; | |
2d120f83 | 827 | |
dfc54541 JS |
828 | // Something other than text |
829 | if (cbs->text->ptr == NULL) | |
830 | return; | |
2d120f83 | 831 | |
02e8b2f9 | 832 | tw->m_tempCallbackStruct = (void*) cbs; |
2d120f83 | 833 | |
02e8b2f9 JS |
834 | wxKeyEvent event (wxEVT_CHAR); |
835 | event.SetId(tw->GetId()); | |
02e8b2f9 | 836 | event.SetEventObject(tw); |
dfc54541 | 837 | event.m_keyCode = (cbs->text->ptr[0] == 10 ? 13 : cbs->text->ptr[0]); |
2d120f83 | 838 | |
02e8b2f9 JS |
839 | // Only if wxTextCtrl::OnChar is called |
840 | // will this be set to True (and the character | |
841 | // passed through) | |
842 | cbs->doit = False; | |
2d120f83 | 843 | |
02e8b2f9 | 844 | tw->GetEventHandler()->ProcessEvent(event); |
2d120f83 | 845 | |
02e8b2f9 | 846 | tw->m_tempCallbackStruct = NULL; |
2d120f83 | 847 | |
a4294b78 | 848 | if (tw->InSetValue()) |
dfc54541 JS |
849 | return; |
850 | ||
851 | if (tw->m_processedDefault) | |
852 | { | |
853 | // Can generate a command | |
854 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, tw->GetId()); | |
855 | commandEvent.SetEventObject(tw); | |
856 | tw->ProcessCommand(commandEvent); | |
857 | } | |
02e8b2f9 JS |
858 | } |
859 | ||
860 | static void | |
861 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs) | |
862 | { | |
2d120f83 JS |
863 | if (!wxGetWindowFromTable(w)) |
864 | return; | |
865 | ||
866 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
867 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
868 | event.SetEventObject(tw); | |
869 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 JS |
870 | } |
871 | ||
872 | static void | |
873 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs) | |
874 | { | |
2d120f83 JS |
875 | if (!wxGetWindowFromTable(w)) |
876 | return; | |
877 | ||
878 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
879 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
880 | event.SetEventObject(tw); | |
881 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 | 882 | } |
dfc54541 JS |
883 | |
884 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
2d120f83 | 885 | XmAnyCallbackStruct *ptr) |
dfc54541 | 886 | { |
2d120f83 JS |
887 | if (!wxGetWindowFromTable(w)) |
888 | return; | |
889 | ||
890 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
891 | /* | |
892 | case XmCR_ACTIVATE: | |
dfc54541 JS |
893 | type_event = wxEVENT_TYPE_TEXT_ENTER_COMMAND ; |
894 | break; | |
2d120f83 | 895 | default: |
dfc54541 JS |
896 | type_event = wxEVENT_TYPE_TEXT_COMMAND ; |
897 | break; | |
2d120f83 JS |
898 | } |
899 | */ | |
dfc54541 | 900 | |
2d120f83 JS |
901 | if (tw->InSetValue()) |
902 | return; | |
903 | ||
904 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); | |
905 | event.SetId(tw->GetId()); | |
906 | event.SetEventObject(tw); | |
907 | tw->ProcessCommand(event); | |
dfc54541 | 908 | } |