Dynamic Variables

In the GeneralModule, the class a2dVariablesHash, and a global instance of it can be reached via: a2dDocviewGlobals->GetVariablesHash().

You can set variable in it like this:

   1     a2dDocviewGlobals->GetVariablesHash().SetVariableString( "wxart2dlayers", "maskproclayers.cvg" );

This will create or set a variable called wxart2dlayers with the string value maskproclayers.cvg. But variables of various types ( double, int, complex types) can be set, since they are stored as a2dNamedProperty. So a double is stored as a2dDoubleProperty. But any derived a2dNamedProperty can be stored as a variable.

You can get the value of a variable back as a string like this:

   1 wxString* layerfile = a2dGeneralGlobals->GetVariablesHash().GetVariableString( "wxart2dlayers" );

If you want to have the original type again, you get it as a a2dNamedProperty first. For the simple types, there are methods to get the value straight away. Complex types, you can cast back to the original a2dNamedProperty class, and get the value from that.

   1 a2dNamedProperty* myprop = a2dVariablesHash::GetVariable( "variableName" )
   2 double value = myprop->GetDouble();

When setting a double variable, the id of the created a2dDoubleProperty is the following: a2dPropertyIdDouble::GetDummy(). This can be used to test the type.

   1     a2dDocviewGlobals->GetVariablesHash().SetVariableDouble( "myDoubleVar", 123.456 );
   2     a2dNamedProperty* myprop = a2dVariablesHash::GetVariable( "myDoubleVar" )
   3     a2dPropertyId id = myprop->GetId()    

What is its use

The variables system is used internal in the library for getting to files and paths, which can be defined by the application. Internal a reserved variable name is used to get to it. At the start if the application the variables is set by the application, and wxArt2D will use that path from then on. For instance "wxart2dlayers" and "layerFileSavePath" are used to find a default layer file where it is needed.

Variables as part of paths

A path to a file can be defined as "%{layerFileSavePath}/someSubDir", and this can be evaluated by a2dPathList. That will find the internal variable "layerFileSavePath" and substitute its value in it. But you can even add paths to the pathlist containing variables, like:

   1    a2dPathList mySearchPaths;
   2    mySearchPaths.Add( "%{mySpecialSearchDir}/${MyEnvironmentVar}/someSubDir" );
   3    mySearchPaths.FindValidPath( "SomeFile" );

The above will evaluate on the fly %{mySpecialSearchDir} in the a2dPathList, and use that path to find SomeFile. The ${MyEnvironmentVar} will be taken from the Environment variable, and gives even more flexibility. This makes it all very flexible. And combined with wxLua, you can configure your application paths without limitation. Just an example:

   1 cmdh = wx.a2dLuaWP;
   2 res, myproc = wx.wxGetEnv( "MYAPP_ROOT"  )
   3 --cmdh:Message( myproc )
   4 home = wx.wxGetHomeDir()
   5 
   6 cmdh:AddLayersPath( home .. "/config" )
   7 cmdh:AddConfigPath( home .. "/config" )
   8 cmdh:AddImagePath( home .. "/config" )
   9 cmdh:AddLayersPath( myproc .. "/config" )
  10 cmdh:AddFontPath( myproc .. "/config" )
  11 cmdh:AddImagePath( myproc .. "/config" )

cursors

The following is used to get to cursors used in wxArt2D:

   1         wxString* artdir = a2dGeneralGlobals->GetVariablesHash().GetVariableString(  wxT( "WXART2D_ART" ) );
   2         wxString artroot;
   3         if ( artdir && !artdir->IsEmpty() )
   4         {
   5                 artroot = *artdir;
   6                 artroot += wxT("/cursors/");
   7         }

For the complete code see file canglob.cpp and in there: wxCursor& a2dArtProvider::GetCursor( a2dCursorId id ). So at the beginning of your applicattion (e.g. wxApp::OnInit ), you can set:

   1     a2dDocviewGlobals->GetVariablesHash().SetVariableString( wxT("WXART2D_ART"), pathToArt );

In "pathToArt/Cursors" you can store your own cursor images, and copy the wxArt2D cursors. It is all about having a central location where your application and wxArt2D can get its images, which are used internal. But how is "pathToArt" itself set? You can have it set by finding the path to the executable of your application, are you can define your own MY_SUPER_APP_ROOT environment variable. Also wxStandardPaths might help you. I prefer to have an environment variable set for an application, before starting it. Using a shell script or bat file, this is transparent to the user. During the installation of an application you already know where your application is installed, and so the variable is know at that moment, and can be set right.

InternalVariables (last edited 2011-07-04 14:55:33 by KlaasHolwerda)