#                    M G E D P A G E . I T K
# BRL-CAD
#
# Copyright (c) 2004-2021 United States Government as represented by
# the U.S. Army Research Laboratory.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this file; see the file named COPYING for more
# information.
#
###
#
# This megawidget is the general viewer/selector page.
#

package require Itcl
package require Itk
package require Iwidgets

namespace eval RtWizard {

    package provide MGEDpage 1.0

    #
    # mgedpage - Provides a lowercased access method for the
    #            MGED page class
    #
    proc mgedpage {pathName args} {
	uplevel MGEDpage $pathName $arg
    }

    #
    # Add options
    #
    option add *MGEDpage.borderWidth 2 widgetDefault
    option add *MGEDpage.labelPos nw widgetDefault
    option add *MGEDpage.listHeight 150 widgetDefault
    option add *MGEDpage.hscrollMode dynamic widgetDefault
    option add *MGEDpage.vscrollMode dynamic widgetDefault
    option add *MGEDpage.textBackground seashell
    option add *MGEDpage.stepName "No Step" widgetDefault
    option add *MGEDpage.helpText "You're on your own!" widgetDefault
    option add *MGEDpage.relief groove widgetDefault
    option add *MGEDpage.onSelectCallback {} widgetDefault
    option add *MGEDpage.onDeselectCallback {} widgetDefault
    option add *MGEDpage.statusCallback {} widgetDefault


    ::itk::usual MGEDpage {
	keep    -background \
	    -borderwidth \
	    -cursor \
	    -elementborderwidth \
	    -foreground \
	    -highlightcolor \
	    -highlightthickness \
	    -insertbackground \
	    -insertborderwidth \
	    -insertofftime \
	    -insertontime \
	    -insertwidth \
	    -jump \
	    -labelfont \
	    -selectbackground \
	    -selectborderwidth \
	    -textbackground \
	    -textfont \
	    -troughcolor \
	    -stepName
    }

    ::itcl::class MGEDpage {
	inherit ::iwidgets::Labeledwidget

	constructor {database args} { }

	#
	# Configuration variables
	#
	itk_option define -stepName stepName StepName "No Step"
	itk_option define -helpText helptext HelpText ""

	itk_option define -onSelectCallback \
	    onselectcallback \
	    OnSelectCallback \
	    {}
	itk_option define -onDeselectCallback \
	    ondeselectcallback \
	    OnDeselectCallback \
	    {}
	itk_option define -statusCallback \
	    statuscallback \
	    StatusCallback \
	    {}

	#
	# All pages must support these methods
	#
	public {
	    method onSelect {}
	    method onDeselect {}
	    method getStepInfo {}
	    method setHelpText {text}
	    method draw {selected}
	    method getObjects {}
	    method getOrientation
	    method getLineColor {} {return $lineColor}
	    method getNonLineColor {} {return $nonLineColor}
	    method getBackgroundColor {} {return $backgroundColor}
	    method getIntensity {} {return [expr {$intensity + 1}]}
	    method getRegionColorFlag {} {return $useRegionColors}
	    method getOcclusionMode {}

	    variable selected ""
	}

	protected {
	    method getChildren {parent}
	    method selectNode {tags isOn}
	    method clearDisplay {}
	    method refreshDisplay {}

	    method updateStatus {}

	    method captureView {}
	    method restoreView {}

	    method createSizeDialog {parent}
	    method sizeDialogReveal {}
	    method sizeDialogOK {}
	    method sizeDialogApply {}
	    method sizeDialogCancel {}

	    method createPerspectiveDialog {parent}
	    method perspectiveDialogReveal {}
	    method perspectiveDialogOK {}
	    method perspectiveDialogApply {}
	    method perspectiveDialogCancel {}

	    method createCenterDialog {parent}
	    method centerDialogReveal {}
	    method centerDialogOK {}
	    method centerDialogApply {}
	    method centerDialogCancel {}

	    method createAETDialog {parent}
	    method aetDialogReveal {}
	    method aetDialogOK {}
	    method aetDialogApply {}
	    method aetDialogCancel {}

	    method createBotthresholdDialog {parent}
	    method botthresholdDialogReveal {}
	    method botthresholdDialogOK {}
	    method botthresholdDialogApply {}
	    method botthresholdDialogCancel {}

	    method lineColorChooser {}
	    method nonLineColorChooser {}
	    method backgroundColorChooser {}

	    method createOccModeDialog {parent}
	    method occModeDialogReveal {}
	    method occModeDialogOK {}

	    method createIntensityDialog {parent}
	    method intensityDialogReveal {}
	    method intensityDialogOK {}
	    method intensityDialogApply {}
	    method intensityDialogCancel {}
	}

	private {
	    #
	    # Am I currently selected
	    #
	    variable isSelected false

	    # Size Stuff
	    variable oldSize

	    # Perspective Stuff
	    variable oldPerspective

	    # Line-color stuff
	    variable lineColor "#000000"
	    variable useRegionColors false

	    #
	    # nonlinecolor stuff
	    #
	    variable nonLineColor "#ffffff"

	    # Background Color stuff
	    variable backgroundColor "#ffffff"

	    # Occlusion Mode stuff
	    variable occMode 1

	    # Intensity Stuff
	    variable intensity
	    variable oldIntensity 12

	    # Menu stuff
	    variable viewMenu ""
	    common viewCount 0

	    variable optionsMenu ""
	    common optionsCount 0

	    variable status
	}
    }

    #--------------------#
    #   Public Methods   #
    #--------------------#
    #
    # constructor
    #
    itcl::body MGEDpage::constructor {database args} {

	#
	# The database must exist for the MGED object to be created.
	# Test it.
	#
	if { ! [file exists $database] } {
	    error "$database does not exist."
	}

	#
	# Set up the childsite
	#
	set cs [ $this childsite ]
	$cs configure -relief groove -bd 2

	#
	# Create the help panel
	#
	itk_component add helpPanel {
	    ::iwidgets::scrolledtext $cs.helpPanel \
		-hscrollmode none \
		-vscrollmode dynamic \
		-visibleitems 80x3 \
		-wrap word \
		-relief flat\
		-textfont {helvetica 12 italic}
	} {
	    usual
	    ignore -textbackground
	}

	#
	# Create a frame for the center of the panel. On the left
	# will be the hierarchy window, on the right will be the
	# mged window over its controls
	#
	itk_component add midFrame {
	    ::iwidgets::panedwindow $cs.midFrame \
	    -orient vertical \
	    -thickness 8 \
	    -sashborderwidth 1 \
	    -sashcursor sb_h_double_arrow \
	    -showhandle 0
	} {
	    usual
        }
	set ::panedwin $itk_component(midFrame)
	$itk_component(midFrame) add hierarchyView
	set hparent [$itk_component(midFrame) childsite hierarchyView]
	$itk_component(midFrame) add mgedView

	#
	# Note that the MGED object is produced separately.
	# It resides in global scope. The pages pack/pack forget
	# it as necessary.
	#



	#
	# Create a frame to manage the controls
	#
	itk_component add conFrame {
	    iwidgets::labeledframe $hparent.conFrame \
		-background #666699 \
		-relief groove \
		-labeltext "View Controls"
	}
	set conFrameCS [ $itk_component(conFrame) childsite ]

	# Reset view based on current view contents
	itk_component add autoview {
	    button $conFrameCS.autoview \
		-text AutoView\
		-width 8 \
	        -command "eval $::mgedObj gedCmd autoview"
	} {
	    usual
	}

	# Clear
	itk_component add clear {
	    button $conFrameCS.clear \
		-text Clear \
		-width 8 \
		-command "[itcl::code $this clearDisplay]"
	} {
	    usual
	}

	#
	# Create the hierarchy browser
	#
	itk_component add hier {
	    iwidgets::hierarchy $hparent.#auto \
		-querycommand  "[itcl::code $this getChildren] %n " \
		-selectcommand "[itcl::code $this selectNode] %n %s " \
		-alwaysquery 0 \
		-expanded 0 \
		-visibleitems 30x20 \
		-vscrollmode dynamic \
		-hscrollmode dynamic \
		-labeltext $database \
		-labelpos nw
	} {
	    usual
	}

	#
	# Create the dialogs
	#
	createSizeDialog        $itk_interior
	createPerspectiveDialog $itk_interior
	createCenterDialog $itk_interior
	createAETDialog $itk_interior
	createBotthresholdDialog $itk_interior
	createIntensityDialog $itk_interior
	createOccModeDialog   $itk_interior

	#
	# Grid/Pack the items
	#
	grid $itk_component(clear) \
	    -row 0 \
	    -column 0 \
	    -padx 3 \
	    -pady 3
	grid $itk_component(autoview) \
	    -row 0 \
	    -column 1 \
	    -padx 3 \
	    -pady 3

	pack $itk_component(hier) -expand 1 -fill both -anchor w
	pack $itk_component(midFrame) -expand 1 -fill both
	pack $itk_component(conFrame) -anchor s
	pack $itk_component(helpPanel) -anchor s -fill x

	eval itk_initialize $args

	# Set initial width for hierarchy pane
	if {![info exists ::gpane]} {
	    set ::gpane [list 40 60]
	}
	$itk_component(midFrame) fraction [lindex $::gpane 0] [lindex $::gpane 1]
    }

    #
    # draw
    #
    # draws with autoview if this is the first draw, otherwise
    # regular draw.
    #
    itcl::body MGEDpage::draw {selected} {
	if {$::first_draw == 1} {
	    eval $::mgedObj gedCmd draw -L $::bot_threshold $selected
	    set ::first_draw 0
	} else {
	    eval $::mgedObj gedCmd draw -R -L $::bot_threshold $selected
	}
    }

    #
    # getStepInfo
    #
    # Returns the name of the this step, and the variable that monitors
    # the state of the step.
    #
    itcl::body MGEDpage::getStepInfo {} {
	set a [cget -stepName]
	set b [itcl::scope status]
	return [ list $a $b ]
    }

    #
    # onSelect
    #
    # Does stuff when the page is selected
    #
    itcl::body MGEDpage::onSelect {} {
	#
	# update isSelected flag
	#
	set isSelected true

	updateStatus

	#
	# Restore the view
	#
	restoreView

	#
	# Pack the mged object in _my_ mged frame
	#
	pack $::mgedObj \
	    -in [$itk_component(midFrame) childsite mgedView] \
	    -anchor e \
	    -expand 1 -fill both


	# set the pane to the global pane setting
	$itk_component(midFrame) fraction [lindex $::gpane 0] [lindex $::gpane 1]

	#
	# Create menus - use the singleton
	#
	global wizardInstance
	set w $wizardInstance

	#
	# Generate a unique name
	#
	set viewMenu ""
	append viewMenu "view" $viewCount

	incr viewCount

	$w menubar add menubutton $viewMenu \
	    -text "View" \
	    -menu {
		options -tearoff true -selectcolor green
	    }

	$w menubar add command .$viewMenu.aet -label "Azimuth/Elevation/Twist..." \
	    -helpstr "Set azimuth, elevation and twist" \
	    -command "[::itcl::code $this aetDialogReveal]"

	$w menubar add command .$viewMenu.size -label "Size..." \
	    -helpstr "Set view size" \
	    -command "[::itcl::code $this sizeDialogReveal]"

	$w menubar add command .$viewMenu.perspective -label "Perspective..." \
	    -helpstr "Set view perspective" \
	    -command "[::itcl::code $this perspectiveDialogReveal]"

	$w menubar add command .$viewMenu.center -label "Center..." \
	    -helpstr "Set view center" \
	    -command "[::itcl::code $this centerDialogReveal]"

	$w menubar add command .$viewMenu.botthreshold -label "BoT Drawing Threshold..." \
	    -helpstr "Set limit at which BoTs are drawn as boxes" \
	    -command "[::itcl::code $this botthresholdDialogReveal]"

	#
	# Do the options menu, callback to the child
	#
	set optionsMenu ""
	append optionsMenu "rtOptMenu" $optionsCount

	incr optionsCount

	$w menubar add menubutton $optionsMenu \
	    -text "Options" \
	    -menu {
		options -selectcolor green
	    }

	if {[cget -onSelectCallback] != ""} {
	    eval [cget -onSelectCallback] $optionsMenu
	}
    }

    #
    # onDeselect
    #
    # Cleans up when a page is deselected
    #
    itcl::body MGEDpage::onDeselect {} {
	updateStatus

	#
	# Capture the current view
	#
	captureView

	# Stash pane settings, as long as the width is greater
	# than three pixels
	set tgpane [$itk_component(midFrame) fraction]
	if { [lindex $tgpane 0] > 3 } {
	    set ::gpane $tgpane
	}

	#
	# Unpack the mged object from my mgedFrame so that
	# someone else may pack it in theirs.
	#
	pack forget $::mgedObj
	#
	# Delete our specific menu entries
	#
	global wizardInstance
	set w $wizardInstance

	if { [string length $viewMenu] > 0 } {
	    $w menubar delete $viewMenu
	}

	if { [string length $optionsMenu] > 0 } {
	    $w menubar delete $optionsMenu
	}

	if {[cget -onDeselectCallback] != ""} {
	    eval [cget -onDeselectCallback]
	}

	#
	# update isSelectedFlag
	#
	set isSelected false
    }

    #
    # getObjects - returns the list of currently selected objects
    #
    ::itcl::body MGEDpage::getObjects { } {
	if {$isSelected == "true"} {
	    set selected [$itk_component(hier) selection get]
	}
	return $selected
    }

    #
    # getOrientation - returns the current orientation quaternion
    #
    itcl::body MGEDpage::getOrientation {} {
	if {$isSelected == "true"} {
	    set currentRmat [ $::mgedObj gedCmd rmat ]
	}
	return [ quat_mat2quat $currentRmat]
    }

    #--------------------#
    #  Private Methods   #
    #--------------------#
    #
    # getChildren - returns the children of the given parent.
    #
    #
    ::itcl::body MGEDpage::getChildren {parent} {

	set objs {}

	if { [string length $parent] == 0 } {
	    #
	    # When the widget is initially opened, the parent is null.
	    # In this case, use tops.
	    #
	    set raw [ $::mgedObj gedCmd tops ]
	    foreach i $raw {
		if { $i != "_GLOBAL" } {
		    regsub {/R*} $i {} clean
		    lappend objs $clean
		}
	    }
	    set objs [lsort $objs]
	} else {
	    #
	    # For the given parent, get its constituents using the 'lt'
	    # command. This produces a tcl list of the items.
	    #
	    set t [file tail $parent]
	    set raw [ $::mgedObj gedCmd lt $t ]
	    foreach i $raw {
		set obj [lindex $i 1]
		lappend objs [list [file join $parent $obj] $obj]
	    }
	}

	return $objs
    }

    #
    # selectNode - toggles a node selection state.
    #
    itcl::body MGEDpage::selectNode {tag isOn} {

	if {$isOn} {
	    $::mgedObj gedCmd erase $tag
	    $itk_component(hier) selection remove $tag
	} else {
	    draw $tag
	    $itk_component(hier) selection add $tag
	}
	updateStatus
    }

    #
    # setHelpText - sets the contents of the help widget
    #
    ::itcl::body MGEDpage::setHelpText { text } {

	$itk_component(helpPanel) clear
	$itk_component(helpPanel) insert end $text
    }

    #
    # clearDisplay - clears the mged display and the selections in the
    #                hierarchy widget.
    #
    ::itcl::body MGEDpage::clearDisplay { } {

	$itk_component(hier) selection clear
	$::mgedObj gedCmd zap
	updateStatus
    }

    #
    # captureView - executed by onDeselect, this method records the
    #               currently selected objects and the view info so
    #               that it may be restored by onSelect
    #
    ::itcl::body MGEDpage::captureView {} {
	#
	# All of the get* methods set the current values, so we'll
	# use them.
	#
	set selected  [$itk_component(hier) selection get]
    }


    # refreshDisplay - clears the mged display, and redraws the selected items.
    #
    ::itcl::body MGEDpage::refreshDisplay { } {

    # get the list of currently selected items
	set selected [$itk_component(hier) selection get]

	# clear the display
	$::mgedObj gedCmd zap

	# redraw the selection list
	if {[llength $selected] > 0} {
	    draw $selected
	}

	updateStatus
    }


    #
    # restoreView - restores the mgedObj to its previous state.
    #
    ::itcl::body MGEDpage::restoreView {} {
	$::mgedObj gedCmd zap

	#
	# Reselect the objects - XXX
	#
	if {[llength $selected] > 0} {
	    draw $selected
	}
    }

    #
    # updateStatus
    #
    ::itcl::body MGEDpage::updateStatus { } {

	#
	# Something must be selected for the page to be ready.
	# If nothing is selected, then the wrong picture type
	# has been picked.
	#
	set selected [$itk_component(hier) selection get]

	if {[llength $selected] == 0 } {
	    set status 0
	    $::wizardInstance updateRenderMenu
	    return
	}
	if {[cget -statusCallback] != ""} {
	    if {[eval [cget -statusCallback]] == 0} {
		set status 0
		$::wizardInstance updateRenderMenu
		return
	    }
	}

	set status 1
	$::wizardInstance updateRenderMenu
	return
    }

    #--------------------#
    #    Size Control    #
    #--------------------#
    #
    # createSizeDialog - creates a dialog window that allows the user
    #                         to set the view size
    #
    ::itcl::body MGEDpage::createSizeDialog {parent} {

	#
	# Get the units
	#
	set rawUnits [ $::mgedObj gedCmd units ]
	set units [ string trim [ lindex $rawUnits 4 ] {'.} ]

	#
	# Get the current size
	#
	set currSize  [ $::mgedObj gedCmd size ]

	#
	# Build the dialog
	#
	itk_component add sizeDialog {
	    iwidgets::dialog $parent.#auto -title Size
	} {
	    usual
	}

	set cs [ eval $itk_component(sizeDialog) childsite ]

	#
	# Build the current size indicator
	#
	itk_component add sizeLabel {
	    iwidgets::labeledwidget $cs.scl \
		-labeltext "Current Size:" \
		-labelpos "w"
	} {
	    usual
	}

	set cs2 [$itk_component(sizeLabel) childsite]

	itk_component add sizeCurrent {
	    label $cs2.l1 \
		-text $currSize \
		-width 10
	} {
	    usual
	}

	itk_component add sizeUnits1 {
	    label $cs2.l2 \
		-text $units
	} {
	    usual
	}

	#
	# Build the entry field for the size.
	#
	itk_component add sizeEntry {
	    iwidgets::entryfield $cs.sze \
		-labeltext "Desired Size:" \
		-labelpos w \
		-width 10\
		-command "[::itcl::code $this sizeDialogApply]"
	} {
	    usual
	}

	set cs3 [ $itk_component(sizeEntry) childsite ]

	# units
	itk_component add sizeUnits2 {
	    label $cs3.l1 \
		-text $units
	} {
	    usual
	}


	$itk_component(sizeDialog) hide Help
	$itk_component(sizeDialog) buttonconfigure OK \
	    -command "[::itcl::code $this sizeDialogOK]"
	$itk_component(sizeDialog) buttonconfigure Apply \
	    -command "[::itcl::code $this sizeDialogApply]"
	$itk_component(sizeDialog) buttonconfigure Cancel \
	    -command "[::itcl::code $this sizeDialogCancel]"

	pack $itk_component(sizeCurrent) -side left
	pack $itk_component(sizeUnits1)

	pack $itk_component(sizeUnits2)

	pack $itk_component(sizeLabel) -anchor c
	pack $itk_component(sizeEntry) -anchor c

	#
	# Align the widgets
	#
	eval iwidgets::Labeledwidget::alignlabels \
	    $itk_component(sizeLabel) \
	    $itk_component(sizeEntry)

	$itk_component(sizeDialog) deactivate
    }

    #
    # sizeDialogReveal - executed when the size dialog window
    #                       is requested from the menu bar
    #
    ::itcl::body MGEDpage::sizeDialogReveal {} {
	set oldSize [ $::mgedObj gedCmd size ]
	$itk_component(sizeCurrent) configure -text $oldSize
	$itk_component(sizeEntry) clear
	$itk_component(sizeEntry) insert 0 $oldSize
	$itk_component(sizeDialog) activate
    }

    #
    # sizeDialogOK - executed when the OK button is pressed.
    #
    ::itcl::body MGEDpage::sizeDialogOK {} {
	$::mgedObj gedCmd size [ $itk_component(sizeEntry) get ]
	$itk_component(sizeDialog) deactivate
	updateStatus
    }

    #
    # sizeDialogApply - executed when the apply button is pressed.
    #
    ::itcl::body MGEDpage::sizeDialogApply {} {
	set newSize [ $itk_component(sizeEntry) get ]
	$::mgedObj gedCmd size $newSize
	$itk_component(sizeCurrent) configure -text $newSize
	updateStatus
    }

    #
    # sizeDialogCancel - executed when the close button is pressed.
    #
    ::itcl::body MGEDpage::sizeDialogCancel {} {
	$::mgedObj gedCmd size $oldSize
	$itk_component(sizeDialog) deactivate
	updateStatus
    }

    #---------------------#
    # Perspective Control #
    #---------------------#
    #
    # createPerspectiveDialog - creates a dialog window that allows the user
    #                         to set the view perspective
    #
    ::itcl::body MGEDpage::createPerspectiveDialog {parent} {

	itk_component add perspectiveDialog {
	    iwidgets::dialog $parent.#auto -title Perspective
	} {
	    usual
	}

	set cs [ eval $itk_component(perspectiveDialog) childsite ]

	itk_component add perspective {
	    scale $cs.el \
		-orient horizontal \
		-from 0 \
		-to 180 \
		-length 180 \
		-resolution 1\
		-tickinterval 45 \
		-font {Helvetica 8  bold} \
		-repeatinterval 3000 \
		-repeatdelay 3000 \
		-command {}
	} {
	    usual
	}

	$itk_component(perspective) set [$::mgedObj gedCmd perspective]
	$itk_component(perspectiveDialog) hide Help
	$itk_component(perspectiveDialog) buttonconfigure OK \
	    -command "[::itcl::code $this perspectiveDialogOK]"
	$itk_component(perspectiveDialog) buttonconfigure Apply \
	    -command "[::itcl::code $this perspectiveDialogApply]"
	$itk_component(perspectiveDialog) buttonconfigure Cancel \
	    -command "[::itcl::code $this perspectiveDialogCancel]"
	pack $itk_component(perspective)
	$itk_component(perspectiveDialog) deactivate
    }

    #
    # perspectiveDialogReveal - executed when the perspective dialog window
    #                       is requested from the menu bar
    #
    ::itcl::body MGEDpage::perspectiveDialogReveal {} {
	set oldPerspective [$::mgedObj gedCmd perspective]
	$itk_component(perspective) set $oldPerspective
	$itk_component(perspectiveDialog) activate
    }

    #
    # perspectiveDialogOK - executed when the OK button is pressed.
    #
    ::itcl::body MGEDpage::perspectiveDialogOK {} {
	$::mgedObj gedCmd perspective [ $itk_component(perspective) get]
	$itk_component(perspectiveDialog) deactivate
	updateStatus
    }

    #
    # perspectiveDialogApply - executed when the apply button is pressed.
    #
    ::itcl::body MGEDpage::perspectiveDialogApply {} {
	$::mgedObj gedCmd perspective [ $itk_component(perspective) get]
	updateStatus
    }

    #
    # perspectiveDialogCancel - executed when the close button is pressed.
    #
    ::itcl::body MGEDpage::perspectiveDialogCancel {} {
	$::mgedObj gedCmd perspective $oldPerspective
	$itk_component(perspectiveDialog) deactivate
	updateStatus
    }

    #---------------------#
    #    Center Control   #
    #---------------------#
    #
    # createCenterDialog - creates a dialog window that allows the user
    #                         to set the view center
    #
    ::itcl::body MGEDpage::createCenterDialog {parent} {

	# Get current Center
	set currCenter [$::mgedObj gedCmd center]

	itk_component add centerDialog {
	    iwidgets::dialog $parent.#auto -title Center
	} {
	    usual
	}

	set cs [ eval $itk_component(centerDialog) childsite ]
#
	# Build the current size indicator
	#
	itk_component add centerLabel {
	    iwidgets::labeledwidget $cs.scl \
		-labeltext "Center(x y z):" \
		-labelpos "w"
	} {
	    usual
	}

	#
	# Build the entry field for the size.
	#
	itk_component add centerEntry {
	    iwidgets::entryfield $cs.cxyz \
		-labeltext "Center (x y z):" \
		-labelpos w \
		-width 30\
		-command "[::itcl::code $this centerDialogApply]"
	} {
	    usual
	}

	$itk_component(centerDialog) hide Help
	$itk_component(centerDialog) buttonconfigure OK \
	    -command "[::itcl::code $this centerDialogOK]"
	$itk_component(centerDialog) buttonconfigure Apply \
	    -command "[::itcl::code $this centerDialogApply]"
	$itk_component(centerDialog) buttonconfigure Cancel \
	    -command "[::itcl::code $this centerDialogCancel]"
	pack $itk_component(centerLabel) -anchor c
	pack $itk_component(centerEntry) -anchor c
	eval iwidgets::Labeledwidget::alignlabels \
	    $itk_component(centerLabel) \
	    $itk_component(centerEntry)

	$itk_component(sizeCurrent) configure -text $currCenter
	$itk_component(centerDialog) deactivate
    }

    #
    # centerDialogReveal - executed when the center dialog window
    #                       is requested from the menu bar
    #
    ::itcl::body MGEDpage::centerDialogReveal {} {
	set oldCenter [$::mgedObj gedCmd center]
	$itk_component(centerEntry) clear
	$itk_component(centerEntry) insert 0 $oldCenter
	$itk_component(centerDialog) activate
    }

    #
    # centerDialogOK - executed when the OK button is pressed.
    #
    ::itcl::body MGEDpage::centerDialogOK {} {
	$::mgedObj gedCmd center [ $itk_component(centerEntry) get]
	$itk_component(centerDialog) deactivate
	updateStatus
    }

    #
    # centerDialogApply - executed when the apply button is pressed.
    #
    ::itcl::body MGEDpage::centerDialogApply {} {
	set newCenter [ $itk_component(centerEntry) get]
	$::mgedObj gedCmd center $newCenter
	$itk_component(sizeCurrent) configure -text $newCenter
	updateStatus
    }

    #
    # centerDialogCancel - executed when the close button is pressed.
    #
    ::itcl::body MGEDpage::centerDialogCancel {} {
	set oldCenter [$::mgedObj gedCmd center]
	$::mgedObj gedCmd center $oldCenter
	$itk_component(centerDialog) deactivate
	updateStatus
    }



    #------------------#
    #    aet Control   #
    #------------------#
    #
    # createAETDialog - creates a dialog window that allows the user
    #                            to set the bot drawing threshold
    #
    ::itcl::body MGEDpage::createAETDialog {parent} {

	itk_component add aetDialog {
	    iwidgets::dialog $parent.#auto -title "Set the View Azimuth, Elevation and Twist"
	} {
	    usual
	}

	set cs [ eval $itk_component(aetDialog) childsite ]

	#
	# Build the entry field for the azimuth.
	#
	itk_component add azEntry {
	    iwidgets::entryfield $cs.az \
		-labeltext "Azimuth:" \
		-labelpos "w" \
		-width 40\
		-command "[::itcl::code $this aetDialogApply]"
	} {
	    usual
	}

	#
	# Build the entry field for the azimuth.
	#
	itk_component add elEntry {
	    iwidgets::entryfield $cs.el \
		-labeltext "Elevation:" \
		-labelpos "w" \
		-width 40\
		-command "[::itcl::code $this aetDialogApply]"
	} {
	    usual
	}

	#
	# Build the entry field for the azimuth.
	#
	itk_component add twEntry {
	    iwidgets::entryfield $cs.tw \
		-labeltext "Twist:" \
		-labelpos "w" \
		-width 40\
		-command "[::itcl::code $this aetDialogApply]"
	} {
	    usual
	}

	$itk_component(aetDialog) hide Help
	$itk_component(aetDialog) buttonconfigure OK \
	    -command "[::itcl::code $this aetDialogOK]"
	$itk_component(aetDialog) buttonconfigure Apply \
	    -command "[::itcl::code $this aetDialogApply]"
	$itk_component(aetDialog) buttonconfigure Cancel \
	    -command "[::itcl::code $this aetDialogCancel]"
	pack $itk_component(azEntry) -anchor c
	pack $itk_component(elEntry) -anchor c
	pack $itk_component(twEntry) -anchor c
	eval iwidgets::Labeledwidget::alignlabels \
	    $itk_component(azEntry) \
	    $itk_component(elEntry) \
	    $itk_component(twEntry)

	$itk_component(azEntry) clear
	$itk_component(azEntry) insert 0 [lindex [$::mgedObj gedCmd aet] 0 ]

	$itk_component(elEntry) clear
	$itk_component(elEntry) insert 0 [lindex [$::mgedObj gedCmd aet] 1 ]

	$itk_component(twEntry) clear
	$itk_component(twEntry) insert 0 [lindex [$::mgedObj gedCmd aet] 2 ]

	$itk_component(aetDialog) deactivate
    }

    #
    # aetDialogReveal - executed when the aet dialog window
    #                       is requested from the menu bar
    #
    ::itcl::body MGEDpage::aetDialogReveal {} {

	$itk_component(azEntry) clear
	$itk_component(azEntry) insert 0 [lindex [$::mgedObj gedCmd aet] 0 ]

	$itk_component(elEntry) clear
	$itk_component(elEntry) insert 0 [lindex [$::mgedObj gedCmd aet] 1 ]

	$itk_component(twEntry) clear
	$itk_component(twEntry) insert 0 [lindex [$::mgedObj gedCmd aet] 2 ]

	$itk_component(aetDialog) activate
    }

    #
    # aetDialogOK - executed when the OK button is pressed.
    #
    ::itcl::body MGEDpage::aetDialogOK {} {
	set az [ $itk_component(azEntry) get]
	set el [ $itk_component(elEntry) get ]
	set tw [ $itk_component(twEntry) get ]
	$::mgedObj gedCmd aet $az $el $tw
	$itk_component(aetDialog) deactivate
	updateStatus
    }

    #
    # aetDialogApply - executed when the apply button is pressed.
    #
    ::itcl::body MGEDpage::aetDialogApply {} {
	set az [ $itk_component(azEntry) get]
	set el [ $itk_component(elEntry) get ]
	set tw [ $itk_component(twEntry) get ]
	$::mgedObj gedCmd aet $az $el $tw
	updateStatus
    }

    #
    # aetDialogCancel - executed when the close button is pressed.
    #
    ::itcl::body MGEDpage::aetDialogCancel {} {
	$itk_component(aetDialog) deactivate
    }



    #----------------------------#
    #    BoT Threshold Control   #
    #----------------------------#
    #
    # createBotthresholdDialog - creates a dialog window that allows the user
    #                            to set the bot drawing threshold
    #
    ::itcl::body MGEDpage::createBotthresholdDialog {parent} {

	# Get current threshold
	set currThreshold $::bot_threshold

	itk_component add botthresholdDialog {
	    iwidgets::dialog $parent.#auto -title "BoT Drawing Threshold (max # of faces)"
	} {
	    usual
	}

	set cs [ eval $itk_component(botthresholdDialog) childsite ]
#
	# Build the label
	#
	itk_component add botthresholdLabel {
	    iwidgets::labeledwidget $cs.scl \
		-labeltext "BoT triangle count above which bounding boxes are drawn\n(Note: a value of 0 disables bounding box drawing):" \
		-labelpos "w"
	} {
	    usual
	}

	#
	# Build the entry field for the threshold.
	#
	itk_component add botthresholdEntry {
	    iwidgets::entryfield $cs.bte \
		-width 50\
		-command "[::itcl::code $this botthresholdDialogApply]"
	} {
	    usual
	}

	$itk_component(botthresholdDialog) hide Help
	$itk_component(botthresholdDialog) buttonconfigure OK \
	    -command "[::itcl::code $this botthresholdDialogOK]"
	$itk_component(botthresholdDialog) buttonconfigure Apply \
	    -command "[::itcl::code $this botthresholdDialogApply]"
	$itk_component(botthresholdDialog) buttonconfigure Cancel \
	    -command "[::itcl::code $this botthresholdDialogCancel]"
	pack $itk_component(botthresholdLabel) -anchor c
	pack $itk_component(botthresholdEntry) -anchor c
	eval iwidgets::Labeledwidget::alignlabels \
	    $itk_component(botthresholdLabel) \
	    $itk_component(botthresholdEntry)

	$itk_component(botthresholdEntry) clear
	$itk_component(botthresholdEntry) insert 0 $currThreshold
	$itk_component(botthresholdDialog) deactivate
    }

    #
    # botthresholdDialogReveal - executed when the botthreshold dialog window
    #                       is requested from the menu bar
    #
    ::itcl::body MGEDpage::botthresholdDialogReveal {} {
	set oldThreshold $::bot_threshold
	$itk_component(botthresholdEntry) clear
	$itk_component(botthresholdEntry) insert 0 $oldThreshold
	$itk_component(botthresholdDialog) activate
    }

    #
    # botthresholdDialogOK - executed when the OK button is pressed.
    #
    ::itcl::body MGEDpage::botthresholdDialogOK {} {
	set ::bot_threshold [ $itk_component(botthresholdEntry) get]
	$itk_component(botthresholdDialog) deactivate
	updateStatus
	refreshDisplay
    }

    #
    # botthresholdDialogApply - executed when the apply button is pressed.
    #
    ::itcl::body MGEDpage::botthresholdDialogApply {} {
	set ::bot_threshold [ $itk_component(botthresholdEntry) get]
	$itk_component(botthresholdEntry) clear
	$itk_component(botthresholdEntry) insert 0 $::bot_threshold
	updateStatus
	refreshDisplay
    }

    #
    # botthresholdDialogCancel - executed when the close button is pressed.
    #
    ::itcl::body MGEDpage::botthresholdDialogCancel {} {
	$itk_component(botthresholdDialog) deactivate
    }


    #--------------------#
    # Line Color Control #
    #--------------------#
    #
    # The LineColor dialog uses the existing tk_chooseColor widget.
    # This widget is created on demand rather than created beforehand
    # and activated.
    #
    #
    # lineColorChooser - executed when the line color dialog window
    #                    is requested from the menu bar
    #
    ::itcl::body MGEDpage::lineColorChooser {} {
	set inColor $lineColor

	set outColor [tk_chooseColor \
			  -initialcolor $inColor \
			  -title "Line Color"]

	if {[string length $outColor] == 7 } {
	    set lineColor $outColor

	    #
	    # Since the user has chosen a color, unselect the
	    # useRegionColors field
	    #
	    set useRegionColors false
	}
	updateStatus
    }

    #------------------------#
    # Non-Line Color Control #
    #------------------------#
    #
    # Non-Line colors are colors for non-edge hits when occMode > 1
    #
    #
    # nonLineColorChooser - executed when the line color dialog window
    #                    is requested from the menu bar
    #
    ::itcl::body MGEDpage::nonLineColorChooser {} {
	set inColor $nonLineColor

	set outColor [tk_chooseColor \
			  -initialcolor $inColor \
			  -title "Non-Line Color"]

	if {[string length $outColor] == 7 } {
	    set nonLineColor $outColor
	}
	updateStatus
    }

    #--------------------------#
    # Background Color Control #
    #--------------------------#
    #
    # The Background Color dialog uses the existing tk_chooseColor widget.
    # This widget is created on demand rather than created beforehand
    # and activated.
    #
    #
    # BackgroundColorChooser - executed when the line color dialog window
    #                          is requested from the menu bar
    #
    ::itcl::body MGEDpage::backgroundColorChooser {} {
	set inColor $backgroundColor

	set outColor [tk_chooseColor \
			  -initialcolor $inColor \
			  -title "Background Color"]

	if {[string length $outColor] == 7 } {
	    set backgroundColor $outColor
	}
	updateStatus
    }

    #------------------------#
    # Occlusion Mode Control #
    #------------------------#
    #
    # createOccModeDialog - creates a dialog window that allows the user
    #                       to set the occlusion mode
    #
    ::itcl::body MGEDpage::createOccModeDialog {parent} {

	#
	# Build the dialog
	#
	itk_component add occModeDialog {
	    iwidgets::dialog $parent.#auto -title "Occlusion Mode"
	} {
	    usual
	}

	set cs [ eval $itk_component(occModeDialog) childsite ]

	#
	# Build the radiobox
	#
	itk_component add occModeRB {
	    iwidgets::radiobox $cs.rb \
		-labeltext "Select the Occlusion Mode:" \
		-labelpos "nw" \
		-ipady 10
	} {
	    usual
	}

	$itk_component(occModeRB) add "mode1" \
	    -text "Draw foreground lines only"
	$itk_component(occModeRB) add "mode2" \
	    -text "Also draw non-line surfaces in the background color"
	$itk_component(occModeRB) add "mode3" \
	    -text "Dither the non-line surfaces (semi-transparent)"
	$itk_component(occModeRB) select mode1

	$itk_component(occModeDialog) hide Help
	$itk_component(occModeDialog) buttonconfigure OK \
	    -command "[::itcl::code $this occModeDialogOK]"

	$itk_component(occModeDialog) hide Apply
	$itk_component(occModeDialog) hide Cancel

	pack $itk_component(occModeRB)

	$itk_component(sizeDialog) deactivate
    }

    #
    # occModeDialogReveal - executed when the occlusion mode dialog
    #                       window is requested from the menu bar
    #
    ::itcl::body MGEDpage::occModeDialogReveal {} {
	set occMode [$itk_component(occModeRB) get]
	$itk_component(occModeDialog) activate
    }

    #
    # occModeDialogOK - executed when the OK button is pressed.
    #
    ::itcl::body MGEDpage::occModeDialogOK {} {
	set occMode [ $itk_component(occModeRB) get ]
	$itk_component(occModeDialog) deactivate
	updateStatus
    }

    #
    # getOcclusionMode
    #
    ::itcl::body MGEDpage::getOcclusionMode {} {
	switch $occMode {
	    "mode1" { return 1 }
	    "mode2" { return 2 }
	    "mode3" { return 3 }
	}
	return 0
    }

    #--------------------#
    #  Intensity Control #
    #--------------------#
    #
    # createIntensityDialog - creates a dialog window that allows the user
    #                         to set the intensity of the ghost image
    #
    ::itcl::body MGEDpage::createIntensityDialog {parent} {

	itk_component add intensityDialog {
	    iwidgets::dialog $parent.#auto -title Intensity
	} {
	    usual
	}

	set cs [ eval $itk_component(intensityDialog) childsite ]

	itk_component add intensity {
	    scale $cs.#auto \
		-orient horizontal \
		-from 0 \
		-to 100 \
		-length 180 \
		-resolution 1\
		-tickinterval 20 \
		-font {Helvetica 8  bold} \
		-variable [::itcl::scope intensity]
	} {
	    usual
	}

	set intensity $oldIntensity
	$itk_component(intensityDialog) hide Help
	$itk_component(intensityDialog) buttonconfigure OK \
	    -command "[::itcl::code $this intensityDialogOK]"
	$itk_component(intensityDialog) buttonconfigure Apply \
	    -command "[::itcl::code $this intensityDialogApply]"
	$itk_component(intensityDialog) buttonconfigure Cancel \
	    -command "[::itcl::code $this intensityDialogCancel]"
	pack $itk_component(intensity)
	$itk_component(intensityDialog) deactivate
    }

    #
    # intensityDialogReveal - executed when the intensity dialog window
    #                       is requested from the menu bar
    #
    ::itcl::body MGEDpage::intensityDialogReveal {} {
	$itk_component(intensityDialog) activate
    }

    #
    # intensityDialogOK - executed when the OK button is pressed.
    #
    ::itcl::body MGEDpage::intensityDialogOK {} {
	$itk_component(intensityDialog) deactivate
	set oldIntensity $intensity
	updateStatus
    }

    #
    # intensityDialogApply - executed when the apply button is pressed.
    #
    ::itcl::body MGEDpage::intensityDialogApply {} {
	set oldIntensity $intensity
	updateStatus
    }

    #
    # intensityDialogCancel - executed when the close button is pressed.
    #
    ::itcl::body MGEDpage::intensityDialogCancel {} {
	set intensity $oldIntensity
	$itk_component(intensityDialog) deactivate
	updateStatus
    }

}; # end namespace


# Local Variables:
# mode: Tcl
# tab-width: 8
# c-basic-offset: 4
# tcl-indent-level: 4
# indent-tabs-mode: t
# End:
# ex: shiftwidth=4 tabstop=8
