//-----------------------------------------------------------------------------
// wx.NET - Config.cs
//
// The wxConfig wrapper class.
//
// Written by Bryan Bulten (bryan@bulten.ca)
// (C) 2003 Bryan Bulten
// Licensed under the wxWidgets license, see LICENSE.txt for details.
//
// $Id: Config.cs,v 1.15 2007/11/11 14:14:45 harald_meyer Exp $
//-----------------------------------------------------------------------------
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace wx{
public enum EntryType
{
Unknown,
String,
Boolean,
Integer,
Float
}
/** This is the class for writing and reading configuration parameters via \e wxWidgets \c wxConfigBase implementations.
*
* The idea of \e wxWidgets configuration class is to provide an interface that allows
* applications to manage configuration data without regard to the mathod of storing the
* information. Configurations may either be stored in a global or local file, or into the
* registry database. The syntax of the files is according to the good old Ini-files as
* common to 16-Bit Windows.
*
* This interface might be extended in the future to provide some additional styles that
* implement the standard way of configuring .NET applications (XML file \c myApp.exe.config for
* application \c myApp.exe read by class System.Configuration.Configuration).
*
* Although \c wxConfig is not derived from \c wxObject, this class is derived from wx.Object since
* the main concern of wx.Object is to manage pointers to C++ objects.
* Use Config.Get() to get an instance.
*
* The original C++ implementation uses Method \c Read with many, many signatures. Since this may lead to
* some confusion (at least I have been confused on using this class), this class implements \c Read
* functionality in two flavours: Returning the value or a default as method result and loading a
* reference with the value from the configuration if this has been found. Methods of the first flavour
* still are called \c Read whereas methods of the second flavour contain the data type as part of their name
* like wx.Config.ReadInt(). In the past both flavours were named simply \c Read. So, missing the small
* keyword \c ref turned an intended call of flavour 2 into one of flavour 1 without meaning this.
*
* \b Problems:
* There is some evidence that reading strings for non-existing keys using a default value
* might lead to memory corruption at least in some cases.
* */
public class Config : Object
{
[DllImport("wx-c")] static extern IntPtr wxConfigBase_Set(IntPtr pConfig);
[DllImport("wx-c")] static extern IntPtr wxConfigBase_Get(bool createOnDemand);
[DllImport("wx-c")] static extern IntPtr wxConfigBase_Create();
[DllImport("wx-c")] static extern void wxConfigBase_DontCreateOnDemand();
[DllImport("wx-c")] static extern void wxConfigBase_SetPath(IntPtr self, IntPtr strPath);
[DllImport("wx-c")] static extern IntPtr wxConfigBase_GetPath(IntPtr self);
[DllImport("wx-c")] static extern bool wxConfigBase_GetFirstGroup(IntPtr self, IntPtr str, ref int lIndex);
[DllImport("wx-c")] static extern bool wxConfigBase_GetNextGroup(IntPtr self, IntPtr str, ref int lIndex);
[DllImport("wx-c")] static extern bool wxConfigBase_GetFirstEntry(IntPtr self, IntPtr str, ref int lIndex);
[DllImport("wx-c")] static extern bool wxConfigBase_GetNextEntry(IntPtr self, IntPtr str, ref int lIndex);
[DllImport("wx-c")] static extern int wxConfigBase_GetNumberOfEntries(IntPtr self, bool bRecursive);
[DllImport("wx-c")] static extern int wxConfigBase_GetNumberOfGroups(IntPtr self, bool bRecursive);
[DllImport("wx-c")] static extern bool wxConfigBase_HasGroup(IntPtr self, IntPtr strName);
[DllImport("wx-c")] static extern bool wxConfigBase_HasEntry(IntPtr self, IntPtr strName);
[DllImport("wx-c")] static extern bool wxConfigBase_Exists(IntPtr self, IntPtr strName);
[DllImport("wx-c")] static extern int wxConfigBase_GetEntryType(IntPtr self, IntPtr name);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadStr(IntPtr self, IntPtr key, IntPtr pStr);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadStrDef(IntPtr self, IntPtr key, IntPtr pStr, IntPtr defVal);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadInt(IntPtr self, IntPtr key, ref int pl);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadIntDef(IntPtr self, IntPtr key, ref int pl, int defVal);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadDbl(IntPtr self, IntPtr key, ref double val);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadDblDef(IntPtr self, IntPtr key, ref double val, double defVal);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadBool(IntPtr self, IntPtr key, ref bool val);
[DllImport("wx-c")] static extern bool wxConfigBase_ReadBoolDef(IntPtr self, IntPtr key, ref bool val, bool defVal);
[DllImport("wx-c")] static extern IntPtr wxConfigBase_ReadStrRet(IntPtr self, IntPtr key, IntPtr defVal);
[DllImport("wx-c")] static extern int wxConfigBase_ReadIntRet(IntPtr self, IntPtr key, int defVal);
[DllImport("wx-c")] static extern bool wxConfigBase_WriteStr(IntPtr self, IntPtr key, IntPtr val);
[DllImport("wx-c")] static extern bool wxConfigBase_WriteInt(IntPtr self, IntPtr key, int val);
[DllImport("wx-c")] static extern bool wxConfigBase_WriteDbl(IntPtr self, IntPtr key, double val);
[DllImport("wx-c")] static extern bool wxConfigBase_WriteBool(IntPtr self, IntPtr key, bool val);
[DllImport("wx-c")] static extern bool wxConfigBase_Flush(IntPtr self, bool bCurrentOnly);
[DllImport("wx-c")] static extern bool wxConfigBase_RenameEntry(IntPtr self, IntPtr oldName, IntPtr newName);
[DllImport("wx-c")] static extern bool wxConfigBase_RenameGroup(IntPtr self, IntPtr oldName, IntPtr newName);
[DllImport("wx-c")] static extern bool wxConfigBase_DeleteEntry(IntPtr self, IntPtr key, bool bDeleteGroupIfEmpty);
[DllImport("wx-c")] static extern bool wxConfigBase_DeleteGroup(IntPtr self, IntPtr key);
[DllImport("wx-c")] static extern bool wxConfigBase_DeleteAll(IntPtr self);
[DllImport("wx-c")] static extern bool wxConfigBase_IsExpandingEnvVars(IntPtr self);
[DllImport("wx-c")] static extern void wxConfigBase_SetExpandEnvVars(IntPtr self, bool bDoIt);
[DllImport("wx-c")] static extern IntPtr wxConfigBase_ExpandEnvVars(IntPtr self, IntPtr str);
[DllImport("wx-c")] static extern void wxConfigBase_SetRecordDefaults(IntPtr self, bool bDoIt);
[DllImport("wx-c")] static extern bool wxConfigBase_IsRecordingDefaults(IntPtr self);
[DllImport("wx-c")] static extern IntPtr wxConfigBase_GetAppName(IntPtr self);
[DllImport("wx-c")] static extern void wxConfigBase_SetAppName(IntPtr self, IntPtr appName);
[DllImport("wx-c")] static extern IntPtr wxConfigBase_GetVendorName(IntPtr self);
[DllImport("wx-c")] static extern void wxConfigBase_SetVendorName(IntPtr self, IntPtr vendorName);
[DllImport("wx-c")] static extern void wxConfigBase_SetStyle(IntPtr self, uint style);
[DllImport("wx-c")] static extern uint wxConfigBase_GetStyle(IntPtr self);
//---------------------------------------------------------------------
public Config(IntPtr wxObject)
: base(wxObject) { }
public static Config Set(Config config)
{
return (Config)FindObject(wxConfigBase_Set(Object.SafePtr(config)), typeof(Config));
}
/** This will get the current configuration of the application.
* If the argument is \c true, this method will wx.Config.Create() a new configuration
* is required.
*/
public static Config Get(bool createOnDemand)
{
return (Config)FindObject(wxConfigBase_Get(createOnDemand), typeof(Config));
}
/** Equivalent to wx.Config.Get(true).
*/
public static Config Get()
{
return (Config)FindObject(wxConfigBase_Get(true), typeof(Config));
}
/** Create a new configuration according to properties of the application.
* On Windows, the configuration will preferably recorded in the registry database
* within the \c HKEY_CURRENT_USER section using the vendor's name and the application's name
* to create subkeys. However, this depends on whether \c wxUSE_CONFIG_NATIVE has been defined
* on compiling \e wxWidgets or not. In most other cases, this will use a file of the application
* name in the current working directory. Refer also to wx.App.AppName and wx.App.VendorName.
*/
public static Config Create()
{
return new Config(wxConfigBase_Create());
}
public static Config Default
{
get
{
return Get();
}
set
{
Set(value);
}
}
//---------------------------------------------------------------------
public void DontCreateOnDemand()
{
wxConfigBase_DontCreateOnDemand();
}
//---------------------------------------------------------------------
public string Path
{
set
{
wxString wStr = new wxString(value);
wxConfigBase_SetPath(wxObject, wStr.wxObject);
}
get { return new wxString(wxConfigBase_GetPath(wxObject), true); }
}
//---------------------------------------------------------------------
#region Iterate Groups
public bool GetFirstGroup(ref string str, ref int lIndex)
{
bool ret;
wxString wstr = new wxString(str);
ret = wxConfigBase_GetFirstGroup(wxObject, wxString.SafePtr(wstr), ref lIndex);
str = wstr;
return ret;
}
public bool GetNextGroup(ref string str, ref int lIndex)
{
bool ret;
wxString wstr = new wxString(str);
ret = wxConfigBase_GetNextGroup(wxObject, wxString.SafePtr(wstr), ref lIndex);
str = wstr;
return ret;
}
#endregion
//---------------------------------------------------------------------
#region Iterate Entries
public bool GetFirstEntry(ref string str, ref int lIndex)
{
bool ret;
wxString wstr = new wxString(str);
ret = wxConfigBase_GetFirstEntry(wxObject, wxString.SafePtr(wstr), ref lIndex);
str = wstr;
return ret;
}
public bool GetNextEntry(ref string str, ref int lIndex)
{
bool ret;
wxString wstr = new wxString(str);
ret = wxConfigBase_GetNextEntry(wxObject, wxString.SafePtr(wstr), ref lIndex);
str = wstr;
return ret;
}
#endregion
//---------------------------------------------------------------------
public int GetNumberOfEntries(bool bRecursive)
{
return wxConfigBase_GetNumberOfEntries(wxObject, bRecursive);
}
public int GetNumberOfGroups(bool bRecursive)
{
return wxConfigBase_GetNumberOfGroups(wxObject, bRecursive);
}
//---------------------------------------------------------------------
public bool HasGroup(string strName)
{
wxString wStr = new wxString(strName);
return wxConfigBase_HasGroup(wxObject, wStr.wxObject);
}
public bool HasEntry(string strName)
{
wxString wStr = new wxString(strName);
return wxConfigBase_HasEntry(wxObject, wStr.wxObject);
}
//---------------------------------------------------------------------
public bool Exists(string strName)
{
wxString wStr = new wxString(strName);
return wxConfigBase_Exists(this.wxObject, wStr.wxObject);
}
public EntryType GetEntryType(string name)
{
wxString wStr = new wxString(name);
return (EntryType)wxConfigBase_GetEntryType(this.wxObject, wStr.wxObject);
}
//---------------------------------------------------------------------
public bool ReadString(string key, ref string str)
{
bool ret;
wxString wstr = new wxString(str);
wxString wKey = new wxString(key);
ret = wxConfigBase_ReadStr(this.wxObject, wKey.wxObject, wxString.SafePtr(wstr));
str = wstr;
return ret;
}
public bool ReadString(string key, ref string str, string defVal)
{
bool ret;
wxString wstr = new wxString(str);
wxString wkey = new wxString(key);
wxString wdefVal=new wxString(defVal);
ret = wxConfigBase_ReadStrDef(this.wxObject, wkey.wxObject, wxString.SafePtr(wstr), wdefVal.wxObject);
str = wstr;
return ret;
}
//---------------------------------------------------------------------
public bool ReadInt(string key, ref int pl)
{
wxString wkey = new wxString(key);
return wxConfigBase_ReadInt(this.wxObject, wkey.wxObject, ref pl);
}
public bool ReadInt(string key, ref int pl, int defVal)
{
wxString wkey = new wxString(key);
return wxConfigBase_ReadIntDef(this.wxObject, wkey.wxObject, ref pl, defVal);
}
//---------------------------------------------------------------------
public bool ReadDouble(string key, ref double val)
{
wxString wkey = new wxString(key);
return wxConfigBase_ReadDbl(this.wxObject, wkey.wxObject, ref val);
}
public bool ReadDouble(string key, ref double val, double defVal)
{
wxString wkey = new wxString(key);
return wxConfigBase_ReadDblDef(this.wxObject, wkey.wxObject, ref val, defVal);
}
//---------------------------------------------------------------------
public bool ReadBool(string key, ref bool val)
{
wxString wkey = new wxString(key);
return wxConfigBase_ReadBool(this.wxObject, wkey.wxObject, ref val);
}
public bool ReadBool(string key, ref bool val, bool defVal)
{
wxString wkey = new wxString(key);
return wxConfigBase_ReadBoolDef(this.wxObject, wkey.wxObject, ref val, defVal);
}
//---------------------------------------------------------------------
public bool ReadFont(string key, ref Font val)
{
return ReadFont(key, ref val, Font.wxNORMAL_FONT);
}
public bool ReadFont(string key, ref Font val, Font defVal)
{
bool ret = true;
int pointSize = 0, family = 0, style = 0, weight = 0, encoding = 0;
bool underline = false;
string faceName = "";
ret &= ReadInt(key + "/PointSize", ref pointSize, (int)defVal.PointSize);
ret &= ReadInt(key + "/Family", ref family, (int)defVal.Family);
ret &= ReadInt(key + "/Style", ref style, (int)defVal.Style);
ret &= ReadInt(key + "/Weight", ref weight, (int)defVal.Weight);
ret &= ReadBool(key + "/Underline", ref underline, (bool)defVal.Underlined);
ret &= ReadString(key + "/FaceName", ref faceName, defVal.FaceName);
ret &= ReadInt(key + "/Encoding", ref encoding, (int)defVal.Encoding);
val.PointSize = pointSize;
val.Family = (FontFamily)family;
val.Style = (FontStyle)style;
val.Weight = (FontWeight)weight;
val.Underlined = underline;
val.FaceName = faceName;
val.Encoding = (FontEncoding)encoding;
return ret;
}
//---------------------------------------------------------------------
public bool ReadColour(string key, ref Colour val)
{
Colour def = new Colour(0, 0, 0);
return ReadColour(key, ref val, def);
}
public bool ReadColour(string key, ref Colour val, Colour defVal)
{
bool ret = true;
int r = 0, b = 0, g = 0;
ret &= ReadInt(key + "/Red", ref r, defVal.Red);
ret &= ReadInt(key + "/Blue", ref b, defVal.Blue);
ret &= ReadInt(key + "/Green", ref g, defVal.Green);
val = new Colour((byte)r, (byte)g, (byte)b);
return ret;
}
//---------------------------------------------------------------------
public string Read(string key, string defVal)
{
wxString wkey = new wxString(key);
wxString wdefVal = new wxString(defVal);
return new wxString(wxConfigBase_ReadStrRet(this.wxObject, wkey.wxObject, wdefVal.wxObject), true);
}
public int Read(string key, int defVal)
{
wxString wkey = new wxString(key);
return wxConfigBase_ReadIntRet(this.wxObject, wkey.wxObject, defVal);
}
public bool Read(string key, bool defVal)
{
bool val = false;
ReadBool(key, ref val, defVal);
return val;
}
public Colour Read(string key, Colour defVal)
{
Colour col = new Colour();
ReadColour(key, ref col, defVal);
return col;
}
public Font Read(string key, Font defVal)
{
Font fnt = new Font();
ReadFont(key, ref fnt, defVal);
return fnt;
}
//---------------------------------------------------------------------
public bool Write(string key, string val)
{
wxString wkey = new wxString(key);
wxString wval = new wxString(val);
return wxConfigBase_WriteStr(this.wxObject, wkey.wxObject, wval.wxObject);
}
public bool Write(string key, int val)
{
wxString wkey = new wxString(key);
return wxConfigBase_WriteInt(this.wxObject, wkey.wxObject, val);
}
public bool Write(string key, uint val)
{
return this.Write(key, (int) val);
}
public bool Write(string key, double val)
{
wxString wkey = new wxString(key);
return wxConfigBase_WriteDbl(this.wxObject, wkey.wxObject, val);
}
public bool Write(string key, bool val)
{
wxString wkey = new wxString(key);
return wxConfigBase_WriteBool(this.wxObject, wkey.wxObject, val);
}
public bool Write(string key, Colour col)
{
bool ret = true;
ret &= Write(key + "/Red", col.Red);
ret &= Write(key + "/Blue", col.Blue);
ret &= Write(key + "/Green", col.Green);
return ret;
}
public bool Write(string key, Font val)
{
bool ret = true;
ret &= Write(key + "/PointSize", (int)val.PointSize);
ret &= Write(key + "/Family", (int)val.Family);
ret &= Write(key + "/Style", (int)val.Style);
ret &= Write(key + "/Weight", (int)val.Weight);
ret &= Write(key + "/Underline", (bool)val.Underlined);
ret &= Write(key + "/FaceName", val.FaceName);
ret &= Write(key + "/Encoding", (int)val.Encoding);
return ret;
}
//---------------------------------------------------------------------
public bool Flush(bool bCurrentOnly)
{
return wxConfigBase_Flush(wxObject, bCurrentOnly);
}
//---------------------------------------------------------------------
public bool RenameEntry(string oldName, string newName)
{
wxString woldName = new wxString(oldName);
wxString wnewName = new wxString(newName);
return wxConfigBase_RenameEntry(this.wxObject, woldName.wxObject, wnewName.wxObject);
}
public bool RenameGroup(string oldName, string newName)
{
wxString woldName = new wxString(oldName);
wxString wnewName = new wxString(newName);
return wxConfigBase_RenameGroup(this.wxObject, woldName.wxObject, wnewName.wxObject);
}
//---------------------------------------------------------------------
public bool DeleteEntry(string key, bool bDeleteGroupIfEmpty)
{
wxString wkey = new wxString(key);
return wxConfigBase_DeleteEntry(this.wxObject, wkey.wxObject, bDeleteGroupIfEmpty);
}
public bool DeleteGroup(string key)
{
wxString wkey = new wxString(key);
return wxConfigBase_DeleteGroup(this.wxObject, wkey.wxObject);
}
public bool DeleteAll()
{
return wxConfigBase_DeleteAll(this.wxObject);
}
//---------------------------------------------------------------------
/** This read/write property defines automatic expansion of environment variables.
* Use WithEnvVarsExpanded() to read a string with expanded environment variables.
* */
public bool ExpandEnvVars
{
get { return wxConfigBase_IsExpandingEnvVars(this.wxObject); }
set { wxConfigBase_SetExpandEnvVars(this.wxObject, value); }
}
/** This is the equivalent to \c wxConfig::EnvVarsExpanded().
* This has to be renamed since \c EnvVarsExpanded is required as name of the property
* for automatic expansion.
* */
public string WithEnvVarsExpanded(string str)
{
wxString wstr = new wxString(str);
return new wxString(wxConfigBase_ExpandEnvVars(this.wxObject, wstr.wxObject));
}
//---------------------------------------------------------------------
public bool RecordDefaults
{
set { wxConfigBase_SetRecordDefaults(wxObject, value); }
get { return wxConfigBase_IsRecordingDefaults(wxObject); }
}
//---------------------------------------------------------------------
public string AppName
{
get { return new wxString(wxConfigBase_GetAppName(wxObject), true); }
set
{
wxString wvalue = new wxString(value);
wxConfigBase_SetAppName(this.wxObject, wvalue.wxObject);
}
}
//---------------------------------------------------------------------
public string VendorName
{
get { return new wxString(wxConfigBase_GetVendorName(wxObject), true); }
set
{
wxString wvalue = new wxString(value);
wxConfigBase_SetVendorName(this.wxObject, wvalue.wxObject);
}
}
//---------------------------------------------------------------------
public uint style
{
set { wxConfigBase_SetStyle(wxObject, value); }
get { return wxConfigBase_GetStyle(wxObject); }
}
//---------------------------------------------------------------------
}
}
|