Author Topic: wxWidgets XML resource files  (Read 6132 times)

sethjackson

  • Guest
wxWidgets XML resource files
« on: September 29, 2005, 10:10:00 pm »
I'm trying to load a menu from an .xrc file. Unfortuately I keep getting errors.

main.cpp
Code: [Select]
#include <wx/xrc/xmlres.h>
#include "main.h"

int idFileQuit = XRCID("idFileQuit");
int idHelpAbout = XRCID("idHelpAbout");

BEGIN_EVENT_TABLE(MainFrame, wxFrame)

EVT_MENU(idFileQuit, MainFrame::OnQuit)

EVT_MENU(idHelpAbout, MainFrame::OnAbout)

END_EVENT_TABLE()

MainFrame::MainFrame(wxFrame *frame, const wxString& title,
                     int xpos, int ypos, int width, int height)
: wxFrame(frame, -1, title, wxPoint(xpos, ypos), wxSize(width, height))
{
wxXmlResource::Get()->InitAllHandlers();

wxXmlResource::Get()->Load(_T("/resources.zip#zip*.xrc"));

CreateMenubar();
}

MainFrame::~MainFrame()
{

}

bool MainFrame::CreateMenubar()
{
wxMenuBar* mbar = 0L;

wxXmlResource *myres = wxXmlResource::Get();

myres->Load(_T("/resources.zip#zip:main_menu.xrc"));

myres->LoadMenuBar(0L, _T("main_menu_bar"));

SetMenuBar(mbar);

return true;
}

void MainFrame::OnQuit(wxCommandEvent& event)
{
Close(true);
}

void MainFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox(_("wxWidgets Example"), _("Welcome to..."));
}


main_menu.xrc
Code: [Select]
<?xml version="1.0"?>
<resource>
  <object class="wxMenuBar" name="main_menu_bar">
    <object class="wxMenu" name="file_menu">
      <label>&amp;File</label>     
      <object class="wxMenuItem" name="idFileExit">
        <label>&amp;Quit</label>
        <accel>Ctrl-Q</accel>
        <help>Quit wxWidgets Example</help>
      </object>
    </object>   
    <object class="wxMenu" name="help_men">
      <label>&amp;Help</label>
      <object class="wxMenuItem" name="wxID_ABOUT">
        <label>&amp;About...</label>
        <help>About wxWidgets Example</help>
      </object>     
    </object>
  </object>
</resource>


So wxWidgets tells me "XRC resource 'main_menu_bar' (class 'wxMenuBar') not found!"
It gives me a nice little error box I click on details and it says "can't load resources from file '"/resources.zip#zip:main_menu.xrc' (time)"
It gives me an option to save to a log and here is the log.

16:05:59: Cannot load resources from file '/resources.zip#zip:main_menu.xrc'.
16:05:59: Cannot load resources from file '/resources.zip#zip:main_menu.xrc'.
16:05:59: XRC resource 'main_menu_bar' (class 'wxMenuBar') not found!

What am I doing wrong? This is totally new to me, I have never used wxWidgets before.

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4291
    • Code::Blocks IDE
Re: wxWidgets XML resource files
« Reply #1 on: September 29, 2005, 11:16:55 pm »
Quote
wxXmlResource::Get()->Load(_T("/resources.zip#zip*.xrc"));

You should change this to relative path, e.g.
Code: [Select]
wxXmlResource::Get()->Load(_T("resources.zip#zip*.xrc"));(notice it's "resources" not "/resources")
Be patient!
This bug will be fixed soon...

sethjackson

  • Guest
Re: wxWidgets XML resource files
« Reply #2 on: September 29, 2005, 11:27:08 pm »
Quote
wxXmlResource::Get()->Load(_T("/resources.zip#zip*.xrc"));

You should change this to relative path, e.g.
Code: [Select]
wxXmlResource::Get()->Load(_T("resources.zip#zip*.xrc"));(notice it's "resources" not "/resources")

I did that still nothing. I keep getting the same error.

Maybe something else is wrong?

Offline Urxae

  • Regular
  • ***
  • Posts: 376
Re: wxWidgets XML resource files
« Reply #3 on: September 30, 2005, 12:39:15 am »
Did you initialize the zip file handler? If not, wxWidgets has no idea how to get the stuff in a zip file.

On application startup, call
Code: [Select]
wxFileSystem::AddHandler(new wxZipFSHandler); (don't forget to include <wx/filesys.h> and <wx/fs_zip.h>)

HTH

sethjackson

  • Guest
Re: wxWidgets XML resource files
« Reply #4 on: September 30, 2005, 12:51:21 am »
Did you initialize the zip file handler? If not, wxWidgets has no idea how to get the stuff in a zip file.

On application startup, call
Code: [Select]
wxFileSystem::AddHandler(new wxZipFSHandler); (don't forget to include <wx/filesys.h> and <wx/fs_zip.h>)

HTH

Thanks Urxae that makes sense. Duh  :oops:

EDIT:

Ok so I change

Code: [Select]
myres->LoadMenuBar(0L, _T("main_menu_bar"));

to

Code: [Select]
mbar = myres->LoadMenuBar(0L, _T("main_menu_bar"));

and I fixed the .xrc file to have the right XRCID's

 :oops: :roll:

Sometimes I need to think before I dive right in to coding.
« Last Edit: September 30, 2005, 01:03:00 am by sethjackson »