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 Tue 22 Jul 2014, 23:49
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Togglebutton with SVG images
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [7 Posts]  
Author Message
don570


Joined: 10 Mar 2010
Posts: 3208
Location: Ontario

PostPosted: Wed 12 Feb 2014, 19:41    Post subject:  Togglebutton with SVG images  

Here is an example of a togglebutton using 2 SVG images. Each image
corresponds to different data ---> a list widget is filled with data

One button is used and the label text changes.

The two svg images are renamed to make programming the togglebutton
easier

Code:

cp /tmp/SVG_Description.svg /tmp/true.svg
cp /tmp/SVG_Cities.svg /tmp/false.svg


...since true and false is the variables output.

There is a third svg image created in /tmp/ ---> SVG_Function.svg

This is for a simple button that is hidden when the script is first launched,
then it becomes the 'Function' button.

Note that
Code:
<action>clear:LIST</action>

is needed to stop the list from
growing.



Code:

#!/bin/bash
# make two svg images - labeled Cities and Description
for LABEL in 1 2 3 ;do
[  $LABEL -eq 1 ] && TEXT="Cities List"
[  $LABEL -eq 2 ] && TEXT="Description"
[  $LABEL -eq 3 ] && TEXT="Function"

X1=$(echo "$TEXT"|wc -c)  # number of letters
W=$((${X1}*6*95/31))
DISPLACE=$(($W/7))

echo '<svg version="1.1"> 
      <rect
      style="fill:white;fill-opacity:.7;stroke-width:2;stroke:black;stroke-opacity:1;"
     width='\"$W\"' height="36" rx="10" ry="10" x="1" y="1"/>
           <text style="font-family:DejaVu;font-size:24;fill-opacity:1"
      x='\"$DISPLACE\"'  y="26" >
'$TEXT'
     </text>

</svg>
'>/tmp/SVG_$TEXT.svg

done

cp /tmp/SVG_Description.svg /tmp/true.svg
cp /tmp/"SVG_Cities List.svg" /tmp/false.svg

#ln -sf /tmp/SVG_Description.svg /tmp/togglebutton.svg


mkdir -p /tmp/cities
echo 'High crime rate
Dirty
Skyscrappers' > /tmp/cities/"New York"
cp -f /tmp/cities/"New York" /tmp/cities/default
echo 'Windy
Cold
Sausage factories' > /tmp/cities/Chicago
echo 'Sunny
Dry
Hollywood' > /tmp/cities/"Los Angeles"
echo 'New York
Chicago
Los Angeles' > /tmp/cities/CITY_LIST
echo CITY_LIST > /tmp/cities/city

function function_switch {
   
   if [ "$(cat /tmp/cities/city)" = CITY_LIST ];then
   echo "New York" > /tmp/cities/city
   else
   echo CITY_LIST > /tmp/cities/city
   fi
}
export -f function_switch

function function_switch2 {
   
   if [ "$LIST" = "NEW York" ];then
   echo "New York" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
   
   if [ "$LIST" = "Chicago" ];then
   echo "Chicago" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
   
   if [ "$LIST" = "Los Angeles" ];then
   echo "Los Angeles" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
}
export -f function_switch2

export MAIN_DIALOG='
<window title="Toggle text" border-width="20">
   <vbox>   
            <list selected-row="0">
            <variable>LIST</variable>
            <height>200</height>
            <width>250</width>            
            <input file>/tmp/cities/default</input>                                 
         </list>   
      <hbox>   
      
              <button>
                     <variable>BUTTON</variable>
                     <input file>/tmp/SVG_Function.svg</input>
                     <action>xmessage  "You clicked a button - $LIST" &</action>
              </button>            
            
                <togglebutton active="true">
                       <variable>togglebutton</variable>                    
                  <action>ln -sf /tmp/"$togglebutton".svg /tmp/togglebutton.svg</action>
                  <action>function_switch2</action>                  
                  <action>cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default</action>                  
                  <action>function_switch</action>
                     <action>if true hide:BUTTON</action>
                     <action>if false show:BUTTON</action>                               
                  <action>clear:LIST</action>
                  <action>refresh:LIST</action>
                  <action>refresh:togglebutton</action>                                    
                  <input file>/tmp/togglebutton.svg</input>
            </togglebutton>
      </hbox>
      
   </vbox>
   <variable>MAIN_DIALOG</variable>
</window>'

gtkdialog --program=MAIN_DIALOG



Last edited by don570 on Sat 15 Feb 2014, 15:00; edited 1 time in total
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2500
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Thu 13 Feb 2014, 04:42    Post subject: Re: Togglebutton with SVG images  

I am thinking about
Code:
for LABEL in $(gettext 'Cities')  $(gettext('Description') $(gettext 'Function');do
now Cool

that is really useful.

Thank you don570
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 3208
Location: Ontario

PostPosted: Sat 15 Feb 2014, 15:10    Post subject:  

I decided to rewrite the script -----> one line wasn't needed
so I commented it out

Code:
#ln -sf /tmp/SVG_Description.svg /tmp/togglebutton.svg


and I realised that a small change would allow white space in the
button labels

Code:

for LABEL in 1 2 3 ;do
[  $LABEL -eq 1 ] && TEXT="Cities List"
[  $LABEL -eq 2 ] && TEXT="Description"
[  $LABEL -eq 3 ] && TEXT="Function"



The three SVG images are generated in /tmp

Code:
'>/tmp/SVG_$TEXT.svg 



The revised script is available in the first post.

_______________________________

Note to L18L : Apparently whitespace in the variable LABEL isn't
important when looping?? You have more experience in gettext than me.

Code:
for LABEL in $(gettext 'Cities')  $(gettext('Description') $(gettext 'Function');do
Back to top
View user's profile Send private message 
L18L

Joined: 19 Jun 2010
Posts: 2500
Location: Burghaslach, Germany somewhere also known as "Hosla"

PostPosted: Sun 16 Feb 2014, 07:57    Post subject:  

don570 wrote:
Apparently whitespace in the variable LABEL isn't
important when looping?
Code:
for LABEL in $(gettext 'Cities')  $(gettext('Description') $(gettext 'Function');do


Code:
for LABEL in "$(gettext 'Cities')"  "$(gettext('Description')" "$(gettext 'Function')";do
should work also for whitespaced items.

I have been playing with your script:
Code:
#!/bin/sh
# make two svg images - labeled Cities and Description
export TEXTDOMAIN=togglebuttons

for LABEL in 1 2 3 ;do
[  $LABEL -eq 1 ] && TEXT="$(gettext 'Cities List')"
[  $LABEL -eq 2 ] && TEXT="$(gettext 'Description')"
[  $LABEL -eq 3 ] && TEXT="$(gettext 'Function')"

X1=$(echo "$TEXT"|wc -c)  # number of letters
W=$((${X1}*6*95/31))
DISPLACE=$(($W/7))

echo '<svg version="1.1">
      <rect
      style="fill:white;fill-opacity:.7;stroke-width:2;stroke:black;stroke-opacity:1;"
     width='\"$W\"' height="36" rx="10" ry="10" x="1" y="1"/>
           <text style="font-family:DejaVu;font-size:24;fill-opacity:1"
      x='\"$DISPLACE\"'  y="26" >
'$TEXT'
     </text>

</svg>
'>/tmp/SVG_$TEXT.svg

done

#cp /tmp/SVG_Description.svg /tmp/true.svg
cp /tmp/SVG_"$(gettext 'Description')".svg /tmp/true.svg
#cp /tmp/"SVG_Cities List.svg" /tmp/false.svg
cp /tmp/SVG_"$(gettext 'Cities List')".svg /tmp/false.svg

#ln -sf /tmp/SVG_Description.svg /tmp/togglebutton.svg


mkdir -p /tmp/cities
echo "$(gettext 'High crime rate
Dirty
Skyscrappers')" > /tmp/cities/"New York"
cp -f /tmp/cities/"New York" /tmp/cities/default
echo "$(gettext 'Windy
Cold
Sausage factories')" > /tmp/cities/Chicago
echo "$(gettext 'Sunny
Dry
Hollywood')" > /tmp/cities/"Los Angeles"
echo 'New York
Chicago
Los Angeles' > /tmp/cities/CITY_LIST
echo CITY_LIST > /tmp/cities/city

function function_switch {
   
   if [ "$(cat /tmp/cities/city)" = CITY_LIST ];then
   echo "New York" > /tmp/cities/city
   else
   echo CITY_LIST > /tmp/cities/city
   fi
}
export -f function_switch

function function_switch2 {
   
   if [ "$LIST" = "NEW York" ];then
   echo "New York" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
   
   if [ "$LIST" = "Chicago" ];then
   echo "Chicago" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
   
   if [ "$LIST" = "Los Angeles" ];then
   echo "Los Angeles" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
}
export -f function_switch2


export MAIN_DIALOG='
<window title="Toggle text" border-width="20">
   <vbox>   
            <list selected-row="0">
            <variable>LIST</variable>
            <height>200</height>
            <width>250</width>           
            <input file>/tmp/cities/default</input>                                 
         </list>   
      <hbox>   
     
              <button>
                     <variable>BUTTON</variable>
                     <input file>/tmp/SVG_'$(gettext 'Function')'.svg</input>
                     <action>MSG="'$(gettext 'You clicked a button')'";xmessage "$MSG - $LIST" &</action>
              </button>           
           
                <togglebutton active="true">
                       <variable>togglebutton</variable>                   
                  <action>ln -sf /tmp/"$togglebutton".svg /tmp/togglebutton.svg</action>
                  <action>function_switch2</action>                 
                  <action>cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default</action>                 
                  <action>function_switch</action>
                     <action>if true hide:BUTTON</action>
                     <action>if false show:BUTTON</action>                               
                  <action>clear:LIST</action>
                  <action>refresh:LIST</action>
                  <action>refresh:togglebutton</action>                                   
                  <input file>/tmp/togglebutton.svg</input>
            </togglebutton>
      </hbox>
     
   </vbox>
   <variable>MAIN_DIALOG</variable>
</window>'

gtkdialog --program=MAIN_DIALOG
...
fun

changed "Städteliste" to "Liste der Städte" to have some whitespace. It worked using "$(gettext '... ... ...')"

Hope to see your fr_CA localization soon.
togglebuttons.png
 Description   Quiz: which city?
 Filesize   5.02 KB
 Viewed   175 Time(s)

togglebuttons.png

Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 3208
Location: Ontario

PostPosted: Tue 18 Feb 2014, 16:23    Post subject:  

I finished the gettext version of script. I made it as general as possible
so that it can be converted to other uses.


Code:

#!/bin/sh
# make two svg images - labeled Cities and Description
export TEXTDOMAIN=Script_togglebutton.sh
export OUTPUT_CHARSET=UTF-8
for LABEL in 1 2 3 ;do
[  $LABEL -eq 1 ] && TEXT="$(gettext 'Cities List')"
[  $LABEL -eq 2 ] && TEXT="$(gettext 'Description')"
[  $LABEL -eq 3 ] && TEXT="$(gettext 'Function')"

X1=$(echo "$TEXT"|wc -c)  # number of letters
W=$((${X1}*6*95/31))
DISPLACE=$(($W/7))

echo '<svg version="1.1"> 
      <rect
      style="fill:white;fill-opacity:.7;stroke-width:2;stroke:black;stroke-opacity:1;"
     width='\"$W\"' height="36" rx="10" ry="10" x="1" y="1"/>
           <text style="font-family:DejaVu;font-size:24;fill-opacity:1"
      x='\"$DISPLACE\"'  y="26" >
'$TEXT'
     </text>

</svg>
'>/tmp/SVG_$TEXT.svg

done

cp /tmp/SVG_"$(gettext 'Description')".svg /tmp/true.svg
cp /tmp/SVG_"$(gettext 'Cities List')".svg /tmp/false.svg

#ln -sf /tmp/SVG_Description.svg /tmp/togglebutton.svg


mkdir -p /tmp/cities
echo "$(gettext 'High crime rate
Dirty
Skyscrappers')" > /tmp/cities/"$(gettext 'New York')"
cp -f /tmp/cities/"$(gettext 'New York')" /tmp/cities/default
echo "$(gettext 'Windy
Cold
Sausage factories')" > /tmp/cities/"$(gettext 'Chicago')"
echo "$(gettext 'Sunny
Dry
Hollywood')" > /tmp/cities/"$(gettext 'Los Angeles')"
echo "$(gettext 'New York')
$(gettext 'Chicago')
$(gettext 'Los Angeles')" > /tmp/cities/CITY_LIST
echo CITY_LIST > /tmp/cities/city

function function_switch {
   
   if [ "$(cat /tmp/cities/city)" = CITY_LIST ];then
   echo "$(gettext 'New York')" > /tmp/cities/city
   else
   echo CITY_LIST > /tmp/cities/city
   fi
}
export -f function_switch

function function_switch2 {
   
   if [ "$LIST" = "$(gettext 'New York')" ];then
   echo "$(gettext 'New York')" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
   
   if [ "$LIST" = "$(gettext 'Chicago')" ];then
   echo "$(gettext 'Chicago')" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
   
   if [ "$LIST" = "$(gettext 'Los Angeles')" ];then
   echo "$(gettext 'Los Angeles')" > /tmp/cities/city
   cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default
   fi
}
export -f function_switch2

export MAIN_DIALOG='
<window title="'$(gettext 'Toggle text')'" border-width="20">
   <vbox>   
            <list selected-row="0">
            <variable>LIST</variable>
            <height>200</height>
            <width>250</width>            
            <input file>/tmp/cities/default</input>                                 
         </list>   
      <hbox>   
      
              <button>
                     <variable>BUTTON</variable>
                     <input file>/tmp/SVG_Function.svg</input>
                     <action>xmessage  "You clicked a button - $LIST" &</action>
              </button>            
            
                <togglebutton active="true">
                       <variable>togglebutton</variable>                    
                  <action>ln -sf /tmp/"$togglebutton".svg /tmp/togglebutton.svg</action>
                  <action>function_switch2</action>                  
                  <action>cp -f /tmp/cities/"$(cat /tmp/cities/city)" /tmp/cities/default</action>                  
                  <action>function_switch</action>
                     <action>if true hide:BUTTON</action>
                     <action>if false show:BUTTON</action>                               
                  <action>clear:LIST</action>
                  <action>refresh:LIST</action>
                  <action>refresh:togglebutton</action>                                    
                  <input file>/tmp/togglebutton.svg</input>
            </togglebutton>
      </hbox>
      
   </vbox>
   <variable>MAIN_DIALOG</variable>
</window>'

gtkdialog --program=MAIN_DIALOG



Last edited by don570 on Tue 18 Feb 2014, 16:32; edited 1 time in total
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 3208
Location: Ontario

PostPosted: Tue 18 Feb 2014, 16:29    Post subject:  

On the subject of white space....

Put some files with white space in your home folder
and run this script. The double quotes protect the white space.
Apparently it only goes around the loop once.

Afterwards remove the double quotes and try again. It will loop but it will not
show the filenames with white space properly.

Code:

#!/bin/sh
for file in "$(ls)";do
echo  "file = $file"
done
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 3208
Location: Ontario

PostPosted: Thu 20 Feb 2014, 21:22    Post subject: unit conversion  

I've made a practical script for unit conversion.

It modifies the above script...

You can test it here....

http://murga-linux.com/puppy/viewtopic.php?p=760041#760041

_________________________________________________
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [7 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1264s ][ Queries: 13 (0.0054s) ][ GZIP on ]