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_KEY_DOWN(wxTextCtrl::OnKeyDown
)
57 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
58 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
59 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
60 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
61 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
62 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
63 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
65 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
67 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
68 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
69 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
70 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
71 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
72 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
73 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
77 void wxTextCtrl::Init()
81 m_privateContextMenu
= NULL
;
82 m_triggerUpdateEvents
= true ;
85 wxTextCtrl::~wxTextCtrl()
88 delete m_privateContextMenu
;
92 bool wxTextCtrl::Create( wxWindow
*parent
,
98 const wxValidator
& validator
,
99 const wxString
& name
)
101 m_macIsUserPane
= false ;
104 if ( ! (style
& wxNO_BORDER
) )
105 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
107 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
110 if ( m_windowStyle
& wxTE_MULTILINE
)
112 // always turn on this style for multi-line controls
113 m_windowStyle
|= wxTE_PROCESS_ENTER
;
114 style
|= wxTE_PROCESS_ENTER
;
118 m_peer
= wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str
, pos
, size
, style
, GetExtraStyle() );
120 MacPostControlCreate(pos
, size
) ;
123 // under carbon everything can already be set before the MacPostControlCreate embedding takes place
124 // but under cocoa for single line textfields this only works after everything has been set up
125 GetTextPeer()->SetStringValue(str
);
128 // only now the embedding is correct and we can do a positioning update
130 MacSuperChangedPosition() ;
132 if ( m_windowStyle
& wxTE_READONLY
)
133 SetEditable( false ) ;
135 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
140 wxTextWidgetImpl
* wxTextCtrl::GetTextPeer() const
142 return dynamic_cast<wxTextWidgetImpl
*> (m_peer
);
145 void wxTextCtrl::MacSuperChangedPosition()
147 wxWindow::MacSuperChangedPosition() ;
149 GetPeer()->SuperChangedPosition() ;
153 void wxTextCtrl::MacVisibilityChanged()
156 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
160 void wxTextCtrl::MacCheckSpelling(bool check
)
162 GetTextPeer()->CheckSpelling(check
);
165 void wxTextCtrl::SetMaxLength(unsigned long len
)
170 bool wxTextCtrl::SetFont( const wxFont
& font
)
172 if ( !wxTextCtrlBase::SetFont( font
) )
175 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
180 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
183 GetTextPeer()->SetStyle( start
, end
, style
) ;
188 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
190 wxTextCtrlBase::SetDefaultStyle( style
) ;
191 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
196 bool wxTextCtrl::IsModified() const
201 bool wxTextCtrl::AcceptsFocus() const
203 // we don't want focus if we can't be edited
204 return /*IsEditable() && */ wxControl::AcceptsFocus();
207 wxSize
wxTextCtrl::DoGetBestSize() const
211 wxSize size
= GetTextPeer()->GetBestSize();
212 if (size
.x
> 0 && size
.y
> 0)
218 // these are the numbers from the HIG:
219 // we reduce them by the borders first
222 switch ( m_windowVariant
)
224 case wxWINDOW_VARIANT_NORMAL
:
228 case wxWINDOW_VARIANT_SMALL
:
232 case wxWINDOW_VARIANT_MINI
:
241 // as the above numbers have some free space around the text
242 // we get 5 lines like this anyway
243 if ( m_windowStyle
& wxTE_MULTILINE
)
246 if ( !HasFlag(wxNO_BORDER
) )
249 return wxSize(wText
, hText
);
252 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
254 return GetTextPeer()->GetStyle(position
, style
);
257 void wxTextCtrl::MarkDirty()
262 void wxTextCtrl::DiscardEdits()
267 int wxTextCtrl::GetNumberOfLines() const
269 return GetTextPeer()->GetNumberOfLines() ;
272 long wxTextCtrl::XYToPosition(long x
, long y
) const
274 return GetTextPeer()->XYToPosition( x
, y
) ;
277 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
279 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
282 void wxTextCtrl::ShowPosition(long pos
)
284 return GetTextPeer()->ShowPosition(pos
) ;
287 int wxTextCtrl::GetLineLength(long lineNo
) const
289 return GetTextPeer()->GetLineLength(lineNo
) ;
292 wxString
wxTextCtrl::GetLineText(long lineNo
) const
294 return GetTextPeer()->GetLineText(lineNo
) ;
297 void wxTextCtrl::Remove(long from
, long to
)
299 wxTextEntry::Remove(from
, to
);
300 if ( m_triggerUpdateEvents
)
301 SendTextUpdatedEvent();
304 void wxTextCtrl::WriteText(const wxString
& str
)
306 wxTextEntry::WriteText( str
) ;
307 if ( m_triggerUpdateEvents
)
308 SendTextUpdatedEvent();
311 void wxTextCtrl::Clear()
313 wxTextEntry::Clear() ;
314 SendTextUpdatedEvent();
317 void wxTextCtrl::Cut()
323 SendTextUpdatedEvent();
327 void wxTextCtrl::Paste()
331 wxTextEntry::Paste();
333 // TODO: eventually we should add setting the default style again
334 SendTextUpdatedEvent();
338 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
340 // By default, load the first file into the text window.
341 if (event
.GetNumberOfFiles() > 0)
342 LoadFile( event
.GetFiles()[0] );
345 void wxTextCtrl::OnKeyDown(wxKeyEvent
& event
)
347 if ( event
.GetModifiers() == wxMOD_CMD
)
349 switch( event
.GetKeyCode() )
370 // no, we didn't process it
374 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
376 int key
= event
.GetKeyCode() ;
377 bool eat_key
= false ;
380 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
381 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
382 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
389 // Check if we have reached the max # of chars (if it is set), but still
390 // allow navigation and deletion
391 GetSelection( &from
, &to
);
392 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
393 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
394 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
397 // eat it, we don't want to add more than allowed # of characters
399 // TODO: generate EVT_TEXT_MAXLEN()
403 // assume that any key not processed yet is going to modify the control
409 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
411 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
412 event
.SetEventObject( this );
413 event
.SetString( GetValue() );
414 if ( HandleWindowEvent(event
) )
418 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
420 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
421 if ( tlw
&& tlw
->GetDefaultItem() )
423 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
424 if ( def
&& def
->IsEnabled() )
426 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
427 event
.SetEventObject(def
);
434 // this will make wxWidgets eat the ENTER key so that
435 // we actually prevent line wrapping in a single line text control
441 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
444 if (!event
.ShiftDown())
445 flags
|= wxNavigationKeyEvent::IsForward
;
446 if (event
.ControlDown())
447 flags
|= wxNavigationKeyEvent::WinChange
;
454 // This is necessary (don't know why);
455 // otherwise the tab will not be inserted.
456 WriteText(wxT("\t"));
467 // perform keystroke handling
471 // osx_cocoa sends its event upon insertText
473 if ( ( key
>= 0x20 && key
< WXK_START
) ||
474 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
479 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
480 event1
.SetEventObject( this );
481 wxPostEvent( GetEventHandler(), event1
);
486 void wxTextCtrl::Command(wxCommandEvent
& event
)
488 SetValue(event
.GetString());
489 ProcessCommand(event
);
492 // ----------------------------------------------------------------------------
493 // standard handlers for standard edit menu events
494 // ----------------------------------------------------------------------------
496 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
498 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
503 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
508 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
513 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
518 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
523 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
527 GetSelection( &from
, &to
);
528 if (from
!= -1 && to
!= -1)
532 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
534 SetSelection(-1, -1);
537 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
539 event
.Enable( CanCut() );
542 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
544 event
.Enable( CanCopy() );
547 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
549 event
.Enable( CanPaste() );
552 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
554 event
.Enable( CanUndo() );
557 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
559 event
.Enable( CanRedo() );
562 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
566 GetSelection( &from
, &to
);
567 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
570 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
572 event
.Enable(GetLastPosition() > 0);
575 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
577 if ( GetTextPeer()->HasOwnContextMenu() )
584 if (m_privateContextMenu
== NULL
)
586 m_privateContextMenu
= new wxMenu
;
587 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
588 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
589 m_privateContextMenu
->AppendSeparator();
590 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
591 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
592 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
593 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
594 m_privateContextMenu
->AppendSeparator();
595 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
598 PopupMenu(m_privateContextMenu
);
602 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
604 if ( !GetTextPeer()->SetupCursor( pt
) )
605 return wxWindow::MacSetupCursor( pt
) ;
610 // ----------------------------------------------------------------------------
611 // implementation base class
612 // ----------------------------------------------------------------------------
614 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
615 wxTextAttr
& WXUNUSED(style
))
620 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
622 const wxTextAttr
& WXUNUSED(style
))
626 void wxTextWidgetImpl::Copy()
630 void wxTextWidgetImpl::Cut()
634 void wxTextWidgetImpl::Paste()
638 bool wxTextWidgetImpl::CanPaste() const
643 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
647 long wxTextWidgetImpl::GetLastPosition() const
649 return GetStringValue().length() ;
652 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
654 SetSelection( from
, to
) ;
658 void wxTextWidgetImpl::Remove( long from
, long to
)
660 SetSelection( from
, to
) ;
661 WriteText( wxEmptyString
) ;
664 void wxTextWidgetImpl::Clear()
666 SetStringValue( wxEmptyString
) ;
669 bool wxTextWidgetImpl::CanUndo() const
674 void wxTextWidgetImpl::Undo()
678 bool wxTextWidgetImpl::CanRedo() const
683 void wxTextWidgetImpl::Redo()
687 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
692 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
694 long *WXUNUSED(y
)) const
699 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
703 int wxTextWidgetImpl::GetNumberOfLines() const
705 wxString content
= GetStringValue() ;
708 for (size_t i
= 0; i
< content
.length() ; i
++)
711 if (content
[i
] == '\n')
713 if (content
[i
] == '\r')
721 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
723 // TODO: change this if possible to reflect real lines
724 wxString content
= GetStringValue() ;
728 for (size_t i
= 0; i
< content
.length() ; i
++)
732 // Add chars in line then
735 for (size_t j
= i
; j
< content
.length(); j
++)
737 if (content
[j
] == '\n')
746 if (content
[i
] == '\n')
750 return wxEmptyString
;
753 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
755 // TODO: change this if possible to reflect real lines
756 wxString content
= GetStringValue() ;
760 for (size_t i
= 0; i
< content
.length() ; i
++)
764 // Count chars in line then
766 for (size_t j
= i
; j
< content
.length(); j
++)
769 if (content
[j
] == '\n')
776 if (content
[i
] == '\n')
783 #endif // wxUSE_TEXTCTRL