home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1997 May / Pcwk0597.iso / borland / cb / setup / cbuilder / data.z / DUALLIST.CPP < prev    next >
C/C++ Source or Header  |  1997-02-28  |  3KB  |  109 lines

  1. //---------------------------------------------------------------------
  2. #include <vcl.h>
  3. #pragma hdrstop
  4.  
  5. #include "DualList.h"
  6. //--------------------------------------------------------------------- 
  7. #pragma resource "*.dfm"
  8. TDualListDlg *DualListDlg;
  9. //--------------------------------------------------------------------- 
  10. __fastcall TDualListDlg::TDualListDlg(TComponent* AOwner)
  11.     : TForm(AOwner)
  12. {
  13. }
  14. //--------------------------------------------------------------------- 
  15. void __fastcall TDualListDlg::IncludeBtnClick(TObject *Sender)
  16. {
  17.     int Index;
  18.  
  19.     Index = GetFirstSelection(SrcList);
  20.     MoveSelected(SrcList, DstList->Items);
  21.     SetItem(SrcList, Index);
  22. }
  23. //--------------------------------------------------------------------- 
  24. void __fastcall TDualListDlg::ExcludeBtnClick(TObject *Sender)
  25. {
  26.     int Index;
  27.  
  28.     Index = GetFirstSelection(DstList);
  29.     MoveSelected(DstList, SrcList->Items);
  30.     SetItem(DstList, Index);
  31. }
  32. //---------------------------------------------------------------------
  33. void __fastcall TDualListDlg::IncAllBtnClick(TObject *Sender)
  34. {
  35.     int i;
  36.  
  37.     for (i=0; i < SrcList->Items->Count; i++)
  38.         DstList->Items->AddObject(SrcList->Items->Strings[i], SrcList->Items->Objects[i]);
  39.     SrcList->Items->Clear();
  40.     SetItem(SrcList, 0);
  41. }
  42. //---------------------------------------------------------------------
  43. void __fastcall TDualListDlg::ExcAllBtnClick(TObject *Sender)
  44. {
  45.     int i;
  46.  
  47.     for (i=0; i < DstList->Items->Count; i++)
  48.         SrcList->Items->AddObject(DstList->Items->Strings[i], DstList->Items->Objects[i]);
  49.  
  50.     DstList->Items->Clear();
  51.     SetItem(DstList, 0);
  52. }
  53. //---------------------------------------------------------------------
  54. void __fastcall TDualListDlg::MoveSelected(TCustomListBox *List, TStrings *Items)
  55. {
  56.     int i;
  57.  
  58.     for (i=List->Items->Count-1; i >= 0; i--)
  59.     {
  60.         if (List->Selected[i])
  61.         {
  62.             Items->AddObject(List->Items->Strings[i], List->Items->Objects[i]);
  63.             List->Items->Delete(i);
  64.         }
  65.     }
  66. }
  67. //---------------------------------------------------------------------
  68. void __fastcall TDualListDlg::SetButtons()
  69. {
  70.     bool SrcEmpty, DstEmpty;
  71.  
  72.     SrcEmpty = (SrcList->Items->Count == 0);
  73.     DstEmpty = (DstList->Items->Count == 0);
  74.     IncludeBtn->Enabled = (! SrcEmpty);
  75.     IncAllBtn->Enabled = (! SrcEmpty);
  76.     ExcludeBtn->Enabled = (! DstEmpty);
  77.     ExAllBtn->Enabled = (! DstEmpty);
  78. }
  79. //---------------------------------------------------------------------
  80. int __fastcall TDualListDlg::GetFirstSelection(TCustomListBox *List)
  81. {
  82.     int i;
  83.  
  84.     for (i=0; i < List->Items->Count; i++)
  85.     {
  86.         if (List->Selected[i])
  87.             return i;
  88.     }
  89.  
  90.     return LB_ERR;
  91. }
  92. //---------------------------------------------------------------------
  93. void __fastcall TDualListDlg::SetItem(TListBox *List, int Index)
  94. {
  95.     int MaxIndex;
  96.  
  97.     List->SetFocus();
  98.     MaxIndex = List->Items->Count - 1;
  99.  
  100.     if (Index == LB_ERR) 
  101.         Index = 0;
  102.     else if (Index > MaxIndex)
  103.         Index = MaxIndex;
  104.     List->Selected[Index] = true;
  105.  
  106.     SetButtons();
  107. }
  108. //---------------------------------------------------------------------
  109.