# AC3D
# (C) Copyright 2003 by Inivis


# main AC3D TCL file defines control panel UI

#tk_setPalette #bdbdbd

#puts [winfo name .]
  wm protocol . WM_DELETE_WINDOW { menu_quit }

puts "platform: [ac3d get_platform]"

catch {
	if {$prefs_default_dir != ""} { cd $prefs_default_dir }
}



puts "TCL/TK appname: [tk appname ac3d]"

set tk_strictMotif $prefs_tk_strictmotif

if { $tcl_platform(platform) != "windows" } {
  if { $prefs_font_main != "" } {
    option add *font     $prefs_font_main
    }
  if { $prefs_font_menu != ""} {
    option add *Menubutton*font  $prefs_font_menu
    }
}


# try and please the Tucows windows reviewer:
if { $tcl_platform(platform) == "windows" } {
	wm minsize . 745 520
}


#option add *highlightThickness 0

option add *Button*borderWidth 1
option add *Checkbutton*borderWidth 1





set highlight 0

# if strict motif is on then you must add one to ypad for same appearance
if { $tk_strictMotif == 1 } {
    set ypad 1
} else {
    set ypad 0
}





proc section_frame {w text} {


return [labelframe $w -text $text -pady 4 -padx 4]


return;


#    frame $name -bd 1 -relief ridge
	# -borderwidth 2 
  frame $w -bd 0
  label $w.l -pady 0
  frame $w.f -bd 2 -relief groove
  frame $w.f.f
  pack $w.f.f
  $w.l config -text $text
  set padx 3
  set pady 7
  set ipadx 2
  set ipady 7

  pack $w.f -padx $padx -pady $pady -fill both -expand 1
  place $w.l -x [expr $padx+10] -y $pady -anchor w
  pack $w.f.f -padx $ipadx -pady $ipady -fill both -expand 1
  raise $w.l
  return $w.f.f


}

proc horiz_pack {parent args } {
    foreach i $args {
        pack $i -in $parent -side left -fill both -expand 1 -pady 0 -padx 0 -ipady 0 -ipadx 0
    }
}

proc vert_pack {parent args } {
    foreach i $args {
        pack $i -in $parent -side top -fill both -expand 1 -pady 0 -padx 0
#        pack $i -in $parent -side top  -pady 0 -padx 0
    }
}


set draw_mode move

proc trace_proc { name1 name2 op } {
global currentrb cp
global drawmode_preserve
global oldbg

    upvar $name1 newval

    set n $name1.id($newval)
    upvar #0 $n shit

	# if it's the same mode we are in at the moment - do nothing
	if { $shit == $currentrb($name1) } { return }
		
	# raise the current radio button
    $currentrb($name1) configure -relief raised -bg $oldbg


    set currentrb($name1) $shit
	# sink the new one
    $currentrb($name1) configure -relief sunken
#	puts traceproc
}


proc check_keep_down { name variable value } {
global drawmode_preserve
global $variable

    if { [set $variable] == $value} {
		set drawmode_preserve true	
		$name configure -bg green
	} else {
		set drawmode_preserve false
	}

}

proc mode_button {name text command variable value { helptext "" } { keepdownable false} } {
global highlight
global ypad
global tcl_version
global $variable
global currentrb
global bitmap
global oldbg

#        radiobutton $name -text $text -command $command -padx 0 -pady $ypad -height 1\
#            -highlightthickness  $highlight -indicatoron false \
#			 -bd 1 -variable $variable -value $value


#puts "$name $text"

    label $name -text $text -relief raised -pady 0 -bd 1
	set oldbg [$name cget -bg]

    bind $name <ButtonPress-1> "%W configure -relief sunken"

	if { $keepdownable } {
		set ckd "check_keep_down $name $variable $value"
	} else {
		set ckd "set drawmode_preserve false; set $variable $value"
	}
    bind $name <ButtonRelease-1> "$ckd ; $command; if { \$drawmode_preserve } {$name configure -bg green}"
    catch {$name config -image $bitmap($value)}

    set winname "$variable.id($value)"
    uplevel #0 " set $winname $name "
    upvar #0 $winname poo

    if { [set $variable] == $value} {
        $name configure -relief sunken
		set currentrb($variable) $name
		trace variable $variable w trace_proc
#		puts "tracing $variable"
    }

	if { $helptext != "" } {
		add_balloon $name $helptext
	}
	return $name

}

proc cbutton {name text command {helptext ""} } {
global highlight
global ypad
global tcl_version

# standard buttons are too big
#button $name -text $text -command $command -borderwidth 1 -highlightthickness 0 -padx 0 -pady 0
#return $name

	label $name -text $text -relief raised -pady 0 -bd 1
	if { $helptext != ""} {
		add_balloon $name $helptext
	}

	bind $name <ButtonPress-1> { %W configure -relief sunken }
	bind $name <ButtonRelease-1> " if { \[%W cget -relief\] == \"sunken\" } { %W configure -relief raised ; $command } "

	return $name
}

proc bhit { widget var } {

upvar $var val
#puts "val of $var is $val"

	if { $val } {
		$widget configure -relief raised
		set val 0
	} else {
		$widget configure -relief sunken
		set val 1
	}

}


proc switch_button { name text command variable { helptext "" } } {

	checkbutton $name -text $text -highlightthickness 0 -variable $variable -command $command
	if { $helptext != "" } {
		add_balloon $name $helptext
	}
	return $name


return
#used to have own widget because didn't know that it was highlightthickness that was making it take up so much space
	label $name -text $text -relief raised -pady 0
	bind $name <ButtonPress-1> "bhit $name $variable; $command"

	upvar #0 $variable val

	if { $val } {
		$name configure -relief sunken
	} else {
		$name configure -relief raised
	}

	if { $helptext != "" } {
		add_balloon $name $helptext
	}
	return $name

}





proc maximize_cp {} {

set g [wm maxsize .]
puts $g

}








proc foldbuttontoggle { name } {

global foldbuttonstate
global foldbuttoninner
global foldcanvas

	set inner $foldbuttoninner($name)

	if { $foldbuttonstate($name)} {
		 $name configure -relief sunken
		$name itemconfigure indicator -text "-" 
		pack $inner -side top -fill both -padx 3 -pady 3 -expand 1

	} else {
		$name configure -relief raised
		$name itemconfigure indicator -text "+" 
		pack forget $inner

	}
	set  foldbuttonstate($name)  [expr !$foldbuttonstate($name)] 



	set b [$foldcanvas bbox win]
#	puts "bbox $b"

	set height [$foldcanvas cget -height]
	set height [ lindex $b 3]
	set width [$foldcanvas cget -width] 
#	puts "$width $height" 

	$foldcanvas configure -scrollregion " 0 0 100 $height "
}

proc positiontext { canv x2 y} {

	$canv move text [expr $x2/2] $y
}

proc foldbutton {name text inner} {
global foldbuttonstate
global foldbuttoninner

	set foldbuttonstate($name) 1
	set foldbuttoninner($name) $inner 
 
	canvas $name -relief raised -height 10 -bd 2
	pack $name -fill x


	$name create text 0 0 -text  $text -justify center -tags text

	$name create text 0 0 -text  "+" -justify right -tags indicator

	set font [$name itemcget text -font]
	set a [font metrics $font -ascent]
	set d [font metrics $font -descent]

	set h [expr $a + $d]
	set amd [expr $a - $d]

	set cw [$name cget -width]
	set cw2 [expr $cw/2]

#	puts "font $font $a $d $h"

	$name configure -height $h 

#	$name move text $cw2 $amd
	$name move indicator 10 $amd

bind $name <Configure> "positiontext $name %w $amd"

	bind $name <ButtonPress-1> " foldbuttontoggle %W"
	return $name
}







proc folditem { parent name title} {
	set f [frame $parent.$name  -relief groove -bd 2  ]	
	pack $f -fill x -expand 1 
	set inner [frame $f.inner ]	
	set togglevar($inner) 0

	foldbutton $f.b  $title $inner

	pack $f.b -side top -fill x -pady 1

#	label $inner.l -text hello -height 10
#	pack $inner.l -fill both
	return $inner
}



proc createfoldcanvas { c } {
global foldcanvas
#set width 100
#set height 200
set region {0 0 10 10}


	set pwidth [$c cget -width]

	scrollbar $c.yscroll -orient vertical -command "$c.canvas yview" -width 10

	pack $c.yscroll -side right -fill y
	update

	set cwidth [expr $pwidth - [$c.yscroll cget -width]]

	canvas $c.canvas  \
		-yscrollcommand "$c.yscroll set" -width $cwidth

	set foldcanvas $c.canvas

	$c.canvas configure -scrollregion $region


	pack $c.canvas -side right -fill both -expand true


	set f [frame $c.canvas.f  -bd 0]
	$c.canvas create window 0 0 -anchor nw -window $f -tags win -width $cwidth

	return $c.canvas.f
}




















#    wm title . [ac3d get_version_string]
	wm title . "Inivis AC3D"

#    wm geometry . +$prefs_cpwin_x+$prefs_cpwin_y

wm geometry . ${prefs_cpwin_width}x${prefs_cpwin_height}+$prefs_cpwin_x+$prefs_cpwin_y

# menu bar

    if { $tcl_version >= 8 } {
menu .mbar -tearoff 0 -type normal
#menu .mbar -tearoff 0 -type toplevel
    } else {
        frame .mbar -relief raised -bd 2
        pack .mbar -side top -fill x
    }


# cp is the main control panel

#    toplevel .cp
#    wm title .cp "AC3D"
#    wm transient .cp [winfo toplevel [winfo parent .cp]]

    frame .bottombar -relief raised -bd 1
    pack .bottombar -side bottom -fill x
	
   frame .cp
    pack .cp -side left -fill y
    set cp .cp

    frame .toolbar -bd 1 -relief groove
    pack .toolbar -side top -fill x 
    set toolbar .toolbar

 


    canvas .views -relief raised -bd 2 -bg black
    pack .views -side left -fill both -expand 1


# file menu
    catch { menubutton .mbar.file -text "File" -menu .mbar.file.menu -underline 0} err
    if { ($err != ".mbar.file") } {
        puts "$err An error has occured which is probably due a bad menu font\nClearing options and retrying."
        option clear
        menubutton .mbar.file -text "File" -menu .mbar.file.menu -underline 0
        }    


    menu .mbar.file.menu -tearoff 0
	set UI(menu_file) .mbar.file.menu


    .mbar.file.menu add command -label "New..." -command "menu_clear" -accelerator Ctrl+N -underline 0
    .mbar.file.menu add command -label "Open..." -command { menu_open ""  } -accelerator Ctrl+O -underline 0
    .mbar.file.menu add separator
    .mbar.file.menu add command -label "Merge.." -command { menu_merge } -accelerator Ctrl+M -underline 0
#   .mbar.file.menu add separator

    .mbar.file.menu add command -label "Import..." -command {menu_import } -accelerator Ctrl+I -underline 0
    .mbar.file.menu add cascade -label "Export" -menu .mbar.file.menu.export -underline 0
    .mbar.file.menu add command -label "Object library..." -command { obj_lib_dlg } -underline 7
    .mbar.file.menu add separator
    .mbar.file.menu add command -label "Save" -command "menu_save_last" -underline 0  -accelerator Ctrl+S
    .mbar.file.menu add command -label "Save as..." -command "menu_save_ac" -underline 5
    .mbar.file.menu add command -label "Save selection..." -command "menu_save_selection" -underline 7
    .mbar.file.menu add separator
		# only add Print under Windows
	if { $tcl_platform(platform) == "windows" } {
		.mbar.file.menu add command -label "Print..." -command "ac3d windows_print_dialog" -underline 1 -accelerator Ctrl+P
		.mbar.file.menu add separator
		bind . <Control-p> "ac3d windows_print_dialog"

	}

    .mbar.file.menu add command -label "Settings..." -command "show_settings" -underline 2
    .mbar.file.menu add separator

    .mbar.file.menu add cascade -label "Recent models" -menu .mbar.file.menu.recent
    menu .mbar.file.menu.recent -tearoff 0
    .mbar.file.menu.recent add command -label $prefs_recentfiles0 -command { if { $prefs_recentfiles0 != ""} { menu_open $prefs_recentfiles0} }
	.mbar.file.menu.recent add command -label $prefs_recentfiles1 -command { if { $prefs_recentfiles1 != ""} { menu_open $prefs_recentfiles1} }
    .mbar.file.menu.recent add command -label $prefs_recentfiles2 -command { if { $prefs_recentfiles2 != ""} { menu_open $prefs_recentfiles2} }
    .mbar.file.menu.recent add command -label $prefs_recentfiles3 -command { if { $prefs_recentfiles3 != ""} { menu_open $prefs_recentfiles3} }

	.mbar.file.menu add separator

    .mbar.file.menu add command -label "Exit..." -command "menu_quit" -underline 1




     bind . <Control-n> "menu_clear"
     bind . <Control-o> { menu_open "" }
     bind . <Control-m> "menu_merge"

     bind . <Control-i> "menu_import"
     bind . <Control-s> "menu_save_last"
#     bind . <Control-q> "menu_quit"


	set filehelplist { "Clear everything" "Open an AC3D file" "" "insert a model" \
		"Insert a non AC3D format file" "save the model in a different format" "Open the object library" \
		"save the current model" \
        "save the current model with a new name" "save selection as an AC3D file" "" \
        }

	if { $tcl_platform(platform) == "windows" } {
		lappend filehelplist  "Print the 3D window" ""
	}

    lappend filehelplist  "edit preferences" "" "Load a recently used model file" "" "Exit AC3D" 

	add_menu_balloons .mbar.file.menu $filehelplist

    menu .mbar.file.menu.export -tearoff 0
	# menu items are now added inside AC3D code and from within plugins
#    .mbar.file.menu.export add command -label "POV..." -command "menu_write_POV" -underline 0
#    .mbar.file.menu.export add command -label "RenderMan..." -command "menu_write_rib" -underline 0
#    .mbar.file.menu.export add command -label "VRML 1..." -command "menu_write_vrml" -underline 5
#    .mbar.file.menu.export add command -label "VRML 2..." -command "menu_write_vrml2" -underline 5
#    .mbar.file.menu.export add command -label "VRML 2 proto,.." -command "menu_write_vrml2_proto" -underline 5
#    .mbar.file.menu.export add command -label "Dive..." -command "menu_write_dive" -underline 0
#    .mbar.file.menu.export add command -label "Massive..." -command "menu_write_massive" -underline 0
 #   .mbar.file.menu.export add command -label "DVS..." -command "menu_write_DVS" -underline 1
#    .mbar.file.menu.export add command -label "Triangles..." -command "menu_write_triangles" -underline 0
#    .mbar.file.menu.export add command -label "Inventor..." -command "menu_write_inventor" -underline 1


# edit menu
    menubutton .mbar.edit -text "Edit" -menu .mbar.edit.menu -underline 0
    menu .mbar.edit.menu -tearoff 0
	set UI(menu_edit) .mbar.edit.menu
    .mbar.edit.menu add command -label "Undo" -command "ac3d undo" -accelerator Ctrl+Z -underline 0
    .mbar.edit.menu add command -label "Redo" -command "ac3d redo" -accelerator Ctrl+Y -underline 0
    .mbar.edit.menu add separator
    .mbar.edit.menu add command -label "Cut" -command "ac3d clipboard_cut" -accelerator Ctrl+X  -underline 2
    .mbar.edit.menu add command -label "Copy" -command "ac3d clipboard_copy" -accelerator Ctrl+C  -underline 0
    .mbar.edit.menu add command -label "Paste" -command "ac3d clipboard_paste" -accelerator Ctrl+V  -underline 0
    .mbar.edit.menu add command -label "Duplicate" -command "ac3d duplicate" -accelerator Ctrl+D  -underline 0

    .mbar.edit.menu add separator
    .mbar.edit.menu add command -label "Select all" -command "ac3d select_all" -accelerator Ctrl+A  -underline 7
    .mbar.edit.menu add command -label "Select nothing" -command "ac3d clear_selection"
    .mbar.edit.menu add command -label "Select/unselect by name..." -command "dialog_select_by_name"  -underline 19
    .mbar.edit.menu add command -label "Select whole surfaces" -command "ac3d select_part_selected_surfaces" -underline 2

    .mbar.edit.menu add separator
    .mbar.edit.menu add command -label "Group" -command "ac3d group"  -underline 0
    .mbar.edit.menu add command -label "Ungroup" -command "ac3d ungroup"  -underline 3


#    .mbar.edit.menu add separator
#    .mbar.edit.menu add command -label "Move to..." -command "dialog_moveto" -underline 0
#    .mbar.edit.menu add command -label "Size to..." -command "dialog_sizeto" -underline 0
#    .mbar.edit.menu add command -label "Rotate..." -command "dialog_rotate" -underline 0
#    .mbar.edit.menu add command -label "Scale..." -command "dialog_scale" -underline 1

    .mbar.edit.menu add separator
    .mbar.edit.menu add command -label "Delete" -command "ac3d delete" -accelerator Delete 
#    .mbar.edit.menu add command -label "Delete all" -command "menu_clear" 


	if { $tcl_platform(platform) == "windows" } {
		.mbar.edit.menu add separator
		.mbar.edit.menu add command -label "Copy 3D view to windows clipboard" -command "update; ac3d copy_3d_win_to_clipboard"
	}

    add_menu_balloons .mbar.edit.menu { \
        "undo this operation" \
		"redo what was undone (also Ctrl+Shift+Z)" \
        "" \
        "cut selected to the AC3D clipboard" \
        "copy selected to the AC3D clipboard" \
        "paste the AC3D clipboard" \
        "make another copy of the selected" \
        "" \
        "select everything" \
        "clear the selection" \
        "select or unselect objects by entering their names" \
        "select surfaces that have one or move vertex selected" \
        "" \
        "delete selected" "" "Copy the image in the 3D window to the Windows clipboard" }



# KEY BINDINGS
# removed any sinlge key press binding becasue they interfered with
# typing in the object name window. (code moved to keys.c)

# standard bindings for Entry widget but with break added so that
# the event doesn't permiate to the toplevel UI
# this was done because cutting and pasting into the object name or entry fields 
# caused AC3D cut.paste events to occur.
# updated for tk 8.4 (taken from Entry.tcl and break added at ends

bind Entry <<Cut>> {
    if {![catch {tk::EntryGetSelection %W} tk::Priv(data)]} {
	clipboard clear -displayof %W
	clipboard append -displayof %W $tk::Priv(data)
	%W delete sel.first sel.last
	unset tk::Priv(data)
    }
	break
}
bind Entry <<Copy>> {
    if {![catch {tk::EntryGetSelection %W} tk::Priv(data)]} {
	clipboard clear -displayof %W
	clipboard append -displayof %W $tk::Priv(data)
	unset tk::Priv(data)
    }
	break
}
bind Entry <<Paste>> {
    global tcl_platform
    catch {
	if {[string compare [tk windowingsystem] "x11"]} {
	    catch {
		%W delete sel.first sel.last
	    }
	}
	%W insert insert [::tk::GetSelection %W CLIPBOARD]
	tk::EntrySeeInsert %W
    }
	break
}



     bind . <Control-x> "ac3d clipboard_cut ; break"
     bind . <Control-c> "ac3d clipboard_copy ; break"
     bind . <Control-v> "ac3d clipboard_paste ; break"
     bind . <Control-z> "ac3d undo"
     bind . <Control-Z> "ac3d redo"
     bind . <Control-y> "ac3d redo"

     bind . <Control-a> "ac3d select_all"
     bind . <Control-d> "ac3d duplicate"
#     bind . <Delete> "ac3d delete"
#     bind . <BackSpace> "ac3d delete"




# View menu
    menubutton .mbar.view -text "View" -menu .mbar.view.menu -underline 0
    menu .mbar.view.menu -tearoff 0
	set UI(menu_view) .mbar.view.menu
    .mbar.view.menu add command -label "Zoom in" -command "ac3d zoom_in" -accelerator z -underline 5
    .mbar.view.menu add command -label "Zoom out" -command "ac3d zoom_out" -accelerator x -underline 5
    .mbar.view.menu add command -label "Fit selection" -command "ac3d fit_selected_all" -accelerator Control+F -underline 0
    .mbar.view.menu add command -label "Fit all" -command "ac3d fit_all" -accelerator Ctrl+Shift+F -underline 4
    .mbar.view.menu add command -label "Goto selection" -command "ac3d goto_selected_all" -accelerator Ctrl+G -underline 0
    .mbar.view.menu add cascade -label "Background image" -menu .mbar.view.menu.bgimage -underline 0

    menu .mbar.view.menu.bgimage -tearoff 0
    .mbar.view.menu.bgimage add cascade -label "Set" -menu .mbar.view.menu.bgimage.set -underline 0
    .mbar.view.menu.bgimage add cascade -label "Clear" -menu .mbar.view.menu.bgimage.clear -underline 0

    menu .mbar.view.menu.bgimage.set -tearoff 0
    .mbar.view.menu.bgimage.set add command -label $prefs_window_label_xy -command "ac3d file_select_background_image 0"
    .mbar.view.menu.bgimage.set add command -label $prefs_window_label_zy -command "ac3d file_select_background_image 1"
    .mbar.view.menu.bgimage.set add command -label $prefs_window_label_xz -command "ac3d file_select_background_image 2"
	.mbar.view.menu.bgimage.set add command -label "All" -command "ac3d file_select_background_image 3"

    menu .mbar.view.menu.bgimage.clear -tearoff 0
    .mbar.view.menu.bgimage.clear add command -label $prefs_window_label_xy -command "ac3d clear_background_image 0"
    .mbar.view.menu.bgimage.clear add command -label $prefs_window_label_zy -command "ac3d clear_background_image 1"
    .mbar.view.menu.bgimage.clear add command -label $prefs_window_label_xz -command "ac3d clear_background_image 2"
    .mbar.view.menu.bgimage.clear add command -label "All" -command "ac3d clear_background_image 3"


    .mbar.view.menu add separator
    .mbar.view.menu add command -label "3D view to selection" -command "ac3d set_view_3d_selection"
    .mbar.view.menu add command -label "3D view to cursor" -command "ac3d set_view_3d_cursor"
    .mbar.view.menu add command -label "3D view to origin" -command "ac3d set_view_3d_origin"
    .mbar.view.menu add separator


    .mbar.view.menu add cascade -label "Realtime mirror" -menu .mbar.view.menu.mirror -underline 0

    menu .mbar.view.menu.mirror -tearoff 0
	.mbar.view.menu.mirror add radiobutton -label "Off" -variable prefs_realtime_mirror -value 0 -command {set prefs_realtime_mirror 0 ; ac3d redraw_all} -underline 0
	.mbar.view.menu.mirror add radiobutton -label "X" -variable prefs_realtime_mirror -value 1 -command { set prefs_realtime_mirror 1 ;ac3d redraw_all} -underline 0
	.mbar.view.menu.mirror add radiobutton -label "Y" -variable prefs_realtime_mirror -value 2 -command {set prefs_realtime_mirror 2 ;ac3d redraw_all} -underline 0
	.mbar.view.menu.mirror add radiobutton -label "Z" -variable prefs_realtime_mirror -value 3 -command {set prefs_realtime_mirror 3 ;ac3d redraw_all} -underline 0


    .mbar.view.menu add separator
    .mbar.view.menu add command -label "Reset all" -command "set prefs_realtime_mirror 0; ac3d reset_views; "  -underline 0



    add_menu_balloons .mbar.view.menu { \
        "magnify the views in the orthographic windows" \
        "minify the views in the orthographic windows" \
        "set the views so that the selection fills the windows" \
		"set the views so that the whole model can be seen" \
        "centre the views around the centre of the selection" \
        "" \
        "" \
        "set the 3D view to the centre of the selection" \
        "set the 3D view to the 3D cursor" \
        "set the 3D view to the origin" \
        "" \
		"mirror the model across the selected axis" \
		"" \
        "reset the view centering, zoom levels and mirroring" }
        
	# f will fit the selction to a single view (key code in main program)
	# control f fits the selection to all views
	# control shift f fits everything to all views

     bind . <Control-f> "ac3d fit_selected_all"
	 bind . <Control-g> "ac3d goto_selected_all"
     bind . <Control-F> "ac3d fit_all"

# Object menu


    menubutton .mbar.object -text "Object" -menu .mbar.object.menu -underline 0

    menu .mbar.object.menu -tearoff 0
	set UI(menu_object) .mbar.object.menu    

	
	.mbar.object.menu add cascade -label "Texture" -menu .mbar.object.menu.texture -underline 0
	menu .mbar.object.menu.texture -tearoff 0
	.mbar.object.menu.texture add command -label "Set no texture" -command "ac3d set_no_texture"
	.mbar.object.menu.texture add command -label "Set texture repeat/offset..." -command "menu_edit_texture_repeats"
	.mbar.object.menu.texture add command -label "Load texture..." -command "ac3d file_select_texture"
	.mbar.object.menu add separator



	.mbar.object.menu add command -label "Revolve..." -command "menu_revolve" -underline 0
    .mbar.object.menu add cascade -label "Mirror" -menu .mbar.object.menu.mirror
    menu .mbar.object.menu.mirror -tearoff 0
    .mbar.object.menu.mirror add command -label "+X" -command "ac3d object_mirror 0"
    .mbar.object.menu.mirror add command -label "+Y" -command "ac3d object_mirror 1"
	.mbar.object.menu.mirror add command -label "+Z" -command "ac3d object_mirror 2"
	.mbar.object.menu.mirror add command -label "-X" -command "ac3d object_mirror 3"
	.mbar.object.menu.mirror add command -label "-Y" -command "ac3d object_mirror 4"
	.mbar.object.menu.mirror add command -label "-Z" -command "ac3d object_mirror 5"
	.mbar.object.menu.mirror add command -label "X axis" -command "ac3d object_mirror 6"
	.mbar.object.menu.mirror add command -label "Y axis" -command "ac3d object_mirror 7"
	.mbar.object.menu.mirror add command -label "Z axis" -command "ac3d object_mirror 8"



   .mbar.object.menu add separator

    .mbar.object.menu add command -label "Optimize vertices..." -command "ac3d optimize" -underline 9 -accelerator Ctrl+Shift+V
    .mbar.object.menu add command -label "Optimize surfaces..." -command "ac3d optimize_surfaces" -underline 9 
	.mbar.object.menu add command -label "Reduce..." -command "menu_reduce"
	.mbar.object.menu add separator


    .mbar.object.menu add command -label "Commit subdivision" -command "ac3d commit_subdivisions" -underline 0
    .mbar.object.menu add command -label "Subdivide once" -command "ac3d object_subdivision" -underline 1

 
    .mbar.object.menu add separator

    .mbar.object.menu add command -label "Re-centre" -command "ac3d object_recentre ; ac3d redraw_orth" -underline 3
    .mbar.object.menu add command -label "Minimum-centre" -command "ac3d object_mincentre ; ac3d redraw_orth" -underline 0
    .mbar.object.menu add separator

    .mbar.object.menu add command -label "Fragment" -command "ac3d object_fragment ; ac3d redraw_all" -underline 0
    .mbar.object.menu add command -label "Merge" -command "ac3d object_merge ; ac3d redraw_all" -underline 1 -accelerator Ctrl+Shift+M
    .mbar.object.menu add command -label "Explode..." -command "menu_object_explode" -underline 0



	bind . <Control-V> { ac3d optimize }
	bind . <Control-M> { ac3d object_merge ; ac3d redraw_all}
#	bind . <Control-F> { ac3d object_fragment ; ac3d redraw_all}
	bind . <Control-S> { ac3d object_subdivision }

#    .mbar.object.menu add command -label "Define extrude path" -command "ac3d define_extrude_path" -underline 15
 #   .mbar.object.menu add command -label "Extrude along path" -command "ac3d extrude_along_path" -underline 1

    add_menu_balloons .mbar.object.menu { \
		"handle object textures" \
		"" \
		"create a volume of revolution" \
		"add a mirror image of the geometry" \
		"" \
        "remove duplicate vertices" \
        "remove duplicate surface and polygons with 1 or 2 vertices" \
		"reduce surfaces by removing least important vertices" \
		"" \
		"subdivide the object geometry to the subdivision preview level" \
		"perform a single surface subdivision" \
		"" \
        "recalculate the centre of each object" \
        "calculate the minimum xyz coor and use this as the centre" \
        "" \
		"put each surface into an individual object" \
        "put all selected surfaces into a single object" \
        "fragment and move each new surface in the direction it faces" }

#        "define the path object (a single line)" \
#        "extrude selection along defined path"\

# Surface menu

    menubutton .mbar.surface -text "Surface" -menu .mbar.surface.menu -underline 0
    menu .mbar.surface.menu -tearoff 0
	set UI(menu_surface) .mbar.surface.menu    

    .mbar.surface.menu add command -label "Flip normal" -command "ac3d surface_flip_normals" -underline 0 -accelerator Ctrl+Shift+N
    .mbar.surface.menu add command -label "Change vertex order" -command "ac3d surface_change_vertex_order" -accelerator Ctrl+Shift+O
	#-underline 14 -accelerator Ctrl+Alt+O
    .mbar.surface.menu add separator
    .mbar.surface.menu add command -label "Flip triangle edge" -command "ac3d flip_edge" -underline 0 

#    .mbar.surface.menu add command -label "Remove triangle edge" -command "ac3d quad_from_triangle_pair" -underline 0 
    .mbar.surface.menu add separator

    .mbar.surface.menu add command -label "Divide" -command "ac3d surface_subdivide" -underline 0 -accelerator Ctrl+Shift+D
    .mbar.surface.menu add command -label "Combine" -command "ac3d combine_surfaces" -underline 0 -accelerator c


    .mbar.surface.menu add command -label "Spike..." -command "menu_spike" -underline 3
    .mbar.surface.menu add command -label "Indent..." -command menu_indent -underline 0 -accelerator i
    .mbar.surface.menu add command -label "Bevel..." -command menu_bevel -underline 0 -accelerator b
    .mbar.surface.menu add command -label "Make hole" -command "ac3d surface_make_hole" -underline 5 -accelerator Ctrl+Shift+H
    .mbar.surface.menu add command -label "Triangulate" -command "ac3d surface_triangulate"  -underline 0 -accelerator Ctrl+Shift+T
    .mbar.surface.menu add command -label "Spline" -command "ac3d surface_spline" -underline 1
    .mbar.surface.menu add separator

    .mbar.surface.menu add command -label "Cut-away object" -command "ac3d surface_create_object"  -underline 9 -accelerator Ctrl+Shift+X
    .mbar.surface.menu add command -label "Remove surface only" -command "ac3d removesurface" -underline 0 
    .mbar.surface.menu add separator

    .mbar.surface.menu add cascade -label "Remap texture coords" -menu .mbar.surface.menu.remaptexture
    menu .mbar.surface.menu.remaptexture -tearoff 0
    .mbar.surface.menu.remaptexture add command -label "XY (Front)" -command "ac3d remap_textures 0" -underline 4
    .mbar.surface.menu.remaptexture add command -label "ZY (Side)" -command "ac3d remap_textures 1" -underline 4
    .mbar.surface.menu.remaptexture add command -label "XZ (Plan)" -command "ac3d remap_textures 2" -underline 4
    .mbar.surface.menu.remaptexture add command -label "Spherical" -command "ac3d remap_textures 3" -underline 1
    .mbar.surface.menu add separator
    .mbar.surface.menu add command -label "Calculate surface area..." -command "ac3d calc_surface_area"  -underline 18


	bind . <Control-D> { ac3d surface_subdivide }
	bind . <Control-O> { ac3d surface_change_vertex_order }
	bind . <Control-N> { ac3d surface_flip_normals }
	bind . <Control-H> { ac3d surface_make_hole }
	bind . <Control-T> { ac3d surface_triangulate }
	bind . <Control-X> { ac3d surface_create_object }
#	bind . <Control-Q> { ac3d quad_from_triangle_pair }

    add_menu_balloons .mbar.surface.menu { \
        "reverse the order of the vertices so that the normal is flipped" \
        "put the first vertex to the end of the surface" \
		"" \
		"flip the edge of two adjacent triangles" \
        "" \
		"split quads into 4 smaller surfaces, triangles into 3" \
		"combine two or more surfaces together to make one surfaces" \
        "create a spike on each quad/triangle" \
		"create new surfaces aroundthe inside of each selected surface (flat bevel)"
        "create a beveled surface by creating more surfaces" \
		"make a hole in each selected surface" \
        "divide polygons into triangles" \
		"interpolate points between the current vertices" \
        "" \
		"take selected surfaces and put them into a new object" \
		"remove selected surfaces, leave the vertices"\
        "" \
		"remap texture coors along an axis" \
		"" \
		"calculate the total surface area of all selected polygons"\
		}




# Vertex menu
    menubutton .mbar.vertex -text "Vertex" -menu .mbar.vertex.menu -underline 5
    menu .mbar.vertex.menu -tearoff 0
	set UI(menu_vertex) .mbar.vertex.menu

   .mbar.vertex.menu add command -label "Snap together" -command "ac3d snaptogether"  -underline 0 -accelerator Ctrl+T
    .mbar.vertex.menu add command -label "Snap together by distance..." -command "menu_snap_together_distance"
    .mbar.vertex.menu add command -label "Snap to grid" -command "ac3d snaptogrid" -underline 8
    .mbar.vertex.menu add command -label "Snap objects by vertices" -command "ac3d object_snap_together_by_vertices" -underline 16
#    .mbar.vertex.menu add command -label "Snap object vertex pairs" -command "ac3d snap_object_vertex_pairs" -underline 19
    .mbar.vertex.menu add separator

	.mbar.vertex.menu add command -label "Smooth shape" -command "ac3d smooth_vertices"
    .mbar.vertex.menu add separator

    .mbar.vertex.menu add cascade -label "Align to axis" -menu .mbar.vertex.menu.alignaxis
    menu .mbar.vertex.menu.alignaxis -tearoff 0
    .mbar.vertex.menu.alignaxis add command -label "X" -command "ac3d align_vertices_to_axis 0" -underline 0
    .mbar.vertex.menu.alignaxis add command -label "Y" -command "ac3d align_vertices_to_axis 1" -underline 0
    .mbar.vertex.menu.alignaxis add command -label "Z" -command "ac3d align_vertices_to_axis 2" -underline 0

    .mbar.vertex.menu add separator

    .mbar.vertex.menu add command -label "Unshare" -command "ac3d unshare_vertices" -underline 0 
    .mbar.vertex.menu add command -label "Insert vertex" -command "ac3d insert_vertex" -underline 0

    .mbar.vertex.menu add command -label "Divide triangle edge" -command "ac3d divide_edge" -underline 0 

	    .mbar.vertex.menu add separator

    .mbar.vertex.menu add command -label "Create convex surface/object" -command "ac3d convexhull" -underline 0
    .mbar.vertex.menu add command -label "Create ordered surface" -command "ac3d createsurface" -underline 7 -accelerator Ctrl+Shift+S
    .mbar.vertex.menu add cascade -label "Create 2D mesh" -menu .mbar.vertex.menu.createmesh
    menu .mbar.vertex.menu.createmesh -tearoff 0
    .mbar.vertex.menu.createmesh add command -label "XY (Front)" -command "ac3d delaunay 0" -underline 4
    .mbar.vertex.menu.createmesh add command -label "ZY (Side)" -command "ac3d delaunay 1" -underline 4
    .mbar.vertex.menu.createmesh add command -label "XZ (Plan)" -command "ac3d delaunay 2" -underline 4



	bind . <Control-S> { ac3d createsurface }

    add_menu_balloons .mbar.vertex.menu { \
        "move selected vertices to the same point" \
		"move vertices together that are within a specified distance" \
        "move selected vertices to the nearest grid point" \
        "move one object to another so that the two selected vertices match position" \
		"" \
		"move the selected vertice so the overall shape is smoothed" \
		"" \
		"move all vertices to zero on the specified axis" \
		"" \
		"add new vertices so that each surface references a unique vertex" \
        "insert a new vertex between each pair of selected vertices" \
		"insert a new vertex and split the surface at that point" \
        "" \
		"make a new object with surfaces spanning selected vertices" \
		"create a single surface using vertices in order selected" \
		"create a 2D (triangle) mesh from the selected vertices" }


     bind . <Control-t> {if {$select_mode == "vertex" || $select_mode == "surface" } {ac3d snaptogether} }


# 3D menu

    menubutton .mbar.3d -text "3D" -menu .mbar.3d.menu -underline 0
    menu .mbar.3d.menu -tearoff 0
	set UI(menu_3d) .mbar.3d.menu

    .mbar.3d.menu add checkbutton -label "Wireframe" -variable prefs_view3dmode -command "ac3d redraw_3d" -underline 0 -accelerator w
    .mbar.3d.menu add checkbutton -label "Texture" -variable prefs_view3dtextures -command "ac3d redraw_3d" -underline 0 -accelerator t
    .mbar.3d.menu add checkbutton -label "Vertices" -variable prefs_show_vertices_3d -command "ac3d redraw_3d" -underline 0
    .mbar.3d.menu add checkbutton -label "Surfaces" -variable prefs_show_surfaces_3d -command "ac3d redraw_3d" -underline 0
    .mbar.3d.menu add checkbutton -label "Headlight" -variable prefs_headlight -command "ac3d redraw_3d" -underline 4 -accelerator l
    .mbar.3d.menu add checkbutton -label "Normals" -variable prefs_show_3d_normals -command "ac3d redraw_3d" -underline 0 -accelerator n
    .mbar.3d.menu add checkbutton -label "Grid" -variable prefs_grid3d_show -command "ac3d redraw_3d" -underline 0 -accelerator g
    .mbar.3d.menu add checkbutton -label "Subdivisions" -variable prefs_preview_subdivisions_3d -command "ac3d redraw_3d" -underline 3 -accelerator d
    .mbar.3d.menu add separator

    .mbar.3d.menu add checkbutton -label "Interactive wireframe" -variable prefs_wireframe_move -underline 0 
    .mbar.3d.menu add separator

    .mbar.3d.menu add checkbutton -label "Axis" -variable prefs_show_axis -command "ac3d redraw_3d" -underline 0
    .mbar.3d.menu add checkbutton -label "Show hidden" -variable prefs_show_hidden_3d -command "ac3d redraw_3d" -underline 0 
    .mbar.3d.menu add checkbutton -label "Show locked" -variable prefs_show_locked_3d -command "ac3d redraw_3d" -underline 0 

    .mbar.3d.menu add checkbutton -label "Show subdivision originals" -variable prefs_show_proxy_subdivisions_3d -command "ac3d redraw_3d" -underline 0 

    .mbar.3d.menu add separator

    .mbar.3d.menu add radiobutton -label "Spin" -variable person_walk_vehicle -value 0 -command {ac3d set_spin_viewer ; ac3d redraw_3d} -underline 0 -accelerator 1
    .mbar.3d.menu add radiobutton -label "Walk" -variable person_walk_vehicle -value 1 -command {ac3d set_walk_viewer ; ac3d redraw_3d} -underline 3 -accelerator 2
    .mbar.3d.menu add separator
    .mbar.3d.menu add command -label "FOV..." -command "dialog_FOV" -underline 2

    add_menu_balloons .mbar.3d.menu { \
        "view filled or wireframe polygons?" \
        "view textures?" \
		"view vertices (in vertex mode)"\
		"outline surfaces (when not in vertex mode)"\
        "switch on the viewing light?" \
        "show surface normals?" \
		"view the 3D grid?" \
		"preview any subdivided objects?" \
		"" \
        "display model in wireframe when dragging the mouse?" \
        "" \
        "view the axis guide?" \
        "show hidden objects?" \
        "show locked objects?" \
		"show the original surfaces and vertices from subdivided objects" \
        "" \
        "select spin view mode - mouse and cursor keys spin the model" \
        "select walk view mode - cursor keys (with alt/ctrl) move view point" \
        "" \
        "adjust the field-of-view" }



# Orth menu

    menubutton .mbar.orth -text "Orth" -menu .mbar.orth.menu -underline 0
    menu .mbar.orth.menu -tearoff 0
	set UI(menu_orth) .mbar.orth.menu

    .mbar.orth.menu add checkbutton -label "Wireframe" -variable prefs_filled_orth -onvalue 0 -offvalue 1 -command "ac3d redraw_orth" -underline 0 -accelerator w
    .mbar.orth.menu add checkbutton -label "Texture" -variable prefs_orth_textures -command "ac3d redraw_orth" -underline 0 -accelerator t
    .mbar.orth.menu add checkbutton -label "Vertices" -variable prefs_show_vertices -command "ac3d redraw_orth" -underline 0 
    .mbar.orth.menu add checkbutton -label "Surfaces" -variable prefs_show_surfaces -command "ac3d redraw_orth" -underline 0
    .mbar.orth.menu add checkbutton -label "Headlight" -variable prefs_orth_headlight -command "ac3d redraw_orth" -underline 4 -accelerator l
    .mbar.orth.menu add checkbutton -label "Normals" -variable prefs_show_normals_orth -command "ac3d redraw_orth" -underline 0 -accelerator n
    .mbar.orth.menu add checkbutton -label "Grid" -variable prefs_show_drawgrid -command "ac3d redraw_orth" -underline 0 -accelerator g
    .mbar.orth.menu add checkbutton -label "Subdivisions" -variable prefs_preview_subdivisions_orth -command "ac3d redraw_orth" -underline 3 -accelerator d

    .mbar.orth.menu add separator
    .mbar.orth.menu add checkbutton -label "Interactive wireframe" -variable prefs_move_wireframe_orth -underline 0 

    .mbar.orth.menu add separator
    .mbar.orth.menu add checkbutton -label "Col wireframe" -variable prefs_use_rgb_for_orth -command "ac3d redraw_orth" -underline 0 
    .mbar.orth.menu add separator
    .mbar.orth.menu add checkbutton -label "Object centres" -variable prefs_show_objectcentres -command "ac3d redraw_orth" -underline 0 
#    .mbar.orth.menu add checkbutton -label "Selected Vertices" -variable prefs_show_selected_vertices -command "ac3d redraw_orth" -underline 0 


    add_menu_balloons .mbar.orth.menu { \
        "view the orth model as wireframe or filled?" \
        "view textures on filled polygons?" \
		"show selectable vertices?" \
		"show selectable surface?" \
        "use viewing light?" \
		"show surface normals?" \
        "show the grid?" \
		"preview any subdivided objects?" \
        "" \
        "switch to wireframe when dragging/scaling view?" \
		"" \
        "use actual surface cols in wireframe?" \
        "" \
        "show object centres?" \
        }






# tools menu
    menubutton .mbar.tools -text "Tools" -menu .mbar.tools.menu -underline 0
    menu .mbar.tools.menu -tearoff 0
	set UI(menu_tools) .mbar.tools.menu

    if { [ac3d get_platform] == "SGI" } {
        .mbar.tools.menu add command -label "Go Stereo" -command "tools_stereo" -underline 4
        }

    .mbar.tools.menu add command -label "Model info..." -command "menu_info" -underline 6 -accelerator F6
    .mbar.tools.menu add command -label "Render..." -command "menu_render" -underline 0 -accelerator F7
    .mbar.tools.menu add command -label "Hierarchy view..." -command "menu_hier" -underline 0 -accelerator F8
    .mbar.tools.menu add command -label "Object property editor..." -command "edit_dive_data" -underline 7 -accelerator F9
    .mbar.tools.menu add command -label "Texture Coordinate Editor..." -command "menu_uveditor" -underline 0 -accelerator F10
    .mbar.tools.menu add command -label "Replicator..." -command "menu_replicator" -underline 0 -accelerator F11
    .mbar.tools.menu add command -label "Create 2D/3D text..." -command "ttfont_dialog" -underline 0 -accelerator F12
    .mbar.tools.menu add separator
#    .mbar.tools.menu add command -label "Add simple text..." -command "dialog_addtext" -underline 4


#    .mbar.tools.menu add command -label "TEST" -command "ac3d test"


    add_menu_balloons .mbar.tools.menu { \
        "display information about the current model" \
        "render the current model in an external renderer" \
        "display the model tree" \
        "edit object specific data" \
		"start the texture coordinate editor" \
        "replicate geometry" \
		"create text from a truetype font file" \
		"" \
        
	}

	bind . <F6> { menu_info }
	bind . <F7> { menu_render }
	bind . <F8> { menu_hier }
	bind . <F9> { edit_dive_data }
	bind . <F10> { menu_uveditor }
	bind . <F11> { menu_replicator }
	bind . <F12> { ttfont_dialog }


# HELP menu
    menubutton .mbar.help -text "Help" -menu .mbar.help.menu -underline 0    
    menu .mbar.help.menu -tearoff 0
    .mbar.help.menu add command -label "About AC3D..." -command "ac3d menu_about" -underline 0
    .mbar.help.menu add command -label "About Importers..." -command "menu_about_importers" -underline 1
    .mbar.help.menu add command -label "About Exporters..." -command "menu_about_exporters" -underline 1
    .mbar.help.menu add command -label "About Plugins..." -command "menu_about_plugins" -underline 6
    .mbar.help.menu add separator
	
    .mbar.help.menu add command -label "Online Homepage..." -underline 7 -command "ac3d start_web_browser http://www.ac3d.org"
    .mbar.help.menu add command -label "Online Manual..." -underline 7 -command "ac3d start_web_browser http://www.ac3d.org/manual"
	.mbar.help.menu add command -label "Online Forum..." -underline 7 -command "ac3d start_web_browser http://www.ac3d.org/forum"

    .mbar.help.menu add separator

    .mbar.help.menu add command -label "Credits..." -command "ac3d menu_credits" -underline 0
	.mbar.help.menu add separator



#    .mbar.help.menu add command -label "Check for updates..." -underline 5 -command menu_check_for_updates

#    .mbar.help.menu add separator

	if { [ac3d get_app_mode] != 2 } {
		.mbar.help.menu add command -label "Enter licence key..." -command menu_register -underline 0
	} else {
		.mbar.help.menu add command -label "Upgrade licence..." -command menu_register -underline 0
	}

    add_menu_balloons .mbar.help.menu { "Information about AC3D" "info on file importers" "info on file exporters" \
		 "plugin information" \
        "" "start up a web browser with the AC3D home page" "start a web-browser with the AC3D manual" "start web browser and goto the AC3D community forum" "" \
		"Credits" "" \
		 "enter your registration key to fully enable/upgrade AC3D" "" }

# pack the menus into the menubar

    if { $tcl_version >= 8 } {
	    set m .mbar
#		menu $m -tearoff 0 -type toplevel
		$m add cascade -menu .mbar.file.menu -label "File" -underline 0
		$m add cascade -menu .mbar.edit.menu -label "Edit" -underline 0
		$m add cascade -menu .mbar.view.menu -label "View" -underline 0
		$m add cascade -menu .mbar.object.menu -label "Object" -underline 0
		$m add cascade -menu .mbar.surface.menu -label "Surface" -underline 0
		$m add cascade -menu .mbar.vertex.menu -label "Vertex" -underline 5
		$m add cascade -menu .mbar.orth.menu -label "Orth" -underline 0
		$m add cascade -menu .mbar.3d.menu -label "3D" -underline 0
		$m add cascade -menu .mbar.tools.menu -label "Tools" -underline 0
		$m add cascade -menu .mbar.help.menu -label "Help" -underline 0

		. configure -menu .mbar -bd 1

        pack .mbar.file .mbar.edit .mbar.view .mbar.object .mbar.surface .mbar.vertex .mbar.orth .mbar.3d .mbar.tools -side left
        pack .mbar.help -side right

    } else {    
        pack .mbar.file .mbar.edit .mbar.view .mbar.object .mbar.surface .mbar.vertex .mbar.orth .mbar.3d .mbar.tools -side left
        pack .mbar.help -side right
    }



proc make_coorwindow { w menu } {

#    if ([winfo exists .coorwindow]) {
#        destroy .coorwindow
#    }

    frame $w.message_frame  -bd 2 -relief groove
    label $w.message_frame.select_mode_info -text "objects/vert selected" -textvariable select_info  -padx 0 -pady 0
    label $w.message_frame.display_line_coors -text "" -textvariable coors -padx 0 -pady 0
    label $w.message_frame.display_line_size -text "" -textvariable size -padx 0 -pady 0
    pack $w.message_frame.select_mode_info $w.message_frame.display_line_coors $w.message_frame.display_line_size  -in $w.message_frame -side top -fill x
    pack $w.message_frame -in $w -side top -fill x -expand 0 -padx 3

    bind $w.message_frame.select_mode_info <3> " tk_popup $menu %X %Y "
    bind $w.message_frame.display_line_coors <3> " tk_popup $menu %X %Y "
    bind $w.message_frame.display_line_size <3> " tk_popup $menu %X %Y "
}


proc coormenu_sep { w } {
global cp

    destroy $w
    toplevel .coorwindow
    wm transient .coorwindow [winfo toplevel [winfo parent .coorwindow]]
    if ([winfo exists .coormenu]) {
        destroy .coormenu
    }
    menu .coormenu -tearoff false 
    .coormenu add command -label "put back" -command "destroy .coorwindow; coorwindow_add_to_cp"
    make_coorwindow .coorwindow .coormenu
}

proc coorwindow_add_to_cp { } {
global cp

    if ([winfo exists .coormenu]) {
        destroy .coormenu
    }

    menu .coormenu -tearoff false 
    .coormenu add command -label "Separate coord window" -command "coormenu_sep $cp.message_frame"

    make_coorwindow $cp .coormenu
}



# main control panel



proc make_control_panel { w } {
global drag_mode draw_mode cp object_detail_frame undo_string
global UI

    coorwindow_add_to_cp

#select mode
    set select_mode_frame [ section_frame $w.select_mode_frame Select ]

    set UI(select_mode_tree) [mode_button $select_mode_frame.select_mode_tree  "Group"  "ac3d set_select_mode  0; ac3d redraw_all" select_mode tree "Select/manipulate\ngroups and objects"]
    set UI(select_mode_object) [mode_button $select_mode_frame.select_mode_object  "Object"  "ac3d set_select_mode  1 ; ac3d redraw_all" select_mode object "Select/manipulate\nwhole objects (key o)"]
    set UI(select_mode_surface) [mode_button $select_mode_frame.select_mode_surface  "Surface"  "ac3d set_select_mode  2 ; ac3d redraw_all"  select_mode surface "Select/manipulate individual\nsurfaces (key s)"]
    set UI(select_mode_vertex) [mode_button $select_mode_frame.select_mode_vertex  "Vertex"  "ac3d set_select_mode  3 ; ac3d redraw_all"  select_mode vertex "Select/manipulate individual\nvertices (key v)"]

    pack $UI(select_mode_tree) $UI(select_mode_object) $UI(select_mode_surface) $UI(select_mode_vertex) -side left\
         -fill x -expand 1
#grid $UI(select_mode_tree) -row 0 -column 0 -sticky we
#grid $UI(select_mode_object) -row 0 -column 1 -sticky we
#grid $UI(select_mode_surface) -row 0 -column 2 -sticky we
#grid $UI(select_mode_vertex) -row 0 -column 3 -sticky we

#    label $w.selection_title -text "Selection:"  -padx 0 -pady 0
    vert_pack $w.select_mode_frame 
	#$w.selection_title




	# $w.select_lock_frame

    # group/ungroup
#    frame $w.function_frame1
#    set UI(function_group) [cbutton $w.function_group  "Group"  "ac3d group" "Create a group object with selected as children"]
#    set UI(function_ungroup) [cbutton $w.function_ungroup  "Ungroup"  "ac3d ungroup" "Destroy group object leaving children"]
#    horiz_pack $w.function_frame1 $w.function_group $w.function_ungroup 

#   pack $w.function_frame1 -in $w.select_mode_frame  -side top -fill both -expand 1 -pady 3




# drag mode
#    section_frame $w.drag_mode_frame
#    label $w.drag_mode_title -text "Mode:"  -padx 0 -pady 0
#    pack $w.drag_mode_title  -side top -in $w.drag_mode_frame

    set drag_mode move



#    horiz_pack $w.drag_mode_frame $w.drag_mode_move $w.drag_mode_rotate $w.drag_mode_extrude 
#    grid  $w.drag_mode_move -in $w.drag_mode_frame -row 0 -column 0 -sticky we
#    grid  $w.drag_mode_rotate -in $w.drag_mode_frame -row 0 -column 1 -sticky we
#    grid  $w.drag_mode_extrude -in $w.drag_mode_frame -row 0 -column 2 -sticky we

#grid columnconfigure $w.drag_mode_frame 0 -minsize 60
#grid columnconfigure $w.drag_mode_frame 1 -minsize 60
#grid columnconfigure $w.drag_mode_frame 2 -minsize 60


#pack $w.drag_mode_frame

    set modeframe [ section_frame $w.draw_mode_frame Mode ]
#    label $w.draw_mode_frame.drag_mode_title -text "Mode:"  -padx 0 -pady 0
#    pack $w.draw_mode_frame.drag_mode_title  -side top

#    frame $w.draw_mode_frame_col3

#    set modeframe $w.draw_mode_frame 


    frame $modeframe.draw_mode_frame_col1
pack $modeframe.draw_mode_frame_col1 -side top -fill both -expand 1


    mode_button $modeframe.drag_mode_move  "MoveSize"  "ac3d set_draw_mode  move" draw_mode move "Left mouse button will select/drag selection, handles resize (key = m)"
    mode_button $modeframe.drag_mode_rotate  "Rotate"  "ac3d set_draw_mode  rotate" draw_mode rotate "Left mouse button will select/rotate\nselection about mouse pos (key = r)"
    mode_button $modeframe.drag_mode_extrude  "Extrude"  "ac3d set_draw_mode  extrude" draw_mode extrude "Left mouse button will select/extrude selected surfaces to release point (key = e)"
    set UI(extrude_button) $modeframe.drag_mode_extrude

    mode_button $modeframe.draw_mode_polygon  "Poly"  "ac3d set_draw_mode  polygon"  draw_mode polygon "Draw a polygon\n (Click left mouse button for each vertex,\n right or middle button to finish)" true
    mode_button $modeframe.draw_mode_polyline  "Polyline"  "ac3d set_draw_mode  polyline"  draw_mode polyline "Draw a polygon outline\n (Click left mouse button for each vertex,\n right or middle button to finish)" true
    mode_button $modeframe.draw_mode_line "Line"   "ac3d set_draw_mode  line"  draw_mode line "Hand draw a line\n (Click left mouse button for each vertex,\n right or middle button to finish)" true
    mode_button $modeframe.draw_mode_rect  " Rect "  "ac3d set_draw_mode  rect"  draw_mode rect "Draw a rectangle" true
    mode_button $modeframe.draw_mode_circle  "Ellipse"  "ac3d set_draw_mode  circle"  draw_mode circle "Draw an circle/ellipse" true
    mode_button $modeframe.draw_mode_disk  "Disk"  "ac3d set_draw_mode  disk"  draw_mode disk "Draw a disk" true
    mode_button $modeframe.draw_mode_cube  "Box"  "ac3d set_draw_mode  box"  draw_mode box  "Draw a cuboid" true
    mode_button $modeframe.draw_mode_sphere  "Sphere"  "ac3d set_draw_mode  sphere" draw_mode sphere "Draw a sphere" true
    mode_button $modeframe.draw_mode_cone  "Cylinder"  "ac3d set_draw_mode  cylinder"  draw_mode cylinder "Draw a cylinder" true 
    mode_button $modeframe.draw_mode_mesh  "Mesh"  "ac3d set_draw_mode  mesh"  draw_mode mesh "Draw a mesh" true
    mode_button $modeframe.draw_mode_grid  "Grid"  "ac3d set_draw_mode  grid"  draw_mode grid "Draw a gridline object" true
    mode_button $modeframe.draw_mode_light  "Light"  "ac3d set_draw_mode  light"  draw_mode light "Click to position a new light" true


    grid $modeframe.drag_mode_move -in $modeframe.draw_mode_frame_col1 -row 0 -column 0 -sticky we
    grid $modeframe.drag_mode_rotate -in $modeframe.draw_mode_frame_col1 -row 0 -column 1 -sticky we
    grid $modeframe.drag_mode_extrude -in $modeframe.draw_mode_frame_col1 -row 0 -column 2 -sticky we
    grid $modeframe.draw_mode_polygon -in $modeframe.draw_mode_frame_col1 -row 1 -column 0 -sticky we
    grid $modeframe.draw_mode_polyline -in $modeframe.draw_mode_frame_col1 -row 1 -column 1 -sticky we 
    grid $modeframe.draw_mode_line -in $modeframe.draw_mode_frame_col1 -row 1 -column 2 -sticky we
    grid $modeframe.draw_mode_circle -in $modeframe.draw_mode_frame_col1 -row 2 -column 0 -sticky we
    grid $modeframe.draw_mode_disk -in $modeframe.draw_mode_frame_col1 -row 2 -column 1 -sticky we
    grid $modeframe.draw_mode_rect -in $modeframe.draw_mode_frame_col1 -row 2 -column 2 -sticky we
    grid $modeframe.draw_mode_grid -in $modeframe.draw_mode_frame_col1 -row 3 -column 0 -sticky we
    grid $modeframe.draw_mode_mesh -in $modeframe.draw_mode_frame_col1 -row 3 -column 1 -sticky we
    grid $modeframe.draw_mode_cube -in $modeframe.draw_mode_frame_col1 -row 3 -column 2 -sticky we
    grid $modeframe.draw_mode_cone -in $modeframe.draw_mode_frame_col1 -row 4 -column 0 -sticky we
    grid $modeframe.draw_mode_sphere -in $modeframe.draw_mode_frame_col1 -row 4 -column 1 -sticky we
    grid $modeframe.draw_mode_light -in $modeframe.draw_mode_frame_col1 -row 4 -column 2 -sticky we

	grid columnconfigure $modeframe.draw_mode_frame_col1 0 -minsize 50
	grid columnconfigure $modeframe.draw_mode_frame_col1 1 -minsize 50
	grid columnconfigure $modeframe.draw_mode_frame_col1 2 -minsize 50

	grid rowconfigure $modeframe.draw_mode_frame_col1 0 -pad 5
	grid rowconfigure $modeframe.draw_mode_frame_col1 1 -pad 10




trace variable draw_mode w draw_mode_changed

frame $w.object_detail_frame -bd 0

set object_detail_frame $w.object_detail_frame


    set undo_string "UNDO"




	# HIDE/LOCK
    set hide_frame [section_frame $w.select_hide_frame Visibility ]
	set hide_label [label $hide_frame.hide_label -text "Hide: "]
    set UI(hide_selected) [cbutton $hide_frame.hide_selected " Sel " "ac3d hide_selected" "Hide selected objects from the display"]
    set UI(hide_unselected) [cbutton $hide_frame.hide_unselected " Unsel " "ac3d hide_unselected" "Hide objects not selected"]
    set UI(unhide) [cbutton $hide_frame.unhide " None " "ac3d unhide" "Restore hidden objects"]

#    set UI(hide_in_3d) [ checkbutton $hide_frame.hide_in_3d -pady 0 -indicatoron 0  -text "3D" -variable prefs_show_hidden_3d -highlightthickness 1 -padx 0 -pady 0 -command "ac3d redraw_3d"]



set UI(hide_in_3d) [ switch_button $hide_frame.hide_in_3d "3D" "ac3d redraw_3d" prefs_show_hidden_3d "show hidden objects in 3D window" ]
    add_balloon $UI(hide_in_3d) "show hidden objects in 3D window"

	grid $hide_label  -row 0 -column 0 -sticky w
	grid $UI(hide_selected)  -row 0 -column 1 -sticky w
	grid $UI(hide_unselected)  -row 0 -column 2 -sticky w
	grid $UI(unhide) -row 0 -column 3 -sticky w
	grid $UI(hide_in_3d) -row 0 -column 4 -sticky w


	set lock_label [label $hide_frame.lock_label -text "Lock: "]
    set UI(lock_selected) [cbutton $hide_frame.lock_selected " Sel " "ac3d lock_selected" "lock selected objects"]
    set UI(lock_unselected) [cbutton $hide_frame.lock_unselected " Unsel " "ac3d lock_unselected" "lock objects not selected"]
    set UI(unlock) [cbutton $hide_frame.unlock " None " "ac3d unlock" "Unlock all objects"]

#    set UI(lock_in_3d) [ checkbutton $hide_frame.lock_in_3d -indicatoron 0  -text "3D" -variable prefs_show_locked_3d -highlightthickness 1 -padx 0 -pady 0 -command "ac3d redraw_3d"]
	set UI(lock_in_3d) [ switch_button $hide_frame.lock_in_3d "3D" "ac3d redraw_3d" prefs_show_locked_3d "show locked objects in 3D window"]
    add_balloon $hide_frame.lock_in_3d "show locked objects in 3D window"


	grid $lock_label  -row 1 -column 0 -sticky w
	grid $UI(lock_selected)  -row 1 -column 1 -sticky w
	grid $UI(lock_unselected) -row 1 -column 2 -sticky w
	grid $UI(unlock) -row 1 -column 3 -sticky w
	grid $UI(lock_in_3d) -row 1 -column 4 -sticky w








    set surface_frame [section_frame $w.surface_type_frame "Set Surface type"]
 
    set sf2 [frame $surface_frame.surface_type_frame2 -bg red]

    set UI(function_surface_poly) [cbutton $sf2.function_surface_poly  "Poly"  "ac3d set_surface_type 0 ; ac3d redraw_all"]
    add_balloon $sf2.function_surface_poly "Set selected surfaces to polygons"
    set UI(function_surface_polyline) [cbutton $sf2.function_surface_polyline  "Polyline"  "ac3d set_surface_type 1 ; ac3d redraw_all"]
    add_balloon $sf2.function_surface_polyline "Set selected surfaces to closed lines"
    set UI(function_surface_line) [cbutton $sf2.function_surface_line  "Line"  "ac3d set_surface_type 2 ; ac3d redraw_all"]
    add_balloon $sf2.function_surface_line "Set selected surfaces to lines"

    horiz_pack $sf2 $sf2.function_surface_poly $sf2.function_surface_polyline $sf2.function_surface_line


    set sf3 [frame $surface_frame.surface_type_frame3 ]
    set UI(function_surface_smooth) [ cbutton $sf3.function_surface_smooth "Smooth" "ac3d surface_set_shaded 1 ; ac3d redraw_all"]
    add_balloon $sf3.function_surface_smooth "Set smooth-shaded surfaces"
    set UI(function_surface_flat) [cbutton $sf3.function_surface_flat "Flat" "ac3d surface_set_shaded 0 ; ac3d redraw_all"]
    add_balloon $sf3.function_surface_flat "Set flat-shaded surfaces"
    set UI(function_surface_one_sided) [ cbutton $sf3.function_surface_one_sided "1S" "ac3d surface_set_twosided 0 ; ac3d redraw_all"]
    add_balloon $sf3.function_surface_one_sided "Set single-sided surfaces"
    set UI(function_surface_two_sided) [cbutton $sf3.function_surface_two_sided "2S"  "ac3d surface_set_twosided 1 ; ac3d redraw_all"]
    add_balloon $sf3.function_surface_two_sided "Set double-sided surfaces"

    horiz_pack $sf3 \
        $sf3.function_surface_smooth $sf3.function_surface_flat $sf3.function_surface_one_sided $sf3.function_surface_two_sided

    vert_pack $surface_frame $sf2 $sf3


# the palette


	make_palette $w.palette_frame



# object data
    frame $w.other_frame3
    label $w.current_ob_label -text "Obj name:"
    add_balloon $w.current_ob_label "Name of current object"


    entry $w.current_object_name_entry -relief sunken -bd 2 -textvariable current_object_name
    bind $w.current_object_name_entry <KeyRelease> {
        ac3d set_current_object_name $current_object_name
        hier_update
        }

    pack $w.current_ob_label\
        -in $w.other_frame3 -side left -fill x -expand 0 -pady 3
    pack $w.current_object_name_entry\
        -in $w.other_frame3 -side left -fill x -expand 1 -pady 3


    pack   $w.select_mode_frame  -in $w -side top -fill x -expand 0
    pack  $w.draw_mode_frame -in $w -side top -fill x -expand 0
    pack $w.object_detail_frame -in $w -side top -fill x -expand 0

    
    pack  $w.palette_frame $w.other_frame3 $w.surface_type_frame  $w.select_hide_frame \
	  -in $w -side bottom -fill x -expand 0






}


make_control_panel .cp        




# in ac3dview.tcl:
make_views 



proc viewbutton {name label value } {
    radiobutton $name -text $label -command configure_views -value $value \
    -variable viewsetting -indicatoron false -highlightthickness 0
}



# put some most used funtions in the toolbar

    cbutton .toolbar.select_all "All" "ac3d select_all"
    add_balloon .toolbar.select_all "select everything"
    cbutton .toolbar.select_none "None" "ac3d clear_selection"
    add_balloon .toolbar.select_none "unselect everything"
label .toolbar.sp0 -text ""
#    cbutton .toolbar.delete "Del" "ac3d delete"
#    add_balloon .toolbar.delete "destroy selected"

#label .toolbar.sp1 -text ""
    cbutton .toolbar.duplicate "Dup" "ac3d duplicate"
    add_balloon .toolbar.duplicate "make another copy of the selection"

label .toolbar.sp1a -text ""
    cbutton .toolbar.cut "Cut" "ac3d clipboard_cut"
    add_balloon .toolbar.cut "Cut the selection to the clipboard"
    cbutton .toolbar.copy "Copy" "ac3d clipboard_copy"
    add_balloon .toolbar.copy "Copy the selection to the clipboard"
    cbutton .toolbar.paste "Paste" "ac3d clipboard_paste"
    add_balloon .toolbar.paste "paste a copy of the clipboard\nand make it the selection"
label .toolbar.sp2 -text ""


    set UI(function_group) [cbutton .toolbar.function_group  "Group"  "ac3d group" "Create a group object with selected as children"]
    set UI(function_ungroup) [cbutton .toolbar.function_ungroup  "Ungroup"  "ac3d ungroup" "Destroy group object leaving children"]



#    cbutton .toolbar.fitsel "Fit sel" "ac3d fit_selected_all"
#    add_balloon .toolbar.fitsel "alter views to fit selection"
#    cbutton .toolbar.fitall "Fit all" "ac3d fit_all"
#    add_balloon .toolbar.fitall "alter views to fit all"
#label .toolbar.sp3 -text " "

#renmoved these because they don't make sense for the 3d window
#    cbutton .toolbar.zoomin ">" "ac3d zoom_in"
#    add_balloon .toolbar.zoomin "zoom in"
#    cbutton .toolbar.zoomout "<" "ac3d zoom_out"
#    add_balloon .toolbar.zoomout "zoom out"


label .toolbar.sp4 -text "Flip:"


    cbutton .toolbar.function_mirrorx  "X"  "ac3d mirror 0" 
    add_balloon .toolbar.function_mirrorx "Flip selected about X axis"
    cbutton .toolbar.function_mirrory  "Y"  "ac3d mirror 1"
    add_balloon .toolbar.function_mirrory "Flip selected about Y axis"
    cbutton .toolbar.function_mirrorz  "Z"  "ac3d mirror 2"
    add_balloon .toolbar.function_mirrorz "Flip selected about Z axis"


    label .toolbar.sp5 -text ""

    cbutton .toolbar.function_double_size  "200%"  "ac3d scale  2.0 2.0 2.0"
    add_balloon .toolbar.function_double_size "Double size of selected\n(Key = } )"

    cbutton .toolbar.function_half_size  "50%"  "ac3d scale 0.5 0.5 0.5"
    add_balloon .toolbar.function_half_size "Half size of selected\n(key = { )"

    cbutton .toolbar.function_plus_size  "+10%"  "ac3d scale 1.1 1.1 1.1"
    add_balloon .toolbar.function_plus_size "Increase size of selected\n(key = \[)"

    cbutton .toolbar.function_minus_size  "-10%"  "ac3d scale 0.9 0.9 0.9"
    add_balloon .toolbar.function_minus_size "Decrease size of selected\n(key = \])"


#    .toolbar.fitsel .toolbar.fitall .toolbar.sp3 \


pack .toolbar.select_all .toolbar.select_none  .toolbar.sp0 \
    .toolbar.duplicate .toolbar.sp1a .toolbar.cut .toolbar.copy  .toolbar.paste .toolbar.sp2 \
	$UI(function_group) $UI(function_ungroup) .toolbar.sp4 \
    .toolbar.function_mirrorx .toolbar.function_mirrory \
    .toolbar.function_mirrorz \
.toolbar.sp5 .toolbar.function_double_size .toolbar.function_half_size\
.toolbar.function_plus_size .toolbar.function_minus_size\
    -side left -padx 1 -pady 1



mode_button .toolbar.all "ALL" configure_views viewsetting all "Tile all windows \[F5]"
mode_button .toolbar.xyz "3D" configure_views viewsetting xyz  "$prefs_window_label_xyz \[F4]"
mode_button .toolbar.xy "XY" configure_views viewsetting xy "$prefs_window_label_xy \[F1]"
mode_button .toolbar.zy "ZY" configure_views viewsetting zy "$prefs_window_label_zy \[F2]"
mode_button .toolbar.xz "XZ" configure_views viewsetting xz  "$prefs_window_label_xz \[F3]"

bind . <F1> { set viewsetting xy ; configure_views ; display_message "Front XY"}
bind . <F2> { set viewsetting zy ; configure_views ; display_message "Side ZY"}
bind . <F3> { set viewsetting xz ; configure_views ; display_message "Plan XZ"}
bind . <F4> { set viewsetting xyz ; configure_views ; display_message "3D XYZ"}
bind . <F5> { set viewsetting all ; configure_views ; display_message "All views"}

pack .toolbar.all -side right 
pack .toolbar.xyz -side right 
pack .toolbar.xz -side right 
pack .toolbar.zy -side right
pack .toolbar.xy -side right


    # bottombar

#switch_button .setting_show_centres "+" "ac3d redraw_orth" prefs_show_objectcentres  "Show/hide object centres"
#switch_button .setting_show_grid "Grid" "" prefs_show_drawgrid "Switch drawn grid"
switch_button .setting_gridlock "Gridsnap  " ""  prefs_gridsnap "snap to grid when drawing and dragging"
switch_button .setting_snapnear "Nearsnap  " "" prefs_snap_near "snap to near points when drawing\npolys/polylines/lines"
switch_button .setting_realtimefeedback "Feedback  " "" prefs_realtime_manipulation "show realtime manipulation or drag a box?"
switch_button .setting_antialias_3d "AA 3D  " "ac3d redraw_3d" prefs_antialias_3d "draw 3d window antialiased (smoothed)? (rendering in 3D window will be slower)"

    pack \
       .setting_gridlock .setting_snapnear .setting_realtimefeedback .setting_antialias_3d \
	   -in .bottombar -side left -padx 1 -pady 1



    canvas .progressbar -width 3c -height 0.3c -relief sunken -bd 0
     .progressbar create rectangle 0 0 0 0 -fill blue -tags rect 
#     .progressbar create text 20 10  -fill black -tags progresstext -text hello
    add_balloon .progressbar "This is a progress bar"
    pack .progressbar -in .bottombar -side right

    label .message -textvariable message -width 60
    add_balloon .message "This is a message area"
    pack .message -in .bottombar -side right -fill x 
set message_bg [.message cget -background]
set message_fg [.message cget -foreground]







proc get_cmd {sock} {
global prefs_allow_socket_commands
global errorInfo

    gets $sock cmd

    set result Error

    if { !$prefs_allow_socket_commands } {
        set result "Socket commands not authorized - settings->allow_socket_commands is not enabled"
    } else {
		if { $cmd == "exit" } {
			set cmd menu_quit
		}
        catch { set result [ uplevel #0 $cmd ] } errmsg
        if {$errmsg != 0} { set result $errmsg }
    }

    set len [string length $result]
    if { $len == 0 } {
       puts $sock "0"
    } else {
        # remember to add one to the length of the string
        puts $sock [expr $len+1]
    }

    set err [catch {flush $sock} ]
    if {$err == 1} 	{ 
        puts "Socket connection $sock gone"; close $sock ; return;
        }
    if { $len > 0 } {
        set err [catch {puts $sock $result}]
        set err [catch {flush $sock}]
        }

}


proc send_sock { str } {
global rsock

    set len [string length $str]
    puts $rsock $len
    flush $rsock
    if {$len > 0} {
        puts $rsock $str
        flush $rsock
        }
}



set listensock 0
set rsock 0

proc do_connection {sock ip port} {

	fileevent $sock readable "get_cmd $sock"
	puts "New socket connected to AC3D (socket id $sock, remote ip:port $ip:$port)"

	close_current_command_socket
	set_command_socket $sock
}


proc enable_socket_commands {} {

	global prefs_tcl_socket_port
	global listensock

	set sock 0

	set err [catch {set sock [socket -server do_connection $prefs_tcl_socket_port]} ]

	if { $err != 0 } {
		set sock 0
	}

	if { $sock != 0 } {
		puts "tcl socket id: $sock"
	}

	set listensock $sock
}


proc disable_socket_commands {} {

	global listensock

	if { $listensock != 0 } {

		close_current_command_socket

		set err [catch { close $listensock }]
		set listensock 0
	}

	puts "Command socket connections disabled"
}


proc set_command_socket { sock } {

	global rsock

	set rsock $sock
}


proc close_current_command_socket {} {

	global rsock

	if { $rsock != 0 } {
		set err [catch { close $rsock }]
		puts "tcl socket $rsock disconnected by AC3D"
		set rsock 0
	}
}


proc set_socket_commands {} {

	global prefs_allow_socket_commands
	global prefs_tcl_socket_port
	global listensock

	if { $prefs_allow_socket_commands } {

		if { $listensock != 0 } {
			return
		} else {
			enable_socket_commands
		}

		if { $listensock != 0 } {
			puts "Command socket connections enabled on port $prefs_tcl_socket_port"
		} else {
			puts "Failed to enable AC3D tcl socket on port $prefs_tcl_socket_port"
		}

	} else {
		disable_socket_commands
	}
}

# init command socket settings
set_socket_commands



#fconfigure $sock -blocking true
#puts "tcl_socket_port $prefs_tcl_socket_port"












if { [file exists "tcl/patch.tcl"]} { 
    source "tcl/patch.tcl"
} 



set last_draw_mode nothing



proc setting_spin { parent var { text "" } { balloon "" } {from 0} {to ""} { width ""} } {

    set f [ frame $parent.fr$var ]

    label $f.lel$var -text $text
    if { $balloon != "" } {
        add_balloon $f.lel$var $balloon
        }

#   entry $f.le$var -relief sunken -bd 2 -textvariable $var

	spinbox  $f.le$var -textvariable $var -from $from -to $to -width $width
    if {$width != ""} {
        $f.le$var configure -width $width
    }

    pack $f.lel$var $f.le$var -side left
    pack $f -side top
    return $f
}


proc setting_edit { parent var { text "" } { balloon "" } { width ""} } {

    set f [ frame $parent.fr$var ]

    label $f.lel$var -text $text
    if { $balloon != "" } {
        add_balloon $f.lel$var $balloon
        }

   entry $f.le$var -relief sunken -bd 2 -textvariable $var

    if {$width != ""} {
        $f.le$var configure -width $width
    }

    pack $f.lel$var $f.le$var -side left
    pack $f -side top
    return $f
}

proc setting_bool { parent var { text "" } { balloon "" } } {

    checkbutton $parent.label_edit_check$var -text $text -variable $var 
    if { $balloon != "" } {
        add_balloon $parent.label_edit_check$var $balloon
        }
   pack $parent.label_edit_check$var -side top
   return $parent.label_edit_check$var
}

proc setting_title { parent text } {
    label $parent.label$text -text $text
    pack $parent.label$text -side top
}


set move_x 0
set move_y 0
set move_z 0
set size_x 1
set size_y 1
set size_z 1
set rotate_angle 90

set scale_x 100
set scale_y 100
set scale_z 100

proc draw_mode_changed { name1 name2 op } {
global draw_mode last_draw_mode object_detail_frame drag_mode last_drag_mode

#puts "ac3d.tcl:draw_mode_changed drawmode $draw_mode  dragmode $drag_mode"

#pack propagate $object_detail_frame  0

if {$name1 == "draw_mode" } {
    if {$draw_mode == $last_draw_mode } {return}

    # remove any widgets that are in there
    set todie [winfo children $object_detail_frame]
    foreach die $todie { destroy $die }

    if {$draw_mode == "sphere"} {
        setting_title $object_detail_frame "Sphere parameters:"
        set s1 [setting_spin $object_detail_frame prefs_num_surface_div "Segments" "number segments of new sphere" 3 999 3]
        set s2 [setting_bool $object_detail_frame prefs_sphere_triangles "triangles" "construct new spheres from triangles or quads?"]
        pack $s1 $s2 -side left
    } elseif { $draw_mode == "mesh" } {
        setting_title $object_detail_frame "Mesh parameters:"
        set m3 [setting_bool $object_detail_frame prefs_mesh_triangles "triangles" "construct new meshes from triangles or quads?"]
        pack $m3 -side bottom

        set m1 [setting_spin  $object_detail_frame prefs_meshx "Mesh X" "X sections for squares in new 'Mesh' objects" 1 999 3]
        set m2 [setting_spin $object_detail_frame  prefs_meshy "Mesh Y" "Y sections for squares in new 'Mesh' objects" 1 999 3]
        pack $m1 $m2 -side left
    } elseif { $draw_mode == "grid" } {
        setting_title $object_detail_frame "Grid parameters:"
        set g1 [setting_spin  $object_detail_frame prefs_grid_object_x "Grid X" "X sections for squares in new grid" 1 999 3]
        set g2 [setting_spin $object_detail_frame  prefs_grid_object_y "Grid Y" "Y sections for squares in new grid" 1 999 3]
        pack $g1 $g2 -side left

    } elseif { $draw_mode == "circle"} {
        setting_title $object_detail_frame "Parameters:"
        setting_spin $object_detail_frame prefs_ellipse_segments "Ellipse points" "number of points in circle" 3 255 3
    } elseif { $draw_mode == "disk"} {
        setting_title $object_detail_frame "Parameters:"
        setting_spin $object_detail_frame prefs_disk_segments "Disk segments" "segments of disk" 3 999 4
    } elseif { $draw_mode == "cylinder"} {
        setting_title $object_detail_frame "Parameters:"
        setting_spin $object_detail_frame prefs_cylinder_segments "Cylinder segments" "segments of cylinder" 3 999 3
		set m3 [setting_bool $object_detail_frame prefs_cylinder_has_ends "cap ends" "add surfaces to the ends of the new cylinder?"]

    } elseif {$draw_mode == "extrude"} {
        setting_title $object_detail_frame "Extrude parameters:"
        set s1 [setting_spin $object_detail_frame prefs_extrude_num "Sections" "number sections in extrude" 1 999 3]
        set s2 [setting_bool $object_detail_frame prefs_extrude_cap_end "Cap end" "Cap the end of the extrusion?"]
		set s4 [setting_bool $object_detail_frame prefs_extrude_flip_cap "Flip new surfaces" "Flip the direction of the new surfaces?"]
		set s3 [setting_bool $object_detail_frame prefs_extrude_delete_original "Remove original" "Remove the original surfaces?"]


    } elseif {$draw_mode == "rotate"} {
		

        set lab3 [label $object_detail_frame.rotate -text "Numeric :"]
		grid config $lab3 -row 0 -column 0 -columnspan 4

#        set s1 [setting_edit  $object_detail_frame rotate_x "x" "X angle (degrees)" 3]
#        set s2 [setting_edit  $object_detail_frame rotate_y "y" "Y angle (degrees)" 3]
#        set s3 [setting_edit  $object_detail_frame rotate_z "z" "Z angle (degrees)" 3]
#		set sb [cbutton $object_detail_frame.rb1 "Rotate"  {ac3d rotate $rotate_x $rotate_y $rotate_z; ac3d redraw_all} "rotate the selection\nabout the centre of the selection"]

#		grid config $s1 -row 0 -column 0 -sticky e 
#		grid config $s2 -row 0 -column 1 -sticky e
#		grid config $s3 -row 0 -column 2 -sticky e
#		grid config $sb -row 0 -column 3 -sticky we -pady 3 -columnspan 3


		# rotate axis angle
		set rf [frame $object_detail_frame.rframe]
		set s1 [label $rf.tit -text "  axis: "]
		tk_optionMenu $rf.axis rotate_axis x y z
		set s2 $rf.axis
		pack $s1 $s2 -side left 

		set s3 [entry $object_detail_frame.angle -relief sunken -bd 2 -width 5 -textvariable rotate_angle]

		set sb [cbutton $object_detail_frame.rb1 "Rotate"  {if {$rotate_axis == "x" } {set a 0}; if {$rotate_axis == "y"} {set a 1}; if {$rotate_axis == "z"} {set a 2}; ac3d rotate_axis_angle $a $rotate_angle; ac3d redraw_all} "rotate the selection around this axis (degs)"]
		grid config $rf -row 10 -column 0 -sticky e -columnspan 3 
		grid config $s3 -row 10 -column 3 -sticky e
		grid config $sb -row 10 -column 4 -sticky we -pady 3 -columnspan 3


    } elseif {$draw_mode == "move"} {


#        set lab3 [label $object_detail_frame.rotate -text "Numeric :"]#
#		grid config $lab3 -row 0 -column 0 -columnspan 4

		set labx [label $object_detail_frame.labx -text "x" -pady 0]
		set laby [label $object_detail_frame.laby -text "y"  -pady 0]
		set labz [label $object_detail_frame.labz -text "z"  -pady 0]
		grid config $labx -row 0 -column 1 -sticky we
		grid config $laby -row 0 -column 2 -sticky we
		grid config $labz -row 0 -column 3 -sticky we


        set m1 [setting_edit  $object_detail_frame move_x "" "X cor" 4]
        set m2 [setting_edit  $object_detail_frame move_y "" "Y cor" 4]
        set m3 [setting_edit  $object_detail_frame move_z "" "Z cor" 4]
		set mb [cbutton $object_detail_frame.sb1 "Move to"  {ac3d move_to  $move_x $move_y $move_z} "Move the centre of the selection to this point"]


		set mget [cbutton $object_detail_frame.mget ">"  {    set pos [ac3d get_selection_centre]; set move_x [lindex $pos 0];\
			set move_y [lindex $pos 1];\
			set move_z [lindex $pos 2]} "fill these fields with the current selection centre"]

		grid config $m1 -row 1 -column 1 -sticky e
		grid config $m2 -row 1 -column 2 -sticky e
		grid config $m3 -row 1 -column 3 -sticky e
		grid config $mb -row 1 -column 4 -sticky we -pady 3 -columnspan 3
		grid config $mget -row 1 -column 0 -sticky e

		set s1 [setting_edit  $object_detail_frame size_x "" "X width" 4]
        set s2 [setting_edit  $object_detail_frame size_y "" "Y height" 4]
        set s3 [setting_edit  $object_detail_frame size_z "" "Z depth" 4]
		set sb [cbutton $object_detail_frame.sb2 "Size to"  {ac3d size_to  $size_x $size_y $size_z} "Size the selection to these dimensions"]
		set sget [cbutton $object_detail_frame.sget ">"  {    set pos [ac3d get_selection_size]; set size_x [lindex $pos 0];\
			set size_y [lindex $pos 1];\
			set size_z [lindex $pos 2]} "fill these fields with the current selection size"]

#	set sp1 [label  $object_detail_frame.sp1 -text " " -pady 0]
#		grid config $sp1 -row 3 -column 0 -sticky e


		grid config $sget -row 4 -column 0 -sticky e
		grid config $s1 -row 4 -column 1 -sticky e
		grid config $s2 -row 4 -column 2 -sticky e
		grid config $s3 -row 4 -column 3 -sticky e
		grid config $sb -row 4 -column 4 -sticky we -pady 3 -columnspan 3





        set s1 [setting_edit  $object_detail_frame scale_x "" "X scale percentage" 4]
        set s2 [setting_edit  $object_detail_frame scale_y "" "Y scale percentage" 4]
        set s3 [setting_edit  $object_detail_frame scale_z "" "Z scale percentage" 4]
		set sb [cbutton $object_detail_frame.sc1 "Scale"  {ac3d scale [expr $scale_x/100.0] [expr $scale_y/100.0] [expr $scale_z/100.0]} "scale the selection\n(100% means there will be no change)"]
set sl [label $object_detail_frame.sl -text "%" ]
#	set sp3 [label  $object_detail_frame.sp3 -text " " -pady 0]
		grid config $sl -row 7 -column 0 -sticky e

		grid config $s1 -row 7 -column 1 -sticky e 
		grid config $s2 -row 7 -column 2 -sticky e 
		grid config $s3 -row 7 -column 3 -sticky e
		grid config $sb -row 7 -column 4 -sticky we  -pady 3 -columnspan 3



		# rotate axis angle
		set rf [frame $object_detail_frame.rframe]
		set s1 [label $rf.tit -text "  axis: "]
		tk_optionMenu $rf.axis rotate_axis x y z
		set s2 $rf.axis
		pack $s1 $s2 -side left 

		set s3 [entry $object_detail_frame.angle -relief sunken -bd 2 -width 5 -textvariable rotate_angle]

		set sb [cbutton $object_detail_frame.rb1 "Rotate"  {if {$rotate_axis == "x" } {set a 0}; if {$rotate_axis == "y"} {set a 1}; if {$rotate_axis == "z"} {set a 2}; ac3d rotate_axis_angle $a $rotate_angle; ac3d redraw_all} "rotate the selection around this axis (degs)"]
		grid config $rf -row 10 -column 0 -sticky e -columnspan 3 
		grid config $s3 -row 10 -column 3 -sticky e
		grid config $sb -row 10 -column 4 -sticky we -pady 3 -columnspan 3








# update ui sizes after scale/move etc!!!!!!!!!!!!!!!!!

    }

}

    set last_draw_mode $draw_mode
    set last_drag_mode $drag_mode
}


bind . <Escape> { set escape_pressed 1 }






