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


set DIRE
set wish_dir "about"
set enter_temp "about"
set w .scada
catch {destroy $w}
toplevel $w
wm title $w "SciSCADA's Source"
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 

#######################Top frame###########################

frame $w.top -width 1024 -height 670
pack $w.top -padx 0 -pady 0
pack propagate $w.top false 

#######################Top.Left frame###########################

frame $w.top.left -width 200 -height 700
pack $w.top.left -padx 0 -pady 0
pack propagate $w.top.left false 

 
listbox  $w.top.left.list -width 180 -height 700 -bg green -font {Courier 10 bold}
$w.top.left.list insert 0 main.sci main.tcl opc.tcl modbus.tcl ethernet.tcl serial.tcl mysql.tcl source.tcl about.tcl application_main.tcl application_acquision.tcl application_monitor.tcl application_net.tcl application_about.tcl opc_client.cpp sqlMysql.c modbus_tcp.cpp modbus.c OPCDA_I.C OPCDA.H OpcError.h
pack $w.top.left.list

#######################Top.Right frame###########################

frame $w.top.right -width 850 -height 700
pack $w.top.right -padx 0 -pady 0
pack propagate $w.top.right false 

text $w.top.right.text -width 550 -height 558 -yscrollcommand "$w.top.right.yscroll set" -bg yellow -font {Courier 14}
scrollbar $w.top.right.yscroll -command "$w.top.right.text yview" -orient vertical
pack $w.top.right.yscroll -side right -fill y
	    		
pack $w.top.right.yscroll $w.top.right.text -side right

#######################State frame###########################

frame $w.statusbar -width 1020 -height 20 
pack $w.statusbar -padx 3 -pady 3
pack propagate $w.statusbar false
label $w.statusbar.l1 -text "Local time: "
label $w.statusbar.time -textvar local_time -fg red
label $w.statusbar.l2 -text " | "
label $w.statusbar.author -text "Designed by Zhe.Peng Longhua.Ma ZheJiang Univ. china" -fg green
label $w.statusbar.l3 -text " | "
label $w.statusbar.email -text "pengzhe1113@gmail.com" -fg blue
pack  $w.statusbar.email $w.statusbar.l3 $w.statusbar.author $w.statusbar.l2 $w.statusbar.time $w.statusbar.l1  -side right

proc time_loop {} {
global local_time
set m [expr { [clock seconds] - [clock scan 00:00:00] }]
set local_time [format %2.2d:%2.2d:%2.2d [expr {$m/3600}] [expr {($m%3600)/60}] [expr {($m%3600)%60}]]
after 1000 time_loop
}

time_loop 

#######################packframe###########################
pack  $w.top $w.statusbar -side top
pack $w.top.left $w.top.right -side left




bind $w.top.left.list <ButtonRelease-1> {
set file_name [$w.top.left.list get [$w.top.left.list curselection] ]
set file [file join $DIRE "src" $file_name]
   		set id [open $file]
    		$w.top.right.text delete 1.0 end
    		$w.top.right.text insert 1.0 [read $id]
    		$w.top.right.text mark set insert 1.0
}



