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.
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:
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:
Insert #include statements at the beginning of the file to specify other files that contain information that your application requires. Alternatively, list these files as primary source files in your project's configuration. The latter method is recommended if you will be building your application exclusively with VisualAge for C++.
The following order is recommended for including source files in your application:
In certain cases, the Open Class Library headers
can detect whether the OS/2 Toolkit headers are
included and can define some Toolkit-specific
functions.
Files that you would typically specify as source files for your project include:
A header file that contains information about an Open Class Library class that your application uses. Note: all Open Class Library header files begin with the letter "i".
A header file that contains the definition of a class that you created.
A header file that contains your symbolic definitions. Include these as macro source files in your project's configuration.
Create the primary application window, call its functions to change settings, such as color, size, or position; call its inherited frame window functions to give it focus and have it displayed; and call ICurrentApplication::run to begin event processing.
Typically, a new class derived from IFrameWindow defines the main application window. This new class' constructor can follow the Main function. Its purpose is to initialize the inherited IFrameWindow, initialize data members that compose the new class, start event handlers, and set controls used in the frame window.
Once the application window is constructed, your application can call other classes to insert controls and dialogs into the window and to handle events.
Use a destructor to stop event handlers and delete any composed objects that you created using the new operator, except those IWindow objects set as autoDelete.
If the new class that defines the application window defines any new functions or overrides inherited functions, include them here.
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();
There are two ways to load resources into your application:
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(...) */
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.