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_triggerOnSetValue
= 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::GetValue() const
162 return GetTextPeer()->GetStringValue() ;
165 void wxTextCtrl::GetSelection(long* from
, long* to
) const
167 GetTextPeer()->GetSelection( from
, to
) ;
170 void wxTextCtrl::DoSetValue(const wxString
& str
, int flags
)
173 if ( GetValue() == str
)
176 GetTextPeer()->SetStringValue( str
) ;
178 if ( (flags
& SetValue_SendEvent
) && m_triggerOnSetValue
)
180 SendTextUpdatedEvent();
184 void wxTextCtrl::SetMaxLength(unsigned long len
)
189 bool wxTextCtrl::SetFont( const wxFont
& font
)
191 if ( !wxTextCtrlBase::SetFont( font
) )
194 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
199 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
201 GetTextPeer()->SetStyle( start
, end
, style
) ;
206 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
208 wxTextCtrlBase::SetDefaultStyle( style
) ;
209 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
214 // Clipboard operations
216 void wxTextCtrl::Copy()
219 GetTextPeer()->Copy() ;
222 void wxTextCtrl::Cut()
226 GetTextPeer()->Cut() ;
228 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
229 event
.SetEventObject( this );
230 HandleWindowEvent( event
);
234 void wxTextCtrl::Paste()
238 GetTextPeer()->Paste() ;
240 // TODO: eventually we should add setting the default style again
242 wxCommandEvent
event( wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
243 event
.SetEventObject( this );
244 HandleWindowEvent( event
);
248 bool wxTextCtrl::CanCopy() const
250 // Can copy if there's a selection
252 GetSelection( &from
, &to
);
257 bool wxTextCtrl::CanCut() const
262 // Can cut if there's a selection
264 GetSelection( &from
, &to
);
269 bool wxTextCtrl::CanPaste() const
274 return GetTextPeer()->CanPaste() ;
277 void wxTextCtrl::SetEditable(bool editable
)
279 if ( editable
!= m_editable
)
281 m_editable
= editable
;
282 GetTextPeer()->SetEditable( editable
) ;
286 void wxTextCtrl::SetInsertionPoint(long pos
)
288 SetSelection( pos
, pos
) ;
291 void wxTextCtrl::SetInsertionPointEnd()
293 long pos
= GetLastPosition();
294 SetInsertionPoint( pos
);
297 long wxTextCtrl::GetInsertionPoint() const
300 GetSelection( &begin
, &end
) ;
305 wxTextPos
wxTextCtrl::GetLastPosition() const
307 return GetTextPeer()->GetLastPosition() ;
310 void wxTextCtrl::Replace(long from
, long to
, const wxString
& str
)
312 GetTextPeer()->Replace( from
, to
, str
) ;
315 void wxTextCtrl::Remove(long from
, long to
)
317 GetTextPeer()->Remove( from
, to
) ;
320 void wxTextCtrl::SetSelection(long from
, long to
)
322 GetTextPeer()->SetSelection( from
, to
) ;
325 void wxTextCtrl::WriteText(const wxString
& str
)
327 GetTextPeer()->WriteText( str
) ;
330 void wxTextCtrl::AppendText(const wxString
& text
)
332 SetInsertionPointEnd();
336 void wxTextCtrl::Clear()
338 GetTextPeer()->Clear() ;
341 bool wxTextCtrl::IsModified() const
346 bool wxTextCtrl::IsEditable() const
348 return IsEnabled() && m_editable
;
351 bool wxTextCtrl::AcceptsFocus() const
353 // we don't want focus if we can't be edited
354 return /*IsEditable() && */ wxControl::AcceptsFocus();
357 wxSize
wxTextCtrl::DoGetBestSize() const
361 // these are the numbers from the HIG:
362 // we reduce them by the borders first
365 switch ( m_windowVariant
)
367 case wxWINDOW_VARIANT_NORMAL
:
371 case wxWINDOW_VARIANT_SMALL
:
375 case wxWINDOW_VARIANT_MINI
:
384 // as the above numbers have some free space around the text
385 // we get 5 lines like this anyway
386 if ( m_windowStyle
& wxTE_MULTILINE
)
389 if ( !HasFlag(wxNO_BORDER
) )
392 return wxSize(wText
, hText
);
395 // ----------------------------------------------------------------------------
397 // ----------------------------------------------------------------------------
399 void wxTextCtrl::Undo()
402 GetTextPeer()->Undo() ;
405 void wxTextCtrl::Redo()
408 GetTextPeer()->Redo() ;
411 bool wxTextCtrl::CanUndo() const
416 return GetTextPeer()->CanUndo() ;
419 bool wxTextCtrl::CanRedo() const
424 return GetTextPeer()->CanRedo() ;
427 void wxTextCtrl::MarkDirty()
432 void wxTextCtrl::DiscardEdits()
437 int wxTextCtrl::GetNumberOfLines() const
439 return GetTextPeer()->GetNumberOfLines() ;
442 long wxTextCtrl::XYToPosition(long x
, long y
) const
444 return GetTextPeer()->XYToPosition( x
, y
) ;
447 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
449 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
452 void wxTextCtrl::ShowPosition(long pos
)
454 return GetTextPeer()->ShowPosition(pos
) ;
457 int wxTextCtrl::GetLineLength(long lineNo
) const
459 return GetTextPeer()->GetLineLength(lineNo
) ;
462 wxString
wxTextCtrl::GetLineText(long lineNo
) const
464 return GetTextPeer()->GetLineText(lineNo
) ;
467 void wxTextCtrl::Command(wxCommandEvent
& event
)
469 SetValue(event
.GetString());
470 ProcessCommand(event
);
473 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
475 // By default, load the first file into the text window.
476 if (event
.GetNumberOfFiles() > 0)
477 LoadFile( event
.GetFiles()[0] );
480 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
482 int key
= event
.GetKeyCode() ;
483 bool eat_key
= false ;
486 if ( key
== 'a' && event
.MetaDown() )
493 if ( key
== 'c' && event
.MetaDown() )
501 if ( !IsEditable() && key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_DOWN
&& key
!= WXK_UP
&& key
!= WXK_TAB
&&
502 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
503 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
510 // Check if we have reached the max # of chars (if it is set), but still
511 // allow navigation and deletion
512 GetSelection( &from
, &to
);
513 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
514 key
!= WXK_LEFT
&& key
!= WXK_RIGHT
&& key
!= WXK_TAB
&& key
!= WXK_UP
&& key
!= WXK_DOWN
&&
515 key
!= WXK_BACK
&& key
!= WXK_DELETE
&& !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
518 // eat it, we don't want to add more than allowed # of characters
520 // TODO: generate EVT_TEXT_MAXLEN()
524 // assume that any key not processed yet is going to modify the control
527 if ( key
== 'v' && event
.MetaDown() )
535 if ( key
== 'x' && event
.MetaDown() )
546 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
548 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
549 event
.SetEventObject( this );
550 event
.SetString( GetValue() );
551 if ( HandleWindowEvent(event
) )
555 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
557 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
558 if ( tlw
&& tlw
->GetDefaultItem() )
560 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
561 if ( def
&& def
->IsEnabled() )
563 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
564 event
.SetEventObject(def
);
571 // this will make wxWidgets eat the ENTER key so that
572 // we actually prevent line wrapping in a single line text control
578 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
581 if (!event
.ShiftDown())
582 flags
|= wxNavigationKeyEvent::IsForward
;
583 if (event
.ControlDown())
584 flags
|= wxNavigationKeyEvent::WinChange
;
591 // This is necessary (don't know why);
592 // otherwise the tab will not be inserted.
593 WriteText(wxT("\t"));
604 // perform keystroke handling
608 if ( ( key
>= 0x20 && key
< WXK_START
) ||
609 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
614 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
615 event1
.SetEventObject( this );
616 wxPostEvent( GetEventHandler(), event1
);
620 // ----------------------------------------------------------------------------
621 // standard handlers for standard edit menu events
622 // ----------------------------------------------------------------------------
624 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
629 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
634 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
639 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
644 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
649 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
653 GetSelection( &from
, &to
);
654 if (from
!= -1 && to
!= -1)
658 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
660 SetSelection(-1, -1);
663 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
665 event
.Enable( CanCut() );
668 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
670 event
.Enable( CanCopy() );
673 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
675 event
.Enable( CanPaste() );
678 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
680 event
.Enable( CanUndo() );
683 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
685 event
.Enable( CanRedo() );
688 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
692 GetSelection( &from
, &to
);
693 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
696 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
698 event
.Enable(GetLastPosition() > 0);
701 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
703 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
705 if ( GetTextPeer()->HasOwnContextMenu() )
712 if (m_privateContextMenu
== NULL
)
714 m_privateContextMenu
= new wxMenu
;
715 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
716 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
717 m_privateContextMenu
->AppendSeparator();
718 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
719 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
720 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
721 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
722 m_privateContextMenu
->AppendSeparator();
723 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
726 if (m_privateContextMenu
!= NULL
)
727 PopupMenu(m_privateContextMenu
);
731 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
733 if ( !GetTextPeer()->SetupCursor( pt
) )
734 return wxWindow::MacSetupCursor( pt
) ;
739 // ----------------------------------------------------------------------------
740 // implementation base class
741 // ----------------------------------------------------------------------------
743 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
745 const wxTextAttr
& WXUNUSED(style
))
749 void wxTextWidgetImpl::Copy()
753 void wxTextWidgetImpl::Cut()
757 void wxTextWidgetImpl::Paste()
761 bool wxTextWidgetImpl::CanPaste() const
766 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
770 long wxTextWidgetImpl::GetLastPosition() const
772 return GetStringValue().length() ;
775 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
777 SetSelection( from
, to
) ;
781 void wxTextWidgetImpl::Remove( long from
, long to
)
783 SetSelection( from
, to
) ;
784 WriteText( wxEmptyString
) ;
787 void wxTextWidgetImpl::Clear()
789 SetStringValue( wxEmptyString
) ;
792 bool wxTextWidgetImpl::CanUndo() const
797 void wxTextWidgetImpl::Undo()
801 bool wxTextWidgetImpl::CanRedo() const
806 void wxTextWidgetImpl::Redo()
810 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
815 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
817 long *WXUNUSED(y
)) const
822 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
826 int wxTextWidgetImpl::GetNumberOfLines() const
828 ItemCount lines
= 0 ;
829 wxString content
= GetStringValue() ;
832 for (size_t i
= 0; i
< content
.length() ; i
++)
834 if (content
[i
] == '\r')
841 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
843 // TODO: change this if possible to reflect real lines
844 wxString content
= GetStringValue() ;
848 for (size_t i
= 0; i
< content
.length() ; i
++)
852 // Add chars in line then
855 for (size_t j
= i
; j
< content
.length(); j
++)
857 if (content
[j
] == '\n')
866 if (content
[i
] == '\n')
870 return wxEmptyString
;
873 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
875 // TODO: change this if possible to reflect real lines
876 wxString content
= GetStringValue() ;
880 for (size_t i
= 0; i
< content
.length() ; i
++)
884 // Count chars in line then
886 for (size_t j
= i
; j
< content
.length(); j
++)
889 if (content
[j
] == '\n')
896 if (content
[i
] == '\n')
903 #endif // wxUSE_TEXTCTRL