Example #1
0
/* GdkEvent */
static VALUE
gdkevent_initialize(VALUE self, VALUE type)
{
    GdkEventType event_type;
    VALUE klass;

    event_type = RVAL2GENUM(type, GDK_TYPE_EVENT_TYPE);
    klass = rb_gdk_event_type_to_class(event_type);
    if (!RVAL2CBOOL(rb_obj_is_kind_of(self, klass))) {
        rb_raise(rb_eArgError, "Wrong event type for this class: %s: %s",
                 RBG_INSPECT(type), RBG_INSPECT(klass));
    }

    G_INITIALIZE(self, gdk_event_new(event_type));
    return Qnil;
}
Example #2
0
static GType
rb_gdk_event_to_gtype(VALUE event)
{
    VALUE klass;
    GType type = GDK_TYPE_EVENT;

    klass = rb_obj_class(event);
    if (klass == rb_cGdkEvent) {
        type = GDK_TYPE_EVENT;
    } else if (klass == rb_cGdkEventAny) {
        type = GDK_TYPE_EVENT_ANY;
    } else if (klass == rb_cGdkEventExpose) {
        type = GDK_TYPE_EVENT_EXPOSE;
    } else if (klass == rb_cGdkEventNoExpose) {
        type = GDK_TYPE_EVENT_NO_EXPOSE;
    } else if (klass == rb_cGdkEventVisibility) {
        type = GDK_TYPE_EVENT_VISIBILITY;
    } else if (klass == rb_cGdkEventMotion) {
        type = GDK_TYPE_EVENT_MOTION;
    } else if (klass == rb_cGdkEventButton) {
        type = GDK_TYPE_EVENT_BUTTON;
    } else if (klass == rb_cGdkEventScroll) {
        type = GDK_TYPE_EVENT_SCROLL;
    } else if (klass == rb_cGdkEventKey) {
        type = GDK_TYPE_EVENT_KEY;
    } else if (klass == rb_cGdkEventCrossing) {
        type = GDK_TYPE_EVENT_CROSSING;
    } else if (klass == rb_cGdkEventFocus) {
        type = GDK_TYPE_EVENT_FOCUS;
    } else if (klass == rb_cGdkEventConfigure) {
        type = GDK_TYPE_EVENT_CONFIGURE;
    } else if (klass == rb_cGdkEventProperty) {
        type = GDK_TYPE_EVENT_PROPERTY;
    } else if (klass == rb_cGdkEventSelection) {
        type = GDK_TYPE_EVENT_SELECTION;
    } else if (klass == rb_cGdkEventOwnerChange) {
        type = GDK_TYPE_EVENT_OWNER_CHANGE;
    } else if (klass == rb_cGdkEventProximity) {
        type = GDK_TYPE_EVENT_PROXIMITY;
    } else if (klass == rb_cGdkEventClient) {
        type = GDK_TYPE_EVENT_CLIENT;
    } else if (klass == rb_cGdkEventDND) {
        type = GDK_TYPE_EVENT_DND;
    } else if (klass == rb_cGdkEventWindowState) {
        type = GDK_TYPE_EVENT_WINDOW_STATE;
    } else if (klass == rb_cGdkEventSetting) {
        type = GDK_TYPE_EVENT_SETTING;
    } else if (klass == rb_cGdkEventGrabBroken) {
        type = GDK_TYPE_EVENT_GRAB_BROKEN;
    } else {
        rb_raise(rb_eArgError, "Not event object: %s", RBG_INSPECT(event));
    }

    return type;
}
static VALUE
encode_replace_body (VALUE self, VALUE chunk)
{
    const gchar *packet;
    gsize packet_size, packed_size;

    if (!RVAL2CBOOL(rb_obj_is_kind_of(chunk, rb_cString)))
	rb_raise(rb_eArgError,
		 "chunk should be a String: %s",
		 RBG_INSPECT(chunk));
    milter_reply_encoder_encode_replace_body(SELF(self),
					     &packet, &packet_size,
					     RSTRING_PTR(chunk),
					     RSTRING_LEN(chunk),
					     &packed_size);

    return rb_ary_new3(2,
		       CSTR2RVAL_SIZE(packet, packet_size),
		       UINT2NUM(packed_size));
}
Example #4
0
static VALUE
rg_initialize(int argc, VALUE *argv, VALUE self)
{
    GType gtype;
    VALUE params_hash;
    GObject* gobj;

    gtype = CLASS2GTYPE(CLASS_OF(self));
    if (G_TYPE_IS_ABSTRACT(gtype)) {
        rb_raise(rb_eTypeError,
                 "initializing abstract class: %s",
                 RBG_INSPECT(CLASS_OF(self)));
    }

    rb_scan_args(argc, argv, "01", &params_hash);

    if (!NIL_P(params_hash))
        Check_Type(params_hash, RUBY_T_HASH);

    gobj = rbgobj_gobject_new(RVAL2GTYPE(self), params_hash);

    G_INITIALIZE(self, gobj);
    return Qnil;
}
Example #5
0
void
rbgobj_register_type(VALUE klass, VALUE type_name, GClassInitFunc class_init)
{
    GType parent_type;
    GTypeInfo *info;

    {
        const RGObjClassInfo *cinfo = rbgobj_lookup_class(klass);
        if (cinfo->klass == klass)
            rb_raise(rb_eTypeError,
                     "already registered class: <%s>",
                     RBG_INSPECT(klass));
    }

    {
        VALUE superclass = rb_funcall(klass, rb_intern("superclass"), 0);
        const RGObjClassInfo *cinfo = rbgobj_lookup_class(superclass);
        if (cinfo->klass != superclass)
            rb_raise(rb_eTypeError,
                     "super class must be registered to GLib: <%s>",
                     RBG_INSPECT(superclass));
        parent_type = cinfo->gtype;
    }

    if (NIL_P(type_name)) {
        VALUE klass_name = rb_funcall(klass, rb_intern("name"), 0);

        if (strlen(StringValueCStr(klass_name)) == 0)
            rb_raise(rb_eTypeError,
                     "can't determine type name: <%s>",
                     RBG_INSPECT(klass));

        type_name = rb_funcall(klass_name, rb_intern("gsub"),
                               2,
                               rb_str_new_cstr("::"),
                               rb_str_new_cstr(""));
    }

    {
        GTypeQuery query;
        g_type_query(parent_type, &query);

        /* TODO: Why new?  g_type_register_static() doesn’t retain a copy, so
         * this should be allocated on the stack. */
        info = g_new0(GTypeInfo, 1);
        info->class_size     = query.class_size;
        info->base_init      = NULL;
        info->base_finalize  = NULL;
        info->class_init     = class_init;
        info->class_finalize = NULL;
        info->class_data     = NULL;
        info->instance_size  = query.instance_size;
        info->n_preallocs    = 0;
        info->instance_init  = NULL;
        info->value_table    = NULL;
    }

    {
        GType type = g_type_register_static(parent_type,
                                            StringValueCStr(type_name),
                                            info,
                                            0);

        rbgobj_register_class(klass, type, TRUE, TRUE);

        {
            RGObjClassInfo *cinfo = (RGObjClassInfo *)rbgobj_lookup_class(klass);
            cinfo->flags |= RBGOBJ_DEFINED_BY_RUBY;
        }

        {
            GType parent = g_type_parent(type);
            const RGObjClassInfo *cinfo =  GTYPE2CINFO(parent);
            VALUE initialize_module;

            initialize_module = rb_define_module_under(klass,
                                                       RubyGObjectHookModule);
            if (!(cinfo->flags & RBGOBJ_DEFINED_BY_RUBY)) {
                rbg_define_method(initialize_module,
                                  "initialize", rg_initialize, -1);
            }

            rb_include_module(klass, initialize_module);
        }
    }
}
Example #6
0
static guint
resolve_flags_value(VALUE klass, GFlagsClass *gclass, VALUE flag_or_flags)
{
    guint value = 0;

    switch (TYPE(flag_or_flags)) {
    case RUBY_T_NIL:
        value = 0;
        break;
    case RUBY_T_FIXNUM:
    case RUBY_T_BIGNUM:
        value = NUM2UINT(flag_or_flags);
        break;
    case RUBY_T_STRING:
    case RUBY_T_SYMBOL:
    {
        const gchar *name;
        GFlagsValue *info;

        name = RVAL2CSTR_ACCEPT_SYMBOL(flag_or_flags);
        info = g_flags_get_value_by_name(gclass, name);
        if (!info) {
            gchar *nick;
            nick = rbg_name_to_nick(name);
            info = g_flags_get_value_by_nick(gclass, nick);
            g_free(nick);
        }
        if (!info) {
            rb_raise(rb_eArgError,
                     "unknown flag name: <%s>(%s)",
                     name,
                     g_type_name(G_TYPE_FROM_CLASS(gclass)));
        }
        value = info->value;
        break;
    }
    case RUBY_T_ARRAY:
    {
        int i, n;
        n = RARRAY_LEN(flag_or_flags);
        for (i = 0; i < n; i++) {
            value |= resolve_flags_value(klass,
                                         gclass,
                                         RARRAY_PTR(flag_or_flags)[i]);
        }
        break;
    }
    default:
        if (RVAL2CBOOL(rb_obj_is_kind_of(flag_or_flags, klass))) {
            value = NUM2UINT(rb_funcall(flag_or_flags, id_to_i, 0));
        } else {
            rb_raise(rb_eArgError,
                     "flag value must be one of "
                     "nil, Fixnum, String, Symbol, %s or Array of them: "
                     "<%s>(%s)",
                     RBG_INSPECT(klass),
                     RBG_INSPECT(flag_or_flags),
                     g_type_name(G_TYPE_FROM_CLASS(gclass)));
        }
        break;
    }

    return value;
}