⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.tex

📁 很牛的GUI源码wxWidgets-2.8.0.zip 可在多种平台下运行.
💻 TEX
📖 第 1 页 / 共 2 页
字号:
\section{\class{wxConfigBase}}\label{wxconfigbase}wxConfigBase class defines the basic interface of all config classes. It cannot be used by itself (it is an abstract base class) and you will always use oneof its derivations: \helpref{wxFileConfig}{wxfileconfig},wxRegConfig or any other.However, usually you don't even need to know the precise nature of the classyou're working with but you would just use the wxConfigBase methods. Thisallows you to write the same code regardless of whether you're working withthe registry under Win32 or text-based config files under Unix (or evenWindows 3.1 .INI files if you're really unlucky). To make writing the portablecode even easier, wxWidgets provides a typedef wxConfigwhich is mapped onto the native wxConfigBase implementation on the givenplatform: i.e. wxRegConfig under Win32 andwxFileConfig otherwise.See \helpref{config overview}{wxconfigoverview} for the descriptions of allfeatures of this class.It is highly recommended to use static functions {\it Get()} and/or {\it Set()}, so please have a \helpref{look at them.}{wxconfigstaticfunctions}\wxheading{Derived from}No base class\wxheading{Include files}<wx/config.h> (to let wxWidgets choose a wxConfig class for your platform)\\<wx/confbase.h> (base config class)\\<wx/fileconf.h> (wxFileConfig class)\\<wx/msw/regconf.h> (wxRegConfig class)\wxheading{Example}Here is how you would typically use this class:\begin{verbatim}  // using wxConfig instead of writing wxFileConfig or wxRegConfig enhances  // portability of the code  wxConfig *config = new wxConfig("MyAppName");  wxString str;  if ( config->Read("LastPrompt", &str) ) {    // last prompt was found in the config file/registry and its value is now    // in str    ...  }  else {    // no last prompt...  }  // another example: using default values and the full path instead of just  // key name: if the key is not found , the value 17 is returned  long value = config->Read("/LastRun/CalculatedValues/MaxValue", 17);  ...  ...  ...  // at the end of the program we would save everything back  config->Write("LastPrompt", str);  config->Write("/LastRun/CalculatedValues/MaxValue", value);  // the changes will be written back automatically  delete config;\end{verbatim}This basic example, of course, doesn't show all wxConfig features, such asenumerating, testing for existence and deleting the entries and groups ofentries in the config file, its abilities to automatically store the defaultvalues or expand the environment variables on the fly. However, the main ideais that using this class is easy and that it should normally do what youexpect it to.NB: in the documentation of this class, the words "config file" also mean"registry hive" for wxRegConfig and, generally speaking, might mean anyphysical storage where a wxConfigBase-derived class stores its data.\latexignore{\rtfignore{\wxheading{Function groups}}}\membersection{Static functions}\label{wxconfigstaticfunctions}These functions deal with the "default" config object. Although its usage isnot at all mandatory it may be convenient to use a global config objectinstead of creating and deleting the local config objects each time you needone (especially because creating a wxFileConfig object might be a timeconsuming operation). In this case, you may create this global config objectin the very start of the program and {\it Set()} it as the default. Then, fromanywhere in your program, you may access it using the {\it Get()} function.This global wxConfig object will be deleted by wxWidgets automatically if itexists. Note that this implies that if you do delete this object yourself(usually in \helpref{wxApp::OnExit}{wxapponexit}) you must use {\it Set(NULL)}to prevent wxWidgets from deleting it the second time.As it happens, you may even further simplify the procedure described above:you may forget about calling {\it Set()}. When {\it Get()} is called and thereis no current object, it will create one using {\it Create()} function. Todisable this behaviour {\it DontCreateOnDemand()} is provided.{\bf Note:} You should use either {\it Set()} or {\it Get()} because wxWidgetslibrary itself would take advantage of it and could save various informationin it. For example \helpref{wxFontMapper}{wxfontmapper} or Unix versionof \helpref{wxFileDialog}{wxfiledialog} have the ability to use wxConfig class.\helpref{Set}{wxconfigbaseset}\\\helpref{Get}{wxconfigbaseget}\\\helpref{Create}{wxconfigbasecreate}\\\helpref{DontCreateOnDemand}{wxconfigbasedontcreateondemand}\membersection{Constructor and destructor}\label{congigconstructordestructor}\helpref{wxConfigBase}{wxconfigbasector}\\\helpref{\destruct{wxConfigBase}}{wxconfigbasedtor}\membersection{Path management}\label{configpathmanagement}As explained in \helpref{config overview}{wxconfigoverview}, the config classessupport a file system-like hierarchy of keys (files) and groups (directories).As in the file system case, to specify a key in the config class you must usea path to it. Config classes also support the notion of the current group,which makes it possible to use the relative paths. To clarify all this, hereis an example (it is only for the sake of demonstration, it doesn't do anythingsensible!):\begin{verbatim}  wxConfig *config = new wxConfig("FooBarApp");  // right now the current path is '/'  conf->Write("RootEntry", 1);  // go to some other place: if the group(s) don't exist, they will be created  conf->SetPath("/Group/Subgroup");  // create an entry in subgroup  conf->Write("SubgroupEntry", 3);  // '..' is understood  conf->Write("../GroupEntry", 2);  conf->SetPath("..");  wxASSERT( conf->Read("Subgroup/SubgroupEntry", 0l) == 3 );  // use absolute path: it is allowed, too  wxASSERT( conf->Read("/RootEntry", 0l) == 1 );\end{verbatim}{\it Warning}: it is probably a good idea to always restore the path to itsold value on function exit:\begin{verbatim}  void foo(wxConfigBase *config)  {    wxString strOldPath = config->GetPath();    config->SetPath("/Foo/Data");    ...    config->SetPath(strOldPath);  }\end{verbatim}because otherwise the assert in the following example will surely fail(we suppose here that {\it foo()} function is the same as above except that itdoesn't save and restore the path):\begin{verbatim}  void bar(wxConfigBase *config)  {    config->Write("Test", 17);    foo(config);    // we're reading "/Foo/Data/Test" here! -1 will probably be returned...    wxASSERT( config->Read("Test", -1) == 17 );  }\end{verbatim}Finally, the path separator in wxConfigBase and derived classes is always '/',regardless of the platform (i.e. it is {\bf not} '$\backslash\backslash$' under Windows).\helpref{SetPath}{wxconfigbasesetpath}\\\helpref{GetPath}{wxconfigbasegetpath}\membersection{Enumeration}\label{configenumeration}The functions in this section allow to enumerate all entries and groups in theconfig file. All functions here return \false when there are no more items.You must pass the same index to GetNext and GetFirst (don't modify it).Please note that it is {\bf not} the index of the current item (you will havesome great surprises with wxRegConfig if you assume this) and you shouldn'teven look at it: it is just a "cookie" which stores the state of theenumeration. It can't be stored inside the class because it would prevent youfrom running several enumerations simultaneously, that's why you must pass itexplicitly.Having said all this, enumerating the config entries/groups is very simple:\begin{verbatim}  wxConfigBase *config = ...;  wxArrayString aNames;  // enumeration variables  wxString str;  long dummy;  // first enum all entries  bool bCont = config->GetFirstEntry(str, dummy);  while ( bCont ) {    aNames.Add(str);    bCont = GetConfig()->GetNextEntry(str, dummy);  }  ... we have all entry names in aNames...  // now all groups...  bCont = GetConfig()->GetFirstGroup(str, dummy);  while ( bCont ) {    aNames.Add(str);    bCont = GetConfig()->GetNextGroup(str, dummy);  }  ... we have all group (and entry) names in aNames...\end{verbatim}There are also functions to get the number of entries/subgroups withoutactually enumerating them, but you will probably never need them.\helpref{GetFirstGroup}{wxconfigbasegetfirstgroup}\\\helpref{GetNextGroup}{wxconfigbasegetnextgroup}\\\helpref{GetFirstEntry}{wxconfigbasegetfirstentry}\\\helpref{GetNextEntry}{wxconfigbasegetnextentry}\\\helpref{GetNumberOfEntries}{wxconfigbasegetnumberofentries}\\\helpref{GetNumberOfGroups}{wxconfigbasegetnumberofgroups}\membersection{Tests of existence}\label{configtestsofexistence}\helpref{HasGroup}{wxconfigbasehasgroup}\\\helpref{HasEntry}{wxconfigbasehasentry}\\\helpref{Exists}{wxconfigbaseexists}\\\helpref{GetEntryType}{wxconfigbasegetentrytype}\membersection{Miscellaneous functions}\label{configmiscellaneous}\helpref{GetAppName}{wxconfigbasegetappname}\\\helpref{GetVendorName}{wxconfigbasegetvendorname}\\\helpref{SetUmask}{wxfileconfigsetumask}\membersection{Key access}\label{configkeyaccess}These function are the core of wxConfigBase class: they allow you to read andwrite config file data. All {\it Read} function take a default value whichwill be returned if the specified key is not found in the config file.Currently, only two types of data are supported: string and long (but it mightchange in the near future). To work with other types: for {\it int} or {\itbool} you can work with function taking/returning {\it long} and just use thecasts. Better yet, just use {\it long} for all variables which you're going tosave in the config file: chances are that {\tt sizeof(bool) == sizeof(int) == sizeof(long)} anyhow on your system. For {\it float}, {\it double} and, ingeneral, any other type you'd have to translate them to/from stringrepresentation and use string functions.Try not to read long values into string variables and vice versa: although itjust might work with wxFileConfig, you will get a system error withwxRegConfig because in the Windows registry the different types of entries areindeed used.Final remark: the {\it szKey} parameter for all these functions can contain anarbitrary path (either relative or absolute), not just the key name.\helpref{Read}{wxconfigbaseread}\\\helpref{Write}{wxconfigbasewrite}\\\helpref{Flush}{wxconfigbaseflush}\membersection{Rename entries/groups}\label{configrenaming}The functions in this section allow to rename entries or subgroups of thecurrent group. They will return \false on error. typically because either theentry/group with the original name doesn't exist, because the entry/group withthe new name already exists or because the function is not supported in thiswxConfig implementation.\helpref{RenameEntry}{wxconfigbaserenameentry}\\\helpref{RenameGroup}{wxconfigbaserenamegroup}\membersection{Delete entries/groups}\label{configdeleting}The functions in this section delete entries and/or groups of entries from theconfig file. {\it DeleteAll()} is especially useful if you want to erase alltraces of your program presence: for example, when you uninstall it.\helpref{DeleteEntry}{wxconfigbasedeleteentry}\\\helpref{DeleteGroup}{wxconfigbasedeletegroup}\\\helpref{DeleteAll}{wxconfigbasedeleteall}\membersection{Options}\label{configoptions}Some aspects of wxConfigBase behaviour can be changed during run-time. Thefirst of them is the expansion of environment variables in the string valuesread from the config file: for example, if you have the following in yourconfig file:\begin{verbatim}  # config file for my program  UserData = $HOME/data  # the following syntax is valud only under Windows  UserData = %windir%\\data.dat\end{verbatim}% $ % help EMACS syntax highlighting...the call to {\tt config->Read("UserData")} will return something like{\tt "/home/zeitlin/data"} if you're lucky enough to run a Linux system ;-)Although this feature is very useful, it may be annoying if you read a valuewhich containts '\$' or '\%' symbols (\% is used for environment variablesexpansion under Windows) which are not used for environment variableexpansion. In this situation you may call SetExpandEnvVars(false) just beforereading this value and SetExpandEnvVars(true) just after. Another solutionwould be to prefix the offending symbols with a backslash.The following functions control this option:\helpref{IsExpandingEnvVars}{wxconfigbaseisexpandingenvvars}\\\helpref{SetExpandEnvVars}{wxconfigbasesetexpandenvvars}\\\helpref{SetRecordDefaults}{wxconfigbasesetrecorddefaults}\\\helpref{IsRecordingDefaults}{wxconfigbaseisrecordingdefaults}%%%%% MEMBERS HERE %%%%%\helponly{\insertatlevel{2}{\wxheading{Members}}}\membersection{wxConfigBase::wxConfigBase}\label{wxconfigbasector}\func{}{wxConfigBase}{\param{const wxString\& }{appName = wxEmptyString}, \param{const wxString\& }{vendorName = wxEmptyString}, \param{const wxString\& }{localFilename = wxEmptyString}, \param{const wxString\& }{globalFilename = wxEmptyString}, \param{long}{ style = 0}, \param{wxMBConv\&}{ conv = wxConvUTF8}}This is the default and only constructor of the wxConfigBase class, andderived classes.\wxheading{Parameters}\docparam{appName}{The application name. If this is empty, the class willnormally use \helpref{wxApp::GetAppName}{wxappgetappname} to set it. Theapplication name is used in the registry key on Windows, and can be used todeduce the local filename parameter if that is missing.}\docparam{vendorName}{The vendor name. If this is empty, it is assumed thatno vendor name is wanted, if this is optional for the current config class.The vendor name is appended to the application name for wxRegConfig.}\docparam{localFilename}{Some config classes require a local filename. If thisis not present, but required, the application name will be used instead.}\docparam{globalFilename}{Some config classes require a global filename. Ifthis is not present, but required, the application name will be used instead.}\docparam{style}{Can be one of wxCONFIG\_USE\_LOCAL\_FILE andwxCONFIG\_USE\_GLOBAL\_FILE. The style interpretation depends on the configclass and is ignored by some. For wxFileConfig, these styles determine whethera local or global config file is created or used. If the flag is present butthe parameter is empty, the parameter will be set to a default. If theparameter is present but the style flag not, the relevant flag will be addedto the style. For wxFileConfig you can also add wxCONFIG\_USE\_RELATIVE\_PATH by logically or'ing it to either of the \_FILE options to tell wxFileConfig to use relative instead of absolute paths.  For wxFileConfig, you can also add wxCONFIG\_USE\_NO\_ESCAPE\_CHARACTERS which will turn off character escaping for the values of entries stored in the config file: for example a {\it foo} key with some backslash characters will be stored as {\tt foo=C:$\backslash$mydir} insteadof the usual storage of {\tt foo=C:$\backslash\backslash$mydir}.For wxRegConfig, this flag refers to HKLM, and provides read-only access.The wxCONFIG\_USE\_NO\_ESCAPE\_CHARACTERS style can be helpful if your config file must be read or written to by a non-wxWidgets program (which might not understand the escape characters). Note, however, that if wxCONFIG\_USE\_NO\_ESCAPE\_CHARACTERS style is used, it is is now your application's responsibility to ensure that there is no newline or other illegal characters in a value, before writing that value to the file.}\docparam{conv}{This parameter is only used by wxFileConfig when compiledin Unicode mode. It specifies the encoding in which the configuration fileis written.}\wxheading{Remarks}By default, environment variable expansion is on and recording defaults is

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -