1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/textctrl.cpp
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/textctrl.h"
23 #include "wx/button.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
31 #include <sys/types.h>
37 #if wxUSE_STD_IOSTREAM
45 #include "wx/filefn.h"
46 #include "wx/sysopt.h"
48 #include "wx/mac/uma.h"
49 #include "wx/mac/carbon/private/mactext.h"
55 virtual ~wxMacFunctor() {}
57 virtual void* operator()() = 0 ;
59 static void* CallBackProc( void *param
)
61 wxMacFunctor
* f
= (wxMacFunctor
*) param
;
62 void *result
= (*f
)() ;
67 template<typename classtype
, typename param1type
>
69 class wxMacObjectFunctor1
: public wxMacFunctor
71 typedef void (classtype::*function
)( param1type p1
) ;
72 typedef void (classtype::*ref_function
)( const param1type
& p1
) ;
74 wxMacObjectFunctor1( classtype
*obj
, function f
, param1type p1
) :
82 wxMacObjectFunctor1( classtype
*obj
, ref_function f
, param1type p1
) :
90 virtual ~wxMacObjectFunctor1() {}
92 virtual void* operator()()
94 (m_object
->*m_function
)( m_param1
) ;
100 param1type m_param1
;
103 function m_function
;
104 ref_function m_refFunction
;
108 template<typename classtype
, typename param1type
>
109 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
111 wxMacObjectFunctor1
<classtype
, param1type
> params(object
, function
, p1
) ;
113 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
117 template<typename classtype
, typename param1type
>
118 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
120 wxMacObjectFunctor1
<classtype
,param1type
> params(object
, function
, p1
) ;
122 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
126 template<typename classtype
, typename param1type
>
127 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
130 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
135 template<typename classtype
, typename param1type
>
136 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
139 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
144 class WXDLLEXPORT wxMacPortSaver
146 DECLARE_NO_COPY_CLASS(wxMacPortSaver
)
149 wxMacPortSaver( GrafPtr port
);
157 Clips to the visible region of a control within the current port
160 class WXDLLEXPORT wxMacWindowClipper
: public wxMacPortSaver
162 DECLARE_NO_COPY_CLASS(wxMacWindowClipper
)
165 wxMacWindowClipper( const wxWindow
* win
);
166 ~wxMacWindowClipper();
169 RgnHandle m_formerClip
;
173 // common parts for implementations based on MLTE
175 class wxMacMLTEControl
: public wxMacTextControl
178 wxMacMLTEControl( wxTextCtrl
*peer
) ;
180 virtual wxString
GetStringValue() const ;
181 virtual void SetStringValue( const wxString
&str
) ;
183 static TXNFrameOptions
FrameOptionsFromWXStyle( long wxStyle
) ;
185 void AdjustCreationAttributes( const wxColour
& background
, bool visible
) ;
187 virtual void SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
) ;
188 virtual void SetBackgroundColour(const wxColour
& col
);
189 virtual void SetStyle( long start
, long end
, const wxTextAttr
& style
) ;
190 virtual void Copy() ;
192 virtual void Paste() ;
193 virtual bool CanPaste() const ;
194 virtual void SetEditable( bool editable
) ;
195 virtual wxTextPos
GetLastPosition() const ;
196 virtual void Replace( long from
, long to
, const wxString
&str
) ;
197 virtual void Remove( long from
, long to
) ;
198 virtual void GetSelection( long* from
, long* to
) const ;
199 virtual void SetSelection( long from
, long to
) ;
201 virtual void WriteText( const wxString
& str
) ;
203 virtual bool HasOwnContextMenu() const
205 TXNCommandEventSupportOptions options
;
206 TXNGetCommandEventSupport( m_txn
, & options
) ;
207 return options
& kTXNSupportEditCommandProcessing
;
210 virtual void CheckSpelling(bool check
)
212 TXNSetSpellCheckAsYouType( m_txn
, (Boolean
) check
);
214 virtual void Clear() ;
216 virtual bool CanUndo() const ;
217 virtual void Undo() ;
218 virtual bool CanRedo() const;
219 virtual void Redo() ;
220 virtual int GetNumberOfLines() const ;
221 virtual long XYToPosition(long x
, long y
) const ;
222 virtual bool PositionToXY(long pos
, long *x
, long *y
) const ;
223 virtual void ShowPosition( long pos
) ;
224 virtual int GetLineLength(long lineNo
) const ;
225 virtual wxString
GetLineText(long lineNo
) const ;
227 void SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
) ;
228 TXNObject
GetTXNObject() { return m_txn
; }
231 void TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
) ;
236 // implementation available under OSX
238 class wxMacMLTEHIViewControl
: public wxMacMLTEControl
241 wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
244 const wxSize
& size
, long style
) ;
245 virtual ~wxMacMLTEHIViewControl() ;
247 virtual OSStatus
SetFocus( ControlFocusPart focusPart
) ;
248 virtual bool HasFocus() const ;
249 virtual void SetBackgroundColour(const wxColour
& col
) ;
252 HIViewRef m_scrollView
;
253 HIViewRef m_textView
;
256 // 'classic' MLTE implementation
258 class wxMacMLTEClassicControl
: public wxMacMLTEControl
261 wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
264 const wxSize
& size
, long style
) ;
265 virtual ~wxMacMLTEClassicControl() ;
267 virtual void VisibilityChanged(bool shown
) ;
268 virtual void SuperChangedPosition() ;
270 virtual void MacControlUserPaneDrawProc(wxInt16 part
) ;
271 virtual wxInt16
MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
) ;
272 virtual wxInt16
MacControlUserPaneTrackingProc(wxInt16 x
, wxInt16 y
, void* actionProc
) ;
273 virtual void MacControlUserPaneIdleProc() ;
274 virtual wxInt16
MacControlUserPaneKeyDownProc(wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
) ;
275 virtual void MacControlUserPaneActivateProc(bool activating
) ;
276 virtual wxInt16
MacControlUserPaneFocusProc(wxInt16 action
) ;
277 virtual void MacControlUserPaneBackgroundProc(void* info
) ;
279 virtual bool SetupCursor( const wxPoint
& WXUNUSED(pt
) )
281 MacControlUserPaneIdleProc();
285 virtual void SetRect( Rect
*r
) ;
290 void MacUpdatePosition() ;
291 void MacActivatePaneText(bool setActive
) ;
292 void MacFocusPaneText(bool setFocus
) ;
293 void MacSetObjectVisibility(bool vis
) ;
296 TXNFrameID m_txnFrameID
;
298 WindowRef m_txnWindow
;
299 // bounds of the control as we last did set the txn frames
300 Rect m_txnControlBounds
;
301 Rect m_txnVisBounds
;
303 static pascal void TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
) ;
304 static pascal void TXNScrollInfoProc(
305 SInt32 iValue
, SInt32 iMaximumValue
,
306 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
) ;
308 ControlRef m_sbHorizontal
;
309 SInt32 m_lastHorizontalValue
;
310 ControlRef m_sbVertical
;
311 SInt32 m_lastVerticalValue
;
315 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
317 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
318 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
319 EVT_CHAR(wxTextCtrl::OnChar
)
320 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
321 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
322 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
323 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
324 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
325 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
326 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
328 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
330 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
331 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
332 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
333 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
334 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
335 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
336 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
340 void wxTextCtrl::Init()
346 m_privateContextMenu
= NULL
;
347 m_triggerOnSetValue
= true ;
350 wxTextCtrl::~wxTextCtrl()
352 delete m_privateContextMenu
;
355 bool wxTextCtrl::Create( wxWindow
*parent
,
361 const wxValidator
& validator
,
362 const wxString
& name
)
364 m_macIsUserPane
= false ;
367 if ( ! (style
& wxNO_BORDER
) )
368 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
370 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
373 if ( m_windowStyle
& wxTE_MULTILINE
)
375 // always turn on this style for multi-line controls
376 m_windowStyle
|= wxTE_PROCESS_ENTER
;
377 style
|= wxTE_PROCESS_ENTER
;
380 CreatePeer( str
, pos
, size
, style
);
382 MacPostControlCreate(pos
, size
) ;
384 // only now the embedding is correct and we can do a positioning update
386 MacSuperChangedPosition() ;
388 if ( m_windowStyle
& wxTE_READONLY
)
389 SetEditable( false ) ;
391 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
396 void wxTextCtrl::CreatePeer(
399 const wxSize
& size
, long style
)
401 bool forceMLTE
= false ;
403 #if wxUSE_SYSTEM_OPTIONS
404 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE
) == 1))
410 if ( UMAGetSystemVersion() >= 0x1050 )
415 if ( m_windowStyle
& wxTE_MULTILINE
)
416 m_peer
= new wxMacMLTEHIViewControl( this , str
, pos
, size
, style
) ;
421 if ( !(m_windowStyle
& wxTE_MULTILINE
) && !forceMLTE
)
423 m_peer
= new wxMacUnicodeTextControl( this , str
, pos
, size
, style
) ;
427 // the horizontal single line scrolling bug that made us keep the classic implementation
429 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
431 m_peer
= new wxMacMLTEClassicControl( this , str
, pos
, size
, style
) ;
435 void wxTextCtrl::MacSuperChangedPosition()
437 wxWindow::MacSuperChangedPosition() ;
438 GetPeer()->SuperChangedPosition() ;
441 void wxTextCtrl::MacVisibilityChanged()
443 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
446 void wxTextCtrl::MacCheckSpelling(bool check
)
448 GetPeer()->CheckSpelling(check
);
451 wxString
wxTextCtrl::GetValue() const
453 return GetPeer()->GetStringValue() ;
456 void wxTextCtrl::GetSelection(long* from
, long* to
) const
458 GetPeer()->GetSelection( from
, to
) ;
461 void wxTextCtrl::DoSetValue(const wxString
& str
, int flags
)
464 if ( GetValue() == str
)
467 GetPeer()->SetStringValue( str
) ;
469 if ( (flags
& SetValue_SendEvent
) && m_triggerOnSetValue
)
471 SendTextUpdatedEvent();
475 void wxTextCtrl::SetMaxLength(unsigned long len
)
480 bool wxTextCtrl::SetFont( const wxFont
& font
)
482 if ( !wxTextCtrlBase::SetFont( font
) )
485 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle() ) ;
490 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
492 GetPeer()->SetStyle( start
, end
, style
) ;
497 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
499 wxTextCtrlBase::SetDefaultStyle( style
) ;
500 SetStyle( kTXNUseCurrentSelection
, kTXNUseCurrentSelection
, GetDefaultStyle() ) ;
505 // Clipboard operations
507 void wxTextCtrl::Copy()
513 void wxTextCtrl::Cut()
519 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
520 event
.SetEventObject( this );
521 GetEventHandler()->ProcessEvent( event
);
525 void wxTextCtrl::Paste()
531 // TODO: eventually we should add setting the default style again
533 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
534 event
.SetEventObject( this );
535 GetEventHandler()->ProcessEvent( event
);
539 bool wxTextCtrl::CanCopy() const
541 // Can copy if there's a selection
543 GetSelection( &from
, &to
);
548 bool wxTextCtrl::CanCut() const
553 // Can cut if there's a selection
555 GetSelection( &from
, &to
);
560 bool wxTextCtrl::CanPaste() const
565 return GetPeer()->CanPaste() ;
568 void wxTextCtrl::SetEditable(bool editable
)
570 if ( editable
!= m_editable
)
572 m_editable
= editable
;
573 GetPeer()->SetEditable( editable
) ;
577 void wxTextCtrl::SetInsertionPoint(long pos
)
579 SetSelection( pos
, pos
) ;
582 void wxTextCtrl::SetInsertionPointEnd()
584 wxTextPos pos
= GetLastPosition();
585 SetInsertionPoint( pos
);
588 long wxTextCtrl::GetInsertionPoint() const
591 GetSelection( &begin
, &end
) ;
596 wxTextPos
wxTextCtrl::GetLastPosition() const
598 return GetPeer()->GetLastPosition() ;
601 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
603 GetPeer()->Replace( from
, to
, str
) ;
606 void wxTextCtrl::Remove(long from
, long to
)
608 GetPeer()->Remove( from
, to
) ;
611 void wxTextCtrl::SetSelection(long from
, long to
)
613 GetPeer()->SetSelection( from
, to
) ;
616 void wxTextCtrl::WriteText(const wxString
& str
)
618 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
619 if ( !wxIsMainThread() )
621 // unfortunately CW 8 is not able to correctly deduce the template types,
622 // so we have to instantiate explicitly
623 wxMacMPRemoteGUICall
<wxTextCtrl
,wxString
>( this , &wxTextCtrl::WriteText
, str
) ;
628 GetPeer()->WriteText( str
) ;
631 void wxTextCtrl::AppendText(const wxString
& text
)
633 SetInsertionPointEnd();
637 void wxTextCtrl::Clear()
642 bool wxTextCtrl::IsModified() const
647 bool wxTextCtrl::IsEditable() const
649 return IsEnabled() && m_editable
;
652 bool wxTextCtrl::AcceptsFocus() const
654 // we don't want focus if we can't be edited
655 return /*IsEditable() && */ wxControl::AcceptsFocus();
658 wxSize
wxTextCtrl::DoGetBestSize() const
662 // these are the numbers from the HIG:
663 // we reduce them by the borders first
666 switch ( m_windowVariant
)
668 case wxWINDOW_VARIANT_NORMAL
:
672 case wxWINDOW_VARIANT_SMALL
:
676 case wxWINDOW_VARIANT_MINI
:
685 // as the above numbers have some free space around the text
686 // we get 5 lines like this anyway
687 if ( m_windowStyle
& wxTE_MULTILINE
)
690 if ( !HasFlag(wxNO_BORDER
) )
693 return wxSize(wText
, hText
);
696 // ----------------------------------------------------------------------------
698 // ----------------------------------------------------------------------------
700 void wxTextCtrl::Undo()
706 void wxTextCtrl::Redo()
712 bool wxTextCtrl::CanUndo() const
717 return GetPeer()->CanUndo() ;
720 bool wxTextCtrl::CanRedo() const
725 return GetPeer()->CanRedo() ;
728 void wxTextCtrl::MarkDirty()
733 void wxTextCtrl::DiscardEdits()
738 int wxTextCtrl::GetNumberOfLines() const
740 return GetPeer()->GetNumberOfLines() ;
743 long wxTextCtrl::XYToPosition(long x
, long y
) const
745 return GetPeer()->XYToPosition( x
, y
) ;
748 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
750 return GetPeer()->PositionToXY( pos
, x
, y
) ;
753 void wxTextCtrl::ShowPosition(long pos
)
755 return GetPeer()->ShowPosition(pos
) ;
758 int wxTextCtrl::GetLineLength(long lineNo
) const
760 return GetPeer()->GetLineLength(lineNo
) ;
763 wxString
wxTextCtrl::GetLineText(long lineNo
) const
765 return GetPeer()->GetLineText(lineNo
) ;
768 void wxTextCtrl::Command(wxCommandEvent
& event
)
770 SetValue(event
.GetString());
771 ProcessCommand(event
);
774 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
776 // By default, load the first file into the text window.
777 if (event
.GetNumberOfFiles() > 0)
778 LoadFile( event
.GetFiles()[0] );
781 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
783 int key
= event
.GetKeyCode() ;
784 bool eat_key
= false ;
786 if ( key
== 'a' && event
.MetaDown() )
793 if ( key
== 'c' && event
.MetaDown() )
801 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
802 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
803 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
810 // Check if we have reached the max # of chars (if it is set), but still
811 // allow navigation and deletion
812 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
813 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&&
814 key
!= WXK_BACK
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) )
817 // eat it, we don't want to add more than allowed # of characters
819 // TODO: generate EVT_TEXT_MAXLEN()
823 // assume that any key not processed yet is going to modify the control
826 if ( key
== 'v' && event
.MetaDown() )
834 if ( key
== 'x' && event
.MetaDown() )
845 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
847 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
848 event
.SetEventObject( this );
849 event
.SetString( GetValue() );
850 if ( GetEventHandler()->ProcessEvent(event
) )
854 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
856 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
857 if ( tlw
&& tlw
->GetDefaultItem() )
859 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
860 if ( def
&& def
->IsEnabled() )
862 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
863 event
.SetEventObject(def
);
870 // this will make wxWidgets eat the ENTER key so that
871 // we actually prevent line wrapping in a single line text control
877 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
880 if (!event
.ShiftDown())
881 flags
|= wxNavigationKeyEvent::IsForward
;
882 if (event
.ControlDown())
883 flags
|= wxNavigationKeyEvent::WinChange
;
890 // This is necessary (don't know why);
891 // otherwise the tab will not be inserted.
892 WriteText(wxT("\t"));
903 // perform keystroke handling
907 if ( ( key
>= 0x20 && key
< WXK_START
) ||
908 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
913 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
914 event1
.SetEventObject( this );
915 wxPostEvent( GetEventHandler(), event1
);
919 // ----------------------------------------------------------------------------
920 // standard handlers for standard edit menu events
921 // ----------------------------------------------------------------------------
923 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
928 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
933 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
938 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
943 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
948 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
952 GetSelection( &from
, &to
);
953 if (from
!= -1 && to
!= -1)
957 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
959 SetSelection(-1, -1);
962 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
964 event
.Enable( CanCut() );
967 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
969 event
.Enable( CanCopy() );
972 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
974 event
.Enable( CanPaste() );
977 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
979 event
.Enable( CanUndo() );
982 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
984 event
.Enable( CanRedo() );
987 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
991 GetSelection( &from
, &to
);
992 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
995 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
997 event
.Enable(GetLastPosition() > 0);
1000 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
1002 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1004 if ( GetPeer()->HasOwnContextMenu() )
1010 if (m_privateContextMenu
== NULL
)
1012 m_privateContextMenu
= new wxMenu
;
1013 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1014 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1015 m_privateContextMenu
->AppendSeparator();
1016 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1017 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1018 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1019 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1020 m_privateContextMenu
->AppendSeparator();
1021 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1024 if (m_privateContextMenu
!= NULL
)
1025 PopupMenu(m_privateContextMenu
);
1028 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
1030 if ( !GetPeer()->SetupCursor( pt
) )
1031 return wxWindow::MacSetupCursor( pt
) ;
1036 // ----------------------------------------------------------------------------
1037 // implementation base class
1038 // ----------------------------------------------------------------------------
1040 wxMacTextControl::wxMacTextControl(wxTextCtrl
* peer
) :
1041 wxMacControl( peer
)
1045 wxMacTextControl::~wxMacTextControl()
1049 void wxMacTextControl::SetStyle(long WXUNUSED(start
),
1051 const wxTextAttr
& WXUNUSED(style
))
1055 void wxMacTextControl::Copy()
1059 void wxMacTextControl::Cut()
1063 void wxMacTextControl::Paste()
1067 bool wxMacTextControl::CanPaste() const
1072 void wxMacTextControl::SetEditable(bool WXUNUSED(editable
))
1076 wxTextPos
wxMacTextControl::GetLastPosition() const
1078 return GetStringValue().length() ;
1081 void wxMacTextControl::Replace( long from
, long to
, const wxString
&val
)
1083 SetSelection( from
, to
) ;
1087 void wxMacTextControl::Remove( long from
, long to
)
1089 SetSelection( from
, to
) ;
1090 WriteText( wxEmptyString
) ;
1093 void wxMacTextControl::Clear()
1095 SetStringValue( wxEmptyString
) ;
1098 bool wxMacTextControl::CanUndo() const
1103 void wxMacTextControl::Undo()
1107 bool wxMacTextControl::CanRedo() const
1112 void wxMacTextControl::Redo()
1116 long wxMacTextControl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
1121 bool wxMacTextControl::PositionToXY(long WXUNUSED(pos
),
1123 long *WXUNUSED(y
)) const
1128 void wxMacTextControl::ShowPosition( long WXUNUSED(pos
) )
1132 int wxMacTextControl::GetNumberOfLines() const
1134 ItemCount lines
= 0 ;
1135 wxString content
= GetStringValue() ;
1138 for (size_t i
= 0; i
< content
.length() ; i
++)
1140 if (content
[i
] == '\r')
1147 wxString
wxMacTextControl::GetLineText(long lineNo
) const
1149 // TODO: change this if possible to reflect real lines
1150 wxString content
= GetStringValue() ;
1154 for (size_t i
= 0; i
< content
.length() ; i
++)
1156 if (count
== lineNo
)
1158 // Add chars in line then
1161 for (size_t j
= i
; j
< content
.length(); j
++)
1163 if (content
[j
] == '\n')
1172 if (content
[i
] == '\n')
1176 return wxEmptyString
;
1179 int wxMacTextControl::GetLineLength(long lineNo
) const
1181 // TODO: change this if possible to reflect real lines
1182 wxString content
= GetStringValue() ;
1186 for (size_t i
= 0; i
< content
.length() ; i
++)
1188 if (count
== lineNo
)
1190 // Count chars in line then
1192 for (size_t j
= i
; j
< content
.length(); j
++)
1195 if (content
[j
] == '\n')
1202 if (content
[i
] == '\n')
1209 // ----------------------------------------------------------------------------
1210 // standard unicode control implementation
1211 // ----------------------------------------------------------------------------
1213 // the current unicode textcontrol implementation has a bug : only if the control
1214 // is currently having the focus, the selection can be retrieved by the corresponding
1215 // data tag. So we have a mirroring using a member variable
1216 // TODO : build event table using virtual member functions for wxMacControl
1218 static const EventTypeSpec unicodeTextControlEventList
[] =
1220 { kEventClassControl
, kEventControlSetFocusPart
} ,
1223 static pascal OSStatus
wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1225 OSStatus result
= eventNotHandledErr
;
1226 wxMacUnicodeTextControl
* focus
= (wxMacUnicodeTextControl
*) data
;
1227 wxMacCarbonEvent
cEvent( event
) ;
1229 switch ( GetEventKind( event
) )
1231 case kEventControlSetFocusPart
:
1233 ControlPartCode controlPart
= cEvent
.GetParameter
<ControlPartCode
>(kEventParamControlPart
, typeControlPartCode
);
1234 if ( controlPart
== kControlFocusNoPart
)
1236 // about to loose focus -> store selection to field
1237 focus
->GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1239 result
= CallNextEventHandler(handler
,event
) ;
1240 if ( controlPart
!= kControlFocusNoPart
)
1242 // about to gain focus -> set selection from field
1243 focus
->SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1254 static pascal OSStatus
wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1256 OSStatus result
= eventNotHandledErr
;
1258 switch ( GetEventClass( event
) )
1260 case kEventClassControl
:
1261 result
= wxMacUnicodeTextControlControlEventHandler( handler
, event
, data
) ;
1270 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler
)
1272 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
) : wxMacTextControl( wxPeer
)
1276 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
,
1277 const wxString
& str
,
1279 const wxSize
& size
, long style
)
1280 : wxMacTextControl( wxPeer
)
1282 Create( wxPeer
, str
, pos
, size
, style
);
1285 bool wxMacUnicodeTextControl::Create( wxTextCtrl
*wxPeer
,
1286 const wxString
& str
,
1288 const wxSize
& size
, long style
)
1290 m_font
= wxPeer
->GetFont() ;
1291 m_windowStyle
= style
;
1292 m_selection
.selStart
= m_selection
.selEnd
= 0;
1293 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
1295 wxMacConvertNewlines10To13( &st
) ;
1296 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding()) ;
1297 CFStringRef cfr
= cf
;
1299 m_valueTag
= kControlEditTextCFStringTag
;
1300 CreateControl( wxPeer
, &bounds
, cfr
);
1302 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1303 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextSingleLineTag
, true ) ;
1305 InstallControlEventHandler( m_controlRef
, GetwxMacUnicodeTextControlEventHandlerUPP(),
1306 GetEventTypeCount(unicodeTextControlEventList
), unicodeTextControlEventList
, this,
1312 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1316 void wxMacUnicodeTextControl::VisibilityChanged(bool shown
)
1318 if ( !(m_windowStyle
& wxTE_MULTILINE
) && shown
)
1320 // work around a refresh issue insofar as not always the entire content is shown,
1321 // even if this would be possible
1322 ControlEditTextSelectionRec sel
;
1323 CFStringRef value
= NULL
;
1325 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1326 verify_noerr( GetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1327 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1328 verify_noerr( SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1330 CFRelease( value
) ;
1334 wxString
wxMacUnicodeTextControl::GetStringValue() const
1337 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1340 wxMacCFStringHolder
cf(value
) ;
1341 result
= cf
.AsString() ;
1345 wxMacConvertNewlines13To10( &result
) ;
1347 wxMacConvertNewlines10To13( &result
) ;
1353 void wxMacUnicodeTextControl::SetStringValue( const wxString
&str
)
1356 wxMacConvertNewlines10To13( &st
) ;
1357 wxMacCFStringHolder
cf( st
, m_font
.GetEncoding() ) ;
1358 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, cf
) ) ;
1361 void wxMacUnicodeTextControl::CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef cfr
)
1363 Boolean isPassword
= ( m_windowStyle
& wxTE_PASSWORD
) != 0 ;
1366 m_valueTag
= kControlEditTextPasswordCFStringTag
;
1368 OSStatus err
= CreateEditUnicodeTextControl(
1369 MAC_WXHWND(peer
->MacGetTopLevelWindowRef()), bounds
, cfr
,
1370 isPassword
, NULL
, &m_controlRef
) ;
1371 verify_noerr( err
);
1374 void wxMacUnicodeTextControl::Copy()
1376 SendHICommand( kHICommandCopy
) ;
1379 void wxMacUnicodeTextControl::Cut()
1381 SendHICommand( kHICommandCut
) ;
1384 void wxMacUnicodeTextControl::Paste()
1386 SendHICommand( kHICommandPaste
) ;
1389 bool wxMacUnicodeTextControl::CanPaste() const
1394 void wxMacUnicodeTextControl::SetEditable(bool WXUNUSED(editable
))
1396 #if 0 // leads to problem because text cannot be selected anymore
1397 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextLockedTag
, (Boolean
) !editable
) ;
1401 void wxMacUnicodeTextControl::GetSelection( long* from
, long* to
) const
1403 ControlEditTextSelectionRec sel
;
1405 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ) ;
1410 *from
= sel
.selStart
;
1415 void wxMacUnicodeTextControl::SetSelection( long from
, long to
)
1417 ControlEditTextSelectionRec sel
;
1419 int textLength
= 0 ;
1420 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1423 wxMacCFStringHolder
cf(value
) ;
1424 textLength
= cf
.AsString().length() ;
1427 if ((from
== -1) && (to
== -1))
1434 from
= wxMin(textLength
,wxMax(from
,0)) ;
1438 to
= wxMax(0,wxMin(textLength
,to
)) ;
1441 sel
.selStart
= from
;
1444 SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ;
1449 void wxMacUnicodeTextControl::WriteText( const wxString
& str
)
1452 wxMacConvertNewlines10To13( &st
) ;
1456 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding() ) ;
1457 CFStringRef value
= cf
;
1458 SetData
<CFStringRef
>( 0, kControlEditTextInsertCFStringRefTag
, &value
);
1462 wxString val
= GetStringValue() ;
1464 GetSelection( &start
, &end
) ;
1465 val
.Remove( start
, end
- start
) ;
1466 val
.insert( start
, str
) ;
1467 SetStringValue( val
) ;
1468 SetSelection( start
+ str
.length() , start
+ str
.length() ) ;
1472 // ----------------------------------------------------------------------------
1473 // MLTE control implementation (common part)
1474 // ----------------------------------------------------------------------------
1476 // if MTLE is read only, no changes at all are allowed, not even from
1477 // procedural API, in order to allow changes via API all the same we must undo
1478 // the readonly status while we are executing, this class helps to do so
1480 class wxMacEditHelper
1483 wxMacEditHelper( TXNObject txn
)
1485 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1487 TXNGetTXNObjectControls( m_txn
, 1 , tag
, m_data
) ;
1488 if ( m_data
[0].uValue
== kTXNReadOnly
)
1490 TXNControlData data
[] = { { kTXNReadWrite
} } ;
1491 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, data
) ;
1497 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1498 if ( m_data
[0].uValue
== kTXNReadOnly
)
1499 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, m_data
) ;
1504 TXNControlData m_data
[1] ;
1507 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl
*peer
)
1508 : wxMacTextControl( peer
)
1510 SetNeedsFocusRect( true ) ;
1513 wxString
wxMacMLTEControl::GetStringValue() const
1517 Size actualSize
= 0;
1522 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNUnicodeTextData
);
1531 actualSize
= GetHandleSize( theText
) / sizeof(UniChar
) ;
1532 if ( actualSize
> 0 )
1534 wxChar
*ptr
= NULL
;
1536 #if SIZEOF_WCHAR_T == 2
1537 ptr
= new wxChar
[actualSize
+ 1] ;
1538 wxStrncpy( ptr
, (wxChar
*)(*theText
) , actualSize
) ;
1540 SetHandleSize( theText
, (actualSize
+ 1) * sizeof(UniChar
) ) ;
1542 (((UniChar
*)*theText
)[actualSize
]) = 0 ;
1543 wxMBConvUTF16 converter
;
1544 size_t noChars
= converter
.MB2WC( NULL
, (const char*)*theText
, 0 ) ;
1545 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Unable to count the number of characters in this string!") );
1546 ptr
= new wxChar
[noChars
+ 1] ;
1548 noChars
= converter
.MB2WC( ptr
, (const char*)*theText
, noChars
+ 1 ) ;
1549 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Conversion of string failed!") );
1551 HUnlock( theText
) ;
1554 ptr
[actualSize
] = 0 ;
1555 result
= wxString( ptr
) ;
1559 DisposeHandle( theText
) ;
1563 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1572 actualSize
= GetHandleSize( theText
) ;
1573 if ( actualSize
> 0 )
1576 result
= wxString( *theText
, wxConvLocal
, actualSize
) ;
1577 HUnlock( theText
) ;
1580 DisposeHandle( theText
) ;
1586 wxMacConvertNewlines13To10( &result
) ;
1588 wxMacConvertNewlines10To13( &result
) ;
1594 void wxMacMLTEControl::SetStringValue( const wxString
&str
)
1597 wxMacConvertNewlines10To13( &st
);
1601 wxMacWindowClipper
c( m_peer
) ;
1605 wxMacEditHelper
help( m_txn
);
1606 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
);
1609 TXNSetSelection( m_txn
, 0, 0 );
1610 TXNShowSelection( m_txn
, kTXNShowStart
);
1614 TXNFrameOptions
wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle
)
1616 TXNFrameOptions frameOptions
= kTXNDontDrawCaretWhenInactiveMask
;
1618 frameOptions
|= kTXNDoFontSubstitutionMask
;
1620 if ( ! (wxStyle
& wxTE_NOHIDESEL
) )
1621 frameOptions
|= kTXNDontDrawSelectionWhenInactiveMask
;
1623 if ( wxStyle
& (wxHSCROLL
| wxTE_DONTWRAP
) )
1624 frameOptions
|= kTXNWantHScrollBarMask
;
1626 if ( wxStyle
& wxTE_MULTILINE
)
1628 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
1630 if ( !(wxStyle
& wxTE_NO_VSCROLL
) )
1632 frameOptions
|= kTXNWantVScrollBarMask
;
1634 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1635 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1636 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1637 // if ( frameOptions & kTXNWantHScrollBarMask )
1638 // frameOptions |= kTXNDrawGrowIconMask ;
1643 frameOptions
|= kTXNSingleLineOnlyMask
;
1646 return frameOptions
;
1649 void wxMacMLTEControl::AdjustCreationAttributes(const wxColour
&background
,
1650 bool WXUNUSED(visible
))
1652 TXNControlTag iControlTags
[] =
1654 kTXNDoFontSubstitution
,
1655 kTXNWordWrapStateTag
,
1657 TXNControlData iControlData
[] =
1663 int toptag
= WXSIZEOF( iControlTags
) ;
1665 if ( m_windowStyle
& wxTE_MULTILINE
)
1667 iControlData
[1].uValue
=
1668 (m_windowStyle
& wxTE_DONTWRAP
)
1673 OSStatus err
= TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) ;
1674 verify_noerr( err
);
1676 // setting the default font:
1677 // under 10.2 this causes a visible caret, therefore we avoid it
1683 GetThemeFont( kThemeSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
1685 TXNTypeAttributes typeAttr
[] =
1687 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
1688 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
1689 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
1692 err
= TXNSetTypeAttributes(
1693 m_txn
, sizeof(typeAttr
) / sizeof(TXNTypeAttributes
),
1694 typeAttr
, kTXNStartOffset
, kTXNEndOffset
);
1695 verify_noerr( err
);
1697 if ( m_windowStyle
& wxTE_PASSWORD
)
1699 UniChar c
= 0x00A5 ;
1700 err
= TXNEchoMode( m_txn
, c
, 0 , true );
1701 verify_noerr( err
);
1704 TXNBackground tback
;
1705 tback
.bgType
= kTXNBackgroundTypeRGB
;
1706 background
.GetRGBColor( &tback
.bg
.color
);
1707 TXNSetBackground( m_txn
, &tback
);
1710 TXNCommandEventSupportOptions options
;
1711 if ( TXNGetCommandEventSupport( m_txn
, &options
) == noErr
)
1714 kTXNSupportEditCommandProcessing
1715 | kTXNSupportEditCommandUpdating
1716 | kTXNSupportFontCommandProcessing
1717 | kTXNSupportFontCommandUpdating
;
1719 // only spell check when not read-only
1720 // use system options for the default
1721 bool checkSpelling
= false ;
1722 if ( !(m_windowStyle
& wxTE_READONLY
) )
1724 #if wxUSE_SYSTEM_OPTIONS
1725 if ( wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) == 1) )
1727 checkSpelling
= true ;
1732 if ( checkSpelling
)
1734 kTXNSupportSpellCheckCommandProcessing
1735 | kTXNSupportSpellCheckCommandUpdating
;
1737 TXNSetCommandEventSupport( m_txn
, options
) ;
1741 void wxMacMLTEControl::SetBackgroundColour(const wxColour
& col
)
1743 TXNBackground tback
;
1744 tback
.bgType
= kTXNBackgroundTypeRGB
;
1745 col
.GetRGBColor(&tback
.bg
.color
);
1746 TXNSetBackground( m_txn
, &tback
);
1749 static inline int wxConvertToTXN(int x
)
1751 return wx_static_cast(int, x
/ 254.0 * 72 + 0.5);
1754 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
)
1756 TXNTypeAttributes typeAttr
[4] ;
1758 size_t typeAttrCount
= 0 ;
1761 TXNControlTag controlTags
[4];
1762 TXNControlData controlData
[4];
1763 size_t controlAttrCount
= 0;
1765 TXNTab
* tabs
= NULL
;
1767 bool relayout
= false;
1769 if ( style
.HasFont() )
1771 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1772 const wxFont
&font
= style
.GetFont() ;
1773 typeAttr
[typeAttrCount
].tag
= kTXNATSUIStyle
;
1774 typeAttr
[typeAttrCount
].size
= kTXNATSUIStyleSize
;
1775 typeAttr
[typeAttrCount
].data
.dataPtr
= font
.MacGetATSUStyle() ;
1779 if ( style
.HasTextColour() )
1781 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1782 style
.GetTextColour().GetRGBColor( &color
);
1783 typeAttr
[typeAttrCount
].tag
= kTXNQDFontColorAttribute
;
1784 typeAttr
[typeAttrCount
].size
= kTXNQDFontColorAttributeSize
;
1785 typeAttr
[typeAttrCount
].data
.dataPtr
= (void*) &color
;
1789 if ( style
.HasAlignment() )
1791 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1794 switch ( style
.GetAlignment() )
1796 case wxTEXT_ALIGNMENT_LEFT
:
1797 align
= kTXNFlushLeft
;
1799 case wxTEXT_ALIGNMENT_CENTRE
:
1802 case wxTEXT_ALIGNMENT_RIGHT
:
1803 align
= kTXNFlushRight
;
1805 case wxTEXT_ALIGNMENT_JUSTIFIED
:
1806 align
= kTXNFullJust
;
1809 case wxTEXT_ALIGNMENT_DEFAULT
:
1810 align
= kTXNFlushDefault
;
1814 controlTags
[controlAttrCount
] = kTXNJustificationTag
;
1815 controlData
[controlAttrCount
].sValue
= align
;
1816 controlAttrCount
++ ;
1819 if ( style
.HasLeftIndent() || style
.HasRightIndent() )
1821 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1822 controlTags
[controlAttrCount
] = kTXNMarginsTag
;
1823 controlData
[controlAttrCount
].marginsPtr
= &margins
;
1824 verify_noerr( TXNGetTXNObjectControls (m_txn
, 1 ,
1825 &controlTags
[controlAttrCount
], &controlData
[controlAttrCount
]) );
1826 if ( style
.HasLeftIndent() )
1828 margins
.leftMargin
= wxConvertToTXN(style
.GetLeftIndent());
1830 if ( style
.HasRightIndent() )
1832 margins
.rightMargin
= wxConvertToTXN(style
.GetRightIndent());
1834 controlAttrCount
++ ;
1837 if ( style
.HasTabs() )
1839 const wxArrayInt
& tabarray
= style
.GetTabs();
1840 // unfortunately Mac only applies a tab distance, not individually different tabs
1841 controlTags
[controlAttrCount
] = kTXNTabSettingsTag
;
1842 if ( tabarray
.size() > 0 )
1843 controlData
[controlAttrCount
].tabValue
.value
= wxConvertToTXN(tabarray
[0]);
1845 controlData
[controlAttrCount
].tabValue
.value
= 72 ;
1847 controlData
[controlAttrCount
].tabValue
.tabType
= kTXNLeftTab
;
1848 controlAttrCount
++ ;
1851 // unfortunately the relayout is not automatic
1852 if ( controlAttrCount
> 0 )
1854 verify_noerr( TXNSetTXNObjectControls (m_txn
, false /* don't clear all */, controlAttrCount
,
1855 controlTags
, controlData
) );
1859 if ( typeAttrCount
> 0 )
1861 verify_noerr( TXNSetTypeAttributes( m_txn
, typeAttrCount
, typeAttr
, from
, to
) );
1872 TXNRecalcTextLayout( m_txn
);
1876 void wxMacMLTEControl::SetFont(const wxFont
& font
,
1877 const wxColour
& foreground
,
1878 long WXUNUSED(windowStyle
))
1880 wxMacEditHelper
help( m_txn
) ;
1881 TXNSetAttribute( wxTextAttr( foreground
, wxNullColour
, font
), kTXNStartOffset
, kTXNEndOffset
) ;
1884 void wxMacMLTEControl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1886 wxMacEditHelper
help( m_txn
) ;
1887 TXNSetAttribute( style
, start
, end
) ;
1890 void wxMacMLTEControl::Copy()
1895 void wxMacMLTEControl::Cut()
1900 void wxMacMLTEControl::Paste()
1905 bool wxMacMLTEControl::CanPaste() const
1907 return TXNIsScrapPastable() ;
1910 void wxMacMLTEControl::SetEditable(bool editable
)
1912 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1913 TXNControlData data
[] = { { editable
? kTXNReadWrite
: kTXNReadOnly
} } ;
1914 TXNSetTXNObjectControls( m_txn
, false, WXSIZEOF(tag
), tag
, data
) ;
1917 wxTextPos
wxMacMLTEControl::GetLastPosition() const
1919 wxTextPos actualsize
= 0 ;
1922 OSErr err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1927 actualsize
= GetHandleSize( theText
) ;
1928 DisposeHandle( theText
) ;
1938 void wxMacMLTEControl::Replace( long from
, long to
, const wxString
&str
)
1940 wxString value
= str
;
1941 wxMacConvertNewlines10To13( &value
) ;
1943 wxMacEditHelper
help( m_txn
) ;
1945 wxMacWindowClipper
c( m_peer
) ;
1948 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
) ;
1950 SetTXNData( value
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1953 void wxMacMLTEControl::Remove( long from
, long to
)
1956 wxMacWindowClipper
c( m_peer
) ;
1958 wxMacEditHelper
help( m_txn
) ;
1959 TXNSetSelection( m_txn
, from
, to
) ;
1963 void wxMacMLTEControl::GetSelection( long* from
, long* to
) const
1966 TXNGetSelection( m_txn
, &f
, &t
) ;
1971 void wxMacMLTEControl::SetSelection( long from
, long to
)
1974 wxMacWindowClipper
c( m_peer
) ;
1977 // change the selection
1978 if ((from
== -1) && (to
== -1))
1979 TXNSelectAll( m_txn
);
1981 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
);
1983 TXNShowSelection( m_txn
, kTXNShowStart
);
1986 void wxMacMLTEControl::WriteText( const wxString
& str
)
1989 wxMacConvertNewlines10To13( &st
) ;
1991 long start
, end
, dummy
;
1993 GetSelection( &start
, &dummy
) ;
1995 wxMacWindowClipper
c( m_peer
) ;
1999 wxMacEditHelper
helper( m_txn
) ;
2000 SetTXNData( st
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
2003 GetSelection( &dummy
, &end
) ;
2005 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2008 void wxMacMLTEControl::Clear()
2011 wxMacWindowClipper
c( m_peer
) ;
2013 wxMacEditHelper
st( m_txn
) ;
2014 TXNSetSelection( m_txn
, kTXNStartOffset
, kTXNEndOffset
) ;
2018 bool wxMacMLTEControl::CanUndo() const
2020 return TXNCanUndo( m_txn
, NULL
) ;
2023 void wxMacMLTEControl::Undo()
2028 bool wxMacMLTEControl::CanRedo() const
2030 return TXNCanRedo( m_txn
, NULL
) ;
2033 void wxMacMLTEControl::Redo()
2038 int wxMacMLTEControl::GetNumberOfLines() const
2040 ItemCount lines
= 0 ;
2041 TXNGetLineCount( m_txn
, &lines
) ;
2046 long wxMacMLTEControl::XYToPosition(long x
, long y
) const
2051 // TODO: find a better implementation : while we can get the
2052 // line metrics of a certain line, we don't get its starting
2053 // position, so it would probably be rather a binary search
2054 // for the start position
2055 long xpos
= 0, ypos
= 0 ;
2056 int lastHeight
= 0 ;
2059 lastpos
= GetLastPosition() ;
2060 for ( n
= 0 ; n
<= (ItemCount
) lastpos
; ++n
)
2062 if ( y
== ypos
&& x
== xpos
)
2065 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2067 if ( curpt
.v
> lastHeight
)
2073 lastHeight
= curpt
.v
;
2082 bool wxMacMLTEControl::PositionToXY( long pos
, long *x
, long *y
) const
2092 lastpos
= GetLastPosition() ;
2093 if ( pos
<= lastpos
)
2095 // TODO: find a better implementation - while we can get the
2096 // line metrics of a certain line, we don't get its starting
2097 // position, so it would probably be rather a binary search
2098 // for the start position
2099 long xpos
= 0, ypos
= 0 ;
2100 int lastHeight
= 0 ;
2103 for ( n
= 0 ; n
<= (ItemCount
) pos
; ++n
)
2105 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2107 if ( curpt
.v
> lastHeight
)
2113 lastHeight
= curpt
.v
;
2128 void wxMacMLTEControl::ShowPosition( long pos
)
2130 Point current
, desired
;
2131 TXNOffset selstart
, selend
;
2133 TXNGetSelection( m_txn
, &selstart
, &selend
);
2134 TXNOffsetToPoint( m_txn
, selstart
, ¤t
);
2135 TXNOffsetToPoint( m_txn
, pos
, &desired
);
2137 // TODO: use HIPoints for 10.3 and above
2139 OSErr theErr
= noErr
;
2140 long dv
= desired
.v
- current
.v
;
2141 long dh
= desired
.h
- current
.h
;
2142 TXNShowSelection( m_txn
, kTXNShowStart
) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2143 theErr
= TXNScroll( m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
, &dv
, &dh
);
2145 // there will be an error returned for classic MLTE implementation when the control is
2146 // invisible, but HITextView works correctly, so we don't assert that one
2147 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2150 void wxMacMLTEControl::SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
)
2153 #if SIZEOF_WCHAR_T == 2
2154 size_t len
= st
.length() ;
2155 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)st
.wc_str(), len
* 2, start
, end
);
2157 wxMBConvUTF16 converter
;
2158 ByteCount byteBufferLen
= converter
.WC2MB( NULL
, st
.wc_str(), 0 ) ;
2159 UniChar
*unibuf
= (UniChar
*)malloc( byteBufferLen
) ;
2160 converter
.WC2MB( (char*)unibuf
, st
.wc_str(), byteBufferLen
) ;
2161 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)unibuf
, byteBufferLen
, start
, end
) ;
2165 wxCharBuffer text
= st
.mb_str( wxConvLocal
) ;
2166 TXNSetData( m_txn
, kTXNTextData
, (void*)text
.data(), strlen( text
), start
, end
) ;
2170 wxString
wxMacMLTEControl::GetLineText(long lineNo
) const
2174 if ( lineNo
< GetNumberOfLines() )
2177 Fixed lineWidth
, lineHeight
, currentHeight
;
2180 // get the first possible position in the control
2181 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2183 // Iterate through the lines until we reach the one we want,
2184 // adding to our current y pixel point position
2187 while (ypos
< lineNo
)
2189 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2190 currentHeight
+= lineHeight
;
2193 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2194 TXNOffset theOffset
;
2195 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2197 wxString content
= GetStringValue() ;
2198 Point currentPoint
= thePoint
;
2199 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2201 line
+= content
[theOffset
];
2202 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2209 int wxMacMLTEControl::GetLineLength(long lineNo
) const
2213 if ( lineNo
< GetNumberOfLines() )
2216 Fixed lineWidth
, lineHeight
, currentHeight
;
2219 // get the first possible position in the control
2220 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2222 // Iterate through the lines until we reach the one we want,
2223 // adding to our current y pixel point position
2226 while (ypos
< lineNo
)
2228 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2229 currentHeight
+= lineHeight
;
2232 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2233 TXNOffset theOffset
;
2234 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2236 wxString content
= GetStringValue() ;
2237 Point currentPoint
= thePoint
;
2238 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2241 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2248 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
2250 // ----------------------------------------------------------------------------
2251 // MLTE control implementation (classic part)
2252 // ----------------------------------------------------------------------------
2254 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2255 // has to live on. We have different problems coming from outdated implementations on the
2256 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2257 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2258 // no way out, therefore we are using our own implementation and our own scrollbars ....
2260 wxMacWindowClipper::wxMacWindowClipper( const wxWindow
* win
) :
2261 wxMacPortSaver( (GrafPtr
) GetWindowPort( (WindowRef
) win
->MacGetTopLevelWindowRef() ) )
2263 m_newPort
= (GrafPtr
) GetWindowPort( (WindowRef
) win
->MacGetTopLevelWindowRef() ) ;
2264 m_formerClip
= NewRgn() ;
2265 m_newClip
= NewRgn() ;
2266 GetClip( m_formerClip
) ;
2270 // guard against half constructed objects, this just leads to a empty clip
2271 if ( win
->GetPeer() )
2274 win
->MacWindowToRootWindow( &x
, &y
) ;
2276 // get area including focus rect
2277 HIShapeGetAsQDRgn( ((wxWindow
*)win
)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip
);
2278 if ( !EmptyRgn( m_newClip
) )
2279 OffsetRgn( m_newClip
, x
, y
) ;
2282 SetClip( m_newClip
) ;
2286 wxMacWindowClipper::~wxMacWindowClipper()
2288 SetPort( m_newPort
) ;
2289 SetClip( m_formerClip
) ;
2290 DisposeRgn( m_newClip
) ;
2291 DisposeRgn( m_formerClip
) ;
2294 TXNScrollInfoUPP gTXNScrollInfoProc
= NULL
;
2295 ControlActionUPP gTXNScrollActionProc
= NULL
;
2297 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2298 SInt32 iValue
, SInt32 iMaximumValue
,
2299 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
)
2301 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) iRefCon
;
2302 SInt32 value
= wxMax( iValue
, 0 ) ;
2303 SInt32 maximum
= wxMax( iMaximumValue
, 0 ) ;
2305 if ( iScrollBarOrientation
== kTXNHorizontal
)
2307 if ( mlte
->m_sbHorizontal
)
2309 SetControl32BitValue( mlte
->m_sbHorizontal
, value
) ;
2310 SetControl32BitMaximum( mlte
->m_sbHorizontal
, maximum
) ;
2311 mlte
->m_lastHorizontalValue
= value
;
2314 else if ( iScrollBarOrientation
== kTXNVertical
)
2316 if ( mlte
->m_sbVertical
)
2318 SetControl32BitValue( mlte
->m_sbVertical
, value
) ;
2319 SetControl32BitMaximum( mlte
->m_sbVertical
, maximum
) ;
2320 mlte
->m_lastVerticalValue
= value
;
2325 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
)
2327 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) GetControlReference( controlRef
) ;
2331 if ( controlRef
!= mlte
->m_sbVertical
&& controlRef
!= mlte
->m_sbHorizontal
)
2335 bool isHorizontal
= ( controlRef
== mlte
->m_sbHorizontal
) ;
2337 SInt32 minimum
= 0 ;
2338 SInt32 maximum
= GetControl32BitMaximum( controlRef
) ;
2339 SInt32 value
= GetControl32BitValue( controlRef
) ;
2344 case kControlDownButtonPart
:
2348 case kControlUpButtonPart
:
2352 case kControlPageDownPart
:
2353 delta
= GetControlViewSize( controlRef
) ;
2356 case kControlPageUpPart
:
2357 delta
= -GetControlViewSize( controlRef
) ;
2360 case kControlIndicatorPart
:
2361 delta
= value
- (isHorizontal
? mlte
->m_lastHorizontalValue
: mlte
->m_lastVerticalValue
) ;
2370 SInt32 newValue
= value
;
2372 if ( partCode
!= kControlIndicatorPart
)
2374 if ( value
+ delta
< minimum
)
2375 delta
= minimum
- value
;
2376 if ( value
+ delta
> maximum
)
2377 delta
= maximum
- value
;
2379 SetControl32BitValue( controlRef
, value
+ delta
) ;
2380 newValue
= value
+ delta
;
2383 SInt32 verticalDelta
= isHorizontal
? 0 : delta
;
2384 SInt32 horizontalDelta
= isHorizontal
? delta
: 0 ;
2387 mlte
->m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
,
2388 &verticalDelta
, &horizontalDelta
);
2389 verify_noerr( err
);
2392 mlte
->m_lastHorizontalValue
= newValue
;
2394 mlte
->m_lastVerticalValue
= newValue
;
2398 // make correct activations
2399 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive
)
2401 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference(m_controlRef
);
2403 wxMacWindowClipper
clipper( textctrl
) ;
2404 TXNActivate( m_txn
, m_txnFrameID
, setActive
);
2406 ControlRef controlFocus
= 0 ;
2407 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2408 if ( controlFocus
== m_controlRef
)
2409 TXNFocus( m_txn
, setActive
);
2412 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus
)
2414 TXNFocus( m_txn
, setFocus
);
2417 // guards against inappropriate redraw (hidden objects drawing onto window)
2419 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis
)
2421 ControlRef controlFocus
= 0 ;
2422 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2424 if ( !vis
&& (controlFocus
== m_controlRef
) )
2425 SetKeyboardFocus( m_txnWindow
, m_controlRef
, kControlFocusNoPart
) ;
2427 TXNControlTag iControlTags
[1] = { kTXNVisibilityTag
};
2428 TXNControlData iControlData
[1] = { { (UInt32
)false } };
2430 verify_noerr( TXNGetTXNObjectControls( m_txn
, 1, iControlTags
, iControlData
) ) ;
2432 if ( iControlData
[0].uValue
!= vis
)
2434 iControlData
[0].uValue
= vis
;
2435 verify_noerr( TXNSetTXNObjectControls( m_txn
, false , 1, iControlTags
, iControlData
) ) ;
2438 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2439 // if we run into further problems we might set the FrameBounds to an empty rect here
2442 // make sure that the TXNObject is at the right position
2444 void wxMacMLTEClassicControl::MacUpdatePosition()
2446 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2447 if ( textctrl
== NULL
)
2451 GetRectInWindowCoords( &bounds
);
2453 wxRect visRect
= textctrl
->MacGetClippedClientRect() ;
2454 Rect visBounds
= { visRect
.y
, visRect
.x
, visRect
.y
+ visRect
.height
, visRect
.x
+ visRect
.width
} ;
2457 textctrl
->MacWindowToRootWindow( &x
, &y
) ;
2458 OffsetRect( &visBounds
, x
, y
) ;
2460 if ( !EqualRect( &bounds
, &m_txnControlBounds
) || !EqualRect( &visBounds
, &m_txnVisBounds
) )
2462 m_txnControlBounds
= bounds
;
2463 m_txnVisBounds
= visBounds
;
2464 wxMacWindowClipper
cl( textctrl
) ;
2466 if ( m_sbHorizontal
|| m_sbVertical
)
2468 int w
= bounds
.right
- bounds
.left
;
2469 int h
= bounds
.bottom
- bounds
.top
;
2471 if ( m_sbHorizontal
)
2475 sbBounds
.left
= -1 ;
2476 sbBounds
.top
= h
- 14 ;
2477 sbBounds
.right
= w
+ 1 ;
2478 sbBounds
.bottom
= h
+ 1 ;
2480 SetControlBounds( m_sbHorizontal
, &sbBounds
) ;
2481 SetControlViewSize( m_sbHorizontal
, w
) ;
2488 sbBounds
.left
= w
- 14 ;
2490 sbBounds
.right
= w
+ 1 ;
2491 sbBounds
.bottom
= m_sbHorizontal
? h
- 14 : h
+ 1 ;
2493 SetControlBounds( m_sbVertical
, &sbBounds
) ;
2494 SetControlViewSize( m_sbVertical
, h
) ;
2499 TXNLongRect olddestRect
;
2500 TXNGetRectBounds( m_txn
, &oldviewRect
, &olddestRect
, NULL
) ;
2502 Rect viewRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2503 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2504 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2505 TXNLongRect destRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2506 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2507 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2509 if ( olddestRect
.right
>= 10000 )
2510 destRect
.right
= destRect
.left
+ 32000 ;
2512 if ( olddestRect
.bottom
>= 0x20000000 )
2513 destRect
.bottom
= destRect
.top
+ 0x40000000 ;
2515 SectRect( &viewRect
, &visBounds
, &viewRect
) ;
2516 TXNSetRectBounds( m_txn
, &viewRect
, &destRect
, true ) ;
2521 m_txnControlBounds
.top
,
2522 m_txnControlBounds
.left
,
2523 m_txnControlBounds
.bottom
- (m_sbHorizontal
? 14 : 0),
2524 m_txnControlBounds
.right
- (m_sbVertical
? 14 : 0),
2528 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2529 // movement, therefore we have to force it
2531 // this problem has been reported in OSX as well, so we use this here once again
2533 TXNLongRect textRect
;
2534 TXNGetRectBounds( m_txn
, NULL
, NULL
, &textRect
) ;
2535 if ( textRect
.left
< m_txnControlBounds
.left
)
2536 TXNShowSelection( m_txn
, kTXNShowStart
) ;
2540 void wxMacMLTEClassicControl::SetRect( Rect
*r
)
2542 wxMacControl::SetRect( r
) ;
2543 MacUpdatePosition() ;
2546 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16
WXUNUSED(thePart
))
2548 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2549 if ( textctrl
== NULL
)
2552 if ( textctrl
->MacIsReallyShown() )
2554 wxMacWindowClipper
clipper( textctrl
) ;
2555 TXNDraw( m_txn
, NULL
) ;
2559 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
2561 Point where
= { y
, x
} ;
2562 ControlPartCode result
= kControlNoPart
;
2564 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2565 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2567 if (PtInRect( where
, &m_txnControlBounds
))
2569 result
= kControlEditTextPart
;
2573 // sometimes we get the coords also in control local coordinates, therefore test again
2575 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2579 if (PtInRect( where
, &m_txnControlBounds
))
2580 result
= kControlEditTextPart
;
2587 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x
, wxInt16 y
, void* WXUNUSED(actionProc
) )
2589 ControlPartCode result
= kControlNoPart
;
2591 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2592 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2594 Point startPt
= { y
, x
} ;
2596 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2598 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2602 switch (MacControlUserPaneHitTestProc( startPt
.h
, startPt
.v
))
2604 case kControlEditTextPart
:
2606 wxMacWindowClipper
clipper( textctrl
) ;
2609 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
2610 TXNClick( m_txn
, &rec
);
2622 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2624 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2625 if ( textctrl
== NULL
)
2628 if (textctrl
->MacIsReallyShown())
2630 if (IsControlActive(m_controlRef
))
2634 wxMacWindowClipper
clipper( textctrl
) ;
2639 if (PtInRect(mousep
, &m_txnControlBounds
))
2641 RgnHandle theRgn
= NewRgn();
2642 RectRgn(theRgn
, &m_txnControlBounds
);
2643 TXNAdjustCursor(m_txn
, theRgn
);
2650 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
2652 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2653 if ( textctrl
== NULL
)
2654 return kControlNoPart
;
2656 wxMacWindowClipper
clipper( textctrl
) ;
2659 memset( &ev
, 0 , sizeof( ev
) ) ;
2661 ev
.modifiers
= modifiers
;
2662 ev
.message
= ((keyCode
<< 8) & keyCodeMask
) | (charCode
& charCodeMask
);
2663 TXNKeyDown( m_txn
, &ev
);
2665 return kControlEntireControl
;
2668 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating
)
2670 MacActivatePaneText( activating
);
2673 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action
)
2675 ControlPartCode focusResult
= kControlFocusNoPart
;
2677 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2678 if ( textctrl
== NULL
)
2681 wxMacWindowClipper
clipper( textctrl
) ;
2683 ControlRef controlFocus
= NULL
;
2684 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2685 bool wasFocused
= ( controlFocus
== m_controlRef
) ;
2689 case kControlFocusPrevPart
:
2690 case kControlFocusNextPart
:
2691 MacFocusPaneText( !wasFocused
);
2692 focusResult
= (!wasFocused
? (ControlPartCode
) kControlEditTextPart
: (ControlPartCode
) kControlFocusNoPart
);
2695 case kControlFocusNoPart
:
2697 MacFocusPaneText( false );
2698 focusResult
= kControlFocusNoPart
;
2705 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info
) )
2709 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
2710 const wxString
& str
,
2712 const wxSize
& size
, long style
)
2713 : wxMacMLTEControl( wxPeer
)
2715 m_font
= wxPeer
->GetFont() ;
2716 m_windowStyle
= style
;
2717 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
2720 kControlSupportsEmbedding
| kControlSupportsFocus
| kControlWantsIdle
2721 | kControlWantsActivate
| kControlHandlesTracking
2722 // | kControlHasSpecialBackground
2723 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
2725 OSStatus err
= ::CreateUserPaneControl(
2726 MAC_WXHWND(wxPeer
->GetParent()->MacGetTopLevelWindowRef()),
2727 &bounds
, featureSet
, &m_controlRef
);
2728 verify_noerr( err
);
2732 AdjustCreationAttributes( *wxWHITE
, true ) ;
2734 MacSetObjectVisibility( wxPeer
->MacIsReallyShown() ) ;
2738 wxMacConvertNewlines10To13( &st
) ;
2739 wxMacWindowClipper
clipper( m_peer
) ;
2740 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
2741 TXNSetSelection( m_txn
, 0, 0 ) ;
2745 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2747 TXNDeleteObject( m_txn
);
2751 void wxMacMLTEClassicControl::VisibilityChanged(bool shown
)
2753 MacSetObjectVisibility( shown
) ;
2754 wxMacControl::VisibilityChanged( shown
) ;
2757 void wxMacMLTEClassicControl::SuperChangedPosition()
2759 MacUpdatePosition() ;
2760 wxMacControl::SuperChangedPosition() ;
2763 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
2764 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
2765 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
2766 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
2767 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
2768 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
2769 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
2771 static pascal void wxMacControlUserPaneDrawProc(ControlRef control
, SInt16 part
)
2773 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2774 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2776 win
->MacControlUserPaneDrawProc( part
) ;
2779 static pascal ControlPartCode
wxMacControlUserPaneHitTestProc(ControlRef control
, Point where
)
2781 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2782 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2784 return win
->MacControlUserPaneHitTestProc( where
.h
, where
.v
) ;
2786 return kControlNoPart
;
2789 static pascal ControlPartCode
wxMacControlUserPaneTrackingProc(ControlRef control
, Point startPt
, ControlActionUPP actionProc
)
2791 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2792 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2794 return win
->MacControlUserPaneTrackingProc( startPt
.h
, startPt
.v
, (void*) actionProc
) ;
2796 return kControlNoPart
;
2799 static pascal void wxMacControlUserPaneIdleProc(ControlRef control
)
2801 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2802 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2804 win
->MacControlUserPaneIdleProc() ;
2807 static pascal ControlPartCode
wxMacControlUserPaneKeyDownProc(ControlRef control
, SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
)
2809 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2810 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2812 return win
->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
2814 return kControlNoPart
;
2817 static pascal void wxMacControlUserPaneActivateProc(ControlRef control
, Boolean activating
)
2819 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2820 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2822 win
->MacControlUserPaneActivateProc( activating
) ;
2825 static pascal ControlPartCode
wxMacControlUserPaneFocusProc(ControlRef control
, ControlFocusPart action
)
2827 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2828 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2830 return win
->MacControlUserPaneFocusProc( action
) ;
2832 return kControlNoPart
;
2836 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control
, ControlBackgroundPtr info
)
2838 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2839 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2841 win
->MacControlUserPaneBackgroundProc(info
) ;
2845 // TXNRegisterScrollInfoProc
2847 OSStatus
wxMacMLTEClassicControl::DoCreate()
2850 OSStatus err
= noErr
;
2852 // set up our globals
2853 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc
);
2854 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc
);
2855 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc
);
2856 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc
);
2857 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc
);
2858 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc
);
2859 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc
);
2861 if (gTXNScrollInfoProc
== NULL
) gTXNScrollInfoProc
= NewTXNScrollInfoUPP(TXNScrollInfoProc
) ;
2862 if (gTXNScrollActionProc
== NULL
) gTXNScrollActionProc
= NewControlActionUPP(TXNScrollActionProc
) ;
2864 // set the initial settings for our private data
2866 m_txnWindow
= GetControlOwner(m_controlRef
);
2867 m_txnPort
= (GrafPtr
) GetWindowPort(m_txnWindow
);
2869 // set up the user pane procedures
2870 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
2871 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
2872 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
2873 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
2874 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
2875 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
2876 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
2878 // calculate the rectangles used by the control
2879 GetRectInWindowCoords( &bounds
);
2881 m_txnControlBounds
= bounds
;
2882 m_txnVisBounds
= bounds
;
2887 GetGWorld( &origPort
, &origDev
) ;
2888 SetPort( m_txnPort
);
2890 // create the new edit field
2891 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( m_windowStyle
);
2893 // the scrollbars are not correctly embedded but are inserted at the root:
2894 // this gives us problems as we have erratic redraws even over the structure area
2896 m_sbHorizontal
= 0 ;
2898 m_lastHorizontalValue
= 0 ;
2899 m_lastVerticalValue
= 0 ;
2901 Rect sb
= { 0 , 0 , 0 , 0 } ;
2902 if ( frameOptions
& kTXNWantVScrollBarMask
)
2904 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbVertical
);
2905 SetControlReference( m_sbVertical
, (SInt32
)this );
2906 SetControlAction( m_sbVertical
, gTXNScrollActionProc
);
2907 ShowControl( m_sbVertical
);
2908 EmbedControl( m_sbVertical
, m_controlRef
);
2909 frameOptions
&= ~kTXNWantVScrollBarMask
;
2912 if ( frameOptions
& kTXNWantHScrollBarMask
)
2914 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbHorizontal
);
2915 SetControlReference( m_sbHorizontal
, (SInt32
)this );
2916 SetControlAction( m_sbHorizontal
, gTXNScrollActionProc
);
2917 ShowControl( m_sbHorizontal
);
2918 EmbedControl( m_sbHorizontal
, m_controlRef
);
2919 frameOptions
&= ~(kTXNWantHScrollBarMask
| kTXNDrawGrowIconMask
);
2923 NULL
, m_txnWindow
, &bounds
, frameOptions
,
2924 kTXNTextEditStyleFrameType
, kTXNTextensionFile
, kTXNSystemDefaultEncoding
,
2925 &m_txn
, &m_txnFrameID
, NULL
);
2926 verify_noerr( err
);
2929 TXNControlTag iControlTags
[] = { kTXNUseCarbonEvents
};
2930 TXNControlData iControlData
[] = { { (UInt32
)&cInfo
} };
2931 int toptag
= WXSIZEOF( iControlTags
) ;
2932 TXNCarbonEventInfo cInfo
;
2933 cInfo
.useCarbonEvents
= false ;
2936 cInfo
.fDictionary
= NULL
;
2938 verify_noerr( TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) );
2941 TXNRegisterScrollInfoProc( m_txn
, gTXNScrollInfoProc
, (SInt32
)this );
2943 SetGWorld( origPort
, origDev
) ;
2949 // ----------------------------------------------------------------------------
2950 // MLTE control implementation (OSX part)
2951 // ----------------------------------------------------------------------------
2953 // tiger multi-line textcontrols with no CR in the entire content
2954 // don't scroll automatically, so we need a hack.
2955 // This attempt only works 'before' the key (ie before CallNextEventHandler)
2956 // is processed, thus the scrolling always occurs one character too late, but
2957 // better than nothing ...
2959 static const EventTypeSpec eventList
[] =
2961 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
2964 static pascal OSStatus
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2966 OSStatus result
= eventNotHandledErr
;
2967 wxMacMLTEHIViewControl
* focus
= (wxMacMLTEHIViewControl
*) data
;
2969 switch ( GetEventKind( event
) )
2971 case kEventTextInputUnicodeForKeyEvent
:
2973 TXNOffset from
, to
;
2974 TXNGetSelection( focus
->GetTXNObject() , &from
, &to
) ;
2976 TXNShowSelection( focus
->GetTXNObject() , kTXNShowStart
);
2977 result
= CallNextEventHandler(handler
,event
);
2987 static pascal OSStatus
wxMacTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2989 OSStatus result
= eventNotHandledErr
;
2991 switch ( GetEventClass( event
) )
2993 case kEventClassTextInput
:
2994 result
= wxMacUnicodeTextEventHandler( handler
, event
, data
) ;
3003 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler
)
3005 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
3006 const wxString
& str
,
3008 const wxSize
& size
, long style
) : wxMacMLTEControl( wxPeer
)
3010 m_font
= wxPeer
->GetFont() ;
3011 m_windowStyle
= style
;
3012 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
3014 wxMacConvertNewlines10To13( &st
) ;
3017 { bounds
.left
, bounds
.top
},
3018 { bounds
.right
- bounds
.left
, bounds
.bottom
- bounds
.top
} } ;
3020 m_scrollView
= NULL
;
3021 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( style
) ;
3022 if (( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
)) || !(frameOptions
&kTXNSingleLineOnlyMask
))
3024 if ( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
) )
3027 (frameOptions
& kTXNWantHScrollBarMask
? kHIScrollViewOptionsHorizScroll
: 0)
3028 | (frameOptions
& kTXNWantVScrollBarMask
? kHIScrollViewOptionsVertScroll
: 0) ,
3033 HIScrollViewCreate(kHIScrollViewOptionsVertScroll
,&m_scrollView
);
3034 HIScrollViewSetScrollBarAutoHide(m_scrollView
,true);
3037 HIViewSetFrame( m_scrollView
, &hr
);
3038 HIViewSetVisible( m_scrollView
, true );
3042 HITextViewCreate( NULL
, 0, frameOptions
, &m_textView
) ;
3043 m_txn
= HITextViewGetTXNObject( m_textView
) ;
3044 HIViewSetVisible( m_textView
, true ) ;
3047 HIViewAddSubview( m_scrollView
, m_textView
) ;
3048 m_controlRef
= m_scrollView
;
3049 wxPeer
->MacInstallEventHandler( (WXWidget
) m_textView
) ;
3053 HIViewSetFrame( m_textView
, &hr
);
3054 m_controlRef
= m_textView
;
3057 AdjustCreationAttributes( *wxWHITE
, true ) ;
3059 wxMacWindowClipper
c( m_peer
) ;
3061 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
3063 TXNSetSelection( m_txn
, 0, 0 );
3064 TXNShowSelection( m_txn
, kTXNShowStart
);
3066 InstallControlEventHandler( m_textView
, GetwxMacTextControlEventHandlerUPP(),
3067 GetEventTypeCount(eventList
), eventList
, this,
3071 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3075 OSStatus
wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart
)
3077 return SetKeyboardFocus( GetControlOwner( m_textView
), m_textView
, focusPart
) ;
3080 bool wxMacMLTEHIViewControl::HasFocus() const
3082 ControlRef control
;
3083 if ( GetUserFocusWindow() == NULL
)
3086 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
3087 return control
== m_textView
;
3090 void wxMacMLTEHIViewControl::SetBackgroundColour(const wxColour
& col
)
3092 HITextViewSetBackgroundColor( m_textView
, col
.GetPixel() );
3095 #endif // wxUSE_TEXTCTRL