Getting Started

This section gives you information on example programs that you might find helpful when getting started with ViewKit ObjectPak programming. It first describes the simplest ViewKit ObjectPak program, which displays a window containing a single label, and discusses the structure of the program. Then, it discusses the demonstration programs provided with ViewKit ObjectPak.

Simple ViewKit Program

Applications based on ViewKit ObjectPak must obey certain conventions. To see how this organization works, consider the following simple example of a ViewKit ObjectPak application that displays the label "hello" in a window:

Example 1. hello.C Example


#include <Vk/VkApp.h>
#include <Vk/VkSimpleWindow.h>
#include <Xm/Label.h>
// Define a top-level window class
class HelloWindow: public VkSimpleWindow {
HelloWindow (const char *name);
virtual const char* className();
// Construct a single rooted widget tree, and designate the
// root of the tree as the window's view. This example is very
// simple, just creating a single XmLabel widget to display the
// string "hello".
HelloWindow::HelloWindow (const char *name) : VkSimpleWindow (name)
Widget label = XmCreateLabel (mainWindowWidget(), "hello",
NULL, 0);
const char* HelloWindow::className()
return "HelloWindow"; // Identify this class
// Empty
// Main driver. Just instantiate a VkApp and a top-level window,
// "show" the window and then "run" the application.
void main ( int argc, char **argv )
VkApp *app = new VkApp("Hello", &argc, argv);
HelloWindow *win = new HelloWindow("hello");

To build this example, simply compile the file hello.C and link with the ViewKit ObjectPak library, the help library, and the OSF/Motif and X libraries:

CC -o hello hello.C -lvk -lvkhelp -lXm -lXt -lX11

Running the hello program displays a window that says "hello," as shown in Figure 2.:

Figure 2. Result of Running hello

This example uses two classes: the VkApp class and an application-defined class, HelloWindow. The HelloWindow class is derived from the ViewKit ObjectPak VkSimpleWindow class, which is discussed in a moment.

First look at main(). All ViewKit ObjectPak applications start by creating an instance of VkApp. The arguments to this constructor specify the Xt-style class of the application, a pointer to argc, and the argv array. Instantiating a VkApp object opens a connection to the X server and initializes many other services needed by typical applications. VkApp is described in detail in Chapter 3--Application Class. Next, the hello.C program instantiates a HelloWindow object that serves as the application's top-level window. The constructor for this class requires only a name for the window. Finally, the application concludes by calling the HelloWindow object's show() function and the VkApp object's run() function. The run() method never returns. The body of most ViewKit ObjectPak programs is very similar to this short example.

Now examine the HelloWindow class. ViewKit ObjectPak encourages you to create classes to represent all major elements of the user interface. In this example, the only major user interface component is a top-level window containing a label widget. ViewKit provides a class, VkSimpleWindow, that supports many features common to all top-level windows and works closely with the VkApp class to implement various ViewKit ObjectPak features. To use the VkSimpleWindow class, you derive a new subclass and create a single-rooted widget tree that the window displays as its view. ViewKit applications do not have to create shell widgets directly.

The hello.C example is so simple that the HelloWindow class creates only a single XmLabel widget. The XmLabel widget is created in the constructor and then designated as the window's view. More complex classes might create a manager widget and create other widgets as children, or might instantiate other objects, as well. Chapter 4--ViewKit Windows describes how to create windows using ViewKit ObjectPak.

By convention, all ViewKit ObjectPak classes support the className() member function. This function is used by several ViewKit facilities, and is discussed in "VkComponent Access Functions" .

Demonstration Programs

Demonstration programs illustrate different features of ViewKit. For the purposes of documentation, ${ObjectPak} refers to the directory where ViewKit was installed. Demonstration programs include the following:

  • ${ObjectPak}/examples/ProgrammersGuide contains several of the example programs from this guide.
  • ${ObjectPak}/examples/Components/CBrowser contains the source for a component browser, which shows examples of many ObjectPak components. You might find this particularly useful to run when you read the later chapters in this guide that describe the prebuilt components shipped with ObjectPak.
  • ${ObjectPak}/examples/Applications/PhoneBook creates PhoneBook, a full-fledged application that keeps track of names, phone numbers, and addresses.PhoneBook uses a variety of ObjectPak classes.