# Embedding wxruby?

**URL:** <https://www.ruby-forum.com/t/embedding-wxruby/148434>\
**Category:** wxRuby\
**Created:** [September 12, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434 "2008-09-12T19:55:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 12, 2008, 7:55pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/1 "2008-09-12T19:55:46Z")

</div>

Hello,

is it possible to code a wxWidgets GUI in C++ and pass the whole wxApp  
or just single windows to embedded Ruby code? This would be very nice to  
provide a scriptable plugin system.

Greetings,  
Niklas

---

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 13, 2008, 2:15am UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/2 "2008-09-13T02:15:17Z")

</div>

Is it possible? Yes. How can you do it? I haven’t a clue. Basically,  
you  
would need to wrap the widgets you want available on the Ruby Side,  
through  
the wxRuby controls, using the Swigified Wrappers to create the objects,  
and  
so forth.

Sorry, it’s not been a very much explored system, and I wouldn’t have a  
clue  
in how to do it.

On Fri, Sep 12, 2008 at 12:25 PM, Niklas B. \<

---

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 13, 2008, 12:44pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/3 "2008-09-13T12:44:51Z")

</div>

Hello,

“Mario S.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Is it possible? Yes. How can you do it? I haven’t a clue. Basically, you  
> would need to wrap the widgets you want available on the Ruby Side, through  
> the wxRuby controls, using the Swigified Wrappers to create the objects, and  
> so forth.
> 
> Sorry, it’s not been a very much explored system, and I wouldn’t have a clue  
> in how to do it.

OK, thanks for you answer. Maybe I’ll dive a bit into it to try.

Greetings,  
Niklas

---

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 14, 2008, 11:18pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/4 "2008-09-14T23:18:04Z")

</div>

Hello,

as announced, I played a bit around with swig and now seem to have found  
a first solution for my embedded ruby GUI plugin system. So far, I  
managed to wrap a C++ wxWindow object into a ruby Wx::Window object. I  
had to do a change to the wxRuby source to add a “wxWindow\_to\_ruby”  
function to the SWIG interface file for wxWindow (Window.i).  
Below is the (necessary?) code change and some example code based on the  
minimal.cpp, where i abused the about dialog to test my code.

===== SAMPLE START =====

# ========================================================= Append to the bottom of “wxruby2/swig/classes/Window.i”:

%{

VALUE wxWindow\_to\_ruby(wxWindow\* w) {  
return SWIG\_NewPointerObj(  
(void \*) w,  
SWIGTYPE\_p\_wxWindow,  
0);  
}

%}

# ============================================= My sample app (it’s a modified minimal.cpp):

…  
#include \<ruby.h\>

extern “C” void Init\_wxruby2();  
extern “C” void Init\_wxWindow();

extern VALUE wxWindow\_to\_ruby(wxWindow\* w);

…

void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))  
{  
ruby\_init();

```
ruby_init_loadpath();
// the ruby file is in a sub-directory
rb_eval_string(" $: << 'src' ");
ruby_script("embed");

Init_wxruby2();
Init_wxWindow();

VALUE rubyWindow = wxWindow_to_ruby(this);
cout << "Window in C++: " << rubyWindow << endl;
rb_define_variable("$window", &rubyWindow);

rb_require("ruby_test.rb");

ruby_finalize();

```

}

# ================== the ruby\_test.rb:

if $0 != “embed”  
puts “WARNING: this script is supposed to be run only from ‘embed’”  
exit  
end

puts "Window in ruby: " + $window.inspect  
puts "Name of the window: " + $window.get\_name

# ================== the output

Window in C++: 3019414920  
Window in ruby: #Wxruby2::Window:0xb3f89d88  
Name of the window: frame

===== SAMPLE END =====

the linker has to be told to link to wxruby2.so, wxwidgets and ruby, of  
course.

i share this because it may be of some use for someone.  
i think this seems fine for a first attempt, but

- it is limited to be used with a wxWindow / Wx::Window and not at all  
generic.

- the ruby script doesn’t know about wxRuby. It just gets the raw  
Wx::Window object. it can’t use $window.name instead of  
$window.get\_name, for instance, and it can’t access it’s non-trivial  
members, because the corresponding types are not initialized.  
i didn’t figure out a way to setup the whole wxRuby namespace in one  
rush  
(i would have to call every single Init\_XY).

- less important: the wxruby2.so has a size of 40MB. is it statically  
linked against wxWidgets? If so, can this easily be turned off?

It would be great if someone has an idea on how to solve (some of) these  
problems.

Greetings,  
Niklas

---

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 15, 2008, 1:17pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/5 "2008-09-15T13:17:54Z")

</div>

Hello,

Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You will in some way need to link the wx C++ App object to a ruby  
> instance and make it a constant (like Wx::THE\_APP in wxRuby). Having the  
> App as a constant is the link that wxRuby uses to protect long-lived C++  
> objects like Windows from Ruby’s garbage collection (see mark\_wxApp).

OK, this is more sophisticated than i need it. i just need to wrap a  
wxPanel, because the main interface is managed by C++.

> Take a look at the function wxRuby\_WrapWxObjectInRuby in swig/wx.i -  
> this does exactly what you want. It uses wxWidgets type-information  
> system to wrap a C++ object in the correct ruby class.  
> It is applied to (almost) all methods in wxWidgets that return wxWindow\*  
> by a typemap.

oh, very nice, this function is what i was searching for 🙂 by mario’s  
answer i guessed it would not exist so i tried to write one by myself.

> The rake build system (in rake/rakewx.rb) appends a function called  
> InitializeOtherModules to the generated file src/wx.cpp that initialises  
> all the other modules, in alphabetic order. This is then called in  
> Init\_wxruby (in swig/wx.i).

OK good to know.

> The other important thing is that each class should not be initialised  
> before its parent class. The script swig/fixmodule.rb adds a line to  
> each generated cpp file that calls Init\_wx[PARENTCLASS] before  
> proceeding with initialising self.

i discovered this already.

> > ```
> > the wx.rb could probably be included via rb_eval_string or something 
> > similiar for the sugar functionalities. didn't try it yet, however.
> > 
> > ```

> The ruby API function you want is rb\_require. Some of the things in the  
> ruby code are not just “sugar” but needed to make the classes work  
> properly - eg GC helpers.

another one good to know.

> available, either have wx-config point to it by default, or pass  
> WXRUBY\_DYNAMIC=1 as an environment variable to rake.

I’ll try this. thank you very much!

> Happy to help further but may be better to follow-up on the wxruby-dev  
> list as this is fairly technical…

I would like to, but i can’t subscribe to it properly. It says 403 when  
i want to confirm the subscription ☹ at least it did yesterday.

Greetings,  
Niklas

---

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 15, 2008, 1:03pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/6 "2008-09-15T13:03:12Z")

</div>

Hi Niklas

Niklas B. wrote:

> as announced, I played a bit around with swig and now seem to have found a first solution for my embedded ruby GUI plugin system. So far, I managed to wrap a C++ wxWindow object into a ruby Wx::Window object.

Broadly, I think you should be able to use almost all of the existing  
wxRuby SWIG etc codebase in an embedded setting. The main things that  
will need to change are the library initialisation code (in swig/wx.i)  
and the Wx::App initialisation code (in swig/classes/App.i) .

You will in some way need to link the wx C++ App object to a ruby  
instance and make it a constant (like Wx::THE\_APP in wxRuby). Having the  
App as a constant is the link that wxRuby uses to protect long-lived C++  
objects like Windows from Ruby’s garbage collection (see mark\_wxApp).

> ===== SAMPLE START =====  
> …

> - it is limited to be used with a wxWindow / Wx::Window and not at all generic.

Take a look at the function wxRuby\_WrapWxObjectInRuby in swig/wx.i -  
this does exactly what you want. It uses wxWidgets type-information  
system to wrap a C++ object in the correct ruby class.

It is applied to (almost) all methods in wxWidgets that return wxWindow\*  
by a typemap.

> - the ruby script doesn’t know about wxRuby. It just gets the raw  
> Wx::Window object. it can’t use $window.name instead of  
> $window.get\_name, for instance, and it can’t access it’s non-trivial  
> members, because the corresponding types are not initialized.  
> i didn’t figure out a way to setup the whole wxRuby namespace in one rush  
> (i would have to call every single Init\_XY).

SWIG’s support for multi-module libraries is weak so wxRuby has to work  
around this by massaging the default generated code.

The rake build system (in rake/rakewx.rb) appends a function called  
InitializeOtherModules to the generated file src/wx.cpp that initialises  
all the other modules, in alphabetic order. This is then called in  
Init\_wxruby (in swig/wx.i).

The other important thing is that each class should not be initialised  
before its parent class. The script swig/fixmodule.rb adds a line to  
each generated cpp file that calls Init\_wx[PARENTCLASS] before  
proceeding with initialising self.

> ```
> the wx.rb could probably be included via rb_eval_string or something 
> similiar for the sugar functionalities. didn't try it yet, however.
> 
> ```

The ruby API function you want is rb\_require. Some of the things in the  
ruby code are not just “sugar” but needed to make the classes work  
properly - eg GC helpers.

> - less important: the wxruby2.so has a size of 40MB. is it statically  
> linked against wxWidgets? If so, can this easily be turned off?

You could try strip -x to reduce the size. But SWIG adds a lot of  
overhead - the same runtime code is repeated and compiled for each SWIG  
class. I estimate that just getting round that would reduce the library  
size by about a third.

On Linux and OS X at least - haven’t tried with Windows - you can have a  
wxRuby that is dynamically linked. If you have a dynamic build  
available, either have wx-config point to it by default, or pass  
WXRUBY\_DYNAMIC=1 as an environment variable to rake.

Happy to help further but may be better to follow-up on the wxruby-dev  
list as this is fairly technical…

cheers  
alex

---

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 15, 2008, 1:42pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/7 "2008-09-15T13:42:41Z")

</div>

Niklas B. wrote:

> > You will in some way need to link the wx C++ App object to a ruby  
> > instance and make it a constant (like Wx::THE\_APP in wxRuby). Having the  
> > App as a constant is the link that wxRuby uses to protect long-lived C++  
> > objects like Windows from Ruby’s garbage collection (see mark\_wxApp).
> 
> OK, this is more sophisticated than i need it. i just need to wrap a wxPanel, because the main interface is managed by C++.

OK, but to avoid leaks, or crashes from prematurely deleted objects, you  
will need some sort of memory management strategy. It gets quite  
complicated but most of the work is done 😉

> > Happy to help further but may be better to follow-up on the wxruby-dev  
> > list as this is fairly technical…
> 
> I would like to, but i can’t subscribe to it properly. It says 403 when i want to confirm the subscription ☹ at least it did yesterday.

Just tried it and it seems to be working. If clicking on the link from  
the email make sure the URL isn’t truncated as it contains an  
authentication string.

alex

---

<div class="post-metadata">

**Author:** ![requiem](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@requiem](https://www.ruby-forum.com/u/requiem)\
**Post date:** [September 15, 2008, 2:13pm UTC](https://www.ruby-forum.com/t/embedding-wxruby/148434/8 "2008-09-15T14:13:55Z")

</div>

Hello,

Alex F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> OK, but to avoid leaks, or crashes from prematurely deleted objects, you  
> will need some sort of memory management strategy. It gets quite  
> complicated but most of the work is done 😉

I’ll try to. thank you for the information.

> > > Happy to help further but may be better to follow-up on the wxruby-dev  
> > > list as this is fairly technical…
> > 
> > I would like to, but i can’t subscribe to it properly. It says 403 when i want to confirm the subscription ☹ at least it did yesterday.
> 
> Just tried it and it seems to be working. If clicking on the link from  
> the email make sure the URL isn’t truncated as it contains an  
> authentication string.

Yeah it’s working now. I will ask future questions on this subjects on  
the dev mailing list.

Greetings,  
Niklas
