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