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