Skip to content

Commit

Permalink
Search parent style contexts for a background color with GTK3
Browse files Browse the repository at this point in the history
'background-color' is not an inherited CSS property, but in practical terms if
a widget does not define a background color, it will have the background of the
first ancestor that does define a background. Searching up through the context
heirarchy should allow finding the correct color regardless of what form of
CSS selector a particular theme uses.
See wxWidgets#25048
  • Loading branch information
paulcor committed Jan 12, 2025
1 parent 87135ea commit 76b5d0f
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/gtk/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,12 @@ wxGtkStyleContext& wxGtkStyleContext::AddWindow(const char* className2)
return Add(GTK_TYPE_WINDOW, "window", "background", className2, nullptr);
}

void wxGtkStyleContext::Bg(wxColour& color, int state) const
static void bg(GtkStyleContext* sc, wxColour& color, int state)
{
GdkRGBA* rgba;
cairo_pattern_t* pattern = nullptr;
gtk_style_context_set_state(m_context, GtkStateFlags(state));
gtk_style_context_get(m_context, GtkStateFlags(state),
gtk_style_context_set_state(sc, GtkStateFlags(state));
gtk_style_context_get(sc, GtkStateFlags(state),
"background-color", &rgba, "background-image", &pattern, nullptr);
color = wxColour(*rgba);
gdk_rgba_free(rgba);
Expand Down Expand Up @@ -679,12 +679,26 @@ void wxGtkStyleContext::Bg(wxColour& color, int state) const
}
cairo_pattern_destroy(pattern);
}
}

if (color.Alpha() == 0)
void wxGtkStyleContext::Bg(wxColour& color, int state) const
{
for (GtkStyleContext* sc = m_context; sc; )
{
// Try TLW as last resort, but not if we're already doing it
if (gtk_widget_path_length(m_path) > 1)
wxGtkStyleContext().AddWindow().Bg(color, state);
bg(sc, color, state);
if (color.Alpha())
break;
#if GTK_CHECK_VERSION(3,4,0)
if (gtk_check_version(3,4,0) == nullptr)
sc = gtk_style_context_get_parent(sc);
else
#endif
{
// Try TLW as last resort, but not if we're already doing it
if (gtk_widget_path_length(m_path) > 1)
wxGtkStyleContext().AddWindow().Bg(color, state);
break;
}
}
}

Expand Down

0 comments on commit 76b5d0f

Please sign in to comment.