### Copyright (C) 1995, 1996, 1997 Jeppe Buk (buk@imada.sdu.dk)
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU General Public License as published by
### the Free Software Foundation; either version 2 of the License, or
### (at your option) any later version.
###
### This program 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 General Public License for more details.
###
### You should have received a copy of the GNU General Public License
### along with this program; if not, write to the Free Software
### Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

proc decorations {} {
  # Decorations
  Desc "Define sets of decor-options in so-called Decor's"
  ShortDesc "Decorations"

  Header h -text "Decorations" -frame:relief flat \
      -help "On this page you can define decorations for windows."
  Line l

  Entry decorName -text "Enter name of the Decor:" \
      -help "Here you give the decoration a name. It must be used on the" \
      "Styles of Windows page to activate it."

  Label index -text "Desc:" -label:relief groove -packLabel:ipadx 5

  ######################################################################
  # Decoration type
  #
  Line typeL
  Menu decorType -text "Type of decoration:" \
      -entries { "Title style" "Border style" "Button style" "Hilight color" }\
      -help "Select the type of decoration to define."
  Command hiFore \
      -text "Text" \
      -default [list "#FFFFFF" 0] \
      -setvalue { \
      $widget configure -bg [lindex $hiFore 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $hiFore 0] \
      [lindex $hiFore 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -packFrame:expand no
  Command hiBack \
      -text "Back" \
      -default [list "#000000" 0] \
      -setvalue { \
      $widget configure -bg [lindex $hiBack 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $hiBack 0] \
      [lindex $hiBack 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -packFrame:expand no
  Frame decorTypeFrame -entries { decorType hiFore hiBack }
  Menu buttonNo -text "Which button to decorate:" \
      -entries { All Left Right 1 2 3 4 5 6 7 8 9 0 } \
      -help "Select the button to decorate. 'All' means all buttons, 'Left'" \
      "means buttons to the left, and 'Right' means buttons to the Right." \
      "The numbers are for specific buttons."
  Menu state -text "State to decorate:" \
      -entryhelp {
    All        "Decorate all states of all windows."
    ActiveUp   "Decorate the untouched state of the active window.\
 For borders this just means the active border."
    ActiveDown "Decorate the depressed state of the active window.\
 For borders this just means the active border."
    Inactive   "Decorate the inactive windows."
  }
  Frame decorTypeF -orient top \
      -entries { typeL decorTypeFrame buttonNo state }
  #
  ######################################################################

  ######################################################################
  # Styles
  #
  Line stylesL
  Header stylesH -text Styles: -frame:relief flat
  
  ######################################################################
  # Solid color
  CheckBox solidColorB -text "Set a solid color" \
      -help "Select this if you want to specify a solid color"
  Command solidColor \
      -default [list "#000000" 0] \
      -setvalue { \
      $widget configure -bg [lindex $solidColor 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $solidColor 0] \
      [lindex $solidColor 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -packFrame:expand no
  Frame solidColorF -entries { solidColorB solidColor }
  #
  ######################################################################

  ######################################################################
  # Window font
  CheckBox windowFontB -text "Window title font" \
      -help "Select this if you want to change the font used for window" \
      "titles in this decor."
  Command windowFont \
      -default "--adobe-helvetica-*-*-*-*-*-*-*-*-*-*-*-*" \
      -setvalue {buttonSetText $widget $windowFont} \
      -invoke {FontWidget $windowFont 0} \
      -disable {$widget configure -text "Not set"
	$widget configure -font fixed} \
      -enable {buttonSetText $widget $windowFont}
  Frame windowFontF -entries { windowFontB windowFont }
  #
  ######################################################################

  ######################################################################
  # Title justification / Height
  CheckBox titleJustB -text "Title attributes" \
      -help "Select this if you want to change title attributes."
  Radio titleJust -entryhelp {
    "left"     "The title text will be left justified."
    "centered" "The title text will be centered."
    "right"    "The title text will be right justified."
  }
  Int titleHeight -text "Height" -help "Here you can enter the height of" \
      "the title bar in pixels.  Please note that specifying the font to" \
      "use in the title bar resets this, so changing of fonts must be done" \
      "before changing the height."
  Frame titleJustF -entries { titleJustB titleJust titleHeight }
  #
  ######################################################################

  ######################################################################
  # Vector (button)
  #
  CheckBox vectorB -text "Set a vector button" \
      -help "Select this if you want to specify a vector button."
  Header coordH -text "Enter button decoration coordinates" \
      -frame:relief flat
  Line coordL
  Int coordX -text "X coordinate"
  Int coordY -text "Y coordinate"
  Radio shade -entries { shadow highlight }
  ExtEntry coord -entries { coordX coordY shade }
  Window butW -text "Coordinates" -entries { coordH coordL coord }
  Frame vectorF -entries { vectorB butW }
  #
  ######################################################################

  ######################################################################
  # Gradient H/V
  #
  CheckBox gradientB -text "Linear gradient" \
      -help "Select this if you want to specify a gradient."
  Menu gradientHV -entries {Horizontal Vertical} \
      -help "Select 'Horizontal' for a horizontal gradient and 'Vertical'" \
      "for a vertical one."
  Command gradientC1 \
      -text "from" \
      -default [list "#FFFFFF" 0] \
      -setvalue { \
      $widget configure -bg [lindex $gradientC1 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $gradientC1 0] \
      [lindex $gradientC1 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -packFrame:expand no
  Command gradientC2 \
      -text "to" \
      -default [list "#000000" 0] \
      -setvalue { \
      $widget configure -bg [lindex $gradientC2 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $gradientC2 0] \
      [lindex $gradientC2 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -packFrame:expand no
  
  Frame gradientF -entries { gradientB gradientHV gradientC1 gradientC2 }
  #
  ######################################################################

  ######################################################################
  # Pixmap
  #
  CheckBox pixmapB -text "Set a pixmap" \
      -help "Select this if you want to specify a pixmap."
  Label tiledLbef -text "( tiled"
  CheckBox tiledPixmap -help "Should the pixmap be tiled? Border pixmaps" \
      "will always be tiled."
  Label tiledLaf -text ")"
  Entry pixmap -help "Enter the pixmap file name here."
  Frame pixmapF -entries { pixmapB tiledLbef tiledPixmap tiledLaf pixmap }
  #
  ######################################################################

  ######################################################################
  # Flags
  #
  Line flagsL
  Header flagsH -text Flags: -frame:relief flat

  Radio flagRFS -text "Relief:" -packLabel:side left \
      -help "Buttons and the title can get the relief specified." \
      "Note that the relief for a sunk button is inverted, so if" \
      "you want a button to be raised when pushed, you must specify" \
      "sunken relief for the active down state." \
      -entryhelp {
    Default "Select this if you don't want to specify relief."
    Raised  "Select this to get a 'raised' look."
    Flat    "Select this to get a 'flat' look."
    Sunk    "Select this to get a 'sunked' look."
  }
  Radio flagUS -text "Inherit decor:" -packLabel:side left \
      -help "Buttons can inherit the decor of the title or the decor" \
      "of the border." \
      -entryhelp {
    No                     "Don't inherit."
    "Inherit title decor"  "Inherit the decor of the title."
    "Inherit border decor" "Inherit the decor of the border."
  }

  Radio flagBorder -text "Border flags:" -packLabel:side left \
      -help "Here you can change the way Fvwm draws the border." \
      -entryhelp {
    "Default"        "Select this if you want the default appearance."
    "Hidden handles" "Select this to prevent handles from being drawn."
    "No inset"  "Select this to prevent the border relief on the inside from being drawn -- this resembles the way Win95 does."
  }
  #
  ######################################################################

  ######################################################################
  # ExtEntries
  #
  ExtEntry decorEntry -orient top -count 1 -index index \
      -packFrame:expand yes -packFrame:fill x \
      -entries {
	index decorTypeF stylesL stylesH solidColorF windowFontF titleJustF
	vectorF gradientF pixmapF flagsL flagsH flagRFS flagUS flagBorder
      }
  ExtEntry decor -orient top -count 1 -index decorName \
      -entries { decorName decorEntry }
  #
  ######################################################################

  ######################################################################
  # Change
  #
  Change {
    
    # Decor type
    if {$changeElm == "decorType"} {
      switch $decorType(name) {
	"Title style" {
	  Disable hiFore hiBack buttonNo vectorF
	  Disable flagUS flagBorder
	  set vectorB 0
	  set flagUS(name) "No"
	  set flagBorder(name) "Default"
	  Enable state solidColorF windowFontF titleJustF gradientF pixmapF
	  if {$windowFontB == 1 || $titleJustB == 1} {
	    Disable flagRFS
	    set flagRFS(name) "Default"
	  } else {
	    Enable flagRFS
	  }
	}
	"Border style" {
	  Disable hiFore hiBack buttonNo solidColorF windowFontF titleJustF \
	      vectorF gradientF
	  Disable flagRFS flagUS
	  set solidColorB 0
	  set windowFontB 0
	  set titleJustB 0
	  set vectorB 0
	  set gragientB 0
	  set flagRFS(name) "Default"
	  set flagUS(name) "No"
	  Enable state pixmapF
	  Enable flagBorder
	}
	"Button style" {
	  Disable hiFore hiBack windowFontF titleJustF
	  Disable flagBorder
	  set windowFontB 0
	  set titleJustB 0
	  set flagBorder(name) "Default"
	  Enable buttonNo state solidColorF vectorF gradientF pixmapF
	  Enable flagRFS flagUS
	}
	"Hilight color" {
	  Disable buttonNo state solidColorF windowFontF titleJustF \
	      vectorF gradientF pixmapF
	  Disable flagRFS flagUS flagBorder
	  set solidColorB 0
	  set windowFontB 0
	  set titleJustB 0
	  set vectorB 0
	  set gradientB 0
	  set pixmapB 0
	  set flagRFS(name) "Default"
	  set flagUS(name) "No"
	  set flagBorder(name) "Default"
	  Enable hiFore hiBack
	}
      }
    }

    # Checkboxes
    if {$changeElm == "solidColorB" && $solidColorB == 1} {
      set windowFontB 0
      set titleJustB 0
      set vectorB 0
      set gradientB 0
      set pixmapB 0
    } elseif {$changeElm == "windowFontB" && $windowFontB == 1} {
      set state(name) "All"
      set solidColorB 0
      set titleJustB 0
      set vectorB 0
      set gradientB 0
      set pixmapB 0
    } elseif {$changeElm == "titleJustB" && $titleJustB == 1} {
      set state(name) "All"
      set solidColorB 0
      set windowFontB 0
      set vectorB 0
      set gradientB 0
      set pixmapB 0
    } elseif {$changeElm == "vectorB" && $vectorB == 1} {
      set solidColorB 0
      set windowFontB 0
      set titleJustB 0
      set gradientB 0
      set pixmapB 0
    } elseif {$changeElm == "gradientB" && $gradientB == 1} {
      set solidColorB 0
      set windowFontB 0
      set titleJustB 0
      set vectorB 0
      set pixmapB 0
    } elseif {$changeElm == "pixmapB" && $pixmapB == 1} {
      set solidColorB 0
      set windowFontB 0
      set titleJustB 0
      set vectorB 0
      set gradientB 0
    }

    if {$changeElm != "decorType" &&
	($changeElm == "solidColorB" || $changeElm == "windowFontB" ||
	 $changeElm == "titleJustB" || $changeElm == "vectorB" ||
	 $changeElm == "gradientB" || $changeElm == "pixmapB")} {
      # Check flagRFS
      if {$decorType(name) == "Title style"} {
	if {$windowFontB == 1 || $titleJustB == 1} {
	  Disable flagRFS
	  set flagRFS(name) "Default"
	} else {
	  Enable flagRFS
	}
      }
    }

    # Index
    if {$changeElm == "decorType" || $changeElm == "solidColorB" ||
	$changeElm == "windowFontB" || $changeElm == "titleJustB" ||
	$changeElm == "vectorB" || $changeElm == "gradientB" ||
	$changeElm == "pixmapB"} {
      set index "$decorType(name)"
      switch $decorType(name) {
	"Title style"   {append index ", $state(name)"}
	"Border style"  {append index ", $state(name)"}
	"Button style"  {append index " ($buttonNo(name)), $state(name)"}
	"Hilight color" {append index ""}
      }
      if {$solidColorB == 1} {append index " - Solid color"}
      if {$windowFontB == 1} {append index " - Title font"}
      if {$titleJustB  == 1} {append index " - Title attributes"}
      if {$vectorB     == 1} {append index " - Vector"}
      if {$gradientB   == 1} {append index " - Gradient"}
      if {$pixmapB     == 1} {append index " - Pixmap"}
    }
  }
  #
  ######################################################################

  Save {
    forevery decor {
      if {$decorName != {}} {
	print "AddToDecor \"$decorName\""
	set startedTypes {}
	forevery decorEntry {
	  set decorCmd {}
	  if {$decorType(name) == "Hilight color"} {
	    append decorCmd "HilightColor"
	    append decorCmd " [hash2rgb [lindex $hiFore 0]]"
	    append decorCmd " [hash2rgb [lindex $hiBack 0]]"
	    print "+  $decorCmd"
	    continue
	  }
	  switch $decorType(name) {
	    "Title style"   {append decorCmd "AddTitleStyle"}
	    "Border style"  {append decorCmd "AddBorderStyle"}
	    "Button style"  {append decorCmd "AddButtonStyle $buttonNo(name)"}
	  }
	  if {$state(name) != "All"} {append decorCmd " $state(name)"}
	
	  # flag to check if a decor is specified
	  set noDecors 0
	  if {$solidColorB == 1} {
	    # Solid color
	    append decorCmd " Solid [hash2rgb [lindex $solidColor 0]]"
	  } elseif {$windowFontB == 1} {
	    # Window font - reset decorCmd!
	    set decorCmd "WindowFont $windowFont"
	  } elseif {$titleJustB == 1} {
	    # Title attributes - reset decorCmd!
	    set decorCmd "TitleStyle "
	    switch $titleJust(name) {
	      "left"     {append decorCmd "LeftJustified"}
	      "centered" {append decorCmd "Centered"}
	      "right"    {append decorCmd "RightJustified"}
	    }
	    if {$titleHeight != ""} {append decorCmd " Height $titleHeight"}
	  } elseif {$vectorB == 1} {
	    # Vector buttons
	    set coords 0
	    set buttonStyle ""
	    forevery coord {
	      if { $coordX != "" && $coordY != "" } {
		append buttonStyle " ${coordX}x${coordY}@$shade(index)"
		incr coords
	      }
	    }
	    if { $coords > 0 } {
	      append decorCmd " Vector $coords$buttonStyle"
	    } else {
	      error "No vector coordinates set in decor \"$decorName\""
	    }
	  } elseif {$gradientB == 1} {
	    append decorCmd " [string index $gradientHV(name) 0]Gradient 16"
	    append decorCmd " [hash2rgb [lindex $gradientC1 0]]"
	    append decorCmd " [hash2rgb [lindex $gradientC2 0]]"
	  } elseif {$pixmapB == 1} {
	    if {$tiledPixmap == 1 || $decorType(name) == "Border style"} {
	      append decorCmd " Tiled"
	    } else {
	      append decorCmd " "
	    }
	    if {$pixmap == ""} {
	      error "No pixmap specified in decor \"$decorName\""
	    } else {
	      append decorCmd "Pixmap \"$pixmap\""
	    }
	  } else {
	    set noDecors 1
	  }

	  # flag to check if a flag is specified :)
	  set noFlags 0
	  set flags {}
	  if {$flagRFS(name) != "Default" && \
	      ($decorType(name) == "Title style" \
	      || $decorType(name) == "Button style")} {
	    append flags " $flagRFS(name)"
	  } elseif {$flagUS(name) != "No" && \
	      $decorType(name) == "Button style"} {
	    if {$flagUS(name) == "Inherit title decor"} {
	      append flags " UseTitleStyle"
	    } else {
	      append flags " UseBorderStyle"
	    }
	  } elseif {$flagBorder(name) != "Default" && \
	      $decorType(name) == "Border style"} {
	    append flags " HiddenHandles"
	    if {$flagBorder(name) == "No inset"} {append flags " NoInset"}
	  } else {
	    set noFlags 1
	  }

	  if {$noDecors == 1 && $noFlags == 1} {
	    tk_dialog .__decorwarning "Warning" "No decorations or flags specified, ignoring decorationentry!" warning 0 OK
	  } else {
	    if {$noFlags == 0} {append decorCmd " --$flags"}
	    print "+  $decorCmd"
	  }
	}
      }
    }
  }
}
