]> git.saurik.com Git - wxWidgets.git/blobdiff - src/propgrid/property.cpp
No real changes, just get rid of _wxMenuAt() in wxOSX menu code.
[wxWidgets.git] / src / propgrid / property.cpp
index 5c789adc78596d73b081b058d88d608caec3675a..37edd533d970db5bc3c3bfe1dd9bab190c24c18a 100644 (file)
@@ -6,7 +6,7 @@
 // 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".
@@ -135,8 +135,9 @@ int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell&
     // If possible, use cell colours
     if ( !(flags & DontUseCellBgCol) )
     {
-        dc.SetPen(cell.GetBgCol());
-        dc.SetBrush(cell.GetBgCol());
+        const wxColour& bgCol = cell.GetBgCol();
+        dc.SetPen(bgCol);
+        dc.SetBrush(bgCol);
     }
 
     if ( !(flags & DontUseCellFgCol) )
@@ -149,6 +150,11 @@ int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell&
     if ( !(flags & (Control|ChoicePopup)) )
         dc.DrawRectangle(rect);
 
+    // Use cell font, if provided
+    const wxFont& font = cell.GetFont();
+    if ( font.IsOk() )
+        dc.SetFont(font);
+
     const wxBitmap& bmp = cell.GetBitmap();
     if ( bmp.Ok() &&
         // Do not draw oversized bitmap outside choice popup
@@ -165,14 +171,29 @@ int wxPGCellRenderer::PreDrawCell( wxDC& dc, const wxRect& rect, const wxPGCell&
     return imageWidth;
 }
 
+void wxPGCellRenderer::PostDrawCell( wxDC& dc,
+                                     const wxPropertyGrid* propGrid,
+                                     const wxPGCell& cell,
+                                     int WXUNUSED(flags) ) const
+{
+    // Revert font
+    const wxFont& font = cell.GetFont();
+    if ( font.IsOk() )
+        dc.SetFont(propGrid->GetFont());
+}
+
 // -----------------------------------------------------------------------
 // wxPGDefaultRenderer
 // -----------------------------------------------------------------------
 
-void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
+bool wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
                                   const wxPropertyGrid* propertyGrid, wxPGProperty* property,
                                   int column, int item, int flags ) const
 {
+    const wxPGEditor* editor = NULL;
+    const wxPGCell* cell = NULL;
+
+    wxString text;
     bool isUnspecified = property->IsValueUnspecified();
 
     if ( column == 1 && item == -1 )
@@ -182,17 +203,19 @@ void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
         {
             // Common Value
             if ( !isUnspecified )
-                DrawText( dc, rect, 0, propertyGrid->GetCommonValueLabel(cmnVal) );
-            return;
+            {
+                text = propertyGrid->GetCommonValueLabel(cmnVal);
+                DrawText( dc, rect, 0, text );
+                if ( text.length() )
+                    return true;
+            }
+            return false;
         }
     }
 
-    const wxPGEditor* editor = NULL;
-    const wxPGCell* cell = NULL;
-
-    wxString text;
     int imageWidth = 0;
     int preDrawFlags = flags;
+    bool res = false;
 
     property->GetDisplayInfo(column, item, flags, &text, &cell);
 
@@ -200,10 +223,10 @@ void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
 
     if ( column == 1 )
     {
+        editor = property->GetColumnEditor(column);
+
         if ( !isUnspecified )
         {
-            editor = property->GetColumnEditor(column);
-
             // Regular property value
 
             wxSize imageSize = propertyGrid->GetImageSize(property, item);
@@ -242,14 +265,24 @@ void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
 
         if ( text.length() == 0 )
         {
-            // Try to show inline help if no text
-            wxVariant vInlineHelp = property->GetAttribute(wxPGGlobalVars->m_strInlineHelp);
-            if ( !vInlineHelp.IsNull() )
+            text = property->GetHintText();
+            if ( text.length() > 0 )
             {
-                text = vInlineHelp.GetString();
-                dc.SetTextForeground(propertyGrid->GetCellDisabledTextColour());
+                res = true;
+
+                const wxColour& hCol =
+                    propertyGrid->GetCellDisabledTextColour();
+                dc.SetTextForeground(hCol);
+
+                // Must make the editor NULL to override it's own rendering
+                // code.
+                editor = NULL;
             }
         }
+        else
+        {
+            res = true;
+        }
     }
 
     int imageOffset = property->GetImageOffset(imageWidth);
@@ -257,14 +290,14 @@ void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
     DrawEditorValue( dc, rect, imageOffset, text, property, editor );
 
     // active caption gets nice dotted rectangle
-    if ( property->IsCategory() /*&& column == 0*/ )
+    if ( property->IsCategory() && column == 0 )
     {
         if ( flags & Selected )
         {
-            if ( imageWidth > 0 )
+            if ( imageOffset > 0 )
             {
                 imageOffset -= DEFAULT_IMAGE_OFFSET_INCREMENT;
-                imageWidth += wxCC_CUSTOM_IMAGE_MARGIN2 + 4;
+                imageOffset += wxCC_CUSTOM_IMAGE_MARGIN2 + 4;
             }
 
             DrawCaptionSelectionRect( dc,
@@ -276,6 +309,10 @@ void wxPGDefaultRenderer::Render( wxDC& dc, const wxRect& rect,
                                       propertyGrid->GetFontHeight()+(wxPG_CAPRECTYMARGIN*2) );
         }
     }
+
+    PostDrawCell(dc, propertyGrid, *cell, preDrawFlags);
+
+    return res;
 }
 
 wxSize wxPGDefaultRenderer::GetImageSize( const wxPGProperty* property,
@@ -362,6 +399,13 @@ void wxPGCell::SetFgCol( const wxColour& col )
     GetData()->SetFgCol(col);
 }
 
+void wxPGCell::SetFont( const wxFont& font )
+{
+    AllocExclusive();
+
+    GetData()->SetFont(font);
+}
+
 void wxPGCell::SetBgCol( const wxColour& col )
 {
     AllocExclusive();
@@ -388,6 +432,12 @@ void wxPGCell::MergeFrom( const wxPGCell& srcCell )
         data->SetBitmap(srcCell.GetBitmap());
 }
 
+void wxPGCell::SetEmptyData()
+{
+    AllocExclusive();
+}
+
+
 // -----------------------------------------------------------------------
 // wxPGProperty
 // -----------------------------------------------------------------------
@@ -449,6 +499,23 @@ void wxPGProperty::InitAfterAdded( wxPropertyGridPageState* pageState,
     wxPGProperty* parent = m_parent;
     bool parentIsRoot = parent->IsKindOf(CLASSINFO(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;
 
 #if wxPG_COMPATIBILITY_1_4
@@ -571,6 +638,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()
 {
@@ -642,12 +730,7 @@ wxPropertyGrid* wxPGProperty::GetGrid() 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
@@ -677,8 +760,17 @@ void wxPGProperty::GetDisplayInfo( unsigned int column,
 
     if ( !(flags & wxPGCellRenderer::ChoicePopup) )
     {
-        // Not painting listi of choice popups, so get text from property
-        cell = &GetCell(column);
+        // Not painting list of choice popups, so get text from property
+        if ( column != 1 || !IsValueUnspecified() || IsCategory() )
+        {
+            cell = &GetCell(column);
+        }
+        else
+        {
+            // Use special unspecified value cell
+            cell = &GetGrid()->GetUnspecifiedValueAppearance();
+        }
+
         if ( cell->HasText() )
         {
             *pString = cell->GetText();
@@ -899,8 +991,10 @@ wxString wxPGProperty::GetValueAsString( int argFlags ) const
     }
 #endif
 
+    wxPropertyGrid* pg = GetGrid();
+
     if ( IsValueUnspecified() )
-        return wxEmptyString;
+        return pg->GetUnspecifiedValueText(argFlags);
 
     if ( m_commonValue == -1 )
     {
@@ -910,7 +1004,6 @@ wxString wxPGProperty::GetValueAsString( int argFlags ) const
 
     //
     // Return common value's string representation
-    wxPropertyGrid* pg = GetGrid();
     const wxPGCommonValue* cv = pg->GetCommonValue(m_commonValue);
 
     if ( argFlags & wxPG_FULL_VALUE )
@@ -1007,7 +1100,8 @@ bool wxPGProperty::StringToValue( wxVariant& variant, const wxString& text, int
 
                     // Add only if editable or setting programmatically
                     if ( (argFlags & wxPG_PROGRAMMATIC_VALUE) ||
-                         !child->HasFlag(wxPG_PROP_DISABLED|wxPG_PROP_READONLY) )
+                         (!child->HasFlag(wxPG_PROP_DISABLED) &&
+                          !child->HasFlag(wxPG_PROP_READONLY)) )
                     {
                         if ( len > 0 )
                         {
@@ -1087,7 +1181,8 @@ bool wxPGProperty::StringToValue( wxVariant& variant, const wxString& text, int
                     wxVariant variant(oldChildValue);
 
                     if ( (argFlags & wxPG_PROGRAMMATIC_VALUE) ||
-                         !child->HasFlag(wxPG_PROP_DISABLED|wxPG_PROP_READONLY) )
+                         (!child->HasFlag(wxPG_PROP_DISABLED) &&
+                          !child->HasFlag(wxPG_PROP_READONLY)) )
                     {
                         wxString childName = child->GetBaseName();
 
@@ -1302,6 +1397,12 @@ void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
                 }
                 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() )
@@ -1341,16 +1442,23 @@ void wxPGProperty::SetValue( wxVariant value, wxVariant* pList, int flags )
         UpdateParentValues();
 
     //
-    // Update editor control
-    //
-
-    // We need to check for these, otherwise GetGrid() may fail.
+    // Update editor control.
     if ( flags & wxPG_SETVAL_REFRESH_EDITOR )
     {
-        RefreshEditor();
         wxPropertyGrid* pg = GetGridIfDisplayed();
         if ( pg )
+        {
+            wxPGProperty* selected = pg->GetSelectedProperty();
+
+            // Only refresh the control if this was selected, or
+            // this was some parent of selected, or vice versa)
+            if ( selected && (selected == this ||
+                              selected->IsSomeParent(this) ||
+                              this->IsSomeParent(selected)) )
+                RefreshEditor();
+
             pg->DrawItemAndValueRelated(this);
+        }
     }
 }
 
@@ -1360,7 +1468,7 @@ void wxPGProperty::SetValueInEvent( wxVariant value ) const
     GetGrid()->ValueChangeInEvent(value);
 }
 
-void wxPGProperty::SetFlagRecursively( FlagType flag, bool set )
+void wxPGProperty::SetFlagRecursively( wxPGPropertyFlags flag, bool set )
 {
     ChangeFlag(flag, set);
 
@@ -1422,6 +1530,31 @@ wxVariant wxPGProperty::GetDefaultValue() const
     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() )
@@ -1430,14 +1563,17 @@ void wxPGProperty::EnsureCells( unsigned int column )
         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;
@@ -1522,9 +1658,10 @@ wxPGCell& wxPGProperty::GetOrCreateCell( unsigned int column )
 }
 
 void wxPGProperty::SetBackgroundColour( const wxColour& colour,
-                                        bool recursively )
+                                        int flags )
 {
     wxPGProperty* firstProp = this;
+    bool recursively = flags & wxPG_RECURSE ? true : false;
 
     //
     // If category is tried to set recursively, skip it and only
@@ -1557,9 +1694,10 @@ void wxPGProperty::SetBackgroundColour( const wxColour& colour,
 }
 
 void wxPGProperty::SetTextColour( const wxColour& colour,
-                                  bool recursively )
+                                  int flags )
 {
     wxPGProperty* firstProp = this;
+    bool recursively = flags & wxPG_RECURSE ? true : false;
 
     //
     // If category is tried to set recursively, skip it and only
@@ -1680,7 +1818,7 @@ wxVariant wxPGProperty::GetAttributesAsList() const
 // Slots of utility flags are NULL
 const unsigned int gs_propFlagToStringSize = 14;
 
-static const wxChar* gs_propFlagToString[gs_propFlagToStringSize] = {
+static const wxChar* const gs_propFlagToString[gs_propFlagToStringSize] = {
     NULL,
     wxT("DISABLED"),
     wxT("HIDDEN"),
@@ -1840,8 +1978,14 @@ void wxPGProperty::SetChoiceSelection( int newValue )
     }
 }
 
-bool wxPGProperty::SetChoices( wxPGChoices& choices )
+bool wxPGProperty::SetChoices( const wxPGChoices& choices )
 {
+    // Property must be de-selected first (otherwise choices in
+    // the control would be de-synced with true choices)
+    wxPropertyGrid* pg = GetGrid();
+    if ( pg && pg->GetSelection() == this )
+        pg->ClearSelection();
+
     m_choices.Assign(choices);
 
     {
@@ -1885,6 +2029,32 @@ const wxPGEditor* wxPGProperty::GetEditorClass() const
     return editor;
 }
 
+bool wxPGProperty::Hide( bool hide, int flags )
+{
+    wxPropertyGrid* pg = GetGrid();
+    if ( pg )
+        return pg->HideProperty(this, hide, flags);
+
+    return DoHide( hide, flags );
+}
+
+bool wxPGProperty::DoHide( bool hide, int flags )
+{
+    if ( !hide )
+        ClearFlag( wxPG_PROP_HIDDEN );
+    else
+        SetFlag( wxPG_PROP_HIDDEN );
+
+    if ( flags & wxPG_RECURSE )
+    {
+        unsigned int i;
+        for ( i = 0; i < GetChildCount(); i++ )
+            Item(i)->DoHide(hide, flags | wxPG_RECURSE_STARTS);
+    }
+
+    return true;
+}
+
 bool wxPGProperty::HasVisibleChildren() const
 {
     unsigned int i;
@@ -2380,10 +2550,16 @@ void wxPGProperty::DeleteChildren()
 {
     wxPropertyGridPageState* state = m_parentState;
 
-    while ( GetChildCount() )
+    if ( !GetChildCount() )
+        return;
+
+    // Because deletion is sometimes deferred, we have to use
+    // this sort of code for enumerating the child properties.
+    unsigned int i = GetChildCount();
+    while ( i > 0 )
     {
-        wxPGProperty* child = Item(GetChildCount()-1);
-        state->DoDelete(child, true);
+        i--;
+        state->DoDelete(Item(i), true);
     }
 }
 
@@ -2574,9 +2750,31 @@ wxPropertyCategory::~wxPropertyCategory()
 wxString wxPropertyCategory::ValueToString( wxVariant& WXUNUSED(value),
                                             int WXUNUSED(argFlags) ) const
 {
+    if ( m_value.GetType() == wxPG_VARIANT_TYPE_STRING )
+        return m_value.GetString();
     return wxEmptyString;
 }
 
+wxString wxPropertyCategory::GetValueAsString( int argFlags ) const
+{
+#if wxPG_COMPATIBILITY_1_4
+    // This is backwards compatibility test
+    // That is, to make sure this function is not overridden
+    // (instead, ValueToString() should be).
+    if ( argFlags == 0xFFFF )
+    {
+        // Do not override! (for backwards compliancy)
+        return g_invalidStringContent;
+    }
+#endif
+
+    // Unspecified value is always empty string
+    if ( IsValueUnspecified() )
+        return wxEmptyString;
+
+    return wxPGProperty::GetValueAsString(argFlags);
+}
+
 int wxPropertyCategory::GetTextExtent( const wxWindow* wnd, const wxFont& font ) const
 {
     if ( m_textExtent > 0 )
@@ -2657,12 +2855,12 @@ wxPGChoiceEntry& wxPGChoices::AddAsSorted( const wxString& label, int value )
 
 // -----------------------------------------------------------------------
 
-void wxPGChoices::Add( const wxChar** labels, const ValArrItem* values )
+void wxPGChoices::Add( const wxChar* const* labels, const ValArrItem* values )
 {
     AllocExclusive();
 
     unsigned int itemcount = 0;
-    const wxChar** p = &labels[0];
+    const wxChar* const* p = &labels[0];
     while ( *p ) { p++; itemcount++; }
 
     unsigned int i;