Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 24 Jul 2014, 13:51
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
GtkDialog - How to Perform function conditionally? [SOLVED]
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
Page 1 of 2 Posts_count   Goto page: 1, 2 Next
Author Message
MochiMoppel


Joined: 26 Jan 2011
Posts: 324
Location: Japan

PostPosted: Sat 07 Jun 2014, 07:58    Post_subject:  GtkDialog - How to Perform function conditionally? [SOLVED]
Sub_title: Restricting function to certain key pressed
 

Assuming that varname is the name of a widget variable, then this will execute the given command only when key K is pressed
Code:
<action signal="key-press-event">[ $KEY_VAL = "0x06b" ] && gxmessage $varname</action>


How can I do this for a function? This will obviously not work:
Code:
<action signal="key-press-event">[ $KEY_VAL = "0x06b" ] && Refresh:varname</action>


Any ideas how this can by done with <action> ?


------------

Edited_times_total
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1039

PostPosted: Sat 07 Jun 2014, 08:59    Post_subject:  

This will work (req. Gtkdialog >= 0.8.3):
Code:
<action signal="key-press-event" condition="command_is_true( [ $KEY_VAL = 0x06b ] && echo true )">Refresh:varname</action>

Btw, if you want quotes around $KEY_VAL or 0x06b, they need to be escaped: \"$KEY_VAL\".

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 324
Location: Japan

PostPosted: Sat 07 Jun 2014, 09:45    Post_subject:  

Domo arigato! That helps me a lot. The && echo true seems to be irrelevant. [ $KEY_VAL = 0x06b ] already returns true, so no need for anything else. IMO echo something always returns true, so there is nothing to gain from it, or is it ?
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1039

PostPosted: Sat 07 Jun 2014, 10:03    Post_subject:  

And does it work for you this way?
It never worked for me, no-go without && echo true, I don't know why...
Perhaps there need to be literal string "true"..? Confused

Anyway, glad could help. Smile

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 324
Location: Japan

PostPosted: Sat 07 Jun 2014, 10:12    Post_subject:  

You are right, it doesn't work this way Embarassed
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1039

PostPosted: Sat 07 Jun 2014, 10:53    Post_subject:  

Ah, there's an explanation on Gtkdialog's wiki (any widget):
Quote:
true means "true", "yes" or a non-zero value, false means "false", "no" or zero, therefore the shell command is expected to echo one of these values to stdout.

One mystery down. Wink

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 324
Location: Japan

PostPosted: Sat 07 Jun 2014, 11:16    Post_subject:  

I was just reading the same in the wiki, but now the mystery continues: How did you find out even without having read the terse documentation? Intuition?
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1039

PostPosted: Sat 07 Jun 2014, 11:34    Post_subject:  

I wish I had such an intuition. Laughing
I guess I saw this for the first time (in practice) in eventmanager, which I was workin on, adding some features at that time:
https://github.com/puppylinux-woof-CE/woof-CE/blob/testing/woof-code/rootfs-skeleton/usr/sbin/eventmanager#L310-L319

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 324
Location: Japan

PostPosted: Sun 08 Jun 2014, 06:41    Post_subject:  

Thanks for the link. Good stuff!

Did your intuitive investigatiion by any chance led you to a solution for key-press events involving modifier keys? Would be nice to perform actions upon pressing Ctrl+K. My guess is that this is the end of the line for <action>.
My current workaround uses <menuitem> with accel-key and accel-mods, packed into an invisible menu bar. Works pretty well, is much more readable and doesn't require gtkdialog4, but after all it's a workaround.
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1039

PostPosted: Sun 08 Jun 2014, 07:22    Post_subject:  

Quote:
Did your intuitive investigatiion by any chance led you to a solution for key-press events involving modifier keys? Would be nice to perform actions upon pressing Ctrl+K. My guess is that this is the end of the line for <action>.

Yes, in this case it was indeed my intuition + /usr/share/doc/gtkdialog/examples/miscellaneous/press_and_release_events example, which is very helpful to determine correct values.

An example:
Code:
#!/bin/bash

# Ctrl+Shift+R to refresh the date

export MAIN='
<window>
  <text>
    <variable>varDATE</variable>
    <input>date</input>
  </text>
  <action signal="key-press-event" condition="command_is_true( [ $(( ${KEY_MOD} & 5 )) = 5 ] && [ $KEY_RAW = 0x1b ] && echo true )">Refresh:varDATE</action>
</window>'

gtkdialog -cp MAIN

Some explanations: SHIFT's state is held by by 0th bit and CTRL state by 2nd bit of $KEY_MOD variable (?????C?S) , so, in order to read their state correctly, we need to discard the rest of bits by logical AND: $(( $KEY_MOD & 5 )).
If we would want SHIFT alone, it would be $(( $KEY_MOD & 1)) = 1, only CTRL - $(( $KEY_MOD & 4 )) = 4 and so on.
(I hope it's clear enough?)

Quote:
My current workaround uses <menuitem> with accel-key and accel-mods, packed into an invisible menu bar. Works pretty well, is much more readable and doesn't require gtkdialog4, but after all it's a workaround.

And this is very good workaround in some cases!
For example, I needed simple terminal that is divided into three sections (upper and lower on first tab and one big on the second) and I used exacly the same method to switch between them (Ctrl+1,2,3).
Using the $KEY... method in this case failed, because, regardless of various combinations that I tried, there was always some undesired character (usually "1") printed in one of the areas, while switching between them.
The only downside is that F10 is always being intercepted by this menu widget, no matter what...

Here's the code, btw, if anyone's interested (req. Gtkdialog >= 0.8.3 with VTE):
Code:
#!/bin/sh

# TriTerm v0.1 by SFR'2014

APPNAME="TriTerm v0.1"
CWD=`pwd`

export INDEX=/dev/shm/triterm_index_${$}
echo 0 > $INDEX
trap 'rm $INDEX' EXIT

#==============================================================================

func_term () {
echo '
<terminal argv0="/bin/bash"
          current-directory-uri="'${CWD}'"
          encoding="UTF-8"
         cursor-shape="0"
          scrollback-lines="10000"
           font-name="10"
         text-background-color="#fff"
           text-foreground-color="#000"
           cursor-background-color="#888"
          hscrollbar-policy="1"
          vscrollbar-policy="0">
  <variable>varTERM_'${1}'</variable>
  <width>83</width>
  <height>16</height>
  <action signal="child-exited">exit:EXIT</action>
</terminal>'
}

#==============================================================================

[ -e "/usr/local/lib/X11/mini-icons/mini-sh.xpm" ] && PIX_PATH="/usr/local/lib/X11/mini-icons/mini-sh.xpm" || PIX_PATH="/usr/share/pixmaps/puppy/terminal.svg"

export MAIN='
<window title="'${APPNAME}'" image-name="'${PIX_PATH}'">
  <vbox space-fill="true" space-expand="true">
    <notebook show-tabs="false">
      <vbox>
        '"`func_term 1`"'
        '"`func_term 2`"'     
      </vbox>
 
      <vbox>
        '"`func_term 3`"'
      </vbox>
 
      <variable>varPAGE</variable>
      <input file>'${INDEX}'</input>
    </notebook>

    <vbox space-fill="false" space-expand="false">
      <menubar height-request="1" width-request="1">
        <menu>
          <menuitem accel-key="0x31" accel-mods="4">
            <label>Top Terminal</label>
            <action>echo 0 > '${INDEX}'</action>
            <action>refresh:varPAGE</action>
            <action>grabfocus:varTERM_1</action>
          </menuitem>
          <menuitem accel-key="0x32" accel-mods="4">
            <label>Bottom Terminal</label>
            <action>echo 0 > '${INDEX}'</action>
            <action>refresh:varPAGE</action>
            <action>grabfocus:varTERM_2</action>
          </menuitem>
          <menuitem accel-key="0x33" accel-mods="4">
            <label>Full Terminal</label>
            <action>echo 1 > '${INDEX}'</action>
            <action>refresh:varPAGE</action>
            <action>grabfocus:varTERM_3</action>
          </menuitem>
          <label>""</label>
        </menu>
      </menubar>
    </vbox>
  </vbox>
<action signal="delete-event">exit:abort</action>
</window>'

#==============================================================================

gtkdialog -p MAIN
exit

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
zigbert


Joined: 29 Mar 2006
Posts: 5647
Location: Valåmoen, Norway

PostPosted: Sun 08 Jun 2014, 13:44    Post_subject:  

Code:
<action signal="key-press-event" condition="command_is_true( [ $(( ${KEY_MOD} & 5 )) = 5 ] && [ $KEY_RAW = 0x1b ] && echo true )">Refresh:varDATE</action>
Like it Smile


Thank you
Sigmund

_________________
Stardust resources
Back to top
View user's profile Send_private_message Visit_website 
MochiMoppel


Joined: 26 Jan 2011
Posts: 324
Location: Japan

PostPosted: Sun 08 Jun 2014, 22:49    Post_subject:  

zigbert, in his GtkDialog-Tips thread, wrote:
Hotkeys
>> An <action> signal inside the <window> widget gives the most direct hotkey
<action signal="key-press-event" condition="command_is_true( [ $(( ${KEY_MOD} & 5 )) = 5 ] && [ $KEY_RAW = 0x1b ] && echo true )">Refresh:varDATE</action>
Some explanations: SHIFT's state is held by by 0th bit and CTRL state by 2nd bit of $KEY_MOD variable (?????C?S) , so, in order to read their state correctly, we need to discard the rest of bits by logical AND: $(( $KEY_MOD & 5 )).
If we would want SHIFT alone, it would be $(( $KEY_MOD & 1)) = 1, only CTRL - $(( $KEY_MOD & 4 )) = 4 and so on.

>> Menuitems can set both accelerator key and modifier (SHIFT is 1, CTRL is 4 and ALT is 8 ). For a full list of accelerator key values check this
<menuitem stock-id="gtk-quit" accel-key="0x51" accel-mods="4">
A hidden menubar gives you nice solution


@zigbert. You are quick. I see that you already edited your valuable GtkDialog-Tips, but frankly I'm not sure if this is understandable for simple minded non-nerds like me. Not everybody is familiar with bitwise logical operations and I am still struggling with SFR's example. I'm sure you like it, but before I can see the beauty in this beast I want to understand it. Maybe you should wait until my problems are solved ( I need 1 or 2 more days for testing) and then, after even MochiMoppel understands this stuff, you can be sure that the explanations are foolproof.

Also the remark about the hidden menubar can be misleading. A hidden menubar is not a nice solution at all because the hotkeys will not work Crying or Very sad The menubar needs to be visible. What I meant in my casual remark is that I use a de-facto invisible menu: 1x1px size. Depending on color theme this single dot will be rendered in the same color as the surrounding area, which makes is invisible. Maybe there are better solutions.

Speaking about your thread, which was the starting point for my gtkdialog endeavors: In the list of menuitem modifier keys, could you please include modifier 0 (zero)? Since hotkeys for menuitem must include modifier keys, the present information gives the impression that single key hotkeys are impossible. It took me quite a while to find out that single hotkeys can be realized with a "no modifier" modifier, e.g hotkey for the single DEL key would be accel-key="0xffff" accel-mods="0"
Also I would like to see a short remark, how to combine modifier keys. There is one in the wiki which I found very nerdy ("1. SHIFT is 1, CTRL is 4 and ALT is 8 and you can OR them together.") Made me feel really stupid since I had no idea how to simply "OR them together". Maybe "add the values" doesn't sound cool enough Wink
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1039

PostPosted: Mon 09 Jun 2014, 08:08    Post_subject:  

I'll try to shed more light on the issue. Wink

Unfortunately, due to technical difficulties (problems with precise aligning), the rest of this post is in the attached .txt file.

Greetings!
KEY_MOD_post.txt.gz
Description  .gz is a fake, it's a plain text
gz

 Download 
Filename  KEY_MOD_post.txt.gz 
Filesize  3.4 KB 
Downloaded  13 Time(s) 

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 324
Location: Japan

PostPosted: Mon 09 Jun 2014, 08:59    Post_subject:  

Excellent! Very Happy
After slowly getting to grips with this stuff the only question that puzzled me was why the (none) mode would always be 00010000. Ahhh...NumLock ! Your table made it clear. As for me this part is clear now. Thanks a lot.

Regarding the key value I'm not sure if KEY_RAW is a good choice as it depends on HW and keyboard layout. KEY_RAW = 0x1b may render an R on a US keyboard, but a P on Dvorak keyboard. So far I used KEY_VAL because I could easily look the value up with xev , but KEY_SYM = R seems so much simpler and readable. Are there reasons to avoid it?

Regarding + /usr/share/doc/gtkdialog/examples: I don't have this installed. It's not standard and it's not included in gtkdialog pets. I do have a local copy on my HD, but it's pretty old and I would love to download the newest version. Unfortunately I can't find a link anywhere. Where can I get it?

So many question. Thanks for being patient.
Back to top
View user's profile Send_private_message 
SFR


Joined: 26 Oct 2011
Posts: 1039

PostPosted: Mon 09 Jun 2014, 09:49    Post_subject:  

Yeah, KEY_SYM/KEY_VAL might be indeed a better choice, just note that if SHIFT is pressed, the output values/characters reflect the difference (unlike KEY_RAW), e.g.:

Only A:
KEY_RAW=0x26, KEY_VAL=0x061, KEY_SYM=a

SHIFT+A:
KEY_RAW=0x26, KEY_VAL=0x041, KEY_SYM=A

/usr/share/doc/gtkdialog/examples should be in devx_xxx.sfs, but here's the online version:
http://gtkdialog.googlecode.com/svn/trunk/examples
http://gtkdialog.googlecode.com/svn/trunk/examples/miscellaneous/press_and_release_events

Btw, I found also this, which describes KEY_MOD values more generally:
http://gtk.php.net/manual/en/html/gdk/gdk.enum.modifiertype.html
(I think we can ignore all stuff >= 256)
[Hmm, if this page is loading way too long, just press "Stop" button in your browser and the contents should appear]

In my case the modifiers, described on the above page, look like:
Code:
sh-4.1# xmodmap -pm
xmodmap:  up to 3 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25),  Control_R (0x6d)
mod1        Alt_L (0x40),  Alt_L (0x7d),  Meta_L (0x9c)
mod2        Num_Lock (0x4d)
mod3     
mod4        Super_L (0x7f),  Hyper_L (0x80)
mod5        Mode_switch (0x5d),  ISO_Level3_Shift (0x71),  ISO_Level3_Shift (0x7c)

sh-4.1#

Can't locate mod4, but mod5 is AltGr on my keyboard.

Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send_private_message 
Display_posts:   Sort by:   
Page 1 of 2 Posts_count   Goto page: 1, 2 Next
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1086s ][ Queries: 12 (0.0036s) ][ GZIP on ]