Wiki source for gtkdialogDocTips4.1


Show raw source

[[HomePage]] > [[SoftwareIndex|Software Index]] > [[SoftwareDevelopment|Development]] > [[gtkdialog|gtkdialog]] > [[gtkdialogDocTips|Tips and Tricks]]

====4.1 Set default status of radiobuttons, Comboboxes...====

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.

By default, gtkdialog always activates the first ##[[https://github.com/puppylinux-woof-CE/gtkdialog/wiki/radiobutton <radiobutton>]]## and the first list-item for ##[[https://github.com/puppylinux-woof-CE/gtkdialog/wiki/combobox <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:

%%(language-ref)
COMBOBOX="item 3"
ENTRY="default text"
RADIOBUTTON1="false"
RADIOBUTTON2="true"
%%

Now let's go to the main script. For the ##[[https://github.com/puppylinux-woof-CE/gtkdialog/wiki/radiobutton <radiobutton>]]## and the ##[[https://github.com/puppylinux-woof-CE/gtkdialog/wiki/entry <entry>]]##, we set the ##<default>## tag to the corresponding variable in the config file. Since the ##[[https://github.com/puppylinux-woof-CE/gtkdialog/wiki/combobox <combobox>]]## doesn't support the ##<default>## tag, we need a workaround. We simply build the ##[[https://github.com/puppylinux-woof-CE/gtkdialog/wiki/combobox <combobox>]]## item-list so show our saved variable first.

%%(language-ref)
#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.

===@@**#%[[gtkdialogDocTips3|❰❰❰ Previous]]#% #%[[gtkdialogDocTips|Index]]#% #%[[gtkdialogDocTips4.2|Next ❱❱❱]]#%**@@===
----
==Categories==
CategoryGtkdialog
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki