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()
80 m_privateContextMenu
= NULL
;
81 m_triggerUpdateEvents
= true ;
84 wxTextCtrl::~wxTextCtrl()
87 delete m_privateContextMenu
;
91 bool wxTextCtrl::Create( wxWindow
*parent
,
97 const wxValidator
& validator
,
98 const wxString
& name
)
100 m_macIsUserPane
= false ;
103 if ( ! (style
& wxNO_BORDER
) )
104 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
106 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
109 if ( m_windowStyle
& wxTE_MULTILINE
)
111 // always turn on this style for multi-line controls
112 m_windowStyle
|= wxTE_PROCESS_ENTER
;
113 style
|= wxTE_PROCESS_ENTER
;
117 m_peer
= wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str
, pos
, size
, style
, GetExtraStyle() );
119 MacPostControlCreate(pos
, size
) ;
122 // under carbon everything can already be set before the MacPostControlCreate embedding takes place
123 // but under cocoa for single line textfields this only works after everything has been set up
124 GetTextPeer()->SetStringValue(str
);
127 // only now the embedding is correct and we can do a positioning update
129 MacSuperChangedPosition() ;
131 if ( m_windowStyle
& wxTE_READONLY
)
132 SetEditable( false ) ;
134 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
139 wxTextWidgetImpl
* wxTextCtrl::GetTextPeer() const
141 return dynamic_cast<wxTextWidgetImpl
*> (m_peer
);
144 void wxTextCtrl::MacSuperChangedPosition()
146 wxWindow::MacSuperChangedPosition() ;
148 GetPeer()->SuperChangedPosition() ;
152 void wxTextCtrl::MacVisibilityChanged()
155 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
159 void wxTextCtrl::MacCheckSpelling(bool check
)
161 GetTextPeer()->CheckSpelling(check
);
164 void wxTextCtrl::SetMaxLength(unsigned long len
)
169 bool wxTextCtrl::SetFont( const wxFont
& font
)
171 if ( !wxTextCtrlBase::SetFont( font
) )
174 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
179 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
182 GetTextPeer()->SetStyle( start
, end
, style
) ;
187 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
189 wxTextCtrlBase::SetDefaultStyle( style
) ;
190 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
195 bool wxTextCtrl::IsModified() const
200 bool wxTextCtrl::AcceptsFocus() const
202 // we don't want focus if we can't be edited
203 return /*IsEditable() && */ wxControl::AcceptsFocus();
206 wxSize
wxTextCtrl::DoGetBestSize() const
210 wxSize size
= GetTextPeer()->GetBestSize();
211 if (size
.x
> 0 && size
.y
> 0)
217 // these are the numbers from the HIG:
218 // we reduce them by the borders first
221 switch ( m_windowVariant
)
223 case wxWINDOW_VARIANT_NORMAL
:
227 case wxWINDOW_VARIANT_SMALL
:
231 case wxWINDOW_VARIANT_MINI
:
240 // as the above numbers have some free space around the text
241 // we get 5 lines like this anyway
242 if ( m_windowStyle
& wxTE_MULTILINE
)
245 if ( !HasFlag(wxNO_BORDER
) )
248 return wxSize(wText
, hText
);
251 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
253 return GetTextPeer()->GetStyle(position
, style
);
256 void wxTextCtrl::MarkDirty()
261 void wxTextCtrl::DiscardEdits()
266 int wxTextCtrl::GetNumberOfLines() const
268 return GetTextPeer()->GetNumberOfLines() ;
271 long wxTextCtrl::XYToPosition(long x
, long y
) const
273 return GetTextPeer()->XYToPosition( x
, y
) ;
276 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
278 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
281 void wxTextCtrl::ShowPosition(long pos
)
283 return GetTextPeer()->ShowPosition(pos
) ;
286 int wxTextCtrl::GetLineLength(long lineNo
) const
288 return GetTextPeer()->GetLineLength(lineNo
) ;
291 wxString
wxTextCtrl::GetLineText(long lineNo
) const
293 return GetTextPeer()->GetLineText(lineNo
) ;
296 void wxTextCtrl::Remove(long from
, long to
)
298 wxTextEntry::Remove(from
, to
);
299 if ( m_triggerUpdateEvents
)
300 SendTextUpdatedEvent();
303 void wxTextCtrl::WriteText(const wxString
& str
)
305 wxTextEntry::WriteText( str
) ;
306 if ( m_triggerUpdateEvents
)
307 SendTextUpdatedEvent();
310 void wxTextCtrl::Clear()
312 wxTextEntry::Clear() ;
313 SendTextUpdatedEvent();
316 void wxTextCtrl::Cut()
322 SendTextUpdatedEvent();
326 void wxTextCtrl::Paste()
330 wxTextEntry::Paste();
332 // TODO: eventually we should add setting the default style again
333 SendTextUpdatedEvent();
337 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
339 // By default, load the first file into the text window.
340 if (event
.GetNumberOfFiles() > 0)
341 LoadFile( event
.GetFiles()[0] );
344 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
346 int key
= event
.GetKeyCode() ;
347 bool eat_key
= false ;
350 if ( key
== 'a' && event
.MetaDown() )
357 if ( key
== 'c' && event
.MetaDown() )
365 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
366 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
367 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
374 // Check if we have reached the max # of chars (if it is set), but still
375 // allow navigation and deletion
376 GetSelection( &from
, &to
);
377 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
378 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
379 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
382 // eat it, we don't want to add more than allowed # of characters
384 // TODO: generate EVT_TEXT_MAXLEN()
388 // assume that any key not processed yet is going to modify the control
391 if ( key
== 'v' && event
.MetaDown() )
399 if ( key
== 'x' && event
.MetaDown() )
410 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
412 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
413 event
.SetEventObject( this );
414 event
.SetString( GetValue() );
415 if ( HandleWindowEvent(event
) )
419 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
421 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
422 if ( tlw
&& tlw
->GetDefaultItem() )
424 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
425 if ( def
&& def
->IsEnabled() )
427 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
428 event
.SetEventObject(def
);
435 // this will make wxWidgets eat the ENTER key so that
436 // we actually prevent line wrapping in a single line text control
442 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
445 if (!event
.ShiftDown())
446 flags
|= wxNavigationKeyEvent::IsForward
;
447 if (event
.ControlDown())
448 flags
|= wxNavigationKeyEvent::WinChange
;
455 // This is necessary (don't know why);
456 // otherwise the tab will not be inserted.
457 WriteText(wxT("\t"));
468 // perform keystroke handling
472 // osx_cocoa sends its event upon insertText
474 if ( ( key
>= 0x20 && key
< WXK_START
) ||
475 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
480 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
481 event1
.SetEventObject( this );
482 wxPostEvent( GetEventHandler(), event1
);
487 void wxTextCtrl::Command(wxCommandEvent
& event
)
489 SetValue(event
.GetString());
490 ProcessCommand(event
);
493 // ----------------------------------------------------------------------------
494 // standard handlers for standard edit menu events
495 // ----------------------------------------------------------------------------
497 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
499 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
504 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
509 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
514 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
519 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
524 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
528 GetSelection( &from
, &to
);
529 if (from
!= -1 && to
!= -1)
533 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
535 SetSelection(-1, -1);
538 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
540 event
.Enable( CanCut() );
543 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
545 event
.Enable( CanCopy() );
548 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
550 event
.Enable( CanPaste() );
553 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
555 event
.Enable( CanUndo() );
558 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
560 event
.Enable( CanRedo() );
563 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
567 GetSelection( &from
, &to
);
568 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
571 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
573 event
.Enable(GetLastPosition() > 0);
576 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
578 if ( GetTextPeer()->HasOwnContextMenu() )
585 if (m_privateContextMenu
== NULL
)
587 m_privateContextMenu
= new wxMenu
;
588 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
589 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
590 m_privateContextMenu
->AppendSeparator();
591 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
592 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
593 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
594 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
595 m_privateContextMenu
->AppendSeparator();
596 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
599 PopupMenu(m_privateContextMenu
);
603 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
605 if ( !GetTextPeer()->SetupCursor( pt
) )
606 return wxWindow::MacSetupCursor( pt
) ;
611 // ----------------------------------------------------------------------------
612 // implementation base class
613 // ----------------------------------------------------------------------------
615 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
616 wxTextAttr
& WXUNUSED(style
))
621 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
623 const wxTextAttr
& WXUNUSED(style
))
627 void wxTextWidgetImpl::Copy()
631 void wxTextWidgetImpl::Cut()
635 void wxTextWidgetImpl::Paste()
639 bool wxTextWidgetImpl::CanPaste() const
644 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
648 long wxTextWidgetImpl::GetLastPosition() const
650 return GetStringValue().length() ;
653 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
655 SetSelection( from
, to
) ;
659 void wxTextWidgetImpl::Remove( long from
, long to
)
661 SetSelection( from
, to
) ;
662 WriteText( wxEmptyString
) ;
665 void wxTextWidgetImpl::Clear()
667 SetStringValue( wxEmptyString
) ;
670 bool wxTextWidgetImpl::CanUndo() const
675 void wxTextWidgetImpl::Undo()
679 bool wxTextWidgetImpl::CanRedo() const
684 void wxTextWidgetImpl::Redo()
688 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
693 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
695 long *WXUNUSED(y
)) const
700 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
704 int wxTextWidgetImpl::GetNumberOfLines() const
706 wxString content
= GetStringValue() ;
709 for (size_t i
= 0; i
< content
.length() ; i
++)
712 if (content
[i
] == '\n')
714 if (content
[i
] == '\r')
722 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
724 // TODO: change this if possible to reflect real lines
725 wxString content
= GetStringValue() ;
729 for (size_t i
= 0; i
< content
.length() ; i
++)
733 // Add chars in line then
736 for (size_t j
= i
; j
< content
.length(); j
++)
738 if (content
[j
] == '\n')
747 if (content
[i
] == '\n')
751 return wxEmptyString
;
754 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
756 // TODO: change this if possible to reflect real lines
757 wxString content
= GetStringValue() ;
761 for (size_t i
= 0; i
< content
.length() ; i
++)
765 // Count chars in line then
767 for (size_t j
= i
; j
< content
.length(); j
++)
770 if (content
[j
] == '\n')
777 if (content
[i
] == '\n')
784 #endif // wxUSE_TEXTCTRL