static gboolean gimp_help_menu_item_query_tooltip (GtkWidget *widget, gint x, gint y, gboolean keyboard_mode, GtkTooltip *tooltip) { GtkWidget *vbox; GtkWidget *label; gchar *text; gboolean use_markup = TRUE; text = gtk_widget_get_tooltip_markup (widget); if (! text) { text = gtk_widget_get_tooltip_text (widget); use_markup = FALSE; } if (! text) return FALSE; vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12); label = gtk_label_new (text); gtk_label_set_use_markup (GTK_LABEL (label), use_markup); gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); gtk_label_set_xalign (GTK_LABEL (label), 0.0); gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0); gtk_widget_show (label); g_free (text); label = gtk_label_new (_("Press F1 for more help")); gimp_label_set_attributes (GTK_LABEL (label), PANGO_ATTR_STYLE, PANGO_STYLE_ITALIC, PANGO_ATTR_SCALE, PANGO_SCALE_SMALL, -1); gtk_label_set_xalign (GTK_LABEL (label), 1.0); gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); gtk_widget_show (label); gtk_tooltip_set_custom (tooltip, vbox); return TRUE; }
static void store_populate_menu_items (GtkListStore *store, GtkMenuShell *menu, const gchar *parent_path) { GList *children; GList *node; children = gtk_container_get_children (GTK_CONTAINER (menu)); for (node = children; node; node = node->next) { if (GTK_IS_SEPARATOR_MENU_ITEM (node->data) || ! gtk_widget_get_visible (node->data)) { /* skip that */ } else if (GTK_IS_MENU_ITEM (node->data)) { GtkWidget *submenu; gchar *path; gchar *item_label; gboolean use_underline; GtkStockItem item; if (GTK_IS_IMAGE_MENU_ITEM (node->data) && gtk_image_menu_item_get_use_stock (node->data) && gtk_stock_lookup (gtk_menu_item_get_label (node->data), &item)) { item_label = g_strdup (item.label); use_underline = TRUE; } else { item_label = g_strdup (gtk_menu_item_get_label (node->data)); use_underline = gtk_menu_item_get_use_underline (node->data); } /* remove underlines */ if (use_underline) { gchar *p = item_label; gsize len = strlen (p); while ((p = strchr (p, '_')) != NULL) { len -= (gsize) (p - item_label); memmove (p, p + 1, len); } } if (parent_path) { path = g_strconcat (parent_path, PATH_SEPARATOR, item_label, NULL); } else { path = g_strdup (item_label); } submenu = gtk_menu_item_get_submenu (node->data); if (submenu) { /* go deeper in the menus... */ store_populate_menu_items (store, GTK_MENU_SHELL (submenu), path); } else { gchar *tmp; gchar *tooltip; gchar *label = g_markup_printf_escaped ("<big>%s</big>", item_label); tooltip = gtk_widget_get_tooltip_markup (node->data); if (tooltip) { SETPTR (label, g_strconcat (label, "\n<small>", tooltip, "</small>", NULL)); g_free (tooltip); } tmp = g_markup_escape_text (path, -1); SETPTR (label, g_strconcat (label, "\n<small><i>", tmp, "</i></small>", NULL)); g_free (tmp); gtk_list_store_insert_with_values (store, NULL, -1, COL_LABEL, label, COL_PATH, path, COL_TYPE, COL_TYPE_MENU_ITEM, COL_WIDGET, node->data, -1); g_free (label); } g_free (item_label); g_free (path); } else { g_warning ("Unknown widget type in the menu: %s", G_OBJECT_TYPE_NAME (node->data)); } } g_list_free (children); }
int main (int argc, char *argv[]) { GtkWidget *window; GtkWidget *box; GtkWidget *drawing_area; GtkWidget *button; GtkWidget *label; GtkWidget *tooltip_window; GtkWidget *tooltip_button; GtkWidget *tree_view; GtkTreeViewColumn *column; GtkWidget *text_view; GtkTextBuffer *buffer; GtkTextIter iter; GtkTextTag *tag; gchar *text, *markup; gtk_init (&argc, &argv); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW (window), "Tooltips test"); gtk_container_set_border_width (GTK_CONTAINER (window), 10); g_signal_connect (window, "delete_event", G_CALLBACK (gtk_main_quit), NULL); box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 3); gtk_container_add (GTK_CONTAINER (window), box); /* A check button using the tooltip-markup property */ button = gtk_check_button_new_with_label ("This one uses the tooltip-markup property"); gtk_widget_set_tooltip_text (button, "Hello, I am a static tooltip."); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); text = gtk_widget_get_tooltip_text (button); markup = gtk_widget_get_tooltip_markup (button); g_assert (g_str_equal ("Hello, I am a static tooltip.", text)); g_assert (g_str_equal ("Hello, I am a static tooltip.", markup)); g_free (text); g_free (markup); /* A check button using the query-tooltip signal */ button = gtk_check_button_new_with_label ("I use the query-tooltip signal"); g_object_set (button, "has-tooltip", TRUE, NULL); g_signal_connect (button, "query-tooltip", G_CALLBACK (query_tooltip_cb), NULL); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); /* A label */ button = gtk_label_new ("I am just a label"); gtk_label_set_selectable (GTK_LABEL (button), FALSE); gtk_widget_set_tooltip_text (button, "Label & and tooltip"); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); text = gtk_widget_get_tooltip_text (button); markup = gtk_widget_get_tooltip_markup (button); g_assert (g_str_equal ("Label & and tooltip", text)); g_assert (g_str_equal ("Label & and tooltip", markup)); g_free (text); g_free (markup); /* A selectable label */ button = gtk_label_new ("I am a selectable label"); gtk_label_set_selectable (GTK_LABEL (button), TRUE); gtk_widget_set_tooltip_markup (button, "<b>Another</b> Label tooltip"); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); text = gtk_widget_get_tooltip_text (button); markup = gtk_widget_get_tooltip_markup (button); g_assert (g_str_equal ("Another Label tooltip", text)); g_assert (g_str_equal ("<b>Another</b> Label tooltip", markup)); g_free (text); g_free (markup); /* Another one, with a custom tooltip window */ button = gtk_check_button_new_with_label ("This one has a custom tooltip window!"); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); tooltip_window = gtk_window_new (GTK_WINDOW_POPUP); tooltip_button = gtk_label_new ("blaat!"); gtk_container_add (GTK_CONTAINER (tooltip_window), tooltip_button); gtk_widget_show (tooltip_button); gtk_widget_set_tooltip_window (button, GTK_WINDOW (tooltip_window)); g_signal_connect (button, "query-tooltip", G_CALLBACK (query_tooltip_custom_cb), NULL); g_object_set (button, "has-tooltip", TRUE, NULL); /* An insensitive button */ button = gtk_button_new_with_label ("This one is insensitive"); gtk_widget_set_sensitive (button, FALSE); g_object_set (button, "tooltip-text", "Insensitive!", NULL); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 0); /* Testcases from Kris without a tree view don't exist. */ tree_view = gtk_tree_view_new_with_model (create_model ()); gtk_widget_set_size_request (tree_view, 200, 240); gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view), 0, "Test", gtk_cell_renderer_text_new (), "text", 0, NULL); g_object_set (tree_view, "has-tooltip", TRUE, NULL); g_signal_connect (tree_view, "query-tooltip", G_CALLBACK (query_tooltip_tree_view_cb), NULL); g_signal_connect (gtk_tree_view_get_selection (GTK_TREE_VIEW (tree_view)), "changed", G_CALLBACK (selection_changed_cb), tree_view); /* Set a tooltip on the column */ column = gtk_tree_view_get_column (GTK_TREE_VIEW (tree_view), 0); gtk_tree_view_column_set_clickable (column, TRUE); g_object_set (gtk_tree_view_column_get_button (column), "tooltip-text", "Header", NULL); gtk_box_pack_start (GTK_BOX (box), tree_view, FALSE, FALSE, 2); /* And a text view for Matthias */ buffer = gtk_text_buffer_new (NULL); gtk_text_buffer_get_end_iter (buffer, &iter); gtk_text_buffer_insert (buffer, &iter, "Hello, the text ", -1); tag = gtk_text_buffer_create_tag (buffer, "bold", NULL); g_object_set (tag, "weight", PANGO_WEIGHT_BOLD, NULL); gtk_text_buffer_get_end_iter (buffer, &iter); gtk_text_buffer_insert_with_tags (buffer, &iter, "in bold", -1, tag, NULL); gtk_text_buffer_get_end_iter (buffer, &iter); gtk_text_buffer_insert (buffer, &iter, " has a tooltip!", -1); text_view = gtk_text_view_new_with_buffer (buffer); gtk_widget_set_size_request (text_view, 200, 50); g_object_set (text_view, "has-tooltip", TRUE, NULL); g_signal_connect (text_view, "query-tooltip", G_CALLBACK (query_tooltip_text_view_cb), tag); gtk_box_pack_start (GTK_BOX (box), text_view, FALSE, FALSE, 2); /* Drawing area */ drawing_area = gtk_drawing_area_new (); gtk_widget_set_size_request (drawing_area, 320, 240); g_object_set (drawing_area, "has-tooltip", TRUE, NULL); g_signal_connect (drawing_area, "draw", G_CALLBACK (drawing_area_draw), NULL); g_signal_connect (drawing_area, "query-tooltip", G_CALLBACK (query_tooltip_drawing_area_cb), NULL); gtk_box_pack_start (GTK_BOX (box), drawing_area, FALSE, FALSE, 2); button = gtk_label_new ("Custom tooltip I"); label = gtk_label_new ("See, custom"); g_object_ref_sink (label); g_object_set (button, "has-tooltip", TRUE, NULL); g_signal_connect (button, "query-tooltip", G_CALLBACK (query_tooltip_label_cb), label); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 2); button = gtk_label_new ("Custom tooltip II"); label = gtk_label_new ("See, custom, too"); g_object_ref_sink (label); g_object_set (button, "has-tooltip", TRUE, NULL); gtk_box_pack_start (GTK_BOX (box), button, FALSE, FALSE, 2); g_signal_connect (button, "query-tooltip", G_CALLBACK (query_tooltip_label_cb), label); /* Done! */ gtk_widget_show_all (window); gtk_main (); return 0; }