Adding Application-Specific Behavior

Developers can add application-specific functionality by extending the ViewKit library through subclassing. For example, the following simple program extends the generic application in two ways. First, instead of VkApp, this program creates an instance of VkMsgApp, a subclass of VkApp provided by the ViewKit framework. This class automatically handles the details of allowing applications to send and receive ToolTalk messages. Because this example instantiates a VkMsgApp object, the program can automatically receive and handle certain minimum ToolTalk messages, and it is easy to extend the application to allow it to send or receive additional messages.

This program defines some application-specific behavior by subclassing from the VkWindow class provided by the ViewKit framework. The MyWindow class inherits all the behavior of the VkWindow class, but adds a ViewKit component as a view to be displayed in the window. Subclasses of VkWindow can add any ViewKit component, any application-defined component, or any Motif widget as a view. This example adds an instance of the VkOutline class, which is a simple hierarchy browser included in the ViewKit library. The MyWindow class programmatically adds several items to the hierarchy, and then displays the entire outline.

The main program is similar to the generic application discussed earlier, but instantiates VkMsgApp and MyWindow objects to implement the desired behavior.


///////////////////////////////////////////////////////
// outline: exercise the ViewKit outline component
//////////////////////////////////////////////////////
#include 
#include 
#include 

class MyWindow : public VkWindow {

   public:
     MyWindow(const char *name);  
};


MyWindow::MyWindow(const char *name) : VkWindow(name)
{
     // Create a VkOutliner component and add it as a view
    VkOutline *outliner = new VkOutline("outliner", (*this));
     addView(outliner);

    // Construct a hierarchy by adding parent/child pairs 

    outliner->add("Heading 1",   "SubHeading 1");
    outliner->add("Heading 1",   "SubHeading 2");
    outliner->add("Heading 1",   "SubHeading 3");
    outliner->add("SubHeading 1", "Item 1");

    outliner->displayAll();  // Show the entire outline
}

void main ( int argc, char **argv )
{
    VkMsgApp  *app   = new VkMsgApp("Outline", &argc, argv);
    MyWindow  *win   = new MyWindow("outline");

    win->show();  // Display the window
    app->run();   // Run the application
}

Figure 6 shows this program as it appears on the screen. The various hierarchy levels can be collapsed or expanded by clicking on the arrows beside each heading.

Figure 6. ViewKit application with application-specific behavior.

___________________________________________________________________