home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1991 / 10 / browse.c < prev    next >
C/C++ Source or Header  |  1991-02-26  |  13KB  |  323 lines

  1. /***********************************************************************
  2.  *                                                                     *
  3.  *  MODULE      : BROWSE.c                                             *
  4.  *                                                                     *
  5.  *  PURPOSE     : Contains the Browse dialog and helper functions.     *
  6.  *                                                                     *
  7.  *  FUNCTIONS   : IsWild ()       - Checks a string for DOS wildcard   *
  8.  *                                  characters.                        *
  9.  *                                                                     *
  10.  *                FileExists()    - Checks to see if a file exists.    *                                   *
  11.  *                                                                     *
  12.  *                SelectFile()    - If the filename supplied has a     *
  13.  *                                  wildcard, this function fills the  *
  14.  *                                  listboxes in the Browse dialog,    *
  15.  *                                  otherwise it sets the filename     *
  16.  *                                  and closes the dialog.             *
  17.  *                                                                     *
  18.  *                ChangeExt()     - Changes the default file extension *
  19.  *                                  mask for the Browse dialog.        *
  20.  *                                                                     *
  21.  *                BrowseDlg()     - Dialog funcion for the Browse      *
  22.  *                                  dialog.                            *
  23.  *                                                                     *
  24.  ***********************************************************************/
  25. #include "browse.h"
  26.  
  27. #include <sys\types.h>     /* Additional includes needed      */
  28. #include <sys\stat.h>      /* for the fstat() function        */    
  29.  
  30. OFSTRUCT OfStruct;         /* Information from OpenFile()     */
  31.  
  32. char szProgName[128];
  33. char szDefSpec[14]="*.*";
  34.  
  35. /**********************************************************************
  36.  *                                                                    *
  37.  *  FUNCTION   : IsWild(PSTR)                                         *
  38.  *                                                                    *
  39.  *  PURPOSE    : Checks if the string (referenced by a NEAR pointer)  *
  40.  *               contains a DOS wildcard character ("*" or "?").      *
  41.  *                                                                    *
  42.  *  RETURNS    : TRUE  - if the string contains a wildcard character. *
  43.  *               FALSE - otherwise.                                   *
  44.  *                                                                    *
  45.  **********************************************************************/
  46. BOOL NEAR PASCAL IsWild(psFileName)
  47. register PSTR psFileName;
  48. {
  49.     for(;;)
  50.        switch (*psFileName++)
  51.               {
  52.               case '*':
  53.               case '?':
  54.                    /* Found wildcard */
  55.                    return (TRUE);
  56.  
  57.               case 0:
  58.                    /* Reached end of string */
  59.                    return (FALSE);
  60.  
  61.               default:
  62.                    continue;
  63.               }
  64. }
  65.  
  66.  
  67. /***********************************************************************
  68.  *                                                                     *
  69.  *  FUNCTION   : FileExists(PSTR)                                      *
  70.  *                                                                     *
  71.  *  PURPOSE    : Checks to see if a file exists with the path/filename *
  72.  *               described by the string pointed to by 'psFileName'.   *
  73.  *                                                                     *
  74.  *  RETURNS    : TRUE  - if the described file does exist.             *
  75.  *               FALSE - otherwise.                                    *
  76.  *                                                                     *
  77.  ***********************************************************************/
  78. BOOL NEAR FileExists(PSTR psFileName)
  79. {
  80.     int hFile;  /* DOS file handle */
  81.  
  82.     if ((hFile = _lopen((LPSTR)psFileName, NULL)) < 0)
  83.        return (FALSE);
  84.  
  85.     _lclose(hFile);
  86.  
  87.     return (TRUE);
  88. }
  89.  
  90.  
  91. /***********************************************************************
  92.  *                                                                     *
  93.  *  FUNCTION   : SelectFile (HWND)                                     *
  94.  *                                                                     *
  95.  *  PURPOSE    : Reads the string in the edit control of the File/Open *
  96.  *               dialog. If it contains a wildcard, then it attempts   *
  97.  *               to fill the listboxes in the File/Open dialog.        *
  98.  *               otherwise it ends the dialog.                         *
  99.  *                                                                     *
  100.  ***********************************************************************/
  101. VOID NEAR PASCAL SelectFile(hDlg)
  102. HWND hDlg;
  103. {
  104.  
  105.     /* Get the text from the dialog's edit control into this address */
  106.     GetDlgItemText(hDlg, IDC_FILENAME, szProgName, 64);
  107.  
  108.     if (IsWild(szProgName))
  109.        {
  110.        /* Select the directory and make a listing of the directories */
  111.        DlgDirList(hDlg, (LPSTR)szProgName, IDC_DIRS, IDC_PATH, ATTR_DIRS);
  112.  
  113.        /* List the files in this directory based on the wildcard. */
  114.        DlgDirList(hDlg, (LPSTR)szProgName, IDC_FILES, IDC_PATH, ATTR_FILES);
  115.  
  116.        /* Set the dialog's edit control to the filename. */
  117.        SetDlgItemText (hDlg, IDC_FILENAME, szProgName);
  118.        }
  119.     else /* The filename is not a wildcard */
  120.        {
  121.        if (FileExists(szProgName))
  122.           {
  123.           OpenFile (szProgName, &OfStruct, OF_EXIST);
  124.           lstrcpy (szProgName, OfStruct.szPathName);
  125.           EndDialog (hDlg, TRUE);
  126.           }
  127.        else /* file does not exist */
  128.           {
  129.           /* see if it is a directory without a wildcard */ 
  130.           if (DlgDirList(hDlg, (LPSTR)szProgName, IDC_DIRS, IDC_PATH, ATTR_DIRS))
  131.              {
  132.              lstrcpy((LPSTR)szProgName, (LPSTR)szDefSpec);
  133.              DlgDirList(hDlg, (LPSTR)szProgName, IDC_FILES, IDC_PATH, ATTR_FILES);
  134.              SetDlgItemText(hDlg, IDC_FILENAME, szProgName);
  135.              }
  136.           else /* path was invalid - error */
  137.              {
  138.              lstrcpy((LPSTR)str,"Cannot find file ");
  139.              lstrcat((LPSTR)str,(LPSTR)szProgName);
  140.              lstrcat((LPSTR)str," !\n\n");
  141.              lstrcat((LPSTR)str,"Check to ensure the path and \n");
  142.              lstrcat((LPSTR)str,"filename are correct.");
  143.              MessageBox(hDlg,str,"Browse",MB_OK | MB_ICONEXCLAMATION);
  144.              SendDlgItemMessage(hDlg, IDC_FILENAME, EM_SETSEL,
  145.                                  NULL,MAKELONG(0,64)); 
  146.              SetActiveWindow(hDlg);
  147.              }
  148.           }
  149.        }
  150. }
  151.  
  152.  
  153. /**********************************************************************
  154.  *                                                                    *
  155.  *  FUNCTION   : ChangeExt(HWND,PSTR)                                 *
  156.  *                                                                    *
  157.  *  PURPOSE    : Changes the file extension after an 'Open From'.     *
  158.  *                                                                    *
  159.  *  RETURNS    : void                                                 *
  160.  *                                                                    *
  161.  **********************************************************************/
  162. void NEAR ChangeExt(hDlg,psNewExt)
  163. HWND hDlg;
  164. PSTR psNewExt;
  165. {
  166.     PSTR psNameTemp;
  167.     PSTR psExtTemp;
  168.  
  169.     /* set default file spec */
  170.     lstrcpy (szDefSpec, "*.");
  171.     lstrcat (szDefSpec, psNewExt);
  172.  
  173.     /* set szProgName to new default spec */
  174.     lstrcpy(szProgName,szDefSpec);
  175.  
  176.     /* update the FILENAME edit control to show new extension */
  177.     SetDlgItemText(hDlg,IDC_FILENAME,szProgName);
  178.  
  179.     /* select files that qualify with new extension */
  180.     SelectFile(hDlg);
  181. }
  182.  
  183.  
  184. /***********************************************************************
  185.  *                                                                     *
  186.  *  FUNCTION   : BrowseDlg()                                           *
  187.  *                                                                     *
  188.  *  PURPOSE    : Dialog function for the 'Browse' dialog.  Takes care  *
  189.  *               of calling the appropriate functions for extracting   *
  190.  *               the filename and wildcard, filling the listboxes and  *
  191.  *               changing the ProgName.                                *
  192.  *                                                                     *
  193.  *  RETURNS    : TRUE if a file was selected, else FALSE.              *
  194.  *                                                                     *
  195.  ***********************************************************************/
  196. BOOL FAR PASCAL BrowseDlg(hDlg, message, wParam, lParam)
  197. HWND hDlg;
  198. WORD message;
  199. WORD wParam;
  200. LONG lParam;
  201. {
  202.  
  203.     switch (message)
  204.     {
  205.  
  206.     case WM_INITDIALOG:
  207.  
  208.          /* Set the default file extension on edit window, and try to
  209.           * get a listing of the files and directories.    */
  210.          SetDlgItemText (hDlg, IDC_FILENAME, "*.*");
  211.          SendDlgItemMessage (hDlg, IDC_FILENAME, EM_LIMITTEXT, 80, NULL);
  212.          SendDlgItemMessage (hDlg, IDC_FILENAME, EM_SETSEL,
  213.                              NULL,MAKELONG(NULL,80)); 
  214.  
  215.          /* set the current default file mask specification to '*.*'   */
  216.          SendDlgItemMessage (hDlg, IDC_DEF_STAR, BM_SETCHECK, TRUE, NULL);
  217.          SendMessage (hDlg, WM_COMMAND, IDC_DEF_STAR, NULL);
  218.  
  219.          break;
  220.  
  221.     case WM_COMMAND:
  222.          switch (wParam)
  223.          {
  224.          case IDOK:
  225.               SelectFile(hDlg);
  226.               break;
  227.  
  228.          case IDCANCEL:
  229.               /* Set szProgName to NULL and quit */
  230.               *szProgName = 0;
  231.               EndDialog (hDlg, FALSE);
  232.               break;
  233.  
  234.          case IDC_FILENAME:
  235.               /* Enable the OK button if the edit control has text. */
  236.               EnableWindow (GetDlgItem (hDlg, IDOK),
  237.                             GetWindowTextLength((HWND)LOWORD (lParam)));
  238.               break;
  239.  
  240.          case IDC_FILES:
  241.  
  242.               /* The files listbox. If file selection has changed,
  243.                * set text in edit control. */
  244.               if (HIWORD(lParam) == LBN_SELCHANGE)
  245.                  {
  246.                  DlgDirSelect (hDlg, (LPSTR)szProgName, IDC_FILES);
  247.                  SetDlgItemText (hDlg, IDC_FILENAME, (LPSTR)szProgName);
  248.                  }
  249.               else if (HIWORD(lParam) == LBN_DBLCLK)
  250.                       /* if the item was double-clicked, try to open it */
  251.                       SelectFile(hDlg);
  252.               break;
  253.  
  254.          case IDC_DIRS:
  255.  
  256.               /* The directories listbox. Append current filename in edit
  257.                */
  258.               if (HIWORD(lParam) == LBN_SELCHANGE)
  259.                  {
  260.                  PSTR psFilePart, psTemp1, psTemp2;
  261.  
  262.                  /* Get the new drive/dir and copy it to the TempName */
  263.                  DlgDirSelect (hDlg, szProgName, IDC_DIRS);
  264.                  psFilePart = szProgName + lstrlen(szProgName);
  265.  
  266.                  /* Get contents of dialog's edit control
  267.                   * and add it to the TempName */
  268.                  GetDlgItemText(hDlg,IDC_FILENAME,(LPSTR)psFilePart,80);
  269.  
  270.                  if (*psFilePart == 0 || !IsWild(psFilePart))
  271.                     {  /* file part is null or non-wild */
  272.                     lstrcpy((LPSTR)psFilePart,(LPSTR)szDefSpec);
  273.                     }
  274.                  else
  275.                     {  /* file part is wild - remove old dir part if any */
  276.                     for (psTemp1=psTemp2=psFilePart; *psTemp1; psTemp1++)
  277.                         {
  278.                         if (*psTemp1 == '\\' || *psTemp1 == ':')
  279.                            psTemp2 = psFilePart;
  280.                         else
  281.                            *psTemp2++ = *psTemp1;
  282.                         }
  283.                     *psTemp2 = 0;
  284.                     }
  285.                                    
  286.                  /* Set the edit control with new string */
  287.                  SetDlgItemText (hDlg, IDC_FILENAME, (LPSTR)szProgName);
  288.                  }
  289.               else if (HIWORD(lParam) == LBN_DBLCLK)
  290.                  SelectFile (hDlg);
  291.               break;
  292.  
  293.          case IDC_DEF_STAR:
  294.               ChangeExt(hDlg,"*");
  295.               break;
  296.  
  297.          case IDC_DEF_1:
  298.               ChangeExt(hDlg,"EXE");
  299.               break;
  300.  
  301.          case IDC_DEF_2:
  302.               ChangeExt(hDlg,"PIF");
  303.               break;
  304.  
  305.          case IDC_DEF_3:
  306.               ChangeExt(hDlg,"BAT");
  307.               break;
  308.  
  309.          case IDC_DEF_4:
  310.               ChangeExt(hDlg,"COM");
  311.               break;
  312.  
  313.          default:
  314.               return (FALSE);
  315.          }
  316.          break;
  317.  
  318.     default:
  319.          return (FALSE);
  320.     }
  321.     return (TRUE);
  322. }
  323.