File:  [OS/2 SDKs] / os232sdk / toolkt20 / c / samples / image / img_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

/*==============================================================*\
 *  img_file.c - routines for handling the File menu functions
 *      Created 1989, 1990 IBM, Microsoft Corp.
 *--------------------------------------------------------------*
 *
 *  This module contains the code for handling the functions
 *  offered on the File submenu. These functions are called
 *  from the MainCommand() routine.
 *
 *--------------------------------------------------------------*
 *
 *  This source file contains the following functions:
 *
 *      FileOpen(mp2)
 *      FileExit(mp2)
 *      FileLoadImage()
 *
\*==============================================================*/
/*--------------------------------------------------------------*\
 *  Include files, macros, defined constants, and externs       *
\*--------------------------------------------------------------*/
#define INCL_WINSCROLLBARS
#define INCL_WINFRAMEMGR
#define INCL_WINPOINTERS
#define INCL_WINSTDFILE
#include <os2.h>
#include <string.h>
#include <stdlib.h>
#include "img_main.h"
#include "img_xtrn.h"

/*--------------------------------------------------------------*\
 *  Static Variables & local defines                            *
\*--------------------------------------------------------------*/
#define CCHEXTENSION 6

/* fully qualified filename from open dialog */
CHAR szFullFile[CCHMAXPATH] = {0};

/* list of EA type strings */
PSZ apszITL[]={"Image", (PSZ)NULL};

/*--------------------------------------------------------------*\
 *  Entry point declarations                                    *
\*--------------------------------------------------------------*/
BOOL FileLoadImage(VOID);

/****************************************************************\
 *  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:
 *
\****************************************************************/
BOOL FileOpen(mp2)
MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
{

    BOOL fSuccess = FALSE;
    PCH  pch;
    FILEDLG fdg;
    CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];
    CHAR szExtension[CCHEXTENSION];                       /* eg *.IMG0 */

   /*
    * setup structure to pass to standard file dialog
    */
    fdg.cbsize = sizeof(FILEDLG);
    fdg.usDialogType = OPEN_DIALOG;         /* open as opposed to save */
    fdg.fl = FDS_CENTER | FDS_HELPBUTTON;

    if (!WinLoadString(vhab,
                       NULL,
                       IDS_OPENDLGHEADER,
                       MESSAGELEN,
                       szTitle)) {
        MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0,
                   MB_OK | MB_ERROR, TRUE);
        return FALSE;
    }
    fdg.pszTitle = szTitle;                            /* dialog title */

    if (!WinLoadString(vhab,
                       NULL,
                       IDS_OPENDLGBUTTON,
                       MESSAGELEN,
                       szButton)) {
        MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0,
                   MB_OK | MB_ERROR, TRUE);
        return FALSE;
    }
    fdg.pszOKButton = szButton;                 /* <ENTER> button text */

    fdg.lUser = 0L;
    fdg.pfnDlgProc = NULL;                           /* non-customised */
    fdg.lReturn = 0L;
    fdg.lSRC = NULL;
    fdg.hmod = NULL;
    fdg.idDlg = 0;
    fdg.x = 0;
    fdg.y = 0;

    /* setup EA type list & initial type */
    fdg.pszIType = (PSZ)NULL;
    fdg.ppszITypeList = (PAPSZ)apszITL;

    /* setup drive list & initial drive */
    fdg.pszIDrive = (PSZ)NULL;
    fdg.ppszIDriveList = (PAPSZ)NULL;

   /*
    * get the default file extension (ie *.IMG)
    */
    if (!WinLoadString(vhab, NULL, IDS_FILEOPENEXTENSION,
                       CCHEXTENSION, szExtension))  {
        MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0,
                   MB_OK | MB_ERROR, TRUE);
        return FALSE;
    }

   /*
    * if dialog has been previously invoked, then ensure the
    * dialog is brought with the last user-selected directory
    */
    if (*szFullFile) {
        pch = strrchr(szFullFile, '\\');
        strcpy(++pch, szExtension);
        strcpy(fdg.szFullFile, szFullFile);
    } else
        strcpy(fdg.szFullFile, szExtension);

    fdg.sEAType = -1;

   /*
    * invoke the standard file dialog and get a file
    */
    if (!KitFileDlg(vhwndFrame, (PFILEDLG)&fdg)) {
        MessageBox(vhwndFrame, IDMSG_CANNOTRUNFILEOPEN, 0,
                   MB_OK | MB_ERROR, TRUE);
        return FALSE;
    }

   /*
    *  Upon sucessful return of a file, open it for reading
    */
    if (fdg.lReturn == DID_OK)  {
        strcpy(szFullFile, fdg.szFullFile);

       /*
        * attempt to load selected image
        */
        WinSetPointer(HWND_DESKTOP, vhptrWait);
        fSuccess = FileLoadImage();
        WinSetPointer(HWND_DESKTOP, vhptrArrow);

       /*
        * report error if image cannot be loaded
        */
        if (!fSuccess) {
            MessageBox(vhwndFrame, IDMSG_CANNOTOPENINPUTFILE, 0,
                       MB_OK | MB_ERROR, FALSE);
            return FALSE;
        }

        /* update title with new filename */
        UtilUpdateTitleText(vhab, vhwndFrame, szFullFile);

        /* reset scrollbars */
        WinSendMsg(vhwndVScroll, SBM_SETPOS, 0L, 0L);
        WinSendMsg(vhwndHScroll, SBM_SETPOS, 0L, 0L);

        WinInvalidateRect(vhwndClient, (PRECTL)NULL, FALSE);

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

/****************************************************************\
 *  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(vhwndClient, WM_CLOSE, 0L, 0L);

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

/****************************************************************\
 *  Load Image Routine                                          *
 *--------------------------------------------------------------*
 *                                                              *
 *  Name:    FileLoadImage()                                    *
 *                                                              *
 *  Purpose: To load an image into storage                      *
 *                                                              *
 *  Usage:  Routine is called whenever a WM_COMMAND message     *
 *          is posted to the main window as a result of         *
 *          selecting the 'Open...' item on the 'File'          *
 *          pulldown. It calls SizeCalculateMaxWindow() and     *
 *          DrawImage()                                         *
 *                                                              *
 *  Method:                                                     *
 *          - determine the size of the image to be loaded      *
 *          - allocate allocation storage to contain the        *
 *            image                                             *
 *          - load image into allocated storage                 *
 *                                                              *
 *  Returns:                                                    *
 *           TRUE  - if image loaded successfully               *
 *           FALSE - if image failed to load                    *
\****************************************************************/

BOOL FileLoadImage()
{
    ULONG  ulBufSiz;   /* size of buffer required for the image */
    HFILE  hfile;
#if (defined(PORT_S132) || defined(PORT_32))
    ULONG cByte;
#else
    USHORT cByte;
#endif
    BOOL fFailed = FALSE;
    CHAR szTmp[10];        /* to hold image file header         */

   /*
    * Locate the selected file and determine its size, so that enough
    * storage can be allocated.
    */
    if (!(ulBufSiz = UtilFindFileSize(szFullFile)))
        return FALSE;

   /*
    * free any image storage buffer already in use
    */
    if (vfImgLoaded)
#if (defined(PORT_16) || defined(PORT_S132))
        UtilMemoryFree(SELECTOROF(vpbImgBuf));
#else
        UtilMemoryFree(vpbImgBuf);
#endif
    /* allocate memory for image */
    if (!UtilMemoryAllocate((USHORT)ulBufSiz, &vpbImgBuf))
        return FALSE;

   /*
    * open the image-data file
    */
    if (!UtilGetFileHandle(szFullFile, &hfile)) {
        vfDetail = FALSE;
        vfImgLoaded = FALSE;
#if (defined(PORT_16) || defined(PORT_S132))
        UtilMemoryFree(SELECTOROF(vpbImgBuf));
#else
        UtilMemoryFree(vpbImgBuf);
#endif
        return FALSE;
    }
   /*
    * read the image width and height values from the image-file header
    * note: the image width is in bytes & the image height is in pels
    */
    DosRead(hfile,
           (PVOID)szTmp,
           (ULONG)4,
           &cByte);
    szTmp[4] = '\0';
    vsizlImg.cx = atol(szTmp);

    /* check for comma in header */
    DosRead(hfile,
            (PVOID)szTmp,
            (ULONG)1,
            &cByte);
    szTmp[1] = '\0';

    if (strcmp(szTmp, ","))
        fFailed = TRUE;
    else {

        /* read the image height field */
        DosRead(hfile,
                (PVOID)szTmp,
                (ULONG)4,
                &cByte);
        szTmp[4] = '\0';
        vsizlImg.cy = atol(szTmp);
    }

   /*
    * if header not correct format, fail the load
    */
    if (fFailed || vsizlImg.cx == 0L || vsizlImg.cy == 0L) {
        DosClose((USHORT)hfile);
        vfDetail = FALSE;
        vfImgLoaded = FALSE;
#if (defined(PORT_16) || defined(PORT_S132))
        UtilMemoryFree(SELECTOROF(vpbImgBuf));
#else
        UtilMemoryFree(vpbImgBuf);
#endif
        return FALSE;
    }

   /*
    * adjust the image height (if necessary) to keep within 64KB limit
    */
    if (ulBufSiz == 0xFFFF)
        vsizlImg.cy = ulBufSiz / vsizlImg.cx;

   /*
    * convert the image-width value to pels
    */
    vsizlImg.cx *= 8;

   /*
    * transfer the image data to application storage
    */
    DosRead(hfile,
            (PVOID)vpbImgBuf,
            (USHORT)ulBufSiz,
            &cByte);

    DosClose(hfile);

   /*
    * Invoke the DrawImage function to define a bitmap containing the
    * image.
    */
    PaintDrawImage();
    vfDetail = FALSE;
    vfImgLoaded = TRUE;

   /*
    * As 'Non-Detail' mode is set, scrolling should be disabled. The scroll
    * bars are made invisible by ensuring that they are no longer
    * object windows.
    */
    if (WinIsChild(vhwndVScroll, vhwndFrame)) {
        WinSetParent(vhwndVScroll, HWND_OBJECT, FALSE);
        WinSetParent(vhwndHScroll, HWND_OBJECT, FALSE);
        WinSendMsg(vhwndFrame,
                   WM_UPDATEFRAME,
                   MPFROMLONG(FCF_VERTSCROLL | FCF_HORZSCROLL),
                   0L);
    }
    return TRUE;

}   /* FileLoadImage() */


unix.superglobalmegacorp.com

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