XRC question: how to receive wxMenu non-top object?

Greetings list,

Vaclav or anybody,

could you please explain me why in the following code:
==>
res = wxXmlResourse("menu.xrc")
self.menuBar = res.LoadMenuBar("MainMenuBar")
self.fileMenu = XRCCTRL(self.menuBar, "FileMenu")
<==

I receive a created menubar in self.menuBar (that's ok), but I receive
None if self.fileMenu?

I know, of course, that I can receive the file menu via

==>
self.fileMenu = self.menuBar.GetMenu(self.menuBar.FindMenu("File"))
<==

but it looks a little bit odd. And Vaclav - <object_ref ...> didn't work
either (I've tried to use it with
==>
self.fileMenu = res.LoadMenu("MyFileMenu")
<==
but it says "XRC resource 'MyFileMenu' (class 'wxMenu') not found!"

What am I doing wrong?

Here is my menu.xrc:
==>
<?xml version="1.0" ?>
<resource>
  <object class="wxMenuBar" name="MainMenubar">
    <object class="wxMenu" name="FileMenu">
      <label>&amp;File</label>
      <object class="wxMenuItem" name="NewFileMenu">
        <label>&amp;New</label>
        <accel>Ctrl-N</accel>
        <help>New XRC file</help>
      </object>
[skipped...]
    </object>
  </object>
    <object_ref name="MyFileMenu" ref="FileMenu"/>
</resource>
<==

My second question: is there any way to set window id for MenuItem
inside a .xrc file (instead of writing ugly code such as:
==>
FileMenuItem = <code for receiving 'New file' menu item - it is not
obvious for the above reasons ;)>
FileMenuItem.SetId(wxNEW_ID)
<==

Thanks a lot,

--Victor.

Hi,

Krjukov Victor wrote:

I receive a created menubar in self.menuBar (that's ok), but I
receive None if self.fileMenu?

Such code would not even compile in C++, as I just found out, because
wxMenuBar doesn't have FindWindow method which is needed by
XRCCTRL(). You could run FindWindow (or XRCCTRL) on the parent frame,
but even that doesn't return the menu (probably because it is not
wxWindow).

but it looks a little bit odd. And Vaclav - <object_ref ...> didn't
work either (I've tried to use it with
==>
self.fileMenu = res.LoadMenu("MyFileMenu")
<==
but it says "XRC resource 'MyFileMenu' (class 'wxMenu') not found!"

What am I doing wrong?

Nothing -- there was a bug (omission, really) in XRC library, it
didn't load object_refs properly when the node didn't explicitly
specify "class" property. To explain it better, this didn't work:
  <object_ref name="foo" ref="bar"/>
  <object name="bar" class="wxDialog">
  ...
while this did:
  <object_ref name="foo" ref="bar" class="wxDialog"/>
  <object name="bar" class="wxDialog">
  ...
I have fixed it in CVS now. If you don't want to update wxWindows to
post-2.3.4 version, you can workaround the bug by specifying "class"
in <object_ref>.

Thanks for finding this bug!

My second question: is there any way to set window id for MenuItem
inside a .xrc file (instead of writing ugly code such as:

Yes, by setting the name property. In your example you set the name to
"FileMenu" and the ID is assigned automatically to value returned by
calling XRCID("FileMenu"). Furthermore, if the name matches one of
wxWindows' predefined IDs, it is assigned as-is (e.g. when you have
name="wxID_NEW", the ID will be wxID_NEW).

Regards,
Vaclav

- --
PGP key ID: 0x465264C9 (get it from wwwkeys.pgp.net)