Annotation of os232sdk/toolkt20/c/samples/image/img_file.c, revision 1.1.1.1

1.1       root        1: /*==============================================================*\
                      2:  *  img_file.c - routines for handling the File menu functions
                      3:  *      Created 1989, 1990 IBM, Microsoft Corp.
                      4:  *--------------------------------------------------------------*
                      5:  *
                      6:  *  This module contains the code for handling the functions
                      7:  *  offered on the File submenu. These functions are called
                      8:  *  from the MainCommand() routine.
                      9:  *
                     10:  *--------------------------------------------------------------*
                     11:  *
                     12:  *  This source file contains the following functions:
                     13:  *
                     14:  *      FileOpen(mp2)
                     15:  *      FileExit(mp2)
                     16:  *      FileLoadImage()
                     17:  *
                     18: \*==============================================================*/
                     19: /*--------------------------------------------------------------*\
                     20:  *  Include files, macros, defined constants, and externs       *
                     21: \*--------------------------------------------------------------*/
                     22: #define INCL_WINSCROLLBARS
                     23: #define INCL_WINFRAMEMGR
                     24: #define INCL_WINPOINTERS
                     25: #define INCL_WINSTDFILE
                     26: #include <os2.h>
                     27: #include <string.h>
                     28: #include <stdlib.h>
                     29: #include "img_main.h"
                     30: #include "img_xtrn.h"
                     31: 
                     32: /*--------------------------------------------------------------*\
                     33:  *  Static Variables & local defines                            *
                     34: \*--------------------------------------------------------------*/
                     35: #define CCHEXTENSION 6
                     36: 
                     37: /* fully qualified filename from open dialog */
                     38: CHAR szFullFile[CCHMAXPATH] = {0};
                     39: 
                     40: /* list of EA type strings */
                     41: PSZ apszITL[]={"Image", (PSZ)NULL};
                     42: 
                     43: /*--------------------------------------------------------------*\
                     44:  *  Entry point declarations                                    *
                     45: \*--------------------------------------------------------------*/
                     46: BOOL FileLoadImage(VOID);
                     47: 
                     48: /****************************************************************\
                     49:  *  Open file routine
                     50:  *--------------------------------------------------------------
                     51:  *
                     52:  *  Name:   FileOpen(mp2)
                     53:  *
                     54:  *  Purpose: Processes the File menu's Open item.
                     55:  *
                     56:  *  Usage:  called whenever New from the File menu is selected
                     57:  *
                     58:  *  Method:  calls the standard file open dialog to get the
                     59:  *          file name.  The file name is passed onto DosOpen
                     60:  *          which returns the handle to the file.  The file
                     61:  *          input procedure is called and then the file handle
                     62:  *          is closed.
                     63:  *
                     64:  *  Returns:
                     65:  *
                     66: \****************************************************************/
                     67: BOOL FileOpen(mp2)
                     68: MPARAM mp2; /* second parameter of WM_COMMAND message sent by menu */
                     69: {
                     70: 
                     71:     BOOL fSuccess = FALSE;
                     72:     PCH  pch;
                     73:     FILEDLG fdg;
                     74:     CHAR szTitle[MESSAGELEN], szButton[MESSAGELEN];
                     75:     CHAR szExtension[CCHEXTENSION];                       /* eg *.IMG0 */
                     76: 
                     77:    /*
                     78:     * setup structure to pass to standard file dialog
                     79:     */
                     80:     fdg.cbsize = sizeof(FILEDLG);
                     81:     fdg.usDialogType = OPEN_DIALOG;         /* open as opposed to save */
                     82:     fdg.fl = FDS_CENTER | FDS_HELPBUTTON;
                     83: 
                     84:     if (!WinLoadString(vhab,
                     85:                        NULL,
                     86:                        IDS_OPENDLGHEADER,
                     87:                        MESSAGELEN,
                     88:                        szTitle)) {
                     89:         MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0,
                     90:                    MB_OK | MB_ERROR, TRUE);
                     91:         return FALSE;
                     92:     }
                     93:     fdg.pszTitle = szTitle;                            /* dialog title */
                     94: 
                     95:     if (!WinLoadString(vhab,
                     96:                        NULL,
                     97:                        IDS_OPENDLGBUTTON,
                     98:                        MESSAGELEN,
                     99:                        szButton)) {
                    100:         MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0,
                    101:                    MB_OK | MB_ERROR, TRUE);
                    102:         return FALSE;
                    103:     }
                    104:     fdg.pszOKButton = szButton;                 /* <ENTER> button text */
                    105: 
                    106:     fdg.lUser = 0L;
                    107:     fdg.pfnDlgProc = NULL;                           /* non-customised */
                    108:     fdg.lReturn = 0L;
                    109:     fdg.lSRC = NULL;
                    110:     fdg.hmod = NULL;
                    111:     fdg.idDlg = 0;
                    112:     fdg.x = 0;
                    113:     fdg.y = 0;
                    114: 
                    115:     /* setup EA type list & initial type */
                    116:     fdg.pszIType = (PSZ)NULL;
                    117:     fdg.ppszITypeList = (PAPSZ)apszITL;
                    118: 
                    119:     /* setup drive list & initial drive */
                    120:     fdg.pszIDrive = (PSZ)NULL;
                    121:     fdg.ppszIDriveList = (PAPSZ)NULL;
                    122: 
                    123:    /*
                    124:     * get the default file extension (ie *.IMG)
                    125:     */
                    126:     if (!WinLoadString(vhab, NULL, IDS_FILEOPENEXTENSION,
                    127:                        CCHEXTENSION, szExtension))  {
                    128:         MessageBox(vhwndFrame, IDMSG_CANNOTLOADSTRING, 0,
                    129:                    MB_OK | MB_ERROR, TRUE);
                    130:         return FALSE;
                    131:     }
                    132: 
                    133:    /*
                    134:     * if dialog has been previously invoked, then ensure the
                    135:     * dialog is brought with the last user-selected directory
                    136:     */
                    137:     if (*szFullFile) {
                    138:         pch = strrchr(szFullFile, '\\');
                    139:         strcpy(++pch, szExtension);
                    140:         strcpy(fdg.szFullFile, szFullFile);
                    141:     } else
                    142:         strcpy(fdg.szFullFile, szExtension);
                    143: 
                    144:     fdg.sEAType = -1;
                    145: 
                    146:    /*
                    147:     * invoke the standard file dialog and get a file
                    148:     */
                    149:     if (!KitFileDlg(vhwndFrame, (PFILEDLG)&fdg)) {
                    150:         MessageBox(vhwndFrame, IDMSG_CANNOTRUNFILEOPEN, 0,
                    151:                    MB_OK | MB_ERROR, TRUE);
                    152:         return FALSE;
                    153:     }
                    154: 
                    155:    /*
                    156:     *  Upon sucessful return of a file, open it for reading
                    157:     */
                    158:     if (fdg.lReturn == DID_OK)  {
                    159:         strcpy(szFullFile, fdg.szFullFile);
                    160: 
                    161:        /*
                    162:         * attempt to load selected image
                    163:         */
                    164:         WinSetPointer(HWND_DESKTOP, vhptrWait);
                    165:         fSuccess = FileLoadImage();
                    166:         WinSetPointer(HWND_DESKTOP, vhptrArrow);
                    167: 
                    168:        /*
                    169:         * report error if image cannot be loaded
                    170:         */
                    171:         if (!fSuccess) {
                    172:             MessageBox(vhwndFrame, IDMSG_CANNOTOPENINPUTFILE, 0,
                    173:                        MB_OK | MB_ERROR, FALSE);
                    174:             return FALSE;
                    175:         }
                    176: 
                    177:         /* update title with new filename */
                    178:         UtilUpdateTitleText(vhab, vhwndFrame, szFullFile);
                    179: 
                    180:         /* reset scrollbars */
                    181:         WinSendMsg(vhwndVScroll, SBM_SETPOS, 0L, 0L);
                    182:         WinSendMsg(vhwndHScroll, SBM_SETPOS, 0L, 0L);
                    183: 
                    184:         WinInvalidateRect(vhwndClient, (PRECTL)NULL, FALSE);
                    185: 
                    186:        /* This routine currently doesn't use the mp2 parameter but       *\
                    187:         *  it is referenced here to prevent an 'Unreferenced Parameter'
                    188:        \*  warning at compile time.                                      */
                    189:         mp2;
                    190:     }
                    191: 
                    192: }   /* FileOpen() */
                    193: 
                    194: /****************************************************************\
                    195:  *  Exit routine
                    196:  *--------------------------------------------------------------
                    197:  *
                    198:  *  Name:   FileExit(mp2)
                    199:  *
                    200:  *  Purpose: Processes the File menu's Exit item.
                    201:  *
                    202:  *  Usage:  called whenever Exit from the file menu is
                    203:  *          selected
                    204:  *
                    205:  *  Method:  Routine posts a WM_CLOSE message to the main
                    206:  *           application window.
                    207:  *
                    208:  *  Returns:
                    209:  *
                    210: \****************************************************************/
                    211: VOID FileExit(mp2)
                    212: MPARAM mp2;     /* second parameter of WM_COMMAND message sent by menu */
                    213: {
                    214: 
                    215:     WinPostMsg(vhwndClient, WM_CLOSE, 0L, 0L);
                    216: 
                    217:     /* This routine currently doesn't use the mp2 parameter but       *\
                    218:      *  it is referenced here to prevent an 'Unreferenced Parameter'
                    219:     \*  warning at compile time.                                      */
                    220:     mp2;
                    221: 
                    222: }   /* FileExit() */
                    223: 
                    224: /****************************************************************\
                    225:  *  Load Image Routine                                          *
                    226:  *--------------------------------------------------------------*
                    227:  *                                                              *
                    228:  *  Name:    FileLoadImage()                                    *
                    229:  *                                                              *
                    230:  *  Purpose: To load an image into storage                      *
                    231:  *                                                              *
                    232:  *  Usage:  Routine is called whenever a WM_COMMAND message     *
                    233:  *          is posted to the main window as a result of         *
                    234:  *          selecting the 'Open...' item on the 'File'          *
                    235:  *          pulldown. It calls SizeCalculateMaxWindow() and     *
                    236:  *          DrawImage()                                         *
                    237:  *                                                              *
                    238:  *  Method:                                                     *
                    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:  *  Returns:                                                    *
                    245:  *           TRUE  - if image loaded successfully               *
                    246:  *           FALSE - if image failed to load                    *
                    247: \****************************************************************/
                    248: 
                    249: BOOL FileLoadImage()
                    250: {
                    251:     ULONG  ulBufSiz;   /* size of buffer required for the image */
                    252:     HFILE  hfile;
                    253: #if (defined(PORT_S132) || defined(PORT_32))
                    254:     ULONG cByte;
                    255: #else
                    256:     USHORT cByte;
                    257: #endif
                    258:     BOOL fFailed = FALSE;
                    259:     CHAR szTmp[10];        /* to hold image file header         */
                    260: 
                    261:    /*
                    262:     * Locate the selected file and determine its size, so that enough
                    263:     * storage can be allocated.
                    264:     */
                    265:     if (!(ulBufSiz = UtilFindFileSize(szFullFile)))
                    266:         return FALSE;
                    267: 
                    268:    /*
                    269:     * free any image storage buffer already in use
                    270:     */
                    271:     if (vfImgLoaded)
                    272: #if (defined(PORT_16) || defined(PORT_S132))
                    273:         UtilMemoryFree(SELECTOROF(vpbImgBuf));
                    274: #else
                    275:         UtilMemoryFree(vpbImgBuf);
                    276: #endif
                    277:     /* allocate memory for image */
                    278:     if (!UtilMemoryAllocate((USHORT)ulBufSiz, &vpbImgBuf))
                    279:         return FALSE;
                    280: 
                    281:    /*
                    282:     * open the image-data file
                    283:     */
                    284:     if (!UtilGetFileHandle(szFullFile, &hfile)) {
                    285:         vfDetail = FALSE;
                    286:         vfImgLoaded = FALSE;
                    287: #if (defined(PORT_16) || defined(PORT_S132))
                    288:         UtilMemoryFree(SELECTOROF(vpbImgBuf));
                    289: #else
                    290:         UtilMemoryFree(vpbImgBuf);
                    291: #endif
                    292:         return FALSE;
                    293:     }
                    294:    /*
                    295:     * read the image width and height values from the image-file header
                    296:     * note: the image width is in bytes & the image height is in pels
                    297:     */
                    298:     DosRead(hfile,
                    299:            (PVOID)szTmp,
                    300:            (ULONG)4,
                    301:            &cByte);
                    302:     szTmp[4] = '\0';
                    303:     vsizlImg.cx = atol(szTmp);
                    304: 
                    305:     /* check for comma in header */
                    306:     DosRead(hfile,
                    307:             (PVOID)szTmp,
                    308:             (ULONG)1,
                    309:             &cByte);
                    310:     szTmp[1] = '\0';
                    311: 
                    312:     if (strcmp(szTmp, ","))
                    313:         fFailed = TRUE;
                    314:     else {
                    315: 
                    316:         /* read the image height field */
                    317:         DosRead(hfile,
                    318:                 (PVOID)szTmp,
                    319:                 (ULONG)4,
                    320:                 &cByte);
                    321:         szTmp[4] = '\0';
                    322:         vsizlImg.cy = atol(szTmp);
                    323:     }
                    324: 
                    325:    /*
                    326:     * if header not correct format, fail the load
                    327:     */
                    328:     if (fFailed || vsizlImg.cx == 0L || vsizlImg.cy == 0L) {
                    329:         DosClose((USHORT)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 == 0xFFFF)
                    344:         vsizlImg.cy = ulBufSiz / vsizlImg.cx;
                    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,
                    355:             (PVOID)vpbImgBuf,
                    356:             (USHORT)ulBufSiz,
                    357:             &cByte);
                    358: 
                    359:     DosClose(hfile);
                    360: 
                    361:    /*
                    362:     * Invoke the DrawImage function to define a bitmap containing the
                    363:     * image.
                    364:     */
                    365:     PaintDrawImage();
                    366:     vfDetail = FALSE;
                    367:     vfImgLoaded = TRUE;
                    368: 
                    369:    /*
                    370:     * As 'Non-Detail' mode is set, scrolling should be disabled. The scroll
                    371:     * bars are made invisible by ensuring that they are no longer
                    372:     * object windows.
                    373:     */
                    374:     if (WinIsChild(vhwndVScroll, vhwndFrame)) {
                    375:         WinSetParent(vhwndVScroll, HWND_OBJECT, FALSE);
                    376:         WinSetParent(vhwndHScroll, HWND_OBJECT, FALSE);
                    377:         WinSendMsg(vhwndFrame,
                    378:                    WM_UPDATEFRAME,
                    379:                    MPFROMLONG(FCF_VERTSCROLL | FCF_HORZSCROLL),
                    380:                    0L);
                    381:     }
                    382:     return TRUE;
                    383: 
                    384: }   /* FileLoadImage() */
                    385: 

unix.superglobalmegacorp.com

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