home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d56 / RMCTL.ZIP / rmColumns.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-22  |  14KB  |  526 lines

  1. {================================================================================
  2. Copyright (C) 1997-2001 Mills Enterprise
  3.  
  4. Unit     : rmColumns
  5. Purpose  : Saves column information and details from a listview to the registry
  6. Date     : 02-11-1999
  7. Author   : Ryan J. Mills
  8. Version  : 1.80
  9. ================================================================================}
  10.  
  11. unit rmColumns;
  12.  
  13. interface
  14.  
  15. {$I CompilerDefines.INC}
  16.  
  17. uses
  18.     Classes, ComCtrls, dialogs;
  19.  
  20. type
  21.   TrmColumnTypes = (ctString, ctDateTime, ctInteger, ctFloat);
  22.   TrmListColumns = class;
  23.  
  24.   TrmListColumn = class(TCollectionItem)
  25.   private
  26.     FAlignment: TAlignment;
  27.     FAutoSize: Boolean;
  28.     FCaption: string;
  29.     FMaxWidth: TWidth;
  30.     FMinWidth: TWidth;
  31.     FImageIndex: Integer;
  32.     FWidth: TWidth;
  33.     FColumnType:TrmColumnTypes;
  34.     fVisible: Boolean;
  35.     function IsWidthStored: Boolean;
  36.     procedure SetAlignment(Value: TAlignment);
  37.     procedure SetAutoSize(Value: Boolean);
  38.     procedure SetCaption(const Value: string);
  39.     procedure SetImageIndex(Value: Integer);
  40.     procedure SetMaxWidth(Value: TWidth);
  41.     procedure SetMinWidth(Value: TWidth);
  42.     procedure SetWidth(Value: TWidth);
  43.     function GetWidth:TWidth;
  44.     procedure SetVisible(const Value: Boolean);
  45.     procedure SetColumnIndex(const Value: integer);
  46.     function GetColumnIndex: integer;
  47.   protected
  48.     function GetDisplayName: string; override;
  49.   public
  50.     constructor Create(Collection: TCollection); override;
  51.     procedure Assign(Source: TPersistent); override;
  52.   published
  53.     property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
  54.     property AutoSize: Boolean read FAutoSize write SetAutoSize default False;
  55.     property Caption: string read FCaption write SetCaption;
  56.     property ColumnType : TrmColumnTypes read fColumnType write fColumnType;
  57.     property ImageIndex: Integer read FImageIndex write SetImageIndex default -1;
  58.     property MaxWidth: TWidth read FMaxWidth write SetMaxWidth default 0;
  59.     property MinWidth: TWidth read FMinWidth write SetMinWidth default 0;
  60.     property Width: TWidth read GetWidth write SetWidth stored IsWidthStored default 50;
  61.     property Visible: Boolean read fVisible write SetVisible default true;
  62.     property ColumnIndex: integer read GetColumnIndex write SetColumnIndex;
  63.   end;
  64.  
  65.   TrmListColumns = class(TCollection)
  66.   private
  67.     FOwner : TComponent;
  68.     function GetItem(Index: Integer): TrmListColumn;
  69.     procedure SetItem(Index: Integer; Value: TrmListColumn);
  70.   protected
  71.     function GetOwner: TPersistent; override;
  72.   public
  73.     constructor Create(AOwner:TComponent);
  74.     function Add: TrmListColumn;
  75.     property Items[Index: Integer]: TrmListColumn read GetItem write SetItem; default;
  76.   end;
  77.  
  78.   TrmColumns = class(TComponent)
  79.   private
  80.     { Private declarations }
  81.     FVersionID : Integer;
  82.     FColumns : TrmListColumns;
  83.     FSortColumn : integer;
  84.     FSortDsc : boolean;
  85.     procedure SetListColumns(Value: TrmListColumns);
  86.     procedure SetSortColumn(const Value: integer);
  87.     procedure SetSortDsc(const Value: boolean);
  88.   protected
  89.     { Protected declarations }
  90.   public
  91.     { Public declarations }
  92.     constructor Create(AOwner:TComponent); override;
  93.     destructor Destroy; override;
  94.     procedure Assign(Source: TPersistent); override;
  95.     procedure SaveToFile(FileName:string; Binary:Boolean);
  96.     procedure LoadFromFile(FileName:String; Binary:Boolean);
  97.     procedure SaveToReg(Key, Value:string; Binary:Boolean);
  98.     procedure LoadFromReg(key, Value:string; Binary:Boolean);
  99.     procedure SetListViewCols(lvObject:TListView);
  100.     procedure GetListViewCols(lvObject:TListView);
  101.   published
  102.     { Published declarations }
  103.     property SortColumn : integer read fSortColumn write SetSortColumn;
  104.     property SortDsc : boolean read FSortDsc write SetSortDsc;
  105.     property Columns: TrmListColumns read FColumns write SetListColumns;
  106.     property VersionID:integer read FVersionID write FVersionID;
  107.   end;
  108.  
  109.  
  110. implementation
  111.  
  112. Uses SysUtils,  Registry;
  113.  
  114. { TrmListColumn }
  115.  
  116. constructor TrmListColumn.Create(Collection: TCollection);
  117. begin
  118.   inherited Create(Collection);
  119.   FWidth := 50;
  120.   FAlignment := taLeftJustify;
  121.   FImageIndex := -1;
  122.   fVisible := true;
  123. end;
  124.  
  125. procedure TrmListColumn.SetCaption(const Value: string);
  126. begin
  127.   if FCaption <> Value then
  128.   begin
  129.     FCaption := Value;
  130.   end;
  131. end;
  132.  
  133. function TrmListColumn.GetWidth: TWidth;
  134. begin
  135.   Result := FWidth;
  136. end;
  137.  
  138. function TrmListColumn.IsWidthStored: Boolean;
  139. begin
  140.   Result := not FAutoSize;
  141. end;
  142.  
  143. procedure TrmListColumn.SetWidth(Value: TWidth);
  144. begin
  145.   if FWidth <> Value then
  146.   begin
  147.     FWidth := Value;
  148.   end;
  149. end;
  150.  
  151. procedure TrmListColumn.SetAlignment(Value: TAlignment);
  152. begin
  153.   if FAlignment <> Value then
  154.   begin
  155.     FAlignment := Value;
  156.   end;
  157. end;
  158.  
  159. procedure TrmListColumn.SetAutoSize(Value: Boolean);
  160. begin
  161.   if FAutoSize <> Value then
  162.   begin
  163.     FAutoSize := Value;
  164.   end;
  165. end;
  166.  
  167. procedure TrmListColumn.SetImageIndex(Value: Integer);
  168. begin
  169.   if FImageIndex <> Value then
  170.   begin
  171.     FImageIndex := Value;
  172.   end;
  173. end;
  174.  
  175. procedure TrmListColumn.SetMaxWidth(Value: TWidth);
  176. begin
  177.   if FMaxWidth <> Value then
  178.   begin
  179.     FMaxWidth := Value;
  180.   end;
  181. end;
  182.  
  183. procedure TrmListColumn.SetMinWidth(Value: TWidth);
  184. begin
  185.   if FMinWidth <> Value then
  186.   begin
  187.     FMinWidth := Value;
  188.   end;
  189. end;
  190.  
  191. procedure TrmListColumn.Assign(Source: TPersistent);
  192. var
  193.   Column: TrmListColumn;
  194. begin
  195.   if Source is TrmListColumn then
  196.   begin
  197.     Column := TrmListColumn(Source);
  198.     Alignment := Column.Alignment;
  199.     AutoSize := Column.AutoSize;
  200.     Caption := Column.Caption;
  201.     ImageIndex := Column.ImageIndex;
  202.     MaxWidth := Column.MaxWidth;
  203.     MinWidth := Column.MinWidth;
  204.     Width := Column.Width;
  205.     ColumnType := Column.ColumnType;
  206.   end
  207.   else inherited Assign(Source);
  208. end;
  209.  
  210. function TrmListColumn.GetDisplayName: string;
  211. begin
  212.   Result := Caption;
  213.   if Result = '' then Result := inherited GetDisplayName;
  214. end;
  215.  
  216. procedure TrmListColumn.SetVisible(const Value: Boolean);
  217. begin
  218.   if fVisible <> value then
  219.   begin
  220.      fVisible := Value;
  221.   end;
  222. end;
  223.  
  224. procedure TrmListColumn.SetColumnIndex(const Value: integer);
  225. begin
  226.    Self.Index := value;
  227. end;
  228.  
  229. function TrmListColumn.GetColumnIndex: integer;
  230. begin
  231.    result := Self.Index;
  232. end;
  233.  
  234. { TrmListColumns }
  235.  
  236. function TrmListColumns.GetItem(Index: Integer): TrmListColumn;
  237. begin
  238.   Result := TrmListColumn(inherited GetItem(Index));
  239. end;
  240.  
  241. procedure TrmListColumns.SetItem(Index: Integer; Value: TrmListColumn);
  242. begin
  243.   inherited SetItem(Index, Value);
  244. end;
  245.  
  246. function TrmListColumns.Add: TrmListColumn;
  247. begin
  248.   Result := TrmListColumn(inherited Add);
  249. end;
  250.  
  251. function TrmListColumns.GetOwner: TPersistent;
  252. begin
  253.      Result := FOwner;
  254. end;
  255.  
  256. constructor TrmListColumns.Create(AOwner: TComponent);
  257. begin
  258.      inherited Create(TrmListColumn);
  259.      FOwner := AOwner;
  260. end;
  261.  
  262. { TrmColumns }
  263.  
  264. constructor TrmColumns.Create(AOwner: TComponent);
  265. begin
  266.      inherited Create(AOwner);
  267.      FColumns := TrmListColumns.create(self);
  268. end;
  269.  
  270. destructor TrmColumns.Destroy;
  271. begin
  272.      FColumns.free;
  273.      inherited;
  274. end;
  275.  
  276. procedure TrmColumns.SetListViewCols(lvObject: TListView);
  277. var
  278.    index : integer;
  279. begin
  280.      lvObject.Columns.BeginUpdate;
  281.      try
  282.         lvObject.Columns.Clear;
  283.         index := 0;
  284.         While index < Columns.Count do
  285.         begin
  286.              if Columns[index].visible then
  287.              with lvObject.Columns.Add do
  288.              begin
  289.                   Alignment := Columns[index].Alignment;
  290.                   AutoSize := Columns[index].AutoSize;
  291.                   Caption := Columns[index].Caption;
  292.                   ImageIndex := Columns[index].ImageIndex;
  293.                   MaxWidth := Columns[index].MaxWidth;
  294.                   MinWidth := Columns[index].MinWidth;
  295.                   Width := Columns[index].Width;
  296.              end;
  297.              inc(index);
  298.         end;
  299.      finally
  300.         lvObject.Columns.EndUpdate;
  301.      end;
  302. end;
  303.  
  304. procedure TrmColumns.GetListViewCols(lvObject: TListView);
  305. var
  306.    index : integer;
  307. begin
  308. //     Columns.Clear;
  309.      index := 0;
  310.      While index < lvObject.Columns.Count do
  311.      begin
  312.           with Columns[index] do
  313.           begin
  314.                Alignment := lvObject.Columns[index].Alignment;
  315.                AutoSize := lvObject.Columns[index].AutoSize;
  316.                Caption := lvObject.Columns[index].Caption;
  317.                ImageIndex := lvObject.Columns[index].ImageIndex;
  318.                MaxWidth := lvObject.Columns[index].MaxWidth;
  319.                MinWidth := lvObject.Columns[index].MinWidth;
  320.                Width := lvObject.Columns[index].Width;
  321.           end;
  322.           inc(index);
  323.      end;
  324. end;
  325.  
  326. procedure TrmColumns.LoadFromFile(FileName: String; Binary: Boolean);
  327. var
  328.    StrmIn, TempStrm : TStream;
  329.    TmpCols : TrmColumns;
  330. begin
  331.      TmpCols := TrmColumns.Create(nil);
  332.      try
  333.         StrmIn := TFileStream.Create(fileName,fmOpenRead);
  334.         try
  335.            if Binary then
  336.               StrmIn.ReadComponent(TmpCols)
  337.            else
  338.            begin
  339.                 TempStrm := TMemoryStream.Create;
  340.                 try
  341.                    ObjectTextToBinary(StrmIn,TempStrm);
  342.                    TempStrm.Position := 0;
  343.                    TempStrm.ReadComponent(TmpCols);
  344.                 finally
  345.                    TempStrm.Free;
  346.                 end;
  347.            end;
  348.         finally
  349.            StrmIn.Free;
  350.         end;
  351.         Self.Assign(TmpCols);
  352.      finally
  353.         TmpCols.free;
  354.      end;
  355. end;
  356.  
  357. procedure TrmColumns.LoadFromReg(key, value: string; Binary: Boolean);
  358. var
  359.    StrmIn, TempStrm : TStream;
  360.    TmpCols : TrmColumns;
  361.    Reg : TRegistry;
  362.    Buf : Pointer;
  363.    BufSize : integer;
  364. begin
  365.      BufSize := -1;
  366.      StrmIn := TMemoryStream.Create;
  367.      try
  368.         Reg := TRegistry.Create;
  369.         try
  370.            if reg.OpenKey(key,false) then
  371.            begin
  372.                 if Reg.ValueExists(Value) then
  373.                 begin
  374.                      BufSize := Reg.GetDataSize(Value);
  375.                      if BufSize > -1 then
  376.                      begin
  377.                           GetMem(Buf,BufSize);
  378.                           try
  379.                              Reg.ReadBinaryData(Value,Buf^,BufSize);
  380.                              StrmIn.WriteBuffer(Buf^,BufSize);
  381.                           finally
  382.                              FreeMem(Buf,BufSize);
  383.                           end;
  384.                      end;
  385.                 end;
  386.                 Reg.CloseKey;
  387.            end;
  388.         finally
  389.            Reg.CloseKey;
  390.            Reg.free;
  391.         end;
  392.  
  393.         if BufSize > -1 then
  394.         begin
  395.              StrmIn.Position := 0;
  396.              TmpCols := TrmColumns.Create(nil);
  397.              try
  398.                 if Binary then
  399.                    StrmIn.ReadComponent(TmpCols)
  400.                 else
  401.                 begin
  402.                      TempStrm := TMemoryStream.Create;
  403.                      try
  404.                         ObjectTextToBinary(StrmIn,TempStrm);
  405.                         TempStrm.Position := 0;
  406.                         TempStrm.ReadComponent(TmpCols);
  407.                      finally
  408.                         TempStrm.Free;
  409.                      end;
  410.                 end;
  411.                 Self.Assign(TmpCols);
  412.              finally
  413.                 TmpCols.free;
  414.              end;
  415.         end;
  416.      finally
  417.         StrmIn.Free;
  418.      end;
  419. end;
  420.  
  421. procedure TrmColumns.SaveToFile(FileName: string; Binary: Boolean);
  422. var
  423.    StrmOut, TempStrm : TStream;
  424.    Name : string;
  425. begin
  426.      Name := Self.Name;
  427.      Self.Name := '';
  428.      StrmOut := TFileStream.Create(fileName,fmCreate);
  429.      try
  430.         if Binary then
  431.            StrmOut.WriteComponent(Self)
  432.         else
  433.         begin
  434.              TempStrm := TMemoryStream.Create;
  435.              try
  436.                 TempStrm.WriteComponent(Self);
  437.                 TempStrm.Position := 0;
  438.                 ObjectBinaryToText(TempStrm,StrmOut);
  439.              finally
  440.                 TempStrm.Free;
  441.              end;
  442.         end;
  443.      finally
  444.         StrmOut.Free;
  445.      end;
  446.      Self.Name := Name;
  447. end;
  448.  
  449. procedure TrmColumns.SaveToReg(Key, Value: string; Binary: Boolean);
  450. var
  451.    StrmOut, TempStrm : TStream;
  452.    Name : string;
  453.    reg : TRegistry;
  454.    Buf : pointer;
  455. begin
  456.      Name := Self.Name;
  457.      Self.Name := '';
  458.      StrmOut := TMemoryStream.Create;
  459.      try
  460.         if Binary then
  461.            StrmOut.WriteComponent(Self)
  462.         else
  463.         begin
  464.              TempStrm := TMemoryStream.Create;
  465.              try
  466.                 TempStrm.WriteComponent(Self);
  467.                 TempStrm.Position := 0;
  468.                 ObjectBinaryToText(TempStrm,StrmOut);
  469.              finally
  470.                 TempStrm.Free;
  471.              end;
  472.         end;
  473.         Reg := TRegistry.Create;
  474.         try
  475.            GetMem(buf,StrmOut.Size);
  476.            try
  477.               StrmOut.Position := 0;
  478.               StrmOut.ReadBuffer(Buf^,StrmOut.Size);
  479.               if reg.OpenKey(key,true) then
  480.               begin
  481.                    Reg.WriteBinaryData(Value,Buf^,StrmOut.Size);
  482.                    Reg.CloseKey;
  483.               end;
  484.            finally
  485.               FreeMem(Buf,StrmOut.Size);
  486.            end;
  487.         finally
  488.            Reg.CloseKey;
  489.            Reg.free;
  490.         end;
  491.      finally
  492.         StrmOut.Free;
  493.      end;
  494.      Self.Name := Name;
  495. end;
  496.  
  497. procedure TrmColumns.SetListColumns(Value: TrmListColumns);
  498. begin
  499.   FColumns.Assign(Value);
  500. end;
  501.  
  502. procedure TrmColumns.Assign(Source: TPersistent);
  503. begin
  504.      if source is TrmColumns then
  505.      begin
  506.           VersionID := TrmColumns(Source).VersionID;
  507.           SortColumn := TrmColumns(Source).SortColumn;
  508.           SortDsc := TrmColumns(Source).SortDsc;
  509.           Columns.assign(TrmColumns(Source).Columns);
  510.      end
  511.      else
  512.      inherited assign(source);
  513. end;
  514.  
  515. procedure TrmColumns.SetSortColumn(const Value: integer);
  516. begin
  517.   fSortColumn := Value;
  518. end;
  519.  
  520. procedure TrmColumns.SetSortDsc(const Value: boolean);
  521. begin
  522.   FSortDsc := Value;
  523. end;
  524.  
  525. end.
  526.