#!/usr/bin/bash # # Create embedded GTK functions for BASH - see the demo down below how it looks :-) # # (c) Peter van Eerten 2006, GPL license # # Tested with: # -BASH 3.0 on Slackware 10 and GTK-server 2.1.4 # # October 11, 2006: Initial release # October 12, 2006: Added check to see when created PIPE file is ready # January 16, 2007: Solved problem of unique filename # August 20, 2007: Improved for GTK-server 2.1.4 # October 13, 2007: Added mechanism for finding configfile # May 14, 2008: Added define function, code is even shorter now # Oct 9, 2008: Added default 'gtk' function to use after new definition of calls # # Put the first part of this file at the start of each of your BASH scripts and # you can embed GTK as if you are using the original API. #------------------------------------------------------------------------------------------------- # Set the extending globbing option in BASH shopt -s extglob # Pipe filename must be unique for your application PIPE="/tmp/gtk.bash.\$$" # 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 # Now create global functionnames from GTK API if [[ ! -f $HOME/.gtk4bash || $CFG -nt $HOME/.gtk4bash ]]; then echo "#!/bin/bash" > $HOME/.gtk4bash echo "gtk-server -fifo=$PIPE &" >> $HOME/.gtk4bash echo "while [ ! -p $PIPE ]; do continue; done" >> $HOME/.gtk4bash while read LINE do if [[ $LINE = FUNCTION_NAME* && $LINE = +(*gtk_*|*gdk_*|*g_*|*glade_*) ]]; then LINE=${LINE#*= } printf "\nfunction ${LINE%%,*}\n" >> $HOME/.gtk4bash printf "{\n/bin/echo ${LINE%%,*} \$@ > $PIPE" >> $HOME/.gtk4bash printf "\nread GTK < $PIPE\n}\n" >> $HOME/.gtk4bash fi done < $CFG printf "\nfunction gtk()\n{\necho \$1 > $PIPE; read GTK < $PIPE;\n}\n" >> $HOME/.gtk4bash fi # Declare global variables declare GTK NULL="NULL" unset CFG PIPE LINE # Assignment function function define() { $2 $3 $4 $5 $6 $7 $8 $9; eval $1="$GTK"; } # Wait for user read -p "Press to see the demo..." VAR #------------------------------------------------------------------------------------------------- # Demo on how it works after integration #------------------------------------------------------------------------------------------------- # Include the generated '.gtk4bash'-file in the shellscript to use embedded GTK functions . $HOME/.gtk4bash # Define GUI gtk_init "NULL NULL" define WINDOW gtk_window_new 0 gtk_window_set_title $WINDOW "'This is a title'" gtk_window_set_position $WINDOW 1 define TABLE gtk_table_new 10 10 1 gtk_container_add $WINDOW $TABLE define BUTTON gtk_button_new_with_label "'Click here!'" gtk_table_attach_defaults $TABLE $BUTTON 5 9 7 9 define CHECK gtk_check_button_new_with_label "'Check this \n out!'" gtk_table_attach_defaults $TABLE $CHECK 1 6 1 2 define ENTRY gtk_entry_new gtk_table_attach_defaults $TABLE $ENTRY 1 6 3 4 gtk_widget_show_all $WINDOW # Initialize variables EVENT=0 # Mainloop while [[ $EVENT != $BUTTON && $EVENT != $WINDOW ]] do define EVENT gtk_server_callback "wait" if [[ $EVENT == $ENTRY ]] then define TXT gtk_entry_get_text $ENTRY echo $TXT fi done # Exit GTK gtk_server_exit