# Problems loading .xrc file

**URL:** <https://www.ruby-forum.com/t/problems-loading-xrc-file/143864>\
**Category:** wxRuby\
**Created:** [July 23, 2008, 11:09am UTC](https://www.ruby-forum.com/t/problems-loading-xrc-file/143864 "2008-07-23T11:09:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![psamathos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4491bb/32.png) [@psamathos](https://www.ruby-forum.com/u/psamathos)\
**Post date:** [July 23, 2008, 11:09am UTC](https://www.ruby-forum.com/t/problems-loading-xrc-file/143864/1 "2008-07-23T11:09:06Z")

</div>

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" ?\> wxDEFAULT\_FRAME\_STYLE|wxTAB\_TRAVERSAL 500,300

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

---

<div class="post-metadata">

**Author:** ![psamathos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4491bb/32.png) [@psamathos](https://www.ruby-forum.com/u/psamathos)\
**Post date:** [July 23, 2008, 11:41am UTC](https://www.ruby-forum.com/t/problems-loading-xrc-file/143864/2 "2008-07-23T11:41:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![psamathos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4491bb/32.png) [@psamathos](https://www.ruby-forum.com/u/psamathos)\
**Post date:** [July 23, 2008, 11:53am UTC](https://www.ruby-forum.com/t/problems-loading-xrc-file/143864/3 "2008-07-23T11:53:39Z")

</div>

Hi,

thanks for the files, it works great and I’ll see what differs yours  
from mine 🙂

//Magnus

---

<div class="post-metadata">

**Author:** ![psamathos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/4491bb/32.png) [@psamathos](https://www.ruby-forum.com/u/psamathos)\
**Post date:** [August 27, 2008, 1:36pm UTC](https://www.ruby-forum.com/t/problems-loading-xrc-file/143864/4 "2008-08-27T13:36:28Z")

</div>

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
