]>
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)
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
15 #include "wx/textctrl.h"
22 #include "wx/button.h"
24 #include "wx/settings.h"
25 #include "wx/msgdlg.h"
26 #include "wx/toplevel.h"
30 #include <sys/types.h>
36 #if wxUSE_STD_IOSTREAM
44 #include "wx/filefn.h"
45 #include "wx/sysopt.h"
46 #include "wx/thread.h"
48 #include "wx/osx/private.h"
50 BEGIN_EVENT_TABLE(wxTextCtrl
, wxTextCtrlBase
)
51 EVT_DROP_FILES(wxTextCtrl::OnDropFiles
)
52 EVT_CHAR(wxTextCtrl::OnChar
)
53 EVT_KEY_DOWN(wxTextCtrl::OnKeyDown
)
54 EVT_MENU(wxID_CUT
, wxTextCtrl::OnCut
)
55 EVT_MENU(wxID_COPY
, wxTextCtrl::OnCopy
)
56 EVT_MENU(wxID_PASTE
, wxTextCtrl::OnPaste
)
57 EVT_MENU(wxID_UNDO
, wxTextCtrl::OnUndo
)
58 EVT_MENU(wxID_REDO
, wxTextCtrl::OnRedo
)
59 EVT_MENU(wxID_CLEAR
, wxTextCtrl::OnDelete
)
60 EVT_MENU(wxID_SELECTALL
, wxTextCtrl::OnSelectAll
)
62 EVT_CONTEXT_MENU(wxTextCtrl::OnContextMenu
)
64 EVT_UPDATE_UI(wxID_CUT
, wxTextCtrl::OnUpdateCut
)
65 EVT_UPDATE_UI(wxID_COPY
, wxTextCtrl::OnUpdateCopy
)
66 EVT_UPDATE_UI(wxID_PASTE
, wxTextCtrl::OnUpdatePaste
)
67 EVT_UPDATE_UI(wxID_UNDO
, wxTextCtrl::OnUpdateUndo
)
68 EVT_UPDATE_UI(wxID_REDO
, wxTextCtrl::OnUpdateRedo
)
69 EVT_UPDATE_UI(wxID_CLEAR
, wxTextCtrl::OnUpdateDelete
)
70 EVT_UPDATE_UI(wxID_SELECTALL
, wxTextCtrl::OnUpdateSelectAll
)
74 void wxTextCtrl::Init()
78 m_privateContextMenu
= NULL
;
81 wxTextCtrl::~wxTextCtrl()
84 delete m_privateContextMenu
;
88 bool wxTextCtrl::Create( wxWindow
*parent
,
94 const wxValidator
& validator
,
95 const wxString
& name
)
100 if ( ! (style
& wxNO_BORDER
) )
101 style
= (style
& ~wxBORDER_MASK
) | wxSUNKEN_BORDER
;
103 if ( !wxTextCtrlBase::Create( parent
, id
, pos
, size
, style
& ~(wxHSCROLL
| wxVSCROLL
), validator
, name
) )
106 if ( m_windowStyle
& wxTE_MULTILINE
)
108 // always turn on this style for multi-line controls
109 m_windowStyle
|= wxTE_PROCESS_ENTER
;
110 style
|= wxTE_PROCESS_ENTER
;
114 SetPeer(wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str
, pos
, size
, style
, GetExtraStyle() ));
116 MacPostControlCreate(pos
, size
) ;
119 // under carbon everything can already be set before the MacPostControlCreate embedding takes place
120 // but under cocoa for single line textfields this only works after everything has been set up
121 GetTextPeer()->SetStringValue(str
);
124 // only now the embedding is correct and we can do a positioning update
126 MacSuperChangedPosition() ;
128 if ( m_windowStyle
& wxTE_READONLY
)
129 SetEditable( false ) ;
131 SetCursor( wxCursor( wxCURSOR_IBEAM
) ) ;
136 void wxTextCtrl::MacSuperChangedPosition()
138 wxWindow::MacSuperChangedPosition() ;
140 GetPeer()->SuperChangedPosition() ;
144 void wxTextCtrl::MacVisibilityChanged()
147 GetPeer()->VisibilityChanged( GetPeer()->IsVisible() );
151 void wxTextCtrl::MacCheckSpelling(bool check
)
153 GetTextPeer()->CheckSpelling(check
);
156 bool wxTextCtrl::SetFont( const wxFont
& font
)
158 if ( !wxTextCtrlBase::SetFont( font
) )
161 GetPeer()->SetFont( font
, GetForegroundColour() , GetWindowStyle(), false /* dont ignore black */ ) ;
166 bool wxTextCtrl::SetStyle(long start
, long end
, const wxTextAttr
& style
)
169 GetTextPeer()->SetStyle( start
, end
, style
) ;
174 bool wxTextCtrl::SetDefaultStyle(const wxTextAttr
& style
)
176 wxTextCtrlBase::SetDefaultStyle( style
) ;
177 SetStyle( -1 /*current selection*/ , -1 /*current selection*/ , GetDefaultStyle() ) ;
182 bool wxTextCtrl::IsModified() const
187 bool wxTextCtrl::AcceptsFocus() const
189 // we don't want focus if we can't be edited
190 return /*IsEditable() && */ wxControl::AcceptsFocus();
193 wxSize
wxTextCtrl::DoGetBestSize() const
197 wxSize size
= GetTextPeer()->GetBestSize();
198 if (size
.x
> 0 && size
.y
> 0)
204 // these are the numbers from the HIG:
205 // we reduce them by the borders first
208 switch ( m_windowVariant
)
210 case wxWINDOW_VARIANT_NORMAL
:
214 case wxWINDOW_VARIANT_SMALL
:
218 case wxWINDOW_VARIANT_MINI
:
227 // as the above numbers have some free space around the text
228 // we get 5 lines like this anyway
229 if ( m_windowStyle
& wxTE_MULTILINE
)
232 if ( !HasFlag(wxNO_BORDER
) )
235 return wxSize(wText
, hText
);
238 bool wxTextCtrl::GetStyle(long position
, wxTextAttr
& style
)
240 return GetTextPeer()->GetStyle(position
, style
);
243 void wxTextCtrl::MarkDirty()
248 void wxTextCtrl::DiscardEdits()
253 int wxTextCtrl::GetNumberOfLines() const
255 return GetTextPeer()->GetNumberOfLines() ;
258 long wxTextCtrl::XYToPosition(long x
, long y
) const
260 return GetTextPeer()->XYToPosition( x
, y
) ;
263 bool wxTextCtrl::PositionToXY(long pos
, long *x
, long *y
) const
265 return GetTextPeer()->PositionToXY( pos
, x
, y
) ;
268 void wxTextCtrl::ShowPosition(long pos
)
270 return GetTextPeer()->ShowPosition(pos
) ;
273 int wxTextCtrl::GetLineLength(long lineNo
) const
275 return GetTextPeer()->GetLineLength(lineNo
) ;
278 wxString
wxTextCtrl::GetLineText(long lineNo
) const
280 return GetTextPeer()->GetLineText(lineNo
) ;
283 void wxTextCtrl::Copy()
287 wxClipboardTextEvent
evt(wxEVT_TEXT_COPY
, GetId());
288 evt
.SetEventObject(this);
289 if (!GetEventHandler()->ProcessEvent(evt
))
296 void wxTextCtrl::Cut()
300 wxClipboardTextEvent
evt(wxEVT_TEXT_CUT
, GetId());
301 evt
.SetEventObject(this);
302 if (!GetEventHandler()->ProcessEvent(evt
))
306 SendTextUpdatedEvent();
311 void wxTextCtrl::Paste()
315 wxClipboardTextEvent
evt(wxEVT_TEXT_PASTE
, GetId());
316 evt
.SetEventObject(this);
317 if (!GetEventHandler()->ProcessEvent(evt
))
319 wxTextEntry::Paste();
321 // TODO: eventually we should add setting the default style again
322 SendTextUpdatedEvent();
327 void wxTextCtrl::OnDropFiles(wxDropFilesEvent
& event
)
329 // By default, load the first file into the text window.
330 if (event
.GetNumberOfFiles() > 0)
331 LoadFile( event
.GetFiles()[0] );
334 void wxTextCtrl::OnKeyDown(wxKeyEvent
& event
)
336 if ( event
.GetModifiers() == wxMOD_CONTROL
)
338 switch( event
.GetKeyCode() )
359 // no, we didn't process it
363 void wxTextCtrl::OnChar(wxKeyEvent
& event
)
365 int key
= event
.GetKeyCode() ;
366 bool eat_key
= false ;
369 if ( !IsEditable() && !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
) &&
370 !( key
== WXK_RETURN
&& ( (m_windowStyle
& wxTE_PROCESS_ENTER
) || (m_windowStyle
& wxTE_MULTILINE
) ) )
371 // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END
378 if ( !GetTextPeer()->CanClipMaxLength() )
380 // Check if we have reached the max # of chars (if it is set), but still
381 // allow navigation and deletion
382 GetSelection( &from
, &to
);
383 if ( !IsMultiLine() && m_maxLength
&& GetValue().length() >= m_maxLength
&&
384 !event
.IsKeyInCategory(WXK_CATEGORY_ARROW
| WXK_CATEGORY_TAB
| WXK_CATEGORY_CUT
) &&
385 !( key
== WXK_RETURN
&& (m_windowStyle
& wxTE_PROCESS_ENTER
) ) &&
388 // eat it, we don't want to add more than allowed # of characters
390 // TODO: generate EVT_TEXT_MAXLEN()
395 // assume that any key not processed yet is going to modify the control
401 if (m_windowStyle
& wxTE_PROCESS_ENTER
)
403 wxCommandEvent
event(wxEVT_TEXT_ENTER
, m_windowId
);
404 event
.SetEventObject( this );
405 event
.SetString( GetValue() );
406 if ( HandleWindowEvent(event
) )
410 if ( !(m_windowStyle
& wxTE_MULTILINE
) )
412 wxTopLevelWindow
*tlw
= wxDynamicCast(wxGetTopLevelParent(this), wxTopLevelWindow
);
413 if ( tlw
&& tlw
->GetDefaultItem() )
415 wxButton
*def
= wxDynamicCast(tlw
->GetDefaultItem(), wxButton
);
416 if ( def
&& def
->IsEnabled() )
418 wxCommandEvent
event(wxEVT_BUTTON
, def
->GetId() );
419 event
.SetEventObject(def
);
426 // this will make wxWidgets eat the ENTER key so that
427 // we actually prevent line wrapping in a single line text control
433 if ( !(m_windowStyle
& wxTE_PROCESS_TAB
))
436 if (!event
.ShiftDown())
437 flags
|= wxNavigationKeyEvent::IsForward
;
438 if (event
.ControlDown())
439 flags
|= wxNavigationKeyEvent::WinChange
;
446 // This is necessary (don't know why);
447 // otherwise the tab will not be inserted.
448 WriteText(wxT("\t"));
459 // perform keystroke handling
463 // osx_cocoa sends its event upon insertText
465 if ( ( key
>= 0x20 && key
< WXK_START
) ||
466 ( key
>= WXK_NUMPAD0
&& key
<= WXK_DIVIDE
) ||
471 wxCommandEvent
event1(wxEVT_TEXT
, m_windowId
);
472 event1
.SetEventObject( this );
473 wxPostEvent( GetEventHandler(), event1
);
478 void wxTextCtrl::Command(wxCommandEvent
& event
)
480 SetValue(event
.GetString());
481 ProcessCommand(event
);
484 // ----------------------------------------------------------------------------
485 // standard handlers for standard edit menu events
486 // ----------------------------------------------------------------------------
488 // CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment
490 void wxTextCtrl::OnCut(wxCommandEvent
& WXUNUSED(event
))
495 void wxTextCtrl::OnCopy(wxCommandEvent
& WXUNUSED(event
))
500 void wxTextCtrl::OnPaste(wxCommandEvent
& WXUNUSED(event
))
505 void wxTextCtrl::OnUndo(wxCommandEvent
& WXUNUSED(event
))
510 void wxTextCtrl::OnRedo(wxCommandEvent
& WXUNUSED(event
))
515 void wxTextCtrl::OnDelete(wxCommandEvent
& WXUNUSED(event
))
519 GetSelection( &from
, &to
);
520 if (from
!= -1 && to
!= -1)
524 void wxTextCtrl::OnSelectAll(wxCommandEvent
& WXUNUSED(event
))
526 SetSelection(-1, -1);
529 void wxTextCtrl::OnUpdateCut(wxUpdateUIEvent
& event
)
531 event
.Enable( CanCut() );
534 void wxTextCtrl::OnUpdateCopy(wxUpdateUIEvent
& event
)
536 event
.Enable( CanCopy() );
539 void wxTextCtrl::OnUpdatePaste(wxUpdateUIEvent
& event
)
541 event
.Enable( CanPaste() );
544 void wxTextCtrl::OnUpdateUndo(wxUpdateUIEvent
& event
)
546 event
.Enable( CanUndo() );
549 void wxTextCtrl::OnUpdateRedo(wxUpdateUIEvent
& event
)
551 event
.Enable( CanRedo() );
554 void wxTextCtrl::OnUpdateDelete(wxUpdateUIEvent
& event
)
558 GetSelection( &from
, &to
);
559 event
.Enable( from
!= -1 && to
!= -1 && from
!= to
&& IsEditable() ) ;
562 void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent
& event
)
564 event
.Enable(GetLastPosition() > 0);
567 void wxTextCtrl::OnContextMenu(wxContextMenuEvent
& event
)
569 if ( GetTextPeer()->HasOwnContextMenu() )
576 if (m_privateContextMenu
== NULL
)
578 m_privateContextMenu
= new wxMenu
;
579 m_privateContextMenu
->Append(wxID_UNDO
, _("&Undo"));
580 m_privateContextMenu
->Append(wxID_REDO
, _("&Redo"));
581 m_privateContextMenu
->AppendSeparator();
582 m_privateContextMenu
->Append(wxID_CUT
, _("Cu&t"));
583 m_privateContextMenu
->Append(wxID_COPY
, _("&Copy"));
584 m_privateContextMenu
->Append(wxID_PASTE
, _("&Paste"));
585 m_privateContextMenu
->Append(wxID_CLEAR
, _("&Delete"));
586 m_privateContextMenu
->AppendSeparator();
587 m_privateContextMenu
->Append(wxID_SELECTALL
, _("Select &All"));
590 PopupMenu(m_privateContextMenu
);
594 bool wxTextCtrl::MacSetupCursor( const wxPoint
& pt
)
596 if ( !GetTextPeer()->SetupCursor( pt
) )
597 return wxWindow::MacSetupCursor( pt
) ;
602 bool wxTextCtrl::SetHint(const wxString
& hint
)
606 if ( GetTextPeer() && GetTextPeer()->SetHint(hint
) )
612 wxString
wxTextCtrl::GetHint() const
617 // ----------------------------------------------------------------------------
618 // implementation base class
619 // ----------------------------------------------------------------------------
621 bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position
),
622 wxTextAttr
& WXUNUSED(style
))
627 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start
),
629 const wxTextAttr
& WXUNUSED(style
))
633 void wxTextWidgetImpl::Copy()
637 void wxTextWidgetImpl::Cut()
641 void wxTextWidgetImpl::Paste()
645 bool wxTextWidgetImpl::CanPaste() const
650 void wxTextWidgetImpl::SetEditable(bool WXUNUSED(editable
))
654 long wxTextWidgetImpl::GetLastPosition() const
656 return GetStringValue().length() ;
659 void wxTextWidgetImpl::Replace( long from
, long to
, const wxString
&val
)
661 SetSelection( from
, to
) ;
665 void wxTextWidgetImpl::Remove( long from
, long to
)
667 SetSelection( from
, to
) ;
668 WriteText( wxEmptyString
) ;
671 void wxTextWidgetImpl::Clear()
673 SetStringValue( wxEmptyString
) ;
676 bool wxTextWidgetImpl::CanUndo() const
681 void wxTextWidgetImpl::Undo()
685 bool wxTextWidgetImpl::CanRedo() const
690 void wxTextWidgetImpl::Redo()
694 long wxTextWidgetImpl::XYToPosition(long WXUNUSED(x
), long WXUNUSED(y
)) const
699 bool wxTextWidgetImpl::PositionToXY(long WXUNUSED(pos
),
701 long *WXUNUSED(y
)) const
706 void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos
) )
710 int wxTextWidgetImpl::GetNumberOfLines() const
712 wxString content
= GetStringValue() ;
715 for (size_t i
= 0; i
< content
.length() ; i
++)
718 if (content
[i
] == '\n')
720 if (content
[i
] == '\r')
728 wxString
wxTextWidgetImpl::GetLineText(long lineNo
) const
730 // TODO: change this if possible to reflect real lines
731 wxString content
= GetStringValue() ;
735 for (size_t i
= 0; i
< content
.length() ; i
++)
739 // Add chars in line then
742 for (size_t j
= i
; j
< content
.length(); j
++)
744 if (content
[j
] == '\n')
753 if (content
[i
] == '\n')
757 return wxEmptyString
;
760 int wxTextWidgetImpl::GetLineLength(long lineNo
) const
762 // TODO: change this if possible to reflect real lines
763 wxString content
= GetStringValue() ;
767 for (size_t i
= 0; i
< content
.length() ; i
++)
771 // Count chars in line then
773 for (size_t j
= i
; j
< content
.length(); j
++)
775 if (content
[j
] == '\n')
784 if (content
[i
] == '\n')
791 #endif // wxUSE_TEXTCTRL