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 #if defined(__BORLANDC__) && !defined(__WIN32__)
50 #elif !defined(__MWERKS__) && !defined(__GNUWIN32) && !defined(__DARWIN__)
59 #include <MacTextEditor.h>
60 #include <ATSUnicode.h>
61 #include <TextCommon.h>
62 #include <TextEncodingConverter.h>
65 #include "wx/mac/uma.h"
66 #include "wx/mac/carbon/private/mactext.h"
71 kTXNVisibilityTag
= 'visb' // set the visibility state of the object
80 virtual ~wxMacFunctor() {}
82 virtual void* operator()() = 0 ;
84 static void* CallBackProc( void *param
)
86 wxMacFunctor
* f
= (wxMacFunctor
*) param
;
87 void *result
= (*f
)() ;
92 template<typename classtype
, typename param1type
>
94 class wxMacObjectFunctor1
: public wxMacFunctor
96 typedef void (classtype::*function
)( param1type p1
) ;
97 typedef void (classtype::*ref_function
)( const param1type
& p1
) ;
99 wxMacObjectFunctor1( classtype
*obj
, function f
, param1type p1
) :
107 wxMacObjectFunctor1( classtype
*obj
, ref_function f
, param1type p1
) :
115 virtual ~wxMacObjectFunctor1() {}
117 virtual void* operator()()
119 (m_object
->*m_function
)( m_param1
) ;
124 classtype
* m_object
;
125 param1type m_param1
;
128 function m_function
;
129 ref_function m_refFunction
;
133 template<typename classtype
, typename param1type
>
134 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
136 wxMacObjectFunctor1
<classtype
, param1type
> params(object
, function
, p1
) ;
138 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
142 template<typename classtype
, typename param1type
>
143 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
145 wxMacObjectFunctor1
<classtype
,param1type
> params(object
, function
, p1
) ;
147 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
151 template<typename classtype
, typename param1type
>
152 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
155 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
160 template<typename classtype
, typename param1type
>
161 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
164 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
170 // common parts for implementations based on MLTE
172 class wxMacMLTEControl
: public wxMacTextControl
175 wxMacMLTEControl( wxTextCtrl
*peer
) ;
177 virtual wxString
GetStringValue() const ;
178 virtual void SetStringValue( const wxString
&str
) ;
180 static TXNFrameOptions
FrameOptionsFromWXStyle( long wxStyle
) ;
182 void AdjustCreationAttributes( const wxColour
& background
, bool visible
) ;
184 virtual void SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
) ;
185 virtual void SetBackground( const wxBrush
&brush
) ;
186 virtual void SetStyle( long start
, long end
, const wxTextAttr
& style
) ;
187 virtual void Copy() ;
189 virtual void Paste() ;
190 virtual bool CanPaste() const ;
191 virtual void SetEditable( bool editable
) ;
192 virtual wxTextPos
GetLastPosition() const ;
193 virtual void Replace( long from
, long to
, const wxString
&str
) ;
194 virtual void Remove( long from
, long to
) ;
195 virtual void GetSelection( long* from
, long* to
) const ;
196 virtual void SetSelection( long from
, long to
) ;
198 virtual void WriteText( const wxString
& str
) ;
200 virtual bool HasOwnContextMenu() const
202 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
203 if ( UMAGetSystemVersion() >= 0x1040 )
205 TXNCommandEventSupportOptions options
;
206 TXNGetCommandEventSupport( m_txn
, & options
) ;
207 return options
& kTXNSupportEditCommandProcessing
;
214 virtual void CheckSpelling(bool check
)
216 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
217 TXNSetSpellCheckAsYouType( m_txn
, (Boolean
) check
);
220 virtual void Clear() ;
222 virtual bool CanUndo() const ;
223 virtual void Undo() ;
224 virtual bool CanRedo() const;
225 virtual void Redo() ;
226 virtual int GetNumberOfLines() const ;
227 virtual long XYToPosition(long x
, long y
) const ;
228 virtual bool PositionToXY(long pos
, long *x
, long *y
) const ;
229 virtual void ShowPosition( long pos
) ;
230 virtual int GetLineLength(long lineNo
) const ;
231 virtual wxString
GetLineText(long lineNo
) const ;
233 void SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
) ;
234 TXNObject
GetTXNObject() { return m_txn
; }
237 void TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
) ;
242 #if TARGET_API_MAC_OSX
244 // implementation available under OSX
246 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
248 class wxMacMLTEHIViewControl
: public wxMacMLTEControl
251 wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
254 const wxSize
& size
, long style
) ;
255 virtual ~wxMacMLTEHIViewControl() ;
257 virtual OSStatus
SetFocus( ControlFocusPart focusPart
) ;
258 virtual bool HasFocus() const ;
259 virtual void SetBackground( const wxBrush
&brush
) ;
262 HIViewRef m_scrollView
;
263 HIViewRef m_textView
;
264 EventHandlerRef m_textEventHandlerRef
;
271 // 'classic' MLTE implementation
273 class wxMacMLTEClassicControl
: public wxMacMLTEControl
276 wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
279 const wxSize
& size
, long style
) ;
280 virtual ~wxMacMLTEClassicControl() ;
282 virtual void VisibilityChanged(bool shown
) ;
283 virtual void SuperChangedPosition() ;
285 virtual void MacControlUserPaneDrawProc(wxInt16 part
) ;
286 virtual wxInt16
MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
) ;
287 virtual wxInt16
MacControlUserPaneTrackingProc(wxInt16 x
, wxInt16 y
, void* actionProc
) ;
288 virtual void MacControlUserPaneIdleProc() ;
289 virtual wxInt16
MacControlUserPaneKeyDownProc(wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
) ;
290 virtual void MacControlUserPaneActivateProc(bool activating
) ;
291 virtual wxInt16
MacControlUserPaneFocusProc(wxInt16 action
) ;
292 virtual void MacControlUserPaneBackgroundProc(void* info
) ;
294 virtual bool SetupCursor( const wxPoint
& WXUNUSED(pt
) )
296 MacControlUserPaneIdleProc();
300 virtual void SetRect( Rect
*r
) ;
305 void MacUpdatePosition() ;
306 void MacActivatePaneText(bool setActive
) ;
307 void MacFocusPaneText(bool setFocus
) ;
308 void MacSetObjectVisibility(bool vis
) ;
311 TXNFrameID m_txnFrameID
;
313 WindowRef m_txnWindow
;
314 // bounds of the control as we last did set the txn frames
315 Rect m_txnControlBounds
;
316 Rect m_txnVisBounds
;
319 static pascal void TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
) ;
320 static pascal void TXNScrollInfoProc(
321 SInt32 iValue
, SInt32 iMaximumValue
,
322 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
) ;
324 ControlRef m_sbHorizontal
;
325 SInt32 m_lastHorizontalValue
;
326 ControlRef m_sbVertical
;
327 SInt32 m_lastVerticalValue
;
332 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
334 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
335 EVT_ERASE_BACKGROUND( wxTextCtrl::OnEraseBackground
)
336 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
337 EVT_CHAR(wxTextCtrl::OnChar
)
338 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
339 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
340 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
341 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
342 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
343 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
344 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
346 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
348 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
349 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
350 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
351 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
352 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
353 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
354 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
358 void wxTextCtrl::Init()
364 m_privateContextMenu
= NULL
;
365 m_triggerOnSetValue
= true ;
368 wxTextCtrl::~wxTextCtrl()
370 delete m_privateContextMenu
;
373 bool wxTextCtrl::Create( wxWindow
*parent
,
379 const wxValidator
& validator
,
380 const wxString
& name
)
382 m_macIsUserPane
= false ;
385 if ( ! (style
& wxNO_BORDER
) )
386 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
388 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
391 if ( m_windowStyle
& wxTE_MULTILINE
)
393 // always turn on this style for multi-line controls
394 m_windowStyle
|= wxTE_PROCESS_ENTER
;
395 style
|= wxTE_PROCESS_ENTER
;
398 CreatePeer( str
, pos
, size
, style
);
400 MacPostControlCreate(pos
, size
) ;
402 // only now the embedding is correct and we can do a positioning update
404 MacSuperChangedPosition() ;
406 if ( m_windowStyle
& wxTE_READONLY
)
407 SetEditable( false ) ;
409 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
414 void wxTextCtrl::CreatePeer(
417 const wxSize
& size
, long style
)
419 bool forceMLTE
= false ;
421 #if wxUSE_SYSTEM_OPTIONS
422 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE
) == 1))
427 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
432 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
433 if ( UMAGetSystemVersion() >= 0x1030 && !forceMLTE
)
435 if ( m_windowStyle
& wxTE_MULTILINE
)
436 m_peer
= new wxMacMLTEHIViewControl( this , str
, pos
, size
, style
) ;
442 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
443 if ( !(m_windowStyle
& wxTE_MULTILINE
) && !forceMLTE
)
446 m_peer
= new wxMacUnicodeTextControl( this , str
, pos
, size
, style
) ;
451 // the horizontal single line scrolling bug that made us keep
452 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
454 m_peer
= new wxMacMLTEClassicControl( this , str
, pos
, size
, style
) ;
458 void wxTextCtrl::MacSuperChangedPosition()
460 wxWindow::MacSuperChangedPosition() ;
461 GetPeer()->SuperChangedPosition() ;
464 void wxTextCtrl::MacVisibilityChanged()
466 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
469 void wxTextCtrl::MacEnabledStateChanged()
473 void wxTextCtrl::MacCheckSpelling(bool check
)
475 GetPeer()->CheckSpelling(check
);
478 wxString
wxTextCtrl::GetValue() const
480 return GetPeer()->GetStringValue() ;
483 void wxTextCtrl::GetSelection(long* from
, long* to
) const
485 GetPeer()->GetSelection( from
, to
) ;
488 void wxTextCtrl::DoSetValue(const wxString
& str
, int flags
)
491 if ( GetValue() == str
)
494 GetPeer()->SetStringValue( str
) ;
496 if ( (flags
& SetValue_SendEvent
) && m_triggerOnSetValue
)
498 SendTextUpdatedEvent();
502 void wxTextCtrl::SetMaxLength(unsigned long len
)
507 bool wxTextCtrl::SetFont( const wxFont
& font
)
509 if ( !wxTextCtrlBase::SetFont( font
) )
512 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle() ) ;
517 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
519 GetPeer()->SetStyle( start
, end
, style
) ;
524 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
526 wxTextCtrlBase::SetDefaultStyle( style
) ;
527 SetStyle( kTXNUseCurrentSelection
, kTXNUseCurrentSelection
, GetDefaultStyle() ) ;
532 // Clipboard operations
534 void wxTextCtrl::Copy()
540 void wxTextCtrl::Cut()
546 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
547 event
.SetEventObject( this );
548 GetEventHandler()->ProcessEvent( event
);
552 void wxTextCtrl::Paste()
558 // TODO: eventually we should add setting the default style again
560 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
561 event
.SetEventObject( this );
562 GetEventHandler()->ProcessEvent( event
);
566 bool wxTextCtrl::CanCopy() const
568 // Can copy if there's a selection
570 GetSelection( &from
, &to
);
575 bool wxTextCtrl::CanCut() const
580 // Can cut if there's a selection
582 GetSelection( &from
, &to
);
587 bool wxTextCtrl::CanPaste() const
592 return GetPeer()->CanPaste() ;
595 void wxTextCtrl::SetEditable(bool editable
)
597 if ( editable
!= m_editable
)
599 m_editable
= editable
;
600 GetPeer()->SetEditable( editable
) ;
604 void wxTextCtrl::SetInsertionPoint(long pos
)
606 SetSelection( pos
, pos
) ;
609 void wxTextCtrl::SetInsertionPointEnd()
611 wxTextPos pos
= GetLastPosition();
612 SetInsertionPoint( pos
);
615 long wxTextCtrl::GetInsertionPoint() const
618 GetSelection( &begin
, &end
) ;
623 wxTextPos
wxTextCtrl::GetLastPosition() const
625 return GetPeer()->GetLastPosition() ;
628 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
630 GetPeer()->Replace( from
, to
, str
) ;
633 void wxTextCtrl::Remove(long from
, long to
)
635 GetPeer()->Remove( from
, to
) ;
638 void wxTextCtrl::SetSelection(long from
, long to
)
640 GetPeer()->SetSelection( from
, to
) ;
643 void wxTextCtrl::WriteText(const wxString
& str
)
645 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
646 if ( !wxIsMainThread() )
648 // unfortunately CW 8 is not able to correctly deduce the template types,
649 // so we have to instantiate explicitly
650 wxMacMPRemoteGUICall
<wxTextCtrl
,wxString
>( this , &wxTextCtrl::WriteText
, str
) ;
655 GetPeer()->WriteText( str
) ;
658 void wxTextCtrl::AppendText(const wxString
& text
)
660 SetInsertionPointEnd();
664 void wxTextCtrl::Clear()
669 bool wxTextCtrl::IsModified() const
674 bool wxTextCtrl::IsEditable() const
676 return IsEnabled() && m_editable
;
679 bool wxTextCtrl::AcceptsFocus() const
681 // we don't want focus if we can't be edited
682 return /*IsEditable() && */ wxControl::AcceptsFocus();
685 wxSize
wxTextCtrl::DoGetBestSize() const
689 // these are the numbers from the HIG:
690 // we reduce them by the borders first
693 switch ( m_windowVariant
)
695 case wxWINDOW_VARIANT_NORMAL
:
699 case wxWINDOW_VARIANT_SMALL
:
703 case wxWINDOW_VARIANT_MINI
:
712 // as the above numbers have some free space around the text
713 // we get 5 lines like this anyway
714 if ( m_windowStyle
& wxTE_MULTILINE
)
717 if ( !HasFlag(wxNO_BORDER
) )
720 return wxSize(wText
, hText
);
723 // ----------------------------------------------------------------------------
725 // ----------------------------------------------------------------------------
727 void wxTextCtrl::Undo()
733 void wxTextCtrl::Redo()
739 bool wxTextCtrl::CanUndo() const
744 return GetPeer()->CanUndo() ;
747 bool wxTextCtrl::CanRedo() const
752 return GetPeer()->CanRedo() ;
755 void wxTextCtrl::MarkDirty()
760 void wxTextCtrl::DiscardEdits()
765 int wxTextCtrl::GetNumberOfLines() const
767 return GetPeer()->GetNumberOfLines() ;
770 long wxTextCtrl::XYToPosition(long x
, long y
) const
772 return GetPeer()->XYToPosition( x
, y
) ;
775 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
777 return GetPeer()->PositionToXY( pos
, x
, y
) ;
780 void wxTextCtrl::ShowPosition(long pos
)
782 return GetPeer()->ShowPosition(pos
) ;
785 int wxTextCtrl::GetLineLength(long lineNo
) const
787 return GetPeer()->GetLineLength(lineNo
) ;
790 wxString
wxTextCtrl::GetLineText(long lineNo
) const
792 return GetPeer()->GetLineText(lineNo
) ;
795 void wxTextCtrl::Command(wxCommandEvent
& event
)
797 SetValue(event
.GetString());
798 ProcessCommand(event
);
801 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
803 // By default, load the first file into the text window.
804 if (event
.GetNumberOfFiles() > 0)
805 LoadFile( event
.GetFiles()[0] );
808 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
810 // all erasing should be done by the real mac control implementation
811 // while this is true for MLTE under classic, the HITextView is somehow
812 // transparent but background erase is not working correctly, so intercept
813 // things while we can...
817 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
819 int key
= event
.GetKeyCode() ;
820 bool eat_key
= false ;
822 if ( key
== 'a' && event
.MetaDown() )
829 if ( key
== 'c' && event
.MetaDown() )
837 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
838 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
839 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
846 // Check if we have reached the max # of chars (if it is set), but still
847 // allow navigation and deletion
848 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
849 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&&
850 key
!= WXK_BACK
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) )
853 // eat it, we don't want to add more than allowed # of characters
855 // TODO: generate EVT_TEXT_MAXLEN()
859 // assume that any key not processed yet is going to modify the control
862 if ( key
== 'v' && event
.MetaDown() )
870 if ( key
== 'x' && event
.MetaDown() )
881 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
883 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
884 event
.SetEventObject( this );
885 event
.SetString( GetValue() );
886 if ( GetEventHandler()->ProcessEvent(event
) )
890 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
892 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
893 if ( tlw
&& tlw
->GetDefaultItem() )
895 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
896 if ( def
&& def
->IsEnabled() )
898 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
899 event
.SetEventObject(def
);
906 // this will make wxWidgets eat the ENTER key so that
907 // we actually prevent line wrapping in a single line text control
913 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
916 if (!event
.ShiftDown())
917 flags
|= wxNavigationKeyEvent::IsForward
;
918 if (event
.ControlDown())
919 flags
|= wxNavigationKeyEvent::WinChange
;
926 // This is necessary (don't know why);
927 // otherwise the tab will not be inserted.
928 WriteText(wxT("\t"));
939 // perform keystroke handling
943 if ( ( key
>= 0x20 && key
< WXK_START
) ||
944 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
949 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
950 event1
.SetEventObject( this );
951 wxPostEvent( GetEventHandler(), event1
);
955 // ----------------------------------------------------------------------------
956 // standard handlers for standard edit menu events
957 // ----------------------------------------------------------------------------
959 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
964 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
969 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
974 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
979 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
984 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
988 GetSelection( &from
, &to
);
989 if (from
!= -1 && to
!= -1)
993 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
995 SetSelection(-1, -1);
998 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1000 event
.Enable( CanCut() );
1003 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1005 event
.Enable( CanCopy() );
1008 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1010 event
.Enable( CanPaste() );
1013 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1015 event
.Enable( CanUndo() );
1018 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1020 event
.Enable( CanRedo() );
1023 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1027 GetSelection( &from
, &to
);
1028 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
1031 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1033 event
.Enable(GetLastPosition() > 0);
1036 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
1038 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1040 if ( GetPeer()->HasOwnContextMenu() )
1046 if (m_privateContextMenu
== NULL
)
1048 m_privateContextMenu
= new wxMenu
;
1049 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1050 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1051 m_privateContextMenu
->AppendSeparator();
1052 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1053 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1054 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1055 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1056 m_privateContextMenu
->AppendSeparator();
1057 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1060 if (m_privateContextMenu
!= NULL
)
1061 PopupMenu(m_privateContextMenu
);
1064 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
1066 if ( !GetPeer()->SetupCursor( pt
) )
1067 return wxWindow::MacSetupCursor( pt
) ;
1072 #if !TARGET_API_MAC_OSX
1074 // user pane implementation
1076 void wxTextCtrl::MacControlUserPaneDrawProc(wxInt16 part
)
1078 GetPeer()->MacControlUserPaneDrawProc( part
) ;
1081 wxInt16
wxTextCtrl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
1083 return GetPeer()->MacControlUserPaneHitTestProc( x
, y
) ;
1086 wxInt16
wxTextCtrl::MacControlUserPaneTrackingProc(wxInt16 x
, wxInt16 y
, void* actionProc
)
1088 return GetPeer()->MacControlUserPaneTrackingProc( x
, y
, actionProc
) ;
1091 void wxTextCtrl::MacControlUserPaneIdleProc()
1093 GetPeer()->MacControlUserPaneIdleProc( ) ;
1096 wxInt16
wxTextCtrl::MacControlUserPaneKeyDownProc(wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
1098 return GetPeer()->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
1101 void wxTextCtrl::MacControlUserPaneActivateProc(bool activating
)
1103 GetPeer()->MacControlUserPaneActivateProc( activating
) ;
1106 wxInt16
wxTextCtrl::MacControlUserPaneFocusProc(wxInt16 action
)
1108 return GetPeer()->MacControlUserPaneFocusProc( action
) ;
1111 void wxTextCtrl::MacControlUserPaneBackgroundProc(void* info
)
1113 GetPeer()->MacControlUserPaneBackgroundProc( info
) ;
1118 // ----------------------------------------------------------------------------
1119 // implementation base class
1120 // ----------------------------------------------------------------------------
1122 wxMacTextControl::wxMacTextControl(wxTextCtrl
* peer
) :
1123 wxMacControl( peer
)
1127 wxMacTextControl::~wxMacTextControl()
1131 void wxMacTextControl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
1135 void wxMacTextControl::Copy()
1139 void wxMacTextControl::Cut()
1143 void wxMacTextControl::Paste()
1147 bool wxMacTextControl::CanPaste() const
1152 void wxMacTextControl::SetEditable(bool editable
)
1156 wxTextPos
wxMacTextControl::GetLastPosition() const
1158 return GetStringValue().length() ;
1161 void wxMacTextControl::Replace( long from
, long to
, const wxString
&val
)
1163 SetSelection( from
, to
) ;
1167 void wxMacTextControl::Remove( long from
, long to
)
1169 SetSelection( from
, to
) ;
1170 WriteText( wxEmptyString
) ;
1173 void wxMacTextControl::Clear()
1175 SetStringValue( wxEmptyString
) ;
1178 bool wxMacTextControl::CanUndo() const
1183 void wxMacTextControl::Undo()
1187 bool wxMacTextControl::CanRedo() const
1192 void wxMacTextControl::Redo()
1196 long wxMacTextControl::XYToPosition(long x
, long y
) const
1201 bool wxMacTextControl::PositionToXY(long pos
, long *x
, long *y
) const
1206 void wxMacTextControl::ShowPosition( long WXUNUSED(pos
) )
1210 int wxMacTextControl::GetNumberOfLines() const
1212 ItemCount lines
= 0 ;
1213 wxString content
= GetStringValue() ;
1216 for (size_t i
= 0; i
< content
.length() ; i
++)
1218 if (content
[i
] == '\r')
1225 wxString
wxMacTextControl::GetLineText(long lineNo
) const
1227 // TODO: change this if possible to reflect real lines
1228 wxString content
= GetStringValue() ;
1232 for (size_t i
= 0; i
< content
.length() ; i
++)
1234 if (count
== lineNo
)
1236 // Add chars in line then
1239 for (size_t j
= i
; j
< content
.length(); j
++)
1241 if (content
[j
] == '\n')
1250 if (content
[i
] == '\n')
1254 return wxEmptyString
;
1257 int wxMacTextControl::GetLineLength(long lineNo
) const
1259 // TODO: change this if possible to reflect real lines
1260 wxString content
= GetStringValue() ;
1264 for (size_t i
= 0; i
< content
.length() ; i
++)
1266 if (count
== lineNo
)
1268 // Count chars in line then
1270 for (size_t j
= i
; j
< content
.length(); j
++)
1273 if (content
[j
] == '\n')
1280 if (content
[i
] == '\n')
1287 // ----------------------------------------------------------------------------
1288 // standard unicode control implementation
1289 // ----------------------------------------------------------------------------
1291 #if TARGET_API_MAC_OSX
1293 // the current unicode textcontrol implementation has a bug : only if the control
1294 // is currently having the focus, the selection can be retrieved by the corresponding
1295 // data tag. So we have a mirroring using a member variable
1296 // TODO : build event table using virtual member functions for wxMacControl
1298 static const EventTypeSpec unicodeTextControlEventList
[] =
1300 { kEventClassControl
, kEventControlSetFocusPart
} ,
1303 static pascal OSStatus
wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1305 OSStatus result
= eventNotHandledErr
;
1306 wxMacUnicodeTextControl
* focus
= (wxMacUnicodeTextControl
*) data
;
1307 wxMacCarbonEvent
cEvent( event
) ;
1309 switch ( GetEventKind( event
) )
1311 case kEventControlSetFocusPart
:
1313 ControlPartCode controlPart
= cEvent
.GetParameter
<ControlPartCode
>(kEventParamControlPart
, typeControlPartCode
);
1314 if ( controlPart
== kControlFocusNoPart
)
1316 // about to loose focus -> store selection to field
1317 focus
->GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1319 result
= CallNextEventHandler(handler
,event
) ;
1320 if ( controlPart
!= kControlFocusNoPart
)
1322 // about to gain focus -> set selection from field
1323 focus
->SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1334 static pascal OSStatus
wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1336 OSStatus result
= eventNotHandledErr
;
1338 switch ( GetEventClass( event
) )
1340 case kEventClassControl
:
1341 result
= wxMacUnicodeTextControlControlEventHandler( handler
, event
, data
) ;
1350 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler
)
1352 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
) : wxMacTextControl( wxPeer
)
1356 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
,
1357 const wxString
& str
,
1359 const wxSize
& size
, long style
)
1360 : wxMacTextControl( wxPeer
)
1362 Create( wxPeer
, str
, pos
, size
, style
);
1365 bool wxMacUnicodeTextControl::Create( wxTextCtrl
*wxPeer
,
1366 const wxString
& str
,
1368 const wxSize
& size
, long style
)
1370 m_font
= wxPeer
->GetFont() ;
1371 m_windowStyle
= style
;
1372 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
1374 wxMacConvertNewlines10To13( &st
) ;
1375 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding()) ;
1376 CFStringRef cfr
= cf
;
1378 m_valueTag
= kControlEditTextCFStringTag
;
1379 CreateControl( wxPeer
, &bounds
, cfr
);
1381 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1382 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextSingleLineTag
, true ) ;
1384 InstallControlEventHandler( m_controlRef
, GetwxMacUnicodeTextControlEventHandlerUPP(),
1385 GetEventTypeCount(unicodeTextControlEventList
), unicodeTextControlEventList
, this,
1386 &m_focusHandlerRef
);
1391 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1393 ::RemoveEventHandler( m_focusHandlerRef
);
1396 void wxMacUnicodeTextControl::VisibilityChanged(bool shown
)
1398 if ( !(m_windowStyle
& wxTE_MULTILINE
) && shown
)
1400 // work around a refresh issue insofar as not always the entire content is shown,
1401 // even if this would be possible
1402 ControlEditTextSelectionRec sel
;
1403 CFStringRef value
= NULL
;
1405 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1406 verify_noerr( GetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1407 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1408 verify_noerr( SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1410 CFRelease( value
) ;
1414 wxString
wxMacUnicodeTextControl::GetStringValue() const
1417 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1420 wxMacCFStringHolder
cf(value
) ;
1421 result
= cf
.AsString() ;
1425 wxMacConvertNewlines13To10( &result
) ;
1427 wxMacConvertNewlines10To13( &result
) ;
1433 void wxMacUnicodeTextControl::SetStringValue( const wxString
&str
)
1436 wxMacConvertNewlines10To13( &st
) ;
1437 wxMacCFStringHolder
cf( st
, m_font
.GetEncoding() ) ;
1438 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, cf
) ) ;
1441 void wxMacUnicodeTextControl::CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef cfr
)
1443 Boolean isPassword
= ( m_windowStyle
& wxTE_PASSWORD
) != 0 ;
1446 m_valueTag
= kControlEditTextPasswordCFStringTag
;
1448 OSStatus err
= CreateEditUnicodeTextControl(
1449 MAC_WXHWND(peer
->MacGetTopLevelWindowRef()), bounds
, cfr
,
1450 isPassword
, NULL
, &m_controlRef
) ;
1451 verify_noerr( err
);
1454 void wxMacUnicodeTextControl::Copy()
1456 SendHICommand( kHICommandCopy
) ;
1459 void wxMacUnicodeTextControl::Cut()
1461 SendHICommand( kHICommandCut
) ;
1464 void wxMacUnicodeTextControl::Paste()
1466 SendHICommand( kHICommandPaste
) ;
1469 bool wxMacUnicodeTextControl::CanPaste() const
1474 void wxMacUnicodeTextControl::SetEditable(bool editable
)
1476 #if 0 // leads to problem because text cannot be selected anymore
1477 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextLockedTag
, (Boolean
) !editable
) ;
1481 void wxMacUnicodeTextControl::GetSelection( long* from
, long* to
) const
1483 ControlEditTextSelectionRec sel
;
1485 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ) ;
1490 *from
= sel
.selStart
;
1495 void wxMacUnicodeTextControl::SetSelection( long from
, long to
)
1497 ControlEditTextSelectionRec sel
;
1499 int textLength
= 0 ;
1500 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1503 wxMacCFStringHolder
cf(value
) ;
1504 textLength
= cf
.AsString().length() ;
1507 if ((from
== -1) && (to
== -1))
1514 from
= wxMin(textLength
,wxMax(from
,0)) ;
1515 to
= wxMax(0,wxMin(textLength
,to
)) ;
1518 sel
.selStart
= from
;
1521 SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ;
1526 void wxMacUnicodeTextControl::WriteText( const wxString
& str
)
1529 wxMacConvertNewlines10To13( &st
) ;
1531 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
1534 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding() ) ;
1535 CFStringRef value
= cf
;
1536 SetData
<CFStringRef
>( 0, kControlEditTextInsertCFStringRefTag
, &value
);
1541 wxString val
= GetStringValue() ;
1543 GetSelection( &start
, &end
) ;
1544 val
.Remove( start
, end
- start
) ;
1545 val
.insert( start
, str
) ;
1546 SetStringValue( val
) ;
1547 SetSelection( start
+ str
.length() , start
+ str
.length() ) ;
1553 // ----------------------------------------------------------------------------
1554 // MLTE control implementation (common part)
1555 // ----------------------------------------------------------------------------
1557 // if MTLE is read only, no changes at all are allowed, not even from
1558 // procedural API, in order to allow changes via API all the same we must undo
1559 // the readonly status while we are executing, this class helps to do so
1561 class wxMacEditHelper
1564 wxMacEditHelper( TXNObject txn
)
1566 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1568 TXNGetTXNObjectControls( m_txn
, 1 , tag
, m_data
) ;
1569 if ( m_data
[0].uValue
== kTXNReadOnly
)
1571 TXNControlData data
[] = { { kTXNReadWrite
} } ;
1572 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, data
) ;
1578 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1579 if ( m_data
[0].uValue
== kTXNReadOnly
)
1580 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, m_data
) ;
1585 TXNControlData m_data
[1] ;
1588 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl
*peer
)
1589 : wxMacTextControl( peer
)
1591 SetNeedsFocusRect( true ) ;
1594 wxString
wxMacMLTEControl::GetStringValue() const
1598 Size actualSize
= 0;
1603 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNUnicodeTextData
);
1612 actualSize
= GetHandleSize( theText
) / sizeof(UniChar
) ;
1613 if ( actualSize
> 0 )
1615 wxChar
*ptr
= NULL
;
1617 #if SIZEOF_WCHAR_T == 2
1618 ptr
= new wxChar
[actualSize
+ 1] ;
1619 wxStrncpy( ptr
, (wxChar
*)(*theText
) , actualSize
) ;
1621 SetHandleSize( theText
, (actualSize
+ 1) * sizeof(UniChar
) ) ;
1623 (((UniChar
*)*theText
)[actualSize
]) = 0 ;
1624 wxMBConvUTF16 converter
;
1625 size_t noChars
= converter
.MB2WC( NULL
, (const char*)*theText
, 0 ) ;
1626 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Unable to count the number of characters in this string!") );
1627 ptr
= new wxChar
[noChars
+ 1] ;
1629 noChars
= converter
.MB2WC( ptr
, (const char*)*theText
, noChars
+ 1 ) ;
1630 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Conversion of string failed!") );
1632 HUnlock( theText
) ;
1635 ptr
[actualSize
] = 0 ;
1636 result
= wxString( ptr
) ;
1640 DisposeHandle( theText
) ;
1644 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1653 actualSize
= GetHandleSize( theText
) ;
1654 if ( actualSize
> 0 )
1657 result
= wxString( *theText
, wxConvLocal
, actualSize
) ;
1658 HUnlock( theText
) ;
1661 DisposeHandle( theText
) ;
1667 wxMacConvertNewlines13To10( &result
) ;
1669 wxMacConvertNewlines10To13( &result
) ;
1675 void wxMacMLTEControl::SetStringValue( const wxString
&str
)
1678 wxMacConvertNewlines10To13( &st
);
1682 wxMacWindowClipper
c( m_peer
) ;
1686 wxMacEditHelper
help( m_txn
);
1687 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
);
1690 TXNSetSelection( m_txn
, 0, 0 );
1691 TXNShowSelection( m_txn
, kTXNShowStart
);
1695 TXNFrameOptions
wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle
)
1697 TXNFrameOptions frameOptions
= kTXNDontDrawCaretWhenInactiveMask
;
1699 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
1700 frameOptions
|= kTXNDoFontSubstitutionMask
;
1703 if ( ! (wxStyle
& wxTE_NOHIDESEL
) )
1704 frameOptions
|= kTXNDontDrawSelectionWhenInactiveMask
;
1706 if ( wxStyle
& (wxHSCROLL
| wxTE_DONTWRAP
) )
1707 frameOptions
|= kTXNWantHScrollBarMask
;
1709 if ( wxStyle
& wxTE_MULTILINE
)
1711 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
1713 if ( !(wxStyle
& wxTE_NO_VSCROLL
) )
1715 frameOptions
|= kTXNWantVScrollBarMask
;
1717 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1718 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1719 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1720 // if ( frameOptions & kTXNWantHScrollBarMask )
1721 // frameOptions |= kTXNDrawGrowIconMask ;
1726 frameOptions
|= kTXNSingleLineOnlyMask
;
1729 return frameOptions
;
1732 void wxMacMLTEControl::AdjustCreationAttributes( const wxColour
&background
, bool visible
)
1734 TXNControlTag iControlTags
[] =
1736 kTXNDoFontSubstitution
,
1737 kTXNWordWrapStateTag
,
1739 TXNControlData iControlData
[] =
1745 int toptag
= WXSIZEOF( iControlTags
) ;
1747 if ( m_windowStyle
& wxTE_MULTILINE
)
1749 iControlData
[1].uValue
=
1750 (m_windowStyle
& wxTE_DONTWRAP
)
1755 OSStatus err
= TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) ;
1756 verify_noerr( err
);
1758 // setting the default font:
1759 // under 10.2 this causes a visible caret, therefore we avoid it
1761 if ( UMAGetSystemVersion() >= 0x1030 )
1767 GetThemeFont( kThemeSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
1769 TXNTypeAttributes typeAttr
[] =
1771 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
1772 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
1773 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
1776 err
= TXNSetTypeAttributes(
1777 m_txn
, sizeof(typeAttr
) / sizeof(TXNTypeAttributes
),
1778 typeAttr
, kTXNStartOffset
, kTXNEndOffset
);
1779 verify_noerr( err
);
1782 if ( m_windowStyle
& wxTE_PASSWORD
)
1784 UniChar c
= 0x00A5 ;
1785 err
= TXNEchoMode( m_txn
, c
, 0 , true );
1786 verify_noerr( err
);
1789 TXNBackground tback
;
1790 tback
.bgType
= kTXNBackgroundTypeRGB
;
1791 tback
.bg
.color
= MAC_WXCOLORREF( background
.GetPixel() );
1792 TXNSetBackground( m_txn
, &tback
);
1794 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1795 if ( UMAGetSystemVersion() >= 0x1040 )
1797 TXNCommandEventSupportOptions options
;
1798 if ( TXNGetCommandEventSupport( m_txn
, &options
) == noErr
)
1801 kTXNSupportEditCommandProcessing
1802 | kTXNSupportEditCommandUpdating
1803 | kTXNSupportSpellCheckCommandProcessing
1804 | kTXNSupportSpellCheckCommandUpdating
1805 | kTXNSupportFontCommandProcessing
1806 | kTXNSupportFontCommandUpdating
;
1808 TXNSetCommandEventSupport( m_txn
, options
) ;
1814 void wxMacMLTEControl::SetBackground( const wxBrush
&brush
)
1816 // currently only solid background are supported
1817 TXNBackground tback
;
1819 tback
.bgType
= kTXNBackgroundTypeRGB
;
1820 tback
.bg
.color
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() );
1821 TXNSetBackground( m_txn
, &tback
);
1824 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
)
1826 TXNTypeAttributes typeAttr
[4] ;
1830 if ( style
.HasFont() )
1832 const wxFont
&font
= style
.GetFont() ;
1834 #if 0 // old version
1835 Str255 fontName
= "\pMonaco" ;
1836 SInt16 fontSize
= 12 ;
1837 Style fontStyle
= normal
;
1838 wxMacStringToPascal( font
.GetFaceName() , fontName
) ;
1839 fontSize
= font
.GetPointSize() ;
1840 if ( font
.GetUnderlined() )
1841 fontStyle
|= underline
;
1842 if ( font
.GetWeight() == wxBOLD
)
1844 if ( font
.GetStyle() == wxITALIC
)
1845 fontStyle
|= italic
;
1847 typeAttr
[attrCount
].tag
= kTXNQDFontNameAttribute
;
1848 typeAttr
[attrCount
].size
= kTXNQDFontNameAttributeSize
;
1849 typeAttr
[attrCount
].data
.dataPtr
= (void*)fontName
;
1852 typeAttr
[attrCount
].tag
= kTXNQDFontSizeAttribute
;
1853 typeAttr
[attrCount
].size
= kTXNFontSizeAttributeSize
;
1854 typeAttr
[attrCount
].data
.dataValue
= (fontSize
<< 16) ;
1857 typeAttr
[attrCount
].tag
= kTXNQDFontStyleAttribute
;
1858 typeAttr
[attrCount
].size
= kTXNQDFontStyleAttributeSize
;
1859 typeAttr
[attrCount
].data
.dataValue
= fontStyle
;
1862 typeAttr
[attrCount
].tag
= kTXNATSUIStyle
;
1863 typeAttr
[attrCount
].size
= kTXNATSUIStyleSize
;
1864 typeAttr
[attrCount
].data
.dataPtr
= font
.MacGetATSUStyle() ;
1869 if ( style
.HasTextColour() )
1871 color
= MAC_WXCOLORREF(style
.GetTextColour().GetPixel()) ;
1873 typeAttr
[attrCount
].tag
= kTXNQDFontColorAttribute
;
1874 typeAttr
[attrCount
].size
= kTXNQDFontColorAttributeSize
;
1875 typeAttr
[attrCount
].data
.dataPtr
= (void*) &color
;
1879 if ( attrCount
> 0 )
1881 verify_noerr( TXNSetTypeAttributes( m_txn
, attrCount
, typeAttr
, from
, to
) );
1882 // unfortunately the relayout is not automatic
1883 TXNRecalcTextLayout( m_txn
);
1887 void wxMacMLTEControl::SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
)
1889 wxMacEditHelper
help( m_txn
) ;
1890 TXNSetAttribute( wxTextAttr( foreground
, wxNullColour
, font
), kTXNStartOffset
, kTXNEndOffset
) ;
1893 void wxMacMLTEControl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1895 wxMacEditHelper
help( m_txn
) ;
1896 TXNSetAttribute( style
, start
, end
) ;
1899 void wxMacMLTEControl::Copy()
1901 ClearCurrentScrap();
1903 TXNConvertToPublicScrap();
1906 void wxMacMLTEControl::Cut()
1908 ClearCurrentScrap();
1910 TXNConvertToPublicScrap();
1913 void wxMacMLTEControl::Paste()
1915 TXNConvertFromPublicScrap();
1919 bool wxMacMLTEControl::CanPaste() const
1921 return TXNIsScrapPastable() ;
1924 void wxMacMLTEControl::SetEditable(bool editable
)
1926 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1927 TXNControlData data
[] = { { editable
? kTXNReadWrite
: kTXNReadOnly
} } ;
1928 TXNSetTXNObjectControls( m_txn
, false, WXSIZEOF(tag
), tag
, data
) ;
1931 wxTextPos
wxMacMLTEControl::GetLastPosition() const
1933 wxTextPos actualsize
= 0 ;
1936 OSErr err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1941 actualsize
= GetHandleSize( theText
) ;
1942 DisposeHandle( theText
) ;
1952 void wxMacMLTEControl::Replace( long from
, long to
, const wxString
&str
)
1954 wxString value
= str
;
1955 wxMacConvertNewlines10To13( &value
) ;
1957 wxMacEditHelper
help( m_txn
) ;
1959 wxMacWindowClipper
c( m_peer
) ;
1962 TXNSetSelection( m_txn
, from
, to
) ;
1964 SetTXNData( value
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1967 void wxMacMLTEControl::Remove( long from
, long to
)
1970 wxMacWindowClipper
c( m_peer
) ;
1972 wxMacEditHelper
help( m_txn
) ;
1973 TXNSetSelection( m_txn
, from
, to
) ;
1977 void wxMacMLTEControl::GetSelection( long* from
, long* to
) const
1979 TXNGetSelection( m_txn
, (TXNOffset
*) from
, (TXNOffset
*) to
) ;
1982 void wxMacMLTEControl::SetSelection( long from
, long to
)
1985 wxMacWindowClipper
c( m_peer
) ;
1988 // change the selection
1989 if ((from
== -1) && (to
== -1))
1990 TXNSelectAll( m_txn
);
1992 TXNSetSelection( m_txn
, from
, to
);
1994 TXNShowSelection( m_txn
, kTXNShowStart
);
1997 void wxMacMLTEControl::WriteText( const wxString
& str
)
2000 wxMacConvertNewlines10To13( &st
) ;
2002 long start
, end
, dummy
;
2004 GetSelection( &start
, &dummy
) ;
2006 wxMacWindowClipper
c( m_peer
) ;
2010 wxMacEditHelper
helper( m_txn
) ;
2011 SetTXNData( st
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
2014 GetSelection( &dummy
, &end
) ;
2016 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2019 void wxMacMLTEControl::Clear()
2022 wxMacWindowClipper
c( m_peer
) ;
2024 wxMacEditHelper
st( m_txn
) ;
2025 TXNSetSelection( m_txn
, kTXNStartOffset
, kTXNEndOffset
) ;
2029 bool wxMacMLTEControl::CanUndo() const
2031 return TXNCanUndo( m_txn
, NULL
) ;
2034 void wxMacMLTEControl::Undo()
2039 bool wxMacMLTEControl::CanRedo() const
2041 return TXNCanRedo( m_txn
, NULL
) ;
2044 void wxMacMLTEControl::Redo()
2049 int wxMacMLTEControl::GetNumberOfLines() const
2051 ItemCount lines
= 0 ;
2052 TXNGetLineCount( m_txn
, &lines
) ;
2057 long wxMacMLTEControl::XYToPosition(long x
, long y
) const
2062 // TODO: find a better implementation : while we can get the
2063 // line metrics of a certain line, we don't get its starting
2064 // position, so it would probably be rather a binary search
2065 // for the start position
2066 long xpos
= 0, ypos
= 0 ;
2067 int lastHeight
= 0 ;
2070 lastpos
= GetLastPosition() ;
2071 for ( n
= 0 ; n
<= (ItemCount
) lastpos
; ++n
)
2073 if ( y
== ypos
&& x
== xpos
)
2076 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2078 if ( curpt
.v
> lastHeight
)
2084 lastHeight
= curpt
.v
;
2093 bool wxMacMLTEControl::PositionToXY( long pos
, long *x
, long *y
) const
2103 lastpos
= GetLastPosition() ;
2104 if ( pos
<= lastpos
)
2106 // TODO: find a better implementation - while we can get the
2107 // line metrics of a certain line, we don't get its starting
2108 // position, so it would probably be rather a binary search
2109 // for the start position
2110 long xpos
= 0, ypos
= 0 ;
2111 int lastHeight
= 0 ;
2114 for ( n
= 0 ; n
<= (ItemCount
) pos
; ++n
)
2116 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2118 if ( curpt
.v
> lastHeight
)
2124 lastHeight
= curpt
.v
;
2139 void wxMacMLTEControl::ShowPosition( long pos
)
2141 Point current
, desired
;
2142 TXNOffset selstart
, selend
;
2144 TXNGetSelection( m_txn
, &selstart
, &selend
);
2145 TXNOffsetToPoint( m_txn
, selstart
, ¤t
);
2146 TXNOffsetToPoint( m_txn
, pos
, &desired
);
2148 // TODO: use HIPoints for 10.3 and above
2150 OSErr theErr
= noErr
;
2151 long dv
= desired
.v
- current
.v
;
2152 long dh
= desired
.h
- current
.h
;
2153 TXNShowSelection( m_txn
, kTXNShowStart
) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2154 theErr
= TXNScroll( m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
, &dv
, &dh
);
2156 // there will be an error returned for classic MLTE implementation when the control is
2157 // invisible, but HITextView works correctly, so we don't assert that one
2158 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2161 void wxMacMLTEControl::SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
)
2164 #if SIZEOF_WCHAR_T == 2
2165 size_t len
= st
.length() ;
2166 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)st
.wc_str(), len
* 2, start
, end
);
2168 wxMBConvUTF16 converter
;
2169 ByteCount byteBufferLen
= converter
.WC2MB( NULL
, st
.wc_str(), 0 ) ;
2170 UniChar
*unibuf
= (UniChar
*)malloc( byteBufferLen
) ;
2171 converter
.WC2MB( (char*)unibuf
, st
.wc_str(), byteBufferLen
) ;
2172 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)unibuf
, byteBufferLen
, start
, end
) ;
2176 wxCharBuffer text
= st
.mb_str( wxConvLocal
) ;
2177 TXNSetData( m_txn
, kTXNTextData
, (void*)text
.data(), strlen( text
), start
, end
) ;
2181 wxString
wxMacMLTEControl::GetLineText(long lineNo
) const
2185 if ( lineNo
< GetNumberOfLines() )
2188 Fixed lineWidth
, lineHeight
, currentHeight
;
2191 // get the first possible position in the control
2192 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2194 // Iterate through the lines until we reach the one we want,
2195 // adding to our current y pixel point position
2198 while (ypos
< lineNo
)
2200 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2201 currentHeight
+= lineHeight
;
2204 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2205 TXNOffset theOffset
;
2206 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2208 wxString content
= GetStringValue() ;
2209 Point currentPoint
= thePoint
;
2210 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2212 line
+= content
[theOffset
];
2213 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2220 int wxMacMLTEControl::GetLineLength(long lineNo
) const
2224 if ( lineNo
< GetNumberOfLines() )
2227 Fixed lineWidth
, lineHeight
, currentHeight
;
2230 // get the first possible position in the control
2231 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2233 // Iterate through the lines until we reach the one we want,
2234 // adding to our current y pixel point position
2237 while (ypos
< lineNo
)
2239 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2240 currentHeight
+= lineHeight
;
2243 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2244 TXNOffset theOffset
;
2245 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2247 wxString content
= GetStringValue() ;
2248 Point currentPoint
= thePoint
;
2249 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2252 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2259 #if MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5
2261 // ----------------------------------------------------------------------------
2262 // MLTE control implementation (classic part)
2263 // ----------------------------------------------------------------------------
2265 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2266 // has to live on. We have different problems coming from outdated implementations on the
2267 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2268 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2269 // no way out, therefore we are using our own implementation and our own scrollbars ....
2271 #ifdef __WXMAC_OSX__
2273 TXNScrollInfoUPP gTXNScrollInfoProc
= NULL
;
2274 ControlActionUPP gTXNScrollActionProc
= NULL
;
2276 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2277 SInt32 iValue
, SInt32 iMaximumValue
,
2278 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
)
2280 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) iRefCon
;
2281 SInt32 value
= wxMax( iValue
, 0 ) ;
2282 SInt32 maximum
= wxMax( iMaximumValue
, 0 ) ;
2284 if ( iScrollBarOrientation
== kTXNHorizontal
)
2286 if ( mlte
->m_sbHorizontal
)
2288 SetControl32BitValue( mlte
->m_sbHorizontal
, value
) ;
2289 SetControl32BitMaximum( mlte
->m_sbHorizontal
, maximum
) ;
2290 mlte
->m_lastHorizontalValue
= value
;
2293 else if ( iScrollBarOrientation
== kTXNVertical
)
2295 if ( mlte
->m_sbVertical
)
2297 SetControl32BitValue( mlte
->m_sbVertical
, value
) ;
2298 SetControl32BitMaximum( mlte
->m_sbVertical
, maximum
) ;
2299 mlte
->m_lastVerticalValue
= value
;
2304 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
)
2306 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) GetControlReference( controlRef
) ;
2310 if ( controlRef
!= mlte
->m_sbVertical
&& controlRef
!= mlte
->m_sbHorizontal
)
2314 bool isHorizontal
= ( controlRef
== mlte
->m_sbHorizontal
) ;
2316 SInt32 minimum
= 0 ;
2317 SInt32 maximum
= GetControl32BitMaximum( controlRef
) ;
2318 SInt32 value
= GetControl32BitValue( controlRef
) ;
2323 case kControlDownButtonPart
:
2327 case kControlUpButtonPart
:
2331 case kControlPageDownPart
:
2332 delta
= GetControlViewSize( controlRef
) ;
2335 case kControlPageUpPart
:
2336 delta
= -GetControlViewSize( controlRef
) ;
2339 case kControlIndicatorPart
:
2340 delta
= value
- (isHorizontal
? mlte
->m_lastHorizontalValue
: mlte
->m_lastVerticalValue
) ;
2349 SInt32 newValue
= value
;
2351 if ( partCode
!= kControlIndicatorPart
)
2353 if ( value
+ delta
< minimum
)
2354 delta
= minimum
- value
;
2355 if ( value
+ delta
> maximum
)
2356 delta
= maximum
- value
;
2358 SetControl32BitValue( controlRef
, value
+ delta
) ;
2359 newValue
= value
+ delta
;
2362 SInt32 verticalDelta
= isHorizontal
? 0 : delta
;
2363 SInt32 horizontalDelta
= isHorizontal
? delta
: 0 ;
2366 mlte
->m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
,
2367 &verticalDelta
, &horizontalDelta
);
2368 verify_noerr( err
);
2371 mlte
->m_lastHorizontalValue
= newValue
;
2373 mlte
->m_lastVerticalValue
= newValue
;
2378 // make correct activations
2379 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive
)
2381 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference(m_controlRef
);
2383 wxMacWindowClipper
clipper( textctrl
) ;
2384 TXNActivate( m_txn
, m_txnFrameID
, setActive
);
2386 ControlRef controlFocus
= 0 ;
2387 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2388 if ( controlFocus
== m_controlRef
)
2389 TXNFocus( m_txn
, setActive
);
2392 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus
)
2394 TXNFocus( m_txn
, setFocus
);
2397 // guards against inappropriate redraw (hidden objects drawing onto window)
2399 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis
)
2401 ControlRef controlFocus
= 0 ;
2402 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2404 if ( !vis
&& (controlFocus
== m_controlRef
) )
2405 SetKeyboardFocus( m_txnWindow
, m_controlRef
, kControlFocusNoPart
) ;
2407 TXNControlTag iControlTags
[1] = { kTXNVisibilityTag
};
2408 TXNControlData iControlData
[1] = { { (UInt32
)false } };
2410 verify_noerr( TXNGetTXNObjectControls( m_txn
, 1, iControlTags
, iControlData
) ) ;
2412 if ( iControlData
[0].uValue
!= vis
)
2414 iControlData
[0].uValue
= vis
;
2415 verify_noerr( TXNSetTXNObjectControls( m_txn
, false , 1, iControlTags
, iControlData
) ) ;
2418 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2419 // if we run into further problems we might set the FrameBounds to an empty rect here
2422 // make sure that the TXNObject is at the right position
2424 void wxMacMLTEClassicControl::MacUpdatePosition()
2426 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2427 if ( textctrl
== NULL
)
2431 UMAGetControlBoundsInWindowCoords( m_controlRef
, &bounds
);
2433 wxRect visRect
= textctrl
->MacGetClippedClientRect() ;
2434 Rect visBounds
= { visRect
.y
, visRect
.x
, visRect
.y
+ visRect
.height
, visRect
.x
+ visRect
.width
} ;
2437 textctrl
->MacWindowToRootWindow( &x
, &y
) ;
2438 OffsetRect( &visBounds
, x
, y
) ;
2440 if ( !EqualRect( &bounds
, &m_txnControlBounds
) || !EqualRect( &visBounds
, &m_txnVisBounds
) )
2442 m_txnControlBounds
= bounds
;
2443 m_txnVisBounds
= visBounds
;
2444 wxMacWindowClipper
cl( textctrl
) ;
2446 #ifdef __WXMAC_OSX__
2447 if ( m_sbHorizontal
|| m_sbVertical
)
2449 int w
= bounds
.right
- bounds
.left
;
2450 int h
= bounds
.bottom
- bounds
.top
;
2452 if ( m_sbHorizontal
)
2456 sbBounds
.left
= -1 ;
2457 sbBounds
.top
= h
- 14 ;
2458 sbBounds
.right
= w
+ 1 ;
2459 sbBounds
.bottom
= h
+ 1 ;
2461 SetControlBounds( m_sbHorizontal
, &sbBounds
) ;
2462 SetControlViewSize( m_sbHorizontal
, w
) ;
2469 sbBounds
.left
= w
- 14 ;
2471 sbBounds
.right
= w
+ 1 ;
2472 sbBounds
.bottom
= m_sbHorizontal
? h
- 14 : h
+ 1 ;
2474 SetControlBounds( m_sbVertical
, &sbBounds
) ;
2475 SetControlViewSize( m_sbVertical
, h
) ;
2480 TXNLongRect olddestRect
;
2481 TXNGetRectBounds( m_txn
, &oldviewRect
, &olddestRect
, NULL
) ;
2483 Rect viewRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2484 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2485 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2486 TXNLongRect destRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2487 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2488 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2490 if ( olddestRect
.right
>= 10000 )
2491 destRect
.right
= destRect
.left
+ 32000 ;
2493 if ( olddestRect
.bottom
>= 0x20000000 )
2494 destRect
.bottom
= destRect
.top
+ 0x40000000 ;
2496 SectRect( &viewRect
, &visBounds
, &viewRect
) ;
2497 TXNSetRectBounds( m_txn
, &viewRect
, &destRect
, true ) ;
2502 m_txnControlBounds
.top
,
2503 m_txnControlBounds
.left
,
2504 m_txnControlBounds
.bottom
- (m_sbHorizontal
? 14 : 0),
2505 m_txnControlBounds
.right
- (m_sbVertical
? 14 : 0),
2511 m_txn
, m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2512 wxMax( m_txnControlBounds
.bottom
, m_txnControlBounds
.top
),
2513 wxMax( m_txnControlBounds
.right
, m_txnControlBounds
.left
), m_txnFrameID
);
2516 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2517 // movement, therefore we have to force it
2519 // this problem has been reported in OSX as well, so we use this here once again
2521 TXNLongRect textRect
;
2522 TXNGetRectBounds( m_txn
, NULL
, NULL
, &textRect
) ;
2523 if ( textRect
.left
< m_txnControlBounds
.left
)
2524 TXNShowSelection( m_txn
, kTXNShowStart
) ;
2528 void wxMacMLTEClassicControl::SetRect( Rect
*r
)
2530 wxMacControl::SetRect( r
) ;
2531 MacUpdatePosition() ;
2534 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16 thePart
)
2536 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2537 if ( textctrl
== NULL
)
2540 if ( textctrl
->MacIsReallyShown() )
2542 wxMacWindowClipper
clipper( textctrl
) ;
2543 TXNDraw( m_txn
, NULL
) ;
2547 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
2549 Point where
= { y
, x
} ;
2550 ControlPartCode result
= kControlNoPart
;
2552 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2553 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2555 if (PtInRect( where
, &m_txnControlBounds
))
2557 result
= kControlEditTextPart
;
2561 // sometimes we get the coords also in control local coordinates, therefore test again
2563 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2567 if (PtInRect( where
, &m_txnControlBounds
))
2568 result
= kControlEditTextPart
;
2575 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x
, wxInt16 y
, void* actionProc
)
2577 ControlPartCode result
= kControlNoPart
;
2579 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2580 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2582 Point startPt
= { y
, x
} ;
2584 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2586 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2590 switch (MacControlUserPaneHitTestProc( startPt
.h
, startPt
.v
))
2592 case kControlEditTextPart
:
2594 wxMacWindowClipper
clipper( textctrl
) ;
2597 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
2598 TXNClick( m_txn
, &rec
);
2610 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2612 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2613 if ( textctrl
== NULL
)
2616 if (textctrl
->MacIsReallyShown())
2618 if (IsControlActive(m_controlRef
))
2622 wxMacWindowClipper
clipper( textctrl
) ;
2627 if (PtInRect(mousep
, &m_txnControlBounds
))
2629 RgnHandle theRgn
= NewRgn();
2630 RectRgn(theRgn
, &m_txnControlBounds
);
2631 TXNAdjustCursor(m_txn
, theRgn
);
2638 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
2640 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2641 if ( textctrl
== NULL
)
2642 return kControlNoPart
;
2644 wxMacWindowClipper
clipper( textctrl
) ;
2647 memset( &ev
, 0 , sizeof( ev
) ) ;
2649 ev
.modifiers
= modifiers
;
2650 ev
.message
= ((keyCode
<< 8) & keyCodeMask
) | (charCode
& charCodeMask
);
2651 TXNKeyDown( m_txn
, &ev
);
2653 return kControlEntireControl
;
2656 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating
)
2658 MacActivatePaneText( activating
);
2661 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action
)
2663 ControlPartCode focusResult
= kControlFocusNoPart
;
2665 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2666 if ( textctrl
== NULL
)
2669 wxMacWindowClipper
clipper( textctrl
) ;
2671 ControlRef controlFocus
= NULL
;
2672 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2673 bool wasFocused
= ( controlFocus
== m_controlRef
) ;
2677 case kControlFocusPrevPart
:
2678 case kControlFocusNextPart
:
2679 MacFocusPaneText( !wasFocused
);
2680 focusResult
= (!wasFocused
? (ControlPartCode
) kControlEditTextPart
: (ControlPartCode
) kControlFocusNoPart
);
2683 case kControlFocusNoPart
:
2685 MacFocusPaneText( false );
2686 focusResult
= kControlFocusNoPart
;
2693 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *info
)
2697 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
2698 const wxString
& str
,
2700 const wxSize
& size
, long style
)
2701 : wxMacMLTEControl( wxPeer
)
2703 m_font
= wxPeer
->GetFont() ;
2704 m_windowStyle
= style
;
2705 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
2708 kControlSupportsEmbedding
| kControlSupportsFocus
| kControlWantsIdle
2709 | kControlWantsActivate
| kControlHandlesTracking
2710 // | kControlHasSpecialBackground
2711 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
2713 OSStatus err
= ::CreateUserPaneControl(
2714 MAC_WXHWND(wxPeer
->GetParent()->MacGetTopLevelWindowRef()),
2715 &bounds
, featureSet
, &m_controlRef
);
2716 verify_noerr( err
);
2720 AdjustCreationAttributes( *wxWHITE
, true ) ;
2722 MacSetObjectVisibility( wxPeer
->MacIsReallyShown() ) ;
2726 wxMacConvertNewlines10To13( &st
) ;
2727 wxMacWindowClipper
clipper( m_peer
) ;
2728 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
2729 TXNSetSelection( m_txn
, 0, 0 ) ;
2733 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2735 TXNDeleteObject( m_txn
);
2739 void wxMacMLTEClassicControl::VisibilityChanged(bool shown
)
2741 MacSetObjectVisibility( shown
) ;
2742 wxMacControl::VisibilityChanged( shown
) ;
2745 void wxMacMLTEClassicControl::SuperChangedPosition()
2747 MacUpdatePosition() ;
2748 wxMacControl::SuperChangedPosition() ;
2751 #ifdef __WXMAC_OSX__
2753 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
2754 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
2755 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
2756 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
2757 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
2758 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
2759 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
2761 static pascal void wxMacControlUserPaneDrawProc(ControlRef control
, SInt16 part
)
2763 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2764 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2766 win
->MacControlUserPaneDrawProc( part
) ;
2769 static pascal ControlPartCode
wxMacControlUserPaneHitTestProc(ControlRef control
, Point where
)
2771 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2772 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2774 return win
->MacControlUserPaneHitTestProc( where
.h
, where
.v
) ;
2776 return kControlNoPart
;
2779 static pascal ControlPartCode
wxMacControlUserPaneTrackingProc(ControlRef control
, Point startPt
, ControlActionUPP actionProc
)
2781 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2782 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2784 return win
->MacControlUserPaneTrackingProc( startPt
.h
, startPt
.v
, (void*) actionProc
) ;
2786 return kControlNoPart
;
2789 static pascal void wxMacControlUserPaneIdleProc(ControlRef control
)
2791 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2792 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2794 win
->MacControlUserPaneIdleProc() ;
2797 static pascal ControlPartCode
wxMacControlUserPaneKeyDownProc(ControlRef control
, SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
)
2799 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2800 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2802 return win
->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
2804 return kControlNoPart
;
2807 static pascal void wxMacControlUserPaneActivateProc(ControlRef control
, Boolean activating
)
2809 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2810 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2812 win
->MacControlUserPaneActivateProc( activating
) ;
2815 static pascal ControlPartCode
wxMacControlUserPaneFocusProc(ControlRef control
, ControlFocusPart action
)
2817 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2818 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2820 return win
->MacControlUserPaneFocusProc( action
) ;
2822 return kControlNoPart
;
2826 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control
, ControlBackgroundPtr info
)
2828 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2829 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2831 win
->MacControlUserPaneBackgroundProc(info
) ;
2835 #endif // __WXMAC_OSX__
2837 // TXNRegisterScrollInfoProc
2839 OSStatus
wxMacMLTEClassicControl::DoCreate()
2842 OSStatus err
= noErr
;
2844 // set up our globals
2845 #ifdef __WXMAC_OSX__
2846 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc
);
2847 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc
);
2848 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc
);
2849 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc
);
2850 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc
);
2851 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc
);
2852 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc
);
2854 if (gTXNScrollInfoProc
== NULL
) gTXNScrollInfoProc
= NewTXNScrollInfoUPP(TXNScrollInfoProc
) ;
2855 if (gTXNScrollActionProc
== NULL
) gTXNScrollActionProc
= NewControlActionUPP(TXNScrollActionProc
) ;
2858 // set the initial settings for our private data
2860 m_txnWindow
= GetControlOwner(m_controlRef
);
2861 m_txnPort
= (GrafPtr
) GetWindowPort(m_txnWindow
);
2863 #ifdef __WXMAC_OSX__
2864 // set up the user pane procedures
2865 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
2866 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
2867 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
2868 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
2869 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
2870 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
2871 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
2874 // calculate the rectangles used by the control
2875 UMAGetControlBoundsInWindowCoords( m_controlRef
, &bounds
);
2877 m_txnControlBounds
= bounds
;
2878 m_txnVisBounds
= bounds
;
2883 GetGWorld( &origPort
, &origDev
) ;
2884 SetPort( m_txnPort
);
2886 // create the new edit field
2887 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( m_windowStyle
);
2889 #ifdef __WXMAC_OSX__
2890 // the scrollbars are not correctly embedded but are inserted at the root:
2891 // this gives us problems as we have erratic redraws even over the structure area
2893 m_sbHorizontal
= 0 ;
2895 m_lastHorizontalValue
= 0 ;
2896 m_lastVerticalValue
= 0 ;
2898 Rect sb
= { 0 , 0 , 0 , 0 } ;
2899 if ( frameOptions
& kTXNWantVScrollBarMask
)
2901 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbVertical
);
2902 SetControlReference( m_sbVertical
, (SInt32
)this );
2903 SetControlAction( m_sbVertical
, gTXNScrollActionProc
);
2904 ShowControl( m_sbVertical
);
2905 EmbedControl( m_sbVertical
, m_controlRef
);
2906 frameOptions
&= ~kTXNWantVScrollBarMask
;
2909 if ( frameOptions
& kTXNWantHScrollBarMask
)
2911 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbHorizontal
);
2912 SetControlReference( m_sbHorizontal
, (SInt32
)this );
2913 SetControlAction( m_sbHorizontal
, gTXNScrollActionProc
);
2914 ShowControl( m_sbHorizontal
);
2915 EmbedControl( m_sbHorizontal
, m_controlRef
);
2916 frameOptions
&= ~(kTXNWantHScrollBarMask
| kTXNDrawGrowIconMask
);
2922 NULL
, m_txnWindow
, &bounds
, frameOptions
,
2923 kTXNTextEditStyleFrameType
, kTXNTextensionFile
, kTXNSystemDefaultEncoding
,
2924 &m_txn
, &m_txnFrameID
, NULL
);
2925 verify_noerr( err
);
2928 TXNControlTag iControlTags
[] = { kTXNUseCarbonEvents
};
2929 TXNControlData iControlData
[] = { { (UInt32
)&cInfo
} };
2930 int toptag
= WXSIZEOF( iControlTags
) ;
2931 TXNCarbonEventInfo cInfo
;
2932 cInfo
.useCarbonEvents
= false ;
2935 cInfo
.fDictionary
= NULL
;
2937 verify_noerr( TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) );
2940 #ifdef __WXMAC_OSX__
2941 TXNRegisterScrollInfoProc( m_txn
, gTXNScrollInfoProc
, (SInt32
)this );
2944 SetGWorld( origPort
, origDev
) ;
2950 // ----------------------------------------------------------------------------
2951 // MLTE control implementation (OSX part)
2952 // ----------------------------------------------------------------------------
2954 #if TARGET_API_MAC_OSX
2956 #if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
2958 // tiger multi-line textcontrols with no CR in the entire content
2959 // don't scroll automatically, so we need a hack.
2960 // This attempt only works 'before' the key (ie before CallNextEventHandler)
2961 // is processed, thus the scrolling always occurs one character too late, but
2962 // better than nothing ...
2964 static const EventTypeSpec eventList
[] =
2966 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
2969 static pascal OSStatus
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2971 OSStatus result
= eventNotHandledErr
;
2972 wxMacMLTEHIViewControl
* focus
= (wxMacMLTEHIViewControl
*) data
;
2974 switch ( GetEventKind( event
) )
2976 case kEventTextInputUnicodeForKeyEvent
:
2978 if ( UMAGetSystemVersion() >= 0x1040 )
2980 TXNOffset from
, to
;
2981 TXNGetSelection( focus
->GetTXNObject() , &from
, &to
) ;
2983 TXNShowSelection( focus
->GetTXNObject() , kTXNShowStart
);
2985 result
= CallNextEventHandler(handler
,event
);
2995 static pascal OSStatus
wxMacTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2997 OSStatus result
= eventNotHandledErr
;
2999 switch ( GetEventClass( event
) )
3001 case kEventClassTextInput
:
3002 result
= wxMacUnicodeTextEventHandler( handler
, event
, data
) ;
3011 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler
)
3013 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
3014 const wxString
& str
,
3016 const wxSize
& size
, long style
) : wxMacMLTEControl( wxPeer
)
3018 m_font
= wxPeer
->GetFont() ;
3019 m_windowStyle
= style
;
3020 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
3022 wxMacConvertNewlines10To13( &st
) ;
3025 { bounds
.left
, bounds
.top
},
3026 { bounds
.right
- bounds
.left
, bounds
.bottom
- bounds
.top
} } ;
3028 m_scrollView
= NULL
;
3029 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( style
) ;
3030 if (( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
)) || !(frameOptions
&kTXNSingleLineOnlyMask
))
3032 if ( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
) )
3035 (frameOptions
& kTXNWantHScrollBarMask
? kHIScrollViewOptionsHorizScroll
: 0)
3036 | (frameOptions
& kTXNWantVScrollBarMask
? kHIScrollViewOptionsVertScroll
: 0) ,
3041 HIScrollViewCreate(kHIScrollViewOptionsVertScroll
,&m_scrollView
);
3042 HIScrollViewSetScrollBarAutoHide(m_scrollView
,true);
3045 HIViewSetFrame( m_scrollView
, &hr
);
3046 HIViewSetVisible( m_scrollView
, true );
3050 HITextViewCreate( NULL
, 0, frameOptions
, &m_textView
) ;
3051 m_txn
= HITextViewGetTXNObject( m_textView
) ;
3052 HIViewSetVisible( m_textView
, true ) ;
3055 HIViewAddSubview( m_scrollView
, m_textView
) ;
3056 m_controlRef
= m_scrollView
;
3057 wxPeer
->MacInstallEventHandler( (WXWidget
) m_textView
) ;
3061 HIViewSetFrame( m_textView
, &hr
);
3062 m_controlRef
= m_textView
;
3065 AdjustCreationAttributes( *wxWHITE
, true ) ;
3067 wxMacWindowClipper
c( m_peer
) ;
3069 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
3071 TXNSetSelection( m_txn
, 0, 0 );
3072 TXNShowSelection( m_txn
, kTXNShowStart
);
3074 InstallControlEventHandler( m_textView
, GetwxMacTextControlEventHandlerUPP(),
3075 GetEventTypeCount(eventList
), eventList
, this,
3076 &m_textEventHandlerRef
);
3079 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3081 ::RemoveEventHandler( m_textEventHandlerRef
) ;
3084 OSStatus
wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart
)
3086 return SetKeyboardFocus( GetControlOwner( m_textView
), m_textView
, focusPart
) ;
3089 bool wxMacMLTEHIViewControl::HasFocus() const
3091 ControlRef control
;
3092 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
3093 return control
== m_textView
;
3096 void wxMacMLTEHIViewControl::SetBackground( const wxBrush
&brush
)
3098 wxMacMLTEControl::SetBackground( brush
) ;
3101 CGColorSpaceRef rgbSpace
= CGColorSpaceCreateDeviceRGB();
3102 RGBColor col
= MAC_WXCOLORREF(brush
.GetColour().GetPixel()) ;
3104 float component
[4] ;
3105 component
[0] = col
.red
/ 65536.0 ;
3106 component
[1] = col
.green
/ 65536.0 ;
3107 component
[2] = col
.blue
/ 65536.0 ;
3108 component
[3] = 1.0 ; // alpha
3110 CGColorRef color
= CGColorCreate( rgbSpace
, component
);
3111 HITextViewSetBackgroundColor( m_textView
, color
);
3112 CGColorSpaceRelease( rgbSpace
);
3116 #endif // MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_2
3121 #endif // wxUSE_TEXTCTRL