Add GTK GUI frontends to a command line program in minutes

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

Add GTK GUI frontends to a command line program in minutes

#1 Post by technosaurus »

Here is a little template I put together adding GTK GUI frontends to command line programs.

Just use the individual parts (as many times as needed - just copy/paste and increment the numbers or comment out unneeded sections) to collect the required data in the GUI and pass it to the command line.

Code: Select all

#! /bin/bash
export CHOOSER="
<window title=\"YOURPROGRAMNAME \"icon-name=\"gtk-cdrom\">
<vbox>

<frame Select your file>
<hbox>
      <entry editable=\"true\" accept=\"filename\">
        <variable>FILE1</variable>
      </entry>
      <button>
        <input file stock=\"gtk-file\"></input>
        <variable>FILE_BROWSE_FILENAME</variable>
			<visible>enabled</visible>
        <action type=\"fileselect\">FILE1</action>
      </button>
</hbox>
</frame>

<frame Select a directory>
	<hbox>
      <entry editable=\"true\" accept=\"directory\">
        <variable>DIR1</variable>
      </entry>
      <button>
        <input file stock=\"gtk-open\"></input>
        <variable>FILE_BROWSE_DIRECTORY</variable>
        <action type=\"fileselect\">DIR1</action>
      </button>
  </hbox>
</frame>

<frame Entry box for user definable strings>
<hbox>
  <entry editable=\"true\">
  <variable>WORD1</variable>
  <default> default text here </default>
  </entry>
  </hbox>
 </frame>

 <frame choose any or all:>
<hbox>
     <checkbox>
      <label>Check the box if you want to ...</label>
      <variable>CBOX1</variable>
      <default>false</default>
    </checkbox>

</hbox>
<hbox>
    <checkbox>
      <label>Check the box if you want to ...</label>
      <variable>CBOX2</variable>
      <default>true</default>
    </checkbox>
</hbox>
 </frame>

 <frame choose one of:>
    <combobox>
     <variable>COMBO1</variable>
      <item>item 1</item>
      <item>item 2</item>
    </combobox>	
</frame>

<frame choose one of:>
    <combobox>
     <variable>COMBO2</variable>
      <item>item 1</item>
      <item>item 2</item>
    </combobox>
</frame>

<hbox>
  <button>
	<input file icon=\"gtk-ok\"></input>
        <label>OK</label>
        <action type=\"exit\">EXIT_OK</action>
   </button>
   <button>
	<input file icon=\"gtk-quit\"></input>
        <label>QUIT</label>
        <action type=\"exit\">EXIT_NOW</action>
      </button>
</hbox>
</vbox>
</window>
"

CHOOSER2==`gtkdialog3 --program=CHOOSER --center`

if [ "`echo $CHOOSER2 | grep EXIT_NOW`" != "" ]; then
exit 0
fi

#Get all of the variables
FILE1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep FILE1 | sed 's/FILE1//'`"
#This is another file example if you need to prepend the flag -f ... it could be any string
#FILE1=" -f ""`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep FILE1 | sed 's/FILE1//'`"
WORD1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep WORD1 | sed 's/WORD1//'`"
DIR1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep DIR1 | sed 's/DIR1//'`"
CBOX1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep CBOX1 | sed 's/CBOX1//'`"
CBOX2="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep CBOX2 | sed 's/CBOX2//'`"
COMBO1="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep COMBO1 | sed 's/COMBO1//'`"
COMBO2="`echo "$CHOOSER2" | sed 's/\"//g' | sed 's/=//g' | grep COMBO2 | sed 's/COMBO2//'`"
if [ $CBOX1 = "true" ];then
CBOX1=" -X " #where -X is the parameter you wish to pass when this checkbox is checked
else
CBOX1="" #setting it back to "null" allows us to pass all parameters without doing more "if thens" 
fi
if [ $CBOX2 = "true" ];then
CBOX2=" -X " #where -Y is the parameter you wish to pass when this checkbox is checked
else
CBOX2="" #setting it back to "null" allows us to pass all parameters without doing more "if thens" 
fi
#you can add any number of any of these variables
#to debug you can add echo $VARNAME for each variable and run the script from rxvt

#The following is an imaginary example to open an 80x4 terminal and execute program_name
#with cbox flags (if set) using file1 (presumably as input of type combo1)
# and output to a file of type combo2 named word1 in dir1
#rxvt +sb -bg orange -geometry 80x4 -e program_name $CBOX1... $FILE1 $COMBO1 $DIR1/$WORD1 $COMBO2

gxmessage -wrap -name YourProgram "This is a message to tell the user that the program is finished.  You can use the variable names outside of the quotes like this: CBOX1 = "$CBOX1", CBOX2 = "$CBOX2", DIR1 = "$DIR1", FILE1 = "$FILE1", WORD1 = "$WORD1", COMBO1 = "$COMBO1", COMBO2 = "$COMBO2"  Make sure you put the output in user understandable terms after you have finished the debugging process"
unset CHOOSER
Attachments
frontend.png
(24.87 KiB) Downloaded 3691 times
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#2 Post by technosaurus »

here is a quick example using tar ()
rxvt -e tar -$COMBO1$CBOX1"f "$FILE1

where:
COMBO1 is x or t (x=extract or y=list files)
CBOX1 is v (verbose if checked)
"f " is to get input file
FILE1 is the input file (default value should be $1 so that if you "open with" your program the user doesn't have to find it again)

Here is a list of available window icons:
document-open-recent
gtk-about
gtk-add
gtk-bold
gtk-caps-lock-warning
gtk-cdrom
gtk-clear
gtk-close
gtk-color-picker
gtk-connect
gtk-convert
gtk-copy
gtk-cut
gtk-delete
gtk-directory
gtk-disconnect
gtk-edit
gtk-execute
gtk-file
gtk-find-and-replace
gtk-find
gtk-floppy
gtk-font
gtk-fullscreen
gtk-go-back-ltr
gtk-go-down
gtk-go-forward-ltr
gtk-goto-bottom
gtk-goto-first-ltr
gtk-goto-last-ltr
gtk-goto-top
gtk-go-up
gtk-harddisk
gtk-help
gtk-home
gtk-indent-ltr
gtk-indent-rtl
gtk-index
gtk-info
gtk-italic
gtk-jump-to-ltr
gtk-jump-to-rtl
gtk-justify-center
gtk-justify-fill
gtk-justify-left
gtk-justify-right
gtk-leave-fullscreen
gtk-media-forward-ltr
gtk-media-next-ltr
gtk-media-pause
gtk-media-play-ltr
gtk-media-play-rtl
gtk-media-previous-ltr
gtk-media-record
gtk-media-rewind-ltr
gtk-media-stop
gtk-missing-image
gtk-network
gtk-new
gtk-open
gtk-orientation-landscape
gtk-orientation-portrait
gtk-orientation-reverse-landscape
gtk-orientation-reverse-portrait
gtk-page-setup
gtk-paste
gtk-preferences
gtk-print-error
gtk-print-paused
gtk-print
gtk-print-preview
gtk-print-report
gtk-print-warning
gtk-properties
gtk-quit
gtk-redo-ltr
gtk-redo-rtl
gtk-refresh
gtk-remove
gtk-revert-to-saved-ltr
gtk-revert-to-saved-rtl
gtk-save-as
gtk-select-all
gtk-select-color
gtk-select-font
gtk-sort-ascending
gtk-sort-descending
gtk-spell-check
gtk-stop
gtk-strikethrough
gtk-undelete-ltr
gtk-undelete-rtl
gtk-underline
gtk-undo-ltr
gtk-undo-rtl
gtk-unindent-ltr
gtk-unindent-rtl
gtk-zoom-100
gtk-zoom-fit
gtk-zoom-in
gtk-zoom-out
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

Re: Add GTK GUI frontends to a command line program in minutes

#3 Post by seaside »

technosaurus wrote:Here is a little template I put together adding GTK GUI frontends to command line programs.
technosaurus,

Thanks very much for posting this. I'm just now trying to experiment with GTKdialog and this is a definite help.

Does anyone actually produce Puppy GTKdialog programs using Glade? It seems to me that the learning curve for Glade is rather steep.

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#4 Post by 8-bit »

I got in a hurry and uploaded a menu based on your work before finishing it fully. But it goes along with a command line program to format usb floppy drives.
And it all works!
The program and gui are in the Additional Software section and called:
USB Floppy Formatter.
Also, this was my first attempt ever of doing something like this.

User avatar
ttuuxxx
Posts: 11171
Joined: Sat 05 May 2007, 10:00
Location: Ontario Canada,Sydney Australia
Contact:

Re: Add GTK GUI frontends to a command line program in minutes

#5 Post by ttuuxxx »

technosaurus wrote:Here is a little template I put together adding GTK GUI frontends to command line programs.
I don't know if you have much time but a really good pdf, program called mupdf needs a GUI, its really fast rendering and displays nicely, way better than epdfview. But its command-line, http://www.murga-linux.com/puppy/viewto ... 167#329167
ttuuxxx
http://audio.online-convert.com/ <-- excellent site
http://samples.mplayerhq.hu/A-codecs/ <-- Codec Test Files
http://html5games.com/ <-- excellent HTML5 games :)

PupGeek
Posts: 353
Joined: Sun 06 Sep 2009, 11:30

Re: Add GTK GUI frontends to a command line program in minut

#6 Post by PupGeek »

seaside wrote: Does anyone actually produce Puppy GTKdialog programs using Glade? It seems to me that the learning curve for Glade is rather steep.
trouble with glade is that it is pretty much for programming rather than scripting.... kinda like ncurses..... id love to be able to use ncurses with shell scripts.

Post Reply