home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / IOC / MLE / AMLE.CPP next >
Text File  |  1995-05-01  |  15KB  |  385 lines

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