1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/textctrl.cpp
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #include <sys/types.h>
27 #include "wx/textctrl.h"
31 #include "wx/settings.h"
34 #include "wx/filefn.h"
37 #pragma message disable nosimpint
41 #pragma message enable nosimpint
44 #include "wx/motif/private.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 // helper: inserts the new text in the value of the text ctrl and returns the
52 static void MergeChangesIntoString(wxString
& value
,
53 XmTextVerifyCallbackStruct
*textStruct
);
56 static void wxTextWindowChangedProc(Widget w
, XtPointer clientData
, XtPointer ptr
);
57 static void wxTextWindowModifyProc(Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
);
58 static void wxTextWindowGainFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
59 static void wxTextWindowLoseFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
60 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*ptr
);
62 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
63 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
64 EVT_CHAR(wxTextCtrl::OnChar
)
66 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
67 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
68 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
69 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
70 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
72 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
73 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
74 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
75 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
76 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
80 // ============================================================================
82 // ============================================================================
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
89 wxTextCtrl::wxTextCtrl()
91 m_tempCallbackStruct
= NULL
;
93 m_processedDefault
= false;
96 bool wxTextCtrl::Create(wxWindow
*parent
,
98 const wxString
& value
,
102 const wxValidator
& validator
,
103 const wxString
& name
)
105 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
109 m_tempCallbackStruct
= NULL
;
111 m_processedDefault
= false;
113 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
115 Bool wantHorizScroll
= (m_windowStyle
& wxHSCROLL
) != 0 ? True
: False
;
116 // If we don't have horizontal scrollbars, we want word wrap.
117 // OpenMotif 2.1 crashes if wantWordWrap is True in Japanese
118 // locale (and probably other multibyte locales). The check might be
120 #if wxCHECK_LESSTIF() || wxCHECK_MOTIF_VERSION( 2, 2 )
121 Bool wantWordWrap
= wantHorizScroll
== True
? False
: True
;
123 Bool wantWordWrap
= False
;
126 if (m_windowStyle
& wxTE_MULTILINE
)
130 XtSetArg (args
[count
], XmNscrollHorizontal
, wantHorizScroll
); ++count
;
132 XtSetArg (args
[count
], (String
) wxFont::GetFontTag(),
133 m_font
.GetFontType( XtDisplay(parentWidget
) ) ); ++count
;
134 XtSetArg (args
[count
], XmNwordWrap
, wantWordWrap
); ++count
;
135 XtSetArg (args
[count
], XmNvalue
, (const char*)value
.mb_str()); ++count
;
136 XtSetArg (args
[count
], XmNeditable
,
137 style
& wxTE_READONLY
? False
: True
); ++count
;
138 XtSetArg (args
[count
], XmNeditMode
, XmMULTI_LINE_EDIT
); ++count
;
141 (WXWidget
) XmCreateScrolledText(parentWidget
,
145 XtManageChild ((Widget
) m_mainWidget
);
149 m_mainWidget
= (WXWidget
)XtVaCreateManagedWidget
154 wxFont::GetFontTag(), m_font
.GetFontType( XtDisplay(parentWidget
) ),
155 XmNvalue
, (const char*)value
.mb_str(),
156 XmNeditable
, (style
& wxTE_READONLY
) ?
162 // TODO: Is this relevant? What does it do?
164 if (!value
.empty() && (value
.length() > (unsigned int) noCols
))
165 noCols
= value
.length();
166 XtVaSetValues((Widget
) m_mainWidget
,
172 // remove border if asked for
173 if ( style
& wxNO_BORDER
)
175 XtVaSetValues((Widget
)m_mainWidget
,
176 XmNshadowThickness
, 0,
181 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
183 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
185 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
187 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
189 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
192 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
193 pos
.x
, pos
.y
, size
.x
, size
.y
);
198 WXWidget
wxTextCtrl::GetTopWidget() const
200 return IsMultiLine() ? (WXWidget
)XtParent((Widget
)m_mainWidget
)
204 wxString
wxTextCtrl::GetValue() const
206 wxString str
; // result
208 if (m_windowStyle
& wxTE_PASSWORD
)
210 // the value is stored always in m_value because it can't be retrieved
211 // from the text control
216 str
= wxTextEntry::GetValue();
218 if ( m_tempCallbackStruct
)
220 // the string in the control isn't yet updated, can't use it as is
221 MergeChangesIntoString(str
, (XmTextVerifyCallbackStruct
*)
222 m_tempCallbackStruct
);
229 void wxTextCtrl::DoSetValue(const wxString
& text
, int flags
)
233 XmTextSetString ((Widget
) m_mainWidget
, text
.char_str());
234 XtVaSetValues ((Widget
) m_mainWidget
,
235 XmNcursorPosition
, text
.length(),
238 SetInsertionPoint(text
.length());
239 XmTextShowPosition ((Widget
) m_mainWidget
, text
.length());
242 m_inSetValue
= false;
244 if ( flags
& SetValue_SendEvent
)
245 SendTextUpdatedEvent();
248 bool wxTextCtrl::IsModified() const
253 // Makes modified or unmodified
254 void wxTextCtrl::MarkDirty()
259 void wxTextCtrl::DiscardEdits()
264 int wxTextCtrl::GetNumberOfLines() const
266 // HIDEOUSLY inefficient, but we have no choice.
267 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
272 bool finished
= false;
295 long wxTextCtrl::XYToPosition(long x
, long y
) const
297 /* It seems, that there is a bug in some versions of the Motif library,
298 so the original wxWin-Code doesn't work. */
300 Widget textWidget = (Widget) handle;
301 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
303 /* Now a little workaround: */
305 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
309 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
312 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
321 void wxTextCtrl::ShowPosition(long pos
)
323 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
326 int wxTextCtrl::GetLineLength(long lineNo
) const
328 wxString str
= GetLineText (lineNo
);
329 return (int) str
.length();
332 wxString
wxTextCtrl::GetLineText(long lineNo
) const
334 // HIDEOUSLY inefficient, but we have no choice.
335 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
342 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
347 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
354 return wxEmptyString
;
361 void wxTextCtrl::Command(wxCommandEvent
& event
)
363 SetValue (event
.GetString());
364 ProcessCommand (event
);
367 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
369 // By default, load the first file into the text window.
370 if (event
.GetNumberOfFiles() > 0)
372 LoadFile(event
.GetFiles()[0]);
376 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
378 // Indicates that we should generate a normal command, because
379 // we're letting default behaviour happen (otherwise it's vetoed
380 // by virtue of overriding OnChar)
381 m_processedDefault
= true;
383 if (m_tempCallbackStruct
)
385 XmTextVerifyCallbackStruct
*textStruct
=
386 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
387 textStruct
->doit
= True
;
388 if (wxIsascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
390 textStruct
->text
->ptr
[0] = (char)((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
395 void wxTextCtrl::ChangeFont(bool keepOriginalSize
)
397 wxWindow::ChangeFont(keepOriginalSize
);
400 void wxTextCtrl::ChangeBackgroundColour()
402 wxWindow::ChangeBackgroundColour();
404 /* TODO: should scrollbars be affected? Should probably have separate
405 * function to change them (by default, taken from wxSystemSettings)
407 if (m_windowStyle
& wxTE_MULTILINE
)
409 Widget parent
= XtParent ((Widget
) m_mainWidget
);
412 XtVaGetValues (parent
,
413 XmNhorizontalScrollBar
, &hsb
,
414 XmNverticalScrollBar
, &vsb
,
416 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
418 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
420 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
422 // MBN: why change parent background?
423 // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
427 void wxTextCtrl::ChangeForegroundColour()
429 wxWindow::ChangeForegroundColour();
431 if (m_windowStyle
& wxTE_MULTILINE
)
433 Widget parent
= XtParent ((Widget
) m_mainWidget
);
436 XtVaGetValues (parent
,
437 XmNhorizontalScrollBar
, &hsb
,
438 XmNverticalScrollBar
, &vsb
,
441 /* TODO: should scrollbars be affected? Should probably have separate
442 * function to change them (by default, taken from wxSystemSettings)
444 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
446 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
448 wxDoChangeForegroundColour((WXWidget
) parent
, m_foregroundColour
);
452 void wxTextCtrl::DoSendEvents(void *wxcbs
, long keycode
)
454 // we're in process of updating the text control
455 m_tempCallbackStruct
= wxcbs
;
457 XmTextVerifyCallbackStruct
*cbs
= (XmTextVerifyCallbackStruct
*)wxcbs
;
459 wxKeyEvent
event (wxEVT_CHAR
);
460 event
.SetId(GetId());
461 event
.m_keyCode
= keycode
;
462 event
.SetEventObject(this);
464 // Only if wxTextCtrl::OnChar is called will this be set to True (and
465 // the character passed through)
468 HandleWindowEvent(event
);
470 if ( !InSetValue() && m_processedDefault
)
472 // Can generate a command
473 wxCommandEvent
commandEvent(wxEVT_TEXT
, GetId());
474 commandEvent
.SetEventObject(this);
475 ProcessCommand(commandEvent
);
478 // do it after the (user) event handlers processed the events because
479 // otherwise GetValue() would return incorrect (not yet updated value)
480 m_tempCallbackStruct
= NULL
;
483 wxSize
wxDoGetSingleTextCtrlBestSize( Widget textWidget
,
484 const wxWindow
* window
)
486 Dimension xmargin
, ymargin
, highlight
, shadow
;
489 XtVaGetValues( textWidget
,
490 XmNmarginWidth
, &xmargin
,
491 XmNmarginHeight
, &ymargin
,
493 XmNhighlightThickness
, &highlight
,
494 XmNshadowThickness
, &shadow
,
498 value
= wxMOTIF_STR("|");
501 window
->GetTextExtent( value
, &x
, &y
);
506 return wxSize( x
+ 2 * xmargin
+ 2 * highlight
+ 2 * shadow
,
507 // MBN: +2 necessary: Lesstif bug or mine?
508 y
+ 2 * ymargin
+ 2 * highlight
+ 2 * shadow
+ 2 );
511 wxSize
wxTextCtrl::DoGetBestSize() const
515 wxSize best
= wxControl::DoGetBestSize();
516 #if wxCHECK_MOTIF_VERSION( 2, 3 )
517 // OpenMotif 2.3 gives way too big X sizes
518 wxSize other_best
= wxDoGetSingleTextCtrlBestSize
519 ( (Widget
) GetTopWidget(), this );
520 return wxSize( other_best
.x
, best
.y
);
522 if( best
.x
< 90 ) best
.x
= 90;
528 return wxWindow::DoGetBestSize();
531 // ----------------------------------------------------------------------------
532 // helpers and Motif callbacks
533 // ----------------------------------------------------------------------------
535 static void MergeChangesIntoString(wxString
& value
,
536 XmTextVerifyCallbackStruct
*cbs
)
539 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
540 * every event as a replace event. cbs->text->ptr gives the replacement
541 * text, cbs->startPos gives the index of the first char affected by the
542 * replace, and cbs->endPos gives the index one more than the last char
543 * affected by the replace (startPos == endPos implies an empty range).
544 * Hence, a deletion is represented by replacing all input text with a
545 * blank string ("", *not* NULL!). A simple insertion that does not
546 * overwrite any text has startPos == endPos.
551 // easy case: the ol value was empty
552 value
= cbs
->text
->ptr
;
556 // merge the changes into the value
557 const char * const passwd
= value
;
558 int len
= value
.length();
560 len
+= ( cbs
->text
->ptr
?
561 strlen(cbs
->text
->ptr
) :
562 0 ) + 1; // + new text (if any) + NUL
563 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
565 char * newS
= new char [len
];
567 * insert
= cbs
->text
->ptr
;
569 // Copy (old) text from passwd, up to the start posn of the change.
571 const char * p
= passwd
;
572 for (i
= 0; i
< cbs
->startPos
; ++i
)
575 // Copy the text to be inserted).
580 // Finally, copy into newS any remaining text from passwd[endPos] on.
581 for (p
= passwd
+ cbs
->endPos
; *p
; )
592 wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
594 if (!wxGetWindowFromTable(w
))
595 // Widget has been deleted!
598 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
599 tw
->SetModified(true);
603 wxTextWindowModifyProc (Widget
WXUNUSED(w
), XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
605 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
606 tw
->m_processedDefault
= false;
608 // First, do some stuff if it's a password control: in this case, we need
609 // to store the string inside the class because GetValue() can't retrieve
610 // it from the text ctrl. We do *not* do it in other circumstances because
611 // it would double the amount of memory needed.
613 if ( tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
615 MergeChangesIntoString(tw
->m_value
, cbs
);
617 if ( cbs
->text
->length
> 0 )
620 for (i
= 0; i
< cbs
->text
->length
; ++i
)
621 cbs
->text
->ptr
[i
] = '*';
622 cbs
->text
->ptr
[i
] = '\0';
629 // If we're already within an OnChar, return: probably a programmatic
631 if (tw
->m_tempCallbackStruct
)
634 // Check for a backspace
635 if (cbs
->startPos
== (cbs
->currInsert
- 1))
637 tw
->DoSendEvents((void *)cbs
, WXK_DELETE
);
642 // Pasting operation: let it through without calling OnChar
643 if (cbs
->text
->length
> 1)
646 // Something other than text
647 if (cbs
->text
->ptr
== NULL
)
651 char ch
= cbs
->text
->ptr
[0];
652 tw
->DoSendEvents((void *)cbs
, ch
== '\n' ? '\r' : ch
);
656 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
658 if (!wxGetWindowFromTable(w
))
661 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
662 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
663 event
.SetEventObject(tw
);
664 tw
->HandleWindowEvent(event
);
668 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
670 if (!wxGetWindowFromTable(w
))
673 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
674 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
675 event
.SetEventObject(tw
);
676 tw
->HandleWindowEvent(event
);
679 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
680 XmAnyCallbackStruct
*WXUNUSED(ptr
))
682 if (!wxGetWindowFromTable(w
))
685 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
687 if (tw
->InSetValue())
690 wxCommandEvent
event(wxEVT_TEXT_ENTER
);
691 event
.SetId(tw
->GetId());
692 event
.SetEventObject(tw
);
693 tw
->ProcessCommand(event
);
696 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
701 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
706 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
711 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
716 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
721 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
723 event
.Enable( CanCut() );
726 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
728 event
.Enable( CanCopy() );
731 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
733 event
.Enable( CanPaste() );
736 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
738 event
.Enable( CanUndo() );
741 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
743 event
.Enable( CanRedo() );