tags:

views:

40

answers:

2

Hi there, I'm almost a gtk newbie, and I'm looking for a way to get the background color for the current theme in gtk. So this code:

GdkColor color = gtk_widget_get_style(mainWindowHandle)->bg[GTK_STATE_NORMAL];

works only after the main window is shown, before returns an strange ugly gray.

+1  A: 

Try to attach to the widget's "realize" signal and then grab the style information you want.

static void
widget_realized_cb (GtkWidget *widget) {
  GdkColor *color = NULL;
  GtkStyle *style = gtk_widget_get_style (widget);

  if (style != NULL) {
    color = style->bg[GTK_STATE_NORMAL];

    /* Do whatever you want with it here */
  }
}

void foobar () {
  g_signal_connect (mainWindowHandle,
                    "realize",
                    G_CALLBACK (widget_realized_cb),
                    NULL);
}
bratsche
Many many thanks for the suggestion, I've just added a gtk_widget_realize after the window creation (without the signal_connect) and now works perfectly!
Nicola Leoni
Nice! Feel free to rank me up and mark my answer as the solution to your problem. I need the SO points. :)
bratsche
Actually, now that I'm reading this.. why did you explicitly call gtk_widget_realize() instead of listening for the signal? I don't think you should do it that way. The widget will be automatically realized in the correct way when you call gtk_widget_show() or gtk_widget_show_all().
bratsche
Ya, I know. But I've a very big application, whit a lot of plugins that needs that color to build the interface before the main windows is shown. So after the main window creation I suddently call the realize, according to the documentation, and it perfectly works. Why you think that's wrong?
Nicola Leoni
From http://developer.gnome.org/doc/GGAD/z57.html: "In typical user code, you only need to call gtk_widget_show(); this implies realizing and mapping the widget as soon as its parent is realized and mapped. [...] It's uncommon to need gtk_widget_realize(); if you find that you do, perhaps you are approaching the problem incorrectly." So it says that it's uncommon, but not incorrect ;)
Nicola Leoni
A: 

I've added a

gtk_widget_realize(mainWindowHandle);

before the gtk_widget_get_style and works perfectly!

Nicola Leoni
See my comment on the other answer.. you probably should listen for the "realize" signal instead of explicitly realizing the widget.
bratsche