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 >
Text File  |  1993-10-18  |  15KB  |  382 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.                                         // define trace level
  32. //#define  IC_TRACE_DEVELOP             // uncomment this line and recompile
  33.                                         // to include trace macros
  34.  
  35.                                         // Include IBM UI class headers:
  36. #include <iapp.hpp>
  37. #include <imsgbox.hpp>
  38. #include <ireslib.hpp>
  39. #include <ifiledlg.hpp>
  40. #include <isubmenu.hpp>
  41. #include <ifont.hpp>
  42. #include <ifontdlg.hpp>
  43. #include <itrace.hpp>
  44. #include <ithread.hpp>
  45.  
  46. #include "amle.h"
  47. #include "amle.hpp"
  48.  
  49. /******************************************************************************/
  50. /* main  - Application entry point                                            */
  51. /******************************************************************************/
  52. void main()
  53. {
  54.   AEditorWindow   mainWindow(WND_MAIN);
  55.   IApplication::current().run();
  56. } /* end main */
  57.  
  58. /******************************************************************************/
  59. /* AMultiCellCanvas :: AMultiCellCanvas - Constructor for our main window     */
  60. /******************************************************************************/
  61. AEditorWindow::AEditorWindow(unsigned long windowId)
  62.   : IFrameWindow( windowId )
  63.   , AUserMessageHandler( UWM_THREADEND )
  64.   , mle( WND_MLE, this, this )
  65.   , titleBar( this, IResourceId(WND_MAIN) )
  66.   , menuBar( WND_MAIN, this )
  67.   , infoArea( this )
  68. {
  69.   IFUNCTRACE_DEVELOP();
  70.                                              // make mle the client
  71.   setClient( &mle );
  72.   ((ICommandHandler *)this)->handleEventsFor( this );
  73.   ((AUserMessageHandler *)this)->handleEventsFor( this );
  74.   ((IMenuHandler *)this)->handleEventsFor( this );
  75.  
  76.   menuBar.checkItem(MI_WORDWRAP);
  77.   mle.enableWordWrap();
  78.  
  79.   mle.setFocus();                            // set focus to mle
  80.   show();                                    // show main window
  81.  
  82. } /* end AEditorWindow :: AEditorWindow(...) */
  83.  
  84. /******************************************************************************/
  85. /* AEditorWindow::command - command handler (menus and accelerators)          */
  86. /******************************************************************************/
  87. Boolean AEditorWindow::command(ICommandEvent& cmdEvent)
  88. {
  89.   IFUNCTRACE_DEVELOP();
  90.   Boolean fProcessed = true;
  91.  
  92.    switch( cmdEvent.commandId() )
  93.      {
  94.                 // two open choices are provided to illustrate the difference
  95.                 // between using a thread for a long operation & not using one
  96.      case MI_OPEN:
  97.         openFile(false);
  98.         break;
  99.      case MI_OPEN_THREAD2:
  100.         openFile(true);
  101.         break;
  102.      case MI_SAVE:
  103.         saveFile();
  104.         break;
  105.      case MI_SAVEAS:
  106.         saveAsFile();
  107.         break;
  108.      case MI_FONT:
  109.         openFont();
  110.         break;
  111.      case MI_CUT:
  112.         mle.cut();
  113.         break;
  114.      case MI_COPY:
  115.         mle.copy();
  116.         break;
  117.      case MI_PASTE:
  118.         mle.paste();
  119.         break;
  120.      case MI_WORDWRAP:
  121.         {
  122.         Boolean f = menuBar.isItemChecked(MI_WORDWRAP);
  123.         if (f)
  124.            {
  125.            menuBar.uncheckItem(MI_WORDWRAP);
  126.            mle.disableWordWrap();
  127.            }
  128.         else
  129.            {
  130.            menuBar.checkItem(MI_WORDWRAP);
  131.            mle.enableWordWrap();
  132.            }
  133.         }
  134.         break;
  135.      default:
  136.         fProcessed = false;
  137.         break;
  138.      }
  139.   return fProcessed;
  140. }  /*  end AEditorWindow::command(...)  */
  141.  
  142. /******************************************************************************/
  143. /* AEditorWindow::menuShowing - enable/disable edit menu choices              */
  144. /******************************************************************************/
  145. Boolean AEditorWindow::menuShowing( IMenuEvent& mnEvt
  146.                                   , ISubmenu&   smnAboutToShow)
  147. {
  148.   IFUNCTRACE_DEVELOP();
  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. void AEditorWindow::openFile(Boolean fUseThread)
  177. {
  178.   IFUNCTRACE_DEVELOP();
  179.   IFileDialog::Settings fdSettings;
  180.   fdSettings.setOpenDialog();
  181.   if ( filename.size() )
  182.      fdSettings.setFileName(filename);
  183.   else
  184.      fdSettings.setFileName(DEFAULT_FILE_SPEC);
  185.  
  186.   IFileDialog fileDlg( desktopWindow(), this, fdSettings );
  187.   if ( fileDlg.pressedOK() )
  188.      {
  189.        filename=fileDlg.fileName();
  190.        titleBar.setObjectText( filename );
  191.        titleBar.setViewText( IResourceId(STR_VIEWNAME) );
  192.        if ( filename.size() )
  193.           {
  194.                                         // determine whether to use thread
  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.              {
  210.              load();
  211.              }
  212.           }
  213.      }
  214. }  /* end  AEditorWindow::openFile(...)  */
  215.  
  216. /******************************************************************************/
  217. /* AEditorWindow::saveFile - save file                                        */
  218. /******************************************************************************/
  219. void AEditorWindow::saveFile()
  220. {
  221.   IFUNCTRACE_DEVELOP();
  222.   if ( filename.size() )                // if there is a filename then
  223.      {                                  // save else call saveAs code
  224.      mle.exportToFile(filename);
  225.      }
  226.   else
  227.      {
  228.      saveAsFile();
  229.      }
  230. }  /* end  AEditorWindow::saveFile()  */
  231.  
  232. /******************************************************************************/
  233. /* AEditorWindow::saveAsFile - display 'Save As' file dialog and save mle     */
  234. /******************************************************************************/
  235. void AEditorWindow::saveAsFile()
  236. {
  237.   IFUNCTRACE_DEVELOP();
  238.   IFileDialog::Settings fdSettings;
  239.   fdSettings.setSaveAsDialog();
  240.   fdSettings.setFileName(filename);
  241.  
  242.   IFileDialog fileDlg( desktopWindow(), this, fdSettings );
  243.   if ( fileDlg.pressedOK() )
  244.      {
  245.        filename=fileDlg.fileName();
  246.        if ( filename.size() )
  247.           {
  248.           mle.exportToFile(filename);
  249.           }
  250.      }
  251. }  /* end  AEditorWindow::saveAsFile()  */
  252.  
  253. /******************************************************************************/
  254. /* AEditorWindow::openFont - display font dialog and set font of mle          */
  255. /******************************************************************************/
  256. void AEditorWindow::openFont()
  257. {
  258.   IFUNCTRACE_DEVELOP();
  259.   IFont curFont( &mle );
  260.   IFontDialog::Settings fontSettings(&curFont);
  261.   fontSettings.setTitle(STR_FONTDLGT);
  262.  
  263.   IFontDialog fontDlg( desktopWindow(), this,
  264.                        IFontDialog::defaultStyle() | IFontDialog::bitmapOnly,
  265.                        fontSettings );
  266.  
  267.   if ( fontDlg.pressedOK() )
  268.      {
  269.      mle.setFont(curFont);
  270.      }
  271. } /* end  AEditorWindow::openFont()  */
  272.  
  273. /******************************************************************************/
  274. /* AEditorWindow::displayLoadFailedMsg - display an 'unable to load file' msg */
  275. /*   A message box is only displayed if called from thread 1. This is because */
  276. /*   owner / child windows must be created on the same thread                 */
  277. /*   see the loadOnThread() comment for possible improvements                 */
  278. /******************************************************************************/
  279. void AEditorWindow::displayLoadFailedMsg()
  280. {
  281.   if ( IThread::current().id() == IThreadId(1) )
  282.      {
  283.      IMessageBox msgbox( this );
  284.      IResourceLibrary  reslib =
  285.                             IApplication::current().userResourceLibrary();
  286.      IString  str( reslib.loadString(STR_OPENFAILEDTEXT) );
  287.      str += "\"";
  288.      str += filename;
  289.      str += "\"";
  290.      msgbox.setTitle( IResourceId(STR_OPENFAILED) );
  291.      msgbox.show( str, IMessageBox::okButton         |
  292.                        IMessageBox::informationIcon  |
  293.                        IMessageBox::applicationModal |
  294.                        IMessageBox::moveable         );
  295.      }
  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. 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. void AEditorWindow::load()
  324. {
  325.   IFUNCTRACE_DEVELOP();
  326.   mle.removeAll();
  327.   loadMLE();
  328.   mle.setCursorAtLine( 0 );
  329. }  /* end  AEditorWindow::load()  */
  330.  
  331. /******************************************************************************/
  332. /* AEditorWindow::loadOnThread                                                */
  333. /*   This function is designed to be called from a separate thread.           */
  334. /*   The function posts an event back to thread one to signal completion      */
  335. /*   If an error occurs no message box is displayed. One possible improvement */
  336. /*   is to return a Boolean value back with the postEvent. The user message   */
  337. /*   handler should then be altered to interrogate this value and call        */
  338. /*   the message box code                                                     */
  339. /******************************************************************************/
  340. void AEditorWindow::loadOnThread(unsigned long eventId)
  341. {
  342.   IFUNCTRACE_DEVELOP();
  343.   mle.removeAll();
  344.   loadMLE();
  345.   mle.setCursorAtLine( 0 );
  346.   postEvent( eventId );                   // post message back to frame window
  347. } /* end  AEditorWindow::loadOnThread(...)  */
  348.  
  349. /******************************************************************************/
  350. /* AEditorWindow::setFileMenuitemsState - enable/disable file menu items      */
  351. /******************************************************************************/
  352. Boolean AEditorWindow::setFileMenuitemsState(Boolean f)
  353. {
  354.   IFUNCTRACE_DEVELOP();
  355.   if (f)                                     // if true enable items
  356.      {
  357.      menuBar.enableItem( MI_OPEN );
  358.      menuBar.enableItem( MI_OPEN_THREAD2 );
  359.      menuBar.enableItem( MI_SAVE );
  360.      menuBar.enableItem( MI_SAVEAS );
  361.      }
  362.   else
  363.      {
  364.      menuBar.disableItem( MI_OPEN );
  365.      menuBar.disableItem( MI_OPEN_THREAD2 );
  366.      menuBar.disableItem( MI_SAVE );
  367.      menuBar.disableItem( MI_SAVEAS );
  368.      }
  369.  
  370.   return f;
  371. }  /* end  AEditorWindow::setFileMenuitemsState(...)  */
  372.  
  373. /******************************************************************************/
  374. /* AEditorWindow::userMessage - handle user message events                    */
  375. /******************************************************************************/
  376. Boolean AEditorWindow::userMessage( IEvent& evt )
  377. {
  378.   IFUNCTRACE_DEVELOP();
  379.   setFileMenuitemsState(true);               // enable file menu items
  380.   return true;
  381. }  /* end  AEditorWindow::userMessage(...)  */
  382.