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 void wxTextCtrl::SetMaxLength(unsigned long len
)
171 bool wxTextCtrl::SetFont( const wxFont
& font
)
173 if ( !wxTextCtrlBase::SetFont( font
) )
176 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
181 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
184 GetTextPeer()->SetStyle( start
, end
, style
) ;
189 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
191 wxTextCtrlBase::SetDefaultStyle( style
) ;
192 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
197 bool wxTextCtrl::IsModified() const
202 bool wxTextCtrl::AcceptsFocus() const
204 // we don't want focus if we can't be edited
205 return /*IsEditable() && */ wxControl::AcceptsFocus();
208 wxSize
wxTextCtrl::DoGetBestSize() const
212 wxSize size
= GetTextPeer()->GetBestSize();
213 if (size
.x
> 0 && size
.y
> 0)
219 // these are the numbers from the HIG:
220 // we reduce them by the borders first
223 switch ( m_windowVariant
)
225 case wxWINDOW_VARIANT_NORMAL
:
229 case wxWINDOW_VARIANT_SMALL
:
233 case wxWINDOW_VARIANT_MINI
:
242 // as the above numbers have some free space around the text
243 // we get 5 lines like this anyway
244 if ( m_windowStyle
& wxTE_MULTILINE
)
247 if ( !HasFlag(wxNO_BORDER
) )
250 return wxSize(wText
, hText
);
253 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
255 return GetTextPeer()->GetStyle(position
, style
);
258 void wxTextCtrl::MarkDirty()
263 void wxTextCtrl::DiscardEdits()
268 int wxTextCtrl::GetNumberOfLines() const
270 return GetTextPeer()->GetNumberOfLines() ;
273 long wxTextCtrl::XYToPosition(long x
, long y
) const
275 return GetTextPeer()->XYToPosition( x
, y
) ;
278 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
280 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
283 void wxTextCtrl::ShowPosition(long pos
)
285 return GetTextPeer()->ShowPosition(pos
) ;
288 int wxTextCtrl::GetLineLength(long lineNo
) const
290 return GetTextPeer()->GetLineLength(lineNo
) ;
293 wxString
wxTextCtrl::GetLineText(long lineNo
) const
295 return GetTextPeer()->GetLineText(lineNo
) ;
298 void wxTextCtrl::Remove(long from
, long to
)
300 wxTextEntry::Remove(from
, to
);
301 if ( m_triggerUpdateEvents
)
302 SendTextUpdatedEvent();
305 void wxTextCtrl::WriteText(const wxString
& str
)
307 wxTextEntry::WriteText( str
) ;
308 if ( m_triggerUpdateEvents
)
309 SendTextUpdatedEvent();
312 void wxTextCtrl::Clear()
314 wxTextEntry::Clear() ;
315 SendTextUpdatedEvent();
318 void wxTextCtrl::Cut()
324 SendTextUpdatedEvent();
328 void wxTextCtrl::Paste()
332 wxTextEntry::Paste();
334 // TODO: eventually we should add setting the default style again
335 SendTextUpdatedEvent();
339 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
341 // By default, load the first file into the text window.
342 if (event
.GetNumberOfFiles() > 0)
343 LoadFile( event
.GetFiles()[0] );
346 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
348 int key
= event
.GetKeyCode() ;
349 bool eat_key
= false ;
352 if ( key
== 'a' && event
.MetaDown() )
359 if ( key
== 'c' && event
.MetaDown() )
367 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
368 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
369 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
376 // Check if we have reached the max # of chars (if it is set), but still
377 // allow navigation and deletion
378 GetSelection( &from
, &to
);
379 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
380 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
381 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
384 // eat it, we don't want to add more than allowed # of characters
386 // TODO: generate EVT_TEXT_MAXLEN()
390 // assume that any key not processed yet is going to modify the control
393 if ( key
== 'v' && event
.MetaDown() )
401 if ( key
== 'x' && event
.MetaDown() )
412 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
414 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
415 event
.SetEventObject( this );
416 event
.SetString( GetValue() );
417 if ( HandleWindowEvent(event
) )
421 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
423 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
424 if ( tlw
&& tlw
->GetDefaultItem() )
426 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
427 if ( def
&& def
->IsEnabled() )
429 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
430 event
.SetEventObject(def
);
437 // this will make wxWidgets eat the ENTER key so that
438 // we actually prevent line wrapping in a single line text control
444 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
447 if (!event
.ShiftDown())
448 flags
|= wxNavigationKeyEvent::IsForward
;
449 if (event
.ControlDown())
450 flags
|= wxNavigationKeyEvent::WinChange
;
457 // This is necessary (don't know why);
458 // otherwise the tab will not be inserted.
459 WriteText(wxT("\t"));
470 // perform keystroke handling
474 // osx_cocoa sends its event upon insertText
476 if ( ( key
>= 0x20 && key
< WXK_START
) ||
477 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
482 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
483 event1
.SetEventObject( this );
484 wxPostEvent( GetEventHandler(), event1
);
489 void wxTextCtrl::Command(wxCommandEvent
& event
)
491 SetValue(event
.GetString());
492 ProcessCommand(event
);
495 // ----------------------------------------------------------------------------
496 // standard handlers for standard edit menu events
497 // ----------------------------------------------------------------------------
499 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
501 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
506 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
511 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
516 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
521 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
526 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
530 GetSelection( &from
, &to
);
531 if (from
!= -1 && to
!= -1)
535 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
537 SetSelection(-1, -1);
540 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
542 event
.Enable( CanCut() );
545 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
547 event
.Enable( CanCopy() );
550 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
552 event
.Enable( CanPaste() );
555 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
557 event
.Enable( CanUndo() );
560 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
562 event
.Enable( CanRedo() );
565 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
569 GetSelection( &from
, &to
);
570 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
573 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
575 event
.Enable(GetLastPosition() > 0);
578 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
580 if ( GetTextPeer()->HasOwnContextMenu() )
587 if (m_privateContextMenu
== NULL
)
589 m_privateContextMenu
= new wxMenu
;
590 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
591 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
592 m_privateContextMenu
->AppendSeparator();
593 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
594 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
595 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
596 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
597 m_privateContextMenu
->AppendSeparator();
598 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
601 PopupMenu(m_privateContextMenu
);
605 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
607 if ( !GetTextPeer()->SetupCursor( pt
) )
608 return wxWindow::MacSetupCursor( pt
) ;
613 // ----------------------------------------------------------------------------
614 // implementation base class
615 // ----------------------------------------------------------------------------
617 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
618 wxTextAttr
& WXUNUSED(style
))
623 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
625 const wxTextAttr
& WXUNUSED(style
))
629 void wxTextWidgetImpl::Copy()
633 void wxTextWidgetImpl::Cut()
637 void wxTextWidgetImpl::Paste()
641 bool wxTextWidgetImpl::CanPaste() const
646 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
650 long wxTextWidgetImpl::GetLastPosition() const
652 return GetStringValue().length() ;
655 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
657 SetSelection( from
, to
) ;
661 void wxTextWidgetImpl::Remove( long from
, long to
)
663 SetSelection( from
, to
) ;
664 WriteText( wxEmptyString
) ;
667 void wxTextWidgetImpl::Clear()
669 SetStringValue( wxEmptyString
) ;
672 bool wxTextWidgetImpl::CanUndo() const
677 void wxTextWidgetImpl::Undo()
681 bool wxTextWidgetImpl::CanRedo() const
686 void wxTextWidgetImpl::Redo()
690 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
695 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
697 long *WXUNUSED(y
)) const
702 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
706 int wxTextWidgetImpl::GetNumberOfLines() const
708 wxString content
= GetStringValue() ;
711 for (size_t i
= 0; i
< content
.length() ; i
++)
714 if (content
[i
] == '\n')
716 if (content
[i
] == '\r')
724 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
726 // TODO: change this if possible to reflect real lines
727 wxString content
= GetStringValue() ;
731 for (size_t i
= 0; i
< content
.length() ; i
++)
735 // Add chars in line then
738 for (size_t j
= i
; j
< content
.length(); j
++)
740 if (content
[j
] == '\n')
749 if (content
[i
] == '\n')
753 return wxEmptyString
;
756 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
758 // TODO: change this if possible to reflect real lines
759 wxString content
= GetStringValue() ;
763 for (size_t i
= 0; i
< content
.length() ; i
++)
767 // Count chars in line then
769 for (size_t j
= i
; j
< content
.length(); j
++)
772 if (content
[j
] == '\n')
779 if (content
[i
] == '\n')
786 #endif // wxUSE_TEXTCTRL