how to set a specific css class to a widget in gtk3? (c)
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
How to set CSS class for specific button in gtkmm3 in c++ on Linux
You are mixing GTK+ with gtkmm (C++ interface for GTK+). b1
is not a GtkButton but Gtk::Button.
Use Gtk::StyleContext:
Glib::RefPtr<StyleContext> context = b1->get_style_context();
context->add_class("myButton");
Can't set CSS to specified widget in GTK+
I haven't programmed in Vala, but you should add class to StyleContext.
This is in C
sidebar = gtk_label_new ("Hello');
gtk_style_context_add_class ( gtk_widget_get_style_context ("mysidebar"), sidebar);
Also, style "sidebar", is already defined in GtkStyle. You should change the "sidebar" in CSS into something else (sidebar is used by views, toolbar etc)
But if you persist, the syntax should be:
.mysidebar {
#anything
}
Problems while using gtk+3 and Css
GTK stopped using widget type names as CSS node names in version 3.18 or so, and from then on, you have to check the C class documentation to see what node names, classes, and so on are available to theme. In this case, it would be
label { [...] }
I also recommend loading the StyleContext to the Display, not individual widgets. So, basically, use a modern version of GTK (ideally latest point 3.24.x, but at least 3.22) and the documented CSS selectors, and you're good to go.
Once doing that, if you only want to affect individual widgets, then just add CSS classes to them and select on those classes:
gtk_style_context_add_class(my_label_style_context, "the-precious");
and then select in CSS on
label.the-precious { [...] }
or just
.the-precious { [...] }
A fuller example is available in this other answer.
That is better than adding StyleContexts to individual widgets because doing that tends not to work how users expect (in terms of inheritance and such).
You can also set CSS IDs on widgets (like #the-precious
), but that is less often used and IMO not really needed in GTK and more of a faff to set up IMO.
Note that the default GTK theme, Adwaita, was refreshed during 3.24 - so if you want to theme your application against that, it's best to do so from the latest available version of 3.24 - and hope it doesn't change again in 3.x...
Changing widget preoperties programmatically in gtkmm3 / gtk3
Well, after some more research from questions such as this and thisI found what I think is the correct way of doing what I need.
Basically define the properties for the states that the widget style will hold when adding the CSS provider to the window:
Updated from main() above in question:
Glib::ustring theme(
"@define-color bg rgb(57.64%, 48.62%, 36.47%);"
"@define-color BtnBg rgb(23.13%, 19.60%, 16.07%);"
".background { background-color: @bg; }"
"button"
"{"
" background-image:image(@BtnBg);"
" border-color: @bg;"
" color: rgb(80.%, 80.%, 80.%);"
" padding-left: 4px;"
" padding-right: 4px;"
"}"
"button:hover"
"{"
//" background-image:image(rgb(77%, 81%, 84%));"
" background-image:image(#FF4300);"
" color: rgb(100.%, 100.%, 100.%);"
"}"
"#LSK0 { background-image: image(@bg); border-color: @bg }"
"#RSK0 { background-image: image(@bg); border-color: @bg }"
".RunButton { background-image: image(rgb(80%, 80%, 80%)); color: rgb(0.%, 0.%, 0.%); }"
);
Also note that the properties for the "RunButton" style are now listed as a class (.RunButton) and NOT as an ID (#RunButton).
This added the style as a class inside the context of the window widgets but does not apply it to anything yet. Then programmatically during the execution of the code and in response to the state change event, the widget style is updated by adding and removing the style property classes:
void GWin::onBtnLight(bool on)
{
if(on)
{
mRunButton.get_style_context()->add_class("RunButton");
}
else
{
mRunButton.get_style_context()->remove_class("RunButton");
}
}
Now the button and label colors on the widget toggle between the RunButton style class and the default style class.
Why is CSS style not being applied to GTK+ code?
An application can make GTK+ parse a specific CSS style sheet by calling gtk_css_provider_load_from_file() or gtk_css_provider_load_from_resource() and adding the provider with gtk_style_context_add_provider() or gtk_style_context_add_provider_for_screen(). The following objects are used to implement the style:
GtkStyleContext is an object that stores styling information affecting a widget defined by GtkWidgetPath. In order to construct the final style information, GtkStyleContext queries information from all attached GtkStyleProviders.
GtkCssProvider is an object implementing the GtkStyleProvider interface. GtkStyleProvider is used to provide style information to a GtkStyleContext
GtkCssProvider is an object implementing the GtkStyleProvider interface. It is able to parse CSS-like input in order to style widgets.
for more information https://developer.gnome.org/gtk3/stable/GtkCssProvider.html
The code is the following:
#include <gtk/gtk.h>
static void
activate (GtkApplication *app, gpointer user_data) {
GtkStyleContext *context;
GtkWidget *button_01;
GtkWidget *button_02;
button_01 = gtk_button_new_with_label("This is a simple button");
button_02 = gtk_button_new_with_label("This is a stylish button");
context = gtk_widget_get_style_context (button_02);
GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_path (provider,
"my_style.css", NULL);
GtkWidget *window;
GtkWidget *box;
window = gtk_application_window_new (app);
box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 25);
gtk_style_context_add_provider (context,
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_box_set_homogeneous (GTK_BOX (box), TRUE);
gtk_container_add (GTK_CONTAINER (window), box);
gtk_container_add (GTK_CONTAINER (box), button_01);
gtk_container_add (GTK_CONTAINER (box), button_02);
gtk_widget_show_all (window);
}
int main (int argc, char **argv) {
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
the CSS selectors is simply called 'button', the code is the following:
button {
background: #669999;
text-shadow: 1px 1px 5px black;
border-radius: 3px;
box-shadow: 0px 0px 5px black;
}
button:hover {
background: #3085a9;
}
CSS styles are not being applied on a Gtk widget
Note what the documentation for gtk_style_context_add_provider() says:
Note that a style provider added by this function only affects the style of the widget to which
context
belongs. If you want to affect the style of all widgets, use gtk_style_context_add_provider_for_screen().
The styles apply to that widget only, not the widget and its children. The preferred way to style GTK applications is to create one stylesheet for the whole application, then use gtk_style_context_add_class to add style classes to individual widgets.
Related Topics
Prevent a Pseudo Element from Triggering Hover
CSS Transform: Scale Does Not Change Dom Size
Does IE9 Not Support Display: Inline-Flex at All
Why Does Width/Height Work on an Inline Img Element
Ie10/11 Uses Transition:-Webkit-Transform
CSS Alternative for Display:Box for Ie and Opera
How to Display Image Over Image on Hover with CSS
How to Override Z-Index Inheritance from Parent Element
Converting "Long Shadow" SASS Mixin to Less
CSS Nth-Child Apply Odd-Even Rule But Switch Every 4 Items
How to Combine and Use Multiple Next.Js Plugins
Css3 Animation and Background-Image in Firefox
Why Do I Have to Put Media Queries at the Bottom of the Stylesheet