############################################################################################
##     SciSCADA Toolbox
##     Mysql.tcl
##     Developed by Zhe.Peng & Longhua.Ma
##     Email:pengzhe1113@gmail.com
##     Hangzhou, China
##     Sep.2006
############################################################################################

source combobox.tcl
package require combobox 2.3
catch {namespace import combobox::*}

set DIRE
set wish_dir "mysql"
set text_temp "dfsd"
set status "status"
set sql_result "result"

set w .mysql
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's Interface:Mysql"
wm state $w zoom 
canvas $w.c -width 1020 -height 758 -bg blue

####################### menu ###########################

	menu $w.menu -tearoff 0

	set mfile $w.menu.file
	menu $mfile -tearoff 0
	$w.menu add cascade -label "File" -menu $mfile -underline 0
	$mfile add command -label "Open" 
	$mfile add separator
	$mfile add command -label "Quit" -command exit

	set minterface $w.menu.interface
	menu $minterface -tearoff 0 
	$w.menu add cascade -label "Interface" -menu $minterface -underline 0
	
	$minterface add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/main.tcl')" 
	}
 
	$minterface add command -label "OPC" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/opc.tcl')" 
	}
 
	$minterface add command -label "Modbus" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/modbus.tcl')" 
	}

	$minterface add command -label "Ethernet" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/ethernet.tcl')" 
	}

	$minterface add command -label "Mysql" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/mysql.tcl')" 
	}

	$minterface add command -label "Serial" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/serial.tcl')" 
	}


	set mapplication $w.menu.application
	menu $mapplication -tearoff 0 
	$w.menu add cascade -label "Application" -menu $mapplication -underline 0
	$mapplication add command -label "Intro to Tong Lu's water supply"  -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_about.tcl')" 
	}

	$mapplication add command -label "Main" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/Application_main.tcl')" 
	}

	$mapplication add command -label "Networks" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_net.tcl')" 
	}

	$mapplication add command -label "Data Acquisition" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_acquision.tcl')" 
	}

	$mapplication add command -label "Data Monitor" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/application_monitor.tcl')" 
	}

	

	set mhelp $w.menu.mhelp
	menu $mhelp -tearoff 0
	$w.menu add cascade -label "Help" -menu $mhelp -underline 0
	
	$mhelp add cascade -label "Source codes" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/source.tcl')" 
	}
	


	
	$mhelp add command -label "Help" -command {
        set tmp [file join $DIRE index.htm]
        regsub -all "/" $tmp "\\" tmp
        exec $env(COMSPEC) /c $tmp &
	}
	
	
	$mhelp add command -label "About" -command {
	destroy $w
	ScilabEval " TCL_EvalFile('$DIRE/about.tcl')" 
	}
	 
	$w configure -menu $w.menu 

#######################left frame###########################

frame $w.left -width 300 -height 704
pack $w.left -padx 0 -pady 0
pack propagate $w.left false

#######################middle frame###########################

frame $w.middle -width 524 -height 704 -bg white
pack $w.middle -padx 0 -pady 0 
pack propagate $w.middle false

#######################middle.top frame###########################

frame $w.middle.top -width 524 -height 145  -bg white
pack $w.middle.top -padx 0 -pady 0 
pack propagate $w.middle.top false

#######################middle.middle1 1frame###########################

labelframe $w.middle.middle1 -width 524 -height 200 -text "Connect" -bg white
pack $w.middle.middle1 -padx 0 -pady 1 
pack propagate $w.middle.middle1 false

#######################middle.middle1.left ×óframe###########################

frame $w.middle.middle1.left -width 150 -height 200 -bg white
pack $w.middle.middle1.left -padx 0 -pady 1 
pack propagate $w.middle.middle1.left false

label $w.middle.middle1.left.l1 -text "Host:"
label $w.middle.middle1.left.l2 -text "Username:"
label $w.middle.middle1.left.l3 -text "Password:"
label $w.middle.middle1.left.l4 -text "Database:"

#######################middle.middle1.middle frame###########################

frame $w.middle.middle1.middle -width 150 -height 200 -bg white
pack $w.middle.middle1.middle -padx 0 -pady 1 
pack propagate $w.middle.middle1.middle false

entry $w.middle.middle1.middle.e1 -textvariable host
entry $w.middle.middle1.middle.e2 -textvariable user
entry $w.middle.middle1.middle.e3 -textvariable pwd
entry $w.middle.middle1.middle.e4 -textvariable db

#######################middle.middle1.right frame###########################

frame $w.middle.middle1.right -width 150 -height 200 -bg white
pack $w.middle.middle1.right -padx 0 -pady 1 
pack propagate $w.middle.middle1.right false

button $w.middle.middle1.right.button -text "Connect" -command {
ScilabEval "sql_host='$host'" 
ScilabEval "sql_user='$user'" 
ScilabEval "sql_pwd='$pwd'" 
ScilabEval "sql_db='$db'" 
ScilabEval {sql_temp_flag=call('sql_connect',sql_host,1,'c',sql_user,2,'c',sql_pwd,3,'c',sql_db,4,'c','out',[1,1],5,'i')}
}

#######################middle.middle2 frame###########################

labelframe $w.middle.middle2 -width 524 -height 50 -text "Status" -bg white
pack $w.middle.middle2 -padx 0 -pady 1 
pack propagate $w.middle.middle2 false

label $w.middle.middle2.label -text $status

#######################middle.buttom frame###########################

labelframe $w.middle.buttom -width 524 -height 200 -text "Operation" -bg white
pack $w.middle.buttom -padx 0 -pady 1 
pack propagate $w.middle.buttom false

entry $w.middle.buttom.entry -textvariable entrysql
button $w.middle.buttom.button -text "Execute" -command {
ScilabEval {sql_result=call('sql_query',sql_exp,1,'c','out',[10,100],2,'c')}
ScilabEval "sql_exp='$entrysql'"
ScilabEval "TCL_SetVar('sql_result',sql_result)"
}

label $w.middle.buttom.label -text $sql_result

#######################right frame###########################

frame $w.right -width 200 -height 704 
pack $w.right -padx 0 -pady 0 
pack propagate $w.right false
#######################daohang frame###########################

frame $w.middle.bb -width 524 -height 10
pack $w.middle.bb -padx 0 -pady 0 
pack propagate $w.middle.bb
button $w.middle.bb.cmd0 -width 16 -height 1 -text "OPC" -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'opc.tcl')"
	}
button $w.middle.bb.cmd1 -width 16 -height 1 -text "Modbus"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'modbus.tcl')"
	}
button $w.middle.bb.cmd2 -width 16 -height 1 -text "Mysql"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'mysql.tcl')"
	}
button $w.middle.bb.cmd3 -width 16 -height 1 -text "Serial"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'serial.tcl')"
	}
button $w.middle.bb.cmd4 -width 16 -height 1 -text "Ethernet"  -command {
	destroy $w
	ScilabEval "TCL_EvalFile(DIR+'ethernet.tcl')"
	}

pack $w.middle.bb.cmd0 $w.middle.bb.cmd1 $w.middle.bb.cmd2 $w.middle.bb.cmd3 $w.middle.bb.cmd4 -side left

#######################statusbar frame###########################

frame $w.statusbar -width 1020 -height 20 
pack $w.statusbar -padx 3 -pady 3
pack propagate $w.statusbar false
label $w.statusbar.time -text "Time:2006-7-7 18:45:34 | "
label $w.statusbar.mouse -text "  | "
label $w.statusbar.note -text "  | "
label $w.statusbar.author -text "Designed by ZhePeng ZheJiang Univ. china"

pack $w.statusbar.time  $w.statusbar.mouse  $w.statusbar.note  $w.statusbar.author -side left

#######################packframe###########################

pack $w.left $w.middle $w.right  -side left
pack $w.middle.top $w.middle.middle1 $w.middle.middle2 $w.middle.buttom -side top
pack $w.middle.middle1.left $w.middle.middle1.middle $w.middle.middle1.right -side left
pack $w.middle.middle1.left.l1 $w.middle.middle1.left.l2 $w.middle.middle1.left.l3 $w.middle.middle1.left.l4 -side top
pack $w.middle.middle1.middle.e1 $w.middle.middle1.middle.e2 $w.middle.middle1.middle.e3 $w.middle.middle1.middle.e4 -side top
pack $w.middle.middle1.right.button
pack $w.middle.middle2.label
pack $w.middle.buttom.entry $w.middle.buttom.button $w.middle.buttom.label -side top 

#######################Add Pic###########################
catch {image delete imageserial_left}
image create photo imageserial_left -file [file join $DIRE "img" "left.GIF"]
label $w.left.l1 -image imageserial_left -bd 0 -relief sunken
pack $w.left.l1

catch {image delete imageserial_right}
image create photo imageserial_right -file [file join $DIRE "img" "right.GIF"]
label $w.right.l1 -image imageserial_right -bd 0 -relief sunken
pack $w.right.l1

catch {image delete imageserial_top}
image create photo imageserial_top -file [file join $DIRE "img" "mysqltop.GIF"]
label $w.middle.top.l1 -image imageserial_top -bd 0 -relief sunken
pack $w.middle.top.l1


#######################Initial###########################
set host 10.13.24.151
set user root
set pwd sa
set db test
set entrysql {SELECT * FROM tabletest}

