Problem
You want to display tooltips for items in a table as shown below:
Solution
- Create a new GtkTooltips first.
- Then add each tooltip to the corresponding widget with GtkTooltips::set_tip().
- For "transparent" widgets such as GtkLabel, put them in a GtkEventBox first before using GtkTooltips::set_tip()
Sample Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 40 41 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | <?php $window = new GtkWindow(); $window->set_size_request(400, 200); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->add($vbox = new GtkVBox()); // display title $title = new GtkLabel("Add tooltips to items in a table"); $title->modify_font(new PangoFontDescription("Times New Roman Italic 10")); $title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff")); $title->set_size_request(-1, 40); $vbox->pack_start($title, 0, 0); $vbox->pack_start(new GtkLabel(), 0, 0); //add a small gap $tooltips = new GtkTooltips(); // note 1 $vbox->pack_start($hbox = new GtkHBox()); $hbox->pack_start(new GtkHBox()); $hbox->pack_start($table = new GtkTable(), 0, 0); $hbox->pack_start(new GtkHBox()); $data = array( array('', 'header1', 'header2', 'header3'), array('row0', 1, 2, 3), array('row1', 4, 5, 6), array('row2', 7, 8, 9), array('row3', 10, 11, 12), array('row4', 13, 14, 15)); display_table ($table, $data); function display_table($table, $a) { global $tooltips; // note 1 for ($row=0; $row<count($a); ++$row) { for ($col=0; $col<count($a[$row]); ++$col) { $frame = new GtkFrame(); $eventbox = new GtkEventBox(); $frame->add($eventbox); $eventbox->add(new GtkLabel($a[$row][$col])); if ($row==0) { $eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#FFCC66")); } elseif ($row%2==0) { $eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#CCFF99")); } $table->attach($frame, $col, $col+1, $row, $row+1, Gtk::FILL, Gtk::SHRINK, 0, 0); // add tooltips $tooltips->set_tip($eventbox, "this is row $row col $col"); // note 2 } } } $window->show_all(); Gtk::main(); ?> |
Output
As shown above.Explanation
The above example is based on How to display a 2D array in table - Part 4?.
What's new here:
- Note that we declare GtkTooltips in the global level. If you declare it inside the
function display_table()
, you will find that the tooltip won't be displayed. - Don't forget to place the label in the eventbox before adding the tooltip to the eventbox with GtkTooltips::set_tip().
Read more...