Difference between revisions of "Wxs ext tutorial2 properties"

From Code::Blocks
Line 80: Line 80:
 
  void OnEnumWidgetProperties(long Flags);
 
  void OnEnumWidgetProperties(long Flags);
  
wxSmith has complex property managment system which handle most of work related to propertiesautomatically, 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).  
+
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 <tt>Property</tt> function for this variable. Usually ot looks like this:
+
Binding between variable and property managment system is done in function mentioned above. It require generating one variable for one property and calling <tt>Property</tt> function for it. Usually ot looks like this:
  
 
  void ClassName::OnEnumWidgetProperties(long Flags)
 
  void ClassName::OnEnumWidgetProperties(long Flags)

Revision as of 17:02, 18 April 2007

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. It's possible to use members of other class, but it's advanced stuff and won't be covered in this tutorial. And 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.


Properties list

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);