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

  1. /*************************************************************************
  2.  *
  3.  *  DLGOPEN.C
  4.  *
  5.  *  Code implementing Dialog Open box.  Static link version!
  6.  *  The application MUST EXPORT DlgfnOpen() in order to work.
  7.  *
  8.  *************************************************************************/
  9.  
  10. #include <windows.h>
  11. #include <bios.h>
  12. #include "dlgopen.h"
  13.  
  14. /*
  15.  *  DLGOPEN private definitions
  16.  */
  17. #define ATTRFILELIST    0x0000  /* include files only */
  18. #define ATTRDIRLIST 0xC010  /* directories and drives ONLY */
  19. #define _MAX_PATH   128
  20.  
  21. static  char NEAR aszPropInfo[] = "dlgopen";
  22. static  char NEAR aszPeriod[] = ".";
  23. static  char NEAR aszNull[] = "";
  24. static  char NEAR aszExt[] = "*.*";
  25.  
  26. typedef struct tagDLGOPENSTRUCT {
  27.     char    aszExt[_MAX_PATH];      /* default extension to use */
  28.     LPSTR   lszTitle;           /* Dialog box caption/title */
  29.     OFSTRUCT    rOF;            /* Storage for OpenFile */
  30.     DWORD   dwFlags;
  31.     LPSTR   lszBuffer;          /* output file name */
  32.     LPSTR   lszStatic;          /* Optional static text */
  33.     WORD    cbBufLen;
  34. }   DLGOPENSTRUCT,
  35.     NEAR *NPDLGOPEN;
  36.  
  37. //  -   -   -   -   -   -   -   -   -
  38.  
  39. static  BOOL PASCAL NEAR DosChangeDir(
  40.     LPSTR   lszDir)
  41. {
  42.         BOOL    fReturn;
  43.  
  44.     _asm {
  45.         push    ds
  46.         lds     dx,lszDir
  47.         mov     bx,dx
  48.  
  49.         cmp     BYTE PTR ds:[bx+1],':'
  50.         jnz     chdnod                      ; No drive
  51.         mov     dl,ds:[bx]
  52.         or      dl,20h
  53.         sub     dl,'a'
  54.  
  55.         mov     ah,0eh                      ; set current drive
  56.         int     21h
  57.  
  58.         mov     ah,19h                      ; get current drive
  59.         int     21h
  60.  
  61.         cmp     al,dl
  62.         jne     chderror
  63.  
  64.         lds     dx,lszDir
  65.         add     dx,2
  66.         mov     bx,dx
  67.         cmp     BYTE PTR ds:[bx],0          ; path name is ""
  68.         jz      chdok
  69. chdnod:
  70.         mov     ah,3bh
  71.         int     21h
  72.         jc      chderror
  73. chdok:
  74.         mov     ax, 1
  75.         jmp     short chdexit
  76. chderror:
  77.         xor     ax, ax
  78. chdexit:
  79.         pop     ds
  80.                 mov             fReturn, ax
  81.     }
  82.         return fReturn;
  83. }
  84. //  -   -   -   -   -   -   -   -   -
  85.  
  86. static  BOOL PASCAL NEAR FGetEnviron(
  87.     BYTE    bSearchSysDrive,
  88.     LPSTR   lszPath)
  89. {
  90.         BOOL    fReturn;
  91.  
  92.     _asm {
  93.         push    ds
  94.         push    si
  95.         lds     si, lszPath
  96.         mov     ah, 19h         ; Get current drive.
  97.         int     21h
  98.         add     al, 'A'
  99.         mov     BYTE PTR [si], al
  100.         mov     dl, bSearchSysDrive
  101.         mov     BYTE PTR [si + 1], dl
  102.         mov     BYTE PTR [si + 2], ':'
  103.         mov     BYTE PTR [si + 3], '\\'
  104.         add     si, 4
  105.         sub     dl, '@'
  106.         mov     ah, 47h
  107.         int     21h             ; Get current directory.
  108.         mov     ax,0
  109.         adc     ax,ax
  110.         dec     ax
  111.         pop     si
  112.         pop     ds
  113.                 mov             fReturn, ax
  114.     }
  115.         return fReturn;
  116. }
  117.  
  118. //  -   -   -   -   -   -   -   -   -
  119.  
  120. static  void PASCAL NEAR VSetEnviron(
  121.     LPSTR   lszPath)
  122. {
  123.     _asm {
  124.         push    ds
  125.         push    si
  126.         lds     si, lszPath
  127.         mov     dl, BYTE PTR [si]
  128.         sub     dl, 'A'
  129.         mov     ah, 0Eh                 ; Set current drive.
  130.         int     21h
  131.         lea     dx, [si + 1]
  132.         mov     ah, 3Bh                 ; Set current directory.
  133.         int     21h
  134.         pop     si
  135.         pop     ds
  136.     }
  137. }
  138.  
  139. //  -   -   -   -   -   -   -   -   -
  140.  
  141. extern  int FAR PASCAL OpenFileDialog(
  142.     HWND    hwndParent,
  143.     LPSTR   lszTitle,
  144.     LPSTR   lszExtension,
  145.     DWORD   dwFlags,
  146.     LPSTR   lszStatic,
  147.     LPSTR   lszPath,
  148.     WORD    cbBufLen)
  149. {
  150.     NPDLGOPEN   pdlg;
  151.     FARPROC lpfnDlgProc;
  152.     HINSTANCE hInst;
  153.     int nResult;
  154.  
  155.     pdlg = (NPDLGOPEN)LocalAlloc(LPTR, sizeof(DLGOPENSTRUCT));
  156.     pdlg->dwFlags = dwFlags;
  157.     pdlg->cbBufLen = cbBufLen;
  158.     pdlg->lszStatic = lszStatic;
  159.     pdlg->lszBuffer = lszPath;
  160.     lstrcpy(pdlg->aszExt, (lszExtension && *lszExtension) ? lszExtension : aszExt);
  161.     pdlg->lszTitle = lszTitle;
  162.     hInst = (HINSTANCE)GetWindowWord(hwndParent, GWW_HINSTANCE);
  163.     lpfnDlgProc = (FARPROC)MakeProcInstance((FARPROC)DlgfnOpen, hInst);
  164.     nResult = DialogBoxParam(hInst, "DlgOpenBox", hwndParent, (DLGPROC)lpfnDlgProc, (LONG)(WORD)pdlg);
  165.     FreeProcInstance(lpfnDlgProc);
  166.     LocalFree((HANDLE)pdlg);
  167.     return nResult;
  168. }
  169.  
  170. //  -   -   -   -   -   -   -   -   -
  171.  
  172. static BOOL NEAR PASCAL FSearchSpec(
  173.     LPSTR   lsz)
  174. {
  175.     for (; *lsz; lsz++)
  176.         if (*lsz == '*' || *lsz == '?')
  177.             return TRUE;
  178.     return FALSE;
  179. }
  180.  
  181. //  -   -   -   -   -   -   -   -   -
  182.  
  183. static  void NEAR PASCAL DlgCheckOkEnable(
  184.     HWND    hwnd,
  185.     int     idEdit,
  186.     WORD    wMsg)
  187. {
  188.     BYTE    aszBuf[80];
  189.     HWND    hwndEdit;
  190.     HWND    hwndOk;
  191.  
  192.     if (wMsg == EN_CHANGE) {
  193.         hwndEdit = GetDlgItem(hwnd, idEdit);
  194.         hwndOk = GetDlgItem(hwnd, IDOK);
  195.         EnableWindow(hwndOk, (BOOL)SendMessage(hwndEdit, WM_GETTEXTLENGTH, 0, 0L));
  196.         GetWindowText(hwndEdit, aszBuf, sizeof(aszBuf));
  197.     SendDlgItemMessage(hwnd, (int)DLGOPEN_FILE_LISTBOX, (UINT)LB_SELECTSTRING, (WPARAM)-1, (LONG)(LPSTR)aszBuf);
  198.     }
  199. }
  200.  
  201. //  -   -   -   -   -   -   -   -   -
  202.  
  203. static LPSTR NEAR PASCAL LszFillListBox(
  204.     HWND    hwnd,
  205.     LPSTR   lszFile)
  206. {
  207.     BYTE    asz[20];
  208.     LPSTR   lsz;
  209.     LPSTR   lszDir;   // Directory name or path */
  210.  
  211.     lsz = lszFile;
  212.     lszDir = asz;
  213.     while (*lsz && *lsz != ';')
  214.         lsz++;
  215.     while (lsz > lszFile && *lsz != '\\')
  216.         lsz--;
  217.     if (lsz > lszFile) {
  218.         *lsz = 0;
  219.         lstrcpy(lszDir, lszFile);
  220.         lszFile = lsz+1;
  221.     } else
  222.         lstrcpy(lszDir, aszPeriod);
  223.     DlgDirList(hwnd, lszDir, DLGOPEN_DIR_LISTBOX, DLGOPEN_PATH, ATTRDIRLIST);
  224.     SendDlgItemMessage(hwnd, DLGOPEN_FILE_LISTBOX, LB_RESETCONTENT, 0, 0L);
  225.     SendDlgItemMessage(hwnd, DLGOPEN_FILE_LISTBOX, WM_SETREDRAW, FALSE, 0L);
  226.     lszDir = lszFile;        // save lszFile to return */
  227.     while (*lszFile) {
  228.         lsz = asz;
  229.         while (*lszFile == ' ')
  230.             lszFile++;
  231.         while (*lszFile && *lszFile != ';')
  232.             *lsz++ = *lszFile++;
  233.         *lsz = 0;
  234.         if (*lszFile)
  235.             lszFile++;
  236.         SendDlgItemMessage(hwnd, DLGOPEN_FILE_LISTBOX, LB_DIR, ATTRFILELIST, (LONG)(LPSTR)asz);
  237.     }
  238.     SendDlgItemMessage(hwnd, DLGOPEN_FILE_LISTBOX, WM_SETREDRAW, TRUE, 0L);
  239.     InvalidateRect(GetDlgItem(hwnd, DLGOPEN_FILE_LISTBOX), NULL, TRUE);
  240.     return lszDir;
  241. }
  242.  
  243. //  -   -   -   -   -   -   -   -   -
  244.  
  245. static  void NEAR PASCAL VInitDialog(
  246.     HWND    hwnd,
  247.     LONG    lParam)
  248. {
  249.     NPDLGOPEN   pdlg;
  250.  
  251.     SetProp(hwnd, aszPropInfo, (HANDLE)(WORD)lParam);
  252.     pdlg = (NPDLGOPEN)(WORD)lParam;
  253.     if (pdlg->lszTitle)
  254.         SetWindowText(hwnd, pdlg->lszTitle);
  255.     if (pdlg->lszStatic)
  256.         SetDlgItemText(hwnd, DLGOPEN_STATIC, pdlg->lszStatic);
  257.     SendDlgItemMessage(hwnd, DLGOPEN_EDIT, EM_LIMITTEXT, pdlg->cbBufLen, 0L);
  258.     if (pdlg->dwFlags & DLGOPEN_NOSHOWSPEC)
  259.         SetDlgItemText(hwnd, DLGOPEN_EDIT, aszNull);
  260.     else
  261.         SetDlgItemText(hwnd, DLGOPEN_EDIT, pdlg->aszExt);
  262.     if ((pdlg->dwFlags & DLGOPEN_SAVE) && *pdlg->lszBuffer)
  263.         SetDlgItemText(hwnd, DLGOPEN_EDIT, pdlg->lszBuffer);
  264.     LszFillListBox(hwnd, pdlg->aszExt);
  265.     SendDlgItemMessage(hwnd, DLGOPEN_EDIT, EM_SETSEL, 0, 0x7FFF0000L);
  266. }
  267.  
  268. //  -   -   -   -   -   -   -   -   -
  269.  
  270. static  void NEAR PASCAL VComIdOk(
  271.     HWND    hwnd)
  272. {
  273.     NPDLGOPEN   pdlg;
  274.     LPSTR   lszFile;
  275.     HFILE nResult;
  276.     WORD    w;
  277.  
  278.     if (!IsWindowEnabled(GetDlgItem(hwnd, IDOK)))
  279.         return;
  280.     pdlg = (NPDLGOPEN)GetProp(hwnd, aszPropInfo);
  281.     GetDlgItemText(hwnd, DLGOPEN_EDIT, pdlg->lszBuffer, pdlg->cbBufLen);
  282.     lszFile = pdlg->lszBuffer;
  283.     w = lstrlen(lszFile);
  284.     if (w && (lszFile[w - 1] == '\\'))
  285.         lszFile[w] = '\0';
  286.     if (DosChangeDir(lszFile))
  287.         lstrcpy(lszFile, pdlg->aszExt);
  288.     if (FSearchSpec(lszFile)) {
  289.         lstrcpy(pdlg->aszExt, LszFillListBox(hwnd, lszFile));
  290.         SetDlgItemText(hwnd, DLGOPEN_EDIT, (pdlg->dwFlags & DLGOPEN_NOSHOWSPEC) ? aszNull : pdlg->aszExt);
  291.         return;
  292.     }
  293.     nResult = OpenFile(lszFile, &pdlg->rOF, LOWORD(pdlg->dwFlags));
  294.     if ((nResult == HFILE_ERROR) && (HIWORD(pdlg->dwFlags) & DLGOPEN_MUSTEXIST))
  295.         MessageBeep(0);
  296.     else {
  297.         OemToAnsi(pdlg->rOF.szPathName, pdlg->lszBuffer);
  298.         EndDialog(hwnd, nResult == HFILE_ERROR ? DLGOPEN_NOTFOUND : (int)nResult);
  299.     }
  300. }
  301.  
  302. //  -   -   -   -   -   -   -   -   -
  303.  
  304. extern  BOOL FAR PASCAL __export DlgfnOpen(
  305.     HWND    hwnd,
  306.     WORD    wMsg,
  307.     WORD    wParam,
  308.     LONG    lParam)
  309. {
  310.     WORD    w;
  311.     NPDLGOPEN   pdlg;
  312.     LPSTR   lszFile;
  313.  
  314.     pdlg = (NPDLGOPEN)GetProp(hwnd, aszPropInfo);
  315.     switch (wMsg) {
  316.     case WM_INITDIALOG:
  317.         VInitDialog(hwnd, lParam);
  318.         return TRUE;
  319.     case WM_DESTROY:
  320.         RemoveProp(hwnd, aszPropInfo);
  321.         return TRUE;
  322.     case WM_CLOSE:
  323.         EndDialog(hwnd, DLGOPEN_CANCEL);
  324.         return TRUE;
  325.     case WM_COMMAND:
  326.         switch (wParam) {
  327.         case IDOK:
  328.             VComIdOk(hwnd);
  329.             return TRUE;
  330.         case IDCANCEL:
  331.             *pdlg->lszBuffer = '\0';
  332.             *pdlg->rOF.szPathName = '\0';
  333.             EndDialog(hwnd, DLGOPEN_CANCEL);
  334.             return TRUE;
  335.         case DLGOPEN_FILE_LISTBOX:
  336.         case DLGOPEN_DIR_LISTBOX:
  337.             switch (HIWORD(lParam)) {
  338.             case 1:
  339.                 lszFile = pdlg->lszBuffer;
  340.                 DlgDirSelect(hwnd, lszFile, wParam);
  341.                 w = lstrlen(lszFile) - 1;
  342.                 switch (lszFile[w]) {
  343.                 case ':':
  344.                     lstrcat(lszFile, aszPeriod);
  345.                     break;
  346.                 case '\\':
  347.                     lszFile[w] = 0;
  348.                     break;
  349.                 }
  350.                 SetDlgItemText(hwnd, DLGOPEN_EDIT, lszFile);
  351.                 return TRUE;
  352.             case 2:
  353.                 PostMessage(hwnd, WM_COMMAND, IDOK, 0L);
  354.                 return TRUE;
  355.             }
  356.             break;
  357.         case DLGOPEN_EDIT:
  358.             DlgCheckOkEnable(hwnd, DLGOPEN_EDIT, HIWORD(lParam));
  359.             return TRUE;
  360.         }
  361.         break;
  362.     }
  363.     return FALSE;
  364. }
  365.  
  366. //  -   -   -   -   -   -   -   -   -
  367.