home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 11.ddi / WINDEMOS.ZIP / FILEDLGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-27  |  7.0 KB  |  236 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.  
  91. procedure UpdateFileName;
  92. begin
  93.   SetDlgItemText(Dialog, id_FName, StrLower(GPathName));
  94.   SendDlgItemMessage(Dialog, id_FName, em_SetSel, 0, $7FFF0000);
  95. end;
  96.  
  97. procedure SelectFileName;
  98. begin
  99.   SendDlgItemMessage(Dialog, id_FName, em_SetSel, 0, $7FFF0000);
  100.   SetFocus(GetDlgItem(Dialog, id_FName));
  101. end;
  102.  
  103. function UpdateListBoxes: Boolean;
  104. var
  105.   Result: Integer;
  106.   Path: array[0..fsPathName] of Char;
  107. begin
  108.   UpdateListBoxes := False;
  109.   if GetDlgItem(Dialog, id_FList) <> 0 then
  110.   begin
  111.     StrCopy(Path, GPathName);
  112.     Result := DlgDirList(Dialog, Path, id_FList, id_FPath, 0);
  113.     if Result <> 0 then DlgDirList(Dialog, '*.*', id_DList, 0, $C010);
  114.   end else
  115.   begin
  116.     StrLCopy(Path, GPathName, GetFileName(GPathName) - GPathName);
  117.     StrLCat(Path, '*.*', fsPathName);
  118.     Result := DlgDirList(Dialog, Path, id_DList, id_FPath, $C010);
  119.   end;
  120.   if Result <> 0 then
  121.   begin
  122.     StrLCopy(GFileSpec, GetFileName(GPathName), fsFileSpec);
  123.     StrCopy(GPathName, GFileSpec);
  124.     UpdateFileName;
  125.     UpdateListBoxes := True;
  126.   end;
  127. end;
  128.  
  129. begin
  130.   FileDialog := True;
  131.   case Message of
  132.     wm_InitDialog:
  133.       begin
  134.         SendDlgItemMessage(Dialog, id_FName, em_LimitText, fsPathName, 0);
  135.         if GCaption <> nil then SetWindowText(Dialog, GCaption);
  136.         StrLCopy(GPathName, GFilePath, fsPathName);
  137.         StrLCopy(GExtension, GetExtension(GPathName), fsExtension);
  138.         if not UpdateListBoxes then
  139.         begin
  140.           StrCopy(GPathName, '*.*');
  141.           UpdateListBoxes;
  142.         end;
  143.         SelectFileName;
  144.         Exit;
  145.       end;
  146.     wm_Command:
  147.       case WParam of
  148.         id_FName:
  149.           begin
  150.             if LParam.Hi = en_Change then
  151.               EnableWindow(GetDlgItem(Dialog, id_Ok),
  152.                 SendMessage(LParam.lo, wm_GetTextLength, 0, 0) <> 0);
  153.             Exit;
  154.           end;
  155.         id_FList:
  156.           if (LParam.Hi = lbn_SelChange) or (LParam.Hi = lbn_DblClk) then
  157.           begin
  158.             DlgDirSelect(Dialog, GPathName, id_FList);
  159.             UpdateFileName;
  160.             if LParam.Hi = lbn_DblClk then
  161.               SendMessage(Dialog, wm_Command, id_Ok, 0);
  162.             Exit;
  163.           end;
  164.         id_DList:
  165.           if (LParam.Hi = lbn_SelChange) or (LParam.Hi = lbn_DblClk) then
  166.           begin
  167.             DlgDirSelect(Dialog, GPathName, id_DList);
  168.             StrCat(GPathName, GFileSpec);
  169.             if LParam.Hi = lbn_DblClk then
  170.               UpdateListBoxes else
  171.               UpdateFileName;
  172.             Exit;
  173.           end;
  174.         id_Ok:
  175.           begin
  176.             GetDlgItemText(Dialog, id_FName, GPathName, fsPathName + 1);
  177.             FileExpand(GPathName, GPathName);
  178.             PathLen := StrLen(GPathName);
  179.             if (GPathName[PathLen - 1] = '\') or
  180.               (StrScan(GPathName, '*') <> nil) or
  181.               (StrScan(GPathName, '?') <> nil) or
  182.               (GetFocus = GetDlgItem(Dialog, id_DList)) then
  183.             begin
  184.               if GPathName[PathLen - 1] = '\' then
  185.                 StrLCat(GPathName, GFileSpec, fsPathName);
  186.               if not UpdateListBoxes then
  187.               begin
  188.                 MessageBeep(0);
  189.                 SelectFileName;
  190.               end;
  191.               Exit;
  192.             end;
  193.             StrLCat(StrLCat(GPathName, '\', fsPathName),
  194.               GFileSpec, fsPathName);
  195.             if UpdateListBoxes then Exit;
  196.             GPathName[PathLen] := #0;
  197.             if GetExtension(GPathName)[0] = #0 then
  198.               StrLCat(GPathName, GExtension, fsPathName);
  199.             StrLower(StrCopy(GFilePath, GPathName));
  200.             EndDialog(Dialog, 1);
  201.             Exit;
  202.           end;
  203.         id_Cancel:
  204.           begin
  205.             EndDialog(Dialog, 0);
  206.             Exit;
  207.           end;
  208.       end;
  209.   end;
  210.   FileDialog := False;
  211. end;
  212.  
  213. function DoFileDialog(Window: HWnd;
  214.   FilePath, DialogName, Caption: PChar): Boolean;
  215. var
  216.   DialogProc: TFarProc;
  217. begin
  218.   GFilePath := FilePath;
  219.   GCaption := Caption;
  220.   DialogProc := MakeProcInstance(@FileDialog, HInstance);
  221.   DoFileDialog := DialogBox(HInstance, DialogName, Window, DialogProc) = 1;
  222.   FreeProcInstance(DialogProc);
  223. end;
  224.  
  225. function DoFileOpen(Window: HWnd; FilePath: PChar): Boolean;
  226. begin
  227.   DoFileOpen := DoFileDialog(Window, FilePath, 'FileOpen', nil);
  228. end;
  229.  
  230. function DoFileSave(Window: HWnd; FilePath: PChar): Boolean;
  231. begin
  232.   DoFileSave := DoFileDialog(Window, FilePath, 'FileSave', nil);
  233. end;
  234.  
  235. end.
  236.