1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/textctrl_osx.cpp
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
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 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
52 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
53 EVT_CHAR(wxTextCtrl::OnChar
)
54 EVT_KEY_DOWN(wxTextCtrl::OnKeyDown
)
55 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
56 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
57 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
58 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
59 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
60 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
61 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
63 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
65 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
66 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
67 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
68 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
69 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
70 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
71 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
75 void wxTextCtrl::Init()
79 m_privateContextMenu
= NULL
;
82 wxTextCtrl::~wxTextCtrl()
85 delete m_privateContextMenu
;
89 bool wxTextCtrl::Create( wxWindow
*parent
,
95 const wxValidator
& validator
,
96 const wxString
& name
)
101 if ( ! (style
& wxNO_BORDER
) )
102 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
104 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
107 if ( m_windowStyle
& wxTE_MULTILINE
)
109 // always turn on this style for multi-line controls
110 m_windowStyle
|= wxTE_PROCESS_ENTER
;
111 style
|= wxTE_PROCESS_ENTER
;
115 SetPeer(wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str
, pos
, size
, style
, GetExtraStyle() ));
117 MacPostControlCreate(pos
, size
) ;
120 // under carbon everything can already be set before the MacPostControlCreate embedding takes place
121 // but under cocoa for single line textfields this only works after everything has been set up
122 GetTextPeer()->SetStringValue(str
);
125 // only now the embedding is correct and we can do a positioning update
127 MacSuperChangedPosition() ;
129 if ( m_windowStyle
& wxTE_READONLY
)
130 SetEditable( false ) ;
132 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
137 void wxTextCtrl::MacSuperChangedPosition()
139 wxWindow::MacSuperChangedPosition() ;
141 GetPeer()->SuperChangedPosition() ;
145 void wxTextCtrl::MacVisibilityChanged()
148 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
152 void wxTextCtrl::MacCheckSpelling(bool check
)
154 GetTextPeer()->CheckSpelling(check
);
157 void wxTextCtrl::SetMaxLength(unsigned long len
)
162 bool wxTextCtrl::SetFont( const wxFont
& font
)
164 if ( !wxTextCtrlBase::SetFont( font
) )
167 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
172 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
175 GetTextPeer()->SetStyle( start
, end
, style
) ;
180 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
182 wxTextCtrlBase::SetDefaultStyle( style
) ;
183 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
188 bool wxTextCtrl::IsModified() const
193 bool wxTextCtrl::AcceptsFocus() const
195 // we don't want focus if we can't be edited
196 return /*IsEditable() && */ wxControl::AcceptsFocus();
199 wxSize
wxTextCtrl::DoGetBestSize() const
203 wxSize size
= GetTextPeer()->GetBestSize();
204 if (size
.x
> 0 && size
.y
> 0)
210 // these are the numbers from the HIG:
211 // we reduce them by the borders first
214 switch ( m_windowVariant
)
216 case wxWINDOW_VARIANT_NORMAL
:
220 case wxWINDOW_VARIANT_SMALL
:
224 case wxWINDOW_VARIANT_MINI
:
233 // as the above numbers have some free space around the text
234 // we get 5 lines like this anyway
235 if ( m_windowStyle
& wxTE_MULTILINE
)
238 if ( !HasFlag(wxNO_BORDER
) )
241 return wxSize(wText
, hText
);
244 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
246 return GetTextPeer()->GetStyle(position
, style
);
249 void wxTextCtrl::MarkDirty()
254 void wxTextCtrl::DiscardEdits()
259 int wxTextCtrl::GetNumberOfLines() const
261 return GetTextPeer()->GetNumberOfLines() ;
264 long wxTextCtrl::XYToPosition(long x
, long y
) const
266 return GetTextPeer()->XYToPosition( x
, y
) ;
269 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
271 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
274 void wxTextCtrl::ShowPosition(long pos
)
276 return GetTextPeer()->ShowPosition(pos
) ;
279 int wxTextCtrl::GetLineLength(long lineNo
) const
281 return GetTextPeer()->GetLineLength(lineNo
) ;
284 wxString
wxTextCtrl::GetLineText(long lineNo
) const
286 return GetTextPeer()->GetLineText(lineNo
) ;
289 void wxTextCtrl::Copy()
293 wxClipboardTextEvent
evt(wxEVT_COMMAND_TEXT_COPY
, GetId());
294 evt
.SetEventObject(this);
295 if (!GetEventHandler()->ProcessEvent(evt
))
302 void wxTextCtrl::Cut()
306 wxClipboardTextEvent
evt(wxEVT_COMMAND_TEXT_CUT
, GetId());
307 evt
.SetEventObject(this);
308 if (!GetEventHandler()->ProcessEvent(evt
))
312 SendTextUpdatedEvent();
317 void wxTextCtrl::Paste()
321 wxClipboardTextEvent
evt(wxEVT_COMMAND_TEXT_PASTE
, GetId());
322 evt
.SetEventObject(this);
323 if (!GetEventHandler()->ProcessEvent(evt
))
325 wxTextEntry::Paste();
327 // TODO: eventually we should add setting the default style again
328 SendTextUpdatedEvent();
333 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
335 // By default, load the first file into the text window.
336 if (event
.GetNumberOfFiles() > 0)
337 LoadFile( event
.GetFiles()[0] );
340 void wxTextCtrl::OnKeyDown(wxKeyEvent
& event
)
342 if ( event
.GetModifiers() == wxMOD_CONTROL
)
344 switch( event
.GetKeyCode() )
365 // no, we didn't process it
369 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
371 int key
= event
.GetKeyCode() ;
372 bool eat_key
= false ;
375 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
376 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
377 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
384 // Check if we have reached the max # of chars (if it is set), but still
385 // allow navigation and deletion
386 GetSelection( &from
, &to
);
387 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
388 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
389 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
392 // eat it, we don't want to add more than allowed # of characters
394 // TODO: generate EVT_TEXT_MAXLEN()
398 // assume that any key not processed yet is going to modify the control
404 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
406 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
407 event
.SetEventObject( this );
408 event
.SetString( GetValue() );
409 if ( HandleWindowEvent(event
) )
413 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
415 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
416 if ( tlw
&& tlw
->GetDefaultItem() )
418 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
419 if ( def
&& def
->IsEnabled() )
421 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
422 event
.SetEventObject(def
);
429 // this will make wxWidgets eat the ENTER key so that
430 // we actually prevent line wrapping in a single line text control
436 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
439 if (!event
.ShiftDown())
440 flags
|= wxNavigationKeyEvent::IsForward
;
441 if (event
.ControlDown())
442 flags
|= wxNavigationKeyEvent::WinChange
;
449 // This is necessary (don't know why);
450 // otherwise the tab will not be inserted.
451 WriteText(wxT("\t"));
462 // perform keystroke handling
466 // osx_cocoa sends its event upon insertText
468 if ( ( key
>= 0x20 && key
< WXK_START
) ||
469 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
474 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
475 event1
.SetEventObject( this );
476 wxPostEvent( GetEventHandler(), event1
);
481 void wxTextCtrl::Command(wxCommandEvent
& event
)
483 SetValue(event
.GetString());
484 ProcessCommand(event
);
487 // ----------------------------------------------------------------------------
488 // standard handlers for standard edit menu events
489 // ----------------------------------------------------------------------------
491 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
493 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
498 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
503 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
508 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
513 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
518 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
522 GetSelection( &from
, &to
);
523 if (from
!= -1 && to
!= -1)
527 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
529 SetSelection(-1, -1);
532 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
534 event
.Enable( CanCut() );
537 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
539 event
.Enable( CanCopy() );
542 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
544 event
.Enable( CanPaste() );
547 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
549 event
.Enable( CanUndo() );
552 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
554 event
.Enable( CanRedo() );
557 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
561 GetSelection( &from
, &to
);
562 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
565 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
567 event
.Enable(GetLastPosition() > 0);
570 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
572 if ( GetTextPeer()->HasOwnContextMenu() )
579 if (m_privateContextMenu
== NULL
)
581 m_privateContextMenu
= new wxMenu
;
582 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
583 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
584 m_privateContextMenu
->AppendSeparator();
585 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
586 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
587 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
588 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
589 m_privateContextMenu
->AppendSeparator();
590 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
593 PopupMenu(m_privateContextMenu
);
597 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
599 if ( !GetTextPeer()->SetupCursor( pt
) )
600 return wxWindow::MacSetupCursor( pt
) ;
605 bool wxTextCtrl::SetHint(const wxString
& hint
)
609 if ( GetTextPeer() && GetTextPeer()->SetHint(hint
) )
615 wxString
wxTextCtrl::GetHint() const
620 // ----------------------------------------------------------------------------
621 // implementation base class
622 // ----------------------------------------------------------------------------
624 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
625 wxTextAttr
& WXUNUSED(style
))
630 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
632 const wxTextAttr
& WXUNUSED(style
))
636 void wxTextWidgetImpl::Copy()
640 void wxTextWidgetImpl::Cut()
644 void wxTextWidgetImpl::Paste()
648 bool wxTextWidgetImpl::CanPaste() const
653 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
657 long wxTextWidgetImpl::GetLastPosition() const
659 return GetStringValue().length() ;
662 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
664 SetSelection( from
, to
) ;
668 void wxTextWidgetImpl::Remove( long from
, long to
)
670 SetSelection( from
, to
) ;
671 WriteText( wxEmptyString
) ;
674 void wxTextWidgetImpl::Clear()
676 SetStringValue( wxEmptyString
) ;
679 bool wxTextWidgetImpl::CanUndo() const
684 void wxTextWidgetImpl::Undo()
688 bool wxTextWidgetImpl::CanRedo() const
693 void wxTextWidgetImpl::Redo()
697 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
702 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
704 long *WXUNUSED(y
)) const
709 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
713 int wxTextWidgetImpl::GetNumberOfLines() const
715 wxString content
= GetStringValue() ;
718 for (size_t i
= 0; i
< content
.length() ; i
++)
721 if (content
[i
] == '\n')
723 if (content
[i
] == '\r')
731 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
733 // TODO: change this if possible to reflect real lines
734 wxString content
= GetStringValue() ;
738 for (size_t i
= 0; i
< content
.length() ; i
++)
742 // Add chars in line then
745 for (size_t j
= i
; j
< content
.length(); j
++)
747 if (content
[j
] == '\n')
756 if (content
[i
] == '\n')
760 return wxEmptyString
;
763 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
765 // TODO: change this if possible to reflect real lines
766 wxString content
= GetStringValue() ;
770 for (size_t i
= 0; i
< content
.length() ; i
++)
774 // Count chars in line then
776 for (size_t j
= i
; j
< content
.length(); j
++)
778 if (content
[j
] == '\n')
787 if (content
[i
] == '\n')
794 #endif // wxUSE_TEXTCTRL