Re: [gtkmm] Creating and setting pixmaps with strings


I have understood your problem. As you correctly noticed, the problem can not 
be solved the way you tried.
A string constant like "bla" or "gnim01lrg_xpm" can not be used as a reference 
to a variable or constant with that name, because the computer does not know 
the name of the variable after the compilation of your sourcecode. This is 
not a limitation of gtkmm but a limitation of the C++ language.
What you need is a map (see your C++ reference), to store pairs of strings and 
pointers to your dataconstructs, which then contain your pixmap data. Then 
you can use code like this:


 #include <map>


 map<string, char **> images;
 images["a picture"] = gnim01lrg_xpm;
 images["a other picture"] = gnim02lrg_xpm;


 pixmapGnim01->set(images["a picture"]);


 string picture_identifier;
 picture_identifier = result_of_choose_box();

I hope I could help you.
The map has an additional advantage. If you read the reference, you will 
notice, that you can use the list of identifier strings at another place in 
your program and not only to index the map. Imagine use these strings to 
display them in a choose box. Then you have to manage these strings only one 
time in your program.

Good luck.

Daniel Bausch

Am Saturday, 12. April 2003 18:41 schrieb Christopher David:
> Hi all,
> I'm currently working on a group project using Gtkmm 1.2 that uses
> pixmaps.  According to the Gtk-- Reference, you should be able to create
> a pixmap from an .xpm file by passing a string, or more specifically, an
> nstring a a parameter (as long as you include the pixmap file of
> course).  Now, I have gotten something like this to work:
> pixmapGnim01->set(gnim01lrg_xpm);
> Gtk::Pixmap *pix = new Gtk::Pixmap(gnim01lrg_xpm);
> where it uses the string in the pixmap header file "gnim01lrg_xpm".
>  However, I have not been able to so this:
> pixmapGnim01->set("gnim01lrg_xpm");
> or
> string pixName = "gnim01lrg_xpm";
> pixmapGnim01->set(pixName);
> or
> Gtk::nstring *pixName = new Gtk::nstring(currPixmapName);
> Gtk::Pixmap *pix = new Gtk::Pixmap(*pixName);
> or
> Gtk::nstring *pixName = new Gtk::nstring(currPixmapName);
> pixmapGnim01->set(pixName);
> and so on...
> As you can see, a string or nstring variable won't work, not even a real
> string of "gnim01lrg_xpm" won't work.
> But put in gnim01lrg_xpm by itself without quotation marks and it does
> work fine.  I can't figure out what I am doing wrong.  We really need to
> store the strings and pass them into the set or constructor methods so
> we can change a pixmap dynamically.  I know you can change a pixmap at
> runtime, but we need to store the information so when we load a saved
> program, all of the pixmaps will initialize to whatever string they are
> assigned to.  BTW, I know Gtkmm 1.2 is a bit old, but we have no choice
> in the matter and cannot upgrade at all.  We MUST use Gtkmm 1.2 .  Any
> help here would be appreciated a lot.  Thanks.
> Chris David
> _______________________________________________
> gtkmm-list mailing list
> gtkmm-list gnome org

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]