]>
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 | |
66a007fb | 9 | // Licence: wxWindows licence |
4bb6408c JS |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
66a007fb VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
4bb6408c | 20 | #ifdef __GNUG__ |
66a007fb | 21 | #pragma implementation "textctrl.h" |
4bb6408c JS |
22 | #endif |
23 | ||
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <fstream.h> | |
66a007fb | 27 | #include <ctype.h> |
4bb6408c JS |
28 | |
29 | #include "wx/textctrl.h" | |
30 | #include "wx/settings.h" | |
31 | #include "wx/filefn.h" | |
32 | #include "wx/utils.h" | |
33 | ||
338dd992 JJ |
34 | #ifdef __VMS__ |
35 | #pragma message disable nosimpint | |
36 | #endif | |
02e8b2f9 | 37 | #include <Xm/Text.h> |
338dd992 JJ |
38 | #ifdef __VMS__ |
39 | #pragma message enable nosimpint | |
40 | #endif | |
02e8b2f9 JS |
41 | |
42 | #include "wx/motif/private.h" | |
43 | ||
66a007fb VZ |
44 | // ---------------------------------------------------------------------------- |
45 | // private functions | |
46 | // ---------------------------------------------------------------------------- | |
47 | ||
c27eab7e VZ |
48 | // helper: inserts the new text in the value of the text ctrl and returns the |
49 | // result in place | |
50 | static void MergeChangesIntoString(wxString& value, | |
51 | XmTextVerifyCallbackStruct *textStruct); | |
52 | ||
66a007fb VZ |
53 | // callbacks |
54 | static void wxTextWindowChangedProc(Widget w, XtPointer clientData, XtPointer ptr); | |
55 | static void wxTextWindowModifyProc(Widget w, XtPointer clientData, XmTextVerifyCallbackStruct *cbs); | |
56 | static void wxTextWindowGainFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
57 | static void wxTextWindowLoseFocusProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *cbs); | |
58 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, XmAnyCallbackStruct *ptr); | |
02e8b2f9 | 59 | |
4bb6408c | 60 | #if !USE_SHARED_LIBRARY |
66a007fb | 61 | IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxControl) |
4bb6408c | 62 | |
66a007fb VZ |
63 | BEGIN_EVENT_TABLE(wxTextCtrl, wxControl) |
64 | EVT_DROP_FILES(wxTextCtrl::OnDropFiles) | |
65 | EVT_CHAR(wxTextCtrl::OnChar) | |
e702ff0f JS |
66 | |
67 | EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) | |
68 | EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) | |
69 | EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) | |
70 | EVT_MENU(wxID_UNDO, wxTextCtrl::OnUndo) | |
71 | EVT_MENU(wxID_REDO, wxTextCtrl::OnRedo) | |
72 | ||
73 | EVT_UPDATE_UI(wxID_CUT, wxTextCtrl::OnUpdateCut) | |
74 | EVT_UPDATE_UI(wxID_COPY, wxTextCtrl::OnUpdateCopy) | |
75 | EVT_UPDATE_UI(wxID_PASTE, wxTextCtrl::OnUpdatePaste) | |
76 | EVT_UPDATE_UI(wxID_UNDO, wxTextCtrl::OnUpdateUndo) | |
77 | EVT_UPDATE_UI(wxID_REDO, wxTextCtrl::OnUpdateRedo) | |
78 | ||
66a007fb | 79 | END_EVENT_TABLE() |
4bb6408c JS |
80 | #endif |
81 | ||
66a007fb VZ |
82 | // ============================================================================ |
83 | // implementation | |
84 | // ============================================================================ | |
85 | ||
86 | // ---------------------------------------------------------------------------- | |
87 | // wxTextCtrl | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
4bb6408c JS |
90 | // Text item |
91 | wxTextCtrl::wxTextCtrl() | |
4bb6408c | 92 | { |
02e8b2f9 JS |
93 | m_tempCallbackStruct = (void*) NULL; |
94 | m_modified = FALSE; | |
dfc54541 | 95 | m_processedDefault = FALSE; |
4bb6408c JS |
96 | } |
97 | ||
66a007fb VZ |
98 | bool wxTextCtrl::Create(wxWindow *parent, |
99 | wxWindowID id, | |
2d120f83 JS |
100 | const wxString& value, |
101 | const wxPoint& pos, | |
66a007fb VZ |
102 | const wxSize& size, |
103 | long style, | |
2d120f83 JS |
104 | const wxValidator& validator, |
105 | const wxString& name) | |
4bb6408c | 106 | { |
02e8b2f9 JS |
107 | m_tempCallbackStruct = (void*) NULL; |
108 | m_modified = FALSE; | |
dfc54541 | 109 | m_processedDefault = FALSE; |
94b49b93 JS |
110 | // m_backgroundColour = parent->GetBackgroundColour(); |
111 | m_backgroundColour = * wxWHITE; | |
0d57be45 | 112 | m_foregroundColour = parent->GetForegroundColour(); |
66a007fb | 113 | |
4bb6408c JS |
114 | SetName(name); |
115 | SetValidator(validator); | |
66a007fb VZ |
116 | if (parent) |
117 | parent->AddChild(this); | |
118 | ||
4bb6408c | 119 | m_windowStyle = style; |
66a007fb | 120 | |
4bb6408c | 121 | if ( id == -1 ) |
2d120f83 | 122 | m_windowId = (int)NewControlId(); |
4bb6408c | 123 | else |
2d120f83 | 124 | m_windowId = id; |
66a007fb | 125 | |
02e8b2f9 | 126 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
66a007fb | 127 | |
02e8b2f9 | 128 | bool wantHorizScrolling = ((m_windowStyle & wxHSCROLL) != 0); |
66a007fb | 129 | |
02e8b2f9 JS |
130 | // If we don't have horizontal scrollbars, we want word wrap. |
131 | bool wantWordWrap = !wantHorizScrolling; | |
66a007fb | 132 | |
02e8b2f9 JS |
133 | if (m_windowStyle & wxTE_MULTILINE) |
134 | { | |
135 | Arg args[2]; | |
136 | XtSetArg (args[0], XmNscrollHorizontal, wantHorizScrolling ? True : False); | |
137 | XtSetArg (args[1], XmNwordWrap, wantWordWrap ? True : False); | |
66a007fb VZ |
138 | |
139 | m_mainWidget = (WXWidget) XmCreateScrolledText(parentWidget, | |
140 | (char*)name.c_str(), | |
141 | args, 2); | |
142 | ||
02e8b2f9 | 143 | XtVaSetValues ((Widget) m_mainWidget, |
66a007fb VZ |
144 | XmNeditable, ((style & wxTE_READONLY) ? False : True), |
145 | XmNeditMode, XmMULTI_LINE_EDIT, | |
146 | NULL); | |
02e8b2f9 JS |
147 | XtManageChild ((Widget) m_mainWidget); |
148 | } | |
149 | else | |
150 | { | |
66a007fb VZ |
151 | m_mainWidget = (WXWidget)XtVaCreateManagedWidget |
152 | ( | |
153 | (char*)name.c_str(), | |
154 | xmTextWidgetClass, | |
155 | parentWidget, | |
156 | NULL | |
157 | ); | |
158 | ||
17023320 MB |
159 | XtVaSetValues ((Widget) m_mainWidget, |
160 | XmNeditable, ((style & wxTE_READONLY) ? False : True), | |
161 | NULL); | |
162 | ||
02e8b2f9 JS |
163 | // TODO: Is this relevant? What does it do? |
164 | int noCols = 2; | |
165 | if (!value.IsNull() && (value.Length() > (unsigned int) noCols)) | |
166 | noCols = value.Length(); | |
66a007fb VZ |
167 | XtVaSetValues((Widget) m_mainWidget, |
168 | XmNcolumns, noCols, | |
169 | NULL); | |
02e8b2f9 | 170 | } |
66a007fb VZ |
171 | |
172 | // remove border if asked for | |
173 | if ( style & wxNO_BORDER ) | |
174 | { | |
175 | XtVaSetValues((Widget)m_mainWidget, | |
176 | XmNshadowThickness, 0, | |
177 | NULL); | |
178 | } | |
179 | ||
180 | if ( !!value ) | |
b86de524 MB |
181 | { |
182 | #if 0 | |
183 | // don't do this because it is just linking the text to a source | |
184 | // string which is unsafe. MB | |
185 | // | |
66a007fb | 186 | XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str()); |
b86de524 MB |
187 | #else |
188 | // do this instead... MB | |
189 | // | |
190 | XtVaSetValues( (Widget) m_mainWidget, | |
191 | XmNvalue, (char *)value.c_str(), | |
192 | NULL); | |
193 | #endif | |
194 | } | |
195 | ||
66a007fb | 196 | // install callbacks |
02e8b2f9 | 197 | XtAddCallback((Widget) m_mainWidget, XmNvalueChangedCallback, (XtCallbackProc)wxTextWindowChangedProc, (XtPointer)this); |
66a007fb | 198 | |
02e8b2f9 | 199 | XtAddCallback((Widget) m_mainWidget, XmNmodifyVerifyCallback, (XtCallbackProc)wxTextWindowModifyProc, (XtPointer)this); |
66a007fb | 200 | |
dfc54541 | 201 | XtAddCallback((Widget) m_mainWidget, XmNactivateCallback, (XtCallbackProc)wxTextWindowActivateProc, (XtPointer)this); |
66a007fb | 202 | |
02e8b2f9 | 203 | XtAddCallback((Widget) m_mainWidget, XmNfocusCallback, (XtCallbackProc)wxTextWindowGainFocusProc, (XtPointer)this); |
66a007fb | 204 | |
02e8b2f9 | 205 | XtAddCallback((Widget) m_mainWidget, XmNlosingFocusCallback, (XtCallbackProc)wxTextWindowLoseFocusProc, (XtPointer)this); |
66a007fb VZ |
206 | |
207 | // font | |
da175b2c | 208 | m_font = parent->GetFont(); |
4b5f3fe6 | 209 | ChangeFont(FALSE); |
66a007fb | 210 | |
02e8b2f9 JS |
211 | SetCanAddEventHandler(TRUE); |
212 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, pos.x, pos.y, size.x, size.y); | |
66a007fb | 213 | |
0d57be45 | 214 | ChangeBackgroundColour(); |
66a007fb | 215 | |
4bb6408c JS |
216 | return TRUE; |
217 | } | |
218 | ||
02e8b2f9 | 219 | WXWidget wxTextCtrl::GetTopWidget() const |
4bb6408c | 220 | { |
dfc54541 | 221 | return ((m_windowStyle & wxTE_MULTILINE) ? (WXWidget) XtParent((Widget) m_mainWidget) : m_mainWidget); |
4bb6408c JS |
222 | } |
223 | ||
02e8b2f9 | 224 | wxString wxTextCtrl::GetValue() const |
4bb6408c | 225 | { |
c27eab7e VZ |
226 | wxString str; // result |
227 | ||
dfc54541 | 228 | if (m_windowStyle & wxTE_PASSWORD) |
c27eab7e VZ |
229 | { |
230 | // the value is stored always in m_value because it can't be retrieved | |
231 | // from the text control | |
232 | str = m_value; | |
233 | } | |
dfc54541 JS |
234 | else |
235 | { | |
c27eab7e | 236 | // just get the string from Motif |
dfc54541 | 237 | char *s = XmTextGetString ((Widget) m_mainWidget); |
c27eab7e | 238 | if ( s ) |
dfc54541 | 239 | { |
c27eab7e | 240 | str = s; |
dfc54541 | 241 | XtFree (s); |
2d120f83 | 242 | } |
c27eab7e VZ |
243 | //else: return empty string |
244 | ||
245 | if ( m_tempCallbackStruct ) | |
dfc54541 | 246 | { |
c27eab7e VZ |
247 | // the string in the control isn't yet updated, can't use it as is |
248 | MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *) | |
249 | m_tempCallbackStruct); | |
dfc54541 JS |
250 | } |
251 | } | |
c27eab7e VZ |
252 | |
253 | return str; | |
4bb6408c JS |
254 | } |
255 | ||
02e8b2f9 | 256 | void wxTextCtrl::SetValue(const wxString& value) |
4bb6408c | 257 | { |
dfc54541 | 258 | m_inSetValue = TRUE; |
66a007fb | 259 | |
b86de524 MB |
260 | #if 0 |
261 | // don't do this because it is just linking the text to a source | |
262 | // string which is unsafe. MB | |
263 | // | |
66a007fb | 264 | XmTextSetString ((Widget) m_mainWidget, (char*)value.c_str()); |
b86de524 MB |
265 | #else |
266 | // do this instead... MB | |
267 | // | |
268 | XtVaSetValues( (Widget) m_mainWidget, | |
269 | XmNvalue, (char *)value.c_str(), | |
270 | NULL); | |
271 | #endif | |
66a007fb | 272 | |
dfc54541 | 273 | m_inSetValue = FALSE; |
4bb6408c JS |
274 | } |
275 | ||
276 | // Clipboard operations | |
277 | void wxTextCtrl::Copy() | |
278 | { | |
dfc54541 | 279 | XmTextCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
280 | } |
281 | ||
282 | void wxTextCtrl::Cut() | |
283 | { | |
dfc54541 | 284 | XmTextCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
285 | } |
286 | ||
287 | void wxTextCtrl::Paste() | |
288 | { | |
dfc54541 | 289 | XmTextPaste((Widget) m_mainWidget); |
4bb6408c JS |
290 | } |
291 | ||
ca8b28f2 JS |
292 | bool wxTextCtrl::CanCopy() const |
293 | { | |
294 | // Can copy if there's a selection | |
295 | long from, to; | |
296 | GetSelection(& from, & to); | |
297 | return (from != to) ; | |
298 | } | |
299 | ||
300 | bool wxTextCtrl::CanCut() const | |
301 | { | |
302 | // Can cut if there's a selection | |
303 | long from, to; | |
304 | GetSelection(& from, & to); | |
305 | return (from != to) ; | |
306 | } | |
307 | ||
308 | bool wxTextCtrl::CanPaste() const | |
309 | { | |
310 | return IsEditable() ; | |
311 | } | |
312 | ||
313 | // Undo/redo | |
314 | void wxTextCtrl::Undo() | |
315 | { | |
316 | // Not possible in Motif | |
317 | } | |
318 | ||
319 | void wxTextCtrl::Redo() | |
320 | { | |
321 | // Not possible in Motif | |
322 | } | |
323 | ||
324 | bool wxTextCtrl::CanUndo() const | |
325 | { | |
326 | // No Undo in Motif | |
327 | return FALSE; | |
328 | } | |
329 | ||
330 | bool wxTextCtrl::CanRedo() const | |
331 | { | |
332 | // No Redo in Motif | |
333 | return FALSE; | |
334 | } | |
335 | ||
336 | // If the return values from and to are the same, there is no | |
337 | // selection. | |
338 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
339 | { | |
340 | XmTextPosition left, right; | |
341 | ||
342 | XmTextGetSelectionPosition((Widget) m_mainWidget, & left, & right); | |
343 | ||
344 | *from = (long) left; | |
345 | *to = (long) right; | |
346 | } | |
347 | ||
348 | bool wxTextCtrl::IsEditable() const | |
349 | { | |
350 | return (XmTextGetEditable((Widget) m_mainWidget) != 0); | |
351 | } | |
352 | ||
4bb6408c JS |
353 | void wxTextCtrl::SetEditable(bool editable) |
354 | { | |
dfc54541 | 355 | XmTextSetEditable((Widget) m_mainWidget, (Boolean) editable); |
4bb6408c JS |
356 | } |
357 | ||
358 | void wxTextCtrl::SetInsertionPoint(long pos) | |
359 | { | |
dfc54541 | 360 | XmTextSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
361 | } |
362 | ||
363 | void wxTextCtrl::SetInsertionPointEnd() | |
364 | { | |
365 | long pos = GetLastPosition(); | |
366 | SetInsertionPoint(pos); | |
367 | } | |
368 | ||
369 | long wxTextCtrl::GetInsertionPoint() const | |
370 | { | |
dfc54541 | 371 | return (long) XmTextGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
372 | } |
373 | ||
374 | long wxTextCtrl::GetLastPosition() const | |
375 | { | |
dfc54541 | 376 | return (long) XmTextGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
377 | } |
378 | ||
379 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
380 | { | |
dfc54541 | 381 | XmTextReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
2d120f83 | 382 | (char*) (const char*) value); |
4bb6408c JS |
383 | } |
384 | ||
385 | void wxTextCtrl::Remove(long from, long to) | |
386 | { | |
dfc54541 | 387 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
66a007fb | 388 | (Time) 0); |
dfc54541 | 389 | XmTextRemove ((Widget) m_mainWidget); |
4bb6408c JS |
390 | } |
391 | ||
392 | void wxTextCtrl::SetSelection(long from, long to) | |
393 | { | |
dfc54541 | 394 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
66a007fb | 395 | (Time) 0); |
4bb6408c JS |
396 | } |
397 | ||
398 | bool wxTextCtrl::LoadFile(const wxString& file) | |
399 | { | |
400 | if (!wxFileExists(file)) | |
401 | return FALSE; | |
66a007fb | 402 | |
4bb6408c | 403 | m_fileName = file; |
66a007fb | 404 | |
4bb6408c | 405 | Clear(); |
66a007fb | 406 | |
1a3ac83f | 407 | Widget textWidget = (Widget) m_mainWidget; |
af111fc3 | 408 | FILE *fp = 0; |
66a007fb | 409 | |
1a3ac83f JS |
410 | struct stat statb; |
411 | if ((stat ((char*) (const char*) file, &statb) == -1) || (statb.st_mode & S_IFMT) != S_IFREG || | |
2d120f83 | 412 | !(fp = fopen ((char*) (const char*) file, "r"))) |
4bb6408c | 413 | { |
2d120f83 | 414 | return FALSE; |
1a3ac83f JS |
415 | } |
416 | else | |
417 | { | |
2d120f83 JS |
418 | long len = statb.st_size; |
419 | char *text; | |
420 | if (!(text = XtMalloc ((unsigned) (len + 1)))) | |
421 | { | |
422 | fclose (fp); | |
423 | return FALSE; | |
424 | } | |
425 | if (fread (text, sizeof (char), len, fp) != (size_t) len) | |
426 | { | |
427 | } | |
428 | fclose (fp); | |
66a007fb | 429 | |
2d120f83 JS |
430 | text[len] = 0; |
431 | XmTextSetString (textWidget, text); | |
432 | // m_textPosition = len; | |
433 | XtFree (text); | |
434 | m_modified = FALSE; | |
435 | return TRUE; | |
4bb6408c | 436 | } |
4bb6408c JS |
437 | } |
438 | ||
439 | // If file is null, try saved file name first | |
440 | // Returns TRUE if succeeds. | |
441 | bool wxTextCtrl::SaveFile(const wxString& file) | |
442 | { | |
443 | wxString theFile(file); | |
444 | if (theFile == "") | |
445 | theFile = m_fileName; | |
446 | if (theFile == "") | |
447 | return FALSE; | |
448 | m_fileName = theFile; | |
66a007fb | 449 | |
2d120f83 JS |
450 | Widget textWidget = (Widget) m_mainWidget; |
451 | FILE *fp; | |
66a007fb | 452 | |
2d120f83 | 453 | if (!(fp = fopen ((char*) (const char*) theFile, "w"))) |
1a3ac83f | 454 | { |
2d120f83 | 455 | return FALSE; |
1a3ac83f | 456 | } |
2d120f83 | 457 | else |
1a3ac83f | 458 | { |
2d120f83 JS |
459 | char *text = XmTextGetString (textWidget); |
460 | long len = XmTextGetLastPosition (textWidget); | |
66a007fb | 461 | |
2d120f83 JS |
462 | if (fwrite (text, sizeof (char), len, fp) != (size_t) len) |
463 | { | |
464 | // Did not write whole file | |
465 | } | |
466 | // Make sure newline terminates the file | |
467 | if (text[len - 1] != '\n') | |
468 | fputc ('\n', fp); | |
66a007fb | 469 | |
2d120f83 JS |
470 | fclose (fp); |
471 | XtFree (text); | |
472 | m_modified = FALSE; | |
473 | return TRUE; | |
1a3ac83f | 474 | } |
4bb6408c JS |
475 | } |
476 | ||
477 | void wxTextCtrl::WriteText(const wxString& text) | |
478 | { | |
dfc54541 JS |
479 | long textPosition = GetInsertionPoint() + strlen (text); |
480 | XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), (char*) (const char*) text); | |
481 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
482 | SetInsertionPoint(textPosition); | |
483 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
484 | m_modified = TRUE; | |
4bb6408c JS |
485 | } |
486 | ||
4dba84be JS |
487 | void wxTextCtrl::AppendText(const wxString& text) |
488 | { | |
489 | long textPosition = GetLastPosition() + strlen(text); | |
490 | XmTextInsert ((Widget) m_mainWidget, GetLastPosition(), (char*) (const char*) text); | |
491 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); | |
492 | SetInsertionPoint(textPosition); | |
493 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
494 | m_modified = TRUE; | |
495 | } | |
496 | ||
4bb6408c JS |
497 | void wxTextCtrl::Clear() |
498 | { | |
02e8b2f9 | 499 | XmTextSetString ((Widget) m_mainWidget, ""); |
02e8b2f9 | 500 | m_modified = FALSE; |
4bb6408c JS |
501 | } |
502 | ||
503 | bool wxTextCtrl::IsModified() const | |
504 | { | |
02e8b2f9 | 505 | return m_modified; |
4bb6408c JS |
506 | } |
507 | ||
508 | // Makes 'unmodified' | |
509 | void wxTextCtrl::DiscardEdits() | |
510 | { | |
dfc54541 | 511 | m_modified = FALSE; |
4bb6408c JS |
512 | } |
513 | ||
514 | int wxTextCtrl::GetNumberOfLines() const | |
515 | { | |
dfc54541 JS |
516 | // HIDEOUSLY inefficient, but we have no choice. |
517 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
518 | if (s) | |
519 | { | |
2d120f83 JS |
520 | long i = 0; |
521 | int currentLine = 0; | |
522 | bool finished = FALSE; | |
523 | while (!finished) | |
524 | { | |
525 | int ch = s[i]; | |
526 | if (ch == '\n') | |
527 | { | |
528 | currentLine++; | |
529 | i++; | |
530 | } | |
531 | else if (ch == 0) | |
532 | { | |
533 | finished = TRUE; | |
534 | } | |
535 | else | |
536 | i++; | |
537 | } | |
66a007fb | 538 | |
2d120f83 JS |
539 | XtFree (s); |
540 | return currentLine; | |
dfc54541 | 541 | } |
4bb6408c JS |
542 | return 0; |
543 | } | |
544 | ||
545 | long wxTextCtrl::XYToPosition(long x, long y) const | |
546 | { | |
dfc54541 | 547 | /* It seems, that there is a bug in some versions of the Motif library, |
2d120f83 JS |
548 | so the original wxWin-Code doesn't work. */ |
549 | /* | |
550 | Widget textWidget = (Widget) handle; | |
551 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
552 | */ | |
dfc54541 JS |
553 | /* Now a little workaround: */ |
554 | long r=0; | |
555 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
66a007fb | 556 | return r+x; |
4bb6408c JS |
557 | } |
558 | ||
813c20a6 | 559 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
4bb6408c | 560 | { |
dfc54541 JS |
561 | Position xx, yy; |
562 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
813c20a6 VZ |
563 | if ( x ) |
564 | *x = xx; | |
565 | if ( y ) | |
566 | *y = yy; | |
567 | ||
568 | return TRUE; | |
4bb6408c JS |
569 | } |
570 | ||
571 | void wxTextCtrl::ShowPosition(long pos) | |
572 | { | |
dfc54541 | 573 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
574 | } |
575 | ||
576 | int wxTextCtrl::GetLineLength(long lineNo) const | |
577 | { | |
dfc54541 JS |
578 | wxString str = GetLineText (lineNo); |
579 | return (int) str.Length(); | |
4bb6408c JS |
580 | } |
581 | ||
582 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
583 | { | |
dfc54541 JS |
584 | // HIDEOUSLY inefficient, but we have no choice. |
585 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
66a007fb | 586 | |
dfc54541 JS |
587 | if (s) |
588 | { | |
589 | wxString buf(""); | |
590 | long i; | |
591 | int currentLine = 0; | |
592 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
2d120f83 JS |
593 | if (s[i] == '\n') |
594 | currentLine++; | |
595 | // Now get the text | |
596 | int j; | |
597 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
598 | buf += s[i]; | |
66a007fb | 599 | |
2d120f83 JS |
600 | XtFree(s); |
601 | return buf; | |
602 | } | |
603 | else | |
604 | return wxEmptyString; | |
4bb6408c JS |
605 | } |
606 | ||
607 | /* | |
2d120f83 JS |
608 | * Text item |
609 | */ | |
610 | ||
4bb6408c JS |
611 | void wxTextCtrl::Command(wxCommandEvent & event) |
612 | { | |
613 | SetValue (event.GetString()); | |
614 | ProcessCommand (event); | |
615 | } | |
616 | ||
617 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
618 | { | |
619 | // By default, load the first file into the text window. | |
620 | if (event.GetNumberOfFiles() > 0) | |
621 | { | |
622 | LoadFile(event.GetFiles()[0]); | |
623 | } | |
624 | } | |
625 | ||
02e8b2f9 JS |
626 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
627 | { | |
2d120f83 JS |
628 | // Indicates that we should generate a normal command, because |
629 | // we're letting default behaviour happen (otherwise it's vetoed | |
630 | // by virtue of overriding OnChar) | |
631 | m_processedDefault = TRUE; | |
66a007fb | 632 | |
2d120f83 | 633 | if (m_tempCallbackStruct) |
02e8b2f9 | 634 | { |
2d120f83 JS |
635 | XmTextVerifyCallbackStruct *textStruct = |
636 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
637 | textStruct->doit = True; | |
638 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
639 | { | |
640 | textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); | |
641 | } | |
02e8b2f9 | 642 | } |
02e8b2f9 JS |
643 | } |
644 | ||
4b5f3fe6 | 645 | void wxTextCtrl::ChangeFont(bool keepOriginalSize) |
0d57be45 | 646 | { |
4b5f3fe6 | 647 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
648 | } |
649 | ||
650 | void wxTextCtrl::ChangeBackgroundColour() | |
651 | { | |
321db4b6 | 652 | wxWindow::ChangeBackgroundColour(); |
66a007fb | 653 | |
94b49b93 | 654 | /* TODO: should scrollbars be affected? Should probably have separate |
2d120f83 JS |
655 | * function to change them (by default, taken from wxSystemSettings) |
656 | */ | |
94b49b93 JS |
657 | if (m_windowStyle & wxTE_MULTILINE) |
658 | { | |
659 | Widget parent = XtParent ((Widget) m_mainWidget); | |
660 | Widget hsb, vsb; | |
66a007fb | 661 | |
94b49b93 | 662 | XtVaGetValues (parent, |
2d120f83 JS |
663 | XmNhorizontalScrollBar, &hsb, |
664 | XmNverticalScrollBar, &vsb, | |
665 | NULL); | |
94b49b93 JS |
666 | wxColour backgroundColour = wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DFACE); |
667 | if (hsb) | |
668 | DoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); | |
669 | if (vsb) | |
670 | DoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); | |
66a007fb | 671 | |
94b49b93 JS |
672 | DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE); |
673 | } | |
0d57be45 JS |
674 | } |
675 | ||
676 | void wxTextCtrl::ChangeForegroundColour() | |
677 | { | |
321db4b6 | 678 | wxWindow::ChangeForegroundColour(); |
66a007fb | 679 | |
94b49b93 JS |
680 | if (m_windowStyle & wxTE_MULTILINE) |
681 | { | |
682 | Widget parent = XtParent ((Widget) m_mainWidget); | |
683 | Widget hsb, vsb; | |
66a007fb | 684 | |
94b49b93 | 685 | XtVaGetValues (parent, |
2d120f83 JS |
686 | XmNhorizontalScrollBar, &hsb, |
687 | XmNverticalScrollBar, &vsb, | |
688 | NULL); | |
66a007fb | 689 | |
2d120f83 JS |
690 | /* TODO: should scrollbars be affected? Should probably have separate |
691 | * function to change them (by default, taken from wxSystemSettings) | |
692 | if (hsb) | |
94b49b93 | 693 | DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); |
2d120f83 | 694 | if (vsb) |
94b49b93 | 695 | DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); |
2d120f83 | 696 | */ |
94b49b93 JS |
697 | DoChangeForegroundColour((WXWidget) parent, m_foregroundColour); |
698 | } | |
0d57be45 JS |
699 | } |
700 | ||
c27eab7e VZ |
701 | void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode) |
702 | { | |
703 | // we're in process of updating the text control | |
704 | m_tempCallbackStruct = wxcbs; | |
705 | ||
706 | XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs; | |
707 | ||
708 | wxKeyEvent event (wxEVT_CHAR); | |
709 | event.SetId(GetId()); | |
710 | event.m_keyCode = keycode; | |
711 | event.SetEventObject(this); | |
712 | ||
713 | // Only if wxTextCtrl::OnChar is called will this be set to True (and | |
714 | // the character passed through) | |
715 | cbs->doit = False; | |
716 | ||
717 | GetEventHandler()->ProcessEvent(event); | |
718 | ||
719 | if ( !InSetValue() && m_processedDefault ) | |
720 | { | |
721 | // Can generate a command | |
722 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
723 | commandEvent.SetEventObject(this); | |
724 | ProcessCommand(commandEvent); | |
725 | } | |
726 | ||
727 | // do it after the (user) event handlers processed the events because | |
728 | // otherwise GetValue() would return incorrect (not yet updated value) | |
729 | m_tempCallbackStruct = NULL; | |
730 | } | |
731 | ||
732 | // ---------------------------------------------------------------------------- | |
733 | // helpers and Motif callbacks | |
734 | // ---------------------------------------------------------------------------- | |
735 | ||
736 | static void MergeChangesIntoString(wxString& value, | |
737 | XmTextVerifyCallbackStruct *cbs) | |
738 | { | |
739 | /* _sm_ | |
740 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
741 | * every event as a replace event. cbs->text->ptr gives the replacement | |
742 | * text, cbs->startPos gives the index of the first char affected by the | |
743 | * replace, and cbs->endPos gives the index one more than the last char | |
744 | * affected by the replace (startPos == endPos implies an empty range). | |
745 | * Hence, a deletion is represented by replacing all input text with a | |
746 | * blank string ("", *not* NULL!). A simple insertion that does not | |
747 | * overwrite any text has startPos == endPos. | |
748 | */ | |
749 | ||
750 | if ( !value ) | |
751 | { | |
752 | // easy case: the ol value was empty | |
753 | value = cbs->text->ptr; | |
754 | } | |
755 | else | |
756 | { | |
757 | // merge the changes into the value | |
758 | const char * const passwd = value; | |
759 | int len = value.length(); | |
760 | ||
761 | len += strlen(cbs->text->ptr) + 1; // + new text (if any) + NUL | |
762 | len -= cbs->endPos - cbs->startPos; // - text from affected region. | |
763 | ||
764 | char * newS = new char [len]; | |
765 | char * dest = newS, | |
766 | * insert = cbs->text->ptr; | |
767 | ||
768 | // Copy (old) text from passwd, up to the start posn of the change. | |
769 | int i; | |
770 | const char * p = passwd; | |
771 | for (i = 0; i < cbs->startPos; ++i) | |
772 | *dest++ = *p++; | |
773 | ||
774 | // Copy the text to be inserted). | |
775 | while (*insert) | |
776 | *dest++ = *insert++; | |
777 | ||
778 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
779 | for (p = passwd + cbs->endPos; *p; ) | |
780 | *dest++ = *p++; | |
781 | *dest = 0; | |
782 | ||
783 | value = newS; | |
784 | ||
785 | delete[] newS; | |
786 | } | |
787 | } | |
788 | ||
789 | static void | |
af111fc3 | 790 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 791 | { |
2d120f83 JS |
792 | if (!wxGetWindowFromTable(w)) |
793 | // Widget has been deleted! | |
794 | return; | |
66a007fb | 795 | |
2d120f83 JS |
796 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
797 | tw->SetModified(TRUE); | |
02e8b2f9 JS |
798 | } |
799 | ||
66a007fb | 800 | static void |
af111fc3 | 801 | wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs) |
02e8b2f9 | 802 | { |
dfc54541 JS |
803 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
804 | tw->m_processedDefault = FALSE; | |
66a007fb | 805 | |
c27eab7e VZ |
806 | // First, do some stuff if it's a password control: in this case, we need |
807 | // to store the string inside the class because GetValue() can't retrieve | |
808 | // it from the text ctrl. We do *not* do it in other circumstances because | |
809 | // it would double the amount of memory needed. | |
66a007fb | 810 | |
c27eab7e | 811 | if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD ) |
dfc54541 | 812 | { |
c27eab7e | 813 | MergeChangesIntoString(tw->m_value, cbs); |
66a007fb | 814 | |
c27eab7e | 815 | if ( cbs->text->length > 0 ) |
dfc54541 JS |
816 | { |
817 | int i; | |
818 | for (i = 0; i < cbs->text->length; ++i) | |
819 | cbs->text->ptr[i] = '*'; | |
c27eab7e | 820 | cbs->text->ptr[i] = '\0'; |
dfc54541 JS |
821 | } |
822 | } | |
66a007fb | 823 | |
c27eab7e VZ |
824 | // If we're already within an OnChar, return: probably a programmatic |
825 | // insertion. | |
dfc54541 JS |
826 | if (tw->m_tempCallbackStruct) |
827 | return; | |
66a007fb | 828 | |
dfc54541 JS |
829 | // Check for a backspace |
830 | if (cbs->startPos == (cbs->currInsert - 1)) | |
831 | { | |
c27eab7e | 832 | tw->DoSendEvents((void *)cbs, WXK_DELETE); |
66a007fb | 833 | |
dfc54541 JS |
834 | return; |
835 | } | |
66a007fb | 836 | |
c27eab7e | 837 | // Pasting operation: let it through without calling OnChar |
dfc54541 JS |
838 | if (cbs->text->length > 1) |
839 | return; | |
66a007fb | 840 | |
dfc54541 JS |
841 | // Something other than text |
842 | if (cbs->text->ptr == NULL) | |
843 | return; | |
66a007fb | 844 | |
c27eab7e VZ |
845 | // normal key press |
846 | char ch = cbs->text->ptr[0]; | |
847 | tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch); | |
02e8b2f9 JS |
848 | } |
849 | ||
66a007fb | 850 | static void |
af111fc3 | 851 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 852 | { |
2d120f83 JS |
853 | if (!wxGetWindowFromTable(w)) |
854 | return; | |
66a007fb | 855 | |
2d120f83 JS |
856 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
857 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
858 | event.SetEventObject(tw); | |
859 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 JS |
860 | } |
861 | ||
66a007fb | 862 | static void |
af111fc3 | 863 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 864 | { |
2d120f83 JS |
865 | if (!wxGetWindowFromTable(w)) |
866 | return; | |
66a007fb | 867 | |
2d120f83 JS |
868 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
869 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
870 | event.SetEventObject(tw); | |
871 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 | 872 | } |
dfc54541 JS |
873 | |
874 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
af111fc3 | 875 | XmAnyCallbackStruct *WXUNUSED(ptr)) |
dfc54541 | 876 | { |
2d120f83 JS |
877 | if (!wxGetWindowFromTable(w)) |
878 | return; | |
66a007fb | 879 | |
2d120f83 | 880 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
66a007fb | 881 | |
2d120f83 JS |
882 | if (tw->InSetValue()) |
883 | return; | |
66a007fb | 884 | |
2d120f83 JS |
885 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); |
886 | event.SetId(tw->GetId()); | |
887 | event.SetEventObject(tw); | |
888 | tw->ProcessCommand(event); | |
dfc54541 | 889 | } |
c27eab7e | 890 | |
e702ff0f JS |
891 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
892 | { | |
893 | Cut(); | |
894 | } | |
895 | ||
896 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
897 | { | |
898 | Copy(); | |
899 | } | |
900 | ||
901 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
902 | { | |
903 | Paste(); | |
904 | } | |
905 | ||
906 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
907 | { | |
908 | Undo(); | |
909 | } | |
910 | ||
911 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
912 | { | |
913 | Redo(); | |
914 | } | |
915 | ||
916 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
917 | { | |
918 | event.Enable( CanCut() ); | |
919 | } | |
920 | ||
921 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
922 | { | |
923 | event.Enable( CanCopy() ); | |
924 | } | |
925 | ||
926 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
927 | { | |
928 | event.Enable( CanPaste() ); | |
929 | } | |
930 | ||
931 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
932 | { | |
933 | event.Enable( CanUndo() ); | |
934 | } | |
935 | ||
936 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
937 | { | |
938 | event.Enable( CanRedo() ); | |
939 | } |