home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Pascal / BPASCAL.700 / D12 / WINDEMOS.ZIP / FILEDLGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-01  |  7.1 KB  |  237 lines

  1. {************************************************}
  2. {                                                }
  3. {   Demo unit                                    }
  4. {   Copyright (c) 1991 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit FileDlgs;
  9.  
  10. {$S-}
  11. {$R FILEDLGS}
  12.  
  13. interface
  14.  
  15. uses WinTypes, WinProcs, WinDos, Strings;
  16.  
  17. { DoFileDialog executes a file dialog. Window specifies the
  18.   parent window of the dialog (typically the application's main
  19.   window). FilePath must point to a zero-based character array
  20.   of fsPathName characters. On entry, DoFileDialog changes to
  21.   the drive and directory (if any) specified by FilePath, and
  22.   the name and extension parts specified by FilePath are used
  23.   as the default file specifier. On exit, if the user pressed
  24.   OK, the resulting fully expanded file path is stored in
  25.   FilePath. DialogName specifies the resource name of the
  26.   dialog. Caption specifies an optional new dialog box title.
  27.   If Caption is nil, the dialog's title is not changed. The
  28.   returned value is True if the user pressed OK, or False if
  29.   the user pressed Cancel. }
  30.  
  31. function DoFileDialog(Window: HWnd;
  32.   FilePath, DialogName, Caption: PChar): Boolean;
  33.  
  34. { DoFileOpen calls DoFileDialog with a DialogName of 'FileOpen'
  35.   and a Caption of nil. The 'FileOpen' dialog is contained in
  36.   the FILEDLGS.RES resource file. }
  37.  
  38. function DoFileOpen(Window: HWnd; FilePath: PChar): Boolean;
  39.  
  40. { DoFileOpen calls DoFileDialog with a DialogName of 'FileSave'
  41.   and a Caption of nil. The 'FileSave' dialog is contained in
  42.   the FILEDLGS.RES resource file. }
  43.  
  44. function DoFileSave(Window: HWnd; FilePath: PChar): Boolean;
  45.  
  46. implementation
  47.  
  48. const
  49.   id_FName = 100;
  50.   id_FPath = 101;
  51.   id_FList = 102;
  52.   id_DList = 103;
  53.  
  54. const
  55.   fsFileSpec = fsFileName + fsExtension;
  56.  
  57. type
  58.   TDWord = record
  59.     Lo, Hi: Word;
  60.   end;
  61.  
  62. var
  63.   GCaption: PChar;
  64.   GFilePath: PChar;
  65.   GPathName: array[0..fsPathName] of Char;
  66.   GExtension: array[0..fsExtension] of Char;
  67.   GFileSpec: array[0..fsFileSpec] of Char;
  68.  
  69. function GetFileName(FilePath: PChar): PChar;
  70. var
  71.   P: PChar;
  72. begin
  73.   P := StrRScan(FilePath, '\');
  74.   if P = nil then P := StrRScan(FilePath, ':');
  75.   if P = nil then GetFileName := FilePath else GetFileName := P + 1;
  76. end;
  77.  
  78. function GetExtension(FilePath: PChar): PChar;
  79. var
  80.   P: PChar;
  81. begin
  82.   P := StrScan(GetFileName(FilePath), '.');
  83.   if P = nil then GetExtension := StrEnd(FilePath) else GetExtension := P;
  84. end;
  85.  
  86. function FileDialog(Dialog: HWnd; Message, WParam: Word;
  87.   LParam: TDWord): Bool; export;
  88. var
  89.   PathLen: Word;
  90.   P: PChar;
  91.  
  92. procedure UpdateFileName;
  93. begin
  94.   SetDlgItemText(Dialog, id_FName, StrLower(GPathName));
  95.   SendDlgItemMessage(Dialog, id_FName, em_SetSel, 0, $7FFF0000);
  96. end;
  97.  
  98. procedure SelectFileName;
  99. begin
  100.   SendDlgItemMessage(Dialog, id_FName, em_SetSel, 0, $7FFF0000);
  101.   SetFocus(GetDlgItem(Dialog, id_FName));
  102. end;
  103.  
  104. function UpdateListBoxes: Boolean;
  105. var
  106.   Result: Integer;
  107.   Path: array[0..fsPathName] of Char;
  108. begin
  109.   UpdateListBoxes := False;
  110.   if GetDlgItem(Dialog, id_FList) <> 0 then
  111.   begin
  112.     StrCopy(Path, GPathName);
  113.     Result := DlgDirList(Dialog, Path, id_FList, id_FPath, 0);
  114.     if Result <> 0 then DlgDirList(Dialog, '*.*', id_DList, 0, $C010);
  115.   end else
  116.   begin
  117.     StrLCopy(Path, GPathName, GetFileName(GPathName) - GPathName);
  118.     StrLCat(Path, '*.*', fsPathName);
  119.     Result := DlgDirList(Dialog, Path, id_DList, id_FPath, $C010);
  120.   end;
  121.   if Result <> 0 then
  122.   begin
  123.     StrLCopy(GFileSpec, GetFileName(GPathName), fsFileSpec);
  124.     StrCopy(GPathName, GFileSpec);
  125.     UpdateFileName;
  126.     UpdateListBoxes := True;
  127.   end;
  128. end;
  129.  
  130. begin
  131.   FileDialog := True;
  132.   case Message of
  133.     wm_InitDialog:
  134.       begin
  135.         SendDlgItemMessage(Dialog, id_FName, em_LimitText, fsPathName, 0);
  136.         if GCaption <> nil then SetWindowText(Dialog, GCaption);
  137.         StrLCopy(GPathName, GFilePath, fsPathName);
  138.         StrLCopy(GExtension, GetExtension(GPathName), fsExtension);
  139.         if not UpdateListBoxes then
  140.         begin
  141.           StrCopy(GPathName, '*.*');
  142.           UpdateListBoxes;
  143.         end;
  144.         SelectFileName;
  145.         Exit;
  146.       end;
  147.     wm_Command:
  148.       case WParam of
  149.         id_FName:
  150.           begin
  151.             if LParam.Hi = en_Change then
  152.               EnableWindow(GetDlgItem(Dialog, id_Ok),
  153.                 SendMessage(LParam.lo, wm_GetTextLength, 0, 0) <> 0);
  154.             Exit;
  155.           end;
  156.         id_FList:
  157.           if (LParam.Hi = lbn_SelChange) or (LParam.Hi = lbn_DblClk) then
  158.           begin
  159.             DlgDirSelect(Dialog, GPathName, id_FList);
  160.             UpdateFileName;
  161.             if LParam.Hi = lbn_DblClk then
  162.               SendMessage(Dialog, wm_Command, id_Ok, 0);
  163.             Exit;
  164.           end;
  165.         id_DList:
  166.           if (LParam.Hi = lbn_SelChange) or (LParam.Hi = lbn_DblClk) then
  167.           begin
  168.             DlgDirSelect(Dialog, GPathName, id_DList);
  169.             StrCat(GPathName, GFileSpec);
  170.             if LParam.Hi = lbn_DblClk then
  171.               UpdateListBoxes else
  172.               UpdateFileName;
  173.             Exit;
  174.           end;
  175.         id_Ok:
  176.           begin
  177.             GetDlgItemText(Dialog, id_FName, GPathName, fsPathName + 1);
  178.             FileExpand(GPathName, GPathName);
  179.             PathLen := StrLen(GPathName);
  180.             if (GPathName[PathLen - 1] = '\') or
  181.               (StrScan(GPathName, '*') <> nil) or
  182.               (StrScan(GPathName, '?') <> nil) or
  183.               (GetFocus = GetDlgItem(Dialog, id_DList)) then
  184.             begin
  185.               if GPathName[PathLen - 1] = '\' then
  186.                 StrLCat(GPathName, GFileSpec, fsPathName);
  187.               if not UpdateListBoxes then
  188.               begin
  189.                 MessageBeep(0);
  190.                 SelectFileName;
  191.               end;
  192.               Exit;
  193.             end;
  194.             StrLCat(StrLCat(GPathName, '\', fsPathName),
  195.               GFileSpec, fsPathName);
  196.             if UpdateListBoxes then Exit;
  197.             GPathName[PathLen] := #0;
  198.             if GetExtension(GPathName)[0] = #0 then
  199.               StrLCat(GPathName, GExtension, fsPathName);
  200.             StrLower(StrCopy(GFilePath, GPathName));
  201.             EndDialog(Dialog, 1);
  202.             Exit;
  203.           end;
  204.         id_Cancel:
  205.           begin
  206.             EndDialog(Dialog, 0);
  207.             Exit;
  208.           end;
  209.       end;
  210.   end;
  211.   FileDialog := False;
  212. end;
  213.  
  214. function DoFileDialog(Window: HWnd;
  215.   FilePath, DialogName, Caption: PChar): Boolean;
  216. var
  217.   DialogProc: TFarProc;
  218. begin
  219.   GFilePath := FilePath;
  220.   GCaption := Caption;
  221.   DialogProc := MakeProcInstance(@FileDialog, HInstance);
  222.   DoFileDialog := DialogBox(HInstance, DialogName, Window, DialogProc) = 1;
  223.   FreeProcInstance(DialogProc);
  224. end;
  225.  
  226. function DoFileOpen(Window: HWnd; FilePath: PChar): Boolean;
  227. begin
  228.   DoFileOpen := DoFileDialog(Window, FilePath, 'FileOpen', nil);
  229. end;
  230.  
  231. function DoFileSave(Window: HWnd; FilePath: PChar): Boolean;
  232. begin
  233.   DoFileSave := DoFileDialog(Window, FilePath, 'FileSave', nil);
  234. end;
  235.  
  236. end.
  237.