home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual Thematic 25: Programming / pc_actual_25.iso / Delphi / CompositeComponentsPack / DEMO / BxDual.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1999-02-12  |  6.0 KB  |  216 lines

  1. {*******************************************************}
  2. {                                                       }
  3. {       Delphi Visual Component Library                 }
  4. {                                                       }
  5. {       This Composite created with                     }
  6. {       Composite Components Pack (CCPack)              }
  7. {                                                       }
  8. {       Copyright (c) 1997-99 Sergey Orlik              }
  9. {                                                       }
  10. {     Written by:                                       }
  11. {       Sergey Orlik                                    }
  12. {       product manager                                 }
  13. {       Russia, C.I.S. and Baltic States (former USSR)  }
  14. {       Inprise Moscow office                           }
  15. {       Internet:  sorlik@inprise.ru                    }
  16. {       www.geocities.com/SiliconValley/Way/9006/       }
  17. {                                                       }
  18. {*******************************************************}
  19.  
  20. unit BxDual;
  21.  
  22. interface
  23.  
  24. uses
  25.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  26.   Boxes, ExtCtrls, StdCtrls, ComCtrls, Buttons;
  27.  
  28. type
  29.   TBxDualList = class(TControlGroupBox)
  30.     SrcPanel: TPanel;
  31.     SrcLabel: TLabel;
  32.     SrcList: TListBox;
  33.     BtnPanel: TPanel;
  34.     DstPanel: TPanel;
  35.     DstLabel: TLabel;
  36.     DstList: TListBox;
  37.     IncludeBtn: TSpeedButton;
  38.     IncAllBtn: TSpeedButton;
  39.     ExcludeBtn: TSpeedButton;
  40.     ExAllBtn: TSpeedButton;
  41.     procedure ExcAllBtnClick(Sender: TObject);
  42.     procedure ExcludeBtnClick(Sender: TObject);
  43.     procedure IncAllBtnClick(Sender: TObject);
  44.     procedure IncludeBtnClick(Sender: TObject);
  45.     procedure BxDualListResize(Sender: TObject);
  46.   private
  47.   { SetXxx/GetXxx methods to publish sub-component's properties }
  48.     function GetSrcItems: TStrings;
  49.     function GetDstItems: TStrings;
  50.     procedure SetSrcItems(const Value: TStrings);
  51.     procedure SetDstItems(const Value: TStrings);
  52.     function GetLabelSrc: string;
  53.     procedure SetLabelSrc(const Value: string);
  54.     function GetLabelDst: string;
  55.     procedure SetLabelDst(const Value: string);
  56.   public
  57.     procedure MoveSelected(List: TCustomListBox; Items: TStrings);
  58.     procedure SetItem(List: TListBox; Index: Integer);
  59.     function GetFirstSelection(List: TCustomListBox): Integer;
  60.     procedure SetButtons;
  61.   published
  62.     property Align;
  63.   { Publishing sub-component's properties via SetXxx/GetXxx methods }
  64.     property ItemsSrc: TStrings read GetSrcItems write SetSrcItems;
  65.     property ItemsDst: TStrings read GetDstItems write SetDstItems;
  66.     property LabelSrc: string read GetLabelSrc write SetLabelSrc;
  67.     property LabelDst: string read GetLabelDst write SetLabelDst;    
  68.   end;
  69.  
  70. implementation
  71.  
  72. {$R *.DFM}
  73.  
  74. const
  75.   BtnPanelWidth = 40;
  76.   
  77. procedure TBxDualList.ExcAllBtnClick(Sender: TObject);
  78. var
  79.   I: Integer;
  80. begin
  81.   for I := 0 to DstList.Items.Count - 1 do
  82.     SrcList.Items.AddObject(DstList.Items[I], DstList.Items.Objects[I]);
  83.   DstList.Items.Clear;
  84.   SetItem(DstList, 0);
  85. end;
  86.  
  87. procedure TBxDualList.ExcludeBtnClick(Sender: TObject);
  88. var
  89.   Index: Integer;
  90. begin
  91.   Index := GetFirstSelection(DstList);
  92.   MoveSelected(DstList, SrcList.Items);
  93.   SetItem(DstList, Index);
  94. end;
  95.  
  96. procedure TBxDualList.IncAllBtnClick(Sender: TObject);
  97. var
  98.   I: Integer;
  99. begin
  100.   for I := 0 to SrcList.Items.Count - 1 do
  101.     DstList.Items.AddObject(SrcList.Items[I], 
  102.       SrcList.Items.Objects[I]);
  103.   SrcList.Items.Clear;
  104.   SetItem(SrcList, 0);
  105. end;
  106.  
  107. procedure TBxDualList.IncludeBtnClick(Sender: TObject);
  108. var
  109.   Index: Integer;
  110. begin
  111.   Index := GetFirstSelection(SrcList);
  112.   MoveSelected(SrcList, DstList.Items);
  113.   SetItem(SrcList, Index);
  114. end;
  115.  
  116. procedure TBxDualList.MoveSelected(List: TCustomListBox; Items: TStrings);
  117. var
  118.   I: Integer;
  119. begin
  120.   for I := List.Items.Count - 1 downto 0 do
  121.     if List.Selected[I] then
  122.     begin
  123.       Items.AddObject(List.Items[I], List.Items.Objects[I]);
  124.       List.Items.Delete(I);
  125.     end;
  126. end;
  127.  
  128. procedure TBxDualList.SetButtons;
  129. var
  130.   SrcEmpty, DstEmpty: Boolean;
  131. begin
  132.   SrcEmpty := SrcList.Items.Count = 0;
  133.   DstEmpty := DstList.Items.Count = 0;
  134.   IncludeBtn.Enabled := not SrcEmpty;
  135.   IncAllBtn.Enabled := not SrcEmpty;
  136.   ExcludeBtn.Enabled := not DstEmpty;
  137.   ExAllBtn.Enabled := not DstEmpty;
  138. end;
  139.  
  140. function TBxDualList.GetFirstSelection(List: TCustomListBox): Integer;
  141. begin
  142.   for Result := 0 to List.Items.Count - 1 do
  143.     if List.Selected[Result] then Exit;
  144.   Result := LB_ERR;
  145. end;
  146.  
  147. procedure TBxDualList.SetItem(List: TListBox; Index: Integer);
  148. var
  149.   MaxIndex: Integer;
  150. begin
  151.   with List do
  152.   begin
  153.     SetFocus;
  154.     MaxIndex := List.Items.Count - 1;
  155.     if Index = LB_ERR then Index := 0
  156.     else if Index > MaxIndex then Index := MaxIndex;
  157.     Selected[Index] := True;
  158.   end;
  159.   SetButtons;
  160. end;
  161.  
  162. { SetXxx/GetXxx methods to publish sub-component's properties }
  163.  
  164. function TBxDualList.GetSrcItems: TStrings;
  165. begin
  166.   Result:=SrcList.Items;
  167. end;
  168.  
  169. function TBxDualList.GetDstItems: TStrings;
  170. begin
  171.   Result:=DstList.Items;
  172. end;
  173.  
  174. procedure TBxDualList.SetSrcItems(const Value: TStrings);
  175. begin
  176.   SrcList.Items.Assign(Value);
  177. end;
  178.  
  179. procedure TBxDualList.SetDstItems(const Value: TStrings);
  180. begin
  181.   DstList.Items.Assign(Value);
  182. end;
  183.  
  184. function TBxDualList.GetLabelSrc: string;
  185. begin
  186.   Result:=SrcLabel.Caption;
  187. end;
  188.  
  189. procedure TBxDualList.SetLabelSrc(const Value: string);
  190. begin
  191.   SrcLabel.Caption:=Value;
  192. end;
  193.  
  194. function TBxDualList.GetLabelDst: string;
  195. begin
  196.   Result:=DstLabel.Caption;
  197. end;
  198.  
  199. procedure TBxDualList.SetLabelDst(const Value: string);
  200. begin
  201.   DstLabel.Caption:=Value;
  202. end;
  203.  
  204. { Don't replace this OnResize event handler when you use TBxDualList
  205.   to prevent non-proportional resizing }
  206.  
  207. procedure TBxDualList.BxDualListResize(Sender: TObject);
  208. var
  209.   PanelWidth : integer;
  210. begin
  211.   PanelWidth:=(ClientWidth-BtnPanel.Width) div 2 - 2;
  212.   SrcPanel.Width:=PanelWidth;
  213. end;
  214.  
  215. end.
  216.