Binding questions

Hi,

I’m working on some bindings, but I’ve hit a couple of issues.

Issue #1: how do I deal with “regular” structures that aren’t quite GTK+
classes?

Essentially, I have a structure that’s returned from an already wrapped
method, but I need to turn the wrapped structure into an appropriate
Ruby object. I’m sure there’s already examples of this in the existing
code, but I’m not familiar enough with the GTK+ C API to know where this
has already been necessary.

It has a destroy function, but it doesn’t have any of the rest of the
type information necessary to use the normal macros, e.g. there isn’t a
_get_type function.

Can someone please help by either giving an example or pointing me to
the appropriate place in the source?

Issue #2: what’s the best way to wrap a structure of function pointers
so that I can allow them to be implemented by a Ruby object?

Given:

struct foo
{
GtkWidget * (* create_widget) (ContainerWidgetType *widget, …)
};

void widget_set_foo_callbacks(ContainerWidgetType *widget, foo
*callbacks, gpointer data)

I’d like to end up with the following:

class ContainerWidget
def callbacks=(callbacks)

end

class MyCallbacks
def create_widget(…)
end

cw = ContainerWidget.new
cbw = MyCallbacks.new
cw.callbacks = cbw

Is this possible? Has something similar already been done somewhere?

BTW, I figured out the answer to my earlier question when I found how
the sort functions work for the list store. The example is in there.

66 static gint
67 sort_func(model, a, b, func)
68 GtkTreeModel *model;
69 GtkTreeIter *a, *b;
70 gpointer func;
71 {
72 a->user_data3 = model;
73 b->user_data3 = model;
74 return NUM2INT(rb_funcall((VALUE)func, id_call, 2,
GTKTREEITER2RVAL(a),
75 GTKTREEITER2RVAL(b)));
76 }
77
78 static VALUE
79 treesortable_set_sort_func(self, sort_column_id)
80 VALUE self, sort_column_id;
81 {
82 volatile VALUE func = rb_block_proc();
83 G_RELATIVE(self, func);
84 gtk_tree_sortable_set_sort_func(_SELF(self),
NUM2INT(sort_column_id),
85 (GtkTreeIterCompareFunc)sort_func,
86 (gpointer)func, NULL);
87 return self;
88 }
89

Thanks in advance,

ast

Andrew S. Townley [email protected]
http://atownley.org

Hi,

In 1224244386.12922.31.camel@linna
“[ruby-gnome2-devel-en] Binding questions” on Fri, 17 Oct 2008
12:53:06 +0100,
“Andrew S. Townley” [email protected] wrote:

void widget_set_foo_callbacks(ContainerWidgetType *widget, foo *callbacks, gpointer data)
end

cw = ContainerWidget.new
cbw = MyCallbacks.new
cw.callbacks = cbw

Is this possible? Has something similar already been done somewhere?

Yes. You will change id_call to rb_intern(“create_widget”)
in the below example.

73 b->user_data3 = model;
84 gtk_tree_sortable_set_sort_func(_SELF(self), NUM2INT(sort_column_id),
85 (GtkTreeIterCompareFunc)sort_func,
86 (gpointer)func, NULL);
87 return self;
88 }
89

Thanks,

kou