home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / smarts / ioc / iclui.cpp next >
Encoding:
Text File  |  1996-02-21  |  13.4 KB  |  400 lines

  1. --------------------------------------------------------------------------------
  2. --
  3. -- COPYRIGHT:
  4. --   IBM WorkFrame - Project Smarts
  5. --   (C) Copyright International Business Machines Corporation 1996
  6. --   Licensed Material - Program-Property of IBM - All Rights Reserved.
  7. --   US Government Users Restricted Rights - Use, duplication, or disclosure
  8. --   restricted by GSA ADP Schedule Contract with IBM Corp.
  9. --
  10. --------------------------------------------------------------------------------
  11.  
  12. <include prologcp.tde>
  13.  
  14. /* UI Class Library headers */
  15. #include <ibase.hpp>
  16. #include <iaccel.hpp>
  17. #include <iapp.hpp>
  18. #include <iinfoa.hpp>
  19. #include <ifont.hpp>
  20. #include <ithread.hpp>
  21. #include <ifiledlg.hpp>
  22. #include <imsgbox.hpp>
  23. #include <ihelp.hpp>
  24. #include <iexcept.hpp>
  25.  
  26. /* Application headers */
  27. #include "prodinfo.hpp"
  28. #include "$FILE_NAME$.h"
  29. #include "$FILE_NAME$.hpp"
  30.  
  31.  
  32. /**************************************************************/
  33. /* main() program entry point                                 */
  34. /**************************************************************/
  35. void main()
  36. {
  37. <if ($USE_IPF$)>
  38.    IHelpWindow::setDefaultStyle( IHelpWindow::defaultStyle()
  39.                                  | IHelpWindow::ipfCompatible );
  40. </if>
  41.    AppWindow appWindow(IDW_FRAME_WINDOW);
  42.    IApplication::current().run();
  43. }
  44.  
  45.  
  46. /**************************************************************/
  47. /* Application main window constructor.  Sets events handler, */
  48. /* icon, window style and menu bar.  It also sets static text */
  49. /* as the client area control.  You may want to set           */
  50. /**************************************************************/
  51. AppWindow :: AppWindow(unsigned long windowId)
  52.   :IFrameWindow( IFrameWindow::defaultStyle() |   // Create frame window in default style
  53.                  IFrameWindow::accelerator,
  54.                  windowId)
  55.   ,title( this, IDW_FRAME_WINDOW, IDS_NO_FILE ) // Create title bar
  56.   ,menuBar( IResourceId( windowId ), this )     // Create menu bar
  57.   ,mle(ID_MLE,this,this)                        // Create MLE for client area
  58.   ,openFileName("")                             // No file open yet
  59.   ,hasHelp(true)                                // Assume help is available for now
  60.   ,infoarea( this )
  61.   ,cmdHdr( this )
  62.   ,hasFile( false )
  63. {
  64.    // Set up status line
  65.    infoarea.setInactiveText( IDS_GENERAL_DESC);     // Set status line text
  66.  
  67.    // Set up menu bar
  68.    menuBar.setMenu( IResourceId(IDW_FRAME_WINDOW));
  69.  
  70.    // Set up client area as MLE
  71.    setClient(&mle);       // Set MLE control for client area
  72.  
  73.    // Set self as command event handler
  74.    cmdHdr.handleEventsFor( this );
  75.  
  76.    // Set application icon
  77.    setIcon(IDI_ICON);
  78.  
  79.    try
  80.    {
  81.      // Set up help, help settings
  82.      IHelpWindow::Settings helpSettings;
  83.      helpSettings.setMenuBar(IDM_HELP);
  84.      helpSettings.setTitle(IDS_HELP_TITLE);
  85.      helpSettings.setHelpTable(IDH_MAIN_HELPTABLE);
  86.      helpSettings.setLibraries(HELP_FILE_NAME);
  87.      help = new IHelpWindow(helpSettings,this);
  88.    }
  89.    catch( IAccessError &exc )
  90.    {
  91.       // Inform the user that help is not available
  92.       IMessageBox errorMsg( this );
  93.       errorMsg.setTitle( IResourceId(IDW_FRAME_WINDOW));
  94.       errorMsg.show( IResourceId(IDS_ERROR_NO_HELP),
  95.                      IMessageBox::catastrophic);
  96.  
  97.       // Set internal help flag to indicate that no help is available
  98.       hasHelp = false;
  99.    }
  100.  
  101.    // If there is no text in the clipboard, disable the Paste menuitem.
  102.    if ( ! mle.clipboardHasTextFormat() )
  103.       enablePaste( false );
  104.  
  105.    // Set focus to this window and show
  106.    setFocus().show();
  107. }
  108.  
  109.  
  110. /***********************************************************/
  111. /* Application main window destructor.  It stops the event */
  112. /* handling and deletes any objects on the heap.           */
  113. /***********************************************************/
  114. AppWindow :: ~AppWindow ()
  115. {
  116.    cmdHdr.stopHandlingEventsFor( this );
  117.    if ( isHelpAvailable() )
  118.    {
  119.       delete help;
  120.    }
  121. }
  122.  
  123.  
  124. /*********************************************************************/
  125. /* This productInfo function displays the product information dialog */
  126. /*********************************************************************/
  127. AppWindow& AppWindow :: productInfo()
  128. {
  129.    unsigned short rc;
  130.  
  131.    ProdInfoDialog prodInfo( this );
  132.    prodInfo.showModally();
  133.    rc = prodInfo.result();       //Get result (eg OK or Cancel)         .
  134.    return *this;
  135. }
  136.  
  137.  
  138. /***********************************************************/
  139. /* This function returns TRUE if there is currently no     */
  140. /* help available to the application.                      */
  141. /***********************************************************/
  142. Boolean AppWindow :: isHelpAvailable() const
  143. {
  144.    return (hasHelp);
  145. }
  146.  
  147. /***********************************************************/
  148. /* Sets the information area text given a string           */
  149. /***********************************************************/
  150. AppWindow& AppWindow::setStatus( const char * string )
  151. {
  152.   infoarea.setText( string );
  153.   return *this;
  154. }
  155.  
  156. /***********************************************************/
  157. /* Sets the information area text given a string resource  */
  158. /***********************************************************/
  159. AppWindow& AppWindow::setStatus( IResourceId id )
  160. {
  161.   infoarea.setText( id );
  162.   return *this;
  163. }
  164.  
  165. /***********************************************************/
  166. /* Returns the editor                                      */
  167. /***********************************************************/
  168. IMultiLineEdit* AppWindow::editor()
  169. {
  170.   return &mle;
  171. }
  172.  
  173. /***********************************************************/
  174. /* Returns true if a file has already been selected        */
  175. /***********************************************************/
  176. Boolean AppWindow::hasFileName()
  177. {
  178.   return ( hasFile );
  179. }
  180.  
  181. /***********************************************************/
  182. /* Returns the current file name                           */
  183. /***********************************************************/
  184. IString AppWindow::fileName()
  185. {
  186.   return ( openFileName );
  187. }
  188.  
  189. /***********************************************************/
  190. /* Sets the current file name                              */
  191. /***********************************************************/
  192. AppWindow& AppWindow::setFileName( const char * file )
  193. {
  194.   hasFile = true;
  195.   openFileName = file;
  196.   title.setViewText( file );
  197.   return *this;
  198. }
  199.  
  200. /***********************************************************/
  201. /* Returns the help window                                 */
  202. /***********************************************************/
  203. IHelpWindow* AppWindow::helpWin()
  204. {
  205.   return help;
  206. }
  207.  
  208. /***********************************************************/
  209. /* Enables (or disables) the paste menu item               */
  210. /***********************************************************/
  211. AppWindow& AppWindow::enablePaste( Boolean which )
  212. {
  213.   menuBar.enableItem( IDM_EDIT_PASTE, which );
  214.   return *this;
  215. }
  216.  
  217. /***********************************************************/
  218. /* AppCommandHandler :: AppCommandHandler - constructor    */
  219. /***********************************************************/
  220. AppCommandHandler::AppCommandHandler( AppWindow* appWindow )
  221.     :ICommandHandler()
  222.     ,app( appWindow )
  223. {
  224. }
  225.  
  226. /***********************************************************/
  227. /* AppCommandHandler :: command - handle comment events    */
  228. /***********************************************************/
  229. IBase::Boolean AppCommandHandler::command( ICommandEvent& event )
  230.  {
  231.    IResourceLibrary  resLib;
  232.  
  233.    switch ( event.commandId() )
  234.    {
  235.       case IDM_FILE_OPEN:
  236.        {
  237.         /*-------------------------------------------------------------*/
  238.         /* When the user selects the File->Open menu item to open a    */
  239.         /* file, display the standard "File open" dialog using         */
  240.         /* IFileDialog.  The dialog is set to its most previous        */
  241.         /* values.  Then, a thread is started to open the file and     */
  242.         /* and perform any other application-specific file processing. */
  243.         /*-------------------------------------------------------------*/
  244.  
  245.          IFileDialog::Settings fdSettings;  // Create file dialog settings object
  246.          fdSettings.setOpenDialog();
  247.          fdSettings.setOKButtonText(IResourceId(IDS_OPEN));
  248.          fdSettings.setTitle(IResourceId(IDS_FILE_OPEN));
  249.          fdSettings.setFileName("*.*");
  250.  
  251.          // Create and show File open dialog
  252.          IFileDialog fileDlg( IWindow::desktopWindow(), // Parent is desktop
  253.                               app,                      // Owner is main window
  254.                               IFileDialog::helpButton, // Help button style
  255.                               fdSettings);              // Here are my settings
  256.  
  257.          // Check results
  258.          if ( fileDlg.pressedOK() )
  259.           {
  260.             app->setFileName( fileDlg.fileName() );    // Store selected file name
  261.             app->editor()->importFromFile( app->fileName() );  // Load the file into the MLE
  262.           }
  263.           break;
  264.        }
  265.  
  266.       case IDM_FILE_SAVE:
  267.        {
  268.  
  269.          if ( app->hasFileName() )
  270.          {
  271.            app->editor()->exportToFile( app->fileName() );    // Save to file
  272.  
  273.            IString tmpText = resLib.loadString(IDS_SAVE_FILE);
  274.            tmpText.change( "%s",
  275.                            app->fileName(),
  276.                            1, 1);
  277.            app->setStatus( tmpText );
  278.          }
  279.          else
  280.          {
  281.            IString tmpText = resLib.loadString(IDS_ERROR_NO_FILE_LOADED);
  282.            app->setStatus( tmpText );
  283.          }
  284.          break;
  285.        }
  286.  
  287.  
  288.       case IDM_FILE_SAVEAS:
  289.        {
  290.          /*-------------------------------------------------------------*/
  291.          /* When the user selects the "File->Save as" menu item,        */
  292.          /* display the standard "Save as" dialog using IFileDialog.    */
  293.          /* Use the IFileDialog::Settings class to specify initial      */
  294.          /* settings, such as the currently open file.                  */
  295.          /*-------------------------------------------------------------*/
  296.  
  297.           // Specify Save as dialog settings
  298.           IFileDialog::Settings fdSettings;
  299.           fdSettings.setFileName( app->fileName() );
  300.           fdSettings.setSaveAsDialog();
  301.           fdSettings.setOKButtonText( IResourceId( IDS_SAVEAS ) );
  302.           fdSettings.setTitle( IResourceId( IDS_SAVEAS ) );
  303.  
  304.           // Create and show Save as dialog
  305.           IFileDialog fileDlg( IWindow::desktopWindow(), // Parent is desktop
  306.                                app,                      // Owner is main window
  307.                                IFileDialog::helpButton, // Help button style
  308.                                fdSettings);              // Here are my settings
  309.  
  310.           // Check results
  311.           if ( fileDlg.pressedOK() )
  312.           {
  313.              /*================================================*/
  314.              /* Place processing to save file here.            */
  315.              /*================================================*/
  316.  
  317.              app->setFileName( fileDlg.fileName() );
  318.              app->editor()->exportToFile( app->fileName() );  // Save to file
  319.  
  320.              // Display status
  321.              IString tmpStatus = resLib.loadString(IDS_FILE_SAVED);
  322.              tmpStatus.change( "%s",
  323.                                app->fileName(),
  324.                                1, 1);
  325.              app->setStatus( tmpStatus );
  326.           }
  327.         break;
  328.        }
  329.  
  330.       case IDM_FILE_CLOSE:
  331.        {
  332.          // Display "Are you sure" message box
  333.          IMessageBox closeMsg( app );
  334.          closeMsg.setTitle( IResourceId(IDW_FRAME_WINDOW));  // Title is application title
  335.          IMessageBox::Response msgRC;
  336.          msgRC = closeMsg.show( IResourceId(IDS_EXIT),
  337.                         IMessageBox::yesNoButton      |
  338.                         IMessageBox::defButton1       |
  339.                         IMessageBox::queryIcon        |
  340.                         IMessageBox::applicationModal |
  341.                         IMessageBox::moveable);
  342.  
  343.  
  344.          if (msgRC == IMessageBox::yes)  // If user responded yes, close window
  345.            app->close();
  346.          break;
  347.        }
  348.  
  349.       case IDM_EDIT_COPY:
  350.       {
  351.          if( app->editor()->hasSelectedText())
  352.          {
  353.            app->editor()->copy();
  354.            app->enablePaste();
  355.          }
  356.          app->setStatus( IResourceId( IDS_COPY ));
  357.          break;
  358.       }
  359.  
  360.       case IDM_EDIT_CUT:
  361.       {
  362.          if( app->editor()->hasSelectedText())
  363.          {
  364.            app->editor()->cut();
  365.            app->enablePaste();
  366.          }
  367.          app->setStatus( IResourceId( IDS_CUT ));
  368.          break;
  369.       }
  370.  
  371.       case IDM_EDIT_PASTE:
  372.       {
  373.          if ( app->editor()->clipboardHasTextFormat())
  374.            app->editor()->paste();
  375.          app->setStatus( IResourceId( IDS_PASTE ));
  376.          break;
  377.       }
  378.  
  379.       case IDM_HELP_USING:
  380.       {
  381.          // Show IPF Using Help panel
  382.          if ( app->isHelpAvailable() )
  383.            app->helpWin()->show(IHelpWindow::using);
  384.          app->setStatus( IResourceId( IDS_HELP_USING ) );
  385.          break;
  386.       }
  387.  
  388.       case IDM_HELP_PRODINFO:
  389.       {
  390.          // Show product information dialog
  391.          app->productInfo();
  392.          app->setStatus( IResourceId( IDS_HELP_PRODINFO ) );
  393.          break;
  394.       }
  395.  
  396.    } /* end switch */
  397.    return false;
  398. }
  399.  
  400.