PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou




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:


    Warning: file_get_contents(/var/www/html/php-gtk2/thoughts/php/0033.4.php) [function.file-get-contents]: failed to open stream: No such file or directory in /home/fann/kksou.com/php-gtk2/util/phpgtk2_lib_t34.php(1250) : eval()'d code on line 1
    
    

    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 >

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