]>
git.saurik.com Git - wxWidgets.git/blob - src/osx/textctrl_osx.cpp
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 bool wxTextCtrl::SetFont( const wxFont
& font
)
159 if ( !wxTextCtrlBase::SetFont( font
) )
162 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
167 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
170 GetTextPeer()->SetStyle( start
, end
, style
) ;
175 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
177 wxTextCtrlBase::SetDefaultStyle( style
) ;
178 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
183 bool wxTextCtrl::IsModified() const
188 bool wxTextCtrl::AcceptsFocus() const
190 // we don't want focus if we can't be edited
191 return /*IsEditable() && */ wxControl::AcceptsFocus();
194 wxSize
wxTextCtrl::DoGetBestSize() const
198 wxSize size
= GetTextPeer()->GetBestSize();
199 if (size
.x
> 0 && size
.y
> 0)
205 // these are the numbers from the HIG:
206 // we reduce them by the borders first
209 switch ( m_windowVariant
)
211 case wxWINDOW_VARIANT_NORMAL
:
215 case wxWINDOW_VARIANT_SMALL
:
219 case wxWINDOW_VARIANT_MINI
:
228 // as the above numbers have some free space around the text
229 // we get 5 lines like this anyway
230 if ( m_windowStyle
& wxTE_MULTILINE
)
233 if ( !HasFlag(wxNO_BORDER
) )
236 return wxSize(wText
, hText
);
239 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
241 return GetTextPeer()->GetStyle(position
, style
);
244 void wxTextCtrl::MarkDirty()
249 void wxTextCtrl::DiscardEdits()
254 int wxTextCtrl::GetNumberOfLines() const
256 return GetTextPeer()->GetNumberOfLines() ;
259 long wxTextCtrl::XYToPosition(long x
, long y
) const
261 return GetTextPeer()->XYToPosition( x
, y
) ;
264 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
266 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
269 void wxTextCtrl::ShowPosition(long pos
)
271 return GetTextPeer()->ShowPosition(pos
) ;
274 int wxTextCtrl::GetLineLength(long lineNo
) const
276 return GetTextPeer()->GetLineLength(lineNo
) ;
279 wxString
wxTextCtrl::GetLineText(long lineNo
) const
281 return GetTextPeer()->GetLineText(lineNo
) ;
284 void wxTextCtrl::Copy()
288 wxClipboardTextEvent
evt(wxEVT_TEXT_COPY
, GetId());
289 evt
.SetEventObject(this);
290 if (!GetEventHandler()->ProcessEvent(evt
))
297 void wxTextCtrl::Cut()
301 wxClipboardTextEvent
evt(wxEVT_TEXT_CUT
, GetId());
302 evt
.SetEventObject(this);
303 if (!GetEventHandler()->ProcessEvent(evt
))
307 SendTextUpdatedEvent();
312 void wxTextCtrl::Paste()
316 wxClipboardTextEvent
evt(wxEVT_TEXT_PASTE
, GetId());
317 evt
.SetEventObject(this);
318 if (!GetEventHandler()->ProcessEvent(evt
))
320 wxTextEntry::Paste();
322 // TODO: eventually we should add setting the default style again
323 SendTextUpdatedEvent();
328 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
330 // By default, load the first file into the text window.
331 if (event
.GetNumberOfFiles() > 0)
332 LoadFile( event
.GetFiles()[0] );
335 void wxTextCtrl::OnKeyDown(wxKeyEvent
& event
)
337 if ( event
.GetModifiers() == wxMOD_CONTROL
)
339 switch( event
.GetKeyCode() )
360 // no, we didn't process it
364 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
366 int key
= event
.GetKeyCode() ;
367 bool eat_key
= false ;
370 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
371 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
372 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
379 if ( !GetTextPeer()->CanClipMaxLength() )
381 // Check if we have reached the max # of chars (if it is set), but still
382 // allow navigation and deletion
383 GetSelection( &from
, &to
);
384 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
385 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
386 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
389 // eat it, we don't want to add more than allowed # of characters
391 // TODO: generate EVT_TEXT_MAXLEN()
396 // assume that any key not processed yet is going to modify the control
402 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
404 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
405 event
.SetEventObject( this );
406 event
.SetString( GetValue() );
407 if ( HandleWindowEvent(event
) )
411 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
413 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
414 if ( tlw
&& tlw
->GetDefaultItem() )
416 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
417 if ( def
&& def
->IsEnabled() )
419 wxCommandEvent
event(wxEVT_BUTTON
, def
->GetId() );
420 event
.SetEventObject(def
);
427 // this will make wxWidgets eat the ENTER key so that
428 // we actually prevent line wrapping in a single line text control
434 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
437 if (!event
.ShiftDown())
438 flags
|= wxNavigationKeyEvent::IsForward
;
439 if (event
.ControlDown())
440 flags
|= wxNavigationKeyEvent::WinChange
;
447 // This is necessary (don't know why);
448 // otherwise the tab will not be inserted.
449 WriteText(wxT("\t"));
460 // perform keystroke handling
464 // osx_cocoa sends its event upon insertText
466 if ( ( key
>= 0x20 && key
< WXK_START
) ||
467 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
472 wxCommandEvent
event1(wxEVT_TEXT
, m_windowId
);
473 event1
.SetEventObject( this );
474 wxPostEvent( GetEventHandler(), event1
);
479 void wxTextCtrl::Command(wxCommandEvent
& event
)
481 SetValue(event
.GetString());
482 ProcessCommand(event
);
485 // ----------------------------------------------------------------------------
486 // standard handlers for standard edit menu events
487 // ----------------------------------------------------------------------------
489 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
491 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
496 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
501 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
506 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
511 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
516 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
520 GetSelection( &from
, &to
);
521 if (from
!= -1 && to
!= -1)
525 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
527 SetSelection(-1, -1);
530 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
532 event
.Enable( CanCut() );
535 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
537 event
.Enable( CanCopy() );
540 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
542 event
.Enable( CanPaste() );
545 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
547 event
.Enable( CanUndo() );
550 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
552 event
.Enable( CanRedo() );
555 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
559 GetSelection( &from
, &to
);
560 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
563 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
565 event
.Enable(GetLastPosition() > 0);
568 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
570 if ( GetTextPeer()->HasOwnContextMenu() )
577 if (m_privateContextMenu
== NULL
)
579 m_privateContextMenu
= new wxMenu
;
580 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
581 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
582 m_privateContextMenu
->AppendSeparator();
583 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
584 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
585 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
586 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
587 m_privateContextMenu
->AppendSeparator();
588 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
591 PopupMenu(m_privateContextMenu
);
595 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
597 if ( !GetTextPeer()->SetupCursor( pt
) )
598 return wxWindow::MacSetupCursor( pt
) ;
603 bool wxTextCtrl::SetHint(const wxString
& hint
)
607 if ( GetTextPeer() && GetTextPeer()->SetHint(hint
) )
613 wxString
wxTextCtrl::GetHint() const
618 // ----------------------------------------------------------------------------
619 // implementation base class
620 // ----------------------------------------------------------------------------
622 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
623 wxTextAttr
& WXUNUSED(style
))
628 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
630 const wxTextAttr
& WXUNUSED(style
))
634 void wxTextWidgetImpl::Copy()
638 void wxTextWidgetImpl::Cut()
642 void wxTextWidgetImpl::Paste()
646 bool wxTextWidgetImpl::CanPaste() const
651 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
655 long wxTextWidgetImpl::GetLastPosition() const
657 return GetStringValue().length() ;
660 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
662 SetSelection( from
, to
) ;
666 void wxTextWidgetImpl::Remove( long from
, long to
)
668 SetSelection( from
, to
) ;
669 WriteText( wxEmptyString
) ;
672 void wxTextWidgetImpl::Clear()
674 SetStringValue( wxEmptyString
) ;
677 bool wxTextWidgetImpl::CanUndo() const
682 void wxTextWidgetImpl::Undo()
686 bool wxTextWidgetImpl::CanRedo() const
691 void wxTextWidgetImpl::Redo()
695 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
700 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
702 long *WXUNUSED(y
)) const
707 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
711 int wxTextWidgetImpl::GetNumberOfLines() const
713 wxString content
= GetStringValue() ;
716 for (size_t i
= 0; i
< content
.length() ; i
++)
719 if (content
[i
] == '\n')
721 if (content
[i
] == '\r')
729 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
731 // TODO: change this if possible to reflect real lines
732 wxString content
= GetStringValue() ;
736 for (size_t i
= 0; i
< content
.length() ; i
++)
740 // Add chars in line then
743 for (size_t j
= i
; j
< content
.length(); j
++)
745 if (content
[j
] == '\n')
754 if (content
[i
] == '\n')
758 return wxEmptyString
;
761 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
763 // TODO: change this if possible to reflect real lines
764 wxString content
= GetStringValue() ;
768 for (size_t i
= 0; i
< content
.length() ; i
++)
772 // Count chars in line then
774 for (size_t j
= i
; j
< content
.length(); j
++)
776 if (content
[j
] == '\n')
785 if (content
[i
] == '\n')
792 #endif // wxUSE_TEXTCTRL