# Written By : Krishnakumar.R (C) 2002
# Email      : krishnakumar_r@bharatmail.com
# Homepage   : askus.way.to
# Copyright  : Share under GPL


from gtk import *                                                               
from math import *

# Evaluation string, no. of rows and columns. 
toeval=' '
rows=9 
cols=4

# Create the main window, box, table, text area & close button.
win = GtkWindow()
box = GtkVBox()
table = GtkTable(rows, cols, FALSE)
text = GtkText()
close  = GtkButton("close")


# Now Create the buttons
button_strings=['hypot(','e',',','clear','log(','log10(','pow(','pi','sinh(','cosh(','tanh(','sqrt(','asin(','acos(','atan(','(','sin(','cos(','tan(',')','7','8','9','/','4','5','6','*','1','2','3','-', '0','.','=','+']
button = map(lambda i:GtkButton(button_strings[i]), range(rows*cols))


# Activated on pressing the evaluation key '='.
def myeval(*args):
	global toeval
	try   :
		b=str(eval(toeval))
        except:
		b= "error"
		toeval=''
	else  : toeval=b
	text.backward_delete(text.get_point())	
	text.insert_defaults(b)

 
def mydel(*args):
	global toeval 
	text.backward_delete(text.get_point())	
	toeval=''	

def calcclose(*args):
	global toeval
	myeval()
	win.destroy()

def print_string(args,i):
	global toeval
	text.backward_delete(text.get_point())	
	text.backward_delete(len(toeval))	
        toeval=toeval+button_strings[i]
	text.insert_defaults(toeval)


def main():
	win.set_usize(300, 350)
	win.connect("destroy", mainquit)
	win.set_title("Scientific Calculator: scical (C) 2002 Krishnakumar.R, Share Under GPL.")
	
	# Pack the box into window.	
	win.add(box)
	box.show()
	
	# Prepare and insert the text area into the box.
	text.set_editable(FALSE)
	text.set_usize(300,1)
	text.show()
	text.insert_defaults(" ")
	box.pack_start(text)
	
	# Include the rest of the table also.
	table.set_row_spacings(5)
	table.set_col_spacings(5)
	table.set_border_width(0)
	box.pack_start(table)
	table.show()

	# Insert buttons into table.
	for i in range(rows*cols) :
	      if i==(rows*cols-2) : button[i].connect("clicked",myeval)
              elif  (i==(cols-1)) : button[i].connect("clicked",mydel)	
              else 	          : button[i].connect("clicked",print_string,i)
       	      y,x = divmod(i, 4)
              table.attach(button[i], x,x+1, y,y+1)
              button[i].show()

	close.show()
	close.connect("clicked",calcclose)
	box.pack_start(close)

	win.show()
	mainloop()

# Start Every thing.
main()