#!/bin/bash
#
# Little Searcher Joerg Doehring 21.3.2011
# BASH 4.xx  required !
if [[ $BASH_VERSINFO -lt 4 ]]; then echo "BASH 4.xx required !"; exit 1; fi
ZZ=($(date '+%s'))
PP="${ZZ}$$"
PIPE="/tmp/gtk.bash.$PP"
FTMP="/tmp/ftemp.bash.$PP"
# Find GTK-server configfile first
if [[ -f gtk-server.cfg ]]; then
 CFG=gtk-server.cfg
elif [[ -f /etc/gtk-server.cfg ]]; then
 CFG=/etc/gtk-server.cfg
elif [[ -f /usr/local/etc/gtk-server.cfg ]]; then
 CFG=/usr/local/etc/gtk-server.cfg
else
 echo "No GTK-server configfile found! Please install GTK-server..."
 exit 1
fi
if [[ -z $(which locate) ]]; then MSG="No LOCATE found! Please install LOCATE ..."; echo $MSG; fi
if [[ -z $(which find) ]]; then MSG="No FIND found! Please install FIND ..."; echo $MSG; fi
if [[ -z $(which iconv) ]]; then MSG="No ICONV found! Please install ICONV ..."; echo $MSG; fi
if [[ -z $(which pdftotext) ]]; then pdfOK=0; else pdfOK=1; fi
if [[ -z $(which abiword) ]]; then docOK=0; else docOK=1; fi
# Global functionnames from GTK API
if [[ ! -f $HOME/.gtk4bash.$PP || $CFG -nt $HOME/.gtk4bash.$PP ]]; then
 echo "#!/bin/bash" > $HOME/.gtk4bash.$PP
 echo "gtk-server -fifo=$PIPE &" >> $HOME/.gtk4bash.$PP
 echo "while [ ! -p $PIPE ]; do continue; done" >> $HOME/.gtk4bash.$PP
 while read LINE
 do
 if [[ $LINE = *FUNCTION_NAME* ]]; then
     LINE=${LINE#*= }
     printf "\nfunction ${LINE%%,*}\n" >> $HOME/.gtk4bash.$PP
     printf "{\n/bin/echo ${LINE%%,*} \$@ > $PIPE" >> $HOME/.gtk4bash.$PP
     printf "\nread GTK < $PIPE\n}\n" >> $HOME/.gtk4bash.$PP
 fi
 done < $CFG
fi
# Global variables
declare GTK NULL="NULL"
unset CFG PIPE LINE
# Include the generated file to use embedded GTK functions
. ${HOME}/.gtk4bash.$PP
# GTK-server must go after an error in the script
trap 'echo "gtk_server_exit" > $PIPE' ERR
# Define SIGUSR1 in GTK-server configfile to catch exit of GTK-server
trap 'exit' SIGUSR1
# Assignment function
function define() { $2 $3 $4 $5 $6 $7 $8 $9; eval $1="$GTK"; }
# Directories
DIRTI=($( ls -l / | grep '^d' | awk '{print $9}' | sed 's/dev//;s/lost+found//;s/media//;s/mnt//;s/none//;s/proc//;s/selinux//;s/sys//;s/tmp//' ))
DIR=("MyHome" "ALL" ${DIRTI[*]})
DIX=($HOME "ALL")
DANZ=${#DIR[@]}
for (( i=2; i<$DANZ; i++))
do
	DIX[$i]="/${DIR[$i]}";
done
diedir=${DIX[0]}
fnddir=${DIX[0]}
# Character Sets, depending on the system, read 'man iconv' for more ...
CHAI="ISO_8859-15"
CHAO="UTF-8"
# Initialize variables #
STP=0
GRZ=200
REV=0
ICF=0
MAP=0
ff=0
HELPTXT=("Little GUI for Locate, Find and Man Pages with Bash 4.1x, GTK 2.20x and GTK-Server 2.3x" " "
"+------------ DEPENDENCY ------------+" "bash 4.xx, find" "(s/m)locate for updatedb data base" "iconv - encoding converter" "pdftotext - (optional for reading PDF)" "abiword - (optional for reading DOC/X, ODT, RTF, SXW etc.)"
" " "+------------ FEATURES ------------+" "multiple instances allowed" "found faster by selected directories" "quickly find files with 'locate'" "find files with 'find'" "view of manual pages" "find strings in ASCII files such as .txt, .html, .svg, .xpm, .bash, .c ..."
"### OPTIONAL ###" "find strings in PDF files, Portable Document Format" "find strings in DOC(X) files, MS Word Format" "find strings in ODT/SXW files, OpenDocument/OpenOffice/LibreOffice Format" "find strings in RTF files, Rich Text Format"
"find strings in DBK files, DocBook XML" "find strings in ABW files, Abiword Format" "find strings in KWD files, KWord Format"
" " "+------------ USAGE ------------+" "Please install 'poppler-utils (pdftotext)' and 'abiword' for all options" "If trouble with the view, change the character set in the lines 64/65 CHAI(in)/CHAO(out)" "Magic ==> '*' and '?'" 
"Reverse Order ==> e.g. Result is 400 lines, 'Limit' is 200 lines, normally the first 200 rows displayed, with 'Reverse Order' the last 200 rows displayed." "Directory 'MyHome' ==> ~/" "Directories 'All' ==> /" 
"### LOCATE ###" "MyHome + '.svg' ==> ~/*.svg ==> e.g. ~/abc.svg ~/xyz/abc/mno.svg" "MyHome + '/abc/xyz/*.svg' ==> e.g. ~/abc/xyz/mno.svg ~/abc/xyz/klm/mno.svg" "MyHome + '*.svg' ==> ~/**.svg"
"'.svg*' ==> e.g. ...abc.svg.png ...xyz.svg" "'.svg?' ==> e.g. ...abc.svgz" "### FIND ###" "MyHome + '.svg' ==> ~/.svg ==> e.g. 0 found" "MyHome + '*.svg' ==> ~/*.svg ==> e.g. ~/abc.svg ~/xyz/abc/mno.svg"
"MyHome + '*.svg*' ==> ~/*.svg* ==> e.g. ~/abc.svg ~/abc/xyz/klm/mno.svgz" "MyHome + '*.bash' + 'No GTK' ==> e.g. ~/work/search.bash :LINE:18: echo 'No GTK-server configfile found! Please install GTK-server...'"
"/bin + /usr + 'bash' ==> e.g. /bin/bash /usr/share/doc/bash /usr/share/menu/bash" "/usr + '/share/icons/*.svg' + '000000' ==> e.g. /usr/share/icons/Aluminium/128x128/apps/fusion-icon.svg :LINE:138: style='stop-color:#000000;stop-opacity:0;'"
"MyHome + '/MyFiles/*.odt' + 'Berlin, den' ==> e.g. ~/MyFiles/help.odt :LINE:1: Berlin, den 21.3.2011" "### Man Pages ###" "Man Pages + 'iconv' ==> view manual page for iconv"
"Man Pages + '-f iconv' ==> iconv (1) - Convert encoding of given files from one encoding to another, iconv (3) - perform character set conversion" "Man Pages + '3 iconv' ==> view manual page 3 for iconv")

# Window
gtk_init $NULL $NULL
define WIN gtk_window_new 0
gtk_window_set_default_size $WIN 650 620
gtk_window_set_title $WIN "'Little Searcher'"
gtk_window_set_position $WIN 1
gtk_window_set_resizable $WIN 1
# Accec_group
define GRP gtk_accel_group_new
gtk_window_add_accel_group $WIN $GRP
# Create menubar
define MENUBAR gtk_menu_bar_new
# Create FILE menu popup
define FILEMENU gtk_menu_new
define LOCAMENU gtk_menu_item_new_with_label "'Man Page LOCATE'" $GRP
define FINDMENU gtk_menu_item_new_with_label "'Man Page FIND'" $GRP
define EXITMENU gtk_image_menu_item_new_from_stock "gtk-quit" $GRP
gtk_menu_shell_append $FILEMENU $LOCAMENU
gtk_menu_shell_append $FILEMENU $FINDMENU
gtk_menu_shell_append $FILEMENU $EXITMENU
define FILETITLE gtk_menu_item_new_with_mnemonic "_File"
gtk_menu_item_set_submenu $FILETITLE $FILEMENU
gtk_menu_shell_append $MENUBAR $FILETITLE
# Create HELP menu popup
define HELPMENU gtk_menu_new
define INFOMENU gtk_image_menu_item_new_from_stock "gtk-about" $GRP
define HELPHELP gtk_image_menu_item_new_from_stock "gtk-help" $GRP
gtk_menu_shell_append $HELPMENU $HELPHELP
gtk_menu_shell_append $HELPMENU $INFOMENU
define ABOUTTITLE gtk_menu_item_new_with_mnemonic "_About"
gtk_menu_item_set_right_justified $ABOUTTITLE 1
gtk_menu_item_set_submenu $ABOUTTITLE $HELPMENU
gtk_menu_shell_append $MENUBAR $ABOUTTITLE
# Define status bar
define STATUSBAR gtk_statusbar_new
define CID gtk_statusbar_get_context_id $STATUSBAR $WIN
gtk_server_version
gtk_statusbar_push $STATUSBAR $CID "'BASH ${BASH_VERSION} with GTK-server $GTK'"
define ICCHK gtk_check_button_new_with_label "'Ignore case'"
gtk_widget_set_size_request $ICCHK 130 20
define ROCHK gtk_check_button_new_with_label "'Reverse order'"
gtk_widget_set_size_request $ROCHK 140 20
define COMBO gtk_combo_box_new_text $NULL
gtk_combo_box_append_text $COMBO "100"
gtk_combo_box_append_text $COMBO "200"
gtk_combo_box_append_text $COMBO "500"
gtk_combo_box_append_text $COMBO "1000"
gtk_combo_box_append_text $COMBO "2000"
gtk_combo_box_append_text $COMBO "5000"
define COMLABEL gtk_label_new "'Limit '"
gtk_widget_set_size_request $COMLABEL 40 20
define MANPA gtk_check_button_new_with_label "'Man Pages '"
gtk_widget_set_size_request $MANPA 110 20
define LOLABEL gtk_label_new "'locate: '"
gtk_widget_set_size_request $LOLABEL 65 20
gtk_misc_set_alignment $LOLABEL 1.0 0.5
define LOENTRY gtk_entry_new
define LOBUT gtk_button_new_from_stock "gtk-find"
define HBOX0 gtk_hbox_new 0 0
gtk_box_pack_start $HBOX0 $ICCHK 0 0 1
gtk_box_pack_start $HBOX0 $ROCHK 0 0 1
gtk_box_pack_start $HBOX0 $COMBO 0 0 1
gtk_box_pack_start $HBOX0 $COMLABEL 0 0 1
gtk_box_pack_end $HBOX0 $MANPA 0 0 1
define HBOX1 gtk_hbox_new 0 0
gtk_box_pack_start $HBOX1 $LOLABEL 0 0 1
gtk_box_pack_start $HBOX1 $LOENTRY 1 1 1
gtk_box_pack_end $HBOX1 $LOBUT 0 0 1
define HBOX1B1 gtk_hbox_new 0 8
define HBOX1B2 gtk_hbox_new 0 8
define RADIOB[0] gtk_radio_button_new_with_label_from_widget $NULL "'MyHome'"
LORAD[0]=$GTK
define RADIOB[1] gtk_radio_button_new_with_label_from_widget $LORAD "'All'"
LORAD[1]=$GTK
for (( i=2; i<$DANZ; i++))
do
	define RADIOB[$i] gtk_radio_button_new_with_label_from_widget $LORAD "'/${DIR[$i]}'"
	LORAD[$i]=$GTK
done
DH=$(($DANZ / 2))
for (( i=0; i<$DH; i++))
do
	gtk_box_pack_start $HBOX1B1 ${RADIOB[$i]} 0 0 1 
done
for (( i=$DH; i<$DANZ; i++))
do
	gtk_box_pack_start $HBOX1B2 ${RADIOB[$i]} 0 0 1 
done
# Set Boxen find BOX 2
define FNDLABEL gtk_label_new "'find: '"
gtk_widget_set_size_request $FNDLABEL 65 20
gtk_misc_set_alignment $FNDLABEL 1.0 0.5
define TXTLABEL gtk_label_new "'Text: '"
gtk_widget_set_size_request $TXTLABEL 65 20
gtk_misc_set_alignment $TXTLABEL 1.0 0.5
define FNDENTRY gtk_entry_new
define TXTENTRY gtk_entry_new
gtk_widget_set_size_request $TXTENTRY 480 29
define DUMLABEL gtk_label_new "'  in FILEs '"
define FNDBUT gtk_button_new_from_stock "gtk-find"
define HBOX2 gtk_hbox_new 0 0
gtk_box_pack_start $HBOX2 $FNDLABEL 0 0 1
gtk_box_pack_start $HBOX2 $FNDENTRY 1 1 1
gtk_box_pack_end $HBOX2 $FNDBUT 0 0 1
define HBOX2A gtk_hbox_new 0 0
gtk_box_pack_start $HBOX2A $TXTLABEL 0 0 1
gtk_box_pack_start $HBOX2A $TXTENTRY 0 1 1
gtk_box_pack_start $HBOX2A $DUMLABEL 0 0 1
define HBOX2B1 gtk_hbox_new 0 8
define HBOX2B2 gtk_hbox_new 0 8
define CHKC[0] gtk_check_button_new_with_label "'MyHome'"
FIRAD[0]=$GTK
define CHKC[1] gtk_check_button_new_with_label "'All'"
FIRAD[1]=$GTK
for (( i=2; i<$DANZ; i++))
do
	define CHKC[$i] gtk_check_button_new_with_label "'/${DIR[$i]}'"
	FIRAD[$i]=$GTK
done
for (( i=0; i<$DH; i++))
do
	gtk_box_pack_start $HBOX2B1 ${CHKC[$i]} 0 0 1 
done
for (( i=$DH; i<$DANZ; i++))
do
	gtk_box_pack_start $HBOX2B2 ${CHKC[$i]} 0 0 1 
done
# Frame search
define SUCHVBOX1 gtk_vbox_new 0 0
define SUCHVBOX2 gtk_vbox_new 0 0
define IRFRAME gtk_frame_new $NULL
define SFRAME1 gtk_frame_new $NULL
define SFRAME2 gtk_frame_new $NULL
gtk_frame_set_label $SFRAME1 "' Search with LOCATE or in Man Pages '"
gtk_frame_set_label $SFRAME2 "' Search with FIND '"
gtk_container_add $IRFRAME $HBOX0
gtk_container_add $SFRAME1 $SUCHVBOX1
gtk_container_add $SFRAME2 $SUCHVBOX2
# Frame result
define RESULTVBOX gtk_vbox_new 0 0
define RFRAME gtk_frame_new $NULL
gtk_frame_set_label $RFRAME "' Result '"
gtk_container_add $RFRAME $RESULTVBOX
# Set Output
define TXTBUF gtk_text_buffer_new $NULL
define TXTVIEW gtk_text_view_new_with_buffer $TXTBUF
gtk_text_view_set_wrap_mode $TXTVIEW 1
define SW gtk_scrolled_window_new $NULL $NULL
gtk_scrolled_window_set_policy $SW 1 1
gtk_scrolled_window_set_shadow_type $SW 1
gtk_container_add $SW $TXTVIEW
gtk_text_view_set_editable $TXTVIEW 0
gtk_text_view_set_wrap_mode $TXTVIEW 0
gtk_text_view_set_cursor_visible $TXTVIEW 0
define STPBUT gtk_toggle_button_new_with_label "STOP"
gtk_widget_set_size_request $STPBUT 70 27
define HBOX3 gtk_hbox_new 0 0
define HBOX4 gtk_hbox_new 0 0
gtk_box_pack_start $HBOX3 $SW 1 1 1
gtk_box_pack_start $HBOX4 $STPBUT 0 0 1

# Add all widgets to mainscreen
define VBOX gtk_vbox_new 0 0
gtk_box_pack_start $VBOX $MENUBAR 0 0 1
gtk_box_pack_start $VBOX $IRFRAME 0 0 1
gtk_box_pack_start $VBOX $SFRAME1 0 0 1
gtk_box_pack_start $SUCHVBOX1 $HBOX1 0 0 1
gtk_box_pack_start $SUCHVBOX1 $HBOX1B1 0 0 1
gtk_box_pack_start $SUCHVBOX1 $HBOX1B2 0 0 1
gtk_box_pack_start $VBOX $SFRAME2 0 0 1
gtk_box_pack_start $SUCHVBOX2 $HBOX2 0 0 1
gtk_box_pack_start $SUCHVBOX2 $HBOX2B1 0 0 1
gtk_box_pack_start $SUCHVBOX2 $HBOX2B2 0 0 1
gtk_box_pack_start $SUCHVBOX2 $HBOX2A 0 0 1
gtk_box_pack_start $VBOX $RFRAME 1 1 1
gtk_box_pack_start $RESULTVBOX $HBOX3 1 1 1
gtk_box_pack_start $RESULTVBOX $HBOX4 0 0 1
gtk_box_pack_end $VBOX $STATUSBAR 0 0 1
gtk_container_add $WIN $VBOX
gtk_container_set_border_width $VBOX 3
gtk_container_set_border_width $SUCHVBOX1 5
gtk_container_set_border_width $SUCHVBOX2 5
gtk_container_set_border_width $HBOX0 5

# Show of mainscreen
gtk_widget_show_all $WIN
define STARTITER gtk_server_opaque
define ENDITER gtk_server_opaque
gtk_widget_grab_focus $LOENTRY
gtk_server_connect  $LOCAMENU "activate" "myloca"
gtk_server_connect  $FINDMENU "activate" "myfind"
gtk_server_connect  $EXITMENU "activate" "myquit"
gtk_server_connect  $HELPHELP "activate" "myhelp"
gtk_server_connect  $INFOMENU "activate" "myabout"
gtk_combo_box_set_active $COMBO 1
for (( i=0; i<$DANZ; i++))
do
	gtk_toggle_button_set_active ${RADIOB[$i]} 0
	gtk_toggle_button_set_active ${CHKC[$i]} 0
done
gtk_toggle_button_set_active ${CHKC[0]} 1
gtk_toggle_button_set_active $STPBUT 0
gtk_toggle_button_set_active $ICCHK 0
gtk_toggle_button_set_active $ROCHK 0
gtk_toggle_button_set_active $MANPA 0
gtk_server_callback "update"

# functions
function on_stop
{
	gtk_toggle_button_get_active $STPBUT
	if [[ $GTK -eq 0 ]]; then
		STP=0; updaten "$1"; gtk_toggle_button_set_active $STPBUT 0
	else
		STP=1; updaten "$2"; gtk_toggle_button_set_active $STPBUT 1
	fi
}

function updaten
{
	gtk_statusbar_push $STATUSBAR $CID "' $1 '"
	gtk_server_callback "update"
}

function on_about
{
	gtk_server_version
	ABO="#+#   Little Searcher   #+#\r\rJoerg Doehring 21.3.2011\r\rhttp://www.jeydee.net"
	define DIALOG gtk_message_dialog_new $WIN 0 0 2 "'$ABO'" "''"
	gtk_window_set_title $DIALOG "' Little Searcher '"
	gtk_widget_show $DIALOG
	gtk_dialog_run $DIALOG
	gtk_widget_destroy $DIALOG
}

function finden
{
	updaten "temporary saved ..."
	anz=($( wc -l < $FTMP ))
	local a=1
	local z=0
	local ARR
	if [[ $REV -eq 0 ]]; then
		i=0
		while read LIN
		do
			ARR[$i]="$LIN"
			((i++))
		done < $FTMP
	else
		i=$(($anz - 1))
		while read LIN
		do
			ARR[$i]="$LIN"
			((i--))
		done < $FTMP
	fi
  gtk_text_buffer_get_bounds $TXTBUF $STARTITER $ENDITER
  gtk_text_buffer_delete $TXTBUF $STARTITER $ENDITER
	if [[ $anz -le $GRZ ]]; then end=$anz; else end=$GRZ; fi
	for (( i=0; i<$end; i++))
	do
		[[ $STP -eq 1 ]] && break
		gtk_text_buffer_get_end_iter ${TXTBUF} $ENDITER
		ARR[$i]=${ARR[$i]//\'/\"}
		gtk_text_buffer_insert $TXTBUF $ENDITER "'$a. ${ARR[$i]}\n'" -1
		if [[ $i -lt 100 ]]; then
			updaten "$a\($anz\) - ${ARR[$i]}"
		else
			z=$(($i % 200))
			if [[ $z -eq 0 ]]; then updaten "$i\($anz\) - ${ARR[$i]}"; fi
		fi
		((a++))
	done
}

function on_locate
{
	if [[ $MAP -eq 1 ]]; then
		updaten "man in progress ..."
		man $TXT | iconv -f $CHAI -t $CHAO > $FTMP
		altgrz=$GRZ
		GRZ=5000
		finden
		GRZ=$altgrz
		if [[ $anz -eq 0 ]]; then updaten "No entry found"; else updaten "Found $i lines for \"$TXT\""; fi
	else
		updaten "locate in progress ..."
		if [[ ${TXT:0:1} == "/" ]]; then TXT=${TXT/\//}; fi
		if [[ ${diedir:0:1} == "/" ]]; then stx="$diedir/*$TXT"; else stx=$TXT; fi
		if [[ $ICF -eq 0 ]]; then ic=""; else ic="-i"; fi
		locate $ic "$stx" | iconv -f $CHAI -t $CHAO > $FTMP
		finden
		updaten "Found $i\($anz\) matches for \"$stx\""
	fi
}

function on_find
{
	local j=0
	local k=0
	local l=0
	local z=0
	local ATT
	local AGG
	local AGP
	updaten "find in progress ..."
	if [[ ${fnddir:0:3} == "ALL" ]]; then stx="/"; else stx=$fnddir; fi
	if [[ ${TXT} =~ \/ ]]; then
		topa=${TXT%\/*}
		TXT=${TXT##$topa\/}
		if [[ ${topa:0:1} == "/" ]]; then stx="$stx$topa"; else stx="$stx/$topa"; fi
	fi
	if [[ $ICF -eq 0 ]]; then ic="-name"; ig=""; else ic="-iname"; ig="-i"; fi
	gtk_entry_get_text $TXTENTRY
	INH=${GTK:0:80}
	find $stx $ic "$TXT" | iconv -f $CHAI -t $CHAO > $FTMP
	if [[ -n $INH ]]; then
		while read LIN
		do
			ATT[$z]="$LIN"
			((z++))
		done < $FTMP
		while [[ $j -lt ${#ATT[@]} ]]
		do
			KK=${ATT[$j],,}
			if [[ pdfOK -eq 1 && $KK =~ \.pdf ]]; then
				pdftotext -q ${ATT[$j]} $FTMP
				dd="$FTMP"
			elif [[ docOK -eq 1 && $KK =~ \.do[cx]|\.odt|\.sxw|\.rtf|\.abw|\.dbk|\.kwd ]]; then
				abiword --to=txt --to-name=$FTMP "${ATT[$j]}"
				dd="$FTMP"
			else
				dd="${ATT[$j]}"
			fi
			local IFS=$'\n'
			AGP=($( grep $ig -sn "$INH" "$dd" ))
			z=$(($j % 10))
			if [[ $z -eq 0 ]]; then updaten "Reading \"${ATT[$j]}\""; fi
			l=0
			while [[ $l -lt ${#AGP[@]} ]]
			do
				if [[ -n ${AGP[$l]} ]]; then
					yy="${ATT[$j]} :LINE:${AGP[$l]}"
					AGG[$k]=${yy:0:300}
					((k++))
				fi
				((l++))
			done
			((j++))
		done
		rm -f $FTMP
		j=0
		if [[ ${#AGG[0]} -gt 0 && ${#AGG[@]} -gt 0 ]]; then
			updaten "Writing ..."
			while [[ $j -lt ${#AGG[@]} ]]
			do
				echo ${AGG[$j]} | iconv -f $CHAI -t $CHAO >> $FTMP
				((j++))
			done
		else
			touch $FTMP
		fi
	fi
	finden
	if [[ -z $INH ]]; then
		updaten "Found $i\($anz\) matches for \"$TXT\" in $stx"
	else
		updaten "Found $i\($anz\) matches for \"$INH\" in $stx \"$TXT\""
	fi
}

function on_help
{
	local h=0
	rm -f $FTMP
	while [[ $h -lt ${#HELPTXT[@]} ]]
	do
		echo ${HELPTXT[$h]} >> $FTMP
		((h++))
	done
	finden
	updaten "Little Searcher ..."
}

function on_msg
{
	echo ${MSG} > $FTMP
	finden
	updaten "Error message ..."
}

# Mainloop
until [[ $EVENT = "myquit" || $EVENT = $WIN ]]
do
    define EVENT gtk_server_callback "wait"
		case $EVENT in

			$STPBUT) on_stop "Ready !" "STOP !";;
      "myabout") 	on_about;;
			"myloca") altmap=$MAP; MAP=1; TXT="locate"; on_locate; MAP=$altmap;;
			"myfind") altmap=$MAP; MAP=1; TXT="find"; on_locate; MAP=$altmap;;
			"myhelp") on_help;;
			$LOBUT)
				gtk_entry_get_text $LOENTRY
				TXT=${GTK:0:80}
				len=${#TXT}
				if [ $len -gt 2 ]; then 
					if [[ ${MSG} =~ No ]]; then on_msg; else on_locate; fi
				else
					updaten "No searchstring !"
				fi
			;;
			$FNDBUT)
				gtk_entry_get_text $FNDENTRY
				TXT=${GTK:0:80}
				len=${#TXT}
				if [ $len -gt 2 ]; then
					if [[ ${MSG} =~ No ]]; then on_msg; else on_find; fi
				else
					updaten "No searchstring !"
				fi
			;;
			$ROCHK)
				gtk_toggle_button_get_active $ROCHK
				if [[ $GTK -eq 0 ]]; then
					REV=0; updaten "Normal Order !"; gtk_toggle_button_set_active $ROCHK 0
				else
					REV=1; updaten "Reverse Order !"; gtk_toggle_button_set_active $ROCHK 1
				fi
			;;
			$ICCHK)
				gtk_toggle_button_get_active $ICCHK
				if [[ $GTK -eq 0 ]]; then
					ICF=0; updaten "Case sensitive !"; gtk_toggle_button_set_active $ICCHK 0
				else
					ICF=1; updaten "Ignore case !"; gtk_toggle_button_set_active $ICCHK 1
				fi
			;;
			$MANPA)
				gtk_toggle_button_get_active $MANPA
				if [[ $GTK -eq 0 ]]; then
					MAP=0; updaten "Search with locate .."; gtk_toggle_button_set_active $MANPA 0
				else
					MAP=1; updaten "Search in man pages .."; gtk_toggle_button_set_active $MANPA 1
				fi
			;;
      $GTK)
				if [[ $GTK -eq $COMBO ]]; then
					gtk_combo_box_get_active $COMBO
					case $GTK in
						0) GRZ=100;;
						1) GRZ=200;;
						2) GRZ=500;;
						3) GRZ=1000;;
						4) GRZ=2000;;
						5) GRZ=5000;;
					esac
				fi
				for (( n=0; n<$DANZ; n++))
				do
					if [[ $GTK -eq ${LORAD[$n]} ]]; then diedir=${DIX[$n]}; updaten "$diedir"; break; fi
					if [[ $GTK -eq ${FIRAD[$n]} ]]; then ff=1; break; fi
				done
				if [[ $ff -eq 1 ]]; then
					case $n in
						0|1) 
							fnddir=${DIX[$n]}
							for (( m=0; m<$DANZ; m++)) 
							do
								gtk_toggle_button_set_active ${FIRAD[$m]} 0
							done
							gtk_toggle_button_set_active ${FIRAD[$n]} 1
						;;
						*)
							gtk_toggle_button_set_active ${FIRAD[0]} 0
							gtk_toggle_button_set_active ${FIRAD[1]} 0
							gtk_toggle_button_get_active ${FIRAD[$n]}
							if [[ $GTK -eq 0 ]]; then
								fnddir=${fnddir/${DIX[$n]}/}
								gtk_toggle_button_set_active ${FIRAD[$n]} 0
							else
								fnddir=${fnddir/${DIX[0]}/}
								fnddir=${fnddir/${DIX[1]}/}
								fnddir="$fnddir ${DIX[$n]}"
								gtk_toggle_button_set_active ${FIRAD[$n]} 1
							fi
						;;
					esac
					updaten "$fnddir";
				fi
				ff=0;
			;;
    esac
done

# Exit GTK
gtk_server_exit
rm -f $FTMP
rm -f /tmp/gtk.bash.$PP
rm -f $HOME/.gtk4bash.$PP
exit 0

