com gtk/php-gtk: Little image browser: demos/imgbrowser.php

[email protected] (David Soria Parra) Thu, 06 Apr 2006 06:43:35 +0000
Newsgroups php.gtk.cvs
Message-ID <[email protected]>
Commit:    3987fa934ca04ccca53612fde988b03be51b4cb0
Author:    Christian Weiske <[email protected]>         Thu, 6 Apr 2006 06:43:35 +0000
Parents:   ffcfb176d415997369e4f34a097230c292aee283
Branches:  master

Link:       http://git.php.net/?p=gtk/php-gtk.git;a=commitdiff;h=3987fa934ca04ccca53612fde988b03be51b4cb0

Log:
Little image browser

Changed paths:
  A  demos/imgbrowser.php


Diff:
3987fa934ca04ccca53612fde988b03be51b4cb0
diff --git a/demos/imgbrowser.php b/demos/imgbrowser.php
new file mode 100644
index 0000000..d2400ff
--- /dev/null
+++ b/demos/imgbrowser.php
@@ -0,0 +1,128 @@
+<?php
+
+
+class ImgBrowser extends GtkWindow
+{
+    var $img = null;
+
+
+
+    function __construct($parent = null)
+    {
+        parent::__construct();
+
+        if (@$GLOBALS['framework']) {
+            return;
+        }
+
+        if ($parent) {
+            $this->set_screen($parent->get_screen());
+        } else {
+            $this->connect_simple('destroy', array('Gtk', 'main_quit'));
+        }
+
+        $this->set_title(__CLASS__);
+        $this->set_position(Gtk::WIN_POS_CENTER);
+        $this->set_default_size(500, 400);
+
+        $this->add($this->__create_box());
+        $this->show_all();
+    }
+
+
+
+    function __create_box()
+    {
+        $box = new GtkVBox();
+        $box->pack_start(new GtkLabel('Double click with the mouse on a file, or select by key and press return'), false);
+
+        $paned = new GtkHPaned();
+        $paned->set_position(200);
+
+        //filename, fullpath, is_dir
+        $mFile = new GtkListStore(Gtk::TYPE_STRING, Gtk::TYPE_STRING, Gtk::TYPE_BOOLEAN);
+        $vFile = new GtkTreeView($mFile);
+        $vFile->append_column(
+            new GtkTreeViewColumn(
+                'Files',
+                new GtkCellRendererText(),
+                'markup',
+                0
+            )
+        );
+        $vFile->connect('key-press-event', array($this, 'onPressFile'));
+        $vFile->connect('button-press-event', array($this, 'onPressFile'));
+        $this->loadFiles($mFile, getcwd());
+
+        $scrwndFiles = new GtkScrolledWindow();
+        $scrwndFiles->add($vFile);
+        $scrwndFiles->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
+
+        $paned->add1($scrwndFiles);
+
+
+        $this->img = new GtkImage();
+
+        $scrwndImg = new GtkScrolledWindow();
+        $scrwndImg->add_with_viewport($this->img);
+        $scrwndImg->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
+        $paned->add2($scrwndImg);
+
+        $box->pack_end($paned);
+        return $box;
+    }
+
+
+
+    function loadFiles($mFile, $strDir)
+    {
+        $hdl = dir($strDir);
+        $mFile->clear();
+        while (false !== ($file = $hdl->read())) {
+            $path = $hdl->path . DIRECTORY_SEPARATOR . $file;
+            if (is_dir($path) && $file !== '.') {
+                $mFile->append(array('<span color="#00F">' . $file . '</span>', $path, true));
+            } else if (substr($path, -4) == '.png' || substr($path, -4) == '.jpg') {
+                $mFile->append(array($file, $path, false));
+            }
+        }
+    }
+
+
+
+    function onPressFile($vFile, $event)
+    {
+        if (
+            ($event->type == Gdk::KEY_PRESS && $event->keyval == Gdk::KEY_Return)
+         || ($event->type == Gdk::_2BUTTON_PRESS && $event->button == 1)
+        ) {
+            list($model, $iter) = $vFile->get_selection()->get_selected();
+            if ($iter !== null) {
+                $path = $model->get_value($iter, 1);
+                if ($model->get_value($iter, 2)) {
+                    $this->loadFiles($model, $path);
+                } else {
+                    $this->showImage($path);
+                }
+            }
+        }
+    }
+
+
+
+    function showImage($path)
+    {
+        $this->img->set_from_file($path);
+    }
+
+}
+
+$GLOBALS['class']       = 'ImgBrowser';
+$GLOBALS['description'] = 'Uses GdkPixbuf and GtkIconView to show the list of images in folders
+of your hard disk.';
+
+if (!@$GLOBALS['framework']) {
+    new ImgBrowser();
+    Gtk::main();
+}
+?>
\ No newline at end of file