home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / SAMPLES / PENPAD / PPOPEN.C_ / PPOPEN.C
Encoding:
C/C++ Source or Header  |  1993-02-08  |  9.9 KB  |  275 lines

  1. /***************************************************************************
  2.  *                                     *
  3.  *  MODULE  : PpOpen.c                         *
  4.  *                                     *
  5.  *  PURPOSE : Contains the file open dialog function and it's helper   *
  6.  *        functions.                           *
  7.  *                                     *
  8.  *  FUNCTIONS   : IsWild ()       - Ascertains that the input string   *
  9.  *                  contains a DOS wildcard character. *
  10.  *                                     *
  11.  *        SelectFile()        - If filename supplied contains a    *
  12.  *                  wildcard, this function fills the  *
  13.  *                  listboxes in File/Open dialog, else*
  14.  *                  the dialog is closed.          *
  15.  *                                     *
  16.  *        FileOpenDlgProc()   - Dialog funcion for the File/Open   *
  17.  *                  dialog.                *
  18.  *                                     *
  19.  *        GetFileName ()      - Gets a file name from the user.    *
  20.  *                                     *
  21.  ***************************************************************************/
  22. #include "penpad.h"
  23.  
  24. char szPropertyName [] = "FILENAME";/* Name of the File name property list item */
  25.  
  26. int FAR PASCAL DialogBoxParam ( HANDLE, LPCSTR, HWND, FARPROC, LONG);
  27. BOOL FAR PASCAL __export FileOpenDlgProc ( HWND,WORD,WORD,LONG);
  28.  
  29.  
  30. /****************************************************************************
  31.  *                                      *
  32.  *  FUNCTION   : IsWild ( psz )                         *
  33.  *                                      *
  34.  *  PURPOSE    : Checks if the string (referenced by a NEAR pointer)        *
  35.  *       contains a DOS wildcard character ("*" or "?").        *
  36.  *                                      *
  37.  *  RETURNS    : TRUE  - iff the string contains a wildcard character.      *
  38.  *       FALSE - otherwise.                  .      *
  39.  *                                      *
  40.  ****************************************************************************/
  41. BOOL NEAR PASCAL IsWild( psz )
  42. register PSTR psz;
  43. {
  44.     for(;;)
  45.     switch (*psz++){
  46.         case '*':
  47.         case '?':
  48.         /* Found wildcard */
  49.         return TRUE;
  50.  
  51.         case 0:
  52.         /* Reached end of string */
  53.         return FALSE;
  54.  
  55.         default:
  56.         continue;
  57.     }
  58. }
  59.  
  60. /****************************************************************************
  61.  *                                      *
  62.  *  FUNCTION   : FileExists(pch)                                            *
  63.  *                                      *
  64.  *  PURPOSE    : Checks to see if a file exists with the path/filename      *
  65.  *               described by the string pointed to by 'pch'.               *
  66.  *                                      *
  67.  *  RETURNS    : TRUE  - if the described file does exist.                  *
  68.  *               FALSE - otherwise.                                         *
  69.  *                                      *
  70.  ****************************************************************************/
  71.  
  72. BOOL FileExists(PSTR pch)
  73. {
  74.     int fh;
  75.  
  76.     if ((fh = _lopen((LPSTR) pch, 0)) < 0)
  77.          return(FALSE);
  78.  
  79.     _lclose(fh);
  80.     return(TRUE);
  81. }
  82.  
  83. /****************************************************************************
  84.  *                                      *
  85.  *  FUNCTION   : SelectFile ( hwnd )                        *
  86.  *                                      *
  87.  *  PURPOSE    : Reads the string in the edit control of the File/Open      *
  88.  *       dialog. If it contains a wildcard, then it attempts to     *
  89.  *       fill the listboxes in the File/Open dialog. Othewise it    *
  90.  *       ends the dialog. Modifies the FILENAME item in the property*
  91.  *       list of the window.                        *
  92.  *                                      *
  93.  ****************************************************************************/
  94.  
  95. VOID NEAR PASCAL SelectFile( hwnd )
  96.  
  97. register HWND hwnd;
  98. {
  99.     register PSTR pch;
  100.     PSTR      pch2;
  101.  
  102.     /* Get handle (near address) to filename data in window's property list */
  103.     pch = (PSTR)GetProp (hwnd, PROP_FILENAME);
  104.  
  105.     /* Get the text from the dialog's edit control into this address */
  106.     GetDlgItemText (hwnd, IDD_FILENAME, pch, 64);
  107.  
  108.     if ( IsWild (pch)){
  109.     /* Select the directory and make a listing of the directories */
  110.     DlgDirList(hwnd, (LPSTR)pch, IDD_DIRS, IDD_PATH, ATTR_DIRS);
  111.  
  112.     /* Obtain the filename-only part of the path in the edit control */
  113.     for (pch2 = pch; *pch; pch++)
  114.         if (*pch == '\\' || *pch == ':')
  115.         pch2 = pch + 1;
  116.  
  117.     /* List the files in this directory based on the wildcard. */
  118.     DlgDirList(hwnd, (LPSTR)pch2, IDD_FILES, IDD_PATH, ATTR_FILES);
  119.  
  120.     /* Set the dialog's edit control to the filename part of path
  121.      * string.
  122.      */
  123.     SetDlgItemText (hwnd, IDD_FILENAME, pch2);
  124.     }
  125.     else
  126.     {
  127.     /* The filename in the property list is not a wildcard */
  128.     if (FileExists (pch)){
  129.  
  130.         RemoveProp (hwnd, PROP_FILENAME);
  131.         EndDialog (hwnd, 0);
  132.     }
  133.     else{
  134.         PPError ( hwnd, MB_OK | MB_SYSTEMMODAL, IDS_CANTOPEN, (LPSTR) pch);
  135.         SetActiveWindow (hwnd);
  136.     }
  137.     }
  138. }
  139. /****************************************************************************
  140.  *                                      *
  141.  *  FUNCTION   : FileOpenDlgProc()                      *
  142.  *                                      *
  143.  *  PURPOSE    : Dialog function for the File/Open dialog. Takes care of    *
  144.  *       calling the appropriate functions for extracting the       *
  145.  *       filename and wildcard, filling the listboxes and changing  *
  146.  *       the FILENAME item in the property list for the window.     *
  147.  *                                      *
  148.  ****************************************************************************/
  149.  
  150. BOOL FAR PASCAL __export FileOpenDlgProc ( hwnd, message, wParam, lParam)
  151. register HWND hwnd;
  152. WORD          message;
  153. register WORD wParam;
  154. LONG          lParam;
  155. {
  156.     PSTR pch;
  157.  
  158.     switch (message){
  159.  
  160.     case WM_INITDIALOG:
  161.         /* Set the default file extension on edit window, and try to
  162.          * get a listing of the files and directories.
  163.          */
  164.         SetDlgItemText ( hwnd, IDD_FILENAME, DEFFILESEARCH);
  165.         SetProp (hwnd, PROP_FILENAME, LOWORD(lParam));
  166.         SendDlgItemMessage (hwnd, IDD_FILENAME, EM_LIMITTEXT, 64, 0L);
  167.         SelectFile (hwnd);
  168.         break;
  169.  
  170.     case WM_COMMAND:
  171.         switch (wParam){
  172.         case IDOK:
  173.             SelectFile(hwnd);
  174.             break;
  175.  
  176.         case IDCANCEL:
  177.             /* Set the filename in the prop. list to NULL and quit */
  178.             pch  = (PSTR) GetProp (hwnd, PROP_FILENAME);
  179.             *pch = 0;
  180.             EndDialog (hwnd, 0);
  181.             break;
  182.  
  183.         case IDD_FILENAME:
  184.             /* Enable the OK button if the edit control has text. */
  185.             EnableWindow ( GetDlgItem (hwnd, IDOK),
  186.                    GetWindowTextLength ((HWND)LOWORD (lParam)));
  187.             break;
  188.  
  189.         case IDD_FILES:
  190.  
  191.             /* The files listbox. If file selection has changed, fill
  192.              * the new filename into the property list buffer and set
  193.              * text in edit control.
  194.              */
  195.             if (HIWORD(lParam) == LBN_SELCHANGE){
  196.             pch = (PSTR) GetProp (hwnd, PROP_FILENAME);
  197.             DlgDirSelect (hwnd, (LPSTR)pch, IDD_FILES);
  198.             SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch);
  199.             }
  200.             else if (HIWORD(lParam) == LBN_DBLCLK)
  201.             /* if the item was double-clicked, try to open it */
  202.             SelectFile(hwnd);
  203.             break;
  204.  
  205.         case IDD_DIRS:
  206.  
  207.             /* The directories listbox. Append current filename in edit
  208.              * control (stripped of the path prefix) to the name from
  209.              * the property list and set the new string in the edit
  210.              * control.
  211.              */
  212.             if (HIWORD(lParam) == LBN_SELCHANGE){
  213.  
  214.             PSTR pch2, pchT, pchS;
  215.  
  216.             pch = (PSTR) GetProp (hwnd, PROP_FILENAME);
  217.  
  218.             /* Get the new drive/dir */
  219.             DlgDirSelect (hwnd, pch, IDD_DIRS);
  220.             pch2 = pch + lstrlen(pch);
  221.  
  222.             /* Fetch current contents of dialog's edit control and append
  223.              * it to name from property list... */
  224.             GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64);
  225.             if (*pch2 == 0){
  226.                 SetDlgItemText(hwnd, IDD_FILENAME, DEFFILESEARCH);
  227.                 GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64);
  228.             }
  229.             else {
  230.                 pchS = pch;
  231.                 for (pchT = pch = pch2; *pch; pch++) {
  232.                 if (*pch == '\\' || *pch == ':')
  233.                     pchT = pch2;
  234.                 else
  235.                     *pchT++ = *pch;
  236.                 }
  237.                 *pchT = 0;
  238.                 pch = pchS;
  239.             }
  240.  
  241.             /* Set the edit control with new string */
  242.             SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch);
  243.             }
  244.             else if (HIWORD(lParam) == LBN_DBLCLK)
  245.             SelectFile (hwnd);
  246.             break;
  247.  
  248.         default:
  249.             return FALSE;
  250.         }
  251.         break;
  252.  
  253.     default:
  254.         return FALSE;
  255.     }
  256.     return TRUE;
  257. }
  258.  
  259. /****************************************************************************
  260.  *                                      *
  261.  *  FUNCTION   : GetFilename ( pstr )                       *
  262.  *                                      *
  263.  *  PURPOSE    : Gets a filename from the user by calling the File/Open     *
  264.  *       dialog.                            *
  265.  *                                      *
  266.  ****************************************************************************/
  267. VOID FAR PASCAL GetFileName(PSTR pstr)
  268. {
  269.     FARPROC lpfn;
  270.  
  271.     lpfn = MakeProcInstance (FileOpenDlgProc, hInst);
  272.     DialogBoxParam (hInst, IDD_FILEOPEN, hwndFrame, lpfn, MAKELONG(pstr,0));
  273.     FreeProcInstance (lpfn);
  274. }
  275.