Custom Property questions

Help
mspoerr
2009-08-04
2013-05-22
  • mspoerr
    mspoerr
    2009-08-04

    Hello,

    I have some questions regarding Custom and advanced Properties:
    1) How to make a Time Picker or DateTime Picker control? Is there a built-in property available? I was only able to do a DatePicker property.
    2) When using the first sample from "Creating New Properties" from http://wxpropgrid.sourceforge.net/docs/pg14/index.htm#newprops, I get a compiler error: C2512 - "no appropriate default constructor available". When modifying the contructor to a simple MyStringProperty(){}, the compiler is happy, but the initiaizing paramters are missing... What am I missing? I use exactly the code from the documentation.
    3) Is it possible to change the button Text or displaying an image inside the button when using a xxxAndButton Editor?

    Regards,
    /mspoerr

     
    • Jaakko Salli
      Jaakko Salli
      2009-08-05

      1) There is wxDateProperty (in advprops.h), which uses wxDatePickerCtrl. See docs and sample app for more info. Maybe this is exactly what you found. There is no time picker control in wx, so you probably would need to build one yourself.

      2) You probably need to add the default constructor in addition, not as replacement. In other words, the line

      MyStringProperty() { }

      was basically missing in the sample (not sure if all compilers require this).

      3) Yes, you should be able to return any control as the secondary wxWindow in the CreateControls(). This is already done, for instance, when using multiple buttons (buttons are held in an intermediate wxWindow).

      Maybe a good starting point for your custom property would be to actually copy paste existing wxDateProperty and wxPGDatePickerCtrlEditor code from advprops.h/cpp.

      HTH,
        Jaakko

       
    • mspoerr
      mspoerr
      2009-08-05

      Hello Jaakko,

      thank you for your answer. I still have some questions:
      ad 1) that means, that I can only choose the date and no time? I used a time picker in MFC. Therefore I am asking. I will use a simple string property instead.

      ad 2) I found the problem: In your sample, you mixed up "name" and "label" in the constructor declaration.

      3) May I ask for some more hints? I have no clue where to start. The only thing I want to achieve, is to replace the button text "..." with some other text.

      Thanks,
      Mathias

       
      • Jaakko Salli
        Jaakko Salli
        2009-08-05

        1) Yup, no time just date. If there was a time picker control in MFC (and by picker I mean a textctrl or combo box stylish control, similar to other pickers in wx), maybe there should be one in wxWidgets as well, especially if the MFC one was based on some native Windows control. Of course, such things are always easier said than done - somebody with time and motivation always has to do the grunt job. And yes, a simple string property is often the best way to go, and usually it doesn't even require you to actually create a custom property class.

        2) Yes there was a mistake, but not in the declaration, but in the call to super class ctor. Many thanks for reporting this! Based on your find, I was also able to fix another similar mistake in the same section.

        3) Easiest way to do this is probably to use wxLongStringProperty, and then call wxPropertyGrid::GetEditorControlSecondary() in wxEVT_PG_SELECTED handler (of course after checking that the selected property is correct) and cast the result to wxButton, and then change the text of the button. Also, if the text doesn't fit the button you can change the textctrl and button sizes at the same time. So, yeah, this is a bit hackish workaround, but still easiest to implement, I think. Let me know if you want to go custom editor route, I can give some pointers regarding that as well (basically you would just use wxPGTextCtrlAndButtonEditor code as template, and rebuild CreateControls(). Problem here is that the implementation is not straightforward as it calls helper functions in wxPropertyGrid. Main purpose of these helpers is to align textctrl and button pixel-perfectly).

        Regards,
        Jaakko

         
    • mspoerr
      mspoerr
      2009-08-05

      2) I always mix up the definitions ;)
      3) Thank you - I will try and come back.

      Thanks,
      Mathias

       
    • mspoerr
      mspoerr
      2009-08-05

      Jaakko,

      #3 works with your suggested solution.

      Thank you very much for your help!

      /Mathias

      P.S.: Fore someone elses reference:

      BEGIN_EVENT_TABLE(PropGridName, wxPanel)
      EVT_PG_SELECTED(wxID_ANY, PropGridName::OnPropertySelected)
      END_EVENT_TABLE()

      ....

      void PropGridName::OnPropertySelected( wxPropertyGridEvent& event )
      {
          wxPGProperty *property = event.GetProperty();

          // It may be NULL
          if ( !property )
              return;

          // Get name of changed property
           const wxString& name = property->GetName();

           if (name == "Name")
           {
              wxButton *button = (wxButton*)theGrid->GetEditorControlSecondary();
              button->SetLabel("Bla");
           }
      }

       
  • Noah Roberts
    Noah Roberts
    2009-09-29

    Did this method cut off the label?  I tried a slightly different manner and the label gets cut off because the button is made too small:

        struct MyButtonEditor : wxPGTextCtrlAndButtonEditor
        {
          wxPG_DECLARE_CREATECONTROLS

          WX_PG_DECLARE_EDITOR_CLASS(MyButtonEditor)
        };

        WX_PG_IMPLEMENT_EDITOR_CLASS(UnitEditor,MyButtonEditor,wxPGTextCtrlAndButtonEditor)

        wxPGWindowList MyButtonEditor::CreateControls( wxPropertyGrid* propGrid,
                                                       wxPGProperty* property,
                                                       const wxPoint& pos,
                                                       const wxSize& sz ) const
        {
            wxPGWindowList window_list = wxPGTextCtrlAndButtonEditor::CreateControls(propGrid, property, pos, sz);
            static_cast<wxButton*>(window_list.m_secondary)->SetLabel("unit");
            return window_list;
        }

     
  • Jaakko Salli
    Jaakko Salli
    2009-09-29

    Unfortunately the button size is currently fixed. It should still be possible to manually change position and size of wxTextCtrl and wxButton in CreateControls().

    Regards,
      Jaakko