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 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
64 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
65 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
66 EVT_CHAR(wxTextCtrl::OnChar
)
68 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
69 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
70 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
71 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
72 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
74 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
75 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
76 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
77 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
78 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
82 // ============================================================================
84 // ============================================================================
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
91 wxTextCtrl::wxTextCtrl()
93 m_tempCallbackStruct
= (void*) NULL
;
95 m_processedDefault
= false;
98 bool wxTextCtrl::Create(wxWindow
*parent
,
100 const wxString
& value
,
104 const wxValidator
& validator
,
105 const wxString
& name
)
107 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
111 m_tempCallbackStruct
= (void*) NULL
;
113 m_processedDefault
= false;
115 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
117 Bool wantHorizScroll
= (m_windowStyle
& wxHSCROLL
) != 0 ? True
: False
;
118 // If we don't have horizontal scrollbars, we want word wrap.
119 // OpenMotif 2.1 crashes if wantWordWrap is True in Japanese
120 // locale (and probably other multibyte locales). The check might be
122 #if wxCHECK_LESSTIF() || wxCHECK_MOTIF_VERSION( 2, 2 )
123 Bool wantWordWrap
= wantHorizScroll
== True
? False
: True
;
125 Bool wantWordWrap
= False
;
128 if (m_windowStyle
& wxTE_MULTILINE
)
132 XtSetArg (args
[count
], XmNscrollHorizontal
, wantHorizScroll
); ++count
;
134 XtSetArg (args
[count
], (String
) wxFont::GetFontTag(),
135 m_font
.GetFontType( XtDisplay(parentWidget
) ) ); ++count
;
136 XtSetArg (args
[count
], XmNwordWrap
, wantWordWrap
); ++count
;
137 XtSetArg (args
[count
], XmNvalue
, (const char*)value
.mb_str()); ++count
;
138 XtSetArg (args
[count
], XmNeditable
,
139 style
& wxTE_READONLY
? False
: True
); ++count
;
140 XtSetArg (args
[count
], XmNeditMode
, XmMULTI_LINE_EDIT
); ++count
;
143 (WXWidget
) XmCreateScrolledText(parentWidget
,
147 XtManageChild ((Widget
) m_mainWidget
);
151 m_mainWidget
= (WXWidget
)XtVaCreateManagedWidget
156 wxFont::GetFontTag(), m_font
.GetFontType( XtDisplay(parentWidget
) ),
157 XmNvalue
, (const char*)value
.mb_str(),
158 XmNeditable
, (style
& wxTE_READONLY
) ?
164 // TODO: Is this relevant? What does it do?
166 if (!value
.IsNull() && (value
.length() > (unsigned int) noCols
))
167 noCols
= value
.length();
168 XtVaSetValues((Widget
) m_mainWidget
,
174 // remove border if asked for
175 if ( style
& wxNO_BORDER
)
177 XtVaSetValues((Widget
)m_mainWidget
,
178 XmNshadowThickness
, 0,
183 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
185 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
187 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
189 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
191 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
194 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
195 pos
.x
, pos
.y
, size
.x
, size
.y
);
200 WXWidget
wxTextCtrl::GetTopWidget() const
202 return IsMultiLine() ? (WXWidget
)XtParent((Widget
)m_mainWidget
)
206 wxString
wxTextCtrl::GetValue() const
208 wxString str
; // result
210 if (m_windowStyle
& wxTE_PASSWORD
)
212 // the value is stored always in m_value because it can't be retrieved
213 // from the text control
218 str
= wxTextEntry::GetValue();
220 if ( m_tempCallbackStruct
)
222 // the string in the control isn't yet updated, can't use it as is
223 MergeChangesIntoString(str
, (XmTextVerifyCallbackStruct
*)
224 m_tempCallbackStruct
);
231 void wxTextCtrl::DoSetValue(const wxString
& text
, int flags
)
235 XmTextSetString ((Widget
) m_mainWidget
, text
.char_str());
236 XtVaSetValues ((Widget
) m_mainWidget
,
237 XmNcursorPosition
, text
.length(),
240 SetInsertionPoint(text
.length());
241 XmTextShowPosition ((Widget
) m_mainWidget
, text
.length());
244 m_inSetValue
= false;
246 if ( flags
& SetValue_SendEvent
)
247 SendTextUpdatedEvent();
250 bool wxTextCtrl::IsModified() const
255 // Makes modified or unmodified
256 void wxTextCtrl::MarkDirty()
261 void wxTextCtrl::DiscardEdits()
266 int wxTextCtrl::GetNumberOfLines() const
268 // HIDEOUSLY inefficient, but we have no choice.
269 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
274 bool finished
= false;
297 long wxTextCtrl::XYToPosition(long x
, long y
) const
299 /* It seems, that there is a bug in some versions of the Motif library,
300 so the original wxWin-Code doesn't work. */
302 Widget textWidget = (Widget) handle;
303 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
305 /* Now a little workaround: */
307 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
311 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
314 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
323 void wxTextCtrl::ShowPosition(long pos
)
325 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
328 int wxTextCtrl::GetLineLength(long lineNo
) const
330 wxString str
= GetLineText (lineNo
);
331 return (int) str
.length();
334 wxString
wxTextCtrl::GetLineText(long lineNo
) const
336 // HIDEOUSLY inefficient, but we have no choice.
337 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
344 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
349 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
356 return wxEmptyString
;
363 void wxTextCtrl::Command(wxCommandEvent
& event
)
365 SetValue (event
.GetString());
366 ProcessCommand (event
);
369 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
371 // By default, load the first file into the text window.
372 if (event
.GetNumberOfFiles() > 0)
374 LoadFile(event
.GetFiles()[0]);
378 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
380 // Indicates that we should generate a normal command, because
381 // we're letting default behaviour happen (otherwise it's vetoed
382 // by virtue of overriding OnChar)
383 m_processedDefault
= true;
385 if (m_tempCallbackStruct
)
387 XmTextVerifyCallbackStruct
*textStruct
=
388 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
389 textStruct
->doit
= True
;
390 if (isascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
392 textStruct
->text
->ptr
[0] = (char)((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
397 void wxTextCtrl::ChangeFont(bool keepOriginalSize
)
399 wxWindow::ChangeFont(keepOriginalSize
);
402 void wxTextCtrl::ChangeBackgroundColour()
404 wxWindow::ChangeBackgroundColour();
406 /* TODO: should scrollbars be affected? Should probably have separate
407 * function to change them (by default, taken from wxSystemSettings)
409 if (m_windowStyle
& wxTE_MULTILINE
)
411 Widget parent
= XtParent ((Widget
) m_mainWidget
);
414 XtVaGetValues (parent
,
415 XmNhorizontalScrollBar
, &hsb
,
416 XmNverticalScrollBar
, &vsb
,
418 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
420 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
422 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
424 // MBN: why change parent background?
425 // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
429 void wxTextCtrl::ChangeForegroundColour()
431 wxWindow::ChangeForegroundColour();
433 if (m_windowStyle
& wxTE_MULTILINE
)
435 Widget parent
= XtParent ((Widget
) m_mainWidget
);
438 XtVaGetValues (parent
,
439 XmNhorizontalScrollBar
, &hsb
,
440 XmNverticalScrollBar
, &vsb
,
443 /* TODO: should scrollbars be affected? Should probably have separate
444 * function to change them (by default, taken from wxSystemSettings)
446 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
448 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
450 wxDoChangeForegroundColour((WXWidget
) parent
, m_foregroundColour
);
454 void wxTextCtrl::DoSendEvents(void *wxcbs
, long keycode
)
456 // we're in process of updating the text control
457 m_tempCallbackStruct
= wxcbs
;
459 XmTextVerifyCallbackStruct
*cbs
= (XmTextVerifyCallbackStruct
*)wxcbs
;
461 wxKeyEvent
event (wxEVT_CHAR
);
462 event
.SetId(GetId());
463 event
.m_keyCode
= keycode
;
464 event
.SetEventObject(this);
466 // Only if wxTextCtrl::OnChar is called will this be set to True (and
467 // the character passed through)
470 HandleWindowEvent(event
);
472 if ( !InSetValue() && m_processedDefault
)
474 // Can generate a command
475 wxCommandEvent
commandEvent(wxEVT_COMMAND_TEXT_UPDATED
, GetId());
476 commandEvent
.SetEventObject(this);
477 ProcessCommand(commandEvent
);
480 // do it after the (user) event handlers processed the events because
481 // otherwise GetValue() would return incorrect (not yet updated value)
482 m_tempCallbackStruct
= NULL
;
485 wxSize
wxDoGetSingleTextCtrlBestSize( Widget textWidget
,
486 const wxWindow
* window
)
488 Dimension xmargin
, ymargin
, highlight
, shadow
;
491 XtVaGetValues( textWidget
,
492 XmNmarginWidth
, &xmargin
,
493 XmNmarginHeight
, &ymargin
,
495 XmNhighlightThickness
, &highlight
,
496 XmNshadowThickness
, &shadow
,
500 value
= wxMOTIF_STR("|");
503 window
->GetTextExtent( value
, &x
, &y
);
508 return wxSize( x
+ 2 * xmargin
+ 2 * highlight
+ 2 * shadow
,
509 // MBN: +2 necessary: Lesstif bug or mine?
510 y
+ 2 * ymargin
+ 2 * highlight
+ 2 * shadow
+ 2 );
513 wxSize
wxTextCtrl::DoGetBestSize() const
517 wxSize best
= wxControl::DoGetBestSize();
518 #if wxCHECK_MOTIF_VERSION( 2, 3 )
519 // OpenMotif 2.3 gives way too big X sizes
520 wxSize other_best
= wxDoGetSingleTextCtrlBestSize
521 ( (Widget
) GetTopWidget(), this );
522 return wxSize( other_best
.x
, best
.y
);
524 if( best
.x
< 90 ) best
.x
= 90;
530 return wxWindow::DoGetBestSize();
533 // ----------------------------------------------------------------------------
534 // helpers and Motif callbacks
535 // ----------------------------------------------------------------------------
537 static void MergeChangesIntoString(wxString
& value
,
538 XmTextVerifyCallbackStruct
*cbs
)
541 * At least on my system (SunOS 4.1.3 + Motif 1.2), you need to think of
542 * every event as a replace event. cbs->text->ptr gives the replacement
543 * text, cbs->startPos gives the index of the first char affected by the
544 * replace, and cbs->endPos gives the index one more than the last char
545 * affected by the replace (startPos == endPos implies an empty range).
546 * Hence, a deletion is represented by replacing all input text with a
547 * blank string ("", *not* NULL!). A simple insertion that does not
548 * overwrite any text has startPos == endPos.
553 // easy case: the ol value was empty
554 value
= cbs
->text
->ptr
;
558 // merge the changes into the value
559 const char * const passwd
= value
;
560 int len
= value
.length();
562 len
+= ( cbs
->text
->ptr
?
563 strlen(cbs
->text
->ptr
) :
564 0 ) + 1; // + new text (if any) + NUL
565 len
-= cbs
->endPos
- cbs
->startPos
; // - text from affected region.
567 char * newS
= new char [len
];
569 * insert
= cbs
->text
->ptr
;
571 // Copy (old) text from passwd, up to the start posn of the change.
573 const char * p
= passwd
;
574 for (i
= 0; i
< cbs
->startPos
; ++i
)
577 // Copy the text to be inserted).
582 // Finally, copy into newS any remaining text from passwd[endPos] on.
583 for (p
= passwd
+ cbs
->endPos
; *p
; )
594 wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
596 if (!wxGetWindowFromTable(w
))
597 // Widget has been deleted!
600 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
601 tw
->SetModified(true);
605 wxTextWindowModifyProc (Widget
WXUNUSED(w
), XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
607 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
608 tw
->m_processedDefault
= false;
610 // First, do some stuff if it's a password control: in this case, we need
611 // to store the string inside the class because GetValue() can't retrieve
612 // it from the text ctrl. We do *not* do it in other circumstances because
613 // it would double the amount of memory needed.
615 if ( tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
617 MergeChangesIntoString(tw
->m_value
, cbs
);
619 if ( cbs
->text
->length
> 0 )
622 for (i
= 0; i
< cbs
->text
->length
; ++i
)
623 cbs
->text
->ptr
[i
] = '*';
624 cbs
->text
->ptr
[i
] = '\0';
631 // If we're already within an OnChar, return: probably a programmatic
633 if (tw
->m_tempCallbackStruct
)
636 // Check for a backspace
637 if (cbs
->startPos
== (cbs
->currInsert
- 1))
639 tw
->DoSendEvents((void *)cbs
, WXK_DELETE
);
644 // Pasting operation: let it through without calling OnChar
645 if (cbs
->text
->length
> 1)
648 // Something other than text
649 if (cbs
->text
->ptr
== NULL
)
653 char ch
= cbs
->text
->ptr
[0];
654 tw
->DoSendEvents((void *)cbs
, ch
== '\n' ? '\r' : ch
);
658 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
660 if (!wxGetWindowFromTable(w
))
663 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
664 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
665 event
.SetEventObject(tw
);
666 tw
->HandleWindowEvent(event
);
670 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
672 if (!wxGetWindowFromTable(w
))
675 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
676 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
677 event
.SetEventObject(tw
);
678 tw
->HandleWindowEvent(event
);
681 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
682 XmAnyCallbackStruct
*WXUNUSED(ptr
))
684 if (!wxGetWindowFromTable(w
))
687 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
689 if (tw
->InSetValue())
692 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
);
693 event
.SetId(tw
->GetId());
694 event
.SetEventObject(tw
);
695 tw
->ProcessCommand(event
);
698 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
703 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
708 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
713 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
718 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
723 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
725 event
.Enable( CanCut() );
728 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
730 event
.Enable( CanCopy() );
733 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
735 event
.Enable( CanPaste() );
738 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
740 event
.Enable( CanUndo() );
743 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
745 event
.Enable( CanRedo() );