]> git.saurik.com Git - wxWidgets.git/blobdiff - src/osx/textctrl_osx.cpp
Don't query metrics that won't be used.
[wxWidgets.git] / src / osx / textctrl_osx.cpp
index 9ace17a932626b1c67bed0d13660218152e9d446..5d243607e0c9a0d62156e9370b9d3ed55f3f03e6 100644 (file)
@@ -75,12 +75,10 @@ END_EVENT_TABLE()
 
 void wxTextCtrl::Init()
 {
-    m_editable = true ;
     m_dirty = false;
 
-    m_maxLength = 0;
     m_privateContextMenu = NULL;
-    m_triggerOnSetValue = true ;
+    m_triggerUpdateEvents = true ;
 }
 
 wxTextCtrl::~wxTextCtrl()
@@ -119,7 +117,13 @@ bool wxTextCtrl::Create( wxWindow *parent,
     m_peer = 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() ;
@@ -134,7 +138,7 @@ bool wxTextCtrl::Create( wxWindow *parent,
 
 wxTextWidgetImpl* wxTextCtrl::GetTextPeer() const
 {
-    return dynamic_cast<wxTextWidgetImpl*> (m_peer); 
+    return dynamic_cast<wxTextWidgetImpl*> (m_peer);
 }
 
 void wxTextCtrl::MacSuperChangedPosition()
@@ -157,30 +161,6 @@ void wxTextCtrl::MacCheckSpelling(bool check)
     GetTextPeer()->CheckSpelling(check);
 }
 
-wxString wxTextCtrl::GetValue() const
-{
-    return GetTextPeer()->GetStringValue() ;
-}
-
-void wxTextCtrl::GetSelection(long* from, long* to) const
-{
-    GetTextPeer()->GetSelection( from , to ) ;
-}
-
-void wxTextCtrl::DoSetValue(const wxString& str, int flags)
-{
-    // optimize redraws
-    if ( GetValue() == str )
-        return;
-
-    GetTextPeer()->SetStringValue( str ) ;
-
-    if ( (flags & SetValue_SendEvent) && m_triggerOnSetValue )
-    {
-        SendTextUpdatedEvent();
-    }
-}
-
 void wxTextCtrl::SetMaxLength(unsigned long len)
 {
     m_maxLength = len ;
@@ -198,7 +178,8 @@ bool wxTextCtrl::SetFont( const wxFont& font )
 
 bool wxTextCtrl::SetStyle(long start, long end, const wxTextAttr& style)
 {
-    GetTextPeer()->SetStyle( start , end , style ) ;
+    if (GetTextPeer())
+        GetTextPeer()->SetStyle( start , end , style ) ;
 
     return true ;
 }
@@ -211,143 +192,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() ;
-
-        wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
-        event.SetEventObject( this );
-        HandleWindowEvent( event );
-      }
-}
-
-void wxTextCtrl::Paste()
-{
-    if (CanPaste())
-    {
-        GetTextPeer()->Paste() ;
-
-        // TODO: eventually we should add setting the default style again
-
-        wxCommandEvent event( wxEVT_COMMAND_TEXT_UPDATED, m_windowId );
-        event.SetEventObject( this );
-        HandleWindowEvent( event );
-    }
-}
-
-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::Replace(long from, long to, const wxString& str)
-{
-    GetTextPeer()->Replace( from , to , str ) ;
-}
-
-void wxTextCtrl::Remove(long from, long to)
-{
-    GetTextPeer()->Remove( from , to ) ;
-}
-
-void wxTextCtrl::SetSelection(long from, long to)
-{
-    GetTextPeer()->SetSelection( from , to ) ;
-}
-
-void wxTextCtrl::WriteText(const wxString& str)
-{
-    GetTextPeer()->WriteText( str ) ;
-}
-
-void wxTextCtrl::AppendText(const wxString& text)
-{
-    SetInsertionPointEnd();
-    WriteText( text );
-}
-
-void wxTextCtrl::Clear()
-{
-    GetTextPeer()->Clear() ;
-}
-
 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
@@ -356,6 +205,13 @@ bool wxTextCtrl::AcceptsFocus() const
 
 wxSize wxTextCtrl::DoGetBestSize() const
 {
+    if (GetTextPeer())
+    {
+        wxSize size = GetTextPeer()->GetBestSize();
+        if (size.x > 0 && size.y > 0)
+            return size;
+    }
+    
     int wText, hText;
 
     // these are the numbers from the HIG:
@@ -392,36 +248,9 @@ wxSize wxTextCtrl::DoGetBestSize() const
     return wxSize(wText, hText);
 }
 
-// ----------------------------------------------------------------------------
-// Undo/redo
-// ----------------------------------------------------------------------------
-
-void wxTextCtrl::Undo()
-{
-    if (CanUndo())
-        GetTextPeer()->Undo() ;
-}
-
-void wxTextCtrl::Redo()
+bool wxTextCtrl::GetStyle(long position, wxTextAttr& style)
 {
-    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() ;
+    return GetTextPeer()->GetStyle(position, style);
 }
 
 void wxTextCtrl::MarkDirty()
@@ -464,10 +293,45 @@ wxString wxTextCtrl::GetLineText(long lineNo) const
     return GetTextPeer()->GetLineText(lineNo) ;
 }
 
-void wxTextCtrl::Command(wxCommandEvent & event)
+void wxTextCtrl::Remove(long from, long to)
 {
-    SetValue(event.GetString());
-    ProcessCommand(event);
+    wxTextEntry::Remove(from, to);
+    if ( m_triggerUpdateEvents )
+        SendTextUpdatedEvent();
+}
+
+void wxTextCtrl::WriteText(const wxString& str)
+{
+    wxTextEntry::WriteText( str ) ;
+    if ( m_triggerUpdateEvents )
+        SendTextUpdatedEvent();
+}
+
+void wxTextCtrl::Clear()
+{
+    wxTextEntry::Clear() ;
+    SendTextUpdatedEvent();
+}
+
+void wxTextCtrl::Cut()
+{
+    if (CanCut())
+    {
+        wxTextEntry::Cut() ;
+
+        SendTextUpdatedEvent();
+    }
+}
+
+void wxTextCtrl::Paste()
+{
+    if (CanPaste())
+    {
+        wxTextEntry::Paste();
+
+        // TODO: eventually we should add setting the default style again
+        SendTextUpdatedEvent();
+    }
 }
 
 void wxTextCtrl::OnDropFiles(wxDropFilesEvent& event)
@@ -498,7 +362,7 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
         return ;
     }
 
-    if ( !IsEditable() && key != WXK_LEFT && key != WXK_RIGHT && key != WXK_DOWN && key != WXK_UP && key != WXK_TAB &&
+    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
         )
@@ -511,8 +375,8 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
     // allow navigation and deletion
     GetSelection( &from, &to );
     if ( !IsMultiLine() && m_maxLength && GetValue().length() >= m_maxLength &&
-        key != WXK_LEFT && key != WXK_RIGHT && key != WXK_TAB && key != WXK_UP && key != WXK_DOWN && 
-        key != WXK_BACK && key != WXK_DELETE && !( key == WXK_RETURN && (m_windowStyle & wxTE_PROCESS_ENTER) ) &&
+        !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
@@ -605,6 +469,8 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
         event.Skip(true) ;
     }
 
+    // osx_cocoa sends its event upon insertText
+#if wxOSX_USE_CARBON
     if ( ( key >= 0x20 && key < WXK_START ) ||
          ( key >= WXK_NUMPAD0 && key <= WXK_DIVIDE ) ||
          key == WXK_RETURN ||
@@ -615,12 +481,21 @@ void wxTextCtrl::OnChar(wxKeyEvent& event)
         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();
@@ -698,8 +573,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() )
@@ -723,8 +596,7 @@ void wxTextCtrl::OnContextMenu(wxContextMenuEvent& event)
         m_privateContextMenu->Append(wxID_SELECTALL, _("Select &All"));
     }
 
-    if (m_privateContextMenu != NULL)
-        PopupMenu(m_privateContextMenu);
+    PopupMenu(m_privateContextMenu);
 #endif
 }
 
@@ -740,6 +612,12 @@ bool wxTextCtrl::MacSetupCursor( const wxPoint& pt )
 // implementation base class
 // ----------------------------------------------------------------------------
 
+bool wxTextWidgetImpl::GetStyle(long WXUNUSED(position),
+                                wxTextAttr& WXUNUSED(style))
+{
+    return false;
+}
+
 void wxTextWidgetImpl::SetStyle(long WXUNUSED(start),
                                 long WXUNUSED(end),
                                 const wxTextAttr& WXUNUSED(style))
@@ -825,13 +703,16 @@ 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++)
     {
+#if wxOSX_USE_COCOA
+        if (content[i] == '\n')
+#else
         if (content[i] == '\r')
+#endif
             lines++;
     }