# pulldown menus for hexpuzzle

set num_gamesaves 5
frame .m
pack .m -side top -fill x

menubutton .m.game	-text "Game  "		-menu .m.game.m1
menubutton .m.options	-text "Options  "	-menu .m.options.m1
menubutton .m.help	-text "Help  "		-menu .m.help.m1

pack .m.game	-in .m -side left
pack .m.options	-in .m -side left
pack .m.help	-in .m -side left

###### game menu #######

menu .m.game.m1 -tearoff 0
.m.game.m1 add cascade -label "New" -menu .m.game.m1.new
.m.game.m1 add separator
.m.game.m1 add cascade -label "Save" -menu .m.game.m1.save
.m.game.m1 add cascade -label "Load" -menu .m.game.m1.load
.m.game.m1 add separator
.m.game.m1 add command -label "Quit" -command {Quit}

menu .m.game.m1.new -tearoff 0
foreach i {small medium large bonus} {
	.m.game.m1.new add command -label $i -command "NewGame $i"
}

menu .m.game.m1.save -tearoff 0
for {set i 1} {$i<=$num_gamesaves} {incr i} {
	.m.game.m1.save add command -label "Game $i" -command "SaveGame $i"
}
.m.game.m1.save add separator
.m.game.m1.save add command -label "Save As" -command {SaveGameAs}

menu .m.game.m1.load -tearoff 0
for {set i 1} {$i<=$num_gamesaves} {incr i} {
	.m.game.m1.load add command -label "Game $i" -command "LoadGame $i"
}
.m.game.m1.load add separator
.m.game.m1.load add command -label "Load File" -command {LoadGameAs}

###### options menu #######

menu .m.options.m1 -tearoff 0
.m.options.m1 add cascade -label "Piece colours"  -menu .m.options.m1.colours
.m.options.m1 add cascade -label "Show overlapping" -menu .m.options.m1.black
.m.options.m1 add cascade -label "Check if finished" -menu .m.options.m1.finished

menu .m.options.m1.colours -tearoff 0
for {set i 1} {$i <= $colour(num)} {incr i} {
	.m.options.m1.colours add radio -label $colour(name-$i)  \
	-var colour(current) -value $colour(name-$i) \
	-command {SetPieceColours ; RefreshPieceColours}
}

menu .m.options.m1.finished -tearoff 0
.m.options.m1.finished add radio -label yes -var flag(check_finished) -value 1
.m.options.m1.finished add radio -label no  -var flag(check_finished) -value 0

menu .m.options.m1.black -tearoff 0
.m.options.m1.black add radio -label yes -var flag(black_outline) -value 1
.m.options.m1.black add radio -label no  -var flag(black_outline) -value 0

###### help menu #######

menu .m.help.m1 -tearoff 0

### create a menu item for all the files which start with an uppercase letter
# ( "[lsort [glob {[A-Z]*}]]" doesn't work on vfat file-systems )

foreach {i} [lsort [glob *]]] {
    if {[string match {[A-Z]*} $i]} {
	.m.help.m1 add command -label "$i" -command "ShowFile $i"
    }
}

#####procedures###############################################

proc NewGame {size} {
        global gamemode
          
        destroy .c
	set gamemode $size
	NewCanvas
}       
##################
proc SaveGameAs {} {
	global savedir
	set file [tk_getSaveFile -initialdir $savedir]
	if {$file!=""} {SaveGameGo $file}
}
proc SaveGame {slot} {
	global savedir
	if {![file isdirectory $savedir]} {file mkdir $savedir}
	SaveGameGo "$savedir/game$slot"
}
proc SaveGameGo {file} {
	global p gamemode

	set fid [open "$file" w]
	puts $fid "set gamemode $gamemode"

	for {set i 1} {$i<=$p(count)} {incr i} {
		set coordlist [.c coords piece$i]      
		set x [expr int([lindex $coordlist 2])] 
		set y [expr int(([lindex $coordlist 5]+[lindex $coordlist 7])/2)]
		puts $fid "set p(coords,$i) \"[string trim $p(coords,$i)]\""
		puts $fid "DrawPiece $i $x $y"
	}
	close $fid
}
##################
proc LoadGameAs {} {
	global savedir
	set file [tk_getOpenFile -initialdir $savedir]
	if {$file!="" && ![string match "*options" $file]} {LoadGameGo $file}
}
proc LoadGame {slot} {
	global savedir
	LoadGameGo "$savedir/game$slot"
}
proc LoadGameGo {file} {
	global gamemode p flag

	# these two lines are for cosmetic purposes
	# redraw . (which was covered by a menu or tk_chooseColour)
	raise .
	update idletasks

	# destroy old canvas, even if it is the same size
	destroy .c

	# the first line gives us the gamemode
	set fid [open "$file" r]
	eval [gets $fid]
	close $fid

	# for backwards compatability (you're too nice S.A.)
	if {[info exists dx]} {
	    switch $dx {
		11 { set gamemode large }
		8  { set gamemode medium }
		5  { set gamemode small }
	    }	
	}

	NewCanvas

	.c delete piece
	source "$file"
        .c raise text all
        set flag(report_finished) 0
	set flag(black_outline) 1

}
##################
proc ShowFile {file} {

	if {[winfo exists .show]} {destroy .show}; # it's a cruel world

	toplevel .show
	wm title .show "HexPuzzle: $file"

	set maxlen 0
	set numlines 0

	text .show.t -font 9x15 -relief groove -spacing1 3 -back grey80
	set fid [open $file r]

	while {![eof $fid]} {
		gets $fid line
		.show.t insert end "$line\n"
		if {[string length $line]>$maxlen} \
			{set maxlen [string length $line]}
		incr numlines
	}
	close $fid
	incr numlines 2
	.show.t configure -width $maxlen -height $numlines

	set title_len [string length $file]
	set spaces [string repeat " " [expr ($maxlen-$title_len)/2]]
	
	.show.t insert 1.0 "$spaces[string repeat "-" $title_len]\n"
	.show.t insert 1.0 "$spaces$file\n"
	
	pack .show.t
	.show.t configure -state disabled
	
	button .show.b -text ok -command {destroy .show} -pady .7
	pack .show.b -side bottom
}
######end#########
