This appendix lists and discusses some of the source files for the simple calculator application built in Chapter 1, “RapidApp Tour.” The version of the calculator program in this appendix includes the Calculator component created in “Creating Components.”
The body of any program generated by RapidApp is very simple. Example D-1 lists the main.C file for the calculator application.
//////////////////////////////////////////////////////////////////////
// This is a driver ViewKit program generated by RapidApp 1.2
//
// This program instantiates a ViewKit VkApp object and creates
// any main window objects that are meant to be shown at startup.
// Although editable code blocks are provided, there should rarely.
// be any reason to modify this file. Make application-specific
// changes in the classes created by the main window classes
// You can add also additional initialization in subclasses of VkApp
//////////////////////////////////////////////////////////////////////
#include <Vk/VkApp.h>
// Headers for window classes used in this program
#include "CalcWindow.h"
//---- Start editable code block: headers and declarations
//---- End editable code block: headers and declarations
void main ( int argc, char **argv )
{
//---- Start editable code block: main initialization
//---- End editable code block: main initialization
VkApp *app;
// Create an application object
app = new VkApp("Calculator", &argc, argv);
//---- Start editable code block: post init
//---- End editable code block: post init
// Create the top level windows
VkSimpleWindow *calcWindow = new CalcWindow("calcWindow");
calcWindow->show();
//---- Start editable code block: event loop
//---- End editable code block: event loop
app->run ();
}
|
This file simply instantiates an IRIS ViewKit VkApp class, then creates a CalcWindowMainWindow object before entering an event loop (the run() statement).
The CalcWindowMainWindow class is a simple top-level IRIS ViewKit window class derived from VkSimpleWindow. This class provides the basic functionality of a shell widget and handles window manager interaction. You normally shouldn't edit this class's files, but it's worthwhile to see what the code does. Example D-2 lists the CalcWindowMainWindow header file, and Example D-3 lists the CalcWindowMainWindow source file.
//////////////////////////////////////////////////////////////
//
// Header file for CalcWindow
//
// This class is a subclass of VkSimpleWindow
//
// Normally, very little in this file should need to be changed.
// Create/add/modify menus using RapidApp.
//
// Restrict changes to those sections between
// the "//--- Start/End editable code block" markers
// Doing so will allow you to make changes using RapidApp
// without losing any changes you may have made manually
//
//////////////////////////////////////////////////////////////
#ifndef CALCWINDOW_H
#define CALCWINDOW_H
#include <Vk/VkSimpleWindow.h>
//---- Start editable code block: headers and declarations
//---- End editable code block: headers and declarations
//---- CalcWindow class declaration
class CalcWindow: public VkSimpleWindow {
public:
CalcWindow( const char * name,
ArgList args = NULL,
Cardinal argCount = 0 );
~CalcWindow();
const char *className();
virtual Boolean okToQuit();
//---- Start editable code block: CalcWindow public
//---- End editable code block: CalcWindow public
protected:
// Classes created by this class
class Calculator *_calculator;
// Widgets created by this class
//---- Start editable code block: CalcWindow protected
//---- End editable code block: CalcWindow protected
private:
static String _defaultCalcWindowResources[];
//---- Start editable code block: CalcWindow private
//---- End editable code block: CalcWindow private
};
//---- Start editable code block: End of generated code
//---- End editable code block: End of generated code
#endif
|
//////////////////////////////////////////////////////////////
//
// Source file for CalcWindow
//
// This class is a subclass of VkSimpleWindow
//
//
// Normally, very little in this file should need to be changed.
// Create/add/modify menus using RapidApp.
//
// Try to restrict any changes to the bodies of functions
// corresponding to menu items, the constructor and destructor.
//
// Restrict changes to those sections between
// the "//--- Start/End editable code block" markers
//
// Doing so will allow you to make changes using RapidApp
// without losing any changes you may have made manually
//
// Avoid gratuitous reformatting and other changes that might
// make it difficult to integrate changes made using RapidApp
//////////////////////////////////////////////////////////////
#include "CalcWindow.h"
#include <Vk/VkApp.h>
#include <Vk/VkResource.h>
// Externally defined classes referenced by this class:
#include "Calculator.h"
extern void VkUnimplemented ( Widget, const char * );
//---- Start editable code block: headers and declarations
//---- End editable code block: headers and declarations
// These are default resources for widgets in objects of this class
// All resources will be prepended by *<name> at instantiation,
// where <name> is the name of the specific instance, as well as the
// name of the baseWidget. These are only defaults, and may be overriden
// in a resource file by providing a more specific resource name
String CalcWindow::_defaultCalcWindowResources[] = {
"*title: Calculator",
(char*)NULL
};
//---- Class declaration
CalcWindow::CalcWindow ( const char *name,
ArgList args,
Cardinal argCount) :
VkSimpleWindow ( name, args, argCount )
{
// Load any class-default resources for this object
setDefaultResources ( baseWidget(), _defaultCalcWindowResources );
// Create the view component contained by this window
_calculator = new Calculator ( "calculator",mainWindowWidget() );
XtVaSetValues ( _calculator->baseWidget(),
XmNwidth, 201,
XmNheight, 141,
(XtPointer) NULL );
// Add the component as the main view
addView ( _calculator );
//---- Start editable code block: CalcWindow constructor
//---- End editable code block: CalcWindow constructor
} // End Constructor
CalcWindow::~CalcWindow()
{
delete _calculator;
//---- Start editable code block: CalcWindow destructor
//---- End editable code block: CalcWindow destructor
} // End destructor
const char *CalcWindow::className()
{
return ("CalcWindow");
} // End className()
Boolean CalcWindow::okToQuit()
{
//---- Start editable code block: CalcWindow okToQuit
// This member function is called when the user quits by calling
// theApplication->terminate() or uses the window manager close protocol
// This function can abort the operation by returning FALSE, or do some.
// cleanup before returning TRUE. The actual decision is normally passed on
// to the view object
// Query the view object, and give it a chance to cleanup
return ( _calculator->okToQuit() );
//---- End editable code block: CalcWindow okToQuit
} // End okToQuit()
//---- Start editable code block: End of generated code
//---- End editable code block: End of generated code
|
Note the “End Generated Code Section” markers. If you need to modify this class, do so below these markers only.
CalcWindowMainWindow declares the pointer to the Calculator component that it creates as a protected data member. This allows you to access the Calculator component in any member functions that you add to this class.
The CalcWindowMainWindow constructor calls the VkSimpleWindow constructor and then instantiates a Calculator object. After setting the initial size of the component, the constructor adds the Calculator object as a view of the window.
The CalcWindowMainWindow destructor deletes the Calculator object created by the window.
The className() member function is a “boilerplate” function that all IRIS ViewKit components must implement to support X resource management.
Before the program exits, the VkApp class calls the okToQuit() member function for each top-level window in the program. This gives a program a chance to clean up (for example, closing databases) or abort the shutdown if necessary. The CalcWindowMainWindow::okToQuit() member function that RapidApp generates simply calls the okToQuit() function of the Calculator component.
The CalculatorUI class contains all the code required to create the user interface. These files are rather long, so aren't listed in this appendix. Normally, you shouldn't change the header or source files for this class. Almost everything you might want to do can be handled in the derived class or by using RapidApp.
Calculator is the user-defined class you created in RapidApp. RapidApp automatically places most of the user interface code in the base class, CalculatorUI, so the Calculator class itself is very simple. The class header, shown in Example D-4, declares constructors, destructors, and a virtual function, add(), which is the function called when the user presses the “=” button on the calculator interface.
//////////////////////////////////////////////////////////////
//
// Header file for Calculator
//
// This file is generated by RapidApp 1.2
//
// This class is derived from CalculatorUI which
// implements the user interface created in
// RapidApp. This class contains virtual
// functions that are called from the user interface.
//
// When you modify this header file, limit your changes to those
// areas between the "//--- Start/End editable code block" markers
//
// This will allow RapidApp to integrate changes more easily
//
// This class is a ViewKit user interface "component".
// For more information on how components are used, see the
// "ViewKit Programmers' Manual", and the RapidApp
// User's Guide.
//////////////////////////////////////////////////////////////
#ifndef CALCULATOR_H
#define CALCULATOR_H
#include "CalculatorUI.h"
//---- Start editable code block: headers and declarations
//---- End editable code block: headers and declarations
//---- Calculator class declaration
class Calculator : public CalculatorUI
{
public:
Calculator(const char *, Widget);
Calculator(const char *);
~Calculator();
const char * className();
static VkComponent *CreateCalculator( const char *name, Widget parent );
//---- Start editable code block: Calculator public
//---- End editable code block: Calculator public
protected:
// These functions will be called as a result of callbacks
// registered in CalculatorUI
virtual void add ( Widget, XtPointer );
//---- Start editable code block: Calculator protected
//---- End editable code block: Calculator protected
private:
static void* RegisterCalculatorInterface();
//---- Start editable code block: Calculator private
//---- End editable code block: Calculator private
};
#endif
|
The Calculator.C source file consists primarily of empty functions. Most of the work is done by the CalculatorUI class. The listing shown in Example D-5 displays in bold the code you added to implement the class's functionality. This consists of changes to the add() function and two additional header files.
///////////////////////////////////////////////////////////// // // Source file for Calculator // // This file is generated by RapidApp 1.2 // // This class is derived from CalculatorUI which // implements the user interface created in // RapidApp. This class contains virtual // functions that are called from the user interface. // // When you modify this source, limit your changes to // modifying the sections between the // "//--- Start/End editable code block" markers // // This will allow RapidApp to integrate changes more easily // // This class is a ViewKit user interface "component". // For more information on how components are used, see the // "ViewKit Programmers' Manual", and the RapidApp // User's Guide. ///////////////////////////////////////////////////////////// #include "Calculator.h" #include <Xm/BulletinB.h> #include <Xm/Label.h> #include <Xm/PushB.h> #include <Xm/Separator.h> #include <Xm/TextF.h> #include <Vk/VkResource.h> #include <Vk/VkSimpleWindow.h> extern void VkUnimplemented(Widget, const char *); /////////////////////////////////////////////////////////////////////////////// // The following non-container elements are created by CalculatorUI and are // available as protected data members inherited by this class // // XmPushButton _pushbutton // XmTextField _result // XmSeparator _separator // XmLabel _label // XmTextField _value2 // XmTextField _value1 // /////////////////////////////////////////////////////////////////////////////// //---- Start editable code block: headers and declarations #include <stdlib.h> #include <Vk/VkFormat.h> //---- End editable code block: headers and declarations //---- Calculator Constructor Calculator::Calculator(const char *name, Widget parent) : CalculatorUI(name, parent) { // This constructor calls CalculatorUI(parent, name) // which calls CalculatorUI::create() to create // the widgets for this component. Any code added here // is called after the component's interface has been built //---- Start editable code block: Calculator constructor //---- End editable code block: Calculator constructor } // End Constructor Calculator::Calculator(const char *name) : CalculatorUI(name) { // This constructor calls CalculatorUI(name) // which does not create any widgets. Usually, this // constructor is not used //---- Start editable code block: Calculator constructor 2 //---- End editable code block: Calculator constructor 2 } // End Constructor Calculator::~Calculator() { // The base class destructors are responsible for // destroying all widgets and objects used in this component. // Only additional items created directly in this class // need to be freed here. //---- Start editable code block: Calculator destructor //---- End editable code block: Calculator destructor } const char * Calculator::className() // classname { return ("Calculator"); } // End className() void Calculator::add ( Widget w, XtPointer callData ) { //---- Start editable code block: Calculator add XmPushButtonCallbackStruct *cbs = (XmPushButtonCallbackStruct*) callData; //--- Comment out the following line when Calculator::add is implemented: // ::VkUnimplemented ( w, "Calculator::add" ); int x, y; x = atoi(XmTextFieldGetString(_value1)); y = atoi(XmTextFieldGetString(_value2)); XmTextFieldSetString(_result, (char *)VkFormat("%d", x+y)); //---- End editable code block: Calculator add } // End Calculator::add() /////////////////////////////////////////////////////////////////// // static creation function, for importing class into rapidapp // or dynamically loading, using VkComponent::loadComponent /////////////////////////////////////////////////////////////////// VkComponent *Calculator::CreateCalculator( const char *name, Widget parent ) { VkComponent *obj = new Calculator ( name, parent ); return ( obj ); } // End CreateCalculator /////////////////////////////////////////////////////////////////// // Function for accessing a description of the dynamic interface // to this class. /////////////////////////////////////////////////////////////////// void *Calculator::RegisterCalculatorInterface() { // This structure registers information about this class // that allows RapidApp to create and manipulate an instance. // Each entry provides a resource name that will appear in the // resource manager palette when an instance of this class is // selected, the name of the member function as a string, // the type of the single argument to this function, and an. // optional argument indicating the class that defines this function. // All functions must have the form // // void memberFunction(Type); // // where "Type" is one of: // const char * (Use XmRString) // Boolean (Use XmRBoolean) // int (Use XmRInt) // float (Use XmRFloat) // No argument (Use VkRNoArg // A filename (Use VkRFilename) // An enumeration (Use "Enumeration:ClassName:TYPE: VALUE1, VALUE2, VALUE3") static VkCallbackObject::InterfaceMap map[] = { //---- Start editable code block: CalculatorUI resource table // { "resourceName", "setAttribute", XmRString}, //---- End editable code block: CalculatorUI resource table { NULL }, // MUST be NULL terminated }; return map; } // End RegisterCalculatorInterface() //---- End of generated code //---- Start editable code block: End of generated code //---- End editable code block: End of generated code |
The Calculator file contains the default values for various X resources used by the calculator application. Example D-6 lists the calculator resource file. You typically shouldn't need to edit this file.
! ! Generated by Silicon Graphic's RapidApp. ! ! ! RapidApp 1.2. ! ! ! !Activate schemes and sgi mode by default ! Calculator*useSchemes: all Calculator*sgiMode: true ! !SGI Style guide specifies explicit focus within applications ! Calculator*keyboardFocusPolicy: explicit Calculator*calcWindow.title: Calculator Calculator*label.labelString: + Calculator*pushbutton.labelString: = Calculator*showHelp: True !!---- Start editable code block: app-defaults Calculator*applicationVersionString: Version 1.0 Created by RapidApp(tm) !!---- End editable code block: app-defaults |
The Makefile follows Silicon Graphics conventions. It also uses a few simple conventions that make it easier to maintain from within RapidApp.
#!smake
#
# Makefile for calculator
# Generated by RapidApp 1.2
#
# This makefile follows various conventions used by SGI makefiles
# See the RapidApp User's Guide for more information
# This makefile supports most common default rules, including:
# make (or make all): build the application or library
# make install: install the application or library on the local machine
# make image: create "inst" images for distribution
# make clean: remove .o's, core, etc.
# make clobber: make clean + remove the target application.
# You should be able to customize this Makefile by editing
# only the section between the ##---- markers below.
# Specify additional files, compiler flags, libraries
# by changing the appropriate variables
include $(ROOT)/usr/include/make/commondefs
##---- Start editable code block: definitions
###########################################################
###########################################################
# Modify the following variables to customize this makefile
###########################################################
###########################################################
#
# Local Definitions
#
# Directory in which inst images are placed
IMAGEDIR= images
# Add Additional libraries to USERLIBS:
USERLIBS=
# While developing, leave OPTIMIZER set to -g.
# For delivery, change to -O2
OPTIMIZER= -g
#
# Add any files added outside the builder here
#
USERFILES =
#
# Add compiler flags here
#
USERFLAGS =
##---- End editable code block: definitions
# The GL library being used, if needed
GLLIBS=
COMPONENTLIBS=
#
# The ViewKit stub help library (-lvkhelp) provides a simple
# implementation of the SGI help API. Changing this to -lhelpmsg
# switches to the full IRIS Insight help system
#
HELPLIB= -lvkhelp
# Standard ViewKit header and libraries
VIEWKITFLAGS= -I$(ROOT)/usr/include/Vk
TOOLTALKLIBS=
NETLS=
EZLIB =
VIEWKITLIBS= $(TOOLTALKLIBS) $(EZLIB) -lvk -lvkSGI $(HELPLIB) $(NETLS) -lSgm -lXpm
# Local C++ options.
# woff 3262 shuts off warnings about arguments that are declared
# but not referenced.
WOFF= -woff 3262
LCXXOPTS = -nostdinc -I. -I$(ROOT)/usr/include $(SAFLAG) $(WOFF) $(VIEWKITFLAGS) $(USERFLAGS)
LLDLIBS = -L$(ROOT)/usr/lib $(USERLIBS) $(COMPONENTLIBS) $(VIEWKITLIBS) $(GLLIBS) -lXm -lXt -lX11 -lgen
# SGI makefiles don't recognize all C++ sufixes, so set up
# the one being used here.
CXXO3=$(CXXO2:.C=.o)
CXXOALL=$(CXXO3)
#
# Source Files generated by RapidApp. If files are added
# manually, add them to USERFILES
#
BUILDERFILES = main.C\\p CalcWindow.C\\p Calculator.C\\p CalculatorUI.C\\p unimplemented.C\\p $(NULL)
C++FILES = $(BUILDERFILES) $(USERFILES)
#
# The program being built
#
TARGETS=calculator
APPDEFAULTS=Calculator
default all: $(TARGETS)
$(TARGETS): $(OBJECTS)
$(C++F) $(OPTIMIZER) $(OBJECTS) $(LDFLAGS) -o $@
#
# These flags instruct the compiler to output
# analysis information for cvstatic
# Uncoment to enable
# Be sure to also disable smake if cvstatic is used
#SADIR= Motif.cvdb
#SAFLAG= -sa,$(SADIR)
#$(OBJECTS):$(SADIR)/cvdb.dbd
#$(SADIR)/cvdb.dbd :
# [ -d $(SADIR) ] || mkdir $(SADIR)
# cd $(SADIR); initcvdb.sh
#LDIRT=$(SADIR) vista.taf
#
# To install on the local machine, do 'make install'
#
install: all
$(INSTALL) -F /usr/lib/X11/app-defaults Calculator
$(INSTALL) -F /usr/sbin calculator
$(INSTALL) -F /usr/lib/images Calculator.icon
#
# To create inst images, do 'make image'
# An image subdirectory should already exist
#
$(IMAGEDIR):
@mkdir $(IMAGEDIR)
image: $(TARGETS) $(IMAGEDIR)
/usr/sbin/gendist -rbase / -sbase `pwd` -idb calculator.idb \\p -spec calculator.spec \\p -dist /usr/share/src/RapidApp/Calculator/Motif/images -all
include $(COMMONRULES)
|
You should rarely need to modify most of the Makefile. However, the editable code block area is intended to be changed. You can edit variables declared in that area to add files, libraries, and change other aspects of the Makefile.
The variable IMAGEDIR controls the location at which installable images are generated. The default is a subdirectory of the current directory called images.
To add source files to the Makefile that you create outside of RapidApp, simply list them after the USERFILES variable; they will be compiled the next time you build your application.