]> git.saurik.com Git - wxWidgets.git/blobdiff - samples/propgrid/propgrid.cpp
MinGW-w64 provides isfinite() in both 32 and 64 bit builds.
[wxWidgets.git] / samples / propgrid / propgrid.cpp
index 431199b2496a52975c6737591cfcea15c1fc832c..5b0d08e0f3a9ad504e2d95ddcc00b6efa9985412 100644 (file)
@@ -4,7 +4,6 @@
 // Author:      Jaakko Salli
 // Modified by:
 // Created:     2004-09-25
-// RCS-ID:      $Id$
 // Copyright:   (c) Jaakko Salli
 // Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #include <wx/artprov.h>
 
-#ifndef __WXMSW__
+#ifndef wxHAS_IMAGES_IN_RESOURCES
     #include "../sample.xpm"
 #endif
 
+#include <wx/popupwin.h>
+
 // -----------------------------------------------------------------------
 // wxSampleMultiButtonEditor
 //   A sample editor class that has multiple buttons.
@@ -128,7 +129,7 @@ bool wxSampleMultiButtonEditor::OnEvent( wxPropertyGrid* propGrid,
                                          wxWindow* ctrl,
                                          wxEvent& event ) const
 {
-    if ( event.GetEventType() == wxEVT_COMMAND_BUTTON_CLICKED )
+    if ( event.GetEventType() == wxEVT_BUTTON )
     {
         wxPGMultiButton* buttons = (wxPGMultiButton*) propGrid->GetEditorControlSecondary();
 
@@ -198,255 +199,6 @@ private:
 
 #endif // wxUSE_VALIDATORS
 
-// -----------------------------------------------------------------------
-// AdvImageFile Property
-// -----------------------------------------------------------------------
-
-class wxMyImageInfo;
-
-WX_DECLARE_OBJARRAY(wxMyImageInfo, wxArrayMyImageInfo);
-
-class wxMyImageInfo
-{
-public:
-    wxString    m_path;
-    wxBitmap*   m_pThumbnail1; // smaller thumbnail
-    wxBitmap*   m_pThumbnail2; // larger thumbnail
-
-    wxMyImageInfo ( const wxString& str )
-    {
-        m_path = str;
-        m_pThumbnail1 = (wxBitmap*) NULL;
-        m_pThumbnail2 = (wxBitmap*) NULL;
-    }
-    virtual ~wxMyImageInfo()
-    {
-        if ( m_pThumbnail1 )
-            delete m_pThumbnail1;
-        if ( m_pThumbnail2 )
-            delete m_pThumbnail2;
-    }
-
-};
-
-
-#include <wx/arrimpl.cpp>
-WX_DEFINE_OBJARRAY(wxArrayMyImageInfo);
-
-wxArrayMyImageInfo  g_myImageArray;
-
-
-// Preferred thumbnail height.
-#define PREF_THUMBNAIL_HEIGHT       64
-
-
-wxPGChoices wxAdvImageFileProperty::ms_choices;
-
-WX_PG_IMPLEMENT_PROPERTY_CLASS(wxAdvImageFileProperty,wxFileProperty,
-                               wxString,const wxString&,ChoiceAndButton)
-
-
-wxAdvImageFileProperty::wxAdvImageFileProperty( const wxString& label,
-                                                          const wxString& name,
-                                                          const wxString& value)
-    : wxFileProperty(label,name,value)
-{
-    m_wildcard = wxPGGetDefaultImageWildcard();
-
-    m_index = -1;
-
-    m_pImage = (wxImage*) NULL;
-
-    // Only show names.
-    m_flags &= ~(wxPG_PROP_SHOW_FULL_FILENAME);
-}
-
-wxAdvImageFileProperty::~wxAdvImageFileProperty ()
-{
-    // Delete old image
-    wxDELETE(m_pImage);
-}
-
-void wxAdvImageFileProperty::OnSetValue()
-{
-    wxFileProperty::OnSetValue();
-
-    // Delete old image
-    wxDELETE(m_pImage);
-
-    wxString imagename = GetValueAsString(0);
-
-    if ( imagename.length() )
-    {
-        wxFileName filename = GetFileName();
-        size_t prevCount = g_myImageArray.GetCount();
-        int index = ms_choices.Index(imagename);
-
-        // If not in table, add now.
-        if ( index == wxNOT_FOUND )
-        {
-            ms_choices.Add( imagename );
-            g_myImageArray.Add( new wxMyImageInfo( filename.GetFullPath() ) );
-
-            index = g_myImageArray.GetCount() - 1;
-        }
-
-        // If no thumbnail ready, then need to load image.
-        if ( !g_myImageArray[index].m_pThumbnail2 )
-        {
-            // Load if file exists.
-            if ( filename.FileExists() )
-                m_pImage = new wxImage( filename.GetFullPath() );
-        }
-
-        m_index = index;
-
-        wxPropertyGrid* pg = GetGrid();
-        wxWindow* control = pg->GetEditorControl();
-
-        if ( pg->GetSelection() == this && control )
-        {
-            wxString name = GetValueAsString(0);
-
-            if ( g_myImageArray.GetCount() != prevCount )
-            {
-                wxASSERT( g_myImageArray.GetCount() == (prevCount+1) );
-
-                // Add to the control's array.
-                // (should be added to own array earlier)
-
-                if ( control )
-                    GetEditorClass()->InsertItem(control, name, -1);
-            }
-
-            if ( control )
-               GetEditorClass()->UpdateControl(this, control);
-        }
-    }
-    else
-        m_index = -1;
-}
-
-bool wxAdvImageFileProperty::IntToValue( wxVariant& variant, int number, int WXUNUSED(argFlags) ) const
-{
-    wxASSERT( number >= 0 );
-    return StringToValue( variant, ms_choices.GetLabel(number), wxPG_FULL_VALUE );
-}
-
-bool wxAdvImageFileProperty::OnEvent( wxPropertyGrid* propgrid, wxWindow* primary,
-   wxEvent& event )
-{
-    if ( propgrid->IsMainButtonEvent(event) )
-    {
-        return wxFileProperty::OnEvent(propgrid,primary,event);
-    }
-    return false;
-}
-
-wxSize wxAdvImageFileProperty::OnMeasureImage( int item ) const
-{
-    if ( item == -1 )
-        return wxPG_DEFAULT_IMAGE_SIZE;
-
-    return wxSize(PREF_THUMBNAIL_HEIGHT,PREF_THUMBNAIL_HEIGHT);
-}
-
-void wxAdvImageFileProperty::LoadThumbnails( size_t index )
-{
-    wxMyImageInfo& mii = g_myImageArray[index];
-
-    if ( !mii.m_pThumbnail2 )
-    {
-        wxFileName filename = GetFileName();
-
-        if ( !m_pImage || !m_pImage->Ok() ||
-             filename != mii.m_path
-           )
-        {
-            if ( m_pImage )
-                delete m_pImage;
-            m_pImage = new wxImage( mii.m_path );
-        }
-
-        if ( m_pImage && m_pImage->Ok() )
-        {
-            int im_wid = m_pImage->GetWidth();
-            int im_hei = m_pImage->GetHeight();
-            if ( im_hei > PREF_THUMBNAIL_HEIGHT )
-            {
-                // TNW = (TNH*IW)/IH
-                im_wid = (PREF_THUMBNAIL_HEIGHT*m_pImage->GetWidth())/m_pImage->GetHeight();
-                im_hei = PREF_THUMBNAIL_HEIGHT;
-            }
-
-            m_pImage->Rescale( im_wid, im_hei );
-
-            mii.m_pThumbnail2 = new wxBitmap( *m_pImage );
-
-            wxSize cis = GetParentState()->GetGrid()->GetImageSize();
-            m_pImage->Rescale ( cis.x, cis.y );
-
-            mii.m_pThumbnail1 = new wxBitmap( *m_pImage );
-
-        }
-
-        wxDELETE(m_pImage);
-    }
-}
-
-void wxAdvImageFileProperty::OnCustomPaint( wxDC& dc,
-                                                 const wxRect& rect,
-                                                 wxPGPaintData& pd )
-{
-    int index = m_index;
-    if ( pd.m_choiceItem >= 0 )
-        index = pd.m_choiceItem;
-
-    //wxLogDebug(wxT("%i"),index);
-
-    if ( index >= 0 )
-    {
-        LoadThumbnails(index);
-
-        // Is this a measure item call?
-        if ( rect.x < 0 )
-        {
-            // Variable height
-            //pd.m_drawnHeight = PREF_THUMBNAIL_HEIGHT;
-            wxBitmap* pBitmap = (wxBitmap*)g_myImageArray[index].m_pThumbnail2;
-            if ( pBitmap )
-                pd.m_drawnHeight = pBitmap->GetHeight();
-            else
-                pd.m_drawnHeight = 16;
-            return;
-        }
-
-        // Draw the thumbnail
-
-        wxBitmap* pBitmap;
-
-        if ( pd.m_choiceItem >= 0 )
-            pBitmap = (wxBitmap*)g_myImageArray[index].m_pThumbnail2;
-        else
-            pBitmap = (wxBitmap*)g_myImageArray[index].m_pThumbnail1;
-
-        if ( pBitmap )
-        {
-            dc.DrawBitmap ( *pBitmap, rect.x, rect.y, FALSE );
-
-            // Tell the caller how wide we drew.
-            pd.m_drawnWidth = pBitmap->GetWidth();
-
-            return;
-        }
-    }
-
-    // No valid file - just draw a white box.
-    dc.SetBrush ( *wxWHITE_BRUSH );
-    dc.DrawRectangle ( rect );
-}
-
-
 // -----------------------------------------------------------------------
 // wxVectorProperty
 // -----------------------------------------------------------------------
@@ -571,7 +323,7 @@ public:
         wxString s = ::wxGetSingleChoice(wxT("Message"),
                                          wxT("Caption"),
                                          m_choices.GetLabels());
-        if ( s.length() )
+        if ( !s.empty() )
         {
             SetValue(s);
             return true;
@@ -680,7 +432,9 @@ enum
     ID_ENABLELABELEDITING,
     ID_VETOCOLDRAG,
     ID_SHOWHEADER,
-    ID_ONEXTENDEDKEYNAV
+    ID_ONEXTENDEDKEYNAV,
+    ID_SHOWPOPUP,
+    ID_POPUPGRID
 };
 
 // -----------------------------------------------------------------------
@@ -802,6 +556,7 @@ BEGIN_EVENT_TABLE(FormMain, wxFrame)
     EVT_MENU( ID_RUNMINIMAL, FormMain::OnRunMinimalClick )
 
     EVT_CONTEXT_MENU( FormMain::OnContextMenu )
+    EVT_BUTTON(ID_SHOWPOPUP, FormMain::OnShowPopup)
 END_EVENT_TABLE()
 
 // -----------------------------------------------------------------------
@@ -899,7 +654,7 @@ void FormMain::OnPropertyGridChanging( wxPropertyGridEvent& event )
             event.Veto();
 
             // Since we ask a question, it is better if we omit any validation
-            // failure behavior.
+            // failure behaviour.
             event.SetValidationFailureBehavior(0);
         }
     }
@@ -960,7 +715,7 @@ void FormMain::OnPropertyGridChange( wxPropertyGridEvent& event )
     if ( name == wxT("Font") )
     {
         wxFont font = wxANY_AS(value, wxFont);
-        wxASSERT( font.Ok() );
+        wxASSERT( font.IsOk() );
 
         m_pPropGridManager->SetFont( font );
     }
@@ -1347,7 +1102,7 @@ void FormMain::PopulateWithStandardItems ()
     pg->SetPropertyAttribute(wxT("Y"), wxPG_ATTR_UNITS, wxT("Pixels") );
     pg->SetPropertyHelpString(wxT("Y"), wxT("This property uses \"Units\" attribute.") );
 
-    const wxChar* disabledHelpString = wxT("This property is simply disabled. Inorder to have label disabled as well, ")
+    const wxChar* disabledHelpString = wxT("This property is simply disabled. In order to have label disabled as well, ")
                                        wxT("you need to set wxPG_EX_GREY_LABEL_WHEN_DISABLED using SetExtraStyle.");
 
     pg->Append( new wxPropertyCategory(wxT("Environment"),wxPG_LABEL) );
@@ -1381,12 +1136,6 @@ void FormMain::PopulateWithStandardItems ()
         wxT("with custom action (dir dialog popup) defined.")
         );
 
-    pg->Append( new wxAdvImageFileProperty(wxT("AdvImageFileProperty"),wxPG_LABEL) );
-    pg->SetPropertyHelpString( wxT("AdvImageFileProperty"),
-        wxT("This demonstrates wxAdvImageFileProperty class defined in this sample app. ")
-        wxT("Button can be used to add new images to the popup list.")
-        );
-
     wxArrayDouble arrdbl;
     arrdbl.Add(-1.0);
     arrdbl.Add(-0.5);
@@ -1514,13 +1263,19 @@ void FormMain::PopulateWithExamples ()
 #endif
 
     pid = pg->Append( new wxColourProperty(wxT("ColourProperty"),wxPG_LABEL,*wxRED) );
-    //pg->SetPropertyAttribute(pid,wxPG_COLOUR_ALLOW_CUSTOM,false);
     pg->SetPropertyEditor( wxT("ColourProperty"), wxPGEditor_ComboBox );
     pg->GetProperty(wxT("ColourProperty"))->SetAutoUnspecified(true);
     pg->SetPropertyHelpString( wxT("ColourProperty"),
         wxT("wxPropertyGrid::SetPropertyEditor method has been used to change ")
         wxT("editor of this property to wxPGEditor_ComboBox)"));
 
+    pid = pg->Append( new wxColourProperty("ColourPropertyWithAlpha",
+                                           wxPG_LABEL,
+                                           wxColour(15, 200, 95, 128)) );
+    pg->SetPropertyAttribute("ColourPropertyWithAlpha", "HasAlpha", true);
+    pg->SetPropertyHelpString("ColourPropertyWithAlpha",
+        "Attribute \"HasAlpha\" is set to true for this property.");
+
     //
     // This demonstrates using alternative editor for colour property
     // to trigger colour dialog directly from button.
@@ -2094,6 +1849,9 @@ void FormMain::FinalizePanel( bool wasCreated )
     m_topSizer->Add( new wxButton(m_panel, wxID_ANY,
                      wxS("Should be able to move here with Tab")),
                      0, wxEXPAND );
+    m_topSizer->Add( new wxButton(m_panel, ID_SHOWPOPUP,
+                     wxS("Show Popup")),
+                     0, wxEXPAND );
 
     m_panel->SetSizer( m_topSizer );
     m_topSizer->SetSizeHints( m_panel );
@@ -2181,7 +1939,7 @@ void FormMain::CreateGrid( int style, int extraStyle )
 
     pgman->SetExtraStyle(extraStyle);
 
-    // This is the default validation failure behavior
+    // This is the default validation failure behaviour
     m_pPropGridManager->SetValidationFailureBehavior( wxPG_VFB_MARK_CELL |
                                                       wxPG_VFB_SHOW_MESSAGEBOX );
 
@@ -2273,7 +2031,7 @@ FormMain::FormMain(const wxString& title, const wxPoint& pos, const wxSize& size
     wxMenu *menuTools2 = new wxMenu;
     wxMenu *menuHelp = new wxMenu;
 
-    menuHelp->Append(ID_ABOUT, wxT("&About..."), wxT("Show about dialog") );
+    menuHelp->Append(ID_ABOUT, wxT("&About"), wxT("Show about dialog") );
 
     menuTools1->Append(ID_APPENDPROP, wxT("Append New Property") );
     menuTools1->Append(ID_APPENDCAT, wxT("Append New Category\tCtrl-S") );
@@ -3361,3 +3119,172 @@ void FormMain::OnIdle( wxIdleEvent& event )
 }
 
 // -----------------------------------------------------------------------
+
+
+wxPGProperty* GetRealRoot(wxPropertyGrid *grid)
+{
+    wxPGProperty *property = grid->GetRoot();
+    return property ? grid->GetFirstChild(property) : NULL;
+}
+
+void GetColumnWidths(wxClientDC &dc, wxPropertyGrid *grid, wxPGProperty *root, int width[3])
+{
+    wxPropertyGridPageState *state = grid->GetState();
+
+    width[0] =
+    width[1] =
+    width[2] = 0;
+    int minWidths[3] = { state->GetColumnMinWidth(0),
+                         state->GetColumnMinWidth(1),
+                         state->GetColumnMinWidth(2) };
+    unsigned ii;
+    for (ii = 0; ii < root->GetChildCount(); ++ii)
+    {
+        wxPGProperty* p = root->Item(ii);
+
+        width[0] = wxMax(width[0], state->GetColumnFullWidth(dc, p, 0));
+        width[1] = wxMax(width[1], state->GetColumnFullWidth(dc, p, 1));
+        width[2] = wxMax(width[2], state->GetColumnFullWidth(dc, p, 2));
+    }
+    for (ii = 0; ii < root->GetChildCount(); ++ii)
+    {
+        wxPGProperty* p = root->Item(ii);
+        if (p->IsExpanded())
+        {
+            int w[3];
+            GetColumnWidths(dc, grid, p, w);
+            width[0] = wxMax(width[0], w[0]);
+            width[1] = wxMax(width[1], w[1]);
+            width[2] = wxMax(width[2], w[2]);
+        }
+    }
+
+    width[0] = wxMax(width[0], minWidths[0]);
+    width[1] = wxMax(width[1], minWidths[1]);
+    width[2] = wxMax(width[2], minWidths[2]);
+}
+
+void GetColumnWidths(wxPropertyGrid *grid, wxPGProperty *root, int width[3])
+{
+    wxClientDC dc(grid);
+    dc.SetFont(grid->GetFont());
+    GetColumnWidths(dc, grid, root, width);
+}
+
+void SetMinSize(wxPropertyGrid *grid)
+{
+    wxPGProperty *p = GetRealRoot(grid);
+    wxPGProperty *first = grid->wxPropertyGridInterface::GetFirst(wxPG_ITERATE_ALL);
+    wxPGProperty *last = grid->GetLastItem(wxPG_ITERATE_DEFAULT);
+    wxRect rect = grid->GetPropertyRect(first, last);
+    int height = rect.height + 2 * grid->GetVerticalSpacing();
+
+    // add some height when the root item is collapsed,
+    // this is needed to prevent the vertical scroll from showing
+    if (!grid->IsPropertyExpanded(p))
+        height += 2 * grid->GetVerticalSpacing();
+
+    int width[3];
+    GetColumnWidths(grid, grid->GetRoot(), width);
+    rect.width = width[0]+width[1]+width[2];
+
+    int minWidth = (wxSystemSettings::GetMetric(wxSYS_SCREEN_X, grid->GetParent())*3)/2;
+    int minHeight = (wxSystemSettings::GetMetric(wxSYS_SCREEN_Y, grid->GetParent())*3)/2;
+
+    wxSize size(wxMin(minWidth, rect.width + grid->GetMarginWidth()), wxMin(minHeight, height));
+    grid->SetMinSize(size);
+
+    int proportions[3];
+    proportions[0] = static_cast<int>(floor((double)width[0]/size.x*100.0+0.5));
+    proportions[1] = static_cast<int>(floor((double)width[1]/size.x*100.0+0.5));
+    proportions[2]= wxMax(100 - proportions[0] - proportions[1], 0);
+    grid->SetColumnProportion(0, proportions[0]);
+    grid->SetColumnProportion(1, proportions[1]);
+    grid->SetColumnProportion(2, proportions[2]);
+    grid->ResetColumnSizes(true);
+}
+
+struct PropertyGridPopup : wxPopupWindow
+{
+    wxScrolledWindow *m_panel;
+    wxPropertyGrid *m_grid;
+    wxBoxSizer *m_sizer;
+
+    PropertyGridPopup(wxWindow *parent) : wxPopupWindow(parent, wxBORDER_NONE|wxWANTS_CHARS)
+    {
+        m_panel = new wxScrolledWindow(this, wxID_ANY, wxDefaultPosition, wxSize(200, 200));
+        m_grid = new wxPropertyGrid(m_panel, ID_POPUPGRID, wxDefaultPosition, wxSize(400,400), wxPG_SPLITTER_AUTO_CENTER);
+        m_grid->SetColumnCount(3);
+
+        wxPGProperty *prop=m_grid->Append(new wxStringProperty("test_name", wxPG_LABEL, "test_value"));
+        m_grid->SetPropertyAttribute(prop, wxT("Units"), "type");
+        wxPGProperty *prop1 = m_grid->AppendIn(prop, new wxStringProperty("sub_name1", wxPG_LABEL, "sub_value1"));
+
+        m_grid->AppendIn(prop1, new wxSystemColourProperty(wxT("Cell Colour"),wxPG_LABEL, m_grid->GetGrid()->GetCellBackgroundColour()));
+        wxPGProperty *prop2 = m_grid->AppendIn(prop, new wxStringProperty("sub_name2", wxPG_LABEL, "sub_value2"));
+        m_grid->AppendIn(prop2, new wxStringProperty("sub_name21", wxPG_LABEL, "sub_value21"));
+
+        wxArrayDouble arrdbl;
+        arrdbl.Add(-1.0); arrdbl.Add(-0.5); arrdbl.Add(0.0); arrdbl.Add(0.5); arrdbl.Add(1.0);
+        m_grid->AppendIn(prop, new wxArrayDoubleProperty(wxT("ArrayDoubleProperty"),wxPG_LABEL,arrdbl) );
+        m_grid->AppendIn(prop, new wxFontProperty(wxT("Font"),wxPG_LABEL));
+        m_grid->AppendIn(prop2, new wxStringProperty("sub_name22", wxPG_LABEL, "sub_value22"));
+        m_grid->AppendIn(prop2, new wxStringProperty("sub_name23", wxPG_LABEL, "sub_value23"));
+        prop2->SetExpanded(false);
+
+        ::SetMinSize(m_grid);
+
+        m_sizer = new wxBoxSizer( wxVERTICAL );
+        m_sizer->Add(m_grid, 0, wxALL | wxEXPAND, 0);
+        m_panel->SetAutoLayout(true);
+        m_panel->SetSizer(m_sizer);
+        m_sizer->Fit(m_panel);
+        m_sizer->Fit(this);
+    }
+
+    void OnCollapse(wxPropertyGridEvent& WXUNUSED(event))
+    {
+        wxLogMessage("OnCollapse");
+        Fit();
+    }
+
+    void OnExpand(wxPropertyGridEvent& WXUNUSED(event))
+    {
+        wxLogMessage("OnExpand");
+        Fit();
+    }
+
+    void Fit()
+    {
+        ::SetMinSize(m_grid);
+        m_sizer->Fit(m_panel);
+        wxPoint pos = GetScreenPosition();
+        wxSize size = m_panel->GetScreenRect().GetSize();
+        SetSize(pos.x, pos.y, size.x, size.y);
+    }
+
+    DECLARE_CLASS(PropertyGridPopup)
+    DECLARE_EVENT_TABLE()
+};
+
+IMPLEMENT_CLASS(PropertyGridPopup, wxPopupWindow)
+BEGIN_EVENT_TABLE(PropertyGridPopup, wxPopupWindow)
+    EVT_PG_ITEM_COLLAPSED(ID_POPUPGRID, PropertyGridPopup::OnCollapse)
+    EVT_PG_ITEM_EXPANDED(ID_POPUPGRID, PropertyGridPopup::OnExpand)
+END_EVENT_TABLE()
+
+
+void FormMain::OnShowPopup(wxCommandEvent& WXUNUSED(event))
+{
+    static PropertyGridPopup *popup = NULL;
+    if ( popup )
+    {
+        delete popup;
+        popup = NULL;
+        return;
+    }
+    popup = new PropertyGridPopup(this);
+    wxPoint pt = wxGetMousePosition();
+    popup->Position(pt, wxSize(0, 0));
+    popup->Show();
+}