unit ufrmFilter;
{
  COSBI: Comprehensive Open Source Benchmarking Initiative
  Copyright (c) 2004 Van Smith

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  The current web address of the GNU General Public License is:
  http://www.gnu.org/licenses/gpl.html

  You can contact the authors of this software at:
  cosbi@vanshardware.com
  See www.vanshardware.com or www.cosbi.org for more contact details.
}

//==============================================================================
// Unit name: ufrmFilter
// Unit description: This unit filters results for OSMark's result viewer.
// Author: Van Smith
// Date: February 6, 2004
// OS dependent: Yes: Windows
// Resolution dependent: No
// External unit dependencies: none
//==============================================================================
// Modification History
// ------------ -------
// Ver  Date   Who    Description
// ==== ====== ====== ==========================================================
// 1.0 020604 Van     Created.
//==============================================================================

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBClient, StdCtrls;

type
  TfrmFilter = class(TForm)
    ListBoxVisible: TListBox;
    ListBoxInvisible: TListBox;
    cdsFilter: TClientDataSet;
    Label1: TLabel;
    Label2: TLabel;
    btnHide: TButton;
    btnShow: TButton;
    btnOK: TButton;
    Button2: TButton;
    procedure btnHideClick(Sender: TObject);
    procedure btnShowClick(Sender: TObject);
    procedure ListBoxVisibleDblClick(Sender: TObject);
    procedure ListBoxInvisibleDblClick(Sender: TObject);
    procedure btnOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    fChanged : Boolean;
    procedure HideSelected;
    procedure ShowSelected;
    procedure FilterChanged;
  public
    { Public declarations }
    procedure ListInvisible;
    procedure ListVisible;
    procedure PopulateListBoxes;
  end;

implementation

{$R *.dfm}



procedure TfrmFilter.ListInvisible;
var
  s : string;
  lsFilter : string;
begin
  ListBoxInvisible.Sorted := TRUE;
  with cdsFilter do begin
    if Filter <> '' then begin
      lsFilter := cdsFilter.Filter;
      // invert selection:
      cdsFilter.Filter := 'NOT (' + cdsFilter.Filter + ')';
      cdsFilter.Filtered := TRUE;
      First;
      repeat
        s := FieldByName('PlatformName').Value;
        ListBoxInvisible.Items.Add( s );
        Next;
      until Eof ;  // repeat
      cdsFilter.Filter := lsFilter;
      cdsFilter.Filtered := TRUE;
    end; // if
  end; // with
end;

procedure TfrmFilter.ListVisible;
var
  s : string;
begin
  ListBoxVisible.Sorted := TRUE;
  with cdsFilter do begin
    First;
    repeat
      s := FieldByName('PlatformName').Value;
      ListBoxVisible.Items.Add( s );
      Next;
    until Eof ;  // repeat
  end; // with
end;

procedure TfrmFilter.PopulateListBoxes;
begin
  ListInvisible;
  ListVisible;
end;

procedure TfrmFilter.btnHideClick(Sender: TObject);
begin
  HideSelected;
end;

procedure TfrmFilter.btnShowClick(Sender: TObject);
begin
  ShowSelected;
end; // procedure TfrmFilter.btnShowClick

procedure TfrmFilter.HideSelected;
var
  i : integer;
  s : string;
begin
  for i := ListBoxVisible.Count - 1 downto 0 do begin
    if ListBoxVisible.Selected[ i ] then begin
      s := ListBoxVisible.Items[ i ];
      ListBoxInvisible.Items.Add( s );
    end; // if
  end;
  ListBoxVisible.DeleteSelected;
  FilterChanged;
end; // procedure TfrmFilter.HideSelected;

procedure TfrmFilter.ShowSelected;
var
  i : integer;
  s : string;
begin
  for i := ListBoxInvisible.Count - 1 downto 0 do begin
    if ListBoxInvisible.Selected[ i ] then begin
      s := ListBoxInvisible.Items[ i ];
      ListBoxVisible.Items.Add( s );
    end; // if
  end;
  ListBoxInvisible.DeleteSelected;
  FilterChanged;
end; // procedure TfrmFilter.ShowSelected;

procedure TfrmFilter.ListBoxVisibleDblClick(Sender: TObject);
begin
  HideSelected;
end;

procedure TfrmFilter.ListBoxInvisibleDblClick(Sender: TObject);
begin
  ShowSelected;
end;

procedure TfrmFilter.btnOKClick(Sender: TObject);
var
  i : integer;
  s : string;
begin
  s := '';
  for i := 0 to ListBoxVisible.Count - 1 do begin
    if s <> '' then begin
      s := s + ' OR PlatformName = ''' + ListBoxVisible.Items[ i ] + '''';
    end else begin
      s := 'PlatformName = ''' + ListBoxVisible.Items[ i ] + '''';
    end;
  end; // for
  cdsFilter.Filter := s;
  cdsFilter.Filtered := TRUE;
  modalResult := mrOk;
end;

procedure TfrmFilter.FormCreate(Sender: TObject);
begin
  fChanged := FALSE;
end;

procedure TfrmFilter.FilterChanged;
begin
  fChanged := TRUE;
  btnOK.Enabled := TRUE;
end; // procedure TfrmFilter.FilterChanged;

end.
