]>
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; | |
105fbe1f | 113 | PreCreation(); |
e1aae528 | 114 | |
02e8b2f9 | 115 | m_tempCallbackStruct = (void*) NULL; |
7d8268a1 WS |
116 | m_modified = false; |
117 | m_processedDefault = false; | |
66a007fb | 118 | |
02e8b2f9 | 119 | Widget parentWidget = (Widget) parent->GetClientWidget(); |
66a007fb | 120 | |
996994c7 | 121 | Bool wantHorizScroll = (m_windowStyle & wxHSCROLL) != 0 ? True : False; |
02e8b2f9 | 122 | // If we don't have horizontal scrollbars, we want word wrap. |
996994c7 MB |
123 | // OpenMotif 2.1 crashes if wantWordWrap is True in Japanese |
124 | // locale (and probably other multibyte locales). The check might be | |
125 | // more precise | |
126 | #if wxCHECK_LESSTIF() || wxCHECK_MOTIF_VERSION( 2, 2 ) | |
127 | Bool wantWordWrap = wantHorizScroll == True ? False : True; | |
128 | #else | |
129 | Bool wantWordWrap = False; | |
130 | #endif | |
66a007fb | 131 | |
02e8b2f9 JS |
132 | if (m_windowStyle & wxTE_MULTILINE) |
133 | { | |
996994c7 MB |
134 | Arg args[8]; |
135 | int count = 0; | |
136 | XtSetArg (args[count], XmNscrollHorizontal, wantHorizScroll); ++count; | |
105fbe1f MB |
137 | if( m_font.IsOk() ) |
138 | XtSetArg (args[count], (String) wxFont::GetFontTag(), | |
139 | m_font.GetFontType( XtDisplay(parentWidget) ) ); ++count; | |
996994c7 | 140 | XtSetArg (args[count], XmNwordWrap, wantWordWrap); ++count; |
9b135950 | 141 | XtSetArg (args[count], XmNvalue, value.mb_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, | |
9b135950 | 148 | wxConstCast(name.mb_str(), char), |
996994c7 MB |
149 | args, count); |
150 | ||
02e8b2f9 JS |
151 | XtManageChild ((Widget) m_mainWidget); |
152 | } | |
153 | else | |
154 | { | |
66a007fb VZ |
155 | m_mainWidget = (WXWidget)XtVaCreateManagedWidget |
156 | ( | |
9b135950 | 157 | wxConstCast(name.mb_str(), char), |
66a007fb VZ |
158 | xmTextWidgetClass, |
159 | parentWidget, | |
996994c7 | 160 | wxFont::GetFontTag(), m_font.GetFontType( XtDisplay(parentWidget) ), |
9b135950 | 161 | XmNvalue, value.mb_str(), |
996994c7 MB |
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 | |
105fbe1f | 197 | PostCreation(); |
e1aae528 | 198 | AttachWidget (parent, m_mainWidget, (WXWidget) NULL, |
f9204363 | 199 | pos.x, pos.y, size.x, size.y); |
66a007fb | 200 | |
7d8268a1 | 201 | return true; |
4bb6408c JS |
202 | } |
203 | ||
02e8b2f9 | 204 | WXWidget wxTextCtrl::GetTopWidget() const |
4bb6408c | 205 | { |
ccb234b4 MB |
206 | return IsMultiLine() ? (WXWidget)XtParent((Widget)m_mainWidget) |
207 | : m_mainWidget; | |
4bb6408c JS |
208 | } |
209 | ||
02e8b2f9 | 210 | wxString wxTextCtrl::GetValue() const |
4bb6408c | 211 | { |
c27eab7e VZ |
212 | wxString str; // result |
213 | ||
dfc54541 | 214 | if (m_windowStyle & wxTE_PASSWORD) |
c27eab7e VZ |
215 | { |
216 | // the value is stored always in m_value because it can't be retrieved | |
217 | // from the text control | |
218 | str = m_value; | |
219 | } | |
dfc54541 JS |
220 | else |
221 | { | |
c27eab7e | 222 | // just get the string from Motif |
dfc54541 | 223 | char *s = XmTextGetString ((Widget) m_mainWidget); |
c27eab7e | 224 | if ( s ) |
dfc54541 | 225 | { |
c27eab7e | 226 | str = s; |
dfc54541 | 227 | XtFree (s); |
2d120f83 | 228 | } |
c27eab7e VZ |
229 | //else: return empty string |
230 | ||
231 | if ( m_tempCallbackStruct ) | |
dfc54541 | 232 | { |
c27eab7e VZ |
233 | // the string in the control isn't yet updated, can't use it as is |
234 | MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *) | |
235 | m_tempCallbackStruct); | |
dfc54541 JS |
236 | } |
237 | } | |
c27eab7e VZ |
238 | |
239 | return str; | |
4bb6408c JS |
240 | } |
241 | ||
ee2ec18e | 242 | void wxTextCtrl::DoSetValue(const wxString& text, int flags) |
4bb6408c | 243 | { |
7d8268a1 | 244 | m_inSetValue = true; |
66a007fb | 245 | |
9b135950 | 246 | XmTextSetString ((Widget) m_mainWidget, wxConstCast(text.mb_str(), char)); |
996994c7 MB |
247 | XtVaSetValues ((Widget) m_mainWidget, |
248 | XmNcursorPosition, text.length(), | |
b86de524 | 249 | NULL); |
ccb234b4 | 250 | |
996994c7 MB |
251 | SetInsertionPoint(text.length()); |
252 | XmTextShowPosition ((Widget) m_mainWidget, text.length()); | |
355b4d3d | 253 | m_modified = true; |
66a007fb | 254 | |
7d8268a1 | 255 | m_inSetValue = false; |
ee2ec18e VZ |
256 | |
257 | if ( flags & SetValue_SendEvent ) | |
258 | SendTextUpdatedEvent(); | |
4bb6408c JS |
259 | } |
260 | ||
261 | // Clipboard operations | |
262 | void wxTextCtrl::Copy() | |
263 | { | |
dfc54541 | 264 | XmTextCopy((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
265 | } |
266 | ||
267 | void wxTextCtrl::Cut() | |
268 | { | |
dfc54541 | 269 | XmTextCut((Widget) m_mainWidget, CurrentTime); |
4bb6408c JS |
270 | } |
271 | ||
272 | void wxTextCtrl::Paste() | |
273 | { | |
dfc54541 | 274 | XmTextPaste((Widget) m_mainWidget); |
4bb6408c JS |
275 | } |
276 | ||
ca8b28f2 JS |
277 | bool wxTextCtrl::CanCopy() const |
278 | { | |
279 | // Can copy if there's a selection | |
280 | long from, to; | |
281 | GetSelection(& from, & to); | |
282 | return (from != to) ; | |
283 | } | |
284 | ||
285 | bool wxTextCtrl::CanCut() const | |
286 | { | |
287 | // Can cut if there's a selection | |
288 | long from, to; | |
289 | GetSelection(& from, & to); | |
dbf28859 | 290 | return (from != to) && (IsEditable()); |
ca8b28f2 JS |
291 | } |
292 | ||
293 | bool wxTextCtrl::CanPaste() const | |
294 | { | |
295 | return IsEditable() ; | |
296 | } | |
297 | ||
298 | // Undo/redo | |
299 | void wxTextCtrl::Undo() | |
300 | { | |
301 | // Not possible in Motif | |
302 | } | |
303 | ||
304 | void wxTextCtrl::Redo() | |
305 | { | |
306 | // Not possible in Motif | |
307 | } | |
308 | ||
309 | bool wxTextCtrl::CanUndo() const | |
310 | { | |
311 | // No Undo in Motif | |
7d8268a1 | 312 | return false; |
ca8b28f2 JS |
313 | } |
314 | ||
315 | bool wxTextCtrl::CanRedo() const | |
316 | { | |
317 | // No Redo in Motif | |
7d8268a1 | 318 | return false; |
ca8b28f2 JS |
319 | } |
320 | ||
321 | // If the return values from and to are the same, there is no | |
322 | // selection. | |
323 | void wxTextCtrl::GetSelection(long* from, long* to) const | |
324 | { | |
325 | XmTextPosition left, right; | |
326 | ||
327 | XmTextGetSelectionPosition((Widget) m_mainWidget, & left, & right); | |
328 | ||
329 | *from = (long) left; | |
330 | *to = (long) right; | |
331 | } | |
332 | ||
333 | bool wxTextCtrl::IsEditable() const | |
334 | { | |
335 | return (XmTextGetEditable((Widget) m_mainWidget) != 0); | |
336 | } | |
337 | ||
4bb6408c JS |
338 | void wxTextCtrl::SetEditable(bool editable) |
339 | { | |
dfc54541 | 340 | XmTextSetEditable((Widget) m_mainWidget, (Boolean) editable); |
4bb6408c JS |
341 | } |
342 | ||
343 | void wxTextCtrl::SetInsertionPoint(long pos) | |
344 | { | |
dfc54541 | 345 | XmTextSetInsertionPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
346 | } |
347 | ||
348 | void wxTextCtrl::SetInsertionPointEnd() | |
349 | { | |
7d8268a1 | 350 | wxTextPos pos = GetLastPosition(); |
4bb6408c JS |
351 | SetInsertionPoint(pos); |
352 | } | |
353 | ||
354 | long wxTextCtrl::GetInsertionPoint() const | |
355 | { | |
dfc54541 | 356 | return (long) XmTextGetInsertionPosition ((Widget) m_mainWidget); |
4bb6408c JS |
357 | } |
358 | ||
7d8268a1 | 359 | wxTextPos wxTextCtrl::GetLastPosition() const |
4bb6408c | 360 | { |
dfc54541 | 361 | return (long) XmTextGetLastPosition ((Widget) m_mainWidget); |
4bb6408c JS |
362 | } |
363 | ||
364 | void wxTextCtrl::Replace(long from, long to, const wxString& value) | |
365 | { | |
dfc54541 | 366 | XmTextReplace ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
9b135950 | 367 | wxConstCast(value.mb_str(), char)); |
4bb6408c JS |
368 | } |
369 | ||
370 | void wxTextCtrl::Remove(long from, long to) | |
371 | { | |
dfc54541 | 372 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
66a007fb | 373 | (Time) 0); |
dfc54541 | 374 | XmTextRemove ((Widget) m_mainWidget); |
4bb6408c JS |
375 | } |
376 | ||
377 | void wxTextCtrl::SetSelection(long from, long to) | |
378 | { | |
44d130a3 MB |
379 | if( to == -1 ) |
380 | to = GetLastPosition(); | |
381 | ||
dfc54541 | 382 | XmTextSetSelection ((Widget) m_mainWidget, (XmTextPosition) from, (XmTextPosition) to, |
66a007fb | 383 | (Time) 0); |
4bb6408c JS |
384 | } |
385 | ||
4bb6408c JS |
386 | void wxTextCtrl::WriteText(const wxString& text) |
387 | { | |
96be256b | 388 | long textPosition = GetInsertionPoint() + text.length(); |
d3a80c92 | 389 | XmTextInsert ((Widget) m_mainWidget, GetInsertionPoint(), |
9b135950 | 390 | wxConstCast(text.mb_str(), char)); |
dfc54541 JS |
391 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); |
392 | SetInsertionPoint(textPosition); | |
393 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
7d8268a1 | 394 | m_modified = true; |
4bb6408c JS |
395 | } |
396 | ||
4dba84be JS |
397 | void wxTextCtrl::AppendText(const wxString& text) |
398 | { | |
7d8268a1 | 399 | wxTextPos textPosition = GetLastPosition() + text.length(); |
d3a80c92 | 400 | XmTextInsert ((Widget) m_mainWidget, GetLastPosition(), |
9b135950 | 401 | wxConstCast(text.mb_str(), char)); |
4dba84be JS |
402 | XtVaSetValues ((Widget) m_mainWidget, XmNcursorPosition, textPosition, NULL); |
403 | SetInsertionPoint(textPosition); | |
404 | XmTextShowPosition ((Widget) m_mainWidget, textPosition); | |
7d8268a1 | 405 | m_modified = true; |
4dba84be JS |
406 | } |
407 | ||
4bb6408c JS |
408 | void wxTextCtrl::Clear() |
409 | { | |
da8cf723 | 410 | XmTextSetString ((Widget) m_mainWidget, wxMOTIF_STR("")); |
7d8268a1 | 411 | m_modified = false; |
4bb6408c JS |
412 | } |
413 | ||
414 | bool wxTextCtrl::IsModified() const | |
415 | { | |
02e8b2f9 | 416 | return m_modified; |
4bb6408c JS |
417 | } |
418 | ||
3a9fa0d6 VZ |
419 | // Makes modified or unmodified |
420 | void wxTextCtrl::MarkDirty() | |
421 | { | |
7d8268a1 | 422 | m_modified = true; |
3a9fa0d6 VZ |
423 | } |
424 | ||
4bb6408c JS |
425 | void wxTextCtrl::DiscardEdits() |
426 | { | |
7d8268a1 | 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; | |
7d8268a1 | 438 | bool finished = false; |
2d120f83 JS |
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 | { | |
7d8268a1 | 449 | finished = true; |
2d120f83 JS |
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 | ||
7d8268a1 | 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 | 494 | wxString str = GetLineText (lineNo); |
7520f3da | 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 | { | |
355b4d3d | 505 | wxString buf; |
dfc54541 JS |
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) | |
7d8268a1 | 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 | { | |
355b4d3d | 556 | textStruct->text->ptr[0] = (char)((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); |
2d120f83 | 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 | 583 | if (hsb) |
7d8268a1 | 584 | wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true); |
94b49b93 | 585 | if (vsb) |
7d8268a1 | 586 | wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true); |
66a007fb | 587 | |
1119b899 | 588 | // MBN: why change parent background? |
7d8268a1 | 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 | */ |
a8680e3e | 614 | wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour); |
94b49b93 | 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 | 663 | if( !value ) |
da8cf723 | 664 | value = wxMOTIF_STR("|"); |
e1aae528 MB |
665 | |
666 | int x, y; | |
667 | window->GetTextExtent( value, &x, &y ); | |
668 | ||
105fbe1f MB |
669 | if( x < 90 ) |
670 | x = 90; | |
1119b899 | 671 | |
e1aae528 MB |
672 | return wxSize( x + 2 * xmargin + 2 * highlight + 2 * shadow, |
673 | // MBN: +2 necessary: Lesstif bug or mine? | |
7d8268a1 | 674 | y + 2 * ymargin + 2 * highlight + 2 * shadow + 2 ); |
e1aae528 MB |
675 | } |
676 | ||
677 | wxSize wxTextCtrl::DoGetBestSize() const | |
678 | { | |
679 | if( IsSingleLine() ) | |
f9204363 MB |
680 | { |
681 | wxSize best = wxControl::DoGetBestSize(); | |
105fbe1f MB |
682 | #if wxCHECK_MOTIF_VERSION( 2, 3 ) |
683 | // OpenMotif 2.3 gives way too big X sizes | |
684 | wxSize other_best = wxDoGetSingleTextCtrlBestSize | |
685 | ( (Widget) GetTopWidget(), this ); | |
686 | return wxSize( other_best.x, best.y ); | |
687 | #else | |
688 | if( best.x < 90 ) best.x = 90; | |
f9204363 MB |
689 | |
690 | return best; | |
105fbe1f | 691 | #endif |
f9204363 | 692 | } |
e1aae528 MB |
693 | else |
694 | return wxWindow::DoGetBestSize(); | |
695 | } | |
696 | ||
c27eab7e VZ |
697 | // ---------------------------------------------------------------------------- |
698 | // helpers and Motif callbacks | |
699 | // ---------------------------------------------------------------------------- | |
700 | ||
701 | static void MergeChangesIntoString(wxString& value, | |
702 | XmTextVerifyCallbackStruct *cbs) | |
703 | { | |
704 | /* _sm_ | |
705 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
706 | * every event as a replace event. cbs->text->ptr gives the replacement | |
707 | * text, cbs->startPos gives the index of the first char affected by the | |
708 | * replace, and cbs->endPos gives the index one more than the last char | |
709 | * affected by the replace (startPos == endPos implies an empty range). | |
710 | * Hence, a deletion is represented by replacing all input text with a | |
711 | * blank string ("", *not* NULL!). A simple insertion that does not | |
712 | * overwrite any text has startPos == endPos. | |
713 | */ | |
714 | ||
715 | if ( !value ) | |
716 | { | |
717 | // easy case: the ol value was empty | |
718 | value = cbs->text->ptr; | |
719 | } | |
720 | else | |
721 | { | |
722 | // merge the changes into the value | |
723 | const char * const passwd = value; | |
724 | int len = value.length(); | |
725 | ||
44d130a3 MB |
726 | len += ( cbs->text->ptr ? |
727 | strlen(cbs->text->ptr) : | |
728 | 0 ) + 1; // + new text (if any) + NUL | |
c27eab7e VZ |
729 | len -= cbs->endPos - cbs->startPos; // - text from affected region. |
730 | ||
731 | char * newS = new char [len]; | |
732 | char * dest = newS, | |
733 | * insert = cbs->text->ptr; | |
734 | ||
735 | // Copy (old) text from passwd, up to the start posn of the change. | |
736 | int i; | |
737 | const char * p = passwd; | |
738 | for (i = 0; i < cbs->startPos; ++i) | |
739 | *dest++ = *p++; | |
740 | ||
741 | // Copy the text to be inserted). | |
44d130a3 MB |
742 | if (insert) |
743 | while (*insert) | |
744 | *dest++ = *insert++; | |
c27eab7e VZ |
745 | |
746 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
747 | for (p = passwd + cbs->endPos; *p; ) | |
748 | *dest++ = *p++; | |
749 | *dest = 0; | |
750 | ||
751 | value = newS; | |
752 | ||
753 | delete[] newS; | |
754 | } | |
755 | } | |
756 | ||
757 | static void | |
af111fc3 | 758 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 759 | { |
2d120f83 JS |
760 | if (!wxGetWindowFromTable(w)) |
761 | // Widget has been deleted! | |
762 | return; | |
66a007fb | 763 | |
2d120f83 | 764 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
7d8268a1 | 765 | tw->SetModified(true); |
02e8b2f9 JS |
766 | } |
767 | ||
66a007fb | 768 | static void |
af111fc3 | 769 | wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs) |
02e8b2f9 | 770 | { |
dfc54541 | 771 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
7d8268a1 | 772 | tw->m_processedDefault = false; |
66a007fb | 773 | |
c27eab7e VZ |
774 | // First, do some stuff if it's a password control: in this case, we need |
775 | // to store the string inside the class because GetValue() can't retrieve | |
776 | // it from the text ctrl. We do *not* do it in other circumstances because | |
777 | // it would double the amount of memory needed. | |
66a007fb | 778 | |
c27eab7e | 779 | if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD ) |
dfc54541 | 780 | { |
c27eab7e | 781 | MergeChangesIntoString(tw->m_value, cbs); |
66a007fb | 782 | |
c27eab7e | 783 | if ( cbs->text->length > 0 ) |
dfc54541 JS |
784 | { |
785 | int i; | |
786 | for (i = 0; i < cbs->text->length; ++i) | |
787 | cbs->text->ptr[i] = '*'; | |
c27eab7e | 788 | cbs->text->ptr[i] = '\0'; |
dfc54541 JS |
789 | } |
790 | } | |
66a007fb | 791 | |
ccb234b4 MB |
792 | if(tw->InSetValue()) |
793 | return; | |
794 | ||
c27eab7e VZ |
795 | // If we're already within an OnChar, return: probably a programmatic |
796 | // insertion. | |
dfc54541 JS |
797 | if (tw->m_tempCallbackStruct) |
798 | return; | |
66a007fb | 799 | |
dfc54541 JS |
800 | // Check for a backspace |
801 | if (cbs->startPos == (cbs->currInsert - 1)) | |
802 | { | |
c27eab7e | 803 | tw->DoSendEvents((void *)cbs, WXK_DELETE); |
66a007fb | 804 | |
dfc54541 JS |
805 | return; |
806 | } | |
66a007fb | 807 | |
c27eab7e | 808 | // Pasting operation: let it through without calling OnChar |
dfc54541 JS |
809 | if (cbs->text->length > 1) |
810 | return; | |
66a007fb | 811 | |
dfc54541 JS |
812 | // Something other than text |
813 | if (cbs->text->ptr == NULL) | |
814 | return; | |
66a007fb | 815 | |
c27eab7e VZ |
816 | // normal key press |
817 | char ch = cbs->text->ptr[0]; | |
818 | tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch); | |
02e8b2f9 JS |
819 | } |
820 | ||
66a007fb | 821 | static void |
af111fc3 | 822 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 823 | { |
2d120f83 JS |
824 | if (!wxGetWindowFromTable(w)) |
825 | return; | |
66a007fb | 826 | |
2d120f83 JS |
827 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
828 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
829 | event.SetEventObject(tw); | |
830 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 JS |
831 | } |
832 | ||
66a007fb | 833 | static void |
af111fc3 | 834 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 835 | { |
2d120f83 JS |
836 | if (!wxGetWindowFromTable(w)) |
837 | return; | |
66a007fb | 838 | |
2d120f83 JS |
839 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
840 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
841 | event.SetEventObject(tw); | |
842 | tw->GetEventHandler()->ProcessEvent(event); | |
02e8b2f9 | 843 | } |
dfc54541 JS |
844 | |
845 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
af111fc3 | 846 | XmAnyCallbackStruct *WXUNUSED(ptr)) |
dfc54541 | 847 | { |
2d120f83 JS |
848 | if (!wxGetWindowFromTable(w)) |
849 | return; | |
66a007fb | 850 | |
2d120f83 | 851 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
66a007fb | 852 | |
2d120f83 JS |
853 | if (tw->InSetValue()) |
854 | return; | |
66a007fb | 855 | |
2d120f83 JS |
856 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); |
857 | event.SetId(tw->GetId()); | |
858 | event.SetEventObject(tw); | |
859 | tw->ProcessCommand(event); | |
dfc54541 | 860 | } |
c27eab7e | 861 | |
e702ff0f JS |
862 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
863 | { | |
864 | Cut(); | |
865 | } | |
866 | ||
867 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
868 | { | |
869 | Copy(); | |
870 | } | |
871 | ||
872 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
873 | { | |
874 | Paste(); | |
875 | } | |
876 | ||
877 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
878 | { | |
879 | Undo(); | |
880 | } | |
881 | ||
882 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
883 | { | |
884 | Redo(); | |
885 | } | |
886 | ||
887 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
888 | { | |
889 | event.Enable( CanCut() ); | |
890 | } | |
891 | ||
892 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
893 | { | |
894 | event.Enable( CanCopy() ); | |
895 | } | |
896 | ||
897 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
898 | { | |
899 | event.Enable( CanPaste() ); | |
900 | } | |
901 | ||
902 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
903 | { | |
904 | event.Enable( CanUndo() ); | |
905 | } | |
906 | ||
907 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
908 | { | |
909 | event.Enable( CanRedo() ); | |
910 | } |