#!/bin/bash
#pTheme
#Copyright - GPL 2014,2015
#Sigmund Berglund

IFS=$'\n'
export TEXTDOMAIN=ptheme
export PID_PTHEME=$$

export WORKDIR=$HOME/.ptheme/tmp/ptheme
export CONFDIR=/usr/share/ptheme/globals
[ ! -d $WORKDIR ] && mkdir -p $WORKDIR
[ ! -d $CONFDIR ] && mkdir -p $CONFDIR
echo -n > $WORKDIR/save_name
echo -n > $WORKDIR/save_icon
[ ! -d $HOME/.config/ptheme ] && mkdir -p $HOME/.config/ptheme

#build menus
MENUITEMS_ICONS="$(find /usr/local/lib/X11/themes/ -mindepth 1 -maxdepth 1 -type d -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-icons</action><action>refresh:PTHEME_ICONS</action></menuitem>" | sort &)"
MENUITEMS_ROX_PIN="$(find /usr/share/ptheme/rox_pinboard -mindepth 1 -maxdepth 1 -type f -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-rox_pin</action><action>refresh:PTHEME_ROX_PIN</action></menuitem>" | sort &)"
MENUITEMS_GTK="$(find /usr/share/themes/ -mindepth 1 -maxdepth 1 -type d -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-gtk</action><action>refresh:PTHEME_GTK</action></menuitem>" | sort &)"
MENUITEMS_JWM_COLOR="$(find /usr/share/jwm/themes/ -mindepth 1 -maxdepth 1 -iname "*jwmrc" -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-jwm</action><action>refresh:PTHEME_JWM_COLOR</action></menuitem>" | sort | sed -e 's/-jwmrc//g' &)"
MENUITEMS_JWM_TRAY="$(find /usr/share/jwm/tray_templates -mindepth 1 -maxdepth 1 -type d -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-jwm_tray</action><action>refresh:PTHEME_JWM_TRAY</action></menuitem>" | sort &)"
MENUITEMS_JWM_BUTTONS="$(find /usr/share/jwm/themes_window_buttons/ -mindepth 1 -maxdepth 1 -type d -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-jwm_buttons</action><action>refresh:PTHEME_JWM_BUTTONS</action></menuitem>" | sort &)"
MENUITEMS_MOUSE="$(find $HOME/.icons -mindepth 1 -maxdepth 1 -type d -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-mouse</action><action>refresh:PTHEME_MOUSE</action></menuitem>" | sort | grep -vE "ROX|default" &)"
MENUITEMS_GTKDIALOG="$(find /usr/share/ptheme/gtkdialog/ -mindepth 1 -maxdepth 1 -type f -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-gtkdialog</action><action>refresh:PTHEME_GTKDIALOG</action></menuitem>" | sort &)"
MENUITEMS_WALL="$(find /usr/share/backgrounds -mindepth 1 -maxdepth 1 -type f -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-wall</action><action>refresh:PTHEME_WALL</action></menuitem>" | sort)"
MENUITEMS_PWIDGETS_PROFILE="$(find $HOME/.pwidgets/profiles -mindepth 1 -maxdepth 1 -type d -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-pwidgets_profile</action><action>refresh:PTHEME_PWIDGETS_PROFILE</action></menuitem>" | sort)"
MENUITEMS_PWIDGETS_THEME="$(find /usr/local/pwidgets/widgets/themes -mindepth 1 -maxdepth 1 -type d -printf "<menuitem label=\"%f\"><action>echo %f > $WORKDIR/ptheme-pwidgets_theme</action><action>refresh:PTHEME_PWIDGETS_THEME</action></menuitem>" | sort)"



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


save (){
	find /usr/share/ptheme/globals -mindepth 1 -maxdepth 1 -type f -printf "<item>%f</item>\n" | sort > $WORKDIR/globals
	#check if something is defined
	[ -s $WORKDIR/save_name ] && DEFAULT_NAME="$(<$WORKDIR/save_name)" || DEFAULT_NAME="$(gettext 'My Puppy theme')"
	[ ! -s $WORKDIR/save_icon ] && echo '/usr/share/pixmaps/puppy/puppy_theme_generic.svg' > $WORKDIR/save_icon
	TMP="$PTHEME_ICONS$PTHEME_ROX_PIN$PTHEME_GTK$PTHEME_JWM_COLOR$PTHEME_JWM_BUTTONS$PTHEME_JWM_TRAY$PTHEME_JWM_SIZE$PTHEME_WALL$PTHEME_MOUSE$PTHEME_GTKDIALOG$PTHEME_PWIDGETS_PROFILE$PTHEME_PWIDGETS_THEME"
	if [ ! "$TMP" ]; then
		. /usr/lib/gtkdialog/box_ok "$(gettext 'pTheme')" error "$(gettext 'Nothing to save')"
		exit
	fi
	#reset gtk-theme
	export GTK2_RC_FILES="`grep -m 1 gtkrc $HOME/.gtkrc-2.0 | cut -d'\"' -f2 2> /dev/null`"
	export savebox='
	<window title="pTheme - '$(gettext 'Save')'" icon-name="gtk-save" resizable="false">
	<vbox>
	  <frame Save>
	    <hbox>
	      <text><label>'$(gettext 'Theme name')'</label></text>
	      <comboboxentry>
	        <variable>SAVEFILE</variable>
	        '"$(find /usr/share/ptheme/globals -mindepth 1 -maxdepth 1 -type f -printf "<item>%f</item>" | sort)"'
	        <default>'$DEFAULT_NAME'</default>
	        <action>grep -F "ICON=" '$CONFDIR'/"$SAVEFILE" | cut -d\" -f2 > '$WORKDIR'/save_icon</action>
	        <action>refresh:ICON</action>
	      </comboboxentry>
	    </hbox>
	    <hbox>
	      <text><label>'$(gettext 'Icon')'</label></text>
	      <entry accept="file" width-request="300" fs-folder="'$HOME'" fs-title="pTheme - '$(gettext 'Theme icon')'">
	        <variable>ICON</variable>
	        <input file>'$WORKDIR'/save_icon</input>
	      </entry>
	      <button>
	        <input file stock="gtk-open"></input>
	        <action type="fileselect">ICON</action>
	      </button>
	    </hbox>
	  </frame>
	  <hbox space-expand="false" space-fill="false">
	    <button>
	      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
	      <label>'$(gettext 'Cancel')'</label>
	      <action type="exit">cancel</action>
	    </button>
	    <button can-default="true" has-default="true" use-stock="true">
	      '"`/usr/lib/gtkdialog/xml_button-icon save`"'
	      <label>'$(gettext 'Save')'</label>
	      <action type="exit">save</action>
	    </button>
	  </hbox>
	</vbox>
	</window>'
	for STATEMENTS in "$(gtkdialog -p savebox --center)"; do
		eval $STATEMENTS 2> /dev/null
	done
	if [ "$EXIT" = "save" ]; then
		if [ -f "$CONFDIR/$SAVEFILE" ]; then
			. /usr/lib/gtkdialog/box_yesno "pTheme" "$(gettext 'Theme already exist.')" "<b>$(gettext 'Do you want to overwrite?')</b>"
			[ $EXIT = "no" ] && exit
		fi
		echo "ICON=\"$ICON\"
PTHEME_ICONS=\"$PTHEME_ICONS\"
PTHEME_ROX_PIN=\"$PTHEME_ROX_PIN\"
PTHEME_GTK=\"$PTHEME_GTK\"
PTHEME_JWM_COLOR=\"$PTHEME_JWM_COLOR\"
PTHEME_JWM_BUTTONS=\"$PTHEME_JWM_BUTTONS\"
PTHEME_JWM_TRAY=\"$PTHEME_JWM_TRAY\"
PTHEME_JWM_SIZE=\"$PTHEME_JWM_SIZE\"
PTHEME_WALL=\"$PTHEME_WALL\"
PTHEME_MOUSE=\"$PTHEME_MOUSE\"
PTHEME_GTKDIALOG=\"$PTHEME_GTKDIALOG\"
PTHEME_PWIDGETS_PROFILE=\"$PTHEME_PWIDGETS_PROFILE\"
PTHEME_PWIDGETS_THEME=\"$PTHEME_PWIDGETS_THEME\"
" > $CONFDIR/"$SAVEFILE"
		#reload gui to show changes
		ptheme & #start a new instance to see the changes in libstardust
		TMP="`ps -eo pid,command`"
		for I in `echo "$TMP" | grep -F $PID_PTHEME | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
	fi
}

set_theme (){
	/usr/lib/gtkdialog/box_splash -close never -fontsize large -icon_width 60 -icon /usr/share/pixmaps/puppy/puppy_config.svg -text "$(gettext 'Changing theme')" &
	XPID=$!
	#icons
	if [ "$PTHEME_ICONS" ] && [ -d /usr/local/lib/X11/themes/"$PTHEME_ICONS" ]; then
		icon_switcher -a "$PTHEME_ICONS"
		SWITCH_THEME=true
		UPDATE_ROX=true
		UPDATE_JWM=true
	fi
	#rox pinborad - icon placement
	if [ "$PTHEME_ROX_PIN" ] && [ -f /usr/share/ptheme/rox_pinboard/"$PTHEME_ROX_PIN" ]; then
		TMP_BACKDROP="`grep -F '<backdrop ' $HOME/Choices/ROX-Filer/PuppyPin`"
		TMP_DRIVEICONS="`grep -F '.pup_event' $HOME/Choices/ROX-Filer/PuppyPin`"
		echo -e '<?xml version="1.0"?>\n<pinboard>' > $WORKDIR/tmp_puppypin
		echo "$TMP_BACKDROP" >> $WORKDIR/tmp_puppypin
		cat /usr/share/ptheme/rox_pinboard/"$PTHEME_ROX_PIN" >> $WORKDIR/tmp_puppypin
		echo "$TMP_DRIVEICONS" >> $WORKDIR/tmp_puppypin
		echo '</pinboard>' >> $WORKDIR/tmp_puppypin
		mv $WORKDIR/tmp_puppypin $HOME/Choices/ROX-Filer/PuppyPin
		SWITCH_THEME=true
		UPDATE_ROX=true
	fi
	[ "$UPDATE_ROX" = "true" ] && rox -p $HOME/Choices/ROX-Filer/PuppyPin
	#cursor
	if [ "$PTHEME_MOUSE" ] && [ -d $HOME/.icons/$PTHEME_MOUSE ]; then
		if [ "`echo "$PTHEME_MOUSE" | grep 'ORIGINAL THEME'`" ]; then
			ln -snf $HOME/.icons/default/default_nonsense $HOME/.icons/default #make broken link to get original pointer
		else
			ln -snf $HOME/.icons/$PTHEME_MOUSE $HOME/.icons/default
		fi
		SWITCH_THEME=true
	fi
	#wallpaper
	if [ "$PTHEME_WALL" ] && [ -f "/usr/share/backgrounds/$PTHEME_WALL" ]; then
		echo "/usr/share/backgrounds/$PTHEME_WALL" > $HOME/.config/wallpaper/bg_img
		echo 'Stretch' > $HOME/.config/wallpaper/backgroundmode
		"/usr/local/apps/Wallpaper/set_bg" "/usr/share/backgrounds/$PTHEME_WALL"
		SWITCH_THEME=true
	fi
	#gtk
	if [ "$PTHEME_GTK" ] && [ -d "/usr/share/themes/$PTHEME_GTK" ]; then
		export GTK2_RC_FILES="$HOME/.gtkrc-2.0" #reset gtk-theme
		ptheme_gtk -t "$PTHEME_GTK" &
		SWITCH_THEME=true
	fi
	#jwm
	if [ "$PTHEME_JWM_COLOR" ] && [ -f /usr/share/jwm/themes/"$PTHEME_JWM_COLOR"-jwmrc ]; then
		/usr/local/jwm_config/theme_switcher -a "$PTHEME_JWM_COLOR"
		UPDATE_JWM=true
		SWITCH_THEME=true
	fi
	if [ "$PTHEME_JWM_BUTTONS" ] && [ -d /usr/share/jwm/themes_window_buttons/"$PTHEME_JWM_BUTTONS" ]; then
		/usr/local/jwm_config/window_buttons -a "$PTHEME_JWM_BUTTONS"
		UPDATE_JWM=true
		SWITCH_THEME=true
	fi
	if [ "$PTHEME_JWM_TRAY" ] && [ -d /usr/share/jwm/tray_templates/"$PTHEME_JWM_TRAY" ]; then
		cp -f /usr/share/jwm/tray_templates/"$PTHEME_JWM_TRAY"/jwmrc-tray* $HOME/.jwm/
		[ -s /usr/share/jwm/tray_templates/"$PTHEME_JWM_TRAY"/install ] && /usr/share/jwm/tray_templates/"$PTHEME_JWM_TRAY"/install
		echo "$PTHEME_JWM_TRAY" > $HOME/.jwm/tmp/tray_active_preset
		UPDATE_JWM=true
		SWITCH_THEME=true
	fi
	if [ "$PTHEME_JWM_SIZE" ]; then
		case $PTHEME_JWM_SIZE in
		small)
			#menu height 16
			if [ -s $HOME/.jwm/menuheights ]; then
				rm $HOME/.jwm/menuheights
				fixmenus
				UPDATE_JWM=true
				SWITCH_THEME=true
			fi
			;;
		normal)
			#menu height 24
			if [ ! "`grep -F '24' $HOME/.jwm/menuheights`" ]; then
				echo "MENHEIGHT=24" > $HOME/.jwm/menuheights
				fixmenus
				UPDATE_JWM=true
				SWITCH_THEME=true
			fi
			;;
		large)
			#menu height 32
			if [ ! "`grep -F '32' $HOME/.jwm/menuheights`" ]; then
				echo "MENHEIGHT=32" > $HOME/.jwm/menuheights
				fixmenus
				UPDATE_JWM=true
				SWITCH_THEME=true
			fi
			;;
		huge)
			#menu height 40
			if [ ! "`grep -F '40' $HOME/.jwm/menuheights`" ]; then
				echo "MENHEIGHT=40" > $HOME/.jwm/menuheights
				fixmenus
				UPDATE_JWM=true
				SWITCH_THEME=true
			fi
			;;
		esac
	fi
	#kill splash
	kill $XPID
	#restart jwm
	if [ "$UPDATE_JWM" = "true" ]; then
		[ -s /usr/local/jwm_config/jwm_restart ] && /usr/local/jwm_config/jwm_restart || jwm -restart
	fi
	#gtkdialog
	if [ "$PTHEME_GTKDIALOG" ] && [ -f "/usr/share/ptheme/gtkdialog/$PTHEME_GTKDIALOG" ]; then
		cp -f "/usr/share/ptheme/gtkdialog/$PTHEME_GTKDIALOG" $HOME/.config/ptheme/gtkdialog_active
		[ ! "$CLI" = "true" ] && ptheme & #start a new instance to see the changes in libstardust
		TMP="`ps -eo pid,command`"
		for I in `echo "$TMP" | grep -F $PID_PTHEME | awk '{print $1}'`; do kill -9 $I 2> /dev/null; done
		SWITCH_THEME=true
	fi 
	#pWidgets
	if [ "$PTHEME_PWIDGETS_PROFILE" ] && [ -d $HOME/.pwidgets/profiles/"$PTHEME_PWIDGETS_PROFILE" ]; then
		cp -f $HOME/.pwidgets/profiles/$PTHEME_PWIDGETS_PROFILE/pwidgetsrc $HOME/.pwidgets/pwidgetsrc
		cp -f $HOME/.pwidgets/profiles/$PTHEME_PWIDGETS_PROFILE/pwidgets_list $HOME/.pwidgets/tmp/pwidgets_list
		echo "$PTHEME_PWIDGETS_PROFILE" > $HOME/.pwidgets/tmp/PROFILE
		/usr/local/pwidgets/func -apply
		UPDATE_PWIDGETS=true
		SWITCH_THEME=true
	fi
	if [ "$PTHEME_PWIDGETS_THEME" ] && [ -d /usr/local/pwidgets/widgets/themes/"$PTHEME_PWIDGETS_THEME" ]; then
		TMP="`grep THEME $HOME/.pwidgets/pwidgetsrc`"
		sed -i "s/$TMP/export THEME=$PTHEME_PWIDGETS_THEME/" $HOME/.pwidgets/pwidgetsrc
		/usr/local/pwidgets/func -set_theme
		UPDATE_PWIDGETS=true
		SWITCH_THEME=true
	fi
	if [ "$UPDATE_PWIDGETS" = "true" ]; then
		fixwidgets
	fi

	if [ "$SWITCH_THEME" != "true" ]; then
		/usr/lib/gtkdialog/box_ok "pTheme" error "$(gettext "Theme modules aren't set, or not valid")"
	fi
}

update_preset (){
	. $CONFDIR/"$1"
	echo "$PTHEME_ICONS"			> $WORKDIR/ptheme-icons
	echo "$PTHEME_ROX_PIN"			> $WORKDIR/ptheme-rox_pin
	echo "$PTHEME_GTK"				> $WORKDIR/ptheme-gtk
	echo "$PTHEME_JWM_COLOR"		> $WORKDIR/ptheme-jwm
	echo "$PTHEME_JWM_BUTTONS"		> $WORKDIR/ptheme-jwm_buttons
	echo "$PTHEME_JWM_TRAY"			> $WORKDIR/ptheme-jwm_tray
	echo "$PTHEME_JWM_SIZE"			> $WORKDIR/ptheme-jwm_size
	echo "$PTHEME_WALL" 			> $WORKDIR/ptheme-wall
	echo "$PTHEME_MOUSE"			> $WORKDIR/ptheme-mouse
	echo "$PTHEME_GTKDIALOG"		> $WORKDIR/ptheme-gtkdialog
	echo "$PTHEME_PWIDGETS_PROFILE"	> $WORKDIR/ptheme-pwidgets_profile
	echo "$PTHEME_PWIDGETS_THEME"	> $WORKDIR/ptheme-pwidgets_theme
}

#taskbar_height (){
#	HEIGHT_NEW=$1
#	#find trayrc file with tasklist
#	RCFILE="`grep -Fil '<TaskList ' $HOME/.jwm/* | grep -vF '.bak' | head -n1`"
#	TRAY_OLD="`grep -Fi '<Tray ' $RCFILE`"
#	HEIGHT_OLD="`grep -Eio 'height=".*"' <<< $TRAY_OLD | cut -d'"' -f2`"
#	TRAY_NEW="`grep -Fi '<Tray ' $RCFILE | sed "s%height=\\\"$HEIGHT_OLD\\\"%height=\\\"$HEIGHT_NEW\\\"%"`"
#	sed -i "s%$TRAY_OLD%$TRAY_NEW%" $RCFILE
#}

export -f save set_theme update_preset #taskbar_height

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

#parameters
while [ $# != 0 ]; do
	I=1
	while [ $I -le `echo $# | wc -c` ]; do 
		case $1 in
			-p) PRESET="$2"; . $CONFDIR/$PRESET; export CLI=true; set_theme; exit;;
			-h|--help)
echo 'Usage: ptheme [OPTION]

Options
  -p PRESET   Activate global theme
  -h          Show this help message'; exit;;
		esac
		shift
		I=$[$I+1]
	done
done

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

#clean up
echo -n > $WORKDIR/ptheme-icons
echo -n > $WORKDIR/ptheme-rox_pin
echo -n > $WORKDIR/ptheme-gtk
echo -n > $WORKDIR/ptheme-jwm
echo -n > $WORKDIR/ptheme-jwm_buttons
echo -n > $WORKDIR/ptheme-jwm_tray
echo -n > $WORKDIR/ptheme-jwm_size
echo -n > $WORKDIR/ptheme-wall
echo -n > $WORKDIR/ptheme-mouse
echo -n > $WORKDIR/ptheme-gtkdialog
echo -n > $WORKDIR/ptheme-pwidgets_profile
echo -n > $WORKDIR/ptheme-pwidgets_theme

XML_GLOBALS=''
while read I in; do
	ICON="$(grep -F 'ICON=' /usr/share/ptheme/globals/$I | cut -d= -f2 | tr -d '"')"
	[ ! "$ICON" ] && ICON=/usr/share/pixmaps/puppy/puppy_theme_generic.svg
	XML_GLOBALS=${XML_GLOBALS}'
	<button image-position="2" relief="2">
	  <label>"'$I'"</label>
	  '"`/usr/lib/gtkdialog/xml_button-icon "$ICON" huge`"'
	  <action>update_preset "'$I'"</action>
	  <action>echo "'$I'" > '$WORKDIR'/save_name</action>
	  <action>echo "'$ICON'" > '$WORKDIR'/save_icon</action>
	  <action>activate:UPDATER</action>
	</button>'
done <<< "$(find /usr/share/ptheme/globals -mindepth 1 -maxdepth 1 -type f -printf "%f\n" | sort)"


S='
<window title="pTheme" icon-name="gtk-preferences" default-height="550" default-width="520">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed "puppy_theme.svg" 60 "$(gettext 'A <b>global theme</b> affects all themable modules in Puppy. Choose either a global theme or dig deeper into one of the modules.')"`"' 
  <button visible="false">
    <variable>UPDATER</variable>
    <action>refresh:PTHEME_ICONS</action>
    <action>refresh:PTHEME_ROX_PIN</action>
    <action>refresh:PTHEME_GTK</action>
    <action>refresh:PTHEME_JWM_COLOR</action>
    <action>refresh:PTHEME_JWM_BUTTONS</action>
    <action>refresh:PTHEME_JWM_TRAY</action>
    <action>refresh:PTHEME_JWM_SIZE</action>
    <action>refresh:PTHEME_WALL</action>
    <action>refresh:PTHEME_MOUSE</action>
    <action>refresh:PTHEME_GTKDIALOG</action>
    <action>refresh:PTHEME_PWIDGETS_PROFILE</action>
    <action>refresh:PTHEME_PWIDGETS_THEME</action>
  </button>
  <vbox space-expand="true" space-fill="true">
    <vbox space-expand="false" space-fill="false">
      <hbox margin="5" height="130" scrollable="true" space-expand="false" space-fill="false">
      '$XML_GLOBALS'
      </hbox>
      </vbox>
  <vbox space-expand="true" space-fill="true">
    <frame '$(gettext 'Theme modules')'>
      <vbox scrollable="true" margin="5" shadow-type="0" space-expand="true" space-fill="true">

##  W A L L P A P E R 

   <hbox space-expand="true" space-fill="true">
     <menubar space-expand="true" space-fill="true">
       <menu label="'$(gettext 'Wallpaper')'" stock="gtk-arrow_down">
         '$MENUITEMS_WALL'
         <menuitem label="">
           <action>echo "" > $WORKDIR/ptheme-wall</action>
           <action>refresh:PTHEME_WALL</action>
         </menuitem>
       </menu>
     </menubar>
     <entry editable="false" width-request="150" space-expand="false" space-fill="false">
       <variable>PTHEME_WALL</variable>
      <input>cat '$WORKDIR'/ptheme-wall</input>
     </entry>
     <button space-expand="false" space-fill="false">
       <label>'$(gettext 'More')'</label>
       '"`/usr/lib/gtkdialog/xml_button-icon wallpaper`"'
       <action>wallpaper &</action>
     </button>
   </hbox>

##  G T K 

   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Program decoration (gtk)')'" stock="gtk-arrow_down">
        '$MENUITEMS_GTK'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-gtk</action>
          <action>refresh:PTHEME_GTK</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_GTK</variable>
     <input>cat '$WORKDIR'/ptheme-gtk</input>
    </entry>
    <button space-expand="false" space-fill="false">
     <label>'$(gettext 'More')'</label>
     '"`/usr/lib/gtkdialog/xml_button-icon button`"'
     <action>ptheme_gtk &</action>
    </button>
   </hbox>'

##  J W M

   [ "`grep -F 'jwm' <<< $(ps -eo pid,command)`" ] && S=$S'
   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Colors of Window / Tray / Menu')'" stock="gtk-arrow_down">
        '$MENUITEMS_JWM_COLOR'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-jwm</action>
          <action>refresh:PTHEME_JWM_COLOR</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_JWM_COLOR</variable>
     <input>cat '$WORKDIR'/ptheme-jwm</input>
    </entry>
    <button space-expand="false" space-fill="false">
     <label>'$(gettext 'More')'</label>
     '"`/usr/lib/gtkdialog/xml_button-icon windows`"'
     <action>/usr/local/jwm_config/theme_switcher &</action>
    </button>
   </hbox>
   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Window buttons')'" stock="gtk-arrow_down">
        '$MENUITEMS_JWM_BUTTONS'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-jwm_buttons</action>
          <action>refresh:PTHEME_JWM_BUTTONS</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_JWM_BUTTONS</variable>
     <input>cat '$WORKDIR'/ptheme-jwm_buttons</input>
    </entry>
    <button space-expand="false" space-fill="false">
     <label>'$(gettext 'More')'</label>
     '"`/usr/lib/gtkdialog/xml_button-icon windows_buttons`"'
     <action>/usr/local/jwm_config/window_buttons &</action>
    </button>
   </hbox>

   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Tray templates')'" stock="gtk-arrow_down">
        '$MENUITEMS_JWM_TRAY'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-jwm_tray</action>
          <action>refresh:PTHEME_JWM_TRAY</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_JWM_TRAY</variable>
     <input file>'$WORKDIR'/ptheme-jwm_tray</input>
    </entry>
    <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon desktop_tray`"'
     <label>'$(gettext 'More')'</label>
     <action>/usr/local/jwm_config/tray &</action>
    </button>
   </hbox>


   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Menu size')'" stock="gtk-arrow_down">
        <menuitem label="small">
          <action>echo small > $WORKDIR/ptheme-jwm_size</action>
          <action>refresh:PTHEME_JWM_SIZE</action>
        </menuitem>
        <menuitem label="normal">
          <action>echo normal > $WORKDIR/ptheme-jwm_size</action>
          <action>refresh:PTHEME_JWM_SIZE</action>
        </menuitem>
        <menuitem label="large">
          <action>echo large > $WORKDIR/ptheme-jwm_size</action>
          <action>refresh:PTHEME_JWM_SIZE</action>
        </menuitem>
        <menuitem label="huge">
          <action>echo huge > $WORKDIR/ptheme-jwm_size</action>
          <action>refresh:PTHEME_JWM_SIZE</action>
        </menuitem>
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-jwm_size</action>
          <action>refresh:PTHEME_JWM_SIZE</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_JWM_SIZE</variable>
     <input>cat '$WORKDIR'/ptheme-jwm_size</input>
    </entry>
    <button space-expand="false" space-fill="false">
     <label>'$(gettext 'More')'</label>
     '"`/usr/lib/gtkdialog/xml_button-icon size`"'
     <action>cd /usr/local/jwm_config; ./menu &</action>
    </button>
   </hbox>'

##  I C O N S 

   S=$S'
   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Icon theme')'" stock="gtk-arrow_down">
        '$MENUITEMS_ICONS'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-icons</action>
          <action>refresh:PTHEME_ICONS</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_ICONS</variable>
     <input>cat '$WORKDIR'/ptheme-icons</input>
    </entry>
    <button space-expand="false" space-fill="false">
     <label>'$(gettext 'More')'</label>
     '"`/usr/lib/gtkdialog/xml_button-icon desktop_icons`"'
     <action>icon_switcher &</action>
    </button>
   </hbox>'

##  R O X   P I N

   [ "`grep -Fi 'rox' <<< $(ps -eo pid,command)`" ] && S=$S'
   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Icons on desktop')'" stock="gtk-arrow_down">
        '$MENUITEMS_ROX_PIN'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-rox_pin</action>
          <action>refresh:PTHEME_ROX_PIN</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_ROX_PIN</variable>
     <input>cat '$WORKDIR'/ptheme-rox_pin</input>
    </entry>
    <button space-expand="false" space-fill="false">
     <label>'$(gettext 'More')'</label>
     '"`/usr/lib/gtkdialog/xml_button-icon desktop_icons`"'
     <action>/usr/sbin/ptheme_roxpin &</action>
    </button>
   </hbox>'


##  M O U S E

   S=$S'
   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Mouse pointer')'" stock="gtk-arrow_down">
        '$MENUITEMS_MOUSE'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-mouse</action>
          <action>refresh:PTHEME_MOUSE</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_MOUSE</variable>
     <input>cat '$WORKDIR'/ptheme-mouse</input>
    </entry>
    <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon mouse_cursor`"'
     <label>'$(gettext 'More')'</label>
     <action>pcur &</action>
    </button>
   </hbox>

##  G T K D I A L O G

   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'Puppy top-layer')'" stock="gtk-arrow_down">
        '$MENUITEMS_GTKDIALOG'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-gtkdialog</action>
          <action>refresh:PTHEME_GTKDIALOG</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_GTKDIALOG</variable>
     <input>cat '$WORKDIR'/ptheme-gtkdialog</input>
    </entry>
    <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon info`"'
     <label>'$(gettext 'More')'</label>
     <action>echo "'$(gettext 'The Puppy top-layer controls color and size of gui-templates in /usr/lib/gtkdialog/. The config files is found in /usr/share/ptheme/gtkdialog.')'" > /tmp/box_help; /usr/lib/gtkdialog/box_help "pTheme" info.svg &</action>
    </button>
   </hbox>'

##  P W I D G E T S   P R O F I L E 

   [ "`which pwidgets`" ] && S=$S'
   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'pWidgets profile')'" stock="gtk-arrow_down">
        '$MENUITEMS_PWIDGETS_PROFILE'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-pwidgets_profile</action>
          <action>refresh:PTHEME_PWIDGETS_PROFILE</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_PWIDGETS_PROFILE</variable>
     <input>cat '$WORKDIR'/ptheme-pwidgets_profile</input>
    </entry>
    <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon clock`"'
     <label>'$(gettext 'More')'</label>
     <action>pwidgets &</action>
    </button>
   </hbox>
   
##  P W I D G E T S   T H E M E

   <hbox space-expand="true" space-fill="true">
    <menubar space-expand="true" space-fill="true">
      <menu label="'$(gettext 'pWidgets theme')'" stock="gtk-arrow_down">
        '$MENUITEMS_PWIDGETS_THEME'
        <menuitem label="">
          <action>echo "" > $WORKDIR/ptheme-pwidgets_theme</action>
          <action>refresh:PTHEME_PWIDGETS_THEME</action>
        </menuitem>
      </menu>
    </menubar>
    <entry editable="false" width-request="150" space-expand="false" space-fill="false">
     <variable>PTHEME_PWIDGETS_THEME</variable>
     <input>cat '$WORKDIR'/ptheme-pwidgets_theme</input>
    </entry>
    <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon clock`"'
     <label>'$(gettext 'More')'</label>
     <action>/usr/local/pwidgets/widgets/plugins/Theme/Theme &</action>
    </button>
   </hbox>'

   S=$S'</vbox>
  </frame>
  </vbox>
  </vbox>
  <hbox space-expand="false" space-fill="false">
   <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon internet`"'
     <label>'$(gettext 'More themes')'</label>
     <action>defaultbrowser http://murga-linux.com/puppy/viewtopic.php?p=785778#785778 &</action>
   </button>
   <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon save`"'
     <label>'$(gettext 'Save global theme')'</label>
     <action>save</action>
     <action>refresh:PRESET</action>
   </button>
   <text space-expand="true" space-fill="true"><label>""</label></text>
   <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon apply`"'
    <label>'$(gettext 'Apply')'</label>
    <action>set_theme</action>
   </button>
   <button space-expand="false" space-fill="false">
     '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
     <label>'$(gettext 'Quit')'</label>
   </button>
   '"`/usr/lib/gtkdialog/xml_scalegrip`"'
  </hbox>
 </vbox>
<action signal="show">kill '$XPID'</action>
</window>'
echo "$S" | sed -e 's/##.*//' > $WORKDIR/xml_$PID_PTHEME  #I use double hash (##) for comments. Colors are defined as #FF0000

#gtk theme
echo 'pixmap_path "/usr/share/pixmaps/puppy/"
style "icon-style"
{
  stock["gtk-arrow_down"] = {{ "arrow_mini_down.svg", *, *, *}}
}
class "GtkWidget" style "icon-style"' > /tmp/gtkrc_ptheme
export GTK2_RC_FILES=/tmp/gtkrc_ptheme:$HOME/.gtkrc-2.0
#---

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