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__)
55 #include <MacTextEditor.h>
56 #include <ATSUnicode.h>
57 #include <TextCommon.h>
58 #include <TextEncodingConverter.h>
61 #include "wx/mac/uma.h"
62 #include "wx/mac/carbon/private/mactext.h"
67 kTXNVisibilityTag
= 'visb' // set the visibility state of the object
76 virtual ~wxMacFunctor() {}
78 virtual void* operator()() = 0 ;
80 static void* CallBackProc( void *param
)
82 wxMacFunctor
* f
= (wxMacFunctor
*) param
;
83 void *result
= (*f
)() ;
88 template<typename classtype
, typename param1type
>
90 class wxMacObjectFunctor1
: public wxMacFunctor
92 typedef void (classtype::*function
)( param1type p1
) ;
93 typedef void (classtype::*ref_function
)( const param1type
& p1
) ;
95 wxMacObjectFunctor1( classtype
*obj
, function f
, param1type p1
) :
103 wxMacObjectFunctor1( classtype
*obj
, ref_function f
, param1type p1
) :
111 virtual ~wxMacObjectFunctor1() {}
113 virtual void* operator()()
115 (m_object
->*m_function
)( m_param1
) ;
120 classtype
* m_object
;
121 param1type m_param1
;
124 function m_function
;
125 ref_function m_refFunction
;
129 template<typename classtype
, typename param1type
>
130 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
132 wxMacObjectFunctor1
<classtype
, param1type
> params(object
, function
, p1
) ;
134 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
138 template<typename classtype
, typename param1type
>
139 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
141 wxMacObjectFunctor1
<classtype
,param1type
> params(object
, function
, p1
) ;
143 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
147 template<typename classtype
, typename param1type
>
148 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
151 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
156 template<typename classtype
, typename param1type
>
157 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
160 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
166 // common parts for implementations based on MLTE
168 class wxMacMLTEControl
: public wxMacTextControl
171 wxMacMLTEControl( wxTextCtrl
*peer
) ;
173 virtual wxString
GetStringValue() const ;
174 virtual void SetStringValue( const wxString
&str
) ;
176 static TXNFrameOptions
FrameOptionsFromWXStyle( long wxStyle
) ;
178 void AdjustCreationAttributes( const wxColour
& background
, bool visible
) ;
180 virtual void SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
) ;
181 virtual void SetBackground( const wxBrush
&brush
) ;
182 virtual void SetStyle( long start
, long end
, const wxTextAttr
& style
) ;
183 virtual void Copy() ;
185 virtual void Paste() ;
186 virtual bool CanPaste() const ;
187 virtual void SetEditable( bool editable
) ;
188 virtual wxTextPos
GetLastPosition() const ;
189 virtual void Replace( long from
, long to
, const wxString
&str
) ;
190 virtual void Remove( long from
, long to
) ;
191 virtual void GetSelection( long* from
, long* to
) const ;
192 virtual void SetSelection( long from
, long to
) ;
194 virtual void WriteText( const wxString
& str
) ;
196 virtual bool HasOwnContextMenu() const
198 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
199 if ( UMAGetSystemVersion() >= 0x1040 )
201 TXNCommandEventSupportOptions options
;
202 TXNGetCommandEventSupport( m_txn
, & options
) ;
203 return options
& kTXNSupportEditCommandProcessing
;
210 virtual void CheckSpelling(bool check
)
212 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
213 TXNSetSpellCheckAsYouType( m_txn
, (Boolean
) check
);
216 virtual void Clear() ;
218 virtual bool CanUndo() const ;
219 virtual void Undo() ;
220 virtual bool CanRedo() const;
221 virtual void Redo() ;
222 virtual int GetNumberOfLines() const ;
223 virtual long XYToPosition(long x
, long y
) const ;
224 virtual bool PositionToXY(long pos
, long *x
, long *y
) const ;
225 virtual void ShowPosition( long pos
) ;
226 virtual int GetLineLength(long lineNo
) const ;
227 virtual wxString
GetLineText(long lineNo
) const ;
229 void SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
) ;
230 TXNObject
GetTXNObject() { return m_txn
; }
233 void TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
) ;
238 // implementation available under OSX
240 class wxMacMLTEHIViewControl
: public wxMacMLTEControl
243 wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
246 const wxSize
& size
, long style
) ;
247 virtual ~wxMacMLTEHIViewControl() ;
249 virtual OSStatus
SetFocus( ControlFocusPart focusPart
) ;
250 virtual bool HasFocus() const ;
251 virtual void SetBackground( const wxBrush
&brush
) ;
254 HIViewRef m_scrollView
;
255 HIViewRef m_textView
;
258 // 'classic' MLTE implementation
260 class wxMacMLTEClassicControl
: public wxMacMLTEControl
263 wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
266 const wxSize
& size
, long style
) ;
267 virtual ~wxMacMLTEClassicControl() ;
269 virtual void VisibilityChanged(bool shown
) ;
270 virtual void SuperChangedPosition() ;
272 virtual void MacControlUserPaneDrawProc(wxInt16 part
) ;
273 virtual wxInt16
MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
) ;
274 virtual wxInt16
MacControlUserPaneTrackingProc(wxInt16 x
, wxInt16 y
, void* actionProc
) ;
275 virtual void MacControlUserPaneIdleProc() ;
276 virtual wxInt16
MacControlUserPaneKeyDownProc(wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
) ;
277 virtual void MacControlUserPaneActivateProc(bool activating
) ;
278 virtual wxInt16
MacControlUserPaneFocusProc(wxInt16 action
) ;
279 virtual void MacControlUserPaneBackgroundProc(void* info
) ;
281 virtual bool SetupCursor( const wxPoint
& WXUNUSED(pt
) )
283 MacControlUserPaneIdleProc();
287 virtual void SetRect( Rect
*r
) ;
292 void MacUpdatePosition() ;
293 void MacActivatePaneText(bool setActive
) ;
294 void MacFocusPaneText(bool setFocus
) ;
295 void MacSetObjectVisibility(bool vis
) ;
298 TXNFrameID m_txnFrameID
;
300 WindowRef m_txnWindow
;
301 // bounds of the control as we last did set the txn frames
302 Rect m_txnControlBounds
;
303 Rect m_txnVisBounds
;
306 static pascal void TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
) ;
307 static pascal void TXNScrollInfoProc(
308 SInt32 iValue
, SInt32 iMaximumValue
,
309 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
) ;
311 ControlRef m_sbHorizontal
;
312 SInt32 m_lastHorizontalValue
;
313 ControlRef m_sbVertical
;
314 SInt32 m_lastVerticalValue
;
319 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
321 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
322 EVT_ERASE_BACKGROUND( wxTextCtrl::OnEraseBackground
)
323 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
324 EVT_CHAR(wxTextCtrl::OnChar
)
325 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
326 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
327 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
328 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
329 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
330 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
331 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
333 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
335 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
336 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
337 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
338 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
339 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
340 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
341 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
345 void wxTextCtrl::Init()
351 m_privateContextMenu
= NULL
;
352 m_triggerOnSetValue
= true ;
355 wxTextCtrl::~wxTextCtrl()
357 delete m_privateContextMenu
;
360 bool wxTextCtrl::Create( wxWindow
*parent
,
366 const wxValidator
& validator
,
367 const wxString
& name
)
369 m_macIsUserPane
= false ;
372 if ( ! (style
& wxNO_BORDER
) )
373 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
375 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
378 if ( m_windowStyle
& wxTE_MULTILINE
)
380 // always turn on this style for multi-line controls
381 m_windowStyle
|= wxTE_PROCESS_ENTER
;
382 style
|= wxTE_PROCESS_ENTER
;
385 CreatePeer( str
, pos
, size
, style
);
387 MacPostControlCreate(pos
, size
) ;
389 // only now the embedding is correct and we can do a positioning update
391 MacSuperChangedPosition() ;
393 if ( m_windowStyle
& wxTE_READONLY
)
394 SetEditable( false ) ;
396 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
401 void wxTextCtrl::CreatePeer(
404 const wxSize
& size
, long style
)
406 bool forceMLTE
= false ;
408 #if wxUSE_SYSTEM_OPTIONS
409 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE
) == 1))
415 if ( UMAGetSystemVersion() >= 0x1050 )
418 if ( UMAGetSystemVersion() >= 0x1030 && !forceMLTE
)
420 if ( m_windowStyle
& wxTE_MULTILINE
)
421 m_peer
= new wxMacMLTEHIViewControl( this , str
, pos
, size
, style
) ;
426 if ( !(m_windowStyle
& wxTE_MULTILINE
) && !forceMLTE
)
428 m_peer
= new wxMacUnicodeTextControl( this , str
, pos
, size
, style
) ;
432 // the horizontal single line scrolling bug that made us keep the classic implementation
434 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
436 m_peer
= new wxMacMLTEClassicControl( this , str
, pos
, size
, style
) ;
440 void wxTextCtrl::MacSuperChangedPosition()
442 wxWindow::MacSuperChangedPosition() ;
443 GetPeer()->SuperChangedPosition() ;
446 void wxTextCtrl::MacVisibilityChanged()
448 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
451 void wxTextCtrl::MacEnabledStateChanged()
455 void wxTextCtrl::MacCheckSpelling(bool check
)
457 GetPeer()->CheckSpelling(check
);
460 wxString
wxTextCtrl::GetValue() const
462 return GetPeer()->GetStringValue() ;
465 void wxTextCtrl::GetSelection(long* from
, long* to
) const
467 GetPeer()->GetSelection( from
, to
) ;
470 void wxTextCtrl::DoSetValue(const wxString
& str
, int flags
)
473 if ( GetValue() == str
)
476 GetPeer()->SetStringValue( str
) ;
478 if ( (flags
& SetValue_SendEvent
) && m_triggerOnSetValue
)
480 SendTextUpdatedEvent();
484 void wxTextCtrl::SetMaxLength(unsigned long len
)
489 bool wxTextCtrl::SetFont( const wxFont
& font
)
491 if ( !wxTextCtrlBase::SetFont( font
) )
494 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle() ) ;
499 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
501 GetPeer()->SetStyle( start
, end
, style
) ;
506 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
508 wxTextCtrlBase::SetDefaultStyle( style
) ;
509 SetStyle( kTXNUseCurrentSelection
, kTXNUseCurrentSelection
, GetDefaultStyle() ) ;
514 // Clipboard operations
516 void wxTextCtrl::Copy()
522 void wxTextCtrl::Cut()
528 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
529 event
.SetEventObject( this );
530 GetEventHandler()->ProcessEvent( event
);
534 void wxTextCtrl::Paste()
540 // TODO: eventually we should add setting the default style again
542 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
543 event
.SetEventObject( this );
544 GetEventHandler()->ProcessEvent( event
);
548 bool wxTextCtrl::CanCopy() const
550 // Can copy if there's a selection
552 GetSelection( &from
, &to
);
557 bool wxTextCtrl::CanCut() const
562 // Can cut if there's a selection
564 GetSelection( &from
, &to
);
569 bool wxTextCtrl::CanPaste() const
574 return GetPeer()->CanPaste() ;
577 void wxTextCtrl::SetEditable(bool editable
)
579 if ( editable
!= m_editable
)
581 m_editable
= editable
;
582 GetPeer()->SetEditable( editable
) ;
586 void wxTextCtrl::SetInsertionPoint(long pos
)
588 SetSelection( pos
, pos
) ;
591 void wxTextCtrl::SetInsertionPointEnd()
593 wxTextPos pos
= GetLastPosition();
594 SetInsertionPoint( pos
);
597 long wxTextCtrl::GetInsertionPoint() const
600 GetSelection( &begin
, &end
) ;
605 wxTextPos
wxTextCtrl::GetLastPosition() const
607 return GetPeer()->GetLastPosition() ;
610 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
612 GetPeer()->Replace( from
, to
, str
) ;
615 void wxTextCtrl::Remove(long from
, long to
)
617 GetPeer()->Remove( from
, to
) ;
620 void wxTextCtrl::SetSelection(long from
, long to
)
622 GetPeer()->SetSelection( from
, to
) ;
625 void wxTextCtrl::WriteText(const wxString
& str
)
627 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
628 if ( !wxIsMainThread() )
630 // unfortunately CW 8 is not able to correctly deduce the template types,
631 // so we have to instantiate explicitly
632 wxMacMPRemoteGUICall
<wxTextCtrl
,wxString
>( this , &wxTextCtrl::WriteText
, str
) ;
637 GetPeer()->WriteText( str
) ;
640 void wxTextCtrl::AppendText(const wxString
& text
)
642 SetInsertionPointEnd();
646 void wxTextCtrl::Clear()
651 bool wxTextCtrl::IsModified() const
656 bool wxTextCtrl::IsEditable() const
658 return IsEnabled() && m_editable
;
661 bool wxTextCtrl::AcceptsFocus() const
663 // we don't want focus if we can't be edited
664 return /*IsEditable() && */ wxControl::AcceptsFocus();
667 wxSize
wxTextCtrl::DoGetBestSize() const
671 // these are the numbers from the HIG:
672 // we reduce them by the borders first
675 switch ( m_windowVariant
)
677 case wxWINDOW_VARIANT_NORMAL
:
681 case wxWINDOW_VARIANT_SMALL
:
685 case wxWINDOW_VARIANT_MINI
:
694 // as the above numbers have some free space around the text
695 // we get 5 lines like this anyway
696 if ( m_windowStyle
& wxTE_MULTILINE
)
699 if ( !HasFlag(wxNO_BORDER
) )
702 return wxSize(wText
, hText
);
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 void wxTextCtrl::Undo()
715 void wxTextCtrl::Redo()
721 bool wxTextCtrl::CanUndo() const
726 return GetPeer()->CanUndo() ;
729 bool wxTextCtrl::CanRedo() const
734 return GetPeer()->CanRedo() ;
737 void wxTextCtrl::MarkDirty()
742 void wxTextCtrl::DiscardEdits()
747 int wxTextCtrl::GetNumberOfLines() const
749 return GetPeer()->GetNumberOfLines() ;
752 long wxTextCtrl::XYToPosition(long x
, long y
) const
754 return GetPeer()->XYToPosition( x
, y
) ;
757 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
759 return GetPeer()->PositionToXY( pos
, x
, y
) ;
762 void wxTextCtrl::ShowPosition(long pos
)
764 return GetPeer()->ShowPosition(pos
) ;
767 int wxTextCtrl::GetLineLength(long lineNo
) const
769 return GetPeer()->GetLineLength(lineNo
) ;
772 wxString
wxTextCtrl::GetLineText(long lineNo
) const
774 return GetPeer()->GetLineText(lineNo
) ;
777 void wxTextCtrl::Command(wxCommandEvent
& event
)
779 SetValue(event
.GetString());
780 ProcessCommand(event
);
783 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
785 // By default, load the first file into the text window.
786 if (event
.GetNumberOfFiles() > 0)
787 LoadFile( event
.GetFiles()[0] );
790 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
792 // all erasing should be done by the real mac control implementation
793 // while this is true for MLTE under classic, the HITextView is somehow
794 // transparent but background erase is not working correctly, so intercept
795 // things while we can...
799 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
801 int key
= event
.GetKeyCode() ;
802 bool eat_key
= false ;
804 if ( key
== 'a' && event
.MetaDown() )
811 if ( key
== 'c' && event
.MetaDown() )
819 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
820 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
821 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
828 // Check if we have reached the max # of chars (if it is set), but still
829 // allow navigation and deletion
830 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
831 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&&
832 key
!= WXK_BACK
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) )
835 // eat it, we don't want to add more than allowed # of characters
837 // TODO: generate EVT_TEXT_MAXLEN()
841 // assume that any key not processed yet is going to modify the control
844 if ( key
== 'v' && event
.MetaDown() )
852 if ( key
== 'x' && event
.MetaDown() )
863 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
865 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
866 event
.SetEventObject( this );
867 event
.SetString( GetValue() );
868 if ( GetEventHandler()->ProcessEvent(event
) )
872 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
874 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
875 if ( tlw
&& tlw
->GetDefaultItem() )
877 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
878 if ( def
&& def
->IsEnabled() )
880 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
881 event
.SetEventObject(def
);
888 // this will make wxWidgets eat the ENTER key so that
889 // we actually prevent line wrapping in a single line text control
895 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
898 if (!event
.ShiftDown())
899 flags
|= wxNavigationKeyEvent::IsForward
;
900 if (event
.ControlDown())
901 flags
|= wxNavigationKeyEvent::WinChange
;
908 // This is necessary (don't know why);
909 // otherwise the tab will not be inserted.
910 WriteText(wxT("\t"));
921 // perform keystroke handling
925 if ( ( key
>= 0x20 && key
< WXK_START
) ||
926 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
931 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
932 event1
.SetEventObject( this );
933 wxPostEvent( GetEventHandler(), event1
);
937 // ----------------------------------------------------------------------------
938 // standard handlers for standard edit menu events
939 // ----------------------------------------------------------------------------
941 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
946 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
951 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
956 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
961 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
966 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
970 GetSelection( &from
, &to
);
971 if (from
!= -1 && to
!= -1)
975 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
977 SetSelection(-1, -1);
980 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
982 event
.Enable( CanCut() );
985 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
987 event
.Enable( CanCopy() );
990 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
992 event
.Enable( CanPaste() );
995 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
997 event
.Enable( CanUndo() );
1000 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1002 event
.Enable( CanRedo() );
1005 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1009 GetSelection( &from
, &to
);
1010 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
1013 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1015 event
.Enable(GetLastPosition() > 0);
1018 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
1020 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1022 if ( GetPeer()->HasOwnContextMenu() )
1028 if (m_privateContextMenu
== NULL
)
1030 m_privateContextMenu
= new wxMenu
;
1031 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1032 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1033 m_privateContextMenu
->AppendSeparator();
1034 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1035 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1036 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1037 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1038 m_privateContextMenu
->AppendSeparator();
1039 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1042 if (m_privateContextMenu
!= NULL
)
1043 PopupMenu(m_privateContextMenu
);
1046 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
1048 if ( !GetPeer()->SetupCursor( pt
) )
1049 return wxWindow::MacSetupCursor( pt
) ;
1054 #if !TARGET_API_MAC_OSX
1056 // user pane implementation
1058 void wxTextCtrl::MacControlUserPaneDrawProc(wxInt16 part
)
1060 GetPeer()->MacControlUserPaneDrawProc( part
) ;
1063 wxInt16
wxTextCtrl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
1065 return GetPeer()->MacControlUserPaneHitTestProc( x
, y
) ;
1068 wxInt16
wxTextCtrl::MacControlUserPaneTrackingProc(wxInt16 x
, wxInt16 y
, void* actionProc
)
1070 return GetPeer()->MacControlUserPaneTrackingProc( x
, y
, actionProc
) ;
1073 void wxTextCtrl::MacControlUserPaneIdleProc()
1075 GetPeer()->MacControlUserPaneIdleProc( ) ;
1078 wxInt16
wxTextCtrl::MacControlUserPaneKeyDownProc(wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
1080 return GetPeer()->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
1083 void wxTextCtrl::MacControlUserPaneActivateProc(bool activating
)
1085 GetPeer()->MacControlUserPaneActivateProc( activating
) ;
1088 wxInt16
wxTextCtrl::MacControlUserPaneFocusProc(wxInt16 action
)
1090 return GetPeer()->MacControlUserPaneFocusProc( action
) ;
1093 void wxTextCtrl::MacControlUserPaneBackgroundProc(void* info
)
1095 GetPeer()->MacControlUserPaneBackgroundProc( info
) ;
1100 // ----------------------------------------------------------------------------
1101 // implementation base class
1102 // ----------------------------------------------------------------------------
1104 wxMacTextControl::wxMacTextControl(wxTextCtrl
* peer
) :
1105 wxMacControl( peer
)
1109 wxMacTextControl::~wxMacTextControl()
1113 void wxMacTextControl::SetStyle(long WXUNUSED(start
),
1115 const wxTextAttr
& WXUNUSED(style
))
1119 void wxMacTextControl::Copy()
1123 void wxMacTextControl::Cut()
1127 void wxMacTextControl::Paste()
1131 bool wxMacTextControl::CanPaste() const
1136 void wxMacTextControl::SetEditable(bool WXUNUSED(editable
))
1140 wxTextPos
wxMacTextControl::GetLastPosition() const
1142 return GetStringValue().length() ;
1145 void wxMacTextControl::Replace( long from
, long to
, const wxString
&val
)
1147 SetSelection( from
, to
) ;
1151 void wxMacTextControl::Remove( long from
, long to
)
1153 SetSelection( from
, to
) ;
1154 WriteText( wxEmptyString
) ;
1157 void wxMacTextControl::Clear()
1159 SetStringValue( wxEmptyString
) ;
1162 bool wxMacTextControl::CanUndo() const
1167 void wxMacTextControl::Undo()
1171 bool wxMacTextControl::CanRedo() const
1176 void wxMacTextControl::Redo()
1180 long wxMacTextControl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
1185 bool wxMacTextControl::PositionToXY(long WXUNUSED(pos
),
1187 long *WXUNUSED(y
)) const
1192 void wxMacTextControl::ShowPosition( long WXUNUSED(pos
) )
1196 int wxMacTextControl::GetNumberOfLines() const
1198 ItemCount lines
= 0 ;
1199 wxString content
= GetStringValue() ;
1202 for (size_t i
= 0; i
< content
.length() ; i
++)
1204 if (content
[i
] == '\r')
1211 wxString
wxMacTextControl::GetLineText(long lineNo
) const
1213 // TODO: change this if possible to reflect real lines
1214 wxString content
= GetStringValue() ;
1218 for (size_t i
= 0; i
< content
.length() ; i
++)
1220 if (count
== lineNo
)
1222 // Add chars in line then
1225 for (size_t j
= i
; j
< content
.length(); j
++)
1227 if (content
[j
] == '\n')
1236 if (content
[i
] == '\n')
1240 return wxEmptyString
;
1243 int wxMacTextControl::GetLineLength(long lineNo
) const
1245 // TODO: change this if possible to reflect real lines
1246 wxString content
= GetStringValue() ;
1250 for (size_t i
= 0; i
< content
.length() ; i
++)
1252 if (count
== lineNo
)
1254 // Count chars in line then
1256 for (size_t j
= i
; j
< content
.length(); j
++)
1259 if (content
[j
] == '\n')
1266 if (content
[i
] == '\n')
1273 // ----------------------------------------------------------------------------
1274 // standard unicode control implementation
1275 // ----------------------------------------------------------------------------
1277 #if TARGET_API_MAC_OSX
1279 // the current unicode textcontrol implementation has a bug : only if the control
1280 // is currently having the focus, the selection can be retrieved by the corresponding
1281 // data tag. So we have a mirroring using a member variable
1282 // TODO : build event table using virtual member functions for wxMacControl
1284 static const EventTypeSpec unicodeTextControlEventList
[] =
1286 { kEventClassControl
, kEventControlSetFocusPart
} ,
1289 static pascal OSStatus
wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1291 OSStatus result
= eventNotHandledErr
;
1292 wxMacUnicodeTextControl
* focus
= (wxMacUnicodeTextControl
*) data
;
1293 wxMacCarbonEvent
cEvent( event
) ;
1295 switch ( GetEventKind( event
) )
1297 case kEventControlSetFocusPart
:
1299 ControlPartCode controlPart
= cEvent
.GetParameter
<ControlPartCode
>(kEventParamControlPart
, typeControlPartCode
);
1300 if ( controlPart
== kControlFocusNoPart
)
1302 // about to loose focus -> store selection to field
1303 focus
->GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1305 result
= CallNextEventHandler(handler
,event
) ;
1306 if ( controlPart
!= kControlFocusNoPart
)
1308 // about to gain focus -> set selection from field
1309 focus
->SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1320 static pascal OSStatus
wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1322 OSStatus result
= eventNotHandledErr
;
1324 switch ( GetEventClass( event
) )
1326 case kEventClassControl
:
1327 result
= wxMacUnicodeTextControlControlEventHandler( handler
, event
, data
) ;
1336 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler
)
1338 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
) : wxMacTextControl( wxPeer
)
1342 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
,
1343 const wxString
& str
,
1345 const wxSize
& size
, long style
)
1346 : wxMacTextControl( wxPeer
)
1348 Create( wxPeer
, str
, pos
, size
, style
);
1351 bool wxMacUnicodeTextControl::Create( wxTextCtrl
*wxPeer
,
1352 const wxString
& str
,
1354 const wxSize
& size
, long style
)
1356 m_font
= wxPeer
->GetFont() ;
1357 m_windowStyle
= style
;
1358 m_selection
.selStart
= m_selection
.selEnd
= 0;
1359 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
1361 wxMacConvertNewlines10To13( &st
) ;
1362 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding()) ;
1363 CFStringRef cfr
= cf
;
1365 m_valueTag
= kControlEditTextCFStringTag
;
1366 CreateControl( wxPeer
, &bounds
, cfr
);
1368 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1369 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextSingleLineTag
, true ) ;
1371 InstallControlEventHandler( m_controlRef
, GetwxMacUnicodeTextControlEventHandlerUPP(),
1372 GetEventTypeCount(unicodeTextControlEventList
), unicodeTextControlEventList
, this,
1378 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1382 void wxMacUnicodeTextControl::VisibilityChanged(bool shown
)
1384 if ( !(m_windowStyle
& wxTE_MULTILINE
) && shown
)
1386 // work around a refresh issue insofar as not always the entire content is shown,
1387 // even if this would be possible
1388 ControlEditTextSelectionRec sel
;
1389 CFStringRef value
= NULL
;
1391 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1392 verify_noerr( GetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1393 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1394 verify_noerr( SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1396 CFRelease( value
) ;
1400 wxString
wxMacUnicodeTextControl::GetStringValue() const
1403 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1406 wxMacCFStringHolder
cf(value
) ;
1407 result
= cf
.AsString() ;
1411 wxMacConvertNewlines13To10( &result
) ;
1413 wxMacConvertNewlines10To13( &result
) ;
1419 void wxMacUnicodeTextControl::SetStringValue( const wxString
&str
)
1422 wxMacConvertNewlines10To13( &st
) ;
1423 wxMacCFStringHolder
cf( st
, m_font
.GetEncoding() ) ;
1424 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, cf
) ) ;
1427 void wxMacUnicodeTextControl::CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef cfr
)
1429 Boolean isPassword
= ( m_windowStyle
& wxTE_PASSWORD
) != 0 ;
1432 m_valueTag
= kControlEditTextPasswordCFStringTag
;
1434 OSStatus err
= CreateEditUnicodeTextControl(
1435 MAC_WXHWND(peer
->MacGetTopLevelWindowRef()), bounds
, cfr
,
1436 isPassword
, NULL
, &m_controlRef
) ;
1437 verify_noerr( err
);
1440 void wxMacUnicodeTextControl::Copy()
1442 SendHICommand( kHICommandCopy
) ;
1445 void wxMacUnicodeTextControl::Cut()
1447 SendHICommand( kHICommandCut
) ;
1450 void wxMacUnicodeTextControl::Paste()
1452 SendHICommand( kHICommandPaste
) ;
1455 bool wxMacUnicodeTextControl::CanPaste() const
1460 void wxMacUnicodeTextControl::SetEditable(bool WXUNUSED(editable
))
1462 #if 0 // leads to problem because text cannot be selected anymore
1463 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextLockedTag
, (Boolean
) !editable
) ;
1467 void wxMacUnicodeTextControl::GetSelection( long* from
, long* to
) const
1469 ControlEditTextSelectionRec sel
;
1471 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ) ;
1476 *from
= sel
.selStart
;
1481 void wxMacUnicodeTextControl::SetSelection( long from
, long to
)
1483 ControlEditTextSelectionRec sel
;
1485 int textLength
= 0 ;
1486 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1489 wxMacCFStringHolder
cf(value
) ;
1490 textLength
= cf
.AsString().length() ;
1493 if ((from
== -1) && (to
== -1))
1500 from
= wxMin(textLength
,wxMax(from
,0)) ;
1501 to
= wxMax(0,wxMin(textLength
,to
)) ;
1504 sel
.selStart
= from
;
1507 SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ;
1512 void wxMacUnicodeTextControl::WriteText( const wxString
& str
)
1515 wxMacConvertNewlines10To13( &st
) ;
1519 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding() ) ;
1520 CFStringRef value
= cf
;
1521 SetData
<CFStringRef
>( 0, kControlEditTextInsertCFStringRefTag
, &value
);
1525 wxString val
= GetStringValue() ;
1527 GetSelection( &start
, &end
) ;
1528 val
.Remove( start
, end
- start
) ;
1529 val
.insert( start
, str
) ;
1530 SetStringValue( val
) ;
1531 SetSelection( start
+ str
.length() , start
+ str
.length() ) ;
1537 // ----------------------------------------------------------------------------
1538 // MLTE control implementation (common part)
1539 // ----------------------------------------------------------------------------
1541 // if MTLE is read only, no changes at all are allowed, not even from
1542 // procedural API, in order to allow changes via API all the same we must undo
1543 // the readonly status while we are executing, this class helps to do so
1545 class wxMacEditHelper
1548 wxMacEditHelper( TXNObject txn
)
1550 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1552 TXNGetTXNObjectControls( m_txn
, 1 , tag
, m_data
) ;
1553 if ( m_data
[0].uValue
== kTXNReadOnly
)
1555 TXNControlData data
[] = { { kTXNReadWrite
} } ;
1556 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, data
) ;
1562 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1563 if ( m_data
[0].uValue
== kTXNReadOnly
)
1564 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, m_data
) ;
1569 TXNControlData m_data
[1] ;
1572 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl
*peer
)
1573 : wxMacTextControl( peer
)
1575 SetNeedsFocusRect( true ) ;
1578 wxString
wxMacMLTEControl::GetStringValue() const
1582 Size actualSize
= 0;
1587 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNUnicodeTextData
);
1596 actualSize
= GetHandleSize( theText
) / sizeof(UniChar
) ;
1597 if ( actualSize
> 0 )
1599 wxChar
*ptr
= NULL
;
1601 #if SIZEOF_WCHAR_T == 2
1602 ptr
= new wxChar
[actualSize
+ 1] ;
1603 wxStrncpy( ptr
, (wxChar
*)(*theText
) , actualSize
) ;
1605 SetHandleSize( theText
, (actualSize
+ 1) * sizeof(UniChar
) ) ;
1607 (((UniChar
*)*theText
)[actualSize
]) = 0 ;
1608 wxMBConvUTF16 converter
;
1609 size_t noChars
= converter
.MB2WC( NULL
, (const char*)*theText
, 0 ) ;
1610 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Unable to count the number of characters in this string!") );
1611 ptr
= new wxChar
[noChars
+ 1] ;
1613 noChars
= converter
.MB2WC( ptr
, (const char*)*theText
, noChars
+ 1 ) ;
1614 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Conversion of string failed!") );
1616 HUnlock( theText
) ;
1619 ptr
[actualSize
] = 0 ;
1620 result
= wxString( ptr
) ;
1624 DisposeHandle( theText
) ;
1628 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1637 actualSize
= GetHandleSize( theText
) ;
1638 if ( actualSize
> 0 )
1641 result
= wxString( *theText
, wxConvLocal
, actualSize
) ;
1642 HUnlock( theText
) ;
1645 DisposeHandle( theText
) ;
1651 wxMacConvertNewlines13To10( &result
) ;
1653 wxMacConvertNewlines10To13( &result
) ;
1659 void wxMacMLTEControl::SetStringValue( const wxString
&str
)
1662 wxMacConvertNewlines10To13( &st
);
1666 wxMacWindowClipper
c( m_peer
) ;
1670 wxMacEditHelper
help( m_txn
);
1671 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
);
1674 TXNSetSelection( m_txn
, 0, 0 );
1675 TXNShowSelection( m_txn
, kTXNShowStart
);
1679 TXNFrameOptions
wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle
)
1681 TXNFrameOptions frameOptions
= kTXNDontDrawCaretWhenInactiveMask
;
1683 frameOptions
|= kTXNDoFontSubstitutionMask
;
1685 if ( ! (wxStyle
& wxTE_NOHIDESEL
) )
1686 frameOptions
|= kTXNDontDrawSelectionWhenInactiveMask
;
1688 if ( wxStyle
& (wxHSCROLL
| wxTE_DONTWRAP
) )
1689 frameOptions
|= kTXNWantHScrollBarMask
;
1691 if ( wxStyle
& wxTE_MULTILINE
)
1693 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
1695 if ( !(wxStyle
& wxTE_NO_VSCROLL
) )
1697 frameOptions
|= kTXNWantVScrollBarMask
;
1699 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1700 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1701 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1702 // if ( frameOptions & kTXNWantHScrollBarMask )
1703 // frameOptions |= kTXNDrawGrowIconMask ;
1708 frameOptions
|= kTXNSingleLineOnlyMask
;
1711 return frameOptions
;
1714 void wxMacMLTEControl::AdjustCreationAttributes(const wxColour
&background
,
1715 bool WXUNUSED(visible
))
1717 TXNControlTag iControlTags
[] =
1719 kTXNDoFontSubstitution
,
1720 kTXNWordWrapStateTag
,
1722 TXNControlData iControlData
[] =
1728 int toptag
= WXSIZEOF( iControlTags
) ;
1730 if ( m_windowStyle
& wxTE_MULTILINE
)
1732 iControlData
[1].uValue
=
1733 (m_windowStyle
& wxTE_DONTWRAP
)
1738 OSStatus err
= TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) ;
1739 verify_noerr( err
);
1741 // setting the default font:
1742 // under 10.2 this causes a visible caret, therefore we avoid it
1744 if ( UMAGetSystemVersion() >= 0x1030 )
1750 GetThemeFont( kThemeSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
1752 TXNTypeAttributes typeAttr
[] =
1754 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
1755 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
1756 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
1759 err
= TXNSetTypeAttributes(
1760 m_txn
, sizeof(typeAttr
) / sizeof(TXNTypeAttributes
),
1761 typeAttr
, kTXNStartOffset
, kTXNEndOffset
);
1762 verify_noerr( err
);
1765 if ( m_windowStyle
& wxTE_PASSWORD
)
1767 UniChar c
= 0x00A5 ;
1768 err
= TXNEchoMode( m_txn
, c
, 0 , true );
1769 verify_noerr( err
);
1772 TXNBackground tback
;
1773 tback
.bgType
= kTXNBackgroundTypeRGB
;
1774 tback
.bg
.color
= MAC_WXCOLORREF( background
.GetPixel() );
1775 TXNSetBackground( m_txn
, &tback
);
1777 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
1778 if ( UMAGetSystemVersion() >= 0x1040 )
1780 TXNCommandEventSupportOptions options
;
1781 if ( TXNGetCommandEventSupport( m_txn
, &options
) == noErr
)
1784 kTXNSupportEditCommandProcessing
1785 | kTXNSupportEditCommandUpdating
1786 | kTXNSupportFontCommandProcessing
1787 | kTXNSupportFontCommandUpdating
;
1789 // only spell check when not read-only
1790 // use system options for the default
1791 bool checkSpelling
= false ;
1792 if ( !(m_windowStyle
& wxTE_READONLY
) )
1794 #if wxUSE_SYSTEM_OPTIONS
1795 if ( wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) == 1) )
1797 checkSpelling
= true ;
1802 if ( checkSpelling
)
1804 kTXNSupportSpellCheckCommandProcessing
1805 | kTXNSupportSpellCheckCommandUpdating
;
1807 TXNSetCommandEventSupport( m_txn
, options
) ;
1813 void wxMacMLTEControl::SetBackground( const wxBrush
&brush
)
1815 // currently only solid background are supported
1816 TXNBackground tback
;
1818 tback
.bgType
= kTXNBackgroundTypeRGB
;
1819 tback
.bg
.color
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() );
1820 TXNSetBackground( m_txn
, &tback
);
1823 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
)
1825 TXNTypeAttributes typeAttr
[4] ;
1827 size_t typeAttrCount
= 0 ;
1830 TXNControlTag controlTags
[4];
1831 TXNControlData controlData
[4];
1832 size_t controlAttrCount
= 0;
1834 TXNTab
* tabs
= NULL
;
1836 bool relayout
= false;
1838 if ( style
.HasFont() )
1840 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1841 const wxFont
&font
= style
.GetFont() ;
1842 typeAttr
[typeAttrCount
].tag
= kTXNATSUIStyle
;
1843 typeAttr
[typeAttrCount
].size
= kTXNATSUIStyleSize
;
1844 typeAttr
[typeAttrCount
].data
.dataPtr
= font
.MacGetATSUStyle() ;
1848 if ( style
.HasTextColour() )
1850 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1851 color
= MAC_WXCOLORREF(style
.GetTextColour().GetPixel()) ;
1853 typeAttr
[typeAttrCount
].tag
= kTXNQDFontColorAttribute
;
1854 typeAttr
[typeAttrCount
].size
= kTXNQDFontColorAttributeSize
;
1855 typeAttr
[typeAttrCount
].data
.dataPtr
= (void*) &color
;
1859 if ( style
.HasAlignment() )
1861 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1864 switch ( style
.GetAlignment() )
1866 case wxTEXT_ALIGNMENT_LEFT
:
1867 align
= kTXNFlushLeft
;
1869 case wxTEXT_ALIGNMENT_CENTRE
:
1872 case wxTEXT_ALIGNMENT_RIGHT
:
1873 align
= kTXNFlushRight
;
1875 case wxTEXT_ALIGNMENT_JUSTIFIED
:
1876 align
= kTXNFullJust
;
1879 case wxTEXT_ALIGNMENT_DEFAULT
:
1880 align
= kTXNFlushDefault
;
1884 controlTags
[controlAttrCount
] = kTXNJustificationTag
;
1885 controlData
[controlAttrCount
].sValue
= align
;
1886 controlAttrCount
++ ;
1889 if ( style
.HasLeftIndent() || style
.HasRightIndent() )
1891 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1892 controlTags
[controlAttrCount
] = kTXNMarginsTag
;
1893 controlData
[controlAttrCount
].marginsPtr
= &margins
;
1894 verify_noerr( TXNGetTXNObjectControls (m_txn
, 1 ,
1895 &controlTags
[controlAttrCount
], &controlData
[controlAttrCount
]) );
1896 if ( style
.HasLeftIndent() )
1898 margins
.leftMargin
= style
.GetLeftIndent() / 254.0 * 72 + 0.5;
1900 if ( style
.HasRightIndent() )
1902 margins
.rightMargin
= style
.GetRightIndent() / 254.0 * 72 + 0.5;
1904 controlAttrCount
++ ;
1907 if ( style
.HasTabs() )
1909 const wxArrayInt
& tabarray
= style
.GetTabs();
1910 // unfortunately Mac only applies a tab distance, not individually different tabs
1911 controlTags
[controlAttrCount
] = kTXNTabSettingsTag
;
1912 if ( tabarray
.size() > 0 )
1913 controlData
[controlAttrCount
].tabValue
.value
= tabarray
[0] / 254.0 * 72 + 0.5;
1915 controlData
[controlAttrCount
].tabValue
.value
= 72 ;
1917 controlData
[controlAttrCount
].tabValue
.tabType
= kTXNLeftTab
;
1918 controlAttrCount
++ ;
1921 // unfortunately the relayout is not automatic
1922 if ( controlAttrCount
> 0 )
1924 verify_noerr( TXNSetTXNObjectControls (m_txn
, false /* don't clear all */, controlAttrCount
,
1925 controlTags
, controlData
) );
1929 if ( typeAttrCount
> 0 )
1931 verify_noerr( TXNSetTypeAttributes( m_txn
, typeAttrCount
, typeAttr
, from
, to
) );
1942 TXNRecalcTextLayout( m_txn
);
1946 void wxMacMLTEControl::SetFont(const wxFont
& font
,
1947 const wxColour
& foreground
,
1948 long WXUNUSED(windowStyle
))
1950 wxMacEditHelper
help( m_txn
) ;
1951 TXNSetAttribute( wxTextAttr( foreground
, wxNullColour
, font
), kTXNStartOffset
, kTXNEndOffset
) ;
1954 void wxMacMLTEControl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1956 wxMacEditHelper
help( m_txn
) ;
1957 TXNSetAttribute( style
, start
, end
) ;
1960 void wxMacMLTEControl::Copy()
1965 void wxMacMLTEControl::Cut()
1970 void wxMacMLTEControl::Paste()
1975 bool wxMacMLTEControl::CanPaste() const
1977 return TXNIsScrapPastable() ;
1980 void wxMacMLTEControl::SetEditable(bool editable
)
1982 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1983 TXNControlData data
[] = { { editable
? kTXNReadWrite
: kTXNReadOnly
} } ;
1984 TXNSetTXNObjectControls( m_txn
, false, WXSIZEOF(tag
), tag
, data
) ;
1987 wxTextPos
wxMacMLTEControl::GetLastPosition() const
1989 wxTextPos actualsize
= 0 ;
1992 OSErr err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1997 actualsize
= GetHandleSize( theText
) ;
1998 DisposeHandle( theText
) ;
2008 void wxMacMLTEControl::Replace( long from
, long to
, const wxString
&str
)
2010 wxString value
= str
;
2011 wxMacConvertNewlines10To13( &value
) ;
2013 wxMacEditHelper
help( m_txn
) ;
2015 wxMacWindowClipper
c( m_peer
) ;
2018 TXNSetSelection( m_txn
, from
, to
) ;
2020 SetTXNData( value
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
2023 void wxMacMLTEControl::Remove( long from
, long to
)
2026 wxMacWindowClipper
c( m_peer
) ;
2028 wxMacEditHelper
help( m_txn
) ;
2029 TXNSetSelection( m_txn
, from
, to
) ;
2033 void wxMacMLTEControl::GetSelection( long* from
, long* to
) const
2036 TXNGetSelection( m_txn
, &f
, &t
) ;
2041 void wxMacMLTEControl::SetSelection( long from
, long to
)
2044 wxMacWindowClipper
c( m_peer
) ;
2047 // change the selection
2048 if ((from
== -1) && (to
== -1))
2049 TXNSelectAll( m_txn
);
2051 TXNSetSelection( m_txn
, from
, to
);
2053 TXNShowSelection( m_txn
, kTXNShowStart
);
2056 void wxMacMLTEControl::WriteText( const wxString
& str
)
2059 wxMacConvertNewlines10To13( &st
) ;
2061 long start
, end
, dummy
;
2063 GetSelection( &start
, &dummy
) ;
2065 wxMacWindowClipper
c( m_peer
) ;
2069 wxMacEditHelper
helper( m_txn
) ;
2070 SetTXNData( st
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
2073 GetSelection( &dummy
, &end
) ;
2075 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2078 void wxMacMLTEControl::Clear()
2081 wxMacWindowClipper
c( m_peer
) ;
2083 wxMacEditHelper
st( m_txn
) ;
2084 TXNSetSelection( m_txn
, kTXNStartOffset
, kTXNEndOffset
) ;
2088 bool wxMacMLTEControl::CanUndo() const
2090 return TXNCanUndo( m_txn
, NULL
) ;
2093 void wxMacMLTEControl::Undo()
2098 bool wxMacMLTEControl::CanRedo() const
2100 return TXNCanRedo( m_txn
, NULL
) ;
2103 void wxMacMLTEControl::Redo()
2108 int wxMacMLTEControl::GetNumberOfLines() const
2110 ItemCount lines
= 0 ;
2111 TXNGetLineCount( m_txn
, &lines
) ;
2116 long wxMacMLTEControl::XYToPosition(long x
, long y
) const
2121 // TODO: find a better implementation : while we can get the
2122 // line metrics of a certain line, we don't get its starting
2123 // position, so it would probably be rather a binary search
2124 // for the start position
2125 long xpos
= 0, ypos
= 0 ;
2126 int lastHeight
= 0 ;
2129 lastpos
= GetLastPosition() ;
2130 for ( n
= 0 ; n
<= (ItemCount
) lastpos
; ++n
)
2132 if ( y
== ypos
&& x
== xpos
)
2135 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2137 if ( curpt
.v
> lastHeight
)
2143 lastHeight
= curpt
.v
;
2152 bool wxMacMLTEControl::PositionToXY( long pos
, long *x
, long *y
) const
2162 lastpos
= GetLastPosition() ;
2163 if ( pos
<= lastpos
)
2165 // TODO: find a better implementation - while we can get the
2166 // line metrics of a certain line, we don't get its starting
2167 // position, so it would probably be rather a binary search
2168 // for the start position
2169 long xpos
= 0, ypos
= 0 ;
2170 int lastHeight
= 0 ;
2173 for ( n
= 0 ; n
<= (ItemCount
) pos
; ++n
)
2175 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2177 if ( curpt
.v
> lastHeight
)
2183 lastHeight
= curpt
.v
;
2198 void wxMacMLTEControl::ShowPosition( long pos
)
2200 Point current
, desired
;
2201 TXNOffset selstart
, selend
;
2203 TXNGetSelection( m_txn
, &selstart
, &selend
);
2204 TXNOffsetToPoint( m_txn
, selstart
, ¤t
);
2205 TXNOffsetToPoint( m_txn
, pos
, &desired
);
2207 // TODO: use HIPoints for 10.3 and above
2209 OSErr theErr
= noErr
;
2210 long dv
= desired
.v
- current
.v
;
2211 long dh
= desired
.h
- current
.h
;
2212 TXNShowSelection( m_txn
, kTXNShowStart
) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2213 theErr
= TXNScroll( m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
, &dv
, &dh
);
2215 // there will be an error returned for classic MLTE implementation when the control is
2216 // invisible, but HITextView works correctly, so we don't assert that one
2217 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2220 void wxMacMLTEControl::SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
)
2223 #if SIZEOF_WCHAR_T == 2
2224 size_t len
= st
.length() ;
2225 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)st
.wc_str(), len
* 2, start
, end
);
2227 wxMBConvUTF16 converter
;
2228 ByteCount byteBufferLen
= converter
.WC2MB( NULL
, st
.wc_str(), 0 ) ;
2229 UniChar
*unibuf
= (UniChar
*)malloc( byteBufferLen
) ;
2230 converter
.WC2MB( (char*)unibuf
, st
.wc_str(), byteBufferLen
) ;
2231 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)unibuf
, byteBufferLen
, start
, end
) ;
2235 wxCharBuffer text
= st
.mb_str( wxConvLocal
) ;
2236 TXNSetData( m_txn
, kTXNTextData
, (void*)text
.data(), strlen( text
), start
, end
) ;
2240 wxString
wxMacMLTEControl::GetLineText(long lineNo
) const
2244 if ( lineNo
< GetNumberOfLines() )
2247 Fixed lineWidth
, lineHeight
, currentHeight
;
2250 // get the first possible position in the control
2251 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2253 // Iterate through the lines until we reach the one we want,
2254 // adding to our current y pixel point position
2257 while (ypos
< lineNo
)
2259 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2260 currentHeight
+= lineHeight
;
2263 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2264 TXNOffset theOffset
;
2265 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2267 wxString content
= GetStringValue() ;
2268 Point currentPoint
= thePoint
;
2269 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2271 line
+= content
[theOffset
];
2272 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2279 int wxMacMLTEControl::GetLineLength(long lineNo
) const
2283 if ( lineNo
< GetNumberOfLines() )
2286 Fixed lineWidth
, lineHeight
, currentHeight
;
2289 // get the first possible position in the control
2290 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2292 // Iterate through the lines until we reach the one we want,
2293 // adding to our current y pixel point position
2296 while (ypos
< lineNo
)
2298 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2299 currentHeight
+= lineHeight
;
2302 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2303 TXNOffset theOffset
;
2304 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2306 wxString content
= GetStringValue() ;
2307 Point currentPoint
= thePoint
;
2308 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2311 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2318 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
2320 // ----------------------------------------------------------------------------
2321 // MLTE control implementation (classic part)
2322 // ----------------------------------------------------------------------------
2324 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2325 // has to live on. We have different problems coming from outdated implementations on the
2326 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2327 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2328 // no way out, therefore we are using our own implementation and our own scrollbars ....
2330 #ifdef __WXMAC_OSX__
2332 TXNScrollInfoUPP gTXNScrollInfoProc
= NULL
;
2333 ControlActionUPP gTXNScrollActionProc
= NULL
;
2335 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2336 SInt32 iValue
, SInt32 iMaximumValue
,
2337 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
)
2339 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) iRefCon
;
2340 SInt32 value
= wxMax( iValue
, 0 ) ;
2341 SInt32 maximum
= wxMax( iMaximumValue
, 0 ) ;
2343 if ( iScrollBarOrientation
== kTXNHorizontal
)
2345 if ( mlte
->m_sbHorizontal
)
2347 SetControl32BitValue( mlte
->m_sbHorizontal
, value
) ;
2348 SetControl32BitMaximum( mlte
->m_sbHorizontal
, maximum
) ;
2349 mlte
->m_lastHorizontalValue
= value
;
2352 else if ( iScrollBarOrientation
== kTXNVertical
)
2354 if ( mlte
->m_sbVertical
)
2356 SetControl32BitValue( mlte
->m_sbVertical
, value
) ;
2357 SetControl32BitMaximum( mlte
->m_sbVertical
, maximum
) ;
2358 mlte
->m_lastVerticalValue
= value
;
2363 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
)
2365 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) GetControlReference( controlRef
) ;
2369 if ( controlRef
!= mlte
->m_sbVertical
&& controlRef
!= mlte
->m_sbHorizontal
)
2373 bool isHorizontal
= ( controlRef
== mlte
->m_sbHorizontal
) ;
2375 SInt32 minimum
= 0 ;
2376 SInt32 maximum
= GetControl32BitMaximum( controlRef
) ;
2377 SInt32 value
= GetControl32BitValue( controlRef
) ;
2382 case kControlDownButtonPart
:
2386 case kControlUpButtonPart
:
2390 case kControlPageDownPart
:
2391 delta
= GetControlViewSize( controlRef
) ;
2394 case kControlPageUpPart
:
2395 delta
= -GetControlViewSize( controlRef
) ;
2398 case kControlIndicatorPart
:
2399 delta
= value
- (isHorizontal
? mlte
->m_lastHorizontalValue
: mlte
->m_lastVerticalValue
) ;
2408 SInt32 newValue
= value
;
2410 if ( partCode
!= kControlIndicatorPart
)
2412 if ( value
+ delta
< minimum
)
2413 delta
= minimum
- value
;
2414 if ( value
+ delta
> maximum
)
2415 delta
= maximum
- value
;
2417 SetControl32BitValue( controlRef
, value
+ delta
) ;
2418 newValue
= value
+ delta
;
2421 SInt32 verticalDelta
= isHorizontal
? 0 : delta
;
2422 SInt32 horizontalDelta
= isHorizontal
? delta
: 0 ;
2425 mlte
->m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
,
2426 &verticalDelta
, &horizontalDelta
);
2427 verify_noerr( err
);
2430 mlte
->m_lastHorizontalValue
= newValue
;
2432 mlte
->m_lastVerticalValue
= newValue
;
2437 // make correct activations
2438 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive
)
2440 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference(m_controlRef
);
2442 wxMacWindowClipper
clipper( textctrl
) ;
2443 TXNActivate( m_txn
, m_txnFrameID
, setActive
);
2445 ControlRef controlFocus
= 0 ;
2446 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2447 if ( controlFocus
== m_controlRef
)
2448 TXNFocus( m_txn
, setActive
);
2451 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus
)
2453 TXNFocus( m_txn
, setFocus
);
2456 // guards against inappropriate redraw (hidden objects drawing onto window)
2458 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis
)
2460 ControlRef controlFocus
= 0 ;
2461 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2463 if ( !vis
&& (controlFocus
== m_controlRef
) )
2464 SetKeyboardFocus( m_txnWindow
, m_controlRef
, kControlFocusNoPart
) ;
2466 TXNControlTag iControlTags
[1] = { kTXNVisibilityTag
};
2467 TXNControlData iControlData
[1] = { { (UInt32
)false } };
2469 verify_noerr( TXNGetTXNObjectControls( m_txn
, 1, iControlTags
, iControlData
) ) ;
2471 if ( iControlData
[0].uValue
!= vis
)
2473 iControlData
[0].uValue
= vis
;
2474 verify_noerr( TXNSetTXNObjectControls( m_txn
, false , 1, iControlTags
, iControlData
) ) ;
2477 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2478 // if we run into further problems we might set the FrameBounds to an empty rect here
2481 // make sure that the TXNObject is at the right position
2483 void wxMacMLTEClassicControl::MacUpdatePosition()
2485 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2486 if ( textctrl
== NULL
)
2490 UMAGetControlBoundsInWindowCoords( m_controlRef
, &bounds
);
2492 wxRect visRect
= textctrl
->MacGetClippedClientRect() ;
2493 Rect visBounds
= { visRect
.y
, visRect
.x
, visRect
.y
+ visRect
.height
, visRect
.x
+ visRect
.width
} ;
2496 textctrl
->MacWindowToRootWindow( &x
, &y
) ;
2497 OffsetRect( &visBounds
, x
, y
) ;
2499 if ( !EqualRect( &bounds
, &m_txnControlBounds
) || !EqualRect( &visBounds
, &m_txnVisBounds
) )
2501 m_txnControlBounds
= bounds
;
2502 m_txnVisBounds
= visBounds
;
2503 wxMacWindowClipper
cl( textctrl
) ;
2505 #ifdef __WXMAC_OSX__
2506 if ( m_sbHorizontal
|| m_sbVertical
)
2508 int w
= bounds
.right
- bounds
.left
;
2509 int h
= bounds
.bottom
- bounds
.top
;
2511 if ( m_sbHorizontal
)
2515 sbBounds
.left
= -1 ;
2516 sbBounds
.top
= h
- 14 ;
2517 sbBounds
.right
= w
+ 1 ;
2518 sbBounds
.bottom
= h
+ 1 ;
2520 SetControlBounds( m_sbHorizontal
, &sbBounds
) ;
2521 SetControlViewSize( m_sbHorizontal
, w
) ;
2528 sbBounds
.left
= w
- 14 ;
2530 sbBounds
.right
= w
+ 1 ;
2531 sbBounds
.bottom
= m_sbHorizontal
? h
- 14 : h
+ 1 ;
2533 SetControlBounds( m_sbVertical
, &sbBounds
) ;
2534 SetControlViewSize( m_sbVertical
, h
) ;
2539 TXNLongRect olddestRect
;
2540 TXNGetRectBounds( m_txn
, &oldviewRect
, &olddestRect
, NULL
) ;
2542 Rect viewRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2543 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2544 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2545 TXNLongRect destRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2546 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2547 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2549 if ( olddestRect
.right
>= 10000 )
2550 destRect
.right
= destRect
.left
+ 32000 ;
2552 if ( olddestRect
.bottom
>= 0x20000000 )
2553 destRect
.bottom
= destRect
.top
+ 0x40000000 ;
2555 SectRect( &viewRect
, &visBounds
, &viewRect
) ;
2556 TXNSetRectBounds( m_txn
, &viewRect
, &destRect
, true ) ;
2561 m_txnControlBounds
.top
,
2562 m_txnControlBounds
.left
,
2563 m_txnControlBounds
.bottom
- (m_sbHorizontal
? 14 : 0),
2564 m_txnControlBounds
.right
- (m_sbVertical
? 14 : 0),
2570 m_txn
, m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2571 wxMax( m_txnControlBounds
.bottom
, m_txnControlBounds
.top
),
2572 wxMax( m_txnControlBounds
.right
, m_txnControlBounds
.left
), m_txnFrameID
);
2575 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2576 // movement, therefore we have to force it
2578 // this problem has been reported in OSX as well, so we use this here once again
2580 TXNLongRect textRect
;
2581 TXNGetRectBounds( m_txn
, NULL
, NULL
, &textRect
) ;
2582 if ( textRect
.left
< m_txnControlBounds
.left
)
2583 TXNShowSelection( m_txn
, kTXNShowStart
) ;
2587 void wxMacMLTEClassicControl::SetRect( Rect
*r
)
2589 wxMacControl::SetRect( r
) ;
2590 MacUpdatePosition() ;
2593 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16
WXUNUSED(thePart
))
2595 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2596 if ( textctrl
== NULL
)
2599 if ( textctrl
->MacIsReallyShown() )
2601 wxMacWindowClipper
clipper( textctrl
) ;
2602 TXNDraw( m_txn
, NULL
) ;
2606 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
2608 Point where
= { y
, x
} ;
2609 ControlPartCode result
= kControlNoPart
;
2611 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2612 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2614 if (PtInRect( where
, &m_txnControlBounds
))
2616 result
= kControlEditTextPart
;
2620 // sometimes we get the coords also in control local coordinates, therefore test again
2622 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2626 if (PtInRect( where
, &m_txnControlBounds
))
2627 result
= kControlEditTextPart
;
2634 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x
, wxInt16 y
, void* WXUNUSED(actionProc
) )
2636 ControlPartCode result
= kControlNoPart
;
2638 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2639 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2641 Point startPt
= { y
, x
} ;
2643 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2645 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2649 switch (MacControlUserPaneHitTestProc( startPt
.h
, startPt
.v
))
2651 case kControlEditTextPart
:
2653 wxMacWindowClipper
clipper( textctrl
) ;
2656 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
2657 TXNClick( m_txn
, &rec
);
2669 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2671 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2672 if ( textctrl
== NULL
)
2675 if (textctrl
->MacIsReallyShown())
2677 if (IsControlActive(m_controlRef
))
2681 wxMacWindowClipper
clipper( textctrl
) ;
2686 if (PtInRect(mousep
, &m_txnControlBounds
))
2688 RgnHandle theRgn
= NewRgn();
2689 RectRgn(theRgn
, &m_txnControlBounds
);
2690 TXNAdjustCursor(m_txn
, theRgn
);
2697 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
2699 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2700 if ( textctrl
== NULL
)
2701 return kControlNoPart
;
2703 wxMacWindowClipper
clipper( textctrl
) ;
2706 memset( &ev
, 0 , sizeof( ev
) ) ;
2708 ev
.modifiers
= modifiers
;
2709 ev
.message
= ((keyCode
<< 8) & keyCodeMask
) | (charCode
& charCodeMask
);
2710 TXNKeyDown( m_txn
, &ev
);
2712 return kControlEntireControl
;
2715 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating
)
2717 MacActivatePaneText( activating
);
2720 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action
)
2722 ControlPartCode focusResult
= kControlFocusNoPart
;
2724 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2725 if ( textctrl
== NULL
)
2728 wxMacWindowClipper
clipper( textctrl
) ;
2730 ControlRef controlFocus
= NULL
;
2731 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2732 bool wasFocused
= ( controlFocus
== m_controlRef
) ;
2736 case kControlFocusPrevPart
:
2737 case kControlFocusNextPart
:
2738 MacFocusPaneText( !wasFocused
);
2739 focusResult
= (!wasFocused
? (ControlPartCode
) kControlEditTextPart
: (ControlPartCode
) kControlFocusNoPart
);
2742 case kControlFocusNoPart
:
2744 MacFocusPaneText( false );
2745 focusResult
= kControlFocusNoPart
;
2752 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info
) )
2756 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
2757 const wxString
& str
,
2759 const wxSize
& size
, long style
)
2760 : wxMacMLTEControl( wxPeer
)
2762 m_font
= wxPeer
->GetFont() ;
2763 m_windowStyle
= style
;
2764 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
2767 kControlSupportsEmbedding
| kControlSupportsFocus
| kControlWantsIdle
2768 | kControlWantsActivate
| kControlHandlesTracking
2769 // | kControlHasSpecialBackground
2770 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
2772 OSStatus err
= ::CreateUserPaneControl(
2773 MAC_WXHWND(wxPeer
->GetParent()->MacGetTopLevelWindowRef()),
2774 &bounds
, featureSet
, &m_controlRef
);
2775 verify_noerr( err
);
2779 AdjustCreationAttributes( *wxWHITE
, true ) ;
2781 MacSetObjectVisibility( wxPeer
->MacIsReallyShown() ) ;
2785 wxMacConvertNewlines10To13( &st
) ;
2786 wxMacWindowClipper
clipper( m_peer
) ;
2787 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
2788 TXNSetSelection( m_txn
, 0, 0 ) ;
2792 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2794 TXNDeleteObject( m_txn
);
2798 void wxMacMLTEClassicControl::VisibilityChanged(bool shown
)
2800 MacSetObjectVisibility( shown
) ;
2801 wxMacControl::VisibilityChanged( shown
) ;
2804 void wxMacMLTEClassicControl::SuperChangedPosition()
2806 MacUpdatePosition() ;
2807 wxMacControl::SuperChangedPosition() ;
2810 #ifdef __WXMAC_OSX__
2812 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
2813 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
2814 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
2815 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
2816 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
2817 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
2818 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
2820 static pascal void wxMacControlUserPaneDrawProc(ControlRef control
, SInt16 part
)
2822 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2823 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2825 win
->MacControlUserPaneDrawProc( part
) ;
2828 static pascal ControlPartCode
wxMacControlUserPaneHitTestProc(ControlRef control
, Point where
)
2830 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2831 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2833 return win
->MacControlUserPaneHitTestProc( where
.h
, where
.v
) ;
2835 return kControlNoPart
;
2838 static pascal ControlPartCode
wxMacControlUserPaneTrackingProc(ControlRef control
, Point startPt
, ControlActionUPP actionProc
)
2840 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2841 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2843 return win
->MacControlUserPaneTrackingProc( startPt
.h
, startPt
.v
, (void*) actionProc
) ;
2845 return kControlNoPart
;
2848 static pascal void wxMacControlUserPaneIdleProc(ControlRef control
)
2850 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2851 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2853 win
->MacControlUserPaneIdleProc() ;
2856 static pascal ControlPartCode
wxMacControlUserPaneKeyDownProc(ControlRef control
, SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
)
2858 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2859 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2861 return win
->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
2863 return kControlNoPart
;
2866 static pascal void wxMacControlUserPaneActivateProc(ControlRef control
, Boolean activating
)
2868 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2869 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2871 win
->MacControlUserPaneActivateProc( activating
) ;
2874 static pascal ControlPartCode
wxMacControlUserPaneFocusProc(ControlRef control
, ControlFocusPart action
)
2876 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2877 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2879 return win
->MacControlUserPaneFocusProc( action
) ;
2881 return kControlNoPart
;
2885 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control
, ControlBackgroundPtr info
)
2887 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2888 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2890 win
->MacControlUserPaneBackgroundProc(info
) ;
2894 #endif // __WXMAC_OSX__
2896 // TXNRegisterScrollInfoProc
2898 OSStatus
wxMacMLTEClassicControl::DoCreate()
2901 OSStatus err
= noErr
;
2903 // set up our globals
2904 #ifdef __WXMAC_OSX__
2905 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc
);
2906 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc
);
2907 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc
);
2908 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc
);
2909 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc
);
2910 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc
);
2911 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc
);
2913 if (gTXNScrollInfoProc
== NULL
) gTXNScrollInfoProc
= NewTXNScrollInfoUPP(TXNScrollInfoProc
) ;
2914 if (gTXNScrollActionProc
== NULL
) gTXNScrollActionProc
= NewControlActionUPP(TXNScrollActionProc
) ;
2917 // set the initial settings for our private data
2919 m_txnWindow
= GetControlOwner(m_controlRef
);
2920 m_txnPort
= (GrafPtr
) GetWindowPort(m_txnWindow
);
2922 #ifdef __WXMAC_OSX__
2923 // set up the user pane procedures
2924 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
2925 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
2926 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
2927 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
2928 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
2929 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
2930 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
2933 // calculate the rectangles used by the control
2934 UMAGetControlBoundsInWindowCoords( m_controlRef
, &bounds
);
2936 m_txnControlBounds
= bounds
;
2937 m_txnVisBounds
= bounds
;
2942 GetGWorld( &origPort
, &origDev
) ;
2943 SetPort( m_txnPort
);
2945 // create the new edit field
2946 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( m_windowStyle
);
2948 #ifdef __WXMAC_OSX__
2949 // the scrollbars are not correctly embedded but are inserted at the root:
2950 // this gives us problems as we have erratic redraws even over the structure area
2952 m_sbHorizontal
= 0 ;
2954 m_lastHorizontalValue
= 0 ;
2955 m_lastVerticalValue
= 0 ;
2957 Rect sb
= { 0 , 0 , 0 , 0 } ;
2958 if ( frameOptions
& kTXNWantVScrollBarMask
)
2960 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbVertical
);
2961 SetControlReference( m_sbVertical
, (SInt32
)this );
2962 SetControlAction( m_sbVertical
, gTXNScrollActionProc
);
2963 ShowControl( m_sbVertical
);
2964 EmbedControl( m_sbVertical
, m_controlRef
);
2965 frameOptions
&= ~kTXNWantVScrollBarMask
;
2968 if ( frameOptions
& kTXNWantHScrollBarMask
)
2970 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbHorizontal
);
2971 SetControlReference( m_sbHorizontal
, (SInt32
)this );
2972 SetControlAction( m_sbHorizontal
, gTXNScrollActionProc
);
2973 ShowControl( m_sbHorizontal
);
2974 EmbedControl( m_sbHorizontal
, m_controlRef
);
2975 frameOptions
&= ~(kTXNWantHScrollBarMask
| kTXNDrawGrowIconMask
);
2981 NULL
, m_txnWindow
, &bounds
, frameOptions
,
2982 kTXNTextEditStyleFrameType
, kTXNTextensionFile
, kTXNSystemDefaultEncoding
,
2983 &m_txn
, &m_txnFrameID
, NULL
);
2984 verify_noerr( err
);
2987 TXNControlTag iControlTags
[] = { kTXNUseCarbonEvents
};
2988 TXNControlData iControlData
[] = { { (UInt32
)&cInfo
} };
2989 int toptag
= WXSIZEOF( iControlTags
) ;
2990 TXNCarbonEventInfo cInfo
;
2991 cInfo
.useCarbonEvents
= false ;
2994 cInfo
.fDictionary
= NULL
;
2996 verify_noerr( TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) );
2999 #ifdef __WXMAC_OSX__
3000 TXNRegisterScrollInfoProc( m_txn
, gTXNScrollInfoProc
, (SInt32
)this );
3003 SetGWorld( origPort
, origDev
) ;
3009 // ----------------------------------------------------------------------------
3010 // MLTE control implementation (OSX part)
3011 // ----------------------------------------------------------------------------
3013 // tiger multi-line textcontrols with no CR in the entire content
3014 // don't scroll automatically, so we need a hack.
3015 // This attempt only works 'before' the key (ie before CallNextEventHandler)
3016 // is processed, thus the scrolling always occurs one character too late, but
3017 // better than nothing ...
3019 static const EventTypeSpec eventList
[] =
3021 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
3024 static pascal OSStatus
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
3026 OSStatus result
= eventNotHandledErr
;
3027 wxMacMLTEHIViewControl
* focus
= (wxMacMLTEHIViewControl
*) data
;
3029 switch ( GetEventKind( event
) )
3031 case kEventTextInputUnicodeForKeyEvent
:
3033 if ( UMAGetSystemVersion() >= 0x1040 )
3035 TXNOffset from
, to
;
3036 TXNGetSelection( focus
->GetTXNObject() , &from
, &to
) ;
3038 TXNShowSelection( focus
->GetTXNObject() , kTXNShowStart
);
3040 result
= CallNextEventHandler(handler
,event
);
3050 static pascal OSStatus
wxMacTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
3052 OSStatus result
= eventNotHandledErr
;
3054 switch ( GetEventClass( event
) )
3056 case kEventClassTextInput
:
3057 result
= wxMacUnicodeTextEventHandler( handler
, event
, data
) ;
3066 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler
)
3068 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
3069 const wxString
& str
,
3071 const wxSize
& size
, long style
) : wxMacMLTEControl( wxPeer
)
3073 m_font
= wxPeer
->GetFont() ;
3074 m_windowStyle
= style
;
3075 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
3077 wxMacConvertNewlines10To13( &st
) ;
3080 { bounds
.left
, bounds
.top
},
3081 { bounds
.right
- bounds
.left
, bounds
.bottom
- bounds
.top
} } ;
3083 m_scrollView
= NULL
;
3084 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( style
) ;
3085 if (( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
)) || !(frameOptions
&kTXNSingleLineOnlyMask
))
3087 if ( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
) )
3090 (frameOptions
& kTXNWantHScrollBarMask
? kHIScrollViewOptionsHorizScroll
: 0)
3091 | (frameOptions
& kTXNWantVScrollBarMask
? kHIScrollViewOptionsVertScroll
: 0) ,
3096 HIScrollViewCreate(kHIScrollViewOptionsVertScroll
,&m_scrollView
);
3097 HIScrollViewSetScrollBarAutoHide(m_scrollView
,true);
3100 HIViewSetFrame( m_scrollView
, &hr
);
3101 HIViewSetVisible( m_scrollView
, true );
3105 HITextViewCreate( NULL
, 0, frameOptions
, &m_textView
) ;
3106 m_txn
= HITextViewGetTXNObject( m_textView
) ;
3107 HIViewSetVisible( m_textView
, true ) ;
3110 HIViewAddSubview( m_scrollView
, m_textView
) ;
3111 m_controlRef
= m_scrollView
;
3112 wxPeer
->MacInstallEventHandler( (WXWidget
) m_textView
) ;
3116 HIViewSetFrame( m_textView
, &hr
);
3117 m_controlRef
= m_textView
;
3120 AdjustCreationAttributes( *wxWHITE
, true ) ;
3122 wxMacWindowClipper
c( m_peer
) ;
3124 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
3126 TXNSetSelection( m_txn
, 0, 0 );
3127 TXNShowSelection( m_txn
, kTXNShowStart
);
3129 InstallControlEventHandler( m_textView
, GetwxMacTextControlEventHandlerUPP(),
3130 GetEventTypeCount(eventList
), eventList
, this,
3134 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3138 OSStatus
wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart
)
3140 return SetKeyboardFocus( GetControlOwner( m_textView
), m_textView
, focusPart
) ;
3143 bool wxMacMLTEHIViewControl::HasFocus() const
3145 ControlRef control
;
3146 if ( GetUserFocusWindow() == NULL
)
3149 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
3150 return control
== m_textView
;
3153 void wxMacMLTEHIViewControl::SetBackground( const wxBrush
&brush
)
3155 wxMacMLTEControl::SetBackground( brush
) ;
3158 CGColorSpaceRef rgbSpace
= CGColorSpaceCreateDeviceRGB();
3159 RGBColor col
= MAC_WXCOLORREF(brush
.GetColour().GetPixel()) ;
3161 float component
[4] ;
3162 component
[0] = col
.red
/ 65536.0 ;
3163 component
[1] = col
.green
/ 65536.0 ;
3164 component
[2] = col
.blue
/ 65536.0 ;
3165 component
[3] = 1.0 ; // alpha
3167 CGColorRef color
= CGColorCreate( rgbSpace
, component
);
3168 HITextViewSetBackgroundColor( m_textView
, color
);
3169 CGColorSpaceRelease( rgbSpace
);
3173 #endif // wxUSE_TEXTCTRL