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