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
) ;
123 // only now the embedding is correct and we can do a positioning update
125 MacSuperChangedPosition() ;
127 if ( m_windowStyle
& wxTE_READONLY
)
128 SetEditable( false ) ;
130 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
135 wxTextWidgetImpl
* wxTextCtrl::GetTextPeer() const
137 return dynamic_cast<wxTextWidgetImpl
*> (m_peer
);
140 void wxTextCtrl::MacSuperChangedPosition()
142 wxWindow::MacSuperChangedPosition() ;
144 GetPeer()->SuperChangedPosition() ;
148 void wxTextCtrl::MacVisibilityChanged()
151 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
155 void wxTextCtrl::MacCheckSpelling(bool check
)
157 GetTextPeer()->CheckSpelling(check
);
160 wxString
wxTextCtrl::DoGetValue() const
162 return GetTextPeer()->GetStringValue() ;
165 void wxTextCtrl::GetSelection(long* from
, long* to
) const
167 GetTextPeer()->GetSelection( from
, to
) ;
170 void wxTextCtrl::SetMaxLength(unsigned long len
)
175 bool wxTextCtrl::SetFont( const wxFont
& font
)
177 if ( !wxTextCtrlBase::SetFont( font
) )
180 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
185 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
187 GetTextPeer()->SetStyle( start
, end
, style
) ;
192 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
194 wxTextCtrlBase::SetDefaultStyle( style
) ;
195 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
200 // Clipboard operations
202 void wxTextCtrl::Copy()
205 GetTextPeer()->Copy() ;
208 void wxTextCtrl::Cut()
212 GetTextPeer()->Cut() ;
214 SendTextUpdatedEvent();
218 void wxTextCtrl::Paste()
222 GetTextPeer()->Paste() ;
224 // TODO: eventually we should add setting the default style again
225 SendTextUpdatedEvent();
229 bool wxTextCtrl::CanCopy() const
231 // Can copy if there's a selection
233 GetSelection( &from
, &to
);
238 bool wxTextCtrl::CanCut() const
243 // Can cut if there's a selection
245 GetSelection( &from
, &to
);
250 bool wxTextCtrl::CanPaste() const
255 return GetTextPeer()->CanPaste() ;
258 void wxTextCtrl::SetEditable(bool editable
)
260 if ( editable
!= m_editable
)
262 m_editable
= editable
;
263 GetTextPeer()->SetEditable( editable
) ;
267 void wxTextCtrl::SetInsertionPoint(long pos
)
269 SetSelection( pos
, pos
) ;
272 void wxTextCtrl::SetInsertionPointEnd()
274 long pos
= GetLastPosition();
275 SetInsertionPoint( pos
);
278 long wxTextCtrl::GetInsertionPoint() const
281 GetSelection( &begin
, &end
) ;
286 wxTextPos
wxTextCtrl::GetLastPosition() const
288 return GetTextPeer()->GetLastPosition() ;
291 void wxTextCtrl::Remove(long from
, long to
)
293 GetTextPeer()->Remove( from
, to
) ;
294 if ( m_triggerUpdateEvents
)
295 SendTextUpdatedEvent();
298 void wxTextCtrl::SetSelection(long from
, long to
)
300 GetTextPeer()->SetSelection( from
, to
) ;
303 void wxTextCtrl::WriteText(const wxString
& str
)
305 GetTextPeer()->WriteText( str
) ;
306 if ( m_triggerUpdateEvents
)
307 SendTextUpdatedEvent();
310 void wxTextCtrl::Clear()
312 GetTextPeer()->Clear() ;
313 SendTextUpdatedEvent();
316 bool wxTextCtrl::IsModified() const
321 bool wxTextCtrl::IsEditable() const
323 return IsEnabled() && m_editable
;
326 bool wxTextCtrl::AcceptsFocus() const
328 // we don't want focus if we can't be edited
329 return /*IsEditable() && */ wxControl::AcceptsFocus();
332 wxSize
wxTextCtrl::DoGetBestSize() const
336 // these are the numbers from the HIG:
337 // we reduce them by the borders first
340 switch ( m_windowVariant
)
342 case wxWINDOW_VARIANT_NORMAL
:
346 case wxWINDOW_VARIANT_SMALL
:
350 case wxWINDOW_VARIANT_MINI
:
359 // as the above numbers have some free space around the text
360 // we get 5 lines like this anyway
361 if ( m_windowStyle
& wxTE_MULTILINE
)
364 if ( !HasFlag(wxNO_BORDER
) )
367 return wxSize(wText
, hText
);
370 // ----------------------------------------------------------------------------
372 // ----------------------------------------------------------------------------
374 void wxTextCtrl::Undo()
377 GetTextPeer()->Undo() ;
380 void wxTextCtrl::Redo()
383 GetTextPeer()->Redo() ;
386 bool wxTextCtrl::CanUndo() const
391 return GetTextPeer()->CanUndo() ;
394 bool wxTextCtrl::CanRedo() const
399 return GetTextPeer()->CanRedo() ;
402 void wxTextCtrl::MarkDirty()
407 void wxTextCtrl::DiscardEdits()
412 int wxTextCtrl::GetNumberOfLines() const
414 return GetTextPeer()->GetNumberOfLines() ;
417 long wxTextCtrl::XYToPosition(long x
, long y
) const
419 return GetTextPeer()->XYToPosition( x
, y
) ;
422 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
424 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
427 void wxTextCtrl::ShowPosition(long pos
)
429 return GetTextPeer()->ShowPosition(pos
) ;
432 int wxTextCtrl::GetLineLength(long lineNo
) const
434 return GetTextPeer()->GetLineLength(lineNo
) ;
437 wxString
wxTextCtrl::GetLineText(long lineNo
) const
439 return GetTextPeer()->GetLineText(lineNo
) ;
442 void wxTextCtrl::Command(wxCommandEvent
& event
)
444 SetValue(event
.GetString());
445 ProcessCommand(event
);
448 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
450 // By default, load the first file into the text window.
451 if (event
.GetNumberOfFiles() > 0)
452 LoadFile( event
.GetFiles()[0] );
455 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
457 int key
= event
.GetKeyCode() ;
458 bool eat_key
= false ;
461 if ( key
== 'a' && event
.MetaDown() )
468 if ( key
== 'c' && event
.MetaDown() )
476 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
477 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
478 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
485 // Check if we have reached the max # of chars (if it is set), but still
486 // allow navigation and deletion
487 GetSelection( &from
, &to
);
488 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
489 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&& key
!= WXK_UP
&& key
!= WXK_DOWN
&&
490 key
!= WXK_BACK
&& key
!= WXK_DELETE
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
493 // eat it, we don't want to add more than allowed # of characters
495 // TODO: generate EVT_TEXT_MAXLEN()
499 // assume that any key not processed yet is going to modify the control
502 if ( key
== 'v' && event
.MetaDown() )
510 if ( key
== 'x' && event
.MetaDown() )
521 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
523 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
524 event
.SetEventObject( this );
525 event
.SetString( GetValue() );
526 if ( HandleWindowEvent(event
) )
530 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
532 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
533 if ( tlw
&& tlw
->GetDefaultItem() )
535 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
536 if ( def
&& def
->IsEnabled() )
538 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
539 event
.SetEventObject(def
);
546 // this will make wxWidgets eat the ENTER key so that
547 // we actually prevent line wrapping in a single line text control
553 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
556 if (!event
.ShiftDown())
557 flags
|= wxNavigationKeyEvent::IsForward
;
558 if (event
.ControlDown())
559 flags
|= wxNavigationKeyEvent::WinChange
;
566 // This is necessary (don't know why);
567 // otherwise the tab will not be inserted.
568 WriteText(wxT("\t"));
579 // perform keystroke handling
583 // osx_cocoa sends its event upon insertText
585 if ( ( key
>= 0x20 && key
< WXK_START
) ||
586 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
591 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
592 event1
.SetEventObject( this );
593 wxPostEvent( GetEventHandler(), event1
);
598 // ----------------------------------------------------------------------------
599 // standard handlers for standard edit menu events
600 // ----------------------------------------------------------------------------
602 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
607 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
612 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
617 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
622 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
627 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
631 GetSelection( &from
, &to
);
632 if (from
!= -1 && to
!= -1)
636 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
638 SetSelection(-1, -1);
641 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
643 event
.Enable( CanCut() );
646 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
648 event
.Enable( CanCopy() );
651 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
653 event
.Enable( CanPaste() );
656 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
658 event
.Enable( CanUndo() );
661 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
663 event
.Enable( CanRedo() );
666 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
670 GetSelection( &from
, &to
);
671 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
674 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
676 event
.Enable(GetLastPosition() > 0);
679 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
681 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
683 if ( GetTextPeer()->HasOwnContextMenu() )
690 if (m_privateContextMenu
== NULL
)
692 m_privateContextMenu
= new wxMenu
;
693 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
694 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
695 m_privateContextMenu
->AppendSeparator();
696 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
697 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
698 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
699 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
700 m_privateContextMenu
->AppendSeparator();
701 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
704 if (m_privateContextMenu
!= NULL
)
705 PopupMenu(m_privateContextMenu
);
709 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
711 if ( !GetTextPeer()->SetupCursor( pt
) )
712 return wxWindow::MacSetupCursor( pt
) ;
717 // ----------------------------------------------------------------------------
718 // implementation base class
719 // ----------------------------------------------------------------------------
721 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
723 const wxTextAttr
& WXUNUSED(style
))
727 void wxTextWidgetImpl::Copy()
731 void wxTextWidgetImpl::Cut()
735 void wxTextWidgetImpl::Paste()
739 bool wxTextWidgetImpl::CanPaste() const
744 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
748 long wxTextWidgetImpl::GetLastPosition() const
750 return GetStringValue().length() ;
753 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
755 SetSelection( from
, to
) ;
759 void wxTextWidgetImpl::Remove( long from
, long to
)
761 SetSelection( from
, to
) ;
762 WriteText( wxEmptyString
) ;
765 void wxTextWidgetImpl::Clear()
767 SetStringValue( wxEmptyString
) ;
770 bool wxTextWidgetImpl::CanUndo() const
775 void wxTextWidgetImpl::Undo()
779 bool wxTextWidgetImpl::CanRedo() const
784 void wxTextWidgetImpl::Redo()
788 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
793 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
795 long *WXUNUSED(y
)) const
800 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
804 int wxTextWidgetImpl::GetNumberOfLines() const
806 ItemCount lines
= 0 ;
807 wxString content
= GetStringValue() ;
810 for (size_t i
= 0; i
< content
.length() ; i
++)
812 if (content
[i
] == '\r')
819 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
821 // TODO: change this if possible to reflect real lines
822 wxString content
= GetStringValue() ;
826 for (size_t i
= 0; i
< content
.length() ; i
++)
830 // Add chars in line then
833 for (size_t j
= i
; j
< content
.length(); j
++)
835 if (content
[j
] == '\n')
844 if (content
[i
] == '\n')
848 return wxEmptyString
;
851 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
853 // TODO: change this if possible to reflect real lines
854 wxString content
= GetStringValue() ;
858 for (size_t i
= 0; i
< content
.length() ; i
++)
862 // Count chars in line then
864 for (size_t j
= i
; j
< content
.length(); j
++)
867 if (content
[j
] == '\n')
874 if (content
[i
] == '\n')
881 #endif // wxUSE_TEXTCTRL