File:  [OS/2 SDKs] / os232sdk / toolkt20 / c / samples / template / file.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:31 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: os2sdk-1990, HEAD
Microsoft OS/2 SDK 2.0 05-30-1990

/*==============================================================*\
 *  File.c - routines for handling the standard file menu
 *                  commands
 *      Created 1989, 1990 Microsoft Corp.
 *--------------------------------------------------------------
 *
 *  This module contains the code for the WM_COMMAND messages
 *  posted by the standard File menu.
 *
 *--------------------------------------------------------------
 *
 *  This source file contains the following functions:
 *
 *           FileNew(mp2);
 *           FileOpen(mp2);
 *           FileSave(mp2);
 *           FileSaveAs(mp2);
 *           FilePrint(mp2);
 *           FilePageSetup(mp2);
 *           FilePrintSetup(mp2);
 *           FileExit(mp2);
 *           WriteFileToDisk(hf);
 *           GetFileName();
 *           UpdateTitleText(hwnd);
 *
\*==============================================================*/

/*--------------------------------------------------------------*\
 *  Include files, macros, defined constants, and externs
\*--------------------------------------------------------------*/

#define INCL_WINFRAMEMGR
#define INCL_WINSWITCHLIST
#define INCL_WINSTDFILE

#include <os2.h>
#include <string.h>
#include "main.h"
#include "xtrn.h"

/*--------------------------------------------------------------*\
 *  Global variables
\*--------------------------------------------------------------*/

CHAR szFullPath[CCHMAXPATH] = "";

/*--------------------------------------------------------------*\
 *  Entry point declarations
\*--------------------------------------------------------------*/


/****************************************************************\
 *  New file routine
 *--------------------------------------------------------------
 *
 *  Name:   FileNew(mp2)
 *
 *  Purpose: Processes the File menu's New item
 *
 *  Usage:  called whenever New from the File menu is selected
 *
 *  Method:
 *
 *
 *  Returns:
 *
\****************************************************************/
VOID FileNew(mp2)
MPARAM mp2;         /* second parameter of WM_COMMAND message */
{
    /*--------------------------------------------------------------*\
     *  Enter routines for creating a new file and window
    \*--------------------------------------------------------------*/


    /* clear file name and reset the titlebar text */
    szFullPath[0] = '\0';
    UpdateTitleText(hwndMainFrame);


    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FileNew() */


/****************************************************************\
 *  Open file routine
 *--------------------------------------------------------------
 *
 *  Name:   FileOpen(mp2)
 *
 *  Purpose: Processes the File menu's Open item.
 *
 *  Usage:  called whenever New from the File menu is selected
 *
 *  Method:  calls the standard file open dialog to get the
 *          file name.  The file name is passed onto DosOpen
 *          which returns the handle to the file.  The file
 *          input procedure is called and then the file handle
 *          is closed.
 *
 *  Returns:
 *
\****************************************************************/
VOID FileOpen(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
{
    FILEDLG fdg;
    HFILE hfIn;
    ULONG ulAction;
    CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];

    fdg.cbsize = sizeof(FILEDLG);

    if(!WinLoadString(hab, NULL, IDS_OPENDLGTITLE, MESSAGELEN, szTitle))  {
        MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
        return;
    }

    if(!WinLoadString(hab, NULL, IDS_OPENDLGBUTTON, MESSAGELEN, szButton))  {
        MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
        return;
    }

    fdg.usDialogType = OPEN_DIALOG;
    fdg.pszTitle = szTitle;
    fdg.pszOKButton = szButton;
    fdg.lUser = 0L;
    fdg.fl = FDS_HELPBUTTON | FDS_CENTER;
    fdg.pfnDlgProc = NULL;
    fdg.lReturn = 0L;
    fdg.lSRC = 0L;
    fdg.hmod = NULL;
    fdg.idDlg = FILEOPEN;
    fdg.x = 0;
    fdg.y = 0;

    if(!WinLoadString(hab,
                      NULL,
                      IDS_FILEOPENEXT,
                      CCHMAXPATH,
                      fdg.szFullFile))  {

        MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
        return;
    }

    fdg.pszIType = 0L;
    fdg.ppszITypeList = 0L;
    fdg.pszIDrive = 0L;
    fdg.ppszIDriveList = 0L;
    fdg.sEAType = 0;

    /* get the file */
    if(!KitFileDlg(hwndMain, (PFILEDLG)&fdg))
        return;


    /*--------------------------------------------------------------*\
     *  Upon sucessful return of a file, open it for reading
    \*--------------------------------------------------------------*/

    if(fdg.lReturn == ID_OK)  {
        if( DosOpen(fdg.szFullFile,     /* file name from Open dialog */
                    &hfIn,              /* file handle returned */
                    &ulAction,
                    0L,
                    FILE_NORMAL,
                    FILE_OPEN,
                    OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
                    0L))  {
            MessageBox(hwndMain,
                       IDMSG_CANNOTOPENINPUTFILE,
                       MB_OK | MB_ERROR,
                       FALSE);

            /*-------------------------------------------------------*\
             *  NOTE:  You now have several options on how to proceed
             *      from this point:
             *          - You can abort the File Open by returning from
             *            this procedure.
             *          - You can bring up the File Open dialog again
             *            and have the user pick another file.
             *          - You can check the error code from the DosOpen,
             *            determine why the open failed, and take an
             *            action appropriate to the specific failure.
            \*-------------------------------------------------------*/


            return;
        }

        /* copy file name into file name buffer */
        strcpy(szFullPath, fdg.szFullFile);

        /*--------------------------------------------------------------*\
         *  Place routine for reading the file here
        \*--------------------------------------------------------------*/

        DosClose(hfIn);

        UpdateTitleText(hwndMainFrame);
    }


    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FileOpen() */


/****************************************************************\
 *  Save file routine
 *--------------------------------------------------------------
 *
 *  Name:   FileSave(mp2)
 *
 *  Purpose: Processes the File menu's Save item.
 *
 *  Usage:  called whenever Save from the File menu is
 *          selected
 *
 *  Method:  Routine opens the file for output, calls the
 *           application's save routine, and closes the file
 *
 *  Returns:
 *
\****************************************************************/
VOID FileSave(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
{
    HFILE hf;
    ULONG ulAction;

    /*
     * If the file currently is untitled, we will need to get a file
     * name from the user before we can open the file.  Getting a
     * file name is normally done during the FileSaveAs operation
     * so we will treat this save as a SaveAs and call FileSaveAs().
     * If the file is titled, then we save the file.
     *
     * NOTE:  This routine will be called by FileSaveAs(), but only
     *  after a valid file name has been obtained.  So, FileSaveAs()
     *  will not be called again from this routine.
     */
    if(szFullPath[0] == '\0')  {
        FileSaveAs(mp2);
        return;
    }

    /* open the file */
    if( DosOpen(szFullPath,         /* file name of current document */
                &hf,                /* file handle of output file */
                &ulAction,
                0L,
                FILE_NORMAL,
                FILE_OPEN | FILE_CREATE,
                OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
                0L))  {

        MessageBox(hwndMain,
                   IDMSG_CANNOTOPENOUTPUTFILE,
                   MB_OK | MB_ERROR,
                   FALSE);
        return;
    }

    WriteFileToDisk(hf);

    DosClose(hf);


    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FileSave() */

/****************************************************************\
 *  Save As file routine
 *--------------------------------------------------------------
 *
 *  Name:   FileSaveAs(mp2)
 *
 *  Purpose: Processes the File menu's Save As item.
 *
 *  Usage:  called whenever Save As from the File menu is
 *          selected
 *
 *  Method:  Routine prompts the user for a name for the
 *           file and then saves the file.
 *
 *  Returns:
 *
\****************************************************************/
VOID FileSaveAs(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
{
    HFILE hf;
    ULONG ulAction;
    SHORT sT;

    while(TRUE)  {      /* infinite loop until we break out of it */

        /* Get a name for the file */
        if(!GetFileName())  {
            return;
        }

        /* See if the file exists.  If it does, then confirm that the
         * user wants to overwrite it.  If he doesn't, then get a new
         * file name
         */
        if( DosOpen(szFullPath,     /* file name from, GetFileName() */
                    &hf,            /* handle of opened file */
                    &ulAction,
                    0L,
                    FILE_NORMAL,
                    FILE_CREATE,
                    OPEN_ACCESS_WRITEONLY | OPEN_SHARE_DENYNONE,
                    0L))  {

            MessageBox(hwndMain,
                       IDMSG_CANNOTOPENOUTPUTFILE,
                       MB_OK | MB_ERROR,
                       FALSE);
            return;
        } else
            DosClose(hf);

        /* if file exists, ask if we want to overwrite it */
        if(ulAction == FILE_EXISTED)  {
            sT = MessageBox(hwndMain,
                            IDMSG_OVERWRITEFILE,
                            MB_QUERY | MB_YESNOCANCEL,
                            FALSE);


            if(sT == MBID_CANCEL)
                return;

            if(sT == MBID_YES)
                break;

            /* if user selected no, repeat the sequence */
        }

    }   /* while(TRUE) */

    UpdateTitleText(hwndMainFrame);

    /*
     * Now that we have a valid file name, save the file.  This is
     * normally done under the File Save function so we can just
     * call the FileSave() function here.  Note that FileSave() will
     * not call FileSaveAs() back since there is a valid file name
     */
     FileSave(mp2);



    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FileSaveAs() */

#ifdef PRINT_DLGS_ENABLED

/****************************************************************\
 *  Print file routine
 *--------------------------------------------------------------
 *
 *  Name:   FilePrint(mp2)
 *
 *  Purpose: Processes the File menu's Print item.
 *
 *  Usage:  called whenever Print from the File menu is
 *          selected
 *
 *  Method:  Routine calls the application's print routine
 *
 *  Returns:
 *
\****************************************************************/
VOID FilePrint(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
{

    Print(hwndMain);

    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FilePrint() */

/****************************************************************\
 *  Page setup routine
 *--------------------------------------------------------------
 *
 *  Name:   FilePageSetup(mp2)
 *
 *  Purpose: Processes the File menu's Page Setup item.
 *
 *  Usage:  called whenever Page Setup from the File menu is
 *          selected
 *
 *  Method:  Routine calls the application's page setup routine
 *
 *  Returns:
 *
\****************************************************************/
VOID FilePageSetup(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
{

    PageSetup(hwndMain);

    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FilePageSetup() */

/****************************************************************\
 *  Print Setup routine
 *--------------------------------------------------------------
 *
 *  Name:   FilePrintSetup(mp2)
 *
 *  Purpose: Processes the File menu's Print Setup item.
 *
 *  Usage:  called whenever Print Setup from the File menu is
 *          selected
 *
 *  Method:  Routine calls the application's Print Setup routine
 *
 *  Returns:
 *
\****************************************************************/
VOID FilePrintSetup(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
{

    PrintSetup(hwndMain);

    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FilePrintSetup() */

#endif  /* PRINT_DLGS_ENABLED */

/****************************************************************\
 *  Exit routine
 *--------------------------------------------------------------
 *
 *  Name:   FileExit(mp2)
 *
 *  Purpose: Processes the File menu's Exit item.
 *
 *  Usage:  called whenever Exit from the file menu is
 *          selected
 *
 *  Method:  Routine posts a WM_CLOSE message to the main
 *           application window.
 *
 *  Returns:
 *
\****************************************************************/
VOID FileExit(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
{

    WinPostMsg(hwndMain, WM_CLOSE, (MPARAM)NULL, (MPARAM)NULL);

    /* This routine currently doesn't use the mp2 parameter but       *\
     *  it is referenced here to prevent an 'Unreferenced Parameter'
    \*  warning at compile time.                                      */
    mp2;

}   /* FileExit() */

/****************************************************************\
 *  Write file routine
 *--------------------------------------------------------------
 *
 *  Name:   WriteFileToDisk(hf)
 *
 *  Purpose: Writes the current file to the file in hf
 *
 *  Usage:  called from FileSave and FileSaveAs when a file
 *          is to be saved to disk
 *
 *  Method:
 *
 *
 *      NOTE:  This routine must not close the file.
 *
 *  Returns:
 *
\****************************************************************/
VOID WriteFileToDisk(hf)                                          \
HFILE hf;       /* file handle to file opened for input */
{

    /*--------------------------------------------------------------*\
     *  Place routine to write a disk file here
    \*--------------------------------------------------------------*/

    hf;


}   /* WriteFileToDisk() */

/****************************************************************\
 *  Get file name routine
 *--------------------------------------------------------------
 *
 *  Name:   GetFileName()
 *
 *  Purpose: Gets the name of the save file.
 *
 *  Usage:  called when the user needs to supply a name for
 *          the file to be saved
 *
 *  Method:  calls the standard file open dialog to get the
 *          file name.
 *
 *  Returns: TRUE if successful in getting a file name, FALSE
 *              if not
 *
\****************************************************************/
BOOL GetFileName(VOID)
{
    FILEDLG fdg;
    CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];

    fdg.cbsize = sizeof(FILEDLG);
    fdg.usDialogType = SAVEAS_DIALOG;

    if(!WinLoadString(hab, NULL, IDS_SAVEDLGTITLE, MESSAGELEN, szTitle))  {
        MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
        return FALSE;
    }

    if(!WinLoadString(hab, NULL, IDS_SAVEDLGBUTTON, MESSAGELEN, szButton))  {
        MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
        return FALSE;
    }

    fdg.pszTitle = szTitle;
    fdg.pszOKButton = szButton;

    fdg.lUser = 0L;
    fdg.fl = FDS_HELPBUTTON | FDS_CENTER;
    fdg.pfnDlgProc = NULL;
    fdg.lReturn = 0L;
    fdg.lSRC = 0L;
    fdg.hmod = NULL;
    fdg.idDlg = FILESAVE;
    fdg.x = 0;
    fdg.y = 0;
    fdg.pszIType = 0L;
    fdg.ppszITypeList = 0L;
    fdg.pszIDrive = 0L;
    fdg.ppszIDriveList = 0L;
    fdg.sEAType = 0;
    strcpy(fdg.szFullFile, szFullPath);

    /* get the file */
    if(!KitFileDlg(hwndMain, (PFILEDLG)&fdg))
        return FALSE;

    if(fdg.lReturn != ID_OK)
        return FALSE;

    /* copy file name and path returned into buffers */
    strcpy(szFullPath, fdg.szFullFile);

    return TRUE;

}   /* GetFileName() */

/****************************************************************\
 *  Appends the app name to the title bar text
 *--------------------------------------------------------------
 *
 *  Name:   UpdateTitleText(hwnd)
 *
 *  Purpose: Updates the text in the main window's title bar to
 *          display the app name, followed by the separator,
 *          followed by the file name
 *
 *  Usage:  called at init time and when the text file is changed
 *
 *  Method:  gets the program name, appends the separator, and
 *          appends the file name.
 *
 *  Returns:
 *
\****************************************************************/
VOID UpdateTitleText(hwnd)
HWND hwnd;      /* handle to frame window */
{
    CHAR szBuf[MAXNAMEL];
    CHAR szSeparator[TITLESEPARATORLEN+1];
    PSZ pszT;

    WinQueryTaskTitle(NULL, szBuf, MAXNAMEL);

    WinLoadString(hab,
                  NULL,
                  IDS_TITLEBARSEPARATOR,
                  TITLESEPARATORLEN,
                  szSeparator);

    strcat(szBuf, szSeparator);

    if(szFullPath[0] == '\0')
        pszT = szUntitled;
    else
        pszT = szFullPath;

    strcat(szBuf, pszT);

    WinSetWindowText(WinWindowFromID(hwnd, FID_TITLEBAR), szBuf);

}   /* UpdateTitleText() */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.