]>
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; |
6991087b | 141 | XtSetArg (args[count], XmNvalue, (const char*)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, | |
6991087b | 148 | name.char_str(), |
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 | ( | |
6991087b | 157 | name.mb_str(), |
66a007fb VZ |
158 | xmTextWidgetClass, |
159 | parentWidget, | |
996994c7 | 160 | wxFont::GetFontTag(), m_font.GetFontType( XtDisplay(parentWidget) ), |
6991087b | 161 | XmNvalue, (const char*)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 | { | |
89954433 | 222 | str = wxTextEntry::GetValue(); |
c27eab7e VZ |
223 | |
224 | if ( m_tempCallbackStruct ) | |
dfc54541 | 225 | { |
c27eab7e VZ |
226 | // the string in the control isn't yet updated, can't use it as is |
227 | MergeChangesIntoString(str, (XmTextVerifyCallbackStruct *) | |
228 | m_tempCallbackStruct); | |
dfc54541 JS |
229 | } |
230 | } | |
c27eab7e VZ |
231 | |
232 | return str; | |
4bb6408c JS |
233 | } |
234 | ||
ee2ec18e | 235 | void wxTextCtrl::DoSetValue(const wxString& text, int flags) |
4bb6408c | 236 | { |
7d8268a1 | 237 | m_inSetValue = true; |
66a007fb | 238 | |
6991087b | 239 | XmTextSetString ((Widget) m_mainWidget, text.char_str()); |
996994c7 MB |
240 | XtVaSetValues ((Widget) m_mainWidget, |
241 | XmNcursorPosition, text.length(), | |
b86de524 | 242 | NULL); |
ccb234b4 | 243 | |
996994c7 MB |
244 | SetInsertionPoint(text.length()); |
245 | XmTextShowPosition ((Widget) m_mainWidget, text.length()); | |
355b4d3d | 246 | m_modified = true; |
66a007fb | 247 | |
7d8268a1 | 248 | m_inSetValue = false; |
ee2ec18e VZ |
249 | |
250 | if ( flags & SetValue_SendEvent ) | |
251 | SendTextUpdatedEvent(); | |
4bb6408c JS |
252 | } |
253 | ||
4bb6408c JS |
254 | bool wxTextCtrl::IsModified() const |
255 | { | |
02e8b2f9 | 256 | return m_modified; |
4bb6408c JS |
257 | } |
258 | ||
3a9fa0d6 VZ |
259 | // Makes modified or unmodified |
260 | void wxTextCtrl::MarkDirty() | |
261 | { | |
7d8268a1 | 262 | m_modified = true; |
3a9fa0d6 VZ |
263 | } |
264 | ||
4bb6408c JS |
265 | void wxTextCtrl::DiscardEdits() |
266 | { | |
7d8268a1 | 267 | m_modified = false; |
4bb6408c JS |
268 | } |
269 | ||
270 | int wxTextCtrl::GetNumberOfLines() const | |
271 | { | |
dfc54541 JS |
272 | // HIDEOUSLY inefficient, but we have no choice. |
273 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
274 | if (s) | |
275 | { | |
2d120f83 JS |
276 | long i = 0; |
277 | int currentLine = 0; | |
7d8268a1 | 278 | bool finished = false; |
2d120f83 JS |
279 | while (!finished) |
280 | { | |
281 | int ch = s[i]; | |
282 | if (ch == '\n') | |
283 | { | |
284 | currentLine++; | |
285 | i++; | |
286 | } | |
287 | else if (ch == 0) | |
288 | { | |
7d8268a1 | 289 | finished = true; |
2d120f83 JS |
290 | } |
291 | else | |
292 | i++; | |
293 | } | |
66a007fb | 294 | |
2d120f83 JS |
295 | XtFree (s); |
296 | return currentLine; | |
dfc54541 | 297 | } |
4bb6408c JS |
298 | return 0; |
299 | } | |
300 | ||
301 | long wxTextCtrl::XYToPosition(long x, long y) const | |
302 | { | |
dfc54541 | 303 | /* It seems, that there is a bug in some versions of the Motif library, |
2d120f83 JS |
304 | so the original wxWin-Code doesn't work. */ |
305 | /* | |
306 | Widget textWidget = (Widget) handle; | |
307 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
308 | */ | |
dfc54541 JS |
309 | /* Now a little workaround: */ |
310 | long r=0; | |
311 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
66a007fb | 312 | return r+x; |
4bb6408c JS |
313 | } |
314 | ||
813c20a6 | 315 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const |
4bb6408c | 316 | { |
dfc54541 JS |
317 | Position xx, yy; |
318 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
813c20a6 VZ |
319 | if ( x ) |
320 | *x = xx; | |
321 | if ( y ) | |
322 | *y = yy; | |
323 | ||
7d8268a1 | 324 | return true; |
4bb6408c JS |
325 | } |
326 | ||
327 | void wxTextCtrl::ShowPosition(long pos) | |
328 | { | |
dfc54541 | 329 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); |
4bb6408c JS |
330 | } |
331 | ||
332 | int wxTextCtrl::GetLineLength(long lineNo) const | |
333 | { | |
dfc54541 | 334 | wxString str = GetLineText (lineNo); |
7520f3da | 335 | return (int) str.length(); |
4bb6408c JS |
336 | } |
337 | ||
338 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
339 | { | |
dfc54541 JS |
340 | // HIDEOUSLY inefficient, but we have no choice. |
341 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
66a007fb | 342 | |
dfc54541 JS |
343 | if (s) |
344 | { | |
355b4d3d | 345 | wxString buf; |
dfc54541 JS |
346 | long i; |
347 | int currentLine = 0; | |
348 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
2d120f83 JS |
349 | if (s[i] == '\n') |
350 | currentLine++; | |
351 | // Now get the text | |
352 | int j; | |
353 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
354 | buf += s[i]; | |
66a007fb | 355 | |
2d120f83 JS |
356 | XtFree(s); |
357 | return buf; | |
358 | } | |
359 | else | |
360 | return wxEmptyString; | |
4bb6408c JS |
361 | } |
362 | ||
363 | /* | |
2d120f83 JS |
364 | * Text item |
365 | */ | |
366 | ||
4bb6408c JS |
367 | void wxTextCtrl::Command(wxCommandEvent & event) |
368 | { | |
369 | SetValue (event.GetString()); | |
370 | ProcessCommand (event); | |
371 | } | |
372 | ||
373 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
374 | { | |
375 | // By default, load the first file into the text window. | |
376 | if (event.GetNumberOfFiles() > 0) | |
377 | { | |
378 | LoadFile(event.GetFiles()[0]); | |
379 | } | |
380 | } | |
381 | ||
02e8b2f9 JS |
382 | void wxTextCtrl::OnChar(wxKeyEvent& event) |
383 | { | |
2d120f83 JS |
384 | // Indicates that we should generate a normal command, because |
385 | // we're letting default behaviour happen (otherwise it's vetoed | |
386 | // by virtue of overriding OnChar) | |
7d8268a1 | 387 | m_processedDefault = true; |
66a007fb | 388 | |
2d120f83 | 389 | if (m_tempCallbackStruct) |
02e8b2f9 | 390 | { |
2d120f83 JS |
391 | XmTextVerifyCallbackStruct *textStruct = |
392 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
393 | textStruct->doit = True; | |
394 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
395 | { | |
355b4d3d | 396 | textStruct->text->ptr[0] = (char)((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); |
2d120f83 | 397 | } |
02e8b2f9 | 398 | } |
02e8b2f9 JS |
399 | } |
400 | ||
4b5f3fe6 | 401 | void wxTextCtrl::ChangeFont(bool keepOriginalSize) |
0d57be45 | 402 | { |
4b5f3fe6 | 403 | wxWindow::ChangeFont(keepOriginalSize); |
0d57be45 JS |
404 | } |
405 | ||
406 | void wxTextCtrl::ChangeBackgroundColour() | |
407 | { | |
321db4b6 | 408 | wxWindow::ChangeBackgroundColour(); |
66a007fb | 409 | |
94b49b93 | 410 | /* TODO: should scrollbars be affected? Should probably have separate |
2d120f83 JS |
411 | * function to change them (by default, taken from wxSystemSettings) |
412 | */ | |
94b49b93 JS |
413 | if (m_windowStyle & wxTE_MULTILINE) |
414 | { | |
415 | Widget parent = XtParent ((Widget) m_mainWidget); | |
416 | Widget hsb, vsb; | |
66a007fb | 417 | |
94b49b93 | 418 | XtVaGetValues (parent, |
2d120f83 JS |
419 | XmNhorizontalScrollBar, &hsb, |
420 | XmNverticalScrollBar, &vsb, | |
421 | NULL); | |
a756f210 | 422 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); |
94b49b93 | 423 | if (hsb) |
7d8268a1 | 424 | wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, true); |
94b49b93 | 425 | if (vsb) |
7d8268a1 | 426 | wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, true); |
66a007fb | 427 | |
1119b899 | 428 | // MBN: why change parent background? |
7d8268a1 | 429 | // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true); |
94b49b93 | 430 | } |
0d57be45 JS |
431 | } |
432 | ||
433 | void wxTextCtrl::ChangeForegroundColour() | |
434 | { | |
321db4b6 | 435 | wxWindow::ChangeForegroundColour(); |
66a007fb | 436 | |
94b49b93 JS |
437 | if (m_windowStyle & wxTE_MULTILINE) |
438 | { | |
439 | Widget parent = XtParent ((Widget) m_mainWidget); | |
440 | Widget hsb, vsb; | |
66a007fb | 441 | |
94b49b93 | 442 | XtVaGetValues (parent, |
2d120f83 JS |
443 | XmNhorizontalScrollBar, &hsb, |
444 | XmNverticalScrollBar, &vsb, | |
445 | NULL); | |
66a007fb | 446 | |
2d120f83 JS |
447 | /* TODO: should scrollbars be affected? Should probably have separate |
448 | * function to change them (by default, taken from wxSystemSettings) | |
449 | if (hsb) | |
94b49b93 | 450 | DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); |
2d120f83 | 451 | if (vsb) |
94b49b93 | 452 | DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); |
2d120f83 | 453 | */ |
a8680e3e | 454 | wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour); |
94b49b93 | 455 | } |
0d57be45 JS |
456 | } |
457 | ||
c27eab7e VZ |
458 | void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode) |
459 | { | |
460 | // we're in process of updating the text control | |
461 | m_tempCallbackStruct = wxcbs; | |
462 | ||
463 | XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs; | |
464 | ||
465 | wxKeyEvent event (wxEVT_CHAR); | |
466 | event.SetId(GetId()); | |
467 | event.m_keyCode = keycode; | |
468 | event.SetEventObject(this); | |
469 | ||
470 | // Only if wxTextCtrl::OnChar is called will this be set to True (and | |
471 | // the character passed through) | |
472 | cbs->doit = False; | |
473 | ||
937013e0 | 474 | HandleWindowEvent(event); |
c27eab7e VZ |
475 | |
476 | if ( !InSetValue() && m_processedDefault ) | |
477 | { | |
478 | // Can generate a command | |
479 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
480 | commandEvent.SetEventObject(this); | |
481 | ProcessCommand(commandEvent); | |
482 | } | |
483 | ||
484 | // do it after the (user) event handlers processed the events because | |
485 | // otherwise GetValue() would return incorrect (not yet updated value) | |
486 | m_tempCallbackStruct = NULL; | |
487 | } | |
488 | ||
e1aae528 MB |
489 | wxSize wxDoGetSingleTextCtrlBestSize( Widget textWidget, |
490 | const wxWindow* window ) | |
491 | { | |
492 | Dimension xmargin, ymargin, highlight, shadow; | |
493 | char* value; | |
494 | ||
495 | XtVaGetValues( textWidget, | |
496 | XmNmarginWidth, &xmargin, | |
497 | XmNmarginHeight, &ymargin, | |
498 | XmNvalue, &value, | |
499 | XmNhighlightThickness, &highlight, | |
500 | XmNshadowThickness, &shadow, | |
501 | NULL ); | |
1119b899 | 502 | |
e1aae528 | 503 | if( !value ) |
da8cf723 | 504 | value = wxMOTIF_STR("|"); |
e1aae528 MB |
505 | |
506 | int x, y; | |
507 | window->GetTextExtent( value, &x, &y ); | |
508 | ||
105fbe1f MB |
509 | if( x < 90 ) |
510 | x = 90; | |
1119b899 | 511 | |
e1aae528 MB |
512 | return wxSize( x + 2 * xmargin + 2 * highlight + 2 * shadow, |
513 | // MBN: +2 necessary: Lesstif bug or mine? | |
7d8268a1 | 514 | y + 2 * ymargin + 2 * highlight + 2 * shadow + 2 ); |
e1aae528 MB |
515 | } |
516 | ||
517 | wxSize wxTextCtrl::DoGetBestSize() const | |
518 | { | |
519 | if( IsSingleLine() ) | |
f9204363 MB |
520 | { |
521 | wxSize best = wxControl::DoGetBestSize(); | |
105fbe1f MB |
522 | #if wxCHECK_MOTIF_VERSION( 2, 3 ) |
523 | // OpenMotif 2.3 gives way too big X sizes | |
524 | wxSize other_best = wxDoGetSingleTextCtrlBestSize | |
525 | ( (Widget) GetTopWidget(), this ); | |
526 | return wxSize( other_best.x, best.y ); | |
527 | #else | |
528 | if( best.x < 90 ) best.x = 90; | |
f9204363 MB |
529 | |
530 | return best; | |
105fbe1f | 531 | #endif |
f9204363 | 532 | } |
e1aae528 MB |
533 | else |
534 | return wxWindow::DoGetBestSize(); | |
535 | } | |
536 | ||
c27eab7e VZ |
537 | // ---------------------------------------------------------------------------- |
538 | // helpers and Motif callbacks | |
539 | // ---------------------------------------------------------------------------- | |
540 | ||
541 | static void MergeChangesIntoString(wxString& value, | |
542 | XmTextVerifyCallbackStruct *cbs) | |
543 | { | |
544 | /* _sm_ | |
545 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
546 | * every event as a replace event. cbs->text->ptr gives the replacement | |
547 | * text, cbs->startPos gives the index of the first char affected by the | |
548 | * replace, and cbs->endPos gives the index one more than the last char | |
549 | * affected by the replace (startPos == endPos implies an empty range). | |
550 | * Hence, a deletion is represented by replacing all input text with a | |
551 | * blank string ("", *not* NULL!). A simple insertion that does not | |
552 | * overwrite any text has startPos == endPos. | |
553 | */ | |
554 | ||
555 | if ( !value ) | |
556 | { | |
557 | // easy case: the ol value was empty | |
558 | value = cbs->text->ptr; | |
559 | } | |
560 | else | |
561 | { | |
562 | // merge the changes into the value | |
563 | const char * const passwd = value; | |
564 | int len = value.length(); | |
565 | ||
44d130a3 MB |
566 | len += ( cbs->text->ptr ? |
567 | strlen(cbs->text->ptr) : | |
568 | 0 ) + 1; // + new text (if any) + NUL | |
c27eab7e VZ |
569 | len -= cbs->endPos - cbs->startPos; // - text from affected region. |
570 | ||
571 | char * newS = new char [len]; | |
572 | char * dest = newS, | |
573 | * insert = cbs->text->ptr; | |
574 | ||
575 | // Copy (old) text from passwd, up to the start posn of the change. | |
576 | int i; | |
577 | const char * p = passwd; | |
578 | for (i = 0; i < cbs->startPos; ++i) | |
579 | *dest++ = *p++; | |
580 | ||
581 | // Copy the text to be inserted). | |
44d130a3 MB |
582 | if (insert) |
583 | while (*insert) | |
584 | *dest++ = *insert++; | |
c27eab7e VZ |
585 | |
586 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
587 | for (p = passwd + cbs->endPos; *p; ) | |
588 | *dest++ = *p++; | |
589 | *dest = 0; | |
590 | ||
591 | value = newS; | |
592 | ||
593 | delete[] newS; | |
594 | } | |
595 | } | |
596 | ||
597 | static void | |
af111fc3 | 598 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) |
02e8b2f9 | 599 | { |
2d120f83 JS |
600 | if (!wxGetWindowFromTable(w)) |
601 | // Widget has been deleted! | |
602 | return; | |
66a007fb | 603 | |
2d120f83 | 604 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
7d8268a1 | 605 | tw->SetModified(true); |
02e8b2f9 JS |
606 | } |
607 | ||
66a007fb | 608 | static void |
af111fc3 | 609 | wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs) |
02e8b2f9 | 610 | { |
dfc54541 | 611 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
7d8268a1 | 612 | tw->m_processedDefault = false; |
66a007fb | 613 | |
c27eab7e VZ |
614 | // First, do some stuff if it's a password control: in this case, we need |
615 | // to store the string inside the class because GetValue() can't retrieve | |
616 | // it from the text ctrl. We do *not* do it in other circumstances because | |
617 | // it would double the amount of memory needed. | |
66a007fb | 618 | |
c27eab7e | 619 | if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD ) |
dfc54541 | 620 | { |
c27eab7e | 621 | MergeChangesIntoString(tw->m_value, cbs); |
66a007fb | 622 | |
c27eab7e | 623 | if ( cbs->text->length > 0 ) |
dfc54541 JS |
624 | { |
625 | int i; | |
626 | for (i = 0; i < cbs->text->length; ++i) | |
627 | cbs->text->ptr[i] = '*'; | |
c27eab7e | 628 | cbs->text->ptr[i] = '\0'; |
dfc54541 JS |
629 | } |
630 | } | |
66a007fb | 631 | |
ccb234b4 MB |
632 | if(tw->InSetValue()) |
633 | return; | |
634 | ||
c27eab7e VZ |
635 | // If we're already within an OnChar, return: probably a programmatic |
636 | // insertion. | |
dfc54541 JS |
637 | if (tw->m_tempCallbackStruct) |
638 | return; | |
66a007fb | 639 | |
dfc54541 JS |
640 | // Check for a backspace |
641 | if (cbs->startPos == (cbs->currInsert - 1)) | |
642 | { | |
c27eab7e | 643 | tw->DoSendEvents((void *)cbs, WXK_DELETE); |
66a007fb | 644 | |
dfc54541 JS |
645 | return; |
646 | } | |
66a007fb | 647 | |
c27eab7e | 648 | // Pasting operation: let it through without calling OnChar |
dfc54541 JS |
649 | if (cbs->text->length > 1) |
650 | return; | |
66a007fb | 651 | |
dfc54541 JS |
652 | // Something other than text |
653 | if (cbs->text->ptr == NULL) | |
654 | return; | |
66a007fb | 655 | |
c27eab7e VZ |
656 | // normal key press |
657 | char ch = cbs->text->ptr[0]; | |
658 | tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch); | |
02e8b2f9 JS |
659 | } |
660 | ||
66a007fb | 661 | static void |
af111fc3 | 662 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 663 | { |
2d120f83 JS |
664 | if (!wxGetWindowFromTable(w)) |
665 | return; | |
66a007fb | 666 | |
2d120f83 JS |
667 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
668 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
669 | event.SetEventObject(tw); | |
937013e0 | 670 | tw->HandleWindowEvent(event); |
02e8b2f9 JS |
671 | } |
672 | ||
66a007fb | 673 | static void |
af111fc3 | 674 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) |
02e8b2f9 | 675 | { |
2d120f83 JS |
676 | if (!wxGetWindowFromTable(w)) |
677 | return; | |
66a007fb | 678 | |
2d120f83 JS |
679 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
680 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
681 | event.SetEventObject(tw); | |
937013e0 | 682 | tw->HandleWindowEvent(event); |
02e8b2f9 | 683 | } |
dfc54541 JS |
684 | |
685 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
af111fc3 | 686 | XmAnyCallbackStruct *WXUNUSED(ptr)) |
dfc54541 | 687 | { |
2d120f83 JS |
688 | if (!wxGetWindowFromTable(w)) |
689 | return; | |
66a007fb | 690 | |
2d120f83 | 691 | wxTextCtrl *tw = (wxTextCtrl *) clientData; |
66a007fb | 692 | |
2d120f83 JS |
693 | if (tw->InSetValue()) |
694 | return; | |
66a007fb | 695 | |
2d120f83 JS |
696 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); |
697 | event.SetId(tw->GetId()); | |
698 | event.SetEventObject(tw); | |
699 | tw->ProcessCommand(event); | |
dfc54541 | 700 | } |
c27eab7e | 701 | |
e702ff0f JS |
702 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) |
703 | { | |
704 | Cut(); | |
705 | } | |
706 | ||
707 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
708 | { | |
709 | Copy(); | |
710 | } | |
711 | ||
712 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
713 | { | |
714 | Paste(); | |
715 | } | |
716 | ||
717 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
718 | { | |
719 | Undo(); | |
720 | } | |
721 | ||
722 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
723 | { | |
724 | Redo(); | |
725 | } | |
726 | ||
727 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
728 | { | |
729 | event.Enable( CanCut() ); | |
730 | } | |
731 | ||
732 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
733 | { | |
734 | event.Enable( CanCopy() ); | |
735 | } | |
736 | ||
737 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
738 | { | |
739 | event.Enable( CanPaste() ); | |
740 | } | |
741 | ||
742 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
743 | { | |
744 | event.Enable( CanUndo() ); | |
745 | } | |
746 | ||
747 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
748 | { | |
749 | event.Enable( CanRedo() ); | |
750 | } |