GtkToolTip: new API to add css classes to GtkTooltipWindow

we add a simple API to allow styling GtkTooltip's window
by setting a css class on it:

void gtk_tooltip_set_css_class (GtkTooltip *tooltip,
                                const char *css_class);

By passing NULL it will remove any previously set css class
by this function.

Issue #5925
This commit is contained in:
Nelson Benítez León
2023-07-01 21:05:14 -04:00
parent 1f3db35271
commit 1cae0cd54c
4 changed files with 57 additions and 0 deletions

View File

@@ -1065,3 +1065,26 @@ gtk_tooltip_unset_surface (GtkNative *native)
gtk_tooltip_set_surface (tooltip, NULL);
}
/**
* gtk_tooltip_set_css_class:
* @tooltip: a #GtkTooltip
* @css_class: (allow-none): a css class name, or %NULL
*
* This function allows to add a single css class
* to @tooltip window, that means it will remove any
* css class previously added by this function before
* adding @css_class as the currently active one.
*
* if %NULL is passed then any active css class (which
* was added by this function) will be cleared.
*
* Since: 4.12
*/
void
gtk_tooltip_set_css_class (GtkTooltip *tooltip,
const char *css_class)
{
g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
gtk_tooltip_window_set_css_class (GTK_TOOLTIP_WINDOW (tooltip->window), css_class);
}

View File

@@ -56,6 +56,9 @@ void gtk_tooltip_set_custom (GtkTooltip *tooltip,
GDK_AVAILABLE_IN_ALL
void gtk_tooltip_set_tip_area (GtkTooltip *tooltip,
const GdkRectangle *rect);
GDK_AVAILABLE_IN_4_12
void gtk_tooltip_set_css_class (GtkTooltip *tooltip,
const char *css_class);
G_DEFINE_AUTOPTR_CLEANUP_FUNC(GtkTooltip, g_object_unref)

View File

@@ -59,6 +59,7 @@ struct _GtkTooltipWindow
GtkWidget *image;
GtkWidget *label;
GtkWidget *custom_widget;
char *css_class;
};
struct _GtkTooltipWindowClass
@@ -392,6 +393,7 @@ static void
gtk_tooltip_window_init (GtkTooltipWindow *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
self->css_class = NULL;
}
GtkWidget *
@@ -520,3 +522,30 @@ gtk_tooltip_window_position (GtkTooltipWindow *window,
gtk_tooltip_window_relayout (window);
}
/* See gtk_tooltip_set_css_class() description */
void
gtk_tooltip_window_set_css_class (GtkTooltipWindow *window,
const char *css_class)
{
GtkWidget *wid = GTK_WIDGET (window);
if (g_strcmp0 (window->css_class, css_class) == 0)
return;
if (css_class)
{
if (window->css_class)
{
gtk_widget_remove_css_class (wid, window->css_class);
g_free (window->css_class);
}
window->css_class = g_strdup (css_class);
gtk_widget_add_css_class (wid, css_class);
}
else
{
gtk_widget_remove_css_class (wid, window->css_class);
g_clear_pointer (&window->css_class, g_free);
}
}

View File

@@ -59,6 +59,8 @@ void gtk_tooltip_window_position (GtkTooltipWindo
GdkAnchorHints anchor_hints,
int dx,
int dy);
void gtk_tooltip_window_set_css_class (GtkTooltipWindow *window,
const char *css_class);
G_END_DECLS