Problem
In Part 1, I've showed you how to display tooltip as plain text on a GtkButton.
In this Part 2, instead of plain text, we will display the tooltip as markup text as shown below:
Key method used: GtkTooltip::set_markup().
Solution
- The setup of tooltip is exactly the same as that of Part 1.
- The only difference is that instead of using
GtkTooltip::set_text()
, we useGtkTooltip::set_markup()
to display the markup text. - Note that the markup follows that of the Pango Markup Language. I've written a quick reference guide for the Pango Markup Language here.
Important Note: This only works for PHP-GTK v2.0 (or PHP-GTK2 compliled with gtk+ v2.12 and above. If you are using an older version, for linux, you may follow the step-by-step instructions to recompile php-gtk2 with gtk+ v2.12. For windows, please refer to How to install php gtk2 on windows?
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 30 31 32 33 34 35 36 37 38 39 41 43 44 45 46 47 48 | <?php $window = new GtkWindow(); $window->set_title($argv[0]); $window->connect_simple('destroy', array( 'Gtk', 'main_quit')); $window->set_size_request(400,150); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Display tooltip on GtkButton using GtkTooltip\n". " Part 2 - display tooltip with pango markup text"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 60); $vbox->pack_start($title, 0, 0); $vbox->pack_start($hbox=new GtkHBox(), 0, 0); create_button($hbox, 'Blue'); create_button($hbox, 'Green'); create_button($hbox, 'Yellow'); $window->show_all(); Gtk::main(); function create_button($hbox, $button_label) { $button = new GtkButton($button_label); $button->set_size_request(80, 32); $hbox->pack_start($button, 1, 0); $button->connect('clicked', "on_button", $button_label); $button->set_property('has-tooltip', true); // note 1 $button->connect('query-tooltip', 'on_tooltip'); // note 2 } function on_button($button, $button_label) { echo "You have clicked: $button_label!\n"; } function on_tooltip($widget, $x, $y, $keyboard_mode, $tooltip) { $label = $widget->get_label(); $tooltip->set_markup("this is tooltip for <i>button</i>: ". "<span font_desc=\"Times New Roman Bold Italic 14\" foreground=\"$label\">$label</span>"); // note 3 return true; } ?> |
Output
As shown above.
Explanation
- Enable tooltip on the widget.
- Connect to the signal
'query-tooltip'
. - Display the tooltip as markup text.
Related Links
- How to display tooltips in GtkTreeView - Part 4?
- How to display tooltip on buttons using GtkTooltip - Part 1 - plain text?
- How to display tooltip on buttons using GtkTooltip - Part 3 - tooltip with image?
- How to display tooltip on buttons using GtkTooltip - Part 4 - tooltip with markup text and image?
- How to display tooltip on buttons using GtkTooltip - Part 5 - tooltip with image and label below image?
- How to display tooltip on buttons using GtkTooltip - Part 6 - tooltip with GtkTable?
Read more...