with GNATCOM.IInterface;

with GNATCOM.Errors;

package body MSHTML.IEventTarget_Interface is

   procedure Initialize (This : in out IEventTarget_Type) is
   begin
      Set_IID (This, IID_IEventTarget);
   end Initialize;

   function Pointer (This : IEventTarget_Type)
     return Pointer_To_IEventTarget
   is
   begin
      return To_Pointer_To_IEventTarget (Address (This));
   end Pointer;

   procedure Attach (This    : in out IEventTarget_Type;
                     Pointer : in     Pointer_To_IEventTarget)
   is
   begin
      Attach (This, GNATCOM.IInterface.To_Pointer_To_IUnknown
              (Pointer.all'Address));
   end Attach;

   procedure addEventListener
     (This       : IEventTarget_Type;
      utype      : GNATCOM.Types.BSTR;
      listener   : GNATCOM.Types.Pointer_To_IDispatch;
      useCapture : GNATCOM.Types.VARIANT_BOOL;
      Free       : Boolean := True)
   is
   begin
      GNATCOM.Errors.Error_Check
        (Pointer (This).Vtbl.addEventListener
         (Pointer (This),
          utype,
          listener,
          useCapture));

      if Free then
               GNATCOM.IInterface.Free (utype);
      
      end if;

   end addEventListener;

   procedure removeEventListener
     (This       : IEventTarget_Type;
      utype      : GNATCOM.Types.BSTR;
      listener   : GNATCOM.Types.Pointer_To_IDispatch;
      useCapture : GNATCOM.Types.VARIANT_BOOL;
      Free       : Boolean := True)
   is
   begin
      GNATCOM.Errors.Error_Check
        (Pointer (This).Vtbl.removeEventListener
         (Pointer (This),
          utype,
          listener,
          useCapture));

      if Free then
               GNATCOM.IInterface.Free (utype);
      
      end if;

   end removeEventListener;

   function dispatchEvent
     (This     : IEventTarget_Type;
      evt      : Pointer_To_IDOMEvent)
     return GNATCOM.Types.VARIANT_BOOL
   is
       RetVal : aliased GNATCOM.Types.VARIANT_BOOL;
   begin
      GNATCOM.Errors.Error_Check
        (Pointer (This).Vtbl.dispatchEvent
         (Pointer (This),
          evt,
          RetVal'Unchecked_Access));

      return RetVal;
   end dispatchEvent;

end MSHTML.IEventTarget_Interface;

