how to set a specific css class to a widget in gtk3? (c)

Marco Rossi picture Marco Rossi · Jun 3, 2016 · Viewed 7.7k times · Source

I'm trying css in gtk3 and I don't understand how to use specific class.

C code:

provider = gtk_css_provider_new();
display = gdk_display_get_default();
screen = gdk_display_get_default_screen (display);
gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_css_provider_load_from_path(GTK_CSS_PROVIDER(provider),"styles.css",NULL);

enter_button = gtk_button_new_with_label("Print");
g_signal_connect(G_OBJECT(enter_button), "clicked", G_CALLBACK(print_entry_dialog),&t_data);
gtk_box_pack_start(GTK_BOX(hbox3), enter_button, TRUE, TRUE, 0);

Css (styles.css):

GtkButton{
  background: #669999;
  text-shadow: 1px 1px 5px black;
  box-shadow: 0px 0px 5px black;
  border: 1px solid black;
}

In this way it works : enter image description here

But I want to set a 'enter_button' class that set properties for only the 'enter_button', not for all widget under GtkButton name.

I read about gtk_style_context_add_class () func, but I don't know how it works with the 'styles.css' file. What I should do?

Answer

Marco Rossi picture Marco Rossi · Jun 4, 2016

I solved in this way:

GtkStyleContext *context;
enter_button = gtk_button_new_with_label("Print");
context = gtk_widget_get_style_context(enter_button);
gtk_style_context_add_class(context,"enter_button");

CSS:

.enter_button{
    background: #669999;
    text-shadow: 1px 1px 5px black;
    border-radius: 3px;
    box-shadow: 0px 0px 5px black;
}

For further informations look up here : GtkStyleContext examples