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