Re: Gtk::Button set_image()



Hi,

Am Donnerstag, den 28.05.2009, 11:13 -0400 schrieb Caroline Kierstead:

> Sorry to bother you again, but I hit another bizarre stumbling block.
> 
> >     const Glib::RefPtr<Gdk::Pixbuf>
> >       pixbuf = Gdk::Pixbuf::create_from_file("filename");
> >     // ...
> > 
> 
> I managed to get this to work if I initialized the pixel buffers by hard-coding 
> the calls as you described. Do you know why, if I switch to trying to use a 
> class that returns the pixel buffers, I would get the error:
> 
> (process:4209): GLib-GObject-CRITICAL **: gtype.c:2458: initialization assertion 
> failed, use g_type_init() prior to this function

That's because of the static class data here:

> const Glib::RefPtr<Gdk::Pixbuf> DeckGUI::deck[ACE+1][SPADE+1] =
> {   // Set up NINE of {DIAMOND,CLUB,HEART,SPADE}
> 	{Gdk::Pixbuf::create_from_file("pixmaps/card_0_0.xpm"), 
> Gdk::Pixbuf::create_from_file("pixmaps/card_0_1.xpm"),
[...]
> 	Gdk::Pixbuf::create_from_file("pixmaps/card_5_2.xpm"), 
> Gdk::Pixbuf::create_from_file("pixmaps/card_5_3.xpm") }
> };

The initialization is guaranteed to have been completed by the time
main() is entered.  That implies it is also guaranteed to execute before
the Gtk::Main initialization. :-)

You need to create the pixbufs at some point after Gtk::Main has been
instantiated.

A couple of comments:

> 	// Holds 2-D array of pixmaps so can easily index by Faces and Suit.
> 	const static Glib::RefPtr<Gdk::Pixbuf> deck[ACE+1][SPADE+1];
> 	const static Glib::RefPtr<Gdk::Pixbuf> nullCard;                           // 

Yuck.  Putting objects of non-POD type into plain C arrays is almost
always a bad idea.  It should work in this case, but the static
initialization won't.  You may want to look at pixbuf-demo in gtkmm for
an example that creates pixbufs from a static array of filenames.

> 	const Glib::RefPtr<Gdk::Pixbuf> getCardImage( Faces f, Suits s );

It isn't wrong to return a const value, but pointless in most cases.

> 	// Initialize 4 empty cards and place them in the box.
> 	for (int i = 0; i < 4; i++ ) {
> 		card[i] = new Gtk::Image( nullCardPixbuf );
> 		hbox.add( *card[i] );
> 	} // for
[...]
> OtherWorld::~OtherWorld() {
> 	for (int i = 0; i < 5; i++ ) delete card[i];
> } // OtherWorld::~OtherWorld()

You can avoid the manual (and unsafe) memory management by taking
advantage of container ownership:

    hbox.add(*Gtk::manage(new Gtk::Image(pixbuf)));

This works for all non-toplevel widgets.

> const Glib::RefPtr<Gdk::Pixbuf> DeckGUI::nullCard = 
> Gdk::Pixbuf::create_from_file("pixmaps/cardback2.xpm");

Any particular reason why you're using the obsolete XPM format?  It
isn't a big deal, but I suspect you might be using XPM only because it
used to be the standard image format for X applications, ages ago.

Oh, and please always carbon-copy to the mailing list when you're
replying.

Cheers,
--Daniel




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