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