home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 19 / CD_ASCQ_19_010295.iso / vrac / mpad32.zip / MPOPEN.C < prev    next >
C/C++ Source or Header  |  1994-04-15  |  14KB  |  323 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /***************************************************************************
  13.  *                                                                         *
  14.  *  MODULE      : MpOpen.c                                                 *
  15.  *                                                                         *
  16.  *  PURPOSE     : Contains the file open dialog function and it's helper   *
  17.  *                functions.                                               *
  18.  *                                                                         *
  19.  *  FUNCTIONS   : IsWild ()           - Ascertains that the input string   *
  20.  *                                      contains a DOS wildcard character. *
  21.  *                                                                         *
  22.  *                SelectFile()        - If filename supplied contains a    *
  23.  *                                      wildcard, this function fills the  *
  24.  *                                      listboxes in File/Open dialog, else*
  25.  *                                      the dialog is closed.              *
  26.  *                                                                         *
  27.  *                FileOpenDlgProc()   - Dialog funcion for the File/Open   *
  28.  *                                      dialog.                            *
  29.  *                                                                         *
  30.  *                GetFileName ()      - Gets a file name from the user.    *
  31.  *                                                                         *
  32.  ***************************************************************************/
  33. #include "multipad.h"
  34. #include <fcntl.h>
  35. #include <io.h>
  36. #include <string.h>
  37. CHAR szPropertyName [] = "FILENAME";/* Name of the File name property list item */
  38.  
  39.  
  40.  
  41. /****************************************************************************
  42.  *                                                                          *
  43.  *  FUNCTION   : IsWild ( psz )                                             *
  44.  *                                                                          *
  45.  *  PURPOSE    : Checks if the string (referenced by a NEAR pointer)        *
  46.  *               contains a DOS wildcard character ("*" or "?").            *
  47.  *                                                                          *
  48.  *  RETURNS    : TRUE  - iff the string contains a wildcard character.      *
  49.  *               FALSE - otherwise.                                  .      *
  50.  *                                                                          *
  51.  ****************************************************************************/
  52. BOOL NEAR PASCAL IsWild(register PSTR psz)
  53. {
  54.     for(;;)
  55.         switch (*psz++){
  56.             case '*':
  57.             case '?':
  58.                 /* Found wildcard */
  59.                 return TRUE;
  60.  
  61.             case 0:
  62.                 /* Reached end of string */
  63.                 return FALSE;
  64.  
  65.             default:
  66.                 continue;
  67.         }
  68. }
  69.  
  70. /****************************************************************************
  71.  *                                                                          *
  72.  *  FUNCTION   : FileExists(pch)                                            *
  73.  *                                                                          *
  74.  *  PURPOSE    : Checks to see if a file exists with the path/filename      *
  75.  *               described by the string pointed to by 'pch'.               *
  76.  *                                                                          *
  77.  *  RETURNS    : TRUE  - if the described file does exist.                  *
  78.  *               FALSE - otherwise.                                         *
  79.  *                                                                          *
  80.  ****************************************************************************/
  81.  
  82.  
  83. BOOL FileExists(PSTR pch)
  84. {
  85.         int fh;
  86.  
  87.         if ((fh = open(pch, O_RDONLY)) < 0)
  88.              return(FALSE);
  89.  
  90.         _lclose(fh);
  91.         return(TRUE);
  92. }
  93.  
  94. /****************************************************************************
  95.  *                                                                          *
  96.  *  FUNCTION   : SelectFile ( hwnd )                                        *
  97.  *                                                                          *
  98.  *  PURPOSE    : Reads the string in the edit control of the File/Open      *
  99.  *               dialog. If it contains a wildcard, then it attempts to     *
  100.  *               fill the listboxes in the File/Open dialog. Othewise it    *
  101.  *               ends the dialog. Modifies the FILENAME item in the property*
  102.  *               list of the window.                                        *
  103.  *                                                                          *
  104.  ****************************************************************************/
  105.  
  106. VOID NEAR PASCAL SelectFile(register HWND hwnd)
  107. {
  108.     register PSTR pch;
  109.     PSTR          pch2;
  110.  
  111.     /* Get handle (near address) to filename data in window's property list */
  112.     pch = (PSTR)GetProp (hwnd, PROP_FILENAME);
  113.  
  114.     /* Get the text from the dialog's edit control into this address */
  115.     GetDlgItemText (hwnd, IDD_FILENAME, pch, 64);
  116.  
  117.     if ( IsWild (pch)){
  118.         /* Select the directory and make a listing of the directories */
  119.         DlgDirList(hwnd, (LPSTR)pch, (int)IDD_DIRS, (int)IDD_PATH, (WORD)ATTR_DIRS);
  120.  
  121.         /* Obtain the filename-only part of the path in the edit control */
  122.         for (pch2 = pch; *pch; pch++)
  123.             if (*pch == '\\' || *pch == ':')
  124.                 pch2 = pch + 1;
  125.  
  126.         /* List the files in this directory based on the wildcard. */
  127.         DlgDirList(hwnd, (LPSTR)pch2, IDD_FILES, IDD_PATH, ATTR_FILES);
  128.  
  129.         /* Set the dialog's edit control to the filename part of path
  130.          * string.
  131.          */
  132.         SetDlgItemText (hwnd, IDD_FILENAME, pch2);
  133.     }
  134.     else
  135.     {
  136.         /* The filename in the property list is not a wildcard */
  137.         if (FileExists (pch)){
  138.  
  139.             RemoveProp (hwnd, PROP_FILENAME);
  140.             EndDialog (hwnd, 0);
  141.         }
  142.         else{
  143.             MPError ( hwnd, MB_OK | MB_SYSTEMMODAL, IDS_CANTOPEN, (LPSTR) pch);
  144.             SetActiveWindow (hwnd);
  145.         }
  146.     }
  147. }
  148.  
  149.  
  150.  
  151. /****************************************************************************
  152.  *                                                                          *
  153.  *  FUNCTION   : FileOpenDlgProc()                                          *
  154.  *                                                                          *
  155.  *  PURPOSE    : Dialog function for the File/Open dialog. Takes care of    *
  156.  *               calling the appropriate functions for extracting the       *
  157.  *               filename and wildcard, filling the listboxes and changing  *
  158.  *               the FILENAME item in the property list for the window.     *
  159.  *                                                                          *
  160.  ****************************************************************************/
  161.  
  162. BOOL APIENTRY FileOpenDlgProc (
  163.         register HWND hwnd,
  164.         WORD          message,
  165.         register UINT wParam,
  166.         LONG          lParam)
  167. {
  168.     PSTR pch;
  169.  
  170.     switch (message){
  171.  
  172.         case WM_INITDIALOG:
  173.             /* Set the default file extension on edit window, and try to
  174.              * get a listing of the files and directories.
  175.              */
  176.             SetDlgItemText ( hwnd, IDD_FILENAME, DEFFILESEARCH);
  177.             SetProp (hwnd, PROP_FILENAME, (HANDLE) lParam);
  178.             SendDlgItemMessage (hwnd, IDD_FILENAME, EM_LIMITTEXT, 64, 0L);
  179.             SelectFile (hwnd);
  180.             break;
  181.  
  182.         case WM_COMMAND:
  183.             switch (LOWORD(wParam)) {
  184.                 case IDOK:
  185.                     SelectFile(hwnd);
  186.                     break;
  187.  
  188.                 case IDCANCEL:
  189.                     /* Set the filename in the prop. list to NULL and quit */
  190.                     pch  = (PSTR) GetProp (hwnd, PROP_FILENAME);
  191.                     *pch = 0;
  192.                     EndDialog (hwnd, 0);
  193.                     break;
  194.  
  195.                 case IDD_FILENAME:
  196.                     /* Enable the OK button if the edit control has text. */
  197.                     EnableWindow ( GetDlgItem (hwnd, IDOK),
  198.                                    GetWindowTextLength (GET_WM_COMMAND_HWND(wParam, lParam)));
  199.                     break;
  200.  
  201.                 case IDD_FILES:
  202.  
  203.                     /* The files listbox. If file selection has changed, fill
  204.                      * the new filename into the property list buffer and set
  205.                      * text in edit control.
  206.                      */
  207.                     if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_SELCHANGE){
  208.                             pch = (PSTR) GetProp (hwnd, PROP_FILENAME);
  209.                             DlgDirSelectEx(hwnd, (LPSTR)pch, 128, IDD_FILES);
  210.                             SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch);
  211.                     }
  212.                     else if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_DBLCLK)
  213.                             /* if the item was double-clicked, try to open it */
  214.                             SelectFile(hwnd);
  215.                     break;
  216.  
  217.                 case IDD_DIRS:
  218.  
  219.                     /* The directories listbox. Append current filename in edit
  220.                      * control (stripped of the path prefix) to the name from
  221.                      * the property list and set the new string in the edit
  222.                      * control.
  223.                      */
  224.                     if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_SELCHANGE) {
  225.  
  226.                             PSTR pch2, pchT, pchS;
  227.  
  228.                             pch = (PSTR) GetProp (hwnd, PROP_FILENAME);
  229.  
  230.                             /* Get the new drive/dir */
  231.                             DlgDirSelectEx(hwnd, pch, 128, IDD_DIRS);
  232.                             pch2 = pch + lstrlen(pch);
  233.  
  234.                             /* Fetch current contents of dialog's edit control and append
  235.                              * it to name from property list... */
  236.                             GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64);
  237.                             if (*pch2 == 0){
  238.                                 SetDlgItemText(hwnd, IDD_FILENAME, DEFFILESEARCH);
  239.                                 GetDlgItemText(hwnd,IDD_FILENAME,(LPSTR)pch2,64);
  240.                             }
  241.                             else {
  242.                                 pchS = pch;
  243.                                 for (pchT = pch = pch2; *pch; pch++) {
  244.                                         if (*pch == '\\' || *pch == ':')
  245.                                             pchT = pch2;
  246.                                         else
  247.                                             *pchT++ = *pch;
  248.                                 }
  249.                                 *pchT = 0;
  250.                                 pch = pchS;
  251.                             }
  252.  
  253.                             /* Set the edit control with new string */
  254.                             SetDlgItemText (hwnd, IDD_FILENAME, (LPSTR)pch);
  255.                     }
  256.                     else if (GET_WM_COMMAND_CMD(wParam, lParam) == LBN_DBLCLK)
  257.                             SelectFile (hwnd);
  258.                     break;
  259.  
  260.                 default:
  261.                     return FALSE;
  262.             }
  263.             break;
  264.  
  265.         default:
  266.             return FALSE;
  267.     }
  268.     return TRUE;
  269. }
  270.  
  271. /****************************************************************************
  272.  *                                                                          *
  273.  *  FUNCTION   : GetFilename ( pstr )                                       *
  274.  *                                                                          *
  275.  *  PURPOSE    : Gets a filename from the user by calling the File/Open     *
  276.  *               dialog.                                                    *
  277.  *                                                                          *
  278.  ****************************************************************************/
  279. VOID APIENTRY GetFileName(PSTR pstr)
  280. {
  281. #ifdef NOTCOMMONDIALOGS
  282.     DialogBoxParam (hInst, IDD_FILEOPEN, hwndFrame, FileOpenDlgProc, (LONG)pstr);
  283. #else
  284.     OPENFILENAME ofn;
  285.     CHAR szFilterSpec [128] =                       /* file type filters */
  286.              "TEXT FILES(*.TXT)\0*.TXT\0";
  287.  
  288.     #define MAXFILENAME 256
  289.     CHAR szFileName[MAXFILENAME];
  290.     CHAR szFileTitle[MAXFILENAME];
  291.  
  292.     strcpy(szFileName, "");   /* these need be NULL*/
  293.     strcpy(szFileTitle, "");
  294.  
  295.     /* fill in non-variant fields of OPENFILENAME struct. */
  296.     ofn.lStructSize       = sizeof(OPENFILENAME);
  297.     ofn.hwndOwner         = NULL;
  298.     ofn.lpstrFilter       = szFilterSpec;
  299.     ofn.lpstrCustomFilter = NULL;
  300.     ofn.nMaxCustFilter    = 0;
  301.     ofn.nFilterIndex      = 0;
  302.     ofn.lpstrFile         = szFileName;
  303.     ofn.nMaxFile          = MAXFILENAME;
  304.     ofn.lpstrInitialDir   = NULL;
  305.     ofn.lpstrFileTitle    = szFileTitle;
  306.     ofn.nMaxFileTitle     = MAXFILENAME;
  307.     ofn.lpstrTitle        = "Open TextFiles";
  308.     ofn.lpstrDefExt       = "TXT";
  309.     ofn.Flags             = OFN_FILEMUSTEXIST;
  310.     /* Use standard open dialog */
  311.     if (!GetOpenFileName ((LPOPENFILENAME)&ofn)){
  312.         *pstr = 0;
  313.         MessageBox(hwndFrame, " FileOpen FAILed!", "Multipad", MB_OK | IDOK);
  314.     }
  315.     else{
  316.         strcpy(pstr, ofn.lpstrFile);
  317.     }
  318.  
  319. #endif
  320.    return;
  321.  
  322. }
  323.