home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1998 April A / Pcwk4a98.iso / PROGRAM / DELPHI16 / Calmira / Src / SRC / ALIAS.PAS < prev    next >
Pascal/Delphi Source File  |  1997-02-15  |  6KB  |  197 lines

  1. {**************************************************************************}
  2. {                                                                          }
  3. {    Calmira shell for Microsoft« Windows(TM) 3.1                          }
  4. {    Source Release 1.0                                                    }
  5. {    Copyright (C) 1997  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 = 'CMSR';
  40.  
  41. type
  42.   TAlias = class(TFileItem)
  43.   private
  44.     FRef : TReference;
  45.     procedure RefChange(Sender : TObject);
  46.   public
  47.     constructor Create(const details : TSearchRec;
  48.       ADir : TDirectory; Stream : TStreamer);
  49.     destructor Destroy; override;
  50.     procedure Draw(Canvas: TCanvas; const Rect : TRect); override;
  51.     procedure Open; override;
  52.     procedure DragDrop(Source : TObject); override;
  53.     procedure Edit;
  54.     function AcceptsDrops : Boolean; override;
  55.     function GetStartInfo : string; override;
  56.     procedure AssignRef(ref: TReference); override;
  57.     function GetTitle: string; override;
  58.     class procedure Store(const filename: TFilename;
  59.       ARef : TReference; AnIcon : TIcon);
  60.     property Ref : TReference read FRef;
  61.   end;
  62.  
  63.  
  64. implementation
  65.  
  66. uses Resource, Controls, Settings, WinProcs, Start;
  67.  
  68. { The constructor assumes that the stream is a valid alias and has
  69.   already been verified by reading off the signature.
  70.  
  71.   FRef.OnChange is only assigned after loading is complete,
  72.   otherwise it would be triggered before the icon is created,
  73.   causing a GPF. }
  74.  
  75. constructor TAlias.Create(const details : TSearchRec;
  76.   ADir : TDirectory; Stream : TStreamer);
  77. begin
  78.   inherited Create(details, ADir);
  79.   FIsProgram := True;
  80.   FRef := TAliasReference.Create;
  81.   FRef.LoadFromStream(Stream);
  82.   Icon := TIcon.Create;
  83.   Icon.LoadFromStream(Stream);
  84.   FRef.OnChange := RefChange;
  85. end;
  86.  
  87.  
  88. { This is a class procedure so that the format of an alias only needs to
  89.   be defined in one place.  TIconic needs to write an alias without
  90.   creating a TAlias instance so it uses this procedure.
  91.  
  92.   The current format consists of a signature 'CMSR', meaning "Calmira
  93.   source release", followed by reference data, followed by the icon data }
  94.  
  95. class procedure TAlias.Store(const filename: TFilename;
  96.   ARef: TReference; AnIcon : TIcon);
  97. var s: TStreamer;
  98. begin
  99.   s := TStreamer.Create(filename, fmCreate);
  100.   try
  101.     s.WriteString(AliasSignature);
  102.     ARef.SaveToStream(s);
  103.     AnIcon.SaveToStream(s);
  104.   finally
  105.     s.Free;
  106.   end;
  107. end;
  108.  
  109.  
  110. destructor TAlias.Destroy;
  111. begin
  112.   Icon.Free;
  113.   FRef.Free;
  114.   inherited Destroy;
  115. end;
  116.  
  117.  
  118. procedure TAlias.Open;
  119. begin
  120.   Ref.Open;
  121. end;
  122.  
  123.  
  124. procedure TAlias.RefChange(Sender : TObject);
  125. begin
  126.   Ref.AssignIcon(Icon);
  127.   Dir.Update;
  128. end;
  129.  
  130.  
  131. procedure TAlias.Draw(Canvas: TCanvas; const Rect : TRect);
  132. begin
  133.   InternalDraw(Canvas, Rect, Ref.Caption);
  134.   if AliasArrows then with Rect do
  135.     Canvas.Draw(Left + ((Right - Left - 32) div 2), Top + 22, AliasArrow);
  136. end;
  137.  
  138.  
  139. procedure TAlias.Edit;
  140. begin
  141.   if Ref.Edit = mrOK then Store(Fullname, Ref, Icon);
  142. end;
  143.  
  144.  
  145. function TAlias.AcceptsDrops: Boolean;
  146. begin
  147.   Result := True;
  148. end;
  149.  
  150.  
  151. procedure TAlias.DragDrop(Source : TObject);
  152. begin
  153.   Ref.DragDrop(Source);
  154. end;
  155.  
  156.  
  157. procedure TAlias.AssignRef(ref: TReference);
  158. begin
  159.   { This just copies the reference fields across }
  160.   with ref do begin
  161.     BeginUpdate;
  162.     Kind := FRef.Kind;
  163.     Target := FRef.Target;
  164.     Caption := FRef.Caption;
  165.     Params := FRef.Params;
  166.     IconFile := FRef.IconFile;
  167.     IconIndex := FRef.IconIndex;
  168.     WorkingFolder := FRef.WorkingFolder;
  169.     ShowMode := FRef.ShowMode;
  170.     EndUpdate;
  171.   end;
  172. end;
  173.  
  174.  
  175. function TAlias.GetStartInfo : string;
  176. begin
  177.   with Ref do begin
  178.     if Kind = rkFile then
  179.       Result := PackStartInfo(Target, WorkingFolder, IconFile,
  180.         ShowMode, IconIndex)
  181.     else
  182.       Result := PackStartInfo('$Folder '+Target, '', IconFile,
  183.         0, IconIndex)
  184.   end;
  185. end;
  186.  
  187.  
  188. { Since aliases always draw their own caption, GetTitle
  189.   returns this so that TDirectory's sorting will work correctly }
  190.  
  191. function TAlias.GetTitle : string;
  192. begin
  193.   Result := Ref.Caption;
  194. end;
  195.  
  196. end.
  197.