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"
51 IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl
, wxTextCtrlBase
)
53 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
54 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
55 EVT_CHAR(wxTextCtrl::OnChar
)
56 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
57 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
58 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
59 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
60 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
61 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
62 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
64 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
66 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
67 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
68 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
69 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
70 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
71 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
72 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
76 void wxTextCtrl::Init()
82 m_privateContextMenu
= NULL
;
83 m_triggerUpdateEvents
= true ;
86 wxTextCtrl::~wxTextCtrl()
89 delete m_privateContextMenu
;
93 bool wxTextCtrl::Create( wxWindow
*parent
,
99 const wxValidator
& validator
,
100 const wxString
& name
)
102 m_macIsUserPane
= false ;
105 if ( ! (style
& wxNO_BORDER
) )
106 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
108 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
111 if ( m_windowStyle
& wxTE_MULTILINE
)
113 // always turn on this style for multi-line controls
114 m_windowStyle
|= wxTE_PROCESS_ENTER
;
115 style
|= wxTE_PROCESS_ENTER
;
119 m_peer
= wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str
, pos
, size
, style
, GetExtraStyle() );
121 MacPostControlCreate(pos
, size
) ;
124 // under carbon everything can already be set before the MacPostControlCreate embedding takes place
125 // but under cocoa for single line textfields this only works after everything has been set up
126 GetTextPeer()->SetStringValue(str
);
129 // only now the embedding is correct and we can do a positioning update
131 MacSuperChangedPosition() ;
133 if ( m_windowStyle
& wxTE_READONLY
)
134 SetEditable( false ) ;
136 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
141 wxTextWidgetImpl
* wxTextCtrl::GetTextPeer() const
143 return dynamic_cast<wxTextWidgetImpl
*> (m_peer
);
146 void wxTextCtrl::MacSuperChangedPosition()
148 wxWindow::MacSuperChangedPosition() ;
150 GetPeer()->SuperChangedPosition() ;
154 void wxTextCtrl::MacVisibilityChanged()
157 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
161 void wxTextCtrl::MacCheckSpelling(bool check
)
163 GetTextPeer()->CheckSpelling(check
);
166 wxString
wxTextCtrl::DoGetValue() const
168 return GetTextPeer()->GetStringValue() ;
171 void wxTextCtrl::GetSelection(long* from
, long* to
) const
173 GetTextPeer()->GetSelection( from
, to
) ;
176 void wxTextCtrl::SetMaxLength(unsigned long len
)
181 bool wxTextCtrl::SetFont( const wxFont
& font
)
183 if ( !wxTextCtrlBase::SetFont( font
) )
186 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
191 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
193 GetTextPeer()->SetStyle( start
, end
, style
) ;
198 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
200 wxTextCtrlBase::SetDefaultStyle( style
) ;
201 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
206 // Clipboard operations
208 void wxTextCtrl::Copy()
211 GetTextPeer()->Copy() ;
214 void wxTextCtrl::Cut()
218 GetTextPeer()->Cut() ;
220 SendTextUpdatedEvent();
224 void wxTextCtrl::Paste()
228 GetTextPeer()->Paste() ;
230 // TODO: eventually we should add setting the default style again
231 SendTextUpdatedEvent();
235 bool wxTextCtrl::CanCopy() const
237 // Can copy if there's a selection
239 GetSelection( &from
, &to
);
244 bool wxTextCtrl::CanCut() const
249 // Can cut if there's a selection
251 GetSelection( &from
, &to
);
256 bool wxTextCtrl::CanPaste() const
261 return GetTextPeer()->CanPaste() ;
264 void wxTextCtrl::SetEditable(bool editable
)
266 if ( editable
!= m_editable
)
268 m_editable
= editable
;
269 GetTextPeer()->SetEditable( editable
) ;
273 void wxTextCtrl::SetInsertionPoint(long pos
)
275 SetSelection( pos
, pos
) ;
278 void wxTextCtrl::SetInsertionPointEnd()
280 long pos
= GetLastPosition();
281 SetInsertionPoint( pos
);
284 long wxTextCtrl::GetInsertionPoint() const
287 GetSelection( &begin
, &end
) ;
292 wxTextPos
wxTextCtrl::GetLastPosition() const
294 return GetTextPeer()->GetLastPosition() ;
297 void wxTextCtrl::Remove(long from
, long to
)
299 GetTextPeer()->Remove( from
, to
) ;
300 if ( m_triggerUpdateEvents
)
301 SendTextUpdatedEvent();
304 void wxTextCtrl::SetSelection(long from
, long to
)
306 GetTextPeer()->SetSelection( from
, to
) ;
309 void wxTextCtrl::WriteText(const wxString
& str
)
311 GetTextPeer()->WriteText( str
) ;
312 if ( m_triggerUpdateEvents
)
313 SendTextUpdatedEvent();
316 void wxTextCtrl::Clear()
318 GetTextPeer()->Clear() ;
319 SendTextUpdatedEvent();
322 bool wxTextCtrl::IsModified() const
327 bool wxTextCtrl::IsEditable() const
329 return IsEnabled() && m_editable
;
332 bool wxTextCtrl::AcceptsFocus() const
334 // we don't want focus if we can't be edited
335 return /*IsEditable() && */ wxControl::AcceptsFocus();
338 wxSize
wxTextCtrl::DoGetBestSize() const
342 // these are the numbers from the HIG:
343 // we reduce them by the borders first
346 switch ( m_windowVariant
)
348 case wxWINDOW_VARIANT_NORMAL
:
352 case wxWINDOW_VARIANT_SMALL
:
356 case wxWINDOW_VARIANT_MINI
:
365 // as the above numbers have some free space around the text
366 // we get 5 lines like this anyway
367 if ( m_windowStyle
& wxTE_MULTILINE
)
370 if ( !HasFlag(wxNO_BORDER
) )
373 return wxSize(wText
, hText
);
376 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
378 return GetTextPeer()->GetStyle(position
, style
);
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 void wxTextCtrl::Undo()
388 GetTextPeer()->Undo() ;
391 void wxTextCtrl::Redo()
394 GetTextPeer()->Redo() ;
397 bool wxTextCtrl::CanUndo() const
402 return GetTextPeer()->CanUndo() ;
405 bool wxTextCtrl::CanRedo() const
410 return GetTextPeer()->CanRedo() ;
413 void wxTextCtrl::MarkDirty()
418 void wxTextCtrl::DiscardEdits()
423 int wxTextCtrl::GetNumberOfLines() const
425 return GetTextPeer()->GetNumberOfLines() ;
428 long wxTextCtrl::XYToPosition(long x
, long y
) const
430 return GetTextPeer()->XYToPosition( x
, y
) ;
433 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
435 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
438 void wxTextCtrl::ShowPosition(long pos
)
440 return GetTextPeer()->ShowPosition(pos
) ;
443 int wxTextCtrl::GetLineLength(long lineNo
) const
445 return GetTextPeer()->GetLineLength(lineNo
) ;
448 wxString
wxTextCtrl::GetLineText(long lineNo
) const
450 return GetTextPeer()->GetLineText(lineNo
) ;
453 void wxTextCtrl::Command(wxCommandEvent
& event
)
455 SetValue(event
.GetString());
456 ProcessCommand(event
);
459 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
461 // By default, load the first file into the text window.
462 if (event
.GetNumberOfFiles() > 0)
463 LoadFile( event
.GetFiles()[0] );
466 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
468 int key
= event
.GetKeyCode() ;
469 bool eat_key
= false ;
472 if ( key
== 'a' && event
.MetaDown() )
479 if ( key
== 'c' && event
.MetaDown() )
487 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
488 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
489 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
496 // Check if we have reached the max # of chars (if it is set), but still
497 // allow navigation and deletion
498 GetSelection( &from
, &to
);
499 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
500 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
501 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
504 // eat it, we don't want to add more than allowed # of characters
506 // TODO: generate EVT_TEXT_MAXLEN()
510 // assume that any key not processed yet is going to modify the control
513 if ( key
== 'v' && event
.MetaDown() )
521 if ( key
== 'x' && event
.MetaDown() )
532 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
534 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
535 event
.SetEventObject( this );
536 event
.SetString( GetValue() );
537 if ( HandleWindowEvent(event
) )
541 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
543 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
544 if ( tlw
&& tlw
->GetDefaultItem() )
546 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
547 if ( def
&& def
->IsEnabled() )
549 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
550 event
.SetEventObject(def
);
557 // this will make wxWidgets eat the ENTER key so that
558 // we actually prevent line wrapping in a single line text control
564 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
567 if (!event
.ShiftDown())
568 flags
|= wxNavigationKeyEvent::IsForward
;
569 if (event
.ControlDown())
570 flags
|= wxNavigationKeyEvent::WinChange
;
577 // This is necessary (don't know why);
578 // otherwise the tab will not be inserted.
579 WriteText(wxT("\t"));
590 // perform keystroke handling
594 // osx_cocoa sends its event upon insertText
596 if ( ( key
>= 0x20 && key
< WXK_START
) ||
597 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
602 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
603 event1
.SetEventObject( this );
604 wxPostEvent( GetEventHandler(), event1
);
609 // ----------------------------------------------------------------------------
610 // standard handlers for standard edit menu events
611 // ----------------------------------------------------------------------------
613 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
618 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
623 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
628 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
633 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
638 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
642 GetSelection( &from
, &to
);
643 if (from
!= -1 && to
!= -1)
647 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
649 SetSelection(-1, -1);
652 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
654 event
.Enable( CanCut() );
657 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
659 event
.Enable( CanCopy() );
662 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
664 event
.Enable( CanPaste() );
667 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
669 event
.Enable( CanUndo() );
672 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
674 event
.Enable( CanRedo() );
677 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
681 GetSelection( &from
, &to
);
682 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
685 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
687 event
.Enable(GetLastPosition() > 0);
690 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
692 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
694 if ( GetTextPeer()->HasOwnContextMenu() )
701 if (m_privateContextMenu
== NULL
)
703 m_privateContextMenu
= new wxMenu
;
704 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
705 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
706 m_privateContextMenu
->AppendSeparator();
707 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
708 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
709 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
710 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
711 m_privateContextMenu
->AppendSeparator();
712 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
715 if (m_privateContextMenu
!= NULL
)
716 PopupMenu(m_privateContextMenu
);
720 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
722 if ( !GetTextPeer()->SetupCursor( pt
) )
723 return wxWindow::MacSetupCursor( pt
) ;
728 // ----------------------------------------------------------------------------
729 // implementation base class
730 // ----------------------------------------------------------------------------
732 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
733 wxTextAttr
& WXUNUSED(style
))
738 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
740 const wxTextAttr
& WXUNUSED(style
))
744 void wxTextWidgetImpl::Copy()
748 void wxTextWidgetImpl::Cut()
752 void wxTextWidgetImpl::Paste()
756 bool wxTextWidgetImpl::CanPaste() const
761 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
765 long wxTextWidgetImpl::GetLastPosition() const
767 return GetStringValue().length() ;
770 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
772 SetSelection( from
, to
) ;
776 void wxTextWidgetImpl::Remove( long from
, long to
)
778 SetSelection( from
, to
) ;
779 WriteText( wxEmptyString
) ;
782 void wxTextWidgetImpl::Clear()
784 SetStringValue( wxEmptyString
) ;
787 bool wxTextWidgetImpl::CanUndo() const
792 void wxTextWidgetImpl::Undo()
796 bool wxTextWidgetImpl::CanRedo() const
801 void wxTextWidgetImpl::Redo()
805 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
810 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
812 long *WXUNUSED(y
)) const
817 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
821 int wxTextWidgetImpl::GetNumberOfLines() const
823 ItemCount lines
= 0 ;
824 wxString content
= GetStringValue() ;
827 for (size_t i
= 0; i
< content
.length() ; i
++)
829 if (content
[i
] == '\r')
836 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
838 // TODO: change this if possible to reflect real lines
839 wxString content
= GetStringValue() ;
843 for (size_t i
= 0; i
< content
.length() ; i
++)
847 // Add chars in line then
850 for (size_t j
= i
; j
< content
.length(); j
++)
852 if (content
[j
] == '\n')
861 if (content
[i
] == '\n')
865 return wxEmptyString
;
868 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
870 // TODO: change this if possible to reflect real lines
871 wxString content
= GetStringValue() ;
875 for (size_t i
= 0; i
< content
.length() ; i
++)
879 // Count chars in line then
881 for (size_t j
= i
; j
< content
.length(); j
++)
884 if (content
[j
] == '\n')
891 if (content
[i
] == '\n')
898 #endif // wxUSE_TEXTCTRL