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