File:  [OS/2 SDKs] / os232sdk / toolkt20 / c / samples / style / sty_file.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 12:26:30 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 1990, Microsoft, IBM  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_WINMLE
#define INCL_WINSTDFILE


/* The comments around this line should be removed when the import/export
    bug in MLEs is fixed */
/* #define MLE_BUGS_FIXED */

#include <os2.h>
#include <string.h>
#include "sty_main.h"
#include "sty_xtrn.h"
#include "sty_dlg.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 */
{
    SHORT sT;

    /*--------------------------------------------------------------*\
     *  Enter routines for creating a new file and window
    \*--------------------------------------------------------------*/

    /* save file if changed */
    if(WinSendMsg(hwndMLE, MLM_QUERYCHANGED, NULL, NULL))  {
        sT = MessageBox(hwndMLE,
                        IDMSG_OVERWRITEFILE,
                        MB_QUERY | MB_YESNOCANCEL,
                        FALSE);


        if(sT == MBID_CANCEL)   /* if user cancels the New, then return */
            return;
#ifdef MLE_BUGS_FIXED
        else
            if(sT == MBID_YES)
                FileSave(NULL);
#endif
            /* if sT == MBID_NO, continue with New File processing */


    }

    /* disable redrawing of the MLE so the text doesn't "flash" when
        the MLE is cleared */
    WinSendMsg(hwndMLE, MLM_DISABLEREFRESH, NULL, NULL);

    /* clear the MLE by selecting all of the text and clearing it */
    WinSendMsg(hwndMLE,
               MLM_SETSEL,
               MPFROMSHORT(NULL),
               (MPARAM)WinSendMsg(hwndMLE, MLM_QUERYTEXTLENGTH, NULL, NULL));

    WinSendMsg(hwndMLE, MLM_CLEAR, NULL, NULL);

    /* reset the changed flag */
    WinSendMsg(hwndMLE, MLM_SETCHANGED, MPFROMSHORT((BOOL)FALSE), NULL);

    /* enable redrawing of the MLE */
    WinSendMsg(hwndMLE, MLM_ENABLEREFRESH, NULL, NULL);

    /* reset file name to NULL and update the main title bar*/
    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() */


#ifdef MLE_BUGS_FIXED


/****************************************************************\
 *  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 */
{
    FILEDLG fdg;
    HFILE hfIn;
    ULONG ulAction;
    FILESTATUS fstsInfo;
    PVOID pvBuf;
    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 = IDD_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,
                    &hfIn,
                    &ulAction,
                    0L,
                    FILE_NORMAL,
                    FILE_OPEN,
                    OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE,
                    0L))  {

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

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

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

        /* get the length of the file */
        if(DosQueryFileInfo(hfIn,
                            1,
                            (PVOID)&fstsInfo,
                            sizeof(FILESTATUS)))  {
            MessageBox(hwndMain,
                       IDMSG_CANNOTGETFILEINFO,
                       MB_OK | MB_ERROR,
                       FALSE);

            DosClose(hfIn);
            return;
        }


        /* allocate a buffer for the file */
        if(DosAllocMem((PPVOID)&pvBuf,
                       (ULONG)fstsInfo.cbFileAlloc,
                        fALLOC))  {
            MessageBox(hwndMain,
                       IDMSG_CANNOTALLOCATEMEMORY,
                       MB_OK | MB_ERROR,
                       FALSE);

            DosClose(hfIn);
            return;
        }

        /* read in the file */
        if(DosRead(hfIn, pvBuf, fstsInfo.cbFileAlloc, &ulAction))  {
            MessageBox(hwndMain,
                       IDMSG_CANNOTREADFILE,
                       MB_OK | MB_ERROR,
                       FALSE);

            DosClose(hfIn);
            return;
        }

        /* set the file into the MLE */
        WinSendMsg(hwndMLE,
                   MLM_SETIMPORTEXPORT,
                   MPFROMP((PBYTE)pvBuf),
                   MPFROMSHORT(fstsInfo.cbFileAlloc));

        WinSendMsg(hwndMLE,
                   MLM_IMPORT,
                   MPFROMP(NULL),
                   MPFROMSHORT(fstsInfo.cbFileAlloc));

        /* reset the changed flag */
        WinSendMsg(hwndMLE, MLM_SETCHANGED, MPFROMSHORT((BOOL)FALSE), NULL);

        DosFreeMem(pvBuf);

        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 calls the application's save routine
 *
 *  Returns:
 *
\****************************************************************/
VOID FileSave(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message */
{
    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,
                &hf,
                &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 calls the application's Save As routine
 *
 *  Returns:
 *
\****************************************************************/
VOID FileSaveAs(mp2)
MPARAM mp2;     /* second parameter of WM_COMMAND message */
{
    HFILE hf;
    ULONG ulAction;
    SHORT sT;

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

        /* if no file name, then get a file name */
        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(hwndMLE,
                            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() */

#endif

#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 */
{

    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 */
{

    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:   FilePageSetup(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 */
{

    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 */
{

    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() */

#ifdef MLE_BUGS_FIXED

/****************************************************************\
 *  Write file routine
 *--------------------------------------------------------------
 *
 *  Name:   WriteFileToDisk(hf)
 *
 *  Purpose: Writes the current file to the file in szFileName
 *
 *  Usage:  called from FileSave and FileSaveAs when a file
 *          is to be saved to disk
 *
 *  Method:  Routine uses the file handle specified and gets
 *           the text from the MLE and writes the text to the
 *           file.
 *
 *  Returns:
 *
\****************************************************************/
VOID WriteFileToDisk(hf)
HFILE hf;       /* handle to the input file */
{
    ULONG ulWrite;
    PVOID pvBuf;
    ULONG ulFileLen;

    /* get the length of the file */
    ulFileLen = (ULONG)WinSendMsg(hwndMLE, MLM_QUERYTEXTLENGTH, NULL, NULL);
    if(!ulFileLen)
        return;

    /* allocate a buffer for the file */
    if(DosAllocMem((PPVOID) &pvBuf, ulFileLen, fALLOC))  {
        MessageBox(hwndMLE,
                   IDMSG_CANNOTALLOCATEMEMORY,
                   MB_OK | MB_ERROR,
                   FALSE);
        return;
    }


    /* get the file from the MLE */
    WinSendMsg(hwndMLE,
               MLM_SETIMPORTEXPORT,
               MPFROMP((PBYTE)pvBuf),
               MPFROMLONG(ulFileLen));

    WinSendMsg(hwndMLE,
               MLM_EXPORT,
               MPFROMP(NULL),
               MPFROMLONG(&ulFileLen));

    /* Write the file */
    if(DosWrite(hf, pvBuf, ulFileLen, &ulWrite))  {
        MessageBox(hwndMLE,
                   IDMSG_CANNOTWRITETOFILE,
                   MB_OK | MB_ERROR,
                   FALSE);
        return;
    }

    /* reset the changed flag */
    WinSendMsg(hwndMLE, MLM_SETCHANGED, MPFROMSHORT((BOOL)FALSE), NULL);

    DosFreeMem(pvBuf);


}   /* WriteFileToDisk() */

/****************************************************************\
 *  Get file name routine
 *--------------------------------------------------------------
 *
 *  Name:   GetFileName()
 *
 *  Purpose: Gets the name of the save file.
 *
 *  Usage:  called when the user is 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 successfull 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 = IDD_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(hwndMLE, (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() */

#endif  /* MLE_BUGS_FIXED */


/****************************************************************\
 *  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.