Class: WIN32OLE::Event

Inherits:
Object
  • Object
show all
Defined in:
ext/win32ole/win32ole_event.c,
ext/win32ole/win32ole_event.c

Overview

WIN32OLE::Event objects controls OLE event.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(ole, event) ⇒ Object

Returns OLE event object. The first argument specifies WIN32OLE object. The second argument specifies OLE event name.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE::Event.new(ie, 'DWebBrowserEvents')


985
986
987
988
989
990
991
992
993
# File 'ext/win32ole/win32ole_event.c', line 985

static VALUE
fev_initialize(int argc, VALUE *argv, VALUE self)
{
    ev_advise(argc, argv, self);
    evs_push(self);
    rb_ivar_set(self, id_events, rb_ary_new());
    fev_set_handler(self, Qnil);
    return self;
}

Class Method Details

.message_loopObject

Translates and dispatches Windows message.



1011
1012
1013
1014
1015
1016
# File 'ext/win32ole/win32ole_event.c', line 1011

static VALUE
fev_s_msg_loop(VALUE klass)
{
    ole_msg_loop();
    return Qnil;
}

Instance Method Details

#handlerObject

returns handler object.



1254
1255
1256
1257
1258
# File 'ext/win32ole/win32ole_event.c', line 1254

static VALUE
fev_get_handler(VALUE self)
{
    return rb_ivar_get(self, rb_intern("handler"));
}

#handler=Object

sets event handler object. If handler object has onXXX method according to XXX event, then onXXX method is called when XXX event occurs.

If handler object has method_missing and there is no method according to the event, then method_missing called and 1-st argument is event name.

If handler object has onXXX method and there is block defined by on_event('XXX'){}, then block is executed but handler object method is not called when XXX event occurs.

class Handler
  def onStatusTextChange(text)
    puts "StatusTextChanged"
  end
  def onPropertyChange(prop)
    puts "PropertyChanged"
  end
  def method_missing(ev, *arg)
    puts "other event #{ev}"
  end
end

handler = Handler.new
ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE::Event.new(ie)
ev.on_event("StatusTextChange") {|*args|
  puts "this block executed."
  puts "handler.onStatusTextChange method is not called."
}
ev.handler = handler


1241
1242
1243
1244
1245
# File 'ext/win32ole/win32ole_event.c', line 1241

static VALUE
fev_set_handler(VALUE self, VALUE val)
{
    return rb_ivar_set(self, rb_intern("handler"), val);
}

#off_event([event]) ⇒ Object

removes the callback of event.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE::Event.new(ie)
ev.on_event('BeforeNavigate2') {|*args|
  args.last[6] = true
}
# ...
ev.off_event('BeforeNavigate2')
# ...


1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'ext/win32ole/win32ole_event.c', line 1123

static VALUE
fev_off_event(int argc, VALUE *argv, VALUE self)
{
    VALUE event = Qnil;
    VALUE events;

    rb_scan_args(argc, argv, "01", &event);
    if(!NIL_P(event)) {
        if(!RB_TYPE_P(event, T_STRING) && !RB_TYPE_P(event, T_SYMBOL)) {
            rb_raise(rb_eTypeError, "wrong argument type (expected String or Symbol)");
        }
        if (RB_TYPE_P(event, T_SYMBOL)) {
            event = rb_sym2str(event);
        }
    }
    events = rb_ivar_get(self, id_events);
    if (NIL_P(events)) {
        return Qnil;
    }
    ole_delete_event(events, event);
    return Qnil;
}

#on_event([event]) { ... } ⇒ Object

Defines the callback event. If argument is omitted, this method defines the callback of all events. If you want to modify reference argument in callback, return hash in callback. If you want to return value to OLE server as result of callback use ‘return’ or :return.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE::Event.new(ie)
ev.on_event("NavigateComplete") {|url| puts url}
ev.on_event() {|ev, *args| puts "#{ev} fired"}

ev.on_event("BeforeNavigate2") {|*args|
  # ...
  # set true to BeforeNavigate reference argument `Cancel'.
  # Cancel is 7-th argument of BeforeNavigate,
  # so you can use 6 as key of hash instead of 'Cancel'.
  # The argument is counted from 0.
  # The hash key of 0 means first argument.)
  {:Cancel => true}  # or {'Cancel' => true} or {6 => true}
}

ev.on_event(event_name) {|*args|
  {:return => 1, :xxx => yyy}
}

Yields:



1082
1083
1084
1085
1086
# File 'ext/win32ole/win32ole_event.c', line 1082

static VALUE
fev_on_event(int argc, VALUE *argv, VALUE self)
{
    return ev_on_event(argc, argv, self, Qfalse);
}

#on_event_with_outargs([event]) { ... } ⇒ Object

Defines the callback of event. If you want modify argument in callback, you could use this method instead of WIN32OLE::Event#on_event.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE::Event.new(ie)
ev.on_event_with_outargs('BeforeNavigate2') {|*args|
  args.last[6] = true
}

Yields:



1102
1103
1104
1105
1106
# File 'ext/win32ole/win32ole_event.c', line 1102

static VALUE
fev_on_event_with_outargs(int argc, VALUE *argv, VALUE self)
{
    return ev_on_event(argc, argv, self, Qtrue);
}

#unadvisenil

disconnects OLE server. If this method called, then the WIN32OLE::Event object does not receive the OLE server event any more. This method is trial implementation.

ie = WIN32OLE.new('InternetExplorer.Application')
ev = WIN32OLE::Event.new(ie)
ev.on_event() { something }
# ...
ev.unadvise

Returns:

  • (nil)


1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
# File 'ext/win32ole/win32ole_event.c', line 1161

static VALUE
fev_unadvise(VALUE self)
{
    struct oleeventdata *poleev;
    TypedData_Get_Struct(self, struct oleeventdata, &oleevent_datatype, poleev);
    if (poleev->pConnectionPoint) {
        ole_msg_loop();
        evs_delete(poleev->event_id);
        poleev->pConnectionPoint->lpVtbl->Unadvise(poleev->pConnectionPoint, poleev->dwCookie);
        OLE_RELEASE(poleev->pConnectionPoint);
        poleev->pConnectionPoint = NULL;
    }
    OLE_FREE(poleev->pDispatch);
    return Qnil;
}