#!/bin/bash
#this generates xml code for a gtkdialog gui.
#it is NOT a standalone window
#
#usage :
# xml_info BOX-HEIGHT ICON ICON-HEIGHT "text-string1" "text-string2" ...
# xml_info gtk
#
# GTK must be run before gtkdialog is executed to update GTK2_RC_FILES with the image used in background of msgbox
# BOX-HEIGHT is the height of the field.
#  - integer    : the height in pixels
#  - fixed      : autoresize msgbox, but do NOT scale this area
#  - scale or 0 : autoresize msgbox, and scale this area
# ICON can either be a gtk stock icon, a normal raster/vector file or no icon. if no path is set, /usr/share/pixmaps/puppy/ is used
# ICON-HEIGHT is a integer - 0 uses default size of icon
#
#Sigmund Berglund, dec 2013
#GPL

case $1 in
	gtk)
		#. $HOME/.theme_nnnnn #this to allow some kind of theming in the future
		case $2 in
			green|yes|1)
				COLOR1='#BED1C1'
				COLOR2='#006F04'
				;;
			red|no|0)
				COLOR1='#DFCECE'
				COLOR2='#8E0000'
				;;
		esac
		#if no theme-values is defined - use the default
		#this theming doesn't work in combination with all gtk2 themes. 
		#read config if exist
		[ -s $HOME/.config/ptheme/gtkdialog_active ] && . $HOME/.config/ptheme/gtkdialog_active
		[ ! "$XML_INFO_OPACITY" ]	&& XML_INFO_OPACITY=0.5 #background opacity to melt into different global gtk-themes. gradients does not support this.
		[ ! "$XML_INFO_COLOR1" ]	&& XML_INFO_COLOR1='#EDEAC6' #background color
		[ ! "$XML_INFO_COLOR2" ]	&& XML_INFO_COLOR2='#000000' #gradient spot color
		[ ! "$XML_INFO_MODE" ]		&& XML_INFO_MODE="flat" #drawing mode

		#build background svg for info-widget 
		export FLAT='<svg version="1.0"><rect width="100" height="100" style="fill:'${XML_INFO_COLOR1}';fill-opacity:'${XML_INFO_OPACITY}';stroke:none"/></svg>'
		export GRADIENT='
			<svg version="1.1" width="2000" height="1000">
			  <defs>
				<linearGradient id="LG_01" x1="30" y1="95" x2="-50" y2="-125" gradientUnits="userSpaceOnUse">
				  <stop style="stop-color:'${XML_INFO_COLOR1}';stop-opacity:1" offset="0" />
				  <stop style="stop-color:'${XML_INFO_COLOR2}';stop-opacity:1" offset="1" />
				</linearGradient>
			  </defs>
			  <rect width="2000" height="1000" style="fill:'${XML_INFO_COLOR1}';stroke:none"/>
			  <rect
				style="fill:url(#LG_01);fill-opacity:1;stroke-width:0"
				width="250" height="200"/>
			</svg>'
		case $XML_INFO_MODE in gradient) BGSVG="$GRADIENT";; *) BGSVG="$FLAT";; esac
		echo "$BGSVG" > /tmp/xml_info.svg

		#redefine gtk-theme
		echo 'pixmap_path "/tmp/"
		style "Bgsvg" { bg_pixmap[NORMAL] = "xml_info.svg" }
		widget "*BgSVG" style "Bgsvg"
		style "ScaleGrip" { GtkStatusbar::shadow_type = GTK_SHADOW_NONE }
		widget "*ScaleGrip" style "ScaleGrip"' > /tmp/gtkrc_xml_info
		#combine theme stuff with system themes
		if [ ! "$GTK2_RC_FILES" ]; then
			export GTK2_RC_FILES=~/.gtkrc-2.0:/tmp/gtkrc_xml_info
		else
			export GTK2_RC_FILES="$GTK2_RC_FILES:/tmp/gtkrc_xml_info"
		fi
		;;
	fixed)
		HEIGHT='space-expand="false" space-fill="false"'
		;;
	0|scale|' '|'')
		HEIGHT='space-expand="true" space-fill="true"'
		;;
	*[0-9]*)
		[ $1 != 0 ] && HEIGHT="height-request=\"$1\" space-expand=\"false\" space-fill=\"false\""
		;;
	*)
		HEIGHT='space-expand="true" space-fill="true"'
		;;
esac

#define icon
case $2 in
	/*)
		#icon from file
		ICON_INPUT="<input file>${2}</input>"
		case $3 in 0) ICON_HEIGHT="";; *) ICON_HEIGHT="<height>${3}</height>";; esac
		ICON="<vbox><pixmap>${ICON_INPUT}${ICON_HEIGHT}</pixmap></vbox>"
		;;
	0|none|' '|'')
		ICON=""
		;;
	*) 
		case $2 in
			*.svg)
				#icon from puppy stock
				ICON_INPUT="<input file>/usr/share/pixmaps/puppy/${2}</input>"
				case $3 in 0) ICON_HEIGHT="";; *) ICON_HEIGHT="<height>${3}</height>";; esac
				ICON="<vbox><pixmap>${ICON_INPUT}${ICON_HEIGHT}</pixmap></vbox>"
				;;
			*)
				#icon from gtk stock
				ICON_INPUT="<input file stock=\"gtk-${2}\"></input>"
				case $3 in [1-6]) ICON_HEIGHT=" icon_size=\"${3}\"";; 0|*) ICON_HEIGHT="";; esac
				ICON="<vbox><pixmap${ICON_HEIGHT}>${ICON_INPUT}</pixmap></vbox>"
				;;
		esac
esac

if [ "$3" ] || [ "$4" ] || [ "$5" ]; then #generate XML code
	echo '
	<notebook name="BgSVG" show-tabs="false" '${HEIGHT}'>
	  <hbox '${HEIGHT}'>
		<vbox space-expand="false" space-fill="false">
		  <hbox border-width="10">
			'${ICON}'
			<text width-request="5"><label>""</label></text>
			<vbox space-expand="false" space-fill="false">'
			  [ "$4" ] && echo '<text xalign="0" use-markup="true"><label>"'$4'"</label></text>'
			  [ "$5" ] && echo '<text xalign="0" use-markup="true"><label>"'$5'"</label></text>'
			  [ "$6" ] && echo '<text xalign="0" use-markup="true"><label>"'$6'"</label></text>'
			  [ "$7" ] && echo '<text xalign="0" use-markup="true"><label>"'$7'"</label></text>'
			  [ "$8" ] && echo '<text xalign="0" use-markup="true"><label>"'$8'"</label></text>'
			  [ "$9" ] && echo '<text xalign="0" use-markup="true"><label>"'$9'"</label></text>'
			  echo '</vbox>
		  </hbox>
		</vbox>
		<text space-expand="true" space-fill="true"><label>""</label></text>
	  </hbox>
	</notebook>' 
fi
