Version: 0.5
Type: Sample Code (HOWTO)
Category: Other
License: GNU General Public License
Description: This is a very simple random number generator (rolling dice) written in gtk-php. Unfortunately I have not been able do put the output back into the GUI, any help would be welcome! From this you can figure out how to make a simple GTK+ script in PHP
#!/usr/bin/php4 -q
<?
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* Produced by Patrik Olterman
* patrik@olterman.pp.se
*
*/
// This little snippet checks if the libs are loaded or not, and loads them!
// it checks if the OS is stupid windows or if its something cool and
//chooses libs accordingly
if (!class_exists('gtk')) {
if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
dl('php_gtk.dll');
else
dl('php_gtk.so');
}
//Called when delete-event happens. Returns false to indicate that the event should proceed
function delete_event()
{
return false;
}
// function destry called when program is closed
function destroy()
{
Gtk::main_quit();
}
// function main is the real program with the form in it
function main()
{
// deal with old window
global $window;
$window->hide();
//creating all widgets
$window2 = &new GtkWindow(); //the main window
$label = &new GtkLabel("Type in number of dice\nand how many faces."); //the text
$vb = &new GtkVbox(); // The vertical box
$inputNum = &new GtkEntry(); //the input field for number of dice
$inputFace = &new GtkEntry(); //The input field for faces on the dice
$broll = &new GtkButton('Roll'); //The button for rolling dice
$hb = &new GtkHbox; //The horizontal box for result window and scrollbar
$result = &new GtkText; //The result window itself
$adj = $result->vadj; //setting the adjustment so I can make scrollbar
$scrollbar = &new GtkVScrollbar($adj); //The scrollbar
$qb = &new GtkButton('Quit'); //The quit button
$statusb = &new GtkStatusbar();
//Setting up the main window
$window2->set_title("VirtuaDice");
$window2->connect('destroy', 'destroy');
//Setting up The input boxes
$inputNum->connect("changed", "set_num");
$inputNum->show();
$inputFace->connect("changed", "set_face");
$inputFace->show();
//Setting up the buttons
$broll->connect('clicked', 'roll', $result, $statusb);
$qb->connect('clicked', 'destroy');
//Setting upp the result box and the scrollbar
$result->set_word_wrap(true);
$fortune="Welcome to VirtuaDice by Patrik Olterman\n\nThis project was made as an exersize in coding with the new php-gtk engine and make a nonweb application with PHP4. \n\nAs such this is a success. \nThis application is somewhat useless unless you want to play a roleplaying game on a train and cant be bothered with Dice rolling all over the place";
$result->insert_text($fortune, 0);
$result->show();
//Setting up the GUI
$window2->add($vb); //putting the vertical box in the window
$vb->pack_start($label); //adding label to vbox
$vb->pack_start($inputNum); // the number of Dice input box
$vb->pack_start($inputFace);// the faces of the dice inputbox
$vb->pack_start($broll); //Roll button
$vb->pack_start($hb); //adding the horisontal box
$hb->pack_start($result); // putting in result window
$hb->pack_end($scrollbar, false); //and scrollbar
$vb->pack_start($qb); //putting the quit button
$vb->pack_end($statusb); //putting te statusbar on the bottom
//Time to display it all
$window2->show_all();
}
/*
* function for rolling dice and displaying result
*
*/
function roll($broll, $result, $statusb)
{
// printing out what is being rolled
$output .= "roooolin ".$GLOBALS["number"]."d".$GLOBALS["faces"]."!!!\n";
// here comes the random generator
//first seed generator
mt_srand(doubleval(microtime()) * 100000000);
//now for the random numbers
for($i=0; $i < $GLOBALS["number"]; $i++)
{
$n = mt_rand(1, $GLOBALS["faces"]);
$die[$i] = $n;
$count = $i + 1;
$output .= "Die $count:\t$die[$i]\n";
$total += $n;
}
$output .= "\nTotal: $total\n\n\n\n\n\n\n";
//putting out the result to the result window
$result->freeze();
$result->delete_text(0,-1);
$result->insert_text($output, 0);
$result->thaw();
//Putting in total in statusbar, havent figured out context id but hey it works :)
$status_context=$statusb->get_context_id('foo');
$statusb->push($status_context, "Total: $total");
//print $output; //for debugging
}
/*
*Functions to set the numbers
*
*/
function set_num($inputNum)
{
//global $inputNum;
$num = $inputNum->get_text();
//print "number:$num\n";
$GLOBALS["number"]=$num;
}
function set_face($inputFace)
{
//global $inputFace;
$face = $inputFace->get_text();
//print "face:$face\n";
$GLOBALS["faces"]=$face;
}
//Here is the root window and the calling of the main loop
//
//create all widgets
$window = &new GtkWindow(); //Gtk window
$wvb = &new GtkVbox(); //Vertical box
$button = &new GtkButton('enter'); //Button
$wlabel = &new GtkLabel("Welcome to VirtuaDice\nBy Patrik Olterman"); // Label
//setting up the window
$window->set_title("VirtuaDice");
$window->connect('destroy', 'hide');
$window->connect('delete-event', 'delete_event');
$window->set_border_width(10);
//setting up the button
$button->connect('clicked', 'main');
//Placing all the parts of the GUI
$window->add($wvb); //add vertical box to window
$wvb->pack_start($wlabel); //add label to vbox
$wvb->pack_end($button); //add button to vbox
// Here we go and show it all
$window->show_all();
//main loop
Gtk::main();
?>