#!/bin/bash
#JWM Tray Configuration
#Copyright 2008,2009,2010,2014,2015 GPL
#Sigmund Berglund

export TEXTDOMAIN=jwm
export OUTPUT_CHARSET=UTF-8

/usr/lib/gtkdialog/box_splash -close never -fontsize large -icon_width 60 -icon /usr/share/pixmaps/puppy/puppy_config.svg -text " $(gettext 'Loading JWM Tray configuration...')" &
XPID=$!

APPDIR="`dirname $0`"
[ "$APPDIR" = "." ] && APPDIR="`pwd`"
export APPDIR="$APPDIR"
export WORKDIR=$HOME/.jwm/tmp
[ ! -d "$WORKDIR" ] && mkdir -p "$WORKDIR"

case $1 in
	-clock)
		PAGE_NR=6
		DEFAULT_CLOCK=true
		;;
	*)
		PAGE_NR=0
		DEFAULT_CLOCK=false
		;;
esac

#default values
echo 1 > $WORKDIR/tray_nr
echo 32 > $WORKDIR/tray_SWALLOW_WIDTH
echo '' > $WORKDIR/tray_SWALLOW_HEIGHT
find /usr/share/jwm/tray_templates -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort > $WORKDIR/tray_templates



[ ! -f $HOME/.jwm/jwmrc-tray4 ] && echo -n > $HOME/.jwm/jwmrc-tray4
[ ! -f $HOME/.jwm/jwmrc-tray3 ] && echo -n > $HOME/.jwm/jwmrc-tray3
[ ! -f $HOME/.jwm/jwmrc-tray2 ] && echo -n > $HOME/.jwm/jwmrc-tray2
[ ! -f $HOME/.jwm/jwmrc-tray1 ] && echo '
<?xml version="1.0"?>
<JWM>
<Tray autohide="off" insert="right" layer="above" halign="center" valign="bottom" height="28" layout="horizontal" >
    <TrayButton label="Menu" icon="puppy.svg">root:9</TrayButton>
    <TrayButton popup="Show desktop" icon="desktop.svg">showdesktop</TrayButton>
    <TrayButton popup="Wizard Wizard" icon="/usr/share/pixmaps/puppy/puppy_config.svg">exec:wizardwizard</TrayButton>
    <TrayButton popup="Urxvt terminal emulator" icon="/usr/share/pixmaps/puppy/terminal.svg">exec:urxvt</TrayButton>
    <Pager/>
    <TaskList maxwidth="200"/>
    <Dock/>
    <Swallow name="xload" width="32">xload -nolabel -fg "#225F7C" -hl white -bg "#222"</Swallow>
    <Clock format="%I:%M">/usr/local/jwm_config/time</Clock>
</Tray>
</JWM>' > $HOME/.jwm/jwmrc-tray1



  ##########################################################################
  ##                                                                      ##
  ##                           F U N C T I O N S                          ##
  ##                                                                      ##
  ##########################################################################
  

load (){
	TEMPLATE="$1"
	cp -f /usr/share/jwm/tray_templates/"$TEMPLATE"/jwmrc-tray* $HOME/.jwm/
	[ -s /usr/share/jwm/tray_templates/"$TEMPLATE"/install ] && /usr/share/jwm/tray_templates/"$TEMPLATE"/install
	read_settings
	apply
}

save (){
	[ "$1" = "$(gettext 'Load setup')" ] && TMP="" || TMP="$1"
	export JWM_tray='
	<window title="'$(gettext 'Save')'" icon-name="save">
	<vbox>
	  <frame '$(gettext 'Save tray setup')'>
	    <text><label>"'$(gettext 'Note!
This will save the last APPLIED tray setup.')'
"</label></text> 
	    <hbox>
	      <text><label>'$(gettext 'Name')'</label></text> 
	      <entry>
	        <variable>SAVE</variable>
	        <default>"'$TMP'"</default>
	      </entry>
	    </hbox>
	  </frame>
	  <hbox>
	    <button cancel></button>
	    <button can-default="true" has-default="true" use-stock="true">
	      <label>'$(gettext 'Save')'</label>
	      <input file stock="gtk-save"></input>
	      <action type="exit">save</action>
	    </button>
	  </hbox>
	</vbox>
	</window>'
	I=$IFS; IFS=""
	for STATEMENTS in $(gtkdialog -p JWM_tray --center); do
		eval $STATEMENTS
	done
	IFS=$I
	if [ "$EXIT" = "save" ]; then
		if [ ! "$SAVE" ]; then
			. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext 'No name defined')"
			exit
		elif [ -d /usr/share/jwm/tray_templates/"$SAVE"/ ]; then #already exist
			. /usr/lib/gtkdialog/box_yesno "$(gettext 'JWM tray')" "<b>$(gettext 'Template already exist')</b>" "$(gettext 'Do you want to overwrite it?')"
			[ $EXIT != yes ] && exit
		else
			mkdir -p /usr/share/jwm/tray_templates/"$SAVE"/
		fi
		cp -f $HOME/.jwm/jwmrc-tray* /usr/share/jwm/tray_templates/"$SAVE"/
		find /usr/share/jwm/tray_templates -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort > $WORKDIR/tray_templates
	fi
}

delete (){
	if [ ! -d /usr/share/jwm/tray_templates/"$1"/ ]; then
		. /usr/lib/gtkdialog/box_ok "$(gettext 'JWM tray')" error "$(gettext 'Nothing to delete')"
	else
		. /usr/lib/gtkdialog/box_yesno "$(gettext 'JWM tray')" "$(gettext 'Do you really want to delete the Tray template')" "<b>$1</b>"
		[ $EXIT != yes ] && exit
		rm -fr /usr/share/jwm/tray_templates/"$1"
		find /usr/share/jwm/tray_templates -mindepth 1 -maxdepth 1 -type d -printf "%f\n" | sort > $WORKDIR/tray_templates
	fi
}

add (){
	case $1 in
	Tray)
		#include header lines - if any
		grep -iFB 10 -m 1 '<tray ' $WORKDIR/tray_rc | grep -viF '<tray ' > $WORKDIR/tray_tmp1
		grep -iFA 100 -m 1 '<tray ' $WORKDIR/tray_rc | grep -viF '<tray ' > $WORKDIR/tray_tmp
		HALIGN="$(<$WORKDIR/tray_HALIGN)"
		VALIGN="$(<$WORKDIR/tray_VALIGN)"
		if [ $AUTOHIDE = true ]; then
			if [ $VALIGN = top ] && [ $DIRECTION_HOR = true ]; then
				AUTOHIDE=top
			elif [ $VALIGN = top ] && [ $DIRECTION_VER = true ] && [ $HALIGN = center ]; then
				AUTOHIDE=top
			elif [ $VALIGN = bottom ] && [ $DIRECTION_HOR = true ]; then
				AUTOHIDE=bottom
			elif [ $VALIGN = bottom ] && [ $DIRECTION_VER = true ] && [ $HALIGN = center ]; then
				AUTOHIDE=bottom
			elif [ $HALIGN = left ]; then
				AUTOHIDE=left
			elif [ $HALIGN = right ]; then
				AUTOHIDE=right
			else
				AUTOHIDE=off
			fi
		else
			AUTOHIDE=off
		fi
		echo -e "<Tray autohide=\"$AUTOHIDE\" insert=\"right\" layer=\"$LAYER\" halign=\"$HALIGN\" valign=\"$VALIGN\" \c" >> $WORKDIR/tray_tmp1
		[ $DIRECTION_HOR = true ] && echo -e "height=\"$HEIGHT\" layout=\"horizontal\" \c"  >> $WORKDIR/tray_tmp1
		[ $DIRECTION_VER = true ] && echo -e "width=\"$WIDTH\" layout=\"vertical\" \c" >> $WORKDIR/tray_tmp1
		echo -e ">" >> $WORKDIR/tray_tmp1
		cat $WORKDIR/tray_tmp >> $WORKDIR/tray_tmp1
		mv $WORKDIR/tray_tmp1 $WORKDIR/tray_rc
		;;
	Menu)
		case $2 in
		main)
			echo -n '    <TrayButton label="'$(gettext 'Menu')'"' >> $WORKDIR/tray_rc
			[ $MENU_MAIN_CHK = true ] && echo -n ' icon="puppy.svg"' >> $WORKDIR/tray_rc
			echo '>root:9</TrayButton>' >> $WORKDIR/tray_rc
			;;
		bookmarks)
			echo -n '    <TrayButton label="'$(gettext 'Bookmarks')'"' >> $WORKDIR/tray_rc
			[ $MENU_BOOKMARKS_CHK = true ] && echo -n ' icon="bookmark.svg"' >> $WORKDIR/tray_rc
			echo '>root:6</TrayButton>' >> $WORKDIR/tray_rc
			;;
		places)
			echo -n '    <TrayButton label="'$(gettext 'Places')'"' >> $WORKDIR/tray_rc
			[ $MENU_PLACES_CHK = true ] && echo -n ' icon="directory.svg"' >> $WORKDIR/tray_rc
			echo '>root:8</TrayButton>' >> $WORKDIR/tray_rc
			;;
		esac
		;;
	Dock\ area)
		echo '    <Dock/>' >> $WORKDIR/tray_rc
		;;
	Tasklist)
		case $TASKLIST_STYLE in
			"$(gettext 'Iconify')")			TMP=25;;
			"$(gettext 'Fixed width')")		TMP=200;;
			*)								TMP=0;;
		esac
		if [ "`grep -F '<TaskList' $WORKDIR/tray_rc`" ]; then
			TMP1="`grep '<TaskList' $WORKDIR/tray_rc`"
			sed -i "s%$TMP1%    <TaskList maxwidth=\"$TMP\"/>%" $WORKDIR/tray_rc
		else
			echo "    <TaskList maxwidth=\"$TMP\"/>" >> $WORKDIR/tray_rc
		fi
		;;
	Spacer)
		echo "    <Spacer width=\"$SPACER\" height=\"$SPACER\"/> <!-- $(date +%s) -->" >> $WORKDIR/tray_rc
		;;
	Clock)
		[ ! "$CLOCK" ] && CLOCK="24:mm"
		if [ "$CLOCK_ACTION" = "$(gettext 'Clock tools')" ]; then
			CLOCK_ACTION="exec:/usr/local/jwm_config/time"
		elif [ ! "$CLOCK_ACTION" ] || [ "$CLOCK_ACTION" = "$(gettext 'Clock menu')" ]; then
			CLOCK_ACTION="root:7"
		else
			CLOCK_ACTION="exec:$CLOCK_ACTION"
		fi
		TMP=''
		[ "`grep -F 12 <<< "$CLOCK"`" ] && TMP=$TMP'%I'
		[ "`grep -F 24 <<< "$CLOCK"`" ] && TMP=$TMP'%H'
		[ "`grep -F mm <<< "$CLOCK"`" ] && TMP=$TMP':%M'
		[ "`grep -F ss <<< "$CLOCK"`" ] && TMP=$TMP':%S'
		[ "`grep -F AM <<< "$CLOCK"`" ] && TMP=$TMP':%p'
		if [ "`grep -F '<Clock' $WORKDIR/tray_rc`" ]; then
			TMP1="`grep '<Clock' $WORKDIR/tray_rc`"
			sed -i "s|${TMP1}|    <Clock format=\"$TMP\">${CLOCK_ACTION}</Clock>|" $WORKDIR/tray_rc
		else
			echo "    <Clock format=\"${TMP}\">${CLOCK_ACTION}</Clock>" >> $WORKDIR/tray_rc
		fi
		;;
	Pager)
		if [ "`grep -F '<Desktops ' $HOME/.jwm/jwmrc-personal`" ]; then
			TMP="`grep '<Desktops ' $HOME/.jwm/jwmrc-personal`"
			sed -i "s%$TMP%<Desktops width=\"$PAGER_COLUMNS\" height=\"$PAGER_ROWS\"/>%" $HOME/.jwm/jwmrc-personal
		else
			TMP="`grep -i '<JWM>' $HOME/.jwm/jwmrc-personal`"
			sed -i "s%$TMP%<JWM>\n\n<!-- Number of virtual desktops -->\n<Desktops width=\"$PAGER_COLUMNS\" height=\"$PAGER_ROWS\"/>%" $HOME/.jwm/jwmrc-personal
		fi
		[ ! "`grep -F 'Pager' $WORKDIR/tray_rc`" ] && echo '    <Pager/>' >> $WORKDIR/tray_rc
		;;
	Function\ button)
		case $FUNCTION in
		$(gettext 'Show desktop'))
			echo '    <TrayButton popup="'$(gettext 'Show desktop')'" icon="desktop.svg">showdesktop</TrayButton>' >> $WORKDIR/tray_rc
			;;
		$(gettext 'Lock screen'))
			echo '    <TrayButton popup="'$(gettext 'Lock screen')'" icon="screen_lock.svg">exec:/usr/local/apps/Xlock/AppRun</TrayButton>' >> $WORKDIR/tray_rc
			;;
		$(gettext 'Puppy Help'))
			echo '    <TrayButton popup="'$(gettext 'Puppy Help')'" icon="dialog-info.svg">exec:defaulthtmlviewer file:///usr/share/doc/index.html</TrayButton>' >> $WORKDIR/tray_rc
			;;
		$(gettext 'Leave'))
			echo '    <TrayButton popup="'$(gettext 'Leave Puppy')'" icon="quit.svg">exec:/usr/sbin/logout_gui</TrayButton>' >> $WORKDIR/tray_rc
			;;
		$(gettext 'Shutdown NOW'))
			echo '    <TrayButton popup="'$(gettext 'Power OFF computer')'" icon="shutdown.svg">exec:wmpoweroff</TrayButton>' >> $WORKDIR/tray_rc
			;;
		$(gettext 'Reboot NOW'))
			echo '    <TrayButton popup="'$(gettext 'Reboot computer')'" icon="reboot.svg">exec:wmreboot</TrayButton>' >> $WORKDIR/tray_rc
			;;
		$(gettext 'Reboot NOW'))
			echo '    <TrayButton popup="'$(gettext 'Reboot computer')'" icon="reboot.svg">exec:wmreboot</TrayButton>' >> $WORKDIR/tray_rc
			;;
		$(gettext 'Edit tray'))
			echo '    <TrayButton popup="'$(gettext 'Edit tray')'" icon="desktop_tray_config.svg">exec:/usr/local/jwm_config/tray</TrayButton>' >> $WORKDIR/tray_rc
			;;
		Theme*)
			THEME="`cut -d: -f2- <<< "$FUNCTION"`"
			ICON="`grep -F 'ICON=' "/usr/share/ptheme/globals/${THEME}" | cut -d'=' -f2 | tr -d '"'`"
			echo '    <TrayButton popup="'$(gettext 'Puppy global theme')'" icon="'$ICON'">exec:ptheme -p "'$THEME'"</TrayButton>' >> $WORKDIR/tray_rc
			;;
		esac
		;;
	Program\ button)
		POPUP="`grep -Fm 1 Name "/usr/share/applications/${PROGRAM}.desktop" | cut -d'=' -f2`" #find description
		ICON="`grep -Fm 1 Icon "/usr/share/applications/${PROGRAM}.desktop" | cut -d'=' -f2`" #find icon
		EXEC="`grep -Fm 1 Exec "/usr/share/applications/${PROGRAM}.desktop" | cut -d'=' -f2`"
		#write item to jwm
		echo "    <TrayButton popup=\"$POPUP\" icon=\"$ICON\">exec:$EXEC</TrayButton>" >> $WORKDIR/tray_rc
		;;
	Swallowed)
		SWALLOW_NAME="`cut -d' ' -f1 <<< "$SWALLOW"`"
		[ "$SWALLOW_HEIGHT" ] && TMP="height=\"$SWALLOW_HEIGHT\""
		[ "$SWALLOW_WIDTH" ] && TMP1="width=\"$SWALLOW_WIDTH\""
		echo -e '    <Swallow name="'${SWALLOW_NAME}'" '${TMP}' '${TMP1}'>'${SWALLOW}'</Swallow>' >> $WORKDIR/tray_rc
		;;
	esac
}

apply (){
	echo -e "<?xml version="'"1.0"'"?>\n<JWM>\n" > "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
	if [ "`grep -Fiv '<tray ' $WORKDIR/tray_rc | grep -v "^$"`" ]; then #with an empty <tray> tag, video fullscreen fails.
		cat $WORKDIR/tray_rc >> "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
		echo '</Tray>' >> "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
	fi
	echo '</JWM>' >> "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"
	$APPDIR/jwm_restart
	set_icons
}

move_up (){
	TRAY="$(<$WORKDIR/tray_treeselect)"
	SWITCH_ITEM="`grep -B 1 "$TRAY" $WORKDIR/tray_rc | grep -v "$TRAY"`" 
	if [ ! "$SWITCH_ITEM" ] || [ "`grep -F '<Tray ' <<< "$SWITCH_ITEM"`" ]; then exit; fi
	ABOVE="`grep -B 1000 "$TRAY" $WORKDIR/tray_rc | grep -v "$TRAY" | grep -v "$SWITCH_ITEM"`" 
	BELOW="`grep -A 1000 "$TRAY" $WORKDIR/tray_rc | grep -v "$TRAY"`"
	echo -e "$ABOVE\n    $TRAY\n$SWITCH_ITEM\n$BELOW" | grep -v "^$" > $WORKDIR/tray_rc
}

move_down (){
	TRAY="$(<$WORKDIR/tray_treeselect)"
	SWITCH_ITEM="`grep -A 1 "$TRAY" $WORKDIR/tray_rc | grep -v "$TRAY"`"
	if [ ! "$SWITCH_ITEM" ] || [ "`grep -F '<Tray ' <<< "$SWITCH_ITEM"`" ]; then exit; fi
	ABOVE="`grep -B 1000 "$TRAY" $WORKDIR/tray_rc | grep -v "$TRAY"`" 
	BELOW="`grep -A 1000 "$TRAY" $WORKDIR/tray_rc | grep -v "$TRAY" | grep -v "$SWITCH_ITEM"`"
	echo -e "$ABOVE\n$SWITCH_ITEM\n    $TRAY\n$BELOW" | grep -v "^$" > $WORKDIR/tray_rc
}

read_settings (){
	#read tray rc
	grep -vEi "jwm>|/tray>|<?xml" "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep "[a-z]" > $WORKDIR/tray_rc
	#---
	TMP="`grep -Fi '<Tray' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep -om1 " halign=.* " | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_HALIGN || echo center > $WORKDIR/tray_HALIGN
	TMP="`grep -Fi '<Tray' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep -om1 " valign=.* " | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_VALIGN || echo bottom > $WORKDIR/tray_VALIGN
    	TMP="`grep -Fi '<Tray' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep -om1 " height=.* " | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_HEIGHT || echo 28 > $WORKDIR/tray_HEIGHT
	TMP="`grep -Fi '<Tray' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep -om1 " width=.* " | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_WIDTH || echo 28 > $WORKDIR/tray_WIDTH
	TMP="`grep -o " layer=.* " "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_LAYER || echo above > $WORKDIR/tray_LAYER
	TMP="`grep -o " autohide=.* " "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | cut -d'"' -f2`"
	[ "$TMP" = "off" ] && echo false > $WORKDIR/tray_AUTOHIDE || echo true > $WORKDIR/tray_AUTOHIDE
	if [ "`grep -F 'layout="vertical"' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)"`" ]; then
		echo true > $WORKDIR/tray_DIRECTION_VER
		echo false > $WORKDIR/tray_DIRECTION_HOR
	else
		echo true > $WORKDIR/tray_DIRECTION_HOR
		echo false > $WORKDIR/tray_DIRECTION_VER
	fi
	#pager
	TMP="`grep -F '<Desktops ' $HOME/.jwm/jwmrc-personal | cut -d'"' -f4`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_PAGER_ROWS || echo 1 > $WORKDIR/tray_PAGER_ROWS
	TMP="`grep -F '<Desktops ' $HOME/.jwm/jwmrc-personal | cut -d'"' -f2`"
	[ "$TMP" ] && echo "$TMP" > $WORKDIR/tray_PAGER_COLUMNS || echo 3 > $WORKDIR/tray_PAGER_COLUMNS
	#Tasklist
	TMP="`grep -F '<TaskList' "$HOME/.jwm/jwmrc-tray$(<$WORKDIR/tray_nr)" | grep -F maxwidth | cut -d'"' -f2`"
	[ ! "$TMP" ] && TMP=0
	case $TMP in
		25)		echo "$(gettext 'Iconify')" > $WORKDIR/tray_TASKLIST_STYLE;;
		200)	echo "$(gettext 'Fixed width')" > $WORKDIR/tray_TASKLIST_STYLE;;
		*)		echo "$(gettext 'Variable width')" > $WORKDIR/tray_TASKLIST_STYLE;;
	esac
}

set_icons (){
	#set placement icons
	for I in 1 2 3 4; do
		if [ "`grep -Fi 'valign="bottom"' $HOME/.jwm/jwmrc-tray$I | grep -Eiv 'halign="left"|halign="right"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_bottom.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Fi 'valign="top"' $HOME/.jwm/jwmrc-tray$I | grep -Eiv 'halign="left"|halign="right"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_top.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Fi 'valign="top"' $HOME/.jwm/jwmrc-tray$I | grep -Fi 'halign="left"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_topleft.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Fi 'valign="top"' $HOME/.jwm/jwmrc-tray$I | grep -Fi 'halign="right"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_topright.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Fi 'valign="bottom"' $HOME/.jwm/jwmrc-tray$I | grep -Fi 'halign="left"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_bottomleft.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Fi 'valign="bottom"' $HOME/.jwm/jwmrc-tray$I | grep -Fi 'halign="right"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_bottomright.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Fi 'halign="left"' $HOME/.jwm/jwmrc-tray$I | grep -Eiv 'valign="top"|valign="bottom"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_left.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Fi 'halign="right"' $HOME/.jwm/jwmrc-tray$I | grep -Eiv 'valign="top"|valign="bottom"'`" ]; then
			cp /usr/share/pixmaps/puppy/placement_right.svg $WORKDIR/tray_$I.svg
		elif [ "`grep -Ei ' y=|x=' $HOME/.jwm/jwmrc-tray$I`" ]; then
			cp /usr/share/pixmaps/puppy/placement.svg $WORKDIR/tray_$I.svg
		else
			cp /usr/share/pixmaps/puppy/placement_none.svg $WORKDIR/tray_$I.svg
		fi
	done
}

export -f load save delete add apply read_settings move_up move_down set_icons



  ##########################################################################
  ##                                                                      ##
  ##                             P R O G R A M                            ##
  ##                                                                      ##
  ##########################################################################



#build program-list
echo > $WORKDIR/tray_applist
ls -1 /usr/share/applications/ | sed 's/.desktop//g' >> $WORKDIR/tray_applist
#build theme-list
ITEMS_THEMES="`find /usr/share/ptheme/globals/ -mindepth 1 -maxdepth 1 -type f -printf "<item>Theme:%f</item>\n"`"

read_settings
set_icons

echo '
<window title="'$(gettext 'Tray configuration')'" icon-name="gtk-preferences" default-height="450" default-width="700">
<vbox space-fill="true" space-expand="true">
  <vbox space-fill="false" space-expand="false">
    <hbox space-fill="true" space-expand="true">
      <pixmap>
        <variable>ICON1</variable>
        <input file>'$WORKDIR'/tray_1.svg</input>
        <height>60</height>
      </pixmap>
      <pixmap>
        <variable>ICON2</variable>
        <input file>'$WORKDIR'/tray_2.svg</input>
        <height>60</height>
      </pixmap>
      <pixmap>
        <variable>ICON3</variable>
        <input file>'$WORKDIR'/tray_3.svg</input>
        <height>60</height>
      </pixmap>
      <pixmap>
        <variable>ICON4</variable>
        <input file>'$WORKDIR'/tray_4.svg</input>
        <height>60</height>
      </pixmap>

    </hbox>
    <hbox space-fill="true" space-expand="true">
      <radiobutton draw-indicator="false">
        <label>'$(gettext 'Tray 1')'</label>
        <variable>RADIO_TRAY1</variable>
        <default>true</default>
        <action signal="button-release-event">echo 0 > '$WORKDIR'/notebook_page</action>
        <action signal="button-release-event">refresh:GUI_OPTIONS</action>
        <action signal="button-release-event">echo true > '$WORKDIR'/tray_radio_null</action>
        <action signal="button-release-event">refresh:RADIO_NULL</action>
        <action signal="button-release-event">disable:BOX_TREEBUTTONS</action>
        <action>if true echo 1 > $WORKDIR/tray_nr</action>
        <action>if true read_settings</action>
        <action>if true refresh:HEIGHT</action>
        <action>if true refresh:WIDTH</action>
        <action>if true refresh:AUTOHIDE</action>
        <action>if true refresh:DIRECTION_HOR</action>
        <action>if true refresh:DIRECTION_VER</action>
        <action>if true refresh:LAYER</action>
        <action>if true refresh:PAGER_ROWS</action>
        <action>if true refresh:PAGER_COLUMNS</action>
        <action>if true refresh:TASKLIST_STYLE</action>
        <action>if true add Tray</action>
        <action>if true refresh:TRAY</action>
      </radiobutton>
      <radiobutton draw-indicator="false">
        <label>'$(gettext 'Tray 2')'</label>
        <variable>RADIO_TRAY2</variable>
        <action signal="button-release-event">echo 0 > '$WORKDIR'/notebook_page</action>
        <action signal="button-release-event">refresh:GUI_OPTIONS</action>
        <action signal="button-release-event">echo true > '$WORKDIR'/tray_radio_null</action>
        <action signal="button-release-event">refresh:RADIO_NULL</action>
        <action signal="button-release-event">disable:BOX_TREEBUTTONS</action>
        <action>if true echo 2 > $WORKDIR/tray_nr</action>
        <action>if true read_settings</action>
        <action>if true refresh:HEIGHT</action>
        <action>if true refresh:WIDTH</action>
        <action>if true refresh:AUTOHIDE</action>
        <action>if true refresh:DIRECTION_HOR</action>
        <action>if true refresh:DIRECTION_VER</action>
        <action>if true refresh:LAYER</action>
        <action>if true refresh:PAGER_ROWS</action>
        <action>if true refresh:PAGER_COLUMNS</action>
        <action>if true refresh:TASKLIST_STYLE</action>
        <action>if true add Tray</action>
        <action>if true refresh:TRAY</action>
      </radiobutton>
      <radiobutton draw-indicator="false">
        <label>'$(gettext 'Tray 3')'</label>
        <variable>RADIO_TRAY3</variable>
        <action signal="button-release-event">echo 0 > '$WORKDIR'/notebook_page</action>
        <action signal="button-release-event">refresh:GUI_OPTIONS</action>
        <action signal="button-release-event">echo true > '$WORKDIR'/tray_radio_null</action>
        <action signal="button-release-event">refresh:RADIO_NULL</action>
        <action signal="button-release-event">disable:BOX_TREEBUTTONS</action>
        <action>if true echo 3 > $WORKDIR/tray_nr</action>
        <action>if true read_settings</action>
        <action>if true refresh:HEIGHT</action>
        <action>if true refresh:WIDTH</action>
        <action>if true refresh:AUTOHIDE</action>
        <action>if true refresh:DIRECTION_HOR</action>
        <action>if true refresh:DIRECTION_VER</action>
        <action>if true refresh:LAYER</action>
        <action>if true refresh:PAGER_ROWS</action>
        <action>if true refresh:PAGER_COLUMNS</action>
        <action>if true refresh:TASKLIST_STYLE</action>
        <action>if true add Tray</action>
        <action>if true refresh:TRAY</action>
      </radiobutton>
      <radiobutton draw-indicator="false">
        <label>'$(gettext 'Tray 4')'</label>
        <variable>RADIO_TRAY4</variable>
        <action signal="button-release-event">echo 0 > '$WORKDIR'/notebook_page</action>
        <action signal="button-release-event">refresh:GUI_OPTIONS</action>
        <action signal="button-release-event">echo true > '$WORKDIR'/tray_radio_null</action>
        <action signal="button-release-event">refresh:RADIO_NULL</action>
        <action signal="button-release-event">disable:BOX_TREEBUTTONS</action>
        <action>if true echo 4 > $WORKDIR/tray_nr</action>
        <action>if true read_settings</action>
        <action>if true refresh:HEIGHT</action>
        <action>if true refresh:WIDTH</action>
        <action>if true refresh:AUTOHIDE</action>
        <action>if true refresh:DIRECTION_HOR</action>
        <action>if true refresh:DIRECTION_VER</action>
        <action>if true refresh:LAYER</action>
        <action>if true refresh:PAGER_ROWS</action>
        <action>if true refresh:PAGER_COLUMNS</action>
        <action>if true refresh:TASKLIST_STYLE</action>
        <action>if true add Tray</action>
        <action>if true refresh:TRAY</action>
      </radiobutton>
    </hbox>
  </vbox>
  
  <hbox space-fill="true" space-expand="true">

    <hbox space-fill="false" space-expand="false">
      <vbox space-fill="true" space-expand="true">
        <radiobutton visible="false">
          <input file>'$WORKDIR'/tray_radio_null</input>
          <variable>RADIO_NULL</variable>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Buttons')'</label>
          <action>if true echo 6 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Menus')'</label>
          <action>if true echo 1 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>" '$(gettext 'Desktops')' "</label>
          <action>if true echo 4 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Tasklist')'</label>
          <action>if true echo 3 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Apps')'</label>
          <action>if true enable:BUTTON_DOCK_ADD</action>
          <action condition="command_is_true([[ `grep -F \"Dock\" '$WORKDIR'/tray_rc` ]] && echo true)">if true disable:BUTTON_DOCK_ADD</action>
          <action>if true echo 2 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Clock')'</label>
          <default>'$DEFAULT_CLOCK'</default>
          <action>if true echo 5 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
        <radiobutton draw-indicator="false">
          <label>'$(gettext 'Spacer')'</label>
          <action>if true echo 7 > '$WORKDIR'/notebook_page</action>
          <action>if true refresh:GUI_OPTIONS</action>
        </radiobutton>
      </vbox>
    </hbox>

    <hbox space-fill="false" space-expand="false">
      <vbox space-fill="true" space-expand="true">
        <notebook page="'$PAGE_NR'" show-tabs="false" show-border="false" labels="1|2|3|4|5|6|7|8">' > $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                   T R A Y                    ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Tray settings')'>
    <text use-markup="true"><label>"'$(gettext 'Nothing will be stored or activated until you press the <b>Apply</b> button.')'"</label></text>
    <hbox margin="20">
      <vbox homogeneous="true">
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_topleft big`"'
          <action>echo left > '$WORKDIR'/tray_HALIGN; echo top > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_top big`"'
          <action>echo center > '$WORKDIR'/tray_HALIGN; echo top > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_topright big`"'
          <action>echo right > '$WORKDIR'/tray_HALIGN; echo top > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
      </vbox>
      <vbox homogeneous="true">
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_left big`"'
          <action>echo left > '$WORKDIR'/tray_HALIGN; echo center > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
        <text><label>'$(gettext 'Placement')'</label></text>
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_right big`"'
          <action>echo right > '$WORKDIR'/tray_HALIGN; echo center > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
      </vbox>
      <vbox homogeneous="true">
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_bottomleft big`"'
          <action>echo left > '$WORKDIR'/tray_HALIGN; echo bottom > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_bottom big`"'
          <action>echo center > '$WORKDIR'/tray_HALIGN; echo bottom > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
        <button relief="2">
          '"`/usr/lib/gtkdialog/xml_button-icon placement_bottomright big`"'
          <action>echo right > '$WORKDIR'/tray_HALIGN; echo bottom > '$WORKDIR'/tray_VALIGN; add Tray</action>
          <action>refresh:TRAY</action>
        </button>
      </vbox>
      <vseparator width-request="40"></vseparator>

      <vbox>
        <text><label>""</label></text>
        <checkbox tooltip-text="'$(gettext 'Hide when not active')'">
          <variable>AUTOHIDE</variable>
          <label>'$(gettext 'Autohide')'</label>
          <input file>'$WORKDIR'/tray_AUTOHIDE</input>
          <action>add Tray</action>
          <action>refresh:TRAY</action>
        </checkbox>
        <text><label>""</label></text>
        <vbox tooltip-text="'$(gettext 'Direction of the tray')'">
          <radiobutton>
            <variable>DIRECTION_HOR</variable>
            <label>'$(gettext 'Horizontal Tray')'</label>
            <input file>'$WORKDIR'/tray_DIRECTION_HOR</input>
            <action signal="realize">if true hide:BOX_TRAY_WIDTH</action>
            <action>if true show:BOX_TRAY_HEIGHT</action>
            <action>if true hide:BOX_TRAY_WIDTH</action>
            <action>if true add Tray</action>
            <action>if true refresh:TRAY</action>
          </radiobutton>
          <radiobutton>
            <variable>DIRECTION_VER</variable>
            <input file>'$WORKDIR'/tray_DIRECTION_VER</input>
            <label>'$(gettext 'Vertical Tray')'</label>
            <action signal="realize">if true hide:BOX_TRAY_HEIGHT</action>
            <action>if true hide:BOX_TRAY_HEIGHT</action>
            <action>if true show:BOX_TRAY_WIDTH</action>
            <action>if true add Tray</action>
            <action>if true refresh:TRAY</action>
          </radiobutton>
        </vbox>
        <hbox>
          <text xalign="0"><label>'$(gettext 'Tray Height (px)')'</label></text>
          <entry editable="true" allow-empty="false" width-request="40">
            <input file>'$WORKDIR'/tray_HEIGHT</input>
            <variable>HEIGHT</variable>
            <action signal="focus-out-event">if [ "`echo "$HEIGHT" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $HEIGHT > '$WORKDIR'/tray_HEIGHT; fi</action>
            <action signal="focus-out-event">refresh:HEIGHT</action>
            <action signal="focus-out-event">add Tray</action>
            <action signal="focus-out-event">refresh:TRAY</action>
          </entry>
          <variable>BOX_TRAY_HEIGHT</variable>
        </hbox>
        <hbox>
          <text xalign="0"><label>'$(gettext 'Tray Width (px)')'</label></text>
          <entry editable="true" allow-empty="false" width-request="40">
            <input file>'$WORKDIR'/tray_WIDTH</input>
            <variable>WIDTH</variable>
            <action signal="focus-out-event">if [ "`echo "$WIDTH" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $WIDTH > '$WORKDIR'/tray_WIDTH; fi</action>
            <action signal="focus-out-event">refresh:WIDTH</action>
            <action signal="focus-out-event">add Tray</action>
            <action signal="focus-out-event">refresh:TRAY</action>
          </entry>
          <variable>BOX_TRAY_WIDTH</variable>
        </hbox>
        <text><label>""</label></text>
        <vbox>
          <hbox tooltip-text="'$(gettext 'Should the tray be above/below programs.')'">
            <text xalign="0"><label>"'$(gettext 'Layer')'"</label></text>
            <comboboxtext>
              <variable>LAYER</variable>
              <input file>'$WORKDIR'/tray_LAYER</input>
              <item>below</item>
              <item>normal</item>
              <item>above</item>
              <action>add Tray</action>
              <action>refresh:TRAY</action>
            </comboboxtext>
          </hbox>
        </vbox>
      </vbox>
    </hbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##             M E N U    B U T T O N           ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Menu')'>
    <vbox margin="20">
      <text><label>"'$(gettext 'Add menu-button - if not already added.')'
      "</label></text>
      <hbox>
        <button>
          <label>" '$(gettext 'Main menu')' "</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>[ ! "`grep -F ">root:9" '$WORKDIR'/tray_rc`" ] && add Menu main</action>
          <action>refresh:TRAY</action>
        </button>
        <checkbox>
          <variable>MENU_MAIN_CHK</variable>
          <label>" '$(gettext 'Include icon')' "</label>
        </checkbox>
      </hbox>
      <text><label>""</label></text>
      <hbox>
        <button>
          <label>" '$(gettext 'Bookmarks menu')' "</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>[ ! "`grep -F ">root:6" '$WORKDIR'/tray_rc`" ] && add Menu bookmarks</action>
          <action>refresh:TRAY</action>
        </button>
        <checkbox>
          <variable>MENU_BOOKMARKS_CHK</variable>
          <label>" '$(gettext 'Include icon')' "</label>
        </checkbox>
      </hbox>
      <text><label>""</label></text>
      <hbox>
        <button>
          <label>" '$(gettext 'Places menu')' "</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>[ ! "`grep -F ">root:8" '$WORKDIR'/tray_rc`" ] && add Menu places</action>
          <action>refresh:TRAY</action>
        </button>
        <checkbox>
          <variable>MENU_PLACES_CHK</variable>
          <label>" '$(gettext 'Include icon')' "</label>
        </checkbox>
      </hbox>
    </vbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml
   

               ##################################################
               ##                                              ##
               ##                   A P P S                    ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Apps')'>
    <vbox margin="20">
      <text><label>"'$(gettext 'Generally, running tray-apps place themself in the Dock area. Only one Dock area is supported.')'"</label></text>
      <hbox>
        <button>
          <variable>BUTTON_DOCK_ADD</variable>
          <label>" '$(gettext 'Add Dock area')' "</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>[ ! "`grep -F "Dock" '$WORKDIR'/tray_rc`" ] && add "Dock area"</action>
          <action>refresh:TRAY</action>
        </button>
      </hbox>
      
      <hseparator height-request="40"></hseparator>
     
      <text><label>"'$(gettext 'In addition to docked apps, you can define specific apps to appear (swallowed) in the tray. Choose from the list or edit to swallow app (few apps allow swallowing).')'"</label></text>
      <hbox>
        <text><label>""</label></text>
        <vbox>
          <hbox>
            <text xalign="0" space-fill="true" space-expand="true"><label>'$(gettext 'Command')'</label></text>
            <comboboxentry>
              <variable>SWALLOW</variable>' >> $WORKDIR/tray_xml
              [ "`which asapm`" ]			&& echo '<item>asapmshell -u 4 -status PaleGreen3 -green PaleGreen3</item>' >> $WORKDIR/tray_xml
              [ "`which xload`" ]			&& echo '<item>xload -nolabel -fg "#225F7C" -hl white -bg "#222"</item>' >> $WORKDIR/tray_xml
              [ "`which freememapplet`" ]	&& echo '<item>freememappletshell</item>' >> $WORKDIR/tray_xml
              [ "`which blinkydelayed`" ]	&& echo '<item>blinkydelayed -bg "#939191"</item>' >> $WORKDIR/tray_xml
              [ "`which xvkbd`" ]			&& echo '<item>xvkbd -geometry 800x300 -no-keypad</item>' >> $WORKDIR/tray_xml
            echo '</comboboxentry>
          
          </hbox>
          <hbox>
            <text xalign="0" space-fill="true" space-expand="true"><label>'$(gettext 'Height (optional)')'</label></text>
            <entry editable="true" allow-empty="false" width-request="40" space-fill="false" space-expand="false">
              <input file>'$WORKDIR'/tray_SWALLOW_HEIGHT</input>
              <variable>SWALLOW_HEIGHT</variable>
              <action signal="focus-out-event">if [ "`echo "$SWALLOW_HEIGHT" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $SWALLOW_HEIGHT > '$WORKDIR'/tray_SWALLOW_HEIGHT; fi</action>
              <action signal="focus-out-event">refresh:SWALLOW_HEIGHT</action>
            </entry>
          </hbox>
          <hbox>
            <text xalign="0" space-fill="true" space-expand="true"><label>'$(gettext 'Width (optional)')'</label></text>
            <entry editable="true" allow-empty="false" width-request="40" space-fill="false" space-expand="false">
              <input file>'$WORKDIR'/tray_SWALLOW_WIDTH</input>
              <variable>SWALLOW_WIDTH</variable>
              <action signal="focus-out-event">if [ "`echo "$SWALLOW_WIDTH" | grep -v "[a-z]" | grep "[0-9]"`" ]; then echo $SWALLOW_WIDTH > '$WORKDIR'/tray_SWALLOW_WIDTH; fi</action>
              <action signal="focus-out-event">refresh:SWALLOW_WIDTH</action>
            </entry>
          </hbox>
        </vbox>
        <text><label>""</label></text>
        <button image-position="2">
          <label>" '$(gettext 'Swallow app')' "</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>add Swallowed</action>
          <action>refresh:TRAY</action>
        </button>
      </hbox>
    </vbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml




               ##################################################
               ##                                              ##
               ##               T A S K L I S T                ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Tasklist')'>
    <vbox margin="20">
      <text><label>"'$(gettext 'Show running programs in tray.
NOTE! The tasklist makes the tray stretch horizontaly.')'
      "</label></text>
      <hbox>
        <text xalign="0" space-fill="true" space-expand="true"><label>"'$(gettext 'How programs show up in tray')'"</label></text>
        <comboboxtext space-fill="false" space-expand="false">
          <variable>TASKLIST_STYLE</variable>
          <input file>'$WORKDIR'/tray_TASKLIST_STYLE</input>
          <item>'$(gettext 'Iconify')'</item>
          <item>'$(gettext 'Fixed width')'</item>
          <item>'$(gettext 'Variable width')'</item>
        </comboboxtext>
      </hbox>
      <text><label>""</label></text>
      <hbox>
        <button>
          <label>" '$(gettext 'Set Tasklist')' "</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>add Tasklist</action>
          <action>refresh:TRAY</action>
        </button>
      </hbox>
    </vbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                D E S K T O P S               ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Desktop selector')'>
    <vbox margin="20">
      <text><label>"'$(gettext "Define how many virtual desktops to show.")'
      "</label></text>
      <hbox>
        <vbox>
          <hbox>
            <text><label>"'$(gettext 'Rows')'"</label></text>
            <comboboxtext width-request="50">
              <variable>PAGER_ROWS</variable>
              <input file>'$WORKDIR'/tray_PAGER_ROWS</input>
              <item>1</item><item>2</item>
              <action>add Pager</action>
              <action>refresh:TRAY</action>
            </comboboxtext>
          </hbox>
          <hbox>
            <text><label>"'$(gettext 'Columns')'"</label></text>
            <comboboxtext width-request="50">
              <variable>PAGER_COLUMNS</variable>
              <input file>'$WORKDIR'/tray_PAGER_COLUMNS</input>
              <item>1</item><item>2</item><item>3</item><item>4</item><item>5</item>
              <action>add Pager</action>
              <action>refresh:TRAY</action>
            </comboboxtext>
          </hbox>
        </vbox>
        <button image-position="2">
          <label>"'$(gettext 'Define desktops')'"</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>[ ! "`grep -F "Pager" '$WORKDIR'/tray_rc`" ] && add "Pager"</action>
          <action>refresh:TRAY</action>
        </button>
      </hbox>
    </vbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                   C L O C K                  ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Clock')'>
    <vbox margin="20">
      <text><label>"'$(gettext 'The clock font is set by the graphical theme.')'
      "</label></text>
          <hbox>
            <text><label>'$(gettext 'Clock format')'</label></text>
            <comboboxtext width-request="150">
              <variable>CLOCK</variable>
              <item>""</item>
              <item>12:mm</item>
              <item>12:mm AM</item>
              <item>12:mm:ss</item>
              <item>12:mm:ss AM</item>
              <item>24:mm</item>
              <item>24:mm:ss</item>
            </comboboxtext>
          </hbox>
          <hbox>
            <text><label>'$(gettext 'Press-action')'</label></text>
            <comboboxentry width-request="150">
              <variable>CLOCK_ACTION</variable>
              <item>'$(gettext 'Clock menu')'</item>
              <item>'$(gettext 'Clock tools')'</item>' >> $WORKDIR/tray_xml
              [ "`which minixcal`" ]			&& echo '<item>minixcal</item>' >> $WORKDIR/tray_xml
              [ "`which pclock`" ]				&& echo '<item>pclock</item>' >> $WORKDIR/tray_xml
              [ "`which set-time-for-puppy`" ]	&& echo '<item>set-time-for-puppy</item>' >> $WORKDIR/tray_xml
            echo '</comboboxentry>
          </hbox>
        <text><label>""</label></text>
        <hbox>
        <button>
          <label>"'$(gettext 'Add / update clock')'"</label>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <action>add Clock</action>
          <action>refresh:TRAY</action>
        </button>
      </hbox>
    </vbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml

   
               ##################################################
               ##                                              ##
               ##                 B U T T O N                  ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Tray buttons')'>
    <vbox margin="30">
      <text><label>"'$(gettext 'Add predefined function button')'"</label></text>
      <comboboxtext>
        <variable>FUNCTION</variable>
        <item>""</item>
        <item>'$(gettext 'Show desktop')'</item>' >> $WORKDIR/tray_xml
        [ "`which xlock`" ] && echo "<item>$(gettext 'Lock screen')</item>" >> $WORKDIR/tray_xml
        [ -s "/usr/share/doc/index.html" ] && echo "<item>$(gettext 'Puppy Help')</item>" >> $WORKDIR/tray_xml
        echo '<item>'$(gettext 'Leave')'</item>
        <item>'$(gettext 'Shutdown NOW')'</item>
        <item>'$(gettext 'Reboot NOW')'</item>
        <item>'$(gettext 'Edit tray')'</item>
        '$ITEMS_THEMES'
        <action>add "Function button"</action>
        <action>refresh:TRAY</action>
      </comboboxtext>
      <text><label>"
      '$(gettext 'Add shortcut-button to installed app')'"</label></text>
      <comboboxtext>
        <variable>PROGRAM</variable>
        <input file>'$WORKDIR'/tray_applist</input>
        <action>add "Program button"</action>
        <action>refresh:TRAY</action>
      </comboboxtext>
    </vbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml


               ##################################################
               ##                                              ##
               ##                  S P A C E R                 ##
               ##                                              ##
               ##################################################

echo '
<vbox>
  <frame '$(gettext 'Spacer')'>
    <vbox>
      <text><label>"'$(gettext 'Set the height/width to 0, and the spacer will fill.')'
      "</label></text>
    </vbox>
    <vbox margin="30">
      <hbox>
        <text><label>"'$(gettext 'Width')' "</label></text>
        <spinbutton range-max="999" width-request="47">
          <variable>SPACER</variable>
          <default>10</default>
        </spinbutton>
      </hbox>
      <hbox>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon add`"'
          <label>" '$(gettext 'Add empty gap')' "</label>
          <action>add Spacer</action>
          <action>refresh:TRAY</action>
        </button>
      </hbox>
    </vbox>
  </frame>
</vbox>' >> $WORKDIR/tray_xml




        echo '<variable>GUI_OPTIONS</variable>
        <input file>'$WORKDIR'/notebook_page</input>
      </notebook>
    </vbox>
  </hbox>



    <frame '$(gettext 'Tray setup (left/top to right/bottom)')'>
      <hbox space-fill="true" space-expand="true">
        <vbox space-fill="true" space-expand="true">
        <tree headers_visible="false" selection-mode="1" space-fill="true" space-expand="true">
          <label>""</label>
          <variable>TRAY</variable>
          <input file>'$WORKDIR'/tray_rc</input>
          <action signal="button-release-event">echo $TRAY > '$WORKDIR'/tray_treeselect</action>
          <action signal="button-release-event" condition="command_is_true([[ `grep -Fi \"<\" <<< \"$TRAY\"` ]] && echo true)">enable:BOX_TREEBUTTONS</action>
          <action signal="button-release-event" condition="command_is_true([[ `grep -Fi \"<Tray \" <<< \"$TRAY\"` ]] && echo true)">disable:BOX_TREEBUTTONS</action>
        </tree>
        </vbox>
        <vbox space-fill="false" space-expand="false">
          <button space-fill="false" space-expand="false" tooltip-text="'$(gettext 'Clear list / Delete tray')'">
            '"`/usr/lib/gtkdialog/xml_button-icon clear`"'
            <action>. /usr/lib/gtkdialog/box_yesno "'$(gettext 'Delete tray')'" "'$(gettext 'Do you want to delete this tray?')'"; if [ "$EXIT" = "yes" ]; then echo -n > '$WORKDIR'/tray_rc; add Tray; fi</action>
            <action>refresh:TRAY</action>
            <action>apply</action>
            <action>refresh:ICON1</action>
            <action>refresh:ICON2</action>
            <action>refresh:ICON3</action>
            <action>refresh:ICON4</action>
          </button>
          <vbox>
          <button space-fill="false" space-expand="false">
            '"`/usr/lib/gtkdialog/xml_button-icon remove`"'
            <action>if [ "$TRAY" ]; then grep -vF "$TRAY" '$WORKDIR'/tray_rc > '$WORKDIR'/tray_tmp; mv '$WORKDIR'/tray_tmp '$WORKDIR'/tray_rc; fi</action>
            <action>refresh:TRAY</action>
            <action>disable:BOX_TREEBUTTONS</action>
          </button>
          <button space-fill="false" space-expand="false">
            '"`/usr/lib/gtkdialog/xml_button-icon go-up`"'
            <action>move_up</action>
            <action>refresh:TRAY</action>
          </button>
          <button space-fill="false" space-expand="false">
            '"`/usr/lib/gtkdialog/xml_button-icon go-down`"'
            <action>move_down</action>
            <action>refresh:TRAY</action>
          </button>
            <variable>BOX_TREEBUTTONS</variable>
            <sensitive>false</sensitive>
          </vbox>
        </vbox>
      </hbox>
    </frame>

  </hbox>

  <hbox space-expand="false" space-fill="false">
    <comboboxentry editable="false" space-expand="false" space-fill="false">
      <variable>TEMPLATE</variable>
      <default>'$(gettext 'Load setup')'</default>
      <input file>'$WORKDIR'/tray_templates</input>
      <action>load "$TEMPLATE"</action>
      <action>refresh:ICON1</action>
      <action>refresh:ICON2</action>
      <action>refresh:ICON3</action>
      <action>refresh:ICON4</action>
      <action>refresh:TRAY</action>
    </comboboxentry>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon delete`"'
      <action>delete "$TEMPLATE"</action>
      <action>refresh:TEMPLATE</action>
    </button>
    <button space-expand="false" space-fill="false">
      '"`/usr/lib/gtkdialog/xml_button-icon save`"'
      <label>"'$(gettext 'Save setup')'"</label>
      <action>save "$TEMPLATE"</action>
      <action>refresh:TEMPLATE</action>
    </button>
    <text space-expand="true" space-fill="true"><label>""</label></text>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <label>'$(gettext 'Cancel')'</label>
      <action>exit:EXIT</action>
    </button>
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
      <label>'$(gettext 'Apply')'</label>
      <action>apply</action>
      <action>refresh:ICON1</action>
      <action>refresh:ICON2</action>
      <action>refresh:ICON3</action>
      <action>refresh:ICON4</action>
    </button>
    '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
  
</vbox>
<action signal="show">kill '$XPID'</action>
</window>' >> $WORKDIR/tray_xml

. /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
gtkdialog -f $WORKDIR/tray_xml
