]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/propgrid/sampleprops.cpp
Add missing wxEVT_COMMAND_TEXT_ENTER
[wxWidgets.git] / samples / propgrid / sampleprops.cpp
index 03978de880032f95906db234b3b8610145970be1..61438b748808e430e8a6c3e6e320e9c663e809d3 100644 (file)
@@ -4,9 +4,9 @@
 // Author:      Jaakko Salli
 // Modified by:
 // Created:     2006-03-05
-// RCS-ID:      $Id:
+// RCS-ID:      $Id$
 // Copyright:   (c) Jaakko Salli
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 // For compilers that support precompilation, includes "wx/wx.h".
@@ -22,6 +22,8 @@
     #include "wx/wx.h"
 #endif
 
+#include "wx/fontdlg.h"
+
 // -----------------------------------------------------------------------
 
 
@@ -46,9 +48,7 @@ bool operator == (const wxFontData&, const wxFontData&)
 // Custom version of wxFontProperty that also holds colour in the value.
 // Original version by Vladimir Vainer.
 
-#include <wx/fontdlg.h>
-
-WX_PG_IMPLEMENT_WXOBJECT_VARIANT_DATA(wxPGVariantDataFontData, wxFontData)
+IMPLEMENT_VARIANT_OBJECT_SHALLOWCMP(wxFontData)
 
 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFontDataProperty,wxFontProperty,
                                wxFontData,const wxFontData&,TextCtrlAndButton)
@@ -56,51 +56,68 @@ WX_PG_IMPLEMENT_PROPERTY_CLASS(wxFontDataProperty,wxFontProperty,
 wxFontDataProperty::wxFontDataProperty( const wxString& label, const wxString& name,
    const wxFontData& value ) : wxFontProperty(label,name,value.GetInitialFont())
 {
-    // Set initial value - should be done in a simpler way like this
-    // (instead of calling SetValue) in derived (wxObject) properties.
-    m_value_wxFontData << value;
-
-    wxFontData& fontData = wxFontDataFromVariant(m_value_wxFontData);
+    wxFontData fontData(value);
 
     // Fix value.
     fontData.SetChosenFont(value.GetInitialFont());
-    if ( !fontData.GetColour().Ok() )
+    if ( !fontData.GetColour().IsOk() )
         fontData.SetColour(*wxBLACK);
 
-    // Add extra children.
-    AddChild( new wxColourProperty(_("Colour"),wxPG_LABEL,
-                                   fontData.GetColour() ) );
+    // Set initial value - should be done in a simpler way like this
+    // (instead of calling SetValue) in derived (wxObject) properties.
+    m_value_wxFontData << value;
 
+    // Add extra children.
+    AddPrivateChild( new wxColourProperty(_("Colour"), wxPG_LABEL,
+                                          fontData.GetColour() ) );
 }
 
 wxFontDataProperty::~wxFontDataProperty () { }
 
 void wxFontDataProperty::OnSetValue()
 {
-    if ( !(&wxFontDataFromVariant(m_value)) )
+    if ( m_value.GetType() != "wxFontData" )
     {
-        wxFont* pFont = &wxFontFromVariant(m_value);
-        if ( pFont )
+        if ( m_value.GetType() == "wxFont" )
         {
+            wxFont font;
+            font << m_value;
             wxFontData fontData;
-            fontData.SetChosenFont(*pFont);
-            m_value = WXVARIANT(fontData);
+            fontData.SetChosenFont(font);
+            if ( !m_value_wxFontData.IsNull() )
+            {
+                wxFontData oldFontData;
+                oldFontData << m_value_wxFontData;
+                fontData.SetColour(oldFontData.GetColour());
+            }
+            else
+            {
+                fontData.SetColour(*wxBLACK);
+            }
+            wxVariant variant;
+            variant << fontData;
+            m_value_wxFontData = variant;
         }
         else
         {
             wxFAIL_MSG(wxT("Value to wxFontDataProperty must be eithe wxFontData or wxFont"));
         }
     }
+    else
+    {
+        // Set m_value to wxFont so that wxFontProperty methods will work
+        // correctly.
+        m_value_wxFontData = m_value;
+
+        wxFontData fontData;
+        fontData << m_value_wxFontData;
 
-    // Set m_value to wxFont so that wxFontProperty methods will work
-    // correctly.
-    m_value_wxFontData = m_value;
-    wxFontData& fontData = wxFontDataFromVariant(m_value_wxFontData);
+        wxFont font = fontData.GetChosenFont();
+        if ( !font.IsOk() )
+            font = wxFont(10,wxSWISS,wxNORMAL,wxNORMAL);
 
-    wxFont font = fontData.GetChosenFont();
-    if ( !font.Ok() )
-        font = wxFont(10,wxSWISS,wxNORMAL,wxNORMAL);
-    m_value = WXVARIANT(font);
+        m_value = WXVARIANT(font);
+    }
 }
 
 wxVariant wxFontDataProperty::DoGetValue() const
@@ -114,10 +131,10 @@ bool wxFontDataProperty::OnEvent( wxPropertyGrid* propgrid,
 {
     if ( propgrid->IsMainButtonEvent(event) )
     {
-        // Update value from last minute changes
-        PrepareValueForDialogEditing(propgrid);
+        wxVariant useValue = propgrid->GetUncommittedPropertyValue();
 
-        wxFontData& fontData = wxFontDataFromVariant(m_value_wxFontData);
+        wxFontData fontData;
+        fontData << useValue;
 
         fontData.SetInitialFont(fontData.GetChosenFont());
 
@@ -125,7 +142,9 @@ bool wxFontDataProperty::OnEvent( wxPropertyGrid* propgrid,
 
         if ( dlg.ShowModal() == wxID_OK )
         {
-            SetValueInEvent( wxFontDataToVariant(dlg.GetFontData()) );
+            wxVariant variant;
+            variant << dlg.GetFontData();
+            SetValueInEvent( variant );
             return true;
         }
     }
@@ -135,16 +154,19 @@ bool wxFontDataProperty::OnEvent( wxPropertyGrid* propgrid,
 void wxFontDataProperty::RefreshChildren()
 {
     wxFontProperty::RefreshChildren();
-    if ( GetChildCount() < 6 ) // Number is count of inherit prop's children + 1.
+    if ( GetChildCount() < 6 ) // Number is count of wxFontProperty's children + 1.
         return;
-    wxFontData& fontData = wxFontDataFromVariant(m_value_wxFontData);
+    wxFontData fontData; fontData << m_value_wxFontData;
     wxVariant variant; variant << fontData.GetColour();
     Item(6)->SetValue( variant );
 }
 
-void wxFontDataProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
+wxVariant wxFontDataProperty::ChildChanged( wxVariant& thisValue,
+                                            int childIndex,
+                                            wxVariant& childValue ) const
 {
-    wxFontData& fontData = wxFontDataFromVariant(thisValue);
+    wxFontData fontData;
+    fontData << thisValue;
     wxColour col;
     wxVariant variant;
 
@@ -155,11 +177,17 @@ void wxFontDataProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxV
             fontData.SetColour( col );
             break;
         default:
-            // Transfer between subset to superset.
-            variant = WXVARIANT(fontData.GetChosenFont());
+            // Transfer from subset to superset.
+            wxFont font = fontData.GetChosenFont();
+            variant = WXVARIANT(font);
             wxFontProperty::ChildChanged( variant, childIndex, childValue );
-            fontData.SetChosenFont(wxFontFromVariant(variant));
+            font << variant;
+            fontData.SetChosenFont(font);
     }
+
+    wxVariant newVariant;
+    newVariant << fontData;
+    return newVariant;
 }
 
 // -----------------------------------------------------------------------
@@ -173,8 +201,8 @@ wxSizeProperty::wxSizeProperty( const wxString& label, const wxString& name,
     const wxSize& value) : wxPGProperty(label,name)
 {
     SetValueI(value);
-    AddChild( new wxIntProperty(wxT("Width"),wxPG_LABEL,value.x) );
-    AddChild( new wxIntProperty(wxT("Height"),wxPG_LABEL,value.y) );
+    AddPrivateChild( new wxIntProperty(wxT("Width"),wxPG_LABEL,value.x) );
+    AddPrivateChild( new wxIntProperty(wxT("Height"),wxPG_LABEL,value.y) );
 }
 
 wxSizeProperty::~wxSizeProperty() { }
@@ -182,20 +210,25 @@ wxSizeProperty::~wxSizeProperty() { }
 void wxSizeProperty::RefreshChildren()
 {
     if ( !GetChildCount() ) return;
-    const wxSize& size = wxSizeFromVariant(m_value);
+    const wxSize& size = wxSizeRefFromVariant(m_value);
     Item(0)->SetValue( (long)size.x );
     Item(1)->SetValue( (long)size.y );
 }
 
-void wxSizeProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
+wxVariant wxSizeProperty::ChildChanged( wxVariant& thisValue,
+                                        int childIndex,
+                                        wxVariant& childValue ) const
 {
-    wxSize& size = wxSizeFromVariant(thisValue);
-    int val = wxPGVariantToInt(childValue);
+    wxSize& size = wxSizeRefFromVariant(thisValue);
+    int val = childValue.GetLong();
     switch ( childIndex )
     {
         case 0: size.x = val; break;
         case 1: size.y = val; break;
     }
+    wxVariant newVariant;
+    newVariant << size;
+    return newVariant;
 }
 
 // -----------------------------------------------------------------------
@@ -209,8 +242,8 @@ wxPointProperty::wxPointProperty( const wxString& label, const wxString& name,
     const wxPoint& value) : wxPGProperty(label,name)
 {
     SetValueI(value);
-    AddChild( new wxIntProperty(wxT("X"),wxPG_LABEL,value.x) );
-    AddChild( new wxIntProperty(wxT("Y"),wxPG_LABEL,value.y) );
+    AddPrivateChild( new wxIntProperty(wxT("X"),wxPG_LABEL,value.x) );
+    AddPrivateChild( new wxIntProperty(wxT("Y"),wxPG_LABEL,value.y) );
 }
 
 wxPointProperty::~wxPointProperty() { }
@@ -218,20 +251,25 @@ wxPointProperty::~wxPointProperty() { }
 void wxPointProperty::RefreshChildren()
 {
     if ( !GetChildCount() ) return;
-    const wxPoint& point = wxPointFromVariant(m_value);
+    const wxPoint& point = wxPointRefFromVariant(m_value);
     Item(0)->SetValue( (long)point.x );
     Item(1)->SetValue( (long)point.y );
 }
 
-void wxPointProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVariant& childValue ) const
+wxVariant wxPointProperty::ChildChanged( wxVariant& thisValue,
+                                         int childIndex,
+                                         wxVariant& childValue ) const
 {
-    wxPoint& point = wxPointFromVariant(thisValue);
-    int val = wxPGVariantToInt(childValue);
+    wxPoint& point = wxPointRefFromVariant(thisValue);
+    int val = childValue.GetLong();
     switch ( childIndex )
     {
         case 0: point.x = val; break;
         case 1: point.y = val; break;
     }
+    wxVariant newVariant;
+    newVariant << point;
+    return newVariant;
 }
 
 
@@ -239,7 +277,8 @@ void wxPointProperty::ChildChanged( wxVariant& thisValue, int childIndex, wxVari
 // Dirs Property
 // -----------------------------------------------------------------------
 
-WX_PG_IMPLEMENT_ARRAYSTRING_PROPERTY_WITH_VALIDATOR(wxDirsProperty,wxT(','),wxT("Browse"))
+WX_PG_IMPLEMENT_ARRAYSTRING_PROPERTY_WITH_VALIDATOR(wxDirsProperty, ',',
+                                                    "Browse")
 
 #if wxUSE_VALIDATORS
 
@@ -276,7 +315,7 @@ bool wxDirsProperty::OnCustomStringEdit( wxWindow* parent, wxString& value )
 // by replacing each ArrayDouble with ArrayXXX.
 //
 
-class wxArrayDoubleEditorDialog : public wxArrayEditorDialog
+class wxArrayDoubleEditorDialog : public wxPGArrayEditorDialog
 {
 public:
     wxArrayDoubleEditorDialog();
@@ -330,7 +369,7 @@ private:
     DECLARE_DYNAMIC_CLASS_NO_COPY(wxArrayDoubleEditorDialog)
 };
 
-IMPLEMENT_DYNAMIC_CLASS(wxArrayDoubleEditorDialog, wxArrayEditorDialog)
+IMPLEMENT_DYNAMIC_CLASS(wxArrayDoubleEditorDialog, wxPGArrayEditorDialog)
 
 //
 // Array dialog array access and manipulation
@@ -388,14 +427,14 @@ void wxArrayDoubleEditorDialog::ArraySwap( size_t first, size_t second )
 //
 
 wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog()
-    : wxArrayEditorDialog()
+    : wxPGArrayEditorDialog()
 {
     Init();
 }
 
 void wxArrayDoubleEditorDialog::Init()
 {
-    wxArrayEditorDialog::Init();
+    wxPGArrayEditorDialog::Init();
     SetPrecision(-1);
 }
 
@@ -406,7 +445,7 @@ wxArrayDoubleEditorDialog::wxArrayDoubleEditorDialog(wxWindow *parent,
                               long style,
                               const wxPoint& pos,
                               const wxSize& sz )
-                              : wxArrayEditorDialog()
+                              : wxPGArrayEditorDialog()
 {
     Init();
     Create(parent,message,caption,array,style,pos,sz);
@@ -423,7 +462,7 @@ bool wxArrayDoubleEditorDialog::Create(wxWindow *parent,
 
     m_array = array;
 
-    return wxArrayEditorDialog::Create (parent,message,caption,style,pos,sz);
+    return wxPGArrayEditorDialog::Create (parent,message,caption,style,pos,sz);
 }
 
 // -----------------------------------------------------------------------
@@ -452,7 +491,7 @@ bool operator == (const wxArrayDouble& a, const wxArrayDouble& b)
     return TRUE;
 }
 
-WX_PG_IMPLEMENT_VARIANT_DATA(wxPGVariantDataArrayDouble, wxArrayDouble)
+WX_PG_IMPLEMENT_VARIANT_DATA_DUMMY_EQ(wxArrayDouble)
 
 WX_PG_IMPLEMENT_PROPERTY_CLASS(wxArrayDoubleProperty,
                                wxPGProperty,
@@ -478,23 +517,36 @@ wxArrayDoubleProperty::wxArrayDoubleProperty (const wxString& label,
 
     m_delimiter = use_delimiter;
 
-    SetValue( wxArrayDoubleToVariant(array) );
+    SetValue( WXVARIANT(array) );
 }
 
 wxArrayDoubleProperty::~wxArrayDoubleProperty () { }
 
 void wxArrayDoubleProperty::OnSetValue()
 {
+    // Generate cached display string, to optimize grid drawing
     GenerateValueAsString( m_display, m_precision, true );
 }
 
-wxString wxArrayDoubleProperty::GetValueAsString( int arg_flags ) const
+wxString wxArrayDoubleProperty::ValueToString( wxVariant& value,
+                                               int argFlags ) const
 {
-    if ( !(arg_flags & wxPG_FULL_VALUE ))
-        return m_display;
-
     wxString s;
-    GenerateValueAsString(s,-1,false);
+
+    if ( argFlags & wxPG_FULL_VALUE )
+    {
+        GenerateValueAsString(s,-1,false);
+    }
+    else
+    {
+        //
+        // Display cached string only if value truly matches m_value
+        if ( value.GetData() == m_value.GetData() )
+            return m_display;
+        else
+            GenerateValueAsString( s, m_precision, true );
+    }
+
     return s;
 }
 
@@ -509,7 +561,7 @@ void wxArrayDoubleProperty::GenerateValueAsString( wxString& target, int prec, b
 
     target.Empty();
 
-    const wxArrayDouble& value = wxArrayDoubleFromVariant(m_value);
+    const wxArrayDouble& value = wxArrayDoubleRefFromVariant(m_value);
 
     for ( i=0; i<value.GetCount(); i++ )
     {
@@ -529,10 +581,10 @@ bool wxArrayDoubleProperty::OnEvent( wxPropertyGrid* propgrid,
 {
     if ( propgrid->IsMainButtonEvent(event) )
     {
-        wxArrayDouble& value = wxArrayDoubleFromVariant(m_value);
-
         // Update the value in case of last minute changes
-        PrepareValueForDialogEditing(propgrid);
+        wxVariant useValue = propgrid->GetUncommittedPropertyValue();
+
+        wxArrayDouble& value = wxArrayDoubleRefFromVariant(useValue);
 
         // Create editor dialog.
         wxArrayDoubleEditorDialog dlg;
@@ -544,7 +596,7 @@ bool wxArrayDoubleProperty::OnEvent( wxPropertyGrid* propgrid,
         int res = dlg.ShowModal();
         if ( res == wxID_OK && dlg.IsModified() )
         {
-            SetValueInEvent( wxArrayDoubleToVariant(dlg.GetArray()) );
+            SetValueInEvent( WXVARIANT(dlg.GetArray()) );
             return true;
         }
         return false;
@@ -566,7 +618,7 @@ bool wxArrayDoubleProperty::StringToValue( wxVariant& variant, const wxString& t
 
     WX_PG_TOKENIZER1_BEGIN(text,delimiter)
 
-        if ( token.length() )
+        if ( !token.empty() )
         {
 
             // If token was invalid, exit the loop now
@@ -591,9 +643,9 @@ bool wxArrayDoubleProperty::StringToValue( wxVariant& variant, const wxString& t
         return false;
     }
 
-    if ( !(wxArrayDoubleFromVariant(m_value) == new_array) )
+    if ( !(wxArrayDoubleRefFromVariant(m_value) == new_array) )
     {
-        variant = wxArrayDoubleToVariant(new_array);
+        variant = WXVARIANT(new_array);
         return true;
     }