home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / template.zip / FILE.C < prev    next >
C/C++ Source or Header  |  1998-04-20  |  18KB  |  603 lines

  1. /**************************************************************************
  2.  *  File name  :  file.c
  3.  *
  4.  *  Description:  This module contains the code for the WM_COMMAND messages
  5.  *                posted by the standard File menu.
  6.  *
  7.  *                This source file contains the following functions:
  8.  *
  9.  *                FileNew(mp2);
  10.  *                FileOpen(mp2);
  11.  *                FileSave(mp2);
  12.  *                FileSaveAs(mp2);
  13.  *                WriteFileToDisk(hf);
  14.  *                GetFileName();
  15.  *                UpdateTitleText(hwnd);
  16.  *                TemplateOpenFilterProc(hwnd, msg, mp1, mp2)
  17.  *                TemplateSaveFilterProc(hwnd, msg, mp1, mp2)
  18.  *
  19.  *  Concepts   :  file management
  20.  *
  21.  *  API's      :  WinLoadString
  22.  *                WinFileDlg
  23.  *                DosOpen
  24.  *                DosClose
  25.  *                WinSetWindowText
  26.  *                WinWindowFromID
  27.  *                WinDefFileDlgProc
  28.  *
  29.  *  Required
  30.  *    Files    :  OS2.H, STRING.H, MAIN.H, HELP.H, XTRN.H
  31.  *
  32.  *  Copyright (C) 1991 IBM Corporation
  33.  *
  34.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  35.  *      sample code created by IBM Corporation. This sample code is not
  36.  *      part of any standard or IBM product and is provided to you solely
  37.  *      for  the purpose of assisting you in the development of your
  38.  *      applications.  The code is provided "AS IS", without
  39.  *      warranty of any kind.  IBM shall not be liable for any damages
  40.  *      arising out of your use of the sample code, even if they have been
  41.  *      advised of the possibility of such damages.                                                    *
  42.  *************************************************************************/
  43. /*
  44.  *  Include files, macros, defined constants, and externs
  45.  */
  46.  
  47. #define INCL_WINFRAMEMGR
  48. #define INCL_WINSWITCHLIST
  49. #define INCL_WINSTDFILE
  50.  
  51. #include <os2.h>
  52. #include <string.h>
  53. #include "main.h"
  54. #include "xtrn.h"
  55. #include "help.h"
  56.  
  57. /*
  58.  *  Global variables
  59.  */
  60.  
  61. CHAR szFullPath[CCHMAXPATH] = "";
  62.  
  63. /*
  64.  *  Entry point declarations
  65.  */
  66.  
  67.  
  68. /**************************************************************************
  69.  *
  70.  *  Name       : FileNew(mp2)
  71.  *
  72.  *  Description: Processes the File menu's New item
  73.  *
  74.  *  Concepts:  called whenever NEW from the FILE menu is selected
  75.  *
  76.  *  API's      : [none]
  77.  *
  78.  *  Parameters :  mp2      = second message parameter
  79.  *
  80.  *  Return     :  [none]
  81.  *
  82.  *************************************************************************/
  83. VOID FileNew(MPARAM mp2)
  84. {
  85.     /*
  86.      *  Enter routines for creating a new file and window
  87.      */
  88.  
  89.     /* clear file name and reset the titlebar text */
  90.    szFullPath[0] = '\0';
  91.    UpdateTitleText(hwndMainFrame);
  92.  
  93.     /*  This routine currently doesn't use the mp2 parameter but
  94.      *  it is referenced here to prevent an 'Unreferenced Parameter'
  95.      *  warning at compile time.
  96.      */
  97.     mp2;
  98. }   /* End of FileNew   */
  99.  
  100.  
  101. /**************************************************************************
  102.  *
  103.  *  Name       : FileOpen(mp2)
  104.  *
  105.  *  Description: Processes the File menu's Open item
  106.  *
  107.  *  Concepts:  Called whenever OPEN from the FILE menu is selected
  108.  *
  109.  *             Calls the standard file open dialog to get the
  110.  *             file name.  The file name is passed onto DosOpen
  111.  *             which returns the handle to the file.  The file
  112.  *             input procedure is called and then the file handle
  113.  *             is closed.
  114.  *
  115.  *  API's      : WinLoadString
  116.  *               WinFileDlg
  117.  *               DosOpen
  118.  *               DosClose
  119.  *
  120.  *  Parameters :  mp2      = second message parameter
  121.  *
  122.  *  Return     :  [none]
  123.  *
  124.  *************************************************************************/
  125. VOID FileOpen(MPARAM mp2)
  126. {
  127.    FILEDLG fdg;
  128.    HFILE hfIn;
  129.    ULONG ulAction;
  130.    CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];
  131.  
  132.    fdg.cbSize = sizeof(FILEDLG);
  133.  
  134.    if(!WinLoadString(hab, (HMODULE)0, IDS_OPEN, MESSAGELEN, szTitle))
  135.    {
  136.       MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  137.       return;
  138.    }
  139.  
  140.    if(!WinLoadString(hab, (HMODULE)0, IDS_OPEN, MESSAGELEN, szButton))
  141.    {
  142.       MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  143.       return;
  144.    }
  145.  
  146.    fdg.pszTitle = szTitle;
  147.    fdg.pszOKButton = szButton;
  148.    fdg.ulUser = 0L;
  149.    fdg.fl = FDS_HELPBUTTON | FDS_CENTER | FDS_OPEN_DIALOG;
  150.    fdg.pfnDlgProc = (PFNWP)TemplateOpenFilterProc;
  151.    fdg.lReturn = 0L;
  152.    fdg.lSRC = 0L;
  153.    fdg.hMod = 0;
  154.    fdg.usDlgId = FILEOPEN;
  155.    fdg.x = 0;
  156.    fdg.y = 0;
  157.  
  158.    if(!WinLoadString(hab, (HMODULE)0, IDS_FILEOPENEXT, CCHMAXPATH,
  159.                      fdg.szFullFile))
  160.    {
  161.       MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  162.       return;
  163.    }
  164.  
  165.    fdg.pszIType       = (PSZ)NULL;
  166.    fdg.papszITypeList = (PAPSZ)NULL;
  167.    fdg.pszIDrive      = (PSZ)NULL;
  168.    fdg.papszIDriveList= (PAPSZ)NULL;
  169.    fdg.sEAType        = (SHORT)0;
  170.    fdg.papszFQFilename= (PAPSZ)NULL;
  171.    fdg.ulFQFCount     = 0L;
  172.  
  173.    /* get the file */
  174.    if(!WinFileDlg(HWND_DESKTOP, hwndMain, &fdg))
  175.       return;
  176.  
  177.    /*
  178.     *  Upon sucessful return of a file, open it for reading
  179.     */
  180.  
  181.    if(fdg.lReturn == ID_OK)
  182.    {
  183.       if( DosOpen(fdg.szFullFile,     /* file name from Open dialog */
  184.                   &hfIn,              /* file handle returned */
  185.                   &ulAction,
  186.                   0L,
  187.                   FILE_NORMAL,
  188.                   FILE_OPEN,
  189.                   OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
  190.                   (PEAOP2)NULL))
  191.       {
  192.          MessageBox(hwndMain,
  193.                       IDMSG_CANNOTOPENINPUTFILE,
  194.                       MB_OK | MB_ERROR,
  195.                       FALSE);
  196.  
  197.            /*
  198.             *  NOTE:  You now have several options on how to proceed
  199.             *      from this point:
  200.             *          - You can abort the File Open by returning from
  201.             *            this procedure.
  202.             *          - You can bring up the File Open dialog again
  203.             *            and have the user pick another file.
  204.             *          - You can check the error code from the DosOpen,
  205.             *            determine why the open failed, and take an
  206.             *            action appropriate to the specific failure.
  207.             */
  208.  
  209.          return;
  210.       }
  211.  
  212.        /* copy file name into file name buffer */
  213.       strcpy(szFullPath, fdg.szFullFile);
  214.  
  215.        /*
  216.         *  Place routine for reading the file here.
  217.         */
  218.  
  219.       DosClose(hfIn);
  220.  
  221.       UpdateTitleText(hwndMainFrame);
  222.    }
  223.  
  224.    /* This routine currently doesn't use the mp2 parameter but       *\
  225.     *  it is referenced here to prevent an 'Unreferenced Parameter'
  226.    \*  warning at compile time.                                      */
  227.    mp2;
  228. }   /* End of FileOpen   */
  229.  
  230. /**************************************************************************
  231.  *
  232.  *  Name       : FileSave(mp2)
  233.  *
  234.  *  Description: Processes the File menu's Save item
  235.  *
  236.  *  Concepts:  Called whenever SAVE from the FILE menu is selected
  237.  *
  238.  *             Routine opens the file for output, calls the
  239.  *             application's save routine, and closes the file.
  240.  *
  241.  *  API's      : DosOpen
  242.  *               DosClose
  243.  *
  244.  *  Parameters :  mp2      = second message parameter
  245.  *
  246.  *  Return     :  [none]
  247.  *
  248.  *************************************************************************/
  249. VOID FileSave(MPARAM mp2)
  250. {
  251.    HFILE hf;
  252.    ULONG ulAction;
  253.    /*
  254.     * If the file currently is untitled, we will need to get a file
  255.     * name from the user before we can open the file.  Getting a
  256.     * file name is normally done during the FileSaveAs operation
  257.     * so we will treat this save as a SaveAs and call FileSaveAs().
  258.     * If the file is titled, then we save the file.
  259.     *
  260.     * NOTE:  This routine will be called by FileSaveAs(), but only
  261.     *  after a valid file name has been obtained.  So, FileSaveAs()
  262.     *  will not be called again from this routine.
  263.     */
  264.    if(szFullPath[0] == '\0')
  265.    {
  266.       FileSaveAs(mp2);
  267.       return;
  268.    }
  269.  
  270.    /* open the file */
  271.    if( DosOpen(szFullPath,         /* file name of current document */
  272.                &hf,                /* file handle of output file */
  273.                &ulAction,
  274.                0L,
  275.                FILE_NORMAL,
  276.                FILE_OPEN | FILE_CREATE,
  277.                OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
  278.                (PEAOP2)NULL))
  279.    {
  280.        MessageBox(hwndMain,
  281.                   IDMSG_CANNOTOPENOUTPUTFILE,
  282.                   MB_OK | MB_ERROR,
  283.                   FALSE);
  284.        return;
  285.    }
  286.  
  287.    WriteFileToDisk(hf);
  288.  
  289.    DosClose(hf);
  290.  
  291.    /* This routine currently doesn't use the mp2 parameter but       *\
  292.     *  it is referenced here to prevent an 'Unreferenced Parameter'
  293.    \*  warning at compile time.                                      */
  294.    mp2;
  295. }   /* End of FileSave   */
  296.  
  297. /**************************************************************************
  298.  *
  299.  *  Name       : FileSaveAs(mp2)
  300.  *
  301.  *  Description: Processes the File menu's Save as item
  302.  *
  303.  *  Concepts:  Called whenever SAVE AS from the FILE menu is selected
  304.  *
  305.  *             Routine prompts the user for the name of the file,
  306.  *             then saves the file.
  307.  *
  308.  *  API's      : DosOpen
  309.  *               DosClose
  310.  *
  311.  *  Parameters :  mp2      = second message parameter
  312.  *
  313.  *  Return     :  [none]
  314.  *
  315.  *************************************************************************/
  316. VOID FileSaveAs(MPARAM mp2)
  317. {
  318.    HFILE hf;
  319.    ULONG ulAction;
  320.    ULONG sT;
  321.  
  322.    while(TRUE)     /* infinite loop until we break out of it */
  323.    {
  324.       /* Get a name for the file */
  325.       if(!GetFileName())
  326.          return;
  327.  
  328.       /* See if the file exists.  If it does, then confirm that the
  329.        * user wants to overwrite it.  If he doesn't, then get a new
  330.        * file name
  331.        */
  332.       if( DosOpen(szFullPath,     /* file name from, GetFileName() */
  333.                   &hf,            /* handle of opened file */
  334.                   &ulAction,
  335.                   0L,
  336.                   FILE_NORMAL,
  337.                   FILE_CREATE,
  338.                   OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
  339.                   (PEAOP2)NULL))
  340.       {
  341.          MessageBox(hwndMain,
  342.                     IDMSG_CANNOTOPENOUTPUTFILE,
  343.                     MB_OK | MB_ERROR,
  344.                     FALSE);
  345.          return;
  346.       }
  347.       else
  348.          DosClose(hf);
  349.  
  350.       /* if file exists, ask if we want to overwrite it */
  351.       if(ulAction == FILE_EXISTED)
  352.       {
  353.          sT = MessageBox(hwndMain,
  354.                          IDMSG_OVERWRITEFILE,
  355.                          MB_QUERY | MB_YESNOCANCEL,
  356.                          FALSE);
  357.  
  358.          if(sT == MBID_CANCEL)
  359.             return;
  360.  
  361.          if(sT == MBID_YES)
  362.             break;
  363.  
  364.           /* if user selected no, repeat the sequence */
  365.       }
  366.  
  367.    }   /* while(TRUE) */
  368.  
  369.    UpdateTitleText(hwndMainFrame);
  370.  
  371.    /*
  372.     * Now that we have a valid file name, save the file.  This is
  373.     * normally done under the File Save function so we can just
  374.     * call the FileSave() function here.  Note that FileSave() will
  375.     * not call FileSaveAs() back since there is a valid file name
  376.     */
  377.     FileSave(mp2);
  378.  
  379.    /*  This routine currently doesn't use the mp2 parameter but
  380.     *  it is referenced here to prevent an 'Unreferenced Parameter'
  381.     *  warning at compile time.
  382.     */
  383.    mp2;
  384. }   /* End of FileSaveAs   */
  385.  
  386.  
  387. /**************************************************************************
  388.  *
  389.  *  Name       : WriteFileToDisk(hfile)
  390.  *
  391.  *  Description: writes the current file to the file in hf
  392.  *
  393.  *  Concepts:  called from FileSave and FileSaveAs when a fileelected
  394.  *             is to be saved to disk
  395.  *
  396.  *             NOTE:  This routine must not close the file.
  397.  *
  398.  *  API's      : [none]
  399.  *
  400.  *  Parameters :  hfile    = file handle
  401.  *
  402.  *  Return     :  [none]
  403.  *
  404.  *************************************************************************/
  405. VOID WriteFileToDisk(HFILE hf)                                          \
  406. {
  407.     /*
  408.      *  Place routine to write a disk file here.
  409.      */
  410.  
  411.     hf;
  412. }   /* End of WriteFileToDisk   */
  413.  
  414.  
  415. /**************************************************************************
  416.  *
  417.  *  Name       : GetFileName()
  418.  *
  419.  *  Description: gets the name of the save file
  420.  *
  421.  *  Concepts:    called when the user needs to supply a name for
  422.  *               the file to be saved
  423.  *
  424.  *                calls the standard file open dialog to get the
  425.  *               file name.
  426.  *
  427.  *  API's      : WinLoadString
  428.  *               WinFileDlg
  429.  *
  430.  *  Parameters : [none]
  431.  *
  432.  *  Return     :  TRUE if successful in getting a file name
  433.  *                FALSE if not successful in getting a file name
  434.  *
  435.  *************************************************************************/
  436. BOOL GetFileName(VOID)
  437. {
  438.    FILEDLG fdg;
  439.    CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];
  440.  
  441.    fdg.cbSize = sizeof(FILEDLG);
  442.  
  443.    if(!WinLoadString(hab, 0, IDS_SAVE, MESSAGELEN, szTitle))
  444.    {
  445.        MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  446.        return FALSE;
  447.    }
  448.  
  449.    if(!WinLoadString(hab, 0, IDS_SAVE, MESSAGELEN, szButton))
  450.    {
  451.        MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
  452.        return FALSE;
  453.    }
  454.  
  455.    fdg.pszTitle = szTitle;
  456.    fdg.pszOKButton = szButton;
  457.  
  458.    fdg.ulUser = 0L;
  459.    fdg.fl = FDS_HELPBUTTON | FDS_CENTER | FDS_SAVEAS_DIALOG;
  460.    fdg.pfnDlgProc = (PFNWP)TemplateSaveFilterProc;
  461.    fdg.lReturn = 0L;
  462.    fdg.lSRC = 0L;
  463.    fdg.hMod = (HMODULE)NULL;
  464.    fdg.usDlgId = FILESAVE;
  465.    fdg.x = 0;
  466.    fdg.y = 0;
  467.    fdg.pszIType = (PSZ)NULL;
  468.    fdg.papszITypeList = (PAPSZ)NULL;
  469.    fdg.pszIDrive = (PSZ)NULL;
  470.    fdg.papszIDriveList = (PAPSZ)NULL;
  471.    fdg.sEAType = (SHORT)0;
  472.    fdg.papszFQFilename = (PAPSZ)NULL;
  473.    fdg.ulFQFCount = 0L;
  474.  
  475.    strcpy(fdg.szFullFile, szFullPath);
  476.  
  477.    /* get the file */
  478.    if(!WinFileDlg(HWND_DESKTOP, hwndMain, &fdg))
  479.        return FALSE;
  480.  
  481.    if(fdg.lReturn != ID_OK)
  482.        return FALSE;
  483.  
  484.    /* copy file name and path returned into buffers */
  485.    strcpy(szFullPath, fdg.szFullFile);
  486.  
  487.    return TRUE;
  488. }   /* End of GetFileName   */
  489.  
  490. /**************************************************************************
  491.  *
  492.  *  Name       : UpdateTitleText
  493.  *
  494.  *  Description: Updates the text in the main window's title bar to
  495.  *               display the app name, followed by the separator,
  496.  *               followed by the file name.
  497.  *
  498.  *  Concepts :   Called at init time and when the text file is changed.
  499.  *               Gets the program name, appends the separator, and
  500.  *               appends the file name.
  501.  *
  502.  *  API's      : WinLoadString
  503.  *               WinSetWindowText
  504.  *               WinWindowFromID
  505.  *
  506.  *  Parameters : hwnd = window handle
  507.  *
  508.  *  Return     :  [none]
  509.  *
  510.  *************************************************************************/
  511. VOID UpdateTitleText(HWND hwnd)
  512. {
  513.    CHAR szBuf[MAXNAMEL+TITLESEPARATORLEN+CCHMAXPATH];
  514.    CHAR szSeparator[TITLESEPARATORLEN+1];
  515.    PSZ pszT;
  516.  
  517.    WinLoadString(hab, (HMODULE)0, IDS_APPNAME, MAXNAMEL, szBuf);
  518.    WinLoadString(hab,
  519.                  (HMODULE)0,
  520.                  IDS_TITLEBARSEPARATOR,
  521.                  TITLESEPARATORLEN,
  522.                  szSeparator);
  523.  
  524.    strcat(szBuf, szSeparator);
  525.  
  526.    if(szFullPath[0] == '\0')
  527.       pszT = szUntitled;
  528.    else
  529.       pszT = szFullPath;
  530.  
  531.    strcat(szBuf, pszT);
  532.  
  533.    WinSetWindowText(WinWindowFromID(hwnd, FID_TITLEBAR), szBuf);
  534. }   /* End of UpdateTitleText   */
  535.  
  536. /**************************************************************************
  537.  *
  538.  *  Name       : TemplateOpenFilterProc(hwnd, msg, mp1, mp2)
  539.  *
  540.  *  Description: Filters the help messages to the open dialog
  541.  *
  542.  *  Concepts :   If help is requested during the standard OPEN
  543.  *               dialog, this routine will display an appropriate
  544.  *               panel.
  545.  *
  546.  *  API's      : WinDefFileDlgProc
  547.  *
  548.  *  Parameters : hwnd = window handle
  549.  *               message = message i.d.
  550.  *               mp1     = first message parameter
  551.  *               mp2     = second message parameter
  552.  *
  553.  *  Return     :  the result of the message
  554.  *
  555.  *************************************************************************/
  556. MRESULT EXPENTRY TemplateOpenFilterProc(HWND hwnd, ULONG message,
  557.                                         MPARAM mp1, MPARAM mp2 )
  558. {
  559.    if(message == WM_HELP)
  560.    {
  561.       DisplayHelpPanel(HID_FS_OPEN_DLG_HELP_PANEL);
  562.       return (MRESULT)FALSE ;
  563.    }
  564.  
  565.    return WinDefFileDlgProc( hwnd, message, mp1, mp2 ) ;
  566. }  /*  End of TemplateSaveFilterProc  */
  567.  
  568.  
  569. /**************************************************************************
  570.  *
  571.  *  Name       : TemplateSaveFilterProc(hwnd, msg, mp1, mp2)
  572.  *
  573.  *  Description: Filters the help messages to the save dialog
  574.  *
  575.  *  Concepts :   If help is requested during the standard SAVEAS
  576.  *               dialog, this routine will display an appropriate
  577.  *               panel.
  578.  *
  579.  *  API's      : WinDefFileDlgProc
  580.  *
  581.  *  Parameters : hwnd = window handle
  582.  *               message = message i.d.
  583.  *               mp1     = first message parameter
  584.  *               mp2     = second message parameter
  585.  *
  586.  *  Return     :  the result of the message
  587.  *
  588.  *************************************************************************/
  589. MRESULT EXPENTRY TemplateSaveFilterProc(HWND hwnd,
  590.                                         ULONG message,
  591.                                         MPARAM mp1,
  592.                                         MPARAM mp2 )
  593. {
  594.    if(message == WM_HELP)
  595.    {
  596.       DisplayHelpPanel(HID_FS_SAVE_DLG_HELP_PANEL);
  597.       return (MRESULT)FALSE ;
  598.    }
  599.  
  600.    return WinDefFileDlgProc( hwnd, message, mp1, mp2 );
  601. }  /*  End of TemplateSaveFilterProc  */
  602. /***************************  End of file.c  ***************************/
  603.