#                 E X A M P L E 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 page provides the user with a set of examples from which they
# can choose the type of picture they would like to make.
#

namespace eval RtWizard {

    package provide ExamplePage 1.0

    #
    # Add options
    #
    option add *ExamplePage.borderWidth 2 widgetDefault
    option add *ExamplePage.labelPos wn widgetDefault
    option add *ExamplePage.listHeight 150 widgetDefault
    option add *ExamplePage.hscrollMode dynamic widgetDefault
    option add *ExamplePage.vscrollMode dynamic widgetDefault
    option add *ExamplePage.textBackground seashell

    #
    # Define the usual options
    #
    ::itk::usual ExamplePage {
	keep -activebackground \
	    -activerelief \
	    -background \
	    -borderwidth \
	    -cursor \
	    -elementborderwidth \
	    -foreground \
	    -highlightcolor \
	    -highlightthickness \
	    -insertbackground \
	    -insertborderwidth \
	    -insertofftime \
	    -insertontime \
	    -insertwidth \
	    -jump \
	    -labelfont \
	    -selectbackground \
	    -selectborderwidth \
	    -textbackground \
	    -textfont \
	    -troughcolor
    }

    #
    # ExamplePage
    #
    # The example page class provides a series of pictures that
    # show examples of the type of pictures that can be made with
    # the wizard.
    #
    ::itcl::class ExamplePage {
	inherit ::iwidgets::Labeledwidget

	#
	# Configuration variables
	#

	# None!

	#
	# Public interface
	#
	public {
	    constructor {args} {}

	    method onSelect {}
	    method onDeselect {}
	    method getStepInfo {}

	    method addType {type token args}
	    method setType {}
	    method changePicType {}
	    method getImageType {} {return $imageType}

	    # XXX something to get script info
	    method getCurrentTypeObj {} {return $currentTypeObj}
	    method getViewPage {} {return $viewPage}
	    method setViewPage {page} {
		set viewPage $page
	    }
	}

	private {
	    method updatePreview {}
	    method onOK {}
	    method onCancel {}
	    method insertHelpText {}

	    variable typeObjects

	    variable imageType
	    variable currentTypeObj ""

	    variable previewImages
	    variable descText
	    variable keyMap

	    common viewPage
	}
    }

    #--------------------#
    #   Public Methods   #
    #--------------------#
    #
    # constructor - builds the Example Page
    #
    itcl::body ExamplePage::constructor {args} {

	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 none \
		-visibleitems 80x8 \
		-wrap word \
		-relief flat \
		-textfont $::RtWizard::helpFont
	} {
	    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 {
	    frame $cs.midFrame \
		-relief flat
	} {
	    usual
	}

	#
	# Build the middle, the left side contains an option menu over
	# descriptive text, the right is an a sample image
	#
	itk_component add leftFrame {
	    frame $itk_component(midFrame).lf \
		-relief flat
	} {
	    usual
	}

	itk_component add picTypes {
	    iwidgets::optionmenu $itk_component(leftFrame).pt \
		-labeltext "Select an Image Type" \
		-labelpos nw \
		-command "[::itcl::code $this updatePreview]"
	} {
	    usual
	}

	#
	# Add text description
	#
	itk_component add descBox {
	    iwidgets::scrolledtext $itk_component(leftFrame).descBox \
		-visibleitems 40x12 \
		-hscrollmode dynamic \
		-vscrollmode dynamic \
		-wrap word \
		-textbackground seashell
	} {
	    usual
	}

	#
	# Pack the optionmenu and the descriptive text within
	# the left frame
	#
	pack $itk_component(picTypes) \
	    -side top \
	    -anchor w \
	    -pady 5 \
	    -fill x \
	    -expand y
	pack $itk_component(descBox) \
	    -fill both \
	    -expand y \
	    -pady 5

	#
	# On the right side, put the preview image
	#
	itk_component add previewImg  {
	    label $itk_component(midFrame).pimg \
		-relief sunken
	} {
	    usual
	}

	#
	# Create a button box for the OK button
	#
	itk_component add bbox {
	    ::iwidgets::buttonbox $cs.#auto
	} {
	    usual
	}

	$itk_component(bbox) add OK \
	    -text "OK" \
	    -command "[::itcl::code $this onOK]"

	$itk_component(bbox) add cancel \
	    -text "Cancel" \
	    -command "[::itcl::code $this onCancel]"

	$itk_component(bbox) default cancel

	#
	# Now pack the left frame and the preview image.
	#
	pack $itk_component(leftFrame) \
	    -side left \
	    -anchor n \
	    -padx 5 \
	    -fill y \
	    -expand y

	pack $itk_component(previewImg) \
	    -anchor center \
	    -padx 5 \
	    -pady 5

	#
	# Pack the top
	#
	pack $itk_component(helpPanel)
	pack $itk_component(midFrame)
	pack $itk_component(bbox) -side right

	#
	# Configure the labeled widget
	#
	$this configure -labeltext "Select Image Type"
	$this configure -labelpos nw

	#
	# Insert help text
	#
	insertHelpText

	#
	# Evaluate any user supplied args.
	#
	eval configure $args

    }

    #
    # onSelect
    #
    # Makes any necessary changes to the Wizard configuration
    # when this page is selected.
    #
    itcl::body ExamplePage::onSelect {} {
	#
	# Nothing yet!
	#
    }

    #
    # onDeselect
    #
    # Cleans up when the page is deselected
    #
    itcl::body ExamplePage::onDeselect {} {
	#
	# Nothing yet!
	#
    }

    #
    # getStepInfo
    #
    # In principal, returns a name for the step, and a variable that
    # can be queried for state. The database page is not a 'step', so
    # it returns "No Step"
    #
    itcl::body ExamplePage::getStepInfo {} {
	set a "No Step"
	set b "banana"
	return [list $a $b]
    }

    #
    # addType
    #
    # Adds a picture type to the set of supported pictures.
    #
    itcl::body ExamplePage::addType { type token args } {

	#
	# Create the picture type object. Note that it defines
	# steps and the "preview" and "fullSize" methods. It
	# provides the sample image and the help text.
	#
	set typeObj [ $type #auto $args ]
		      set typeObjects($token) $typeObj

		      #
		      # Set up the arrays that map the option menu.
		      # entries to the sample image and the descriptive
		      # text.
		      #
		      # The option menu outputs the "title" string. We
		      # get the token via the keyMap. That then lets us
		      # access the text and image.
		      #
		      set title [$typeObj getTitle]
		      set keyMap($title) $token
		      set previewImages($token)  [$typeObj getSampleImage]
		      set descText($token) [$typeObj getDescText]

		      #
		      # Add this type to the radiobox in the Image
		      # menu. If changed, it calls the setType method.
		      #
		      $::wizardInstance menubar add radiobutton .image.$token \
			  -label $title \
			  -variable [itcl::scope imageType] \
			  -value $title \
			  -helpstr [$typeObj getHelpStr] \
			  -command "[itcl::code $this changePicType]"

		      #
		      # Finally, add the new type (title) to the option
		      # menu
		      #
		      $itk_component(picTypes) insert end  [$typeObj getTitle]
		      $itk_component(picTypes) select 0
		      updatePreview
		  }

	#
	# changePicType
	#
	# This method is activated when the radiobuttons are changed.
	#
	itcl::body ExamplePage::changePicType {} {
	    #
	    # update the optionmenu
	    #
	    $itk_component(picTypes) select $imageType

	    #
	    # call setType
	    #
	    setType
	}

	#
	# setType
	#
	# Sets the current picture type to whatever is specified in the
	# option menu.
	#
	itcl::body ExamplePage::setType {} {

	    #
	    # deactivate the current type object
	    #
	    if {[string length $currentTypeObj] != 0} {
		$currentTypeObj deactivate
	    }
	    #
	    # Get the new type selection
	    #
	    set title [ $itk_component(picTypes) get ]
	    set token $keyMap($title)
	    set typeObj $typeObjects($token)
	    set imageType $title

	    #
	    # activate the new type
	    #
	    $typeObj activate
	    set currentTypeObj $typeObj
	}

	#--------------------#
	#  Private Methods   #
	#--------------------#
	#
	# updatePreview -
	#
	# Called whenever the optionmenu is changed, this method updates the
	# sample image and the help text.
	#
	::itcl::body ExamplePage::updatePreview {} {
	    set selection [ $itk_component(picTypes) get ]

	    if {[string length $selection] == 0} {
		return
	    }

	    #
	    # Convert the long string to the key
	    #
	    set key $keyMap($selection)

	    $itk_component(previewImg) configure \
		-image $previewImages($key)
	    $itk_component(descBox) configure -state normal
	    $itk_component(descBox) delete 1.0 end
	    $itk_component(descBox) insert end $descText($key)
	    $itk_component(descBox) configure -state disabled

	}

	#
	# onOK - called when the OK button is called
	#
	::itcl::body ExamplePage::onOK {} {

	    #
	    # Select the new picture type
	    #
	    $this setType

	    #
	    # Activate the introductory page
	    #
	    $::wizardInstance select intro

	    # If we have command line args, we may
	    # already be able to render a preview
	    #
	    $::wizardInstance updateRenderMenu
	}

	#
	# onCancel - called when the OK button is called
	#
	::itcl::body ExamplePage::onCancel {} {
	    $::wizardInstance selectPrev
	}

	#
	# insertHelpText - puts in the helpful text at the top of the
	#                  page
	#
	::itcl::body ExamplePage::insertHelpText {} {

	    #
	    # Short alias
	    #
	    set hw $itk_component(helpPanel)

	    $hw configure -state normal

	    $hw insert end \
		"Please select the type of image that you would like to "
	    $hw insert end \
		"make. There are currently six types from which to choose. "
	    $hw insert end \
		"Each image type requires slightly different information "
	    $hw insert end \
		"to configure. "
	    $hw insert end \
		"Use the menu button below to view an example of each type "
	    $hw insert end \
		"of picture. \n\nWhen you have decided on a type, select it "
	    $hw insert end \
		"and hit \"OK\"."

	    $hw configure -state disabled
	}
    }
    #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
