Ruby Forum wxRuby > Problems loading .xrc file

Posted by Magnus Sjöstrand (psamathos)
on 23.07.2008 11:09
Hi,

I'm trying to load a gui with a xrcise/.xrc combo. I've tried both with
this trivial .xrc - which is just a frame with nothing in it - and a few
more advanced which worked great when compiled to c++.

I've tried both win xp sp2 and vista, ruby 1.8.6 from the one-click
installer and 1.8.7, wxRuby (1.9.7) and wx_sugar (0.1.20) installed with
gems.

C:\Users\magnuss\workspace\TestProject>ruby main.rb
10:55:50: Error: Cannot load resources from file
'file:/C%3A/Users/magnuss/workspace/TestProject/noname.xrc'.
c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-x86-mswin32/lib/wx/classes/xmlresource.rb:31:in
`load': Failed to load XRC from 'noname.xrc'; check the file exists
and is valid XML (RuntimeError)
        from ./forms.rb:16:in `initialize'
        from main.rb:10:in `new'
        from main.rb:10

The .xrc file is in the same directory as my main.rb, and does indeed
exist :)

Any ideas or tips would be greatly appreciated :)

best regards,
Magnus Sjöstrand

=====================================================
main.rb

require "wx"
require "forms"

include Wx

class MainWindow < MainWindowBase

end

MainWindow.new.main_loop
=====================================================
noname.xrc

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<resource xmlns="http://www.wxwindows.org/wxxrc" version="2.3.0.1">
  <object class="wxFrame" name="MainFrame" subclass="MainWindowBase">
    <style>wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL</style>
    <size>500,300</size>
    <title></title>
  </object>
</resource>
=====================================================
forms.rb

class MainWindowBase < Wx::Frame

  def initialize(parent = nil)
    super()
    xml = Wx::XmlResource.get
    xml.flags = 2 # Wx::XRC_NO_SUBCLASSING
    xml.init_all_handlers
    xml.load("noname.xrc")
    xml.load_frame_subclass(self, parent, "MainFrame")

    finder = lambda do | x |
      int_id = Wx::xrcid(x)
      begin
        Wx::Window.find_window_by_id(int_id, self) || int_id
      # Temporary hack to work around regression in 1.9.2; remove
      # begin/rescue clause in later versions
      rescue RuntimeError
        int_id
      end
    end

    if self.class.method_defined? "on_init"
      self.on_init()
    end
  end
end
Posted by Magnus Engström (Guest)
on 23.07.2008 11:41
Attachment: wxtemplate.zip (4,3 KB)
(Received via mailing list)
Hi Magnus,

I'm using XRC on both Windows and Linux, and I'm usually building
applications the same way, so I wrote a template to have a quick
starting point. It should work fine on Windows too I think.
Make sure to start it from the right directory though (double-clicking
on wxtemplate should work), currently it looks for ui/ui.xrc relative to
starting directory.

/Magnus
Posted by Magnus Sjöstrand (psamathos)
on 23.07.2008 11:53
Hi,

thanks for the files, it works great and I'll see what differs yours 
from mine :)

//Magnus
Posted by Max Salov (amx)
on 27.08.2008 13:36
Magnus Engström wrote:
> Hi Magnus,
> 
> I'm using XRC on both Windows and Linux, and I'm usually building
> applications the same way, so I wrote a template to have a quick
> starting point. It should work fine on Windows too I think.
> Make sure to start it from the right directory though (double-clicking
> on wxtemplate should work), currently it looks for ui/ui.xrc relative to
> starting directory.
> 
> /Magnus

Hi Magnus,
I've found Your template very helpful for me. I have few ideas that can 
be used in wxhelper::map_events

def map_events parent
  parent.instance_variables.each do |variable|
    object = parent.instance_variable_get(variable)
    case object
      when Wx::Button
        method = variable.gsub(/^@(.+)$/,'\1_click').to_sym
        parent.evt_button object, method if parent.respond_to? method
# and so on
    end
  end
end

Changes are:
1. find controls not by name, but by class
2. associate event handler only if it is present
I hope idea is clear.

Max