Re: set custom entry background?



On Mon, 16 Nov 2015, Stefan Salewski wrote:

On Sun, 2015-11-15 at 19:30 -0500, Allin Cottrell wrote:
But how can I "undo" this? (I mean, make it so that the background
color for the selected state reverts to what it was before I messed
with it, for subsequent selections.) That's trivial with the old
APIs but I have no idea how it can be done now.

You may see

https://developer.gnome.org/gtk3/stable/GtkWidget.html#gtk-widget-get-st
yle-context

https://developer.gnome.org/gtk3/stable/GtkStyleContext.html#gtk-style-c
ontext-remove-provider

No time to test it, sorry.

Ah, this works, thanks! I somehow missed the "remove" API.

Here's the working code:

void normalize_style (GtkWidget *w, gpointer p)
{
    GtkStyleContext *context;

    context = gtk_widget_get_style_context(w);
    gtk_style_context_remove_provider(context,
                                      GTK_STYLE_PROVIDER(p));
}

void entry_alert (GtkWidget *entry)
{
    GtkCssProvider *prov;
    GtkStyleContext *context;
    const gchar *snippet =
        "GtkEntry:selected {background-color: #ff0000;}";

    prov = gtk_css_provider_new();
    gtk_css_provider_load_from_data(prov, snippet, -1, NULL);
    context = gtk_widget_get_style_context(entry);
    gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(prov),
        GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
    gtk_widget_grab_focus(entry);
    gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
    g_signal_connect(G_OBJECT(entry), "changed",
                     G_CALLBACK(normalize_style), prov);
}

Allin Cottrell


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]