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"
24 #define XtParent XTPARENT
27 #include <sys/types.h>
31 #include "wx/textctrl.h"
35 #include "wx/settings.h"
38 #include "wx/filefn.h"
41 #pragma message disable nosimpint
45 #pragma message enable nosimpint
48 #include "wx/motif/private.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 // helper: inserts the new text in the value of the text ctrl and returns the
56 static void MergeChangesIntoString(wxString
& value
,
57 XmTextVerifyCallbackStruct
*textStruct
);
60 static void wxTextWindowChangedProc(Widget w
, XtPointer clientData
, XtPointer ptr
);
61 static void wxTextWindowModifyProc(Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
);
62 static void wxTextWindowGainFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
63 static void wxTextWindowLoseFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
64 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*ptr
);
66 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
68 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
69 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
70 EVT_CHAR(wxTextCtrl::OnChar
)
72 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
73 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
74 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
75 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
76 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
78 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
79 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
80 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
81 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
82 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
86 // ============================================================================
88 // ============================================================================
90 // ----------------------------------------------------------------------------
92 // ----------------------------------------------------------------------------
95 wxTextCtrl::wxTextCtrl()
97 m_tempCallbackStruct
= (void*) NULL
;
99 m_processedDefault
= false;
102 bool wxTextCtrl::Create(wxWindow
*parent
,
104 const wxString
& value
,
108 const wxValidator
& validator
,
109 const wxString
& name
)
111 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
115 m_tempCallbackStruct
= (void*) NULL
;
117 m_processedDefault
= false;
119 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
121 Bool wantHorizScroll
= (m_windowStyle
& wxHSCROLL
) != 0 ? True
: False
;
122 // If we don't have horizontal scrollbars, we want word wrap.
123 // OpenMotif 2.1 crashes if wantWordWrap is True in Japanese
124 // locale (and probably other multibyte locales). The check might be
126 #if wxCHECK_LESSTIF() || wxCHECK_MOTIF_VERSION( 2, 2 )
127 Bool wantWordWrap
= wantHorizScroll
== True
? False
: True
;
129 Bool wantWordWrap
= False
;
132 if (m_windowStyle
& wxTE_MULTILINE
)
136 XtSetArg (args
[count
], XmNscrollHorizontal
, wantHorizScroll
); ++count
;
138 XtSetArg (args
[count
], (String
) wxFont::GetFontTag(),
139 m_font
.GetFontType( XtDisplay(parentWidget
) ) ); ++count
;
140 XtSetArg (args
[count
], XmNwordWrap
, wantWordWrap
); ++count
;
141 XtSetArg (args
[count
], XmNvalue
, (const char*)value
.mb_str()); ++count
;
142 XtSetArg (args
[count
], XmNeditable
,
143 style
& wxTE_READONLY
? False
: True
); ++count
;
144 XtSetArg (args
[count
], XmNeditMode
, XmMULTI_LINE_EDIT
); ++count
;
147 (WXWidget
) XmCreateScrolledText(parentWidget
,
151 XtManageChild ((Widget
) m_mainWidget
);
155 m_mainWidget
= (WXWidget
)XtVaCreateManagedWidget
160 wxFont::GetFontTag(), m_font
.GetFontType( XtDisplay(parentWidget
) ),
161 XmNvalue
, (const char*)value
.mb_str(),
162 XmNeditable
, (style
& wxTE_READONLY
) ?
168 // TODO: Is this relevant? What does it do?
170 if (!value
.IsNull() && (value
.length() > (unsigned int) noCols
))
171 noCols
= value
.length();
172 XtVaSetValues((Widget
) m_mainWidget
,
178 // remove border if asked for
179 if ( style
& wxNO_BORDER
)
181 XtVaSetValues((Widget
)m_mainWidget
,
182 XmNshadowThickness
, 0,
187 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
189 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
191 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
193 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
195 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
198 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
199 pos
.x
, pos
.y
, size
.x
, size
.y
);
204 WXWidget
wxTextCtrl::GetTopWidget() const
206 return IsMultiLine() ? (WXWidget
)XtParent((Widget
)m_mainWidget
)
210 wxString
wxTextCtrl::GetValue() const
212 wxString str
; // result
214 if (m_windowStyle
& wxTE_PASSWORD
)
216 // the value is stored always in m_value because it can't be retrieved
217 // from the text control
222 str
= wxTextEntry::GetValue();
224 if ( m_tempCallbackStruct
)
226 // the string in the control isn't yet updated, can't use it as is
227 MergeChangesIntoString(str
, (XmTextVerifyCallbackStruct
*)
228 m_tempCallbackStruct
);
235 void wxTextCtrl::DoSetValue(const wxString
& text
, int flags
)
239 XmTextSetString ((Widget
) m_mainWidget
, text
.char_str());
240 XtVaSetValues ((Widget
) m_mainWidget
,
241 XmNcursorPosition
, text
.length(),
244 SetInsertionPoint(text
.length());
245 XmTextShowPosition ((Widget
) m_mainWidget
, text
.length());
248 m_inSetValue
= false;
250 if ( flags
& SetValue_SendEvent
)
251 SendTextUpdatedEvent();
254 bool wxTextCtrl::IsModified() const
259 // Makes modified or unmodified
260 void wxTextCtrl::MarkDirty()
265 void wxTextCtrl::DiscardEdits()
270 int wxTextCtrl::GetNumberOfLines() const
272 // HIDEOUSLY inefficient, but we have no choice.
273 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
278 bool finished
= false;
301 long wxTextCtrl::XYToPosition(long x
, long y
) const
303 /* It seems, that there is a bug in some versions of the Motif library,
304 so the original wxWin-Code doesn't work. */
306 Widget textWidget = (Widget) handle;
307 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
309 /* Now a little workaround: */
311 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
315 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
318 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
327 void wxTextCtrl::ShowPosition(long pos
)
329 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
332 int wxTextCtrl::GetLineLength(long lineNo
) const
334 wxString str
= GetLineText (lineNo
);
335 return (int) str
.length();
338 wxString
wxTextCtrl::GetLineText(long lineNo
) const
340 // HIDEOUSLY inefficient, but we have no choice.
341 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
348 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
353 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
360 return wxEmptyString
;
367 void wxTextCtrl::Command(wxCommandEvent
& event
)
369 SetValue (event
.GetString());
370 ProcessCommand (event
);
373 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
375 // By default, load the first file into the text window.
376 if (event
.GetNumberOfFiles() > 0)
378 LoadFile(event
.GetFiles()[0]);
382 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
384 // Indicates that we should generate a normal command, because
385 // we're letting default behaviour happen (otherwise it's vetoed
386 // by virtue of overriding OnChar)
387 m_processedDefault
= true;
389 if (m_tempCallbackStruct
)
391 XmTextVerifyCallbackStruct
*textStruct
=
392 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
393 textStruct
->doit
= True
;
394 if (isascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
396 textStruct
->text
->ptr
[0] = (char)((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
401 void wxTextCtrl::ChangeFont(bool keepOriginalSize
)
403 wxWindow::ChangeFont(keepOriginalSize
);
406 void wxTextCtrl::ChangeBackgroundColour()
408 wxWindow::ChangeBackgroundColour();
410 /* TODO: should scrollbars be affected? Should probably have separate
411 * function to change them (by default, taken from wxSystemSettings)
413 if (m_windowStyle
& wxTE_MULTILINE
)
415 Widget parent
= XtParent ((Widget
) m_mainWidget
);
418 XtVaGetValues (parent
,
419 XmNhorizontalScrollBar
, &hsb
,
420 XmNverticalScrollBar
, &vsb
,
422 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
424 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
426 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
428 // MBN: why change parent background?
429 // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
433 void wxTextCtrl::ChangeForegroundColour()
435 wxWindow::ChangeForegroundColour();
437 if (m_windowStyle
& wxTE_MULTILINE
)
439 Widget parent
= XtParent ((Widget
) m_mainWidget
);
442 XtVaGetValues (parent
,
443 XmNhorizontalScrollBar
, &hsb
,
444 XmNverticalScrollBar
, &vsb
,
447 /* TODO: should scrollbars be affected? Should probably have separate
448 * function to change them (by default, taken from wxSystemSettings)
450 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
452 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
454 wxDoChangeForegroundColour((WXWidget
) parent
, m_foregroundColour
);
458 void wxTextCtrl::DoSendEvents(void *wxcbs
, long keycode
)
460 // we're in process of updating the text control
461 m_tempCallbackStruct
= wxcbs
;
463 XmTextVerifyCallbackStruct
*cbs
= (XmTextVerifyCallbackStruct
*)wxcbs
;
465 wxKeyEvent
event (wxEVT_CHAR
);
466 event
.SetId(GetId());
467 event
.m_keyCode
= keycode
;
468 event
.SetEventObject(this);
470 // Only if wxTextCtrl::OnChar is called will this be set to True (and
471 // the character passed through)
474 HandleWindowEvent(event
);
476 if ( !InSetValue() && m_processedDefault
)
478 // Can generate a command
479 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
480 commandEvent
.SetEventObject(this);
481 ProcessCommand(commandEvent
);
484 // do it after the (user) event handlers processed the events because
485 // otherwise GetValue() would return incorrect (not yet updated value)
486 m_tempCallbackStruct
= NULL
;
489 wxSize
wxDoGetSingleTextCtrlBestSize( Widget textWidget
,
490 const wxWindow
* window
)
492 Dimension xmargin
, ymargin
, highlight
, shadow
;
495 XtVaGetValues( textWidget
,
496 XmNmarginWidth
, &xmargin
,
497 XmNmarginHeight
, &ymargin
,
499 XmNhighlightThickness
, &highlight
,
500 XmNshadowThickness
, &shadow
,
504 value
= wxMOTIF_STR("|");
507 window
->GetTextExtent( value
, &x
, &y
);
512 return wxSize( x
+ 2 * xmargin
+ 2 * highlight
+ 2 * shadow
,
513 // MBN: +2 necessary: Lesstif bug or mine?
514 y
+ 2 * ymargin
+ 2 * highlight
+ 2 * shadow
+ 2 );
517 wxSize
wxTextCtrl::DoGetBestSize() const
521 wxSize best
= wxControl::DoGetBestSize();
522 #if wxCHECK_MOTIF_VERSION( 2, 3 )
523 // OpenMotif 2.3 gives way too big X sizes
524 wxSize other_best
= wxDoGetSingleTextCtrlBestSize
525 ( (Widget
) GetTopWidget(), this );
526 return wxSize( other_best
.x
, best
.y
);
528 if( best
.x
< 90 ) best
.x
= 90;
534 return wxWindow::DoGetBestSize();
537 // ----------------------------------------------------------------------------
538 // helpers and Motif callbacks
539 // ----------------------------------------------------------------------------
541 static void MergeChangesIntoString(wxString
& value
,
542 XmTextVerifyCallbackStruct
*cbs
)
545 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
546 * every event as a replace event. cbs->text->ptr gives the replacement
547 * text, cbs->startPos gives the index of the first char affected by the
548 * replace, and cbs->endPos gives the index one more than the last char
549 * affected by the replace (startPos == endPos implies an empty range).
550 * Hence, a deletion is represented by replacing all input text with a
551 * blank string ("", *not* NULL!). A simple insertion that does not
552 * overwrite any text has startPos == endPos.
557 // easy case: the ol value was empty
558 value
= cbs
->text
->ptr
;
562 // merge the changes into the value
563 const char * const passwd
= value
;
564 int len
= value
.length();
566 len
+= ( cbs
->text
->ptr
?
567 strlen(cbs
->text
->ptr
) :
568 0 ) + 1; // + new text (if any) + NUL
569 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
571 char * newS
= new char [len
];
573 * insert
= cbs
->text
->ptr
;
575 // Copy (old) text from passwd, up to the start posn of the change.
577 const char * p
= passwd
;
578 for (i
= 0; i
< cbs
->startPos
; ++i
)
581 // Copy the text to be inserted).
586 // Finally, copy into newS any remaining text from passwd[endPos] on.
587 for (p
= passwd
+ cbs
->endPos
; *p
; )
598 wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
600 if (!wxGetWindowFromTable(w
))
601 // Widget has been deleted!
604 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
605 tw
->SetModified(true);
609 wxTextWindowModifyProc (Widget
WXUNUSED(w
), XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
611 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
612 tw
->m_processedDefault
= false;
614 // First, do some stuff if it's a password control: in this case, we need
615 // to store the string inside the class because GetValue() can't retrieve
616 // it from the text ctrl. We do *not* do it in other circumstances because
617 // it would double the amount of memory needed.
619 if ( tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
621 MergeChangesIntoString(tw
->m_value
, cbs
);
623 if ( cbs
->text
->length
> 0 )
626 for (i
= 0; i
< cbs
->text
->length
; ++i
)
627 cbs
->text
->ptr
[i
] = '*';
628 cbs
->text
->ptr
[i
] = '\0';
635 // If we're already within an OnChar, return: probably a programmatic
637 if (tw
->m_tempCallbackStruct
)
640 // Check for a backspace
641 if (cbs
->startPos
== (cbs
->currInsert
- 1))
643 tw
->DoSendEvents((void *)cbs
, WXK_DELETE
);
648 // Pasting operation: let it through without calling OnChar
649 if (cbs
->text
->length
> 1)
652 // Something other than text
653 if (cbs
->text
->ptr
== NULL
)
657 char ch
= cbs
->text
->ptr
[0];
658 tw
->DoSendEvents((void *)cbs
, ch
== '\n' ? '\r' : ch
);
662 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
664 if (!wxGetWindowFromTable(w
))
667 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
668 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
669 event
.SetEventObject(tw
);
670 tw
->HandleWindowEvent(event
);
674 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
676 if (!wxGetWindowFromTable(w
))
679 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
680 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
681 event
.SetEventObject(tw
);
682 tw
->HandleWindowEvent(event
);
685 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
686 XmAnyCallbackStruct
*WXUNUSED(ptr
))
688 if (!wxGetWindowFromTable(w
))
691 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
693 if (tw
->InSetValue())
696 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
);
697 event
.SetId(tw
->GetId());
698 event
.SetEventObject(tw
);
699 tw
->ProcessCommand(event
);
702 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
707 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
712 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
717 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
722 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
727 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
729 event
.Enable( CanCut() );
732 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
734 event
.Enable( CanCopy() );
737 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
739 event
.Enable( CanPaste() );
742 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
744 event
.Enable( CanUndo() );
747 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
749 event
.Enable( CanRedo() );