PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 462: How to display tooltip on buttons using GtkTooltip - Part 1 - plain text?
Written by kksou   
Wednesday, 26 March 2008
Problem

GtkTooltip is a new widget introduced in gtk+ 2.12. It was intended to replace the other widget GtkTooltips.

We'll learn about the new GtkTooltip through a series of examples. Theoretically, GtkTooltip works on any GtkWidget. I used GtkButton in this example. But you can try it on other widgets such as GtkLabel and GtkImage.

In this Part 1, I will show how to display tooltip as plain text on a GtkButton as shown below.

Key method used: GtkTooltip::set_text().

How to display tooltip on buttons using GtkTooltip - Part 1 - plain text?


Solution

Showing tooltip using GtkTooltip is a simple 3-step process:

  • Enable tooltip on the widget with GtkWidget::set_property('has-tooltip', true);
  • Connect to the signal 'query-tooltip'.
  • Display the tooltip in the signal handler. There are quite a number of ways to display the tooltip. In this Part 1, we use GtkTooltip::set_text() to display the tooltip as plain text.

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   
29   
30   
<?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 1 - display tooltip with only plain 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
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
Explanation
  1. Enable tooltip on the widget.
  2. Connect to the signal 'query-tooltip'.
  3. Display the tooltip as plain text.

Related Links

User reviews   Average user ratings:    5.0   (from 1 user)
  1. ecc
    May 18, 2008 5:58am

    Hi,
    it is also possible to add an tooltip to an widget without an callback function? For plain text tooltips, this looks a little bit oversized if you have added tooltips!

    Btw. Another question. How to prevent double signal connects if you update an widget state?
    Im searching for something like connect_signal_once :-)

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Copyright © 2006-2008. kksou.com. All Rights Reserved