home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
OS/2 Shareware BBS: 10 Tools
/
10-Tools.zip
/
cset21v1.zip
/
IBMCPP
/
SAMPLES
/
ICLUI
/
MLE
/
AMLE.CPP
< prev
next >
Wrap
Text File
|
1993-10-18
|
15KB
|
382 lines
/******************************************************************************/
/* Multi-Line Entry Field 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 */
/******************************************************************************/
/* Multi-Line Entry Field Sample Program */
/* key functions: */
/* - create a main window */
/* - run the current application */
/* - create a multi-line entry field and use as client area */
/* - use file dialog for file selection */
/* - use font dialog for font selection */
/* - start a member function with parameters on a secondary thread */
/* - post user message to message queue */
/* - process 'Command' events generated by the menu item selection */
/* - process user message events posted from thread two */
/* - enable/disable menu choices when menu is displayed */
/* - change title bar text */
/******************************************************************************/
// define trace level
//#define IC_TRACE_DEVELOP // uncomment this line and recompile
// to include trace macros
// Include IBM UI class headers:
#include <iapp.hpp>
#include <imsgbox.hpp>
#include <ireslib.hpp>
#include <ifiledlg.hpp>
#include <isubmenu.hpp>
#include <ifont.hpp>
#include <ifontdlg.hpp>
#include <itrace.hpp>
#include <ithread.hpp>
#include "amle.h"
#include "amle.hpp"
/******************************************************************************/
/* main - Application entry point */
/******************************************************************************/
void main()
{
AEditorWindow mainWindow(WND_MAIN);
IApplication::current().run();
} /* end main */
/******************************************************************************/
/* AMultiCellCanvas :: AMultiCellCanvas - Constructor for our main window */
/******************************************************************************/
AEditorWindow::AEditorWindow(unsigned long windowId)
: IFrameWindow( windowId )
, AUserMessageHandler( UWM_THREADEND )
, mle( WND_MLE, this, this )
, titleBar( this, IResourceId(WND_MAIN) )
, menuBar( WND_MAIN, this )
, infoArea( this )
{
IFUNCTRACE_DEVELOP();
// make mle the client
setClient( &mle );
((ICommandHandler *)this)->handleEventsFor( this );
((AUserMessageHandler *)this)->handleEventsFor( this );
((IMenuHandler *)this)->handleEventsFor( this );
menuBar.checkItem(MI_WORDWRAP);
mle.enableWordWrap();
mle.setFocus(); // set focus to mle
show(); // show main window
} /* end AEditorWindow :: AEditorWindow(...) */
/******************************************************************************/
/* AEditorWindow::command - command handler (menus and accelerators) */
/******************************************************************************/
Boolean AEditorWindow::command(ICommandEvent& cmdEvent)
{
IFUNCTRACE_DEVELOP();
Boolean fProcessed = true;
switch( cmdEvent.commandId() )
{
// two open choices are provided to illustrate the difference
// between using a thread for a long operation & not using one
case MI_OPEN:
openFile(false);
break;
case MI_OPEN_THREAD2:
openFile(true);
break;
case MI_SAVE:
saveFile();
break;
case MI_SAVEAS:
saveAsFile();
break;
case MI_FONT:
openFont();
break;
case MI_CUT:
mle.cut();
break;
case MI_COPY:
mle.copy();
break;
case MI_PASTE:
mle.paste();
break;
case MI_WORDWRAP:
{
Boolean f = menuBar.isItemChecked(MI_WORDWRAP);
if (f)
{
menuBar.uncheckItem(MI_WORDWRAP);
mle.disableWordWrap();
}
else
{
menuBar.checkItem(MI_WORDWRAP);
mle.enableWordWrap();
}
}
break;
default:
fProcessed = false;
break;
}
return fProcessed;
} /* end AEditorWindow::command(...) */
/******************************************************************************/
/* AEditorWindow::menuShowing - enable/disable edit menu choices */
/******************************************************************************/
Boolean AEditorWindow::menuShowing( IMenuEvent& mnEvt
, ISubmenu& smnAboutToShow)
{
IFUNCTRACE_DEVELOP();
switch ( smnAboutToShow.id() )
{
case MI_EDIT:
if ( mle.hasSelectedText() )
{
menuBar.enableItem(MI_COPY);
menuBar.enableItem(MI_CUT);
}
else
{
menuBar.disableItem(MI_COPY);
menuBar.disableItem(MI_CUT);
}
if ( mle.clipboardHasTextFormat() )
menuBar.enableItem(MI_PASTE);
else
menuBar.disableItem(MI_PASTE);
break;
default:
break;
}
return false;
} /* end AEditorWindow::menuShowing(...) */
/******************************************************************************/
/* AEditorWindow::openFile - display open file dialog and load file into mle */
/******************************************************************************/
void AEditorWindow::openFile(Boolean fUseThread)
{
IFUNCTRACE_DEVELOP();
IFileDialog::Settings fdSettings;
fdSettings.setOpenDialog();
if ( filename.size() )
fdSettings.setFileName(filename);
else
fdSettings.setFileName(DEFAULT_FILE_SPEC);
IFileDialog fileDlg( desktopWindow(), this, fdSettings );
if ( fileDlg.pressedOK() )
{
filename=fileDlg.fileName();
titleBar.setObjectText( filename );
titleBar.setViewText( IResourceId(STR_VIEWNAME) );
if ( filename.size() )
{
// determine whether to use thread
if (fUseThread)
{
// disable file menu items to avoid them
// being reselected while thead running
setFileMenuitemsState( false );
// create our thread function and
// specify the message id we want
// posted back
AThreadFn *atmFn = new AThreadFn( *this, UWM_THREADEND );
// dispatch thread to run function
// thread will have a PM environment
IThread thread( atmFn );
}
else
{
load();
}
}
}
} /* end AEditorWindow::openFile(...) */
/******************************************************************************/
/* AEditorWindow::saveFile - save file */
/******************************************************************************/
void AEditorWindow::saveFile()
{
IFUNCTRACE_DEVELOP();
if ( filename.size() ) // if there is a filename then
{ // save else call saveAs code
mle.exportToFile(filename);
}
else
{
saveAsFile();
}
} /* end AEditorWindow::saveFile() */
/******************************************************************************/
/* AEditorWindow::saveAsFile - display 'Save As' file dialog and save mle */
/******************************************************************************/
void AEditorWindow::saveAsFile()
{
IFUNCTRACE_DEVELOP();
IFileDialog::Settings fdSettings;
fdSettings.setSaveAsDialog();
fdSettings.setFileName(filename);
IFileDialog fileDlg( desktopWindow(), this, fdSettings );
if ( fileDlg.pressedOK() )
{
filename=fileDlg.fileName();
if ( filename.size() )
{
mle.exportToFile(filename);
}
}
} /* end AEditorWindow::saveAsFile() */
/******************************************************************************/
/* AEditorWindow::openFont - display font dialog and set font of mle */
/******************************************************************************/
void AEditorWindow::openFont()
{
IFUNCTRACE_DEVELOP();
IFont curFont( &mle );
IFontDialog::Settings fontSettings(&curFont);
fontSettings.setTitle(STR_FONTDLGT);
IFontDialog fontDlg( desktopWindow(), this,
IFontDialog::defaultStyle() | IFontDialog::bitmapOnly,
fontSettings );
if ( fontDlg.pressedOK() )
{
mle.setFont(curFont);
}
} /* end AEditorWindow::openFont() */
/******************************************************************************/
/* AEditorWindow::displayLoadFailedMsg - display an 'unable to load file' msg */
/* A message box is only displayed if called from thread 1. This is because */
/* owner / child windows must be created on the same thread */
/* see the loadOnThread() comment for possible improvements */
/******************************************************************************/
void AEditorWindow::displayLoadFailedMsg()
{
if ( IThread::current().id() == IThreadId(1) )
{
IMessageBox msgbox( this );
IResourceLibrary reslib =
IApplication::current().userResourceLibrary();
IString str( reslib.loadString(STR_OPENFAILEDTEXT) );
str += "\"";
str += filename;
str += "\"";
msgbox.setTitle( IResourceId(STR_OPENFAILED) );
msgbox.show( str, IMessageBox::okButton |
IMessageBox::informationIcon |
IMessageBox::applicationModal |
IMessageBox::moveable );
}
} /* end AEditorWindow::displayLoadFailedMsg() */
/******************************************************************************/
/* AEditorWindow::loadMLE - load mle and catch exception */
/* If the import fails due to an invalid file name, an IAccessError is */
/* thrown. This is caught and an error message is displayed */
/******************************************************************************/
Boolean AEditorWindow::loadMLE()
{
Boolean loaded = true;
try
{
mle.importFromFile( filename );
}
catch ( IAccessError &exc )
{
displayLoadFailedMsg();
loaded = false;
}
return loaded;
} /* end AEditorWindow::loadMLE() */
/******************************************************************************/
/* AEditorWindow::load - load file into mle */
/******************************************************************************/
void AEditorWindow::load()
{
IFUNCTRACE_DEVELOP();
mle.removeAll();
loadMLE();
mle.setCursorAtLine( 0 );
} /* end AEditorWindow::load() */
/******************************************************************************/
/* AEditorWindow::loadOnThread */
/* This function is designed to be called from a separate thread. */
/* The function posts an event back to thread one to signal completion */
/* If an error occurs no message box is displayed. One possible improvement */
/* is to return a Boolean value back with the postEvent. The user message */
/* handler should then be altered to interrogate this value and call */
/* the message box code */
/******************************************************************************/
void AEditorWindow::loadOnThread(unsigned long eventId)
{
IFUNCTRACE_DEVELOP();
mle.removeAll();
loadMLE();
mle.setCursorAtLine( 0 );
postEvent( eventId ); // post message back to frame window
} /* end AEditorWindow::loadOnThread(...) */
/******************************************************************************/
/* AEditorWindow::setFileMenuitemsState - enable/disable file menu items */
/******************************************************************************/
Boolean AEditorWindow::setFileMenuitemsState(Boolean f)
{
IFUNCTRACE_DEVELOP();
if (f) // if true enable items
{
menuBar.enableItem( MI_OPEN );
menuBar.enableItem( MI_OPEN_THREAD2 );
menuBar.enableItem( MI_SAVE );
menuBar.enableItem( MI_SAVEAS );
}
else
{
menuBar.disableItem( MI_OPEN );
menuBar.disableItem( MI_OPEN_THREAD2 );
menuBar.disableItem( MI_SAVE );
menuBar.disableItem( MI_SAVEAS );
}
return f;
} /* end AEditorWindow::setFileMenuitemsState(...) */
/******************************************************************************/
/* AEditorWindow::userMessage - handle user message events */
/******************************************************************************/
Boolean AEditorWindow::userMessage( IEvent& evt )
{
IFUNCTRACE_DEVELOP();
setFileMenuitemsState(true); // enable file menu items
return true;
} /* end AEditorWindow::userMessage(...) */