Coding Challenge #2! (115)

53 Name: Wiseman 2006-02-07 00:01 ID:+L8zYhjy

Looks like neither was :) Ok, I think I got how it's done:

<?php
//PHP-GTK calculator for geeks by Wiseman

//GTK COMMON CODE
if (!extension_loaded('gtk')) dl('php_gtk.'.PHP_SHLIB_SUFFIX);

function delete_event() {
return false;
}

function destroy() {
Gtk::main_quit();
}


//BEGIN WINDOW DEFINITIONS

function form_Main() {
global $q, $entry;

$w = new GtkWindow(GTK_WINDOW_DIALOG);
$w->set_name('Main');
$w->set_title('GeekCalc');
$w->set_usize(512, 280);
$w->connect('destroy', 'destroy');
$w->connect('delete-event', 'delete_event');
$w->set_border_width(2);
$w->set_position(GTK_WIN_POS_CENTER);
$w->set_policy(false, false, false);

$box1 = new GtkVBox();
$w->add($box1);

$box2 = new GtkHBox();
$box1->pack_start($box2);

$prompt = new GtkLabel('PHP:');
$box2->pack_start($prompt, false, true, 0);

$entry = new GtkEntry();
$box2->pack_start($entry, true, true, 0);
$entry->connect('activate', 'Calculate');

$separator = new GtkHSeparator();
$box1->pack_start($separator);

$t = new GtkTable(4, 2, true);
$box1->pack_start($t);

foreach (array('raw' => 'Result', 'int' => 'Decimal', 'uint' => 'Unsigned 32', 'hex' => 'Hexadecimal', 'oct' => 'Octal', 'bin' => 'Binary', 'char' => 'Character', 'float' => 'Float', 'sci' => 'Scientific') as $k => $v) {
$frame[$k] = new GtkFrame($v);
$q[$k] = new GtkLabel('-' . $k . '-');
$q[$k]->set_usize(246, -1);
$frame[$k]->add($q[$k]);
}

$z = GTK_FILL;

$t->attach($frame['raw'],0,2,0,1,$z,$z);
$t->attach($frame['int'],0,1,1,2,$z,$z);
$t->attach($frame['uint'],1,2,1,2,$z,$z);
$t->attach($frame['hex'],0,1,2,3,$z,$z);
$t->attach($frame['oct'],1,2,2,3,$z,$z);
$t->attach($frame['bin'],0,1,3,4,$z,$z);
$t->attach($frame['char'],1,2,3,4,$z,$z);
$t->attach($frame['float'],0,1,4,5,$z,$z);
$t->attach($frame['sci'],1,2,4,5,$z,$z);

$t->set_row_spacings(8);
$t->set_col_spacings(8);
$t->resize(2, 4);

$y = new GtkStyle();
$y->fg[GTK_STATE_NORMAL] = new GdkColor('#000000');
$y->fg[GTK_STATE_ACTIVE] = new GdkColor('#000000');
$y->fg[GTK_STATE_PRELIGHT] = new GdkColor('#000000');
$y->fg[GTK_STATE_SELECTED] = new GdkColor('#FFFFFF');
$y->fg[GTK_STATE_INSENSITIVE] = new GdkColor('#808080');
$y->bg[GTK_STATE_NORMAL] = new GdkColor('#D4D0C8');
$y->bg[GTK_STATE_ACTIVE] = new GdkColor('#C8C3B8');
$y->bg[GTK_STATE_PRELIGHT] = new GdkColor('#E1DED7');
$y->bg[GTK_STATE_SELECTED] = new GdkColor('#000080');
$y->bg[GTK_STATE_INSENSITIVE] = new GdkColor('#D4D0C8');
$y->font = gdk::font_load('-*-Verdana-normal-r-normal--18-*-96-96-p-0-iso8859-1');
$w->set_style($y->copy());
$prompt->set_style($y->copy());
$separator->set_style($y->copy());
foreach ($frame as $x)
$x->set_style($y->copy());
$y->font = gdk::font_load('-*-Lucida Console-normal-r-normal--18-*-96-96-p-0-iso8859-1');
$entry->set_style($y->copy());
foreach ($q as $x)
$x->set_style($y->copy());

$w->show_all();
$entry->grab_focus();

return $w;
}


//CALCULATOR FUNCTIONS

//Constants
define('pi', pi());
define('e', exp(1));


//Returns an string containing a character, or its name if it's not printable
function pchar($c) {
$c %= 256;
if ($c < 0)
$c += 256;
if ($c >= 32)
return chr($c);
else {
$names = array('NUL','SOH','STX','ETX','EOT','ENQ','ACK','BEL','BS' ,'TAB','LF' ,'VT' ,'NP' ,'CR' ,'SO' ,'SI' ,'DLE','DC1','DC2','DC3','DC4','NAK','SYN','ETB','CAN','EM' ,'EOF','ESC','FS' ,'GS' ,'RS' ,'US');
return $names[$c];
}
}


//CALCULATOR

function Calculate() {
global $q, $entry;

@eval('$x=' . $entry->get_text() . ';');
$r = round($x);

$q['raw']->set_text($x);
$q['int']->set_text($r);
$q['uint']->set_text($r >= 0 ? $r : 4294967296 + $r);
$q['hex']->set_text(sprintf('%X', $r));
$q['oct']->set_text(sprintf('%o', $r));
$q['bin']->set_text(sprintf('%b', $r));
$q['char']->set_text(pchar($r));
$q['float']->set_text(sprintf(is_infinite((float) $x) ? 'INF' : '%f', $x));
$q['sci']->set_text(sprintf(is_infinite((float) $x) ? 'INF' : '%e', $x));
}


//MAIN
form_Main();
Calculate();
Gtk::main();
?>
Name: Link:
Leave these fields empty (spam trap):
More options...
Verification: