Revision history for gtkdialogDocTips5


Revision [33323]

Last edited on 2023-02-24 06:48:29 by zigbert
Additions:
[[HomePage]] > [[SoftwareIndex|Software Index]] > [[SoftwareDevelopment|Development]] > [[gtkdialog|gtkdialog]] > [[gtkdialogDocTips|Tips and Tricks]]
===@@**#%[[gtkdialogDocTips4.2|❰❰❰ Previous]]#% #%[[gtkdialogDocTips|Index]]#% #%[[gtkdialogDocTips6.1|Next ❱❱❱]]#%**@@===
Deletions:
[[HomePage]] > [[SoftwareIndex Software Index]] > [[SoftwareDevelopment Development]] > [[gtkdialog gtkdialog]] > [[gtkdialogDocTips Tips and Tricks]]
@@**#%[[gtkdialogDocTips4.2|❰❰❰ Previous]]#% #%[[gtkdialogDocTips|Index]]#% #%[[gtkdialogDocTips6.1|Next ❱❱❱]]#%**@@


Revision [32894]

Edited on 2020-07-28 05:23:55 by zigbert
Additions:
@@**#%[[gtkdialogDocTips4.2|❰❰❰ Previous]]#% #%[[gtkdialogDocTips|Index]]#% #%[[gtkdialogDocTips6.1|Next ❱❱❱]]#%**@@


Revision [32864]

Edited on 2020-07-24 19:47:07 by zigbert
Additions:
----
==Categories==
CategoryGtkdialog


Revision [32829]

Edited on 2020-07-22 22:27:42 by zigbert
Additions:
====5. Speed issues====
Gtkdialog is not a fast gui-lib. If you are building a gui where speed matters, please check the following notes.
For an unknown reason, it is much faster to start your gtkdialog-xml-code from a file than from a variable.
##gtkdialog --file=filename
gtkdialog --program=variable##
The file alternative is highly recommended if your xml-code is getting complex.
Progressbars can suck your cpu-power. Adding a reasonable sleep value in the loop helps a lot.
Run large calculations as a background process. [[pMusic]] builtin filebrowser (version 0.9) first shows songs in the directory, then it starts a background process to find meta information of the songs. When finished, it renders the song-list once again, now with complete information. How to use a background process with gtkdialog is explained in the chapter '[[gtkdialogDocTips3 Let external code act on your gtkdialog gui]]'.
Even if gtkdialog is slow, your bash-code might make it MUCH slower. Be careful with the use of subshells, and slow commands such as ##sed, ps, expr, cat## ... This is of course most important when building a loop.
The comparison of code shows how to do the same operation in 2 different ways. The speed differs a lot. First an example why you should avoid the command ##cat##, then why never use ##expr## if speed matters. My system needed 4 seconds to calculate 1+1 thousand times with ##expr##. Using the builtin bash command it took 'no time'.
time for I in $(seq 1 1000); do B="`cat /tmp/tmp`"; done
real 0m3.095s
time `IFS=$'\n'; for I in $(seq 1 1000); do B=($(<"/tmp/tmp")); done`
real 0m0.927s
time for I in $(seq 1 1000); do expr 1 + 1; done
real 0m4.286s
time for I in $(seq 1 1000); do A=$((1+1)); done
real 0m0.032s
There are many ways of building a gui. The builtin file browser/filesearch in [[pBurn]] was updated from version 2 to [[pBurn]] 3. The code has shrunk from 3110 to 997 chars. It does exactly the same, and the speed improvement is notable.
Deletions:
====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 ##[[http://01micko.com/reference/radiobutton.html <radiobutton>]]## and the first list-item for ##[[http://01micko.com/reference/combobox.html <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 ##[[http://01micko.com/reference/radiobutton.html <radiobutton>]]## and the ##[[http://01micko.com/reference/entry.html <entry>]]##, we set the ##<default>## tag to the corresponding variable in the config file. Since the ##[[http://01micko.com/reference/combobox.html <combobox>]]## doesn't support the ##<default>## tag, we need a workaround. We simply build the ##[[http://01micko.com/reference/combobox.html <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}}


Revision [32828]

The oldest known version of this page was created on 2020-07-21 07:16:13 by zigbert
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki