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 Sat 30 Apr 2016, 09:07
All times are UTC - 4
 Forum index » House Training » HOWTO ( Solutions )
Gtkdialog tutorial for beginners
Moderators: Flash, Ian, JohnMurga
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: 4205
Location: Ontario

PostPosted: Wed 03 Feb 2016, 19:24    Post subject:  Gtkdialog tutorial for beginners  

For beginners in programming here is a tutorial in gtkdialog script writing.
gtkdialog is pre-installed in all versions of puppy linux.

Some useful tips for using the terminal...
Memorize
ctrl-a will place cursor at beginning of line
ctrl-e will place cursor at end of line
ctrl-l will clear screen
Up and down arrow keys will enter previous commands.

To study how gtkdialog behaves with a text line fed to it, we will take advantage
of a special option of gtkdialog, -s.

In a terminal a program line is fed into gtkdialog with the echo command.
So type each line in a terminal and press <ENTER> to execute the line
or copy the lines into the terminal with a drag and click of the mouse.
Strong or weak quoting is possible. Here is an example with weak quoting.

Code:
echo "<window><text><label>Hello World</label></text></window>" | gtkdialog -s


A window is formed with text inside. There are two widgets used, a window widget to form a window,
and a text widget to write the text inside the window.
Note that the display text is actually inside label tags. Here is the alternative
method with strong quoting.

Code:
echo '<window><text><label>Hello World</label></text></window>' | gtkdialog -s


So either strong quoting or weak quoting can be used.
However they aren't quite equivalent.
The special characters are treated differently.

As an example type the following as one line and observe that a substitution has occurred.
$World is substituted with the contents of the variable World
Code:
World=MESSAGE; echo "<window><text><label>Hello $World</label></text></window>" | gtkdialog -s


Strong quoting would have resulted in a literal translation i.e. no substitution. You should test this
by changing the line (weak quoting to strong quoting) and running it in a terminal.

Code:
World=MESSAGE; echo '<window><text><label>Hello $World</label></text></window>' | gtkdialog -s


Notice that there is no substitution!!

Warning ! After running this command in your terminal window you must
now close your terminal and launch another terminal window,
since you have assigned the variable World to a value. If you don't
the results are not consistent.
__________________________________________________________________________

The backslash will provide protection of special characters so that a literal translation is made.
In this example Hello \$World will be shown in the window even though weak quoting is being used.

Code:
echo "<window><text><label>Hello \$World</label></text></window>" | gtkdialog -s


_____________________________________________________________________________

Note that if the variable isn't assigned then $World is blank . To see this
be sure to launch a new terminal and type....
Code:
 echo "<window><text><label>Hello $World</label></text></window>" | gtkdialog -s



_______________________________________________________________________

In situations where a variable needs substitution , weak quoting is preferred by some programmers.
However, there is a potential problem. Widgets can have attributes and these attributes must use
weak quoting. They will need protection. Here's an example...
Code:
echo "<window  title=\"Window Title\"><text><label>"Hello World"</label></text></window>" | gtkdialog -s


Note the that the text in the label tag can have weak quotes as well. You mustn't protect those
quotes, unless you want those quotes to show in window.

Another benefit of using weak quoting when feeding gtkdialog is the automatic protection of the apostophe
in the display text of the text widget.

Code:
echo "<window><text><label>Tom's book</label></text></window>" | gtkdialog -s


However protection of the apostrophe is needed in the following example
because the display text is between weak quotes.

Code:
echo "<window><text><label>"Tom\'s book"</label></text></window>" | gtkdialog -s 


Here is another example to show how strong quotes in the text widget label
can have unexpected consequences...

Code:
echo "<window><text><label>'Toms book'</label></text></window>" | gtkdialog -s

________________________________________________________________________________

By putting the text to be fed to gtkdialog in strong quotes avoids the need for protection
of the attributes.

Note that no protection of the attribute is needed in this example...
Code:
echo '<window  title="Window Title"><text><label>Hello World</label></text></window>' | gtkdialog -s



In the following the display text will be Hello $World since no substitution occurs
i.e. there is a literal translation.

Code:
echo '<window><text><label>Hello $World</label></text></window>' | gtkdialog -s   



If you DO want substitution to occur then it is possible with the right quoting...
Code:
World=MESSAGE;echo '<window><text><label>"'Hello $World'"</label></text></window>' | gtkdialog -s


EDIT: As explained by forum member mcewanw in next post the whitespace isn't
properly protected in the above example so weak quoting is needed around text, then
enclose with strong quotes...
Code:
World=MESSAGE;echo '<window><text><label>'"Hello $World"'</label></text></window>' | gtkdialog -s

_______________________________________________________________________

There are 5 predefined buttons possible, cancel , ok , help , yes and no
The cancel button is demonstrated ...
Code:
echo "<window><button cancel></button></window>" | gtkdialog -s

Code:
echo '<window><button cancel></button></window>' | gtkdialog -s


You can make your own button. The most common method is putting a label
on the button...
Code:
echo '<window><button><label>Click Here</label></button></window>' | gtkdialog -s

__________________________________________________________________________

Warning !! When feeding with strong quotes, the hyphen causes problems
as shown by this example...

Code:
echo '<window><text><label>"Tom's book"</label></text></window>' | gtkdialog -s


but there are solutions possible. Here are two possible solutions...
Code:
echo '<window><text><label>'"Tom's book"'</label></text></window>' | gtkdialog -s

Code:
echo '<window><text><label>"'"Tom's book"'"</label></text></window>' | gtkdialog -s


_____________________________________________________________________________________________


By using the -e option with the echo command, the new line command (\n) is possible.

Code:
echo -e '<window  title="Window Title"><text><label>"Hello\n World"</label></text></window>' | gtkdialog -s


Note that the Hello World text needs to be in quotes.

_____________________________________________________________________

The text widget can have width or height requests. Try this example...

Code:
echo '<window  title="Window Title"><text width-request="200"><label>"Hello World"</label></text></window>' | gtkdialog -s

________________________________________________________________

Note that label tags can be replaced with a label attribute if you wish...

Code:
echo '<window  title="Window Title"><text width-request="200" label="Hello World"></text></window>' | gtkdialog -s


_______________________________________________________________________________________

Widgets naturally flow from the top of the window to the bottom of the window.
However in the hbox widget the flow is from the right side of the window
to the left side. Here's an example ...

Code:
echo '<window><hbox><button help></button><button cancel></button></hbox></window>' | gtkdialog -s


Note that the cancel button is on the right side of the window and the help button is to the left.
____________________________________________________________

You now have enough background information to write your first script.
You can now convert the terminal line into a gtkdialog script.
The bash shell should be used to ensure consistent results.

Create a blank text file. Copy the final script into the file and save.
Make sure the script is executable and run
it in the terminal.

Here's how to convert...

Let us convert a terminal line...

Code:
echo '<window><button><label>Click Here</label></button></window>' | gtkdialog -s


The final result...
Code:

#! /bin/bash

export APP='
<window>
    <button>
    <label>Click Here</label>
    </button>
</window>
'
gtkdialog --program=APP



Congratulations ! You are now a gtkdialog programmer.
______________________________________________________________

Additional info:

Inside the label you can have multiple lines, however
this requires weak quoting inside the label tags.
Here is an example. Compare with the previous example.

Code:
 
#!/bin/bash

export APP='
<window>
     <text>
     <label>"Click
Here"</label>
     </text>
</window>
'
gtkdialog --program=APP


_____________________________________________________________

The text widget also supports multiple lines.
As with the previous example, weak quoting will preserve the line endings.

Code:

#!/bin/bash

export APP='
<window>
      <text width-request="300">
      <label>"Click
Here"</label>
      </text>
</window>
'
gtkdialog --program=APP

Last edited by don570 on Mon 08 Feb 2016, 17:00; edited 5 times in total
Back to top
View user's profile Send private message 
mcewanw

Joined: 16 Aug 2007
Posts: 2668
Location: New Zealand

PostPosted: Wed 03 Feb 2016, 21:53    Post subject: Re: Gtkdialog tutorial for beginners  

Just thought I'd comment to help fix an otherwise excellent little tutorial.

In particular:

don570 wrote:
If you DO want substitution to occur then it is possible with the right quoting...
Code:
World=MESSAGE;echo '<window><text><label>"'Hello $World'"</label></text></window>' | gtkdialog -s



In the above case the double quotes around the Hello $World have no purpose at all in practice. Does work as given above, but try putting extra spaces in between the Hello and $World and you should see it is not quite correct. I suggest it could instead be as follows (even spaces between Hello and $World work then):

Code:
World=MESSAGE;echo '<window><text><label>'"Hello $World"'</label></text></window>' | gtkdialog -s


All that I'm doing in above is to close the strong quotes early to allow weak quoting of Hello $Message using double quotes and finally using strong quotes around the remaining code again. But with that explanation in mind, simpler indeed would then be to just use double quotes in such a case:

Code:
World=MESSAGE;echo "<window><text><label>Hello $World</label></text></window>" | gtkdialog -s


Spaces inserted between Hello and $World are then printed as supplied.

William

_________________
SomeOfMyWork: pAVrecord Precord Premote DoMyFile DoMyCommand fokSyfEyeR xhippo-mod flite_hts_pet Pfetch WIAKAPPS Pcreole DebMintDogDevWork
Back to top
View user's profile Send private message Visit poster's website 
step

Joined: 04 May 2012
Posts: 603

PostPosted: Thu 04 Feb 2016, 17:10    Post subject:  

Thanks for writing this tutorial.

I want to add another way to feed gtkdialog -s. I prefer it because it makes the issue of quoting secondary, almost irrelevant.
Code:

VALUE="a very long default value with spaces"
cat << EOF | gtkdialog -s
<window title="enter some text">
  <entry>
    <default>$VALUE</default>
  </entry>
</window>
EOF

You see, you need not worry about quoting $VALUE inside the here-doc, that's how the '<<' redirection syntax is often called.

Now, i wrote that quoting gets almost irrelevant because there still are some things that need quoting, but in the form of _escaping_, with the backslash character. Since here-docs expand variables, when you need to include a literal dollar sign in a here-doc you need to escape it.
Code:

cat << EOF | gtkdialog -s
<label>\$1 please</label>

This gets very important when you're writing <action> statements. In an action statement write var=$VALUE to bind var to the $VALUE set before gtkdialog starts. effectively, var is constant. Write instead echo \$ENTRY when you want to use the widget's own ENTRY variable value, as in
Code:

<entry>
  <default>$VALUE</default>         use $VALUE set before gtkdialog
  <variable>ENTRY</variable>
  <action>echo \$ENTRY >&2</action>  use $ENTRY set by typing into the widget input field
</entry>


There is a way to write a here-doc where everything is escaped, so all $ signs are literal. It isn't very useful for our purpose, but here it is:
Code:

cat << \EOF
 $ $ $
EOF

cat << 'EOF'     alternative form, same result
 $ $ $
EOF


Two neat things about here-docs:
- they're easy to format, kind of self documenting
- they can include sub-shelled commands (TBH, even humble double-quoted strings can, but quoting often gets messy)
Code:

cat << EOF | gtkdialog -s
<window>
  <text>
    <label>"$(for i in A B C; do echo $i; done)"</label>
  </text>
</window>
EOF

This will set label to "A
B
C" and run only once - think of it as a constant constructor.


Why did I need exterior double quotes for $(...) ? Because otherwise gtkdialg would display the last line (C) only. In this case it's gtkdialog interpreting quote marks. The shell isn't involved at all.

Tip: if you use a text editor with rich shell syntax highlighting, the color codes make writing this sort of things much easier. Colors help you spot typos and wrong escapes early.

_________________
Fatdog64-702|+Packages|Findnrun|+forum|OpenMediaVault
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 964
Location: Japan

PostPosted: Fri 05 Feb 2016, 00:05    Post subject:  

don570 wrote:
For beginners in programming here is a tutorial in gtkdialog script writing.

You have already written a very comprehensice Gtkdialog Tutorial Manual (" a manual for Gtkdialog scripting that would be suitable for beginners"), so I wonder where your new tutorial would fit in.

step wrote:
Two neat things about here-docs:
- they're easy to format, kind of self documenting
- they can include sub-shelled commands (TBH, even humble double-quoted strings can, but quoting often gets messy)
Code:

cat << EOF | gtkdialog -s
<window>
  <text>
    <label>"$(for i in A B C; do echo $i; done)"</label>
  </text>
</window>
EOF

Using here documents is a nice idea! Dont't know if they are "self documenting" , but they surely can make quoting much easier.

Only 2 comments:
- You don't need a cat
- Preceding the first here-doc delimiter with a '-' will strip any tabs used for indenting lines. Doesn't make a big difference, but compacter code is always better.
Code:
gtkdialog -s <<-EOF
<window>
   <text>
         <label>"$(for i in A B C; do echo $i; done)"</label>
   </text>
</window>
EOF
Back to top
View user's profile Send private message 
step

Joined: 04 May 2012
Posts: 603

PostPosted: Fri 05 Feb 2016, 12:38    Post subject:  

MochiMoppel wrote:

- Preceding the first here-doc delimiter with a '-' will strip any tabs used for indenting lines.
Bashism, I think. in ash '<< -EOF' makes '-EOF' the here-doc input ending delimiter.

I'm glad you like the here-doc idea. I've adopted and tested it extensively while writing findnrun. One "trick" I discovered with here-docs and gtkdialog is that you can easily add comments:
Code:

# replace 'gtkdialog -s' with 'cat' to view resulting output in the terminal.
gtkdialog -s << DIALOG-END
${REMARK# Using REMARK is just a convention, any non-existing variable name will work.}
${REMARK# The hash swallows all this text, so the text never appears in the final gtkdialog code.}
${REMARK# There are a few limitations, i.e. you can not use a curly braces and single quotes inside a comment.}
<window>
  <entry>${REMARK# input field 1, an inline comment.}
 ...
</window>
DIALOG-END

_________________
Fatdog64-702|+Packages|Findnrun|+forum|OpenMediaVault
Back to top
View user's profile Send private message 
MochiMoppel


Joined: 26 Jan 2011
Posts: 964
Location: Japan

PostPosted: Sat 06 Feb 2016, 00:17    Post subject:  

step wrote:
Bashism, I think
Please think twice.

Quote:
One "trick" I discovered with here-docs and gtkdialog is that you can easily add comments

This is not unique to here-docs and works the same with the other methods, only quoting rules are different (see below). Still too many limitations and causes for errors makes this a questionable approach. Zigbert in his GtkDialog-tips (see chapter 8.) has a better idea.

BTW: In your example REMARK is not a variable. Bash treats it as a - non existing - command and subsequently throws a "REMARK# command not found" error. It would be better to replace it by an existing command, e.g. bash's built-in null command ":"

And don't assume that the '#' starts a comment in your $(...) command substitution construct. If used with a preceding space it has the unpleasant effect of crashing the whole script. In your case bash takes it as part of the command name. You don't see whatever comes after # because this part is regarded as a list of command line parameters for the invalid command REMARK#. But interpretation continues when you add a new command. Try this:
$(REMARK# Not everything after the hash will be ignored as you will hear soon; beep)

Code:
echo '<window>
<text>
 <label>"Hello world"</label>   '$(: this can be used as a comment but actually these are arguments for the null command)'
</text>                         '$(: this is another comment)'
</window>' | gtkdialog -s
Back to top
View user's profile Send private message 
don570


Joined: 10 Mar 2010
Posts: 4205
Location: Ontario

PostPosted: Mon 08 Feb 2016, 17:16    Post subject:  

I edited first post for accuracy about whitespace. Thanks to mcewanw.

___________________________________________________________

Quote:
You have already written a very comprehensice Gtkdialog Tutorial Manual (" a manual for Gtkdialog scripting that would be suitable for beginners"), so I wonder where your new tutorial would fit in.


Gtkdialog Tutorial Manual needs a rewrite. I should warn about
the need to use the bash shell

...and I discovered that in Ubuntu version of
gtkdialog that window widgets and vbox widgets are important to include
or else other types of widgets disappear.

___________________________________________________
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 » House Training » HOWTO ( Solutions )
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.1085s ][ Queries: 11 (0.0082s) ][ GZIP on ]