GtkStatusbar Constructor

GtkStatusbar Constructor

Creating a status bar is very straightforward; creating and updating messages marginally less so.

Example 41. Setting up a status bar.

<?php

dl('php_gtk.' . (strstr(PHP_OS, 'WIN') ? 'dll' : 'so'));

/* function to clear the entry field and give it keyboard focus */
function focusback($entry) {
  $entry->set_text('');
  $entry->grab_focus();
  }

/* on this occasion, the update function *is* the process */
function change_status($entry, $event, $status, $stuff, &$i) {
  /* pick up the value(s) you are intending to use */
  $value = $event->keyval;
  $string = $event->string;
  /* prevent the message stack building up */
  $popcontext = $status->get_context_id($stuff[$i]);
  $status->pop($popcontext);
  /* sort according to value */
  switch($value) {
    case ($value >= GDK_KEY__a && $value <= GDK_KEY__z):
      $i = 1;
      break;
    case ($value >= GDK_KEY_A && $value <= GDK_KEY_Z):
      $i = 2;
      break;
    case ($value >= GDK_KEY_0 && $value <= GDK_KEY_9):
      $i = 3;
      break;
    case GDK_KEY_Return:
      $i = 4;
      break;
    case GDK_KEY_space:
      $i = 5;
      break;
    default:
      $i = 6;
    }

  /* create and push the new message according to the value */
  $pushcontext = $status->get_context_id($stuff[$i]);
  if($string && $string!==' ') 
  $status->push($pushcontext, $string.' is a '.$stuff[$i].' character');
  else $status->push($pushcontext, $stuff[$i]);
  /* call any other function the value triggers */
  if($i == 4) focusback($entry);
}

/* set up the main window */
$window = &new GtkWindow();
$window->set_position(GTK_WIN_POS_CENTER);
$window->connect_object('destroy', array('gtk', 'main_quit'));

/* if you want anything else besides the statusbar, you'll need a box */
$vbox = &new GtkVBox();
$window->add($vbox);

/* add the statusbar first, else connecting other widgets to it is messy */
$status = &new GtkStatusbar();
$stuff = array('Here we go...', 'lower case alpha', 'UPPER CASE ALPHA', 
'numeric', 'return', 'spacebar', 'non-alphanumeric');
$status->push($status->get_context_id($stuff[0]), $stuff[0]);
$vbox->pack_end($status, false);
$status->show();

/* create, connect and pack your other widget(s) */
$entry = &new GtkEntry();
$entry->set_usize(400,20);
/* this will usually be connect_object(). $entry is only passed here because
   it is used as a parameter in a function called from the callback.  &$i
   is passed so that the correct message will get 'popped' from the stack */
$i = 1;
$entry->connect('key-press-event','change_status', $status, $stuff, 
&$i);
$vbox->pack_start($entry, false);
$entry->show();

$window->show_all();

focusback($entry);
gtk::main();

?>

© Copyright 2003-2023 www.php-editors.com. The ultimate PHP Editor and PHP IDE site.