### 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 FvwmButtons {} {
  # Modules / FvwmButtons
  Desc "Configuration of the FvwmButtons module"
  ShortDesc "FvwmButtons"

  Header h -text "FvwmButtons" -frame:relief flat \
      -help "On this page you configure the FvwmButtons module. This"\
      "module is used for creating a panel of buttons, indicators, etc."
  Line l
  CheckBox spawn -text "Spawn this module when Fvwm2 starts" \
      -help "If you check this option the module will be spawned from"\
      "the two functions 'InitFunction' and 'RestartFunction'. You will"\
      "not have to insert them yourself then."

  # Global settings
  CheckBox colorB -text "Specify the colors for" -help "Check this"\
      "button if you want to specify default colors of the buttons you"\
      "create."
  Command foreColor \
      -text "foreground" \
      -default [list "black" 0] \
      -setvalue {
	$widget configure -bg [lindex $foreColor 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $foreColor 0] \
		   [lindex $foreColor 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -help "In this widget you can set the default color of text (and"\
      "foreground of monochrome bitmaps) on your buttons."
  Command backColor \
      -text "background" \
      -default [list "white" 0] \
      -setvalue {
	$widget configure -bg [lindex $backColor 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $backColor 0] \
		   [lindex $backColor 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -help "In this widget you can set the default color of"\
      "backgrounds on you buttons."
  Frame colorF -entries { colorB foreColor backColor }
  CheckBox fontB -text "Specify font" -help "Check this button if you"\
      "want to set the default font used for text on your buttons."
  Command font \
      -default "--adobe-helvetica-*-*-*-*-*-*-*-*-*-*-*-*" \
      -setvalue {buttonSetText $widget $font} \
      -invoke {FontWidget $font 0} \
      -disable {$widget configure -text "Not set"
	$widget configure -font fixed} \
      -enable {buttonSetText $widget $font} \
      -help "In this widget you can set the default font to use for"\
      "text on your buttons."
  Frame fontF -entries { fontB font }
  CheckBox noFont -text "Prevent text on buttons" -help "If you check"\
      "this option there will be no titles on your buttons. Use this if"\
      "you want an icon to occupy as large an area of the buttons as"\
      "possible."
  Int columns -text "Number of columns to use" -help "Here you can"\
      "specify the number of columns to use. If you do not specify"\
      "this, FvwmButtons will use two rows of buttons and the required"\
      "number of columns. If you specify both rows and columns and"\
      "there are not enough room, this setting will be ignored."
  Int rows -text "Number of rows to use" -help "Here you can specify"\
      "the number of rows to use. The default is two rows. If both the"\
      "number of rows and columns are specified the columns setting"\
      "will be ignored."
  Int frame -text "Width of reliefs on buttons" -help "Here you can"\
      "specify the default width of reliefs on buttons. If you specify"\
      "a negative value, the relief will be inverted meaning that the"\
      "button will look 'sunken' when inactive and 'raised' while"\
      "activated."
  Label paddingL -text "Space between a button's\nrelief and it's contents" \
      -labelBefore:justify left -help "Here you specify the space"\
      "between a buttons relief and it contents. The default is two"\
      "pixels in the horizontal direction and four pixels in the"\
      "vertical direction. Swallowed windows and containers have no"\
      "space by default."
  Int xpadding -text "Horizontal" -help "Here you enter the horizontal"\
      "space between a buttons relief and its contents."
  Int ypadding -text "Vertical" -help "Here you enter the vertical"\
      "space between a buttons relief and its contents."
  Frame paddingF -entries { paddingL xpadding ypadding }
  CheckBox geomB -text "Specify placement: " -help "You can specify the"\
      "location that FvwmButtons should start at. If the coordinates"\
      "are positive they are measured from the top left of the screen,"\
      "if they are negative they are measured from the bottom right of"\
      "the screen."
  Int xgeom -text "X coordinate" -help "Here you enter the startup"\
      "x-coordinate."
  Int ygeom -text "Y coordinate" -help "Here you enter the startup"\
      "y-coordinate."
  Frame geom -entries { geomB xgeom ygeom }
  Window global -text "Global settings" -entries {
    colorF fontF noFont columns rows frame paddingF geom
  } -pageEnd {
    if {$geomB == 1 && ($xgeom == "" || $ygeom == "" ||
			$xgeom == "-" || $ygeom == "-")} {
      error "Invalid placement specification"
    }
    if {($xpadding != "" && $ypadding == "") ||
	($xpadding == "" && $ypadding != "")} {
      error "You must specify both horizontal and vertical padding!"
    }
  } -help "This window contains global settings for FvwmButtons."

  # Buttons

  Label index -label:relief groove -help "This is an index label used"\
      "for quick navigation of the buttons."

  Line fL
  CheckBox fileB -text "Read this button from file" -help "If desired"\
      "you can read a button description from a file. The file is in"\
      "the same format as Fvwm2's configuration file, but each line is"\
      "read as prefixed by *FvwmButtons."
  File-Dir-Browser file
  Help file "Here you can enter the file name yourself."
  Help com_file "If you press this button you will get a browser for"\
      "the file name."
  Frame fileF1 -entries { fileB frame_file }
  Frame fileF -entries { fL fileF1 } -orient top

  # Title
  Entry title -text "Title on the button" -help "Here you can enter a"\
      "title for your button. Unless you have selected 'Prevent text on"\
      "buttons' in global settings this text will be written on the"\
      "button."
  Radio titleJust -entryhelp {
    "Left justified" "Left justify the text below the (optional) icon."
    Centered "Center the text below the (optional) icon."
    "Right justified" "Right justify the text below the (optional) icon."
    "Right side of icon" "Place the text on the right side of the\
 (optional) icon - as in menus."
  } -default Centered
  Frame titleF -entries { title titleJust } -orient top

  # Swallowing
  CheckBox swallowB -text "Swallow a program/module" -help "Check this"\
      "option if you want to create a button containing a program or a"\
      "module."
  Entry hangon -text "Name of the window to wait for" -help "Here you"\
      "must enter a window name to wait for to swallow. You can find"\
      "out this name by using the FvwmIdent module. Also most X"\
      "programs knows about the '-name' option to specify the name of"\
      "the window."
  CheckBox swallowProgB -text Program -help "Check this option if you"\
      "want to swallow a program (not a module)."
  Entry swallowProg -width 50 \
      -help "Enter the program to spawn"\
      "here. This program must be started in the background, so if you"\
      "don't append '&' to it I will."
  Frame swallowProgF -entries { swallowProgB swallowProg } \
      -packFrame:expand yes
  Radio swallowclose -entryhelp {
    "Close on exit" "Close this program when FvwmButtons exits."
    "Unswallow on exit" "Leave this program when FvwmButtons exits."
  }
  Radio swallowkill -entryhelp {
    "Close program by message" "Close program in the nicest possible way."
    "Close program by killing" "Close program in a sure, but violent way."
  }
  Radio swallowhint -entryhelp {
    "Respect program hints" "Respect programs wishes regarding size etc."
    "Ignore program hints" "Force resizing, possibly against programs wishes."
  }
  Radio swallowrespawn -entryhelp {
    "Ignore if program dies" "Do nothing is the swallowed program dies."
    "Respawn if program dies" "Start the program again, if it dies."
  }
  Radio swallowuseold -entryhelp {
    "Always spawn a new program" "Ignore existing windows when creating\
 this button."
    "Use existing window if possible" "If a window with the correct name\
 exists, swallow it instead of starting a new program."
  }
  Radio swallowusetitle -entryhelp {
    "Hide button title" "This button will not get a title."
    "Use window name as title" "The button title will be updated dynamically\
 to show the window title of the swallowed program."
  }
  Frame swallowOptsF -entries {
    swallowclose swallowkill swallowhint swallowrespawn
    swallowuseold swallowusetitle
  } -orient top
  Label optsL -text "   "
  Frame swallowOpts -entries { optsL swallowOptsF }
  CheckBox swallowModB -text Module -help "Check this option if you"\
      "want to swallow an Fvwm2 module."
  ComboBox swallowMod -entryhelp {
    FvwmIconBox "A window to iconify to."
    FvwmIconMan "A list of icon names."
    FvwmPager "A module facilitating changing pages."
    FvwmTalk "A module for sending commands to Fvwm2."
    FvwmWinList "A list of all windows."
  }
  Int pager1 -text "First desk" -help "This is the first desk the pager"\
      "will show (numbered from 0 and up)."
  Int pager2 -text "Last desk" -help "This is the last desk the pager"\
      "will show (numbered from 0 and up)."
  Frame swallowModF -entries { swallowModB swallowMod pager1 pager2 }
  Window swallowW -entries {
    hangon swallowProgF swallowModF swallowOpts
  } -text "Swallow configuration" -help "Specify what to swallow" \
      -pageEnd {
	if {$swallowB == 1 &&
	    ($hangon == "" ||
	     ($swallowProgB == 1 && $swallowProg == "") ||
	     ($swallowModB  == 1 && $swallowMod  == "") ||
	     ($swallowProgB == 0 && $swallowModB == 0 ) )} {
	  error "You must specify both a window name to wait for and a\
 program or module to spawn in swallow configuration!"
	}
      }
  Frame swallowF -entries { swallowB swallowW }

  # Action
  CheckBox actionB -text "Specify action" -help "Check this option if"\
      "you want to associate an action to this button."
  Entry actionHang -text "     Name of the window to wait for" -help "If you"\
      "want the button to stay pressed down until a window emerges you"\
      "can enter a window name to wait for here. You can find this name"\
      "by using FvwmIdent or (for most X programs) passing the option"\
      "'-name' to the program."
  global Builtins
  ComboBox builtin -noedit 1 -text "     Builtin" -default Beep \
      -entryhelp $Builtins \
      -help "Select a function to call from this menu"
  Entry arguments -text "Arguments" \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility) - see help for the individual commands."
  Frame actF -entries {builtin arguments}
  Frame actionF -orient top -entries { actionB actionHang actF }

  # Width x height
  Label whLab -text "Specify" \
      -labelBefore:justify left \
      -help "Here you can specify that a button should be a number of"\
      "times wider and/or taller than the normal-sized buttons"\
      "(calculated by FvwmButtons)."
  Int width -text "width" -help "Enter the number of times wider than"\
      "normal-sized buttons this button should be."
  Int height -text "height" -help "Enter the number of times taller than"\
      "normal-sized buttons this button should be."
  Frame widthheightF -entries { whLab width height }

  # Colors
  CheckBox addcolorB -text "Specify the colors for" -help "Check this"\
      "option if you want to change the colors of this button."
  Command addforeColor \
      -text "foreground" \
      -default [list "black" 0] \
      -setvalue {
	$widget configure -bg [lindex $addforeColor 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $addforeColor 0] \
		   [lindex $addforeColor 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -help "In this widget you can change the color of text and"\
      "monochrome bitmaps on this button."
  Command addbackColor \
      -text "background" \
      -default [list "white" 0] \
      -setvalue {
	$widget configure -bg [lindex $addbackColor 0] -text "      "} \
      -invoke {ColorWidget $colorList [lindex $addbackColor 0] \
		   [lindex $addbackColor 1]} \
      -disable {$widget configure -relief flat } \
      -enable {$widget configure -relief raised } \
      -help "In this widget you can change the background color of this"\
      "button."
  Frame addcolorF -entries { addcolorB addforeColor addbackColor }

  # Font
  CheckBox addfontB -text "Specify font" -help "Check this option if"\
      "you want to change the font used for text on this button."
  Command addfont \
      -default "--adobe-helvetica-*-*-*-*-*-*-*-*-*-*-*-*" \
      -setvalue {buttonSetText $widget $addfont} \
      -invoke {FontWidget $addfont 0} \
      -disable {$widget configure -text "Not set"
	$widget configure -font fixed} \
      -inactive {$widget configure -text "Not set"
	$widget configure -font fixed} \
      -enable {buttonSetText $widget $addfont} \
      -help "In this widget you can change the font used for text on"\
      "this button."
  Frame addfontF -entries { addfontB addfont }
  CheckBox addnoFont -text "Prevent text on buttons" -help "Check this"\
      "option if you want to prevent text on this button."

  # Spacing
  Int addframe -text "Width of reliefs on buttons" -help "Here you can"\
      "specify the width of this button's relief. If you specify a"\
      "negative value the button will look 'sunken' when inactive and"\
      "'raised' when activated."
  Label addpaddingL \
      -text "Space between a button's relief and it's contents:" \
      -labelBefore:justify left -help "Here you can enter the space"\
      "between the relief and contents of this button. The default is"\
      "two pixels in the horizontal direction, and four pixels in the"\
      "vertical direction. Swallowed buttons and containers are not"\
      "padded by default."
  Int addxpadding -text "Horizontal" -help "Here you can enter the"\
      "horizontal space between the relief and contents of this button"\
      "in pixels."
  Int addypadding -text "Vertical" -help "Here you can enter the"\
      "vertical space between the relief and contents of this button"\
      "in pixels."
  Frame addpaddingF1 -entries { addxpadding addypadding }
  Frame addpaddingF -entries { addpaddingL addpaddingF1 } -orient top

  # Icon
  Entry addicon -text "Specify icon for this button" -help "If you want"\
      "an icon on this button you can enter the file name here. The"\
      "IconPath of Fvwm2 is searched for this icon."

  # Sizing
  CheckBox addnoSize -text "Ignore this button when calculating sizes" \
      -help "If you have a button that destroys you layout because it"\
      "gets just a pixel or two too big, you can make FvwmButtons"\
      "ignore this button when calculating the default size for"\
      "buttons."
  Label addsizeL -text "Specify minimum size in pixels" -help "Here you"\
      "can enter the minimal size (in pixel) you want this button to"\
      "have. This is very useful if you only have swallowed buttons,"\
      "since they are normally ignored when calculating the normal"\
      "button size."
  Int addsizeW -text Width -help "Here you can enter the width in"\
      "pixels."
  Int addsizeH -text Height -help "Here you can enter the height in"\
      "pixels."
  Frame addsizeF -entries { addsizeL addsizeW addsizeH }

  Header bH -text "Buttons" -help "Definition of individual buttons."

  Window addLnF -entries {
    addcolorF
    addfontF
    addnoFont
    addframe
    addpaddingF
    addnoSize
    addsizeF
  } -text "Additional Look-and-Feel Configuration" \
      -help "Configuration of colors, font, etc. of this button."

  Frame noFileF -entries {
    titleF
    swallowF
    actionF
    widthheightF
    addicon
    addLnF
  } -orient top

  Line linE
  ExtEntry buttons -label:relief raised -entries {
    index
    fileF
    linE
    noFileF
  } -orient top -count 1 -index index

  Change {
    if {$changeElm == "colorB"} {
      if {$colorB == 1} {Enable foreColor backColor
      } else {Disable foreColor backColor}
    }
    if {$changeElm == "fontB"} {
      if {$fontB == 1} {
	Enable font
	set noFont 0
      } else {Disable font}
    }
    if {$changeElm == "noFont"} {
      if {$noFont == 1} {
	set fontB 0
	Disable font
      }
    }
    if {$changeElm == "columns"} {if {$columns == "-"} {set columns ""}}
    if {$changeElm == "rows"} {if {$rows == "-"} {set rows ""}}
    if {$changeElm == "geomB"} {
      if {$geomB == 1} {Enable xgeom ygeom
      } else {Disable xgeom ygeom}
    }
    if {$changeElm == "xpadding"} {if {$xpadding == "-"} {set xpadding ""}}
    if {$changeElm == "ypadding"} {if {$ypadding == "-"} {set ypadding ""}}
    # Buttons
    if {$changeElm == "fileB"} {
      if {$fileB == 1} {
	Disable noFileF
	Enable frame_file
      } else {
	Disable frame_file
	Enable noFileF
	# Update all visual dependencies
	if {$swallowB == 0} {Disable swallowW
	} else {
	  if {$swallowProgB == 0} {Disable swallowProg}
	  if {$swallowModB  == 0} {Disable swallowMod pager1 pager2
	  } elseif {$swallowMod != "FvwmPager"} {Disable pager1 pager2}
	}
	if {$actionB == 0} {Disable actionHang builtin arguments}
	if {$addcolorB == 0} {Disable addforeColor addbackColor}
	if {$addfontB == 0} {Disable addfont}
      }
    }
    if {$changeElm == "swallowB"} {
      if {$swallowB == 1} {
	Enable swallowW
	if {$swallowProgB == 0} {Disable swallowProg}
	if {$swallowModB  == 0} {Disable swallowMod pager1 pager2
	} elseif {$swallowMod != "FvwmPager"} {Disable pager1 pager2}
      } else {
	Disable swallowW
      }
    }
    if {$changeElm == "pager1" && $pager1 == "-"} {set pager1 ""}
    if {$changeElm == "pager1" && $pager2 == "-"} {set pager2 ""}
    if {$changeElm == "swallowProgB"} {
      if {$swallowProgB == 1} {
	Enable swallowProg
	set swallowModB 0
	Disable swallowMod pager1 pager2
      } else {
	Disable swallowProg
      }
    }
    if {$changeElm == "swallowModB"} {
      if {$swallowModB == 1} {
	Enable swallowMod
	if {$swallowMod == "FvwmPager"} {Enable pager1 pager2}
	set swallowProgB 0
	Disable swallowProg
      } else {
	Disable swallowMod pager1 pager2
      }
    }
    if {$changeElm == "swallowMod"} {
      if {$swallowMod == "FvwmPager"} {
	Enable pager1 pager2
      } else {
	Disable pager1 pager2
      }
    }
    if {$changeElm == "width" && $width == "-"} {set width ""}
    if {$changeElm == "height" && $height == "-"} {set height ""}
    if {$changeElm == "actionB"} {
      if {$actionB == 1} {Enable actionHang builtin arguments
      } else {Disable actionHang builtin arguments}
    }
    if {$changeElm == "addcolorB"} {
      if {$addcolorB == 1} {Enable addforeColor addbackColor
      } else {Disable addforeColor addbackColor}
    }
    if {$changeElm == "addfontB"} {
      if {$addfontB == 1} {
	Enable addfont
	set addnoFont 0
      } else {Disable addfont}
    }
    if {$changeElm == "addnoFont"} {
      if {$addnoFont == 1} {
	set addfontB 0
	Disable addfont
      }
    }
    if {$changeElm == "addxpadding" && $addxpadding == "-"} {
      set addxpadding ""}
    if {$changeElm == "addypadding" && $addypadding == "-"} {
      set addypadding ""}
    if {$changeElm == "addsizeW" && $addsizeW == "-"} {set addsizeW ""}
    if {$changeElm == "addsizeH" && $addsizeH == "-"} {set addsizeH ""}
    # Update index and additionalW
    if {[lsearch {
      fileB file
      title addicon
      swallowB hangon
      } $changeElm] != -1} {
      if {$fileB == 1} {
	set index "File: $file"
      } elseif {$title != ""} {
	set index "Title: $title"
      } elseif {$addicon != ""} {
	set index "Icon: $addicon"
      } elseif {$swallowB == 1} {
	set index "Swallow: $hangon"
      } else {
	set index "Unspecified"
      }
    }
  }

  Save {
    if {$colorB == 1} {
      print "*FvwmButtonsFore\t[hash2rgb [lindex $foreColor 0]]"
      print "*FvwmButtonsBack\t[hash2rgb [lindex $backColor 0]]"
    }
    if {$fontB   == 1} {print "*FvwmButtonsFont\t$font"}
    if {$noFont  == 1} {print "*FvwmButtonsFont\tNone"}
    if {$columns != ""} {print "*FvwmButtonsColumns\t$columns"}
    if {$rows    != ""} {print "*FvwmButtonsRows\t$rows"}
    if {$frame   != "" && $frame != "-"} {print "*FvwmButtonsFrame\t$frame"}
    if {$xpadding != ""} {print "*FvwmButtonsPadding\t$xpadding $ypadding"}
    if {$geomB   == 1} {
      print "*FvwmButtonsGeometry\t[signed $xgeom][signed $ygeom]"
    }
    forevery buttons {
      if {$fileB == 1} {
	print "*FvwmButtonsFile\t$file"
      } else {
	set options {}
	if {$width != ""} {append options "${width}x$height,"}
	if {$title != "" || $titleJust(name) != "Centered"} {
	  append options "Title"
	  if {$titleJust(name) != "Centered"} {
	    switch $titleJust(name) \
		"Left justified"     {append options "(Left)"} \
		"Right justified"    {append options "(Right)"} \
		"Right side of icon" {append options "(Side)"}
	  }
	  if {$title != ""} {append options " \"$title\""}
	  append options ","
	}
	if {$swallowB == 1} {
	  append options "Swallow"
	  set swalOpts ""
	  if {$swallowclose(index)    == 1} {append swalOpts ",NoClose"}
	  if {$swallowkill(index)     == 1} {append swalOpts ",Kill"}
	  if {$swallowhint(index)     == 1} {append swalOpts ",Hints"}
	  if {$swallowrespawn(index)  == 1} {append swalOpts ",Respawn"}
	  if {$swallowuseold(index)   == 1} {append swalOpts ",UseOld"}
	  if {$swallowusetitle(index) == 1} {append swalOpts ",UseTitle"}
	  if {$swalOpts != ""} {
	    append options "([string range $swalOpts 1 end])"
	  }
	  append options " \"$hangon\" "
	  if {$swallowProgB == 1} {
	    append options "`Exec [makeBg $swallowProg]`,"
	  } else {
	    append options "`Module $swallowMod"
	    if {$swallowMod == "FvwmPager"} {append options " $pager1 $pager2"}
	    append options "`,"
	  }
	}
	if {$actionB == 1} {
	  append options "Action `$builtin"
	  if {$actionHang != ""} {append options " \"$actionHang\""}
	  if {$arguments != ""} {
	    if {$builtin == "Exec"} {append options " [makeBg $arguments]"
	    } else {append options " $arguments"}
	  }
	  append options "`,"
	}
	if {$addcolorB == 1} {
	  append options "Fore [hash2rgb [lindex $addforeColor 0]],\
 Back [hash2rgb [lindex $addbackColor 0]],"
	}
	if {$addfontB == 1} {append options "Font $addfont,"}
	if {$addnoFont == 1} {append options "Font None,"}
	if {$addframe != ""} {append options "Frame $addframe,"}
	if {$addxpadding != ""} {
	  append options "Padding $addxpadding $addypadding,"
	}
	if {$addicon != ""} {append options "Icon $addicon,"}
	if {$addnoSize == 1} {append options "NoSize,"}
	if {$addsizeW != ""} {append options "Size $addsizeW $addsizeH,"}
	if {$options == ""} {
	  tk_dialog .fvwmbuttonsWarn Warning "You had a button in FvwmButtons\
 that contained no configurations. This button will not be generated." info\
	      0 OK
	} else {
	  # Strip last comma
	  set options \
	      [string range $options 0 [expr [string length $options] - 2]]
	  print "*FvwmButtons($options)"
	}
      }
    }
    if {$spawn   == 1} {
      print "AddToFunc InitFunction \"I\" Module FvwmButtons"
      print "AddToFunc RestartFunction \"I\" Module FvwmButtons"
    }
  }
}
