1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/textctrl_osx.cpp
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
7 // RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $
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"
47 #include "wx/thread.h"
49 #include "wx/osx/private.h"
50 #include "wx/osx/carbon/private/mactext.h"
52 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
54 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
55 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
56 EVT_CHAR(wxTextCtrl::OnChar
)
57 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
58 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
59 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
60 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
61 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
62 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
63 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
65 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
67 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
68 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
69 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
70 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
71 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
72 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
73 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
77 void wxTextCtrl::Init()
83 m_privateContextMenu
= NULL
;
84 m_triggerOnSetValue
= true ;
87 wxTextCtrl::~wxTextCtrl()
90 delete m_privateContextMenu
;
94 bool wxTextCtrl::Create( wxWindow
*parent
,
100 const wxValidator
& validator
,
101 const wxString
& name
)
103 m_macIsUserPane
= false ;
106 if ( ! (style
& wxNO_BORDER
) )
107 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
109 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
112 if ( m_windowStyle
& wxTE_MULTILINE
)
114 // always turn on this style for multi-line controls
115 m_windowStyle
|= wxTE_PROCESS_ENTER
;
116 style
|= wxTE_PROCESS_ENTER
;
119 m_peer
= wxWidgetImpl::CreateTextControl( this, parent
, id
, str
, pos
, size
, style
, GetExtraStyle() );
121 // CreatePeer( str, pos, size, style );
123 MacPostControlCreate(pos
, size
) ;
125 // only now the embedding is correct and we can do a positioning update
127 MacSuperChangedPosition() ;
129 if ( m_windowStyle
& wxTE_READONLY
)
130 SetEditable( false ) ;
132 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
137 void wxTextCtrl::CreatePeer(
140 const wxSize
& size
, long style
)
144 void wxTextCtrl::MacSuperChangedPosition()
146 wxWindow::MacSuperChangedPosition() ;
148 GetPeer()->SuperChangedPosition() ;
152 void wxTextCtrl::MacVisibilityChanged()
155 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
159 void wxTextCtrl::MacCheckSpelling(bool check
)
161 GetPeer()->CheckSpelling(check
);
164 wxString
wxTextCtrl::GetValue() const
166 return GetPeer()->GetStringValue() ;
169 void wxTextCtrl::GetSelection(long* from
, long* to
) const
171 GetPeer()->GetSelection( from
, to
) ;
174 void wxTextCtrl::DoSetValue(const wxString
& str
, int flags
)
177 if ( GetValue() == str
)
180 GetPeer()->SetStringValue( str
) ;
182 if ( (flags
& SetValue_SendEvent
) && m_triggerOnSetValue
)
184 SendTextUpdatedEvent();
188 void wxTextCtrl::SetMaxLength(unsigned long len
)
193 bool wxTextCtrl::SetFont( const wxFont
& font
)
195 if ( !wxTextCtrlBase::SetFont( font
) )
198 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle() ) ;
203 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
205 GetPeer()->SetStyle( start
, end
, style
) ;
210 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
212 wxTextCtrlBase::SetDefaultStyle( style
) ;
213 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
218 // Clipboard operations
220 void wxTextCtrl::Copy()
226 void wxTextCtrl::Cut()
232 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
233 event
.SetEventObject( this );
234 HandleWindowEvent( event
);
238 void wxTextCtrl::Paste()
244 // TODO: eventually we should add setting the default style again
246 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
247 event
.SetEventObject( this );
248 HandleWindowEvent( event
);
252 bool wxTextCtrl::CanCopy() const
254 // Can copy if there's a selection
256 GetSelection( &from
, &to
);
261 bool wxTextCtrl::CanCut() const
266 // Can cut if there's a selection
268 GetSelection( &from
, &to
);
273 bool wxTextCtrl::CanPaste() const
278 return GetPeer()->CanPaste() ;
281 void wxTextCtrl::SetEditable(bool editable
)
283 if ( editable
!= m_editable
)
285 m_editable
= editable
;
286 GetPeer()->SetEditable( editable
) ;
290 void wxTextCtrl::SetInsertionPoint(long pos
)
292 SetSelection( pos
, pos
) ;
295 void wxTextCtrl::SetInsertionPointEnd()
297 wxTextPos pos
= GetLastPosition();
298 SetInsertionPoint( pos
);
301 long wxTextCtrl::GetInsertionPoint() const
304 GetSelection( &begin
, &end
) ;
309 wxTextPos
wxTextCtrl::GetLastPosition() const
311 return GetPeer()->GetLastPosition() ;
314 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
316 GetPeer()->Replace( from
, to
, str
) ;
319 void wxTextCtrl::Remove(long from
, long to
)
321 GetPeer()->Remove( from
, to
) ;
324 void wxTextCtrl::SetSelection(long from
, long to
)
326 GetPeer()->SetSelection( from
, to
) ;
329 void wxTextCtrl::WriteText(const wxString
& str
)
331 GetPeer()->WriteText( str
) ;
334 void wxTextCtrl::AppendText(const wxString
& text
)
336 SetInsertionPointEnd();
340 void wxTextCtrl::Clear()
345 bool wxTextCtrl::IsModified() const
350 bool wxTextCtrl::IsEditable() const
352 return IsEnabled() && m_editable
;
355 bool wxTextCtrl::AcceptsFocus() const
357 // we don't want focus if we can't be edited
358 return /*IsEditable() && */ wxControl::AcceptsFocus();
361 wxSize
wxTextCtrl::DoGetBestSize() const
365 // these are the numbers from the HIG:
366 // we reduce them by the borders first
369 switch ( m_windowVariant
)
371 case wxWINDOW_VARIANT_NORMAL
:
375 case wxWINDOW_VARIANT_SMALL
:
379 case wxWINDOW_VARIANT_MINI
:
388 // as the above numbers have some free space around the text
389 // we get 5 lines like this anyway
390 if ( m_windowStyle
& wxTE_MULTILINE
)
393 if ( !HasFlag(wxNO_BORDER
) )
396 return wxSize(wText
, hText
);
399 // ----------------------------------------------------------------------------
401 // ----------------------------------------------------------------------------
403 void wxTextCtrl::Undo()
409 void wxTextCtrl::Redo()
415 bool wxTextCtrl::CanUndo() const
420 return GetPeer()->CanUndo() ;
423 bool wxTextCtrl::CanRedo() const
428 return GetPeer()->CanRedo() ;
431 void wxTextCtrl::MarkDirty()
436 void wxTextCtrl::DiscardEdits()
441 int wxTextCtrl::GetNumberOfLines() const
443 return GetPeer()->GetNumberOfLines() ;
446 long wxTextCtrl::XYToPosition(long x
, long y
) const
448 return GetPeer()->XYToPosition( x
, y
) ;
451 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
453 return GetPeer()->PositionToXY( pos
, x
, y
) ;
456 void wxTextCtrl::ShowPosition(long pos
)
458 return GetPeer()->ShowPosition(pos
) ;
461 int wxTextCtrl::GetLineLength(long lineNo
) const
463 return GetPeer()->GetLineLength(lineNo
) ;
466 wxString
wxTextCtrl::GetLineText(long lineNo
) const
468 return GetPeer()->GetLineText(lineNo
) ;
471 void wxTextCtrl::Command(wxCommandEvent
& event
)
473 SetValue(event
.GetString());
474 ProcessCommand(event
);
477 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
479 // By default, load the first file into the text window.
480 if (event
.GetNumberOfFiles() > 0)
481 LoadFile( event
.GetFiles()[0] );
484 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
486 int key
= event
.GetKeyCode() ;
487 bool eat_key
= false ;
490 if ( key
== 'a' && event
.MetaDown() )
497 if ( key
== 'c' && event
.MetaDown() )
505 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
506 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
507 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
514 // Check if we have reached the max # of chars (if it is set), but still
515 // allow navigation and deletion
516 GetSelection( &from
, &to
);
517 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
518 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&& key
!= WXK_UP
&& key
!= WXK_DOWN
&&
519 key
!= WXK_BACK
&& key
!= WXK_DELETE
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
522 // eat it, we don't want to add more than allowed # of characters
524 // TODO: generate EVT_TEXT_MAXLEN()
528 // assume that any key not processed yet is going to modify the control
531 if ( key
== 'v' && event
.MetaDown() )
539 if ( key
== 'x' && event
.MetaDown() )
550 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
552 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
553 event
.SetEventObject( this );
554 event
.SetString( GetValue() );
555 if ( HandleWindowEvent(event
) )
559 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
561 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
562 if ( tlw
&& tlw
->GetDefaultItem() )
564 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
565 if ( def
&& def
->IsEnabled() )
567 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
568 event
.SetEventObject(def
);
575 // this will make wxWidgets eat the ENTER key so that
576 // we actually prevent line wrapping in a single line text control
582 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
585 if (!event
.ShiftDown())
586 flags
|= wxNavigationKeyEvent::IsForward
;
587 if (event
.ControlDown())
588 flags
|= wxNavigationKeyEvent::WinChange
;
595 // This is necessary (don't know why);
596 // otherwise the tab will not be inserted.
597 WriteText(wxT("\t"));
608 // perform keystroke handling
612 if ( ( key
>= 0x20 && key
< WXK_START
) ||
613 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
618 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
619 event1
.SetEventObject( this );
620 wxPostEvent( GetEventHandler(), event1
);
624 // ----------------------------------------------------------------------------
625 // standard handlers for standard edit menu events
626 // ----------------------------------------------------------------------------
628 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
633 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
638 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
643 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
648 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
653 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
657 GetSelection( &from
, &to
);
658 if (from
!= -1 && to
!= -1)
662 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
664 SetSelection(-1, -1);
667 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
669 event
.Enable( CanCut() );
672 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
674 event
.Enable( CanCopy() );
677 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
679 event
.Enable( CanPaste() );
682 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
684 event
.Enable( CanUndo() );
687 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
689 event
.Enable( CanRedo() );
692 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
696 GetSelection( &from
, &to
);
697 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
700 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
702 event
.Enable(GetLastPosition() > 0);
705 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
707 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
709 if ( GetPeer()->HasOwnContextMenu() )
716 if (m_privateContextMenu
== NULL
)
718 m_privateContextMenu
= new wxMenu
;
719 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
720 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
721 m_privateContextMenu
->AppendSeparator();
722 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
723 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
724 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
725 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
726 m_privateContextMenu
->AppendSeparator();
727 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
730 if (m_privateContextMenu
!= NULL
)
731 PopupMenu(m_privateContextMenu
);
735 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
737 if ( !GetPeer()->SetupCursor( pt
) )
738 return wxWindow::MacSetupCursor( pt
) ;
743 // ----------------------------------------------------------------------------
744 // implementation base class
745 // ----------------------------------------------------------------------------
748 wxMacTextControl::wxMacTextControl(wxTextCtrl
* peer
) :
751 wxMacTextControl::wxMacTextControl(wxTextCtrl
* peer
, WXWidget w
) :
752 wxWidgetCocoaImpl( peer
, w
)
757 wxMacTextControl::~wxMacTextControl()
761 void wxMacTextControl::SetStyle(long WXUNUSED(start
),
763 const wxTextAttr
& WXUNUSED(style
))
767 void wxMacTextControl::Copy()
771 void wxMacTextControl::Cut()
775 void wxMacTextControl::Paste()
779 bool wxMacTextControl::CanPaste() const
784 void wxMacTextControl::SetEditable(bool WXUNUSED(editable
))
788 wxTextPos
wxMacTextControl::GetLastPosition() const
790 return GetStringValue().length() ;
793 void wxMacTextControl::Replace( long from
, long to
, const wxString
&val
)
795 SetSelection( from
, to
) ;
799 void wxMacTextControl::Remove( long from
, long to
)
801 SetSelection( from
, to
) ;
802 WriteText( wxEmptyString
) ;
805 void wxMacTextControl::Clear()
807 SetStringValue( wxEmptyString
) ;
810 bool wxMacTextControl::CanUndo() const
815 void wxMacTextControl::Undo()
819 bool wxMacTextControl::CanRedo() const
824 void wxMacTextControl::Redo()
828 long wxMacTextControl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
833 bool wxMacTextControl::PositionToXY(long WXUNUSED(pos
),
835 long *WXUNUSED(y
)) const
840 void wxMacTextControl::ShowPosition( long WXUNUSED(pos
) )
844 int wxMacTextControl::GetNumberOfLines() const
846 ItemCount lines
= 0 ;
847 wxString content
= GetStringValue() ;
850 for (size_t i
= 0; i
< content
.length() ; i
++)
852 if (content
[i
] == '\r')
859 wxString
wxMacTextControl::GetLineText(long lineNo
) const
861 // TODO: change this if possible to reflect real lines
862 wxString content
= GetStringValue() ;
866 for (size_t i
= 0; i
< content
.length() ; i
++)
870 // Add chars in line then
873 for (size_t j
= i
; j
< content
.length(); j
++)
875 if (content
[j
] == '\n')
884 if (content
[i
] == '\n')
888 return wxEmptyString
;
891 int wxMacTextControl::GetLineLength(long lineNo
) const
893 // TODO: change this if possible to reflect real lines
894 wxString content
= GetStringValue() ;
898 for (size_t i
= 0; i
< content
.length() ; i
++)
902 // Count chars in line then
904 for (size_t j
= i
; j
< content
.length(); j
++)
907 if (content
[j
] == '\n')
914 if (content
[i
] == '\n')
921 void wxMacTextControl::SetFont( const wxFont
& font
, const wxColour
& foreground
, long windowStyle
)
924 wxMacControl::SetFont(font
, foreground
, windowStyle
);
926 // overrule the barrier in wxMacControl for supporting disabled controls, in order to support
927 // setting the color to eg red and back to black by controllers
929 if ( foreground
== *wxBLACK
)
931 ControlFontStyleRec fontStyle
;
932 fontStyle
.foreColor
.red
= fontStyle
.foreColor
.green
= fontStyle
.foreColor
.blue
= 0;
933 fontStyle
.flags
= kControlUseForeColorMask
;
934 ::SetControlFontStyle( m_controlRef
, &fontStyle
);
939 #endif // wxUSE_TEXTCTRL