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
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1411 Post by smokey01 »

I've been beaten again by this gtkdialog.

It's an entry widget with a checkbox widget. The entry widget is to enter a password. The checkbox widget should show the password when true and hide it when false.

Code: Select all

#!/bin/sh

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

MAIN_DIALOG='
<window>
	<vbox>
		<frame Not Working>
			<entry visibility="$CHECKBOX">
				<default>Password</default>
				<variable>ENTRY</variable>
			</entry>
			<checkbox>
				<label>Show/Hide Password</label>
				<variable>CHECKBOX</variable>
				<action>echo $CHECKBOX</action>
				<action>if true enable:ENTRY</action>
				<action>if false disable:ENTRY</action>
				<action>refresh:ENTRY</action>
			</checkbox>
		</frame>
		<hbox>
			<button ok></button>
			<button cancel></button>
		</hbox>
	</vbox>
</window>
'
export MAIN_DIALOG

case $1 in
	-d | --dump) echo "$MAIN_DIALOG" ;;
	*) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
I disable/enable the entry widget to make sure it is toggling properly.
I am also echoing the $CHECKBOX variable to make sure it's changing state which it is.

How do I make the tag attribute visibility="$CHECKBOX" update when the checkbox state is changed. I am refreshing the ENTRY widget but that's not doing it.

It's looks simple enough but it's got me stumped.

Thanks.
Attachments
password.png
(7.35 KiB) Downloaded 750 times

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#1412 Post by SFR »

That's how I did this once: you need 2 synced <entry> fields (I used <notebook> as a container for them), one with visibility="false" and the other one with visibility="true".
Then by using a checkbox you can simply switch between them, i.e. <notebook>'s tabs.

Also, having a <notebook> with tabs hidden somehow messes with the layout (empty space beneath buttons), so we need to to hide it initially and show on 'map-event' signal.

Code: Select all

#!/bin/sh

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

MAIN_DIALOG='
<window>
   <vbox>

      <frame Working Now>

         <notebook show-tabs="false" show-border="false" visible="false">
			<entry visibility="false">
				<default>Password</default>
				<variable>ENTRY</variable>
				<input>echo "$ENTRY_PLAIN"</input>
				<action>refresh:ENTRY_PLAIN</action>
			</entry>

			<entry visibility="true">
				<default>Password</default>
				<variable>ENTRY_PLAIN</variable>
				<input>echo "$ENTRY"</input>
				<action>refresh:ENTRY</action>
			</entry>

            <variable>PASS_MODE</variable>
            <input>echo $(( (PASS_MODE + 1) & 1 ))</input>
		</notebook>

         <checkbox>
            <label>Show/Hide Password</label>
            <action>refresh:PASS_MODE</action>
         </checkbox>

      </frame>

      <hbox>
         <button ok></button>
         <button cancel></button>
      </hbox>
   </vbox>

   <action signal="map-event">show:PASS_MODE</action>
</window>
'
export MAIN_DIALOG

case $1 in
   -d | --dump) echo "$MAIN_DIALOG" ;;
   *) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
01micko
Posts: 8741
Joined: Sat 11 Oct 2008, 13:39
Location: qld
Contact:

#1413 Post by 01micko »

While doing a tiny bit of research on Smokey's problem I stumbled upon Michael Czapski's work. (Sorry Grant but at least SFR came up with the goods :P )

This guy is a serious programmer and scratched an itch with gtkdialog.

There might be some useful examples on his blog.

Check it out!
Puppy Linux Blog - contact me for access

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1414 Post by smokey01 »

@SFR that does the job nicely but there must be an easier and shorter method.

Thanks.
SFR wrote:That's how I did this once: you need 2 synced <entry> fields (I used <notebook> as a container for them), one with visibility="false" and the other one with visibility="true".
Then by using a checkbox you can simply switch between them, i.e. <notebook>'s tabs.

Also, having a <notebook> with tabs hidden somehow messes with the layout (empty space beneath buttons), so we need to to hide it initially and show on 'map-event' signal.

Code: Select all

#!/bin/sh

[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog

MAIN_DIALOG='
<window>
   <vbox>

      <frame Working Now>

         <notebook show-tabs="false" show-border="false" visible="false">
			<entry visibility="false">
				<default>Password</default>
				<variable>ENTRY</variable>
				<input>echo "$ENTRY_PLAIN"</input>
				<action>refresh:ENTRY_PLAIN</action>
			</entry>

			<entry visibility="true">
				<default>Password</default>
				<variable>ENTRY_PLAIN</variable>
				<input>echo "$ENTRY"</input>
				<action>refresh:ENTRY</action>
			</entry>

            <variable>PASS_MODE</variable>
            <input>echo $(( (PASS_MODE + 1) & 1 ))</input>
		</notebook>

         <checkbox>
            <label>Show/Hide Password</label>
            <action>refresh:PASS_MODE</action>
         </checkbox>

      </frame>

      <hbox>
         <button ok></button>
         <button cancel></button>
      </hbox>
   </vbox>

   <action signal="map-event">show:PASS_MODE</action>
</window>
'
export MAIN_DIALOG

case $1 in
   -d | --dump) echo "$MAIN_DIALOG" ;;
   *) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
Greetings!

User avatar
smokey01
Posts: 2813
Joined: Sat 30 Dec 2006, 23:15
Location: South Australia :-(
Contact:

#1415 Post by smokey01 »

01micko wrote:While doing a tiny bit of research on Smokey's problem I stumbled upon Michael Czapski's work. (Sorry Grant but at least SFR came up with the goods :P )

This guy is a serious programmer and scratched an itch with gtkdialog.

There might be some useful examples on his blog.

Check it out!
Thanks Mick. A very interesting blog.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1416 Post by MochiMoppel »

SFR wrote:Also, having a <notebook> with tabs hidden somehow messes with the layout (empty space beneath buttons), so we need to to hide it initially and show on 'map-event' signal.
You can avoid this empty space with

Code: Select all

<window resizable="false"> 

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1417 Post by MochiMoppel »

Slightly shorter:

Code: Select all

#!/bin/bash
[ -z $GTKDIALOG ] && GTKDIALOG=gtkdialog 

MAIN_DIALOG=' 
<window> 
<vbox> 
   <frame Also working> 

	   <entry visibility="false"> 
		 <default>Password</default> 
		 <variable>ENTRY</variable> 
		 <input>echo "$ENTRY_PLAIN"</input> 
		 <action>refresh:ENTRY_PLAIN</action> 
	  </entry> 

	  <entry visible="false"> 
		 <default>Password</default> 
		 <variable>ENTRY_PLAIN</variable> 
		 <input>echo "$ENTRY"</input> 
		 <action>refresh:ENTRY</action> 
	  </entry> 

	  <checkbox> 
		 <label>Show/Hide Password</label> 
		 <action>if true hide:ENTRY</action> 
		 <action>if true show:ENTRY_PLAIN</action> 
		 <action>if false show:ENTRY</action> 
		 <action>if false hide:ENTRY_PLAIN</action> 
	  </checkbox> 

   </frame> 

   <hbox> 
	  <button ok></button> 
	  <button cancel></button> 
   </hbox> 
</vbox> 

</window> 
' 
export MAIN_DIALOG 
case $1 in 
	-d | --dump) echo "$MAIN_DIALOG" ;; 
	*) $GTKDIALOG --program=MAIN_DIALOG ;; 
esac

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#1418 Post by SFR »

smokey01 wrote:@SFR that does the job nicely but there must be an easier and shorter method.
Surely it would be nice if there was an easier way, but there isn't, to my knowledge.
You can do it The Right Way™ in pure GTK+ (e.g. via C, Python, etc.) using gtk-entry-set-visibility, but AFAIK Gtkdialog does not implement a mechanism to change widget's specific properties on the fly, except for a couple of them that are common to all widgets (sentitive, visible).

@Mochi: thanks for simplifying it!

Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#1419 Post by don570 »

Tip:

Xdialog has a limit to the number of characters in a message box

but 'backtitle' option allows more lines to shown in window

Example:
Xdialog --left --backtitle "\n$(gettext 'Conversion factor\nUnit A to Unit B')" --msgbox "$MSG1\n\n $MSG2\n\n $MSG3\n\n $MSG4\n\n $MSG5\n\n $MSG6" 0 0
Reference:
http://murga-linux.com/puppy/viewtopic.php?t=114156
_________________________________________________

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#1420 Post by wiak »

SFR wrote: Surely it would be nice if there was an easier way, but there isn't, to my knowledge.
You can do it The Right Way™ in pure GTK+ (e.g. via C, Python, etc.) using gtk-entry-set-visibility, but AFAIK Gtkdialog does not implement a mechanism to change widget's specific properties on the fly, except for a couple of them that are common to all widgets (sentitive, visible).
Gtkdialog does use gtk-entry-set-visibility function but only during first initialisation.

Current gtkdialog (since ver 0.7.21) recognises the sensitive states: true, false, or password during that initialisation. That additional 'password' state really refers to gtk-entry-set-visibility, rather than actual GTK GtkWidget sensitive state, which only includes true or false (grayed out or not).

Adding additional sensitive states and associated logic (as was done for 'password' itself), it is possible to toggle at least some widget states 'on-the-fly' via a checkbox refresh action. For example, I am currently trying out an experimental gtkwialog compile that includes extra sensitive state 'toggle_password' (each time the entry is refreshed via a checkbox). I'm still however considering whether the approach is good and what additional 'sensitive' state name(s) to actually use in practice, before any possible gtkwialog release with that extra functionality. The resultant sh/gtkwialog code is certainly a bit simpler than the hide/show password entry-box workarounds described earlier. The following straightforward code, for example, works with my experimental gtkwialog compile:

Code: Select all

#!/bin/bash
[ -z $GTKDIALOG ] && GTKDIALOG=gtkwialog

MAIN_DIALOG='
<window>
<vbox>
 <frame Also working>

  <entry>
   <sensitive>toggle_password</sensitive>
   <variable>ENTRY</variable>
   <default>Password</default>
  </entry>

  <checkbox>
   <variable>CHKBOX</variable>
   <label>Show/Hide Password</label>
   <action>refresh:ENTRY</action>
  </checkbox>

 </frame>

 <hbox>
  <button ok></button>
  <button cancel></button>
 </hbox>   

</vbox>
</window>
'
export MAIN_DIALOG
case $1 in
   -d | --dump) echo "$MAIN_DIALOG" ;;
   *) $GTKDIALOG --program=MAIN_DIALOG ;;
esac
I'll post further about it if I decide to proceed with/publish that addition; just hesitant to adopt a new strategy (since once formalised hard to take it back...) in case a better approach is found. I'm hesitant because I'm not sure if overloading sensitive with additional states is best mechanism (despite that already being done for 'password' state itself) - it is certainly easy to apply. Might instead be better to have a new 'Directive' for such on-the-fly refresh additions, which I have yet to think of a suitable name for... However, I'm inclined towards these additional 'sensitive' states to toggle between since it involves only a little bit extra code to implement. If you feel you have any use for this facility, let me know since it is not worth further consideration if not likely to be used anyway.

wiak

Note: gif created using weX screencapture along with its utility button to fredx181's gifenc-yad utility. On looking at it, according to checkbox label, I should of course make password show on clicking check box, rather than hide on click...
Attachments
toggle_password.gif
(209.5 KiB) Downloaded 446 times

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#1421 Post by fabrice_035 »

Hi,

I think it's not possible, but question in case of

About button label

Code: Select all

<button>
<variable>okbutton</variable>
<label>"'`date`'"</label>
<action>refresh:okbutton</action>
</button>
The button take value only after load ? Update is not possible ?

Thx

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1422 Post by MochiMoppel »

fabrice_035 wrote:The button take value only after load ? Update is not possible ?
Labels are fixed even before gtkdialog starts.

You could try a work around with an updated button icon:

Code: Select all

#!/bin/bash
update_icon () {
echo '<''svg width="100" height="20"><text x="50%" y="70%" text-anchor="middle">'"$(date +%T)"'</text></svg>' > /tmp/buticon.svg
}; export -f update_icon

update_icon
echo '
<button>
	<variable>BUT</variable>
	<input file>/tmp/buticon.svg</input>
	<action>echo do something useful here</action>
	<action>update_icon</action>
	<action>refresh:BUT</action>
</button>
'|gtkdialog -s
rm /tmp/buticon.svg
Attachments
button_with_svg_icon.png
(1.48 KiB) Downloaded 294 times

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#1423 Post by fabrice_035 »

@MochiMoppel : whaouu, very nice! Respect :shock:

User avatar
vovchik
Posts: 1507
Joined: Tue 24 Oct 2006, 00:02
Location: Ukraine

#1424 Post by vovchik »

Dear Mochi,

Brilliant. And thanks for the example. We can also make it pretty without much effort:

Code: Select all

#!/bin/bash

function update_icon() 
{
echo '<''svg width="300" height="60">' \
		'<rect x="1%" y="1%" rx="5%" ry="10%" width="98%" ' \
		'height="98%" fill="darkorange" stroke="darkred" ' \
		'stroke-width="3.5"/> <text x="50%" ' \
		'stroke="darkred" stroke-opacity="1.0" stroke-width="2.4" ' \
		'fill="white" fill-opacity="1.0" font-weight="800" ' \
		'font-size="50" font-family="Sans" ' \
		'y="78%" text-anchor="middle">'"$(date +%T)"'' \
		'</text></svg>' > /tmp/buticon.svg
}

export -f update_icon
update_icon

echo '
<button>
   <variable>BUT</variable>
   <input file>/tmp/buticon.svg</input>
   <action>echo "Do something useful here..."</action>
   <action>update_icon</action>
   <action>refresh:BUT</action>
</button>
'|gtkdialog -s

rm /tmp/buticon.svg
With kind regards,
vovchik
Attachments
screenshot1.png
(16.19 KiB) Downloaded 271 times
update-button-pup.tar.gz
(1.51 KiB) Downloaded 187 times
screenshot.png
(12.98 KiB) Downloaded 270 times

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#1425 Post by fabrice_035 »

@vovchik : terrrrrible 8)

I not understand functions on timer widget
http://01micko.com/reference/timer.html

Code: Select all

<timer tag_attr="value"...>
	<variable>varname</variable>
	<input>command</input>
	<input file>filename</input>
	<sensitive>state</sensitive>
	<action>activity</action>...
	<action signal="type">activity</action>...
	<action function="type">parameter</action>...
</timer>
I try the "input file" activity, never working
I imagine when the input file change then action signal receive a signal ?

example

Code: Select all

#!/bin/bash

touch /tmp/myfile

echo '
<button>
   <variable>BUT</variable>
   <label>press me</label>
   <action>echo "button down"</action>
   </button>
   
   <timer visible="true" milliseconds="true" file-monitor="true">
   <input file>/tmp/myfile</input>
   <action>echo "working signal"</action>
   </timer> 

'| gtkdialog -s
So, if i change /tmp/myfile with echo "xxx" > /tmp/myfile
There is no signal transmitted

Explain me please.

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1426 Post by MochiMoppel »

I made small changes to your example. Every time the button is pressed, the file /tmp/myfile is updated.
The timer widget monitors this file and, when the file changed, emits a "file-changed" signal. I have no idea why the signal is emitted twice.

IMO using the timer widget in this way makes little sense.
Interesting detail: The widget allows more than one input file. Add an <input file> tag for each file and the timer widget will emit a signal when any of these files change.

Code: Select all

#!/bin/bash
touch /tmp/myfile
echo ' 
 <button> 
    <variable>BUT</variable> 
    <label>press me</label> 
    <action>date > /tmp/myfile</action> 
 </button> 
     
 <timer visible="true" milliseconds="true" file-monitor="true"> 
    <input file>/tmp/myfile</input> 
    <action signal="file-changed">echo "working signal"</action> 
 </timer> 
 '| gtkdialog -s

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#1427 Post by fabrice_035 »

Again thank you MochiMoppel.

And I propose this to answer the question of Argolance.
what do you think about it?

Code: Select all

#!/bin/bash
# 

touch /tmp/enable_button
touch /tmp/disable_button

# rem 

myscript () {

echo "script execute until end and disable button, user must wait"

echo 0 > /tmp/disable_button
#...
sleep 5
#...
echo 0 > /tmp/enable_button
echo "enable now!"

} ; export -f myscript 

echo '
 <button>
    <variable>BUT</variable>
	<label> START </label>
    <action>myscript&</action>
 </button>
     
 <timer visible="true" milliseconds="true" file-monitor="true" >
   <input file>/tmp/disable_button</input>
   <action signal="file-changed">disable:BUT</action>
 </timer>
 
  <timer visible="true" milliseconds="true" file-monitor="true" >
   <input file>/tmp/enable_button</input>
   <action signal="file-changed">enable:BUT</action>
 </timer>

 '| gtkdialog -s

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#1428 Post by MochiMoppel »

Methinks that it will not work.
And since Argolances's question was asked in a different thread you should link to his question or better post in his thread.

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#1429 Post by fabrice_035 »

Perhaps. But i think it's the only way to activate and/or desactivate button while waiting for the end of a script. But i am not expert! Regard.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#1430 Post by Argolance »

Bonsoir,
Is it possible to execute a command when a combobox widget is clicked to select an item in the list (for example to echo some message to any temporary file)? If yes, how?

Code: Select all

<combobox>
 <variable>COMBOBOX</variable>
 <item>First item</item>
 <item>Second item</item>
 <item>Third item</item>
 <action>echo yes > /tmp/tmpfile</action>
</combobox>
Thank you.

Cordialement.

Post Reply