wxToolbookXmlHandler

If you have a cool piece of software to share, but you are not hosting it officially yet, please dump it in here. If you have code snippets that are useful, please donate!
Post Reply
Abel
In need of some credit
In need of some credit
Posts: 2
Joined: Mon Sep 24, 2007 7:55 am

wxToolbookXmlHandler

Post by Abel »

Hi everybody!

First of all let me introduce myself. Im Abel Alonso and I currently working as a software developer in CoffeeCup Software. We are developing our solutions using wxWidgets and we are very pleased with the library features.

To get our goals, we had to develop a xml handler to can use wxToolbooks components into xrc files. This handler (wxToolbookXmlHandler), isn't included with the wxWidgets library and may be useful for somebody here.

We would be proud to share this code with somebody who is interested in it. So, if anybody is interested in get that code, please contact with me by posting a reply to this post or sending me an email to [email protected]

Kind regards, Abel.
vdell
Ultimate wxWidgets Guru
Ultimate wxWidgets Guru
Posts: 536
Joined: Fri Jan 07, 2005 3:44 pm
Location: Finland
Contact:

Post by vdell »

If you are willing to share the whole code then why can't you just post it here? It would probably be useful to many people.
Visual C++ 9.0 / Windows XP Pro SP3 / wxWidgets 2.9.0 (SVN) | Colligere
Abel
In need of some credit
In need of some credit
Posts: 2
Joined: Mon Sep 24, 2007 7:55 am

Post by Abel »

That is true, the best way to share my code is to post it here. So, here we go. :D

Header File

Code: Select all

#ifndef _WX_TOOLBOOKXMLHANDLER_H_
#define _WX_TOOLBOOKXMLHANDLER_H_

#include "wx/xrc/xmlres.h"


class wxToolbook;

class wxToolbookXmlHandler : public wxXmlResourceHandler
{
	DECLARE_DYNAMIC_CLASS(wxToolbookXmlHandler)

public:
	wxToolbookXmlHandler();
	virtual wxObject *DoCreateResource();
	virtual bool CanHandle(wxXmlNode *node);

private:
	bool m_isInside;
	wxToolbook *m_toolbook;
};


#endif
And finally, the source file

Code: Select all

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include "wxToolbookXmlHandler.h"

#ifndef WX_PRECOMP
#include "wx/log.h"
#include "wx/sizer.h"
#endif

#include "wx/toolbook.h"
#include "wx/imaglist.h"

IMPLEMENT_DYNAMIC_CLASS(wxToolbookXmlHandler, wxXmlResourceHandler)

///Ctor. We add all styles supported
wxToolbookXmlHandler::wxToolbookXmlHandler()
:wxXmlResourceHandler(),
m_isInside(false),
m_toolbook(NULL)
{
	XRC_ADD_STYLE(wxBK_DEFAULT);
	XRC_ADD_STYLE(wxBK_LEFT);
	XRC_ADD_STYLE(wxBK_RIGHT);
	XRC_ADD_STYLE(wxBK_TOP);
	XRC_ADD_STYLE(wxBK_BOTTOM);

	AddWindowStyles();
}
///Creates the resource
wxObject *wxToolbookXmlHandler::DoCreateResource()
{
	//If the resource is a toolbook page...
	if (m_class == wxT("toolbookpage"))
	{
		//Get the xml node
		wxXmlNode *n = GetParamNode(wxT("object"));

		if ( !n )
			n = GetParamNode(wxT("object_ref"));

		if (n)
		{
			//Creates the resource
			bool old_ins = m_isInside;
			m_isInside = false;
			wxObject *item = CreateResFromNode(n, m_toolbook, NULL);
			m_isInside = old_ins;
			wxWindow *wnd = wxDynamicCast(item, wxWindow);

			if (wnd)
			{
				//It must be initialized to -1 (None icon)
				int imgIndex = -1;

				//If we have a bitmap, we must create it, before add the page to the control
				if ( HasParam(wxT("bitmap")) )
				{
					wxBitmap bmp = GetBitmap(wxT("bitmap"), wxART_OTHER);
					wxImageList *imgList = m_toolbook->GetImageList();
					if ( imgList == NULL )
					{
						imgList = new wxImageList( bmp.GetWidth(), bmp.GetHeight() );
						m_toolbook->AssignImageList( imgList );
					}
					imgIndex = imgList->Add(bmp);
				}
				//Add the new page to the control 
				m_toolbook->AddPage(wnd, GetText(wxT("label")),
					GetBool(wxT("selected")), imgIndex );
			}
			else
				wxLogError(wxT("Error in resource."));
			return wnd;
		}
		else
		{
			wxLogError(wxT("Error in resource: no control within notebook's <page> tag."));
			return NULL;
		}
	}
	//If the xml node is an wxToolbook node, we must create the toolbook
	else
	{
		XRC_MAKE_INSTANCE(nb, wxToolbook)

			nb->Create(m_parentAsWindow,
			GetID(),
			GetPosition(), GetSize(),
			GetStyle(wxT("style")),
			GetName());

		SetupWindow(nb);

		wxToolbook *old_par = m_toolbook;
		m_toolbook = nb;
		bool old_ins = m_isInside;
		m_isInside = true;
		CreateChildren(m_toolbook, true/*only this handler*/);
		m_isInside = old_ins;
		m_toolbook = old_par;

		return nb;
	}
}

bool wxToolbookXmlHandler::CanHandle(wxXmlNode *node)
{
	return ((!m_isInside && IsOfClass(node, wxT("wxToolbook"))) ||
		(m_isInside && IsOfClass(node, wxT("toolbookpage"))));
}
I hope that my code would be useful to somebody.

Regards!
Post Reply