Migration to the new PHP-GTK v2.0 release
Written by kksou   
Wednesday, 05 March 2008

I have migrated a considerable amount of codes to the new PHP-GTK v2.0 release.

Here are some tips and tricks I've found along the way...

  1. If you use any GtkTreeStore or GtkListStore, change the following constants:
    • Gtk::TYPE_STRING to GObject::TYPE_STRING
    • Gtk::TYPE_LONG to GObject::TYPE_LONG
    • Gtk::TYPE_DOUBLE to GObject::TYPE_DOUBLE

  2. If you need to ensure that your codes can be run with all versions of php-gtk2 (including alpha and beta), you can do something like:

  3. if (defined("GObject::TYPE_STRING")) {
        $model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING,
            GObject::TYPE_LONG, GObject::TYPE_DOUBLE);
    } else {
        $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING,
            Gtk::TYPE_LONG, Gtk::TYPE_DOUBLE);
    }
    

    or

    if (Gtk::check_version(2, 12, 0) == null)) {
        $model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING,
            GObject::TYPE_LONG, GObject::TYPE_DOUBLE);
    } else {
        $model = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING,
            Gtk::TYPE_LONG, Gtk::TYPE_DOUBLE);
    }
    
  4. Some of the methods in the new PHP-GTK v2.0 release is very sensitive to type. For example, for the method
  5. GtkTreeViewColumn::set_alignment(double xalign);
    

    For left alignment, you have to put a 0.0. For right alignment, it's 1.0. Try putting a 0 or 1. You will get an error. (For the alpha and beta versions, it's works with 0 and 1.)

  6. This is a very tricky, but important one: Sometimes the error messages given do not correspond to the actual cause of the problem.
  7. To illustrate the case, I managed to replicate the problem faced by Matt in his post titled GtkCellLayout::set_cell_data_func issue in Gtk2. Try the code below:

    <?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("Display 2D Array in GtkTreeView - Part 5\n".
    "                       get user selection");
    $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);
    
    // the 2D table
    $data = array(
    array('row0', 'item 42', 2, 3.1),
    array('row1', 'item 36', 20, 6.21),
    array('row2', 'item 21', 8, 9.36),
    array('row3', 'item 10', 11, 12.4),
    array('row4', 'item 7', 5, 15.5),
    array('row5', 'item 4', 17, 18.6),
    array('row6', 'item 3', 20, 21.73));
    
    display_table($vbox, $data);
    
    $window->show_all();
    Gtk::main();
    
    function display_table($vbox, $data) {
    
        // Set up a scroll window
        $scrolled_win = new GtkScrolledWindow();
        $scrolled_win->set_policy( Gtk::POLICY_AUTOMATIC,
            Gtk::POLICY_AUTOMATIC);
        $vbox->pack_start($scrolled_win);
    
        // Creates the list store
        $model = new GtkListStore(GObject::TYPE_STRING, GObject::TYPE_STRING,
                    GObject::TYPE_LONG, GObject::TYPE_DOUBLE);
        $field_header = array('Row #', 'Description', 'Qty', 'Price');
        $field_justification = array(0, 0, 0.5, 1); // <-- line 43 the offending statement!
        #$field_justification = array(0.0, 0.0, 0.5, 1.0);
    
        // Creates the view to display the list store
        $view = new GtkTreeView($model);
        $scrolled_win->add($view);
    
        // Creates the columns
        for ($col=0; $col<count($field_header); ++$col) {
            $cell_renderer = new GtkCellRendererText();
            $z = $field_justification[$col];
            $cell_renderer->set_property("xalign", $field_justification[$col]); // line 54
            $column = new GtkTreeViewColumn($field_header[$col],
                $cell_renderer, 'text', $col);
           	echo "col = $col\n"; // line 57
            $z = $field_justification[$col];
            $column->set_alignment($field_justification[$col]);
            $column->set_sort_column_id($col);
    
            // set the header font and color
            $label = new GtkLabel($field_header[$col]);
            $label->modify_font(new PangoFontDescription("Arial Bold"));
            $label->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000FF"));
            $column->set_widget($label);
            $label->show();
    
            // setup self-defined function to display alternate row color
            $view->append_column($column);
            $column->set_cell_data_func($cell_renderer, 'format_col', $col);
        }
    
        // pupulates the data
        for ($row=0; $row<count($data); ++$row) {
            $values = array();
            for ($col=0; $col<count($data[$row]); ++$col) {
                $values[] = $data[$row][$col];
            }
            $model->append($values);
        }
    
        $selection = $view->get_selection(); // note 1
        $selection->connect('changed', 'on_selection'); // note 2
    }
    
    // self-defined function to format the price column
    function format_col($column, $cell, $model, $iter, $col_num) {
       $path = $model->get_path($iter); // get the current path
        $row_num = $path[0]; // get the row number
        if ($col_num==3) {
            $amt = $model->get_value($iter, 3);
            $cell->set_property('text', '$'.number_format($amt,2));
        }
        $row_color = ($row_num%2==1) ? '#dddddd' : '#ffffff';
        $cell->set_property('cell-background', $row_color);
    }
    
    // the function that is called when user selects a row
    function on_selection($selection) {
        list($model, $iter) = $selection->get_selected(); // note 3
        $desc = $model->get_value($iter, 1); // note 4
        $qty = $model->get_value($iter, 2); // note 4
        $price = $model->get_value($iter, 3); // note 4
        print "You have selected $desc: $qty ($price)\n";
    }
    
    ?>
    
    
    
    

    You will get the error messages:

    Warning: Unable to invoke callback '' specified in test1.php on line 71 in test1.php on line 29
    

    Note: the code above is a slight modification from the familiar GtkTreeView sample code: Sample Code 33: How to display a 2D array in GtkTreeView - Part 5 - get user selection

    Would you want to guess which is the offending statement causing the error? It's line 43!

    $field_justification = array(0, 0, 0.5, 1);
    

    Interesting, right? The error message is totally unrelated! If you change the above to:

    $field_justification = array(0.0, 0.0, 0.5, 1.0);
    

    The code runs ok now!

    If you reset the above statement to: $field_justification = array(0, 0, 0.5, 1), then comment out line 57:

    #echo "col = $col\n";
    

    Now you get another error message:

    Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate
     1667326510 bytes) in 033.4.php on line 28
    

    To confuse you further, now comment out line 54:

    #$cell_renderer->set_property("xalign", $field_justification[$col]);
    

    Ha, the program now runs ok! Even though we're using integer and not double in line 43: $field_justification = array(0, 0, 0.5, 1)

    Conclusion: Think simple. The PHP-GTK v2.0 release does not change much. And out of the many programs that I've migrated, I have not encountered any strange bugs yet. Take a look at the error message. Use that as a reference, but don't rely entirely on it.

  8. Will add a couple more in the next few days...


  9. 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 >

Copyright © 2006-2008. kksou.com. All Rights Reserved