home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 2000 March / pcp161b.iso / full / delphi / RUNIMAGE / DELPHI30 / SOURCE / VCL / EXTDLGS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1997-08-03  |  7.5 KB  |  291 lines

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