]> git.saurik.com Git - wxWidgets.git/blobdiff - src/propgrid/propgridiface.cpp
simplify Enable()
[wxWidgets.git] / src / propgrid / propgridiface.cpp
index 75bf8bdd5282719d727afd04422850a0922b177b..93e44177390513b52cc62be6f9d65c9a8dbf5f85 100644 (file)
@@ -4,7 +4,7 @@
 // Author:      Jaakko Salli
 // Modified by:
 // Created:     2008-08-24
-// RCS-ID:      $Id:
+// RCS-ID:      $Id$
 // Copyright:   (c) Jaakko Salli
 // Licence:     wxWindows license
 /////////////////////////////////////////////////////////////////////////////
@@ -37,8 +37,8 @@
     #include "wx/intl.h"
 #endif
 
-#include <wx/propgrid/property.h>
-#include <wx/propgrid/propgrid.h>
+#include "wx/propgrid/property.h"
+#include "wx/propgrid/propgrid.h"
 
 
 const wxChar *wxPGTypeName_long = wxT("long");
@@ -56,173 +56,8 @@ const wxChar *wxPGTypeName_wxArrayString = wxT("arrstring");
 WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED(wxPoint, WXDLLIMPEXP_PROPGRID)
 WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED(wxSize, WXDLLIMPEXP_PROPGRID)
 WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED_DUMMY_EQ(wxArrayInt, WXDLLIMPEXP_PROPGRID)
-
-// For wxLongLong and wxULongLong have custom classname << variant
-// implementation for improved flexibility.
-WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED_NO_EQ_NO_GETTER(wxLongLong, WXDLLIMPEXP_PROPGRID)
-WX_PG_IMPLEMENT_VARIANT_DATA_EQ(wxLongLong, WXDLLIMPEXP_PROPGRID)
-WXDLLIMPEXP_PROPGRID wxLongLong& operator << ( wxLongLong &value, const wxVariant &variant )
-{
-    wxLongLong_t ll;
-    if ( !wxPGVariantToLongLong(variant, &ll) )
-    {
-        wxFAIL_MSG("Cannot convert to wxLongLong");
-    }
-    value = ll;
-    return value;
-}
-WXDLLIMPEXP_PROPGRID wxLongLong_t& operator << ( wxLongLong_t &value, const wxVariant &variant )
-{
-    if ( !wxPGVariantToLongLong(variant, &value) )
-    {
-        wxFAIL_MSG("Cannot convert to wxLongLong");
-    }
-    return value;
-}
-
-WX_PG_IMPLEMENT_VARIANT_DATA_EXPORTED_NO_EQ_NO_GETTER(wxULongLong, WXDLLIMPEXP_PROPGRID)
-WX_PG_IMPLEMENT_VARIANT_DATA_EQ(wxULongLong, WXDLLIMPEXP_PROPGRID)
-WXDLLIMPEXP_PROPGRID wxULongLong& operator << ( wxULongLong &value, const wxVariant &variant )
-{
-    wxULongLong_t ull;
-    if ( !wxPGVariantToULongLong(variant, &ull) )
-    {
-        wxFAIL_MSG("Cannot convert to wxULongLong");
-    }
-    value = ull;
-    return value;
-}
-WXDLLIMPEXP_PROPGRID wxULongLong_t& operator << ( wxULongLong_t &value, const wxVariant &variant )
-{
-    if ( !wxPGVariantToULongLong(variant, &value) )
-    {
-        wxFAIL_MSG("Cannot convert to wxULongLong");
-    }
-    return value;
-}
-
 IMPLEMENT_VARIANT_OBJECT_EXPORTED(wxFont, WXDLLIMPEXP_PROPGRID)
 
-// -----------------------------------------------------------------------
-// wxVariant helpers
-// -----------------------------------------------------------------------
-
-long wxPGVariantToInt( const wxVariant& variant, long defVal )
-{
-    if ( variant.IsNull() )
-        return defVal;
-
-    if ( variant.GetType() == wxS("long") )
-        return variant.GetLong();
-
-    if ( variant.GetType() == wxS("bool") )
-        return variant.GetBool() ? 1 : 0;
-
-    if ( variant.GetType() == wxS("wxLongLong") )
-    {
-        wxLongLong ll;
-        ll << variant;
-        if ( ll >= LONG_MAX )
-            return LONG_MAX;
-        else if ( ll <= LONG_MIN )
-            return LONG_MIN;
-        return ll.ToLong();
-    }
-
-    long l = defVal;
-
-    if ( variant.GetType() == wxPG_VARIANT_TYPE_STRING )
-        variant.GetString().ToLong(&l, 0);
-
-    return l;
-}
-
-// -----------------------------------------------------------------------
-
-bool wxPGVariantToLongLong( const wxVariant& variant, wxLongLong_t* pResult )
-{
-    if ( variant.IsNull() )
-        return false;
-
-    wxString variantType = variant.GetType();
-
-    if ( variantType == wxPG_VARIANT_TYPE_LONG )
-    {
-        *pResult = variant.GetLong();
-        return true;
-    }
-
-    if ( variantType == wxLongLong_VariantType )
-    {
-        // NOTE: << operator uses this functions, so we can't use it here
-        *pResult = wxLongLongRefFromVariant(variant).GetValue();
-        return true;
-    }
-
-    return false;
-}
-
-// -----------------------------------------------------------------------
-
-bool wxPGVariantToULongLong( const wxVariant& variant, wxULongLong_t* pResult )
-{
-    if ( variant.IsNull() )
-        return false;
-
-    wxString variantType = variant.GetType();
-
-    if ( variantType == wxPG_VARIANT_TYPE_LONG )
-    {
-        *pResult = (unsigned long)variant.GetLong();
-        return true;
-    }
-
-    if ( variantType == wxULongLong_VariantType )
-    {
-        // NOTE: << operator uses this functions, so we can't use it here
-        *pResult = wxULongLongRefFromVariant(variant).GetValue();
-        return true;
-    }
-
-    return false;
-}
-
-// -----------------------------------------------------------------------
-
-bool wxPGVariantToDouble( const wxVariant& variant, double* pResult )
-{
-    if ( variant.IsNull() )
-        return false;
-
-    wxString variantType = variant.GetType();
-
-    if ( variantType == wxPG_VARIANT_TYPE_DOUBLE )
-    {
-        *pResult = variant.GetDouble();
-        return true;
-    }
-
-    if ( variantType == wxPG_VARIANT_TYPE_LONG )
-    {
-        *pResult = (double)variant.GetLong();
-        return true;
-    }
-
-    if ( variantType == wxLongLong_VariantType )
-    {
-        wxLongLong ll;
-        ll << variant;
-        *pResult = ll.ToDouble();
-        return true;
-    }
-
-    if ( variantType == wxPG_VARIANT_TYPE_STRING )
-        if ( variant.GetString().ToDouble(pResult) )
-            return true;
-
-    return false;
-}
-
 // -----------------------------------------------------------------------
 // wxPGPropArgCls
 // -----------------------------------------------------------------------
@@ -267,7 +102,7 @@ void wxPropertyGridInterface::RefreshGrid( wxPropertyGridPageState* state )
 wxPGProperty* wxPropertyGridInterface::Append( wxPGProperty* property )
 {
     wxPGProperty* retp = m_pState->DoAppend(property);
-    
+
     wxPropertyGrid* grid = m_pState->GetGrid();
     if ( grid )
         grid->RefreshGrid();
@@ -290,7 +125,7 @@ wxPGProperty* wxPropertyGridInterface::AppendIn( wxPGPropArg id, wxPGProperty* n
 wxPGProperty* wxPropertyGridInterface::Insert( wxPGPropArg id, wxPGProperty* property )
 {
     wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
-    wxPGProperty* retp = m_pState->DoInsert(p->GetParent(), p->GetArrIndex(), property);
+    wxPGProperty* retp = m_pState->DoInsert(p->GetParent(), p->GetIndexInParent(), property);
     RefreshGrid();
     return retp;
 }
@@ -312,18 +147,32 @@ void wxPropertyGridInterface::DeleteProperty( wxPGPropArg id )
     wxPG_PROP_ARG_CALL_PROLOG()
 
     wxPropertyGridPageState* state = p->GetParentState();
-    wxPropertyGrid* grid = state->GetGrid();
 
-    if ( grid->GetState() == state )
-    {
-        bool selRes = grid->DoSelectProperty(NULL, wxPG_SEL_DELETING);
-        wxPG_CHECK_RET_DBG( selRes,
-                            wxT("failed to deselect a property (editor probably had invalid value)") );
-    }
+    state->DoDelete( p, true );
 
-    state->DoDelete( p );
+    RefreshGrid(state);
+}
+
+// -----------------------------------------------------------------------
+
+wxPGProperty* wxPropertyGridInterface::RemoveProperty( wxPGPropArg id )
+{
+    wxPG_PROP_ARG_CALL_PROLOG_RETVAL(wxNullProperty)
+
+    wxCHECK( !p->GetChildCount() || p->HasFlag(wxPG_PROP_AGGREGATE),
+             wxNullProperty);
+
+    wxPropertyGridPageState* state = p->GetParentState();
+
+    state->DoDelete( p, false );
+
+    // Mark the property as 'unattached'
+    p->m_parentState = NULL;
+    p->m_parent = NULL;
 
     RefreshGrid(state);
+
+    return p;
 }
 
 // -----------------------------------------------------------------------
@@ -358,14 +207,41 @@ wxPGProperty* wxPropertyGridInterface::ReplaceProperty( wxPGPropArg id, wxPGProp
 // wxPropertyGridInterface property operations
 // -----------------------------------------------------------------------
 
-bool wxPropertyGridInterface::ClearSelection()
+wxPGProperty* wxPropertyGridInterface::GetSelection() const
+{
+    return m_pState->GetSelection();
+}
+
+// -----------------------------------------------------------------------
+
+bool wxPropertyGridInterface::ClearSelection( bool validation )
 {
+    bool res = DoClearSelection(validation, wxPG_SEL_DONT_SEND_EVENT);
+    wxPropertyGrid* pg = GetPropertyGrid();
+    if ( pg )
+        pg->Refresh();
+    return res;
+}
+
+// -----------------------------------------------------------------------
+
+bool wxPropertyGridInterface::DoClearSelection( bool validation,
+                                                int selFlags )
+{
+    if ( !validation )
+        selFlags |= wxPG_SEL_NOVALIDATE;
+
     wxPropertyGridPageState* state = m_pState;
-    wxPropertyGrid* pg = state->GetGrid();
-    if ( pg->GetState() == state )
-        return pg->DoClearSelection();
-    else
-        state->SetSelection(NULL);
+
+    if ( state )
+    {
+        wxPropertyGrid* pg = state->GetGrid();
+        if ( pg->GetState() == state )
+            return pg->DoSelectProperty(NULL, selFlags);
+        else
+            state->DoSetSelection(NULL);
+    }
+
     return true;
 }
 
@@ -428,8 +304,7 @@ bool wxPropertyGridInterface::ExpandAll( bool doExpand )
     if ( GetSelection() && GetSelection() != state->DoGetRoot() &&
          !doExpand )
     {
-        if ( !pg->ClearSelection() )
-            return false;
+        pg->DoClearSelection();
     }
 
     wxPGVIterator it;
@@ -465,14 +340,23 @@ bool wxPropertyGridInterface::ExpandAll( bool doExpand )
 
 // -----------------------------------------------------------------------
 
-void wxPropertyGridInterface::SetPropertyValueUnspecified( wxPGPropArg id )
+void wxPropertyGridInterface::ClearModifiedStatus()
 {
-    wxPG_PROP_ARG_CALL_PROLOG()
-    wxPropertyGrid* propGrid = p->GetGridIfDisplayed();
-    if ( propGrid )
-        propGrid->DoSetPropertyValueUnspecified(p);
-    else
-        p->GetParentState()->DoSetPropertyValueUnspecified(p);
+    unsigned int pageIndex = 0;
+
+    for (;;)
+    {
+        wxPropertyGridPageState* page = GetPageState(pageIndex);
+        if ( !page ) break;
+
+        page->DoGetRoot()->SetFlagRecursively(wxPG_PROP_MODIFIED, false);
+        page->m_anyModified = false;
+
+        pageIndex++;
+    }
+
+    // Update active editor control, if any
+    GetPropertyGrid()->RefreshEditor();
 }
 
 // -----------------------------------------------------------------------
@@ -502,13 +386,7 @@ void wxPropertyGridInterface::SetPropVal( wxPGPropArg id, wxVariant& value )
     wxPG_PROP_ARG_CALL_PROLOG()
 
     if ( p )
-    {
         p->SetValue(value);
-        wxPropertyGrid* propGrid = p->GetGridIfDisplayed();
-        if ( propGrid )
-            propGrid->DrawItemAndValueRelated( p );
-
-    }
 }
 
 // -----------------------------------------------------------------------
@@ -517,12 +395,8 @@ void wxPropertyGridInterface::SetPropertyValueString( wxPGPropArg id, const wxSt
 {
     wxPG_PROP_ARG_CALL_PROLOG()
 
-    if ( m_pState->DoSetPropertyValueString(p,value) )
-    {
-        wxPropertyGrid* propGrid = p->GetGridIfDisplayed();
-        if ( propGrid )
-            propGrid->DrawItemAndValueRelated( p );
-    }
+    if ( p )
+        m_pState->DoSetPropertyValueString(p, value);
 }
 
 // -----------------------------------------------------------------------
@@ -622,34 +496,6 @@ void wxPropertyGridInterface::GetPropertiesWithFlag( wxArrayPGProperty* targetAr
 
 // -----------------------------------------------------------------------
 
-void wxPropertyGridInterface::SetPropertiesFlag( const wxArrayPGProperty& srcArr,
-                                                 wxPGProperty::FlagType flags,
-                                                 bool inverse )
-{
-    unsigned int i;
-
-    for ( i=0; i<srcArr.size(); i++ )
-    {
-        wxPGProperty* property = srcArr[i];
-
-        if ( !inverse )
-            property->SetFlag(flags);
-        else
-            property->ClearFlag(flags);
-    }
-
-    // If collapsed flag or hidden was manipulated, we need to update virtual
-    // size.
-    wxPropertyGrid* pg = GetPropertyGrid();
-    if ( flags & (wxPG_PROP_COLLAPSED|wxPG_PROP_HIDDEN) )
-    {
-        GetState()->VirtualHeightChanged();
-        pg->RecalculateVirtualSize();
-    }
-}
-
-// -----------------------------------------------------------------------
-
 void wxPropertyGridInterface::SetBoolChoices( const wxString& trueChoice,
                                                  const wxString& falseChoice )
 {
@@ -737,6 +583,25 @@ bool wxPropertyGridInterface::Expand( wxPGPropArg id )
 
 // -----------------------------------------------------------------------
 
+void wxPropertyGridInterface::Sort( int flags )
+{
+    wxPropertyGrid* pg = GetPropertyGrid();
+
+    pg->DoClearSelection();
+
+    unsigned int pageIndex = 0;
+
+    for (;;)
+    {
+        wxPropertyGridPageState* page = GetPageState(pageIndex);
+        if ( !page ) break;
+        page->DoSort(flags);
+        pageIndex++;
+    }
+}
+
+// -----------------------------------------------------------------------
+
 void wxPropertyGridInterface::SetPropertyLabel( wxPGPropArg id, const wxString& newproplabel )
 {
     wxPG_PROP_ARG_CALL_PROLOG()
@@ -783,6 +648,60 @@ bool wxPropertyGridInterface::SetPropertyMaxLength( wxPGPropArg id, int maxLen )
     return true;
 }
 
+// -----------------------------------------------------------------------
+
+void
+wxPropertyGridInterface::SetPropertyBackgroundColour( wxPGPropArg id,
+                                                      const wxColour& colour,
+                                                      int flags )
+{
+    wxPG_PROP_ARG_CALL_PROLOG()
+    p->SetBackgroundColour(colour, flags);
+    RefreshProperty(p);
+}
+
+// -----------------------------------------------------------------------
+
+void wxPropertyGridInterface::SetPropertyTextColour( wxPGPropArg id,
+                                                     const wxColour& colour,
+                                                     int flags )
+{
+    wxPG_PROP_ARG_CALL_PROLOG()
+    p->SetTextColour(colour, flags);
+    RefreshProperty(p);
+}
+
+// -----------------------------------------------------------------------
+
+void wxPropertyGridInterface::SetPropertyColoursToDefault( wxPGPropArg id )
+{
+    wxPG_PROP_ARG_CALL_PROLOG()
+
+    p->m_cells.clear();
+}
+
+// -----------------------------------------------------------------------
+
+void wxPropertyGridInterface::SetPropertyCell( wxPGPropArg id,
+                                               int column,
+                                               const wxString& text,
+                                               const wxBitmap& bitmap,
+                                               const wxColour& fgCol,
+                                               const wxColour& bgCol )
+{
+    wxPG_PROP_ARG_CALL_PROLOG()
+
+    wxPGCell& cell = p->GetCell(column);
+    if ( text.length() && text != wxPG_LABEL )
+        cell.SetText(text);
+    if ( bitmap.IsOk() )
+        cell.SetBitmap(bitmap);
+    if ( fgCol != wxNullColour )
+        cell.SetFgCol(fgCol);
+    if ( bgCol != wxNullColour )
+        cell.SetBgCol(bgCol);
+}
+
 // -----------------------------------------------------------------------
 // GetPropertyValueAsXXX methods
 
@@ -824,7 +743,6 @@ bool wxPropertyGridInterface::GetPropertyValueAsBool( wxPGPropArg id ) const
 
 IMPLEMENT_GET_VALUE(long,long,Long,0)
 IMPLEMENT_GET_VALUE(double,double,Double,0.0)
-IMPLEMENT_GET_VALUE(void,void*,VoidPtr,NULL)
 
 bool wxPropertyGridInterface::IsPropertyExpanded( wxPGPropArg id ) const
 {
@@ -947,7 +865,7 @@ wxString wxPropertyGridInterface::SaveEditableState( int includedStates ) const
         if ( includedStates & ExpandedState )
         {
             wxArrayPGProperty ptrs;
-            wxPropertyGridConstIterator it = 
+            wxPropertyGridConstIterator it =
                 wxPropertyGridConstIterator( pageState,
                                              wxPG_ITERATE_ALL_PARENTS_RECURSIVELY|wxPG_ITERATE_HIDDEN,
                                              wxNullProperty );
@@ -962,7 +880,7 @@ wxString wxPropertyGridInterface::SaveEditableState( int includedStates ) const
 
                 if ( !p->HasFlag(wxPG_PROP_COLLAPSED) )
                     result += EscapeDelimiters(p->GetName());
-                    result += wxS(",");
+                result += wxS(",");
 
             }
 
@@ -996,6 +914,12 @@ wxString wxPropertyGridInterface::SaveEditableState( int includedStates ) const
             else
                 result += wxS("0;");
         }
+        if ( includedStates & DescBoxState )
+        {
+            wxVariant v = GetEditableStateItem(wxS("descboxheight"));
+            if ( !v.IsNull() )
+                result += wxString::Format(wxS("descboxheight=%i;"), (int)v.GetLong());
+        }
         result.RemoveLast();  // Remove last semicolon
         result += wxS("|");
     }
@@ -1045,7 +969,7 @@ bool wxPropertyGridInterface::RestoreEditableState( const wxString& src, int res
                 {
                     if ( restoreStates & ExpandedState )
                     {
-                        wxPropertyGridIterator it = 
+                        wxPropertyGridIterator it =
                             wxPropertyGridIterator( pageState,
                                                     wxPG_ITERATE_ALL,
                                                     wxNullProperty );
@@ -1110,7 +1034,7 @@ bool wxPropertyGridInterface::RestoreEditableState( const wxString& src, int res
                             else
                             {
                                 if ( values[0].length() )
-                                    pageState->SetSelection(GetPropertyByName(value));
+                                    pageState->DoSetSelection(GetPropertyByName(value));
                                 else
                                     pageState->DoClearSelection();
                             }
@@ -1133,6 +1057,21 @@ bool wxPropertyGridInterface::RestoreEditableState( const wxString& src, int res
                         }
                     }
                 }
+                else if ( key == wxS("descboxheight") )
+                {
+                    if ( restoreStates & DescBoxState )
+                    {
+                        long descBoxHeight;
+                        if ( values.size() == 1 && values[0].ToLong(&descBoxHeight) )
+                        {
+                            SetEditableStateItem(wxS("descboxheight"), descBoxHeight);
+                        }
+                        else
+                        {
+                            res = false;
+                        }
+                    }
+                }
                 else
                 {
                     res = false;
@@ -1160,9 +1099,9 @@ bool wxPropertyGridInterface::RestoreEditableState( const wxString& src, int res
     if ( pgSelectionSet )
     {
         if ( newSelection )
-            pg->SelectProperty(newSelection);
+            pg->DoSelectProperty(newSelection);
         else
-            pg->ClearSelection();
+            pg->DoClearSelection();
     }
 
     if ( selectedPage != -1 )