#
# Mainwindow
# ----------------------------------------------------------------------
# This class implements a mainwindow containing a menubar, toolbar,
# mousebar, childsite, status line, and help line.  Each item may
# be filled and configured to suit individual needs.
#
# ----------------------------------------------------------------------
#  AUTHOR: Mark L. Ulferts              EMAIL: mulferts@austin.dsccc.com
#
#  @(#) RCS: $Id: mainwindow.itk,v 1.2 1999/01/27 18:56:34 jingham Exp $
# ----------------------------------------------------------------------
#            Copyright (c) 1997 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.
# ======================================================================

# ------------------------------------------------------------------
#                             MAINWINDOW
# ------------------------------------------------------------------
class iwidgets::Mainwindow {
    inherit iwidgets::Shell

    constructor {args} {}

    itk_option define -helpline helpLine HelpLine 1
    itk_option define -statusline statusLine StatusLine 1

    public {
	method childsite {}
	method menubar {args}
	method mousebar {args}
	method msgd {args}
	method toolbar {args}
    }

    protected {
	method _exitCB {}

	common _helpVar
	common _statusVar
    }
}

#
# Provide a lowercased access method for the ::iwidgets::Mainwindow class.
# 
proc iwidgets::mainwindow {pathName args} {
    uplevel ::iwidgets::Mainwindow $pathName $args
}

# ------------------------------------------------------------------
#                           CONSTRUCTOR
# ------------------------------------------------------------------
body iwidgets::Mainwindow::constructor {args} {
    itk_option add hull.width hull.height

    pack propagate $itk_component(hull) no

    wm protocol $itk_component(hull) WM_DELETE_WINDOW [code $this _exitCB]

    #
    # Create a menubar, renaming the font, foreground, and background
    # so they may be separately set.  The help variable will be setup
    # as well.
    #
    itk_component add menubar {
	iwidgets::Menubar $itk_interior.menubar \
	  -helpvariable [scope _helpVar($this)] 
    } {
	keep -disabledforeground -cursor \
	    -highlightbackground -highlightthickness
	rename -font \
	    -menubarfont menuBarFont Font
	rename -foreground \
	    -menubarforeground menuBarForeground Foreground
	rename -background \
	    -menubarbackground menuBarBackground Background
    }

    #
    # Add a toolbar beneath the menubar.
    #
    itk_component add toolbar {
	iwidgets::Toolbar $itk_interior.toolbar -orient horizontal \
	    -helpvariable [scope _helpVar($this)] 
    } {
	keep -balloonbackground -balloondelay1 -balloondelay2 \
	    -balloonfont -balloonforeground -disabledforeground -cursor \
	    -highlightbackground -highlightthickness
	rename -font -toolbarfont toolbarFont Font
	rename -foreground -toolbarforeground toolbarForeground Foreground
	rename -background -toolbarbackground toolbarBackground Background
    }

    #
    # Add a mouse bar on the left.  
    #
    itk_component add mousebar {
	iwidgets::Toolbar $itk_interior.mousebar -orient vertical \
	    -helpvariable [scope _helpVar($this)] 
    } {
	keep -balloonbackground -balloondelay1 -balloondelay2 \
	    -balloonfont -balloonforeground -disabledforeground -cursor \
	    -highlightbackground -highlightthickness
	rename -font -toolbarfont toolbarFont Font
	rename -foreground -toolbarforeground toolbarForeground Foreground
	rename -background -toolbarbackground toolbarBackground Background
    }

    #
    # Create the childsite window window.
    #
    itk_component add -protected mwchildsite {
	frame $itk_interior.mwchildsite
    } 

    #
    # Add the help and system status lines
    #
    itk_component add -protected lineframe {
	frame $itk_interior.lineframe
    }

    itk_component add help {
	label $itk_component(lineframe).help \
	    -textvariable [scope _helpVar($this)] \
	    -relief sunken -borderwidth 2 -width 10
    }

    itk_component add status {
	label $itk_component(lineframe).status \
	    -textvariable [scope _statusVar($this)] \
	    -relief sunken -borderwidth 2 -width 10
    }

    #
    # Create the message dialog for use throughout the mainwindow.
    #
    itk_component add msgd {
	iwidgets::Messagedialog $itk_interior.msgd -modality application
    } {
      usual
      ignore -modality
    }

    #
    # Use the grid to pack together the menubar, toolbar, mousebar,
    # childsite, and status area.
    #
    grid $itk_component(menubar) -row 0 -column 0 -columnspan 2 -sticky ew
    grid $itk_component(toolbar) -row 1 -column 0 -columnspan 2 -sticky ew
    grid $itk_component(mousebar) -row 2 -column 0 -sticky ns 
    grid $itk_component(mwchildsite) -row 2 -column 1 -sticky nsew \
	-padx 5 -pady 5 
    grid $itk_component(lineframe) -row 3 -column 0 -columnspan 2 -sticky ew

    grid columnconfigure $itk_interior 1 -weight 1
    grid rowconfigure $itk_interior 2 -weight 1

    #
    # Set the interior to be the childsite for derived classes.
    #
    set itk_interior $itk_component(mwchildsite)

    #
    # Initialize all the configuration options.
    #
    eval itk_initialize $args
}

# ------------------------------------------------------------------
#                             OPTIONS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# OPTION: -helpline
#
# Specifies whether or not to display the help line.  The value
# may be given in any of the forms acceptable to Tk_GetBoolean.
# ------------------------------------------------------------------
configbody iwidgets::Mainwindow::helpline {
    if {$itk_option(-helpline)} {
	pack $itk_component(help) -side left -fill x -expand yes -padx 2
    } else {
	pack forget $itk_component(help)
    }
}

# ------------------------------------------------------------------
# OPTION: -statusline
#
# Specifies whether or not to display the status line.  The value
# may be given in any of the forms acceptable to Tk_GetBoolean.
# ------------------------------------------------------------------
configbody iwidgets::Mainwindow::statusline {
    if {$itk_option(-statusline)} {
	pack $itk_component(status) -side right -fill x -expand yes -padx 2
    } else {
	pack forget $itk_component(status)
    }
}

# ------------------------------------------------------------------
#                            METHODS
# ------------------------------------------------------------------

# ------------------------------------------------------------------
# METHOD: childsite
#
# Return the childsite widget.
# ------------------------------------------------------------------
body iwidgets::Mainwindow::childsite {} {
    return $itk_component(mwchildsite)
}

# ------------------------------------------------------------------
# METHOD: menubar ?args?
#
# Evaluate the args against the Menubar component.
# ------------------------------------------------------------------
body iwidgets::Mainwindow::menubar {args} {
    if {[llength $args] == 0} {
	return $itk_component(menubar)
    } else {
	return [eval $itk_component(menubar) $args]
    }
}

# ------------------------------------------------------------------
# METHOD: toolbar ?args?
#
# Evaluate the args against the Toolbar component.
# ------------------------------------------------------------------
body iwidgets::Mainwindow::toolbar {args} {
    if {[llength $args] == 0} {
	return $itk_component(toolbar)
    } else {
	return [eval $itk_component(toolbar) $args]
    }
}

# ------------------------------------------------------------------
# METHOD: mousebar ?args?
#
# Evaluate the args against the Mousebar component.
# ------------------------------------------------------------------
body iwidgets::Mainwindow::mousebar {args} {
    if {[llength $args] == 0} {
	return $itk_component(mousebar)
    } else {
	return [eval $itk_component(mousebar) $args]
    }
}

# ------------------------------------------------------------------
# METHOD: msgd ?args?
#
# Evaluate the args against the Messagedialog component.
# ------------------------------------------------------------------
body iwidgets::Mainwindow::msgd {args} {
    if {[llength $args] == 0} {
	return $itk_component(msgd)
    } else {
	return [eval $itk_component(msgd) $args]
    }
}

# ------------------------------------------------------------------
# PRIVATE METHOD: _exitCB
#
# Menu callback for the exit option from the file menu.  The method
# confirms the user's request to exit the application prior to
# taking the action.
# ------------------------------------------------------------------
body iwidgets::Mainwindow::_exitCB {} {
    #
    # Configure the message dialog for confirmation of the exit request.
    #
    msgd configure -title Confirmation -bitmap questhead \
	    -text "Exit confirmation\n\
		   Are you sure ?"
    msgd buttonconfigure OK -text Yes
    msgd buttonconfigure Cancel -text No
    msgd default Cancel
    msgd center $itk_component(hull)

    #
    # Activate the message dialog and given a positive response 
    # proceed to exit the application
    #
    if {[msgd activate]} {
	::exit
    }    
}
