home *** CD-ROM | disk | FTP | other *** search
/ QBasic & Borland Pascal & C / Delphi5.iso / Runimage / Delphi50 / Source / Vcl / EXTDLGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-08-11  |  7.8 KB  |  301 lines

  1.  
  2. {*******************************************************}
  3. {                                                       }
  4. {       Borland Delphi Visual Component Library         }
  5. {                                                       }
  6. {       Copyright (c) 1995,99 Inprise Corporation       }
  7. {                                                       }
  8. {*******************************************************}
  9.  
  10. unit ExtDlgs;
  11.  
  12. {$R-,H+,X+}
  13.  
  14. interface
  15.  
  16. uses Messages, Windows, SysUtils, Classes, Controls, StdCtrls, Graphics,
  17.   ExtCtrls, Buttons, Dialogs;
  18.  
  19. type
  20.  
  21. { TOpenPictureDialog }
  22.  
  23.   TOpenPictureDialog = class(TOpenDialog)
  24.   private
  25.     FPicture: TPicture;
  26.     FPicturePanel: TPanel;
  27.     FPictureLabel: TLabel;
  28.     FPreviewButton: TSpeedButton;
  29.     FPaintPanel: TPanel;
  30.     FPaintBox: TPaintBox;
  31.     function  IsFilterStored: Boolean;
  32.     procedure PaintBoxPaint(Sender: TObject);
  33.     procedure PreviewClick(Sender: TObject);
  34.     procedure PreviewKeyPress(Sender: TObject; var Key: Char);
  35.   protected
  36.     procedure DoClose; override;
  37.     procedure DoSelectionChange; override;
  38.     procedure DoShow; override;
  39.   published
  40.     property Filter stored IsFilterStored;
  41.   public
  42.     constructor Create(AOwner: TComponent); override;
  43.     destructor Destroy; override;
  44.     function Execute: Boolean; override;
  45.   end;
  46.  
  47. { TSavePictureDialog }
  48.  
  49.   TSavePictureDialog = class(TOpenPictureDialog)
  50.   public
  51.     function Execute: Boolean; override;
  52.   end;
  53.  
  54. implementation
  55.  
  56. uses Consts, Forms, CommDlg, Dlgs;
  57.  
  58. { TOpenPictureDialog }
  59.  
  60. {$R EXTDLGS.RES}
  61.  
  62. constructor TOpenPictureDialog.Create(AOwner: TComponent);
  63. begin
  64.   inherited Create(AOwner);
  65.   Filter := GraphicFilter(TGraphic);
  66.   FPicture := TPicture.Create;
  67.   FPicturePanel := TPanel.Create(Self);
  68.   with FPicturePanel do
  69.   begin
  70.     Name := 'PicturePanel';
  71.     Caption := '';
  72.     SetBounds(204, 5, 169, 200);
  73.     BevelOuter := bvNone;
  74.     BorderWidth := 6;
  75.     TabOrder := 1;
  76.     FPictureLabel := TLabel.Create(Self);
  77.     with FPictureLabel do
  78.     begin
  79.       Name := 'PictureLabel';
  80.       Caption := '';
  81.       SetBounds(6, 6, 157, 23);
  82.       Align := alTop;
  83.       AutoSize := False;
  84.       Parent := FPicturePanel;
  85.     end;
  86.     FPreviewButton := TSpeedButton.Create(Self);
  87.     with FPreviewButton do
  88.     begin
  89.       Name := 'PreviewButton';
  90.       SetBounds(77, 1, 23, 22);
  91.       Enabled := False;
  92.       Glyph.LoadFromResourceName(HInstance, 'PREVIEWGLYPH');
  93.       Hint := SPreviewLabel;
  94.       ParentShowHint := False;
  95.       ShowHint := True;
  96.       OnClick := PreviewClick;
  97.       Parent := FPicturePanel;
  98.     end;
  99.     FPaintPanel := TPanel.Create(Self);
  100.     with FPaintPanel do
  101.     begin
  102.       Name := 'PaintPanel';
  103.       Caption := '';
  104.       SetBounds(6, 29, 157, 145);
  105.       Align := alClient;
  106.       BevelInner := bvRaised;
  107.       BevelOuter := bvLowered;
  108.       TabOrder := 0;
  109.       FPaintBox := TPaintBox.Create(Self);
  110.       Parent := FPicturePanel;
  111.       with FPaintBox do
  112.       begin
  113.         Name := 'PaintBox';
  114.         SetBounds(0, 0, 153, 141);
  115.         Align := alClient;
  116.         OnDblClick := PreviewClick;
  117.         OnPaint := PaintBoxPaint;
  118.         Parent := FPaintPanel;
  119.       end;
  120.     end;
  121.   end;
  122. end;
  123.  
  124. destructor TOpenPictureDialog.Destroy;
  125. begin
  126.   FPaintBox.Free;
  127.   FPaintPanel.Free;
  128.   FPreviewButton.Free;
  129.   FPictureLabel.Free;
  130.   FPicturePanel.Free;
  131.   FPicture.Free;
  132.   inherited Destroy;
  133. end;
  134.  
  135. procedure TOpenPictureDialog.DoSelectionChange;
  136. var
  137.   FullName: string;
  138.   ValidPicture: Boolean;
  139.  
  140.   function ValidFile(const FileName: string): Boolean;
  141.   begin
  142.     Result := GetFileAttributes(PChar(FileName)) <> $FFFFFFFF;
  143.   end;
  144.  
  145. begin
  146.   FullName := FileName;
  147.   ValidPicture := FileExists(FullName) and ValidFile(FullName);
  148.   if ValidPicture then
  149.   try
  150.     FPicture.LoadFromFile(FullName);
  151.     FPictureLabel.Caption := Format(SPictureDesc, [FPicture.Width,
  152.       FPicture.Height]);
  153.     FPreviewButton.Enabled := True;
  154.   except
  155.     ValidPicture := False;
  156.   end;
  157.   if not ValidPicture then
  158.   begin
  159.     FPictureLabel.Caption := SPictureLabel;
  160.     FPreviewButton.Enabled := False;
  161.     FPicture.Assign(nil);
  162.   end;
  163.   FPaintBox.Invalidate;
  164.   inherited DoSelectionChange;
  165. end;
  166.  
  167. procedure TOpenPictureDialog.DoClose;
  168. begin
  169.   inherited DoClose;
  170.   { Hide any hint windows left behind }
  171.   Application.HideHint;
  172. end;
  173.  
  174. procedure TOpenPictureDialog.DoShow;
  175. var
  176.   PreviewRect, StaticRect: TRect;
  177. begin
  178.   { Set preview area to entire dialog }
  179.   GetClientRect(Handle, PreviewRect);
  180.   StaticRect := GetStaticRect;
  181.   { Move preview area to right of static area }
  182.   PreviewRect.Left := StaticRect.Left + (StaticRect.Right - StaticRect.Left);
  183.   Inc(PreviewRect.Top, 4);
  184.   FPicturePanel.BoundsRect := PreviewRect;
  185.   FPreviewButton.Left := FPaintPanel.BoundsRect.Right - FPreviewButton.Width - 2;
  186.   FPicture.Assign(nil);
  187.   FPicturePanel.ParentWindow := Handle;
  188.   inherited DoShow;
  189. end;
  190.  
  191. function TOpenPictureDialog.Execute;
  192. begin
  193.   if NewStyleControls and not (ofOldStyleDialog in Options) then
  194.     Template := 'DLGTEMPLATE' else
  195.     Template := nil;
  196.   Result := inherited Execute;
  197. end;
  198.  
  199. procedure TOpenPictureDialog.PaintBoxPaint(Sender: TObject);
  200. var
  201.   DrawRect: TRect;
  202.   SNone: string;
  203. begin
  204.   with TPaintBox(Sender) do
  205.   begin
  206.     Canvas.Brush.Color := Color;
  207.     DrawRect := ClientRect;
  208.     if FPicture.Width > 0 then
  209.     begin
  210.       with DrawRect do
  211.         if (FPicture.Width > Right - Left) or (FPicture.Height > Bottom - Top) then
  212.         begin
  213.           if FPicture.Width > FPicture.Height then
  214.             Bottom := Top + MulDiv(FPicture.Height, Right - Left, FPicture.Width)
  215.           else
  216.             Right := Left + MulDiv(FPicture.Width, Bottom - Top, FPicture.Height);
  217.           Canvas.StretchDraw(DrawRect, FPicture.Graphic);
  218.         end
  219.         else
  220.           with DrawRect do
  221.             Canvas.Draw(Left + (Right - Left - FPicture.Width) div 2, Top + (Bottom - Top -
  222.               FPicture.Height) div 2, FPicture.Graphic);
  223.     end
  224.     else
  225.       with DrawRect, Canvas do
  226.       begin
  227.         SNone := srNone;
  228.         TextOut(Left + (Right - Left - TextWidth(SNone)) div 2, Top + (Bottom -
  229.           Top - TextHeight(SNone)) div 2, SNone);
  230.       end;
  231.   end;
  232. end;
  233.  
  234. procedure TOpenPictureDialog.PreviewClick(Sender: TObject);
  235. var
  236.   PreviewForm: TForm;
  237.   Panel: TPanel;
  238. begin
  239.   PreviewForm := TForm.Create(Self);
  240.   with PreviewForm do
  241.   try
  242.     Name := 'PreviewForm';
  243.     Caption := SPreviewLabel;
  244.     BorderStyle := bsSizeToolWin;
  245.     KeyPreview := True;
  246.     Position := poScreenCenter;
  247.     OnKeyPress := PreviewKeyPress;
  248.     Panel := TPanel.Create(PreviewForm);
  249.     with Panel do
  250.     begin
  251.       Name := 'Panel';
  252.       Caption := '';
  253.       Align := alClient;
  254.       BevelOuter := bvNone;
  255.       BorderStyle := bsSingle;
  256.       BorderWidth := 5;
  257.       Color := clWindow;
  258.       Parent := PreviewForm;
  259.       with TImage.Create(PreviewForm) do
  260.       begin
  261.         Name := 'Image';
  262.         Caption := '';
  263.         Align := alClient;
  264.         Stretch := True;
  265.         Picture.Assign(FPicture);
  266.         Parent := Panel;
  267.       end;
  268.     end;
  269.     if FPicture.Width > 0 then
  270.     begin
  271.       ClientWidth := FPicture.Width + (ClientWidth - Panel.ClientWidth)+ 10;
  272.       ClientHeight := FPicture.Height + (ClientHeight - Panel.ClientHeight) + 10;
  273.     end;
  274.     ShowModal;
  275.   finally
  276.     Free;
  277.   end;
  278. end;
  279.  
  280. procedure TOpenPictureDialog.PreviewKeyPress(Sender: TObject; var Key: Char);
  281. begin
  282.   if Key = #27 then TForm(Sender).Close;
  283. end;
  284.  
  285. { TSavePictureDialog }
  286.  
  287. function TSavePictureDialog.Execute: Boolean;
  288. begin
  289.   if NewStyleControls and not (ofOldStyleDialog in Options) then
  290.     Template := 'DLGTEMPLATE' else
  291.     Template := nil;
  292.   Result := DoExecute(@GetSaveFileName);
  293. end;
  294.  
  295. function TOpenPictureDialog.IsFilterStored: Boolean;
  296. begin
  297.   Result := not (Filter = GraphicFilter(TGraphic));
  298. end;
  299.  
  300. end.
  301.