]>
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 'unmodified' | |
424 | void wxTextCtrl::DiscardEdits() | |
425 | { | |
426 | m_modified = FALSE; | |
427 | } | |
428 | ||
429 | int wxTextCtrl::GetNumberOfLines() const | |
430 | { | |
431 | // HIDEOUSLY inefficient, but we have no choice. | |
432 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
433 | if (s) | |
434 | { | |
435 | long i = 0; | |
436 | int currentLine = 0; | |
437 | bool finished = FALSE; | |
438 | while (!finished) | |
439 | { | |
440 | int ch = s[i]; | |
441 | if (ch == '\n') | |
442 | { | |
443 | currentLine++; | |
444 | i++; | |
445 | } | |
446 | else if (ch == 0) | |
447 | { | |
448 | finished = TRUE; | |
449 | } | |
450 | else | |
451 | i++; | |
452 | } | |
453 | ||
454 | XtFree (s); | |
455 | return currentLine; | |
456 | } | |
457 | return 0; | |
458 | } | |
459 | ||
460 | long wxTextCtrl::XYToPosition(long x, long y) const | |
461 | { | |
462 | /* It seems, that there is a bug in some versions of the Motif library, | |
463 | so the original wxWin-Code doesn't work. */ | |
464 | /* | |
465 | Widget textWidget = (Widget) handle; | |
466 | return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y); | |
467 | */ | |
468 | /* Now a little workaround: */ | |
469 | long r=0; | |
470 | for (int i=0; i<y; i++) r+=(GetLineLength(i)+1); | |
471 | return r+x; | |
472 | } | |
473 | ||
474 | bool wxTextCtrl::PositionToXY(long pos, long *x, long *y) const | |
475 | { | |
476 | Position xx, yy; | |
477 | XmTextPosToXY((Widget) m_mainWidget, pos, &xx, &yy); | |
478 | if ( x ) | |
479 | *x = xx; | |
480 | if ( y ) | |
481 | *y = yy; | |
482 | ||
483 | return TRUE; | |
484 | } | |
485 | ||
486 | void wxTextCtrl::ShowPosition(long pos) | |
487 | { | |
488 | XmTextShowPosition ((Widget) m_mainWidget, (XmTextPosition) pos); | |
489 | } | |
490 | ||
491 | int wxTextCtrl::GetLineLength(long lineNo) const | |
492 | { | |
493 | wxString str = GetLineText (lineNo); | |
494 | return (int) str.Length(); | |
495 | } | |
496 | ||
497 | wxString wxTextCtrl::GetLineText(long lineNo) const | |
498 | { | |
499 | // HIDEOUSLY inefficient, but we have no choice. | |
500 | char *s = XmTextGetString ((Widget) m_mainWidget); | |
501 | ||
502 | if (s) | |
503 | { | |
504 | wxString buf(""); | |
505 | long i; | |
506 | int currentLine = 0; | |
507 | for (i = 0; currentLine != lineNo && s[i]; i++ ) | |
508 | if (s[i] == '\n') | |
509 | currentLine++; | |
510 | // Now get the text | |
511 | int j; | |
512 | for (j = 0; s[i] && s[i] != '\n'; i++, j++ ) | |
513 | buf += s[i]; | |
514 | ||
515 | XtFree(s); | |
516 | return buf; | |
517 | } | |
518 | else | |
519 | return wxEmptyString; | |
520 | } | |
521 | ||
522 | /* | |
523 | * Text item | |
524 | */ | |
525 | ||
526 | void wxTextCtrl::Command(wxCommandEvent & event) | |
527 | { | |
528 | SetValue (event.GetString()); | |
529 | ProcessCommand (event); | |
530 | } | |
531 | ||
532 | void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) | |
533 | { | |
534 | // By default, load the first file into the text window. | |
535 | if (event.GetNumberOfFiles() > 0) | |
536 | { | |
537 | LoadFile(event.GetFiles()[0]); | |
538 | } | |
539 | } | |
540 | ||
541 | void wxTextCtrl::OnChar(wxKeyEvent& event) | |
542 | { | |
543 | // Indicates that we should generate a normal command, because | |
544 | // we're letting default behaviour happen (otherwise it's vetoed | |
545 | // by virtue of overriding OnChar) | |
546 | m_processedDefault = TRUE; | |
547 | ||
548 | if (m_tempCallbackStruct) | |
549 | { | |
550 | XmTextVerifyCallbackStruct *textStruct = | |
551 | (XmTextVerifyCallbackStruct *) m_tempCallbackStruct; | |
552 | textStruct->doit = True; | |
553 | if (isascii(event.m_keyCode) && (textStruct->text->length == 1)) | |
554 | { | |
555 | textStruct->text->ptr[0] = ((event.m_keyCode == WXK_RETURN) ? 10 : event.m_keyCode); | |
556 | } | |
557 | } | |
558 | } | |
559 | ||
560 | void wxTextCtrl::ChangeFont(bool keepOriginalSize) | |
561 | { | |
562 | wxWindow::ChangeFont(keepOriginalSize); | |
563 | } | |
564 | ||
565 | void wxTextCtrl::ChangeBackgroundColour() | |
566 | { | |
567 | wxWindow::ChangeBackgroundColour(); | |
568 | ||
569 | /* TODO: should scrollbars be affected? Should probably have separate | |
570 | * function to change them (by default, taken from wxSystemSettings) | |
571 | */ | |
572 | if (m_windowStyle & wxTE_MULTILINE) | |
573 | { | |
574 | Widget parent = XtParent ((Widget) m_mainWidget); | |
575 | Widget hsb, vsb; | |
576 | ||
577 | XtVaGetValues (parent, | |
578 | XmNhorizontalScrollBar, &hsb, | |
579 | XmNverticalScrollBar, &vsb, | |
580 | NULL); | |
581 | wxColour backgroundColour = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE); | |
582 | if (hsb) | |
583 | wxDoChangeBackgroundColour((WXWidget) hsb, backgroundColour, TRUE); | |
584 | if (vsb) | |
585 | wxDoChangeBackgroundColour((WXWidget) vsb, backgroundColour, TRUE); | |
586 | ||
587 | // MBN: why change parent background? | |
588 | // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, TRUE); | |
589 | } | |
590 | } | |
591 | ||
592 | void wxTextCtrl::ChangeForegroundColour() | |
593 | { | |
594 | wxWindow::ChangeForegroundColour(); | |
595 | ||
596 | if (m_windowStyle & wxTE_MULTILINE) | |
597 | { | |
598 | Widget parent = XtParent ((Widget) m_mainWidget); | |
599 | Widget hsb, vsb; | |
600 | ||
601 | XtVaGetValues (parent, | |
602 | XmNhorizontalScrollBar, &hsb, | |
603 | XmNverticalScrollBar, &vsb, | |
604 | NULL); | |
605 | ||
606 | /* TODO: should scrollbars be affected? Should probably have separate | |
607 | * function to change them (by default, taken from wxSystemSettings) | |
608 | if (hsb) | |
609 | DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour); | |
610 | if (vsb) | |
611 | DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour); | |
612 | */ | |
613 | wxDoChangeForegroundColour((WXWidget) parent, m_foregroundColour); | |
614 | } | |
615 | } | |
616 | ||
617 | void wxTextCtrl::DoSendEvents(void *wxcbs, long keycode) | |
618 | { | |
619 | // we're in process of updating the text control | |
620 | m_tempCallbackStruct = wxcbs; | |
621 | ||
622 | XmTextVerifyCallbackStruct *cbs = (XmTextVerifyCallbackStruct *)wxcbs; | |
623 | ||
624 | wxKeyEvent event (wxEVT_CHAR); | |
625 | event.SetId(GetId()); | |
626 | event.m_keyCode = keycode; | |
627 | event.SetEventObject(this); | |
628 | ||
629 | // Only if wxTextCtrl::OnChar is called will this be set to True (and | |
630 | // the character passed through) | |
631 | cbs->doit = False; | |
632 | ||
633 | GetEventHandler()->ProcessEvent(event); | |
634 | ||
635 | if ( !InSetValue() && m_processedDefault ) | |
636 | { | |
637 | // Can generate a command | |
638 | wxCommandEvent commandEvent(wxEVT_COMMAND_TEXT_UPDATED, GetId()); | |
639 | commandEvent.SetEventObject(this); | |
640 | ProcessCommand(commandEvent); | |
641 | } | |
642 | ||
643 | // do it after the (user) event handlers processed the events because | |
644 | // otherwise GetValue() would return incorrect (not yet updated value) | |
645 | m_tempCallbackStruct = NULL; | |
646 | } | |
647 | ||
648 | wxSize wxDoGetSingleTextCtrlBestSize( Widget textWidget, | |
649 | const wxWindow* window ) | |
650 | { | |
651 | Dimension xmargin, ymargin, highlight, shadow; | |
652 | char* value; | |
653 | ||
654 | XtVaGetValues( textWidget, | |
655 | XmNmarginWidth, &xmargin, | |
656 | XmNmarginHeight, &ymargin, | |
657 | XmNvalue, &value, | |
658 | XmNhighlightThickness, &highlight, | |
659 | XmNshadowThickness, &shadow, | |
660 | NULL ); | |
661 | ||
662 | if( !value ) | |
663 | value = "|"; | |
664 | ||
665 | int x, y; | |
666 | window->GetTextExtent( value, &x, &y ); | |
667 | ||
668 | if( x < 100 ) x = 100; | |
669 | ||
670 | return wxSize( x + 2 * xmargin + 2 * highlight + 2 * shadow, | |
671 | // MBN: +2 necessary: Lesstif bug or mine? | |
672 | y + 2 * ymargin + 2 * highlight + 2 * shadow + 2 ); | |
673 | } | |
674 | ||
675 | wxSize wxTextCtrl::DoGetBestSize() const | |
676 | { | |
677 | if( IsSingleLine() ) | |
678 | return wxDoGetSingleTextCtrlBestSize( (Widget)m_mainWidget, this ); | |
679 | else | |
680 | return wxWindow::DoGetBestSize(); | |
681 | } | |
682 | ||
683 | // ---------------------------------------------------------------------------- | |
684 | // helpers and Motif callbacks | |
685 | // ---------------------------------------------------------------------------- | |
686 | ||
687 | static void MergeChangesIntoString(wxString& value, | |
688 | XmTextVerifyCallbackStruct *cbs) | |
689 | { | |
690 | /* _sm_ | |
691 | * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of | |
692 | * every event as a replace event. cbs->text->ptr gives the replacement | |
693 | * text, cbs->startPos gives the index of the first char affected by the | |
694 | * replace, and cbs->endPos gives the index one more than the last char | |
695 | * affected by the replace (startPos == endPos implies an empty range). | |
696 | * Hence, a deletion is represented by replacing all input text with a | |
697 | * blank string ("", *not* NULL!). A simple insertion that does not | |
698 | * overwrite any text has startPos == endPos. | |
699 | */ | |
700 | ||
701 | if ( !value ) | |
702 | { | |
703 | // easy case: the ol value was empty | |
704 | value = cbs->text->ptr; | |
705 | } | |
706 | else | |
707 | { | |
708 | // merge the changes into the value | |
709 | const char * const passwd = value; | |
710 | int len = value.length(); | |
711 | ||
712 | len += ( cbs->text->ptr ? | |
713 | strlen(cbs->text->ptr) : | |
714 | 0 ) + 1; // + new text (if any) + NUL | |
715 | len -= cbs->endPos - cbs->startPos; // - text from affected region. | |
716 | ||
717 | char * newS = new char [len]; | |
718 | char * dest = newS, | |
719 | * insert = cbs->text->ptr; | |
720 | ||
721 | // Copy (old) text from passwd, up to the start posn of the change. | |
722 | int i; | |
723 | const char * p = passwd; | |
724 | for (i = 0; i < cbs->startPos; ++i) | |
725 | *dest++ = *p++; | |
726 | ||
727 | // Copy the text to be inserted). | |
728 | if (insert) | |
729 | while (*insert) | |
730 | *dest++ = *insert++; | |
731 | ||
732 | // Finally, copy into newS any remaining text from passwd[endPos] on. | |
733 | for (p = passwd + cbs->endPos; *p; ) | |
734 | *dest++ = *p++; | |
735 | *dest = 0; | |
736 | ||
737 | value = newS; | |
738 | ||
739 | delete[] newS; | |
740 | } | |
741 | } | |
742 | ||
743 | static void | |
744 | wxTextWindowChangedProc (Widget w, XtPointer clientData, XtPointer WXUNUSED(ptr)) | |
745 | { | |
746 | if (!wxGetWindowFromTable(w)) | |
747 | // Widget has been deleted! | |
748 | return; | |
749 | ||
750 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
751 | tw->SetModified(TRUE); | |
752 | } | |
753 | ||
754 | static void | |
755 | wxTextWindowModifyProc (Widget WXUNUSED(w), XtPointer clientData, XmTextVerifyCallbackStruct *cbs) | |
756 | { | |
757 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
758 | tw->m_processedDefault = FALSE; | |
759 | ||
760 | // First, do some stuff if it's a password control: in this case, we need | |
761 | // to store the string inside the class because GetValue() can't retrieve | |
762 | // it from the text ctrl. We do *not* do it in other circumstances because | |
763 | // it would double the amount of memory needed. | |
764 | ||
765 | if ( tw->GetWindowStyleFlag() & wxTE_PASSWORD ) | |
766 | { | |
767 | MergeChangesIntoString(tw->m_value, cbs); | |
768 | ||
769 | if ( cbs->text->length > 0 ) | |
770 | { | |
771 | int i; | |
772 | for (i = 0; i < cbs->text->length; ++i) | |
773 | cbs->text->ptr[i] = '*'; | |
774 | cbs->text->ptr[i] = '\0'; | |
775 | } | |
776 | } | |
777 | ||
778 | if(tw->InSetValue()) | |
779 | return; | |
780 | ||
781 | // If we're already within an OnChar, return: probably a programmatic | |
782 | // insertion. | |
783 | if (tw->m_tempCallbackStruct) | |
784 | return; | |
785 | ||
786 | // Check for a backspace | |
787 | if (cbs->startPos == (cbs->currInsert - 1)) | |
788 | { | |
789 | tw->DoSendEvents((void *)cbs, WXK_DELETE); | |
790 | ||
791 | return; | |
792 | } | |
793 | ||
794 | // Pasting operation: let it through without calling OnChar | |
795 | if (cbs->text->length > 1) | |
796 | return; | |
797 | ||
798 | // Something other than text | |
799 | if (cbs->text->ptr == NULL) | |
800 | return; | |
801 | ||
802 | // normal key press | |
803 | char ch = cbs->text->ptr[0]; | |
804 | tw->DoSendEvents((void *)cbs, ch == '\n' ? '\r' : ch); | |
805 | } | |
806 | ||
807 | static void | |
808 | wxTextWindowGainFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) | |
809 | { | |
810 | if (!wxGetWindowFromTable(w)) | |
811 | return; | |
812 | ||
813 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
814 | wxFocusEvent event(wxEVT_SET_FOCUS, tw->GetId()); | |
815 | event.SetEventObject(tw); | |
816 | tw->GetEventHandler()->ProcessEvent(event); | |
817 | } | |
818 | ||
819 | static void | |
820 | wxTextWindowLoseFocusProc (Widget w, XtPointer clientData, XmAnyCallbackStruct *WXUNUSED(cbs)) | |
821 | { | |
822 | if (!wxGetWindowFromTable(w)) | |
823 | return; | |
824 | ||
825 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
826 | wxFocusEvent event(wxEVT_KILL_FOCUS, tw->GetId()); | |
827 | event.SetEventObject(tw); | |
828 | tw->GetEventHandler()->ProcessEvent(event); | |
829 | } | |
830 | ||
831 | static void wxTextWindowActivateProc(Widget w, XtPointer clientData, | |
832 | XmAnyCallbackStruct *WXUNUSED(ptr)) | |
833 | { | |
834 | if (!wxGetWindowFromTable(w)) | |
835 | return; | |
836 | ||
837 | wxTextCtrl *tw = (wxTextCtrl *) clientData; | |
838 | ||
839 | if (tw->InSetValue()) | |
840 | return; | |
841 | ||
842 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER); | |
843 | event.SetId(tw->GetId()); | |
844 | event.SetEventObject(tw); | |
845 | tw->ProcessCommand(event); | |
846 | } | |
847 | ||
848 | void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) | |
849 | { | |
850 | Cut(); | |
851 | } | |
852 | ||
853 | void wxTextCtrl::OnCopy(wxCommandEvent& WXUNUSED(event)) | |
854 | { | |
855 | Copy(); | |
856 | } | |
857 | ||
858 | void wxTextCtrl::OnPaste(wxCommandEvent& WXUNUSED(event)) | |
859 | { | |
860 | Paste(); | |
861 | } | |
862 | ||
863 | void wxTextCtrl::OnUndo(wxCommandEvent& WXUNUSED(event)) | |
864 | { | |
865 | Undo(); | |
866 | } | |
867 | ||
868 | void wxTextCtrl::OnRedo(wxCommandEvent& WXUNUSED(event)) | |
869 | { | |
870 | Redo(); | |
871 | } | |
872 | ||
873 | void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent& event) | |
874 | { | |
875 | event.Enable( CanCut() ); | |
876 | } | |
877 | ||
878 | void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent& event) | |
879 | { | |
880 | event.Enable( CanCopy() ); | |
881 | } | |
882 | ||
883 | void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent& event) | |
884 | { | |
885 | event.Enable( CanPaste() ); | |
886 | } | |
887 | ||
888 | void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent& event) | |
889 | { | |
890 | event.Enable( CanUndo() ); | |
891 | } | |
892 | ||
893 | void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent& event) | |
894 | { | |
895 | event.Enable( CanRedo() ); | |
896 | } |