[wxruby-users] embedding wxruby?

Niklas Baumstark niklas.baumstark at googlemail.com
Sun Sep 14 17:15:49 EDT 2008


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). 

    the wx.rb could probably be included via rb_eval_string or something 
    similiar for the sugar functionalities. didn't try it yet, however.
  * 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
URL: <http://rubyforge.org/pipermail/wxruby-users/attachments/20080914/a5142012/attachment.bin>


More information about the wxruby-users mailing list