#
# Copyright (c) 1993 Eric Schenk.
# All rights reserved.
#
# Permission is hereby granted, without written agreement and without
# license or royalty fees, to use, copy, modify, and distribute this
# software and its documentation for any purpose, provided that the
# above copyright notice and the following two paragraphs appear in
# all copies of this software.
# 
# IN NO EVENT SHALL ERIC SCHENK BE LIABLE TO ANY PARTY FOR
# DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
# OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF ERIC
# SCHENK HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# ERIC SCHENK SPECIFICALLY DISCLAIMS ANY WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
# ON AN "AS IS" BASIS, AND ERIC SCHENK HAS NO OBLIGATION TO
# PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
#



# Special treatment
#
# 	resname.iconWidth:	int
# 	resname.iconHeight:	int
#	resname.iconBitmap:	choice
#	resname.clientManager:	string
#	resname.iconManager:	string
#
# "option set"able settings
#
#
# Configurable settings
#
# 	cursor:					choice
# 	decoration.font:			font
# 	decoration.cursor:			choice
# 	decoration.activeForeground:		color
# 	decoration.activeBackground:		color
# 	decoration.foreground:			color
# 	decoration.background:			color
# 	icon.decoration.font:			font
# 	icon.decoration.cursor:			choice
# 	icon.decoration.activeForeground:	color
# 	icon.decoration.activeBackground:	color
# 	icon.decoration.foreground:		color
# 	icon.decoration.background:		color
#
#{configure string help pathSpec resource type}
#{special string help resource type setting-proc}
#
#pathSpec can be empty in which case no "config" is run.
#
#type = ON	(positive boolean)
#type = OFF	(negative boolean)
#type = POS_INT	(positive integer value)
#type = STRING   (string value)
#type = CHOICE varname	(Choice of n from list $varname)
#type = CHOICE {s1 s2 s3 s4 ... sn} (Choice of n fixed)
#type = FONT     (font value)

set clientPrefs {
    {Boolean "Window Titlebar" "" .decoration -titlebar}
    {Entry   "Titlebar Font"   "" .decoration -font}
    {Entry   "Frame Cursor"   "" .decoration -cursor}
    {Entry   "Foreground"   "" .decoration -foreground}
    {Entry   "Background"   "" .decoration -background}
    {Entry   "Active Foreground"   "" .decoration -activeForeground}
    {Entry   "Active Background"   "" .decoration -activeBackground}
    {RBoolean "Constrained Move" "" constrained Constrained}
    {RBoolean "Opaque Move"      "" opaqueMove OpaqueMove}
    {RBoolean "Opaque Resize"    "" opaqueResize OpaqueResize}
    {RBoolean "Title Transients" "" transientsTitled TransientsTitled}
}
#    {Boolean "Icon Titlebar"   "" .icon.decoration -titlebar}
#	{"Title Font"      "" .decoration -font Font}
#	{"Frame Cursor"    "" .decoration -cursor Cursor}

proc TkwmPType {list} {lindex $list 0}
proc TkwmPTitle {list} {lindex $list 1}
proc TkwmPHelp {list} {lindex $list 2}
proc TkwmPPath {list} {lindex $list 3}
proc TkwmPOption {list} {lindex $list 4}
proc TkwmPRName {list} {lindex $list 3}
proc TkwmPRClass {list} {lindex $list 4}

# Set the initial value from a config entry
proc TkwmPrefInitializeValue {path win entry} {
    global $path
    set $path [lindex [$win[TkwmPPath $entry] config [TkwmPOption $entry]] 4]
}

# Set the value for a config entry
proc TkwmPrefSetValue {path win entry} {
    global $path
    handleError {
       $win[TkwmPPath $entry] config [TkwmPOption $entry] [set $path]
    } {
	TkwmPrefInitializeValue $path $win $entry
    }
}

proc TkwmPrefWidgetBoolean {path win entry} {
    frame $path
    TkwmPrefTWidget $path $entry
    TkwmPrefInitializeValue $path $win $entry
    checkbutton $path.button -text "On" -variable $path \
	-command [list TkwmPrefSetValue $path $win $entry]
    pack $path.label $path.button -side left -anchor w -pady 2 -padx 2
}

proc TkwmPrefSetEntry {path win entry} {
    global $path
    set $path [$path.entry get]
    TkwmPrefSetValue $path $win $entry
    $path.entry delete 0 end
    $path.entry insert 0 [set $path]
}

proc TkwmPrefWidgetEntry {path win entry} {
    global $path
    frame $path
    TkwmPrefTWidget $path $entry
    TkwmPrefInitializeValue $path $win $entry
    entry $path.entry -width 50 -rel sunken
    $path.entry insert 0 [set $path]
    bind $path.entry <Return> [list TkwmPrefSetEntry $path $win $entry]
    pack $path.label $path.entry -side left -anchor w -pady 2 -padx 2
}

# Set the initial value from a config entry
proc TkwmPrefRInitializeValue {path win entry} {
    global $path
    set $path [get_resource_value $win [TkwmPRName $entry] [TkwmPRClass $entry]]
}

# Set the value for a config entry
proc TkwmPrefRSetValue {path win entry} {
    global $path $win
    option add Tk[set ${win}(res_name)].[TkwmPRName $entry] [set $path]
}

proc TkwmPrefTWidget {path entry} {
    label $path.label -text [TkwmPTitle $entry] -width 30 -anchor w
}


proc TkwmPrefWidgetRBoolean {path win entry} {
    frame $path
    TkwmPrefTWidget $path $entry
    TkwmPrefRInitializeValue $path $win $entry
    checkbutton $path.button -text "On" -variable $path \
	-command [list TkwmPrefRSetValue $path $win $entry]
    pack $path.label $path.button -side left -anchor w -padx 2 -pady 2
}

proc client_preferences {win} {
    global clientPrefs

    catch {destroy $win.preferences}
    set name $win.preferences
    set title "Tkwm Window Preferences"
    set inside [create_iwindow $name tkwmWindowPrefs TkwmWindowPrefs]

    wm iconname $name "Preferences"
    wm minsize $name 1 1
    wm iconbitmap $name info

    wm title $name $title
    $name.decoration config -title [wm title $name]

    frame $inside.buttons
    pack $inside.buttons -side bottom -fill x -expand 0 -pady 5

    button $inside.buttons.quit -text "Dismiss" -command [list destroy $name]
    pack $inside.buttons.quit -side bottom

    set i 0
    foreach item $clientPrefs {
	TkwmPrefWidget[TkwmPType $item] $inside.$i $win $item
    	pack $inside.$i -side top -anchor w -expand yes
	incr i
    }

    update idletasks
    set x [expr [winfo screenwidth $name]/2 - [winfo reqwidth $name]/2 \
	    - [winfo vrootx [winfo parent $name]]]
    set y [expr [winfo screenheight $name]/2 - [winfo reqheight $name]/2 \
	    - [winfo vrooty [winfo parent $name]]]
    wm geom $name +$x+$y
    imap_request $name 0
}
