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