]>
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 | ||
14f355c2 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
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 | ||
3a9fa0d6 VZ |
423 | // Makes modified or unmodified |
424 | void wxTextCtrl::MarkDirty() | |
425 | { | |
1a3bb45e | 426 | m_modified = TRUE; |
3a9fa0d6 VZ |
427 | } |
428 | ||
4bb6408c JS |
429 | void wxTextCtrl::DiscardEdits() |
430 | { | |
dfc54541 | 431 | m_modified = FALSE; |
4bb6408c JS |
432 | } |
433 | ||
434 | int wxTextCtrl::GetNumberOfLines() const | |
435 | { | |
dfc54541 JS |
436 | // HIDEOUSLY inefficient, but we have no choice. |
437 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
438 | if (s) | |
439 | { | |
2d120f83 JS |
440 | long i = 0; |
441 | int currentLine = 0; | |
442 | bool finished = FALSE; | |
443 | while (!finished) | |
444 | { | |
445 | int ch = s[i]; | |
446 | if (ch == '\n') | |
447 | { | |
448 | currentLine++; | |
449 | i++; | |
450 | } | |
451 | else if (ch == 0) | |
452 | { | |
453 | finished = TRUE; | |
454 | } | |
455 | else | |
456 | i++; | |
457 | } | |
66a007fb | 458 | |
2d120f83 JS |
459 | XtFree (s); |
460 | return currentLine; | |
dfc54541 | 461 | } |
4bb6408c JS |
462 | return 0; |
463 | } | |
464 | ||
465 | long wxTextCtrl::XYToPosition(long x, long y) const | |
466 | { | |
dfc54541 | 467 | /* It seems, that there is a bug in some versions of the Motif library, |
2d120f83 JS |
468 | so the original wxWin-Code doesn't work. */ |
469 | /* | |
470 | Widget textWidget = (Widget) handle; | |
471 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
472 | */ | |
dfc54541 JS |
473 | /* Now a little workaround: */ |
474 | long r=0; | |
475 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
66a007fb | 476 | return r+x; |
4bb6408c JS |
477 | } |
478 | ||
813c20a6 | 479 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
4bb6408c | 480 | { |
dfc54541 JS |
481 | Position xx, yy; |
482 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
813c20a6 VZ |
483 | if ( x ) |
484 | *x = xx; | |
485 | if ( y ) | |
486 | *y = yy; | |
487 | ||
488 | return TRUE; | |
4bb6408c JS |
489 | } |
490 | ||
491 | void wxTextCtrl::ShowPosition(long pos) | |
492 | { | |
dfc54541 | 493 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
494 | } |
495 | ||
496 | int wxTextCtrl::GetLineLength(long lineNo) const | |
497 | { | |
dfc54541 JS |
498 | wxString str = GetLineText (lineNo); |
499 | return (int) str.Length(); | |
4bb6408c JS |
500 | } |
501 | ||
502 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
503 | { | |
dfc54541 JS |
504 | // HIDEOUSLY inefficient, but we have no choice. |
505 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
66a007fb | 506 | |
dfc54541 JS |
507 | if (s) |
508 | { | |
509 | wxString buf(""); | |
510 | long i; | |
511 | int currentLine = 0; | |
512 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
2d120f83 JS |
513 | if (s[i] == '\n') |
514 | currentLine++; | |
515 | // Now get the text | |
516 | int j; | |
517 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
518 | buf += s[i]; | |
66a007fb | 519 | |
2d120f83 JS |
520 | XtFree(s); |
521 | return buf; | |
522 | } | |
523 | else | |
524 | return wxEmptyString; | |
4bb6408c JS |
525 | } |
526 | ||
527 | /* | |
2d120f83 JS |
528 | * Text item |
529 | */ | |
530 | ||
4bb6408c JS |
531 | void wxTextCtrl::Command(wxCommandEvent & event) |
532 | { | |
533 | SetValue (event.GetString()); | |
534 | ProcessCommand (event); | |
535 | } | |
536 | ||
537 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
538 | { | |
539 | // By default, load the first file into the text window. | |
540 | if (event.GetNumberOfFiles() > 0) | |
541 | { | |
542 | LoadFile(event.GetFiles()[0]); | |
543 | } | |
544 | } | |
545 | ||
02e8b2f9 JS |
546 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
547 | { | |
2d120f83 JS |
548 | // Indicates that we should generate a normal command, because |
549 | // we're letting default behaviour happen (otherwise it's vetoed | |
550 | // by virtue of overriding OnChar) | |
551 | m_processedDefault = TRUE; | |
66a007fb | 552 | |
2d120f83 | 553 | if (m_tempCallbackStruct) |
02e8b2f9 | 554 | { |
2d120f83 JS |
555 | XmTextVerifyCallbackStruct *textStruct = |
556 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
557 | textStruct->doit = True; | |
558 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
559 | { | |
560 | textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); | |
561 | } | |
02e8b2f9 | 562 | } |
02e8b2f9 JS |
563 | } |
564 | ||
4b5f3fe6 | 565 | void wxTextCtrl::ChangeFont(bool keepOriginalSize) |
0d57be45 | 566 | { |
4b5f3fe6 | 567 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
568 | } |
569 | ||
570 | void wxTextCtrl::ChangeBackgroundColour() | |
571 | { | |
321db4b6 | 572 | wxWindow::ChangeBackgroundColour(); |
66a007fb | 573 | |
94b49b93 | 574 | /* TODO: should scrollbars be affected? Should probably have separate |
2d120f83 JS |
575 | * function to change them (by default, taken from wxSystemSettings) |
576 | */ | |
94b49b93 JS |
577 | if (m_windowStyle & wxTE_MULTILINE) |
578 | { | |
579 | Widget parent = XtParent ((Widget) m_mainWidget); | |
580 | Widget hsb, vsb; | |
66a007fb | 581 | |
94b49b93 | 582 | XtVaGetValues (parent, |
2d120f83 JS |
583 | XmNhorizontalScrollBar, &hsb, |
584 | XmNverticalScrollBar, &vsb, | |
585 | NULL); | |
a756f210 | 586 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
94b49b93 | 587 | if (hsb) |
a8680e3e | 588 | wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); |
94b49b93 | 589 | if (vsb) |
a8680e3e | 590 | wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); |
66a007fb | 591 | |
1119b899 MB |
592 | // MBN: why change parent background? |
593 | // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE); | |
94b49b93 | 594 | } |
0d57be45 JS |
595 | } |
596 | ||
597 | void wxTextCtrl::ChangeForegroundColour() | |
598 | { | |
321db4b6 | 599 | wxWindow::ChangeForegroundColour(); |
66a007fb | 600 | |
94b49b93 JS |
601 | if (m_windowStyle & wxTE_MULTILINE) |
602 | { | |
603 | Widget parent = XtParent ((Widget) m_mainWidget); | |
604 | Widget hsb, vsb; | |
66a007fb | 605 | |
94b49b93 | 606 | XtVaGetValues (parent, |
2d120f83 JS |
607 | XmNhorizontalScrollBar, &hsb, |
608 | XmNverticalScrollBar, &vsb, | |
609 | NULL); | |
66a007fb | 610 | |
2d120f83 JS |
611 | /* TODO: should scrollbars be affected? Should probably have separate |
612 | * function to change them (by default, taken from wxSystemSettings) | |
613 | if (hsb) | |
94b49b93 | 614 | DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); |
2d120f83 | 615 | if (vsb) |
94b49b93 | 616 | DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); |
2d120f83 | 617 | */ |
a8680e3e | 618 | wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour); |
94b49b93 | 619 | } |
0d57be45 JS |
620 | } |
621 | ||
c27eab7e VZ |
622 | void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode) |
623 | { | |
624 | // we're in process of updating the text control | |
625 | m_tempCallbackStruct = wxcbs; | |
626 | ||
627 | XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs; | |
628 | ||
629 | wxKeyEvent event (wxEVT_CHAR); | |
630 | event.SetId(GetId()); | |
631 | event.m_keyCode = keycode; | |
632 | event.SetEventObject(this); | |
633 | ||
634 | // Only if wxTextCtrl::OnChar is called will this be set to True (and | |
635 | // the character passed through) | |
636 | cbs->doit = False; | |
637 | ||
638 | GetEventHandler()->ProcessEvent(event); | |
639 | ||
640 | if ( !InSetValue() && m_processedDefault ) | |
641 | { | |
642 | // Can generate a command | |
643 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
644 | commandEvent.SetEventObject(this); | |
645 | ProcessCommand(commandEvent); | |
646 | } | |
647 | ||
648 | // do it after the (user) event handlers processed the events because | |
649 | // otherwise GetValue() would return incorrect (not yet updated value) | |
650 | m_tempCallbackStruct = NULL; | |
651 | } | |
652 | ||
e1aae528 MB |
653 | wxSize wxDoGetSingleTextCtrlBestSize( Widget textWidget, |
654 | const wxWindow* window ) | |
655 | { | |
656 | Dimension xmargin, ymargin, highlight, shadow; | |
657 | char* value; | |
658 | ||
659 | XtVaGetValues( textWidget, | |
660 | XmNmarginWidth, &xmargin, | |
661 | XmNmarginHeight, &ymargin, | |
662 | XmNvalue, &value, | |
663 | XmNhighlightThickness, &highlight, | |
664 | XmNshadowThickness, &shadow, | |
665 | NULL ); | |
1119b899 | 666 | |
e1aae528 MB |
667 | if( !value ) |
668 | value = "|"; | |
669 | ||
670 | int x, y; | |
671 | window->GetTextExtent( value, &x, &y ); | |
672 | ||
1119b899 MB |
673 | if( x < 100 ) x = 100; |
674 | ||
e1aae528 MB |
675 | return wxSize( x + 2 * xmargin + 2 * highlight + 2 * shadow, |
676 | // MBN: +2 necessary: Lesstif bug or mine? | |
677 | y + 2 * ymargin + 2 * highlight + 2 * shadow + 2 ); | |
678 | } | |
679 | ||
680 | wxSize wxTextCtrl::DoGetBestSize() const | |
681 | { | |
682 | if( IsSingleLine() ) | |
683 | return wxDoGetSingleTextCtrlBestSize( (Widget)m_mainWidget, this ); | |
684 | else | |
685 | return wxWindow::DoGetBestSize(); | |
686 | } | |
687 | ||
c27eab7e VZ |
688 | // ---------------------------------------------------------------------------- |
689 | // helpers and Motif callbacks | |
690 | // ---------------------------------------------------------------------------- | |
691 | ||
692 | static void MergeChangesIntoString(wxString& value, | |
693 | XmTextVerifyCallbackStruct *cbs) | |
694 | { | |
695 | /* _sm_ | |
696 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
697 | * every event as a replace event. cbs->text->ptr gives the replacement | |
698 | * text, cbs->startPos gives the index of the first char affected by the | |
699 | * replace, and cbs->endPos gives the index one more than the last char | |
700 | * affected by the replace (startPos == endPos implies an empty range). | |
701 | * Hence, a deletion is represented by replacing all input text with a | |
702 | * blank string ("", *not* NULL!). A simple insertion that does not | |
703 | * overwrite any text has startPos == endPos. | |
704 | */ | |
705 | ||
706 | if ( !value ) | |
707 | { | |
708 | // easy case: the ol value was empty | |
709 | value = cbs->text->ptr; | |
710 | } | |
711 | else | |
712 | { | |
713 | // merge the changes into the value | |
714 | const char * const passwd = value; | |
715 | int len = value.length(); | |
716 | ||
44d130a3 MB |
717 | len += ( cbs->text->ptr ? |
718 | strlen(cbs->text->ptr) : | |
719 | 0 ) + 1; // + new text (if any) + NUL | |
c27eab7e VZ |
720 | len -= cbs->endPos - cbs->startPos; // - text from affected region. |
721 | ||
722 | char * newS = new char [len]; | |
723 | char * dest = newS, | |
724 | * insert = cbs->text->ptr; | |
725 | ||
726 | // Copy (old) text from passwd, up to the start posn of the change. | |
727 | int i; | |
728 | const char * p = passwd; | |
729 | for (i = 0; i < cbs->startPos; ++i) | |
730 | *dest++ = *p++; | |
731 | ||
732 | // Copy the text to be inserted). | |
44d130a3 MB |
733 | if (insert) |
734 | while (*insert) | |
735 | *dest++ = *insert++; | |
c27eab7e VZ |
736 | |
737 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
738 | for (p = passwd + cbs->endPos; *p; ) | |
739 | *dest++ = *p++; | |
740 | *dest = 0; | |
741 | ||
742 | value = newS; | |
743 | ||
744 | delete[] newS; | |
745 | } | |
746 | } | |
747 | ||
748 | static void | |
af111fc3 | 749 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 750 | { |
2d120f83 JS |
751 | if (!wxGetWindowFromTable(w)) |
752 | // Widget has been deleted! | |
753 | return; | |
66a007fb | 754 | |
2d120f83 JS |
755 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
756 | tw->SetModified(TRUE); | |
02e8b2f9 JS |
757 | } |
758 | ||
66a007fb | 759 | static void |
af111fc3 | 760 | wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs) |
02e8b2f9 | 761 | { |
dfc54541 JS |
762 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
763 | tw->m_processedDefault = FALSE; | |
66a007fb | 764 | |
c27eab7e VZ |
765 | // First, do some stuff if it's a password control: in this case, we need |
766 | // to store the string inside the class because GetValue() can't retrieve | |
767 | // it from the text ctrl. We do *not* do it in other circumstances because | |
768 | // it would double the amount of memory needed. | |
66a007fb | 769 | |
c27eab7e | 770 | if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD ) |
dfc54541 | 771 | { |
c27eab7e | 772 | MergeChangesIntoString(tw->m_value, cbs); |
66a007fb | 773 | |
c27eab7e | 774 | if ( cbs->text->length > 0 ) |
dfc54541 JS |
775 | { |
776 | int i; | |
777 | for (i = 0; i < cbs->text->length; ++i) | |
778 | cbs->text->ptr[i] = '*'; | |
c27eab7e | 779 | cbs->text->ptr[i] = '\0'; |
dfc54541 JS |
780 | } |
781 | } | |
66a007fb | 782 | |
ccb234b4 MB |
783 | if(tw->InSetValue()) |
784 | return; | |
785 | ||
c27eab7e VZ |
786 | // If we're already within an OnChar, return: probably a programmatic |
787 | // insertion. | |
dfc54541 JS |
788 | if (tw->m_tempCallbackStruct) |
789 | return; | |
66a007fb | 790 | |
dfc54541 JS |
791 | // Check for a backspace |
792 | if (cbs->startPos == (cbs->currInsert - 1)) | |
793 | { | |
c27eab7e | 794 | tw->DoSendEvents((void *)cbs, WXK_DELETE); |
66a007fb | 795 | |
dfc54541 JS |
796 | return; |
797 | } | |
66a007fb | 798 | |
c27eab7e | 799 | // Pasting operation: let it through without calling OnChar |
dfc54541 JS |
800 | if (cbs->text->length > 1) |
801 | return; | |
66a007fb | 802 | |
dfc54541 JS |
803 | // Something other than text |
804 | if (cbs->text->ptr == NULL) | |
805 | return; | |
66a007fb | 806 | |
c27eab7e VZ |
807 | // normal key press |
808 | char ch = cbs->text->ptr[0]; | |
809 | tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch); | |
02e8b2f9 JS |
810 | } |
811 | ||
66a007fb | 812 | static void |
af111fc3 | 813 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 814 | { |
2d120f83 JS |
815 | if (!wxGetWindowFromTable(w)) |
816 | return; | |
66a007fb | 817 | |
2d120f83 JS |
818 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
819 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
820 | event.SetEventObject(tw); | |
821 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 JS |
822 | } |
823 | ||
66a007fb | 824 | static void |
af111fc3 | 825 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 826 | { |
2d120f83 JS |
827 | if (!wxGetWindowFromTable(w)) |
828 | return; | |
66a007fb | 829 | |
2d120f83 JS |
830 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
831 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
832 | event.SetEventObject(tw); | |
833 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 | 834 | } |
dfc54541 JS |
835 | |
836 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
af111fc3 | 837 | XmAnyCallbackStruct *WXUNUSED(ptr)) |
dfc54541 | 838 | { |
2d120f83 JS |
839 | if (!wxGetWindowFromTable(w)) |
840 | return; | |
66a007fb | 841 | |
2d120f83 | 842 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
66a007fb | 843 | |
2d120f83 JS |
844 | if (tw->InSetValue()) |
845 | return; | |
66a007fb | 846 | |
2d120f83 JS |
847 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); |
848 | event.SetId(tw->GetId()); | |
849 | event.SetEventObject(tw); | |
850 | tw->ProcessCommand(event); | |
dfc54541 | 851 | } |
c27eab7e | 852 | |
e702ff0f JS |
853 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
854 | { | |
855 | Cut(); | |
856 | } | |
857 | ||
858 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
859 | { | |
860 | Copy(); | |
861 | } | |
862 | ||
863 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
864 | { | |
865 | Paste(); | |
866 | } | |
867 | ||
868 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
869 | { | |
870 | Undo(); | |
871 | } | |
872 | ||
873 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
874 | { | |
875 | Redo(); | |
876 | } | |
877 | ||
878 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
879 | { | |
880 | event.Enable( CanCut() ); | |
881 | } | |
882 | ||
883 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
884 | { | |
885 | event.Enable( CanCopy() ); | |
886 | } | |
887 | ||
888 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
889 | { | |
890 | event.Enable( CanPaste() ); | |
891 | } | |
892 | ||
893 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
894 | { | |
895 | event.Enable( CanUndo() ); | |
896 | } | |
897 | ||
898 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
899 | { | |
900 | event.Enable( CanRedo() ); | |
901 | } |