I am new to Gtk+3. I'm used to the Visual Basic way of designing GUIs. I've been very frustrated by all the hoops Gtk put me through, but I think I've researched and solved all my problems but one. This is latest Gtk+3 on a raspberry pi, and I used Glade to tack together the layout. The goal is to have individual labels change background color and text color under software control. Note this is strictly a display - no input controls, just grids, layout boxes, and labels.
I almost have it working. When my code runs, it alternates applying and removing a given css style. I can see the result. But the label has 3 separate attributes of interest: the background color of the label itself, the background color of the text (the text doesn't fill the whole label), and the color of the text itself. My code successfully changes the background of the label, and the color of the text, but not the background of the text. I need the text background to match the label background in all cases. Making the text background color be always transparent would be one approach; changing both background colors at the same time would be another. How do I?
Here's the CSS of interest:
* {
background-color: black;
color: whitesmoke;
}
.redStyle {
background-color: rgb(182, 8, 8);
color: whitesmoke;
}
.pinkStyle {
background-color: rgb(241, 135, 135);
color: black;
}
The test code alternately calls these two functions from Gtk's idle mechanism.
gboolean flashing1(void*)
{
GtkWidget* widget = GTK_WIDGET(gtk_builder_get_object (builder, "labObsession"));
GtkStyleContext *context = gtk_widget_get_style_context(widget);
gtk_style_context_add_class(context,"pinkStyle");
gtk_widget_queue_draw(widget);
return false;
}
gboolean flashing2(void*)
{
GtkWidget* widget = GTK_WIDGET(gtk_builder_get_object (builder, "labObsession"));
GtkStyleContext *context = gtk_widget_get_style_context(widget);
gtk_style_context_remove_class(context,"pinkStyle");
gtk_widget_queue_draw(widget);
return false;
}
Please help. It two two days of googling to get even this far. I know there's a depreciated override function that probably does this but I'm trying to avoid that.
For extra credit, why do I see ** (a.out:26251): WARNING **: Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service files Whenever I start?
Here's the full code of the test app:
#include <gtk/gtk.h>
#include <thread>
#include <iostream>
static GtkBuilder *builder;
gboolean flashing1(void*)
{
GtkWidget* widget = GTK_WIDGET(gtk_builder_get_object (builder, "labObsession"));
GtkStyleContext *context = gtk_widget_get_style_context(widget);
gtk_style_context_add_class(context,"pinkStyle");
gtk_widget_queue_draw(widget);
return false;
}
gboolean flashing2(void*)
{
GtkWidget* widget = GTK_WIDGET(gtk_builder_get_object (builder, "labObsession"));
GtkStyleContext *context = gtk_widget_get_style_context(widget);
gtk_style_context_remove_class(context,"pinkStyle");
gtk_widget_queue_draw(widget);
return false;
}
void flashingTh(GtkWidget*) //may need this param someday
{
for (;;)
{
usleep(50 * 1000);
gdk_threads_add_idle (flashing1, nullptr);
usleep(90 * 1000);
gdk_threads_add_idle (flashing2, nullptr);
}
}
int main(int argc, char *argv[])
{
GtkWidget *window;
gtk_init(&argc, &argv);
builder = gtk_builder_new();
gtk_builder_add_from_file (builder, "randomness/Lachesis.glade", NULL);
window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
gtk_builder_connect_signals(builder, NULL);
// g_object_unref(builder); //keep. needed to find things. Ugh.
//GtkStyleContext *context;
GtkCssProvider *provider = gtk_css_provider_new ();
gtk_css_provider_load_from_path (provider,
"randomness/lachesis.css",
nullptr);
gtk_style_context_add_provider_for_screen (gdk_screen_get_default(),
GTK_STYLE_PROVIDER(provider),
GTK_STYLE_PROVIDER_PRIORITY_USER);
gtk_widget_show(window);
std::thread(flashingTh, window).detach();
gtk_main();
return 0;
}
//not hooked up yet, signals can wait until painting works
// called when window is closed
void on_window_main_destroy()
{
gtk_main_quit();
}
And here's a fragment of the .glade:
<interface>
<requires lib="gtk+" version="3.0"/>
<object class="GtkWindow" id="window1">
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="title" translatable="yes">Lachesis</property>
<property name="window_position">center-always</property>
<property name="hide_titlebar_when_maximized">True</property>
<child>
<object class="GtkGrid" id="topGrid">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="halign">start</property>
<property name="row_spacing">2</property>
<property name="column_spacing">2</property>
<child>
<object class="GtkBox" id="box1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="labObsession">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="label" translatable="yes">Controller: unknown</property>
<attributes>
<attribute name="background" value="#000000000000"/>
</attributes>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>