home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / tpw / windemos / filedlgs.pas < prev    next >
Pascal/Delphi Source File  |  1991-05-20  |  7KB  |  238 lines

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