]> git.saurik.com Git - wxWidgets.git/blobdiff - src/propgrid/property.cpp
new file added
[wxWidgets.git] / src / propgrid / property.cpp
index bfa70e38386b853ae4918c977f3f7e994ef3d1c3..fc1b9e948ceb5d63cf9d7a981ab8829c22894b9b 100644 (file)
@@ -6,7 +6,7 @@
 // Created:     2008-08-23
 // RCS-ID:      $Id$
 // Copyright:   (c) Jaakko Salli
 // Created:     2008-08-23
 // RCS-ID:      $Id$
 // Copyright:   (c) Jaakko Salli
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // For compilers that support precompilation, includes "wx/wx.h".
 /////////////////////////////////////////////////////////////////////////////
 
 // For compilers that support precompilation, includes "wx/wx.h".
@@ -24,6 +24,7 @@
     #include "wx/hash.h"
     #include "wx/string.h"
     #include "wx/log.h"
     #include "wx/hash.h"
     #include "wx/string.h"
     #include "wx/log.h"
+    #include "wx/math.h"
     #include "wx/event.h"
     #include "wx/window.h"
     #include "wx/panel.h"
     #include "wx/event.h"
     #include "wx/window.h"
     #include "wx/panel.h"
@@ -35,6 +36,8 @@
     #include "wx/intl.h"
 #endif
 
     #include "wx/intl.h"
 #endif
 
+#include "wx/image.h"
+
 #include "wx/propgrid/propgrid.h"
 
 
 #include "wx/propgrid/propgrid.h"
 
 
@@ -156,7 +159,7 @@ int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell&
         dc.SetFont(font);
 
     const wxBitmap& bmp = cell.GetBitmap();
         dc.SetFont(font);
 
     const wxBitmap& bmp = cell.GetBitmap();
-    if ( bmp.Ok() &&
+    if ( bmp.IsOk() &&
         // Do not draw oversized bitmap outside choice popup
          ((flags & ChoicePopup) || bmp.GetHeight() < rect.height )
         )
         // Do not draw oversized bitmap outside choice popup
          ((flags & ChoicePopup) || bmp.GetHeight() < rect.height )
         )
@@ -206,7 +209,7 @@ bool wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
             {
                 text = propertyGrid->GetCommonValueLabel(cmnVal);
                 DrawText( dc, rect, 0, text );
             {
                 text = propertyGrid->GetCommonValueLabel(cmnVal);
                 DrawText( dc, rect, 0, text );
-                if ( text.length() )
+                if ( !text.empty() )
                     return true;
             }
             return false;
                     return true;
             }
             return false;
@@ -258,15 +261,15 @@ bool wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
             if ( propertyGrid->GetColumnCount() <= 2 )
             {
                 wxString unitsString = property->GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
             if ( propertyGrid->GetColumnCount() <= 2 )
             {
                 wxString unitsString = property->GetAttribute(wxPGGlobalVars->m_strUnits, wxEmptyString);
-                if ( unitsString.length() )
+                if ( !unitsString.empty() )
                     text = wxString::Format(wxS("%s %s"), text.c_str(), unitsString.c_str() );
             }
         }
 
                     text = wxString::Format(wxS("%s %s"), text.c_str(), unitsString.c_str() );
             }
         }
 
-        if ( text.length() == 0 )
+        if ( text.empty() )
         {
             text = property->GetHintText();
         {
             text = property->GetHintText();
-            if ( text.length() > 0 )
+            if ( !text.empty() )
             {
                 res = true;
 
             {
                 res = true;
 
@@ -274,7 +277,7 @@ bool wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
                     propertyGrid->GetCellDisabledTextColour();
                 dc.SetTextForeground(hCol);
 
                     propertyGrid->GetCellDisabledTextColour();
                 dc.SetTextForeground(hCol);
 
-                // Must make the editor NULL to override it's own rendering
+                // Must make the editor NULL to override its own rendering
                 // code.
                 editor = NULL;
             }
                 // code.
                 editor = NULL;
             }
@@ -325,7 +328,7 @@ wxSize wxPGDefaultRenderer::GetImageSize( const wxPGProperty* property,
         {
             wxBitmap* bmp = property->GetValueImage();
 
         {
             wxBitmap* bmp = property->GetValueImage();
 
-            if ( bmp && bmp->Ok() )
+            if ( bmp && bmp->IsOk() )
                 return wxSize(bmp->GetWidth(),bmp->GetHeight());
         }
     }
                 return wxSize(bmp->GetWidth(),bmp->GetHeight());
         }
     }
@@ -497,7 +500,24 @@ void wxPGProperty::InitAfterAdded( wxPropertyGridPageState* pageState,
     // (so propgrid can be NULL, too).
 
     wxPGProperty* parent = m_parent;
     // (so propgrid can be NULL, too).
 
     wxPGProperty* parent = m_parent;
-    bool parentIsRoot = parent->IsKindOf(CLASSINFO(wxPGRootProperty));
+    bool parentIsRoot = parent->IsKindOf(wxCLASSINFO(wxPGRootProperty));
+
+    //
+    // Convert invalid cells to default ones in this grid
+    for ( unsigned int i=0; i<m_cells.size(); i++ )
+    {
+        wxPGCell& cell = m_cells[i];
+        if ( cell.IsInvalid() )
+        {
+            const wxPGCell& propDefCell = propgrid->GetPropertyDefaultCell();
+            const wxPGCell& catDefCell = propgrid->GetCategoryDefaultCell();
+
+            if ( !HasFlag(wxPG_PROP_CATEGORY) )
+                cell = propDefCell;
+            else
+                cell = catDefCell;
+        }
+    }
 
     m_parentState = pageState;
 
 
     m_parentState = pageState;
 
@@ -621,6 +641,27 @@ void wxPGProperty::InitAfterAdded( wxPropertyGridPageState* pageState,
     }
 }
 
     }
 }
 
+void wxPGProperty::OnDetached(wxPropertyGridPageState* WXUNUSED(state),
+                              wxPropertyGrid* propgrid)
+{
+    if ( propgrid )
+    {
+        const wxPGCell& propDefCell = propgrid->GetPropertyDefaultCell();
+        const wxPGCell& catDefCell = propgrid->GetCategoryDefaultCell();
+
+        // Make default cells invalid
+        for ( unsigned int i=0; i<m_cells.size(); i++ )
+        {
+            wxPGCell& cell = m_cells[i];
+            if ( cell.IsSameAs(propDefCell) ||
+                 cell.IsSameAs(catDefCell) )
+            {
+                cell.UnRef();
+            }
+        }
+    }
+}
+
 wxPGProperty::wxPGProperty()
     : wxObject()
 {
 wxPGProperty::wxPGProperty()
     : wxObject()
 {
@@ -677,7 +718,7 @@ wxString wxPGProperty::GetName() const
 {
     wxPGProperty* parent = GetParent();
 
 {
     wxPGProperty* parent = GetParent();
 
-    if ( !m_name.length() || !parent || parent->IsCategory() || parent->IsRoot() )
+    if ( m_name.empty() || !parent || parent->IsCategory() || parent->IsRoot() )
         return m_name;
 
     return m_parent->GetName() + wxS(".") + m_name;
         return m_name;
 
     return m_parent->GetName() + wxS(".") + m_name;
@@ -692,12 +733,7 @@ wxPropertyGrid* wxPGProperty::GetGrid() const
 
 int wxPGProperty::Index( const wxPGProperty* p ) const
 {
 
 int wxPGProperty::Index( const wxPGProperty* p ) const
 {
-    for ( unsigned int i = 0; i<m_children.size(); i++ )
-    {
-        if ( p == m_children[i] )
-            return i;
-    }
-    return wxNOT_FOUND;
+    return wxPGFindInVector(m_children, p);
 }
 
 bool wxPGProperty::ValidateValue( wxVariant& WXUNUSED(value), wxPGValidationInfo& WXUNUSED(validationInfo) ) const
 }
 
 bool wxPGProperty::ValidateValue( wxVariant& WXUNUSED(value), wxPGValidationInfo& WXUNUSED(validationInfo) ) const
@@ -891,7 +927,7 @@ void wxPGProperty::DoGenerateComposedValue( wxString& text,
             (*childResults)[curChild->GetName()] = s;
 
         bool skip = false;
             (*childResults)[curChild->GetName()] = s;
 
         bool skip = false;
-        if ( (argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT) && !s.length() )
+        if ( (argFlags & wxPG_UNEDITABLE_COMPOSITE_FRAGMENT) && s.empty() )
             skip = true;
 
         if ( !curChild->GetChildCount() || skip )
             skip = true;
 
         if ( !curChild->GetChildCount() || skip )
@@ -1139,7 +1175,7 @@ bool wxPGProperty::StringToValue( wxVariant& variant, const wxString& text, int
 
                     token = text.substr(startPos,pos-startPos-1);
 
 
                     token = text.substr(startPos,pos-startPos-1);
 
-                    if ( !token.length() )
+                    if ( token.empty() )
                         break;
 
                     const wxPGProperty* child = Item(curChild);
                         break;
 
                     const wxPGProperty* child = Item(curChild);
@@ -1262,7 +1298,7 @@ void wxPGProperty::OnCustomPaint( wxDC& dc,
 {
     wxBitmap* bmp = m_valueBitmap;
 
 {
     wxBitmap* bmp = m_valueBitmap;
 
-    wxCHECK_RET( bmp && bmp->Ok(), wxT("invalid bitmap") );
+    wxCHECK_RET( bmp && bmp->IsOk(), wxT("invalid bitmap") );
 
     wxCHECK_RET( rect.x >= 0, wxT("unexpected measure call") );
 
 
     wxCHECK_RET( rect.x >= 0, wxT("unexpected measure call") );
 
@@ -1364,6 +1400,12 @@ void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
                 }
                 i++;
             }
                 }
                 i++;
             }
+
+            // Always call OnSetValue() for a parent property (do not call it
+            // here if the value is non-null because it will then be called
+            // below)
+            if ( value.IsNull() )
+                OnSetValue();
         }
 
         if ( !value.IsNull() )
         }
 
         if ( !value.IsNull() )
@@ -1491,6 +1533,31 @@ wxVariant wxPGProperty::GetDefaultValue() const
     return wxVariant();
 }
 
     return wxVariant();
 }
 
+void wxPGProperty::Enable( bool enable )
+{
+    wxPropertyGrid* pg = GetGrid();
+
+    // Preferably call the version in the owning wxPropertyGrid,
+    // since it handles the editor de-activation.
+    if ( pg )
+        pg->EnableProperty(this, enable);
+    else
+        DoEnable(enable);
+}
+
+void wxPGProperty::DoEnable( bool enable )
+{
+    if ( enable )
+        ClearFlag(wxPG_PROP_DISABLED);
+    else
+        SetFlag(wxPG_PROP_DISABLED);
+
+    // Apply same to sub-properties as well
+    unsigned int i;
+    for ( i = 0; i < GetChildCount(); i++ )
+        Item(i)->DoEnable( enable );
+}
+
 void wxPGProperty::EnsureCells( unsigned int column )
 {
     if ( column >= m_cells.size() )
 void wxPGProperty::EnsureCells( unsigned int column )
 {
     if ( column >= m_cells.size() )
@@ -1499,14 +1566,17 @@ void wxPGProperty::EnsureCells( unsigned int column )
         wxPropertyGrid* pg = GetGrid();
         wxPGCell defaultCell;
 
         wxPropertyGrid* pg = GetGrid();
         wxPGCell defaultCell;
 
-        // Work around possible VC6 bug by using intermediate variables
-        const wxPGCell& propDefCell = pg->GetPropertyDefaultCell();
-        const wxPGCell& catDefCell = pg->GetCategoryDefaultCell();
+        if ( pg )
+        {
+            // Work around possible VC6 bug by using intermediate variables
+            const wxPGCell& propDefCell = pg->GetPropertyDefaultCell();
+            const wxPGCell& catDefCell = pg->GetCategoryDefaultCell();
 
 
-        if ( !HasFlag(wxPG_PROP_CATEGORY) )
-            defaultCell = propDefCell;
-        else
-            defaultCell = catDefCell;
+            if ( !HasFlag(wxPG_PROP_CATEGORY) )
+                defaultCell = propDefCell;
+            else
+                defaultCell = catDefCell;
+        }
 
         // TODO: Replace with resize() call
         unsigned int cellCountMax = column+1;
 
         // TODO: Replace with resize() call
         unsigned int cellCountMax = column+1;
@@ -1781,7 +1851,7 @@ wxString wxPGProperty::GetFlagsAsString( FlagType flagsMask ) const
         {
             const wxChar* fs = gs_propFlagToString[i];
             wxASSERT(fs);
         {
             const wxChar* fs = gs_propFlagToString[i];
             wxASSERT(fs);
-            if ( s.length() )
+            if ( !s.empty() )
                 s << wxS("|");
             s << fs;
         }
                 s << wxS("|");
             s << fs;
         }
@@ -1951,11 +2021,11 @@ const wxPGEditor* wxPGProperty::GetEditorClass() const
     if ( GetDisplayedCommonValueCount() )
     {
         // TextCtrlAndButton -> ComboBoxAndButton
     if ( GetDisplayedCommonValueCount() )
     {
         // TextCtrlAndButton -> ComboBoxAndButton
-        if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlAndButtonEditor)) )
+        if ( wxDynamicCast(editor, wxPGTextCtrlAndButtonEditor) )
             editor = wxPGEditor_ChoiceAndButton;
 
         // TextCtrl -> ComboBox
             editor = wxPGEditor_ChoiceAndButton;
 
         // TextCtrl -> ComboBox
-        else if ( editor->IsKindOf(CLASSINFO(wxPGTextCtrlEditor)) )
+        else if ( wxDynamicCast(editor, wxPGTextCtrlEditor) )
             editor = wxPGEditor_ComboBox;
     }
 
             editor = wxPGEditor_ComboBox;
     }
 
@@ -2022,7 +2092,7 @@ void wxPGProperty::SetValueImage( wxBitmap& bmp )
 {
     delete m_valueBitmap;
 
 {
     delete m_valueBitmap;
 
-    if ( &bmp && bmp.Ok() )
+    if ( &bmp && bmp.IsOk() )
     {
         // Resize the image
         wxSize maxSz = GetGrid()->GetImageSize();
     {
         // Resize the image
         wxSize maxSz = GetGrid()->GetImageSize();
@@ -2030,19 +2100,23 @@ void wxPGProperty::SetValueImage( wxBitmap& bmp )
 
         if ( imSz.y != maxSz.y )
         {
 
         if ( imSz.y != maxSz.y )
         {
-            // Create a memory DC
+        #if wxUSE_IMAGE
+            // Here we use high-quality wxImage scaling functions available
+            wxImage img = bmp.ConvertToImage();
+            double scaleY = (double)maxSz.y / (double)imSz.y;
+            img.Rescale(wxRound(bmp.GetWidth()*scaleY),
+                        wxRound(bmp.GetHeight()*scaleY),
+                        wxIMAGE_QUALITY_HIGH);
+            wxBitmap* bmpNew = new wxBitmap(img, 32);
+        #else
+            // This is the old, deprecated method of scaling the image
             wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
             wxBitmap* bmpNew = new wxBitmap(maxSz.x,maxSz.y,bmp.GetDepth());
-
             wxMemoryDC dc;
             dc.SelectObject(*bmpNew);
             wxMemoryDC dc;
             dc.SelectObject(*bmpNew);
-
-            // Scale
-            // FIXME: This is ugly - use image or wait for scaling patch.
             double scaleY = (double)maxSz.y / (double)imSz.y;
             double scaleY = (double)maxSz.y / (double)imSz.y;
-
             dc.SetUserScale(scaleY, scaleY);
             dc.SetUserScale(scaleY, scaleY);
-
             dc.DrawBitmap(bmp, 0, 0);
             dc.DrawBitmap(bmp, 0, 0);
+        #endif
 
             m_valueBitmap = bmpNew;
         }
 
             m_valueBitmap = bmpNew;
         }
@@ -2259,9 +2333,9 @@ void wxPGProperty::AdaptListToValue( wxVariant& list, wxVariant* value ) const
     else
         allChildrenSpecified = true;
 
     else
         allChildrenSpecified = true;
 
-    wxVariant childValue = list[0];
     unsigned int i;
     unsigned int n = 0;
     unsigned int i;
     unsigned int n = 0;
+    wxVariant childValue = list[n];
 
     //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
 
 
     //wxLogDebug(wxT(">> %s.AdaptListToValue()"),GetBaseName().c_str());
 
@@ -2496,6 +2570,25 @@ void wxPGProperty::DeleteChildren()
     }
 }
 
     }
 }
 
+bool wxPGProperty::IsChildSelected( bool recursive ) const
+{
+    size_t i;
+    for ( i = 0; i < GetChildCount(); i++ )
+    {
+        wxPGProperty* child = Item(i);
+
+        // Test child
+        if ( m_parentState->DoIsPropertySelected( child ) )
+            return true;
+
+        // Test sub-childs
+        if ( recursive && child->IsChildSelected( recursive ) )
+            return true;
+    }
+
+    return false;
+}
+
 wxVariant wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
                                       int WXUNUSED(childIndex),
                                       wxVariant& WXUNUSED(childValue) ) const
 wxVariant wxPGProperty::ChildChanged( wxVariant& WXUNUSED(thisValue),
                                       int WXUNUSED(childIndex),
                                       wxVariant& WXUNUSED(childValue) ) const