Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: kencamargo on October 16, 2009, 01:37:05 am

Title: Feature request? - xpm images
Post by: kencamargo on October 16, 2009, 01:37:05 am
Hello,

I tend to prefer using embedded .xpm images in my programs. This means going back to the code generated by wxSmith, replacing the file name with the char * array in the .xpm file and adding it as an include file, as below:

wxImage(_T("/files/whatever/directory/images/xyz.xpm")) ;

becomes

#include "xyz.xpm"
wxImage(xyz);

(I generated the .xpm files and adopted as a convention naming the char array with the name of the file minus the extension)

If for any reason I reedit the GUI file with wxSmith, these changes are lost (not the #include directives, though), and I have to redo everything by hand. Yesterday I forgot to do that, and got a desperate user e-mailing me repeatedly today... :)

Is there a way to either (a) force the code generator in wxSmith to ignore the edited initialization code or (b) automatically replace the name of the .xpm file with the corresponding array name?

Thanks in advance,
Ken
Title: Re: Feature request? - xpm images
Post by: mandrav on October 16, 2009, 07:40:18 am
wxSmith uses/supports the wxWidgets' standard ways of including images in your programs. I 'll describe the way to achieve your goal without hand editing anything. It's the way I do it so feel free to take it or leave it :).

If you have noticed, in the wxSmith image dialog (for toolbars, bitmap buttons, etc), there's an option named "Image from wxArtProvider". Also note that the "Art Id" field is editable (i.e. you can type whatever you want in there). That's the key to what you want to achieve.

Now, I could give you the link to wxArtProvider's documentation (http://docs.wxwidgets.org/stable/wx_wxartprovider.html#wxartprovider) and let you figure it out from there but I 'll go on and describe what you can do.

Create a file (or break it up in header/source) like this:

Code
#ifndef MY_ARTPROVIDER_H
#define MY_ARTPROVIDER_H

#include <wx/artprov.h>

// include *your* xpm icons here:
#include "icons/xxx.xpm"
#include "icons/yyy.xpm"
#include "icons/zzz.xpm"

class MyArtProvider : public wxArtProvider
{
public:
MyArtProvider() {}
virtual ~MyArtProvider() {}

protected:
wxBitmap CreateBitmap(const wxArtID& id,
  const wxArtClient& client,
  const wxSize& size)
{
if (id == _T("MY_ART_ID_XXX"))
{
return wxBitmap(xxx_xpm);
}
else if (id == _T("MY_ART_ID_YYY"))
{
return wxBitmap(yyy_xpm);
}
else if (id == _T("MY_ART_ID_ZZZ"))
{
return wxBitmap(zzz_xpm);
}
// possibly more Art Ids follow
}
};

#endif // MY_ARTPROVIDER_H

I hope it's clear enough and you can already see where this is going ;).

Anyway, there's only one thing still missing. Somewhere in your app's initialization (wxApp or your main frame/dialog constructor) just add this line:

Code
    wxArtProvider::Push(new MyArtProvider);

That's it. Now you can use your own Art Ids (e.g. MY_ART_ID_ZZZ) with wxSmith. The only down side is that you won't be able to actually see your bitmaps in the wxSmith editor but that's a small price to pay :).

Hope this helps.
Title: Re: Feature request? - xpm images
Post by: kencamargo on October 17, 2009, 02:35:10 am
Thanks mandrav, it sure helps. I actually used wxArtProvider before, I didn't know that new IDs can be added to it.

There's still the problem of me not seeing the pretty pics, though. :)

Ken