1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/mac/carbon/textctrl.cpp
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/textctrl.h"
23 #include "wx/button.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
31 #include <sys/types.h>
37 #if wxUSE_STD_IOSTREAM
45 #include "wx/filefn.h"
46 #include "wx/sysopt.h"
48 #include "wx/mac/uma.h"
49 #include "wx/mac/carbon/private/mactext.h"
55 virtual ~wxMacFunctor() {}
57 virtual void* operator()() = 0 ;
59 static void* CallBackProc( void *param
)
61 wxMacFunctor
* f
= (wxMacFunctor
*) param
;
62 void *result
= (*f
)() ;
67 template<typename classtype
, typename param1type
>
69 class wxMacObjectFunctor1
: public wxMacFunctor
71 typedef void (classtype::*function
)( param1type p1
) ;
72 typedef void (classtype::*ref_function
)( const param1type
& p1
) ;
74 wxMacObjectFunctor1( classtype
*obj
, function f
, param1type p1
) :
82 wxMacObjectFunctor1( classtype
*obj
, ref_function f
, param1type p1
) :
90 virtual ~wxMacObjectFunctor1() {}
92 virtual void* operator()()
94 (m_object
->*m_function
)( m_param1
) ;
100 param1type m_param1
;
103 function m_function
;
104 ref_function m_refFunction
;
108 template<typename classtype
, typename param1type
>
109 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
111 wxMacObjectFunctor1
<classtype
, param1type
> params(object
, function
, p1
) ;
113 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
117 template<typename classtype
, typename param1type
>
118 void* wxMacMPRemoteCall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
120 wxMacObjectFunctor1
<classtype
,param1type
> params(object
, function
, p1
) ;
122 MPRemoteCall( wxMacFunctor::CallBackProc
, ¶ms
, kMPOwningProcessRemoteContext
) ;
126 template<typename classtype
, typename param1type
>
127 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( param1type p1
) , param1type p1
)
130 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
135 template<typename classtype
, typename param1type
>
136 void* wxMacMPRemoteGUICall( classtype
*object
, void (classtype::*function
)( const param1type
& p1
) , param1type p1
)
139 void *result
= wxMacMPRemoteCall( object
, function
, p1
) ;
144 class WXDLLEXPORT wxMacPortSaver
146 DECLARE_NO_COPY_CLASS(wxMacPortSaver
)
149 wxMacPortSaver( GrafPtr port
);
157 Clips to the visible region of a control within the current port
160 class WXDLLEXPORT wxMacWindowClipper
: public wxMacPortSaver
162 DECLARE_NO_COPY_CLASS(wxMacWindowClipper
)
165 wxMacWindowClipper( const wxWindow
* win
);
166 ~wxMacWindowClipper();
169 RgnHandle m_formerClip
;
173 wxMacPortSaver::wxMacPortSaver( GrafPtr port
)
175 ::GetPort( &m_port
);
179 wxMacPortSaver::~wxMacPortSaver()
184 wxMacWindowClipper::wxMacWindowClipper( const wxWindow
* win
) :
185 wxMacPortSaver( (GrafPtr
) GetWindowPort( (WindowRef
) win
->MacGetTopLevelWindowRef() ) )
187 m_newPort
= (GrafPtr
) GetWindowPort( (WindowRef
) win
->MacGetTopLevelWindowRef() ) ;
188 m_formerClip
= NewRgn() ;
189 m_newClip
= NewRgn() ;
190 GetClip( m_formerClip
) ;
194 // guard against half constructed objects, this just leads to a empty clip
195 if ( win
->GetPeer() )
198 win
->MacWindowToRootWindow( &x
, &y
) ;
200 // get area including focus rect
201 HIShapeGetAsQDRgn( ((wxWindow
*)win
)->MacGetVisibleRegion(true).GetWXHRGN() , m_newClip
);
202 if ( !EmptyRgn( m_newClip
) )
203 OffsetRgn( m_newClip
, x
, y
) ;
206 SetClip( m_newClip
) ;
210 wxMacWindowClipper::~wxMacWindowClipper()
212 SetPort( m_newPort
) ;
213 SetClip( m_formerClip
) ;
214 DisposeRgn( m_newClip
) ;
215 DisposeRgn( m_formerClip
) ;
218 // common parts for implementations based on MLTE
220 class wxMacMLTEControl
: public wxMacTextControl
223 wxMacMLTEControl( wxTextCtrl
*peer
) ;
225 virtual wxString
GetStringValue() const ;
226 virtual void SetStringValue( const wxString
&str
) ;
228 static TXNFrameOptions
FrameOptionsFromWXStyle( long wxStyle
) ;
230 void AdjustCreationAttributes( const wxColour
& background
, bool visible
) ;
232 virtual void SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
) ;
233 virtual void SetBackgroundColour(const wxColour
& col
);
234 virtual void SetStyle( long start
, long end
, const wxTextAttr
& style
) ;
235 virtual void Copy() ;
237 virtual void Paste() ;
238 virtual bool CanPaste() const ;
239 virtual void SetEditable( bool editable
) ;
240 virtual wxTextPos
GetLastPosition() const ;
241 virtual void Replace( long from
, long to
, const wxString
&str
) ;
242 virtual void Remove( long from
, long to
) ;
243 virtual void GetSelection( long* from
, long* to
) const ;
244 virtual void SetSelection( long from
, long to
) ;
246 virtual void WriteText( const wxString
& str
) ;
248 virtual bool HasOwnContextMenu() const
250 TXNCommandEventSupportOptions options
;
251 TXNGetCommandEventSupport( m_txn
, & options
) ;
252 return options
& kTXNSupportEditCommandProcessing
;
255 virtual void CheckSpelling(bool check
)
257 TXNSetSpellCheckAsYouType( m_txn
, (Boolean
) check
);
259 virtual void Clear() ;
261 virtual bool CanUndo() const ;
262 virtual void Undo() ;
263 virtual bool CanRedo() const;
264 virtual void Redo() ;
265 virtual int GetNumberOfLines() const ;
266 virtual long XYToPosition(long x
, long y
) const ;
267 virtual bool PositionToXY(long pos
, long *x
, long *y
) const ;
268 virtual void ShowPosition( long pos
) ;
269 virtual int GetLineLength(long lineNo
) const ;
270 virtual wxString
GetLineText(long lineNo
) const ;
272 void SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
) ;
273 TXNObject
GetTXNObject() { return m_txn
; }
276 void TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
) ;
281 // implementation available under OSX
283 class wxMacMLTEHIViewControl
: public wxMacMLTEControl
286 wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
289 const wxSize
& size
, long style
) ;
290 virtual ~wxMacMLTEHIViewControl() ;
292 virtual OSStatus
SetFocus( ControlFocusPart focusPart
) ;
293 virtual bool HasFocus() const ;
294 virtual void SetBackgroundColour(const wxColour
& col
) ;
297 HIViewRef m_scrollView
;
298 HIViewRef m_textView
;
301 // 'classic' MLTE implementation
303 class wxMacMLTEClassicControl
: public wxMacMLTEControl
306 wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
309 const wxSize
& size
, long style
) ;
310 virtual ~wxMacMLTEClassicControl() ;
312 virtual void VisibilityChanged(bool shown
) ;
313 virtual void SuperChangedPosition() ;
315 virtual void MacControlUserPaneDrawProc(wxInt16 part
) ;
316 virtual wxInt16
MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
) ;
317 virtual wxInt16
MacControlUserPaneTrackingProc(wxInt16 x
, wxInt16 y
, void* actionProc
) ;
318 virtual void MacControlUserPaneIdleProc() ;
319 virtual wxInt16
MacControlUserPaneKeyDownProc(wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
) ;
320 virtual void MacControlUserPaneActivateProc(bool activating
) ;
321 virtual wxInt16
MacControlUserPaneFocusProc(wxInt16 action
) ;
322 virtual void MacControlUserPaneBackgroundProc(void* info
) ;
324 virtual bool SetupCursor( const wxPoint
& WXUNUSED(pt
) )
326 MacControlUserPaneIdleProc();
330 virtual void SetRect( Rect
*r
) ;
335 void MacUpdatePosition() ;
336 void MacActivatePaneText(bool setActive
) ;
337 void MacFocusPaneText(bool setFocus
) ;
338 void MacSetObjectVisibility(bool vis
) ;
341 TXNFrameID m_txnFrameID
;
343 WindowRef m_txnWindow
;
344 // bounds of the control as we last did set the txn frames
345 Rect m_txnControlBounds
;
346 Rect m_txnVisBounds
;
348 static pascal void TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
) ;
349 static pascal void TXNScrollInfoProc(
350 SInt32 iValue
, SInt32 iMaximumValue
,
351 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
) ;
353 ControlRef m_sbHorizontal
;
354 SInt32 m_lastHorizontalValue
;
355 ControlRef m_sbVertical
;
356 SInt32 m_lastVerticalValue
;
360 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
362 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
363 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
364 EVT_CHAR(wxTextCtrl::OnChar
)
365 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
366 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
367 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
368 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
369 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
370 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
371 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
373 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
375 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
376 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
377 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
378 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
379 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
380 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
381 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
385 void wxTextCtrl::Init()
391 m_privateContextMenu
= NULL
;
392 m_triggerOnSetValue
= true ;
395 wxTextCtrl::~wxTextCtrl()
397 delete m_privateContextMenu
;
400 bool wxTextCtrl::Create( wxWindow
*parent
,
406 const wxValidator
& validator
,
407 const wxString
& name
)
409 m_macIsUserPane
= false ;
412 if ( ! (style
& wxNO_BORDER
) )
413 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
415 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
418 if ( m_windowStyle
& wxTE_MULTILINE
)
420 // always turn on this style for multi-line controls
421 m_windowStyle
|= wxTE_PROCESS_ENTER
;
422 style
|= wxTE_PROCESS_ENTER
;
425 CreatePeer( str
, pos
, size
, style
);
427 MacPostControlCreate(pos
, size
) ;
429 // only now the embedding is correct and we can do a positioning update
431 MacSuperChangedPosition() ;
433 if ( m_windowStyle
& wxTE_READONLY
)
434 SetEditable( false ) ;
436 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
441 void wxTextCtrl::CreatePeer(
444 const wxSize
& size
, long style
)
446 bool forceMLTE
= false ;
448 #if wxUSE_SYSTEM_OPTIONS
449 if (wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_MLTE
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_MLTE
) == 1))
455 if ( UMAGetSystemVersion() >= 0x1050 )
460 if ( m_windowStyle
& wxTE_MULTILINE
|| ( UMAGetSystemVersion() >= 0x1050 ) )
461 m_peer
= new wxMacMLTEHIViewControl( this , str
, pos
, size
, style
) ;
466 if ( !(m_windowStyle
& wxTE_MULTILINE
) && !forceMLTE
)
468 m_peer
= new wxMacUnicodeTextControl( this , str
, pos
, size
, style
) ;
472 // the horizontal single line scrolling bug that made us keep the classic implementation
474 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
476 m_peer
= new wxMacMLTEClassicControl( this , str
, pos
, size
, style
) ;
480 void wxTextCtrl::MacSuperChangedPosition()
482 wxWindow::MacSuperChangedPosition() ;
483 GetPeer()->SuperChangedPosition() ;
486 void wxTextCtrl::MacVisibilityChanged()
488 GetPeer()->VisibilityChanged( MacIsReallyShown() ) ;
491 void wxTextCtrl::MacCheckSpelling(bool check
)
493 GetPeer()->CheckSpelling(check
);
496 wxString
wxTextCtrl::GetValue() const
498 return GetPeer()->GetStringValue() ;
501 void wxTextCtrl::GetSelection(long* from
, long* to
) const
503 GetPeer()->GetSelection( from
, to
) ;
506 void wxTextCtrl::DoSetValue(const wxString
& str
, int flags
)
509 if ( GetValue() == str
)
512 GetPeer()->SetStringValue( str
) ;
514 if ( (flags
& SetValue_SendEvent
) && m_triggerOnSetValue
)
516 SendTextUpdatedEvent();
520 void wxTextCtrl::SetMaxLength(unsigned long len
)
525 bool wxTextCtrl::SetFont( const wxFont
& font
)
527 if ( !wxTextCtrlBase::SetFont( font
) )
530 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle() ) ;
535 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
537 GetPeer()->SetStyle( start
, end
, style
) ;
542 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
544 wxTextCtrlBase::SetDefaultStyle( style
) ;
545 SetStyle( kTXNUseCurrentSelection
, kTXNUseCurrentSelection
, GetDefaultStyle() ) ;
550 // Clipboard operations
552 void wxTextCtrl::Copy()
558 void wxTextCtrl::Cut()
564 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
565 event
.SetEventObject( this );
566 HandleWindowEvent( event
);
570 void wxTextCtrl::Paste()
576 // TODO: eventually we should add setting the default style again
578 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
579 event
.SetEventObject( this );
580 HandleWindowEvent( event
);
584 bool wxTextCtrl::CanCopy() const
586 // Can copy if there's a selection
588 GetSelection( &from
, &to
);
593 bool wxTextCtrl::CanCut() const
598 // Can cut if there's a selection
600 GetSelection( &from
, &to
);
605 bool wxTextCtrl::CanPaste() const
610 return GetPeer()->CanPaste() ;
613 void wxTextCtrl::SetEditable(bool editable
)
615 if ( editable
!= m_editable
)
617 m_editable
= editable
;
618 GetPeer()->SetEditable( editable
) ;
622 void wxTextCtrl::SetInsertionPoint(long pos
)
624 SetSelection( pos
, pos
) ;
627 void wxTextCtrl::SetInsertionPointEnd()
629 wxTextPos pos
= GetLastPosition();
630 SetInsertionPoint( pos
);
633 long wxTextCtrl::GetInsertionPoint() const
636 GetSelection( &begin
, &end
) ;
641 wxTextPos
wxTextCtrl::GetLastPosition() const
643 return GetPeer()->GetLastPosition() ;
646 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
648 GetPeer()->Replace( from
, to
, str
) ;
651 void wxTextCtrl::Remove(long from
, long to
)
653 GetPeer()->Remove( from
, to
) ;
656 void wxTextCtrl::SetSelection(long from
, long to
)
658 GetPeer()->SetSelection( from
, to
) ;
661 void wxTextCtrl::WriteText(const wxString
& str
)
663 // TODO: this MPRemoting will be moved into a remoting peer proxy for any command
664 if ( !wxIsMainThread() )
666 // unfortunately CW 8 is not able to correctly deduce the template types,
667 // so we have to instantiate explicitly
668 wxMacMPRemoteGUICall
<wxTextCtrl
,wxString
>( this , &wxTextCtrl::WriteText
, str
) ;
673 GetPeer()->WriteText( str
) ;
676 void wxTextCtrl::AppendText(const wxString
& text
)
678 SetInsertionPointEnd();
682 void wxTextCtrl::Clear()
687 bool wxTextCtrl::IsModified() const
692 bool wxTextCtrl::IsEditable() const
694 return IsEnabled() && m_editable
;
697 bool wxTextCtrl::AcceptsFocus() const
699 // we don't want focus if we can't be edited
700 return /*IsEditable() && */ wxControl::AcceptsFocus();
703 wxSize
wxTextCtrl::DoGetBestSize() const
707 // these are the numbers from the HIG:
708 // we reduce them by the borders first
711 switch ( m_windowVariant
)
713 case wxWINDOW_VARIANT_NORMAL
:
717 case wxWINDOW_VARIANT_SMALL
:
721 case wxWINDOW_VARIANT_MINI
:
730 // as the above numbers have some free space around the text
731 // we get 5 lines like this anyway
732 if ( m_windowStyle
& wxTE_MULTILINE
)
735 if ( !HasFlag(wxNO_BORDER
) )
738 return wxSize(wText
, hText
);
741 // ----------------------------------------------------------------------------
743 // ----------------------------------------------------------------------------
745 void wxTextCtrl::Undo()
751 void wxTextCtrl::Redo()
757 bool wxTextCtrl::CanUndo() const
762 return GetPeer()->CanUndo() ;
765 bool wxTextCtrl::CanRedo() const
770 return GetPeer()->CanRedo() ;
773 void wxTextCtrl::MarkDirty()
778 void wxTextCtrl::DiscardEdits()
783 int wxTextCtrl::GetNumberOfLines() const
785 return GetPeer()->GetNumberOfLines() ;
788 long wxTextCtrl::XYToPosition(long x
, long y
) const
790 return GetPeer()->XYToPosition( x
, y
) ;
793 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
795 return GetPeer()->PositionToXY( pos
, x
, y
) ;
798 void wxTextCtrl::ShowPosition(long pos
)
800 return GetPeer()->ShowPosition(pos
) ;
803 int wxTextCtrl::GetLineLength(long lineNo
) const
805 return GetPeer()->GetLineLength(lineNo
) ;
808 wxString
wxTextCtrl::GetLineText(long lineNo
) const
810 return GetPeer()->GetLineText(lineNo
) ;
813 void wxTextCtrl::Command(wxCommandEvent
& event
)
815 SetValue(event
.GetString());
816 ProcessCommand(event
);
819 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
821 // By default, load the first file into the text window.
822 if (event
.GetNumberOfFiles() > 0)
823 LoadFile( event
.GetFiles()[0] );
826 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
828 int key
= event
.GetKeyCode() ;
829 bool eat_key
= false ;
831 if ( key
== 'a' && event
.MetaDown() )
838 if ( key
== 'c' && event
.MetaDown() )
846 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
847 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
848 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
855 // Check if we have reached the max # of chars (if it is set), but still
856 // allow navigation and deletion
857 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
858 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&&
859 key
!= WXK_BACK
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) )
862 // eat it, we don't want to add more than allowed # of characters
864 // TODO: generate EVT_TEXT_MAXLEN()
868 // assume that any key not processed yet is going to modify the control
871 if ( key
== 'v' && event
.MetaDown() )
879 if ( key
== 'x' && event
.MetaDown() )
890 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
892 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
893 event
.SetEventObject( this );
894 event
.SetString( GetValue() );
895 if ( HandleWindowEvent(event
) )
899 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
901 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
902 if ( tlw
&& tlw
->GetDefaultItem() )
904 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
905 if ( def
&& def
->IsEnabled() )
907 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
908 event
.SetEventObject(def
);
915 // this will make wxWidgets eat the ENTER key so that
916 // we actually prevent line wrapping in a single line text control
922 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
925 if (!event
.ShiftDown())
926 flags
|= wxNavigationKeyEvent::IsForward
;
927 if (event
.ControlDown())
928 flags
|= wxNavigationKeyEvent::WinChange
;
935 // This is necessary (don't know why);
936 // otherwise the tab will not be inserted.
937 WriteText(wxT("\t"));
948 // perform keystroke handling
952 if ( ( key
>= 0x20 && key
< WXK_START
) ||
953 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
958 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
959 event1
.SetEventObject( this );
960 wxPostEvent( GetEventHandler(), event1
);
964 // ----------------------------------------------------------------------------
965 // standard handlers for standard edit menu events
966 // ----------------------------------------------------------------------------
968 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
973 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
978 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
983 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
988 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
993 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
997 GetSelection( &from
, &to
);
998 if (from
!= -1 && to
!= -1)
1002 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
1004 SetSelection(-1, -1);
1007 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
1009 event
.Enable( CanCut() );
1012 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
1014 event
.Enable( CanCopy() );
1017 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
1019 event
.Enable( CanPaste() );
1022 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
1024 event
.Enable( CanUndo() );
1027 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
1029 event
.Enable( CanRedo() );
1032 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
1036 GetSelection( &from
, &to
);
1037 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
1040 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
1042 event
.Enable(GetLastPosition() > 0);
1045 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
1047 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
1049 if ( GetPeer()->HasOwnContextMenu() )
1055 if (m_privateContextMenu
== NULL
)
1057 m_privateContextMenu
= new wxMenu
;
1058 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
1059 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
1060 m_privateContextMenu
->AppendSeparator();
1061 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
1062 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
1063 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
1064 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
1065 m_privateContextMenu
->AppendSeparator();
1066 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
1069 if (m_privateContextMenu
!= NULL
)
1070 PopupMenu(m_privateContextMenu
);
1073 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
1075 if ( !GetPeer()->SetupCursor( pt
) )
1076 return wxWindow::MacSetupCursor( pt
) ;
1081 // ----------------------------------------------------------------------------
1082 // implementation base class
1083 // ----------------------------------------------------------------------------
1085 wxMacTextControl::wxMacTextControl(wxTextCtrl
* peer
) :
1086 wxMacControl( peer
)
1090 wxMacTextControl::~wxMacTextControl()
1094 void wxMacTextControl::SetStyle(long WXUNUSED(start
),
1096 const wxTextAttr
& WXUNUSED(style
))
1100 void wxMacTextControl::Copy()
1104 void wxMacTextControl::Cut()
1108 void wxMacTextControl::Paste()
1112 bool wxMacTextControl::CanPaste() const
1117 void wxMacTextControl::SetEditable(bool WXUNUSED(editable
))
1121 wxTextPos
wxMacTextControl::GetLastPosition() const
1123 return GetStringValue().length() ;
1126 void wxMacTextControl::Replace( long from
, long to
, const wxString
&val
)
1128 SetSelection( from
, to
) ;
1132 void wxMacTextControl::Remove( long from
, long to
)
1134 SetSelection( from
, to
) ;
1135 WriteText( wxEmptyString
) ;
1138 void wxMacTextControl::Clear()
1140 SetStringValue( wxEmptyString
) ;
1143 bool wxMacTextControl::CanUndo() const
1148 void wxMacTextControl::Undo()
1152 bool wxMacTextControl::CanRedo() const
1157 void wxMacTextControl::Redo()
1161 long wxMacTextControl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
1166 bool wxMacTextControl::PositionToXY(long WXUNUSED(pos
),
1168 long *WXUNUSED(y
)) const
1173 void wxMacTextControl::ShowPosition( long WXUNUSED(pos
) )
1177 int wxMacTextControl::GetNumberOfLines() const
1179 ItemCount lines
= 0 ;
1180 wxString content
= GetStringValue() ;
1183 for (size_t i
= 0; i
< content
.length() ; i
++)
1185 if (content
[i
] == '\r')
1192 wxString
wxMacTextControl::GetLineText(long lineNo
) const
1194 // TODO: change this if possible to reflect real lines
1195 wxString content
= GetStringValue() ;
1199 for (size_t i
= 0; i
< content
.length() ; i
++)
1201 if (count
== lineNo
)
1203 // Add chars in line then
1206 for (size_t j
= i
; j
< content
.length(); j
++)
1208 if (content
[j
] == '\n')
1217 if (content
[i
] == '\n')
1221 return wxEmptyString
;
1224 int wxMacTextControl::GetLineLength(long lineNo
) const
1226 // TODO: change this if possible to reflect real lines
1227 wxString content
= GetStringValue() ;
1231 for (size_t i
= 0; i
< content
.length() ; i
++)
1233 if (count
== lineNo
)
1235 // Count chars in line then
1237 for (size_t j
= i
; j
< content
.length(); j
++)
1240 if (content
[j
] == '\n')
1247 if (content
[i
] == '\n')
1254 // ----------------------------------------------------------------------------
1255 // standard unicode control implementation
1256 // ----------------------------------------------------------------------------
1258 // the current unicode textcontrol implementation has a bug : only if the control
1259 // is currently having the focus, the selection can be retrieved by the corresponding
1260 // data tag. So we have a mirroring using a member variable
1261 // TODO : build event table using virtual member functions for wxMacControl
1263 static const EventTypeSpec unicodeTextControlEventList
[] =
1265 { kEventClassControl
, kEventControlSetFocusPart
} ,
1268 static pascal OSStatus
wxMacUnicodeTextControlControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1270 OSStatus result
= eventNotHandledErr
;
1271 wxMacUnicodeTextControl
* focus
= (wxMacUnicodeTextControl
*) data
;
1272 wxMacCarbonEvent
cEvent( event
) ;
1274 switch ( GetEventKind( event
) )
1276 case kEventControlSetFocusPart
:
1278 ControlPartCode controlPart
= cEvent
.GetParameter
<ControlPartCode
>(kEventParamControlPart
, typeControlPartCode
);
1279 if ( controlPart
== kControlFocusNoPart
)
1281 // about to loose focus -> store selection to field
1282 focus
->GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1284 result
= CallNextEventHandler(handler
,event
) ;
1285 if ( controlPart
!= kControlFocusNoPart
)
1287 // about to gain focus -> set selection from field
1288 focus
->SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &focus
->m_selection
);
1299 static pascal OSStatus
wxMacUnicodeTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
1301 OSStatus result
= eventNotHandledErr
;
1303 switch ( GetEventClass( event
) )
1305 case kEventClassControl
:
1306 result
= wxMacUnicodeTextControlControlEventHandler( handler
, event
, data
) ;
1315 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacUnicodeTextControlEventHandler
)
1317 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
) : wxMacTextControl( wxPeer
)
1321 wxMacUnicodeTextControl::wxMacUnicodeTextControl( wxTextCtrl
*wxPeer
,
1322 const wxString
& str
,
1324 const wxSize
& size
, long style
)
1325 : wxMacTextControl( wxPeer
)
1327 Create( wxPeer
, str
, pos
, size
, style
);
1330 bool wxMacUnicodeTextControl::Create( wxTextCtrl
*wxPeer
,
1331 const wxString
& str
,
1333 const wxSize
& size
, long style
)
1335 m_font
= wxPeer
->GetFont() ;
1336 m_windowStyle
= style
;
1337 m_selection
.selStart
= m_selection
.selEnd
= 0;
1338 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
1340 wxMacConvertNewlines10To13( &st
) ;
1341 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding()) ;
1342 CFStringRef cfr
= cf
;
1344 m_valueTag
= kControlEditTextCFStringTag
;
1345 CreateControl( wxPeer
, &bounds
, cfr
);
1347 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
1348 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextSingleLineTag
, true ) ;
1350 InstallControlEventHandler( m_controlRef
, GetwxMacUnicodeTextControlEventHandlerUPP(),
1351 GetEventTypeCount(unicodeTextControlEventList
), unicodeTextControlEventList
, this,
1357 wxMacUnicodeTextControl::~wxMacUnicodeTextControl()
1361 void wxMacUnicodeTextControl::VisibilityChanged(bool shown
)
1363 if ( !(m_windowStyle
& wxTE_MULTILINE
) && shown
)
1365 // work around a refresh issue insofar as not always the entire content is shown,
1366 // even if this would be possible
1367 ControlEditTextSelectionRec sel
;
1368 CFStringRef value
= NULL
;
1370 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1371 verify_noerr( GetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1372 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, &value
) );
1373 verify_noerr( SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) );
1375 CFRelease( value
) ;
1379 wxString
wxMacUnicodeTextControl::GetStringValue() const
1382 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1385 wxMacCFStringHolder
cf(value
) ;
1386 result
= cf
.AsString() ;
1390 wxMacConvertNewlines13To10( &result
) ;
1392 wxMacConvertNewlines10To13( &result
) ;
1398 void wxMacUnicodeTextControl::SetStringValue( const wxString
&str
)
1401 wxMacConvertNewlines10To13( &st
) ;
1402 wxMacCFStringHolder
cf( st
, m_font
.GetEncoding() ) ;
1403 verify_noerr( SetData
<CFStringRef
>( 0, m_valueTag
, cf
) ) ;
1406 void wxMacUnicodeTextControl::CreateControl( wxTextCtrl
* peer
, const Rect
* bounds
, CFStringRef cfr
)
1408 Boolean isPassword
= ( m_windowStyle
& wxTE_PASSWORD
) != 0 ;
1411 m_valueTag
= kControlEditTextPasswordCFStringTag
;
1413 OSStatus err
= CreateEditUnicodeTextControl(
1414 MAC_WXHWND(peer
->MacGetTopLevelWindowRef()), bounds
, cfr
,
1415 isPassword
, NULL
, &m_controlRef
) ;
1416 verify_noerr( err
);
1419 void wxMacUnicodeTextControl::Copy()
1421 SendHICommand( kHICommandCopy
) ;
1424 void wxMacUnicodeTextControl::Cut()
1426 SendHICommand( kHICommandCut
) ;
1429 void wxMacUnicodeTextControl::Paste()
1431 SendHICommand( kHICommandPaste
) ;
1434 bool wxMacUnicodeTextControl::CanPaste() const
1439 void wxMacUnicodeTextControl::SetEditable(bool WXUNUSED(editable
))
1441 #if 0 // leads to problem because text cannot be selected anymore
1442 SetData
<Boolean
>( kControlEditTextPart
, kControlEditTextLockedTag
, (Boolean
) !editable
) ;
1446 void wxMacUnicodeTextControl::GetSelection( long* from
, long* to
) const
1448 ControlEditTextSelectionRec sel
;
1450 verify_noerr( GetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ) ;
1455 *from
= sel
.selStart
;
1460 void wxMacUnicodeTextControl::SetSelection( long from
, long to
)
1462 ControlEditTextSelectionRec sel
;
1464 int textLength
= 0 ;
1465 CFStringRef value
= GetData
<CFStringRef
>(0, m_valueTag
) ;
1468 wxMacCFStringHolder
cf(value
) ;
1469 textLength
= cf
.AsString().length() ;
1472 if ((from
== -1) && (to
== -1))
1479 from
= wxMin(textLength
,wxMax(from
,0)) ;
1483 to
= wxMax(0,wxMin(textLength
,to
)) ;
1486 sel
.selStart
= from
;
1489 SetData
<ControlEditTextSelectionRec
>( 0, kControlEditTextSelectionTag
, &sel
) ;
1494 void wxMacUnicodeTextControl::WriteText( const wxString
& str
)
1497 wxMacConvertNewlines10To13( &st
) ;
1501 wxMacCFStringHolder
cf(st
, m_font
.GetEncoding() ) ;
1502 CFStringRef value
= cf
;
1503 SetData
<CFStringRef
>( 0, kControlEditTextInsertCFStringRefTag
, &value
);
1507 wxString val
= GetStringValue() ;
1509 GetSelection( &start
, &end
) ;
1510 val
.Remove( start
, end
- start
) ;
1511 val
.insert( start
, str
) ;
1512 SetStringValue( val
) ;
1513 SetSelection( start
+ str
.length() , start
+ str
.length() ) ;
1517 // ----------------------------------------------------------------------------
1518 // MLTE control implementation (common part)
1519 // ----------------------------------------------------------------------------
1521 // if MTLE is read only, no changes at all are allowed, not even from
1522 // procedural API, in order to allow changes via API all the same we must undo
1523 // the readonly status while we are executing, this class helps to do so
1525 class wxMacEditHelper
1528 wxMacEditHelper( TXNObject txn
)
1530 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1532 TXNGetTXNObjectControls( m_txn
, 1 , tag
, m_data
) ;
1533 if ( m_data
[0].uValue
== kTXNReadOnly
)
1535 TXNControlData data
[] = { { kTXNReadWrite
} } ;
1536 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, data
) ;
1542 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1543 if ( m_data
[0].uValue
== kTXNReadOnly
)
1544 TXNSetTXNObjectControls( m_txn
, false , 1 , tag
, m_data
) ;
1549 TXNControlData m_data
[1] ;
1552 wxMacMLTEControl::wxMacMLTEControl( wxTextCtrl
*peer
)
1553 : wxMacTextControl( peer
)
1555 SetNeedsFocusRect( true ) ;
1558 wxString
wxMacMLTEControl::GetStringValue() const
1562 Size actualSize
= 0;
1567 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNUnicodeTextData
);
1576 actualSize
= GetHandleSize( theText
) / sizeof(UniChar
) ;
1577 if ( actualSize
> 0 )
1579 wxChar
*ptr
= NULL
;
1581 #if SIZEOF_WCHAR_T == 2
1582 ptr
= new wxChar
[actualSize
+ 1] ;
1583 wxStrncpy( ptr
, (wxChar
*)(*theText
) , actualSize
) ;
1585 SetHandleSize( theText
, (actualSize
+ 1) * sizeof(UniChar
) ) ;
1587 (((UniChar
*)*theText
)[actualSize
]) = 0 ;
1588 wxMBConvUTF16 converter
;
1589 size_t noChars
= converter
.MB2WC( NULL
, (const char*)*theText
, 0 ) ;
1590 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Unable to count the number of characters in this string!") );
1591 ptr
= new wxChar
[noChars
+ 1] ;
1593 noChars
= converter
.MB2WC( ptr
, (const char*)*theText
, noChars
+ 1 ) ;
1594 wxASSERT_MSG( noChars
!= wxCONV_FAILED
, _T("Conversion of string failed!") );
1596 HUnlock( theText
) ;
1599 ptr
[actualSize
] = 0 ;
1600 result
= wxString( ptr
) ;
1604 DisposeHandle( theText
) ;
1608 err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1617 actualSize
= GetHandleSize( theText
) ;
1618 if ( actualSize
> 0 )
1621 result
= wxString( *theText
, wxConvLocal
, actualSize
) ;
1622 HUnlock( theText
) ;
1625 DisposeHandle( theText
) ;
1631 wxMacConvertNewlines13To10( &result
) ;
1633 wxMacConvertNewlines10To13( &result
) ;
1639 void wxMacMLTEControl::SetStringValue( const wxString
&str
)
1642 wxMacConvertNewlines10To13( &st
);
1646 wxMacWindowClipper
c( m_peer
) ;
1650 wxMacEditHelper
help( m_txn
);
1651 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
);
1654 TXNSetSelection( m_txn
, 0, 0 );
1655 TXNShowSelection( m_txn
, kTXNShowStart
);
1659 TXNFrameOptions
wxMacMLTEControl::FrameOptionsFromWXStyle( long wxStyle
)
1661 TXNFrameOptions frameOptions
= kTXNDontDrawCaretWhenInactiveMask
;
1663 frameOptions
|= kTXNDoFontSubstitutionMask
;
1665 if ( ! (wxStyle
& wxTE_NOHIDESEL
) )
1666 frameOptions
|= kTXNDontDrawSelectionWhenInactiveMask
;
1668 if ( wxStyle
& (wxHSCROLL
| wxTE_DONTWRAP
) )
1669 frameOptions
|= kTXNWantHScrollBarMask
;
1671 if ( wxStyle
& wxTE_MULTILINE
)
1673 if ( ! (wxStyle
& wxTE_DONTWRAP
) )
1674 frameOptions
|= kTXNAlwaysWrapAtViewEdgeMask
;
1676 if ( !(wxStyle
& wxTE_NO_VSCROLL
) )
1678 frameOptions
|= kTXNWantVScrollBarMask
;
1680 // The following code causes drawing problems on 10.4. Perhaps it can be restored for
1681 // older versions of the OS, but I'm not sure it's appropriate to put a grow icon here
1682 // anyways, as AFAIK users can't actually use it to resize the text ctrl.
1683 // if ( frameOptions & kTXNWantHScrollBarMask )
1684 // frameOptions |= kTXNDrawGrowIconMask ;
1689 frameOptions
|= kTXNSingleLineOnlyMask
;
1692 return frameOptions
;
1695 void wxMacMLTEControl::AdjustCreationAttributes(const wxColour
&background
,
1696 bool WXUNUSED(visible
))
1698 TXNControlTag iControlTags
[] =
1700 kTXNDoFontSubstitution
,
1701 kTXNWordWrapStateTag
,
1703 TXNControlData iControlData
[] =
1709 int toptag
= WXSIZEOF( iControlTags
) ;
1711 if ( m_windowStyle
& wxTE_MULTILINE
)
1713 iControlData
[1].uValue
=
1714 (m_windowStyle
& wxTE_DONTWRAP
)
1719 OSStatus err
= TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) ;
1720 verify_noerr( err
);
1722 // setting the default font:
1723 // under 10.2 this causes a visible caret, therefore we avoid it
1729 GetThemeFont( kThemeSystemFont
, GetApplicationScript() , fontName
, &fontSize
, &fontStyle
) ;
1731 TXNTypeAttributes typeAttr
[] =
1733 { kTXNQDFontNameAttribute
, kTXNQDFontNameAttributeSize
, { (void*) fontName
} } ,
1734 { kTXNQDFontSizeAttribute
, kTXNFontSizeAttributeSize
, { (void*) (fontSize
<< 16) } } ,
1735 { kTXNQDFontStyleAttribute
, kTXNQDFontStyleAttributeSize
, { (void*) normal
} } ,
1738 err
= TXNSetTypeAttributes(
1739 m_txn
, sizeof(typeAttr
) / sizeof(TXNTypeAttributes
),
1740 typeAttr
, kTXNStartOffset
, kTXNEndOffset
);
1741 verify_noerr( err
);
1743 if ( m_windowStyle
& wxTE_PASSWORD
)
1745 UniChar c
= 0x00A5 ;
1746 err
= TXNEchoMode( m_txn
, c
, 0 , true );
1747 verify_noerr( err
);
1750 TXNBackground tback
;
1751 tback
.bgType
= kTXNBackgroundTypeRGB
;
1752 background
.GetRGBColor( &tback
.bg
.color
);
1753 TXNSetBackground( m_txn
, &tback
);
1756 TXNCommandEventSupportOptions options
;
1757 if ( TXNGetCommandEventSupport( m_txn
, &options
) == noErr
)
1760 kTXNSupportEditCommandProcessing
1761 | kTXNSupportEditCommandUpdating
1762 | kTXNSupportFontCommandProcessing
1763 | kTXNSupportFontCommandUpdating
;
1765 // only spell check when not read-only
1766 // use system options for the default
1767 bool checkSpelling
= false ;
1768 if ( !(m_windowStyle
& wxTE_READONLY
) )
1770 #if wxUSE_SYSTEM_OPTIONS
1771 if ( wxSystemOptions::HasOption( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) && (wxSystemOptions::GetOptionInt( wxMAC_TEXTCONTROL_USE_SPELL_CHECKER
) == 1) )
1773 checkSpelling
= true ;
1778 if ( checkSpelling
)
1780 kTXNSupportSpellCheckCommandProcessing
1781 | kTXNSupportSpellCheckCommandUpdating
;
1783 TXNSetCommandEventSupport( m_txn
, options
) ;
1787 void wxMacMLTEControl::SetBackgroundColour(const wxColour
& col
)
1789 TXNBackground tback
;
1790 tback
.bgType
= kTXNBackgroundTypeRGB
;
1791 col
.GetRGBColor(&tback
.bg
.color
);
1792 TXNSetBackground( m_txn
, &tback
);
1795 static inline int wxConvertToTXN(int x
)
1797 return wx_static_cast(int, x
/ 254.0 * 72 + 0.5);
1800 void wxMacMLTEControl::TXNSetAttribute( const wxTextAttr
& style
, long from
, long to
)
1802 TXNTypeAttributes typeAttr
[4] ;
1804 size_t typeAttrCount
= 0 ;
1807 TXNControlTag controlTags
[4];
1808 TXNControlData controlData
[4];
1809 size_t controlAttrCount
= 0;
1811 TXNTab
* tabs
= NULL
;
1813 bool relayout
= false;
1815 if ( style
.HasFont() )
1817 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1818 const wxFont
&font
= style
.GetFont() ;
1819 typeAttr
[typeAttrCount
].tag
= kTXNATSUIStyle
;
1820 typeAttr
[typeAttrCount
].size
= kTXNATSUIStyleSize
;
1821 typeAttr
[typeAttrCount
].data
.dataPtr
= font
.MacGetATSUStyle() ;
1825 if ( style
.HasTextColour() )
1827 wxASSERT( typeAttrCount
< WXSIZEOF(typeAttr
) );
1828 style
.GetTextColour().GetRGBColor( &color
);
1829 typeAttr
[typeAttrCount
].tag
= kTXNQDFontColorAttribute
;
1830 typeAttr
[typeAttrCount
].size
= kTXNQDFontColorAttributeSize
;
1831 typeAttr
[typeAttrCount
].data
.dataPtr
= (void*) &color
;
1835 if ( style
.HasAlignment() )
1837 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1840 switch ( style
.GetAlignment() )
1842 case wxTEXT_ALIGNMENT_LEFT
:
1843 align
= kTXNFlushLeft
;
1845 case wxTEXT_ALIGNMENT_CENTRE
:
1848 case wxTEXT_ALIGNMENT_RIGHT
:
1849 align
= kTXNFlushRight
;
1851 case wxTEXT_ALIGNMENT_JUSTIFIED
:
1852 align
= kTXNFullJust
;
1855 case wxTEXT_ALIGNMENT_DEFAULT
:
1856 align
= kTXNFlushDefault
;
1860 controlTags
[controlAttrCount
] = kTXNJustificationTag
;
1861 controlData
[controlAttrCount
].sValue
= align
;
1862 controlAttrCount
++ ;
1865 if ( style
.HasLeftIndent() || style
.HasRightIndent() )
1867 wxASSERT( controlAttrCount
< WXSIZEOF(controlTags
) );
1868 controlTags
[controlAttrCount
] = kTXNMarginsTag
;
1869 controlData
[controlAttrCount
].marginsPtr
= &margins
;
1870 verify_noerr( TXNGetTXNObjectControls (m_txn
, 1 ,
1871 &controlTags
[controlAttrCount
], &controlData
[controlAttrCount
]) );
1872 if ( style
.HasLeftIndent() )
1874 margins
.leftMargin
= wxConvertToTXN(style
.GetLeftIndent());
1876 if ( style
.HasRightIndent() )
1878 margins
.rightMargin
= wxConvertToTXN(style
.GetRightIndent());
1880 controlAttrCount
++ ;
1883 if ( style
.HasTabs() )
1885 const wxArrayInt
& tabarray
= style
.GetTabs();
1886 // unfortunately Mac only applies a tab distance, not individually different tabs
1887 controlTags
[controlAttrCount
] = kTXNTabSettingsTag
;
1888 if ( tabarray
.size() > 0 )
1889 controlData
[controlAttrCount
].tabValue
.value
= wxConvertToTXN(tabarray
[0]);
1891 controlData
[controlAttrCount
].tabValue
.value
= 72 ;
1893 controlData
[controlAttrCount
].tabValue
.tabType
= kTXNLeftTab
;
1894 controlAttrCount
++ ;
1897 // unfortunately the relayout is not automatic
1898 if ( controlAttrCount
> 0 )
1900 verify_noerr( TXNSetTXNObjectControls (m_txn
, false /* don't clear all */, controlAttrCount
,
1901 controlTags
, controlData
) );
1905 if ( typeAttrCount
> 0 )
1907 verify_noerr( TXNSetTypeAttributes( m_txn
, typeAttrCount
, typeAttr
, from
, to
) );
1918 TXNRecalcTextLayout( m_txn
);
1922 void wxMacMLTEControl::SetFont(const wxFont
& font
,
1923 const wxColour
& foreground
,
1924 long WXUNUSED(windowStyle
))
1926 wxMacEditHelper
help( m_txn
) ;
1927 TXNSetAttribute( wxTextAttr( foreground
, wxNullColour
, font
), kTXNStartOffset
, kTXNEndOffset
) ;
1930 void wxMacMLTEControl::SetStyle( long start
, long end
, const wxTextAttr
& style
)
1932 wxMacEditHelper
help( m_txn
) ;
1933 TXNSetAttribute( style
, start
, end
) ;
1936 void wxMacMLTEControl::Copy()
1941 void wxMacMLTEControl::Cut()
1946 void wxMacMLTEControl::Paste()
1951 bool wxMacMLTEControl::CanPaste() const
1953 return TXNIsScrapPastable() ;
1956 void wxMacMLTEControl::SetEditable(bool editable
)
1958 TXNControlTag tag
[] = { kTXNIOPrivilegesTag
} ;
1959 TXNControlData data
[] = { { editable
? kTXNReadWrite
: kTXNReadOnly
} } ;
1960 TXNSetTXNObjectControls( m_txn
, false, WXSIZEOF(tag
), tag
, data
) ;
1963 wxTextPos
wxMacMLTEControl::GetLastPosition() const
1965 wxTextPos actualsize
= 0 ;
1968 OSErr err
= TXNGetDataEncoded( m_txn
, kTXNStartOffset
, kTXNEndOffset
, &theText
, kTXNTextData
);
1973 actualsize
= GetHandleSize( theText
) ;
1974 DisposeHandle( theText
) ;
1984 void wxMacMLTEControl::Replace( long from
, long to
, const wxString
&str
)
1986 wxString value
= str
;
1987 wxMacConvertNewlines10To13( &value
) ;
1989 wxMacEditHelper
help( m_txn
) ;
1991 wxMacWindowClipper
c( m_peer
) ;
1994 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
) ;
1996 SetTXNData( value
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
1999 void wxMacMLTEControl::Remove( long from
, long to
)
2002 wxMacWindowClipper
c( m_peer
) ;
2004 wxMacEditHelper
help( m_txn
) ;
2005 TXNSetSelection( m_txn
, from
, to
) ;
2009 void wxMacMLTEControl::GetSelection( long* from
, long* to
) const
2012 TXNGetSelection( m_txn
, &f
, &t
) ;
2017 void wxMacMLTEControl::SetSelection( long from
, long to
)
2020 wxMacWindowClipper
c( m_peer
) ;
2023 // change the selection
2024 if ((from
== -1) && (to
== -1))
2025 TXNSelectAll( m_txn
);
2027 TXNSetSelection( m_txn
, from
, to
== -1 ? kTXNEndOffset
: to
);
2029 TXNShowSelection( m_txn
, kTXNShowStart
);
2032 void wxMacMLTEControl::WriteText( const wxString
& str
)
2035 wxMacConvertNewlines10To13( &st
) ;
2037 long start
, end
, dummy
;
2039 GetSelection( &start
, &dummy
) ;
2041 wxMacWindowClipper
c( m_peer
) ;
2045 wxMacEditHelper
helper( m_txn
) ;
2046 SetTXNData( st
, kTXNUseCurrentSelection
, kTXNUseCurrentSelection
) ;
2049 GetSelection( &dummy
, &end
) ;
2051 // TODO: SetStyle( start , end , GetDefaultStyle() ) ;
2054 void wxMacMLTEControl::Clear()
2057 wxMacWindowClipper
c( m_peer
) ;
2059 wxMacEditHelper
st( m_txn
) ;
2060 TXNSetSelection( m_txn
, kTXNStartOffset
, kTXNEndOffset
) ;
2064 bool wxMacMLTEControl::CanUndo() const
2066 return TXNCanUndo( m_txn
, NULL
) ;
2069 void wxMacMLTEControl::Undo()
2074 bool wxMacMLTEControl::CanRedo() const
2076 return TXNCanRedo( m_txn
, NULL
) ;
2079 void wxMacMLTEControl::Redo()
2084 int wxMacMLTEControl::GetNumberOfLines() const
2086 ItemCount lines
= 0 ;
2087 TXNGetLineCount( m_txn
, &lines
) ;
2092 long wxMacMLTEControl::XYToPosition(long x
, long y
) const
2097 // TODO: find a better implementation : while we can get the
2098 // line metrics of a certain line, we don't get its starting
2099 // position, so it would probably be rather a binary search
2100 // for the start position
2101 long xpos
= 0, ypos
= 0 ;
2102 int lastHeight
= 0 ;
2105 lastpos
= GetLastPosition() ;
2106 for ( n
= 0 ; n
<= (ItemCount
) lastpos
; ++n
)
2108 if ( y
== ypos
&& x
== xpos
)
2111 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2113 if ( curpt
.v
> lastHeight
)
2119 lastHeight
= curpt
.v
;
2128 bool wxMacMLTEControl::PositionToXY( long pos
, long *x
, long *y
) const
2138 lastpos
= GetLastPosition() ;
2139 if ( pos
<= lastpos
)
2141 // TODO: find a better implementation - while we can get the
2142 // line metrics of a certain line, we don't get its starting
2143 // position, so it would probably be rather a binary search
2144 // for the start position
2145 long xpos
= 0, ypos
= 0 ;
2146 int lastHeight
= 0 ;
2149 for ( n
= 0 ; n
<= (ItemCount
) pos
; ++n
)
2151 TXNOffsetToPoint( m_txn
, n
, &curpt
) ;
2153 if ( curpt
.v
> lastHeight
)
2159 lastHeight
= curpt
.v
;
2174 void wxMacMLTEControl::ShowPosition( long pos
)
2176 Point current
, desired
;
2177 TXNOffset selstart
, selend
;
2179 TXNGetSelection( m_txn
, &selstart
, &selend
);
2180 TXNOffsetToPoint( m_txn
, selstart
, ¤t
);
2181 TXNOffsetToPoint( m_txn
, pos
, &desired
);
2183 // TODO: use HIPoints for 10.3 and above
2185 OSErr theErr
= noErr
;
2186 long dv
= desired
.v
- current
.v
;
2187 long dh
= desired
.h
- current
.h
;
2188 TXNShowSelection( m_txn
, kTXNShowStart
) ; // NB: should this be kTXNShowStart or kTXNShowEnd ??
2189 theErr
= TXNScroll( m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
, &dv
, &dh
);
2191 // there will be an error returned for classic MLTE implementation when the control is
2192 // invisible, but HITextView works correctly, so we don't assert that one
2193 // wxASSERT_MSG( theErr == noErr, _T("TXNScroll returned an error!") );
2196 void wxMacMLTEControl::SetTXNData( const wxString
& st
, TXNOffset start
, TXNOffset end
)
2199 #if SIZEOF_WCHAR_T == 2
2200 size_t len
= st
.length() ;
2201 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)st
.wc_str(), len
* 2, start
, end
);
2203 wxMBConvUTF16 converter
;
2204 ByteCount byteBufferLen
= converter
.WC2MB( NULL
, st
.wc_str(), 0 ) ;
2205 UniChar
*unibuf
= (UniChar
*)malloc( byteBufferLen
) ;
2206 converter
.WC2MB( (char*)unibuf
, st
.wc_str(), byteBufferLen
) ;
2207 TXNSetData( m_txn
, kTXNUnicodeTextData
, (void*)unibuf
, byteBufferLen
, start
, end
) ;
2211 wxCharBuffer text
= st
.mb_str( wxConvLocal
) ;
2212 TXNSetData( m_txn
, kTXNTextData
, (void*)text
.data(), strlen( text
), start
, end
) ;
2216 wxString
wxMacMLTEControl::GetLineText(long lineNo
) const
2220 if ( lineNo
< GetNumberOfLines() )
2223 Fixed lineWidth
, lineHeight
, currentHeight
;
2226 // get the first possible position in the control
2227 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2229 // Iterate through the lines until we reach the one we want,
2230 // adding to our current y pixel point position
2233 while (ypos
< lineNo
)
2235 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2236 currentHeight
+= lineHeight
;
2239 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2240 TXNOffset theOffset
;
2241 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2243 wxString content
= GetStringValue() ;
2244 Point currentPoint
= thePoint
;
2245 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2247 line
+= content
[theOffset
];
2248 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2255 int wxMacMLTEControl::GetLineLength(long lineNo
) const
2259 if ( lineNo
< GetNumberOfLines() )
2262 Fixed lineWidth
, lineHeight
, currentHeight
;
2265 // get the first possible position in the control
2266 TXNOffsetToPoint(m_txn
, 0, &firstPoint
);
2268 // Iterate through the lines until we reach the one we want,
2269 // adding to our current y pixel point position
2272 while (ypos
< lineNo
)
2274 TXNGetLineMetrics(m_txn
, ypos
++, &lineWidth
, &lineHeight
);
2275 currentHeight
+= lineHeight
;
2278 Point thePoint
= { firstPoint
.v
+ (currentHeight
>> 16), firstPoint
.h
+ (0) };
2279 TXNOffset theOffset
;
2280 TXNPointToOffset(m_txn
, thePoint
, &theOffset
);
2282 wxString content
= GetStringValue() ;
2283 Point currentPoint
= thePoint
;
2284 while (thePoint
.v
== currentPoint
.v
&& theOffset
< content
.length())
2287 TXNOffsetToPoint(m_txn
, ++theOffset
, ¤tPoint
);
2294 #if MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
2296 // ----------------------------------------------------------------------------
2297 // MLTE control implementation (classic part)
2298 // ----------------------------------------------------------------------------
2300 // OS X Notes : We still don't have a full replacement for MLTE, so this implementation
2301 // has to live on. We have different problems coming from outdated implementations on the
2302 // various OS X versions. Most deal with the scrollbars: they are not correctly embedded
2303 // while this can be solved on 10.3 by reassigning them the correct place, on 10.2 there is
2304 // no way out, therefore we are using our own implementation and our own scrollbars ....
2306 TXNScrollInfoUPP gTXNScrollInfoProc
= NULL
;
2307 ControlActionUPP gTXNScrollActionProc
= NULL
;
2309 pascal void wxMacMLTEClassicControl::TXNScrollInfoProc(
2310 SInt32 iValue
, SInt32 iMaximumValue
,
2311 TXNScrollBarOrientation iScrollBarOrientation
, SInt32 iRefCon
)
2313 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) iRefCon
;
2314 SInt32 value
= wxMax( iValue
, 0 ) ;
2315 SInt32 maximum
= wxMax( iMaximumValue
, 0 ) ;
2317 if ( iScrollBarOrientation
== kTXNHorizontal
)
2319 if ( mlte
->m_sbHorizontal
)
2321 SetControl32BitValue( mlte
->m_sbHorizontal
, value
) ;
2322 SetControl32BitMaximum( mlte
->m_sbHorizontal
, maximum
) ;
2323 mlte
->m_lastHorizontalValue
= value
;
2326 else if ( iScrollBarOrientation
== kTXNVertical
)
2328 if ( mlte
->m_sbVertical
)
2330 SetControl32BitValue( mlte
->m_sbVertical
, value
) ;
2331 SetControl32BitMaximum( mlte
->m_sbVertical
, maximum
) ;
2332 mlte
->m_lastVerticalValue
= value
;
2337 pascal void wxMacMLTEClassicControl::TXNScrollActionProc( ControlRef controlRef
, ControlPartCode partCode
)
2339 wxMacMLTEClassicControl
* mlte
= (wxMacMLTEClassicControl
*) GetControlReference( controlRef
) ;
2343 if ( controlRef
!= mlte
->m_sbVertical
&& controlRef
!= mlte
->m_sbHorizontal
)
2347 bool isHorizontal
= ( controlRef
== mlte
->m_sbHorizontal
) ;
2349 SInt32 minimum
= 0 ;
2350 SInt32 maximum
= GetControl32BitMaximum( controlRef
) ;
2351 SInt32 value
= GetControl32BitValue( controlRef
) ;
2356 case kControlDownButtonPart
:
2360 case kControlUpButtonPart
:
2364 case kControlPageDownPart
:
2365 delta
= GetControlViewSize( controlRef
) ;
2368 case kControlPageUpPart
:
2369 delta
= -GetControlViewSize( controlRef
) ;
2372 case kControlIndicatorPart
:
2373 delta
= value
- (isHorizontal
? mlte
->m_lastHorizontalValue
: mlte
->m_lastVerticalValue
) ;
2382 SInt32 newValue
= value
;
2384 if ( partCode
!= kControlIndicatorPart
)
2386 if ( value
+ delta
< minimum
)
2387 delta
= minimum
- value
;
2388 if ( value
+ delta
> maximum
)
2389 delta
= maximum
- value
;
2391 SetControl32BitValue( controlRef
, value
+ delta
) ;
2392 newValue
= value
+ delta
;
2395 SInt32 verticalDelta
= isHorizontal
? 0 : delta
;
2396 SInt32 horizontalDelta
= isHorizontal
? delta
: 0 ;
2399 mlte
->m_txn
, kTXNScrollUnitsInPixels
, kTXNScrollUnitsInPixels
,
2400 &verticalDelta
, &horizontalDelta
);
2401 verify_noerr( err
);
2404 mlte
->m_lastHorizontalValue
= newValue
;
2406 mlte
->m_lastVerticalValue
= newValue
;
2410 // make correct activations
2411 void wxMacMLTEClassicControl::MacActivatePaneText(bool setActive
)
2413 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference(m_controlRef
);
2415 wxMacWindowClipper
clipper( textctrl
) ;
2416 TXNActivate( m_txn
, m_txnFrameID
, setActive
);
2418 ControlRef controlFocus
= 0 ;
2419 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2420 if ( controlFocus
== m_controlRef
)
2421 TXNFocus( m_txn
, setActive
);
2424 void wxMacMLTEClassicControl::MacFocusPaneText(bool setFocus
)
2426 TXNFocus( m_txn
, setFocus
);
2429 // guards against inappropriate redraw (hidden objects drawing onto window)
2431 void wxMacMLTEClassicControl::MacSetObjectVisibility(bool vis
)
2433 ControlRef controlFocus
= 0 ;
2434 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2436 if ( !vis
&& (controlFocus
== m_controlRef
) )
2437 SetKeyboardFocus( m_txnWindow
, m_controlRef
, kControlFocusNoPart
) ;
2439 TXNControlTag iControlTags
[1] = { kTXNVisibilityTag
};
2440 TXNControlData iControlData
[1] = { { (UInt32
)false } };
2442 verify_noerr( TXNGetTXNObjectControls( m_txn
, 1, iControlTags
, iControlData
) ) ;
2444 if ( iControlData
[0].uValue
!= vis
)
2446 iControlData
[0].uValue
= vis
;
2447 verify_noerr( TXNSetTXNObjectControls( m_txn
, false , 1, iControlTags
, iControlData
) ) ;
2450 // currently, we always clip as partial visibility (overlapped) visibility is also a problem,
2451 // if we run into further problems we might set the FrameBounds to an empty rect here
2454 // make sure that the TXNObject is at the right position
2456 void wxMacMLTEClassicControl::MacUpdatePosition()
2458 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2459 if ( textctrl
== NULL
)
2463 GetRectInWindowCoords( &bounds
);
2465 wxRect visRect
= textctrl
->MacGetClippedClientRect() ;
2466 Rect visBounds
= { visRect
.y
, visRect
.x
, visRect
.y
+ visRect
.height
, visRect
.x
+ visRect
.width
} ;
2469 textctrl
->MacWindowToRootWindow( &x
, &y
) ;
2470 OffsetRect( &visBounds
, x
, y
) ;
2472 if ( !EqualRect( &bounds
, &m_txnControlBounds
) || !EqualRect( &visBounds
, &m_txnVisBounds
) )
2474 m_txnControlBounds
= bounds
;
2475 m_txnVisBounds
= visBounds
;
2476 wxMacWindowClipper
cl( textctrl
) ;
2478 if ( m_sbHorizontal
|| m_sbVertical
)
2480 int w
= bounds
.right
- bounds
.left
;
2481 int h
= bounds
.bottom
- bounds
.top
;
2483 if ( m_sbHorizontal
)
2487 sbBounds
.left
= -1 ;
2488 sbBounds
.top
= h
- 14 ;
2489 sbBounds
.right
= w
+ 1 ;
2490 sbBounds
.bottom
= h
+ 1 ;
2492 SetControlBounds( m_sbHorizontal
, &sbBounds
) ;
2493 SetControlViewSize( m_sbHorizontal
, w
) ;
2500 sbBounds
.left
= w
- 14 ;
2502 sbBounds
.right
= w
+ 1 ;
2503 sbBounds
.bottom
= m_sbHorizontal
? h
- 14 : h
+ 1 ;
2505 SetControlBounds( m_sbVertical
, &sbBounds
) ;
2506 SetControlViewSize( m_sbVertical
, h
) ;
2511 TXNLongRect olddestRect
;
2512 TXNGetRectBounds( m_txn
, &oldviewRect
, &olddestRect
, NULL
) ;
2514 Rect viewRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2515 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2516 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2517 TXNLongRect destRect
= { m_txnControlBounds
.top
, m_txnControlBounds
.left
,
2518 m_txnControlBounds
.bottom
- ( m_sbHorizontal
? 14 : 0 ) ,
2519 m_txnControlBounds
.right
- ( m_sbVertical
? 14 : 0 ) } ;
2521 if ( olddestRect
.right
>= 10000 )
2522 destRect
.right
= destRect
.left
+ 32000 ;
2524 if ( olddestRect
.bottom
>= 0x20000000 )
2525 destRect
.bottom
= destRect
.top
+ 0x40000000 ;
2527 SectRect( &viewRect
, &visBounds
, &viewRect
) ;
2528 TXNSetRectBounds( m_txn
, &viewRect
, &destRect
, true ) ;
2533 m_txnControlBounds
.top
,
2534 m_txnControlBounds
.left
,
2535 m_txnControlBounds
.bottom
- (m_sbHorizontal
? 14 : 0),
2536 m_txnControlBounds
.right
- (m_sbVertical
? 14 : 0),
2540 // the SetFrameBounds method under Classic sometimes does not correctly scroll a selection into sight after a
2541 // movement, therefore we have to force it
2543 // this problem has been reported in OSX as well, so we use this here once again
2545 TXNLongRect textRect
;
2546 TXNGetRectBounds( m_txn
, NULL
, NULL
, &textRect
) ;
2547 if ( textRect
.left
< m_txnControlBounds
.left
)
2548 TXNShowSelection( m_txn
, kTXNShowStart
) ;
2552 void wxMacMLTEClassicControl::SetRect( Rect
*r
)
2554 wxMacControl::SetRect( r
) ;
2555 MacUpdatePosition() ;
2558 void wxMacMLTEClassicControl::MacControlUserPaneDrawProc(wxInt16
WXUNUSED(thePart
))
2560 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2561 if ( textctrl
== NULL
)
2564 if ( textctrl
->MacIsReallyShown() )
2566 wxMacWindowClipper
clipper( textctrl
) ;
2567 TXNDraw( m_txn
, NULL
) ;
2571 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneHitTestProc(wxInt16 x
, wxInt16 y
)
2573 Point where
= { y
, x
} ;
2574 ControlPartCode result
= kControlNoPart
;
2576 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2577 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2579 if (PtInRect( where
, &m_txnControlBounds
))
2581 result
= kControlEditTextPart
;
2585 // sometimes we get the coords also in control local coordinates, therefore test again
2587 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2591 if (PtInRect( where
, &m_txnControlBounds
))
2592 result
= kControlEditTextPart
;
2599 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneTrackingProc( wxInt16 x
, wxInt16 y
, void* WXUNUSED(actionProc
) )
2601 ControlPartCode result
= kControlNoPart
;
2603 wxTextCtrl
* textctrl
= (wxTextCtrl
*) GetControlReference( m_controlRef
);
2604 if ( (textctrl
!= NULL
) && textctrl
->MacIsReallyShown() )
2606 Point startPt
= { y
, x
} ;
2608 // for compositing, we must convert these into toplevel window coordinates, because hittesting expects them
2610 textctrl
->MacClientToRootWindow( &x
, &y
) ;
2614 switch (MacControlUserPaneHitTestProc( startPt
.h
, startPt
.v
))
2616 case kControlEditTextPart
:
2618 wxMacWindowClipper
clipper( textctrl
) ;
2621 ConvertEventRefToEventRecord( (EventRef
) wxTheApp
->MacGetCurrentEvent() , &rec
) ;
2622 TXNClick( m_txn
, &rec
);
2634 void wxMacMLTEClassicControl::MacControlUserPaneIdleProc()
2636 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2637 if ( textctrl
== NULL
)
2640 if (textctrl
->MacIsReallyShown())
2642 if (IsControlActive(m_controlRef
))
2646 wxMacWindowClipper
clipper( textctrl
) ;
2651 if (PtInRect(mousep
, &m_txnControlBounds
))
2653 RgnHandle theRgn
= NewRgn();
2654 RectRgn(theRgn
, &m_txnControlBounds
);
2655 TXNAdjustCursor(m_txn
, theRgn
);
2662 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneKeyDownProc (wxInt16 keyCode
, wxInt16 charCode
, wxInt16 modifiers
)
2664 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2665 if ( textctrl
== NULL
)
2666 return kControlNoPart
;
2668 wxMacWindowClipper
clipper( textctrl
) ;
2671 memset( &ev
, 0 , sizeof( ev
) ) ;
2673 ev
.modifiers
= modifiers
;
2674 ev
.message
= ((keyCode
<< 8) & keyCodeMask
) | (charCode
& charCodeMask
);
2675 TXNKeyDown( m_txn
, &ev
);
2677 return kControlEntireControl
;
2680 void wxMacMLTEClassicControl::MacControlUserPaneActivateProc(bool activating
)
2682 MacActivatePaneText( activating
);
2685 wxInt16
wxMacMLTEClassicControl::MacControlUserPaneFocusProc(wxInt16 action
)
2687 ControlPartCode focusResult
= kControlFocusNoPart
;
2689 wxTextCtrl
* textctrl
= (wxTextCtrl
*)GetControlReference( m_controlRef
);
2690 if ( textctrl
== NULL
)
2693 wxMacWindowClipper
clipper( textctrl
) ;
2695 ControlRef controlFocus
= NULL
;
2696 GetKeyboardFocus( m_txnWindow
, &controlFocus
) ;
2697 bool wasFocused
= ( controlFocus
== m_controlRef
) ;
2701 case kControlFocusPrevPart
:
2702 case kControlFocusNextPart
:
2703 MacFocusPaneText( !wasFocused
);
2704 focusResult
= (!wasFocused
? (ControlPartCode
) kControlEditTextPart
: (ControlPartCode
) kControlFocusNoPart
);
2707 case kControlFocusNoPart
:
2709 MacFocusPaneText( false );
2710 focusResult
= kControlFocusNoPart
;
2717 void wxMacMLTEClassicControl::MacControlUserPaneBackgroundProc( void *WXUNUSED(info
) )
2721 wxMacMLTEClassicControl::wxMacMLTEClassicControl( wxTextCtrl
*wxPeer
,
2722 const wxString
& str
,
2724 const wxSize
& size
, long style
)
2725 : wxMacMLTEControl( wxPeer
)
2727 m_font
= wxPeer
->GetFont() ;
2728 m_windowStyle
= style
;
2729 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
2732 kControlSupportsEmbedding
| kControlSupportsFocus
| kControlWantsIdle
2733 | kControlWantsActivate
| kControlHandlesTracking
2734 // | kControlHasSpecialBackground
2735 | kControlGetsFocusOnClick
| kControlSupportsLiveFeedback
;
2737 OSStatus err
= ::CreateUserPaneControl(
2738 MAC_WXHWND(wxPeer
->GetParent()->MacGetTopLevelWindowRef()),
2739 &bounds
, featureSet
, &m_controlRef
);
2740 verify_noerr( err
);
2744 AdjustCreationAttributes( *wxWHITE
, true ) ;
2746 MacSetObjectVisibility( wxPeer
->MacIsReallyShown() ) ;
2750 wxMacConvertNewlines10To13( &st
) ;
2751 wxMacWindowClipper
clipper( m_peer
) ;
2752 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
2753 TXNSetSelection( m_txn
, 0, 0 ) ;
2757 wxMacMLTEClassicControl::~wxMacMLTEClassicControl()
2759 TXNDeleteObject( m_txn
);
2763 void wxMacMLTEClassicControl::VisibilityChanged(bool shown
)
2765 MacSetObjectVisibility( shown
) ;
2766 wxMacControl::VisibilityChanged( shown
) ;
2769 void wxMacMLTEClassicControl::SuperChangedPosition()
2771 MacUpdatePosition() ;
2772 wxMacControl::SuperChangedPosition() ;
2775 ControlUserPaneDrawUPP gTPDrawProc
= NULL
;
2776 ControlUserPaneHitTestUPP gTPHitProc
= NULL
;
2777 ControlUserPaneTrackingUPP gTPTrackProc
= NULL
;
2778 ControlUserPaneIdleUPP gTPIdleProc
= NULL
;
2779 ControlUserPaneKeyDownUPP gTPKeyProc
= NULL
;
2780 ControlUserPaneActivateUPP gTPActivateProc
= NULL
;
2781 ControlUserPaneFocusUPP gTPFocusProc
= NULL
;
2783 static pascal void wxMacControlUserPaneDrawProc(ControlRef control
, SInt16 part
)
2785 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2786 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2788 win
->MacControlUserPaneDrawProc( part
) ;
2791 static pascal ControlPartCode
wxMacControlUserPaneHitTestProc(ControlRef control
, Point where
)
2793 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2794 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2796 return win
->MacControlUserPaneHitTestProc( where
.h
, where
.v
) ;
2798 return kControlNoPart
;
2801 static pascal ControlPartCode
wxMacControlUserPaneTrackingProc(ControlRef control
, Point startPt
, ControlActionUPP actionProc
)
2803 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2804 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2806 return win
->MacControlUserPaneTrackingProc( startPt
.h
, startPt
.v
, (void*) actionProc
) ;
2808 return kControlNoPart
;
2811 static pascal void wxMacControlUserPaneIdleProc(ControlRef control
)
2813 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2814 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2816 win
->MacControlUserPaneIdleProc() ;
2819 static pascal ControlPartCode
wxMacControlUserPaneKeyDownProc(ControlRef control
, SInt16 keyCode
, SInt16 charCode
, SInt16 modifiers
)
2821 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2822 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2824 return win
->MacControlUserPaneKeyDownProc( keyCode
, charCode
, modifiers
) ;
2826 return kControlNoPart
;
2829 static pascal void wxMacControlUserPaneActivateProc(ControlRef control
, Boolean activating
)
2831 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2832 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2834 win
->MacControlUserPaneActivateProc( activating
) ;
2837 static pascal ControlPartCode
wxMacControlUserPaneFocusProc(ControlRef control
, ControlFocusPart action
)
2839 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2840 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2842 return win
->MacControlUserPaneFocusProc( action
) ;
2844 return kControlNoPart
;
2848 static pascal void wxMacControlUserPaneBackgroundProc(ControlRef control
, ControlBackgroundPtr info
)
2850 wxTextCtrl
*textCtrl
= wxDynamicCast( wxFindControlFromMacControl(control
) , wxTextCtrl
) ;
2851 wxMacMLTEClassicControl
* win
= textCtrl
? (wxMacMLTEClassicControl
*)(textCtrl
->GetPeer()) : NULL
;
2853 win
->MacControlUserPaneBackgroundProc(info
) ;
2857 // TXNRegisterScrollInfoProc
2859 OSStatus
wxMacMLTEClassicControl::DoCreate()
2862 OSStatus err
= noErr
;
2864 // set up our globals
2865 if (gTPDrawProc
== NULL
) gTPDrawProc
= NewControlUserPaneDrawUPP(wxMacControlUserPaneDrawProc
);
2866 if (gTPHitProc
== NULL
) gTPHitProc
= NewControlUserPaneHitTestUPP(wxMacControlUserPaneHitTestProc
);
2867 if (gTPTrackProc
== NULL
) gTPTrackProc
= NewControlUserPaneTrackingUPP(wxMacControlUserPaneTrackingProc
);
2868 if (gTPIdleProc
== NULL
) gTPIdleProc
= NewControlUserPaneIdleUPP(wxMacControlUserPaneIdleProc
);
2869 if (gTPKeyProc
== NULL
) gTPKeyProc
= NewControlUserPaneKeyDownUPP(wxMacControlUserPaneKeyDownProc
);
2870 if (gTPActivateProc
== NULL
) gTPActivateProc
= NewControlUserPaneActivateUPP(wxMacControlUserPaneActivateProc
);
2871 if (gTPFocusProc
== NULL
) gTPFocusProc
= NewControlUserPaneFocusUPP(wxMacControlUserPaneFocusProc
);
2873 if (gTXNScrollInfoProc
== NULL
) gTXNScrollInfoProc
= NewTXNScrollInfoUPP(TXNScrollInfoProc
) ;
2874 if (gTXNScrollActionProc
== NULL
) gTXNScrollActionProc
= NewControlActionUPP(TXNScrollActionProc
) ;
2876 // set the initial settings for our private data
2878 m_txnWindow
= GetControlOwner(m_controlRef
);
2879 m_txnPort
= (GrafPtr
) GetWindowPort(m_txnWindow
);
2881 // set up the user pane procedures
2882 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneDrawProcTag
, sizeof(gTPDrawProc
), &gTPDrawProc
);
2883 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneHitTestProcTag
, sizeof(gTPHitProc
), &gTPHitProc
);
2884 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneTrackingProcTag
, sizeof(gTPTrackProc
), &gTPTrackProc
);
2885 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneIdleProcTag
, sizeof(gTPIdleProc
), &gTPIdleProc
);
2886 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneKeyDownProcTag
, sizeof(gTPKeyProc
), &gTPKeyProc
);
2887 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneActivateProcTag
, sizeof(gTPActivateProc
), &gTPActivateProc
);
2888 SetControlData(m_controlRef
, kControlEntireControl
, kControlUserPaneFocusProcTag
, sizeof(gTPFocusProc
), &gTPFocusProc
);
2890 // calculate the rectangles used by the control
2891 GetRectInWindowCoords( &bounds
);
2893 m_txnControlBounds
= bounds
;
2894 m_txnVisBounds
= bounds
;
2899 GetGWorld( &origPort
, &origDev
) ;
2900 SetPort( m_txnPort
);
2902 // create the new edit field
2903 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( m_windowStyle
);
2905 // the scrollbars are not correctly embedded but are inserted at the root:
2906 // this gives us problems as we have erratic redraws even over the structure area
2908 m_sbHorizontal
= 0 ;
2910 m_lastHorizontalValue
= 0 ;
2911 m_lastVerticalValue
= 0 ;
2913 Rect sb
= { 0 , 0 , 0 , 0 } ;
2914 if ( frameOptions
& kTXNWantVScrollBarMask
)
2916 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbVertical
);
2917 SetControlReference( m_sbVertical
, (SInt32
)this );
2918 SetControlAction( m_sbVertical
, gTXNScrollActionProc
);
2919 ShowControl( m_sbVertical
);
2920 EmbedControl( m_sbVertical
, m_controlRef
);
2921 frameOptions
&= ~kTXNWantVScrollBarMask
;
2924 if ( frameOptions
& kTXNWantHScrollBarMask
)
2926 CreateScrollBarControl( m_txnWindow
, &sb
, 0, 0, 100, 1, true, gTXNScrollActionProc
, &m_sbHorizontal
);
2927 SetControlReference( m_sbHorizontal
, (SInt32
)this );
2928 SetControlAction( m_sbHorizontal
, gTXNScrollActionProc
);
2929 ShowControl( m_sbHorizontal
);
2930 EmbedControl( m_sbHorizontal
, m_controlRef
);
2931 frameOptions
&= ~(kTXNWantHScrollBarMask
| kTXNDrawGrowIconMask
);
2935 NULL
, m_txnWindow
, &bounds
, frameOptions
,
2936 kTXNTextEditStyleFrameType
, kTXNTextensionFile
, kTXNSystemDefaultEncoding
,
2937 &m_txn
, &m_txnFrameID
, NULL
);
2938 verify_noerr( err
);
2941 TXNControlTag iControlTags
[] = { kTXNUseCarbonEvents
};
2942 TXNControlData iControlData
[] = { { (UInt32
)&cInfo
} };
2943 int toptag
= WXSIZEOF( iControlTags
) ;
2944 TXNCarbonEventInfo cInfo
;
2945 cInfo
.useCarbonEvents
= false ;
2948 cInfo
.fDictionary
= NULL
;
2950 verify_noerr( TXNSetTXNObjectControls( m_txn
, false, toptag
, iControlTags
, iControlData
) );
2953 TXNRegisterScrollInfoProc( m_txn
, gTXNScrollInfoProc
, (SInt32
)this );
2955 SetGWorld( origPort
, origDev
) ;
2961 // ----------------------------------------------------------------------------
2962 // MLTE control implementation (OSX part)
2963 // ----------------------------------------------------------------------------
2965 // tiger multi-line textcontrols with no CR in the entire content
2966 // don't scroll automatically, so we need a hack.
2967 // This attempt only works 'before' the key (ie before CallNextEventHandler)
2968 // is processed, thus the scrolling always occurs one character too late, but
2969 // better than nothing ...
2971 static const EventTypeSpec eventList
[] =
2973 { kEventClassTextInput
, kEventTextInputUnicodeForKeyEvent
} ,
2976 static pascal OSStatus
wxMacUnicodeTextEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
2978 OSStatus result
= eventNotHandledErr
;
2979 wxMacMLTEHIViewControl
* focus
= (wxMacMLTEHIViewControl
*) data
;
2981 switch ( GetEventKind( event
) )
2983 case kEventTextInputUnicodeForKeyEvent
:
2985 TXNOffset from
, to
;
2986 TXNGetSelection( focus
->GetTXNObject() , &from
, &to
) ;
2988 TXNShowSelection( focus
->GetTXNObject() , kTXNShowStart
);
2989 result
= CallNextEventHandler(handler
,event
);
2999 static pascal OSStatus
wxMacTextControlEventHandler( EventHandlerCallRef handler
, EventRef event
, void *data
)
3001 OSStatus result
= eventNotHandledErr
;
3003 switch ( GetEventClass( event
) )
3005 case kEventClassTextInput
:
3006 result
= wxMacUnicodeTextEventHandler( handler
, event
, data
) ;
3015 DEFINE_ONE_SHOT_HANDLER_GETTER( wxMacTextControlEventHandler
)
3017 wxMacMLTEHIViewControl::wxMacMLTEHIViewControl( wxTextCtrl
*wxPeer
,
3018 const wxString
& str
,
3020 const wxSize
& size
, long style
) : wxMacMLTEControl( wxPeer
)
3022 m_font
= wxPeer
->GetFont() ;
3023 m_windowStyle
= style
;
3024 Rect bounds
= wxMacGetBoundsForControl( wxPeer
, pos
, size
) ;
3026 wxMacConvertNewlines10To13( &st
) ;
3029 { bounds
.left
, bounds
.top
},
3030 { bounds
.right
- bounds
.left
, bounds
.bottom
- bounds
.top
} } ;
3032 m_scrollView
= NULL
;
3033 TXNFrameOptions frameOptions
= FrameOptionsFromWXStyle( style
) ;
3034 if (( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
)) || !(frameOptions
&kTXNSingleLineOnlyMask
))
3036 if ( frameOptions
& (kTXNWantVScrollBarMask
| kTXNWantHScrollBarMask
) )
3039 (frameOptions
& kTXNWantHScrollBarMask
? kHIScrollViewOptionsHorizScroll
: 0)
3040 | (frameOptions
& kTXNWantVScrollBarMask
? kHIScrollViewOptionsVertScroll
: 0) ,
3045 HIScrollViewCreate(kHIScrollViewOptionsVertScroll
,&m_scrollView
);
3046 HIScrollViewSetScrollBarAutoHide(m_scrollView
,true);
3049 HIViewSetFrame( m_scrollView
, &hr
);
3050 HIViewSetVisible( m_scrollView
, true );
3054 HITextViewCreate( NULL
, 0, frameOptions
, &m_textView
) ;
3055 m_txn
= HITextViewGetTXNObject( m_textView
) ;
3056 HIViewSetVisible( m_textView
, true ) ;
3059 HIViewAddSubview( m_scrollView
, m_textView
) ;
3060 m_controlRef
= m_scrollView
;
3061 wxPeer
->MacInstallEventHandler( (WXWidget
) m_textView
) ;
3065 HIViewSetFrame( m_textView
, &hr
);
3066 m_controlRef
= m_textView
;
3069 AdjustCreationAttributes( *wxWHITE
, true ) ;
3071 wxMacWindowClipper
c( m_peer
) ;
3073 SetTXNData( st
, kTXNStartOffset
, kTXNEndOffset
) ;
3075 TXNSetSelection( m_txn
, 0, 0 );
3076 TXNShowSelection( m_txn
, kTXNShowStart
);
3078 InstallControlEventHandler( m_textView
, GetwxMacTextControlEventHandlerUPP(),
3079 GetEventTypeCount(eventList
), eventList
, this,
3083 wxMacMLTEHIViewControl::~wxMacMLTEHIViewControl()
3087 OSStatus
wxMacMLTEHIViewControl::SetFocus( ControlFocusPart focusPart
)
3089 return SetKeyboardFocus( GetControlOwner( m_textView
), m_textView
, focusPart
) ;
3092 bool wxMacMLTEHIViewControl::HasFocus() const
3094 ControlRef control
;
3095 if ( GetUserFocusWindow() == NULL
)
3098 GetKeyboardFocus( GetUserFocusWindow() , &control
) ;
3099 return control
== m_textView
;
3102 void wxMacMLTEHIViewControl::SetBackgroundColour(const wxColour
& col
)
3104 HITextViewSetBackgroundColor( m_textView
, col
.GetPixel() );
3107 #endif // wxUSE_TEXTCTRL