#!/bin/sh

# step 2022: Quirks
#
# On signal "current-folder-changed" the <variable> value matches the currently
# selected folder only if the user has selected and entered that folder.  There
# are no clear rules for deriving the value otherwise. (GTK2 and GTK3)
#
# When fs-filters or fs-filters-mime is defined, at times the <variable> value
# that results from the user filtering the file list does not match the
# selection. (GTK3)

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

[ -d "$1" ] && image_folder="$1" || image_folder="/usr/share/pixmaps"
this="$(find "$image_folder" -maxdepth 1 -type f | shuf | head -1)"

chooser_attrs="
default-file=\"$this\"
fs-filters-mime=\"image/*|image/jpeg|image/png|image/x-xpixmap|x-xpmi\"
fs-filters=\"*|*.xpm|*.[n-z]*|[a-m]*\"
"

MAIN_DIALOG='
<window title="Chooser 2022 Event Counters" resizable="true">
	<vbox border-width="10">
		<chooser '"$chooser_attrs"'>
			<variable>CHOOSER</variable>
			<default>'"$(dirname "$this")"'</default>
			<width>600</width>
			<height>400</height>

			<action signal="selection-changed">refresh:SELECTION_CHANGED</action>
			<action signal="update-preview">refresh:UPDATE_PREVIEW</action>
			<action signal="current-folder-changed">refresh:CURRENT_FOLDER_CHANGED</action>
			<action signal="file-activated">refresh:FILE_ACTIVATED</action>
			<action signal="button-release-event">refresh:BUTTON_RELEASE_EVENT</action>

			<action signal="button-release-event">  echo "button-release-event   ($CHOOSER)"</action>
			<action signal="current-folder-changed">echo "current-folder-changed ($CHOOSER)"</action>
			<action signal="file-activated">        echo "file-activated         ($CHOOSER)"</action>
			<action signal="selection-changed">     echo "selection-changed      ($CHOOSER)"</action>
			<action signal="update-preview">        echo "update-preview         ($CHOOSER)"</action>
		</chooser>
		<frame Event counters>
			<hbox homogeneous="true">
				<text label="selection changed"></text>
				<text label="update preview"></text>
				<text label="current folder changed"></text>
				<text label="file activated"></text>
				<text label="button release event"></text>
			</hbox>
			<hbox homogeneous="true">
				<text label="0">
					<variable>SELECTION_CHANGED</variable>
					<input>printf $(($SELECTION_CHANGED +1))</input>
				</text>
				<text label="0">
					<variable>UPDATE_PREVIEW</variable>
					<input>printf $(($UPDATE_PREVIEW +1))</input>
				</text>
				<text label="0">
					<variable>CURRENT_FOLDER_CHANGED</variable>
					<input>printf $(($CURRENT_FOLDER_CHANGED +1))</input>
				</text>
				<text label="0">
					<variable>FILE_ACTIVATED</variable>
					<input>printf $(($FILE_ACTIVATED +1))</input>
				</text>
				<text label="0">
					<variable>BUTTON_RELEASE_EVENT</variable>
					<input>printf $(($BUTTON_RELEASE_EVENT +1))</input>
				</text>
			</hbox>
		</frame>
		<hbox>
			<button ok></button>
			<button cancel></button>
		</hbox>
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
