Wxs ext tutorial2 properties

From Code::Blocks

WARNING: NOT YET FINISHED

Preface

In previous tutorial we created simple plugin which added one item into wxSmith's palette. After loading this plugin into Code::Blocks we were able to add this item into resource and set some basic properties like size or position. In this tutorial we will add more customization for this item by adding it's specific properties. But before we do this, I need to explain some things.


How wxSmith creates properties

The final set of properties is result of merging three basic sets:

  • Some standard properties which are used in almost any widget (size, position, font etc)
  • Properties added by parent control like border size inside sizers or notebook page name
  • Properties which are specific to one widget only


Standard properties

The first set is added through constructor. In the code from previous tutorial, constructor is as follows:

wxsChart::wxsChart(wxsItemResData* Data):
    wxsWidget(
        Data,               // Data passed to constructor
        &Reg.Info,          // Info taken from Registering object previously created
        NULL,               // Structure describing events, we have no events for wxChart
        wxsChartStyles)     // Structure describing styles
{
}

so the only thing we see here is set of styles (that's one of standard properties). Because it's rather complex and require extra initialization, this property must be specified separately. If there was NULL instead of wxsChartStyles, wxChart would have no style property. Other standard properties are given through flags, which are passed through PropertiesFlags argument in constructor of wxsWidget:

wxsWidget(
    wxsItemResData* Data,
    const wxsItemInfo* Info,
    const wxsEventDesc* EventArray = NULL,
    const wxsStyleSet* StyleSet=NULL,
    long PropertiesFlags = flWidget);

We can see here that default value for this argument is flWidget. It means that standard properties which are usually used by any kind of widget will be added into properties set. That value is silently passed to wxsWidget's constructor in case of wxsChart class so we have all those standard properties in property browser without even line of code. Individual flags which can be used are:

  • flVariable - allow variable and Is Member flag
  • flId - allow identifier
  • flPosition - allow position
  • flSize - allow size
  • flEnabled - allow Enabled switch
  • flFocused - allow Focused switch
  • flHidden - allow Hidden switch
  • flColours - allow foreground and background colours
  • flToolTip - allow tooltip
  • flFont - allow font
  • flHelpText - allow help text
  • flSubclass - allow changing of class name (the "subclass" term comes from XRC terminology)
  • flMinMaxSize - allow MinSize and MaxSize

And when we want for example only variable and position, we would pass flVariable|flPosition instead of flWidget into wxsWidget's contructor. This technique is not recommended though, because it may introduce some problems (just imagine widget which have no variable of any kind - there's problem even with creating it).

Some properties are also filtered depending on some circumstances:

  • flMinMaxSize will be disabled when XRC file is used
  • flVariable / flId are disabled for top-most items (like wxDialog and wxFrame)
  • flHidden does not affect item when it's shown inside editor (but it's used for exact previews shown after pressing Preview button)


Properties added by parent control

Some parent controls require extra information for each of it's children. This data is included in set of properties in property browser and child item have no control of it. Properties added in this way are:

  • Settings for item inside sizer (border size, border flags, proportion and some flags like expand')
  • Names of pages and switch for default page in wxNotebook, wxChoicebook and wxListbook


Properties specific to only one item

Most of items have their own proeprties which can not be generalized and provided as common ones. These properties are added in special function which in case of widgets is declared as:

void OnEnumWidgetProperties(long Flags);

wxSmith has complex property managment system which handle most of work related to properties automatically, including operating on property grid and XRC files. Usually one variable is mapped to one property (in case of most complex properties such as position, special structure is used to keep data in one variable). Binding between variable and property managment system is done in function mentioned above. It require generating one variable for one property and calling Property function for it. Usually ot looks like this:

void ClassName::OnEnumWidgetProperties(long Flags)
{
    static wxsBoolProperty SomePropertyVariable(
                            _("Name in property grid"), _T("name_in_xrc_file"),
                            wxsOFFSET(ClassName,ClassMemberVariable), false );
    Property(SomePropertyValue);
}

Properties may be added in this way or may be defined through macros. Using macro, the code adding bool property will look like this:

void ClassName::OnEnumWidgetProperties(long Flags)
{
    WXS_BOOL(ClassName,ClassMemberVariable,_("Name in property grid"), _T("name_in_xrc_file"), false);
}

Both methods will give the same result (macros simply generate static variable and call Property on it). So it's up to you to choose one of them. I preffer macros because they give cleaner view, but smoetimes they may hide some extra parameters and may not be preffered by other coders (not everyone likes c macros ;) ). So I'll describe both methods.

Before we begin detailed description of available properties, I'll have to make few remarks:

  • Variables used in properties must be members of the class which adds them (it's possible to use members of other class, but it's advanced technique and won't be covered in this tutorial). So it's not possible to create property from pointer to variable (for example when we have bool* member in class and want to make property for value pointer by it). The technique which allow such things will be described later in this tutorial but won't use wxSmith's property managment system.
  • Objects which create binging between property variable and property managment system (like SomePropertyVariable in example above) should be declared static or at least should exist as long as all objects using this property are not deleted. Simple static modifier eliminates all problems.


List of available properties

Here you have list of properties supported by wxSmith's property managment system. Each one of them have exact type of variable it supports and examples of using this property. In those examples, ClassName represents name of class containing properties, ClassMemberVariable represents name of member variable inside ClassName (note that current there's no type checking so you should check if valid types of variables are used).


Here you have list of properties supported by wxSmith's property managment system. Each one of them have exact type of variable it supports and examples of using this property. In those examples, ClassName represents name of class containing properties and ClassMemberVariable represents name of member variable inside ClassName (note that current there's no type checking so you should check if valid types of variables are used).


bool

Variable type: bool

Example using macros:

WXS_BOOL(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    false);                       // Default value

Example using objects:

static wxsBoolProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable), // Offset from class beginning to variable
    false );                                  // Default value
Property(SomePropertyValue);


wxString

Variable type: wxString

Example using macros (for one-line strings):

WXS_SHORT_STRING(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    false,                        // Default value
    false);                       // If set to true, empty strings won't be stored in XRC files (

Example using macros (for multiline strings):

WXS_STRING(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    _T(""),                       // Default value
    false);                       // If set to true, empty strings won't be stored in XRC files

Example using objects:

static wxsStringProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable), // Offset from class beginning to variable
    true,                                     // True for multiline string, false for one-line
    _T("") );                                 // Default value
Property(SomePropertyValue);


long int

Variable type: long int

Example using macros:

WXS_LONG(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    10);                          // Default value

Example using objects:

static wxsLongProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable), // Offset from class beginning to variable
    10);                                      // Default value
Property(SomePropertyValue);


enum

Variable type: long int which is one of predefined values

Example using macros:

static const long    Values[] = { 5, 100 };
static const wxChar* Names[]  = { _("Few"), _("Many"), NULL }; // Must end with NULL entry
WXS_ENUM(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    Values,                       // Array of values
    Names,                        // Array of names
    5);                           // Default value

Example using objects:

static const long    Values[] = { 5, 100 };
static const wxChar* Names[]  = { _("Few"), _("Many"), NULL }; // Must end with NULL entry
static wxsEnumProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable), // Offset from class beginning to variable
    Values,                                   // Array of values
    Names,                                    // Array of names
    false,                                    // Setting to true notifies that content of arrays may change (not tested)
    5,                                        // Default value
    true);                                    // If false, XRC node will contain value as number, of true, it will contain name
Property(SomePropertyValue);


flags

Variable type: long int which is value produced by or-ing some one-bit flags

Example using macros:

static const long    Values[] = { 0x01, 0x02, 0x04, 0x08, 0x10 };
static const wxChar* Names[]  = { _T("C"), _T("C++"), _T("Java"), _T("C#"), _T("D"), NULL };
WXS_FLAGS(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    Values,                       // Array of values
    Names,                        // Array of names
    0x01 | 0x10 );                // Default value

Example using objects:

static const long    Values[] = { 0x01, 0x02, 0x04, 0x08, 0x10 };
static const wxChar* Names[]  = { _T("C"), _T("C++"), _T("Java"), _T("C#"), _T("D"), NULL };
static wxsFlagsProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable), // Offset from class beginning to variable
    Values,                                   // Array of values
    Names,                                    // Array of names
    false,                                    // Setting to true notifies that content of arrays may change (not tested)
    0x01 | 0x10);                             // Default value
Property(SomePropertyValue);


wxArrayString

Variable type: wxArrayString

Example using macros:

WXS_ARRAYSTRING(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    _T("xrc_subnode"));           // Name of sub-node generated for each string in xrc file

Example using objects:

static wxsArrayStringProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    _T("xrc_subnode"),                        // Name of sub-node generated for each string in xrc file
    wxsOFFSET(ClassName,ClassMemberVariable));// Offset from class beginning to variable
Property(SomePropertyValue);


wxArrayString with checked items

Variable type: wxArrayString with wxArrayBool

Example using macros:

WXS_ARRAYSTRINGCHECK(
    ClassName,                    // Name of this class
    ClassMemberVariable1,         // Name of member variable of type wxArrayString
    ClassMemberVariable2,         // Name of member variable of type wxArrayBool
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    _T("xrc_subnode"));           // Name of sub-node generated for each string in xrc file

Example using objects:

static wxsArrayStringCheckProperty SomePropertyVariable(
    _("Name in property grid"),                 // Name of property shown in property grid
    _T("name_in_xrc_file"),                     // Name of XML node used in xrc files
    _T("xrc_subnode"),                          // Name of sub-node generated for each string in xrc file
    wxsOFFSET(ClassName,ClassMemberVariable1),  // Offset from class beginning to variable 
                                                // of type wxArrayString
    wxsOFFSET(ClassName,ClassMemberVariable2)); // Offset from class beginning to variable 
                                                //of type wxArrayBool
Property(SomePropertyValue);


wxBitmap

Variable type: wxsBitmapData

Example using macros:

WXS_BITMAP(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable of type wxBitmapData
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    _T("wxART_OTHER"));           // Name of default art client (see wx docs)

Example using objects:

static wxsBitmapProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable,  // Offset from class beginning to variable of type wxsBitmapData
    _T("wxART_OTHER"));                       // Name of default art client (see wx docs)
Property(SomePropertyValue);


wxIcon

Variable type: wxsIconData

Example using macros:

WXS_ICON(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable of type wxBitmapData
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    _T("wxART_OTHER"));           // Name of default art client (see wx docs)

Example using objects:

static wxsIconProperty SomePropertyVariable(
    _("Name in property grid"),               // Name of property shown in property grid
    _T("name_in_xrc_file"),                   // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable), // Offset from class beginning to variable of type wxsIconData
    _T("wxART_OTHER"));                       // Name of default art client (see wx docs)
Property(SomePropertyValue);


wxColour

Variable type: wxsColourData

Example using macros:

WXS_COLOUR(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"));      // Name of XML node used in xrc files

Example using objects:

static wxsColourProperty SomePropertyVariable(
    _("Name in property grid"),                // Name of property shown in property grid
    _T("name_in_xrc_file"),                    // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable)); // Offset from class beginning to variable
Property(SomePropertyValue);


wxDimension

Variable type: wxsDimensionData

Example using macros:

WXS_DIMENSION(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _("Dialog-Units name"),       // Name of dialog units switch entry in property grid
    _T("name_in_xrc_file"),       // Name of XML node used in xrc files
    10,                           // Default value
    true);                        // Default value for dialog units switch

Example using objects:

static wxsDimensionProperty SomePropertyVariable(
    _("Name in property grid"),                // Name of property shown in property grid
    _("Name of Dialog-Units"),                 // Name of dialog units switch in property grid
    _T("name_in_xrc_file"),                    // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable),  // Offset from class beginning to variable
    10,                                        // Default value
    true);                                     // Default value for dialog units switch
Property(SomePropertyValue);


wxFont

Variable type: wxsFontData

Example using macros:

WXS_FONT(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name in property grid"),   // Name of property shown in property grid
    _T("name_in_xrc_file"));      // Name of XML node used in xrc files

Example using objects:

static wxsFontProperty SomePropertyVariable(
    _("Name in property grid"),                // Name of property shown in property grid
    _T("name_in_xrc_file"),                    // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable)); // Offset from class beginning to variable
Property(SomePropertyValue);


wxPosition

Variable type: wxsPositionData

Example using macros:

WXS_POSITION(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name of Default switch"),  // Name of default position switch in property grid
    _("Name of X data"),          // Name of X coordinate in property grid
    _("Name of Y data"),          // Name of Y coordinate in property grid
    _("Name of Dialog-Units"),    // Name of dialog units switch in property grid
    _T("name_in_xrc_file"));      // Name of XML node used in xrc files

Example using objects:

static wxsPositionProperty SomePropertyVariable(
    _("Name of Default switch"),               // Name of default position switch in property grid
    _("Name of X data"),                       // Name of X coordinate in property grid
    _("Name of Y data"),                       // Name of Y coordinate in property grid
    _("Name of Dialog-Units"),                 // Name of dialog units switch in property grid
    _T("name_in_xrc_file"),                    // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable)); // Offset from class beginning to variable
Property(SomePropertyValue);


wxSize

Variable type: wxsSizeData

Example using macros:

WXS_SIZE(
    ClassName,                    // Name of this class
    ClassMemberVariable,          // Name of member variable
    _("Name of Default switch"),  // Name of default position switch in property grid
    _("Name of Width data"),      // Name of Width value in property grid
    _("Name of Height data"),     // Name of Height value in property grid
    _("Name of Dialog-Units"),    // Name of dialog units switch in property grid
    _T("name_in_xrc_file"));      // Name of XML node used in xrc files

Example using objects:

static wxsSizeProperty SomePropertyVariable(
    _("Name of Default switch"),               // Name of default position switch in property grid
    _("Name of Width data"),                   // Name of Width value in property grid
    _("Name of Height data"),                  // Name of Height vlaue in property grid
    _("Name of Dialog-Units"),                 // Name of dialog units switch in property grid
    _T("name_in_xrc_file"),                    // Name of XML node used in xrc files
    wxsOFFSET(ClassName,ClassMemberVariable)); // Offset from class beginning to variable
Property(SomePropertyValue);


Direct operations on properties

The set of properties available in wxSmith's property managment system may be insufficient for some kind of properties. A good example here may be a situation, when number of available properties dynamically changes. In such cases, we have to take care about properties manually and provide following operations for each property:

  • Adding property to property grid
  • Reacting to change of this property
  • Reading data from XML (XRC) structures
  • Writing data to XML (XRC) structures

Each of these operations is done inside one virtual function. Overriding it and giving out implementation allows to freely operate on properties.

Adding custom property to property grid

To include our own properties in list of properties, we have to add following function into our class:

void OnAddExtraProperties(wxsPropertyGridManager* Grid);

Inside this class we can freely operate on given wxsPropertyGridManager class (see wxPropertyGrid site to learn more on how to operate on this class), but the operations should be limited to ading properties (it may be risky to delete or change existing properties).

At the end of custom implementation of this function, one should also call the original function declared inside wxsWidget class:

void CustomClass::OnAddExtraProperties(wxsPropertyGridManager* Grid)
{
    // Add custom properties here

    wxsWidget::OnAddExtraProperties(Grid);
}

If it won't be called, item wont allow to edit any events because they are added inside wxsItem::OnAddExtraProperties(Grid); call.


Reacting to change of custom properties

Processing changes of custom properties is simillar to creating them. We just have to override following function:

void OnExtraPropertyChanged(wxsPropertyGridManager* Grid,wxPGId Id);

It has extra parameter comparing to previous function - Id. It's id of property which was changed. It's advised that you compare value of Id with identifiers generated inside OnAddExtraProperties to avoid unnecessary reads from property grid. At the end of this function you should also call original one but only if you're sure that your properties have not changed:

void CustomClass::OnExtraPropertyChanged(wxsPropertyGridManager* Grid,wxPGId Id)
{
    if ( Id == Property_Id )
    {
        // Read value of property here
        NotifyPropertyChange(true);
        return;
    }

    wxsWidget::OnExtraPropertyChanged(Grid,Id);
}

Note that there's also call to NorifyPropertyChange(true); at the end of value reading. This call updates all things related to current resource: regenerates source code and XRC files if necessary, updates the content of editor and does few more things to keep wxSmith's state up to date.


Reading data from XML (XRC) structures

Reading of our custom properties is done inside following function:

bool OnXmlRead(TiXmlElement* Element,bool IsXRC,bool IsExtra);

The Element argument is object represending xml node of current widget. To load custom properties we must locate child nodes and read data from them. Code::Blocks and wxSmith are using TinyXml to operate on xml structures so to get more informations on how to read and manipulate xml data, read TinyXml's documentation.

The IsXRC and IsExtra arguments require some better description. From the very beginning wxSmith tends to be compatible with XRC files which allow storing structure of window inside xml file. Usually XRC files are limited to widgets which are provided with wxWidgets library. And they don't provide some extra data used by wxSmith like enteries for event handlers and variable name. wxSmith does split data of widgets into two parts - the firs one is strict XRC data and the second one is extra data provided by wxSmith. If IsXRC parameter is true, this mean that function should read XRC data part, if IsExtra is true, this mean that it should read extra data (both arguments may be true in one call). Most of contrib items would have XRC support disabled so we should read data only when IsExtra is true:

bool CustomClass::OnXmlRead(TiXmlElement* Element,bool IsXRC,bool IsExtra)
{
    if ( IsExtra )
    {
        // Process xml structures here
    }

    return wxsTool::OnXmlRead(Element,IsXRC,IsExtra);
}

At the end of this implementation we must call original function as usual ;)


Writing data to XML (XRC) structures

Writing data into xml structures is simillar to reading them. We have to implement following function:

bool CustomClass::OnXmlWrite(TiXmlElement* Element,bool IsXRC,bool IsExtra)
{
    if ( IsExtra )
    {
        // Store data into xml structure
    }

    return wxsTool::OnXmlRead(Element,IsXRC,IsExtra);
}

The meaning of arguments is same as in case of reading data. The only difference is that we write data here instead of reading it.