home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l040 / 13.ddi / RTLOWL.ZIP / OSTDDLGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-28  |  7.5 KB  |  274 lines

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