gtk: how to put state enable/disable a entry with function?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

gtk: how to put state enable/disable a entry with function?

#1 Post by arivas_2005 »

Hi
I want to use enable and disable property using function.
in the following example, I can not do it
I can only clean the boxes but not set it to disable B entry

Code: Select all

#!/bin/sh

RIGHT=550 DOWN=36 WIDTH=600 HEIGHT=150   # define variables with defaults

function Onlyfile() {
	length_A=${#ORIGINALFILE}
	echo ${ORIGINALFILE##*"/"} | awk -F"." '{print $1}'>/tmp/pdffile
}
function init_2() {	
	length_A=${#ORIGINALFILE}
	#Xdialog --msgbox $ORIGINALFILE" \n"$length_A x
	if [ "$length_A" -eq 0 ]; then
	Xdialog --msgbox "$ORIGINALFILE "" empty" x;
	echo '<action>refresh:ARCHIVO_T</action>
     <action>disable:PDFFILE</action>
	 <action>refresh:PDFFILE</action>' ; 
	else		      
		Xdialog --msgbox "$ORIGINALFILE"" -->"  x;
         echo '<action>refresh:PDFFILE</action>
	    <action>enable:PDFFILE</action>	
       <action>refresh:PDFFILE</action>';
	fi
}

export -f init_2
export -f Onlyfile

export MAIN_DIALOG='
<window title="Learning gtk: modify boxs enable disable and comments ">
   <vbox>
       <text use-markup="true">
         <label>"<span color='"'blue'"' font-family='"'mono'"' weight='"'bold'"' size='"'x-large'"'><big>Learning kgt I</big></span>"
         </label>
        </text>   
       <text use-markup="true">
          <label>"<span color='"'brown'"' weight='"'bold'"' size='"'large'"' >drag ando drop a file to box A</span>"
         </label>
    </text>
   
      <hbox>
         <text label="A" xalign="0"></text>
         <entry>
            <variable>ORIGINALFILE</variable>
            <input>cat /tmp/original</input>
            <action>refresh:PDFFILE</action>
            <action signal="changed">Onlyfile</action>
            <action signal="changed">init_2</action>
            <action>eval ORIGINALFILE</action>
            <action>refresh:PDFFILE</action>
         </entry>
      </hbox>
      
      <hbox>
         <text label="B" xalign="0"></text>      
         <entry>
            <variable>PDFFILE</variable>
            <input>cat /tmp/pdffile</input>         
         </entry>
      </hbox>
      
      <hbox>
         <button>
            <label>Clear A and B</label>
            <action>echo "" /tmp/original</action>
            <action>echo "" /tmp/pdffile</action>
            <action signal="changed">init_2</action>
             <action>refresh:ORIGINALFILE</action>
         </button>
         <button>
            <label>Refresh B</label>
            <action>Onlyfile</action>
            <action>refresh:PDFFILE</action>
         </button>
         <button>
            <label>Exit</label>
             <action>exit:Exit</action>
         </button>         
     </hbox>
    
   </vbox>

</window>
'
I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog -p MAIN_DIALOG -G ${1-${WIDTH}x${HEIGHT}+${RIGHT}+${DOWN}}); do
   eval $STATEMENTS
done
IFS=$I
help in:
1) I need to convert to disable box entry B when the script starts
2) I need to convert to disable entry B when clicking on button 'Clear A and B'
3) How do I include comments in the code?
Iuse:
<!-- comment here -->
'$(rem comment here)'
# comment here,
but Not Works
Thank you!
Last edited by arivas_2005 on Sat 07 Oct 2017, 03:19, edited 2 times in total.

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

#2 Post by MochiMoppel »

1) Please respond to your old thread before you start a new one
2) Do not put the same question into 2 different threads
3) Do not put multiple questions into 1 thread
4) Keep your sample code as simple as possible, just enough to demonstrate your problem.
5) Once again: Pay attention to error messages! A "command not found" message should tell you that something is seriously wrong with your code.
6) Avoid
  • for STATEMENTS in $(gtkdialog -p MAIN_DIALOG ); do
    eval $STATEMENTS
    done
constructs when you don't need them. In your case prevents you from seeing that many of your echo commands do not work as you might have expected.
7) Use something like I=$IFS; IFS="" only if you know what you are doing.
8) Study the examples that come with the Gtkdialog documentation.
9) Study zigbert's GtkDialog - tips thread. Already the first post will answer your question re. comments.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#3 Post by arivas_2005 »

regards
I will take into account the suggestions
and thank you for your contributions
-- I also modified the heading, more specific --
Excuse my insistence :oops:
I have already simplified my sample, so
(I explain with image what I am looking for)

Code: Select all

#!/bin/sh
echo "">/tmp/original
echo "" >/tmp/pdffile
function init_2() {	
	length_A=${#ORIGINALFILE}
	if [ "$length_A" -eq 0 ]; then	 
	 echo '<action>disable:PDFFILE</action>
	       <action>refresh:PDFFILE</action>';
	       echo "empty"
	else		      
       echo '<action>enable:PDFFILE</action>
	       <action>refresh:PDFFILE</action>';
	   echo "$ORIGINALFILE"
	fi
}
export -f init_2
export MAIN_DIALOG='
<window>
   <vbox>    
         <entry>
            <variable>ORIGINALFILE</variable>
            <input>cat /tmp/original</input>
            <action signal="changed">init_2</action>
            <action>refresh:PDFFILE</action>
         </entry>
         <entry>
            <variable>PDFFILE</variable>
            <sensitive>false</sensitive>
            <input>cat /tmp/pdffile</input>     
         </entry>      
         <button>
            <label>Clear A and B</label>
            <action>echo "">/tmp/original</action>
			<action>echo "" /tmp/pdffile</action>
			<action>refresh:ORIGINALFILE</action>
         </button>    
   </vbox>
</window>
'
gtkdialog --program=MAIN_DIALOG

exit
Or it is impossible, as happens with alignment (previous case)
Thanks you
Attachments
boxs-text.png
(6 KiB) Downloaded 227 times

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

#4 Post by MochiMoppel »

Aaah, that's much better...I mean, the code is better digestible now. I'm still not sure if I understand what you are trying to do.
arivas_2005 wrote:Excuse my insistence :oops:
Your admirable insistence is what keeps me going :lol:
Or it is impossible, as happens with alignment (previous case)
What is "it"? If you mean "enable/disable a entry with function" as indicated in your title and your code, then yes, it's not possible. You can't execute a gtkdialog function (e.g. enable:PDFFILE) from an external function and you can't change the gtkdialog code after starting gtkdialog, so this will not work:
function init_2() {
echo '<action>disable:PDFFILE</action>
<action>refresh:PDFFILE</action>';


The good news is that en/disabling or clearing an <entry> widget is much easier than you probably think. I figure that the only reason for your reading and writing (almost) empty temp files is your attempt to clear the widgets.

Try this. No function or temp file required:

Code: Select all

#!/bin/sh
export MAIN_DIALOG=' 
<window> 
<vbox>    
	  <entry> 
		 <variable>ORIGINALFILE</variable> 
		 <action condition="command_is_true( [[ -z $ORIGINALFILE ]] && echo true )">disable:PDFFILE</action>  
		 <action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">enable:PDFFILE</action>  
		 <action>refresh:PDFFILE</action> 
	  </entry> 
	  <entry> 
		 <variable>PDFFILE</variable> 
		 <sensitive>false</sensitive> 
		 <input>basename "$ORIGINALFILE"</input>      
	  </entry>      
	  <button> 
		 <label>Clear A and B</label> 
		 <action>clear:ORIGINALFILE</action> 
		 <action>clear:PDFFILE</action> 
	  </button>    
</vbox> 
</window> 
' 
gtkdialog --program=MAIN_DIALOG 

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#5 Post by arivas_2005 »

ja! that is power. Great ---
(how easy it seems .... that is enable/disable for me)

Code: Select all

<action condition="command_is_true( [[ -z $ORIGINALFILE ]] && echo true )">disable:PDFFILE</action> 
       <action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">enable:PDFFILE</action>

a small explanation: what they do [[ -z $ORIGINALFILE ]] and [[ -n $ORIGINALFILE ]]
--------------------------------
my litle history:
I need to correct many names
drag the original name to the first entry
I correct it in the second (entry), and so the cycle.

is disable the second, to not drop the name there by mistake
and save time
thanks! .. MochiMoppel

in my work there is a lot of information to review and correct, to file, etc.
and I'm not a professional programmer
but I create script to help me in my work
use any example as a base and modify it
(I think there are many who do the same)
...
use google translate to compose questions
(I will learn English someday :oops: )
You have supported me a lot
Thank you again :D[/code]

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

#6 Post by MochiMoppel »

arivas_2005 wrote:I need to correct many names
drag the original name to the first entry
I correct it in the second (entry)
Why don't you correct in the first entry? Why 2 entries?
And since you have to do this with many names: Why drag&drop and clear button? That makes the process slow. If you could paste the original name into the entry field with middle mouse button you wouldn't even need to clear the field first. This could be done automatically before pasting.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#7 Post by arivas_2005 »

Hi
I use the second (entry B) because when I'm wrong to correct,
then, I use refreshment of the first one (entry A). (use the refresh button of the code above)
I avoid dragging the same name twice.

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

#8 Post by MochiMoppel »

So you are using an additional entry as a kind of clipboard? Why not use the real clipboard?

With a single entry box:
1) Select a name (from a list?)
2) Press Ctrl+C to put it into the clipboard
3a) Move your mouse pointer over the entry field and click middle mouse button. This will paste the selected name.
3b) Alternatively use drag&drop or Ctrl+V to fill the entry field (IMO less comfortable and slower)
4) Make changes to the entry
5) If you make a mistake, e.g. delete a portion that you wanted to keep, dblclick the entry to select it and press Ctrl+V. This will revert it to the original name.
6) When everyting is OK, press Enter to start whatever action needs to be done. One of the actions could be to clear the entry field, to make it ready for the next name.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#9 Post by arivas_2005 »

Thanks for the suggestions. I will practice it.
regards.

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#10 Post by arivas_2005 »

Hello
I have doubts. I will ask one by one according to the rule
in the line:

Code: Select all

<input>basename $ORIGINALFILE | awk -F"." '{print $1}'</input>
how it is processed ' (single quote) to avoid errors in '{print $1}', or is not possible.
'"{print $1}"' "'{print $1}'" "'"{print $1}"'" not works or produce error
Thanks!

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

#11 Post by MochiMoppel »

Welcome to quotation hell :twisted:

Code: Select all

<input>basename "$ORIGINALFILE" | awk -F"." '\''{print $1}'\''</input>
Beware that you also need to quote $ORIGINALFILE unless you are dead sure that $ORIGINALFILE contains no space character.

Alternatively you could use bash substring expansion, which requires no quotes at all:

Code: Select all

<input>F=${ORIGINALFILE##*/}; echo ${F%%.*}</input>
Example:
export ORIGINALFILE='/some/sample dir/foo bar.tar.gz'
${ORIGINALFILE##*/} # Strips every character, starting from left, up to last '/' ; Result: foo bar.tar.gz
${F%%.*} # Strips every character, starting from right, up to the last '.' ; Result foo bar

arivas_2005
Posts: 212
Joined: Sun 25 Feb 2007, 14:39

#12 Post by arivas_2005 »

Hello.
here again! :oops:
Welcome to quotation hell
for me, breaking head .. it's an impossible!!
quotation ....it is more than a discipline! .. it's a science!

and, continuing with the questions
-- I need to put the cursor directly in the B box(second entry).
I already did this:
-modified window title

Code: Select all

<window title="Modified_Names"  width-request="400">
-I defined a function with xdotool:

Code: Select all

Wactive () {
	xdotool windowactivate $(xdotool search --name "Modified_Names")
	xdotool click --delay 2 1
	xdotool key Tab Right
}
export -f Wactive
-add the following line in ORIGINALFILE entry:

Code: Select all

<action condition="command_is_true( [[ -n $ORIGINALFILE ]] && Wactive )">refresh:PDFFILE</action>
its works!
information source: your support: MochiMoppel, forum programmer puppylinux and google google...

but, it's possible to do it in gtk action commands?
thanks again for your help!

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

#13 Post by MochiMoppel »

Code: Select all

<entry> 
	<variable>ORIGINALFILE</variable> 
	<action condition="command_is_true( [[ -z $ORIGINALFILE ]] && echo true )">disable:PDFFILE</action>  
	<action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">enable:PDFFILE</action>  
	<action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">grabfocus:PDFFILE</action>  
	<action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">refresh:PDFFILE</action>  
</entry>
The gtkdialog grabfocus function always puts the text cursor in front.
If you need the cursor at the end you could use xdotool like this:

Code: Select all

<entry> 
	<variable>ORIGINALFILE</variable> 
	<action condition="command_is_true( [[ -z $ORIGINALFILE ]] && echo true )">disable:PDFFILE</action>  
	<action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">enable:PDFFILE</action>  
	<action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">refresh:PDFFILE</action>  
	<action condition="command_is_true( [[ -n $ORIGINALFILE ]] && echo true )">xdotool key Tab Right</action>  
</entry>

Post Reply