home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / FILEDLG6.ZIP / SOURCE.ZIP / SAVEDLG.C < prev    next >
Text File  |  1990-11-14  |  19KB  |  422 lines

  1. /****************************************************************************
  2.  * SAVEDLG.C - Save File dialog box routines.                               *
  3.  *                                                                          *
  4.  *  Modifications -                                                         *
  5.  *      21-Aug-1989 : Initial version.                                      *
  6.  *      08-Sep-1989 : Fixed failure to update current directory display to  *
  7.  *                    reflect drive/directory changes after an attempt to   *
  8.  *                    open a file.                                          *
  9.  *      21-Sep-1989 : Restored SAVE button as default button when the       *
  10.  *                    focus passes to a non-button control.                 *
  11.  *                    Returned focus to file name edit control after        *
  12.  *                    an error occurs while opening the file.               *
  13.  *      11-Oct-1989 : Changed to DLL version of ErrMessageBox function.     *
  14.  *      19-Nov-1989 : Fixed protection violation caused when default        *
  15.  *                    filename string is in a read-only segment.            *
  16.  *      11-Oct-1990 : Added long file name support.                         *
  17.  *                    Eliminated code to set SAVE button as default         *
  18.  *                    button when focus passes to file name edit control.   *
  19.  *      14-Nov-1990 : Disabled hard-error processing while executing        *
  20.  *                      FileSaveDlg function.                               *
  21.  *                                                                          *
  22.  * (c)Copyright 1990 Rick Yoder                                             *
  23.  ****************************************************************************/
  24.  
  25.     #define INCL_WIN
  26.     #define INCL_DOS
  27.     #define INCL_GPIBITMAPS
  28.     #include <os2.h>
  29.  
  30.     #include <string.h>
  31.     #include <io.h>
  32.     #include <errmsg.h>
  33.  
  34.     #include "filedlg.h"
  35.     #include "dialog.h"
  36.     #define INCL_ARROWS
  37.     #include "tools.h"
  38.     #include "static.h"
  39.  
  40. /****************************************************************************
  41.  *  Internal data structure definitions                                     *
  42.  ****************************************************************************/
  43.     typedef struct {
  44.         PSZ     pszTitle;       // dialog box title
  45.         PSZ     pszIns;         // dialog box instructions
  46.         void (EXPENTRY *pfnHelpProc)(HWND hDlg); // ptr to help procedure
  47.         PSZ     pszFile;        // ptr to name of opened file
  48.         PHFILE  phf;            // ptr to file handle
  49.         ULONG   ulFileSize;     // initial file size
  50.         PUSHORT pusAction;      // action taken on open
  51.         USHORT  usAttribute;    // file attribute
  52.         USHORT  fsOpenFlags;    // open flags
  53.         USHORT  fsOpenMode;     // open mode
  54.         ULONG   ulReserved;     // reserved
  55.         USHORT  usMaxPathLen;   // maximum path name length
  56.         PSZ     pszScratch;     // ptr to scratch data area
  57.         PSZ     pszCurDir;          // pointer to current directory string.
  58.         USHORT  iFirstChar;         // index of first character of current
  59.                                     //  directory string that is to be displayed.
  60.         HBITMAP hbmLeft;            // Left arrow bitmap handle.
  61.         HBITMAP hbmLeftPressed;     // Pressed left arrow bitmap handle.
  62.         HBITMAP hbmLeftDisabled;    // Disabled left arrow bitmap handle.
  63.         HBITMAP hbmRight;           // Right arrow bitmap handle.
  64.         HBITMAP hbmRightPressed;    // Pressed right arrow bitmap handle.
  65.         HBITMAP hbmRightDisabled;   // Disabled right arrow bitmap handle.
  66.         } DATA;
  67.  
  68.     typedef DATA * PDATA;
  69. /****************************************************************************/
  70.  
  71.  
  72. /****************************************************************************
  73.  *  Internal procedure declarations                                         *
  74.  ****************************************************************************/
  75.     MRESULT EXPENTRY _SaveDlgProc( HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2 );
  76.     static USHORT NEAR OpenFile( HWND hDlg,PDATA pData );
  77.     static VOID NEAR UpdateDir( HWND hDlg,PDATA pData );
  78. /****************************************************************************/
  79.  
  80.  
  81. /****************************************************************************
  82.  *  FileOpenDlg()                                                           *
  83.  ****************************************************************************/
  84.     USHORT EXPENTRY FileSaveDlg( HWND hwndOwner,
  85.                                  PSZ pszTitle,PSZ pszIns,
  86.                                  void (EXPENTRY *pfnHelpProc)(HWND hDlg),
  87.                                  PSZ pszDefault,
  88.                                  PSZ pszFile,
  89.                                  PHFILE phf,
  90.                                  ULONG ulFileSize,
  91.                                  PUSHORT pusAction,
  92.                                  USHORT usAttribute,
  93.                                  USHORT fsOpenFlags,
  94.                                  USHORT fsOpenMode,
  95.                                  ULONG ulReserved )
  96.     {
  97.         USHORT  usMaxPathLen;
  98.         SEL     sel;
  99.         PDATA   pData;
  100.         USHORT  rc;
  101.         HMODULE hmod = 0;
  102.         PSZ     pszTemp;
  103.  
  104.    /* Disable hard-error processing */
  105.         DosError( HARDERROR_DISABLE );
  106.  
  107.    /* Set pszTitle and pszIns to default if NULL */
  108.         if ( pszTitle == NULL ) pszTitle = szDefSaveTitle;
  109.         if ( pszIns == NULL ) pszIns = szDefSaveIns;
  110.  
  111.     /* Get maximum pathname length */
  112.         DosQSysInfo( 0,(PBYTE)&usMaxPathLen,sizeof(USHORT) );
  113.  
  114.     /* Get module handle for FILEDLG dynamic-link library
  115.      *  Note: Remove this code section if the file dialog box
  116.      *        resources are not located in a dynamic-link library.
  117.      */
  118.         if ( (rc = DosGetModHandle(szDLLName,&hmod)) )
  119.             {
  120.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  121.             rc = FDLG_CANCEL;
  122.             goto EXIT;
  123.             }
  124.  
  125.     /* Allocate memory for dialog data */
  126.         if ( (rc = DosAllocSeg(sizeof(DATA),&sel,SEG_NONSHARED)) )
  127.             {
  128.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  129.             rc = FDLG_CANCEL;
  130.             goto EXIT;
  131.             }
  132.         pData = MAKEP(sel,0);
  133.  
  134.     /* Allocate scratch data areas */
  135.         if ( (rc = DosAllocSeg(usMaxPathLen,&sel,SEG_NONSHARED)) )
  136.             {
  137.             DosFreeSeg( SELECTOROF(pData) );
  138.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  139.             rc = FDLG_CANCEL;
  140.             goto EXIT;
  141.             }
  142.         pData->pszScratch = MAKEP(sel,0);
  143.  
  144.     /* Allocate memory to store current directory */
  145.         if ( (rc = DosAllocSeg(usMaxPathLen,&sel,SEG_NONSHARED)) )
  146.             {
  147.             DosFreeSeg( SELECTOROF(pData->pszScratch) );
  148.             DosFreeSeg( SELECTOROF(pData) );
  149.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  150.             rc = FDLG_CANCEL;
  151.             goto EXIT;
  152.             }
  153.         pData->pszCurDir = MAKEP(sel,0);
  154.  
  155.     /* Set current drive and directory to drive and directory listed         */
  156.     /* in default file name, and store filename portion in scratch data area */
  157.         if ( (rc = DosAllocSeg(usMaxPathLen,&sel,SEG_NONSHARED)) )
  158.             {
  159.             DosFreeSeg( SELECTOROF(pData->pszScratch) );
  160.             DosFreeSeg( SELECTOROF(pData->pszCurDir) );
  161.             DosFreeSeg( SELECTOROF(pData) );
  162.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  163.             rc = FDLG_CANCEL;
  164.             goto EXIT;
  165.             }
  166.         pszTemp = MAKEP(sel,0);
  167.         strcpy( pszTemp,pszDefault );
  168.         if ( rc = ParseFileName(pszTemp,pData->pszScratch,szUnnamed) )
  169.             {
  170.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  171.             strcpy( pData->pszScratch,szUnnamed );
  172.             }
  173.         else
  174.             strcpy( pData->pszScratch,strrchr(pData->pszScratch,'\\')+1 );
  175.         DosFreeSeg( SELECTOROF(pszTemp) );
  176.  
  177.     /* Initialize contents of dialog box data structure */
  178.         pData->pszTitle     = pszTitle;
  179.         pData->pszIns       = pszIns;
  180.         pData->pfnHelpProc  = pfnHelpProc;
  181.         pData->pszFile      = pszFile;
  182.         pData->phf          = phf;
  183.         pData->ulFileSize   = ulFileSize;
  184.         pData->pusAction    = pusAction;
  185.         pData->usAttribute  = usAttribute;
  186.         pData->fsOpenFlags  = fsOpenFlags;
  187.         pData->fsOpenMode   = fsOpenMode;
  188.         pData->ulReserved   = ulReserved;
  189.         pData->usMaxPathLen = usMaxPathLen;
  190.         pData->hbmLeft          = WinGetSysBitmap(HWND_DESKTOP,SBMP_SBLFARROW);
  191.         pData->hbmLeftPressed   = WinGetSysBitmap(HWND_DESKTOP,SBMP_SBLFARROWDEP);
  192.         pData->hbmLeftDisabled  = WinGetSysBitmap(HWND_DESKTOP,SBMP_SBLFARROWDIS);
  193.         pData->hbmRight         = WinGetSysBitmap(HWND_DESKTOP,SBMP_SBRGARROW);
  194.         pData->hbmRightPressed  = WinGetSysBitmap(HWND_DESKTOP,SBMP_SBRGARROWDEP);
  195.         pData->hbmRightDisabled = WinGetSysBitmap(HWND_DESKTOP,SBMP_SBRGARROWDIS);
  196.  
  197.     /* Activate open file dialog box */
  198.         rc = WinDlgBox( HWND_DESKTOP,hwndOwner,_SaveDlgProc,
  199.                         hmod,IDD_SAVE,pData );
  200.  
  201.     /* Free resources */
  202.         GpiDeleteBitmap( pData->hbmLeft );
  203.         GpiDeleteBitmap( pData->hbmLeftPressed );
  204.         GpiDeleteBitmap( pData->hbmLeftDisabled );
  205.         GpiDeleteBitmap( pData->hbmRight );
  206.         GpiDeleteBitmap( pData->hbmRightPressed );
  207.         GpiDeleteBitmap( pData->hbmRightDisabled );
  208.         DosFreeSeg( SELECTOROF(pData->pszCurDir) );
  209.         DosFreeSeg( SELECTOROF(pData->pszScratch) );
  210.         DosFreeSeg( SELECTOROF(pData) );
  211.  
  212.     /* re-enable hard error processing and return */
  213. EXIT:   DosError( HARDERROR_ENABLE );
  214.         return rc;
  215.     }
  216. /****************************************************************************/
  217.  
  218.  
  219. /****************************************************************************
  220.  * SaveDlgProc()                                                            *
  221.  ****************************************************************************/
  222.     MRESULT EXPENTRY _SaveDlgProc( HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2 )
  223.     {
  224.         PDATA   pData;
  225.         USHORT  usResult;
  226.  
  227.         switch ( msg ) {
  228.             case WM_INITDLG:
  229.                 pData = PVOIDFROMMP( mp2 );
  230.                 WinSetWindowULong( hwnd,QWL_USER,(ULONG)pData );
  231.                 if ( pData->pfnHelpProc == NULL )
  232.                     WinDestroyWindow( WinWindowFromID(hwnd,SAVE_HELP) );
  233.                 WinSetWindowText( WinWindowFromID(hwnd,FID_TITLEBAR),
  234.                                   pData->pszTitle );
  235.                 WinSetDlgItemText( hwnd,SAVE_HLPTEXT,pData->pszIns );
  236.                 WinSendDlgItemMsg( hwnd,SAVE_FNAME,EM_SETTEXTLIMIT,
  237.                                    MPFROMSHORT(pData->usMaxPathLen),NULL );
  238.                 WinSetDlgItemText( hwnd,SAVE_FNAME,pData->pszScratch );
  239.                 UpdateDir( hwnd,pData );
  240.                 return 0;
  241.  
  242.             case WM_CONTROL:
  243.                 pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  244.                 switch ( SHORT1FROMMP(mp1) ) {
  245.                     case SAVE_FNAME:
  246.                         if ( SHORT2FROMMP(mp1) == EN_SETFOCUS )
  247.                             {
  248.                             usResult = WinQueryDlgItemTextLength( hwnd,SAVE_FNAME );
  249.                             WinSendDlgItemMsg( hwnd,SAVE_FNAME,EM_SETSEL,
  250.                                                MPFROM2SHORT(0,usResult),0L );
  251.                             return 0L;
  252.                             }
  253.  
  254.                     case SAVE_LEFTARROW:
  255.                         return LeftArrow(hwnd,msg,mp1,mp2,
  256.                                          pData->hbmLeft,
  257.                                          pData->hbmLeftPressed,
  258.                                          pData->hbmLeftDisabled);
  259.  
  260.  
  261.                     case SAVE_RIGHTARROW:
  262.                         return RightArrow(hwnd,msg,mp1,mp2,
  263.                                           pData->hbmRight,
  264.                                           pData->hbmRightPressed,
  265.                                           pData->hbmRightDisabled);
  266.                     }
  267.                 break;
  268.  
  269.             case WM_COMMAND:
  270.                 pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  271.                 switch (COMMANDMSG(&msg)->cmd) {
  272.                     case DID_OK:
  273.                     case SAVE_OK:
  274.                         WinQueryDlgItemText( hwnd,SAVE_FNAME,
  275.                                              pData->usMaxPathLen,
  276.                                              pData->pszScratch );
  277.                         if ( !OpenFile(hwnd,pData) )
  278.                             WinDismissDlg( hwnd,FDLG_OK );
  279.                         else
  280.                             {
  281.                             UpdateDir( hwnd,pData );
  282.                             WinSetFocus( HWND_DESKTOP,
  283.                                          WinWindowFromID(hwnd,SAVE_FNAME) );
  284.                             }
  285.                         return 0;
  286.  
  287.                     case DID_CANCEL:
  288.                     case SAVE_CANCEL:
  289.                         WinDismissDlg( hwnd,FDLG_CANCEL );
  290.                         return 0;
  291.  
  292.                     case SAVE_LEFTARROW:
  293.                         if ( pData->iFirstChar == 0 )
  294.                             WinAlarm( HWND_DESKTOP,WA_ERROR );
  295.                         else
  296.                             {
  297.                             pData->iFirstChar--;
  298.                             WinSetDlgItemText( hwnd,SAVE_CURDIR,
  299.                                                &pData->pszCurDir[pData->iFirstChar] );
  300.                             }
  301.                         return 0L;
  302.  
  303.                     case SAVE_RIGHTARROW:
  304.                         {
  305.                         size_t len = strlen(pData->pszCurDir);
  306.  
  307.                         if ( len == 0 || pData->iFirstChar == len-1 )
  308.                             WinAlarm( HWND_DESKTOP,WA_ERROR );
  309.                         else
  310.                             {
  311.                             pData->iFirstChar++;
  312.                             WinSetDlgItemText( hwnd,SAVE_CURDIR,
  313.                                                &pData->pszCurDir[pData->iFirstChar] );
  314.                             }
  315.                         return 0L;
  316.                         }
  317.                     }
  318.                 break;
  319.  
  320.             case WM_HELP:
  321.                 pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  322.                 if ( pData->pfnHelpProc != NULL )
  323.                     {
  324.                     (*pData->pfnHelpProc)( hwnd );
  325.                     return 0L;
  326.                     }
  327.                 break;
  328.             }
  329.         return WinDefDlgProc( hwnd,msg,mp1,mp2 );
  330.     }
  331. /****************************************************************************/
  332.  
  333.  
  334. /****************************************************************************
  335.  * OpenFile() - This function attempts to open the file specified           *
  336.  *              in the scratch data area.                                   *
  337.  *                                                                          *
  338.  *              This function returns a non-zero value if an error occured. *
  339.  ****************************************************************************/
  340.     static USHORT NEAR OpenFile( HWND hDlg,PDATA pData )
  341.     {
  342.         USHORT  usResult;
  343.  
  344.         usResult = ParseFileName( pData->pszScratch,
  345.                                   pData->pszFile,
  346.                                   szStarDotStar );
  347.         if ( usResult )
  348.             {
  349.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  350.             return 1;
  351.             }
  352.  
  353.         if ( 0 == access(pData->pszFile,0) )
  354.             {
  355.             switch ( ErrMessageBox(hDlg,pData->pszTitle,
  356.                                    OVERWRITE_MSG,
  357.                                    appMsgList,appMsgCount) )
  358.                 {
  359.                 case MBID_YES:      break;
  360.  
  361.                 case MBID_CANCEL:   WinDismissDlg( hDlg,FDLG_CANCEL );
  362.                                     return 1;
  363.  
  364.                 default:            return 1;
  365.                 }
  366.             }
  367.  
  368.         usResult = DosOpen( pData->pszFile,
  369.                             pData->phf,
  370.                             pData->pusAction,
  371.                             pData->ulFileSize,
  372.                             pData->usAttribute,
  373.                             pData->fsOpenFlags,
  374.                             pData->fsOpenMode,
  375.                             pData->ulReserved );
  376.         if ( usResult )
  377.             {
  378.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  379.             return 1;
  380.             }
  381.         else
  382.             return 0;
  383.     }
  384. /****************************************************************************/
  385.  
  386.  
  387. /****************************************************************************
  388.  * UpdateDir() - This function updates the current directory display        *
  389.  *               to reflect changes in the current drive/directory.         *
  390.  ****************************************************************************/
  391.     static VOID NEAR UpdateDir( HWND hwnd,PDATA pData )
  392.     {
  393.         USHORT  usResult;
  394.         USHORT  usCount;
  395.         USHORT  usDriveNum;
  396.         ULONG   ulMap;
  397.  
  398.         pData->iFirstChar = 0;
  399.  
  400.         if ( usResult = DosQCurDisk(&usDriveNum,&ulMap) )
  401.             {
  402.             WinSetDlgItemText( hwnd,SAVE_CURDIR,"" );
  403.             pData->pszCurDir = 0;
  404.             ErrMessageBox( hwnd,pData->pszTitle,usResult,NULL,0 );
  405.             return;
  406.             }
  407.  
  408.         pData->pszCurDir[0] = (CHAR)( usDriveNum + '@' );
  409.         pData->pszCurDir[1] = ':';
  410.         pData->pszCurDir[2] = '\\';
  411.         pData->pszCurDir[3] = '\0';
  412.  
  413.         usCount = pData->usMaxPathLen-3;
  414.         if ( usResult = DosQCurDir(0,pData->pszCurDir+3,&usCount) )
  415.             ErrMessageBox( hwnd,pData->pszTitle,usResult,NULL,0 );
  416.  
  417.         WinSetDlgItemText( hwnd,SAVE_CURDIR,pData->pszCurDir );
  418.  
  419.         return;
  420.     }
  421. /****************************************************************************/
  422.