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

  1. /**************************************************************************
  2.  *  File name  :  img_file.c
  3.  *
  4.  *  Description:  This module contains the code for handling the functions
  5.  *                offered on the File submenu. These functions are called
  6.  *                from the MainCommand() routine.
  7.  *
  8.  *                FileOpen(mp2)
  9.  *                FileLoadImage()
  10.  *
  11.  *  Concepts   :  file input and output
  12.  *
  13.  *  API's      :  WinLoadString
  14.  *                WinFileDlg
  15.  *                WinSetPointer
  16.  *                WinSendMsg
  17.  *                WinInvalidateRect
  18.  *                DosRead
  19.  *                DosClose
  20.  *                WinIsChild
  21.  *                WinSetParent
  22.  *                WinDefFileDlgProc
  23.  *
  24.  *  Required
  25.  *    Files    :  OS2.H, STRING.H, STDLIB.H, IMG_MAIN.H, IMG_XTRN.H,
  26.  *                IMG_HELP.H
  27.  *
  28.  *  Copyright (C) 1991 IBM Corporation
  29.  *
  30.  *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  31.  *      sample code created by IBM Corporation. This sample code is not
  32.  *      part of any standard or IBM product and is provided to you solely
  33.  *      for  the purpose of assisting you in the development of your
  34.  *      applications.  The code is provided "AS IS", without
  35.  *      warranty of any kind.  IBM shall not be liable for any damages
  36.  *      arising out of your use of the sample code, even if they have been
  37.  *      advised of the possibility of such damages.                                                    *
  38.  *************************************************************************/
  39. /*
  40.  *  Include files, macros, defined constants, and externs
  41.  */
  42. #define INCL_WINSCROLLBARS
  43. #define INCL_WINFRAMEMGR
  44. #define INCL_WINPOINTERS
  45. #define INCL_WINSTDFILE
  46. #include <os2.h>
  47. #include <string.h>
  48. #include <stdlib.h>
  49.  
  50. #include "img_main.h"
  51. #include "img_help.h"
  52. #include "img_xtrn.h"
  53.  
  54. /*
  55.  *  Static Variables & local defines
  56.  */
  57. #define CCHEXTENSION 6
  58. #define MAX_IMAGE_SIZE 0xFFFF
  59.  
  60. /* fully-qualified filename from open dialog */
  61. CHAR szFullFile[CCHMAXPATH] = {0};
  62.  
  63. /*
  64.  *  Entry point declarations
  65.  */
  66. BOOL             FileLoadImage(VOID);
  67. MRESULT EXPENTRY ImageOpenFilterProc(HWND hwnd, ULONG message,
  68.                   MPARAM mp1, MPARAM mp2 );
  69. #ifdef PORT_32
  70. /**************************************************************************
  71.  *
  72.  *  Name       : FileOpen(mp2)
  73.  *
  74.  *  Description: Processes the File menu's Open item.
  75.  *
  76.  *  Concepts   : Called whenever New from the File menu is selected.
  77.  *               Calls the standard file open dialog to get the
  78.  *               file name.  The file name is passed onto DosOpen
  79.  *               which returns the handle to the file.  The file
  80.  *               input procedure is called and then the file handle
  81.  *               is closed.
  82.  *
  83.  *  API's      : WinLoadString
  84.  *               WinFileDlg
  85.  *               WinSetPointer
  86.  *               WinSendMsg
  87.  *               WinInvalidateRect
  88.  *
  89.  *  Parameters : mp2  = message parameter 2
  90.  *
  91.  *  Return     : TRUE  - if file successfully loaded
  92.  *               FALSE - if it failed to load the file
  93.  *
  94.  *************************************************************************/
  95. BOOL FileOpen(MPARAM mp2)
  96. {
  97.     BOOL fSuccess = FALSE;
  98.     PCH  pch;
  99.     CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];
  100.     CHAR szExtension[CCHEXTENSION];                       /* e.g.: *.IMG */
  101.  
  102.    /*
  103.     * set up structure to pass to standard file dialog
  104.     */
  105.     vfdg.cbSize = sizeof(FILEDLG);
  106.     vfdg.fl = FDS_CENTER | FDS_HELPBUTTON | FDS_OPEN_DIALOG;
  107.  
  108.     if (!WinLoadString(vhab,
  109.                        0,
  110.                        IDS_OPEN,
  111.                        MESSAGELEN,
  112.                        szTitle))
  113.     {
  114.        MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0, MB_OK | MB_ERROR,
  115.                   TRUE);
  116.        return FALSE;
  117.     }
  118.     vfdg.pszTitle = szTitle;                            /* dialog title */
  119.  
  120.     if (!WinLoadString(vhab,
  121.                        (HMODULE)0,
  122.                        IDS_OPEN,
  123.                        MESSAGELEN,
  124.                        szButton))
  125.     {
  126.         MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0, MB_OK | MB_ERROR,
  127.                    TRUE);
  128.         return FALSE;
  129.     }
  130.     vfdg.pszOKButton = szButton;                 /* <Enter> button text */
  131.  
  132.     vfdg.ulUser = 0L;
  133.     vfdg.pfnDlgProc = ImageOpenFilterProc;
  134.     vfdg.lReturn = 0L;
  135.     vfdg.lSRC = 0L;
  136.     vfdg.hMod = (HMODULE)NULL;
  137.     vfdg.usDlgId = FILEOPEN;
  138.     vfdg.x = (SHORT)0;
  139.     vfdg.y = (SHORT)0;
  140.  
  141.     /* set up EA type list & initial type */
  142.     vfdg.pszIType = (PSZ)NULL;
  143.     vfdg.papszITypeList = (PAPSZ)NULL;
  144.  
  145.     /* set up drive list & initial drive */
  146.     vfdg.pszIDrive = (PSZ)NULL;
  147.     vfdg.papszIDriveList = (PAPSZ)NULL;
  148.     vfdg.sEAType = 0;
  149.     vfdg.papszFQFilename = (PAPSZ)NULL;
  150.     vfdg.ulFQFCount = 0L;
  151.  
  152.    /*
  153.     * get the default file extension (i.e. *.IMG)
  154.     */
  155.     if (!WinLoadString(vhab, 0, IDS_FILEOPENEXTENSION,
  156.                        CCHEXTENSION, szExtension))
  157.     {
  158.         MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0,
  159.                    MB_OK | MB_ERROR, TRUE);
  160.         return FALSE;
  161.     }
  162.  
  163.    /*
  164.     * if dialog has been previously invoked, then ensure the
  165.     * dialog is brought with the last user-selected directory
  166.     */
  167.     if (*szFullFile)
  168.     {
  169.         pch = strrchr(szFullFile, '\\');
  170.         strcpy(++pch, szExtension);
  171.         strcpy(vfdg.szFullFile, szFullFile);
  172.     }
  173.     else
  174.         strcpy(vfdg.szFullFile, szExtension);
  175.  
  176.    /*
  177.     * Invoke the standard file dialog and get a file.
  178.     */
  179.     if (!WinFileDlg(HWND_DESKTOP, vhwndFrame, (PFILEDLG)&vfdg))
  180.     {
  181.         MessageBox(vhwndFrame, IDMSG_CANNOTRUNFILEOPEN, 0,
  182.                             MB_OK | MB_ERROR, TRUE);
  183.         return FALSE;
  184.     }
  185.     WinInvalidateRect(vhwndClient, (PRECTL)NULL, FALSE);
  186.  
  187.    /*
  188.     *  Upon successful return of a file, open it for reading.
  189.     */
  190.     if (vfdg.lReturn == DID_OK)
  191.     {
  192.         strcpy(szFullFile, vfdg.szFullFile);
  193.  
  194.        /*
  195.         * attempt to load selected image
  196.         */
  197.         WinSetPointer(HWND_DESKTOP, vhptrWait);
  198.         fSuccess = FileLoadImage();
  199.         WinSetPointer(HWND_DESKTOP, vhptrArrow);
  200.  
  201.        /*
  202.         * Report error if image cannot be loaded.
  203.         */
  204.         if (!fSuccess)
  205.         {
  206.             MessageBox(vhwndFrame, IDMSG_CANNOTOPENINPUTFILE, 0,
  207.                        MB_OK | MB_ERROR, FALSE);
  208.             return FALSE;
  209.         }
  210.  
  211.         /* Update title with new filename.     */
  212.         UtilUpdateTitleText(vhab, vhwndFrame, szFullFile);
  213.  
  214.         /* reset scrollbars */
  215.         WinSendMsg(vhwndVScroll, SBM_SETPOS, (MPARAM)NULL, (MPARAM)NULL);
  216.         WinSendMsg(vhwndHScroll, SBM_SETPOS, (MPARAM)NULL, (MPARAM)NULL);
  217.  
  218.         WinInvalidateRect(vhwndClient, (PRECTL)NULL, FALSE);
  219.  
  220.        /* This routine currently doesn't use the mp2 parameter but       *\
  221.         *  it is referenced here to prevent an 'Unreferenced Parameter'
  222.        \*  warning at compile time.                                      */
  223.         mp2;
  224.     }
  225. }   /*  End of FileOpen */
  226.  
  227. /**************************************************************************
  228.  *
  229.  *  Name       : FileLoadImage()
  230.  *
  231.  *  Description: Loads an image into storage
  232.  *
  233.  *  Concepts   : Routine is called whenever a WM_COMMAND message
  234.  *               is posted to the main window as a result of
  235.  *               selecting the 'Open...' item on the 'File'
  236.  *               pulldown.  It calls SizeCalculateMaxWindow() and
  237.  *               DrawImage().
  238.  *
  239.  *                - determine the size of the image to be loaded
  240.  *                - allocate allocation storage to contain the
  241.  *                  image
  242.  *                - load image into allocated storage
  243.  *
  244.  *  API's      : WinSendMsg
  245.  *               DosRead
  246.  *               DosClose
  247.  *               WinIsChild
  248.  *               WinSetParent
  249.  *
  250.  *  Parameters : [none]
  251.  *
  252.  *  Return     : TRUE  - if image successfully loaded
  253.  *               FALSE - if it failed to load the image
  254.  *
  255.  *************************************************************************/
  256. BOOL FileLoadImage(void)
  257. {
  258.    ULONG  ulBufSiz;   /* size of buffer required for the image */
  259.    HFILE  hfile;
  260. #if (defined(PORT_S132) || defined(PORT_32))
  261.    ULONG cByte;
  262. #else
  263.    USHORT cByte;
  264. #endif
  265.    BOOL fFailed = FALSE;
  266.    CHAR szTmp[10];        /* to hold image file header         */
  267.  
  268.    /*
  269.     * Locate the selected file and determine its size, so that enough
  270.     * storage can be allocated.
  271.     */
  272.    if (!(ulBufSiz = UtilFindFileSize(szFullFile)))
  273.       return FALSE;
  274.  
  275.    /*
  276.     * free any image storage buffer already in use
  277.     */
  278.    if (vfImgLoaded)
  279. #if (defined(PORT_16) || defined(PORT_S132))
  280.       UtilMemoryFree(SELECTOROF(vpbImgBuf));
  281. #else
  282.       UtilMemoryFree(vpbImgBuf);
  283. #endif
  284.     /* allocate memory for image */
  285.    if (!UtilMemoryAllocate((USHORT)ulBufSiz, &vpbImgBuf))
  286.       return FALSE;
  287.  
  288.    /*
  289.     * open the image-data file
  290.     */
  291.    if (!UtilGetFileHandle(szFullFile, &hfile))
  292.    {
  293.       vfDetail = FALSE;
  294.       vfImgLoaded = FALSE;
  295. #if (defined(PORT_16) || defined(PORT_S132))
  296.       UtilMemoryFree(SELECTOROF(vpbImgBuf));
  297. #else
  298.       UtilMemoryFree(vpbImgBuf);
  299. #endif
  300.       return FALSE;
  301.    }
  302.    /*
  303.     * read the image width and height values from the image-file header
  304.     * note: the image width is in bytes & the image height is in pels
  305.     */
  306.    DosRead(hfile, (PVOID)szTmp, 4L, &cByte);
  307.    szTmp[4] = '\0';
  308.    vsizlImg.cx = atol(szTmp);
  309.  
  310.    /* check for comma in header */
  311.    DosRead(hfile, (PVOID)szTmp, 1L, &cByte);
  312.    szTmp[1] = '\0';
  313.  
  314.    if (strcmp(szTmp, ","))
  315.        fFailed = TRUE;
  316.    else
  317.    {
  318.        /* read the image height field */
  319.        DosRead(hfile, (PVOID)szTmp, 4L, &cByte);
  320.        szTmp[4] = '\0';
  321.        vsizlImg.cy = atol(szTmp);
  322.    }
  323.  
  324.    /*
  325.     * if header not correct format, fail the load
  326.     */
  327.    if (fFailed || vsizlImg.cx == 0L || vsizlImg.cy == 0L)
  328.    {
  329.        DosClose(hfile);
  330.        vfDetail = FALSE;
  331.        vfImgLoaded = FALSE;
  332. #if (defined(PORT_16) || defined(PORT_S132))
  333.        UtilMemoryFree(SELECTOROF(vpbImgBuf));
  334. #else
  335.        UtilMemoryFree(vpbImgBuf);
  336. #endif
  337.        return FALSE;
  338.    }
  339.  
  340.    /*
  341.     * adjust the image height (if necessary) to keep within 64KB limit
  342.     */
  343.    if (ulBufSiz >= MAX_IMAGE_SIZE)
  344.       vsizlImg.cy = MAX_IMAGE_SIZE / (vsizlImg.cx + 3);
  345.  
  346.    /*
  347.     * convert the image-width value to pels
  348.     */
  349.    vsizlImg.cx *= 8;
  350.  
  351.    /*
  352.     * transfer the image data to application storage
  353.     */
  354.    DosRead(hfile, (PVOID)vpbImgBuf, ulBufSiz, &cByte);
  355.  
  356.    DosClose(hfile);
  357.  
  358.    /*
  359.     * Invoke the DrawImage function to define a bitmap containing the
  360.     * image.
  361.     */
  362.    PaintDrawImage();
  363.    vfDetail = FALSE;
  364.    vfImgLoaded = TRUE;
  365.  
  366.    /*
  367.     * As 'Non-Detail' mode is set, scrolling should be disabled. The scroll
  368.     * bars are made invisible by ensuring that they are no longer
  369.     * object windows.
  370.     */
  371.    if (WinIsChild(vhwndVScroll, vhwndFrame))
  372.    {
  373.        WinSetParent(vhwndVScroll, HWND_OBJECT, FALSE);
  374.        WinSetParent(vhwndHScroll, HWND_OBJECT, FALSE);
  375.        WinSendMsg(vhwndFrame,
  376.                   WM_UPDATEFRAME,
  377.                   MPFROMLONG(FCF_VERTSCROLL | FCF_HORZSCROLL),
  378.                   (MPARAM)NULL);
  379.    }
  380.    return TRUE;
  381.  
  382. }  /* End of FileLoadImage */
  383.  
  384. /**************************************************************************
  385.  *
  386.  *  Name       : ImageOpenFilterProc
  387.  *
  388.  *  Description: Filters all help messages to the open dialog
  389.  *
  390.  *  Concepts   : Needed to display help from the Open dialog
  391.  *
  392.  *  API's      : WinDefFileDlgProc
  393.  *
  394.  *  Parameters : hwnd   = window handle
  395.  *               msg    = message code
  396.  *               mp1    = first message parameter
  397.  *               mp2    = second message parameter
  398.  *
  399.  *  Return     : same return as WinDefFileDlgProc()
  400.  *
  401.  *************************************************************************/
  402. MRESULT EXPENTRY ImageOpenFilterProc(HWND hwnd,
  403.                                      ULONG message,
  404.                                      MPARAM mp1,
  405.                                      MPARAM mp2 )
  406. {
  407.    if(message == WM_HELP)
  408.    {
  409.        DisplayHelpPanel(HID_FS_OPEN_DLG_HELP_PANEL);
  410.        return (MRESULT)FALSE ;
  411.    }
  412.    return WinDefFileDlgProc( hwnd, message, mp1, mp2 ) ;
  413. } /* End of ImageOpenFilterProc  */
  414. #endif  /*  PORT_32  */
  415. #ifdef PORT_16  /*  This is a dummy function for the 16-bit compile.  */
  416. BOOL FileOpen(MPARAM mp2)
  417. {
  418.    return FALSE;
  419.    mp2;
  420. }  /* End of dummy FileOpen  */
  421. #endif
  422. /***************************  End of img_file.c  *************************/
  423.