home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / pascal / wks / tpw / stddlgsb.pas < prev    next >
Pascal/Delphi Source File  |  1991-09-08  |  7KB  |  268 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Turbo Pascal for Windows                        }
  5. {       Standard dialogs unit for ObjectWindows         }
  6. {       Modified for use with BWCC.dll                  }
  7. {                                                       }
  8. {       Copyright (c) 1991 Borland International        }
  9. {                                                       }
  10. {*******************************************************}
  11.  
  12. unit StdDlgsB;
  13.  
  14. interface
  15.  
  16. {$ifdef BWCC}
  17. { add bwcc at end of uses list}
  18.  
  19. uses WinTypes, WinProcs, WinDos, WObjectB, Strings, Bwcc;
  20.  
  21. {alternate res file}
  22.  
  23. {$R STDDLGSB}
  24.  
  25. {$else}
  26.  
  27. uses WinTypes, WinProcs, WinDos, WObjects, Strings;
  28.  
  29. {$R STDDLGS}
  30.  
  31. {$endif}
  32.  
  33. const
  34.   sd_FileOpen = $7F00;
  35.   sd_FileSave = $7F01;
  36.  
  37. const
  38.   id_FName = 100;
  39.   id_FPath = 101;
  40.   id_FList = 102;
  41.   id_DList = 103;
  42.  
  43. const
  44.   fsFileSpec = fsFileName + fsExtension;
  45.  
  46. type
  47.   PFileDialog = ^TFileDialog;
  48.   TFileDialog = object(TDialog)
  49.     Caption: PChar;
  50.     FilePath: PChar;
  51.     PathName: array[0..fsPathName] of Char;
  52.     Extension: array[0..fsExtension] of Char;
  53.     FileSpec: array[0..fsFileSpec] of Char;
  54.     constructor Init(AParent: PWindowsObject; AName, AFilePath: PChar);
  55.     function CanClose: Boolean; virtual;
  56.     procedure SetupWindow; virtual;
  57.     procedure HandleFName(var Msg: TMessage); virtual id_First + id_FName;
  58.     procedure HandleFList(var Msg: TMessage); virtual id_First + id_FList;
  59.     procedure HandleDList(var Msg: TMessage); virtual id_First + id_DList;
  60.   private
  61.     procedure SelectFileName;
  62.     procedure UpdateFileName;
  63.     function UpdateListBoxes: Boolean;
  64.   end;
  65.  
  66. const
  67.   sd_InputDialog = $7F02;
  68.  
  69. const
  70.   id_Prompt = 100;
  71.   id_Input  = 101;
  72.  
  73. type
  74.   PInputDialog = ^TInputDialog;
  75.   TInputDialog = object(TDialog)
  76.     Caption: PChar;
  77.     Prompt: PChar;
  78.     Buffer: PChar;
  79.     BufferSize: Word;
  80.     constructor Init(AParent: PWindowsObject;
  81.       ACaption, APrompt, ABuffer: PChar; ABufferSize: Word);
  82.     function CanClose: Boolean; virtual;
  83.     procedure SetupWindow; virtual;
  84.   end;
  85.  
  86. implementation
  87.  
  88. function GetFileName(FilePath: PChar): PChar;
  89. var
  90.   P: PChar;
  91. begin
  92.   P := StrRScan(FilePath, '\');
  93.   if P = nil then P := StrRScan(FilePath, ':');
  94.   if P = nil then GetFileName := FilePath else GetFileName := P + 1;
  95. end;
  96.  
  97. function GetExtension(FilePath: PChar): PChar;
  98. var
  99.   P: PChar;
  100. begin
  101.   P := StrScan(GetFileName(FilePath), '.');
  102.   if P = nil then GetExtension := StrEnd(FilePath) else GetExtension := P;
  103. end;
  104.  
  105. function HasWildCards(FilePath: PChar): Boolean;
  106. begin
  107.   HasWildCards := (StrScan(FilePath, '*') <> nil) or
  108.     (StrScan(FilePath, '?') <> nil);
  109. end;
  110.  
  111. { TFileDialog }
  112.  
  113. constructor TFileDialog.Init(AParent: PWindowsObject;
  114.   AName, AFilePath: PChar);
  115. begin
  116.   TDialog.Init(AParent, AName);
  117.   Caption := nil;
  118.   FilePath := AFilePath;
  119. end;
  120.  
  121. function TFileDialog.CanClose: Boolean;
  122. var
  123.   PathLen: Word;
  124. begin
  125.   CanClose := False;
  126.   GetDlgItemText(HWindow, id_FName, PathName, fsPathName + 1);
  127.   FileExpand(PathName, PathName);
  128.   PathLen := StrLen(PathName);
  129.   if (PathName[PathLen - 1] = '\') or HasWildCards(PathName) or
  130.     (GetFocus = GetDlgItem(HWindow, id_DList)) then
  131.   begin
  132.     if PathName[PathLen - 1] = '\' then
  133.       StrLCat(PathName, FileSpec, fsPathName);
  134.     if not UpdateListBoxes then
  135.     begin
  136.       MessageBeep(0);
  137.       SelectFileName;
  138.     end;
  139.     Exit;
  140.   end;
  141.   StrLCat(StrLCat(PathName, '\', fsPathName), FileSpec, fsPathName);
  142.   if UpdateListBoxes then Exit;
  143.   PathName[PathLen] := #0;
  144.   if GetExtension(PathName)[0] = #0 then
  145.     StrLCat(PathName, Extension, fsPathName);
  146.   AnsiLower(StrCopy(FilePath, PathName));
  147.   CanClose := True;
  148. end;
  149.  
  150. procedure TFileDialog.SetupWindow;
  151. begin
  152.   SendDlgItemMessage(HWindow, id_FName, em_LimitText, fsPathName, 0);
  153.   if Caption <> nil then SetWindowText(HWindow, Caption);
  154.   StrLCopy(PathName, FilePath, fsPathName);
  155.   StrLCopy(Extension, GetExtension(PathName), fsExtension);
  156.   if HasWildCards(Extension) then Extension[0] := #0;
  157.   if not UpdateListBoxes then
  158.   begin
  159.     StrCopy(PathName, '*.*');
  160.     UpdateListBoxes;
  161.   end;
  162.   SelectFileName;
  163. end;
  164.  
  165. procedure TFileDialog.HandleFName(var Msg: TMessage);
  166. begin
  167.   if Msg.LParamHi = en_Change then
  168.     EnableWindow(GetDlgItem(HWindow, id_Ok),
  169.       SendMessage(Msg.LParamLo, wm_GetTextLength, 0, 0) <> 0);
  170. end;
  171.  
  172. procedure TFileDialog.HandleFList(var Msg: TMessage);
  173. begin
  174.   case Msg.LParamHi of
  175.     lbn_SelChange, lbn_DblClk:
  176.       begin
  177.         DlgDirSelect(HWindow, PathName, id_FList);
  178.         UpdateFileName;
  179.         if Msg.LParamHi = lbn_DblClk then Ok(Msg);
  180.       end;
  181.     lbn_KillFocus:
  182.       SendMessage(Msg.LParamLo, lb_SetCurSel, Word(-1), 0);
  183.   end;
  184. end;
  185.  
  186. procedure TFileDialog.HandleDList(var Msg: TMessage);
  187. begin
  188.   case Msg.LParamHi of
  189.     lbn_SelChange, lbn_DblClk:
  190.       begin
  191.         DlgDirSelect(HWindow, PathName, id_DList);
  192.         StrCat(PathName, FileSpec);
  193.         if Msg.LParamHi = lbn_DblClk then
  194.           UpdateListBoxes else
  195.           UpdateFileName;
  196.       end;
  197.     lbn_KillFocus:
  198.       SendMessage(Msg.LParamLo, lb_SetCurSel, Word(-1), 0);
  199.   end;
  200. end;
  201.  
  202. procedure TFileDialog.SelectFileName;
  203. begin
  204.   SendDlgItemMessage(HWindow, id_FName, em_SetSel, 0, $7FFF0000);
  205.   SetFocus(GetDlgItem(HWindow, id_FName));
  206. end;
  207.  
  208. procedure TFileDialog.UpdateFileName;
  209. begin
  210.   SetDlgItemText(HWindow, id_FName, AnsiLower(PathName));
  211.   SendDlgItemMessage(HWindow, id_FName, em_SetSel, 0, $7FFF0000);
  212. end;
  213.  
  214. function TFileDialog.UpdateListBoxes: Boolean;
  215. var
  216.   Result: Integer;
  217.   Path: array[0..fsPathName] of Char;
  218. begin
  219.   UpdateListBoxes := False;
  220.   if GetDlgItem(HWindow, id_FList) <> 0 then
  221.   begin
  222.     StrCopy(Path, PathName);
  223.     Result := DlgDirList(HWindow, Path, id_FList, id_FPath, 0);
  224.     if Result <> 0 then DlgDirList(HWindow, '*.*', id_DList, 0, $C010);
  225.   end else
  226.   begin
  227.     StrLCopy(Path, PathName, GetFileName(PathName) - PathName);
  228.     StrLCat(Path, '*.*', fsPathName);
  229.     Result := DlgDirList(HWindow, Path, id_DList, id_FPath, $C010);
  230.   end;
  231.   if Result <> 0 then
  232.   begin
  233.     StrLCopy(FileSpec, GetFileName(PathName), fsFileSpec);
  234.     StrCopy(PathName, FileSpec);
  235.     UpdateFileName;
  236.     UpdateListBoxes := True;
  237.   end;
  238. end;
  239.  
  240. { TInputDialog }
  241.  
  242. constructor TInputDialog.Init(AParent: PWindowsObject;
  243.   ACaption, APrompt, ABuffer: PChar; ABufferSize: Word);
  244. begin
  245.   TDialog.Init(AParent, PChar(sd_InputDialog));
  246.   Caption := ACaption;
  247.   Prompt := APrompt;
  248.   Buffer := ABuffer;
  249.   BufferSize := ABufferSize;
  250. end;
  251.  
  252. function TInputDialog.CanClose: Boolean;
  253. begin
  254.   GetDlgItemText(HWindow, id_Input, Buffer, BufferSize);
  255.   CanClose := True;
  256. end;
  257.  
  258. procedure TInputDialog.SetupWindow;
  259. begin
  260.   TDialog.SetupWindow;
  261.   SetWindowText(HWindow, Caption);
  262.   SetDlgItemText(HWindow, id_Prompt, Prompt);
  263.   SetDlgItemText(HWindow, id_Input, Buffer);
  264.   SendDlgItemMessage(HWindow, id_Input, em_LimitText, BufferSize - 1, 0);
  265. end;
  266.  
  267. end.
  268.