This is the relevant part of the code:
btn1 = gtk_button_new_with_label("one");
gtk_grid_attach (GTK_GRID (grid_ptr), btn1, 0, 1, 1, 1);
gtk_widget_set_name ( btn1, "btn1");
GtkCssProvider *prvdr = gtk_css_provider_new ();
GtkStyleContext *cntxt = gtk_widget_get_style_context ( btn1 );
gtk_css_provider_load_from_data (prvdr, "button#btn1 { text-decoration: underline white;}", -1);
gtk_style_context_add_provider (cntxt, GTK_STYLE_PROVIDER (prvdr), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
It compiles without any issues, and runs without errors or warnings, but nothing gets underlined.
I have also tried these with the same result: "button#btn1 { text-decoration: underline;}" "button#btn1 { text-decoration-line: underline;}"
I have changed the text colour using "button#btn1 { color: #00ff00;}".
Can anyone explain why it does not work, or how to fix it?
For this to work, you shall have to use CSS selector for the label inside the button. Say something like button#btn1 > label {...}
A minimal example for the same:
/* button.c
*
* Compile: cc -ggdb button.c -o button $(pkg-config --cflags --libs gtk4)
* Run: ./button
*
* Author: Mohammed Sadiq <www.sadiqpk.org>
*
* SPDX-License-Identifier: LGPL-2.1-or-later OR CC0-1.0
*/
#include <gtk/gtk.h>
static void
app_activated_cb (GtkApplication *app)
{
g_autoptr(GtkCssProvider) css_provider = NULL;
GtkWindow *window;
GtkWidget *button;
button = gtk_button_new_with_label ("Click me!");
gtk_widget_set_name (button, "main");
css_provider = gtk_css_provider_new ();
gtk_css_provider_load_from_data (css_provider,
"button#main > label { text-decoration: underline white;}", -1);
gtk_style_context_add_provider_for_display (gdk_display_get_default (),
GTK_STYLE_PROVIDER (css_provider),
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
window = GTK_WINDOW (gtk_application_window_new (app));
gtk_window_set_default_size (window, 600,400);
gtk_window_set_child (window, button);
gtk_window_present (window);
}
int
main (int argc,
char *argv[])
{
g_autoptr(GtkApplication) app = gtk_application_new (NULL, 0);
g_signal_connect (app, "activate", G_CALLBACK (app_activated_cb), NULL);
return g_application_run (G_APPLICATION (app), argc, argv);
}