unit fmMain;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, uDispatcher, uMHandlers, uMessageConsts;

type
  TForm1 = class(TForm)
    cbValues: TComboBox;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    Memo1: TMemo;
    Memo2: TMemo;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    cbStrings: TComboBox;
    btnValueDispatcher: TBitBtn;
    btnVSetHandlers: TBitBtn;
    btnSSetHandlers: TBitBtn;
    Edit1: TEdit;
    procedure btnVSetHandlersClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure btnValueDispatcherClick(Sender: TObject);
    procedure btnSSetHandlersClick(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    bVHandlers : Boolean;
    bSHandlers : Boolean;

    Dispatcher : TDispatcher;
    Handler : TMHandler;
  public
    { Public declarations }
    procedure AppMsg(const msg : String);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AppMsg(const msg: String);
begin
  Self.Memo1.Lines.Add(msg)
end;

procedure TForm1.btnVSetHandlersClick(Sender: TObject);
begin
  Dispatcher.AddHandler(MY_MESSAGE1, Handler.DelphiHandler);
  Dispatcher.AddHandler(MY_MESSAGE2, Handler.CBuilderHandler);
  Dispatcher.AddHandler(MY_MESSAGE3, Handler.CSharpHandler);
  Dispatcher.AddHandler(MY_MESSAGE4, Handler.DelphiDotNetHandler);
  bVHandlers := True;
  Self.btnVSetHandlers.Enabled := False;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Dispatcher := TDispatcher.Create;
  Handler := TMHandler.Create;
  bVHandlers := False;
  bSHandlers := False;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FreeAndNil(Dispatcher);
  FreeAndNil(Handler);
end;

procedure TForm1.btnValueDispatcherClick(Sender: TObject);
var
  aMessage : TMessage;
begin
  if (bVHandlers) then
  begin
    aMessage.Msg := MY_BASE + Self.cbValues.ItemIndex + 1;
    aMessage.WParam := StrToInt(Self.Edit1.Text);
    Dispatcher.SendMessage(aMessage);
  end;
end;

procedure TForm1.btnSSetHandlersClick(Sender: TObject);
begin
  Dispatcher.AddSHandler(AUDIHandler, Handler.AUDIHandler);
  Dispatcher.AddSHandler(BMWHandler, Handler.BMWHandler);
  Dispatcher.AddSHandler(BenzBuilderHandler, Handler.BenzHandler);
  Dispatcher.AddSHandler(LexusHanlder, Handler.LexusHandler);
  bSHandlers := True;
  Self.btnSSetHandlers.Enabled := False;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
  aMessage : TMessage;
begin
  if (bSHandlers) then
  begin
    aMessage.Msg := Dispatcher.HashOf(Self.cbStrings.Text) mod 100;
    aMessage.WParam := StrToInt(Self.Edit1.Text);
    Dispatcher.SendMessage2(aMessage);
  end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  Self.cbValues.ItemIndex := 0;
  Self.cbStrings.ItemIndex := 0;
end;

end.
