Revision [32828]
This is an old revision of gtkdialogDocTips5 made by zigbert on 2020-07-21 07:16:13.
4. The benefits of a config file
A config file is a nice way to store the settings in your program. At next startup, it will show the settings like you left them last time.Set default status of radiobuttons, Comboboxes...
By default, gtkdialog always activates the first <radiobutton> and the first list-item for <combobox>.An easy way to use a config file is to run it like an ordinary bash-script, and include variable definitions in it. It is most common to keep config files in home directory as a hidden file ($HOME/.testrc). The file might look like this:
COMBOBOX="item 3" ENTRY="default text" RADIOBUTTON1="false" RADIOBUTTON2="true"
Now let's go to the main script. For the <radiobutton> and the <entry>, we set the <default> tag to the corresponding variable in the config file. Since the <combobox> doesn't support the <default> tag, we need a workaround. We simply build the <combobox> item-list so show our saved variable first.
#in case no testc file (first run), build the file [ ! -s $HOME/.testrc ] && echo -e -n 'COMBOBOX="item 3"\nENTRY="default text"\nRADIOBUTTON1="false"\nRADIOBUTTON2="true"\n' > $HOME/.testrc . $HOME/.testrc #define combobox list items COMBOBOX_ITEMS="<item>$COMBOBOX</item>" #stored value should be first in list for I in 1 2 3 4; do COMBOBOX_ITEMS=`echo "$COMBOBOX_ITEMS<item>item $I</item>"`; done export main=" <window title=\"The benefits of a config file\"> <vbox> <frame The first item of list is the default choice in a Combobox> <combobox> <variable>COMBOBOX</variable> $COMBOBOX_ITEMS </combobox> </frame> <frame If nothing else is set, the first radiobutton is the active one> <radiobutton> <variable>RADIOBUTTON1</variable> <label>Yes I am</label> <default>$RADIOBUTTON1</default> </radiobutton> <radiobutton> <variable>RADIOBUTTON2</variable> <label>No I'm not</label> <default>$RADIOBUTTON2</default> </radiobutton> </frame> <frame Fetch entry-value from config file> <entry> <variable>ENTRY</variable> <default>$ENTRY</default> </entry> </frame> <hbox> <button ok></button> </hbox> </vbox> </window>" gtkdialog -p main > $HOME/.testrc
The last codeline redirects output (variable values) to our config file instead of to the terminal.
Note that line 2 starts with a dot. It makes a huge difference if you skip it.
. $HOME/.config - run script as a childprocess as the main process. The variable values in the config file are reachable for the main script.
$HOME/.config - run script as a new process as the main process. The variable values in the config file will NOT be reachable for the main script.
How to store window size/placement
This example shows how we can save settings for window size and placement for next startup. Be also aware that this solution makes it possible to let user rescale gui smaller than the default size. Normally you define the size of, for example, a <tree>, and the user can only resize the gui larger, but when using <window default_height="$HEIGHT" default_width="$WIDTH">, you don't need to define <height> and <width> for the tree widget.save_geometry (){ XWININFO=`xwininfo -stats -name SizeMe` HEIGHT=`echo "$XWININFO" | grep 'Height:' | awk '{print $2}'` WIDTH=`echo "$XWININFO" | grep 'Width:' | awk '{print $2}'` X1=`echo "$XWININFO" | grep 'Absolute upper-left X' | awk '{print $4}'` Y1=`echo "$XWININFO" | grep 'Absolute upper-left Y' | awk '{print $4}'` X2=`echo "$XWININFO" | grep 'Relative upper-left X' | awk '{print $4}'` Y2=`echo "$XWININFO" | grep 'Relative upper-left Y' | awk '{print $4}'` X=$(($X1-$X2)) Y=$(($Y1-$Y2)) echo "export HEIGHT=$HEIGHT" > /tmp/geometry echo "export WIDTH=$WIDTH" >> /tmp/geometry echo "export X=$X" >> /tmp/geometry echo "export Y=$Y" >> /tmp/geometry chmod 700 /tmp/geometry } export -f save_geometry [ -f /tmp/geometry ] && . /tmp/geometry export DIALOG=" <window title=\"SizeMe\" default_height=\"$HEIGHT\" default_width=\"$WIDTH\"> <vbox> <frame> <text> <label>If you resize or move this window, it will be remembered for next time.</label> </text> </frame> <hbox> <button ok> </button> </hbox> </vbox> <action signal=\"hide\">save_geometry</action> </window>" gtkdialog --program=DIALOG --geometry +"$X"+"$Y"
Another approach for size/placement is to make a call of your app from the cli.
RIGHT=14; DOWN=36; WIDTH=80; HEIGHT=80 # define variables with defaults GUI=' <vbox> <button cancel></button> </vbox>' gtkdialog -p GUI -G ${1-${WIDTH}x${HEIGHT}+${RIGHT}+${DOWN}}