Wednesday, January 16, 2013

Improving Notebook Tearoff Functionality

The Gtk+ libraries provide the necessary support to detach notebook tabs in order to attach them to other notebooks in the same group or to place them in floating windows. This is fine. But, what happens when a floating palette is destroyed? Well, the notebook and any pages that it contains will be lost too. Unfortunately, the Gtk+ libs do not re-attach pages to any original note book when other notebooks in the group are deleted. In otherwords, pages will be lost and it up the application to create new instances. The following script shows how to do it. It relies upon the gnocl::winfo exists sub-command, which has only just been added. So, anyone trying out this script will need to use the nightly build dated 16-Jan-13 or later.

# test-notebook-tearoff.tcl
#!/bin/sh \
exec tclsh "$0" "$@"

package require Gnocl

# create notebook contents
proc addStuff {} {

    set notebook1 [gnocl::notebook \
        -scrollable 1 \
        -tabPosition left \
        -onCreateWindow {
            %w configure -title "TEAR OFF NOTEBOOK"
            } \
        -groupId 1 ]

    proc addFirst {nb} {
        if { ![gnocl::winfo exists $nb] } { return }
        $nb addPage [gnocl::label \
            -text "First Page" \
            -onDestroy "addFirst $nb"] "%__First"

    proc addSecond {nb} {
        if { ![gnocl::winfo exists $nb] } { return }
        $nb addPage [gnocl::label \
            -text "Second Page" \
            -onDestroy "addSecond $nb" ] "%__Second"

    proc addThird {nb} {

        if { ![gnocl::winfo exists $nb] } { return }

        $nb addPage [gnocl::label \
            -text "Third Page" \
            -onDestroy "addThird $nb"] "%__Third"

    proc addFourth {nb} {
        if { ![gnocl::winfo exists $nb] } { return }
        $nb addPage [gnocl::label \
            -text "Fourth Page" \
            -onDestroy "addFourth $nb"] "%__Fourth"

    foreach  a [list First Second Third Fourth] { add$a $notebook1 }

    return $notebook1


gnocl::window \
    -title "Notebook1" \
    -child [addStuff] \
    -setSize 0.3 \
    -onDestroy exit

No comments: