Skip to content

Instantly share code, notes, and snippets.

@mberends
Created September 11, 2011 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mberends/1210303 to your computer and use it in GitHub Desktop.
Save mberends/1210303 to your computer and use it in GitHub Desktop.
half working GUI Calc program for Niecza
# Niecza Calculator
# GTK Sharp docs: http://docs.go-mono.com/monodoc.ashx?link=N%3aGtk
# To run: mono run/Niecza.exe examples/gtk1.pl
# Warning: unfinished, sometimes crashes
constant $GTK = "gtk-sharp,Version=2.12.0.0,Culture=neutral,PublicKeyToken=35e10195dab3c99f";
constant Application = CLR::("Gtk.Application,$GTK");
constant Window = CLR::("Gtk.Window,$GTK");
constant Entry = CLR::("Gtk.Entry,$GTK");
constant Button = CLR::("Gtk.Button,$GTK");
constant Table = CLR::("Gtk.Table,$GTK");
Application.Init;
my $accumulator = 0;
my $operation = '';
# Use a table object for alignment of buttons
my $table = Table.new(5, 4, True);
my $entry = Entry.new();
$entry.Alignment = 1;
$table.Attach($entry,0,4,0,1); # widget, xleft, xright, ytop, ybottom
my @buttons;
my @keytop = <C CE % / 7 8 9 * 4 5 6 - 1 2 3 + 0 . +/- = >;
for 1..5 -> $y {
for 0..3 -> $x {
my $keytop = @keytop.shift;
@buttons[$x][$y] = Button.new($keytop);
@buttons[$x][$y].add_Clicked: sub ($object, $args) {
given $keytop {
when /^\d$/ {
$entry.Text ~= $keytop;
}
when /^<[\+\-\*\/]>$/ {
$operation = $keytop;
$accumulator = +$entry.Text;
$entry.Text = '';
}
when '=' {
given $operation {
when '+' { $accumulator += $entry.Text; }
when '-' { $accumulator -= $entry.Text; }
when '*' { $accumulator *= $entry.Text; }
when '/' { $accumulator /= $entry.Text; }
}
$entry.Text = ~$accumulator;
}
when 'C' {
$accumulator = 0;
$operation = '';
$entry.Text = '';
}
when 'CE' {
$entry.Text = '';
}
when '+/-' {
if $entry.Text ~~ /^\d+$/ {
$entry.Text = ~(-$entry.Text);
}
}
}
$object; $args; # only to suppress "Potential difficulties"
};
$table.Attach(@buttons[$x][$y],$x,$x+1,$y,$y+1);
}
}
my $window = Window.new("Calc");
$window.add_DeleteEvent: sub ($obj, $args) {
Application.Quit;
$obj; $args; # only to suppress "Potential difficulties"
};
$window.Add($table);
$window.ShowAll;
Application.Run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment