This section describes VkApp protected functions and data members that you can use in a VkApp subclass, and presents an example of subclassing VkApp to parse command-line options.
VkApp Protected Functions and Data Members
You can use VkApp::parseCommandLine() to parse command line options:
You should call parseCommandLine() from within the constructor of your VkApp subclass. Provide an XrmOptionDescRec(3) table as the options argument and specify the number of entries in the table with the numOptions argument. parseCommandLine() passes these arguments to XtOpenDisplay(3), which parses the command line and loads recognized options into the application's resource database. parseCommandLine() modifies argv to remove all recognized options and returns an updated value of argc which you must use to update the value of argc. Example 16. shows an example of using parseCommandLine().
You can override VkApp::afterRealizeHook() to perform certain actions after all application windows have been realized:
virtual void afterRealizeHook()
For example, you could override afterRealizeHook() to install a colormap or set properties on the application's windows. By default, function is empty.
When subclassing VkApp, you also have access to the protected data member VkApp::_winList:
VkComponentList _winList
This data member maintains the list of the application's top-level windows. Consult the VkComponentList(3) reference page for more information on the VkComponentList class.
Example of Subclassing VkApp
The most common reason for creating a subclass of VkApp is to parse the command line and set global resources based on command line options. Also, rather than use global variables, you can store data that is needed throughout your application in data members of your VkApp subclass.
The program in Example 16. creates MyApp, a VkApp subclass that recognizes a -verbose command line argument and initializes a protected data member depending on whether or not the flag is present.
Note: Example 16. uses the protected VkApp function parseCommandLine() to extract a flag if it exists. This function returns an updated value of argc that must be used to update the value of argc passed by the calling application.
Example 16. Deriving a Subclass from VkApp
Code
- #include <Vk/VkApp.h>
- #include <Vk/VkResource.h>
- class MyApp : public VkApp {
- public:
- MyApp(char *appClassName,
- int *arg_c,
- char **arg_v,
- XrmOptionDescRec *optionList = NULL,
- int sizeOfOptionList = 0);
- Boolean verbose() { return _verbose; } // Access function
- protected:
- Boolean _verbose; // Data member to initialize
- private:
- static XrmOptionDescRec _cmdLineOptions[]; // Command-line options
- static XtResource _resources[]; // Resource descriptions
- };
- // Describe the command line options
- XrmOptionDescRec MyApp::_cmdLineOptions[] =
- {
- {
- "-verbose", "*verbose", XrmoptionNoArg, "TRUE",
- },
- };
- // Describe the resources to retrieve and use to initialize the class
- XtResource MyApp::_resources [] = {
- {
- "verbose",
- "Verbose",
- XmRBoolean,
- sizeof ( Boolean ),
- XtOffset ( MyApp *, _verbose ),
- XmRString,
- (XtPointer) "FALSE",
- },
- };
- MyApp::MyApp(char *appClassName,
- int *arg_c,
- char **arg_v,
- XrmOptionDescRec *optionList,
- int sizeOfOptionList) : VkApp(appClassName,
- arg_c,
- arg_v,
- optionList,
- sizeOfOptionList)
- {
- // Parse the command line, loading options into the resource database
- *arg_c = parseCommandLine(_cmdLineOptions,
- XtNumber(_cmdLineOptions));
- // Initialize this class from the resource data base
- getResources (_resources, XtNumber(_resources));
- }