home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / ioc / mle / amle.cpp next >
Encoding:
C/C++ Source or Header  |  1996-02-22  |  14.7 KB  |  382 lines

  1. /******************************************************************************
  2. * .FILE:         amle.cpp                                                     *
  3. *                                                                             *
  4. * .DESCRIPTION:  Multiline Edit Sample Program:        Class Implementation   *
  5. *                                                                             *
  6. * .CLASSES:      AEditorWindow                                                *
  7. *                                                                             *
  8. * .COPYRIGHT:                                                                 *
  9. *    Licensed Material - Program-Property of IBM                              *
  10. *    (C) Copyright IBM Corp. 1992, 1996 - All Rights Reserved                 *
  11. *                                                                             *
  12. * .DISCLAIMER:                                                                *
  13. *   The following [enclosed] code is sample code created by IBM               *
  14. *   Corporation.  This sample code is not part of any standard IBM product    *
  15. *   and is provided to you solely for the purpose of assisting you in the     *
  16. *   development of your applications.  The code is provided 'AS IS',          *
  17. *   without warranty of any kind.  IBM shall not be liable for any damages    *
  18. *   arising out of your use of the sample code, even if they have been        *
  19. *   advised of the possibility of such damages.                               *
  20. *                                                                             *
  21. * .NOTE: WE RECOMMEND USING A FIXED SPACE FONT TO LOOK AT THE SOURCE          *
  22. *                                                                             *
  23. ******************************************************************************/
  24. #ifndef _IBASE_                         //Make sure ibase.hpp is included
  25.   #include <ibase.hpp>                  //  since that is where IC_<environ>
  26. #endif                                  //  is defined.
  27. #include <iapp.hpp>
  28. #include <imsgbox.hpp>
  29. #include <ireslib.hpp>
  30. #include <ifiledlg.hpp>
  31. #include <isubmenu.hpp>
  32. #include <ifont.hpp>
  33. #include <ifontdlg.hpp>
  34. #include <itrace.hpp>
  35. #include <icoordsy.hpp>
  36. #ifndef IC_MOTIF
  37. #include <ithread.hpp>
  38. #endif
  39. #include "amle.h"
  40. #include "amle.hpp"
  41.  
  42. /******************************************************************************/
  43. /* main  - Application entry point                                            */
  44. /******************************************************************************/
  45. int main()
  46. {
  47.   ICoordinateSystem::setApplicationOrientation(
  48.           ICoordinateSystem::originLowerLeft );
  49.   AEditorWindow mainWindow(WND_MAIN);
  50.   IApplication::current().run();
  51.  
  52.   return 0;
  53. } /* end main */
  54.  
  55. /******************************************************************************/
  56. /* AMultiCellCanvas :: AMultiCellCanvas - Constructor for our main window     */
  57. /******************************************************************************/
  58. AEditorWindow::AEditorWindow(unsigned long windowId)
  59.   : IFrameWindow( windowId )
  60.   , AUserMessageHandler( UWM_THREADEND )
  61.   , mle( WND_MLE, this, this )
  62.   , titleBar( this, IResourceId(WND_MAIN) )
  63.   , menuBar( WND_MAIN, this )
  64.   , infoArea( this )
  65.   , primaryThreadId( IThread::current().id() )
  66. {
  67.   setIcon( id() );                           // Set icon
  68.   setClient( &mle );                         // make mle the client
  69.   ((ICommandHandler *)this)->handleEventsFor( this );
  70.   ((AUserMessageHandler *)this)->handleEventsFor( this );
  71.   ((IMenuHandler *)this)->handleEventsFor( this );
  72.  
  73.   menuBar.checkItem(MI_WORDWRAP);
  74.   mle.enableWordWrap();
  75.  
  76.   mle.setFocus();                            // set focus to mle
  77.   show();                                    // show main window
  78.  
  79. } /* end AEditorWindow :: AEditorWindow(...) */
  80.  
  81. /******************************************************************************/
  82. /* AEditorWindow::command - command handler (menus and accelerators)          */
  83. /******************************************************************************/
  84. IBase::Boolean AEditorWindow::command(ICommandEvent& cmdEvent)
  85. {
  86.   Boolean fProcessed = true;
  87.  
  88.    switch( cmdEvent.commandId() )
  89.      {
  90.                 // two open choices are provided to illustrate the difference
  91.                 // between using a thread for a long operation & not using one
  92.      case MI_OPEN:
  93.         openFile(false);
  94.         break;
  95.      case MI_OPEN_THREAD2:
  96.         openFile(true);
  97.         break;
  98.      case MI_SAVE:
  99.         saveFile();
  100.         break;
  101.      case MI_SAVEAS:
  102.         saveAsFile();
  103.         break;
  104.      case MI_FONT:
  105.         openFont();
  106.         break;
  107.      case MI_CUT:
  108.         mle.cut();
  109.         break;
  110.      case MI_COPY:
  111.         mle.copy();
  112.         break;
  113.      case MI_PASTE:
  114.         mle.paste();
  115.         break;
  116.      case MI_WORDWRAP:
  117.         {
  118.         Boolean f = menuBar.isItemChecked(MI_WORDWRAP);
  119.         if (f)
  120.            {
  121.            menuBar.uncheckItem(MI_WORDWRAP);
  122.            mle.disableWordWrap();
  123.            }
  124.         else
  125.            {
  126.            menuBar.checkItem(MI_WORDWRAP);
  127.            mle.enableWordWrap();
  128.            }
  129.         }
  130.         break;
  131.      default:
  132.         fProcessed = false;
  133.         break;
  134.      }
  135.   return fProcessed;
  136. }  /*  end AEditorWindow::command(...)  */
  137.  
  138. /******************************************************************************/
  139. /* AEditorWindow::menuShowing - enable/disable edit menu choices              */
  140. /******************************************************************************/
  141. IBase::Boolean AEditorWindow::menuShowing( IMenuEvent& mnEvt
  142.                                   , ISubmenu&   smnAboutToShow)
  143. {
  144.   switch ( smnAboutToShow.id() )
  145.      {
  146.      case MI_EDIT:
  147.        if ( mle.hasSelectedText() )
  148.           {
  149.           menuBar.enableItem(MI_COPY);
  150.           menuBar.enableItem(MI_CUT);
  151.           }
  152.        else
  153.           {
  154.           menuBar.disableItem(MI_COPY);
  155.           menuBar.disableItem(MI_CUT);
  156.           }
  157.        if ( mle.clipboardHasTextFormat() )
  158.           menuBar.enableItem(MI_PASTE);
  159.        else
  160.           menuBar.disableItem(MI_PASTE);
  161.      break;
  162.      default:
  163.      break;
  164.      }
  165.   return false;
  166. }  /* end  AEditorWindow::menuShowing(...)  */
  167.  
  168. /******************************************************************************/
  169. /* AEditorWindow::openFile - display open file dialog and load file into mle  */
  170. /******************************************************************************/
  171. AEditorWindow& AEditorWindow::openFile(Boolean fUseThread)
  172. {
  173.   IFileDialog::Settings fdSettings;
  174.   fdSettings.setOpenDialog();
  175.   if ( filename.size() )
  176.      fdSettings.setFileName(filename);
  177.   else
  178.      fdSettings.setFileName(DEFAULT_FILE_SPEC);
  179.  
  180.   IFileDialog fileDlg( desktopWindow(), this, fdSettings );
  181.   if ( fileDlg.pressedOK() )
  182.      {
  183.        filename=fileDlg.fileName();
  184.        titleBar.setObjectText( filename );
  185.        titleBar.setViewText( IResourceId(STR_VIEWNAME) );
  186.        if ( filename.size() )
  187.           {
  188.                                         // determine whether to use thread
  189. #ifndef IC_MOTIF
  190.           if (fUseThread)
  191.              {
  192.                                         // disable file menu items to avoid them
  193.                                         // being reselected while thead running
  194.              setFileMenuitemsState( false );
  195.                                         // create our thread function and
  196.                                         // specify the message id we want
  197.                                         // posted back
  198.              AThreadFn *atmFn = new AThreadFn( *this, UWM_THREADEND );
  199.                                         // dispatch thread to run function
  200.                                         // thread will have a PM environment
  201.              IThread thread( atmFn, IThread::defaultAutoInitGUI() );
  202.              }
  203.           else
  204. #endif
  205.              {
  206.              load();
  207.              }
  208.           }
  209.      }
  210.   return *this;
  211. }  /* end  AEditorWindow::openFile(...)  */
  212.  
  213. /******************************************************************************/
  214. /* AEditorWindow::saveFile - save file                                        */
  215. /******************************************************************************/
  216. AEditorWindow& AEditorWindow::saveFile()
  217. {
  218.   if ( filename.size() )                // if there is a filename then
  219.      {                                  // save else call saveAs code
  220.      mle.exportToFile(filename);
  221.      }
  222.   else
  223.      {
  224.      saveAsFile();
  225.      }
  226.   return *this;
  227. }  /* end  AEditorWindow::saveFile()  */
  228.  
  229. /******************************************************************************/
  230. /* AEditorWindow::saveAsFile - display 'Save As' file dialog and save mle     */
  231. /******************************************************************************/
  232. AEditorWindow& AEditorWindow::saveAsFile()
  233. {
  234.   IFileDialog::Settings fdSettings;
  235.   fdSettings.setSaveAsDialog();
  236.   fdSettings.setFileName(filename);
  237.  
  238.   IFileDialog fileDlg( desktopWindow(), this, fdSettings );
  239.   if ( fileDlg.pressedOK() )
  240.      {
  241.        filename=fileDlg.fileName();
  242.        if ( filename.size() )
  243.           {
  244.           mle.exportToFile(filename);
  245.           titleBar.setObjectText( filename );
  246.           titleBar.setViewText( IResourceId(STR_VIEWNAME) );
  247.           }
  248.      }
  249.   return *this;
  250. }  /* end  AEditorWindow::saveAsFile()  */
  251.  
  252. /******************************************************************************/
  253. /* AEditorWindow::openFont - display font dialog and set font of mle          */
  254. /******************************************************************************/
  255. AEditorWindow& AEditorWindow::openFont()
  256. {
  257.   IFont curFont( &mle );
  258.   IFontDialog::Settings fontSettings(&curFont);
  259.   fontSettings.setTitle(STR_FONTDLGT);
  260.  
  261.   IFontDialog fontDlg( desktopWindow(), this,
  262.                        IFontDialog::defaultStyle() | IFontDialog::bitmapOnly,
  263.                        fontSettings );
  264.  
  265.   if ( fontDlg.pressedOK() )
  266.      {
  267.      mle.setFont(curFont);
  268.      }
  269.   return *this;
  270. } /* end  AEditorWindow::openFont()  */
  271.  
  272. /******************************************************************************/
  273. /* AEditorWindow::displayLoadFailedMsg - display an 'unable to load file' msg */
  274. /*   A message box is only displayed if called from thread 1. This is because */
  275. /*   owner / child windows must be created on the same thread                 */
  276. /*   see the loadOnThread() comment for possible improvements                 */
  277. /******************************************************************************/
  278. AEditorWindow& AEditorWindow::displayLoadFailedMsg()
  279. {
  280.   if ( IThread::current().id() == primaryThreadId )
  281.      {
  282.      IMessageBox msgbox( this );
  283.      IResourceLibrary  reslib =
  284.                             IApplication::current().userResourceLibrary();
  285.      IString  str( reslib.loadString(STR_OPENFAILEDTEXT) );
  286.      str += reslib.loadString(STR_QUOTE);
  287.      str += filename;
  288.      str += reslib.loadString(STR_QUOTE);
  289.      msgbox.setTitle( IResourceId(STR_OPENFAILED) );
  290.      msgbox.show( str, IMessageBox::okButton         |
  291.                        IMessageBox::informationIcon  |
  292.                        IMessageBox::applicationModal |
  293.                        IMessageBox::moveable         );
  294.      }
  295.   return *this;
  296. }  /* end  AEditorWindow::displayLoadFailedMsg()  */
  297.  
  298. /******************************************************************************/
  299. /* AEditorWindow::loadMLE - load mle and catch exception                      */
  300. /*   If the import fails due to an invalid file name, an IAccessError is      */
  301. /*   thrown. This is caught and an error message is displayed                 */
  302. /******************************************************************************/
  303. IBase::Boolean AEditorWindow::loadMLE()
  304. {
  305.   Boolean loaded = true;
  306.  
  307.   try
  308.      {
  309.      mle.importFromFile( filename );
  310.      }
  311.   catch ( IAccessError &exc )
  312.      {
  313.      displayLoadFailedMsg();
  314.      loaded = false;
  315.      }
  316.  
  317.   return loaded;
  318. }  /* end  AEditorWindow::loadMLE()  */
  319.  
  320. /******************************************************************************/
  321. /* AEditorWindow::load - load file into mle                                   */
  322. /******************************************************************************/
  323. AEditorWindow& AEditorWindow::load()
  324. {
  325.   mle.removeAll();
  326.   loadMLE();
  327.   mle.setCursorLinePosition( 0 );
  328.   return *this;
  329. }  /* end  AEditorWindow::load()  */
  330.  
  331. #ifndef IC_MOTIF
  332. /******************************************************************************/
  333. /* AEditorWindow::loadOnThread                                                */
  334. /*   This function is designed to be called from a separate thread.           */
  335. /*   The function posts an event back to thread one to signal completion      */
  336. /*   If an error occurs no message box is displayed. One possible improvement */
  337. /*   is to return a Boolean value back with the postEvent. The user message   */
  338. /*   handler should then be altered to interrogate this value and call        */
  339. /*   the message box code                                                     */
  340. /******************************************************************************/
  341. AEditorWindow& AEditorWindow::loadOnThread(unsigned long eventId)
  342. {
  343.   mle.removeAll();
  344.   loadMLE();
  345.   mle.setCursorLinePosition( 0 );
  346.   postEvent( eventId );                   // post message back to frame window
  347.   return *this;
  348. } /* end  AEditorWindow::loadOnThread(...)  */
  349. #endif
  350.  
  351. /******************************************************************************/
  352. /* AEditorWindow::setFileMenuitemsState - enable/disable file menu items      */
  353. /******************************************************************************/
  354. IBase::Boolean AEditorWindow::setFileMenuitemsState(Boolean f)
  355. {
  356.   if (f)                                     // if true enable items
  357.      {
  358.      menuBar.enableItem( MI_OPEN );
  359.      menuBar.enableItem( MI_OPEN_THREAD2 );
  360.      menuBar.enableItem( MI_SAVE );
  361.      menuBar.enableItem( MI_SAVEAS );
  362.      }
  363.   else
  364.      {
  365.      menuBar.disableItem( MI_OPEN );
  366.      menuBar.disableItem( MI_OPEN_THREAD2 );
  367.      menuBar.disableItem( MI_SAVE );
  368.      menuBar.disableItem( MI_SAVEAS );
  369.      }
  370.  
  371.   return f;
  372. }  /* end  AEditorWindow::setFileMenuitemsState(...)  */
  373.  
  374. /******************************************************************************/
  375. /* AEditorWindow::userMessage - handle user message events                    */
  376. /******************************************************************************/
  377. IBase::Boolean AEditorWindow::userMessage( IEvent& evt )
  378. {
  379.   setFileMenuitemsState(true);               // enable file menu items
  380.   return true;
  381. }  /* end  AEditorWindow::userMessage(...)  */
  382.