HomePage > Software Index > Development > gtkdialog > 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 <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.


Categories
CategoryGtkdialog
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki