Changing bakground color and foreground color of tooltip

HI!
I’m developing an application on UBUNTU 20.04 using GTK3 + GLADE.
In Glade, where is necessary I’ve write tooltip in widgets.
I know that to manage color in GTK3 css are used.
What I would like is to change the background and foreground color of tooltips.
At the moment I used this css:

tooltip {
background: red;
color: blue;
}

and the background is red but the text is white.

Please could you help me to solve my issue.

Tank you in Advance

I think what you want is a custom tooltip. There is also a whole custom tooltip window thing but that may be a bit overkill for what you want. I’ve achieved this with Pango markup
image

Code is just hello world with a few extra lines;

#include <gtk/gtk.h>

gboolean
query_tooltip (
  GtkWidget* self,
  gint x,
  gint y,
  gboolean keyboard_mode,
  GtkTooltip* tooltip,
  gpointer user_data
){
  gtk_tooltip_set_markup(tooltip, "<span foreground=\"blue\" background=\"red\">Blue text over red background</span>");
  return TRUE;
}

static void
activate (GtkApplication *app,
          gpointer        user_data)
{
  GtkWidget *window;
  GtkWidget *button;
  GtkWidget *button_box;

  window = gtk_application_window_new (app);
  gtk_window_set_title (GTK_WINDOW (window), "Window");
  gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);

  button_box = gtk_button_box_new (GTK_ORIENTATION_HORIZONTAL);
  gtk_container_add (GTK_CONTAINER (window), button_box);

  button = gtk_button_new_with_label ("Hello World");
  gtk_container_add (GTK_CONTAINER (button_box), button);
  
/*
 * Set "has-tooltip" to true and connect it's signal
 */
  gtk_widget_set_has_tooltip(button, TRUE);
  g_signal_connect(G_OBJECT(button), "query-tooltip", G_CALLBACK(query_tooltip), NULL);

  gtk_widget_show_all (window);
}

int
main (int    argc,
      char **argv)
{
  GtkApplication *app;
  int status;

  app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
  g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
  status = g_application_run (G_APPLICATION (app), argc, argv);
  g_object_unref (app);

  return status;
}

Try this:

tooltip {
	background: red;
}
tooltip * {
	color: blue;
}

Thank you for your kind answer and the code.
Probably my request was not well described.
I did not write functions for tooltip, but I write them in Glade.
In any case Tank you again.

Thank you!
Your suggestion solve the issue.
Because your name is Italian:
Grazie!
Buona Pasqua!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.