#!/bin/bash
# based on JWMKeyConfig by SFR
# Sigmund Berglund 2014,2015, GPLv2

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

#------------------------------------------------------------------------------

[ ! "`ps -A | grep jwm`" ] && /usr/lib/gtkdialog/box_ok JWM info "$(gettext 'You are currently NOT running the JWM window manager, and settings here will not work until JWM is active.')"

[ "$1" = "-mouse" ] && PAGE_NR=1 || PAGE_NR=0

TMPDIR=$HOME/.jwm/tmp/shortcuts
mkdir -p $TMPDIR

export CONFIG_FILE="$HOME/.jwm/jwmrc-personal"
export CONFIG_FILE_BAK="$TMPDIR/jwmrc-personal_backup_keyboard_shortcuts"
export TMP_CUTS="$TMPDIR/key_shortcuts"
export TMP_CONFIG_FILE="$TMPDIR/jwmrc-personal_temp"

trap 'rm -rf $TMPDIR' EXIT

export FAKE="`echo -e '\x00\xe2\x88\xa3'`"   # Divides (U+2223)

[ -e /usr/share/pixmaps/puppy/keyboard_shortcut.svg ] && PIC_PATH=/usr/share/pixmaps/puppy/keyboard_shortcut.svg || PIC_PATH=/usr/local/lib/X11/mini-icons/mini-keyboard.xpm

#------------------------------------------------------------------------------

func_modmap_list () {
  xmodmap -pk | grep -o "(.*)" | cut -f1 | grep -v "NoSymbol" | tail -n +3 | tr -d "()" | awk '!x[$0]++' 
}
export -f func_modmap_list

#------------------------------------------------------------------------------

func_get_keys() {
  echo -n > $TMP_CUTS
  CNT=1
  OIFS="$IFS"; IFS=$'\n'
  for i in `grep '^<Key .*>' "$CONFIG_FILE"`; do
    MASK="`echo $i | grep -o ' mask=".*' | cut -f2 -d '"'`"
    KEY="`echo $i | grep -o ' key=".*">' | cut -f2- -d '"'`"
    KEY="${KEY%??}"
    KEYCODE="`echo $i | grep -o ' keycode=".*' | cut -f2 -d '"'`"
    ACTION="`echo $i | rev | cut -f2- -d '<' | rev | cut -f2- -d '>'`"
    # if 'action' contains | we need to convert it or else <tree> would cut the line
    ACTION="${ACTION//|/$FAKE}"

    [ "$KEY" = "" ] && KEY="$KEYCODE"
    
    if [ "$MASK" != "" ]; then
      MASK="`echo $MASK | sed -e 's/.\{1\}/& /g' -e "s/A/Alt/" -e "s/C/Ctrl/" -e "s/S/Shift/" -e "s/1/Mod1/" -e "s/2/Mod2/" -e "s/3/Mod3/" -e "s/4/Mod4/" -e "s/5/Mod5/" | tr ' ' '+'`"
      MASK="${MASK%?}"
    fi
    
    echo "${CNT}|${MASK}|${KEY}|${ACTION}|${i}" >> $TMP_CUTS
    ((CNT++))
  done
  IFS="$OIFS"
}
export -f func_get_keys

#------------------------------------------------------------------------------

func_merge_line () {
  [ "`echo "$varNEW_KEY" | grep -E '\||>'`" != "" ] && { /usr/lib/gtkdialog/box_ok JWM error "$(gettext 'Those characters are not allowed within <b>key</b> field: | >')"; return 1; }
  LINE="<Key"
  
  MASK=""
  [ "$varNEW_A" = "true" ] && MASK="${MASK}A"
  [ "$varNEW_C" = "true" ] && MASK="${MASK}C"
  [ "$varNEW_S" = "true" ] && MASK="${MASK}S"
  [ "$varNEW_1" = "true" ] && MASK="${MASK}1"
  [ "$varNEW_2" = "true" ] && MASK="${MASK}2"
  [ "$varNEW_3" = "true" ] && MASK="${MASK}3"
  [ "$varNEW_4" = "true" ] && MASK="${MASK}4"
  [ "$varNEW_5" = "true" ] && MASK="${MASK}5"
  
  [ "$MASK" != "" ] && LINE="${LINE} mask=\"$MASK\""

  if [ "$varUSEKEY" = "true" ]; then
    varNEW_KEY="${varNEW_KEY// /}"		# no spaces allowed, trim if any
    [ ! "$varNEW_KEY" ] && { /usr/lib/gtkdialog/box_ok JWM error "$(gettext "<b>Key</b> field can not be empty!")"; return 1; }
    LINE="${LINE} key=\"$varNEW_KEY\""
  else
    LINE="${LINE} keycode=\"$varNEW_KEYCODE\""
  fi
}
export -f func_merge_line

#------------------------------------------------------------------------------

func_add () {
  func_merge_line || return 1
  grep -q -F "${LINE}" "$CONFIG_FILE" && { /usr/lib/gtkdialog/box_ok JWM error "$(gettext 'Shortcut already exists!')"; return 1; }
  LINE="${LINE}>${varNEW_ACTION//$FAKE/|}</Key>"

  if [ "$varSHORTCUT" = "" ]; then
    LAST_LINE="<!-- Key bindings -->"
  else
    LAST_LINE="`grep . "$TMP_CUTS" | tail -n 1 | cut -f5- -d '|'`"
  fi
  
  LINE_NO="`grep -m1 -n -F "$LAST_LINE" "$CONFIG_FILE" | cut -f1 -d ':'`"
  head -n $LINE_NO "$CONFIG_FILE" > "$TMP_CONFIG_FILE"
  echo "$LINE" >> "$TMP_CONFIG_FILE"
  tail -n +$((LINE_NO+1)) "$CONFIG_FILE" >> "$TMP_CONFIG_FILE"
  mv -f "$TMP_CONFIG_FILE" "$CONFIG_FILE"
}
export -f func_add

#------------------------------------------------------------------------------

func_edit () {
  OLD_LINE="`grep "^${varSHORTCUT}|" "$TMP_CUTS" | cut -f5- -d '|'`"
  
  func_merge_line || return 1
  LINE="${LINE}>${varNEW_ACTION//$FAKE/|}</Key>"
  
  # sed is useless when there are some special chars, let's use foolproof head/tail pair...
  LINE_NO="`grep -m1 -F -n "$OLD_LINE" "$CONFIG_FILE" | cut -f1 -d ':'`"
  head -n $((LINE_NO-1)) "$CONFIG_FILE" > "$TMP_CONFIG_FILE"
  echo "$LINE" >> "$TMP_CONFIG_FILE"
  tail -n +$((LINE_NO+1)) "$CONFIG_FILE" >> "$TMP_CONFIG_FILE"
  mv -f "$TMP_CONFIG_FILE" "$CONFIG_FILE"
}
export -f func_edit

#------------------------------------------------------------------------------

func_delete () {
  . /usr/lib/gtkdialog/box_yesno "$(gettext 'Delete JWM shortcut')" "$(gettext 'Are you sure you want to delete the selected shortcut?')"
  [ $EXIT != yes ] && return 1
  # last field contains original line from jwmrc-personal
  LINE="`sed -n "${varSHORTCUT}p" "$TMP_CUTS" | cut -f5- -d '|'`"
  grep -F -v "$LINE" "$CONFIG_FILE" > "$TMP_CONFIG_FILE"
  mv -f "$TMP_CONFIG_FILE" "$CONFIG_FILE"
}
export -f func_delete

#------------------------------------------------------------------------------

func_apply_mouseactions () {
	#activate all
	sed -i 's|^<!--<RootMenu label="1" labeled="false" height="MENHEIGHT" onroot="1"><Include>exec:$HOME/.jwm/root_menus/menu1</Include></RootMenu>-->|<RootMenu label="1" labeled="false" height="MENHEIGHT" onroot="1"><Include>exec:$HOME/.jwm/root_menus/menu1</Include></RootMenu>|' /etc/xdg/templates/_root_.jwmrc
	sed -i 's|<!--<RootMenu label="2" labeled="false" height="MENHEIGHT" onroot="2"><Include>exec:$HOME/.jwm/root_menus/menu2</Include></RootMenu>-->|<RootMenu label="2" labeled="false" height="MENHEIGHT" onroot="2"><Include>exec:$HOME/.jwm/root_menus/menu2</Include></RootMenu>|' /etc/xdg/templates/_root_.jwmrc
	sed -i 's|<!--<RootMenu label="3" labeled="false" height="MENHEIGHT" onroot="3"><Include>exec:$HOME/.jwm/root_menus/menu3</Include></RootMenu>-->|<RootMenu label="3" labeled="false" height="MENHEIGHT" onroot="3"><Include>exec:$HOME/.jwm/root_menus/menu3</Include></RootMenu>|' /etc/xdg/templates/_root_.jwmrc
	sed -i 's|<!--<RootMenu label="4" labeled="false" height="MENHEIGHT" onroot="4"><Include>exec:$HOME/.jwm/root_menus/menu4</Include></RootMenu>-->|<RootMenu label="4" labeled="false" height="MENHEIGHT" onroot="4"><Include>exec:$HOME/.jwm/root_menus/menu4</Include></RootMenu>|' /etc/xdg/templates/_root_.jwmrc
	sed -i 's|<!--<RootMenu label="5" labeled="false" height="MENHEIGHT" onroot="5"><Include>exec:$HOME/.jwm/root_menus/menu5</Include></RootMenu>-->|<RootMenu label="5" labeled="false" height="MENHEIGHT" onroot="5"><Include>exec:$HOME/.jwm/root_menus/menu5</Include></RootMenu>|' /etc/xdg/templates/_root_.jwmrc

	if [ "$ROOT1" != "$ROOT_ORG1" ]; then
		case $ROOT1 in
			"$(gettext 'Bookmarks menu')") ln -sf /usr/local/jwm_config/menu_build_bookmarks $HOME/.jwm/root_menus/menu1;;
			"$(gettext 'Main menu')") ln -sf /usr/local/jwm_config/menu_build_main $HOME/.jwm/root_menus/menu1;;
			"$(gettext 'Clock menu')") ln -sf /usr/local/jwm_config/menu_build_clock $HOME/.jwm/root_menus/menu1;;
			"$(gettext 'Places menu')") ln -sf /usr/local/jwm_config/menu_build_places $HOME/.jwm/root_menus/menu1;;
			"$(gettext 'Mouse-action notice')") ln -sf /usr/local/jwm_config/menu_build_mouseaction $HOME/.jwm/root_menus/menu1;;
			*) sed -i 's|<RootMenu label="1" labeled="false" height="MENHEIGHT" onroot="1"><Include>exec:$HOME/.jwm/root_menus/menu1</Include></RootMenu>|<!--<RootMenu label="1" labeled="false" height="MENHEIGHT" onroot="1"><Include>exec:$HOME/.jwm/root_menus/menu1</Include></RootMenu>-->|' /etc/xdg/templates/_root_.jwmrc
			rm $HOME/.jwm/root_menus/menu1 2> /dev/null;;
		esac
	fi
	if [ "$ROOT2" != "$ROOT_ORG2" ]; then
		case $ROOT2 in
			"$(gettext 'Bookmarks menu')") ln -sf /usr/local/jwm_config/menu_build_bookmarks $HOME/.jwm/root_menus/menu2;;
			"$(gettext 'Main menu')") ln -sf /usr/local/jwm_config/menu_build_main $HOME/.jwm/root_menus/menu2;;
			"$(gettext 'Clock menu')") ln -sf /usr/local/jwm_config/menu_build_clock $HOME/.jwm/root_menus/menu2;;
			"$(gettext 'Places menu')") ln -sf /usr/local/jwm_config/menu_build_places $HOME/.jwm/root_menus/menu2;;
			"$(gettext 'Mouse-action notice')") ln -sf /usr/local/jwm_config/menu_build_mouseaction $HOME/.jwm/root_menus/menu2;;
			*) sed -i 's|<RootMenu label="2" labeled="false" height="MENHEIGHT" onroot="2"><Include>exec:$HOME/.jwm/root_menus/menu2</Include></RootMenu>|<!--<RootMenu label="2" labeled="false" height="MENHEIGHT" onroot="2"><Include>exec:$HOME/.jwm/root_menus/menu2</Include></RootMenu>-->|' /etc/xdg/templates/_root_.jwmrc
			rm $HOME/.jwm/root_menus/menu2 2> /dev/null;;
		esac
	fi
	if [ "$ROOT3" != "$ROOT_ORG3" ]; then
		case $ROOT3 in
			"$(gettext 'Bookmarks menu')") ln -sf /usr/local/jwm_config/menu_build_bookmarks $HOME/.jwm/root_menus/menu3;;
			"$(gettext 'Main menu')") ln -sf /usr/local/jwm_config/menu_build_main $HOME/.jwm/root_menus/menu3;;
			"$(gettext 'Clock menu')") ln -sf /usr/local/jwm_config/menu_build_clock $HOME/.jwm/root_menus/menu3;;
			"$(gettext 'Places menu')") ln -sf /usr/local/jwm_config/menu_build_places $HOME/.jwm/root_menus/menu3;;
			"$(gettext 'Mouse-action notice')") ln -sf /usr/local/jwm_config/menu_build_mouseaction $HOME/.jwm/root_menus/menu3;;
			*) sed -i 's|<RootMenu label="3" labeled="false" height="MENHEIGHT" onroot="3"><Include>exec:$HOME/.jwm/root_menus/menu3</Include></RootMenu>|<!--<RootMenu label="3" labeled="false" height="MENHEIGHT" onroot="3"><Include>exec:$HOME/.jwm/root_menus/menu3</Include></RootMenu>-->|' /etc/xdg/templates/_root_.jwmrc
			rm $HOME/.jwm/root_menus/menu3 2> /dev/null;;
		esac
	fi
	if [ "$ROOT4" != "$ROOT_ORG4" ]; then
		sed -i 's|<!--<RootMenu label="4" labeled="false" height="MENHEIGHT" onroot="4"><Include>exec:$HOME/.jwm/root_menus/menu4</Include></RootMenu>-->|<RootMenu label="4" labeled="false" height="MENHEIGHT" onroot="4"><Include>exec:$HOME/.jwm/root_menus/menu4</Include></RootMenu>|' /etc/xdg/templates/_root_.jwmrc
		case $ROOT4 in
			"$(gettext 'Bookmarks menu')") ln -sf /usr/local/jwm_config/menu_build_bookmarks $HOME/.jwm/root_menus/menu4;;
			"$(gettext 'Main menu')") ln -sf /usr/local/jwm_config/menu_build_main $HOME/.jwm/root_menus/menu4;;
			"$(gettext 'Clock menu')") ln -sf /usr/local/jwm_config/menu_build_clock $HOME/.jwm/root_menus/menu4;;
			"$(gettext 'Places menu')") ln -sf /usr/local/jwm_config/menu_build_places $HOME/.jwm/root_menus/menu4;;
			"$(gettext 'Change desktop')")
				sed -i 's|<RootMenu label="4" labeled="false" height="MENHEIGHT" onroot="4"><Include>exec:$HOME/.jwm/root_menus/menu4</Include></RootMenu>|<!--<RootMenu label="4" labeled="false" height="MENHEIGHT" onroot="4"><Include>exec:$HOME/.jwm/root_menus/menu4</Include></RootMenu>-->|' /etc/xdg/templates/_root_.jwmrc
				rm $HOME/.jwm/root_menus/menu4 2> /dev/null;;
			"$(gettext 'Mouse-action notice')") ln -sf /usr/local/jwm_config/menu_build_mouseaction $HOME/.jwm/root_menus/menu4;;
			*)
				ln -sf /usr/local/jwm_config/menu_build_none $HOME/.jwm/root_menus/menu4;;
		esac
	fi
	if [ "$ROOT5" != "$ROOT_ORG5" ]; then
		sed -i 's|<!--<RootMenu label="5" labeled="false" height="MENHEIGHT" onroot="5"><Include>exec:$HOME/.jwm/root_menus/menu5</Include></RootMenu>-->|<RootMenu label="5" labeled="false" height="MENHEIGHT" onroot="5"><Include>exec:$HOME/.jwm/root_menus/menu5</Include></RootMenu>|' /etc/xdg/templates/_root_.jwmrc
		case $ROOT5 in
			"$(gettext 'Bookmarks menu')") ln -sf /usr/local/jwm_config/menu_build_bookmarks $HOME/.jwm/root_menus/menu5;;
			"$(gettext 'Main menu')") ln -sf /usr/local/jwm_config/menu_build_main $HOME/.jwm/root_menus/menu5;;
			"$(gettext 'Clock menu')") ln -sf /usr/local/jwm_config/menu_build_clock $HOME/.jwm/root_menus/menu5;;
			"$(gettext 'Places menu')") ln -sf /usr/local/jwm_config/menu_build_places $HOME/.jwm/root_menus/menu5;;
			"$(gettext 'Change desktop')")
				sed -i 's|<RootMenu label="5" labeled="false" height="MENHEIGHT" onroot="5"><Include>exec:$HOME/.jwm/root_menus/menu5</Include></RootMenu>|<!--<RootMenu label="5" labeled="false" height="MENHEIGHT" onroot="5"><Include>exec:$HOME/.jwm/root_menus/menu5</Include></RootMenu>-->|' /etc/xdg/templates/_root_.jwmrc
				rm $HOME/.jwm/root_menus/menu5 2> /dev/null;;
			"$(gettext 'Mouse-action notice')") ln -sf /usr/local/jwm_config/menu_build_mouseaction $HOME/.jwm/root_menus/menu5;;
			*) ln -sf /usr/local/jwm_config/menu_build_none $HOME/.jwm/root_menus/menu5;;
		esac
	fi
	/usr/lib/gtkdialog/box_splash -close never -fontsize large -icon_width 60 -icon /usr/share/pixmaps/puppy/mouse.svg -text "$(gettext 'Updating settings... please wait')" &
	XPID=$!
	fixmenus
	kill $XPID

	/usr/local/jwm_config/jwm_restart

}
export -f func_apply_mouseactions

#==============================================================================

export GUI_ADD='
<window title="'$(gettext "Add new shortcut")'" modal="true" image-name="'${PIC_PATH}'">
  <vbox>

    <frame '$(gettext "Key/Keycode")'>
      <hbox>
        <radiobutton space-fill="false" space-expand="false">
          <variable>varUSEKEY</variable>
          <default>true</default>
          <label>'$(gettext "Key:")' </label>
          <action>if true enable:varNEW_KEY</action>
          <action>if false disable:varNEW_KEY</action>
        </radiobutton>
      
        <comboboxentry>
          <variable>varNEW_KEY</variable>
          <input>func_modmap_list</input>
        </comboboxentry>
        
        <text space-fill="true" space-expand="true"><label>" "</label></text>
        
        <radiobutton>
          <variable>varUSEKEYCODE</variable>
          <default>false</default>
          <label>'$(gettext "Keycode:")'</label>
          <action>if true enable:varNEW_KEYCODE</action>
          <action>if false disable:varNEW_KEYCODE</action>
        </radiobutton>
        <spinbutton range-min="0" range-max="255" range-step="1" range-value="0">
          <variable>varNEW_KEYCODE</variable>
          <sensitive>false</sensitive>
        </spinbutton>
      
      </hbox>
    </frame>
    
    <frame '$(gettext "Mask")'>
      <hbox homogeneous="true">
        <checkbox>
          <variable>varNEW_A</variable>
          <label>Alt</label>
        </checkbox>
        <checkbox>
          <variable>varNEW_C</variable>
          <label>Ctrl</label>
        </checkbox>
        <checkbox>
          <variable>varNEW_S</variable>
          <label>Shift</label>
        </checkbox>
        <checkbox>
          <variable>varNEW_1</variable>
          <label>Mod1</label>
        </checkbox>
        <checkbox>
          <variable>varNEW_2</variable>
          <label>Mod2</label>
        </checkbox>
        <checkbox>
          <variable>varNEW_3</variable>
          <label>Mod3</label>
        </checkbox>
        <checkbox>
          <variable>varNEW_4</variable>
          <label>Mod4</label>
        </checkbox>
        <checkbox>
          <variable>varNEW_5</variable>
          <label>Mod5</label>
        </checkbox>
      </hbox>
    </frame>
    
    <frame '$(gettext "Action")'>
      <hbox>
        <entry>
          <variable>varNEW_ACTION</variable>
        </entry>
      </hbox>
    </frame>

    <hbox>
      <button>
        '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
        <label>'$(gettext "Cancel")'</label>
        <action>grabfocus:varSHORTCUT</action>
        <action>closewindow:ADD_WINDOW</action>
      </button>
      <button>
        <input file icon="gtk-apply"></input>
        <label>'$(gettext "Apply")'</label>
        <action>func_add</action>
        <action>func_get_keys</action>
        <action>refresh:varSHORTCUT</action>
        <action>grabfocus:varSHORTCUT</action>
      </button>
    </hbox>
    
    <variable>ADD_WINDOW</variable>
  </vbox>
</window>'

#------------------------------------------------------------------------------

export GUI_EDIT='
<window title="'$(gettext "Edit existing shortcut")'" modal="true" image-name="'${PIC_PATH}'">
  <vbox>

    <frame '$(gettext "Key/Keycode")'>
      <hbox>
        <radiobutton space-fill="false" space-expand="false">
          <variable>varUSEKEY</variable>
          <input>[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f5 -d "|" | grep "key="`" != "" ] && echo true || echo false</input>
          <label>'$(gettext "Key:")' </label>
          <action signal="show">if true disable:varNEW_KEYCODE</action>
          <action>if true enable:varNEW_KEY</action>
          <action>if false disable:varNEW_KEY</action>
        </radiobutton>

        <comboboxentry active="0">
          <variable>varNEW_KEY</variable>
          <input>[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f5 -d "|" | grep "key="`" != "" ] && grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f3 -d "|"; func_modmap_list</input>
        </comboboxentry>
        
        <text space-fill="true" space-expand="true"><label>" "</label></text>
        
        <radiobutton>
          <variable>varUSEKEYCODE</variable>
          <input>[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f5 -d "|" | grep "keycode="`" != "" ] && echo true || echo false</input>
          <label>'$(gettext "Keycode:")' </label>
          <action>if true enable:varNEW_KEYCODE</action>
          <action>if false disable:varNEW_KEYCODE</action>
        </radiobutton>
        
        <spinbutton range-min="0" range-max="255" range-step="1" range-value="0">
          <variable>varNEW_KEYCODE</variable>
          <input>[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f5 -d "|" | grep "keycode="`" != "" ] && grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f3 -d "|"</input>
        </spinbutton>
      
      </hbox>
    </frame>

    <frame '$(gettext "Mask")'>
      <hbox homogeneous="true">
        <checkbox>
          <variable>varNEW_A</variable>
          <label>Alt</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Alt"* ]] && echo true</input>
        </checkbox>
        <checkbox>
          <variable>varNEW_C</variable>
          <label>Ctrl</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Ctrl"* ]] && echo true</input>
        </checkbox>
        <checkbox>
          <variable>varNEW_S</variable>
          <label>Shift</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Shift"* ]] && echo true</input>
        </checkbox>
        <checkbox>
          <variable>varNEW_1</variable>
          <label>Mod1</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Mod1"* ]] && echo true</input>
        </checkbox>
        <checkbox>
          <variable>varNEW_2</variable>
          <label>Mod2</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Mod2"* ]] && echo true</input>
        </checkbox>
        <checkbox>
          <variable>varNEW_3</variable>
          <label>Mod3</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Mod3"* ]] && echo true</input>
        </checkbox>
        <checkbox>
          <variable>varNEW_4</variable>
          <label>Mod4</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Mod4"* ]] && echo true</input>
        </checkbox>
        <checkbox>
          <variable>varNEW_5</variable>
          <label>Mod5</label>
          <input>[[ "`grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f2 -d "|"`" == *"Mod5"* ]] && echo true</input>
        </checkbox>
      </hbox>
    </frame>

    <frame '$(gettext "Action")'>
      <hbox>
        <entry>
          <variable>varNEW_ACTION</variable>
          <input>grep "^${varSHORTCUT}|" '${TMP_CUTS}' | cut -f4 -d "|"</input>
        </entry>
      </hbox>
    </frame>

    <hbox>
      <button>
        '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
        <label>'$(gettext "Cancel")'</label>
        <action>grabfocus:varSHORTCUT</action>
        <action>closewindow:EDIT_WINDOW</action>
      </button>
      <button>
        <input file icon="gtk-apply"></input>
        <label>'$(gettext "Apply")'</label>
        <action>func_edit</action>
        <action>func_get_keys</action>
        <action>refresh:varSHORTCUT</action>
        <action>grabfocus:varSHORTCUT</action>
        <action>closewindow:EDIT_WINDOW</action>
      </button>
    </hbox>
    
    <variable>EDIT_WINDOW</variable>
  </vbox>
</window>'

#------------------------------------------------------------------------------


TMP="`readlink $HOME/.jwm/root_menus/menu1`"
case $TMP in
	*bookmarks*) export ROOT_ORG1="$(gettext 'Bookmarks menu')";;
	*main*) export ROOT_ORG1="$(gettext 'Main menu')";;
	*places*) export ROOT_ORG1="$(gettext 'Places menu')";;
	*clock*) export ROOT_ORG1="$(gettext 'Clock menu')";;
	*mouseaction*) export ROOT_ORG1="$(gettext 'Mouse-action notice')";;
	*) export ROOT_ORG1=" ";;
esac
TMP="`readlink $HOME/.jwm/root_menus/menu2`"
case $TMP in
	*bookmarks*) export ROOT_ORG2="$(gettext 'Bookmarks menu')";;
	*main*) export ROOT_ORG2="$(gettext 'Main menu')";;
	*places*) export ROOT_ORG2="$(gettext 'Places menu')";;
	*clock*) export ROOT_ORG2="$(gettext 'Clock menu')";;
	*mouseaction*) export ROOT_ORG2="$(gettext 'Mouse-action notice')";;
	*) export ROOT_ORG2=" ";;
esac
TMP="`readlink $HOME/.jwm/root_menus/menu3`"
case $TMP in
	*bookmarks*) export ROOT_ORG3="$(gettext 'Bookmarks menu')";;
	*main*) export ROOT_ORG3="$(gettext 'Main menu')";;
	*places*) export ROOT_ORG3="$(gettext 'Places menu')";;
	*clock*) export ROOT_ORG3="$(gettext 'Clock menu')";;
	*mouseaction*) export ROOT_ORG3="$(gettext 'Mouse-action notice')";;
	*) export ROOT_ORG3=" ";;
esac
TMP="`readlink $HOME/.jwm/root_menus/menu4`"
if [ ! "$TMP" ]; then
	export ROOT_ORG4="$(gettext 'Change desktop')"
else
	case $TMP in
		*bookmarks*) export ROOT_ORG4="$(gettext 'Bookmarks menu')";;
		*main*) export ROOT_ORG4="$(gettext 'Main menu')";;
		*places*) export ROOT_ORG4="$(gettext 'Places menu')";;
		*clock*) export ROOT_ORG4="$(gettext 'Clock menu')";;
		*mouseaction*) export ROOT_ORG4="$(gettext 'Mouse-action notice')";;
		*) export ROOT_ORG4=" ";;
	esac
fi
TMP="`readlink $HOME/.jwm/root_menus/menu5`"
if [ ! "$TMP" ]; then
	export ROOT_ORG5="$(gettext 'Change desktop')"
else
	case $TMP in
		*bookmarks*) export ROOT_ORG5="$(gettext 'Bookmarks menu')";;
		*main*) export ROOT_ORG5="$(gettext 'Main menu')";;
		*places*) export ROOT_ORG5="$(gettext 'Places menu')";;
		*clock*) export ROOT_ORG5="$(gettext 'Clock menu')";;
		*mouseaction*) export ROOT_ORG5="$(gettext 'Mouse-action notice')";;
		*) export ROOT_ORG5=" ";;
	esac
fi

export JWM_hotkeys='
<window height-request="400" title="'$(gettext "JWM shortcuts")'" image-name="'${PIC_PATH}'">
<vbox space-expand="true" space-fill="true">
  <notebook page="'$PAGE_NR'" labels="'$(gettext 'Keyboard shortcuts')'|'$(gettext 'Mouse actions')'">
    <vbox>
      '"`/usr/lib/gtkdialog/xml_info fixed keyboard_shortcut.svg 60 "$(gettext '<b>You can configure your keyboard shortcuts here.</b>')" "$(gettext 'Note: your changes will take effect after you close this window.')" "$(gettext 'You are currently running the JWM window manager')"`"'        

      <hbox space-expand="false" space-fill="false">
        <button>
          <label>'$(gettext "Add new shortcut")'</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>launch:GUI_ADD</action>
        </button>
        <button>
          <variable>varEDIT_STATUS</variable>
          <sensitive>false</sensitive>
          <label>'$(gettext "Edit selected")'</label>
          '"`/usr/lib/gtkdialog/xml_button-icon edit`"'
          <action>launch:GUI_EDIT</action>
        </button>
        <button>
          <variable>varDELETE_STATUS</variable>
          <sensitive>false</sensitive>
          <label>'$(gettext "Delete selected")'</label>
          '"`/usr/lib/gtkdialog/xml_button-icon delete`"'
          <action>func_delete</action>
          <action>func_get_keys</action>
          <action>refresh:varSHORTCUT</action>
          <action>grabfocus:varSHORTCUT</action>
        </button>
      </hbox>
      <vbox space-expand="true" space-fill="true">
        <tree rules-hint="true" column-visible="false|true|true|true" exported-column="0" file-monitor="true" auto-refresh="true" space-expand="true" space-fill="true">
          <variable>varSHORTCUT</variable>
          <label>No.|Mask|Key/Keycode|Action</label>
          <input file>'${TMP_CUTS}'</input>
          <action>launch:GUI_EDIT</action>
          <action>func_get_keys</action>
          <action>refresh:varSHORTCUT</action>
          <action signal="changed">enable:varEDIT_STATUS</action>
          <action signal="changed">enable:varDELETE_STATUS</action>
          <action signal="focus-in-event" condition="command_is_true([ ! $varSHORTCUT ] && echo true)">disable:varEDIT_STATUS</action>
          <action signal="focus-in-event" condition="command_is_true([ ! $varSHORTCUT ] && echo true)">disable:varDELETE_STATUS</action>
          <action signal="focus-in-event" condition="command_is_true([ $varSHORTCUT ] && echo true)">enable:varEDIT_STATUS</action>
          <action signal="focus-in-event" condition="command_is_true([ $varSHORTCUT ] && echo true)">enable:varDELETE_STATUS</action>
        </tree>
      </vbox>
    </vbox>
    <vbox>
      '"`/usr/lib/gtkdialog/xml_info fixed mouse.svg 60 "$(gettext 'Define <b>mouse actions</b> when clicking on the desktop.')"`"'
      <vbox margin="10">
        <hbox>
          <text use-markup="true"><label>"'$(gettext 'Mouse <b>left</b> click')'"</label></text>
          <comboboxtext>
            <variable>ROOT1</variable>
            <default>"'$ROOT_ORG1'"</default>
            <item>" "</item>
            <item>'$(gettext 'Main menu')'</item>
            <item>'$(gettext 'Bookmarks menu')'</item>
            <item>'$(gettext 'Places menu')'</item>
            <item>'$(gettext 'Clock menu')'</item>
            <item>'$(gettext 'Mouse-action notice')'</item>
          </comboboxtext>
        </hbox>
        <hbox>
          <text use-markup="true"><label>"'$(gettext 'Mouse <b>middle</b> click')'"</label></text>
          <comboboxtext>
            <variable>ROOT2</variable>
            <default>"'$ROOT_ORG2'"</default>
            <item>" "</item>
            <item>'$(gettext 'Main menu')'</item>
            <item>'$(gettext 'Bookmarks menu')'</item>
            <item>'$(gettext 'Places menu')'</item>
            <item>'$(gettext 'Clock menu')'</item>
            <item>'$(gettext 'Mouse-action notice')'</item>
          </comboboxtext>
        </hbox>
        <hbox>
          <text use-markup="true"><label>"'$(gettext 'Mouse <b>right</b> click')'"</label></text>
          <comboboxtext>
            <variable>ROOT3</variable>
            <default>"'$ROOT_ORG3'"</default>
            <item>" "</item>
            <item>'$(gettext 'Main menu')'</item>
            <item>'$(gettext 'Bookmarks menu')'</item>
            <item>'$(gettext 'Places menu')'</item>
            <item>'$(gettext 'Clock menu')'</item>
            <item>'$(gettext 'Mouse-action notice')'</item>
          </comboboxtext>
        </hbox>
        <hbox>
          <text use-markup="true"><label>"'$(gettext 'Mouse scroll <b>up</b>')'"</label></text>
          <comboboxtext>
            <variable>ROOT4</variable>
            <default>"'$ROOT_ORG4'"</default>
            <item>" "</item>
            <item>'$(gettext 'Main menu')'</item>
            <item>'$(gettext 'Bookmarks menu')'</item>
            <item>'$(gettext 'Places menu')'</item>
            <item>'$(gettext 'Clock menu')'</item>
            <item>'$(gettext 'Change desktop')'</item>
            <item>'$(gettext 'Mouse-action notice')'</item>
          </comboboxtext>
        </hbox>
        <hbox>
          <text use-markup="true"><label>"'$(gettext 'Mouse scroll <b>down</b>')'"</label></text>
          <comboboxtext>
            <variable>ROOT5</variable>
            <default>"'$ROOT_ORG5'"</default>
            <item>" "</item>
            <item>'$(gettext 'Main menu')'</item>
            <item>'$(gettext 'Bookmarks menu')'</item>
            <item>'$(gettext 'Places menu')'</item>
            <item>'$(gettext 'Clock menu')'</item>
            <item>'$(gettext 'Change desktop')'</item>
            <item>'$(gettext 'Mouse-action notice')'</item>
          </comboboxtext>
        </hbox>
      </vbox>
    </vbox>
  </notebook>
      
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Help")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon help`"'
      <action>cat /usr/local/jwm_config/keyConfigHelp > /tmp/box_help; /usr/lib/gtkdialog/box_help "'$(gettext "Keyboard shortcuts")'" &</action>
    </button>
    <text space-fill="true" space-expand="true"><label>""</label></text>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>/usr/local/jwm_config/jwm_restart -backup -no_restart</action>
      <action>exit:EXIT</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>func_apply_mouseactions</action>
      <action>exit:EXIT</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
</vbox>
</window>'

#==============================================================================
# START
#==============================================================================

[ -e "$CONFIG_FILE" ] || { /usr/lib/gtkdialog/box_ok JWM error "$(gettext "Configuration file doesn't exist! Aborting...")"; exit 1; }
func_get_keys &

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -p JWM_hotkeys
