Dial widget - a graphical widget to display an analog value.

Dial widget The Dial widget is a graphical object which displays an analog value. This is a direct port of the Gtk tutorial about creating graphical widgets

Details

The Dial widget can display a value from -120 to 120. This could be changed, just write the right mathematical operations (I'm a bit lazy, guys). This widget can draw directly into a Window (direct drawing) or draw into a pixmap, which we can then refresh as needed ; just look at the class source code to see how it is done.

Direct draw is much faster for large windows ; I can't see differences for small ones. The actual graphical part (drawing the dial) is a php-gtk port from the Gtk tutorial. This widget is a good start when you need to build dynamic graphical widgets like this one.

Example of use

<?php
$dial
= new Dial();  # (1)
$dial->set_size_request(80, 80);
$dial->set_value(0);

$window = new GtkWindow();
$window->connect_simple('destroy', array('gtk', 'main_quit'));
$window->set_title('Dial demo');
$window->set_position(Gtk::WIN_POS_CENTER);
$window->set_border_width(8);
$window->add($dial); # (2)
$window->show_all();

Gtk::timeout_add(100, 'dial_timeout', $dial);  # (3)

Gtk::main();

/**
* a timeout function to change the dial value from -120 to 120
* giving an animation to the widget.
*/
function dial_timeout($dial)
  {
  static
$value = 0;
  static
$increment = 4;

  if (
$value < -120)
   
$increment *= -1 ;

  if (
$value > 120)
   
$increment *= -1;

 
$value += $increment;
 
 
# $dial->set_value($value / 180*PI);
 
$dial->set_value($value);  # (4)
   
 
Gtk::timeout_add(20, 'dial_timeout', $dial);
  return
FALSE;
  }
?>

Highlights

  1. create a dial widget,
  2. add this widget to a composite widget (any composite can be used)
  3. setup a timeout function to update the dial value
  4. set the dial value with $dial->set_value($val)

Full source code

See the attached sources file below

AttachmentSize
gtk-drawing-area-dial.php_.txt6.31 KB