简体   繁体   中英

swapping background colors in gtk text field (gtkmm C++)

Within my GUI (C++, GTKMM 3), i have a text field that is providing some status information. i'd like to change the background color of this field (along with the text, which i can easily do), based upon the status.

there's not a lot out there on how to do this with GTKMM 3.X. i know i need to use the CssProvider class, and have found some examples on how to load one into the program. but the examples show how to set the properties one time.

but what i haven't figured out is how i can use the CSS properties to change the color of the background, based upon a state (not a state as in 'hover' or anything like that. i want to be able to swap the background from red to green whenever i please). if the CSS is written in terms of using the name of the widget, or the type of widget, how do you handle a changing state with the widget to change its properties?

if anyone has any clues, or knows of any examples, i could really use some help. the purpose of this is to give the user some immediate feedback at a glance. in a rush, they won't have to read the status of the box (or from a distance). the color will allow them to gauge what is going on at a glance.

Adding code

this is what i have tried so far (condensed):

std::string style_sheet = ".red_bg {background: #FF0000; color: #000000; } ";
style_sheet += ".green_bg {background: #33FF33; color: #000000; }";
Glib::RefPtr<Gtk::StyleContext> stylecontext = my_text_field->get_style_context();
Glib::RefPtr<Gtk::CssProvider> cssprov = Gtk::CssProvider::create();
cssprov->load_from_data(style_sheet);
stylecontext->add_provider(cssprov, GTK_STYLE_PROVIDER_PRIORITY_USER);
stylecontext->add_class("red_bg");
stylecontext->context_save();

so that works. when the program fires up, i get a text entry with a red background.

but later on, if i do the following, nothing happens:

Glib::RefPtr<Gtk::StyleContext>stylecontext = my_text_field->get_style_context();
stylecontext->remove_class("red_bg");
stylecontext->context_save();  // probably not necessary
stylecontext->add_class("green_bg");
stylecontext->context_save();

at that point, the background stays red. no transition from red to green. i've seen suggestions to use the override_background_color function in the GtkWidget object, but that doesn't work. that only changes the color that is used when you highlight the text in the widget. i'd still like to see it done the CSS way.

You could do away with the CSS and just use override_background_color , a standard GTK widget method:

override_background_color (StateFlags state, RGBA color)

Sets the background color to use for a widget.

All other style values are left untouched.

Note: This API is mostly meant as a quick way for applications to change a widget appearance. If you are developing a widgets library and intend this change to be themeable, it is better done by setting meaningful CSS classes and regions in your widget/container implementation through add_class and add_region .

This way, your widget library can install a CssProvider with the STYLE_PROVIDER_PRIORITY_FALLBACK priority in order to provide a default styling for those widgets that need so, and this theming may fully overridden by the user's theme.

Note: Note that for complex widgets this may bring in undesired results (such as uniform background color everywhere), in these cases it is better to fully style such widgets through a CssProvider with the STYLE_PROVIDER_PRIORITY_APPLICATION priority.

Parameters:

  • StateFlags state the state for which to set the background color
  • RGBA color the color to assign, or null to undo the effect of previous calls to override_background_color

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM