{*******************************************************}
{                                                       }
{       nrCommLib Communication Library for Delphi      }
{       Combo Box Control with Device List (VCL)        }
{       Copyright (c) 1997-2011 Roman Novgorodov        }
{       v9.27                                           }
{       last update: 12 Sep 2010                        }
{                                                       }
{       e-mail:noro@deepsoftware.com                    }
{       DeepSoftware                                    }
{       http://www.deepsoftware.com/                    }
{                                                       }
{*******************************************************}
{$I nrcommlib.inc}
unit nrcommbox;
interface                
uses  nrclasses,
     Windows,Messages,stdctrls,nrwin32,
     SysUtils, Classes;

Type
  TnrDeviceBox=class(TCustomComboBox)
  private
    fComm: TnrBaseIO;
    fReset: boolean;
    procedure SetComm(const Value: TnrBaseIO);
    function GetComm: TnrBaseIO;
    function GetReset: boolean;
    procedure SetReset(const Value: boolean);
  protected
    procedure Change; override;
    procedure HandleNotify(Sender:TObject;Code:integer);
  public
    constructor Create(AOwner:TComponent); override;
    destructor Destroy; override;
    procedure Reload;
    procedure Notification(AComponent: TComponent; Operation: TOperation); override;
    procedure WMDeviceChange(var Msg:TMessage); virtual; //message WM_DEVICECHANGE;

//    procedure WMDeviceChange(var Msg:TMessage); message WM_DEVICECHANGE;
  published
    property nrComm:TnrBaseIO read GetComm write SetComm;
    property ResetOnChanged:boolean read GetReset write SetReset;
    property Color;
    property DropDownCount;
    property Enabled;
    property Font;
    property ItemHeight;
    property ParentColor;

    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ShowHint;
    property Sorted;
    property TabOrder;
    property TabStop;
    property Text;
    property Visible;
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property ImeMode;
    property ImeName;
    property Ctl3D;
    property ParentCtl3D;
    {$IFNDEF GEN3}            {if not Delphi3 and not CBuilder3}
    property Anchors;
    property Constraints;
    property BiDiMode;
    property ParentBiDiMode;
    property DragCursor;
    property DragKind;
    property OnEndDock;
    property OnStartDock;
    property DragMode;
    property OnDragDrop;
    property OnDragOver;
    property OnDrawItem;
    property OnDropDown;
    property OnEndDrag;
    property OnMeasureItem;
    property OnStartDrag;
    {$IFNDEF GEN4}          {if not Delphi4 and not CBuilder4}
    property OnContextPopup;
    {$ENDIF}
    {$ENDIF}
  end;

implementation

{ TnrDeviceBox }

constructor TnrDeviceBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Style := csDropDownList;
end;

destructor TnrDeviceBox.Destroy;
begin
  if Assigned(fComm) then fComm.NotifyDelete(HandleNotify);
  inherited Destroy;
end;

procedure TnrDeviceBox.SetComm(const Value: TnrBaseIO);
begin
  Items.Clear;
  ItemIndex := -1;
  if Assigned(Value) then begin
    Value.GetDeviceList(Items);
    ItemIndex := Value.DeviceIndex;
    Value.NotifyAdd( HandleNotify );
    Value.FreeNotification(self);
  end else
    if Assigned(fComm) then fComm.NotifyDelete(HandleNotify);
  fComm := Value;
end;

procedure TnrDeviceBox.Change;
begin
  if not Assigned(fComm) then exit;
  fComm.DeviceIndex:=ItemIndex;
  inherited Change;
end;

procedure TnrDeviceBox.WMDeviceChange(var Msg: TMessage);
begin
  nrComm.WMDeviceChange(Msg);
end;

procedure TnrDeviceBox.Reload;
begin
  SetComm(fComm);
end;

procedure TnrDeviceBox.Notification(AComponent: TComponent;
  Operation: TOperation);
begin
  inherited Notification(AComponent, Operation);
  if Operation=opRemove then
    if AComponent=fComm then begin
       fComm:= nil;
       ItemIndex:=-1;
    end;
end;

procedure TnrDeviceBox.HandleNotify(Sender: TObject; Code: integer);
begin
  if Code = csDeviceIdChanged then Reload;
end;

function TnrDeviceBox.GetComm: TnrBaseIO;
begin
  Result := fComm;
end;

function TnrDeviceBox.GetReset: boolean;
begin
  Result := fReset;
end;

procedure TnrDeviceBox.SetReset(const Value: boolean);
begin
  fReset := Value;
end;

end.
