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


set DIRE
set wish_dir "about"
set enter_temp "about"
set w .scada

set local_time "00:00:00"

catch {destroy $w}
toplevel $w
wm title $w "SciSCADA"
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 505 -height 704
pack $w.left -padx 0 -pady 0
pack propagate $w.left false

#######################rightframe###########################

frame $w.right -width 519 -height 704 
pack $w.right -padx 0 -pady 0 
pack propagate $w.right false

#######################right.top frame###########################

frame $w.right.top -width 519 -height 558 
pack $w.right.top -padx 0 -pady 0 
pack propagate $w.right.top false

#######################right.bottom frame###########################

frame $w.right.bottom -width 519 -height 146 
pack $w.right.bottom -padx 0 -pady 0 
pack propagate $w.right.bottom false

#######################right.top.left frame###########################

frame $w.right.top.left -width 220 -height 558 
pack $w.right.top.left -padx 0 -pady 0 
pack propagate $w.right.top.left false

#######################right.top.right frame###########################

frame $w.right.top.right -width 300 -height 558
pack $w.right.top.right -padx 0 -pady 0
pack propagate $w.right.top.right false

#######################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 -textvar local_time
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.right  -side left
pack $w.right.top $w.right.bottom  -side top
pack $w.right.top.left $w.right.top.right  -side left

pack $w.statusbar -side bottom


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

catch {image delete imagemiddle0}
image create photo imagemiddle0 -file [file join $DIRE "img" "function.GIF"]
label $w.right.top.left.l0 -image imagemiddle0 -bd 0 -relief sunken

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

catch {image delete imagemiddle2}
image create photo imagemiddle2 -file [file join $DIRE "img" "modbus.GIF"]
label $w.right.top.left.l2 -image imagemiddle2 -bd 0 -relief sunken

catch {image delete imagemiddle3}
image create photo imagemiddle3 -file [file join $DIRE "img" "mysql.GIF"]
label $w.right.top.left.l3 -image imagemiddle3 -bd 0 -relief sunken

catch {image delete imagemiddle4}
image create photo imagemiddle4 -file [file join $DIRE "img" "serial.GIF"]
label $w.right.top.left.l4 -image imagemiddle4 -bd 0 -relief sunken

catch {image delete imagemiddle5}
image create photo imagemiddle5 -file [file join $DIRE "img" "ethernet_logo.GIF"]
label $w.right.top.left.l5 -image imagemiddle5 -bd 0 -relief sunken

catch {image delete imagemiddle6}
image create photo imagemiddle6 -file [file join $DIRE "img" "about_logo.GIF"]
label $w.right.top.left.l6 -image imagemiddle6 -bd 0 -relief sunken

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

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

catch {image delete imageabout}
image create photo imageabout -file [file join $DIRE "img" "about.GIF"]
label $w.right.top.right.l2 -image imageabout -bd 0 -relief sunken

pack $w.right.top.left.l0 $w.right.top.left.l1 $w.right.top.left.l2 $w.right.top.left.l3 $w.right.top.left.l4 $w.right.top.left.l5 $w.right.top.left.l6  

pack $w.right.bottom.l1

pack $w.right.top.right.l1 $w.right.top.right.l2


$w.right.top.left.l1 config -cursor hand2
$w.right.top.left.l2 config -cursor hand2
$w.right.top.left.l3 config -cursor hand2
$w.right.top.left.l4 config -cursor hand2
$w.right.top.left.l5 config -cursor hand2
$w.right.top.left.l6 config -cursor hand2
$w.right.bottom.l1 config -cursor hand2
#######################enter mouse###########################

bind $w.right.top.left.l1 <Enter> "enter_opc opc1"
proc enter_opc enter_temp {
global DIRE
catch {image delete imageright}
catch {image delete imagemiddle1}
image create photo imagemiddle1 -file [file join $DIRE "img" "opc2.GIF"]
image create photo imageright -file [file join $DIRE "img" "opc_right.GIF"]
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
label $w.right.top.left.l1 -image imagemiddle1 -bd 0 -relief sunken
}

bind $w.right.top.left.l2 <Enter> "enter_modbus modbus1"
proc enter_modbus enter_temp {
global DIRE
catch {image delete imagemiddle2}
catch {image delete imageright}
image create photo imagemiddle2 -file [file join $DIRE "img" "modbus2.GIF"]
image create photo imageright -file [file join $DIRE "img" "modbus_right.GIF"]
label $w.right.top.left.l2 -image imagemiddle2 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l3 <Enter> "enter_mysql mysql1"
proc enter_mysql enter_temp {
global DIRE
catch {image delete imagemiddle3}
catch {image delete imageright}
image create photo imagemiddle3 -file [file join $DIRE "img" "mysql2.GIF"]
image create photo imageright -file [file join $DIRE "img" "mysql_right.GIF"]
label $w.right.top.left.l3 -image imagemiddle3 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l4 <Enter> "enter_serial serial1"
proc enter_serial enter_temp {
global DIRE
catch {image delete imagemiddle4}
catch {image delete imageright}
image create photo imagemiddle4 -file [file join $DIRE "img" "serial2.GIF"]
image create photo imageright -file [file join $DIRE "img" "serial_right.GIF"]
label $w.right.top.left.l4 -image imagemiddle4 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l5 <Enter> "enter_pid pid1"
proc enter_pid enter_temp {
global DIRE
catch {image delete imagemiddle5}
catch {image delete imageright}
image create photo imagemiddle5 -file [file join $DIRE "img" "Ethernet_logo_2.GIF"]
image create photo imageright -file [file join $DIRE "img" "pid_right.GIF"]
label $w.right.top.left.l5 -image imagemiddle5 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l6 <Enter> "enter_fuzzy fuzzy1"
proc enter_fuzzy enter_temp {
global DIRE
catch {image delete imagemiddle6}
catch {image delete imageright}
image create photo imagemiddle6 -file [file join $DIRE "img" "about_logo_2.GIF"]
image create photo imageright -file [file join $DIRE "img" "default.GIF"]
label $w.right.top.left.l6 -image imagemiddle6 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

#######################leave mouse###########################

bind $w.right.top.left.l1 <Leave> "Leave_opc opc1"
proc Leave_opc Leave_temp {
global DIRE
catch {image delete imagemiddle1}
catch {image delete imageright}
image create photo imagemiddle1 -file [file join $DIRE "img" "opc.GIF"]
image create photo imageright -file [file join $DIRE "img" "default.GIF"]
label $w.right.top.left.l1 -image imagemiddle1 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l2 <Leave> "Leave_modbus modbus1"
proc Leave_modbus enter_temp {
global DIRE
catch {image delete imagemiddle2}
catch {image delete imageright}
image create photo imagemiddle2 -file [file join $DIRE "img" "modbus.GIF"]
image create photo imageright -file [file join $DIRE "img" "default.GIF"]
label $w.right.top.left.l2 -image imagemiddle2 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l3 <Leave> "Leave_mysql mysql1"
proc Leave_mysql enter_temp {
global DIRE
catch {image delete imagemiddle3}
catch {image delete imageright}
image create photo imagemiddle3 -file [file join $DIRE "img" "mysql.GIF"]
image create photo imageright -file [file join $DIRE "img" "default.GIF"]
label $w.right.top.left.l3 -image imagemiddle3 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l4 <Leave> "Leave_serial serial1"
proc Leave_serial enter_temp {
global DIRE
catch {image delete imagemiddle4}
catch {image delete imageright}
image create photo imagemiddle4 -file [file join $DIRE "img" "serial.GIF"]
image create photo imageright -file [file join $DIRE "img" "default.GIF"]
label $w.right.top.left.l4 -image imagemiddle4 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l5 <Leave> "Leave_pid pid1"
proc Leave_pid enter_temp {
global DIRE
catch {image delete imagemiddle5}
catch {image delete imageright}
image create photo imagemiddle5 -file [file join $DIRE "img" "ethernet_logo.GIF"]
image create photo imageright -file [file join $DIRE "img" "default.GIF"]
label $w.right.top.left.l5 -image imagemiddle5 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}

bind $w.right.top.left.l6 <Leave> "Leave_fuzzy fuzzy1"
proc Leave_fuzzy enter_temp {
global DIRE
catch {image delete imagemiddle6}
catch {image delete imageright}
image create photo imagemiddle6 -file [file join $DIRE "img" "about_logo.GIF"]
image create photo imageright -file [file join $DIRE "img" "default.GIF"]
label $w.right.top.left.l6 -image imagemiddle6 -bd 0 -relief sunken
label $w.right.top.right.l1 -image imageright -bd 0 -relief sunken
}


#######################bind mouse###########################
proc wish_run wish_dir {
global DIRE
global w
destroy $w
ScilabEval "TCL_EvalFile('$DIRE/$wish_dir.tcl')"
}

bind $w.right.top.left.l1 <Button-1> "wish_run opc"
bind $w.right.top.left.l2 <Button-1> "wish_run modbus"
bind $w.right.top.left.l3 <Button-1> "wish_run mysql"
bind $w.right.top.left.l4 <Button-1> "wish_run serial"
bind $w.right.top.left.l5 <Button-1> "wish_run ethernet"
bind $w.right.top.left.l6 <Button-1> "wish_run about"
bind $w.right.bottom.l1 <Button-1> "wish_run application_main"



