### Copyright (C) 1995 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 functions {} {
  # Menus and bindings / Function definitions
  Desc "Definitions of functions used for bindings and popup menus"
  ShortDesc "Function definitions"

  Entry functionName -text "Name of the function" -textafter "\t" \
      -help "To define a new function enter the name of the function here"

  Menu builtinCall -text "Builtin" -entryhelp {
    Undefined "This is no command, if it is selected the line is not saved" \
	Beep "Makes the terminal beep" \
	CirculateDown "Circulate 'down' to the next window" \
	CirculateUp "Circulate 'up' to the previous window" \
	Close "Close the current window" \
	CursorMove "Move the cursor" \
	Delete "Delete the current window" \
	Desk "Change to another desktop" \
	Destroy "Remove the current window in a sure but violent way" \
	Exec "Execute an external command" \
	Focus "Gives focus to the current window" \
	Function "Call a previously defined function" \
	GotoPage "Move the desktop viewport to another page" \
	Iconify "(De)Iconify the current window" \
	Lower "Lower the current window" \
	Maximize "(Un)Maximize the current window" \
	Module "Spawn an external Fvwm module" \
	Move "Move the current window" \
	Nop "Do nothing - used for separators in menus" \
	Popup "Call a previously defined popup menu" \
	Quit "Quit Fvwm (generally causing X to exit as well)" \
	Raise "Raise the current window" \
	RaiseLower "Alternately raise and lower the current window" \
	Refresh "Redraw all windows on the screen" \
	Resize "Resize the current window" \
	Restart "Restart fvwm (or another window manager)" \
	Stick "(Un)Stick the current window" \
	Scroll "Scroll the desktops viewport" \
	Title "Do nothing - used for titles in menus" \
	TogglePage "Disables/reenables edge scrolling" \
	Wait "Wait for a window to come up" \
	Warp "Warp to a specified window" \
	WindowsDesk "Move the selected window to a new desktop" \
	WindowList "Pops up a menu of all windows" } \
      -help "Select a built-in function to call from this menu"
  
  Menu events -text "Event" -entryhelp {
    Immediate "Execute the command right away" \
	Motion "Execute the command on motion of the mouse" \
	Click "Execute the command on mouse click" \
	DoubleClick "Execute the command on mouse double-click" } \
      -help "Select an event to cause execution of the command"
  Entry arguments -text "Additional arguments" \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility"

  ExtEntry functionEntry -entries {builtinCall events arguments}
  ExtEntry function -lines 1 -entries {functionName functionEntry} \
      -orient top

  Save {
    forevery function {
      if {$functionName != {}} {
	print "Function \"$functionName\""
	forevery functionEntry {
	  if {$builtinCall(name) != "Undefined"} {
	    print "\t$builtinCall(name)\t\"$events(name)\"\t$arguments"
	  }
	}
	print "EndFunction"
      }
    }
  }
}

proc popupDefinitions {} {
  # Menus and bindings / Popup menu definitions
  Desc "Definition of popup menus"
  ShortDesc "Popup menus"

  Entry popupName -text "Name of the popup menu" -textafter "\t"

  Menu builtinCall -text "Builtin" -entryhelp {
    Undefined "This is no command, if it is selected the line is not saved" \
	Beep "Makes the terminal beep" \
	CirculateDown "Circulate 'down' to the next window" \
	CirculateUp "Circulate 'up' to the previous window" \
	Close "Close the current window" \
	CursorMove "Move the cursor" \
	Delete "Delete the current window" \
	Desk "Change to another desktop" \
	Destroy "Remove the current window in a sure but violent way" \
	Exec "Execute an external command" \
	Focus "Gives focus to the current window" \
	Function "Call a previously defined function" \
	GotoPage "Move the desktop viewport to another page" \
	Iconify "(De)Iconify the current window" \
	Lower "Lower the current window" \
	Maximize "(Un)Maximize the current window" \
	Module "Spawn an external Fvwm module" \
	Move "Move the current window" \
	Nop "Do nothing - used for separators in menus" \
	Popup "Call a previously defined popup menu" \
	Quit "Quit Fvwm (generally causing X to exit as well)" \
	Raise "Raise the current window" \
	RaiseLower "Alternately raise and lower the current window" \
	Refresh "Redraw all windows on the screen" \
	Resize "Resize the current window" \
	Restart "Restart fvwm (or another window manager)" \
	Stick "(Un)Stick the current window" \
	Scroll "Scroll the desktops viewport" \
	Title "Do nothing - used for titles in menus" \
	TogglePage "Disables/reenables edge scrolling" \
	Wait "Wait for a window to come up" \
	Warp "Warp to a specified window" \
	WindowsDesk "Move the selected window to a new desktop" \
	WindowList "Pops up a menu of all windows" } \
      -help "Select a built-in function to call from this menu"
  Entry name -text "Name" \
     -help "Enter the name on the menu here"
  Entry arguments -text "Additional arguments" \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility"

  ExtEntry popupEntry -entries {builtinCall name arguments}
  ExtEntry popup -lines 1 -entries {popupName popupEntry} \
      -orient top

  Save {
    forevery popup {
      if {$popupName != {}} {
	print "Popup \"$popupName\""
	forevery popupEntry {
	  if {$builtinCall(name) != "Undefined"} {
	    print "\t$builtinCall(name)\t\"$name\"\t$arguments"
	  }
	}
	print "EndPopup"
      }
    }
  }
}
  
proc keyBindings {} {
  # Menus and bindings / Key bindings
  Desc "Bindings for key events"
  ShortDesc "Key bindings"

  Entry keysymEntry -text "Keysym" -width 3 \
      -help "If the keysym isn't listed below, you can enter any valid" \
      "keysym here"
  ListBox keysymBox -entries {
    F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 L1 L2 L3 L4 L5 L6 L7 L8 L9 L10} \
      -width 8 -selectmode single \
      -help "Select a keysym from the list, if the desired keysym isn't" \
      "present you can enter it in the entrybox above"
  Frame keysym -entries {keysymEntry keysymBox} -orient top
  ListBox keyContext -text "Context" -selectmode multiple -entries {
    Any Root Window "Title bar" Sides Frame Icon "0 button" "1 button"\
	"2 button" "3 button" "4 button" "5 button" "6 button" "7 button" \
	"8 button" "9 button"} -width 8 \
      -help "Select the context(s) in which to bind the key"
  ListBox keyModifier -text "Modifier" -selectmode multiple -entries {
    None Control Shift Meta 1mod 2mod 3mod 4mod 5mod} -width 8 \
      -help "Select the modifier(s) to press in conjunction with the key"
  Label funcLabel -text "Function to call" \
      -help "This is a list of the built-in functions you can bind the key to"
  Menu builtinCall -entryhelp {
    Beep "Makes the terminal beep" \
	CirculateDown "Circulate 'down' to the next window" \
	CirculateUp "Circulate 'up' to the previous window" \
	Close "Close the current window" \
	CursorMove "Move the cursor" \
	Delete "Delete the current window" \
	Desk "Change to another desktop" \
	Destroy "Remove the current window in a sure but violent way" \
	Exec "Execute an external command" \
	Focus "Gives focus to the current window" \
	Function "Call a previously defined function" \
	GotoPage "Move the desktop viewport to another page" \
	Iconify "(De)Iconify the current window" \
	Lower "Lower the current window" \
	Maximize "(Un)Maximize the current window" \
	Module "Spawn an external Fvwm module" \
	Move "Move the current window" \
	Nop "Do nothing - used for separators in menus" \
	Popup "Call a previously defined popup menu" \
	Quit "Quit Fvwm (generally causing X to exit as well)" \
	Raise "Raise the current window" \
	RaiseLower "Alternately raise and lower the current window" \
	Refresh "Redraw all windows on the screen" \
	Resize "Resize the current window" \
	Restart "Restart fvwm (or another window manager)" \
	Stick "(Un)Stick the current window" \
	Scroll "Scroll the desktops viewport" \
	Title "Do nothing - used for titles in menus" \
	TogglePage "Disables/reenables edge scrolling" \
	Wait "Wait for a window to come up" \
	Warp "Warp to a specified window" \
	WindowsDesk "Move the selected window to a new desktop" \
	WindowList "Pops up a menu of all windows" } \
      -help "Select a built-in function to call from this menu"

  Label argLabel -text "Additional arguments" \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility"
  Entry keyArgs \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility"
  Frame called -entries {funcLabel builtinCall argLabel keyArgs} -orient top

  Frame keyFrame -entries {keysym keyContext keyModifier called}
  ExtEntry keys -lines 1 -entries {keyFrame} -orient top
  Line keyDelimiter

  Change {
    if {$changeElm == "keysymEntry"} {
      if {$keysymEntry != {}} {
	Disable keysymBox
      } else {
	Enable keysymBox
      }
    }
  }
  
  Save {
    forevery keys {
      if {$keysymEntry != {}} {
	set keysym $keysymEntry
      } else {
	set keysym $keysymBox(name)
      }
      if {$keysym!={} && $keyContext(name)!={} && $keyModifier(name)!={}} {
	set context [makeContextOrModifier $keyContext(name)]
	set modifier [makeContextOrModifier $keyModifier(name)]
	print "Key $keysym $context $modifier $builtinCall(name) $keyArgs"
      }
    }
  }
}

proc mouseBindings {} {
  # Menus and bindings / Mouse bindings
  Desc "Bindings for mouse events"
  ShortDesc "Mouse bindings"

  Label buttonLabel -text "Button no" \
      -help "Use this menu to select which mouse button to bind"
  Menu menuButton -entries {Any 1 2 3} \
      -help "Use this menu to select which mouse button to bind"
  Frame mouseButton -entries {buttonLabel menuButton} -orient top
  ListBox mouseContext -text "Context" -selectmode multiple -entries {
    Any Root Window "Title bar" Sides Frame Icon "0 button" "1 button"\
	"2 button" "3 button" "4 button" "5 button" "6 button" "7 button" \
	"8 button" "9 button"} -width 8 \
      -help "Select the context(s) in which to bind the mouse"
  ListBox mouseModifier -text "Modifier" -selectmode multiple -entries {
    None Control Shift Meta 1mod 2mod 3mod 4mod 5mod} -width 8 \
      -help "Select the modifier(s) to press in conjunction with the mouse"
  Label mouseFuncLabel -text "Function to call" \
      -help "This is a list of the built-in functions you can bind the key to"
  Menu mouseBuiltin -entryhelp {
    Beep "Makes the terminal beep" \
	CirculateDown "Circulate 'down' to the next window" \
	CirculateUp "Circulate 'up' to the previous window" \
	Close "Close the current window" \
	CursorMove "Move the cursor" \
	Delete "Delete the current window" \
	Desk "Change to another desktop" \
	Destroy "Remove the current window in a sure but violent way" \
	Exec "Execute an external command" \
	Focus "Gives focus to the current window" \
	Function "Call a previously defined function" \
	GotoPage "Move the desktop viewport to another page" \
	Iconify "(De)Iconify the current window" \
	Lower "Lower the current window" \
	Maximize "(Un)Maximize the current window" \
	Module "Spawn an external Fvwm module" \
	Move "Move the current window" \
	Nop "Do nothing - used for separators in menus" \
	Popup "Call a previously defined popup menu" \
	Quit "Quit Fvwm (generally causing X to exit as well)" \
	Raise "Raise the current window" \
	RaiseLower "Alternately raise and lower the current window" \
	Refresh "Redraw all windows on the screen" \
	Resize "Resize the current window" \
	Restart "Restart fvwm (or another window manager)" \
	Stick "(Un)Stick the current window" \
	Scroll "Scroll the desktops viewport" \
	Title "Do nothing - used for titles in menus" \
	TogglePage "Disables/reenables edge scrolling" \
	Wait "Wait for a window to come up" \
	Warp "Warp to a specified window" \
	WindowsDesk "Move the selected window to a new desktop" \
	WindowList "Pops up a menu of all windows" } \
      -help "Select a built-in function to call from this menu"

  Label mouseArgLabel -text "Additional arguments" \
      -help "Any additional arguments are entered here (the syntax is your" \
      "responsibility"
  Entry mouseArgs
  Frame mouseCalled -entries {mouseFuncLabel mouseBuiltin mouseArgLabel \
				  mouseArgs} -orient top

  Frame mouseEntry -entries {mouseButton mouseContext mouseModifier mouseCalled}
  ExtEntry mouse -lines 1 -entries {mouseEntry} -orient top

  Save {
    forevery mouse {
      if {$mouseContext(name)!={} && $mouseModifier(name)!={}} {
	set context [makeContextOrModifier $mouseContext(name)]
	set modifier [makeContextOrModifier $mouseModifier(name)]
	if {$menuButton(name) == "Any"} {
	  set button 0
	} else {
	  set button $menuButton(name)
	}
	print "Mouse $button $context $modifier $mouseBuiltin(name) $mouseArgs"
      }
    }
  }
}
