Using Pango Text Markup Language in Gnocl Widget Labels

WJG (23/11/08) One of the many enhanced formatting features of Gtk widgets is the use of the Pango Text Markup Language for text formatting [L1 ]. Whilst this isn't a feature that the developer will want to use with every label, it has its uses when colour or font effects are desirable. Here's a brief gnocl sample derived from a recent posting of Tk code taken from the Tcl newsgroup.


http://wjgiddings.googlepages.com/pangoTML.png


And here's to code to produce this:

 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" "[email protected]"

 package require Gnocl

 set etitle "Tcl/Tk 8.5"

 proc nullcmd {} {
   global type
   puts -nonewline "8.5 "
   puts $type
 }

 #1) create buttons, format strings with markup code
 set lab [gnocl::label -text  {%<<span background = "springgreen2"><b>RADIOBUTTONS </b></span>} ]
 set rb1 [gnocl::radioButton -text {%<<span background = "palegreen1">serial       </span>} -variable type -onValue "serial"    -onToggled nullcmd]
 set rb2 [gnocl::radioButton -text {%<<span background = "palegreen1">-non-Serial  </span>} -variable type -onValue "newnonser" -onToggled nullcmd]
 set rb3 [gnocl::radioButton -text {%<<span background = "palegreen1">cross-Ref    </span>} -variable type -onValue "crossref"  -onToggled nullcmd]
 set rb4 [gnocl::radioButton -text {%<<span background = "palegreen1">CODEN Only   </span>} -variable type -onValue "codenonly" -onToggled nullcmd]
 set cb1 [gnocl::checkButton -text {%<<span background = "palegreen1">CheckButton  </span>} ]

 #2) create container and add the buttons
 set box [gnocl::box -orientation vertical]
 $box add [list $lab $rb1 $rb2 $rb3 $rb4 $cb1 ]

 #3) create toplevel and add the widgets in one drop
 set win [gnocl::window -title $etitle -child $box -onDelete {exit} ]
 gnocl::mainLoop

By way of comparison, here's a screengrab of the layout that the Tk code posted produced running on my Linux box, mmm... very retro...

http://wjgiddings.googlepages.com/tkSample.png

ZB Were there any problems with using Ttk (instead of "ordinary" Tk)?

WJG (24/11/08) Problems,no. The Tk script ran smoothly. The Gtk libraries now offer more resources than Tk so, if someone is developing new apps or utilities running on Linux boxes, why not use these instead?

ZB So, actually, why did you compare "look & feel" of GTK-widgets with oldest Tk, as if there were no Ttk? It's not fair comparison.

WJG Of course its a fair comparison. This is the native look and feel of Tk. From where I stand, Ttk is a 'band-aid'.

DKF: The interesting things to compare here are not that the capabilities are different, but rather that the gnome engine can do this by embedded markup fragments rather than options.

WJG: Exactly, this is the point. Gtk has more to offer. Use Tcl packages to harness new resources, fashionable resources, rather that staying with the past. I take your semantic argument that markup strings are not command options, but in pragmatic terms they are because they offer the scripter more choice, more options to choose from.

ZB: And what's wrong with options, that you prefer "embedded markup fragments"?

WJG: They're not my cup of tea, but its a part of Gtk. Here's a sample that I'm sure is not possible with a tk label.

http://wjgiddings.googlepages.com/Screenshot-PangoTML.png

Here's the code to make that label:

 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" "[email protected]"

 package require Gnocl

 set lab [gnocl::label -text {%<<span background="springgreen2" foreground="blue" size="large"><b>Hello</b></span> <span foreground="red" size="large"><i><tt><u>World</u><sup>2</sup></tt></i></span>}]

 set win [gnocl::window -title "Pango TML" -child $lab -onDelete {exit} ]
 gnocl::mainLoop

DKF: OTOH, Tk's good at handling other things, such as tying a label to a variable with the -textvariable option. I'm not claiming superiority of one over the other. (The idea of full HTML in a label seems odd to me, or at least “flow” part of HTML, but that's possibly just me.)

WJG Y'know, I think that under the bonnet Tk is good, so much has grown out of it, Gtk for instance owes a lot to Tk.


WJG (24/11/08) Here's some wild stuff that I think many people would be interested in arbitrarily rotated text with full-antialiasing and the use of a -textVariable option for labels which again has been rendered with Pango Markup Language. I've seen this sort of thing discussed on the wiki using Tk and images but use system resources whenever possible, why re-invent the wheel?

Note: The gnocl::label -angle and -textVariable options are still developmental and will available in the next release of Gnocl but if anyone is eager to try them now, I'd be more than happy to email them the current label.c source code.

http://wjgiddings.googlepages.com/Screenshot-Tcl-Gnocl8.5SpinLabel.png

And here's the Gnocl script:

 #!/bin/sh
 # the next line restarts using tclsh \
 exec tclsh "$0" "[email protected]"

 package require Gnocl

 set etitle "Tcl/Gnocl 8.5"

 proc setString {angle} {
         #return "Angle = <span foreground ="red"$angle degs>"
         return  "Angle = <span foreground = \"red\"><b>$angle</b></span> degs"
        }

 set angle 330
 set string [setString $angle]

 set lab [gnocl::label -textVariable string -baseFont {Sans 30} -angle $angle]
 set but [gnocl::button \
         -icon "%#Refresh" \
         -onClicked {
                 incr angle 15
                 if {$angle == "360" } {set angle 0}
                 set string [setString $angle]
                 $lab configure -angle $angle
        }]

 set box [gnocl::box -orientation vertical]
 $box add $but
 $box add $lab -fill {1 1} -expand 1

 set win [gnocl::window -title $etitle -child $box -onDelete {exit} ]
 gnocl::mainLoop