home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / os2 / filedlg5 / source / opendlg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-11-19  |  32.9 KB  |  755 lines

  1. /****************************************************************************
  2.  * OPENDLG.C - Open File dialog box routines.                               *
  3.  *                                                                          *
  4.  *  Notes -                                                                 *
  5.  *      This following functions will have to be modified in order to       *
  6.  *      support OS/2 v1.2 long file names :                                 *
  7.  *                                                                          *
  8.  *              FillListBoxes()                                             *
  9.  *                                                                          *
  10.  *  Modifications -                                                         *
  11.  *      11-Aug-1989 : Initial version.                                      *
  12.  *      07-Sep-1989 : Added file find function.                             *
  13.  *      21-Sep-1989 : Cleaned up code to make it a little more readable.    *
  14.  *                    Separated open file and find file dialog procedures   *
  15.  *                      into separate files.                                *
  16.  *                    Forced OPEN button to be default button whenever      *
  17.  *                      the focus passes to a non-pushbutton control.       *
  18.  *                    Eliminated duplicate error messages when enter key    *
  19.  *                      is used to select a list box item.                  *
  20.  *      11-Oct-1989 : Changed to DLL version of ErrMessageBox function.     *
  21.  *      19-Nov-1989 : Fixed bug causing protection violation when the       *
  22.  *                    pszSearchSpec string is located in a read-only        *
  23.  *                    segment.                                              *
  24.  *                                                                          *
  25.  * (c)Copyright 1989 Rick Yoder                                             *
  26.  ****************************************************************************/
  27.  
  28.     #define INCL_WIN
  29.     #define INCL_DOS
  30.     #define INCL_DOSERRORS
  31.     #include <os2.h>
  32.  
  33.     #include <string.h>
  34.     #include <errmsg.h>
  35.  
  36.     #include "filedlg.h"
  37.     #include "dialog.h"
  38.     #include "tools.h"
  39.     #include "static.h"
  40.     #include "opendata.h"   // definition of structure holding dialog box
  41.                             //  static data (DATA & PDATA types).
  42.  
  43. /****************************************************************************
  44.  *  Procedure declarations                                                  *
  45.  ****************************************************************************/
  46.     /* Open file dialog box procedures */
  47.     MRESULT CALLBACK _OpenDlgProc( HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2 );
  48.     static MRESULT near OpenInit( HWND hwnd,MPARAM mp2 );
  49.     static MRESULT near DriveListbox( HWND hwnd,USHORT msg,
  50.                                       MPARAM mp1,MPARAM mp2 );
  51.     static MRESULT near DirListbox( HWND hwnd,USHORT msg,
  52.                                     MPARAM mp1,MPARAM mp2 );
  53.     static MRESULT near FileListbox( HWND hwnd,USHORT msg,
  54.                                      MPARAM mp1,MPARAM mp2 );
  55.     static MRESULT near FnameEditCtrl( HWND hwnd,USHORT msg,
  56.                                        MPARAM mp1,MPARAM mp2 );
  57.     static MRESULT near OpenButton( HWND hwnd,PDATA pData );
  58.     static void near FillListBoxes( HWND hDlg,PDATA pData );
  59.     static USHORT near OpenFile( HWND hDlg,PDATA pData );
  60.     static void near ResetDefaultButton( HWND hwnd,PDATA pData );
  61.  
  62.     /* Find file dialog box procedures */
  63.     extern MRESULT CALLBACK _FindDlgProc( HWND hwnd,USHORT msg,
  64.                                           MPARAM mp1,MPARAM mp2 );
  65. /****************************************************************************/
  66.  
  67.  
  68. /****************************************************************************
  69.  *  FileOpenDlg()                                                           *
  70.  ****************************************************************************/
  71.     USHORT CALLBACK FileOpenDlg( HWND hwndOwner,
  72.                                  PSZ pszTitle,PSZ pszIns,
  73.                                  PSZ pszShowSpec,USHORT usShowAttr,
  74.                                  void (CALLBACK *pfnHelpProc)(HWND hDlg),
  75.                                  PSZ pszFile,
  76.                                  PHFILE phf,
  77.                                  ULONG ulFileSize,
  78.                                  PUSHORT pusAction,
  79.                                  USHORT usAttribute,
  80.                                  USHORT fsOpenFlags,
  81.                                  USHORT fsOpenMode,
  82.                                  ULONG ulReserved )
  83.     {
  84.         USHORT  usMaxPathLen;
  85.         SEL     sel;
  86.         PDATA   pData;
  87.         USHORT  rc;
  88.         HMODULE hmod;
  89.  
  90.     /* Set pszTitle and pszIns to default if NULL */
  91.         if ( pszTitle == NULL ) pszTitle = szDefOpenTitle;
  92.         if ( pszIns == NULL ) pszIns = szDefOpenIns;
  93.  
  94.     /* Get maximum pathname length */
  95.         DosQSysInfo( 0,(PBYTE)&usMaxPathLen,sizeof(USHORT) );
  96.  
  97.     /* Get module handle for filedlg dynamic-link library */
  98.         if ( (rc = DosGetModHandle(szDLLName,&hmod)) )
  99.             {
  100.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  101.             return FDLG_CANCEL;
  102.             }
  103.  
  104.     /* Allocate memory for dialog data */
  105.         if ( (rc = DosAllocSeg(sizeof(DATA),&sel,SEG_NONSHARED)) )
  106.             {
  107.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  108.             return FDLG_CANCEL;
  109.             }
  110.         pData = MAKEP(sel,0);
  111.  
  112.     /* Allocate memory for search spec */
  113.         if ( (rc = DosAllocSeg(usMaxPathLen,&sel,SEG_NONSHARED)) )
  114.             {
  115.             DosFreeSeg( SELECTOROF(pData) );
  116.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  117.             return FDLG_CANCEL;
  118.             }
  119.         pData->pszShowSpec = MAKEP(sel,0);
  120.  
  121.     /* Allocate scratch data area */
  122.         if ( (rc = DosAllocSeg(usMaxPathLen+3,&sel,SEG_NONSHARED)) )
  123.             {
  124.             DosFreeSeg( SELECTOROF(pData->pszShowSpec) );
  125.             DosFreeSeg( SELECTOROF(pData) );
  126.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  127.             return FDLG_CANCEL;
  128.             }
  129.         pData->pszScratch = MAKEP(sel,0);
  130.  
  131.     /* Set current drive and directory to drive and directory listed   */
  132.     /* in show file specification, and store filename portion of spec. */
  133.         strcpy( pData->pszShowSpec,pszShowSpec );
  134.         if ( rc = ParseFileName(pData->pszShowSpec,
  135.                                 pData->pszScratch,
  136.                                 szStarDotStar) )
  137.             {
  138.             ErrMessageBox( hwndOwner,pszTitle,rc,NULL,0 );
  139.             strcpy( pData->pszShowSpec,szStarDotStar );
  140.             }
  141.         else
  142.             strcpy( pData->pszShowSpec,strrchr(pData->pszScratch,'\\')+1 );
  143.  
  144.     /* Initialize contents of dialog box data structure */
  145.         pData->pszTitle         = pszTitle;
  146.         pData->pszIns           = pszIns;
  147.         pData->pfnHelpProc      = pfnHelpProc;
  148.         pData->pszFile          = pszFile;
  149.         pData->phf              = phf;
  150.         pData->ulFileSize       = ulFileSize;
  151.         pData->pusAction        = pusAction;
  152.         pData->usAttribute      = usAttribute;
  153.         pData->fsOpenFlags      = fsOpenFlags;
  154.         pData->fsOpenMode       = fsOpenMode;
  155.         pData->ulReserved       = ulReserved;
  156.         pData->usShowAttr       = usShowAttr;
  157.         pData->usMaxPathLen     = usMaxPathLen;
  158.         pData->hmod             = hmod;
  159.         pData->usFocus          = OPEN_FNAME;
  160.  
  161.     /* Activate open file dialog box */
  162.         rc = WinDlgBox( HWND_DESKTOP,hwndOwner,_OpenDlgProc,
  163.                         hmod,IDD_OPEN,pData );
  164.  
  165.     /* Free resources */
  166.         DosFreeSeg( SELECTOROF(pData->pszShowSpec) );
  167.         DosFreeSeg( SELECTOROF(pData->pszScratch) );
  168.         DosFreeSeg( SELECTOROF(pData) );
  169.  
  170.         return rc;
  171.     }
  172. /****************************************************************************/
  173.  
  174.  
  175. /****************************************************************************
  176.  * _OpenDlgProc()                                                           *
  177.  ****************************************************************************/
  178.     MRESULT CALLBACK _OpenDlgProc( HWND hwnd,USHORT msg,MPARAM mp1,MPARAM mp2 )
  179.     {
  180.         PDATA   pData;
  181.  
  182.         switch ( msg ) {
  183.             case WM_INITDLG:
  184.                 return OpenInit( hwnd,mp2 );
  185.  
  186.             case WM_CHAR:
  187.                 if ( CHARMSG(&msg)->fs & KC_ALT )
  188.                     switch ( CHARMSG(&msg)->chr ) {
  189.                         case 'n':
  190.                         case 'N': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,OPEN_FNAME));
  191.                                   return 0;
  192.  
  193.                         case 'r':
  194.                         case 'R': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,OPEN_DRIVES));
  195.                                   return 0;
  196.  
  197.                         case 'd':
  198.                         case 'D': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,OPEN_DIRLIST));
  199.                                   return 0;
  200.  
  201.                         case 'f':
  202.                         case 'F': WinSetFocus(HWND_DESKTOP,WinWindowFromID(hwnd,OPEN_FILELIST));
  203.                                   return 0;
  204.                         }
  205.                 break;
  206.  
  207.             case WM_CONTROL:
  208.                 switch ( SHORT1FROMMP(mp1) ) {
  209.                     case OPEN_DRIVES:
  210.                         return DriveListbox( hwnd,msg,mp1,mp2 );
  211.  
  212.                     case OPEN_DIRLIST:
  213.                         return DirListbox( hwnd,msg,mp1,mp2 );
  214.  
  215.                     case OPEN_FILELIST:
  216.                         return FileListbox( hwnd,msg,mp1,mp2 );
  217.  
  218.                     case OPEN_FNAME:
  219.                         return FnameEditCtrl( hwnd,msg,mp1,mp2 );
  220.                     }
  221.                 break;
  222.  
  223.             case WM_COMMAND:
  224.                 pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  225.                 switch (COMMANDMSG(&msg)->cmd) {
  226.                     case OPEN_OK:
  227.                         return OpenButton( hwnd,pData );
  228.  
  229.                     case DID_CANCEL:
  230.                     case OPEN_CANCEL:
  231.                         WinDismissDlg( hwnd,FDLG_CANCEL );
  232.                         return 0L;
  233.  
  234.                     case OPEN_FIND:
  235.                         if ( FDLG_OK == WinDlgBox(HWND_DESKTOP,hwnd,
  236.                                                   _FindDlgProc,pData->hmod,
  237.                                                   IDD_FIND,pData)
  238.                            ) WinDismissDlg( hwnd,FDLG_OK );
  239.                         WinSetFocus( HWND_DESKTOP,
  240.                                      WinWindowFromID(hwnd,OPEN_FNAME) );
  241.                         return 0L;
  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 0L;
  251.                     }
  252.                 break;
  253.             }
  254.         return WinDefDlgProc( hwnd,msg,mp1,mp2 );
  255.     }
  256. /****************************************************************************/
  257.  
  258.  
  259. /****************************************************************************
  260.  * OpenInit() -- Process WM_INITDLG message for the open file dialog        *
  261.  *               box.                                                       *
  262.  ****************************************************************************/
  263.     static MRESULT near OpenInit( HWND hwnd,MPARAM mp2 )
  264.     {
  265.         PDATA pData;
  266.  
  267.         pData = PVOIDFROMMP( mp2 );
  268.         WinSetWindowULong( hwnd,QWL_USER,(ULONG)pData );
  269.         if ( pData->pfnHelpProc == NULL )
  270.             WinDestroyWindow( WinWindowFromID(hwnd,OPEN_HELP) );
  271.         WinSetWindowText( WinWindowFromID(hwnd,FID_TITLEBAR),pData->pszTitle );
  272.         WinSetDlgItemText( hwnd,OPEN_HLPTEXT,pData->pszIns );
  273.         WinSendDlgItemMsg( hwnd,OPEN_FNAME,EM_SETTEXTLIMIT,
  274.                            MPFROMSHORT(pData->usMaxPathLen),NULL );
  275.         FillListBoxes( hwnd,pData );
  276.         return 0L;
  277.     }
  278. /****************************************************************************/
  279.  
  280.  
  281. /****************************************************************************
  282.  * DriveListbox() -- Handle messages sent by the disk drive list box to     *
  283.  *                   the open file dialog.                                  *
  284.  ****************************************************************************/
  285.     static MRESULT near DriveListbox( HWND hwnd,USHORT msg,
  286.                                       MPARAM mp1,MPARAM mp2 )
  287.     {
  288.         PDATA   pData;
  289.         USHORT  usResult;
  290.  
  291.         pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  292.         switch ( SHORT2FROMMP(mp1) ) {
  293.             case LN_ENTER:
  294.                 pData->usFocus = OPEN_DRIVES;
  295.                 WinSendDlgItemMsg( hwnd,OPEN_DRIVES,LM_QUERYITEMTEXT,
  296.                                    MPFROM2SHORT(pData->usSelectDrive,pData->usMaxPathLen),
  297.                                    MPFROMP(pData->pszScratch) );
  298.                 usResult = DosSelectDisk(pData->pszScratch[0]-'@');
  299.                 if ( usResult )
  300.                     ErrMessageBox( hwnd,pData->pszTitle,usResult,NULL,0 );
  301.                 else
  302.                     WinSendDlgItemMsg( hwnd,OPEN_OK,BM_CLICK,0L,0L );
  303.                 WinSendDlgItemMsg( hwnd,OPEN_DRIVES,LM_SELECTITEM,
  304.                                    MPFROMSHORT(pData->usSelectDrive),
  305.                                    MPFROMSHORT(TRUE) );
  306.                 return 0L;
  307.  
  308.             case LN_SETFOCUS:
  309.                 ResetDefaultButton( hwnd,pData );
  310.                 usResult = (USHORT)WinSendDlgItemMsg( hwnd,OPEN_DRIVES,
  311.                                                       LM_QUERYTOPINDEX,
  312.                                                       0L,0L );
  313.                 if ( usResult != LIT_NONE )
  314.                     {
  315.                     if ( pData->usSelectDrive < usResult )
  316.                         pData->usSelectDrive = usResult;
  317.                     else if (pData->usSelectDrive > usResult+5)
  318.                         pData->usSelectDrive = usResult+5;
  319.  
  320.                     WinSendDlgItemMsg( hwnd,OPEN_DRIVES,LM_SELECTITEM,
  321.                                        MPFROMSHORT(pData->usSelectDrive),
  322.                                        MPFROMSHORT(TRUE) );
  323.                     }
  324.                 return 0L;
  325.  
  326.             case LN_KILLFOCUS:
  327.                 WinSendDlgItemMsg( hwnd,OPEN_DRIVES,LM_SELECTITEM,
  328.                                    MPFROMSHORT(pData->usSelectDrive),
  329.                                    MPFROMSHORT(FALSE) );
  330.                 return 0L;
  331.  
  332.             case LN_SELECT:
  333.                 usResult = (USHORT)WinSendDlgItemMsg( hwnd,OPEN_DRIVES,
  334.                                                       LM_QUERYSELECTION,
  335.                                                       0L,0L );
  336.                 if ( usResult != LIT_NONE ) pData->usSelectDrive = usResult;
  337.                 return 0L;
  338.             }
  339.  
  340.         return WinDefDlgProc( hwnd,msg,mp1,mp2 );
  341.     }
  342. /****************************************************************************/
  343.  
  344.  
  345. /****************************************************************************
  346.  * DirListbox() -- Handle messages sent by directory list box to the        *
  347.  *                 open file dialog.                                        *
  348.  ****************************************************************************/
  349.     static MRESULT near DirListbox( HWND hwnd,USHORT msg,
  350.                                     MPARAM mp1,MPARAM mp2 )
  351.     {
  352.         PDATA  pData;
  353.         USHORT usResult;
  354.  
  355.         pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  356.         switch ( SHORT2FROMMP(mp1) ) {
  357.             case LN_ENTER:
  358.                 pData->usFocus = OPEN_DIRLIST;
  359.                 WinSendDlgItemMsg( hwnd,OPEN_DIRLIST,
  360.                                    LM_QUERYITEMTEXT,
  361.                                    MPFROM2SHORT(pData->usSelectDir,pData->usMaxPathLen),
  362.                                    MPFROMP(pData->pszScratch) );
  363.                 usResult = DosChDir(pData->pszScratch,0L);
  364.                 if ( usResult )
  365.                     ErrMessageBox( hwnd,pData->pszTitle,usResult,NULL,0 );
  366.                 else
  367.                     WinSendDlgItemMsg( hwnd,OPEN_OK,BM_CLICK,0L,0L );
  368.                 WinSendDlgItemMsg( hwnd,OPEN_DIRLIST,LM_SELECTITEM,
  369.                                    MPFROMSHORT(pData->usSelectDir),
  370.                                    MPFROMSHORT(TRUE) );
  371.                 return 0L;
  372.  
  373.             case LN_SETFOCUS:
  374.                 ResetDefaultButton( hwnd,pData );
  375.                 usResult = (USHORT)WinSendDlgItemMsg( hwnd,OPEN_DIRLIST,
  376.                                                       LM_QUERYTOPINDEX,
  377.                                                       0L,0L );
  378.                 if ( usResult != LIT_NONE )
  379.                     {
  380.                     if ( pData->usSelectDir < usResult )
  381.                         pData->usSelectDir = usResult;
  382.                     else if (pData->usSelectDir > usResult+5)
  383.                         pData->usSelectDir = usResult+5;
  384.  
  385.                     WinSendDlgItemMsg( hwnd,OPEN_DIRLIST,LM_SELECTITEM,
  386.                                        MPFROMSHORT(pData->usSelectDir),
  387.                                        MPFROMSHORT(TRUE) );
  388.                     }
  389.                 return 0L;
  390.  
  391.             case LN_KILLFOCUS:
  392.                 WinSendDlgItemMsg( hwnd,OPEN_DIRLIST,LM_SELECTITEM,
  393.                                    MPFROMSHORT(pData->usSelectDir),
  394.                                    MPFROMSHORT(FALSE) );
  395.                 return 0L;
  396.  
  397.             case LN_SELECT:
  398.                 usResult = (USHORT)WinSendDlgItemMsg( hwnd,OPEN_DIRLIST,
  399.                                                       LM_QUERYSELECTION,
  400.                                                       0L,0L );
  401.                 if ( usResult != LIT_NONE ) pData->usSelectDir = usResult;
  402.                 return 0L;
  403.             }
  404.  
  405.         return WinDefDlgProc( hwnd,msg,mp1,mp2 );
  406.     }
  407. /****************************************************************************/
  408.  
  409.  
  410. /****************************************************************************
  411.  * FileListbox() -- Handle messages sent by file list box the open dialog.  *
  412.  ****************************************************************************/
  413.     static MRESULT near FileListbox( HWND hwnd,USHORT msg,
  414.                                      MPARAM mp1,MPARAM mp2 )
  415.     {
  416.         PDATA  pData;
  417.         USHORT usResult;
  418.  
  419.         pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  420.         switch ( SHORT2FROMMP(mp1) ) {
  421.             case LN_SELECT:
  422.                 usResult = (USHORT)WinSendDlgItemMsg( hwnd,OPEN_FILELIST,
  423.                                                       LM_QUERYSELECTION,
  424.                                                       0L,0L );
  425.                 if ( usResult != LIT_NONE ) pData->usSelectFile = usResult;
  426.                 WinSendDlgItemMsg( hwnd,OPEN_FILELIST,
  427.                                    LM_QUERYITEMTEXT,
  428.                                    MPFROM2SHORT(pData->usSelectFile,pData->usMaxPathLen),
  429.                                    MPFROMP(pData->pszScratch) );
  430.                 WinSetDlgItemText( hwnd,OPEN_FNAME,
  431.                                    pData->pszScratch );
  432.                 return 0L;
  433.  
  434.             case LN_ENTER:
  435.                 pData->usFocus = OPEN_FILELIST;
  436.                 WinSendDlgItemMsg( hwnd,OPEN_FILELIST,
  437.                                    LM_QUERYITEMTEXT,
  438.                                    MPFROM2SHORT(pData->usSelectFile,pData->usMaxPathLen),
  439.                                    MPFROMP(pData->pszScratch) );
  440.                 WinSetDlgItemText( hwnd,OPEN_FNAME,pData->pszScratch );
  441.                 WinSendDlgItemMsg( hwnd,OPEN_OK,BM_CLICK,0L,0L );
  442.                 return 0L;
  443.  
  444.             case LN_SETFOCUS:
  445.                 ResetDefaultButton( hwnd,pData );
  446.                 usResult = (USHORT)WinSendDlgItemMsg( hwnd,OPEN_FILELIST,
  447.                                                       LM_QUERYTOPINDEX,
  448.                                                       0L,0L );
  449.                 if ( usResult != LIT_NONE )
  450.                     {
  451.                     if ( pData->usSelectFile < usResult )
  452.                         pData->usSelectFile = usResult;
  453.                     else if (pData->usSelectFile > usResult+8)
  454.                         pData->usSelectFile = usResult+8;
  455.  
  456.                     WinSendDlgItemMsg( hwnd,OPEN_FILELIST,LM_SELECTITEM,
  457.                                        MPFROMSHORT(pData->usSelectFile),
  458.                                        MPFROMSHORT(TRUE) );
  459.                     }
  460.                 return 0L;
  461.  
  462.             case LN_KILLFOCUS:
  463.                 WinSendDlgItemMsg( hwnd,OPEN_FILELIST,LM_SELECTITEM,
  464.                                    MPFROMSHORT(pData->usSelectFile),
  465.                                    MPFROMSHORT(FALSE) );
  466.                 return 0L;
  467.             }
  468.  
  469.         return WinDefDlgProc( hwnd,msg,mp1,mp2 );
  470.     }
  471. /****************************************************************************/
  472.  
  473.  
  474. /****************************************************************************
  475.  * FnameEditCtrl() -- Handles messages sent by OPEN_FNAME edit control      *
  476.  *                    to open file dialog box.                              *
  477.  ****************************************************************************/
  478.     static MRESULT near FnameEditCtrl( HWND hwnd,USHORT msg,
  479.                                        MPARAM mp1,MPARAM mp2 )
  480.     {
  481.         USHORT usResult;
  482.         PDATA  pData;
  483.  
  484.         pData = (PDATA)WinQueryWindowULong( hwnd,QWL_USER );
  485.  
  486.         if ( SHORT2FROMMP(mp1) == EN_SETFOCUS )
  487.             {
  488.             ResetDefaultButton( hwnd,pData );
  489.             usResult = WinQueryDlgItemTextLength( hwnd,OPEN_FNAME );
  490.             WinSendDlgItemMsg( hwnd,OPEN_FNAME,EM_SETSEL,
  491.                                MPFROM2SHORT(0,usResult),0L );
  492.             return 0L;
  493.             }
  494.  
  495.         return WinDefDlgProc( hwnd,msg,mp1,mp2 );
  496.     }
  497. /****************************************************************************/
  498.  
  499.  
  500. /****************************************************************************
  501.  * OpenButton() - Procedure to executed when OPEN button is clicked.        *
  502.  ****************************************************************************/
  503.     static MRESULT near OpenButton( HWND hwnd,PDATA pData )
  504.     {
  505.         HWND    hwndButton;
  506.  
  507.         hwndButton = WinWindowFromID( hwnd,OPEN_OK );
  508.  
  509.         if ( hwndButton == WinQueryFocus(HWND_DESKTOP,FALSE) )
  510.             {
  511.             switch ( pData->usFocus ) {
  512.                 case OPEN_FNAME:
  513.                 case OPEN_FILELIST:
  514.                     WinQueryDlgItemText( hwnd,OPEN_FNAME,
  515.                                          pData->usMaxPathLen,
  516.                                          pData->pszScratch );
  517.                     if ( !OpenFile(hwnd,pData) ) WinDismissDlg( hwnd,FDLG_OK );
  518.                     break;
  519.  
  520.                 case OPEN_DRIVES:
  521.                 case OPEN_DIRLIST:
  522.                     FillListBoxes( hwnd,pData );
  523.                     break;
  524.                 }
  525.             WinSetFocus( HWND_DESKTOP,WinWindowFromID(hwnd,pData->usFocus) );
  526.             pData->usFocus = OPEN_FNAME;
  527.             }
  528.  
  529.         return 0L;
  530.     }
  531. /****************************************************************************/
  532.  
  533.  
  534. /****************************************************************************
  535.  * FillListBoxes() - Fill drive, directory, and file list boxes, and        *
  536.  *                   display the name of the current drive and directory    *
  537.  *                   in the OPEN_CURDIR control.                            *
  538.  *                                                                          *
  539.  *                   This function uses the scratch data area.              *
  540.  *                                                                          *
  541.  *                   Currently, this function does not support OS/2 v1.2    *
  542.  *                   long file names. To fix this, recompile using the      *
  543.  *                   OS/2 1.2 header files so as to use the updated         *
  544.  *                   definition of the FILEFINDBUF structure.               *
  545.  ****************************************************************************/
  546.     static void near FillListBoxes( HWND hDlg,PDATA pData )
  547.     {
  548.         USHORT      usDriveNum;
  549.         ULONG       ulMap;
  550.         USHORT      usResult;
  551.         HDIR        hdir;
  552.         FILEFINDBUF findbuf;
  553.         USHORT      usCount;
  554.  
  555.     /* Clear current contents of list boxes and text controls */
  556.         WinSendDlgItemMsg( hDlg,OPEN_DRIVES,LM_DELETEALL,NULL,NULL );
  557.         WinSendDlgItemMsg( hDlg,OPEN_DIRLIST,LM_DELETEALL,NULL,NULL );
  558.         WinSendDlgItemMsg( hDlg,OPEN_FILELIST,LM_DELETEALL,NULL,NULL );
  559.         WinSetDlgItemText( hDlg,OPEN_CURDIR,"" );
  560.         WinSetDlgItemText( hDlg,OPEN_FNAME,pData->pszShowSpec );
  561.  
  562.         pData->usSelectDrive = 0;
  563.         pData->usSelectDir   = 0;
  564.         pData->usSelectFile  = 0;
  565.  
  566.     /* Fill in disk drive list box */
  567.         if ( usResult = DosQCurDisk(&usDriveNum,&ulMap) )
  568.             {
  569.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  570.             return;
  571.             }
  572.  
  573.         pData->pszScratch[1] = ':';
  574.         pData->pszScratch[2] = '\0';
  575.         for ( usCount = 0; usCount < 26; usCount++ )
  576.             if ( ulMap & 1L << usCount )
  577.                 {
  578.                 pData->pszScratch[0] = (CHAR)usCount + 'A';
  579.  
  580.                 usResult = SHORT1FROMMR(
  581.                             WinSendDlgItemMsg( hDlg,OPEN_DRIVES,
  582.                                                LM_INSERTITEM,
  583.                                                MPFROMSHORT(LIT_END),
  584.                                                MPFROMP(pData->pszScratch) )
  585.                                        );
  586.                 if ( usCount == usDriveNum-1 )
  587.                     {
  588.                     WinSendDlgItemMsg( hDlg,OPEN_DRIVES,
  589.                                        LM_SETTOPINDEX,
  590.                                        MPFROMSHORT(usResult),
  591.                                        0L );
  592.                     pData->usSelectDrive = usResult;
  593.                     }
  594.                 }
  595.  
  596.     /* Set OPEN_CURDIR static text control to current drive/directory */
  597.        usCount = pData->usMaxPathLen-3;
  598.        if ( usResult = DosQCurDir(0,pData->pszScratch+3,&usCount) )
  599.             {
  600.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  601.             return;
  602.             }
  603.         pData->pszScratch[0] = (CHAR)usDriveNum + '@';
  604.         pData->pszScratch[1] = ':';
  605.         pData->pszScratch[2] = '\\';
  606.         WinSetDlgItemText( hDlg,
  607.                            OPEN_CURDIR,
  608.                            FitPathToBox(hDlg,OPEN_CURDIR,pData->pszScratch) );
  609.  
  610.     /* Fill list box with subdirectories of current directory */
  611.         hdir    = HDIR_CREATE;
  612.         usCount = 1;
  613.         usResult = DosFindFirst( szStarDotStar,&hdir,FILE_DIRECTORY,&findbuf,
  614.                                   sizeof(findbuf),&usCount,0L );
  615.  
  616.         while ( !usResult )
  617.             {
  618.             if (   (findbuf.attrFile & FILE_DIRECTORY)
  619.                 && (findbuf.achName[0] != '.' || findbuf.achName[1]) )
  620.                 {
  621.                 WinSendDlgItemMsg( hDlg,OPEN_DIRLIST,LM_INSERTITEM,
  622.                                    MPFROMSHORT(LIT_END),
  623.                                    MPFROMP(findbuf.achName) );
  624.                 }
  625.             usResult = DosFindNext( hdir,&findbuf,sizeof(findbuf),&usCount );
  626.             }
  627.  
  628.         if ( usResult != ERROR_NO_MORE_SEARCH_HANDLES ) DosFindClose(hdir);
  629.         if ( usResult && usResult != ERROR_NO_MORE_FILES )
  630.             {
  631.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  632.             return;
  633.             }
  634.  
  635.     /* Fill file list box with list of files that match search specs. */
  636.         hdir    = HDIR_CREATE;
  637.         usCount = 1;
  638.         usResult = DosFindFirst( pData->pszShowSpec,&hdir,
  639.                                  pData->usShowAttr,&findbuf,
  640.                                  sizeof(findbuf),&usCount,0L );
  641.  
  642.         while ( !usResult )
  643.             {
  644.             WinSendDlgItemMsg( hDlg,OPEN_FILELIST,LM_INSERTITEM,
  645.                                MPFROMSHORT(LIT_END),
  646.                                MPFROMP(findbuf.achName) );
  647.  
  648.             usResult = DosFindNext( hdir,&findbuf,sizeof(findbuf),&usCount );
  649.             }
  650.  
  651.         if ( usResult != ERROR_NO_MORE_SEARCH_HANDLES ) DosFindClose(hdir);
  652.         if ( usResult && usResult != ERROR_NO_MORE_FILES )
  653.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  654.  
  655.     /* Done. Return to caller. */
  656.         return;
  657.     }
  658. /****************************************************************************/
  659.  
  660.  
  661. /****************************************************************************
  662.  * OpenFile() - This function attempts to open the file specified           *
  663.  *              in the scratch data area, or if the file is a search        *
  664.  *              specification, updates the contents of the list boxes.      *
  665.  *                                                                          *
  666.  *              This function returns a non-zero value if an error occured  *
  667.  *              or the input string was a search specification.             *
  668.  ****************************************************************************/
  669.     static USHORT near OpenFile( HWND hDlg,PDATA pData )
  670.     {
  671.         USHORT  usResult;
  672.  
  673.         usResult = ParseFileName( pData->pszScratch,
  674.                                   pData->pszFile,
  675.                                   pData->pszShowSpec );
  676.         if ( usResult )
  677.             {
  678.             ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  679.             return 1;
  680.             }
  681.  
  682.         if ( NULL != strpbrk(pData->pszFile,szWildCardChars) )
  683.             {
  684.             strcpy( pData->pszShowSpec,strrchr(pData->pszFile,'\\')+1 );
  685.             FillListBoxes( hDlg,pData );
  686.             return 1;
  687.             }
  688.         else
  689.             {
  690.             usResult = DosOpen( pData->pszFile,
  691.                                 pData->phf,
  692.                                 pData->pusAction,
  693.                                 pData->ulFileSize,
  694.                                 pData->usAttribute,
  695.                                 pData->fsOpenFlags,
  696.                                 pData->fsOpenMode,
  697.                                 pData->ulReserved );
  698.             if ( usResult )
  699.                 {
  700.                 ErrMessageBox( hDlg,pData->pszTitle,usResult,NULL,0 );
  701.                 return 1;
  702.                 }
  703.             else
  704.                 return 0;
  705.             }
  706.     }
  707. /****************************************************************************/
  708.  
  709.  
  710. /****************************************************************************
  711.  * ResetDefaultButton() - Makes the OPEN button the default pushbutton      *
  712.  *                        whenever the focus is passed to a non-pushbutton  *
  713.  *                        control.                                          *
  714.  ****************************************************************************/
  715.     static void near ResetDefaultButton( HWND hwnd,PDATA pData )
  716.     {
  717.         ULONG ulStyle;
  718.  
  719.         ulStyle = WinQueryWindowULong( WinWindowFromID(hwnd,OPEN_OK),
  720.                                        QWL_STYLE );
  721.         if ( !(ulStyle & BS_DEFAULT) )
  722.             {
  723.             /* Make OPEN button default */
  724.             WinSendDlgItemMsg( hwnd,OPEN_OK,BM_SETDEFAULT,
  725.                                MPFROMSHORT(TRUE),0L );
  726.  
  727.             /* Remove default attribute from CANCEL button */
  728.             ulStyle = WinQueryWindowULong( WinWindowFromID(hwnd,OPEN_CANCEL),
  729.                                            QWL_STYLE );
  730.             if ( ulStyle & BS_DEFAULT )
  731.                 WinSendDlgItemMsg( hwnd,OPEN_CANCEL,BM_SETDEFAULT,
  732.                                    MPFROMSHORT(FALSE),0L );
  733.  
  734.             /* Remove default attribute from FIND button */
  735.             ulStyle = WinQueryWindowULong( WinWindowFromID(hwnd,OPEN_FIND),
  736.                                            QWL_STYLE );
  737.             if ( ulStyle & BS_DEFAULT )
  738.                 WinSendDlgItemMsg( hwnd,OPEN_FIND,BM_SETDEFAULT,
  739.                                    MPFROMSHORT(FALSE),0L );
  740.  
  741.             /* Remove default attribute from HELP button */
  742.             if ( pData->pfnHelpProc != NULL )
  743.                 {
  744.                 ulStyle = WinQueryWindowULong( WinWindowFromID(hwnd,OPEN_HELP),
  745.                                                QWL_STYLE );
  746.                 if ( ulStyle & BS_DEFAULT )
  747.                     WinSendDlgItemMsg( hwnd,OPEN_HELP,BM_SETDEFAULT,
  748.                                        MPFROMSHORT(FALSE),0L );
  749.                 }
  750.             }
  751.  
  752.         return;
  753.     }
  754. /****************************************************************************/
  755.