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