Next: Widgets, Previous: Introduction, Up: Top [Contents]

## 3 Invoking the Program

### 3.1 Examples

Our first example program shows how to call the `gtkdialog` from a BASH script.

    #! /bin/bash

    export MAIN_DIALOG='
     <vbox>
      <text>
        <label>This is a label...</label>
      </text>
      <hbox>
       <button ok></button>
       <button cancel></button>
      </hbox>
     </vbox>'

    gtkdialog --program MAIN_DIALOG

This example uses a very plain way to open a dialog box. We store the description of the dialog box in the `MAIN_DIALOG` environment variable which is exported to the child processes with the BASH `export` built-in. Then we call the `gtkdialog` program with the `--program` option which is followed by the name of the variable holding the dialog box description. It is simple and easy to write BASH scripts in this manner.

A similar calling method can be used when user input is needed. The `gtkdialog` send the state of the widgets to the standard output when exiting and this is how we can get user input for the BASH program. The next example code show the reading process.

    #! /bin/bash

    export DIALOG='
    <vbox>
      <entry>
        <variable>ENTRY</variable>
      </entry>
      <hbox>
        <button ok></button>
        <button cancel></button>
      </hbox>
    </vbox>'

    I=$IFS; IFS=""
    for STATEMENTS in  $(gtkdialog --program DIALOG); do
      eval $STATEMENTS
    done
    IFS=$I

    if [ "$EXIT" = "OK" ]; then
      echo "You entered: $ENTRY."
    else
      echo "You pressed the Cancel button."
    fi

In the example script we use the `for` built-in to go through the list `gtkdialog` produced. Changing the field separator (IFS) is a little bit disturbing but necessary since this is the only way to protect the space characters in user input.

In larger software projects it may be a good idea to break the code to separate files. Since `gtkdialog` can read the description program from file it is easy to write self executable programs with it. This is how the next example constructed.

    #! /usr/local/bin/gtkdialog -f
    <vbox>
      <checkbox>
        <label>This is a checkbox</label>
        <variable>CHECK1</variable>
      </checkbox>
      <checkbox>
        <label>Another one</label>
        <variable>CHECK2</variable>
      </checkbox>
      <button>
        <label>OK</label>
      </button>
    </vbox>

When used in this fashion the state of the widgets can get from the standard output of the script as usually.

Next: Widgets, Previous: Introduction, Up: Top [Contents]