GtkDialog - tips

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Message
Author
User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#346 Post by sunburnt »

### Way to get gtkdialog to do math in a button control`s <action>?

Code: Select all

      <button><label>Increase</label><action>setHT=$(($setHT+1))</action>
          <action type=\"clear\">setHT</action><action type=\"refresh\">setHT</action></button>
      <button><label>Decrease</label><action>setHT=$(($setHT-1))</action>
          <action type=\"clear\">setHT</action><action type=\"refresh\">setHT</action></button>
      <entry><variable>setHT</variable><default>$setHT</default>
          <width>40</width><height>25</height></entry>
It doesn`t complain about the calculations, but all it does is empty the entrybox.
Also... do you have to "clear" the variable before doing "refresh" ? In gtkdialog2 you did.

### Anyway to get just the escape key to close my PopMenu like most normal child dialogs?

potong
Posts: 88
Joined: Fri 06 Mar 2009, 04:01

#347 Post by potong »

sunburnt:

Checkout the gtkdialog_progressbars example on the first page of this thread.

Potong

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#348 Post by sunburnt »

Thanks potong; So... I take it there`s no way to get gtkdialog to do math inside an <action> tag?
A function call is necessary? It seems if shell commands will run in it, why not the math too?
I can`t think of any reason why it doesn`t work...

Anyway, it looks like the function code: ((CLICKS++)) echos it`s math result to the entrybox.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#349 Post by sunburnt »

I picked up this scrap of code in here somewhere ( I can`t find where I got now...).
<action signal=\"key-press-event\">echo Window: key-press-event</action>
Iplaced it in my popmenu code and: Error in line 2, near token '<action>' : syntax error.

Code: Select all

export $PM_NAME="
  <window decorated=\"$WINDECO\"><action signal=\"key-press-event\">echo Window: key-press-event</action>
  <tree hover-selection=\"true\" headers-visible=\"$HV\" headers-clickable=\"false\">
    <variable>POPMENU</variable>$HEADER
$ITEMS
    <item>Exit_Menu</item>
    <action signal=\"button-release-event\">\$POPMENU</action>
    <action signal=\"button-release-event\">killPS</action>
  </tree></window>"
gtkdialog3 -p $PM_NAME --class POPMENU -G "$popW"x"$popH"+"$popX"+"$popY" &
I`m hoping to detect the Escape key and then kill itself.

DMcCunney
Posts: 889
Joined: Tue 03 Feb 2009, 00:45

#350 Post by DMcCunney »

sunburnt wrote:Thanks potong; So... I take it there`s no way to get gtkdialog to do math inside an <action> tag?
A function call is necessary? It seems if shell commands will run in it, why not the math too?
I can`t think of any reason why it doesn`t work...

Anyway, it looks like the function code: ((CLICKS++)) echos it`s math result to the entrybox.
As I understand it, <action> tags simply spawn sub-shells in which the action is performed, so you ought to be able to do math.

You're doing setHT=$(($setHT+1)) to increment $setHT by 1. What is the value of $setHT before you increment it? How do you know? Is the variable explicitly declared and initialized with a value elsewhere?

The other issue is that I believe the calculation is being performed in a subshell, but the result needs to be passed back up to the parent. You may need to export it to make that happen.
______
Dennis

User avatar
8-bit
Posts: 3406
Joined: Wed 04 Apr 2007, 03:37
Location: Oregon

#351 Post by 8-bit »

DMcCunney wrote:
sunburnt wrote:Thanks potong; So... I take it there`s no way to get gtkdialog to do math inside an <action> tag?
A function call is necessary? It seems if shell commands will run in it, why not the math too?
I can`t think of any reason why it doesn`t work...

Anyway, it looks like the function code: ((CLICKS++)) echos it`s math result to the entrybox.
As I understand it, <action> tags simply spawn sub-shells in which the action is performed, so you ought to be able to do math.

You're doing setHT=$(($setHT+1)) to increment $setHT by 1. What is the value of $setHT before you increment it? How do you know? Is the variable explicitly declared and initialized with a value elsewhere?

The other issue is that I believe the calculation is being performed in a subshell, but the result needs to be passed back up to the parent. You may need to export it to make that happen.
______
Dennis
Like this? I did not do any error checking to show you can have minus values also.

Code: Select all

#! /bin/bash
. sett #create a file called sett containing "setHT=10" in the same directory then run this program.
n=$setHT
echo $n > /tmp/tmp
add() {
n=$(cat /tmp/tmp); echo $(($n+1)) > /tmp/tmp
#echo $n
}
sub(){
n=$(cat /tmp/tmp); echo $(($n-1)) > /tmp/tmp	
#echo $n
}
export -f add sub
export MAIN_DIALOG='
<window title="MATH  SAMPLE">
<vbox>
   <hbox>
     <entry>
      <variable>setHT</variable>
      <input>echo $(cat /tmp/tmp)</input><width>40</width><height>20</height>
     </entry>
   <button>
     <label>Increase</label>
     <action>add</action>
     <action>refresh:setHT</action>
   </button>
   <button>
     <label>Decrease</label>
     <action>sub</action>
     <action>refresh:setHT</action>
   </button>
  </hbox>
  <hbox>
    <button ok></button>
  </hbox>  
</vbox></window>
'
gtkdialog3 --program=MAIN_DIALOG > sett

rm /tmp/tmp
unset MAIN_DIALOG
unset add
unset sub

potong
Posts: 88
Joined: Fri 06 Mar 2009, 04:01

#352 Post by potong »

Here's two more ways to do the same thing:

Code: Select all

#!/bin/bash -a
[ -f /tmp/n ] || echo 'VALUE1="0" VALUE2="0"' >/tmp/n
. /tmp/n
a(){ case $BUTTON in 
        1) echo $((++VALUE1));; 
        2) echo 0;; 
        3) echo $((--VALUE1));; 
        *) echo $VALUE1;; 
    esac; }
b(){ eval $(</tmp/n); echo $VALUE2; }
c(){ echo "((++VALUE2))" >/tmp/n; }
d(){ echo "((--VALUE2))" >/tmp/n; }
e(){ echo "((VALUE2=0))" >/tmp/n; }
gui=$(cat <<EOV|sed 's/#.*//'
<window title="MATH  SAMPLE">
 <vbox>
  <hbox>
   <entry width-request="40" height-request="20">
    <variable>VALUE1</variable>
    <input>a</input>
   </entry>
   <button label="add/subtract" tooltip-text="left/middle/right click to add/zero/subtract">
    <action signal="button-press-event" type="refresh">VALUE1</action>
   </button>
  </hbox>
  <hbox>
   <entry>
    <variable>VALUE2</variable>
    <input>b</input>
   </entry>
   <button label="add">
    <action>c</action>
    <action>refresh:VALUE2</action>
   </button>
   <button label="subtract">
    <action>d</action>
    <action>refresh:VALUE2</action>
   </button>
   <button label="reset">
    <action>e</action>
    <action>refresh:VALUE2</action>
   </button>
  </hbox>
  <hbox>
   <button cancel></button>
  </hbox> 
 </vbox>
</window>
EOV)

gtkdialog3 -p gui|sed -nr '/VALUE(1|2)/p' >/tmp/n
HTH

Potong

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#353 Post by sunburnt »

Thanks guys, math outside the gtkdialog code is fairly straight forward. Seeing what`s possible.

My last post above has code about detecting the Escape key with:

Code: Select all

<action signal=\"key-press-event\">echo Window: key-press-event</action>
Obviously the "echo" action needs to be changed, but it won`t even do that, it errors:
Error in line 2, near token '<action>' : syntax error.
This is the last piece to the popmenu utility / gtkdialog library I`m making.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#354 Post by seaside »

Sunburnt,

Moving the "key-press-event" action to just before the "</window" works -

Code: Select all

export PM_NAME="
  <window decorated=\"$WINDECO\">
  <tree hover-selection=\"true\" headers-visible=\"$HV\" headers-clickable=\"false\">
      <label>Popup</label>
    <variable>POPMENU</variable>$HEADER
$ITEMS
    <item>Exit_Menu</item>
    <action signal=\"button-release-event\">\$POPMENU</action>
    <action signal=\"button-release-event\">killPS</action>
  </tree><action signal=\"key-press-event\">echo Window: key-press-event</action></window>"
gtkdialog3 -p PM_NAME 
I think that these action events only work after a widget or button comes before in the dialog.

Can't wait to see the library - this will be really great. :D

Cheers,
s

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#355 Post by sunburnt »

Thanks seaside; The "key-press-event" works of course, but how to detect the Escape key.
I have it calling a function to do the key detection, it passes the GUI name for "ps" to kill it.

Code: Select all

<action signal=\"key-press-event\">escKEY $PM_GTK</action></window>
Seems to me that the actual key-code would have to be passed to the function. How?

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#356 Post by seaside »

sunburnt wrote:Thanks seaside; The "key-press-event" works of course, but how to detect the Escape key.
I have it calling a function to do the key detection, it passes the GUI name for "ps" to kill it.

Code: Select all

<action signal="key-press-event">escKEY $PM_GTK</action></window>
Seems to me that the actual key-code would have to be passed to the function. How?
sunburnt,

I don't know how to trap a particular key in Gtkdialog and just to take a wild stab I tried. "if $KEYCODE or $KEY = 27" and neither KEYCODE or KEY appear to be returned named variables - and I don't see any reference material or examples either.

You could also just say "hit any key to exit" :D

Cheers,
s
(I was hoping it might return in the same way as "if [ $BUTTON = 3 ]" does for a right-click)

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#357 Post by sunburnt »

Yeah... I looked at trying to detect it in Xwin. also and none of the code worked.
I thought of running a background script that waits for the key press, but I can`t detect ESC.
Read returns a blank, and I can`t see how to convert it to something that can be identified.

Obvious Q... ( it`s retorical ) Why a key press event without the key return? It must do it...

DMcCunney
Posts: 889
Joined: Tue 03 Feb 2009, 00:45

#358 Post by DMcCunney »

sunburnt wrote: I thought of running a background script that waits for the key press, but I can`t detect ESC.
Read returns a blank, and I can`t see how to convert it to something that can be identified.
It's a non-printing character, so will return a blank. What happens if you test for the octal value - \033 ?
______
Dennis

User avatar
mister_electronico
Posts: 969
Joined: Sun 20 Jan 2008, 20:20
Location: Asturias_ España
Contact:

Question about compiling gtkdialog program.

#359 Post by mister_electronico »

I was cheking the link post Amit G

 http://linux.pte.hu/~pipas/gtkdialog/

I love it, compiling in C for first time the Gtkdialog example is in the web and no problem.

But I try other complex exercise :

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(){
FILE *stream;
char line[256];

putenv(

"MAIN_DIALOG="
"<vbox>"
" <frame>"
" <pixmap>"
" <input file>/mnt/home/vatiohm/Signing.png</input>"
" </pixmap>"
" <hbox>"
" <pixmap>"
" <input file>/mnt/home/vatiohm/signing1.png</input>"
" </pixmap>"
" <entry>"
" <default> </default> "
" <variable>Resistor</variable>"
" </entry>"
" <text>"
" <label>o vatios</label>"
" </text>"
" <entry>"
" <default>""</default>"
" <variable>V</variable>"
" </entry>"
" </hbox>"
" <hbox>"
" <pixmap>"
" <input file>/mnt/home/vatiohm/image2.png</input>"
" </pixmap>"
" <entry>"
" <default>""</default>"
" <variable>Corriente</variable>"
" </entry>"
" <text>"
" <label>o vatios</label>"
" </text>"
" <entry>"
" <default>""</default>"
" <variable>Val</variable>"
" </entry>"
" </hbox>"
" <hbox>"
" <pixmap>"
" <input file>/mnt/home/vatiohm/letrero3.png</input>"
" </pixmap>"
" <entry>"
" <variable>Voltaje</variable>"
" <input>echo 'scale=6;$Resistor*$Current' | bc</input>"
" <input>echo 'scale=6;$V/$Current' | bc</input>"
" <input>echo 'scale=6;sqrt($Val*$Resistor)' | bc</input>"
" </entry>"
" <text>"
" <label>Volts</label>"
" </text>"
" </hbox>"
" <hbox>"
" <button>"
" <label>Limpiar</label>"
" <action type='clear'>Voltaje</action><action type='clear'>Resistor</action><action type='clear'>Current</action><action type='clear'>V</action><action type='clear'>Val</action>"
" </button>"
" <button>"
" <label>Calcular</label>"
" <action>refresh:Volt</action>"
" </button>"
" </hbox>"
" </frame>"
"</vbox>");

stream = popen("gtkdialog3 --program=MAIN_DIALOG", "r");
pclose(stream);

}


I compiling like:

gcc exercice1 -o exercice

But give me the error:

Error in line 1, near token '</default>' : sintax error.

The compiler have problem with the " " then I had change for ' ' but anyway give me problem , I don't know how to fix it.

Someone know how fix it.

Thanks.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#360 Post by sunburnt »

seaside; I tried all the codes in the ASCII chart for ESC with "read" and nothing.
But more than that, for the gtkdialog key-press-event, what variable would you test?

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#361 Post by technosaurus »

generate basic xpm images on the fly
http://www.murga-linux.com/puppy/viewtopic.php?t=54661

I just finished writing two scripts for generating percent bars as xpm images (horizontal or vertical bar). It is pretty basic but its bash so adapt it how you like.

usage "$0" XX% name=filename fgcolor=XXX bgcolor=XXX height=XXX width=XXX
colors can be 3 or 6 digit hexadecimal or None

@mrelectronico - try removing the default tags - probably not needed since you have them blank anyways... or put a "\" before your inner quotes
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#362 Post by seaside »

sunburnt wrote:seaside; I tried all the codes in the ASCII chart for ESC with "read" and nothing.
But more than that, for the gtkdialog key-press-event, what variable would you test?
Sunburnt, I decided to take a look at the source code for
gtkdialog3-0.7.20-pe-1 and I could not find any evidence that it returns the actual key pressed - it's seems it's a simple boolean return only.

As far as bash goes this only "sort of" works-

Code: Select all

read -sn 1 Key
case "$Key" in
  $'\e') echo "Escape Key!";;
esac
If you press the "esc" key it returns "Escape Key!" -
Any other key it fails - EXCEPT for function keys. They will
return the "esc" plus additional characters. I have no idea how to get around that. :D

Cheers,
s

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#363 Post by sunburnt »

seaside; That`s what I figured for gtkdialog, no key returned ( too much to hope for...).
The Bash example`s the same one I was working with! And yeah, no way to filter out the function keys...
That kinda leaves us programmers up the creek without a paddle as it were... ( whata surprise! )

DMcCunney
Posts: 889
Joined: Tue 03 Feb 2009, 00:45

#364 Post by DMcCunney »

sunburnt wrote:seaside; That`s what I figured for gtkdialog, no key returned ( too much to hope for...).
The Bash example`s the same one I was working with! And yeah, no way to filter out the function keys...
That kinda leaves us programmers up the creek without a paddle as it were... ( whata surprise! )
Not really. The bash read function is a built-in, that reads an entire line up to an EOL. Filtering F-keys is a matter of parsing the line.

One filter would be to test the length of the string returned by read. If it's an F-Key, it will be longer than one character. F-Keys and arrow keys all use a subset of the ANSI 3.64 specification, where special keys send a string beginning with Esc[. For instance, up arrow probably sends Esc[A.
______
Dennis

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#365 Post by sunburnt »

I thought of that, but this code returns 2 for all characters typed:

Code: Select all

read -sn 1 Key ; echo $Key |wc -c
And "wc -l", "wc -L", and "wc -w" put out the same count no matter what also....

Post Reply