PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 520: How to get the position of a label or eventbox?
Written by kksou   
Tuesday, 12 August 2008
Problem

This is in response to Neil's post titled "Get co-ordnates".

He would like to find the position (x,y co-ordinates) of an eventbox, or a label as shown below:

How to get the position of a label or eventbox?


Solution

Note: I find that it only works when you stuff the label in an eventbox. If you just use a GtkLabel without GtkEventBox, for some reason, the value does not seem to be correct.


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   
<?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("Get the position of a label or eventbox");
$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);

$vbox->pack_start(new GtkLabel());

$vbox->pack_start($hbox = new GtkHBox());
$hbox->pack_start(new GtkLabel());
$label = new GtkLabel('this is the label'); // note 1
$hbox->pack_start($eventbox = new GtkEventBox, 0); // note 2
$eventbox->add($label); // note 2
$eventbox->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#aaaaaa"));
$hbox->pack_start(new GtkLabel());
Explanation
  1. This is a sample label.
  2. Create an eventbox and stuff the label in the eventbox.
  3. Get the co-ordinates of the label.
  4. Output the values to the command window.

User reviews

There are no user reviews yet.

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

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2008. kksou.com. All Rights Reserved