246. How to use GtkSheet - Part 1 - create the spreadsheet?

Problem

You would like to set up a spreadsheet in PHP-GTK2 as shown below:

How to use GtkSheet - Part 1 - create the spreadsheet?


Solution

  • Make sure your version of PHP-GTK2 has the GtkExtra library, and the feature turned on in php.ini. (See notes below.)
  • Create a new spreadsheet with GtkSheet($num_rows, $num_cols, $title)
  • The GtkSheet is used just like a standard Gtk widget.
  • You can turn on autoresize using GtkSheet::set_autoresize(boolean)

Important Note: This only works for PHP-GTK2 compliled with the additional library GtkExtra. For linux, you can download the files from http://gtkextra.sourceforge.net/ and do a recompile. For windows, you may use the builds by Elizabeth Smith or the official php-gtk2 beta release available at http://gtk.php.net/download.php. Both contain all the required gtkextra libraries and dll's. In the php.ini, don't forget to add php-gtk.extensions = php_gtk_extra2.dll to turn on GtkExtra.


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
19   
20   
21   
22   
23   
24   
25   
26   
27   
28   
31   
32   
33   
34   
35   
36   
<?php

$window = new GtkWindow();
$window->set_title($argv[0]);
$window->set_size_request(400, 250);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Using GtkSheet\n".
"Part 1 - create the spreadsheet");
$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);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment, 0, 0);
$vbox->pack_start(new GtkLabel(), 0, 0);

$scrolled_win = new GtkScrolledWindow();
$scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC,
Gtk::POLICY_AUTOMATIC);
$vbox->pack_start($scrolled_win);

$sheet = new GtkSheet(6, 12, 'spreadsheet example1'); // note 1
$sheet->set_autoresize(1); // note 2
$scrolled_win->add($sheet); // note 3

$window->show_all();
Gtk::main();

?>

Output

As shown above.
 

Explanation

  1. Create a new spreadsheet with 6 rows and 12 columns.
  2. Turn on auto-resize. Try typing some long text in a cell. You will see what this means.
  3. Stuff the spreadsheet in a GtkScrolledWindow

Note

I find that when the above example is first run, and you immediately start typing something, nothing will appear. Only when you move the cursor to another cell with the mouse or keyboard, then the spreadsheet will start working.

Is it the same on your pc too?

Maybe it's a bug...

Related Links

Add comment


Security code
Refresh