X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9ab7ff537d1ead0f0b07c14841474203c7fa59ba..71a09c3579dd5cb4cd8fa7fdc143561cbff74e12:/src/osx/textctrl_osx.cpp diff --git a/src/osx/textctrl_osx.cpp b/src/osx/textctrl_osx.cpp index ae3328808a..dcfc0e15b4 100644 --- a/src/osx/textctrl_osx.cpp +++ b/src/osx/textctrl_osx.cpp @@ -4,7 +4,6 @@ // Author: Stefan Csomor // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText) // Created: 1998-01-01 -// RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $ // Copyright: (c) Stefan Csomor // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -48,11 +47,10 @@ #include "wx/osx/private.h" -IMPLEMENT_DYNAMIC_CLASS(wxTextCtrl, wxTextCtrlBase) - BEGIN_EVENT_TABLE(wxTextCtrl, wxTextCtrlBase) EVT_DROP_FILES(wxTextCtrl::OnDropFiles) EVT_CHAR(wxTextCtrl::OnChar) + EVT_KEY_DOWN(wxTextCtrl::OnKeyDown) EVT_MENU(wxID_CUT, wxTextCtrl::OnCut) EVT_MENU(wxID_COPY, wxTextCtrl::OnCopy) EVT_MENU(wxID_PASTE, wxTextCtrl::OnPaste) @@ -75,12 +73,9 @@ END_EVENT_TABLE() void wxTextCtrl::Init() { - m_editable = true ; m_dirty = false; - m_maxLength = 0; m_privateContextMenu = NULL; - m_triggerUpdateEvents = true ; } wxTextCtrl::~wxTextCtrl() @@ -99,7 +94,7 @@ bool wxTextCtrl::Create( wxWindow *parent, const wxValidator& validator, const wxString& name ) { - m_macIsUserPane = false ; + DontCreatePeer(); m_editable = true ; if ( ! (style & wxNO_BORDER) ) @@ -116,16 +111,16 @@ bool wxTextCtrl::Create( wxWindow *parent, } - m_peer = wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str, pos, size, style, GetExtraStyle() ); + SetPeer(wxWidgetImpl::CreateTextControl( this, GetParent(), GetId(), str, pos, size, style, GetExtraStyle() )); MacPostControlCreate(pos, size) ; - + #if wxOSX_USE_COCOA // under carbon everything can already be set before the MacPostControlCreate embedding takes place // but under cocoa for single line textfields this only works after everything has been set up GetTextPeer()->SetStringValue(str); #endif - + // only now the embedding is correct and we can do a positioning update MacSuperChangedPosition() ; @@ -138,11 +133,6 @@ bool wxTextCtrl::Create( wxWindow *parent, return true; } -wxTextWidgetImpl* wxTextCtrl::GetTextPeer() const -{ - return dynamic_cast (m_peer); -} - void wxTextCtrl::MacSuperChangedPosition() { wxWindow::MacSuperChangedPosition() ; @@ -163,21 +153,6 @@ void wxTextCtrl::MacCheckSpelling(bool check) GetTextPeer()->CheckSpelling(check); } -wxString wxTextCtrl::DoGetValue() const -{ - return GetTextPeer()->GetStringValue() ; -} - -void wxTextCtrl::GetSelection(long* from, long* to) const -{ - GetTextPeer()->GetSelection( from , to ) ; -} - -void wxTextCtrl::SetMaxLength(unsigned long len) -{ - m_maxLength = len ; -} - bool wxTextCtrl::SetFont( const wxFont& font ) { if ( !wxTextCtrlBase::SetFont( font ) ) @@ -204,132 +179,11 @@ bool wxTextCtrl::SetDefaultStyle(const wxTextAttr& style) return true ; } -// Clipboard operations - -void wxTextCtrl::Copy() -{ - if (CanCopy()) - GetTextPeer()->Copy() ; -} - -void wxTextCtrl::Cut() -{ - if (CanCut()) - { - GetTextPeer()->Cut() ; - - SendTextUpdatedEvent(); - } -} - -void wxTextCtrl::Paste() -{ - if (CanPaste()) - { - GetTextPeer()->Paste() ; - - // TODO: eventually we should add setting the default style again - SendTextUpdatedEvent(); - } -} - -bool wxTextCtrl::CanCopy() const -{ - // Can copy if there's a selection - long from, to; - GetSelection( &from, &to ); - - return (from != to); -} - -bool wxTextCtrl::CanCut() const -{ - if ( !IsEditable() ) - return false; - - // Can cut if there's a selection - long from, to; - GetSelection( &from, &to ); - - return (from != to); -} - -bool wxTextCtrl::CanPaste() const -{ - if (!IsEditable()) - return false; - - return GetTextPeer()->CanPaste() ; -} - -void wxTextCtrl::SetEditable(bool editable) -{ - if ( editable != m_editable ) - { - m_editable = editable ; - GetTextPeer()->SetEditable( editable ) ; - } -} - -void wxTextCtrl::SetInsertionPoint(long pos) -{ - SetSelection( pos , pos ) ; -} - -void wxTextCtrl::SetInsertionPointEnd() -{ - long pos = GetLastPosition(); - SetInsertionPoint( pos ); -} - -long wxTextCtrl::GetInsertionPoint() const -{ - long begin, end ; - GetSelection( &begin , &end ) ; - - return begin ; -} - -wxTextPos wxTextCtrl::GetLastPosition() const -{ - return GetTextPeer()->GetLastPosition() ; -} - -void wxTextCtrl::Remove(long from, long to) -{ - GetTextPeer()->Remove( from , to ) ; - if ( m_triggerUpdateEvents ) - SendTextUpdatedEvent(); -} - -void wxTextCtrl::SetSelection(long from, long to) -{ - GetTextPeer()->SetSelection( from , to ) ; -} - -void wxTextCtrl::WriteText(const wxString& str) -{ - GetTextPeer()->WriteText( str ) ; - if ( m_triggerUpdateEvents ) - SendTextUpdatedEvent(); -} - -void wxTextCtrl::Clear() -{ - GetTextPeer()->Clear() ; - SendTextUpdatedEvent(); -} - bool wxTextCtrl::IsModified() const { return m_dirty; } -bool wxTextCtrl::IsEditable() const -{ - return IsEnabled() && m_editable ; -} - bool wxTextCtrl::AcceptsFocus() const { // we don't want focus if we can't be edited @@ -344,7 +198,7 @@ wxSize wxTextCtrl::DoGetBestSize() const if (size.x > 0 && size.y > 0) return size; } - + int wText, hText; // these are the numbers from the HIG: @@ -386,38 +240,6 @@ bool wxTextCtrl::GetStyle(long position, wxTextAttr& style) return GetTextPeer()->GetStyle(position, style); } -// ---------------------------------------------------------------------------- -// Undo/redo -// ---------------------------------------------------------------------------- - -void wxTextCtrl::Undo() -{ - if (CanUndo()) - GetTextPeer()->Undo() ; -} - -void wxTextCtrl::Redo() -{ - if (CanRedo()) - GetTextPeer()->Redo() ; -} - -bool wxTextCtrl::CanUndo() const -{ - if ( !IsEditable() ) - return false ; - - return GetTextPeer()->CanUndo() ; -} - -bool wxTextCtrl::CanRedo() const -{ - if ( !IsEditable() ) - return false ; - - return GetTextPeer()->CanRedo() ; -} - void wxTextCtrl::MarkDirty() { m_dirty = true; @@ -458,10 +280,48 @@ wxString wxTextCtrl::GetLineText(long lineNo) const return GetTextPeer()->GetLineText(lineNo) ; } -void wxTextCtrl::Command(wxCommandEvent & event) +void wxTextCtrl::Copy() { - SetValue(event.GetString()); - ProcessCommand(event); + if (CanCopy()) + { + wxClipboardTextEvent evt(wxEVT_TEXT_COPY, GetId()); + evt.SetEventObject(this); + if (!GetEventHandler()->ProcessEvent(evt)) + { + wxTextEntry::Copy(); + } + } +} + +void wxTextCtrl::Cut() +{ + if (CanCut()) + { + wxClipboardTextEvent evt(wxEVT_TEXT_CUT, GetId()); + evt.SetEventObject(this); + if (!GetEventHandler()->ProcessEvent(evt)) + { + wxTextEntry::Cut(); + + SendTextUpdatedEvent(); + } + } +} + +void wxTextCtrl::Paste() +{ + if (CanPaste()) + { + wxClipboardTextEvent evt(wxEVT_TEXT_PASTE, GetId()); + evt.SetEventObject(this); + if (!GetEventHandler()->ProcessEvent(evt)) + { + wxTextEntry::Paste(); + + // TODO: eventually we should add setting the default style again + SendTextUpdatedEvent(); + } + } } void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) @@ -471,27 +331,41 @@ void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event) LoadFile( event.GetFiles()[0] ); } +void wxTextCtrl::OnKeyDown(wxKeyEvent& event) +{ + if ( event.GetModifiers() == wxMOD_CONTROL ) + { + switch( event.GetKeyCode() ) + { + case 'A': + SelectAll(); + return; + case 'C': + if ( CanCopy() ) + Copy() ; + return; + case 'V': + if ( CanPaste() ) + Paste() ; + return; + case 'X': + if ( CanCut() ) + Cut() ; + return; + default: + break; + } + } + // no, we didn't process it + event.Skip(); +} + void wxTextCtrl::OnChar(wxKeyEvent& event) { int key = event.GetKeyCode() ; bool eat_key = false ; long from, to; - if ( key == 'a' && event.MetaDown() ) - { - SelectAll() ; - - return ; - } - - if ( key == 'c' && event.MetaDown() ) - { - if ( CanCopy() ) - Copy() ; - - return ; - } - if ( !IsEditable() && !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB) && !( key == WXK_RETURN && ( (m_windowStyle & wxTE_PROCESS_ENTER) || (m_windowStyle & wxTE_MULTILINE) ) ) // && key != WXK_PAGEUP && key != WXK_PAGEDOWN && key != WXK_HOME && key != WXK_END @@ -501,45 +375,32 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) return ; } - // Check if we have reached the max # of chars (if it is set), but still - // allow navigation and deletion - GetSelection( &from, &to ); - if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength && - !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB | WXK_CATEGORY_CUT) && - !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) && - from == to ) + if ( !GetTextPeer()->CanClipMaxLength() ) { - // eat it, we don't want to add more than allowed # of characters + // Check if we have reached the max # of chars (if it is set), but still + // allow navigation and deletion + GetSelection( &from, &to ); + if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength && + !event.IsKeyInCategory(WXK_CATEGORY_ARROW | WXK_CATEGORY_TAB | WXK_CATEGORY_CUT) && + !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) && + from == to ) + { + // eat it, we don't want to add more than allowed # of characters - // TODO: generate EVT_TEXT_MAXLEN() - return; + // TODO: generate EVT_TEXT_MAXLEN() + return; + } } // assume that any key not processed yet is going to modify the control m_dirty = true; - if ( key == 'v' && event.MetaDown() ) - { - if ( CanPaste() ) - Paste() ; - - return ; - } - - if ( key == 'x' && event.MetaDown() ) - { - if ( CanCut() ) - Cut() ; - - return ; - } - switch ( key ) { case WXK_RETURN: if (m_windowStyle & wxTE_PROCESS_ENTER) { - wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId); + wxCommandEvent event(wxEVT_TEXT_ENTER, m_windowId); event.SetEventObject( this ); event.SetString( GetValue() ); if ( HandleWindowEvent(event) ) @@ -554,7 +415,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton); if ( def && def->IsEnabled() ) { - wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() ); + wxCommandEvent event(wxEVT_BUTTON, def->GetId() ); event.SetEventObject(def); def->Command(event); @@ -607,17 +468,25 @@ void wxTextCtrl::OnChar(wxKeyEvent& event) key == WXK_DELETE || key == WXK_BACK) { - wxCommandEvent event1(wxEVT_COMMAND_TEXT_UPDATED, m_windowId); + wxCommandEvent event1(wxEVT_TEXT, m_windowId); event1.SetEventObject( this ); wxPostEvent( GetEventHandler(), event1 ); } #endif } +void wxTextCtrl::Command(wxCommandEvent & event) +{ + SetValue(event.GetString()); + ProcessCommand(event); +} + // ---------------------------------------------------------------------------- // standard handlers for standard edit menu events // ---------------------------------------------------------------------------- +// CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment + void wxTextCtrl::OnCut(wxCommandEvent& WXUNUSED(event)) { Cut(); @@ -695,8 +564,6 @@ void wxTextCtrl::OnUpdateSelectAll(wxUpdateUIEvent& event) event.Enable(GetLastPosition() > 0); } -// CS: Context Menus only work with MLTE implementations or non-multiline HIViews at the moment - void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event) { if ( GetTextPeer()->HasOwnContextMenu() ) @@ -720,8 +587,7 @@ void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event) m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All")); } - if (m_privateContextMenu != NULL) - PopupMenu(m_privateContextMenu); + PopupMenu(m_privateContextMenu); #endif } @@ -733,6 +599,21 @@ bool wxTextCtrl::MacSetupCursor( const wxPoint& pt ) return true ; } +bool wxTextCtrl::SetHint(const wxString& hint) +{ + m_hintString = hint; + + if ( GetTextPeer() && GetTextPeer()->SetHint(hint) ) + return true; + + return false; +} + +wxString wxTextCtrl::GetHint() const +{ + return m_hintString; +} + // ---------------------------------------------------------------------------- // implementation base class // ---------------------------------------------------------------------------- @@ -828,9 +709,8 @@ void wxTextWidgetImpl::ShowPosition( long WXUNUSED(pos) ) int wxTextWidgetImpl::GetNumberOfLines() const { - ItemCount lines = 0 ; wxString content = GetStringValue() ; - lines = 1; + ItemCount lines = 1; for (size_t i = 0; i < content.length() ; i++) { @@ -892,9 +772,10 @@ int wxTextWidgetImpl::GetLineLength(long lineNo) const count = 0; for (size_t j = i; j < content.length(); j++) { - count++; if (content[j] == '\n') return count; + + count++; } return count;