Understanding an Open Class Library Application

An easy way to understand how the classes and objects work together is to look at a simple application, called Hello World version 1. This application has two basic user interface components:

The main window for Hello World version 1 looks like this:

Figure 1. Hello World Version 1 Main Window

One source file, the .cpp file, is required for this application.

Creating a C++ Source File

The Hello World application, versions 1 through 6, illustrates many Open Class Library features. Hello World version 1 has only a .cpp file. This file is the C++ source file used by VisualAge for C++ to generate the executable part of this application. A copy of the "Hello World" version 1 application is included in the online samples.

The listing of the C++ source file for the Hello World version 1 application follows:

#include <iapp.hpp>
#include <istattxt.hpp>
#include <iframe.hpp>
#include <icoordsy.hpp>
#include "ahellow1.h"

/**************************************************************************
* main - Application entry point for Hello World Version 1.               *
*                                                                         *
* Creates a new object mainWindow of class IFrameWindow                   *
* Creates a new object hello of class IStaticText                         *
* Sets the static text value and aligns it                                *
* Sets the static text as the client of the mainWindow                    *
* Sets the size of mainWindow                                             *
* Sets the window focus to mainWindow                                     *
* Displays the mainWindow                                                 *
* Starts the events processing for the application                        *
**************************************************************************/
int main()
{
    ICoordinateSystem::setApplicationOrientation(
    ICoordinateSystem::kOriginLowerLeft );
    IFrameWindow mainWindow (WND_MAIN);
    IStaticText hello(WND_HELLO, &mainWindow, &mainWindow);
    hello.setText(STR_HELLO);
    hello.setAlignment(IStaticText::centerCenter);
    mainWindow.setClient(&hello);

    mainWindow.sizeTo(ISize(400,300));
    mainWindow.setFocus();
    mainWindow.show();
    IApplication::current().run();
    return 0;
} /* end main() */

This application creates the following objects:

mainWindow
This IFrameWindow object is the main window for the application. It is constructed in line 46.
hello
This is the static text control (IStaticText) object that contains the phrase "Hello World!!!" from the .rc file. This object is constructed on line 47.
ISize object
An unnamed temporary ISize object used in the call to mainWindow.sizeTo.

Creating Applications

To create an Open Class Library application, you need to know which files to create and what goes into them. The following list describes the minimum files required for an application. Typically, the name of each file is the same; only the extensions differ.

File Name Contains
filename.cpp Primary C++ code for your application.
filename.hpp
Declaration of any class or classes that you create. You can put each class in a separate .hpp file or all classes in one file. If your classes are used in only one .cpp file, they can be declared in that .cpp file instead.

As you create more complex applications, separate your code into different files.

When you use the Open Class Library to write applications, use the following structure for your files:

Starting Event Processing

To develop an Open Class Library application, you can use IApplication and the single instance of its derived class, ICurrentApplication. Objects of the ICurrentApplication class represent the application that is currently running.

To start event processing for a C++ application using the Open Class Library, obtain a reference to ICurrentApplication by using the static member function IApplication::current. The instance of this class contains information about the application that is accessible to the ICurrentApplication::run member function executing in the process. The following example comes from the Hello World version 1 source file:

IApplication::current().run();

Loading Resources into an Application

There are two ways to load resources into your application:

  1. Code the values directly in the .cpp source file
  2. Create an .rc resource file
  3. Create a .dll resource library as a shared library.

The Open Class Library loads resources where necessary for an application. Use the ICurrentApplication member function setUserResourceLibrary to identify which resource library will be used if none is specified on a call that loads a resource. The following highlighted code shows an example.

int main(int argc, char **argv)                // Main function with arguments  
{   IApplication::current().                   //Save the command line arguments     
     setArgs(argc, argv);                      //in the current application object.
 
  IString Dllname(IApplication::current().argv(1));
 
  IApplication::current().                  // Get current application
   setUserResourceLibrary(dllname.asString()); // Set the name of resource DLL.
 
  AHelloWindow mainWindow (WND_MAIN);          // Create main window
 
  IApplication::current().run();               // Get current & run the application
  return 0;
} /* end main */

First, the library tries to create a frame window by loading a dialog with the WND_MAIN ID from specified resource library.

You can also determine the current user resource library by calling the userResourceLibrary member function. The following highlighted code shows an example.

AHelloWindow :: AHelloWindow(unsigned long windowId) : IFrameWindow(windowId)   // Call IFrameWindow constructor 
{   hello=new IStaticText(WND_HELLO,          // Create static text control     
         this, this);                         // Pass in myself as owner & parent.
  hello->setText(			      // Set text in static text control to library name.
   IApplication::current().
   userResourceLibrary().asString());
 
                                       
  hello->setAlignment(                 // Set alignment to center in both
     IStaticText::centerCenter);       //   directions
  setClient(hello);                    // Set hello control as client window
                                          
  setFocus();                          // Set focus to main window
  show();                              // Set to show main window
} /* end AHelloWindow :: AHelloWindow(...) */

Using Command-Line Arguments

With ICurrentApplication, you can record and query the command line arguments of your application. Set the arguments by calling setArgs with the arguments that were passed to the main function.

To query the number of arguments, use the member function ICurrentApplication::argc. This member function always returns a nonzero value because it has at least the name of the application as a parameter.

To get the nth parameter, use the member function ICurrentApplication::argv, where the argv(0) component is always the name of the application. Because argv is returned as an IString, you can use all the overloaded operators for this class.

For example, the following code records the command line parameters.

int main(int argc, char **argv)
{
    IApplication::current().setArgs(argc, argv);
}

where argc is the number of arguments received and argv represents the actual arguments.