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
|| ( UMAGetSystemVersion() >= 0x1050 ) )
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 HandleWindowEvent( 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 HandleWindowEvent( 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 ( HandleWindowEvent(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 if ( ! (wxStyle
& wxTE_DONTWRAP
) )
1629 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
1631 if ( !(wxStyle
& wxTE_NO_VSCROLL
) )
1633 frameOptions
|= kTXNWantVScrollBarMask
;
1635 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1636 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1637 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1638 // if ( frameOptions & kTXNWantHScrollBarMask )
1639 // frameOptions |= kTXNDrawGrowIconMask ;
1644 frameOptions
|= kTXNSingleLineOnlyMask
;
1647 return frameOptions
;
1650 void wxMacMLTEControl::AdjustCreationAttributes(const wxColour
&background
,
1651 bool WXUNUSED(visible
))
1653 TXNControlTag iControlTags
[] =
1655 kTXNDoFontSubstitution
,
1656 kTXNWordWrapStateTag
,
1658 TXNControlData iControlData
[] =
1664 int toptag
= WXSIZEOF( iControlTags
) ;
1666 if ( m_windowStyle
& wxTE_MULTILINE
)
1668 iControlData
[1].uValue
=
1669 (m_windowStyle
& wxTE_DONTWRAP
)
1674 OSStatus err
= TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) ;
1675 verify_noerr( err
);
1677 // setting the default font:
1678 // under 10.2 this causes a visible caret, therefore we avoid it
1684 GetThemeFont( kThemeSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
1686 TXNTypeAttributes typeAttr
[] =
1688 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
1689 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
1690 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
1693 err
= TXNSetTypeAttributes(
1694 m_txn
, sizeof(typeAttr
) / sizeof(TXNTypeAttributes
),
1695 typeAttr
, kTXNStartOffset
, kTXNEndOffset
);
1696 verify_noerr( err
);
1698 if ( m_windowStyle
& wxTE_PASSWORD
)
1700 UniChar c
= 0x00A5 ;
1701 err
= TXNEchoMode( m_txn
, c
, 0 , true );
1702 verify_noerr( err
);
1705 TXNBackground tback
;
1706 tback
.bgType
= kTXNBackgroundTypeRGB
;
1707 background
.GetRGBColor( &tback
.bg
.color
);
1708 TXNSetBackground( m_txn
, &tback
);
1711 TXNCommandEventSupportOptions options
;
1712 if ( TXNGetCommandEventSupport( m_txn
, &options
) == noErr
)
1715 kTXNSupportEditCommandProcessing
1716 | kTXNSupportEditCommandUpdating
1717 | kTXNSupportFontCommandProcessing
1718 | kTXNSupportFontCommandUpdating
;
1720 // only spell check when not read-only
1721 // use system options for the default
1722 bool checkSpelling
= false ;
1723 if ( !(m_windowStyle
& wxTE_READONLY
) )
1725 #if wxUSE_SYSTEM_OPTIONS
1726 if ( wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) == 1) )
1728 checkSpelling
= true ;
1733 if ( checkSpelling
)
1735 kTXNSupportSpellCheckCommandProcessing
1736 | kTXNSupportSpellCheckCommandUpdating
;
1738 TXNSetCommandEventSupport( m_txn
, options
) ;
1742 void wxMacMLTEControl::SetBackgroundColour(const wxColour
& col
)
1744 TXNBackground tback
;
1745 tback
.bgType
= kTXNBackgroundTypeRGB
;
1746 col
.GetRGBColor(&tback
.bg
.color
);
1747 TXNSetBackground( m_txn
, &tback
);
1750 static inline int wxConvertToTXN(int x
)
1752 return wx_static_cast(int, x
/ 254.0 * 72 + 0.5);
1755 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
)
1757 TXNTypeAttributes typeAttr
[4] ;
1759 size_t typeAttrCount
= 0 ;
1762 TXNControlTag controlTags
[4];
1763 TXNControlData controlData
[4];
1764 size_t controlAttrCount
= 0;
1766 TXNTab
* tabs
= NULL
;
1768 bool relayout
= false;
1770 if ( style
.HasFont() )
1772 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1773 const wxFont
&font
= style
.GetFont() ;
1774 typeAttr
[typeAttrCount
].tag
= kTXNATSUIStyle
;
1775 typeAttr
[typeAttrCount
].size
= kTXNATSUIStyleSize
;
1776 typeAttr
[typeAttrCount
].data
.dataPtr
= font
.MacGetATSUStyle() ;
1780 if ( style
.HasTextColour() )
1782 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1783 style
.GetTextColour().GetRGBColor( &color
);
1784 typeAttr
[typeAttrCount
].tag
= kTXNQDFontColorAttribute
;
1785 typeAttr
[typeAttrCount
].size
= kTXNQDFontColorAttributeSize
;
1786 typeAttr
[typeAttrCount
].data
.dataPtr
= (void*) &color
;
1790 if ( style
.HasAlignment() )
1792 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1795 switch ( style
.GetAlignment() )
1797 case wxTEXT_ALIGNMENT_LEFT
:
1798 align
= kTXNFlushLeft
;
1800 case wxTEXT_ALIGNMENT_CENTRE
:
1803 case wxTEXT_ALIGNMENT_RIGHT
:
1804 align
= kTXNFlushRight
;
1806 case wxTEXT_ALIGNMENT_JUSTIFIED
:
1807 align
= kTXNFullJust
;
1810 case wxTEXT_ALIGNMENT_DEFAULT
:
1811 align
= kTXNFlushDefault
;
1815 controlTags
[controlAttrCount
] = kTXNJustificationTag
;
1816 controlData
[controlAttrCount
].sValue
= align
;
1817 controlAttrCount
++ ;
1820 if ( style
.HasLeftIndent() || style
.HasRightIndent() )
1822 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1823 controlTags
[controlAttrCount
] = kTXNMarginsTag
;
1824 controlData
[controlAttrCount
].marginsPtr
= &margins
;
1825 verify_noerr( TXNGetTXNObjectControls (m_txn
, 1 ,
1826 &controlTags
[controlAttrCount
], &controlData
[controlAttrCount
]) );
1827 if ( style
.HasLeftIndent() )
1829 margins
.leftMargin
= wxConvertToTXN(style
.GetLeftIndent());
1831 if ( style
.HasRightIndent() )
1833 margins
.rightMargin
= wxConvertToTXN(style
.GetRightIndent());
1835 controlAttrCount
++ ;
1838 if ( style
.HasTabs() )
1840 const wxArrayInt
& tabarray
= style
.GetTabs();
1841 // unfortunately Mac only applies a tab distance, not individually different tabs
1842 controlTags
[controlAttrCount
] = kTXNTabSettingsTag
;
1843 if ( tabarray
.size() > 0 )
1844 controlData
[controlAttrCount
].tabValue
.value
= wxConvertToTXN(tabarray
[0]);
1846 controlData
[controlAttrCount
].tabValue
.value
= 72 ;
1848 controlData
[controlAttrCount
].tabValue
.tabType
= kTXNLeftTab
;
1849 controlAttrCount
++ ;
1852 // unfortunately the relayout is not automatic
1853 if ( controlAttrCount
> 0 )
1855 verify_noerr( TXNSetTXNObjectControls (m_txn
, false /* don't clear all */, controlAttrCount
,
1856 controlTags
, controlData
) );
1860 if ( typeAttrCount
> 0 )
1862 verify_noerr( TXNSetTypeAttributes( m_txn
, typeAttrCount
, typeAttr
, from
, to
) );
1873 TXNRecalcTextLayout( m_txn
);
1877 void wxMacMLTEControl::SetFont(const wxFont
& font
,
1878 const wxColour
& foreground
,
1879 long WXUNUSED(windowStyle
))
1881 wxMacEditHelper
help( m_txn
) ;
1882 TXNSetAttribute( wxTextAttr( foreground
, wxNullColour
, font
), kTXNStartOffset
, kTXNEndOffset
) ;
1885 void wxMacMLTEControl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1887 wxMacEditHelper
help( m_txn
) ;
1888 TXNSetAttribute( style
, start
, end
) ;
1891 void wxMacMLTEControl::Copy()
1896 void wxMacMLTEControl::Cut()
1901 void wxMacMLTEControl::Paste()
1906 bool wxMacMLTEControl::CanPaste() const
1908 return TXNIsScrapPastable() ;
1911 void wxMacMLTEControl::SetEditable(bool editable
)
1913 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1914 TXNControlData data
[] = { { editable
? kTXNReadWrite
: kTXNReadOnly
} } ;
1915 TXNSetTXNObjectControls( m_txn
, false, WXSIZEOF(tag
), tag
, data
) ;
1918 wxTextPos
wxMacMLTEControl::GetLastPosition() const
1920 wxTextPos actualsize
= 0 ;
1923 OSErr err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1928 actualsize
= GetHandleSize( theText
) ;
1929 DisposeHandle( theText
) ;
1939 void wxMacMLTEControl::Replace( long from
, long to
, const wxString
&str
)
1941 wxString value
= str
;
1942 wxMacConvertNewlines10To13( &value
) ;
1944 wxMacEditHelper
help( m_txn
) ;
1946 wxMacWindowClipper
c( m_peer
) ;
1949 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
) ;
1951 SetTXNData( value
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1954 void wxMacMLTEControl::Remove( long from
, long to
)
1957 wxMacWindowClipper
c( m_peer
) ;
1959 wxMacEditHelper
help( m_txn
) ;
1960 TXNSetSelection( m_txn
, from
, to
) ;
1964 void wxMacMLTEControl::GetSelection( long* from
, long* to
) const
1967 TXNGetSelection( m_txn
, &f
, &t
) ;
1972 void wxMacMLTEControl::SetSelection( long from
, long to
)
1975 wxMacWindowClipper
c( m_peer
) ;
1978 // change the selection
1979 if ((from
== -1) && (to
== -1))
1980 TXNSelectAll( m_txn
);
1982 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
);
1984 TXNShowSelection( m_txn
, kTXNShowStart
);
1987 void wxMacMLTEControl::WriteText( const wxString
& str
)
1990 wxMacConvertNewlines10To13( &st
) ;
1992 long start
, end
, dummy
;
1994 GetSelection( &start
, &dummy
) ;
1996 wxMacWindowClipper
c( m_peer
) ;
2000 wxMacEditHelper
helper( m_txn
) ;
2001 SetTXNData( st
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
2004 GetSelection( &dummy
, &end
) ;
2006 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2009 void wxMacMLTEControl::Clear()
2012 wxMacWindowClipper
c( m_peer
) ;
2014 wxMacEditHelper
st( m_txn
) ;
2015 TXNSetSelection( m_txn
, kTXNStartOffset
, kTXNEndOffset
) ;
2019 bool wxMacMLTEControl::CanUndo() const
2021 return TXNCanUndo( m_txn
, NULL
) ;
2024 void wxMacMLTEControl::Undo()
2029 bool wxMacMLTEControl::CanRedo() const
2031 return TXNCanRedo( m_txn
, NULL
) ;
2034 void wxMacMLTEControl::Redo()
2039 int wxMacMLTEControl::GetNumberOfLines() const
2041 ItemCount lines
= 0 ;
2042 TXNGetLineCount( m_txn
, &lines
) ;
2047 long wxMacMLTEControl::XYToPosition(long x
, long y
) const
2052 // TODO: find a better implementation : while we can get the
2053 // line metrics of a certain line, we don't get its starting
2054 // position, so it would probably be rather a binary search
2055 // for the start position
2056 long xpos
= 0, ypos
= 0 ;
2057 int lastHeight
= 0 ;
2060 lastpos
= GetLastPosition() ;
2061 for ( n
= 0 ; n
<= (ItemCount
) lastpos
; ++n
)
2063 if ( y
== ypos
&& x
== xpos
)
2066 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2068 if ( curpt
.v
> lastHeight
)
2074 lastHeight
= curpt
.v
;
2083 bool wxMacMLTEControl::PositionToXY( long pos
, long *x
, long *y
) const
2093 lastpos
= GetLastPosition() ;
2094 if ( pos
<= lastpos
)
2096 // TODO: find a better implementation - while we can get the
2097 // line metrics of a certain line, we don't get its starting
2098 // position, so it would probably be rather a binary search
2099 // for the start position
2100 long xpos
= 0, ypos
= 0 ;
2101 int lastHeight
= 0 ;
2104 for ( n
= 0 ; n
<= (ItemCount
) pos
; ++n
)
2106 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2108 if ( curpt
.v
> lastHeight
)
2114 lastHeight
= curpt
.v
;
2129 void wxMacMLTEControl::ShowPosition( long pos
)
2131 Point current
, desired
;
2132 TXNOffset selstart
, selend
;
2134 TXNGetSelection( m_txn
, &selstart
, &selend
);
2135 TXNOffsetToPoint( m_txn
, selstart
, ¤t
);
2136 TXNOffsetToPoint( m_txn
, pos
, &desired
);
2138 // TODO: use HIPoints for 10.3 and above
2140 OSErr theErr
= noErr
;
2141 long dv
= desired
.v
- current
.v
;
2142 long dh
= desired
.h
- current
.h
;
2143 TXNShowSelection( m_txn
, kTXNShowStart
) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2144 theErr
= TXNScroll( m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
, &dv
, &dh
);
2146 // there will be an error returned for classic MLTE implementation when the control is
2147 // invisible, but HITextView works correctly, so we don't assert that one
2148 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2151 void wxMacMLTEControl::SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
)
2154 #if SIZEOF_WCHAR_T == 2
2155 size_t len
= st
.length() ;
2156 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)st
.wc_str(), len
* 2, start
, end
);
2158 wxMBConvUTF16 converter
;
2159 ByteCount byteBufferLen
= converter
.WC2MB( NULL
, st
.wc_str(), 0 ) ;
2160 UniChar
*unibuf
= (UniChar
*)malloc( byteBufferLen
) ;
2161 converter
.WC2MB( (char*)unibuf
, st
.wc_str(), byteBufferLen
) ;
2162 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)unibuf
, byteBufferLen
, start
, end
) ;
2166 wxCharBuffer text
= st
.mb_str( wxConvLocal
) ;
2167 TXNSetData( m_txn
, kTXNTextData
, (void*)text
.data(), strlen( text
), start
, end
) ;
2171 wxString
wxMacMLTEControl::GetLineText(long lineNo
) const
2175 if ( lineNo
< GetNumberOfLines() )
2178 Fixed lineWidth
, lineHeight
, currentHeight
;
2181 // get the first possible position in the control
2182 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2184 // Iterate through the lines until we reach the one we want,
2185 // adding to our current y pixel point position
2188 while (ypos
< lineNo
)
2190 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2191 currentHeight
+= lineHeight
;
2194 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2195 TXNOffset theOffset
;
2196 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2198 wxString content
= GetStringValue() ;
2199 Point currentPoint
= thePoint
;
2200 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2202 line
+= content
[theOffset
];
2203 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2210 int wxMacMLTEControl::GetLineLength(long lineNo
) const
2214 if ( lineNo
< GetNumberOfLines() )
2217 Fixed lineWidth
, lineHeight
, currentHeight
;
2220 // get the first possible position in the control
2221 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2223 // Iterate through the lines until we reach the one we want,
2224 // adding to our current y pixel point position
2227 while (ypos
< lineNo
)
2229 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2230 currentHeight
+= lineHeight
;
2233 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2234 TXNOffset theOffset
;
2235 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2237 wxString content
= GetStringValue() ;
2238 Point currentPoint
= thePoint
;
2239 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2242 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2249 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
2251 // ----------------------------------------------------------------------------
2252 // MLTE control implementation (classic part)
2253 // ----------------------------------------------------------------------------
2255 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2256 // has to live on. We have different problems coming from outdated implementations on the
2257 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2258 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2259 // no way out, therefore we are using our own implementation and our own scrollbars ....
2261 wxMacPortSaver::wxMacPortSaver( GrafPtr port
)
2263 ::GetPort( &m_port
);
2267 wxMacPortSaver::~wxMacPortSaver()
2269 ::SetPort( m_port
);
2272 wxMacWindowClipper::wxMacWindowClipper( const wxWindow
* win
) :
2273 wxMacPortSaver( (GrafPtr
) GetWindowPort( (WindowRef
) win
->MacGetTopLevelWindowRef() ) )
2275 m_newPort
= (GrafPtr
) GetWindowPort( (WindowRef
) win
->MacGetTopLevelWindowRef() ) ;
2276 m_formerClip
= NewRgn() ;
2277 m_newClip
= NewRgn() ;
2278 GetClip( m_formerClip
) ;
2282 // guard against half constructed objects, this just leads to a empty clip
2283 if ( win
->GetPeer() )
2286 win
->MacWindowToRootWindow( &x
, &y
) ;
2288 // get area including focus rect
2289 HIShapeGetAsQDRgn( ((wxWindow
*)win
)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip
);
2290 if ( !EmptyRgn( m_newClip
) )
2291 OffsetRgn( m_newClip
, x
, y
) ;
2294 SetClip( m_newClip
) ;
2298 wxMacWindowClipper::~wxMacWindowClipper()
2300 SetPort( m_newPort
) ;
2301 SetClip( m_formerClip
) ;
2302 DisposeRgn( m_newClip
) ;
2303 DisposeRgn( m_formerClip
) ;
2306 TXNScrollInfoUPP gTXNScrollInfoProc
= NULL
;
2307 ControlActionUPP gTXNScrollActionProc
= NULL
;
2309 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2310 SInt32 iValue
, SInt32 iMaximumValue
,
2311 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
)
2313 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) iRefCon
;
2314 SInt32 value
= wxMax( iValue
, 0 ) ;
2315 SInt32 maximum
= wxMax( iMaximumValue
, 0 ) ;
2317 if ( iScrollBarOrientation
== kTXNHorizontal
)
2319 if ( mlte
->m_sbHorizontal
)
2321 SetControl32BitValue( mlte
->m_sbHorizontal
, value
) ;
2322 SetControl32BitMaximum( mlte
->m_sbHorizontal
, maximum
) ;
2323 mlte
->m_lastHorizontalValue
= value
;
2326 else if ( iScrollBarOrientation
== kTXNVertical
)
2328 if ( mlte
->m_sbVertical
)
2330 SetControl32BitValue( mlte
->m_sbVertical
, value
) ;
2331 SetControl32BitMaximum( mlte
->m_sbVertical
, maximum
) ;
2332 mlte
->m_lastVerticalValue
= value
;
2337 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
)
2339 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) GetControlReference( controlRef
) ;
2343 if ( controlRef
!= mlte
->m_sbVertical
&& controlRef
!= mlte
->m_sbHorizontal
)
2347 bool isHorizontal
= ( controlRef
== mlte
->m_sbHorizontal
) ;
2349 SInt32 minimum
= 0 ;
2350 SInt32 maximum
= GetControl32BitMaximum( controlRef
) ;
2351 SInt32 value
= GetControl32BitValue( controlRef
) ;
2356 case kControlDownButtonPart
:
2360 case kControlUpButtonPart
:
2364 case kControlPageDownPart
:
2365 delta
= GetControlViewSize( controlRef
) ;
2368 case kControlPageUpPart
:
2369 delta
= -GetControlViewSize( controlRef
) ;
2372 case kControlIndicatorPart
:
2373 delta
= value
- (isHorizontal
? mlte
->m_lastHorizontalValue
: mlte
->m_lastVerticalValue
) ;
2382 SInt32 newValue
= value
;
2384 if ( partCode
!= kControlIndicatorPart
)
2386 if ( value
+ delta
< minimum
)
2387 delta
= minimum
- value
;
2388 if ( value
+ delta
> maximum
)
2389 delta
= maximum
- value
;
2391 SetControl32BitValue( controlRef
, value
+ delta
) ;
2392 newValue
= value
+ delta
;
2395 SInt32 verticalDelta
= isHorizontal
? 0 : delta
;
2396 SInt32 horizontalDelta
= isHorizontal
? delta
: 0 ;
2399 mlte
->m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
,
2400 &verticalDelta
, &horizontalDelta
);
2401 verify_noerr( err
);
2404 mlte
->m_lastHorizontalValue
= newValue
;
2406 mlte
->m_lastVerticalValue
= newValue
;
2410 // make correct activations
2411 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive
)
2413 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference(m_controlRef
);
2415 wxMacWindowClipper
clipper( textctrl
) ;
2416 TXNActivate( m_txn
, m_txnFrameID
, setActive
);
2418 ControlRef controlFocus
= 0 ;
2419 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2420 if ( controlFocus
== m_controlRef
)
2421 TXNFocus( m_txn
, setActive
);
2424 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus
)
2426 TXNFocus( m_txn
, setFocus
);
2429 // guards against inappropriate redraw (hidden objects drawing onto window)
2431 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis
)
2433 ControlRef controlFocus
= 0 ;
2434 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2436 if ( !vis
&& (controlFocus
== m_controlRef
) )
2437 SetKeyboardFocus( m_txnWindow
, m_controlRef
, kControlFocusNoPart
) ;
2439 TXNControlTag iControlTags
[1] = { kTXNVisibilityTag
};
2440 TXNControlData iControlData
[1] = { { (UInt32
)false } };
2442 verify_noerr( TXNGetTXNObjectControls( m_txn
, 1, iControlTags
, iControlData
) ) ;
2444 if ( iControlData
[0].uValue
!= vis
)
2446 iControlData
[0].uValue
= vis
;
2447 verify_noerr( TXNSetTXNObjectControls( m_txn
, false , 1, iControlTags
, iControlData
) ) ;
2450 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2451 // if we run into further problems we might set the FrameBounds to an empty rect here
2454 // make sure that the TXNObject is at the right position
2456 void wxMacMLTEClassicControl::MacUpdatePosition()
2458 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2459 if ( textctrl
== NULL
)
2463 GetRectInWindowCoords( &bounds
);
2465 wxRect visRect
= textctrl
->MacGetClippedClientRect() ;
2466 Rect visBounds
= { visRect
.y
, visRect
.x
, visRect
.y
+ visRect
.height
, visRect
.x
+ visRect
.width
} ;
2469 textctrl
->MacWindowToRootWindow( &x
, &y
) ;
2470 OffsetRect( &visBounds
, x
, y
) ;
2472 if ( !EqualRect( &bounds
, &m_txnControlBounds
) || !EqualRect( &visBounds
, &m_txnVisBounds
) )
2474 m_txnControlBounds
= bounds
;
2475 m_txnVisBounds
= visBounds
;
2476 wxMacWindowClipper
cl( textctrl
) ;
2478 if ( m_sbHorizontal
|| m_sbVertical
)
2480 int w
= bounds
.right
- bounds
.left
;
2481 int h
= bounds
.bottom
- bounds
.top
;
2483 if ( m_sbHorizontal
)
2487 sbBounds
.left
= -1 ;
2488 sbBounds
.top
= h
- 14 ;
2489 sbBounds
.right
= w
+ 1 ;
2490 sbBounds
.bottom
= h
+ 1 ;
2492 SetControlBounds( m_sbHorizontal
, &sbBounds
) ;
2493 SetControlViewSize( m_sbHorizontal
, w
) ;
2500 sbBounds
.left
= w
- 14 ;
2502 sbBounds
.right
= w
+ 1 ;
2503 sbBounds
.bottom
= m_sbHorizontal
? h
- 14 : h
+ 1 ;
2505 SetControlBounds( m_sbVertical
, &sbBounds
) ;
2506 SetControlViewSize( m_sbVertical
, h
) ;
2511 TXNLongRect olddestRect
;
2512 TXNGetRectBounds( m_txn
, &oldviewRect
, &olddestRect
, NULL
) ;
2514 Rect viewRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2515 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2516 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2517 TXNLongRect destRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2518 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2519 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2521 if ( olddestRect
.right
>= 10000 )
2522 destRect
.right
= destRect
.left
+ 32000 ;
2524 if ( olddestRect
.bottom
>= 0x20000000 )
2525 destRect
.bottom
= destRect
.top
+ 0x40000000 ;
2527 SectRect( &viewRect
, &visBounds
, &viewRect
) ;
2528 TXNSetRectBounds( m_txn
, &viewRect
, &destRect
, true ) ;
2533 m_txnControlBounds
.top
,
2534 m_txnControlBounds
.left
,
2535 m_txnControlBounds
.bottom
- (m_sbHorizontal
? 14 : 0),
2536 m_txnControlBounds
.right
- (m_sbVertical
? 14 : 0),
2540 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2541 // movement, therefore we have to force it
2543 // this problem has been reported in OSX as well, so we use this here once again
2545 TXNLongRect textRect
;
2546 TXNGetRectBounds( m_txn
, NULL
, NULL
, &textRect
) ;
2547 if ( textRect
.left
< m_txnControlBounds
.left
)
2548 TXNShowSelection( m_txn
, kTXNShowStart
) ;
2552 void wxMacMLTEClassicControl::SetRect( Rect
*r
)
2554 wxMacControl::SetRect( r
) ;
2555 MacUpdatePosition() ;
2558 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16
WXUNUSED(thePart
))
2560 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2561 if ( textctrl
== NULL
)
2564 if ( textctrl
->MacIsReallyShown() )
2566 wxMacWindowClipper
clipper( textctrl
) ;
2567 TXNDraw( m_txn
, NULL
) ;
2571 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
2573 Point where
= { y
, x
} ;
2574 ControlPartCode result
= kControlNoPart
;
2576 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2577 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2579 if (PtInRect( where
, &m_txnControlBounds
))
2581 result
= kControlEditTextPart
;
2585 // sometimes we get the coords also in control local coordinates, therefore test again
2587 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2591 if (PtInRect( where
, &m_txnControlBounds
))
2592 result
= kControlEditTextPart
;
2599 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x
, wxInt16 y
, void* WXUNUSED(actionProc
) )
2601 ControlPartCode result
= kControlNoPart
;
2603 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2604 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2606 Point startPt
= { y
, x
} ;
2608 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2610 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2614 switch (MacControlUserPaneHitTestProc( startPt
.h
, startPt
.v
))
2616 case kControlEditTextPart
:
2618 wxMacWindowClipper
clipper( textctrl
) ;
2621 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
2622 TXNClick( m_txn
, &rec
);
2634 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2636 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2637 if ( textctrl
== NULL
)
2640 if (textctrl
->MacIsReallyShown())
2642 if (IsControlActive(m_controlRef
))
2646 wxMacWindowClipper
clipper( textctrl
) ;
2651 if (PtInRect(mousep
, &m_txnControlBounds
))
2653 RgnHandle theRgn
= NewRgn();
2654 RectRgn(theRgn
, &m_txnControlBounds
);
2655 TXNAdjustCursor(m_txn
, theRgn
);
2662 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
2664 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2665 if ( textctrl
== NULL
)
2666 return kControlNoPart
;
2668 wxMacWindowClipper
clipper( textctrl
) ;
2671 memset( &ev
, 0 , sizeof( ev
) ) ;
2673 ev
.modifiers
= modifiers
;
2674 ev
.message
= ((keyCode
<< 8) & keyCodeMask
) | (charCode
& charCodeMask
);
2675 TXNKeyDown( m_txn
, &ev
);
2677 return kControlEntireControl
;
2680 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating
)
2682 MacActivatePaneText( activating
);
2685 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action
)
2687 ControlPartCode focusResult
= kControlFocusNoPart
;
2689 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2690 if ( textctrl
== NULL
)
2693 wxMacWindowClipper
clipper( textctrl
) ;
2695 ControlRef controlFocus
= NULL
;
2696 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2697 bool wasFocused
= ( controlFocus
== m_controlRef
) ;
2701 case kControlFocusPrevPart
:
2702 case kControlFocusNextPart
:
2703 MacFocusPaneText( !wasFocused
);
2704 focusResult
= (!wasFocused
? (ControlPartCode
) kControlEditTextPart
: (ControlPartCode
) kControlFocusNoPart
);
2707 case kControlFocusNoPart
:
2709 MacFocusPaneText( false );
2710 focusResult
= kControlFocusNoPart
;
2717 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info
) )
2721 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
2722 const wxString
& str
,
2724 const wxSize
& size
, long style
)
2725 : wxMacMLTEControl( wxPeer
)
2727 m_font
= wxPeer
->GetFont() ;
2728 m_windowStyle
= style
;
2729 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
2732 kControlSupportsEmbedding
| kControlSupportsFocus
| kControlWantsIdle
2733 | kControlWantsActivate
| kControlHandlesTracking
2734 // | kControlHasSpecialBackground
2735 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
2737 OSStatus err
= ::CreateUserPaneControl(
2738 MAC_WXHWND(wxPeer
->GetParent()->MacGetTopLevelWindowRef()),
2739 &bounds
, featureSet
, &m_controlRef
);
2740 verify_noerr( err
);
2744 AdjustCreationAttributes( *wxWHITE
, true ) ;
2746 MacSetObjectVisibility( wxPeer
->MacIsReallyShown() ) ;
2750 wxMacConvertNewlines10To13( &st
) ;
2751 wxMacWindowClipper
clipper( m_peer
) ;
2752 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
2753 TXNSetSelection( m_txn
, 0, 0 ) ;
2757 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2759 TXNDeleteObject( m_txn
);
2763 void wxMacMLTEClassicControl::VisibilityChanged(bool shown
)
2765 MacSetObjectVisibility( shown
) ;
2766 wxMacControl::VisibilityChanged( shown
) ;
2769 void wxMacMLTEClassicControl::SuperChangedPosition()
2771 MacUpdatePosition() ;
2772 wxMacControl::SuperChangedPosition() ;
2775 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
2776 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
2777 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
2778 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
2779 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
2780 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
2781 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
2783 static pascal void wxMacControlUserPaneDrawProc(ControlRef control
, SInt16 part
)
2785 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2786 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2788 win
->MacControlUserPaneDrawProc( part
) ;
2791 static pascal ControlPartCode
wxMacControlUserPaneHitTestProc(ControlRef control
, Point where
)
2793 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2794 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2796 return win
->MacControlUserPaneHitTestProc( where
.h
, where
.v
) ;
2798 return kControlNoPart
;
2801 static pascal ControlPartCode
wxMacControlUserPaneTrackingProc(ControlRef control
, Point startPt
, ControlActionUPP actionProc
)
2803 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2804 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2806 return win
->MacControlUserPaneTrackingProc( startPt
.h
, startPt
.v
, (void*) actionProc
) ;
2808 return kControlNoPart
;
2811 static pascal void wxMacControlUserPaneIdleProc(ControlRef control
)
2813 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2814 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2816 win
->MacControlUserPaneIdleProc() ;
2819 static pascal ControlPartCode
wxMacControlUserPaneKeyDownProc(ControlRef control
, SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
)
2821 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2822 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2824 return win
->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
2826 return kControlNoPart
;
2829 static pascal void wxMacControlUserPaneActivateProc(ControlRef control
, Boolean activating
)
2831 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2832 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2834 win
->MacControlUserPaneActivateProc( activating
) ;
2837 static pascal ControlPartCode
wxMacControlUserPaneFocusProc(ControlRef control
, ControlFocusPart action
)
2839 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2840 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2842 return win
->MacControlUserPaneFocusProc( action
) ;
2844 return kControlNoPart
;
2848 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control
, ControlBackgroundPtr info
)
2850 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2851 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2853 win
->MacControlUserPaneBackgroundProc(info
) ;
2857 // TXNRegisterScrollInfoProc
2859 OSStatus
wxMacMLTEClassicControl::DoCreate()
2862 OSStatus err
= noErr
;
2864 // set up our globals
2865 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc
);
2866 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc
);
2867 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc
);
2868 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc
);
2869 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc
);
2870 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc
);
2871 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc
);
2873 if (gTXNScrollInfoProc
== NULL
) gTXNScrollInfoProc
= NewTXNScrollInfoUPP(TXNScrollInfoProc
) ;
2874 if (gTXNScrollActionProc
== NULL
) gTXNScrollActionProc
= NewControlActionUPP(TXNScrollActionProc
) ;
2876 // set the initial settings for our private data
2878 m_txnWindow
= GetControlOwner(m_controlRef
);
2879 m_txnPort
= (GrafPtr
) GetWindowPort(m_txnWindow
);
2881 // set up the user pane procedures
2882 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
2883 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
2884 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
2885 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
2886 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
2887 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
2888 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
2890 // calculate the rectangles used by the control
2891 GetRectInWindowCoords( &bounds
);
2893 m_txnControlBounds
= bounds
;
2894 m_txnVisBounds
= bounds
;
2899 GetGWorld( &origPort
, &origDev
) ;
2900 SetPort( m_txnPort
);
2902 // create the new edit field
2903 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( m_windowStyle
);
2905 // the scrollbars are not correctly embedded but are inserted at the root:
2906 // this gives us problems as we have erratic redraws even over the structure area
2908 m_sbHorizontal
= 0 ;
2910 m_lastHorizontalValue
= 0 ;
2911 m_lastVerticalValue
= 0 ;
2913 Rect sb
= { 0 , 0 , 0 , 0 } ;
2914 if ( frameOptions
& kTXNWantVScrollBarMask
)
2916 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbVertical
);
2917 SetControlReference( m_sbVertical
, (SInt32
)this );
2918 SetControlAction( m_sbVertical
, gTXNScrollActionProc
);
2919 ShowControl( m_sbVertical
);
2920 EmbedControl( m_sbVertical
, m_controlRef
);
2921 frameOptions
&= ~kTXNWantVScrollBarMask
;
2924 if ( frameOptions
& kTXNWantHScrollBarMask
)
2926 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbHorizontal
);
2927 SetControlReference( m_sbHorizontal
, (SInt32
)this );
2928 SetControlAction( m_sbHorizontal
, gTXNScrollActionProc
);
2929 ShowControl( m_sbHorizontal
);
2930 EmbedControl( m_sbHorizontal
, m_controlRef
);
2931 frameOptions
&= ~(kTXNWantHScrollBarMask
| kTXNDrawGrowIconMask
);
2935 NULL
, m_txnWindow
, &bounds
, frameOptions
,
2936 kTXNTextEditStyleFrameType
, kTXNTextensionFile
, kTXNSystemDefaultEncoding
,
2937 &m_txn
, &m_txnFrameID
, NULL
);
2938 verify_noerr( err
);
2941 TXNControlTag iControlTags
[] = { kTXNUseCarbonEvents
};
2942 TXNControlData iControlData
[] = { { (UInt32
)&cInfo
} };
2943 int toptag
= WXSIZEOF( iControlTags
) ;
2944 TXNCarbonEventInfo cInfo
;
2945 cInfo
.useCarbonEvents
= false ;
2948 cInfo
.fDictionary
= NULL
;
2950 verify_noerr( TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) );
2953 TXNRegisterScrollInfoProc( m_txn
, gTXNScrollInfoProc
, (SInt32
)this );
2955 SetGWorld( origPort
, origDev
) ;
2961 // ----------------------------------------------------------------------------
2962 // MLTE control implementation (OSX part)
2963 // ----------------------------------------------------------------------------
2965 // tiger multi-line textcontrols with no CR in the entire content
2966 // don't scroll automatically, so we need a hack.
2967 // This attempt only works 'before' the key (ie before CallNextEventHandler)
2968 // is processed, thus the scrolling always occurs one character too late, but
2969 // better than nothing ...
2971 static const EventTypeSpec eventList
[] =
2973 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
2976 static pascal OSStatus
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2978 OSStatus result
= eventNotHandledErr
;
2979 wxMacMLTEHIViewControl
* focus
= (wxMacMLTEHIViewControl
*) data
;
2981 switch ( GetEventKind( event
) )
2983 case kEventTextInputUnicodeForKeyEvent
:
2985 TXNOffset from
, to
;
2986 TXNGetSelection( focus
->GetTXNObject() , &from
, &to
) ;
2988 TXNShowSelection( focus
->GetTXNObject() , kTXNShowStart
);
2989 result
= CallNextEventHandler(handler
,event
);
2999 static pascal OSStatus
wxMacTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
3001 OSStatus result
= eventNotHandledErr
;
3003 switch ( GetEventClass( event
) )
3005 case kEventClassTextInput
:
3006 result
= wxMacUnicodeTextEventHandler( handler
, event
, data
) ;
3015 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler
)
3017 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
3018 const wxString
& str
,
3020 const wxSize
& size
, long style
) : wxMacMLTEControl( wxPeer
)
3022 m_font
= wxPeer
->GetFont() ;
3023 m_windowStyle
= style
;
3024 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
3026 wxMacConvertNewlines10To13( &st
) ;
3029 { bounds
.left
, bounds
.top
},
3030 { bounds
.right
- bounds
.left
, bounds
.bottom
- bounds
.top
} } ;
3032 m_scrollView
= NULL
;
3033 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( style
) ;
3034 if (( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
)) || !(frameOptions
&kTXNSingleLineOnlyMask
))
3036 if ( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
) )
3039 (frameOptions
& kTXNWantHScrollBarMask
? kHIScrollViewOptionsHorizScroll
: 0)
3040 | (frameOptions
& kTXNWantVScrollBarMask
? kHIScrollViewOptionsVertScroll
: 0) ,
3045 HIScrollViewCreate(kHIScrollViewOptionsVertScroll
,&m_scrollView
);
3046 HIScrollViewSetScrollBarAutoHide(m_scrollView
,true);
3049 HIViewSetFrame( m_scrollView
, &hr
);
3050 HIViewSetVisible( m_scrollView
, true );
3054 HITextViewCreate( NULL
, 0, frameOptions
, &m_textView
) ;
3055 m_txn
= HITextViewGetTXNObject( m_textView
) ;
3056 HIViewSetVisible( m_textView
, true ) ;
3059 HIViewAddSubview( m_scrollView
, m_textView
) ;
3060 m_controlRef
= m_scrollView
;
3061 wxPeer
->MacInstallEventHandler( (WXWidget
) m_textView
) ;
3065 HIViewSetFrame( m_textView
, &hr
);
3066 m_controlRef
= m_textView
;
3069 AdjustCreationAttributes( *wxWHITE
, true ) ;
3071 wxMacWindowClipper
c( m_peer
) ;
3073 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
3075 TXNSetSelection( m_txn
, 0, 0 );
3076 TXNShowSelection( m_txn
, kTXNShowStart
);
3078 InstallControlEventHandler( m_textView
, GetwxMacTextControlEventHandlerUPP(),
3079 GetEventTypeCount(eventList
), eventList
, this,
3083 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3087 OSStatus
wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart
)
3089 return SetKeyboardFocus( GetControlOwner( m_textView
), m_textView
, focusPart
) ;
3092 bool wxMacMLTEHIViewControl::HasFocus() const
3094 ControlRef control
;
3095 if ( GetUserFocusWindow() == NULL
)
3098 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
3099 return control
== m_textView
;
3102 void wxMacMLTEHIViewControl::SetBackgroundColour(const wxColour
& col
)
3104 HITextViewSetBackgroundColor( m_textView
, col
.GetPixel() );
3107 #endif // wxUSE_TEXTCTRL