
global DIRE
source [file join $DIRE dynhelp.tcl]
set w .complexgui
catch {destroy $w}
toplevel $w
wm title $w "ComplexNetwork_ToolBox"  
wm iconname $w "complexgui"
wm minsize $w  30 8
wm protocol $w WM_DELETE_WINDOW onQuit
#状态栏

frame $w.statusBar
pack $w.statusBar -side bottom -fill x -pady 2
label $w.statusBar.label -textvariable statusbar -relief sunken -bd 1\
	-font "Helvetica 10" -anchor w 
pack $w.statusBar.label -side top -fill x
set statusbar ""


#菜单

frame $w.top -borderwidth 1 
pack $w.top -side top -fill x -pady 0
menubutton $w.top.mb1 -text "File"  -relief raised -underline 0 -menu $w.top.mb1.menu 
pack $w.top.mb1 -side left -padx 5 -pady 5 
set m1 [menu $w.top.mb1.menu -tearoff 1]
$m1 add command -label "New" -command New
$m1 add command -label "Open" -command OpnFle
$m1 add command -label "Save" -command Save 
$m1 add command -label "SaveAs" -command SveFleAs
$m1 add command -label "Quit" -command onQuit
$m1 add separator

menubutton $w.top.mb2 -text "About" -relief raised -underline 1 -menu $w.top.mb2.menu
pack $w.top.mb2 -side left -padx 5 -pady 5
set m3 [menu $w.top.mb2.menu -tearoff 1]
$m3 add command -label "About" -command About 

menubutton $w.top.mb4 -text "Help" -relief raised -underline 3 -menu $w.top.mb4.menu
pack $w.top.mb4 -side left -padx 5 -pady 5
set m5 [menu $w.top.mb4.menu -tearoff 1]
$m5 add command -label "Help" -command Help 

image create photo ico -file [file join $DIRE images showlabel.gif]
label $w.top.label -image ico 
pack $w.top.label -side right

#图标：
labelframe $w.img 
pack $w.img -side top -expand no -fill x 
image create photo image1a -file [file join $DIRE images icon3 new.gif]
button $w.img.new -image image1a -command New
DynamicHelp::add $w.img.new -text "New"
pack $w.img.new -side left 
image create photo image1b -file [file join $DIRE images icon3 open.gif]
button $w.img.open -image image1b -command OpnFle
DynamicHelp::add $w.img.open -text "Open a file"
pack $w.img.open -side left 
image create photo image1c -file [file join $DIRE images icon3 saveas.gif]
button $w.img.sveas -image image1c -command SveFleAs 
DynamicHelp::add $w.img.sveas -text "Save current file as..."
pack $w.img.sveas -side left 
image create photo image1d -file [file join $DIRE images icon3 save.gif]
button $w.img.save -image image1d -command Save 
DynamicHelp::add $w.img.save -text "Save"
bind $w.img.save <Control-c> {Save}
pack $w.img.save -side left 
image create photo image1e -file [file join $DIRE images icon3 exit.gif]
button $w.img.exit -image image1e -command onQuit 
DynamicHelp::add $w.img.exit -text "Quit"
pack $w.img.exit -side left 
image create photo image1f -file [file join $DIRE images icon3 help.gif]
button $w.img.help -image image1f -command Help
DynamicHelp::add $w.img.help -text "Help"
pack $w.img.help -side left 

#打开的文件名
frame $w.top1 -borderwidth 5
label $w.top1.lab -text "File:" -padx 0
set fileshow [entry $w.top1.ls   -width 20 -relief sunken\
	-textvariable filename]
set filename ""
set filenmpre ""
button $w.top1.browse -text "Browse..." -command OpnFle
pack $w.top1.lab -side left
pack $w.top1.ls -side left -fill x  -expand yes 
pack $w.top1.browse -side left 
pack $w.top1 -side top -fill x -padx 1 -pady 1
bind $w.top1.ls <Return> Show

#执行命令栏
frame $w.b 
button $w.b.pre -text "Preview" -bg gray -command Prev
button $w.b.stpov -text "Step Over" -bg gray -command Stepov
button $w.b.run -text "Run" -bg gray -command Run
button $w.b.clear -text "ClearResult" -bg gray -command Clear
pack $w.b.pre  -side left -padx 5 -pady 5
pack $w.b.stpov -side left -padx 5 -pady 5
pack $w.b.run $w.b.clear -side left -padx 5 -pady 5
pack $w.b -side top -fill x 

#文件内容列表
frame $w.frame2 -borderwidth .5c 
pack $w.frame2 -side top -expand yes -fill both
scrollbar $w.frame2.scrolly -command [list $w.frame2.list yview] -orient vertical
scrollbar $w.frame2.scrollx -command [list $w.frame2.list xview] -orient horizontal
set lst [listbox $w.frame2.list -yscrollcommand [list $w.frame2.scrolly set] \
	-xscrollcommand [list $w.frame2.scrollx set] \
	-setgrid 1 -height 10]
grid $w.frame2.list $w.frame2.scrolly -sticky news
grid $w.frame2.scrollx -sticky news
grid rowconfigure $w.frame2 0 -weight 1
grid columnconfigure $w.frame2 0 -weight 1
focus $w.frame2.list

#以列表形式保存的文件内容
set globlist ""
set changed 0

#添加、删除按钮
frame $w.d 
pack $w.d -side top 
button $w.d.add -text Add -bg lightpink -command {set indexout [$lst curselection] ;source [file join $DIRE addwin.tcl]}
button $w.d.del -text Delete -bg lightpink -command Del
pack $w.d.add $w.d.del -side left   -padx 5 -pady 3
bind $w.d.del <Key-Delete> Del

#显示结果
labelframe $w.reslt -text Result:
pack $w.reslt -side top -fill both -expand yes
scrollbar $w.reslt.scrolly -command [list $w.reslt.list yview] -orient vertical
scrollbar $w.reslt.scrollx -command [list $w.reslt.list xview] -orient horizontal
set res [listbox $w.reslt.list -yscrollcommand [list $w.reslt.scrolly set] \
	-xscrollcommand [list $w.reslt.scrollx set] -bg gray\
	-setgrid 1 -height 5]
grid $w.reslt.list $w.reslt.scrolly -sticky news
grid $w.reslt.scrollx -sticky news
grid rowconfigure $w.reslt 0 -weight 1
grid columnconfigure $w.reslt 0 -weight 1

#待插入指令位置索引
set indexout [$lst curselection]
set stpovno 0
#过程定义：

proc onQuit {} {
	global changed filename lst globlist
	if {$changed} {
		set reply [tk_dialog .foo "Attention" "Do you want to save the file?" \
				warning 0 Yes No ]
		if {$reply} {
			exit
		} else {
			if {![file exists $filename]} {
				tk_messageBox -message "File does not exist! Please first Save As!"
				break
			} else {
				catch {open $filename w} fileid
				set globlist [$lst get 0 end]
				for {set i 0} {$i <= [llength $globlist] } {incr i 1} {
					puts $fileid [lindex $globlist $i]
				}
				close $fileid
				exit
			}
		}
	} else {
		exit
	}
}

proc New {} {
	global lst indexout filename fileshow changed statusbar filenmpre globlist stpovno
	if {$changed && ![string equal $filenmpre ""]} {
		set reply [tk_dialog .re1 "Attention" "File changed, save or not?" \
				warning 0 Yes No]
		if {!$reply} {
			if {![file exists $filenmpre]} {
				tk_messageBox -message "File does not exist!"
			} else {
				set changed 0
				set stpovno 0
				catch {open $filenmpre w} fileid
				set globlist [$lst get 0 end]
				for {set i 0} {$i <= [llength $globlist] } {incr i 1} {
					puts $fileid [lindex $globlist $i]
				}
				close $fileid
				set filenmpre $filename
			}
		}
	}
	set changed 1
	set statusbar Modified
	$lst delete 0 end
	set filename "#Don't forget to save your file!"
	set filenmpre $filename
	$fileshow xview moveto 1.0
	set indexout 0
	$lst insert $indexout " # Please First Add one command \
		and then delete this one!"
}

proc Show {} {
	global filename lst fileshow globlist changed filenmpre stpovno
	if {$changed && ![string equal $filenmpre ""]} {
		set reply [tk_dialog .re1 "Attention" "File changed, save or not?" \
				warning 0 Yes No]
		if {!$reply} {
			if {![file exists $filenmpre]} {
				tk_messageBox -message "File does not exist!"
					set filenmpre $filename	
				} else {
				set changed 0
				set stpovno 0
				catch {open $filenmpre w} fileid
				set globlist [$lst get 0 end]
				for {set i 0} {$i <= [llength $globlist] } {incr i 1} {
					puts $fileid [lindex $globlist $i]
				}
				close $fileid
			}
		}
	}
	if {![file exists $filename]} {
		tk_messageBox -message "File does not exists!"
	} else {
		if [catch {open $filename r} fileId] {
			tk_messageBox -message $fileId
		} else {
			set filenmpre $filename
			$fileshow xview moveto 1.0
			$lst delete 0 end
			while {[gets $fileId line] >= 0} {
				if {![string equal $line " "]} {
					$lst insert end $line
				}
			}
			set globlist [$lst get 0 end]
			close $fileId
		}
	}
}

proc Run {} {
	global DIRE lst globlist filename changed filenmpre 
	if {$changed} {
		set reply [tk_dialog .run "Attention" "File changed, save or not"\
				warning 0 Yes No]
		if {!$reply} {
			if {![file exists $filename]} {
				tk_messageBox -message "File does not exist,please firse save as!"
				break
			} else {
				set changed 0
				set filenmpre $filename
				catch {open $filename w} fileid
				set globlist [$lst get 0 end]
				for {set i 0} {$i <= [llength $globlist]} {incr i 1} {
					puts $fileid [lindex $globlist $i]
				}
				close $fileid
			}
		}
	}
	source [file join $DIRE cmd run.tcl]
}

proc Prev {} {
	global DIRE lst globlist filename changed filenmpre
	if {$changed} {
		set reply [tk_dialog .run "Attention" "File changed, save or not"\
				warning 0 Yes No]
		if {!$reply} {
			if {![file exists $filename]} {
				tk_messageBox -message "File does not exist,please firse save as!"
				break
			} else {
				set changed 0
				set filenmpre $filename
				catch {open $filename w} fileid
				set globlist [$lst get 0 end]
				for {set i 0} {$i <= [llength $globlist]} {incr i 1} {
					puts $fileid [lindex $globlist $i]
				}
				close $fileid
			}
		}
	}
	source [file join $DIRE cmd prev.tcl]
}

proc Stepov {} {
	global DIRE lst globlist filename changed filenmpre stpvono
	if {$changed} {
		set reply [tk_dialog .run "Attention" "File changed, save or not"\
				warning 0 Yes No]
		if {!$reply} {
			if {![file exists $filename]} {
				tk_messageBox -message "File does not exist,please firse save as!"
				break
			} else {
				set changed 0
				set filenmpre $filename
				catch {open $filename w} fileid
				set globlist [$lst get 0 end]
				for {set i 0} {$i <= [llength $globlist]} {incr i 1} {
					puts $fileid [lindex $globlist $i]
				}
				close $fileid
			}
		}
	}
	source [file join $DIRE cmd stepov.tcl]
}

proc Clear {} {
	global res
	$res delete 0 end
}

proc OpnFle {} {
	global DIRE filename lst fileshow globlist fileshow filenmpre changed stpovno
	if {$changed && ![string equal $filenmpre ""]} {
		set reply [tk_dialog .re1 "Attention" "File changed, save or not?" \
				warning 0 Yes No]
		if {!$reply} {
			if {![file exists $filenmpre]} {
				tk_messageBox -message "File does not exist! Please first Save As!"
				break
			} else {
				set changed 0
				catch {open $filenmpre w} fileid
				set globlist [$lst get 0 end]
				for {set i 0} {$i <= [llength $globlist] } {incr i 1} {
					puts $fileid [lindex $globlist $i]
				}
				close $fileid
			}
		}
	}
	set fname [tk_getOpenFile -initialdir [file join $DIRE script]]
	set space ""
	if {![string equal $fname $space]} {
		set filename $fname
		set filenmpre $fname
		set changed 0
		if [catch {open $filename r} fileId] {
	 		tk_messageBox -message $fileId
		} else {
			$fileshow xview moveto 1.0
			$lst delete 0 end
			set stpovno 0
			while {[gets $fileId line] >= 0} {
				if {![string equal $line ""]} {
					$lst insert end $line
				}
			}
			set globlist [$lst get 0 end]
			close $fileId
		}
	}
}

proc Save {} {
	global filename lst globlist changed statusbar filenmpre
	if {![file exists $filename]} {
		tk_messageBox -message "File does not exist! Please First SaveAs!"
		break
	} else {
		set changed 0
		set filenmpre $filename
		set statusbar ""
		catch {open $filename w} fileid
		set globlist [$lst get 0 end]
		for {set i 0} {$i <= [llength $globlist] } {incr i 1} {
			puts $fileid [lindex $globlist $i]
		}
		close $fileid
	}
}

proc SveFleAs {} { 
	global DIRE filename lst globlist changed statusbar
	if {[string equal $filename ""]} {
		tk_messageBox -message "File does not exist!"
		break
	}
	set fname [tk_getSaveFile -initialdir [file join $DIRE script]]
	set space ""
	if {[string equal $fname $space]} {
		set fname ""
	} else {
		set filename $fname
		set filenmpre $filename
		if [catch {open $filename w} fileid] {
			tk_message -message $fileid
		} else {
			set changed 0
			set statusbar ""
			set globlist [$lst get 0 end]
			for {set i 0} {$i <= [llength $globlist]} {incr i 1} {
				puts $fileid [lindex $globlist $i]
			}
			close $fileid
		}
	}
}

proc About {} {
	tk_messageBox -message "This is the gui of the complexnetwork toolbox.For more\
	imformation, please visit our web site:home.ustc.edu.cn/~chenwei"
}

proc Del {} {
	global lst indexout changed statusbar
	set changed 1
	set indexout [$lst curselection]
	$lst delete $indexout
	set statusbar Modified
}

proc Demo {} {
}

proc Help {} {
	global DIRE
	source [file join $DIRE helpwin.tcl]
}









