# Xml.load\_panel

**URL:** <https://www.ruby-forum.com/t/xml-load-panel/146795>\
**Category:** wxRuby\
**Created:** [August 25, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795 "2008-08-25T15:38:08Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 25, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/1 "2008-08-25T15:38:08Z")

</div>

Hello,  
I cannot correctly load panel from xrc-file. My test programm is below.  
Problem is that panel content is drawn not in place of ‘DYMMYPANEL’, but  
in upper-left corner. Please, help me to find mistake.

#!/usr/bin/ruby -w  
require ‘wx’  
$ui\_dir = File.dirname( **FILE** )  
class MainFrame \< Wx::Frame  
def initialize(parent = nil)  
super  
xrc\_file = File.join $ui\_dir,‘test.xrc’  
xml = Wx::XmlResource.get  
xml.flags = Wx::XRC\_NO\_SUBCLASSING | Wx::XRC\_NO\_RELOADING  
xml.init\_all\_handlers  
xml.load xrc\_file  
xml.load\_frame\_subclass(self,parent,‘MAINFRAME’)  
@finder = lambda do |x|  
id = Wx::xrcid(x)  
Wx::Window.find\_window\_by\_id(id,self)  
end  
@splitter = @finder.call ‘SPLITTER’  
@dummy\_panel = @finder.call ‘DUMMYPANEL’  
@new\_panel = xml.load\_panel self,‘PANEL’  
@splitter.replace\_window @dummy\_panel, @new\_panel  
end  
end  
Wx::App.run do  
MainFrame.new.show  
end

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 25, 2008, 3:52pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/2 "2008-08-25T15:52:29Z")

</div>

Hi

Max S. wrote:

> ```
> super
> @splitter = @finder.call 'SPLITTER'
> @dummy_panel = @finder.call 'DUMMYPANEL'
> @new_panel = xml.load_panel self,'PANEL'
> 
> ```

You’re loading the new panel as a child of ‘self’ - the Frame, when it  
should be a child of @splitter I think?

> ```
> @splitter.replace_window @dummy_panel, @new_panel
> 
> ```

In my experience you probably want to follow this with

@dummy\_panel.destroy

Otherwise you will be left with the old panel hanging around

hth  
alex

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 25, 2008, 4:05pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/3 "2008-08-25T16:05:33Z")

</div>

Alex F. wrote:

> Hi
> 
> You’re loading the new panel as a child of ‘self’ - the Frame, when it  
> should be a child of @splitter I think?
> 
> > ```
> > @splitter.replace_window @dummy_panel, @new_panel
> > 
> > ```
> 
> In my experience you probably want to follow this with
> 
> @dummy\_panel.destroy
> 
> Otherwise you will be left with the old panel hanging around
> 
> hth  
> alex

Hi,  
Thanks for reply.  
Sorry for wrong topic subject  
In such case

@new\_panel = xml.load\_panel @splitter,‘PANEL’  
@splitter.replace\_window @dummy\_panel, @new\_panel  
@dummy\_panel.destroy

‘PANEL’ content(one static text) is rendered in upper-left corner of  
splitter (left pane), while it supposed to be on the right

Max

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 26, 2008, 7:43am UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/4 "2008-08-26T07:43:34Z")

</div>

Alex F. wrote:

> Hard to say without seeing actual code. With any query like this, a  
> minimal and _runnable_ sample that demonstrates the issue is best.
> 
> At a guess, you may need a Sizer within your Panel to ensure that the  
> child text is placed in the right place.
> 
> alex

Complete project (rb-script, wxFormBuilder project, generated xrc and  
screenshot) are attached to post.

‘GC.disable’ is present to avoid random crashes described in  
[http://www.ruby-forum.com/topic/142975](http://www.ruby-forum.com/topic/142975)

OS Windows XP SP2  
ruby 1.8.7 (2008-05-31 patchlevel 0) [i386-mswin32]  
wxRuby 1.9.7 installed from gem

Max

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 26, 2008, 8:43am UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/5 "2008-08-26T08:43:13Z")

</div>

Max S. wrote:

> Complete project (rb-script, wxFormBuilder project, generated xrc and  
> screenshot) are attached to post.

I don’t think this is very hard to fix - you currently have an  
intermediate panel as the right-hand-side child of your upper splitter  
window. I made your DUMMYPANEL the direct second child of the splitter  
window and then it all works fine. You were attaching your replacement  
window in the wrong place.

Generally a garbled layout with an element in the top left means that  
something is being attached to thr wrong parent, or added to the wrong  
sizer, or missing a sizer.add call. I’ve made this mistake a lot of  
times doing wxRuby programming (including earlier today…) and they’re  
always fixed by looking closely at the window hierarchy.

> ‘GC.disable’ is present to avoid random crashes described in  
> [A bug in wxRuby. Segmentation fault in random situations - wxRuby - Ruby-Forum](http://www.ruby-forum.com/topic/142975)

OK, this should be fixed with the upcoming 1.9.8 release. Sorry for the  
inconvenience.

> OS Windows XP SP2  
> ruby 1.8.7 (2008-05-31 patchlevel 0) [i386-mswin32]  
> wxRuby 1.9.7 installed from gem

a

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 26, 2008, 8:51am UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/6 "2008-08-26T08:51:29Z")

</div>

Thank You very mush for help.

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 25, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/7 "2008-08-25T16:14:47Z")

</div>

Max S. wrote:

> Thanks for reply.  
> Sorry for wrong topic subject  
> In such case
> 
> @new\_panel = xml.load\_panel @splitter,‘PANEL’  
> @splitter.replace\_window @dummy\_panel, @new\_panel  
> @dummy\_panel.destroy
> 
> ‘PANEL’ content(one static text) is rendered in upper-left corner of  
> splitter (left pane), while it supposed to be on the right  
> Hard to say without seeing actual code. With any query like this, a  
> minimal and _runnable_ sample that demonstrates the issue is best.

At a guess, you may need a Sizer within your Panel to ensure that the  
child text is placed in the right place.

alex

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 27, 2008, 8:07am UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/8 "2008-08-27T08:07:15Z")

</div>

Alex F. wrote:

> Max S. wrote:
> 
> > If I load panel directly from xrc-file, everything is ok, but if I  
> > create a class
> > 
> > class NewPanel \< Wx::Panel  
> > def initialize parent = nil  
> > super
> 
> Here, you must call super() - with no arguments. Whenever you’re loading  
> an XRC layout into a Frame/Panel/Dialog subclass you must call the  
> default initialize with no arguments, then call load\_xxx\_subclass with  
> the usual arguments.
> 
> > ```
> > $xml.load_panel_subclass(self,parent,'PANEL')
> > 
> > ```
> > 
> > end  
> > end
> 
> cheers  
> aelx

Hm, ‘super()’ call raises error in NewPanel#initalize, while in  
MainFrame#initialize it works perfectly.

c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/keyword\_ctors.rb:180:in  
`pre_wx_kwctor_init': wrong # of arguments(0 for 1) (ArgumentError) from c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/keyword_ctors.rb:180:in `initialize’  
from ./newpanel.rb:3:in `initialize' from test.rb:21:in `new’  
from test.rb:21:in `initialize' from test.rb:35:in `new’  
from test.rb:35:in `on_init' from c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/classes/app.rb:16:in `main\_loop’  
from  
c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/classes/app.rb:16:in  
`run’  
from test.rb:29

And one more question regarding events. What happens to events,  
assosiated to destroyed objects?

my\_button = Wx::Button.new self  
evt\_button my\_button, :my\_button\_click  
my\_button.destroy

Is this code ok, or I have to disconnect event first?

my\_button = Wx::Button.new self  
evt\_button my\_button, :my\_button\_click  
disconnect my\_button, Wx::ID\_ANY, :evt\_button  
my\_button.destroy

Max

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 26, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/9 "2008-08-26T15:21:33Z")

</div>

Hi, Time passes and new questions arise 🙂

If I load panel directly from xrc-file, everything is ok, but if I  
create a class

class NewPanel \< Wx::Panel  
def initialize parent = nil  
super  
$xml.load\_panel\_subclass(self,parent,‘PANEL’)  
end  
end

and load it using

@new\_panel = NewPanel.new @splitter

panel content is not shown correctly, it appears only after resizing  
panel. self.refresh doesn’t help. What can be done? Complete project is  
attached to post.

Max

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 27, 2008, 9:30am UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/10 "2008-08-27T09:30:46Z")

</div>

Hello Max,

On Wed, Aug 27, 2008 at 1:07 AM, Max S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> evt\_button my\_button, :my\_button\_click  
> disconnect my\_button, Wx::ID\_ANY, :evt\_button  
> my\_button.destroy

This part, you don’t have to worry about disconnecting any events, as  
wxRuby  
automatically disconnects any associated events from the object, before  
destruction. Your first example, would work just fine.

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 27, 2008, 11:26am UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/11 "2008-08-27T11:26:48Z")

</div>

Max S. wrote:

> Hm, ‘super()’ call raises error in NewPanel#initalize, while in  
> MainFrame#initialize it works perfectly.
> 
> c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/keyword\_ctors.rb:180:in  
> `pre_wx_kwctor_init': wrong # of arguments(0 for 1) (ArgumentError) from c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/keyword_ctors.rb:180:in `initialize’

Hmm, I get the same error with 1.9.7. I had originally tried with the  
current SVN version, which works fine, although I’m not quite sure  
what’s changed. Perhaps someone with the latest development version  
could confirm that this is fixed there?

> And one more question regarding events. What happens to events,  
> assosiated to destroyed objects?  
> As Mario says, wxRuby, and wxWidgets deal with this internally.

alex

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 27, 2008, 11:49am UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/12 "2008-08-27T11:49:26Z")

</div>

Alex, Mario, thank You for explanations.

Max

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 26, 2008, 3:56pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/13 "2008-08-26T15:56:05Z")

</div>

Max S. wrote:

> If I load panel directly from xrc-file, everything is ok, but if I  
> create a class
> 
> class NewPanel \< Wx::Panel  
> def initialize parent = nil  
> super

Here, you must call super() - with no arguments. Whenever you’re loading  
an XRC layout into a Frame/Panel/Dialog subclass you must call the  
default initialize with no arguments, then call load\_xxx\_subclass with  
the usual arguments.

> ```
> $xml.load_panel_subclass(self,parent,'PANEL')
> 
> ```
> 
> end  
> end

cheers  
aelx

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 29, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/14 "2008-08-29T16:19:14Z")

</div>

Max S. wrote:

> Alex F. wrote:
> 
> > Hmm, I get the same error with 1.9.7. I had originally tried with the  
> > current SVN version, which works fine, although I’m not quite sure  
> > what’s changed.  
> > On going through the ChangeLog, I see this was in fact specifically  
> > fixed for version 1.9.8

> 

Have a look at the code of your XRCPanel. It takes a single-argument,  
parent, but it calls Wx::Panel.new() with no arguments, which is the way  
it’s done for loading from XRC. So you need to call new in XRCPanel with  
a correct parent argument; it will deal with the rest.

> # puts don’t get executed and panel content is not updated at all
> 
> # and is not visible
> 
> class NewPanel \< XRCPanel  
> def initialize parent = nil  
> super()  
> puts ‘Hello, World!’  
> end  
> end

Here you’re telling the Panel it should be attached to +nil+ so it won’t  
display properly, or might even crash.

> #Works fine but ‘on\_init’ is optional  
> class NewPanel \< XRCPanel  
> def on\_init  
> puts “Hello, World!”  
> end  
> end

This is deprecated and will be dropped from future versions of XRCise -  
its use is discouraged.

alex

---

<div class="post-metadata">

**Author:** ![maxmotor](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/cc9497/32.png) [@maxmotor](https://www.ruby-forum.com/u/maxmotor)\
**Post date:** [August 29, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/xml-load-panel/146795/15 "2008-08-29T12:55:04Z")

</div>

Alex F. wrote:

> Max S. wrote:
> 
> > Hm, ‘super()’ call raises error in NewPanel#initalize, while in  
> > MainFrame#initialize it works perfectly.
> > 
> > c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/keyword\_ctors.rb:180:in  
> > `pre_wx_kwctor_init': wrong # of arguments(0 for 1) (ArgumentError) from c:/ruby/lib/ruby/gems/1.8/gems/wxruby-1.9.7-i386-mswin32/lib/wx/keyword_ctors.rb:180:in `initialize’
> 
> Hmm, I get the same error with 1.9.7. I had originally tried with the  
> current SVN version, which works fine, although I’m not quite sure  
> what’s changed. Perhaps someone with the latest development version  
> could confirm that this is fixed there?
> 
> alex

Hello,

I don’t know my new problem is the same, but now I have the following  
XRCPanel is generated from xrc-file by xrcise

#Works fine  
class NewPanel \< XRCPanel  
def initialize parent = nil  
super  
end  
end

# puts don’t get executed and panel content is not updated at all

# and is not visible

class NewPanel \< XRCPanel  
def initialize parent = nil  
super()  
puts ‘Hello, World!’  
end  
end

#Works fine but ‘on\_init’ is optional  
class NewPanel \< XRCPanel  
def on\_init  
puts “Hello, World!”  
end  
end

So, please, what is the correct method?
