home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
VSCPPv8.zip
/
VACPP
/
IBMCPP
/
samples
/
IOC
/
HELLO5
/
AHELLOW5.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1995-05-01
|
35KB
|
464 lines
/*****************************************************************************
* HELLO WORLD SAMPLE PROGRAM - Version 5: Class Implementation (ahellow5.cpp)*
* *
* COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1995. *
* *
* DISCLAIMER OF WARRANTIES: *
* The following [enclosed] code is sample code created by IBM *
* Corporation. This sample code is not part of any standard IBM product *
* and is provided to you solely for the purpose of assisting you in the *
* development of your applications. The code is provided "AS IS", *
* without warranty of any kind. IBM shall not be liable for any damages *
* arising out of your use of the sample code, even if they have been *
* advised of the possibility of such damages. *
*****************************************************************************/
//NOTE: WE RECOMMEND USING A FIXED-SPACE FONT TO LOOK AT THE SOURCE.
/************************************************************************** V1
* C++ Hello World History and Key Functions: * V1
* Version 1: (lines with V1 in column 79-80) * V1
* - Creates and runs a simple application * V1
* - Creates the main window (IFrameWindow) * V1
* - Creates a static text control set to "Hello, World!!!" as the * V1
* client window * V1
* * V2
* Version 2: (lines with V2 in column 79-80) * V2
* - Creates a main window (AHellowWindow) derived from IFrameWindow * V2
* - Gets the "Hello, World!!!" text string and other items from a * V2
* resource file * V2
* - Sets the window title from a resource file * V2
* - Creates and sets the information area below the client window * V2
* * V3
* Version 3: (lines with V3 in column 79-80) * V3
* - Adds alignment menu bar with left, center, and right menu items * V3
* - Adds command processing for aligning the * V3
* "Hello, World!!!" text * V3
* - Places check mark in front of selected menu item * V3
* - Creates and maintains current alignment status line * V3
* * V4
* Version 4: (lines with V4 in column 79-80) * V4
* - Adds accelerator keys for Left (F7), Center(F8), Right(F9) * V4
* - Modifies left, center and right menu items to show function keys * V4
* - Modifies menu bar to create edit menu and "Text..." menu item * V4
* - Adds ADialogText to allow user to change "Hello, World!!!" text * V4
* - Adds push buttons to an ISetCanvas for changing text alignment * V4
* * V5
* Version 5: (lines with V5 in column 79-80) * V5
* - Uses a vertical split canvas as the client window * V5
* - Adds a horizontal split canvas as the left side of the client * V5
* - Adds the hello static text to the top of the horiz. split canvas * V5
* - Adds a new control (AEarthWindow) using native graphics calls * V5
* - Adds AEarthWindow to the bottom of the horizontal split canvas * V5
* - Adds a list box to the right side of the vertical split canvas * V5
* - Adds help windows for the main, dialog, and entry field windows * V5
**************************************************************************/
//Include User Interface Class Library class headers:
#ifndef _IBASE_ //Make sure ibase.hpp is included
#include <ibase.hpp> // since that is where IC_<environ>
#endif // is defined.
#include <iapp.hpp> //IApplication class V1
#include <ifont.hpp> //IFont class V3
#include <istring.hpp> //IString class V4
#include <imsgbox.hpp> //IMessageBox class V5
#include "aearthw5.hpp" //AEarthWindow class V5
#include "ahellow5.hpp" //Include AHelloWindow class headers v5
#include "ahellow5.h" //Include symbolic definitions v5
#include "adialog5.hpp" //Include ATextDialog class v5
/************************************************************************** V1
* main - Application entry point for Hello World Version 5. * v5
* This simple application does the following: * V1
* 1) Creates a new object mainWindow of class AHelloWindow * v2
* 2) Sets Hello World window alignment * V3
* 3) Sets the size of mainWindow * v3
* 4) Sets the window focus to mainWindow * v3
* 5) Displays the mainWindow * v3
* 6) Starts the events processing for the application * v3
**************************************************************************/
int main() //V1
{ //V1
AHelloWindow mainWindow (WND_MAIN); //v2
mainWindow.setAlignment(AHelloWindow::left); //V3
mainWindow.sizeTo(ISize(400,300)); //V1
mainWindow.setFocus(); //V1
mainWindow.show(); //V1
IApplication::current().run(); //V1
return 0;
} /* end main */ //V1
/************************************************************************** V2
* AHelloWindow :: AHelloWindow - Constructor for the main window * V2
* Construct the IFrameWindow using the default style plus minimizedIcon, * V2
* which gets the Icon identified in the resource file and associates it * V2
* with the main window. The accelerator style causes the accelerator * v4
* table to be loaded from the resource file. * V4
* Create a menu bar object for the main window menu bar that was loaded * V3
* from the resource file. menuBar is used by setAlignment to * V3
* manipulate check marks on the menu items. * V3
* Create a static text object for displaying the status of the * V3
* Hello World text alignment. * V3
* Create the clientWindow split canvas to be used as the client window * V5
* of this frame and let its orientation default to vertical. * V5
* Create the helloCanvas split canvas making the clientWindow its parent * V5
* window and orient it horizontally. * V5
* Create the hello and earthWindow static text objects as children of * v5
* the helloCanvas. hello is placed in the topmost window of the * V5
* horizontal split canvas because it is created first. earthWindow * V5
* is placed in the bottom window of the split canvas. * V5
* Create the listBox window making the clientWindow its parent window * V5
* and enabling it for tabbing and preventing it from being resized * V5
* due to an item being too long. helloCanvas is placed in the * V5
* leftmost window of the vertical split canvas because it is created * V5
* first. listBox is placed in the right window of the split canvas. * V5
* Create a set canvas that will contain the push buttons and will be * V4
* added to the frame as an extension. * V4
* Create each push button with the set canvas as parent and owner. * V4
* Additionally for Help, specify help style and noPointerFocus style. * V5
* noPointerFocus prevents the mouse from changing the input focus * V5
* to the Help push button when you select it using the mouse. This * V5
* allows you to use contextual help for the control with the input * V5
* focus rather than for the Help push button itself. * V5
* Create the Hello World information area object from IInfoArea class. * V2
* The information area is automatically added as an extension below * V2
* the client window of the frame. * V2
* Create a command handler to process command events from menu item, * v4
* push button, and accelerator key selections. * v4
* Create a select handler to process selections made in the list box. * V5
* Create an IHelpWindow object that is displayed when help is requested * V5
* from a help menu item, the help push button, or the help accelerator * V5
* key, normally F1. * V5
* An IHelpHandler object, helpHandler is created implicitly, see class * V5
* definition for AHelloWindow in ahellow5.hpp. * V5
**************************************************************************/ //V2
AHelloWindow :: AHelloWindow(unsigned long windowId) //V2
: IFrameWindow(IFrameWindow::defaultStyle() | //V2
IFrameWindow::minimizedIcon | //v4
IFrameWindow::accelerator, //V4
windowId) //V2
,menuBar(windowId, this) //V3
,statusLine(WND_STATUS, this, this) //V3
,clientWindow(WND_CANVAS, this, this) //V5
,helloCanvas(WND_HCANVAS, &clientWindow, &clientWindow, IRectangle(), //V5
IWindow::visible | ISplitCanvas::horizontal) //V5
,hello(WND_HELLO, &helloCanvas, &helloCanvas) //v5
,earthWindow(WND_EARTH, &helloCanvas) //V5
,listBox(WND_LISTBOX, &clientWindow, &clientWindow, IRectangle(), //V5
IListBox::defaultStyle() | //V5
IControl::tabStop | //V5
IListBox::noAdjustPosition) //V5
,buttons(WND_BUTTONS, this, this) //V4
,leftButton(MI_LEFT, &buttons, &buttons) //V4
,centerButton(MI_CENTER, &buttons, &buttons) //V4
,rightButton(MI_RIGHT, &buttons, &buttons) //V4
,helpButton(MI_HELP, &buttons, &buttons, IRectangle(), //V5
IPushButton::defaultStyle() | //V5
IPushButton::help | IButton::noPointerFocus) //V5
,infoArea(this) //V2
,commandHandler(this) //V3
,selectHandler(this) //V5
,helpWindow(HELP_TABLE,this) //V5
{ //V2
/*------------------------- Setup the Client Window ----------------------| V5
| Set the clientWindow split canvas as the client window for the | V5
| AHelloWorld frame. | V5
| Set the HelloCanvas object to occupy 60% of the client canvas. | V5
| Set the list box object to occupy 40% of the client canvas. | V5
|------------------------------------------------------------------------*/ //V5
setClient(&clientWindow); //V5
clientWindow.setSplitWindowPercentage(&helloCanvas, 60); //V5
clientWindow.setSplitWindowPercentage(&listBox, 40); //V5
/*------------------------- Setup the Status Line ------------------------| V3
| Add the status line as an extension to the frame above the client | V3
| window with the height calculated from the maximum height of a | V3
| character in the current font. | V3
|------------------------------------------------------------------------*/ //V3
addExtension(&statusLine, IFrameWindow::aboveClient, //V3
IFont(&statusLine).maxCharHeight()); //V3
/*-------------------------- Setup the List Box --------------------------| V5
| Add the different language versions of Hello World to the list box in | V5
| ascending order. Hello World text strings are stored in the | V5
| resource file with IDs beginning with HI_WORLD with each subsequent | V5
| ID incremented up to HI_WORLD + HI_COUNT - 1. | V5
| Have the select handler handle selections made in the list box. | V5
| The select handler's selected() function is called to process | V5
| list box selections. | V5
|------------------------------------------------------------------------*/ //V5
for (int i=0;i<HI_COUNT;i++ ) //V5
listBox.addAscending(HI_WORLD+i); //V5
selectHandler.handleEventsFor(&listBox); //V5
/*---------------------- Setup the Information Area ----------------------| V5
| Change the size of the information area using the maximum height of | V5
| a character in the current font. | V5
|------------------------------------------------------------------------*/ //V5
setExtensionSize(&infoArea, IFont(&infoArea).maxCharHeight()); //V5
/*---------------------------- Set Text Values ---------------------------| V2
| Set the values for the text controls from strings in the resource file.| V2
| The infoArea inactive text is displayed when no menu item is active. | V2
|------------------------------------------------------------------------*/ //V2
hello.setText(STR_HELLO); //V2
leftButton.setText(STR_LEFTB); //V4
centerButton.setText(STR_CENTERB); //V4
rightButton.setText(STR_RIGHTB); //V4
helpButton.setText(STR_HELPB); //V5
infoArea.setInactiveText(STR_INFO); //V2
/*------------------- Setup Push Buttons Set Canvas ----------------------| V4
| Enable the first button as a tab stop. | V4
| Set the canvas margins and padding between buttons to zero. | V4
| Add the set canvas that contains the push buttons to the frame window | V4
| as an extension immediately below the client window with a height | V4
| equal to the minimum height of the push buttons. You must set the | V4
| text in the push buttons first. | V4
|------------------------------------------------------------------------*/ //V4
leftButton.enableTabStop(); //V4
buttons.setMargin(ISize()); //V4
buttons.setPad(ISize()); //V4
addExtension(&buttons, IFrameWindow::belowClient, //V4
(unsigned long)buttons.minimumSize().height()); //V4
/*----------------------- Setup the Command Handler ----------------------| V3
| Have the command handler handle commands sent from the frame window. | V3
| The command handler's command() function is called to process | V3
| menu item, push button, and accelerator key selections. | v5
|------------------------------------------------------------------------*/ //V3
commandHandler.handleEventsFor(this); //V3
/*---------------------------- Setup Help --------------------------------| V5
| Add the help library to the help window using addLibraries(). | V5
| Set the help window title from a string in the resource file. | V5
| Begin handling help events for the frame window. | V5
|------------------------------------------------------------------------*/ //V5
try //V5
{ //V5
helpWindow.addLibraries( "ahellow5.hlp" ); //V5
helpWindow.setTitle(STR_HTITLE); //V5
helpHandler.handleEventsFor(this); //V5
} //V5
catch( ... ) //V5
{ //V5
IMessageBox //V5
msgBox( this ); //V5
msgBox.show( STR_HELP_NOT_FOUND, IMessageBox::warning ); //V5
} //V5
/*----------------------- Set Default Alignment --------------------------| V2
| Align the static text, set the status line, and set the check mark in | v3
| the menu bar. | v3
|------------------------------------------------------------------------*/ //V2
setAlignment(center); //v3
} /* end AHelloWindow :: AHelloWindow(...) */ //V2
/************************************************************************** V3
* AHelloWindow :: ~AHelloWindow - Destructor for the main window * V3
* Stop handling command events for the frame. * V3
* Stop handling select events for the list box. * V5
* Stop handling help events for the frame. * V5
**************************************************************************/ //V3
AHelloWindow :: ~AHelloWindow() //V3
{ //V3
commandHandler.stopHandlingEventsFor(this); //V3
selectHandler.stopHandlingEventsFor(&listBox); //V5
helpHandler.stopHandlingEventsFor(this); //V5
} /* end AHelloWindow :: ~AHelloWindow() */ //V3
/************************************************************************** V3
* AHelloWindow :: setAlignment - Align static text in client window * V3
**************************************************************************/ //V3
AHelloWindow & //V3
AHelloWindow :: setAlignment(Alignment alignment) //V3
{ //V3
/*--------------------- Update Window for New Alignment ------------------| V3
| Depending on the value passed, update the window as follows: | V3
| Set the alignment of the static text control in the client window. | V3
| Set the text of the alignment status line static text control. | V3
| Check the selected menu item; remove check marks from the other two. | V3
|------------------------------------------------------------------------*/ //V3
switch(alignment) //V3
{ //V3
case left: //V3
hello.setAlignment( //V3
IStaticText::centerLeft); //V3
statusLine.setText(STR_LEFT); //V3
menuBar.uncheckItem(MI_CENTER); //V3
menuBar.checkItem(MI_LEFT); //V3
menuBar.uncheckItem(MI_RIGHT); //V3
break; //V3
case center: //V3
hello.setAlignment( //V3
IStaticText::centerCenter); //V3
statusLine.setText(STR_CENTER); //V3
menuBar.checkItem(MI_CENTER); //V3
menuBar.uncheckItem(MI_LEFT); //V3
menuBar.uncheckItem(MI_RIGHT); //V3
break; //V3
case right: //V3
hello.setAlignment( //V3
IStaticText::centerRight); //V3
statusLine.setText(STR_RIGHT); //V3
menuBar.uncheckItem(MI_CENTER); //V3
menuBar.uncheckItem(MI_LEFT); //V3
menuBar.checkItem(MI_RIGHT); //V3
break; //V3
} //V3
return (*this); //Return a reference to the frame V3
} /* end AHelloWindow :: setAlignment(...) */ //V3
/************************************************************************** V4
* AHelloWindow :: editText * V4
* Create and show a dialog window for inputting text that will be used * V4
* to replace the "Hello World!!!" static text string. * V4
**************************************************************************/ //V4
AHelloWindow & //V4
AHelloWindow :: editText() //V4
{ //V4
/*----------------------- Setup for Displaying Dialog --------------------| V4
| Store the current value of the text to be changed. | V4
| Set the text in the information area from the dialog information | V4
| string in the resource file. | V4
|------------------------------------------------------------------------*/ //V4
IString textValue(hello.text()); //V4
infoArea.setInactiveText(STR_INFODLG); //V4
/*------------------------ Create and Show Dialog ------------------------| V4
| Create a new text dialog with textValue as the string to edit and | V4
| AHelloWindow as the owner window. | V4
| Show the dialog modally. This means that the owner window cannot have | V4
| the focus back until the dialog is ended. | V4
|------------------------------------------------------------------------*/ //V4
ATextDialog textDialog(textValue,this); //V4
textDialog.showModally(); //V4
/*------------------------ Process Dialog Result -------------------------| V4
| If the OK button was used to end the dialog, then the static text, | V4
| hello, is set to the textValue string. Else, it is not changed. | V4
| Reset the information area inactive text. | V4
|------------------------------------------------------------------------*/ //V4
if (textDialog.result() == DID_OK) //V4
hello.setText(textValue); //V4
infoArea.setInactiveText(STR_INFO); //V4
return (*this); //Return a reference to the frame V4
} /* end AHelloWindow :: editText() */ //V4
/************************************************************************** V5
* AHelloWindow :: setTextFromListBox * V5
* Public function used by non-AHelloWindow functions to set the Hello * V5
* World text from the first selected item in the AHelloWindow listBox. * V5
**************************************************************************/ //V5
AHelloWindow & //V5
AHelloWindow :: setTextFromListBox() //V5
{ //V5
/*---------------------- Set Hello Text from ListBox ---------------------| V5
| Create a cursor to the list box. Using the default filter for a | V5
| list box cursor, selectedItems, causes the setToFirst() function | V5
| to position the cursor to the first selected item. | V5
| Set the hello IStaticText control text value. | V5
|------------------------------------------------------------------------*/ //V5
IListBox::Cursor lbCursor(listBox); //V5
lbCursor.setToFirst(); //V5
hello.setText(listBox.elementAt(lbCursor)); //V5
return (*this); //Return a reference to the frame V5
} /* end AHelloWindow :: setTextFromListBox() */ //V5
/************************************************************************** V3
* ACommandHandler :: ACommandHandler - constructor for the command handler* V3
* Construct the command handler from a pointer to the AHelloWindow * V3
* that events will be handled for. * V3
**************************************************************************/ //V3
ACommandHandler :: ACommandHandler(AHelloWindow *helloFrame) //V3
{ //V3
frame=helloFrame; //Save frame to be handled V3
} /* end ACommandHandler :: ACommandHandler(...) */ //V3
/************************************************************************** V3
* ACommandHandler :: command * V3
* Handle menu and button commands * V4
**************************************************************************/ //V3
IBase::Boolean //V3
ACommandHandler :: command(ICommandEvent & cmdEvent) //V3
{ //V3
Boolean eventProcessed(true); //Assume event will be processed V3
/*--------------------- Process command events ---------------------------| V3
| Depending on the command event ID, call the AHelloWindow::setAlignment | V3
| function with the appropriate AHelloWorld::Alignment value. | V3
| Do this except when the Text menu item is selected; | V4
| then call the AHelloWindow::editText function for changing the | V4
| Hello World text using a dialog. | V4
|------------------------------------------------------------------------*/ //V3
switch (cmdEvent.commandId()) { //V3
case MI_CENTER: //V3
frame->setAlignment(AHelloWindow::center); //V3
break; //V3
case MI_LEFT: //V3
frame->setAlignment(AHelloWindow::left); //V3
break; //V3
case MI_RIGHT: //V3
frame->setAlignment(AHelloWindow::right); //V3
break; //V3
case MI_TEXT: //V4
frame->editText(); //V4
break; //V4
default: //Otherwise, V3
eventProcessed=false; // the event wasn't processed V3
} /* end switch */ //V3
return(eventProcessed); //V3
} /* end ACommandHandler :: command(...) */ //V3
/************************************************************************** V5
* ASelectHandler :: ASelectHandler - constructor for the select handler * V5
* Construct the select handler from a pointer to the AHelloWindow * V5
* that will be changed as a result of the selection. * V5
**************************************************************************/ //V5
ASelectHandler :: ASelectHandler(AHelloWindow *helloFrame) //V5
{ //V5
frame=helloFrame; //Save frame to be handled V5
} /* end ASelectHandler :: ASelectHandler(...) */ //V5
/************************************************************************** V5
* ASelectHandler :: selected * V5
* Handle items selected within the list box. * V5
**************************************************************************/ //V5
IBase::Boolean //V5
ASelectHandler :: selected(IControlEvent & evt) //V5
{ //V5
/*---------------------- Call AHelloWindow Function ----------------------| V5
| Call the AHelloWindow::setTextFromListBox function for the frame used | V5
| to construct this select handler. | V5
|------------------------------------------------------------------------*/ //V5
frame->setTextFromListBox(); //V5
return (true); //Event is always processed V5
} /* end ASelectHandler :: selected(...) */ //V5
/************************************************************************** V5
* AHelpHandler :: keysHelpId * V5
* Handle the keys help request event * V5
* This overrides the default action of setting the event result to * V5
* zero, which indicates that no action is required, that is, no * V5
* help information for keys is displayed. * V5
**************************************************************************/ //V5
IBase::Boolean //V5
AHelpHandler :: keysHelpId(IEvent& evt) //V5
{ //V5
evt.setResult(1000); //1000=keys help ID in V5
// ahellow5.ipf file V5
return (true); //Event is always processed V5
} /* end AHelpHandler :: keysHelpId(...) */ //V5