home *** CD-ROM | disk | FTP | other *** search
/ PC Open 19 / pcopen19.iso / Zipped / CALMIR21.ZIP / SOURCE.ZIP / SRC / ALIAS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1998-02-20  |  5.8 KB  |  196 lines

  1. {**************************************************************************}
  2. {                                                                          }
  3. {    Calmira shell for Microsoft« Windows(TM) 3.1                          }
  4. {    Source Release 2.1                                                    }
  5. {    Copyright (C) 1997-1998 Li-Hsin Huang                                 }
  6. {                                                                          }
  7. {    This program is free software; you can redistribute it and/or modify  }
  8. {    it under the terms of the GNU General Public License as published by  }
  9. {    the Free Software Foundation; either version 2 of the License, or     }
  10. {    (at your option) any later version.                                   }
  11. {                                                                          }
  12. {    This program is distributed in the hope that it will be useful,       }
  13. {    but WITHOUT ANY WARRANTY; without even the implied warranty of        }
  14. {    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         }
  15. {    GNU General Public License for more details.                          }
  16. {                                                                          }
  17. {    You should have received a copy of the GNU General Public License     }
  18. {    along with this program; if not, write to the Free Software           }
  19. {    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.             }
  20. {                                                                          }
  21. {**************************************************************************}
  22.  
  23. unit Alias;
  24.  
  25. { TAlias is a descendant of TFileItem that has it's own reference object.
  26.   It overrides many of TFileItem's methods so that operations are
  27.   performed using the reference object instead of the alias file.
  28.  
  29.   Notice that there isn't much code here because all the functionality
  30.   is already handled by TReference.  TAlias just interacts through icon
  31.   windows. }
  32.  
  33. interface
  34.  
  35. uses Classes, Directry, Referenc, Shorts, SysUtils, Graphics,
  36.   Streamer, WinTypes;
  37.  
  38. const
  39.   AliasSignature : array[0..3] of Char = 'CMSR';
  40. var
  41.   AliasSigValue  : Longint absolute AliasSignature;
  42.  
  43. type
  44.   TAlias = class(TFileItem)
  45.   private
  46.     FRef : TReference;
  47.     procedure RefChange(Sender : TObject);
  48.   public
  49.     constructor Create(const details : TSearchRec;
  50.       ADir : TDirectory; Stream : TStreamer);
  51.     destructor Destroy; override;
  52.     procedure Draw(Canvas: TCanvas; const Rect : TRect); override;
  53.     procedure Open; override;
  54.     procedure DragDrop(Source : TObject); override;
  55.     procedure Edit;
  56.     function AcceptsDrops : Boolean; override;
  57.     function GetStartInfo : string; override;
  58.     procedure AssignRef(ref: TReference); override;
  59.     function GetTitle: string; override;
  60.     class procedure Store(const filename: TFilename;
  61.       ARef : TReference; AnIcon : TIcon);
  62.     property Ref : TReference read FRef;
  63.   end;
  64.  
  65.  
  66. implementation
  67.  
  68. uses Resource, Controls, Settings, WinProcs, Start;
  69.  
  70. { The constructor assumes that the stream is a valid alias and has
  71.   already been verified by reading off the signature.
  72.  
  73.   FRef.OnChange is only assigned after loading is complete,
  74.   otherwise it would be triggered before the icon is created,
  75.   causing a GPF. }
  76.  
  77. constructor TAlias.Create(const details : TSearchRec;
  78.   ADir : TDirectory; Stream : TStreamer);
  79. begin
  80.   inherited Create(details, ADir);
  81.   FIsProgram := True;
  82.   FRef := TAliasReference.Create;
  83.   FRef.LoadFromStream(Stream);
  84.   Icon := TIcon.Create;
  85.   Icon.LoadFromStream(Stream);
  86.   FRef.OnChange := RefChange;
  87. end;
  88.  
  89.  
  90. { This is a class procedure so that the format of an alias only needs to
  91.   be defined in one place.  TIconic needs to write an alias without
  92.   creating a TAlias instance so it uses this procedure. }
  93.  
  94. class procedure TAlias.Store(const filename: TFilename;
  95.   ARef: TReference; AnIcon : TIcon);
  96. var s: TStreamer;
  97. begin
  98.   s := TStreamer.Create(filename, fmCreate);
  99.   try
  100.     s.WriteString(AliasSignature);
  101.     ARef.SaveToStream(s);
  102.     AnIcon.SaveToStream(s);
  103.   finally
  104.     s.Free;
  105.   end;
  106. end;
  107.  
  108.  
  109. destructor TAlias.Destroy;
  110. begin
  111.   Icon.Free;
  112.   FRef.Free;
  113.   inherited Destroy;
  114. end;
  115.  
  116.  
  117. procedure TAlias.Open;
  118. begin
  119.   Ref.Open;
  120. end;
  121.  
  122.  
  123. procedure TAlias.RefChange(Sender : TObject);
  124. begin
  125.   Ref.AssignIcon(Icon);
  126.   Dir.Update;
  127. end;
  128.  
  129.  
  130. procedure TAlias.Draw(Canvas: TCanvas; const Rect : TRect);
  131. begin
  132.   InternalDraw(Canvas, Rect, Ref.Caption);
  133.   if AliasArrows then with Rect do
  134.     Canvas.Draw(Left + ((Right - Left - 32) div 2), Top + 22, AliasArrow);
  135. end;
  136.  
  137.  
  138. procedure TAlias.Edit;
  139. begin
  140.   if Ref.Edit = mrOK then Store(Fullname, Ref, Icon);
  141. end;
  142.  
  143.  
  144. function TAlias.AcceptsDrops: Boolean;
  145. begin
  146.   Result := True;
  147. end;
  148.  
  149.  
  150. procedure TAlias.DragDrop(Source : TObject);
  151. begin
  152.   Ref.DragDrop(Source);
  153. end;
  154.  
  155.  
  156. procedure TAlias.AssignRef(ref: TReference);
  157. begin
  158.   { This just copies the reference fields across }
  159.   with ref do begin
  160.     BeginUpdate;
  161.     Kind := FRef.Kind;
  162.     Target := FRef.Target;
  163.     Caption := FRef.Caption;
  164.     Params := FRef.Params;
  165.     IconFile := FRef.IconFile;
  166.     IconIndex := FRef.IconIndex;
  167.     WorkingFolder := FRef.WorkingFolder;
  168.     ShowMode := FRef.ShowMode;
  169.     EndUpdate;
  170.   end;
  171. end;
  172.  
  173.  
  174. function TAlias.GetStartInfo : string;
  175. begin
  176.   with Ref do begin
  177.     if Kind = rkFile then
  178.       Result := PackStartInfo(Target, WorkingFolder, IconFile,
  179.         ShowMode, IconIndex)
  180.     else
  181.       Result := PackStartInfo('$Folder '+Target, '', IconFile,
  182.         0, IconIndex)
  183.   end;
  184. end;
  185.  
  186.  
  187. { Since aliases always draw their own caption, GetTitle
  188.   returns this so that TDirectory's sorting will work correctly }
  189.  
  190. function TAlias.GetTitle : string;
  191. begin
  192.   Result := Ref.Caption;
  193. end;
  194.  
  195. end.
  196.