# Binding questions

**URL:** <https://www.ruby-forum.com/t/binding-questions/151162>\
**Category:** Ruby-Gnome 2\
**Created:** [October 17, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/binding-questions/151162 "2008-10-17T15:40:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Andrew\_SSTownley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Andrew\_SSTownley](https://www.ruby-forum.com/u/Andrew_SSTownley)\
**Post date:** [October 17, 2008, 3:40pm UTC](https://www.ruby-forum.com/t/binding-questions/151162/1 "2008-10-17T15:40:14Z")

</div>

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 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
[http://atownley.org](http://atownley.org)

---

<div class="post-metadata">

**Author:** ![Andrew\_SSTownley](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/d07c76/32.png) [@Andrew\_SSTownley](https://www.ruby-forum.com/u/Andrew_SSTownley)\
**Post date:** [October 18, 2008, 7:42am UTC](https://www.ruby-forum.com/t/binding-questions/151162/2 "2008-10-18T07:42:04Z")

</div>

Hi,

In [1224244386.12922.31.camel@linna](mailto:1224244386.12922.31.camel@linna)  
“[ruby-gnome2-devel-en] Binding questions” on Fri, 17 Oct 2008  
12:53:06 +0100,  
“Andrew S. Townley” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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
