home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / fonts / filedlg / filedlg.cpp next >
Encoding:
C/C++ Source or Header  |  1996-10-29  |  4.1 KB  |  129 lines

  1. //************************************************************
  2. // Views - Using the File Dialog
  3. //
  4. // Copyright (C) 1994, Law, Leong, Love, Olson, Tsuji.
  5. // Copyright (c) 1997 John Wiley & Sons, Inc. 
  6. // All Rights Reserved.
  7. //************************************************************
  8. #include <iapp.hpp>
  9. #include <ifiledlg.hpp>
  10. #include <imsgbox.hpp>
  11. #include <istring.hpp>
  12. #include "filedlg.hpp"
  13. #include "filedlg.h"
  14.  
  15. void main()
  16. {
  17.   // Create a primary window that contains a read-only MLE.
  18.   IFrameWindow primary( "Using the File Dialog", MAIN_WINDOW,
  19.       IFrameWindow::defaultStyle() | IFrameWindow::menuBar );
  20.   IMultiLineEdit
  21.     mle( IC_FRAME_CLIENT_ID, &primary, &primary,
  22.          IRectangle(),
  23.          (IMultiLineEdit::classDefaultStyle
  24.            | IMultiLineEdit::horizontalScroll
  25.            | IMultiLineEdit::readOnly )
  26.           & ~IMultiLineEdit::wordWrap );
  27.   primary.setClient( &mle );
  28.  
  29.   // Create a command handler for the menu bar.
  30.   MyCommandHandler cmdHandler( &primary, &mle );
  31.   cmdHandler.handleEventsFor( &primary );
  32.  
  33.   // Set the input focus, and show the window.
  34.   primary
  35.     .setFocus()
  36.     .show();
  37.  
  38.   // Start event processing.
  39.   IApplication::current().run();
  40. }
  41.  
  42.  
  43. /*------------------------------------------------------------------------------
  44. | MyCommandHandler::command                                                    |
  45. |   Handle menu commands.                                                      |
  46. ------------------------------------------------------------------------------*/
  47. IBase::Boolean MyCommandHandler::command( ICommandEvent& cmdEvent )
  48. {
  49.   switch ( cmdEvent.commandId() )
  50.   {
  51.     case MI_OPENDLG:      // Open dialog processing.
  52.     {
  53.       // Initialize a dialog settings object.
  54.       IFileDialog::Settings settings;
  55.  
  56.       // Set the initial file name, title and position.
  57.       settings.setFileName( __FILE__ );
  58.       settings.setOKButtonText( "Open" );
  59.       settings.setTitle( "Select File to View" );
  60.       settings.setPosition( IPoint( 50, 50 ) );
  61.  
  62.       // Create a file dialog using the default styles.
  63.       IFileDialog fileDlg( IWindow::desktopWindow(),
  64.                            frame, settings );
  65.  
  66.       // Check if a file name was selected.
  67.       if (fileDlg.pressedOK())
  68.       {
  69.         // Empty any contents of the MLE.
  70.         mle->removeAll();
  71.  
  72.         // Retrieve the file name selected.
  73.         IString selectedFile = fileDlg.fileName();
  74.  
  75.         // Read the contents of the file into the MLE
  76.         // and scroll to the top of the file.
  77.         mle->importFromFile( selectedFile );
  78.         mle->setCursorLinePosition( 0 );
  79.       }
  80.       return (true);
  81.     }
  82.  
  83.     case MI_SAVEASDLG:    // Save As dialog processing.
  84.     {
  85.       // Initialize a dialog settings object.
  86.       IFileDialog::Settings settings;
  87.  
  88.       // Set the initial file name, title and position.
  89.       settings.setFileName( "TEMPFILE" );
  90.       settings.setTitle( "Select File Name to Save As" );
  91.       settings.setPosition( IPoint( 50, 50 ) );
  92.  
  93.       // Make this dialog a Save As dialog.
  94.       settings.setSaveAsDialog();
  95.  
  96.       // Create a file dialog using the default styles.
  97.       IFileDialog fileDlg( IWindow::desktopWindow(),
  98.                            frame, settings );
  99.  
  100.       // Check if a file name was selected.
  101.       if (fileDlg.pressedOK())
  102.       {
  103.         // Retrieve the file name selected.
  104.         IString selectedFile = fileDlg.fileName();
  105.  
  106.         // Double check with the user before overwriting file.
  107.         IMessageBox msgBox( frame );
  108.         IMessageBox::Response reply =
  109.           msgBox.show( "Are you sure you want to overwrite this file?",
  110.              IMessageBox::okCancelButton | IMessageBox::queryIcon);
  111.  
  112.         // If user concurs, then overwrite the file.
  113.         if (reply == IMessageBox::ok)
  114.         {
  115.           // Write the contents of the MLE to the file.
  116.           mle->exportToFile( selectedFile );
  117.         }
  118.       }
  119.       return (true);
  120.     }
  121.  
  122.     case MI_EXIT:
  123.       frame->close();
  124.       return(true);
  125.   }
  126.  
  127.   return(false);
  128. }
  129.