home *** CD-ROM | disk | FTP | other *** search
/ Windows Shareware GOLD / NuclearComputingVol3No1.cdr / _bbs2 / f1246.zip / FILEOPEN.C < prev    next >
C/C++ Source or Header  |  1990-07-03  |  8KB  |  260 lines

  1. /****************************************************************************
  2.  
  3.     PROGRAM: FileOpen.c
  4.  
  5.     PURPOSE:
  6.  
  7.     FUNCTIONS:
  8.  
  9.         OpenDlg() - let user select a file, and open it.
  10.         UpdateListBox() - Update the list box of OpenDlg
  11.         ChangeDefExt() - Change the default extension
  12.         SeparateFile() - Separate filename and pathname
  13.         AddExt() - Add default extension
  14.  
  15. ****************************************************************************/
  16.  
  17. #define    _WINDOWS
  18.  
  19. #include <stdio.h>
  20. #include <string.h>
  21.  
  22. #define    NOCOMM
  23. #define    NOKANJI
  24. #define    NOATOMS
  25. #define    NOBITMAP
  26. #define    NOSOUND
  27.  
  28. #include "windows.h"
  29. #include "\winsrc\wtools\fileopen.h"
  30.  
  31. char FileName[128];
  32. char PathName[128];
  33. char OpenName[128];
  34. char DefPath[128];
  35. char DefSpec[13];
  36. char DefExt[4];
  37.  
  38. char str[255];
  39.  
  40.     // Local prototypes
  41.  
  42. HANDLE FAR PASCAL OpenDlg(HWND, unsigned, WORD, LONG);
  43. void SeparateFile(HWND, LPSTR, LPSTR, LPSTR);
  44. void UpdateListBox(HWND);
  45. void AddExt(PSTR, PSTR);
  46. void ChangeDefExt(PSTR, PSTR);
  47.  
  48.  
  49. /****************************************************************************
  50.  
  51.     FUNCTION: GetFileName(PSTR)
  52.  
  53.     PURPOSE: Return the name of the file selected.
  54.  
  55. ****************************************************************************/
  56.  
  57. int GetFileName(hInst, hWnd, szSpec, szName)
  58.     HANDLE hInst;
  59.     HWND hWnd;
  60.     PSTR szSpec, szName;
  61. {
  62.     FARPROC    lpProcOpen;
  63.     int        retVal;
  64.  
  65.     FileName[0] = '\0';
  66.     PathName[0] = '\0';
  67.     OpenName[0] = '\0';
  68.     DefPath[0] = '\0';
  69.     DefExt[0] = '\0';
  70.  
  71.     strcpy( DefSpec, szSpec );
  72.  
  73.     lpProcOpen = MakeProcInstance((FARPROC) OpenDlg, hInst);
  74.     retVal = DialogBox(hInst, "Open", hWnd, lpProcOpen);
  75.     FreeProcInstance(lpProcOpen);
  76.  
  77.     if (retVal)
  78.         strcpy(szName, OpenName);
  79.  
  80.     return retVal;
  81. }
  82.  
  83. /****************************************************************************
  84.  
  85.     FUNCTION: OpenDlg(HWND, unsigned, WORD, LONG)
  86.  
  87.     PURPOSE: Let user select a file, and return.  Open code not provided.
  88.  
  89. ****************************************************************************/
  90.  
  91. HANDLE FAR PASCAL OpenDlg(hDlg, message, wParam, lParam)
  92. HWND hDlg;
  93. unsigned message;
  94. WORD wParam;
  95. LONG lParam;
  96. {
  97.     WORD index;
  98.     PSTR pTptr;
  99.     HANDLE hFile=1;     /* Temp value for return */
  100.  
  101.     switch (message) {
  102.         case WM_COMMAND:
  103.             switch (wParam) {
  104.  
  105.                 case IDC_LISTBOX:
  106.                     switch (HIWORD(lParam)) {
  107.  
  108.                         case LBN_SELCHANGE:
  109.                             /* If item is a directory name, append "*.*" */
  110.                             if (DlgDirSelect(hDlg, str, IDC_LISTBOX))
  111.                                 strcat(str, DefSpec);
  112.  
  113.                             SetDlgItemText(hDlg, IDC_EDIT, str);
  114.                             SendDlgItemMessage(hDlg,
  115.                                 IDC_EDIT,
  116.                                 EM_SETSEL,
  117.                                 NULL,
  118.                                 MAKELONG(0, 0x7fff));
  119.                             break;
  120.  
  121.                         case LBN_DBLCLK:
  122.                             goto openfile;
  123.                     }
  124.                     return (TRUE);
  125.  
  126.                 case IDOK:
  127. openfile:
  128.                     GetDlgItemText(hDlg, IDC_EDIT, OpenName, 128);
  129.                     if (strchr(OpenName, '*') || strchr(OpenName, '?')) {
  130.                         SeparateFile(hDlg, (LPSTR) str, (LPSTR) DefSpec,
  131.                             (LPSTR) OpenName);
  132.                         if (str[0])
  133.                             strcpy(DefPath, str);
  134.                         ChangeDefExt(DefExt, DefSpec);
  135.                         UpdateListBox(hDlg);
  136.                         return (TRUE);
  137.                     }
  138.  
  139.                     if (!OpenName[0]) {
  140.                         MessageBox(hDlg, "No filename specified.",
  141.                             NULL, MB_OK | MB_ICONHAND);
  142.                         return (TRUE);
  143.                     }
  144.  
  145.                     AddExt(OpenName, DefExt);
  146.  
  147.                     /* The routine to open the file would go here, and the */
  148.                     /* file handle would be returned instead of NULL.           */
  149.                     EndDialog(hDlg, hFile);
  150.                     return (TRUE);
  151.  
  152.                 case IDCANCEL:
  153.                     EndDialog(hDlg, NULL);
  154.                     return (FALSE);
  155.             }
  156.             break;
  157.  
  158.         case WM_INITDIALOG:                        /* message: initialize    */
  159.             UpdateListBox(hDlg);
  160.             SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  161.             SendDlgItemMessage(hDlg,               /* dialog handle      */
  162.                 IDC_EDIT,                          /* where to send message  */
  163.                 EM_SETSEL,                         /* select characters      */
  164.                 NULL,                              /* additional information */
  165.                 MAKELONG(0, 0x7fff));              /* entire contents      */
  166.             SetFocus(GetDlgItem(hDlg, IDC_EDIT));
  167.             return (FALSE); /* Indicates the focus is set to a control */
  168.     }
  169.     return FALSE;
  170. }
  171.  
  172. /****************************************************************************
  173.  
  174.     FUNCTION: UpdateListBox(HWND);
  175.  
  176.     PURPOSE: Update the list box of OpenDlg
  177.  
  178. ****************************************************************************/
  179.  
  180. void UpdateListBox(hDlg)
  181. HWND hDlg;
  182. {
  183.     strcpy(str, DefPath);
  184.     strcat(str, DefSpec);
  185.     DlgDirList(hDlg, str, IDC_LISTBOX, IDC_PATH, 0x4010);
  186.     SetDlgItemText(hDlg, IDC_EDIT, DefSpec);
  187. }
  188.  
  189. /****************************************************************************
  190.  
  191.     FUNCTION: ChangeDefExt(PSTR, PSTR);
  192.  
  193.     PURPOSE: Change the default extension
  194.  
  195. ****************************************************************************/
  196.  
  197. void ChangeDefExt(Ext, Name)
  198. PSTR Ext, Name;
  199. {
  200.     PSTR pTptr;
  201.  
  202.     pTptr = Name;
  203.     while (*pTptr && *pTptr != '.')
  204.         pTptr++;
  205.     if (*pTptr)
  206.         if (!strchr(pTptr, '*') && !strchr(pTptr, '?'))
  207.             strcpy(Ext, pTptr);
  208. }
  209.  
  210. /****************************************************************************
  211.  
  212.     FUNCTION: SeparateFile(HWND, LPSTR, LPSTR, LPSTR)
  213.  
  214.     PURPOSE: Separate filename and pathname
  215.  
  216. ****************************************************************************/
  217.  
  218. void SeparateFile(hDlg, lpDestPath, lpDestFileName, lpSrcFileName)
  219. HWND hDlg;
  220. LPSTR lpDestPath, lpDestFileName, lpSrcFileName;
  221. {
  222.     LPSTR lpTmp;
  223.     char  cTmp;
  224.  
  225.     lpTmp = lpSrcFileName + (long) lstrlen(lpSrcFileName);
  226.     while (*lpTmp != ':' && *lpTmp != '\\' && lpTmp > lpSrcFileName)
  227.         lpTmp = AnsiPrev(lpSrcFileName, lpTmp);
  228.     if (*lpTmp != ':' && *lpTmp != '\\') {
  229.         lstrcpy(lpDestFileName, lpSrcFileName);
  230.         lpDestPath[0] = 0;
  231.         return;
  232.     }
  233.     lstrcpy(lpDestFileName, lpTmp + 1);
  234.     cTmp = *(lpTmp + 1);
  235.     lstrcpy(lpDestPath, lpSrcFileName);
  236.      *(lpTmp + 1) = cTmp;
  237.     lpDestPath[(lpTmp - lpSrcFileName) + 1] = 0;
  238. }
  239.  
  240. /****************************************************************************
  241.  
  242.     FUNCTION: AddExt(PSTR, PSTR);
  243.  
  244.     PURPOSE: Add default extension
  245.  
  246. /***************************************************************************/
  247.  
  248. void AddExt(Name, Ext)
  249. PSTR Name, Ext;
  250. {
  251.     PSTR pTptr;
  252.  
  253.     pTptr = Name;
  254.     while (*pTptr && *pTptr != '.')
  255.         pTptr++;
  256.     if (*pTptr != '.')
  257.         strcat(Name, Ext);
  258. }
  259.  
  260.