Resolves: tdf#122662 restore context after changing state

otherwise we may change a context which get reused by gtk for rendering
something we don't ourselves draw directly

Change-Id: I814140cd63489f7e4fac47a7486a006a4aa1fac5
Reviewed-on: https://gerrit.libreoffice.org/66847
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/vcl/unx/gtk3/gtk3salnativewidgets-gtk.cxx b/vcl/unx/gtk3/gtk3salnativewidgets-gtk.cxx
index 6ca1802..c1fb893 100644
--- a/vcl/unx/gtk3/gtk3salnativewidgets-gtk.cxx
+++ b/vcl/unx/gtk3/gtk3salnativewidgets-gtk.cxx
@@ -395,12 +395,40 @@ namespace
        }
    }

    void parent_styles_context_save(GtkStyleContext* context)
    {
        while ((context = gtk_style_context_get_parent(context)))
        {
            gtk_style_context_save(context);
        }
    }

    void parent_styles_context_restore(GtkStyleContext* context)
    {
        while ((context = gtk_style_context_get_parent(context)))
        {
            gtk_style_context_restore(context);
        }
    }

    void style_context_set_state(GtkStyleContext* context, GtkStateFlags flags)
    {
        gtk_style_context_set_state(context, flags);
        parent_styles_context_set_state(context, flags);
    }

    void style_context_save(GtkStyleContext* context)
    {
        gtk_style_context_save(context);
        parent_styles_context_save(context);
    }

    void style_context_restore(GtkStyleContext* context)
    {
        gtk_style_context_restore(context);
        parent_styles_context_restore(context);
    }

    tools::Rectangle render_common(GtkStyleContext *pContext, cairo_t *cr, const tools::Rectangle &rIn, GtkStateFlags flags)
    {
        if (!pContext)
@@ -2491,6 +2519,7 @@ bool GtkSalGraphics::drawNativeControl( ControlType nType, ControlPart nPart, co
    long nWidth = rControlRegion.GetWidth();
    long nHeight = rControlRegion.GetHeight();

    style_context_save(context);
    style_context_set_state(context, flags);

    if (styleClass)
@@ -2625,6 +2654,7 @@ bool GtkSalGraphics::drawNativeControl( ControlType nType, ControlPart nPart, co
    {
        gtk_style_context_remove_class(context, styleClass);
    }
    style_context_restore(context);

    cairo_destroy(cr); // unref

@@ -2929,6 +2959,7 @@ void GtkSalGraphics::updateSettings(AllSettings& rSettings)
    }

    GtkStyleContext* pStyle = gtk_widget_get_style_context( mpWindow );
    style_context_save(pStyle);
    GtkSettings* pSettings = gtk_widget_get_settings( mpWindow );
    StyleSettings aStyleSet = rSettings.GetStyleSettings();
    GdkRGBA color;
@@ -2968,9 +2999,12 @@ void GtkSalGraphics::updateSettings(AllSettings& rSettings)
    aTextColor = getColor( text_color );
    aStyleSet.SetFieldRolloverTextColor( aTextColor );

    style_context_restore(pStyle);

    // button mouse over colors
    {
        GdkRGBA normal_button_rollover_text_color, pressed_button_rollover_text_color;
        style_context_save(mpButtonStyle);
        style_context_set_state(mpButtonStyle, GTK_STATE_FLAG_PRELIGHT);
        gtk_style_context_get_color(mpButtonStyle, gtk_style_context_get_state(mpButtonStyle), &normal_button_rollover_text_color);
        aTextColor = getColor(normal_button_rollover_text_color);
@@ -2980,6 +3014,7 @@ void GtkSalGraphics::updateSettings(AllSettings& rSettings)
        aTextColor = getColor(pressed_button_rollover_text_color);
        style_context_set_state(mpButtonStyle, GTK_STATE_FLAG_NORMAL);
        aStyleSet.SetButtonPressedRolloverTextColor( aTextColor );
        style_context_restore(mpButtonStyle);
    }

    // tooltip colors
@@ -3064,6 +3099,11 @@ void GtkSalGraphics::updateSettings(AllSettings& rSettings)
    aStyleSet.SetSkipDisabledInMenus( true );
    aStyleSet.SetPreferredContextMenuShortcuts( false );

    style_context_save(mpMenuStyle);
    style_context_save(mpMenuBarStyle);
    style_context_save(mpMenuBarItemStyle);
    style_context_save(mpMenuItemLabelStyle);

    // menu colors
    style_context_set_state(mpMenuStyle, GTK_STATE_FLAG_NORMAL);
    gtk_style_context_get_background_color( mpMenuStyle, gtk_style_context_get_state(mpMenuStyle), &background_color );
@@ -3103,47 +3143,62 @@ void GtkSalGraphics::updateSettings(AllSettings& rSettings)
    ::Color aHighlightTextColor = getColor( color );
    aStyleSet.SetMenuHighlightTextColor( aHighlightTextColor );

    style_context_restore(mpMenuItemLabelStyle);
    style_context_restore(mpMenuBarItemStyle);
    style_context_restore(mpMenuBarStyle);
    style_context_restore(mpMenuStyle);

    // hyperlink colors
    style_context_save(mpLinkButtonStyle);
    style_context_set_state(mpLinkButtonStyle, GTK_STATE_FLAG_LINK);
    gtk_style_context_get_color(mpLinkButtonStyle, gtk_style_context_get_state(mpLinkButtonStyle), &text_color);
    aStyleSet.SetLinkColor(getColor(text_color));
    style_context_set_state(mpLinkButtonStyle, GTK_STATE_FLAG_VISITED);
    gtk_style_context_get_color(mpLinkButtonStyle, gtk_style_context_get_state(mpLinkButtonStyle), &text_color);
    aStyleSet.SetVisitedLinkColor(getColor(text_color));
    style_context_restore(mpLinkButtonStyle);

    {
        GtkStyleContext *pCStyle = mpNotebookHeaderTabsTabLabelStyle;
        style_context_save(pCStyle);
        style_context_set_state(pCStyle, GTK_STATE_FLAG_NORMAL);
        gtk_style_context_get_color(pCStyle, gtk_style_context_get_state(pCStyle), &text_color);
        aTextColor = getColor( text_color );
        aStyleSet.SetTabTextColor(aTextColor);
        aStyleSet.SetTabFont(getFont(mpNotebookHeaderTabsTabLabelStyle, rSettings.GetUILanguageTag().getLocale()));
        style_context_restore(pCStyle);
    }

    {
        GtkStyleContext *pCStyle = mpToolButtonStyle;
        style_context_save(pCStyle);
        style_context_set_state(pCStyle, GTK_STATE_FLAG_NORMAL);
        gtk_style_context_get_color(pCStyle, gtk_style_context_get_state(pCStyle), &text_color);
        aTextColor = getColor( text_color );
        aStyleSet.SetToolTextColor(aTextColor);
        aStyleSet.SetToolFont(getFont(mpToolButtonStyle, rSettings.GetUILanguageTag().getLocale()));
        style_context_restore(pCStyle);
    }

    // mouse over text colors
    {
        GtkStyleContext *pCStyle = mpNotebookHeaderTabsTabHoverLabelStyle;
        style_context_save(pCStyle);
        style_context_set_state(pCStyle, GTK_STATE_FLAG_PRELIGHT);
        gtk_style_context_get_color(pCStyle, gtk_style_context_get_state(pCStyle), &text_color);
        aTextColor = getColor( text_color );
        aStyleSet.SetTabRolloverTextColor(aTextColor);
        style_context_restore(pCStyle);
    }

    {
        GtkStyleContext *pCStyle = mpNotebookHeaderTabsTabActiveLabelStyle;
        style_context_save(pCStyle);
        style_context_set_state(pCStyle, ACTIVE_TAB());
        gtk_style_context_get_color(pCStyle, gtk_style_context_get_state(pCStyle), &text_color);
        aTextColor = getColor( text_color );
        aStyleSet.SetTabHighlightTextColor(aTextColor);
        style_context_restore(pCStyle);
    }

    // get cursor blink time