programming.nbk: Home | Index | Next Page: using gdb | Previous Page: Tour 7


 User Code

Use this page to extend Notebook using the the Tcl language. Commands you add here can be used as Magic Buttons and Embedded Macros.

Note that you can intersperse normal prose in between blocks of Tcl code.

User Menu

The User Menu pops up when you right-click or control-click on a page in the Page Browser. You can customize it however you like.

usermenu {
    Back back-page
    Home {goto-page Home}
}

Edit Menu

The Edit Menu pops up when you right-click or control-click on a page in the Page Editor. You can customize it however you like.

editmenu {
    Undo             undo-change
    Redo             redo-change
    separator {}
    Cut              cut-string
    Copy             copy-string
    Paste            paste-string
    "Insert Page..." insert-page
}

Example

The following embedded macro used to be used by the Tour to create a magic button that said "Click here to continue..." and took you to the next page in the tour when you clicked it. It's no longer needed, because now you can write such links directly, like this:

  [Click here to continue...|Tour 2]

Still, it's a nice example of how to write a macro that creates a button.

proc clickToContinue {name} {
	return "\[%Click here to continue...|goto-page [list $name]%\]"
}

# Create a magic button to launch url
proc URL {name} {
     return "\[%$name |goToInternet $name%\]"
}

# Go to internet : Display user agent with the selected url
proc goToInternet {name} {
     set prg "C:/Program Files/Internet Explorer/IEXPLORE.EXE"
     exec $prg $name &
}

package require Tk 8.4
wm withdraw .

proc newpad {cell width} {
    set fontdigits [lindex $cell 0]
    set cellTxt    [lindex $cell 1]
    set cellLink   [lindex $cell 2]
    set padding {}
    set f [fontfromdigits $fontdigits]
    set wt [font measure $f $cellTxt]
    set w $wt
    msg "w=$wt"
    while { $w < $width }  {
        append padding " "
        set w [expr $wt + [font measure "Arial 12" $padding]]
    } 
    return $padding   
}
proc putCellTogether {cell} {
    set fontdigits [lindex $cell 0]
    set cellTxt    [lindex $cell 1]
    set cellLink   [lindex $cell 2]
    set newTxt $cellTxt
    if {[string compare $cellLink ""] != 0} {
        append newTxt "|" $cellLink
    }
    if {[string first {[} $fontdigits] >= 0} {
        set newTxt "\[$newTxt\]"
   }
    set digits [split $fontdigits ""]
    foreach d $digits {
        if {$d != {[}} {
            set newTxt "<$d>$newTxt</$d>"
        }
    }
    
    return $newTxt
}

proc breakCellApart {cellTxt} {
    set linkTxt {}
    set cellTxt [string trim $cellTxt]
    set cellTxt [regsub -all "</.>" $cellTxt ""]
    set cellTxt [string trim $cellTxt]
    set fontdigits ""
    set cellTxt [regsub -all "</.>" $cellTxt ""]
    set start [string index $cellTxt 0]
    while {$start == "<" } {
        append fontdigits [string index $cellTxt 1]
        set cellTxt [string range $cellTxt 3 end]
        set start [string index $cellTxt 0]
    }  
    set start [string index $cellTxt 0]
    set strend [string length $cellTxt]
    incr strend -1
    set end [string index $cellTxt $strend]
    if {$start == {[} && $end == {]}} {
        append fontdigits {[}
        incr strend -1
        set cellTxt [string range $cellTxt 1 $strend]
        set start [string first {|} $cellTxt]
        if {$start > -1} {
            set linkTxt [string range $cellTxt [expr $start + 1] end]
            set cellTxt [string range $cellTxt 0 [expr $start - 1]]
        }
    }
    return [list $fontdigits $cellTxt $linkTxt]
}

proc table_arg_display {data before between} {
    # load the table data page, split it into a list, one element per line
    set table1 $data
    # split each line into a list, one element per cell
    foreach z $table1 {
        set t [split $z ~]
        set y {}
        foreach x $t {
            lappend y [string trim $x]
        }
        lappend table2 $y
    }

    # replace each cell element with a list of the raw text plus font information
    set widths [list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    foreach z $table2 {
        set i 0
        set y {}
        foreach x $z {
            set x [breakCellApart $x]
            lappend y $x
            set cellfont [lindex $x 0]
            set cellfont [fontfromdigits $cellfont]
            set celltext [lindex $x 1]
            set w [font measure $cellfont $celltext]
            if {$w > [lindex $widths $i]} {lset widths $i $w}
            incr i 1
        }
        lappend table3 $y
    }

    # go through the cells again.
    # pad the cell text, then add the formatting back
    # output the final text
    foreach z $table3 {
        set i 0
        append txt $before
        foreach x $z {
            set celltext [putCellTogether $x]
            set padding  [newpad $x [lindex $widths $i]]
            append txt "$celltext$padding$between"
            incr i 1
        }
        append txt "\n"
    }
    append txt "\n"
    return $txt
}

proc table_display {name before between} {
    # load the table data page, split it into a list, one element per line
    set table1 [split [pageget $name] "\n"]
    # split each line into a list, one element per cell
    foreach z $table1 {
        set t [split $z ~]
        set y {}
        foreach x $t {
            lappend y [string trim $x]
        }
        lappend table2 $y
    }

    # replace each cell element with a list of the raw text plus font information
    set widths [list 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
    foreach z $table2 {
        set i 0
        set y {}
        foreach x $z {
            set x [breakCellApart $x]
            lappend y $x
            set cellfont [lindex $x 0]
            set cellfont [fontfromdigits $cellfont]
            set celltext [lindex $x 1]
            set w [font measure $cellfont $celltext]
            if {$w > [lindex $widths $i]} {lset widths $i $w}
            incr i 1
        }
        lappend table3 $y
    }

    # go through the cells again.
    # pad the cell text, then add the formatting back
    # output the final text
    foreach z $table3 {
        set i 0
        append txt $before
        foreach x $z {
            set celltext [putCellTogether $x]
            set padding  [newpad $x [lindex $widths $i]]
            append txt "$celltext$padding$between"
            incr i 1
        }
        append txt "\n"
    }
    append txt "\n"
    return $txt
}

proc fontfromdigits {fontdigits } {
    set isFont "Arial 12"
    set isBold ""
    set isItalic ""
    set digits [split $fontdigits ""]
    foreach d $digits {
        if {[string match $d "b"] } {set isBold "bold"}
        if {[string match $d "i"] } {set isItalic "italic"}
        if {[string match $d "m"] } {set isFont "Courier 10"}
        if {[string match $d "h"] } {set isFont "Helvetica 12"; set isBold "bold"}
        if {[string match $d "s"] } {set isFont "Helvetica 8"}
    }
    set final "$isFont $isBold $isItalic"
    return $final
}

# strip the current page from a list of pages
proc otherpages {pages} {
    set ndx [lsearch -exact $pages [current]]
    if {$ndx != -1} {
        # Replace the current page's entry with nothing.
        set pages [lreplace $pages $ndx $ndx]
    }
    return $pages
}

programming.nbk: Home | Index | Next Page: using gdb | Previous Page: Tour 7


Notebook exported on Monday, 7 July 2008, 18:56:06 PM Eastern Daylight Time