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 if ( ( key
>= 0x20 && key
< WXK_START
) ||
584 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
589 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
590 event1
.SetEventObject( this );
591 wxPostEvent( GetEventHandler(), event1
);
595 // ----------------------------------------------------------------------------
596 // standard handlers for standard edit menu events
597 // ----------------------------------------------------------------------------
599 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
604 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
609 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
614 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
619 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
624 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
628 GetSelection( &from
, &to
);
629 if (from
!= -1 && to
!= -1)
633 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
635 SetSelection(-1, -1);
638 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
640 event
.Enable( CanCut() );
643 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
645 event
.Enable( CanCopy() );
648 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
650 event
.Enable( CanPaste() );
653 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
655 event
.Enable( CanUndo() );
658 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
660 event
.Enable( CanRedo() );
663 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
667 GetSelection( &from
, &to
);
668 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
671 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
673 event
.Enable(GetLastPosition() > 0);
676 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
678 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
680 if ( GetTextPeer()->HasOwnContextMenu() )
687 if (m_privateContextMenu
== NULL
)
689 m_privateContextMenu
= new wxMenu
;
690 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
691 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
692 m_privateContextMenu
->AppendSeparator();
693 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
694 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
695 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
696 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
697 m_privateContextMenu
->AppendSeparator();
698 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
701 if (m_privateContextMenu
!= NULL
)
702 PopupMenu(m_privateContextMenu
);
706 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
708 if ( !GetTextPeer()->SetupCursor( pt
) )
709 return wxWindow::MacSetupCursor( pt
) ;
714 // ----------------------------------------------------------------------------
715 // implementation base class
716 // ----------------------------------------------------------------------------
718 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
720 const wxTextAttr
& WXUNUSED(style
))
724 void wxTextWidgetImpl::Copy()
728 void wxTextWidgetImpl::Cut()
732 void wxTextWidgetImpl::Paste()
736 bool wxTextWidgetImpl::CanPaste() const
741 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
745 long wxTextWidgetImpl::GetLastPosition() const
747 return GetStringValue().length() ;
750 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
752 SetSelection( from
, to
) ;
756 void wxTextWidgetImpl::Remove( long from
, long to
)
758 SetSelection( from
, to
) ;
759 WriteText( wxEmptyString
) ;
762 void wxTextWidgetImpl::Clear()
764 SetStringValue( wxEmptyString
) ;
767 bool wxTextWidgetImpl::CanUndo() const
772 void wxTextWidgetImpl::Undo()
776 bool wxTextWidgetImpl::CanRedo() const
781 void wxTextWidgetImpl::Redo()
785 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
790 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
792 long *WXUNUSED(y
)) const
797 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
801 int wxTextWidgetImpl::GetNumberOfLines() const
803 ItemCount lines
= 0 ;
804 wxString content
= GetStringValue() ;
807 for (size_t i
= 0; i
< content
.length() ; i
++)
809 if (content
[i
] == '\r')
816 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
818 // TODO: change this if possible to reflect real lines
819 wxString content
= GetStringValue() ;
823 for (size_t i
= 0; i
< content
.length() ; i
++)
827 // Add chars in line then
830 for (size_t j
= i
; j
< content
.length(); j
++)
832 if (content
[j
] == '\n')
841 if (content
[i
] == '\n')
845 return wxEmptyString
;
848 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
850 // TODO: change this if possible to reflect real lines
851 wxString content
= GetStringValue() ;
855 for (size_t i
= 0; i
< content
.length() ; i
++)
859 // Count chars in line then
861 for (size_t j
= i
; j
< content
.length(); j
++)
864 if (content
[j
] == '\n')
871 if (content
[i
] == '\n')
878 #endif // wxUSE_TEXTCTRL