# Ayam, a free 3D modeler for the RenderMan interface.
#
# Ayam is copyrighted 1998-2006 by Randolf Schultz
# (rschultz@informatik.uni-rostock.de) and others.
#
# All rights reserved.
#
# See the file License for details.

# hf1.11.4.tcl - hotfix #4 for Ayam 1.11

# This script fixes the following bugs:
# o bug SF1549831 (extra help menu entry Helpblocked)
# o bug SF1549833 (RenderGUI feature can not find cat.exe on Win32)
# o bug SF1553274 ("Special/Save Selected As" only writes one object)
# o help menu problems on Win32 caused by hotfix #1
# o bug SF1560022 (apply of Transformations property notification error)
# o add proper application menu about entry for Mac OS X Aqua
# o bug SF1566337 (inserting/deleting points notify and redraw errors)
#
# Usage:
# Place this file somewhere on your disk, e.g. in ayam/bin/hf/
# then add the following to your "Scripts" preference setting:
# "hf/hf1.11.4.tcl" (without quotes)

global ay

if { $ay(ay_version) != "1.11" } {
    ayError 2 "hf1.11.4" "This hotfix is for Ayam 1.11 only! Bailing out..."
    return;
}

#######################################################

# fix extraneous help menu entry
set m $ay(helpmenu)

$m delete 0 last
update

$m add command -label "Help" -command {
    after idle {
	global ayprefs
	browser_urlOpen $ayprefs(Docs)
    }
}

$m add command -label "Help on object" -command {
    after idle {
	global ayprefs
	set selected ""
	getSel selected
	if { $selected == "" } {
	    ayError 2 "Help on object" "Please select an object!"
	    return;
	}
	getType type
	set type [string tolower $type]
	if { [string first "file://" $ayprefs(Docs)] != -1 } {
	    set lslash [string last "/" $ayprefs(Docs)]
	    set url [string range
		     $ayprefs(Docs) 0 $lslash]/ayam-4.html\#${type}obj
	    browser_urlOpen $url
	} else {
	    browser_urlOpen $ayprefs(Docs)ayam-4.html\#${type}obj
	}
    }
}

$m add command -label "Show Shortcuts" -command "shortcut_show"
$m add command -label "About" -command "aboutAyam"
$m add checkbutton -label "Show Tooltips" -variable ayprefs(showtt)
global aymainshortcuts
bind $w <[repcont $aymainshortcuts(Help)]> "$m invoke 0"
$m entryconfigure 0 -accelerator $aymainshortcuts(Help)


# fix RenderGUI on Win32
global tcl_platform ayprefs
if { $tcl_platform(platform) == "windows" } {
    set ayprefs(Cat) "cat.exe"
}


# fix Special/Save Selected As
proc io_saveMainGeom { } {
    global ay tagnames tagvals

    if { $ay(lb) == 0 } {
	set tree $ay(tree)
	set sel [$tree selection get]
	treeSelect root:0
    } else {
	set lb $ay(olb)

	set selection [$lb curselection]
	goTop
	selOb 0
    }
    getTags tagnames tagvals
    set i 0
    foreach tagname $tagnames {
	if { $tagname == "SaveMainGeom" } {
	    if { [winfo exists .tbw] } {
		set geom "1,[winGetGeom .] [winGetGeom .tbw]"
	    } else {
		set geom 0,[winGetGeom .]
	    }

	    setTags -index $i SaveMainGeom $geom
	}
	incr i
    }

    if { $ay(lb) == 0 } {
	$tree selection set $sel
	treeSelect $sel
	eval [subst "$tree selection set $sel"]
	eval [subst "treeSelect $sel"]
    } else {
	eval [subst "selOb $sel"]
    }

 return;
}
# io_saveMainGeom

proc io_saveScene { ask selected } {
    global ay tcl_platform

    winAutoFocusOff

    set tmp $ay(filename)
    set filename $ay(filename)

    if { ($ask == "ask") || ($filename == "") } {
	if { $filename == "" } {
	    set filename "unnamed.ay"
	    set dirname [pwd]
	} else {
	    set dirname [file dirname $filename]
	    if { $dirname == "." } { set dirname [pwd] }
	}
	set types {{"Ayam Scene" ".ay"} {"All files" *}}

	if { $tcl_platform(os) != "Darwin" } {
	    set filename [tk_getSaveFile -filetypes $types -parent .\
		    -initialfile [file tail $filename]\
		    -initialdir $dirname -title "Save scene to:"]
	} else {
	    set filename [tk_getSaveFile -filetypes $types -parent .\
		    -initialfile [file tail $filename]\
		    -title "Save scene to:"]
	}
    }

    if { $filename != "" } {
	# append extension
	set newfilename [io_appext $filename ".ay"]

	# fix window positions
	viewUPos

	# save main window geometry to tag
	if { ( $ay(lb) == 0 ) || ( ! $selected ) } {
	    io_saveMainGeom
	}

	# save scene to disk
	global ay_error
	set ay_error ""
	saveScene $filename $selected
	if { $ay_error < 2 } {
	    set windowfilename [file tail [file rootname $filename]]
	    wm title . "Ayam - Main - $windowfilename : --"
	    set ay(filename) $filename
	    ayError 4 "saveScene" "Done saving scene to:"
	    ayError 4 "saveScene" "$filename"
	    io_mruAdd $filename
	    set ay(sc) 0
	} else {
	    ayError 2 "Ayam" "There were errors while saving to:"
	    ayError 2 "Ayam" "$filename"
	}

    }

    winAutoFocusOn

 return;
}
# io_saveScene


# fix notify for transformations property
set b .fu.fMain.fProp.fArg.fb.b1
$b conf -command  {
    global ay
    undo save Apply
    set lb $ay(plb)
    set sel [$lb curselection]
    if { $sel == "" } { return }
    set prop [$lb get $sel]
    set setprocp ""
    eval [subst "set setprocp \$${prop}(sproc)"]
    if { $setprocp != "" } { $setprocp } else { setProp }
    set getprocp ""
    eval [subst "set getprocp \$${prop}(gproc)"]
    if { $getprocp != "" } { $getprocp } else { getProp }

    eval [subst "set arrname \$${prop}(arr)"]
    global pclip_reset $arrname
    array set pclip_reset [array get $arrname]
    if { $arrname == "transfPropData" } { forceNot }
    rV
}


# correct application menu (about entry) on MacOSX Aqua
global AYWITHAQUA
if { $AYWITHAQUA } {
    menu .fu.menubar.apple
    .fu.menubar add cascade -menu .fu.menubar.apple
    .fu.menubar.apple add command -label "About Ayam" -command "aboutAyam"
}


# fix insert/delete points notify and redraw problems

#
proc actionInsertP { w } {
    global ayprefs

    viewTitle $w "" "Insert_Points"

    bind $w <ButtonPress-1> {
	undo save InsPnt
	%W mc
	%W insertpac %x %y
    }

    bind $w <B1-Motion> ""

    bind $w <Motion> ""

    if { $ayprefs(FlashPoints) == 1 } {
	bind $w <Motion> {
	    %W startpepac %x %y -flash
	}
    }

    bind $w <ButtonRelease-1> {
	set ay(action) 0
	update
	forceNot
	rV
	plb_update
	focus %W
    }

    if { $ayprefs(FlashPoints) == 1 } {
	bind $w <ButtonRelease-1> "+%W startpepac %x %y -flash -ignoreold"
    }

    $w setconf -drawh 1

 return;
}
# actionInsertP

#
proc actionDeleteP { w } {
    global ayprefs

    viewTitle $w "" "Delete_Points"

    bind $w <ButtonPress-1> {
	undo save DelPnt
	%W mc
	%W deletepac %x %y
    }

    bind $w <B1-Motion> ""

    bind $w <Motion> ""

    if { $ayprefs(FlashPoints) == 1 } {
	bind $w <Motion> {
	    %W startpepac %x %y -flash
	}
    }

    bind $w <ButtonRelease-1> {
	set ay(action) 0
	update
	forceNot
	rV
	plb_update
	focus %W
    }

    if { $ayprefs(FlashPoints) == 1 } {
	bind $w <ButtonRelease-1> "+%W startpepac %x %y -flash -ignoreold"
    }

    $w setconf -drawh 1

 return;
}
# actionDeleteP


#######################################################

ayError 4 "hf1.11.4" "Hotfix \#4 applied successfully."
return;