#.====================================================================
# [incr Tk] File
#.====================================================================
#. $Revision: 1.1.6.1 $
#. $Date: 2009/05/21 13:39:53 $
#.====================================================================
#. File Name:     EuidlScrolledframe.itk
#. Author:        Eric Teagle
#. Description:   This frame provides special behaviors and is to be
#.                used for all ANSYS GUI frame components.
#.
#.====================================================================
#. Copyright 1999 SAS IP, Inc. Proprietary Data.
#. Unauthorized use, distribution, or duplication is prohibited.  
#. All Rights Reserved.
#.====================================================================

#
# Scrolledframe
# ----------------------------------------------------------------------
# Implements horizontal and vertical scrollbars around a childsite
# frame.  Includes options to control display of scrollbars.
#
# ----------------------------------------------------------------------
#  AUTHOR: Mark Ulferts                        mulferts@austin.dsccc.com 
#
#  @(#) $Id: euidlscrolledframe.itk,v 1.1.6.1 2009/05/21 13:39:53 jtm Exp $
# ----------------------------------------------------------------------
#            Copyright (c) 1995 DSC Technologies Corporation
# ======================================================================
# Permission to use, copy, modify, distribute and license this software 
# and its documentation for any purpose, and without fee or written 
# agreement with DSC, is hereby granted, provided that the above copyright 
# notice appears in all copies and that both the copyright notice and 
# warranty disclaimer below appear in supporting documentation, and that 
# the names of DSC Technologies Corporation or DSC Communications 
# Corporation not be used in advertising or publicity pertaining to the 
# software without specific, written prior permission.
# 
# DSC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING 
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND NON-
# INFRINGEMENT. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, AND THE
# AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO PROVIDE MAINTENANCE, 
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. IN NO EVENT SHALL 
# DSC BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR 
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, 
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTUOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS 
# SOFTWARE.
# ======================================================================

#---------------------------------------------------------------------
#  Default Resources
#---------------------------------------------------------------------
option add *EuidlScrolledframe.width 100 widgetDefault
option add *EuidlScrolledframe.height 100 widgetDefault
option add *EuidlScrolledframe.labelPos n widgetDefault

# ------------------------------------------------------------------
# Usual options.
# ------------------------------------------------------------------
itk::usual EuidlScrolledframe {
   keep -activebackground -activerelief -background -borderwidth -cursor \
      -elementborderwidth -foreground -highlightcolor -highlightthickness \
      -jump -labelfont -troughcolor
}

# ====================================================================
# = EuidlScrolledframe CLASS DEFINITION
# ====================================================================
class ::euidl::EuidlScrolledframe {

   # INHERITANCE
   inherit iwidgets::Scrolledwidget ::euidl::EuidlContextMenu

   #
   constructor {args} {}
   destructor {}

   # OPTIONS

   # PRIVATE (private to the world public to the class)

   # PUBLIC
   public {
      # methods
      method childsite {} 
      method justify {direction} 
      method xview {args} 
      method yview {args} 
   }

   # PROTECTED
   protected {
      # methods
      method _configureCanvas {} 
      method _configureFrame {} 
   }
}

# ------------------------------------------------------------------
# Provide a lowercased access method for the EuidlScrolledframe class.
# ------------------------------------------------------------------
proc ::euidl::scrolledframe {pathName args} {
   uplevel ::euidl::EuidlScrolledframe $pathName $args
}

# ------------------------------------------------------------------
#  CONSTRUCTOR
# ------------------------------------------------------------------
body euidl::EuidlScrolledframe::constructor {args} {
   itk_option remove iwidgets::Labeledwidget::state

   itk_option add ::euidl::EuidlContextMenu::anscmd
   itk_option add ::euidl::EuidlContextMenu::contextmenu
   itk_option add ::euidl::EuidlContextMenu::dialogoverview
   itk_option add ::euidl::EuidlContextMenu::whatsthis

   # Create a clipping frame which will provide the border for
   # relief display.
   itk_component add clipper {
      frame $itk_interior.clipper 
   } {
      usual

      keep -borderwidth -relief 
   }   
   grid $itk_component(clipper) -row 0 -column 0 -sticky nsew
   grid rowconfigure $_interior 0 -weight 1
   grid columnconfigure $_interior 0 -weight 1

   # Create a canvas to scroll
   itk_component add canvas {
      canvas $itk_component(clipper).canvas \
         -height 1.0 -width 1.0 \
         -scrollregion "0 0 1 1" \
         -xscrollcommand [code $this _scrollWidget $itk_interior.horizsb] \
         -yscrollcommand [code $this _scrollWidget $itk_interior.vertsb] \
         -highlightthickness 0 -takefocus 0
    } {
      ignore -highlightcolor -highlightthickness
      keep -background -cursor
   }
   grid $itk_component(canvas) -row 0 -column 0 -sticky nsew
   grid rowconfigure $itk_component(clipper) 0 -weight 1
   grid columnconfigure $itk_component(clipper) 0 -weight 1
  
   # Configure the command on the vertical scroll bar in the base class.
   $itk_component(vertsb) configure \
      -command [code $itk_component(canvas) yview]

   # Configure the command on the horizontal scroll bar in the base class.
   $itk_component(horizsb) configure \
      -command [code $itk_component(canvas) xview]
    
   # Handle configure events on the canvas to adjust the frame size
   # according to the scrollregion.
   bind $itk_component(canvas) <Configure> [code $this _configureCanvas]
   
   # Create a Frame inside canvas to hold the frame to be scrolled 
   itk_component add -protected sfchildsite {
      frame $itk_component(canvas).sfchildsite 
   } {
      keep -background -cursor
   }
   pack propagate $itk_component(sfchildsite) 0

   # Create a Frame inside the canvas to hold widgets to be scrolled 
   itk_component add -protected sfChildsite {
      frame $itk_component(sfchildsite).sfChildsite 
   } {
      keep -background -cursor
   }
   bind $itk_component(sfChildsite) <Configure> [code $this _configureCanvas]
   #pack $itk_component(sfChildsite) -fill both -expand yes
   pack $itk_component(sfChildsite) -fill both

   $itk_component(canvas) create window 0 0 -tags frameTag \
      -window $itk_component(sfchildsite) -anchor nw
   set itk_interior $itk_component(sfChildsite)
   bind $itk_component(sfchildsite) <Configure> [code $this _configureFrame]
   bind $itk_component(sfchildsite) <Configure> +[code $this _configureCanvas]
    
   mwbindtags $itk_component(hull) EuidlScrolledframe

   # Initialize the widget based on the command line options.
   eval itk_initialize $args
}

# ------------------------------------------------------------------
#  DESTURCTOR
# ------------------------------------------------------------------
body ::euidl::EuidlScrolledframe::destructor {} {
}


# ====================================================================
# = METHODS
# ====================================================================

# ------------------------------------------------------------------
# PUBLIC METHOD:  childsite
#
# Purpose:        Returns the path name of the child site widget.
#
# Arguments:      N/A
#
# Return Value:   N/A
#
# Comments:       N/A
#
# See Also:       N/A
#
# ------------------------------------------------------------------
body ::euidl::EuidlScrolledframe::childsite {} {
   return $itk_component(sfChildsite)
}

# ------------------------------------------------------------------
# PUBLIC METHOD:  justify
#
# Purpose:        Justifies the scrolled region in one of four directions: top,
#                 bottom, left, or right.
#
# Arguments:      direction - top, bottom, left, or right
#
# Return Value:   N/A
#
# Comments:       N/A
#
# See Also:       N/A
#
# ------------------------------------------------------------------
body ::euidl::EuidlScrolledframe::justify {direction} {
   if {[winfo ismapped $itk_component(canvas)]} {
      update idletasks
   
      switch $direction {
         left {
            $itk_component(canvas) xview moveto 0
         }
         right {
            $itk_component(canvas) xview moveto 1
         }
         top {
            $itk_component(canvas) yview moveto 0
         }
         bottom {
            $itk_component(canvas) yview moveto 1
         }
         default {
            error "bad justify argument \"$direction\": should be\
               left, right, top, or bottom"
         }
      }
   }
}

# ------------------------------------------------------------------
# PUBLIC METHOD:  xview
#
# Purpose:        Adjust the view in the frame so that character position index
#                 is displayed at the left edge of the widget.
#
# Arguments:      index
#
# Return Value:   N/A
#
# Comments:       N/A
#
# See Also:       N/A
#
# ------------------------------------------------------------------
body ::euidl::EuidlScrolledframe::xview {args} {
   return [eval $itk_component(canvas) xview $args]
}

# ------------------------------------------------------------------
# PUBLIC METHOD:  yview
#
# Purpose:        Adjust the view in the frame so that character position index
#                 is displayed at the top edge of the widget.
#
# Arguments:      index
#
# Return Value:   N/A
#
# Comments:       N/A
#
# See Also:       N/A
#
# ------------------------------------------------------------------
body ::euidl::EuidlScrolledframe::yview {args} {
   return [eval $itk_component(canvas) yview $args]
}

# ------------------------------------------------------------------
# PRIVATE METHOD: _configureCanvas 
#
# Purpose:        Responds to configure events on the canvas widget.  When
#                 canvas changes size, adjust frame size.
#
# Arguments:      N/A
#
# Return Value:   N/A
#
# Comments:       N/A
#
# See Also:       N/A
#
# ------------------------------------------------------------------
body ::euidl::EuidlScrolledframe::_configureCanvas {} { 
   set srw1 [winfo width $itk_component(canvas)]
   set srw2 [winfo reqwidth $itk_component(sfChildsite)]
   set srw2 [expr $srw2 + 2]
   if {$srw1 > $srw2} {
      set srw $srw1
   } else {
      set srw $srw2
   }
   set srh [winfo reqheight $itk_component(sfChildsite)]
   set srh [expr $srh + 2]

   $itk_component(sfchildsite) configure -height $srh -width $srw
}

# ------------------------------------------------------------------
# PRIVATE METHOD: _configureFrame 
#
# Purpose:        Responds to configure events on the frame widget.  When the
#                 frame changes size, adjust scrolling region size.
#
# Arguments:      N/A
#
# Return Value:   N/A
#
# Comments:       N/A
#
# See Also:       N/A
#
# ------------------------------------------------------------------
body ::euidl::EuidlScrolledframe::_configureFrame {} {
   $itk_component(canvas) configure \
      -scrollregion [$itk_component(canvas) bbox frameTag] 
}
