:- module(tcl_calculator,[test/0, main/0]).

:- use_module(tk_calculator_aux).

:- use_module(library(tcltk)).
:- use_module(library(lists)).
:- use_module(library(format)).

:-export(test/0).

main :- test.
test :-
	tcl_new(X),
	test_aux(X).

test_aux(X) :-
        tcl_eval(X,[button,'.b1',min(text),dq('1')],_),
        tcl_eval(X,[button,'.b2',min(text),dq('2')],_),
        tcl_eval(X,[button,'.b3',min(text),dq('3')],_),
        tcl_eval(X,[button,'.b4',min(text),dq('4')],_),
        tcl_eval(X,[button,'.b5',min(text),dq('5')],_),
        tcl_eval(X,[button,'.b6',min(text),dq('6')],_),
        tcl_eval(X,[button,'.b7',min(text),dq('7')],_),
        tcl_eval(X,[button,'.b8',min(text),dq('8')],_),
        tcl_eval(X,[button,'.b9',min(text),dq('9')],_),
        tcl_eval(X,[button,'.b0',min(text),dq('0')],_),
        tcl_eval(X,[button,'.b+',min(text),dq('+')],_),
        tcl_eval(X,[button,'.b-',min(text),dq('-')],_),
        tcl_eval(X,[button,'.b*',min(text),dq('*')],_),
        tcl_eval(X,[button,'.b/',min(text),dq('/')],_),
        tcl_eval(X,[button,'.b=',min(text),dq('=')],_),

        tcl_eval(X,[entry,'.e1',min(textvariable),'inputval'],_),
        tcl_eval(X,[set,'inputval',0],_),

        tcl_eval(X,[grid,'.e1',min(row),1,min(columnspan),6],_),
        tcl_eval(X,[grid,'.b1',min(row),2,min(column),1],_),
        tcl_eval(X,[grid,'.b2',min(row),2,min(column),2],_),
        tcl_eval(X,[grid,'.b3',min(row),2,min(column),3],_),
        tcl_eval(X,[grid,'.b4',min(row),3,min(column),1],_),
        tcl_eval(X,[grid,'.b5',min(row),3,min(column),2],_),
        tcl_eval(X,[grid,'.b6',min(row),3,min(column),3],_),
        tcl_eval(X,[grid,'.b7',min(row),4,min(column),1],_),
        tcl_eval(X,[grid,'.b8',min(row),4,min(column),2],_),
        tcl_eval(X,[grid,'.b9',min(row),4,min(column),3],_),
        tcl_eval(X,[grid,'.b0',min(row),5,min(column),1],_),
        tcl_eval(X,[grid,'.b+',min(row),2,min(column),4],_),
        tcl_eval(X,[grid,'.b=',min(row),5,min(column),3],_),
        tcl_eval(X,[grid,'.b-',min(row),3,min(column),4],_),
        tcl_eval(X,[grid,'.b*',min(row),4,min(column),4],_),
        tcl_eval(X,[grid,'.b/',min(row),5,min(column),4],_),

        tcl_eval(X,[set,'operation',0],_),
        tcl_eval(X,[set,'reset',1],_),

        tcl_eval(X,[proc, 'value', br(' newvalue oldvalue delete '),
                    write('{ if { $delete == 0 } { return $oldvalue$newvalue }'), 
                    write(' else { '),  
                    return, '$newvalue',
                    write(' } }')],_),
        tcl_eval(X,[bind,'.b1','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 1 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b2','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 2 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b3','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 3 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b4','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 4 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b5','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 5 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b6','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 6 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b7','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 7 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b8','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 8 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b9','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 9 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b0','<ButtonPress-1>',
                    br([set,'inpaux1','$inputval','\n',
                    set,'inputval',sqb('value 0 $inpaux1 $reset'),'\n',
                    set,'reset',0 ])],_),
        tcl_eval(X,[bind,'.b+','<ButtonPress-1>',
                    br([set,'inputval1','$inputval','\n',
                    set,'operation',1,'\n',
                    set,'reset',1])],_),
        tcl_eval(X,[bind,'.b-','<ButtonPress-1>',
                    br([set,'inputval1','$inputval','\n',
                    set,'operation',2,'\n',
                    set,'reset',1])],_),
        tcl_eval(X,[bind,'.b*','<ButtonPress-1>',
                    br([set,'inputval1','$inputval','\n',
                    set,'operation',3,'\n',
                    set,'reset',1])],_),
        tcl_eval(X,[bind,'.b/','<ButtonPress-1>',
                    br([set,'inputval1','$inputval','\n',
                    set,'operation',4,'\n',
                    set,'reset',1])],_),
        tcl_eval(X,[bind,'.b=','<ButtonPress-1>',
                    br([set,'inputval2','$inputval','\n',
                    write(' if { $operation == 1 } { '),
                    prolog_one_event,dq(write(execute(tk_calculator_aux:plus('$inputval1','$inputval2','Outputval')))),'\n',
                    set, 'inputval','$prolog_variables(Outputval)','\n',
                    write(' }'),
                    write(' elseif { $operation == 2 } { '),
                    prolog_one_event,dq(write(execute(tk_calculator_aux:sub('$inputval1','$inputval2','Outputval')))),'\n',
                    set, 'inputval','$prolog_variables(Outputval)','\n',
                    write(' }'),
                    write(' elseif { $operation == 3 } { '),
                    prolog_one_event,dq(write(execute(tk_calculator_aux:prod('$inputval1','$inputval2','Outputval')))),'\n',
                    set, 'inputval','$prolog_variables(Outputval)','\n',
                    write(' }'),
                    write(' elseif { $operation == 4 } { '),
                    prolog_one_event,dq(write(execute(tk_calculator_aux:div('$inputval1','$inputval2','Outputval')))),'\n',
                    set, 'inputval','$prolog_variables(Outputval)','\n',
                    write(' }'),'\n',
                    set, 'operation',0,'\n',
                    set, 'reset',1
                    ])],_),

        tk_event_loop(X).




