Handle Array Structure in php gtk

|
In this post, we display a tree structure from array in php gtk, for do this please follow below points:

1.) Sets up the treestore to store the data. Here it sets three fields. The first one is integer that stores the id. The second and third are strings.

2.) Creates a GtkTreeview. It is here that you bind the model to the treeview.

3.) Sets up the treeview columns.

4.) Loops through the data list and appends the data one by by to the tree structure. Note that $nodes stores the GtkTreeiter of each node. You can view this as a pointer to the node.


<?php
/* 
PHP GTK tutorials
website: http://phpgtktutorials.blogspot.in/
*/




$window = new GtkWindow();
$window->set_size_request(500, 250);
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// window title
$title = new GtkLabel("Handle Array Structure in php gtk");
$title->modify_font(new PangoFontDescription("Arial 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);

$data = array(
    array ('id' => '101', 'parent' => '0', 'data' => 'value0'),
    array ('id' => '102', 'parent' => '101', 'data' => 'value1'),
    array ('id' => '103', 'parent' => '0', 'data' => 'value2'),
    array ('id' => '104', 'parent' => '0', 'data' => 'value3'),
    array ('id' => '105', 'parent' => '104', 'data' => 'value4'),
    array ('id' => '106', 'parent' => '105', 'data' => 'value5')
);

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
    if (defined("GObject::TYPE_STRING")) {
        $model = new GtkTreeStore(GObject::TYPE_LONG, 
            GObject::TYPE_STRING, GObject::TYPE_STRING); // Point Number 1
    } else {
        $model = new GtkTreeStore(Gtk::TYPE_LONG, 
            Gtk::TYPE_STRING, Gtk::TYPE_STRING); // note 1
    }
    $field_header = array('id', 'title', 'data');

    // Creates the view to display the list store
    $view = new GtkTreeView($model); // Point Number 2
    $scrolled_win->add($view);

    // Creates the columns
    for ($col=0; $colappend_column($column);
    }

    // pupulates the data
    $nodes = array();
    $nodes[0] = null; // root
    foreach($data as $item) {
        $id = $item['id'];
        $parent = $item['parent'];
        $data = $item['data'];
        $nodes[$id] = $model->append($nodes[$parent], 
            array($id, "this is id $id", $data)); // Point Number 4
    }

    $view->expand_all();
}

?>

How to set the font and font size of GtkLabel in PHP GTK

|
If you want to change the font and font size of GtkLabel in PHP GTK 2. may this post is helpful to you.
<?php
/* 
PHP GTK tutorials
website: http://phpgtktutorials.blogspot.in/
*/
$window = new GtkWindow();
$window->set_size_request(500, 300);
$window->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#FFFF00"));
$window->connect_simple('destroy', array('Gtk','main_quit'));
$label = new GtkLabel("hello buddy!");
$label->modify_font(new PangoFontDescription('Times New Roman 48')); // set font style 
$window->add($label);
$window->show_all();
Gtk::main();
?>

Change Background color of GtkWindow in PHP GTK

|
Many People often want to change background color of GtkWindow in PHP GTK. Here is a simple example.

In below example, we use  void modify_bg( GtkStateType state , GdkColor color ); for set background color of GtkWindow. According to PHP-GTK 2 Manual GtkStateType indicates the current state of a widget; the state determines how the widget is drawn. The GtkStateType enumeration is also used to identify different colors in a GtkStyle for drawing, so states can be used for subparts of a widget as well as entire widgets.

For more information follow below links.
GtkStateType
GdkColor

<?php
/* 
PHP GTK tutorials
website: http://phpgtktutorials.blogspot.in/
*/
$window = new GtkWindow();
$window->set_size_request(400, 100);
$window->modify_bg(Gtk::STATE_NORMAL, GdkColor::parse("#FFFF00"));
$window->connect_simple('destroy', array('Gtk','main_quit'));
$label = new GtkLabel("hello buddy!");
$window->add($label);
$window->show_all();
Gtk::main();
?>

Change size of GtkWindow in PHP GTK

|
In this post we use void set_size_request(int width, int height) to change the size of GtkWindow in PHP GTK
<?php
/* 
PHP GTK tutorials
website: http://phpgtktutorials.blogspot.in/
*/
$window = new GtkWindow();
$window->set_size_request(400, 100);

$window->connect_simple('destroy', array('Gtk','main_quit'));
$label = new GtkLabel("PHP GTK Tutorials and Examples!");
$window->add($label);
$window->show_all();
Gtk::main();
?>