1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/motif/textctrl.cpp
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #include <sys/types.h>
26 #include "wx/textctrl.h"
30 #include "wx/settings.h"
33 #include "wx/filefn.h"
36 #pragma message disable nosimpint
40 #pragma message enable nosimpint
43 #include "wx/motif/private.h"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
49 // helper: inserts the new text in the value of the text ctrl and returns the
51 static void MergeChangesIntoString(wxString
& value
,
52 XmTextVerifyCallbackStruct
*textStruct
);
55 static void wxTextWindowChangedProc(Widget w
, XtPointer clientData
, XtPointer ptr
);
56 static void wxTextWindowModifyProc(Widget w
, XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
);
57 static void wxTextWindowGainFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
58 static void wxTextWindowLoseFocusProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*cbs
);
59 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*ptr
);
61 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
62 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
63 EVT_CHAR(wxTextCtrl::OnChar
)
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
)
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
)
79 // ============================================================================
81 // ============================================================================
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
88 wxTextCtrl::wxTextCtrl()
90 m_tempCallbackStruct
= NULL
;
92 m_processedDefault
= false;
95 bool wxTextCtrl::Create(wxWindow
*parent
,
97 const wxString
& value
,
101 const wxValidator
& validator
,
102 const wxString
& name
)
104 if( !CreateControl( parent
, id
, pos
, size
, style
, validator
, name
) )
108 m_tempCallbackStruct
= NULL
;
110 m_processedDefault
= false;
112 Widget parentWidget
= (Widget
) parent
->GetClientWidget();
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
119 #if wxCHECK_LESSTIF() || wxCHECK_MOTIF_VERSION( 2, 2 )
120 Bool wantWordWrap
= wantHorizScroll
== True
? False
: True
;
122 Bool wantWordWrap
= False
;
125 if (m_windowStyle
& wxTE_MULTILINE
)
129 XtSetArg (args
[count
], XmNscrollHorizontal
, wantHorizScroll
); ++count
;
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
;
140 (WXWidget
) XmCreateScrolledText(parentWidget
,
144 XtManageChild ((Widget
) m_mainWidget
);
148 m_mainWidget
= (WXWidget
)XtVaCreateManagedWidget
153 wxFont::GetFontTag(), m_font
.GetFontType( XtDisplay(parentWidget
) ),
154 XmNvalue
, (const char*)value
.mb_str(),
155 XmNeditable
, (style
& wxTE_READONLY
) ?
161 // TODO: Is this relevant? What does it do?
163 if (!value
.empty() && (value
.length() > (unsigned int) noCols
))
164 noCols
= value
.length();
165 XtVaSetValues((Widget
) m_mainWidget
,
171 // remove border if asked for
172 if ( style
& wxNO_BORDER
)
174 XtVaSetValues((Widget
)m_mainWidget
,
175 XmNshadowThickness
, 0,
180 XtAddCallback((Widget
) m_mainWidget
, XmNvalueChangedCallback
, (XtCallbackProc
)wxTextWindowChangedProc
, (XtPointer
)this);
182 XtAddCallback((Widget
) m_mainWidget
, XmNmodifyVerifyCallback
, (XtCallbackProc
)wxTextWindowModifyProc
, (XtPointer
)this);
184 XtAddCallback((Widget
) m_mainWidget
, XmNactivateCallback
, (XtCallbackProc
)wxTextWindowActivateProc
, (XtPointer
)this);
186 XtAddCallback((Widget
) m_mainWidget
, XmNfocusCallback
, (XtCallbackProc
)wxTextWindowGainFocusProc
, (XtPointer
)this);
188 XtAddCallback((Widget
) m_mainWidget
, XmNlosingFocusCallback
, (XtCallbackProc
)wxTextWindowLoseFocusProc
, (XtPointer
)this);
191 AttachWidget (parent
, m_mainWidget
, (WXWidget
) NULL
,
192 pos
.x
, pos
.y
, size
.x
, size
.y
);
197 WXWidget
wxTextCtrl::GetTopWidget() const
199 return IsMultiLine() ? (WXWidget
)XtParent((Widget
)m_mainWidget
)
203 wxString
wxTextCtrl::GetValue() const
205 wxString str
; // result
207 if (m_windowStyle
& wxTE_PASSWORD
)
209 // the value is stored always in m_value because it can't be retrieved
210 // from the text control
215 str
= wxTextEntry::GetValue();
217 if ( m_tempCallbackStruct
)
219 // the string in the control isn't yet updated, can't use it as is
220 MergeChangesIntoString(str
, (XmTextVerifyCallbackStruct
*)
221 m_tempCallbackStruct
);
228 void wxTextCtrl::DoSetValue(const wxString
& text
, int flags
)
232 XmTextSetString ((Widget
) m_mainWidget
, text
.char_str());
233 XtVaSetValues ((Widget
) m_mainWidget
,
234 XmNcursorPosition
, text
.length(),
237 SetInsertionPoint(text
.length());
238 XmTextShowPosition ((Widget
) m_mainWidget
, text
.length());
241 m_inSetValue
= false;
243 if ( flags
& SetValue_SendEvent
)
244 SendTextUpdatedEvent();
247 bool wxTextCtrl::IsModified() const
252 // Makes modified or unmodified
253 void wxTextCtrl::MarkDirty()
258 void wxTextCtrl::DiscardEdits()
263 int wxTextCtrl::GetNumberOfLines() const
265 // HIDEOUSLY inefficient, but we have no choice.
266 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
271 bool finished
= false;
294 long wxTextCtrl::XYToPosition(long x
, long y
) const
296 /* It seems, that there is a bug in some versions of the Motif library,
297 so the original wxWin-Code doesn't work. */
299 Widget textWidget = (Widget) handle;
300 return (long) XmTextXYToPos (textWidget, (Position) x, (Position) y);
302 /* Now a little workaround: */
304 for (int i
=0; i
<y
; i
++) r
+=(GetLineLength(i
)+1);
308 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
311 XmTextPosToXY((Widget
) m_mainWidget
, pos
, &xx
, &yy
);
320 void wxTextCtrl::ShowPosition(long pos
)
322 XmTextShowPosition ((Widget
) m_mainWidget
, (XmTextPosition
) pos
);
325 int wxTextCtrl::GetLineLength(long lineNo
) const
327 wxString str
= GetLineText (lineNo
);
328 return (int) str
.length();
331 wxString
wxTextCtrl::GetLineText(long lineNo
) const
333 // HIDEOUSLY inefficient, but we have no choice.
334 char *s
= XmTextGetString ((Widget
) m_mainWidget
);
341 for (i
= 0; currentLine
!= lineNo
&& s
[i
]; i
++ )
346 for (j
= 0; s
[i
] && s
[i
] != '\n'; i
++, j
++ )
353 return wxEmptyString
;
360 void wxTextCtrl::Command(wxCommandEvent
& event
)
362 SetValue (event
.GetString());
363 ProcessCommand (event
);
366 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
368 // By default, load the first file into the text window.
369 if (event
.GetNumberOfFiles() > 0)
371 LoadFile(event
.GetFiles()[0]);
375 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
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;
382 if (m_tempCallbackStruct
)
384 XmTextVerifyCallbackStruct
*textStruct
=
385 (XmTextVerifyCallbackStruct
*) m_tempCallbackStruct
;
386 textStruct
->doit
= True
;
387 if (wxIsascii(event
.m_keyCode
) && (textStruct
->text
->length
== 1))
389 textStruct
->text
->ptr
[0] = (char)((event
.m_keyCode
== WXK_RETURN
) ? 10 : event
.m_keyCode
);
394 void wxTextCtrl::ChangeFont(bool keepOriginalSize
)
396 wxWindow::ChangeFont(keepOriginalSize
);
399 void wxTextCtrl::ChangeBackgroundColour()
401 wxWindow::ChangeBackgroundColour();
403 /* TODO: should scrollbars be affected? Should probably have separate
404 * function to change them (by default, taken from wxSystemSettings)
406 if (m_windowStyle
& wxTE_MULTILINE
)
408 Widget parent
= XtParent ((Widget
) m_mainWidget
);
411 XtVaGetValues (parent
,
412 XmNhorizontalScrollBar
, &hsb
,
413 XmNverticalScrollBar
, &vsb
,
415 wxColour backgroundColour
= wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
);
417 wxDoChangeBackgroundColour((WXWidget
) hsb
, backgroundColour
, true);
419 wxDoChangeBackgroundColour((WXWidget
) vsb
, backgroundColour
, true);
421 // MBN: why change parent background?
422 // DoChangeBackgroundColour((WXWidget) parent, m_backgroundColour, true);
426 void wxTextCtrl::ChangeForegroundColour()
428 wxWindow::ChangeForegroundColour();
430 if (m_windowStyle
& wxTE_MULTILINE
)
432 Widget parent
= XtParent ((Widget
) m_mainWidget
);
435 XtVaGetValues (parent
,
436 XmNhorizontalScrollBar
, &hsb
,
437 XmNverticalScrollBar
, &vsb
,
440 /* TODO: should scrollbars be affected? Should probably have separate
441 * function to change them (by default, taken from wxSystemSettings)
443 DoChangeForegroundColour((WXWidget) hsb, m_foregroundColour);
445 DoChangeForegroundColour((WXWidget) vsb, m_foregroundColour);
447 wxDoChangeForegroundColour((WXWidget
) parent
, m_foregroundColour
);
451 void wxTextCtrl::DoSendEvents(void *wxcbs
, long keycode
)
453 // we're in process of updating the text control
454 m_tempCallbackStruct
= wxcbs
;
456 XmTextVerifyCallbackStruct
*cbs
= (XmTextVerifyCallbackStruct
*)wxcbs
;
458 wxKeyEvent
event (wxEVT_CHAR
);
459 event
.SetId(GetId());
460 event
.m_keyCode
= keycode
;
461 event
.SetEventObject(this);
463 // Only if wxTextCtrl::OnChar is called will this be set to True (and
464 // the character passed through)
467 HandleWindowEvent(event
);
469 if ( !InSetValue() && m_processedDefault
)
471 // Can generate a command
472 wxCommandEvent
commandEvent(wxEVT_TEXT
, GetId());
473 commandEvent
.SetEventObject(this);
474 ProcessCommand(commandEvent
);
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
;
482 wxSize
wxDoGetSingleTextCtrlBestSize( Widget textWidget
,
483 const wxWindow
* window
)
485 Dimension xmargin
, ymargin
, highlight
, shadow
;
488 XtVaGetValues( textWidget
,
489 XmNmarginWidth
, &xmargin
,
490 XmNmarginHeight
, &ymargin
,
492 XmNhighlightThickness
, &highlight
,
493 XmNshadowThickness
, &shadow
,
497 value
= wxMOTIF_STR("|");
500 window
->GetTextExtent( value
, &x
, &y
);
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 );
510 wxSize
wxTextCtrl::DoGetBestSize() const
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
);
521 if( best
.x
< 90 ) best
.x
= 90;
527 return wxWindow::DoGetBestSize();
530 // ----------------------------------------------------------------------------
531 // helpers and Motif callbacks
532 // ----------------------------------------------------------------------------
534 static void MergeChangesIntoString(wxString
& value
,
535 XmTextVerifyCallbackStruct
*cbs
)
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.
550 // easy case: the ol value was empty
551 value
= cbs
->text
->ptr
;
555 // merge the changes into the value
556 const char * const passwd
= value
;
557 int len
= value
.length();
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.
564 char * newS
= new char [len
];
566 * insert
= cbs
->text
->ptr
;
568 // Copy (old) text from passwd, up to the start posn of the change.
570 const char * p
= passwd
;
571 for (i
= 0; i
< cbs
->startPos
; ++i
)
574 // Copy the text to be inserted).
579 // Finally, copy into newS any remaining text from passwd[endPos] on.
580 for (p
= passwd
+ cbs
->endPos
; *p
; )
591 wxTextWindowChangedProc (Widget w
, XtPointer clientData
, XtPointer
WXUNUSED(ptr
))
593 if (!wxGetWindowFromTable(w
))
594 // Widget has been deleted!
597 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
598 tw
->SetModified(true);
602 wxTextWindowModifyProc (Widget
WXUNUSED(w
), XtPointer clientData
, XmTextVerifyCallbackStruct
*cbs
)
604 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
605 tw
->m_processedDefault
= false;
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.
612 if ( tw
->GetWindowStyleFlag() & wxTE_PASSWORD
)
614 MergeChangesIntoString(tw
->m_value
, cbs
);
616 if ( cbs
->text
->length
> 0 )
619 for (i
= 0; i
< cbs
->text
->length
; ++i
)
620 cbs
->text
->ptr
[i
] = '*';
621 cbs
->text
->ptr
[i
] = '\0';
628 // If we're already within an OnChar, return: probably a programmatic
630 if (tw
->m_tempCallbackStruct
)
633 // Check for a backspace
634 if (cbs
->startPos
== (cbs
->currInsert
- 1))
636 tw
->DoSendEvents((void *)cbs
, WXK_DELETE
);
641 // Pasting operation: let it through without calling OnChar
642 if (cbs
->text
->length
> 1)
645 // Something other than text
646 if (cbs
->text
->ptr
== NULL
)
650 char ch
= cbs
->text
->ptr
[0];
651 tw
->DoSendEvents((void *)cbs
, ch
== '\n' ? '\r' : ch
);
655 wxTextWindowGainFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
657 if (!wxGetWindowFromTable(w
))
660 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
661 wxFocusEvent
event(wxEVT_SET_FOCUS
, tw
->GetId());
662 event
.SetEventObject(tw
);
663 tw
->HandleWindowEvent(event
);
667 wxTextWindowLoseFocusProc (Widget w
, XtPointer clientData
, XmAnyCallbackStruct
*WXUNUSED(cbs
))
669 if (!wxGetWindowFromTable(w
))
672 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
673 wxFocusEvent
event(wxEVT_KILL_FOCUS
, tw
->GetId());
674 event
.SetEventObject(tw
);
675 tw
->HandleWindowEvent(event
);
678 static void wxTextWindowActivateProc(Widget w
, XtPointer clientData
,
679 XmAnyCallbackStruct
*WXUNUSED(ptr
))
681 if (!wxGetWindowFromTable(w
))
684 wxTextCtrl
*tw
= (wxTextCtrl
*) clientData
;
686 if (tw
->InSetValue())
689 wxCommandEvent
event(wxEVT_TEXT_ENTER
);
690 event
.SetId(tw
->GetId());
691 event
.SetEventObject(tw
);
692 tw
->ProcessCommand(event
);
695 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
700 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
705 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
710 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
715 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
720 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
722 event
.Enable( CanCut() );
725 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
727 event
.Enable( CanCopy() );
730 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
732 event
.Enable( CanPaste() );
735 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
737 event
.Enable( CanUndo() );
740 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
742 event
.Enable( CanRedo() );