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

  1. unit FileDlgs;
  2.  
  3. interface
  4.  
  5. {$R FileDlgs.Res}
  6.  
  7. { This unit isn't needed at startup, DemandLoad shortens app load time. }
  8. {$C Moveable, Demandload, Discardable}
  9.  
  10. uses WinProcs, WinTypes, OWindows, ODialogs, CommDlg, WinDos, Strings;
  11.  
  12. type
  13.  
  14.    { TCDFileDlg builds an OWL object around a Windows 3.1 Common Dialog.
  15.      By using the OWL object's Instance function pointer as the
  16.      common dialog's hook procedure, the OWL object will get messages
  17.      just as it would for a normal dialog (for the most part).
  18.  
  19.      Descendents of TCDFileDlg implement specific types of file dialogs:
  20.      File Open, File Save, File Save As, and special purpose dialogs.}
  21.  
  22.    PCDFileDlg = ^TCDFileDlg;
  23.    TCDFileDlg = object(TDialog)
  24.      OFN : TOpenFileName;
  25.      constructor Init(AParent : PWindowsObject;
  26.                       AFlags   : Longint;
  27.                       AFileName : PChar;
  28.                       ANameLength : Word;
  29.                       AFilter: PChar);
  30.      destructor  Done;  virtual;
  31.      function    Create : Boolean; virtual;
  32.      function    Execute : Integer; virtual;
  33.      function    CDExecute: Bool; virtual;
  34.      procedure   OK(var Msg : TMessage);     virtual id_First+id_OK;
  35.      procedure   Cancel(var Msg : TMessage); virtual id_First+id_Cancel;
  36.    end;
  37.  
  38.    { TCDFileOpen implements a File Open common dialog.  If the main program
  39.      is using BWCC, then this object makes the common dialog use a BWCC
  40.      dialog template.  }
  41.       
  42.    PCDFileOpen = ^TCDFileOpen;
  43.    TCDFileOpen = object(TCDFileDlg)
  44.      constructor Init(AParent : PWindowsObject;
  45.                       AFlags : Longint;
  46.                       AFileName: PChar;
  47.                       ANameLength: Word;
  48.                       AFilter: PChar);
  49.    end;
  50.  
  51.    PCDFileSaveAs = ^TCDFileSaveAs;
  52.    TCDFileSaveAs = object(TCDFileOpen)
  53.      constructor Init(AParent : PWindowsObject;
  54.                       AFlags : Longint;
  55.                       AFileName: PChar;
  56.                       ANameLength: Word;
  57.                       AFilter: PChar);
  58.      function CDExecute: Bool; virtual;
  59.    end;
  60.  
  61.  
  62. implementation
  63.  
  64. const
  65.   dlgCDFileOpen_BWCC   = MakeIntResource(32520);
  66.  
  67. constructor TCDFileDlg.Init(AParent : PWindowsObject;
  68.                             AFlags   : Longint;
  69.                             AFileName : PChar;
  70.                             ANameLength : Word;
  71.                             AFilter: PChar);
  72. var
  73.   TempName : array[0..fsFileName] of Char;
  74.   TempExt  : array[0..fsExtension] of Char;
  75.  
  76. begin
  77.    TDialog.Init(AParent,nil);
  78.    FillChar(OFN,Sizeof(OFN),0);
  79.    with OFN do
  80.    begin
  81.      lStructSize := SizeOf(OFN);
  82.      hwndOwner := AParent^.hWindow;
  83.      @lpfnHook := Instance;
  84.      Flags     := AFlags or OFN_ENABLEHOOK;
  85.      hInstance := System.hInstance;
  86.      lpstrFilter := AFilter;
  87.      lpstrFileTitle  := nil;
  88.      nMaxFileTitle   := 0 ;
  89.      GetMem(lpstrInitialDir,Succ(fsDirectory));
  90.      lpstrFile := AFileName;
  91.      nMaxFile  := ANameLength;
  92.      FileExpand(lpstrFile,AFileName);
  93.      FileSplit(lpstrFile,lpstrInitialDir,TempName,TempExt);
  94.      StrCat(StrCopy(lpstrFile,TempName),TempExt);
  95.    end;
  96. end;
  97.  
  98.  
  99. destructor TCDFileDlg.Done;
  100. begin
  101.  FreeMem(OFN.lpstrInitialDir,Succ(fsDirectory));
  102.  TDialog.Done;
  103. end;
  104.  
  105. function    TCDFileDlg.Create : boolean;
  106. begin
  107.   Create := False;  { Cannot create a non-modal File Open dialog }
  108. end;
  109.  
  110. function    TCDFileDlg.Execute : integer;
  111. { Basically, This is the code from TDialog.Execute with the call to
  112.   DialogBoxParam changed to CDExecute }
  113. var
  114.   CDError : Longint;
  115.   OldKbHandler: PWindowsObject;
  116. begin
  117.   if Status = 0 then
  118.   begin
  119.     DisableAutoCreate;
  120.     EnableKBHandler;
  121.     IsModal := True;
  122.     OldKbHandler := Application^.KBHandlerWnd;
  123.     if CDExecute then
  124.       Execute := id_ok
  125.     else
  126.     begin
  127.       CDError := CommDlgExtendedError;
  128.       if CDError = 0 then
  129.         Execute := id_Cancel
  130.       else
  131.       begin
  132.         Status := -CdError;
  133.         Execute := Status;
  134.       end;
  135.     end;
  136.     Application^.KBHandlerWnd := OldKbHandler;
  137.     HWindow := 0;
  138.   end
  139.   else Execute := Status;
  140. end;
  141.  
  142. function TCDFileDlg.CDExecute: Bool;
  143. begin
  144.   CDExecute := GetOpenFileName(OFN);
  145. end;
  146.  
  147. procedure   TCDFileDlg.OK(var Msg : TMessage);
  148. { COMMDLG requires that the hook function (ie: this method) does NOT
  149.   call EndDlg() for it's modal dialogs.  Setting Msg.Result to 0 will
  150.   allow COMMDLG to terminate the dialog.  A value of 1 will cause
  151.   COMMDLG to ignore the OK button press. }
  152. begin
  153.   if CanClose then
  154.     Msg.Result := 0
  155.   else
  156.     Msg.Result := 1;
  157. end;
  158.  
  159. procedure   TCDFileDlg.Cancel(var Msg : TMessage);
  160. begin
  161.   Msg.Result := 0
  162. end;
  163.  
  164.  
  165. { TCDListBox resolves a BWCC <-> CommDlg display glitch by responding
  166.   to WMEraseBkgnd messages to paint the invalidated rect using the
  167.   window background system color.  Without this, partially filled
  168.   CommDlg listboxes would be painted gray in the empty areas, leaving
  169.   the listbox half-white and half-gray.  }
  170.  
  171. type
  172.   PCDListBox = ^TCDListBox;
  173.   TCDListBox = object(TListBox)
  174.     Brush: HBrush;
  175.     constructor InitResource(AParent: PWindowsObject; ResourceID: Word);
  176.     destructor Done; virtual;
  177.     procedure WMEraseBkgnd(var Msg : TMessage);
  178.       virtual wm_First + wm_EraseBkgnd;
  179.   end;
  180.  
  181. constructor TCDListBox.InitResource(AParent: PWindowsObject;
  182.                                     ResourceID: Word);
  183. begin
  184.   inherited InitResource(AParent, ResourceID);
  185.   Brush := CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  186. end;
  187.  
  188. destructor TCDListBox.Done;
  189. begin
  190.   DeleteObject(Brush);
  191.   TListbox.Done;
  192. end;
  193.  
  194. procedure TCDListBox.WMEraseBkgnd(var Msg: TMessage);
  195. var
  196.   R : TRect;
  197. begin                          
  198.   GetClientRect(hWindow,R);
  199.   FillRect(hDC(Msg.wParam),R,Brush);
  200.   Msg.Result := 1;
  201. end;
  202.  
  203.  
  204. constructor TCDFileOpen.Init(AParent : PWindowsObject;
  205.                              AFlags   : Longint;
  206.                              AFileName : Pchar;
  207.                              ANameLength : Word;
  208.                              AFilter: PChar);
  209. var
  210.   Dummy : PWindowsObject;
  211. begin
  212.   inherited Init(AParent, AFlags, AFileName, ANameLength, AFilter);
  213.   with OFN do
  214.   begin
  215.     lpstrTitle := 'File Open';
  216.     if BWCCClassNames then
  217.     begin
  218.       Flags := Flags or OFN_EnableTemplate;
  219.       lpTemplateName := dlgCDFileOpen_BWCC;
  220.       Dummy := New(PCDListBox, InitResource(@Self, 1120));
  221.       Dummy := New(PCDListBox, InitResource(@Self, 1121));
  222.     end;
  223.   end;
  224. end;
  225.  
  226. constructor TCDFileSaveAs.Init(AParent : PWindowsObject;
  227.                                AFlags : Longint;
  228.                                AFileName: PChar;
  229.                                ANameLength: Word;
  230.                                AFilter: PChar);
  231. begin
  232.   inherited Init(AParent, AFlags, AFileName, ANameLength, AFilter);
  233.   OFN.lpstrTitle := 'File Save As';
  234. end;
  235.  
  236. function TCDFileSaveAs.CDExecute: Bool;
  237. begin
  238.   CDExecute := GetSaveFileName(OFN);
  239. end;
  240.  
  241.  
  242. end.
  243.