{********************************************************************************************************}
{                                                                                                        }
{ Carbonsoft cxCpu Processor Detection Toolkit Release 4                                                 }
{                                                                                                        }
{ Feature Set Demo (Clx)                                                                                 }
{                                                                                                        }
{ Copyright © 1995-2003 Kev French, Carbonsoft. All rights reserved.                                     }
{ http://www.carbonsoft.com/cxcpu/                                                                       }
{                                                                                                        }
{ IMPORTANT INFORMATION                                                                                  }
{ You (individual or organisation) are free to use this utility and any example source code              }
{ provided by Carbonsoft for any purpose.                                                                }
{                                                                                                        }
{ Use of this utility and any example source code is entirely at your own risk.                          }
{                                                                                                        }
{ For more information please contact licensing@carbonsoft.com                                           }
{                                                                                                        }
{********************************************************************************************************}
unit FeaturesWindow;

interface

uses
  SysUtils, Types, Classes, Variants, QTypes, QGraphics, QControls, QForms, 
  QDialogs, QStdCtrls, QExtCtrls, QImgList, QComCtrls, cxCpu40;

type
  IListviewUpdate = interface
    ['{8A13CA39-D726-46DA-9265-B39C97119515}']
  end;
  TListviewUpdate = class(TInterfacedObject, IListviewUpdate)
  private
    fCursor: TCursor;
    fListView: TListView;
  public
    constructor Create(const AListView: TListView; Clear: Boolean);
    destructor Destroy; override;
  end;

  TFeaturesDialog = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    ilAvailable: TImageList;
    Label3: TLabel;
    lVersion: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Image1: TImage;
    Button1: TButton;
    Panel1: TPanel;
    Label1: TLabel;
    cbActiveSet: TComboBox;
    lPrompt: TLabel;
    lvStandard: TListView;
    procedure FormCreate(Sender: TObject);
    procedure cbActiveSetChange(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }

    procedure AddItem(const AListview: TListview; Feature: ICpuFeature);
    procedure AddFeatureSet(const AListView: TListView; AFeatureSet: TFeatureSet);
  public
    { Public declarations }
  end;

var
  FeaturesDialog: TFeaturesDialog;

resourcestring
  RsFeatureSet_Available    = 'Available Features (%d):';
  RsFeatureSet_Unavailable  = 'Feature Set Unavailable.';
  
implementation

{$R *.xfm}

{ TListviewUpdate }

constructor TListviewUpdate.Create(const AListView: TListView;
  Clear: Boolean);
begin
  inherited Create;

  fCursor := Screen.Cursor;
  fListview := AListView;

  Screen.Cursor := crHourglass;
  fListView.Items.BeginUpdate;

  if Clear then
    fListView.Items.Clear;
end;

destructor TListviewUpdate.Destroy;
begin
  fListView.Items.EndUpdate;
  Screen.Cursor := fCursor;
  inherited;
end;

{ TForm1 }

procedure TFeaturesDialog.AddFeatureSet(const AListView: TListView;
  AFeatureSet: TFeatureSet);
var
  Update: IListviewUpdate;
  i: Integer;
  FeatureSet: ICpuFeatureSet;
begin
  Update := TListviewUpdate.Create(AListView, True);
  FeatureSet := TCpuFeatureSet.Create(0, AFeatureSet);
  if FeatureSet.Available.AsBoolean then
  begin
    lPrompt.Caption := Format(RsFeatureSet_Available, [FeatureSet.Count.AsNumber]);
    for i := 0 to 31 do
      if FeatureSet.ByIndex(i).Mnemonic <> RsCpu_Feature_ResMnemonic then
        AddItem(AListView, FeatureSet.ByIndex(i));
  end
  else
    lPrompt.Caption := RsFeatureSet_Unavailable;
end;

procedure TFeaturesDialog.AddItem(const AListview: TListview; Feature: ICpuFeature);
begin
  with AListView.Items.Add do
  begin
    Caption := '';
    SubItems.Add(IntToStr(Feature.BitIndex));
    SubItems.Add(Feature.Mnemonic);
    SubItems.Add(Feature.Name);
    if Feature.Available.AsBoolean then
      ImageIndex := 1
    else
      ImageIndex := 0;
  end;
end;

procedure TFeaturesDialog.FormCreate(Sender: TObject);
begin
  lVersion.Caption := 'Release ' + cxCpu.Version.FormatVersion;
  PageControl1.ActivePageIndex := 0;
  AddFeatureSet(lvStandard, TFeatureSet(cbActiveSet.ItemIndex + 1));

end;

procedure TFeaturesDialog.cbActiveSetChange(Sender: TObject);
begin
  AddFeatureSet(lvStandard, TFeatureSet(cbActiveSet.ItemIndex + 1));
end;

procedure TFeaturesDialog.Button1Click(Sender: TObject);
begin
  Close;
end;

end.
