home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
SAMPLES
/
ICLUI
/
LISTBOX
/
ALISTBOX.CPP
next >
Wrap
Text File
|
1993-05-12
|
11KB
|
231 lines
/******************************************************************************/
/* List Box Sample Program */
/* */
/* COPYRIGHT: Copyright (C) International Business Machines Corp., 1992,1993. */
/* */
/* 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 */
/******************************************************************************/
/* List Box Sample Program */
/* key functions: */
/* - create a main window */
/* - run the current application */
/* - use listbox as client area */
/* - use listbox cursors to retrieve items from listbox */
/* - use infoarea control */
/* - have a static text control as a frame extension */
/* - demonstrate the use of tracing macros */
/* - try-catch block around a piece of code */
/* - extract information from an exception object */
/* - register a default exception handler */
/* - create and display a message box */
/* - process 'Command' events generated by menu selection */
/* - load strings from resource bound to the exe */
/******************************************************************************/
#define IC_TRACE_DEVELOP
// Include IBM UI class headers:
#include <iapp.hpp> // IApplication
#include <istattxt.hpp> // IStaticText
#include <ilistbox.hpp> // IListBox
#include <irect.hpp> // IRectangle
#include <imenubar.hpp> // IMenuBar
#include <istring.hpp> // IString
#include <iinfoa.hpp> // IInfoArea
#include <itrace.hpp> // ITrace
#include <imsgbox.hpp> // IMessageBox
#include <iexcbase.hpp> // IException
#include "alistbox.h" // include our Symbolic definitions
#include "alistbox.hpp" // include AListBox Class headers
/******************************************************************************/
/* main - Application entry point */
/******************************************************************************/
void main()
{
AListBox mainWindow (WND_MAIN); // create main window
IApplication::current().run(); // start message loop processing
} /* end main */
/******************************************************************************/
/* AListBox :: AListBox - constructor for main window */
/******************************************************************************/
AListBox :: AListBox(unsigned long windowId)
: IFrameWindow( IFrameWindow::defaultStyle() //Call the IFrame Constructor
| IFrameWindow::accelerator, //with defaultSytles+accelerator
windowId), //and window Id
listbox(WND_HELLO, this, this, IRectangle(),//Create listbox
(IListBox::defaultStyle()
| IListBox::multipleSelect)
& ~IListBox::horizontalScroll),
infoArea(this), //Create information area
statusLine(WND_STATUS, this, this), //Create status line
menuBar(WND_MAIN, this), //Create menu bar
excptHandler(this) //Create exception handler
{
IFUNCTRACE_DEVELOP();
setExceptionFunction(&excptHandler); // register exception handler
setClient(&listbox); // set listbox as client
// add status line as frame extension
statusLine.setAlignment( //Set alignment for status line
IStaticText::centerLeft);
setStatus();
addExtension(&statusLine, //add status line as a frame extension
IFrameWindow::aboveClient, 30UL);
handleEventsFor(this); // set self as command handler
sizeTo(ISize(400,300)); // set the size of main window
update(); // update main window
setFocus(); // set focus to main window
show(); // set to show main window
} /* end AListBox :: AListBox(...) */
/******************************************************************************/
/* AListBox :: setStatus - set the status line text */
/******************************************************************************/
void AListBox :: setStatus()
{
IFUNCTRACE_DEVELOP();
IResourceLibrary reslib=IApplication::current().userResourceLibrary();
IString str=reslib.loadString(STR_INFO);
str += listbox.count(); // get number of lines in listbox
statusLine.setText((char *) str); // and display
}
/******************************************************************************/
/* AListBox :: command - command handler */
/******************************************************************************/
Boolean AListBox :: command(ICommandEvent & cmdEvent)
{
IFUNCTRACE_DEVELOP();
ITRACE_DEVELOP(IString("command id = ") + IString(cmdEvent.commandId()));
Boolean fProcessed = true; // assume event processed
switch (cmdEvent.commandId()) {
case MI_SELECT_ALL:
listbox.selectAll();
break;
case MI_DESELECT_ALL:
listbox.deselectAll();
break;
case MI_ADD_ITEMS:
listbox.addAsLast("some of the classes used in this example");
listbox.addAsLast("IListBox");
listbox.addAsLast("IInfoArea");
listbox.addAsLast("IMessageBox");
listbox.addAsLast("IException");
setStatus();
break;
case MI_READ_SEL_ITEMS:
{
ITRACE_DEVELOP( IString("number of selected items = ") +
IString(listbox.numberOfSelections()) );
IListBox::Cursor lbCursor(listbox); // create listbox cursor
for (lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext())
{
IString str(listbox.elementAt(lbCursor));
unsigned long ul = lbCursor.asIndex();
ITRACE_DEVELOP(IString(ul) + IString(" - ") + str);
/* ... process string or index ... */
}
ITRACE_DEVELOP("end of list of selected items");
break;
}
case MI_READ_ALL_ITEMS:
{
IListBox::Cursor lbCursor(listbox, IListBox::Cursor::allItems);
for (lbCursor.setToFirst(); lbCursor.isValid(); lbCursor.setToNext())
{
IString str(listbox.elementAt(lbCursor));
unsigned long ul = lbCursor.asIndex();
ITRACE_DEVELOP(IString(ul) + IString(" - ") + str);
/* ... process string or index ... */
}
break;
}
case MI_GEN_EXCPT:
{
try //Force an exception
{
IApplication::current().setUserResourceLibrary(IString("NOTFOUND"));
}
catch (IAccessError &exc) // catch access exception
{ // trying to load the dll
ITrace trc("inside catch block");
const char *exText;
unsigned long exId = exc.errorId();
unsigned long cnt = exc.textCount();
ITRACE_DEVELOP( IString("error id = ") + IString(exId) );
listbox.addAsLast( IString(exc.name()) );
listbox.addAsLast( IString("error id = ") + IString(exId) );
listbox.addAsLast( IString("text count = ") + IString(cnt) );
for (unsigned long i = 0; i < cnt; ++i)
{
exText = exc.text(i);
listbox.addAsLast(IString(i) + IString(" - ") + IString(exText));
ITRACE_DEVELOP(IString(i) + IString(exText));
}
IApplication::current().setUserResourceLibrary(0);
setStatus();
}
break;
}
case MI_GEN_EXCPT_UNH:
{
IResourceLibrary reslib=
IApplication::current().userResourceLibrary();
IString str(reslib.loadString(9999));//string resource not found exception
break;
}
default:
fProcessed = false; // event not processed
break;
} /* end switch */
return fProcessed;
} /* end HelloWindow :: command(...) */
/******************************************************************************/
/* AExceptionFn :: handleException - exception handler function */
/******************************************************************************/
Boolean AExceptionFn::handleException (IException& exception, IEvent& event)
{
IFUNCTRACE_DEVELOP();
unsigned long cnt = exception.textCount();
const char *text = (cnt > 0) ? exception.text( cnt-1 )
: "No error text available" ;
IString str( text );
ITRACE_DEVELOP( exception.name() );
ITRACE_DEVELOP( IString("text count = ") + IString(cnt) );
ITRACE_DEVELOP( str );
IMessageBox msgbox( owner );
msgbox.setTitle( IString(exception.name()) + IString(":") +
IString(exception.errorId()) );
msgbox.show( (char *)str ,
IMessageBox::okButton |
IMessageBox::informationIcon |
IMessageBox::applicationModal |
IMessageBox::moveable );
return true; // stop rethrow of exception
}