home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / filedlg5 / source / savedlg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-19  |  14.3 KB  |  345 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.  *                                                                          *
  17.  * (c)Copyright 1989 Rick Yoder                                             *
  18.  ****************************************************************************/
  19.  
  20.     #define INCL_WIN
  21.     #define INCL_DOS
  22.     #include <os2.h>
  23.  
  24.     #include <string.h>
  25.     #include <io.h>
  26.     #include <errmsg.h>
  27.  
  28.     #include "filedlg.h"
  29.     #include "dialog.h"
  30.     #include "tools.h"
  31.     #include "static.h"
  32.  
  33. /****************************************************************************
  34.  *  Internal data structure definitions                                     *
  35.  ****************************************************************************/
  36.     typedef struct {
  37.         PSZ     pszTitle;       // dialog box title
  38.         PSZ     pszIns;         // dialog box instructions
  39.         void (CALLBACK *pfnHelpProc)(HWND hDlg); // ptr to help procedure
  40.         PSZ     pszFile;        // ptr to name of opened file
  41.         PHFILE  phf;            // ptr to file handle
  42.         ULONG   ulFileSize;     // initial file size
  43.         PUSHORT pusAction;      // action taken on open
  44.         USHORT  usAttribute;    // file attribute
  45.         USHORT  fsOpenFlags;    // open flags
  46.         USHORT  fsOpenMode;     // open mode
  47.         ULONG   ulReserved;     // reserved
  48.         USHORT  usMaxPathLen;   // maximum path name length
  49.         PSZ     pszScratch;     // ptr to scratch data area
  50.         } DATA;
  51.  
  52.     typedef DATA * PDATA;
  53. /****************************************************************************/
  54.  
  55.  
  56. /****************************************************************************
  57.  *  Internal procedure declarations                                         *
  58.  ****************************************************************************/
  59.     MRESULT CALLBACK _SaveDlgProc( HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2 );
  60.     static USHORT near OpenFile( HWND hDlg,PDATA pData );
  61.     static void near UpdateDir( HWND hDlg,PDATA pData );
  62. /****************************************************************************/
  63.  
  64.  
  65. /****************************************************************************
  66.  *  FileOpenDlg()                                                           *
  67.  ****************************************************************************/
  68.     USHORT CALLBACK FileSaveDlg( HWND hwndOwner,
  69.                                  PSZ pszTitle,PSZ pszIns,
  70.                                  void (CALLBACK *pfnHelpProc)(HWND hDlg),
  71.                                  PSZ pszDefault,
  72.                                  PSZ pszFile,
  73.                                  PHFILE phf,
  74.                                  ULONG ulFileSize,
  75.                                  PUSHORT pusAction,
  76.                                  USHORT usAttribute,
  77.                                  USHORT fsOpenFlags,
  78.                                  USHORT fsOpenMode,
  79.                                  ULONG ulReserved )
  80.     {
  81.         USHORT  usMaxPathLen;
  82.         SEL     sel;
  83.         PDATA   pData;
  84.         USHORT  rc;
  85.         HMODULE hmod;
  86.         PSZ     pszTemp;
  87.  
  88.     /* Set pszTitle and pszIns to default if NULL */
  89.         if ( pszTitle == NULL ) pszTitle = szDefSaveTitle;
  90.         if ( pszIns == NULL ) pszIns = szDefSaveIns;
  91.  
  92.     /* Get maximum pathname length */
  93.         DosQSysInfo( 0,(PBYTE)&usMaxPathLen,sizeof(USHORT) );
  94.  
  95.     /* Get module handle for filedlg dynamic-link library */
  96.         if ( (rc = DosGetModHandle(szDLLName,&hmod)) )
  97.             {
  98.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  99.             return FDLG_CANCEL;
  100.             }
  101.  
  102.     /* Allocate memory for dialog data */
  103.         if ( (rc = DosAllocSeg(sizeof(DATA),&sel,SEG_NONSHARED)) )
  104.             {
  105.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  106.             return FDLG_CANCEL;
  107.             }
  108.         pData = MAKEP(sel,0);
  109.  
  110.     /* Allocate scratch data areas */
  111.         if ( (rc = DosAllocSeg(usMaxPathLen,&sel,SEG_NONSHARED)) )
  112.             {
  113.             DosFreeSeg( SELECTOROF(pData) );
  114.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  115.             return FDLG_CANCEL;
  116.             }
  117.         pData->pszScratch = MAKEP(sel,0);
  118.  
  119.     /* Set current drive and directory to drive and directory listed         */
  120.     /* in default file name, and store filename portion in scratch data area */
  121.         if ( (rc = DosAllocSeg(usMaxPathLen,&sel,SEG_NONSHARED)) )
  122.             {
  123.             DosFreeSeg( SELECTOROF(pData->pszScratch) );
  124.             DosFreeSeg( SELECTOROF(pData) );
  125.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  126.             return FDLG_CANCEL;
  127.             }
  128.         pszTemp = MAKEP(sel,0);
  129.         strcpy( pszTemp,pszDefault );
  130.         if ( rc = ParseFileName(pszTemp,pData->pszScratch,szUnnamed) )
  131.             {
  132.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  133.             strcpy( pData->pszScratch,szUnnamed );
  134.             }
  135.         else
  136.             strcpy( pData->pszScratch,strrchr(pData->pszScratch,'\\')+1 );
  137.         DosFreeSeg( SELECTOROF(pszTemp) );
  138.  
  139.     /* Initialize contents of dialog box data structure */
  140.         pData->pszTitle     = pszTitle;
  141.         pData->pszIns       = pszIns;
  142.         pData->pfnHelpProc  = pfnHelpProc;
  143.         pData->pszFile      = pszFile;
  144.         pData->phf          = phf;
  145.         pData->ulFileSize   = ulFileSize;
  146.         pData->pusAction    = pusAction;
  147.         pData->usAttribute  = usAttribute;
  148.         pData->fsOpenFlags  = fsOpenFlags;
  149.         pData->fsOpenMode   = fsOpenMode;
  150.         pData->ulReserved   = ulReserved;
  151.         pData->usMaxPathLen = usMaxPathLen;
  152.  
  153.     /* Activate open file dialog box */
  154.         rc = WinDlgBox( HWND_DESKTOP,hwndOwner,_SaveDlgProc,
  155.                         hmod,IDD_SAVE,pData );
  156.  
  157.     /* Free resources */
  158.         DosFreeSeg( SELECTOROF(pData->pszScratch) );
  159.         DosFreeSeg( SELECTOROF(pData) );
  160.  
  161.         return rc;
  162.     }
  163. /****************************************************************************/
  164.  
  165.  
  166. /****************************************************************************
  167.  * SaveDlgProc()                                                            *
  168.  ****************************************************************************/
  169.     MRESULT CALLBACK _SaveDlgProc( HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2 )
  170.     {
  171.         PDATA   pData;
  172.         USHORT  usResult;
  173.  
  174.         switch ( msg ) {
  175.             case WM_INITDLG:
  176.                 pData = PVOIDFROMMP( mp2 );
  177.                 WinSetWindowULong( hwnd,QWL_USER,(ULONG)pData );
  178.                 if ( pData->pfnHelpProc == NULL )
  179.                     WinDestroyWindow( WinWindowFromID(hwnd,SAVE_HELP) );
  180.                 WinSetWindowText( WinWindowFromID(hwnd,FID_TITLEBAR),
  181.                                   pData->pszTitle );
  182.                 WinSetDlgItemText( hwnd,SAVE_HLPTEXT,pData->pszIns );
  183.                 WinSendDlgItemMsg( hwnd,SAVE_FNAME,EM_SETTEXTLIMIT,
  184.                                    MPFROMSHORT(pData->usMaxPathLen),NULL );
  185.                 WinSetDlgItemText( hwnd,SAVE_FNAME,pData->pszScratch );
  186.                 UpdateDir( hwnd,pData );
  187.                 return 0;
  188.  
  189.             case WM_CHAR:
  190.                 if ( CHARMSG(&msg)->fs & KC_ALT )
  191.                     switch ( CHARMSG(&msg)->chr ) {
  192.                         case 'f':
  193.                         case 'F': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,SAVE_FNAME));
  194.                                   return 0;
  195.                         }
  196.                 break;
  197.  
  198.  
  199.             case WM_CONTROL:
  200.                 pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  201.                 if (   SHORT1FROMMP(mp1) == SAVE_FNAME
  202.                     && SHORT2FROMMP(mp1) == EN_SETFOCUS )
  203.                     {
  204.                     usResult = WinQueryDlgItemTextLength( hwnd,SAVE_FNAME );
  205.                     WinSendDlgItemMsg( hwnd,SAVE_FNAME,EM_SETSEL,
  206.                                        MPFROM2SHORT(0,usResult),0L );
  207.  
  208.                     WinSendDlgItemMsg( hwnd,SAVE_OK,BM_SETDEFAULT,
  209.                                        MPFROMSHORT(TRUE),0L );
  210.                     WinSendDlgItemMsg( hwnd,SAVE_CANCEL,BM_SETDEFAULT,
  211.                                        MPFROMSHORT(FALSE),0L );
  212.                     if ( pData->pfnHelpProc != NULL )
  213.                         WinSendDlgItemMsg( hwnd,SAVE_HELP,BM_SETDEFAULT,
  214.                                            MPFROMSHORT(FALSE),0L );
  215.  
  216.                     return 0;
  217.                     }
  218.                 break;
  219.  
  220.             case WM_COMMAND:
  221.                 pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  222.                 switch (COMMANDMSG(&msg)->cmd) {
  223.                     case DID_OK:
  224.                     case SAVE_OK:
  225.                         WinQueryDlgItemText( hwnd,SAVE_FNAME,
  226.                                              pData->usMaxPathLen,
  227.                                              pData->pszScratch );
  228.                         if ( !OpenFile(hwnd,pData) )
  229.                             WinDismissDlg( hwnd,FDLG_OK );
  230.                         else
  231.                             {
  232.                             UpdateDir( hwnd,pData );
  233.                             WinSetFocus( HWND_DESKTOP,
  234.                                          WinWindowFromID(hwnd,SAVE_FNAME) );
  235.                             }
  236.                         return 0;
  237.  
  238.                     case DID_CANCEL:
  239.                     case SAVE_CANCEL:
  240.                         WinDismissDlg( hwnd,FDLG_CANCEL );
  241.                         return 0;
  242.                     }
  243.                 break;
  244.  
  245.             case WM_HELP:
  246.                 pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  247.                 if ( pData->pfnHelpProc != NULL )
  248.                     {
  249.                     (*pData->pfnHelpProc)( hwnd );
  250.                     return 0;
  251.                     }
  252.                 break;
  253.             }
  254.         return WinDefDlgProc( hwnd,msg,mp1,mp2 );
  255.     }
  256. /****************************************************************************/
  257.  
  258.  
  259. /****************************************************************************
  260.  * OpenFile() - This function attempts to open the file specified           *
  261.  *              in the scratch data area.                                   *
  262.  *                                                                          *
  263.  *              This function returns a non-zero value if an error occured. *
  264.  ****************************************************************************/
  265.     static USHORT near OpenFile( HWND hDlg,PDATA pData )
  266.     {
  267.         USHORT  usResult;
  268.  
  269.         usResult = ParseFileName( pData->pszScratch,
  270.                                   pData->pszFile,
  271.                                   szStarDotStar );
  272.         if ( usResult )
  273.             {
  274.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  275.             return 1;
  276.             }
  277.  
  278.         if ( 0 == access(pData->pszFile,0) )
  279.             {
  280.             switch ( ErrMessageBox(hDlg,pData->pszTitle,
  281.                                    OVERWRITE_MSG,
  282.                                    appMsgList,appMsgCount) )
  283.                 {
  284.                 case MBID_YES:      break;
  285.  
  286.                 case MBID_CANCEL:   WinDismissDlg( hDlg,FDLG_CANCEL );
  287.                                     return 1;
  288.  
  289.                 default:            return 1;
  290.                 }
  291.             }
  292.  
  293.         usResult = DosOpen( pData->pszFile,
  294.                             pData->phf,
  295.                             pData->pusAction,
  296.                             pData->ulFileSize,
  297.                             pData->usAttribute,
  298.                             pData->fsOpenFlags,
  299.                             pData->fsOpenMode,
  300.                             pData->ulReserved );
  301.         if ( usResult )
  302.             {
  303.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  304.             return 1;
  305.             }
  306.         else
  307.             return 0;
  308.     }
  309. /****************************************************************************/
  310.  
  311.  
  312. /****************************************************************************
  313.  * UpdateDir() - This function updates the current directory display        *
  314.  *               to reflect changes in the current drive/directory.         *
  315.  ****************************************************************************/
  316.     static void near UpdateDir( HWND hwnd,PDATA pData )
  317.     {
  318.         USHORT  usResult;
  319.         USHORT  usCount;
  320.         USHORT  usDriveNum;
  321.         ULONG   ulMap;
  322.  
  323.         if ( usResult = DosQCurDisk(&usDriveNum,&ulMap) )
  324.             {
  325.             WinSetDlgItemText( hwnd,SAVE_CURDIR,"" );
  326.             ErrMessageBox( hwnd,pData->pszTitle,usResult,NULL,0 );
  327.             return;
  328.             }
  329.  
  330.         pData->pszScratch[0] = (CHAR)usDriveNum + '@';
  331.         pData->pszScratch[1] = ':';
  332.         pData->pszScratch[2] = '\\';
  333.         pData->pszScratch[3] = '\0';
  334.  
  335.         usCount = pData->usMaxPathLen-3;
  336.         if ( usResult = DosQCurDir(0,pData->pszScratch+3,&usCount) )
  337.             ErrMessageBox( hwnd,pData->pszTitle,usResult,NULL,0 );
  338.  
  339.         WinSetDlgItemText( hwnd,SAVE_CURDIR,
  340.                            FitPathToBox(hwnd,SAVE_CURDIR,pData->pszScratch) );
  341.  
  342.         return;
  343.     }
  344. /****************************************************************************/
  345.