1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/textctrl.cpp
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/textctrl.h"
23 #include "wx/button.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
31 #include <sys/types.h>
37 #if wxUSE_STD_IOSTREAM
45 #include "wx/filefn.h"
46 #include "wx/sysopt.h"
48 #include "wx/mac/uma.h"
49 #include "wx/mac/carbon/private/mactext.h"
55 virtual ~wxMacFunctor() {}
57 virtual void* operator()() = 0 ;
59 static void* CallBackProc( void *param
)
61 wxMacFunctor
* f
= (wxMacFunctor
*) param
;
62 void *result
= (*f
)() ;
67 template<typename classtype
, typename param1type
>
69 class wxMacObjectFunctor1
: public wxMacFunctor
71 typedef void (classtype::*function
)( param1type p1
) ;
72 typedef void (classtype::*ref_function
)( const param1type
& p1
) ;
74 wxMacObjectFunctor1( classtype
*obj
, function f
, param1type p1
) :
82 wxMacObjectFunctor1( classtype
*obj
, ref_function f
, param1type p1
) :
90 virtual ~wxMacObjectFunctor1() {}
92 virtual void* operator()()
94 (m_object
->*m_function
)( m_param1
) ;
100 param1type m_param1
;
103 function m_function
;
104 ref_function m_refFunction
;
108 template<typename classtype
, typename param1type
>
109 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
111 wxMacObjectFunctor1
<classtype
, param1type
> params(object
, function
, p1
) ;
113 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
117 template<typename classtype
, typename param1type
>
118 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
120 wxMacObjectFunctor1
<classtype
,param1type
> params(object
, function
, p1
) ;
122 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
126 template<typename classtype
, typename param1type
>
127 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
130 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
135 template<typename classtype
, typename param1type
>
136 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
139 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
145 // common parts for implementations based on MLTE
147 class wxMacMLTEControl
: public wxMacTextControl
150 wxMacMLTEControl( wxTextCtrl
*peer
) ;
152 virtual wxString
GetStringValue() const ;
153 virtual void SetStringValue( const wxString
&str
) ;
155 static TXNFrameOptions
FrameOptionsFromWXStyle( long wxStyle
) ;
157 void AdjustCreationAttributes( const wxColour
& background
, bool visible
) ;
159 virtual void SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
) ;
160 virtual void SetBackground( const wxBrush
&brush
) ;
161 virtual void SetStyle( long start
, long end
, const wxTextAttr
& style
) ;
162 virtual void Copy() ;
164 virtual void Paste() ;
165 virtual bool CanPaste() const ;
166 virtual void SetEditable( bool editable
) ;
167 virtual wxTextPos
GetLastPosition() const ;
168 virtual void Replace( long from
, long to
, const wxString
&str
) ;
169 virtual void Remove( long from
, long to
) ;
170 virtual void GetSelection( long* from
, long* to
) const ;
171 virtual void SetSelection( long from
, long to
) ;
173 virtual void WriteText( const wxString
& str
) ;
175 virtual bool HasOwnContextMenu() const
177 TXNCommandEventSupportOptions options
;
178 TXNGetCommandEventSupport( m_txn
, & options
) ;
179 return options
& kTXNSupportEditCommandProcessing
;
182 virtual void CheckSpelling(bool check
)
184 TXNSetSpellCheckAsYouType( m_txn
, (Boolean
) check
);
186 virtual void Clear() ;
188 virtual bool CanUndo() const ;
189 virtual void Undo() ;
190 virtual bool CanRedo() const;
191 virtual void Redo() ;
192 virtual int GetNumberOfLines() const ;
193 virtual long XYToPosition(long x
, long y
) const ;
194 virtual bool PositionToXY(long pos
, long *x
, long *y
) const ;
195 virtual void ShowPosition( long pos
) ;
196 virtual int GetLineLength(long lineNo
) const ;
197 virtual wxString
GetLineText(long lineNo
) const ;
199 void SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
) ;
200 TXNObject
GetTXNObject() { return m_txn
; }
203 void TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
) ;
208 // implementation available under OSX
210 class wxMacMLTEHIViewControl
: public wxMacMLTEControl
213 wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
216 const wxSize
& size
, long style
) ;
217 virtual ~wxMacMLTEHIViewControl() ;
219 virtual OSStatus
SetFocus( ControlFocusPart focusPart
) ;
220 virtual bool HasFocus() const ;
221 virtual void SetBackground( const wxBrush
&brush
) ;
224 HIViewRef m_scrollView
;
225 HIViewRef m_textView
;
228 // 'classic' MLTE implementation
230 class wxMacMLTEClassicControl
: public wxMacMLTEControl
233 wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
236 const wxSize
& size
, long style
) ;
237 virtual ~wxMacMLTEClassicControl() ;
239 virtual void VisibilityChanged(bool shown
) ;
240 virtual void SuperChangedPosition() ;
242 virtual void MacControlUserPaneDrawProc(wxInt16 part
) ;
243 virtual wxInt16
MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
) ;
244 virtual wxInt16
MacControlUserPaneTrackingProc(wxInt16 x
, wxInt16 y
, void* actionProc
) ;
245 virtual void MacControlUserPaneIdleProc() ;
246 virtual wxInt16
MacControlUserPaneKeyDownProc(wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
) ;
247 virtual void MacControlUserPaneActivateProc(bool activating
) ;
248 virtual wxInt16
MacControlUserPaneFocusProc(wxInt16 action
) ;
249 virtual void MacControlUserPaneBackgroundProc(void* info
) ;
251 virtual bool SetupCursor( const wxPoint
& WXUNUSED(pt
) )
253 MacControlUserPaneIdleProc();
257 virtual void SetRect( Rect
*r
) ;
262 void MacUpdatePosition() ;
263 void MacActivatePaneText(bool setActive
) ;
264 void MacFocusPaneText(bool setFocus
) ;
265 void MacSetObjectVisibility(bool vis
) ;
268 TXNFrameID m_txnFrameID
;
270 WindowRef m_txnWindow
;
271 // bounds of the control as we last did set the txn frames
272 Rect m_txnControlBounds
;
273 Rect m_txnVisBounds
;
275 static pascal void TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
) ;
276 static pascal void TXNScrollInfoProc(
277 SInt32 iValue
, SInt32 iMaximumValue
,
278 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
) ;
280 ControlRef m_sbHorizontal
;
281 SInt32 m_lastHorizontalValue
;
282 ControlRef m_sbVertical
;
283 SInt32 m_lastVerticalValue
;
287 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
289 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
290 EVT_ERASE_BACKGROUND( wxTextCtrl::OnEraseBackground
)
291 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
292 EVT_CHAR(wxTextCtrl::OnChar
)
293 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
294 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
295 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
296 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
297 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
298 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
299 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
301 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
303 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
304 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
305 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
306 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
307 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
308 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
309 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
313 void wxTextCtrl::Init()
319 m_privateContextMenu
= NULL
;
320 m_triggerOnSetValue
= true ;
323 wxTextCtrl::~wxTextCtrl()
325 delete m_privateContextMenu
;
328 bool wxTextCtrl::Create( wxWindow
*parent
,
334 const wxValidator
& validator
,
335 const wxString
& name
)
337 m_macIsUserPane
= false ;
340 if ( ! (style
& wxNO_BORDER
) )
341 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
343 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
346 if ( m_windowStyle
& wxTE_MULTILINE
)
348 // always turn on this style for multi-line controls
349 m_windowStyle
|= wxTE_PROCESS_ENTER
;
350 style
|= wxTE_PROCESS_ENTER
;
353 CreatePeer( str
, pos
, size
, style
);
355 MacPostControlCreate(pos
, size
) ;
357 // only now the embedding is correct and we can do a positioning update
359 MacSuperChangedPosition() ;
361 if ( m_windowStyle
& wxTE_READONLY
)
362 SetEditable( false ) ;
364 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
369 void wxTextCtrl::CreatePeer(
372 const wxSize
& size
, long style
)
374 bool forceMLTE
= false ;
376 #if wxUSE_SYSTEM_OPTIONS
377 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE
) == 1))
383 if ( UMAGetSystemVersion() >= 0x1050 )
388 if ( m_windowStyle
& wxTE_MULTILINE
)
389 m_peer
= new wxMacMLTEHIViewControl( this , str
, pos
, size
, style
) ;
394 if ( !(m_windowStyle
& wxTE_MULTILINE
) && !forceMLTE
)
396 m_peer
= new wxMacUnicodeTextControl( this , str
, pos
, size
, style
) ;
400 // the horizontal single line scrolling bug that made us keep the classic implementation
402 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
404 m_peer
= new wxMacMLTEClassicControl( this , str
, pos
, size
, style
) ;
408 void wxTextCtrl::MacSuperChangedPosition()
410 wxWindow::MacSuperChangedPosition() ;
411 GetPeer()->SuperChangedPosition() ;
414 void wxTextCtrl::MacVisibilityChanged()
416 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
419 void wxTextCtrl::MacEnabledStateChanged()
423 void wxTextCtrl::MacCheckSpelling(bool check
)
425 GetPeer()->CheckSpelling(check
);
428 wxString
wxTextCtrl::GetValue() const
430 return GetPeer()->GetStringValue() ;
433 void wxTextCtrl::GetSelection(long* from
, long* to
) const
435 GetPeer()->GetSelection( from
, to
) ;
438 void wxTextCtrl::DoSetValue(const wxString
& str
, int flags
)
441 if ( GetValue() == str
)
444 GetPeer()->SetStringValue( str
) ;
446 if ( (flags
& SetValue_SendEvent
) && m_triggerOnSetValue
)
448 SendTextUpdatedEvent();
452 void wxTextCtrl::SetMaxLength(unsigned long len
)
457 bool wxTextCtrl::SetFont( const wxFont
& font
)
459 if ( !wxTextCtrlBase::SetFont( font
) )
462 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle() ) ;
467 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
469 GetPeer()->SetStyle( start
, end
, style
) ;
474 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
476 wxTextCtrlBase::SetDefaultStyle( style
) ;
477 SetStyle( kTXNUseCurrentSelection
, kTXNUseCurrentSelection
, GetDefaultStyle() ) ;
482 // Clipboard operations
484 void wxTextCtrl::Copy()
490 void wxTextCtrl::Cut()
496 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
497 event
.SetEventObject( this );
498 GetEventHandler()->ProcessEvent( event
);
502 void wxTextCtrl::Paste()
508 // TODO: eventually we should add setting the default style again
510 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
511 event
.SetEventObject( this );
512 GetEventHandler()->ProcessEvent( event
);
516 bool wxTextCtrl::CanCopy() const
518 // Can copy if there's a selection
520 GetSelection( &from
, &to
);
525 bool wxTextCtrl::CanCut() const
530 // Can cut if there's a selection
532 GetSelection( &from
, &to
);
537 bool wxTextCtrl::CanPaste() const
542 return GetPeer()->CanPaste() ;
545 void wxTextCtrl::SetEditable(bool editable
)
547 if ( editable
!= m_editable
)
549 m_editable
= editable
;
550 GetPeer()->SetEditable( editable
) ;
554 void wxTextCtrl::SetInsertionPoint(long pos
)
556 SetSelection( pos
, pos
) ;
559 void wxTextCtrl::SetInsertionPointEnd()
561 wxTextPos pos
= GetLastPosition();
562 SetInsertionPoint( pos
);
565 long wxTextCtrl::GetInsertionPoint() const
568 GetSelection( &begin
, &end
) ;
573 wxTextPos
wxTextCtrl::GetLastPosition() const
575 return GetPeer()->GetLastPosition() ;
578 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
580 GetPeer()->Replace( from
, to
, str
) ;
583 void wxTextCtrl::Remove(long from
, long to
)
585 GetPeer()->Remove( from
, to
) ;
588 void wxTextCtrl::SetSelection(long from
, long to
)
590 GetPeer()->SetSelection( from
, to
) ;
593 void wxTextCtrl::WriteText(const wxString
& str
)
595 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
596 if ( !wxIsMainThread() )
598 // unfortunately CW 8 is not able to correctly deduce the template types,
599 // so we have to instantiate explicitly
600 wxMacMPRemoteGUICall
<wxTextCtrl
,wxString
>( this , &wxTextCtrl::WriteText
, str
) ;
605 GetPeer()->WriteText( str
) ;
608 void wxTextCtrl::AppendText(const wxString
& text
)
610 SetInsertionPointEnd();
614 void wxTextCtrl::Clear()
619 bool wxTextCtrl::IsModified() const
624 bool wxTextCtrl::IsEditable() const
626 return IsEnabled() && m_editable
;
629 bool wxTextCtrl::AcceptsFocus() const
631 // we don't want focus if we can't be edited
632 return /*IsEditable() && */ wxControl::AcceptsFocus();
635 wxSize
wxTextCtrl::DoGetBestSize() const
639 // these are the numbers from the HIG:
640 // we reduce them by the borders first
643 switch ( m_windowVariant
)
645 case wxWINDOW_VARIANT_NORMAL
:
649 case wxWINDOW_VARIANT_SMALL
:
653 case wxWINDOW_VARIANT_MINI
:
662 // as the above numbers have some free space around the text
663 // we get 5 lines like this anyway
664 if ( m_windowStyle
& wxTE_MULTILINE
)
667 if ( !HasFlag(wxNO_BORDER
) )
670 return wxSize(wText
, hText
);
673 // ----------------------------------------------------------------------------
675 // ----------------------------------------------------------------------------
677 void wxTextCtrl::Undo()
683 void wxTextCtrl::Redo()
689 bool wxTextCtrl::CanUndo() const
694 return GetPeer()->CanUndo() ;
697 bool wxTextCtrl::CanRedo() const
702 return GetPeer()->CanRedo() ;
705 void wxTextCtrl::MarkDirty()
710 void wxTextCtrl::DiscardEdits()
715 int wxTextCtrl::GetNumberOfLines() const
717 return GetPeer()->GetNumberOfLines() ;
720 long wxTextCtrl::XYToPosition(long x
, long y
) const
722 return GetPeer()->XYToPosition( x
, y
) ;
725 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
727 return GetPeer()->PositionToXY( pos
, x
, y
) ;
730 void wxTextCtrl::ShowPosition(long pos
)
732 return GetPeer()->ShowPosition(pos
) ;
735 int wxTextCtrl::GetLineLength(long lineNo
) const
737 return GetPeer()->GetLineLength(lineNo
) ;
740 wxString
wxTextCtrl::GetLineText(long lineNo
) const
742 return GetPeer()->GetLineText(lineNo
) ;
745 void wxTextCtrl::Command(wxCommandEvent
& event
)
747 SetValue(event
.GetString());
748 ProcessCommand(event
);
751 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
753 // By default, load the first file into the text window.
754 if (event
.GetNumberOfFiles() > 0)
755 LoadFile( event
.GetFiles()[0] );
758 void wxTextCtrl::OnEraseBackground(wxEraseEvent
& event
)
760 // all erasing should be done by the real mac control implementation
761 // while this is true for MLTE under classic, the HITextView is somehow
762 // transparent but background erase is not working correctly, so intercept
763 // things while we can...
767 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
769 int key
= event
.GetKeyCode() ;
770 bool eat_key
= false ;
772 if ( key
== 'a' && event
.MetaDown() )
779 if ( key
== 'c' && event
.MetaDown() )
787 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
788 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
789 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
796 // Check if we have reached the max # of chars (if it is set), but still
797 // allow navigation and deletion
798 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
799 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&&
800 key
!= WXK_BACK
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) )
803 // eat it, we don't want to add more than allowed # of characters
805 // TODO: generate EVT_TEXT_MAXLEN()
809 // assume that any key not processed yet is going to modify the control
812 if ( key
== 'v' && event
.MetaDown() )
820 if ( key
== 'x' && event
.MetaDown() )
831 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
833 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
834 event
.SetEventObject( this );
835 event
.SetString( GetValue() );
836 if ( GetEventHandler()->ProcessEvent(event
) )
840 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
842 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
843 if ( tlw
&& tlw
->GetDefaultItem() )
845 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
846 if ( def
&& def
->IsEnabled() )
848 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
849 event
.SetEventObject(def
);
856 // this will make wxWidgets eat the ENTER key so that
857 // we actually prevent line wrapping in a single line text control
863 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
866 if (!event
.ShiftDown())
867 flags
|= wxNavigationKeyEvent::IsForward
;
868 if (event
.ControlDown())
869 flags
|= wxNavigationKeyEvent::WinChange
;
876 // This is necessary (don't know why);
877 // otherwise the tab will not be inserted.
878 WriteText(wxT("\t"));
889 // perform keystroke handling
893 if ( ( key
>= 0x20 && key
< WXK_START
) ||
894 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
899 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
900 event1
.SetEventObject( this );
901 wxPostEvent( GetEventHandler(), event1
);
905 // ----------------------------------------------------------------------------
906 // standard handlers for standard edit menu events
907 // ----------------------------------------------------------------------------
909 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
914 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
919 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
924 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
929 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
934 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
938 GetSelection( &from
, &to
);
939 if (from
!= -1 && to
!= -1)
943 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
945 SetSelection(-1, -1);
948 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
950 event
.Enable( CanCut() );
953 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
955 event
.Enable( CanCopy() );
958 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
960 event
.Enable( CanPaste() );
963 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
965 event
.Enable( CanUndo() );
968 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
970 event
.Enable( CanRedo() );
973 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
977 GetSelection( &from
, &to
);
978 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
981 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
983 event
.Enable(GetLastPosition() > 0);
986 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
988 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
990 if ( GetPeer()->HasOwnContextMenu() )
996 if (m_privateContextMenu
== NULL
)
998 m_privateContextMenu
= new wxMenu
;
999 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1000 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1001 m_privateContextMenu
->AppendSeparator();
1002 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1003 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1004 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1005 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1006 m_privateContextMenu
->AppendSeparator();
1007 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1010 if (m_privateContextMenu
!= NULL
)
1011 PopupMenu(m_privateContextMenu
);
1014 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
1016 if ( !GetPeer()->SetupCursor( pt
) )
1017 return wxWindow::MacSetupCursor( pt
) ;
1022 // ----------------------------------------------------------------------------
1023 // implementation base class
1024 // ----------------------------------------------------------------------------
1026 wxMacTextControl::wxMacTextControl(wxTextCtrl
* peer
) :
1027 wxMacControl( peer
)
1031 wxMacTextControl::~wxMacTextControl()
1035 void wxMacTextControl::SetStyle(long WXUNUSED(start
),
1037 const wxTextAttr
& WXUNUSED(style
))
1041 void wxMacTextControl::Copy()
1045 void wxMacTextControl::Cut()
1049 void wxMacTextControl::Paste()
1053 bool wxMacTextControl::CanPaste() const
1058 void wxMacTextControl::SetEditable(bool WXUNUSED(editable
))
1062 wxTextPos
wxMacTextControl::GetLastPosition() const
1064 return GetStringValue().length() ;
1067 void wxMacTextControl::Replace( long from
, long to
, const wxString
&val
)
1069 SetSelection( from
, to
) ;
1073 void wxMacTextControl::Remove( long from
, long to
)
1075 SetSelection( from
, to
) ;
1076 WriteText( wxEmptyString
) ;
1079 void wxMacTextControl::Clear()
1081 SetStringValue( wxEmptyString
) ;
1084 bool wxMacTextControl::CanUndo() const
1089 void wxMacTextControl::Undo()
1093 bool wxMacTextControl::CanRedo() const
1098 void wxMacTextControl::Redo()
1102 long wxMacTextControl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
1107 bool wxMacTextControl::PositionToXY(long WXUNUSED(pos
),
1109 long *WXUNUSED(y
)) const
1114 void wxMacTextControl::ShowPosition( long WXUNUSED(pos
) )
1118 int wxMacTextControl::GetNumberOfLines() const
1120 ItemCount lines
= 0 ;
1121 wxString content
= GetStringValue() ;
1124 for (size_t i
= 0; i
< content
.length() ; i
++)
1126 if (content
[i
] == '\r')
1133 wxString
wxMacTextControl::GetLineText(long lineNo
) const
1135 // TODO: change this if possible to reflect real lines
1136 wxString content
= GetStringValue() ;
1140 for (size_t i
= 0; i
< content
.length() ; i
++)
1142 if (count
== lineNo
)
1144 // Add chars in line then
1147 for (size_t j
= i
; j
< content
.length(); j
++)
1149 if (content
[j
] == '\n')
1158 if (content
[i
] == '\n')
1162 return wxEmptyString
;
1165 int wxMacTextControl::GetLineLength(long lineNo
) const
1167 // TODO: change this if possible to reflect real lines
1168 wxString content
= GetStringValue() ;
1172 for (size_t i
= 0; i
< content
.length() ; i
++)
1174 if (count
== lineNo
)
1176 // Count chars in line then
1178 for (size_t j
= i
; j
< content
.length(); j
++)
1181 if (content
[j
] == '\n')
1188 if (content
[i
] == '\n')
1195 // ----------------------------------------------------------------------------
1196 // standard unicode control implementation
1197 // ----------------------------------------------------------------------------
1199 // the current unicode textcontrol implementation has a bug : only if the control
1200 // is currently having the focus, the selection can be retrieved by the corresponding
1201 // data tag. So we have a mirroring using a member variable
1202 // TODO : build event table using virtual member functions for wxMacControl
1204 static const EventTypeSpec unicodeTextControlEventList
[] =
1206 { kEventClassControl
, kEventControlSetFocusPart
} ,
1209 static pascal OSStatus
wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1211 OSStatus result
= eventNotHandledErr
;
1212 wxMacUnicodeTextControl
* focus
= (wxMacUnicodeTextControl
*) data
;
1213 wxMacCarbonEvent
cEvent( event
) ;
1215 switch ( GetEventKind( event
) )
1217 case kEventControlSetFocusPart
:
1219 ControlPartCode controlPart
= cEvent
.GetParameter
<ControlPartCode
>(kEventParamControlPart
, typeControlPartCode
);
1220 if ( controlPart
== kControlFocusNoPart
)
1222 // about to loose focus -> store selection to field
1223 focus
->GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1225 result
= CallNextEventHandler(handler
,event
) ;
1226 if ( controlPart
!= kControlFocusNoPart
)
1228 // about to gain focus -> set selection from field
1229 focus
->SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1240 static pascal OSStatus
wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1242 OSStatus result
= eventNotHandledErr
;
1244 switch ( GetEventClass( event
) )
1246 case kEventClassControl
:
1247 result
= wxMacUnicodeTextControlControlEventHandler( handler
, event
, data
) ;
1256 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler
)
1258 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
) : wxMacTextControl( wxPeer
)
1262 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
,
1263 const wxString
& str
,
1265 const wxSize
& size
, long style
)
1266 : wxMacTextControl( wxPeer
)
1268 Create( wxPeer
, str
, pos
, size
, style
);
1271 bool wxMacUnicodeTextControl::Create( wxTextCtrl
*wxPeer
,
1272 const wxString
& str
,
1274 const wxSize
& size
, long style
)
1276 m_font
= wxPeer
->GetFont() ;
1277 m_windowStyle
= style
;
1278 m_selection
.selStart
= m_selection
.selEnd
= 0;
1279 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
1281 wxMacConvertNewlines10To13( &st
) ;
1282 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding()) ;
1283 CFStringRef cfr
= cf
;
1285 m_valueTag
= kControlEditTextCFStringTag
;
1286 CreateControl( wxPeer
, &bounds
, cfr
);
1288 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1289 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextSingleLineTag
, true ) ;
1291 InstallControlEventHandler( m_controlRef
, GetwxMacUnicodeTextControlEventHandlerUPP(),
1292 GetEventTypeCount(unicodeTextControlEventList
), unicodeTextControlEventList
, this,
1298 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1302 void wxMacUnicodeTextControl::VisibilityChanged(bool shown
)
1304 if ( !(m_windowStyle
& wxTE_MULTILINE
) && shown
)
1306 // work around a refresh issue insofar as not always the entire content is shown,
1307 // even if this would be possible
1308 ControlEditTextSelectionRec sel
;
1309 CFStringRef value
= NULL
;
1311 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1312 verify_noerr( GetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1313 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1314 verify_noerr( SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1316 CFRelease( value
) ;
1320 wxString
wxMacUnicodeTextControl::GetStringValue() const
1323 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1326 wxMacCFStringHolder
cf(value
) ;
1327 result
= cf
.AsString() ;
1331 wxMacConvertNewlines13To10( &result
) ;
1333 wxMacConvertNewlines10To13( &result
) ;
1339 void wxMacUnicodeTextControl::SetStringValue( const wxString
&str
)
1342 wxMacConvertNewlines10To13( &st
) ;
1343 wxMacCFStringHolder
cf( st
, m_font
.GetEncoding() ) ;
1344 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, cf
) ) ;
1347 void wxMacUnicodeTextControl::CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef cfr
)
1349 Boolean isPassword
= ( m_windowStyle
& wxTE_PASSWORD
) != 0 ;
1352 m_valueTag
= kControlEditTextPasswordCFStringTag
;
1354 OSStatus err
= CreateEditUnicodeTextControl(
1355 MAC_WXHWND(peer
->MacGetTopLevelWindowRef()), bounds
, cfr
,
1356 isPassword
, NULL
, &m_controlRef
) ;
1357 verify_noerr( err
);
1360 void wxMacUnicodeTextControl::Copy()
1362 SendHICommand( kHICommandCopy
) ;
1365 void wxMacUnicodeTextControl::Cut()
1367 SendHICommand( kHICommandCut
) ;
1370 void wxMacUnicodeTextControl::Paste()
1372 SendHICommand( kHICommandPaste
) ;
1375 bool wxMacUnicodeTextControl::CanPaste() const
1380 void wxMacUnicodeTextControl::SetEditable(bool WXUNUSED(editable
))
1382 #if 0 // leads to problem because text cannot be selected anymore
1383 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextLockedTag
, (Boolean
) !editable
) ;
1387 void wxMacUnicodeTextControl::GetSelection( long* from
, long* to
) const
1389 ControlEditTextSelectionRec sel
;
1391 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ) ;
1396 *from
= sel
.selStart
;
1401 void wxMacUnicodeTextControl::SetSelection( long from
, long to
)
1403 ControlEditTextSelectionRec sel
;
1405 int textLength
= 0 ;
1406 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1409 wxMacCFStringHolder
cf(value
) ;
1410 textLength
= cf
.AsString().length() ;
1413 if ((from
== -1) && (to
== -1))
1420 from
= wxMin(textLength
,wxMax(from
,0)) ;
1424 to
= wxMax(0,wxMin(textLength
,to
)) ;
1427 sel
.selStart
= from
;
1430 SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ;
1435 void wxMacUnicodeTextControl::WriteText( const wxString
& str
)
1438 wxMacConvertNewlines10To13( &st
) ;
1442 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding() ) ;
1443 CFStringRef value
= cf
;
1444 SetData
<CFStringRef
>( 0, kControlEditTextInsertCFStringRefTag
, &value
);
1448 wxString val
= GetStringValue() ;
1450 GetSelection( &start
, &end
) ;
1451 val
.Remove( start
, end
- start
) ;
1452 val
.insert( start
, str
) ;
1453 SetStringValue( val
) ;
1454 SetSelection( start
+ str
.length() , start
+ str
.length() ) ;
1458 // ----------------------------------------------------------------------------
1459 // MLTE control implementation (common part)
1460 // ----------------------------------------------------------------------------
1462 // if MTLE is read only, no changes at all are allowed, not even from
1463 // procedural API, in order to allow changes via API all the same we must undo
1464 // the readonly status while we are executing, this class helps to do so
1466 class wxMacEditHelper
1469 wxMacEditHelper( TXNObject txn
)
1471 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1473 TXNGetTXNObjectControls( m_txn
, 1 , tag
, m_data
) ;
1474 if ( m_data
[0].uValue
== kTXNReadOnly
)
1476 TXNControlData data
[] = { { kTXNReadWrite
} } ;
1477 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, data
) ;
1483 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1484 if ( m_data
[0].uValue
== kTXNReadOnly
)
1485 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, m_data
) ;
1490 TXNControlData m_data
[1] ;
1493 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl
*peer
)
1494 : wxMacTextControl( peer
)
1496 SetNeedsFocusRect( true ) ;
1499 wxString
wxMacMLTEControl::GetStringValue() const
1503 Size actualSize
= 0;
1508 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNUnicodeTextData
);
1517 actualSize
= GetHandleSize( theText
) / sizeof(UniChar
) ;
1518 if ( actualSize
> 0 )
1520 wxChar
*ptr
= NULL
;
1522 #if SIZEOF_WCHAR_T == 2
1523 ptr
= new wxChar
[actualSize
+ 1] ;
1524 wxStrncpy( ptr
, (wxChar
*)(*theText
) , actualSize
) ;
1526 SetHandleSize( theText
, (actualSize
+ 1) * sizeof(UniChar
) ) ;
1528 (((UniChar
*)*theText
)[actualSize
]) = 0 ;
1529 wxMBConvUTF16 converter
;
1530 size_t noChars
= converter
.MB2WC( NULL
, (const char*)*theText
, 0 ) ;
1531 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Unable to count the number of characters in this string!") );
1532 ptr
= new wxChar
[noChars
+ 1] ;
1534 noChars
= converter
.MB2WC( ptr
, (const char*)*theText
, noChars
+ 1 ) ;
1535 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Conversion of string failed!") );
1537 HUnlock( theText
) ;
1540 ptr
[actualSize
] = 0 ;
1541 result
= wxString( ptr
) ;
1545 DisposeHandle( theText
) ;
1549 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1558 actualSize
= GetHandleSize( theText
) ;
1559 if ( actualSize
> 0 )
1562 result
= wxString( *theText
, wxConvLocal
, actualSize
) ;
1563 HUnlock( theText
) ;
1566 DisposeHandle( theText
) ;
1572 wxMacConvertNewlines13To10( &result
) ;
1574 wxMacConvertNewlines10To13( &result
) ;
1580 void wxMacMLTEControl::SetStringValue( const wxString
&str
)
1583 wxMacConvertNewlines10To13( &st
);
1587 wxMacWindowClipper
c( m_peer
) ;
1591 wxMacEditHelper
help( m_txn
);
1592 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
);
1595 TXNSetSelection( m_txn
, 0, 0 );
1596 TXNShowSelection( m_txn
, kTXNShowStart
);
1600 TXNFrameOptions
wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle
)
1602 TXNFrameOptions frameOptions
= kTXNDontDrawCaretWhenInactiveMask
;
1604 frameOptions
|= kTXNDoFontSubstitutionMask
;
1606 if ( ! (wxStyle
& wxTE_NOHIDESEL
) )
1607 frameOptions
|= kTXNDontDrawSelectionWhenInactiveMask
;
1609 if ( wxStyle
& (wxHSCROLL
| wxTE_DONTWRAP
) )
1610 frameOptions
|= kTXNWantHScrollBarMask
;
1612 if ( wxStyle
& wxTE_MULTILINE
)
1614 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
1616 if ( !(wxStyle
& wxTE_NO_VSCROLL
) )
1618 frameOptions
|= kTXNWantVScrollBarMask
;
1620 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1621 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1622 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1623 // if ( frameOptions & kTXNWantHScrollBarMask )
1624 // frameOptions |= kTXNDrawGrowIconMask ;
1629 frameOptions
|= kTXNSingleLineOnlyMask
;
1632 return frameOptions
;
1635 void wxMacMLTEControl::AdjustCreationAttributes(const wxColour
&background
,
1636 bool WXUNUSED(visible
))
1638 TXNControlTag iControlTags
[] =
1640 kTXNDoFontSubstitution
,
1641 kTXNWordWrapStateTag
,
1643 TXNControlData iControlData
[] =
1649 int toptag
= WXSIZEOF( iControlTags
) ;
1651 if ( m_windowStyle
& wxTE_MULTILINE
)
1653 iControlData
[1].uValue
=
1654 (m_windowStyle
& wxTE_DONTWRAP
)
1659 OSStatus err
= TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) ;
1660 verify_noerr( err
);
1662 // setting the default font:
1663 // under 10.2 this causes a visible caret, therefore we avoid it
1669 GetThemeFont( kThemeSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
1671 TXNTypeAttributes typeAttr
[] =
1673 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
1674 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
1675 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
1678 err
= TXNSetTypeAttributes(
1679 m_txn
, sizeof(typeAttr
) / sizeof(TXNTypeAttributes
),
1680 typeAttr
, kTXNStartOffset
, kTXNEndOffset
);
1681 verify_noerr( err
);
1683 if ( m_windowStyle
& wxTE_PASSWORD
)
1685 UniChar c
= 0x00A5 ;
1686 err
= TXNEchoMode( m_txn
, c
, 0 , true );
1687 verify_noerr( err
);
1690 TXNBackground tback
;
1691 tback
.bgType
= kTXNBackgroundTypeRGB
;
1692 tback
.bg
.color
= MAC_WXCOLORREF( background
.GetPixel() );
1693 TXNSetBackground( m_txn
, &tback
);
1696 TXNCommandEventSupportOptions options
;
1697 if ( TXNGetCommandEventSupport( m_txn
, &options
) == noErr
)
1700 kTXNSupportEditCommandProcessing
1701 | kTXNSupportEditCommandUpdating
1702 | kTXNSupportFontCommandProcessing
1703 | kTXNSupportFontCommandUpdating
;
1705 // only spell check when not read-only
1706 // use system options for the default
1707 bool checkSpelling
= false ;
1708 if ( !(m_windowStyle
& wxTE_READONLY
) )
1710 #if wxUSE_SYSTEM_OPTIONS
1711 if ( wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) == 1) )
1713 checkSpelling
= true ;
1718 if ( checkSpelling
)
1720 kTXNSupportSpellCheckCommandProcessing
1721 | kTXNSupportSpellCheckCommandUpdating
;
1723 TXNSetCommandEventSupport( m_txn
, options
) ;
1727 void wxMacMLTEControl::SetBackground( const wxBrush
&brush
)
1729 // currently only solid background are supported
1730 TXNBackground tback
;
1732 tback
.bgType
= kTXNBackgroundTypeRGB
;
1733 tback
.bg
.color
= MAC_WXCOLORREF( brush
.GetColour().GetPixel() );
1734 TXNSetBackground( m_txn
, &tback
);
1737 static inline int wxConvertToTXN(int x
)
1739 return wx_static_cast(int, x
/ 254.0 * 72 + 0.5);
1742 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
)
1744 TXNTypeAttributes typeAttr
[4] ;
1746 size_t typeAttrCount
= 0 ;
1749 TXNControlTag controlTags
[4];
1750 TXNControlData controlData
[4];
1751 size_t controlAttrCount
= 0;
1753 TXNTab
* tabs
= NULL
;
1755 bool relayout
= false;
1757 if ( style
.HasFont() )
1759 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1760 const wxFont
&font
= style
.GetFont() ;
1761 typeAttr
[typeAttrCount
].tag
= kTXNATSUIStyle
;
1762 typeAttr
[typeAttrCount
].size
= kTXNATSUIStyleSize
;
1763 typeAttr
[typeAttrCount
].data
.dataPtr
= font
.MacGetATSUStyle() ;
1767 if ( style
.HasTextColour() )
1769 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1770 color
= MAC_WXCOLORREF(style
.GetTextColour().GetPixel()) ;
1772 typeAttr
[typeAttrCount
].tag
= kTXNQDFontColorAttribute
;
1773 typeAttr
[typeAttrCount
].size
= kTXNQDFontColorAttributeSize
;
1774 typeAttr
[typeAttrCount
].data
.dataPtr
= (void*) &color
;
1778 if ( style
.HasAlignment() )
1780 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1783 switch ( style
.GetAlignment() )
1785 case wxTEXT_ALIGNMENT_LEFT
:
1786 align
= kTXNFlushLeft
;
1788 case wxTEXT_ALIGNMENT_CENTRE
:
1791 case wxTEXT_ALIGNMENT_RIGHT
:
1792 align
= kTXNFlushRight
;
1794 case wxTEXT_ALIGNMENT_JUSTIFIED
:
1795 align
= kTXNFullJust
;
1798 case wxTEXT_ALIGNMENT_DEFAULT
:
1799 align
= kTXNFlushDefault
;
1803 controlTags
[controlAttrCount
] = kTXNJustificationTag
;
1804 controlData
[controlAttrCount
].sValue
= align
;
1805 controlAttrCount
++ ;
1808 if ( style
.HasLeftIndent() || style
.HasRightIndent() )
1810 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1811 controlTags
[controlAttrCount
] = kTXNMarginsTag
;
1812 controlData
[controlAttrCount
].marginsPtr
= &margins
;
1813 verify_noerr( TXNGetTXNObjectControls (m_txn
, 1 ,
1814 &controlTags
[controlAttrCount
], &controlData
[controlAttrCount
]) );
1815 if ( style
.HasLeftIndent() )
1817 margins
.leftMargin
= wxConvertToTXN(style
.GetLeftIndent());
1819 if ( style
.HasRightIndent() )
1821 margins
.rightMargin
= wxConvertToTXN(style
.GetRightIndent());
1823 controlAttrCount
++ ;
1826 if ( style
.HasTabs() )
1828 const wxArrayInt
& tabarray
= style
.GetTabs();
1829 // unfortunately Mac only applies a tab distance, not individually different tabs
1830 controlTags
[controlAttrCount
] = kTXNTabSettingsTag
;
1831 if ( tabarray
.size() > 0 )
1832 controlData
[controlAttrCount
].tabValue
.value
= wxConvertToTXN(tabarray
[0]);
1834 controlData
[controlAttrCount
].tabValue
.value
= 72 ;
1836 controlData
[controlAttrCount
].tabValue
.tabType
= kTXNLeftTab
;
1837 controlAttrCount
++ ;
1840 // unfortunately the relayout is not automatic
1841 if ( controlAttrCount
> 0 )
1843 verify_noerr( TXNSetTXNObjectControls (m_txn
, false /* don't clear all */, controlAttrCount
,
1844 controlTags
, controlData
) );
1848 if ( typeAttrCount
> 0 )
1850 verify_noerr( TXNSetTypeAttributes( m_txn
, typeAttrCount
, typeAttr
, from
, to
) );
1861 TXNRecalcTextLayout( m_txn
);
1865 void wxMacMLTEControl::SetFont(const wxFont
& font
,
1866 const wxColour
& foreground
,
1867 long WXUNUSED(windowStyle
))
1869 wxMacEditHelper
help( m_txn
) ;
1870 TXNSetAttribute( wxTextAttr( foreground
, wxNullColour
, font
), kTXNStartOffset
, kTXNEndOffset
) ;
1873 void wxMacMLTEControl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1875 wxMacEditHelper
help( m_txn
) ;
1876 TXNSetAttribute( style
, start
, end
) ;
1879 void wxMacMLTEControl::Copy()
1884 void wxMacMLTEControl::Cut()
1889 void wxMacMLTEControl::Paste()
1894 bool wxMacMLTEControl::CanPaste() const
1896 return TXNIsScrapPastable() ;
1899 void wxMacMLTEControl::SetEditable(bool editable
)
1901 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1902 TXNControlData data
[] = { { editable
? kTXNReadWrite
: kTXNReadOnly
} } ;
1903 TXNSetTXNObjectControls( m_txn
, false, WXSIZEOF(tag
), tag
, data
) ;
1906 wxTextPos
wxMacMLTEControl::GetLastPosition() const
1908 wxTextPos actualsize
= 0 ;
1911 OSErr err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1916 actualsize
= GetHandleSize( theText
) ;
1917 DisposeHandle( theText
) ;
1927 void wxMacMLTEControl::Replace( long from
, long to
, const wxString
&str
)
1929 wxString value
= str
;
1930 wxMacConvertNewlines10To13( &value
) ;
1932 wxMacEditHelper
help( m_txn
) ;
1934 wxMacWindowClipper
c( m_peer
) ;
1937 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
) ;
1939 SetTXNData( value
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1942 void wxMacMLTEControl::Remove( long from
, long to
)
1945 wxMacWindowClipper
c( m_peer
) ;
1947 wxMacEditHelper
help( m_txn
) ;
1948 TXNSetSelection( m_txn
, from
, to
) ;
1952 void wxMacMLTEControl::GetSelection( long* from
, long* to
) const
1955 TXNGetSelection( m_txn
, &f
, &t
) ;
1960 void wxMacMLTEControl::SetSelection( long from
, long to
)
1963 wxMacWindowClipper
c( m_peer
) ;
1966 // change the selection
1967 if ((from
== -1) && (to
== -1))
1968 TXNSelectAll( m_txn
);
1970 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
);
1972 TXNShowSelection( m_txn
, kTXNShowStart
);
1975 void wxMacMLTEControl::WriteText( const wxString
& str
)
1978 wxMacConvertNewlines10To13( &st
) ;
1980 long start
, end
, dummy
;
1982 GetSelection( &start
, &dummy
) ;
1984 wxMacWindowClipper
c( m_peer
) ;
1988 wxMacEditHelper
helper( m_txn
) ;
1989 SetTXNData( st
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1992 GetSelection( &dummy
, &end
) ;
1994 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
1997 void wxMacMLTEControl::Clear()
2000 wxMacWindowClipper
c( m_peer
) ;
2002 wxMacEditHelper
st( m_txn
) ;
2003 TXNSetSelection( m_txn
, kTXNStartOffset
, kTXNEndOffset
) ;
2007 bool wxMacMLTEControl::CanUndo() const
2009 return TXNCanUndo( m_txn
, NULL
) ;
2012 void wxMacMLTEControl::Undo()
2017 bool wxMacMLTEControl::CanRedo() const
2019 return TXNCanRedo( m_txn
, NULL
) ;
2022 void wxMacMLTEControl::Redo()
2027 int wxMacMLTEControl::GetNumberOfLines() const
2029 ItemCount lines
= 0 ;
2030 TXNGetLineCount( m_txn
, &lines
) ;
2035 long wxMacMLTEControl::XYToPosition(long x
, long y
) const
2040 // TODO: find a better implementation : while we can get the
2041 // line metrics of a certain line, we don't get its starting
2042 // position, so it would probably be rather a binary search
2043 // for the start position
2044 long xpos
= 0, ypos
= 0 ;
2045 int lastHeight
= 0 ;
2048 lastpos
= GetLastPosition() ;
2049 for ( n
= 0 ; n
<= (ItemCount
) lastpos
; ++n
)
2051 if ( y
== ypos
&& x
== xpos
)
2054 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2056 if ( curpt
.v
> lastHeight
)
2062 lastHeight
= curpt
.v
;
2071 bool wxMacMLTEControl::PositionToXY( long pos
, long *x
, long *y
) const
2081 lastpos
= GetLastPosition() ;
2082 if ( pos
<= lastpos
)
2084 // TODO: find a better implementation - while we can get the
2085 // line metrics of a certain line, we don't get its starting
2086 // position, so it would probably be rather a binary search
2087 // for the start position
2088 long xpos
= 0, ypos
= 0 ;
2089 int lastHeight
= 0 ;
2092 for ( n
= 0 ; n
<= (ItemCount
) pos
; ++n
)
2094 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2096 if ( curpt
.v
> lastHeight
)
2102 lastHeight
= curpt
.v
;
2117 void wxMacMLTEControl::ShowPosition( long pos
)
2119 Point current
, desired
;
2120 TXNOffset selstart
, selend
;
2122 TXNGetSelection( m_txn
, &selstart
, &selend
);
2123 TXNOffsetToPoint( m_txn
, selstart
, ¤t
);
2124 TXNOffsetToPoint( m_txn
, pos
, &desired
);
2126 // TODO: use HIPoints for 10.3 and above
2128 OSErr theErr
= noErr
;
2129 long dv
= desired
.v
- current
.v
;
2130 long dh
= desired
.h
- current
.h
;
2131 TXNShowSelection( m_txn
, kTXNShowStart
) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2132 theErr
= TXNScroll( m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
, &dv
, &dh
);
2134 // there will be an error returned for classic MLTE implementation when the control is
2135 // invisible, but HITextView works correctly, so we don't assert that one
2136 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2139 void wxMacMLTEControl::SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
)
2142 #if SIZEOF_WCHAR_T == 2
2143 size_t len
= st
.length() ;
2144 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)st
.wc_str(), len
* 2, start
, end
);
2146 wxMBConvUTF16 converter
;
2147 ByteCount byteBufferLen
= converter
.WC2MB( NULL
, st
.wc_str(), 0 ) ;
2148 UniChar
*unibuf
= (UniChar
*)malloc( byteBufferLen
) ;
2149 converter
.WC2MB( (char*)unibuf
, st
.wc_str(), byteBufferLen
) ;
2150 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)unibuf
, byteBufferLen
, start
, end
) ;
2154 wxCharBuffer text
= st
.mb_str( wxConvLocal
) ;
2155 TXNSetData( m_txn
, kTXNTextData
, (void*)text
.data(), strlen( text
), start
, end
) ;
2159 wxString
wxMacMLTEControl::GetLineText(long lineNo
) const
2163 if ( lineNo
< GetNumberOfLines() )
2166 Fixed lineWidth
, lineHeight
, currentHeight
;
2169 // get the first possible position in the control
2170 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2172 // Iterate through the lines until we reach the one we want,
2173 // adding to our current y pixel point position
2176 while (ypos
< lineNo
)
2178 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2179 currentHeight
+= lineHeight
;
2182 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2183 TXNOffset theOffset
;
2184 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2186 wxString content
= GetStringValue() ;
2187 Point currentPoint
= thePoint
;
2188 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2190 line
+= content
[theOffset
];
2191 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2198 int wxMacMLTEControl::GetLineLength(long lineNo
) const
2202 if ( lineNo
< GetNumberOfLines() )
2205 Fixed lineWidth
, lineHeight
, currentHeight
;
2208 // get the first possible position in the control
2209 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2211 // Iterate through the lines until we reach the one we want,
2212 // adding to our current y pixel point position
2215 while (ypos
< lineNo
)
2217 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2218 currentHeight
+= lineHeight
;
2221 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2222 TXNOffset theOffset
;
2223 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2225 wxString content
= GetStringValue() ;
2226 Point currentPoint
= thePoint
;
2227 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2230 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2237 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
2239 // ----------------------------------------------------------------------------
2240 // MLTE control implementation (classic part)
2241 // ----------------------------------------------------------------------------
2243 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2244 // has to live on. We have different problems coming from outdated implementations on the
2245 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2246 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2247 // no way out, therefore we are using our own implementation and our own scrollbars ....
2249 TXNScrollInfoUPP gTXNScrollInfoProc
= NULL
;
2250 ControlActionUPP gTXNScrollActionProc
= NULL
;
2252 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2253 SInt32 iValue
, SInt32 iMaximumValue
,
2254 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
)
2256 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) iRefCon
;
2257 SInt32 value
= wxMax( iValue
, 0 ) ;
2258 SInt32 maximum
= wxMax( iMaximumValue
, 0 ) ;
2260 if ( iScrollBarOrientation
== kTXNHorizontal
)
2262 if ( mlte
->m_sbHorizontal
)
2264 SetControl32BitValue( mlte
->m_sbHorizontal
, value
) ;
2265 SetControl32BitMaximum( mlte
->m_sbHorizontal
, maximum
) ;
2266 mlte
->m_lastHorizontalValue
= value
;
2269 else if ( iScrollBarOrientation
== kTXNVertical
)
2271 if ( mlte
->m_sbVertical
)
2273 SetControl32BitValue( mlte
->m_sbVertical
, value
) ;
2274 SetControl32BitMaximum( mlte
->m_sbVertical
, maximum
) ;
2275 mlte
->m_lastVerticalValue
= value
;
2280 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
)
2282 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) GetControlReference( controlRef
) ;
2286 if ( controlRef
!= mlte
->m_sbVertical
&& controlRef
!= mlte
->m_sbHorizontal
)
2290 bool isHorizontal
= ( controlRef
== mlte
->m_sbHorizontal
) ;
2292 SInt32 minimum
= 0 ;
2293 SInt32 maximum
= GetControl32BitMaximum( controlRef
) ;
2294 SInt32 value
= GetControl32BitValue( controlRef
) ;
2299 case kControlDownButtonPart
:
2303 case kControlUpButtonPart
:
2307 case kControlPageDownPart
:
2308 delta
= GetControlViewSize( controlRef
) ;
2311 case kControlPageUpPart
:
2312 delta
= -GetControlViewSize( controlRef
) ;
2315 case kControlIndicatorPart
:
2316 delta
= value
- (isHorizontal
? mlte
->m_lastHorizontalValue
: mlte
->m_lastVerticalValue
) ;
2325 SInt32 newValue
= value
;
2327 if ( partCode
!= kControlIndicatorPart
)
2329 if ( value
+ delta
< minimum
)
2330 delta
= minimum
- value
;
2331 if ( value
+ delta
> maximum
)
2332 delta
= maximum
- value
;
2334 SetControl32BitValue( controlRef
, value
+ delta
) ;
2335 newValue
= value
+ delta
;
2338 SInt32 verticalDelta
= isHorizontal
? 0 : delta
;
2339 SInt32 horizontalDelta
= isHorizontal
? delta
: 0 ;
2342 mlte
->m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
,
2343 &verticalDelta
, &horizontalDelta
);
2344 verify_noerr( err
);
2347 mlte
->m_lastHorizontalValue
= newValue
;
2349 mlte
->m_lastVerticalValue
= newValue
;
2353 // make correct activations
2354 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive
)
2356 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference(m_controlRef
);
2358 wxMacWindowClipper
clipper( textctrl
) ;
2359 TXNActivate( m_txn
, m_txnFrameID
, setActive
);
2361 ControlRef controlFocus
= 0 ;
2362 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2363 if ( controlFocus
== m_controlRef
)
2364 TXNFocus( m_txn
, setActive
);
2367 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus
)
2369 TXNFocus( m_txn
, setFocus
);
2372 // guards against inappropriate redraw (hidden objects drawing onto window)
2374 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis
)
2376 ControlRef controlFocus
= 0 ;
2377 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2379 if ( !vis
&& (controlFocus
== m_controlRef
) )
2380 SetKeyboardFocus( m_txnWindow
, m_controlRef
, kControlFocusNoPart
) ;
2382 TXNControlTag iControlTags
[1] = { kTXNVisibilityTag
};
2383 TXNControlData iControlData
[1] = { { (UInt32
)false } };
2385 verify_noerr( TXNGetTXNObjectControls( m_txn
, 1, iControlTags
, iControlData
) ) ;
2387 if ( iControlData
[0].uValue
!= vis
)
2389 iControlData
[0].uValue
= vis
;
2390 verify_noerr( TXNSetTXNObjectControls( m_txn
, false , 1, iControlTags
, iControlData
) ) ;
2393 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2394 // if we run into further problems we might set the FrameBounds to an empty rect here
2397 // make sure that the TXNObject is at the right position
2399 void wxMacMLTEClassicControl::MacUpdatePosition()
2401 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2402 if ( textctrl
== NULL
)
2406 UMAGetControlBoundsInWindowCoords( m_controlRef
, &bounds
);
2408 wxRect visRect
= textctrl
->MacGetClippedClientRect() ;
2409 Rect visBounds
= { visRect
.y
, visRect
.x
, visRect
.y
+ visRect
.height
, visRect
.x
+ visRect
.width
} ;
2412 textctrl
->MacWindowToRootWindow( &x
, &y
) ;
2413 OffsetRect( &visBounds
, x
, y
) ;
2415 if ( !EqualRect( &bounds
, &m_txnControlBounds
) || !EqualRect( &visBounds
, &m_txnVisBounds
) )
2417 m_txnControlBounds
= bounds
;
2418 m_txnVisBounds
= visBounds
;
2419 wxMacWindowClipper
cl( textctrl
) ;
2421 if ( m_sbHorizontal
|| m_sbVertical
)
2423 int w
= bounds
.right
- bounds
.left
;
2424 int h
= bounds
.bottom
- bounds
.top
;
2426 if ( m_sbHorizontal
)
2430 sbBounds
.left
= -1 ;
2431 sbBounds
.top
= h
- 14 ;
2432 sbBounds
.right
= w
+ 1 ;
2433 sbBounds
.bottom
= h
+ 1 ;
2435 SetControlBounds( m_sbHorizontal
, &sbBounds
) ;
2436 SetControlViewSize( m_sbHorizontal
, w
) ;
2443 sbBounds
.left
= w
- 14 ;
2445 sbBounds
.right
= w
+ 1 ;
2446 sbBounds
.bottom
= m_sbHorizontal
? h
- 14 : h
+ 1 ;
2448 SetControlBounds( m_sbVertical
, &sbBounds
) ;
2449 SetControlViewSize( m_sbVertical
, h
) ;
2454 TXNLongRect olddestRect
;
2455 TXNGetRectBounds( m_txn
, &oldviewRect
, &olddestRect
, NULL
) ;
2457 Rect viewRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2458 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2459 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2460 TXNLongRect destRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2461 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2462 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2464 if ( olddestRect
.right
>= 10000 )
2465 destRect
.right
= destRect
.left
+ 32000 ;
2467 if ( olddestRect
.bottom
>= 0x20000000 )
2468 destRect
.bottom
= destRect
.top
+ 0x40000000 ;
2470 SectRect( &viewRect
, &visBounds
, &viewRect
) ;
2471 TXNSetRectBounds( m_txn
, &viewRect
, &destRect
, true ) ;
2476 m_txnControlBounds
.top
,
2477 m_txnControlBounds
.left
,
2478 m_txnControlBounds
.bottom
- (m_sbHorizontal
? 14 : 0),
2479 m_txnControlBounds
.right
- (m_sbVertical
? 14 : 0),
2483 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2484 // movement, therefore we have to force it
2486 // this problem has been reported in OSX as well, so we use this here once again
2488 TXNLongRect textRect
;
2489 TXNGetRectBounds( m_txn
, NULL
, NULL
, &textRect
) ;
2490 if ( textRect
.left
< m_txnControlBounds
.left
)
2491 TXNShowSelection( m_txn
, kTXNShowStart
) ;
2495 void wxMacMLTEClassicControl::SetRect( Rect
*r
)
2497 wxMacControl::SetRect( r
) ;
2498 MacUpdatePosition() ;
2501 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16
WXUNUSED(thePart
))
2503 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2504 if ( textctrl
== NULL
)
2507 if ( textctrl
->MacIsReallyShown() )
2509 wxMacWindowClipper
clipper( textctrl
) ;
2510 TXNDraw( m_txn
, NULL
) ;
2514 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
2516 Point where
= { y
, x
} ;
2517 ControlPartCode result
= kControlNoPart
;
2519 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2520 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2522 if (PtInRect( where
, &m_txnControlBounds
))
2524 result
= kControlEditTextPart
;
2528 // sometimes we get the coords also in control local coordinates, therefore test again
2530 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2534 if (PtInRect( where
, &m_txnControlBounds
))
2535 result
= kControlEditTextPart
;
2542 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x
, wxInt16 y
, void* WXUNUSED(actionProc
) )
2544 ControlPartCode result
= kControlNoPart
;
2546 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2547 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2549 Point startPt
= { y
, x
} ;
2551 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2553 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2557 switch (MacControlUserPaneHitTestProc( startPt
.h
, startPt
.v
))
2559 case kControlEditTextPart
:
2561 wxMacWindowClipper
clipper( textctrl
) ;
2564 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
2565 TXNClick( m_txn
, &rec
);
2577 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2579 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2580 if ( textctrl
== NULL
)
2583 if (textctrl
->MacIsReallyShown())
2585 if (IsControlActive(m_controlRef
))
2589 wxMacWindowClipper
clipper( textctrl
) ;
2594 if (PtInRect(mousep
, &m_txnControlBounds
))
2596 RgnHandle theRgn
= NewRgn();
2597 RectRgn(theRgn
, &m_txnControlBounds
);
2598 TXNAdjustCursor(m_txn
, theRgn
);
2605 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
2607 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2608 if ( textctrl
== NULL
)
2609 return kControlNoPart
;
2611 wxMacWindowClipper
clipper( textctrl
) ;
2614 memset( &ev
, 0 , sizeof( ev
) ) ;
2616 ev
.modifiers
= modifiers
;
2617 ev
.message
= ((keyCode
<< 8) & keyCodeMask
) | (charCode
& charCodeMask
);
2618 TXNKeyDown( m_txn
, &ev
);
2620 return kControlEntireControl
;
2623 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating
)
2625 MacActivatePaneText( activating
);
2628 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action
)
2630 ControlPartCode focusResult
= kControlFocusNoPart
;
2632 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2633 if ( textctrl
== NULL
)
2636 wxMacWindowClipper
clipper( textctrl
) ;
2638 ControlRef controlFocus
= NULL
;
2639 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2640 bool wasFocused
= ( controlFocus
== m_controlRef
) ;
2644 case kControlFocusPrevPart
:
2645 case kControlFocusNextPart
:
2646 MacFocusPaneText( !wasFocused
);
2647 focusResult
= (!wasFocused
? (ControlPartCode
) kControlEditTextPart
: (ControlPartCode
) kControlFocusNoPart
);
2650 case kControlFocusNoPart
:
2652 MacFocusPaneText( false );
2653 focusResult
= kControlFocusNoPart
;
2660 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info
) )
2664 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
2665 const wxString
& str
,
2667 const wxSize
& size
, long style
)
2668 : wxMacMLTEControl( wxPeer
)
2670 m_font
= wxPeer
->GetFont() ;
2671 m_windowStyle
= style
;
2672 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
2675 kControlSupportsEmbedding
| kControlSupportsFocus
| kControlWantsIdle
2676 | kControlWantsActivate
| kControlHandlesTracking
2677 // | kControlHasSpecialBackground
2678 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
2680 OSStatus err
= ::CreateUserPaneControl(
2681 MAC_WXHWND(wxPeer
->GetParent()->MacGetTopLevelWindowRef()),
2682 &bounds
, featureSet
, &m_controlRef
);
2683 verify_noerr( err
);
2687 AdjustCreationAttributes( *wxWHITE
, true ) ;
2689 MacSetObjectVisibility( wxPeer
->MacIsReallyShown() ) ;
2693 wxMacConvertNewlines10To13( &st
) ;
2694 wxMacWindowClipper
clipper( m_peer
) ;
2695 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
2696 TXNSetSelection( m_txn
, 0, 0 ) ;
2700 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2702 TXNDeleteObject( m_txn
);
2706 void wxMacMLTEClassicControl::VisibilityChanged(bool shown
)
2708 MacSetObjectVisibility( shown
) ;
2709 wxMacControl::VisibilityChanged( shown
) ;
2712 void wxMacMLTEClassicControl::SuperChangedPosition()
2714 MacUpdatePosition() ;
2715 wxMacControl::SuperChangedPosition() ;
2718 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
2719 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
2720 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
2721 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
2722 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
2723 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
2724 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
2726 static pascal void wxMacControlUserPaneDrawProc(ControlRef control
, SInt16 part
)
2728 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2729 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2731 win
->MacControlUserPaneDrawProc( part
) ;
2734 static pascal ControlPartCode
wxMacControlUserPaneHitTestProc(ControlRef control
, Point where
)
2736 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2737 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2739 return win
->MacControlUserPaneHitTestProc( where
.h
, where
.v
) ;
2741 return kControlNoPart
;
2744 static pascal ControlPartCode
wxMacControlUserPaneTrackingProc(ControlRef control
, Point startPt
, ControlActionUPP actionProc
)
2746 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2747 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2749 return win
->MacControlUserPaneTrackingProc( startPt
.h
, startPt
.v
, (void*) actionProc
) ;
2751 return kControlNoPart
;
2754 static pascal void wxMacControlUserPaneIdleProc(ControlRef control
)
2756 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2757 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2759 win
->MacControlUserPaneIdleProc() ;
2762 static pascal ControlPartCode
wxMacControlUserPaneKeyDownProc(ControlRef control
, SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
)
2764 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2765 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2767 return win
->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
2769 return kControlNoPart
;
2772 static pascal void wxMacControlUserPaneActivateProc(ControlRef control
, Boolean activating
)
2774 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2775 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2777 win
->MacControlUserPaneActivateProc( activating
) ;
2780 static pascal ControlPartCode
wxMacControlUserPaneFocusProc(ControlRef control
, ControlFocusPart action
)
2782 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2783 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2785 return win
->MacControlUserPaneFocusProc( action
) ;
2787 return kControlNoPart
;
2791 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control
, ControlBackgroundPtr info
)
2793 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2794 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2796 win
->MacControlUserPaneBackgroundProc(info
) ;
2800 // TXNRegisterScrollInfoProc
2802 OSStatus
wxMacMLTEClassicControl::DoCreate()
2805 OSStatus err
= noErr
;
2807 // set up our globals
2808 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc
);
2809 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc
);
2810 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc
);
2811 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc
);
2812 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc
);
2813 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc
);
2814 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc
);
2816 if (gTXNScrollInfoProc
== NULL
) gTXNScrollInfoProc
= NewTXNScrollInfoUPP(TXNScrollInfoProc
) ;
2817 if (gTXNScrollActionProc
== NULL
) gTXNScrollActionProc
= NewControlActionUPP(TXNScrollActionProc
) ;
2819 // set the initial settings for our private data
2821 m_txnWindow
= GetControlOwner(m_controlRef
);
2822 m_txnPort
= (GrafPtr
) GetWindowPort(m_txnWindow
);
2824 // set up the user pane procedures
2825 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
2826 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
2827 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
2828 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
2829 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
2830 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
2831 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
2833 // calculate the rectangles used by the control
2834 UMAGetControlBoundsInWindowCoords( m_controlRef
, &bounds
);
2836 m_txnControlBounds
= bounds
;
2837 m_txnVisBounds
= bounds
;
2842 GetGWorld( &origPort
, &origDev
) ;
2843 SetPort( m_txnPort
);
2845 // create the new edit field
2846 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( m_windowStyle
);
2848 // the scrollbars are not correctly embedded but are inserted at the root:
2849 // this gives us problems as we have erratic redraws even over the structure area
2851 m_sbHorizontal
= 0 ;
2853 m_lastHorizontalValue
= 0 ;
2854 m_lastVerticalValue
= 0 ;
2856 Rect sb
= { 0 , 0 , 0 , 0 } ;
2857 if ( frameOptions
& kTXNWantVScrollBarMask
)
2859 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbVertical
);
2860 SetControlReference( m_sbVertical
, (SInt32
)this );
2861 SetControlAction( m_sbVertical
, gTXNScrollActionProc
);
2862 ShowControl( m_sbVertical
);
2863 EmbedControl( m_sbVertical
, m_controlRef
);
2864 frameOptions
&= ~kTXNWantVScrollBarMask
;
2867 if ( frameOptions
& kTXNWantHScrollBarMask
)
2869 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbHorizontal
);
2870 SetControlReference( m_sbHorizontal
, (SInt32
)this );
2871 SetControlAction( m_sbHorizontal
, gTXNScrollActionProc
);
2872 ShowControl( m_sbHorizontal
);
2873 EmbedControl( m_sbHorizontal
, m_controlRef
);
2874 frameOptions
&= ~(kTXNWantHScrollBarMask
| kTXNDrawGrowIconMask
);
2878 NULL
, m_txnWindow
, &bounds
, frameOptions
,
2879 kTXNTextEditStyleFrameType
, kTXNTextensionFile
, kTXNSystemDefaultEncoding
,
2880 &m_txn
, &m_txnFrameID
, NULL
);
2881 verify_noerr( err
);
2884 TXNControlTag iControlTags
[] = { kTXNUseCarbonEvents
};
2885 TXNControlData iControlData
[] = { { (UInt32
)&cInfo
} };
2886 int toptag
= WXSIZEOF( iControlTags
) ;
2887 TXNCarbonEventInfo cInfo
;
2888 cInfo
.useCarbonEvents
= false ;
2891 cInfo
.fDictionary
= NULL
;
2893 verify_noerr( TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) );
2896 TXNRegisterScrollInfoProc( m_txn
, gTXNScrollInfoProc
, (SInt32
)this );
2898 SetGWorld( origPort
, origDev
) ;
2904 // ----------------------------------------------------------------------------
2905 // MLTE control implementation (OSX part)
2906 // ----------------------------------------------------------------------------
2908 // tiger multi-line textcontrols with no CR in the entire content
2909 // don't scroll automatically, so we need a hack.
2910 // This attempt only works 'before' the key (ie before CallNextEventHandler)
2911 // is processed, thus the scrolling always occurs one character too late, but
2912 // better than nothing ...
2914 static const EventTypeSpec eventList
[] =
2916 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
2919 static pascal OSStatus
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2921 OSStatus result
= eventNotHandledErr
;
2922 wxMacMLTEHIViewControl
* focus
= (wxMacMLTEHIViewControl
*) data
;
2924 switch ( GetEventKind( event
) )
2926 case kEventTextInputUnicodeForKeyEvent
:
2928 TXNOffset from
, to
;
2929 TXNGetSelection( focus
->GetTXNObject() , &from
, &to
) ;
2931 TXNShowSelection( focus
->GetTXNObject() , kTXNShowStart
);
2932 result
= CallNextEventHandler(handler
,event
);
2942 static pascal OSStatus
wxMacTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2944 OSStatus result
= eventNotHandledErr
;
2946 switch ( GetEventClass( event
) )
2948 case kEventClassTextInput
:
2949 result
= wxMacUnicodeTextEventHandler( handler
, event
, data
) ;
2958 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler
)
2960 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
2961 const wxString
& str
,
2963 const wxSize
& size
, long style
) : wxMacMLTEControl( wxPeer
)
2965 m_font
= wxPeer
->GetFont() ;
2966 m_windowStyle
= style
;
2967 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
2969 wxMacConvertNewlines10To13( &st
) ;
2972 { bounds
.left
, bounds
.top
},
2973 { bounds
.right
- bounds
.left
, bounds
.bottom
- bounds
.top
} } ;
2975 m_scrollView
= NULL
;
2976 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( style
) ;
2977 if (( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
)) || !(frameOptions
&kTXNSingleLineOnlyMask
))
2979 if ( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
) )
2982 (frameOptions
& kTXNWantHScrollBarMask
? kHIScrollViewOptionsHorizScroll
: 0)
2983 | (frameOptions
& kTXNWantVScrollBarMask
? kHIScrollViewOptionsVertScroll
: 0) ,
2988 HIScrollViewCreate(kHIScrollViewOptionsVertScroll
,&m_scrollView
);
2989 HIScrollViewSetScrollBarAutoHide(m_scrollView
,true);
2992 HIViewSetFrame( m_scrollView
, &hr
);
2993 HIViewSetVisible( m_scrollView
, true );
2997 HITextViewCreate( NULL
, 0, frameOptions
, &m_textView
) ;
2998 m_txn
= HITextViewGetTXNObject( m_textView
) ;
2999 HIViewSetVisible( m_textView
, true ) ;
3002 HIViewAddSubview( m_scrollView
, m_textView
) ;
3003 m_controlRef
= m_scrollView
;
3004 wxPeer
->MacInstallEventHandler( (WXWidget
) m_textView
) ;
3008 HIViewSetFrame( m_textView
, &hr
);
3009 m_controlRef
= m_textView
;
3012 AdjustCreationAttributes( *wxWHITE
, true ) ;
3014 wxMacWindowClipper
c( m_peer
) ;
3016 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
3018 TXNSetSelection( m_txn
, 0, 0 );
3019 TXNShowSelection( m_txn
, kTXNShowStart
);
3021 InstallControlEventHandler( m_textView
, GetwxMacTextControlEventHandlerUPP(),
3022 GetEventTypeCount(eventList
), eventList
, this,
3026 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3030 OSStatus
wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart
)
3032 return SetKeyboardFocus( GetControlOwner( m_textView
), m_textView
, focusPart
) ;
3035 bool wxMacMLTEHIViewControl::HasFocus() const
3037 ControlRef control
;
3038 if ( GetUserFocusWindow() == NULL
)
3041 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
3042 return control
== m_textView
;
3045 void wxMacMLTEHIViewControl::SetBackground( const wxBrush
&brush
)
3047 wxMacMLTEControl::SetBackground( brush
) ;
3050 CGColorSpaceRef rgbSpace
= CGColorSpaceCreateDeviceRGB();
3051 RGBColor col
= MAC_WXCOLORREF(brush
.GetColour().GetPixel()) ;
3053 float component
[4] ;
3054 component
[0] = col
.red
/ 65536.0 ;
3055 component
[1] = col
.green
/ 65536.0 ;
3056 component
[2] = col
.blue
/ 65536.0 ;
3057 component
[3] = 1.0 ; // alpha
3059 CGColorRef color
= CGColorCreate( rgbSpace
, component
);
3060 HITextViewSetBackgroundColor( m_textView
, color
);
3061 CGColorSpaceRelease( rgbSpace
);
3065 #endif // wxUSE_TEXTCTRL