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 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 void wxTextCtrl::SetMaxLength(unsigned long len
)
165 bool wxTextCtrl::SetFont( const wxFont
& font
)
167 if ( !wxTextCtrlBase::SetFont( font
) )
170 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
175 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
178 GetTextPeer()->SetStyle( start
, end
, style
) ;
183 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
185 wxTextCtrlBase::SetDefaultStyle( style
) ;
186 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
191 bool wxTextCtrl::IsModified() const
196 bool wxTextCtrl::AcceptsFocus() const
198 // we don't want focus if we can't be edited
199 return /*IsEditable() && */ wxControl::AcceptsFocus();
202 wxSize
wxTextCtrl::DoGetBestSize() const
206 wxSize size
= GetTextPeer()->GetBestSize();
207 if (size
.x
> 0 && size
.y
> 0)
213 // these are the numbers from the HIG:
214 // we reduce them by the borders first
217 switch ( m_windowVariant
)
219 case wxWINDOW_VARIANT_NORMAL
:
223 case wxWINDOW_VARIANT_SMALL
:
227 case wxWINDOW_VARIANT_MINI
:
236 // as the above numbers have some free space around the text
237 // we get 5 lines like this anyway
238 if ( m_windowStyle
& wxTE_MULTILINE
)
241 if ( !HasFlag(wxNO_BORDER
) )
244 return wxSize(wText
, hText
);
247 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
249 return GetTextPeer()->GetStyle(position
, style
);
252 void wxTextCtrl::MarkDirty()
257 void wxTextCtrl::DiscardEdits()
262 int wxTextCtrl::GetNumberOfLines() const
264 return GetTextPeer()->GetNumberOfLines() ;
267 long wxTextCtrl::XYToPosition(long x
, long y
) const
269 return GetTextPeer()->XYToPosition( x
, y
) ;
272 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
274 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
277 void wxTextCtrl::ShowPosition(long pos
)
279 return GetTextPeer()->ShowPosition(pos
) ;
282 int wxTextCtrl::GetLineLength(long lineNo
) const
284 return GetTextPeer()->GetLineLength(lineNo
) ;
287 wxString
wxTextCtrl::GetLineText(long lineNo
) const
289 return GetTextPeer()->GetLineText(lineNo
) ;
292 void wxTextCtrl::Remove(long from
, long to
)
294 wxTextEntry::Remove(from
, to
);
295 if ( m_triggerUpdateEvents
)
296 SendTextUpdatedEvent();
299 void wxTextCtrl::WriteText(const wxString
& str
)
301 wxTextEntry::WriteText( str
) ;
302 if ( m_triggerUpdateEvents
)
303 SendTextUpdatedEvent();
306 void wxTextCtrl::Clear()
308 wxTextEntry::Clear() ;
309 SendTextUpdatedEvent();
312 void wxTextCtrl::Copy()
316 wxClipboardTextEvent
evt(wxEVT_COMMAND_TEXT_COPY
, GetId());
317 evt
.SetEventObject(this);
318 if (!GetEventHandler()->ProcessEvent(evt
))
325 void wxTextCtrl::Cut()
329 wxClipboardTextEvent
evt(wxEVT_COMMAND_TEXT_CUT
, GetId());
330 evt
.SetEventObject(this);
331 if (!GetEventHandler()->ProcessEvent(evt
))
335 SendTextUpdatedEvent();
340 void wxTextCtrl::Paste()
344 wxClipboardTextEvent
evt(wxEVT_COMMAND_TEXT_PASTE
, GetId());
345 evt
.SetEventObject(this);
346 if (!GetEventHandler()->ProcessEvent(evt
))
348 wxTextEntry::Paste();
350 // TODO: eventually we should add setting the default style again
351 SendTextUpdatedEvent();
356 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
358 // By default, load the first file into the text window.
359 if (event
.GetNumberOfFiles() > 0)
360 LoadFile( event
.GetFiles()[0] );
363 void wxTextCtrl::OnKeyDown(wxKeyEvent
& event
)
365 if ( event
.GetModifiers() == wxMOD_CMD
)
367 switch( event
.GetKeyCode() )
388 // no, we didn't process it
392 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
394 int key
= event
.GetKeyCode() ;
395 bool eat_key
= false ;
398 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
399 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
400 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
407 // Check if we have reached the max # of chars (if it is set), but still
408 // allow navigation and deletion
409 GetSelection( &from
, &to
);
410 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
411 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
412 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
415 // eat it, we don't want to add more than allowed # of characters
417 // TODO: generate EVT_TEXT_MAXLEN()
421 // assume that any key not processed yet is going to modify the control
427 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
429 wxCommandEvent
event(wxEVT_COMMAND_TEXT_ENTER
, m_windowId
);
430 event
.SetEventObject( this );
431 event
.SetString( GetValue() );
432 if ( HandleWindowEvent(event
) )
436 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
438 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
439 if ( tlw
&& tlw
->GetDefaultItem() )
441 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
442 if ( def
&& def
->IsEnabled() )
444 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, def
->GetId() );
445 event
.SetEventObject(def
);
452 // this will make wxWidgets eat the ENTER key so that
453 // we actually prevent line wrapping in a single line text control
459 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
462 if (!event
.ShiftDown())
463 flags
|= wxNavigationKeyEvent::IsForward
;
464 if (event
.ControlDown())
465 flags
|= wxNavigationKeyEvent::WinChange
;
472 // This is necessary (don't know why);
473 // otherwise the tab will not be inserted.
474 WriteText(wxT("\t"));
485 // perform keystroke handling
489 // osx_cocoa sends its event upon insertText
491 if ( ( key
>= 0x20 && key
< WXK_START
) ||
492 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
497 wxCommandEvent
event1(wxEVT_COMMAND_TEXT_UPDATED
, m_windowId
);
498 event1
.SetEventObject( this );
499 wxPostEvent( GetEventHandler(), event1
);
504 void wxTextCtrl::Command(wxCommandEvent
& event
)
506 SetValue(event
.GetString());
507 ProcessCommand(event
);
510 // ----------------------------------------------------------------------------
511 // standard handlers for standard edit menu events
512 // ----------------------------------------------------------------------------
514 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
516 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
521 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
526 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
531 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
536 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
541 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
545 GetSelection( &from
, &to
);
546 if (from
!= -1 && to
!= -1)
550 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
552 SetSelection(-1, -1);
555 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
557 event
.Enable( CanCut() );
560 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
562 event
.Enable( CanCopy() );
565 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
567 event
.Enable( CanPaste() );
570 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
572 event
.Enable( CanUndo() );
575 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
577 event
.Enable( CanRedo() );
580 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
584 GetSelection( &from
, &to
);
585 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
588 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
590 event
.Enable(GetLastPosition() > 0);
593 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
595 if ( GetTextPeer()->HasOwnContextMenu() )
602 if (m_privateContextMenu
== NULL
)
604 m_privateContextMenu
= new wxMenu
;
605 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
606 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
607 m_privateContextMenu
->AppendSeparator();
608 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
609 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
610 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
611 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
612 m_privateContextMenu
->AppendSeparator();
613 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
616 PopupMenu(m_privateContextMenu
);
620 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
622 if ( !GetTextPeer()->SetupCursor( pt
) )
623 return wxWindow::MacSetupCursor( pt
) ;
628 // ----------------------------------------------------------------------------
629 // implementation base class
630 // ----------------------------------------------------------------------------
632 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
633 wxTextAttr
& WXUNUSED(style
))
638 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
640 const wxTextAttr
& WXUNUSED(style
))
644 void wxTextWidgetImpl::Copy()
648 void wxTextWidgetImpl::Cut()
652 void wxTextWidgetImpl::Paste()
656 bool wxTextWidgetImpl::CanPaste() const
661 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
665 long wxTextWidgetImpl::GetLastPosition() const
667 return GetStringValue().length() ;
670 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
672 SetSelection( from
, to
) ;
676 void wxTextWidgetImpl::Remove( long from
, long to
)
678 SetSelection( from
, to
) ;
679 WriteText( wxEmptyString
) ;
682 void wxTextWidgetImpl::Clear()
684 SetStringValue( wxEmptyString
) ;
687 bool wxTextWidgetImpl::CanUndo() const
692 void wxTextWidgetImpl::Undo()
696 bool wxTextWidgetImpl::CanRedo() const
701 void wxTextWidgetImpl::Redo()
705 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
710 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
712 long *WXUNUSED(y
)) const
717 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
721 int wxTextWidgetImpl::GetNumberOfLines() const
723 wxString content
= GetStringValue() ;
726 for (size_t i
= 0; i
< content
.length() ; i
++)
729 if (content
[i
] == '\n')
731 if (content
[i
] == '\r')
739 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
741 // TODO: change this if possible to reflect real lines
742 wxString content
= GetStringValue() ;
746 for (size_t i
= 0; i
< content
.length() ; i
++)
750 // Add chars in line then
753 for (size_t j
= i
; j
< content
.length(); j
++)
755 if (content
[j
] == '\n')
764 if (content
[i
] == '\n')
768 return wxEmptyString
;
771 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
773 // TODO: change this if possible to reflect real lines
774 wxString content
= GetStringValue() ;
778 for (size_t i
= 0; i
< content
.length() ; i
++)
782 // Count chars in line then
784 for (size_t j
= i
; j
< content
.length(); j
++)
787 if (content
[j
] == '\n')
794 if (content
[i
] == '\n')
801 #endif // wxUSE_TEXTCTRL