# AC3D
# (C) Copyright 2003 by Inivis



proc menu_render {} {
global prefs_render_command render_desc_list render_id_list UI
global prefs_render_filename prefs_render_type prefs_render_command_new


    toplevel .render
    wm transient .render [winfo toplevel [winfo parent .render]]

    wm title .render "Render"

	frame .render.f
	pack .render.f -fill both -expand 1

	set render .render.f

	set ids [ac3d exporters_get_names]
	set descs [ac3d exporters_get_descs]

	set render_desc_list $descs
	set render_id_list $ids

#puts "ids $ids"

#source tcl/combobox.tcl
#package require combobox 2.2
catch {namespace import combobox::*}


	combobox $render.exporterid -editable false -textvariable combo_value
	eval $render.exporterid  list insert end $descs

	set current 1
	catch {set current [lsearch $ids $prefs_render_type]} { set current 1 }

	$render.exporterid select $current


    label $render.ftlabel -text "Output file type:"
	add_balloon $render.ftlabel "Selects the type of file that AC3D will generate"

    label $render.fnamelabel -text "Output filename:"
    entry $render.filename -relief sunken -bd 2 -width 32 -textvariable prefs_render_filename
	add_balloon $render.fnamelabel "The name of the file that is generated by AC3D and is then passed to the render command"

    grid $render.ftlabel -row 0 -column 0  -sticky e
    grid $render.exporterid -row 0 -column 1 -sticky w
	grid $render.fnamelabel -row 2 -column 0 -sticky e
	grid $render.filename -row 2 -column 1 -sticky w

	label $render.commandlabel -text "Render program:"
	grid $render.commandlabel -row 3 -column 0 -sticky e
	add_balloon $render.commandlabel "The command that is executed when 'render' is pressed to start the external render.\nUse a '%s' in the this text to indicate where the filename is to be inserted."
    entry $render.command -relief sunken -bd 2 -width 80 -textvariable prefs_render_command_new
	grid $render.command -row 3 -column 1 -sticky we

	label $render.commandplabel -text "Render parameters:"
	grid $render.commandplabel -row 4 -column 0 -sticky e
	add_balloon $render.commandplabel "parameters - use a '%s' in the this text to indicate where the filename is to be inserted."
    entry $render.commandp -relief sunken -bd 2 -width 80 -textvariable prefs_render_command_param
	grid $render.commandp -row 4 -column 1 -sticky we


	label $render.tlabel -text "Result:"
	grid $render.tlabel -row 7 -column 0  -sticky we
	text $render.output  -height 5 -width 80
	grid $render.output -row 7 -column 1 -columnspan 2  -sticky we
	add_balloon $render.tlabel "Command line output and status report"

	set UI(renderoutput) $render.output

    button $render.ok -text "Render" -command {
		# get currennt combo box value

		# find pos in desc list
		set i [lsearch $render_desc_list $combo_value]

		set id [lindex $render_id_list $i]

#		puts "file type id is $id"


		$UI(renderoutput) delete 0.0 end
        $UI(renderoutput) insert end "GENERATING $combo_value : \"$prefs_render_filename\".\n"

		ac3d exporter_write_file $id $prefs_render_filename

		# if render comand is not empty, look for %s and replace %s with the render_filename
		if { $prefs_render_command_param != "" } {
			set pos [string first "%s" $prefs_render_command_param]
			set pos1 [expr $pos + 1]

			if { $pos != -1 } {
				set ns [string replace $prefs_render_command_param $pos $pos1 $prefs_render_filename]
			} else {
				set ns " $prefs_render_command_param"
			}
		} else {
			set ns ""
		}

		$UI(renderoutput) insert end "EXCECUTING : \"$prefs_render_command_new $ns\".\n"

		set res "command execution failed - check program path and syntax"
		catch {set res [exec $prefs_render_command_new $ns & ]} 

		$UI(renderoutput) insert end $res
		}


    button $render.cancel -text "Close" -command "destroy .render"
    grid $render.ok -row 8 -column 0 -pady 5

    grid $render.cancel -row 8 -column 1


	grid configure .render.f  -ipadx 10 -ipady 10

    centre_window_on_screen .render

#	focus .render.value
	grab $render
}


