Re: Setting the font for Gtk::Button



----- Original Message ----- From: "Tadej BorovÅak" <tadeboro gmail com>

My advice to you would be to create label manually and then simply
pack it into empty GtkButton using gtk_container_add().

I created a derived class that uses an explicit label but strangely, it
didn't work.  Here's my code:-

class FontableTextButton : public Gtk::Button
{
public:
   FontableTextButton (const Glib::ustring& label, bool mnemonic = false);
   ~FontableTextButton () { delete m_pLabel; }

   void modify_font (const Pango::FontDescription& font_desc);
   // Caution!!  Gtk::Widget::modify_font() is not virtual !

private:
   // Other c'tors are private
   FontableTextButton () { m_pLabel = NULL; }
   FontableTextButton (const Gtk::StockID& stock_id) { m_pLabel = NULL; }

   // and this object is non-copyable
   FontableTextButton (const FontableTextButton&);
   FontableTextButton& operator= (const FontableTextButton&);

protected:
   Gtk::Label* m_pLabel;
};


FontableTextButton::FontableTextButton (const Glib::ustring& label, bool
                                                     mnemonic /*= false */)
{
   // We're creating our own label, so call the appropriate base
   // c'tor (i.e. the one that DOESN'T implicitly create a label).
   // Note that since this is the default c'tor, the compiler
   // would have chosen it anyway.
   Gtk::Button();

   // Now create the label explicitly
   m_pLabel = new Gtk::Label (label, mnemonic);

   // and attach it to the button
   if (m_pLabel)
       add (*m_pLabel);
}


void FontableTextButton::modify_font (const Pango::FontDescription&
                                                     font_desc)
{
   if (m_pLabel)
       m_pLabel->modify_font (font_desc);
}

And that's it..... The button works exactly like it did before.  The correct
text gets displayed.  FontableTextButton::modify_font() gets called at the
appropriate time.  But the label's font remains the stubbornly the same  :-(

John



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