Sunday, January 27, 2013

Checking to see which entry has the active input focus.

Gtk provides no simple way telling a calling program which entry widget contains the active keyboard focus. Specific widgets can be interrogated as to whether they possess keyboard input focus on either a global scale or simply within their own toplevel window. My own pet project is working on the translation of Chinese texts and the api of my translation editor will have multiple editable widgets on screen at any one time. Up till now I've used focus events to set a global variable identifying which widget has focus, but there are other ways. Today I just added the hasToplevelFocus and has hasGlobalFocus commands to the gnocl::text widget. There always was, of course the option to use cget -hasFocus. Here's my test script.

#---------------
# test-chinese-punct-toolbar.tcl
#---------------
#
#!/bin/sh
#\
exec tclsh "$0" "$@"

package require Gnocl

#---------------
# insert punc mark it currently active text
#---------------
proc do_chn_punc { mark } {

    foreach w $::texts {
        if { [$w isToplevelFocus] == 1 } {
            $w insert cursor $mark
        }
    }
}

#---------------
# Insert punctuation marks suited to differing character sets.
#---------------
proc chn_punc { {type jianti} } {

    set marks {
        。    {Full Stop}
        ‘    {Open Single Quote}
        “    {Open Double Quote}
        ”    {Close Double Quote}
        ’    {Close Single Quote}
        、    {Enumeration comma}
        ‧    {Middle dot}
        《    {Open Double Title Mark}
        》    {Close Double Title Mark}
        ……    {Ellipsis}
        ——    {Em Dash}
        —    {En Dash}
        ~    {Wavy Dash}
        }

    if {$type eq "fanti" } {   
    set marks {
        。    {Full Stop}
        「    {Open Single Quote}
        『    {Open Double Quote}
        』    {Close Double Quote}
        」    {Close Single Quote}
        、    {Enumeration comma}
        ‧    {Middle dot}
        《    {Open Double Title Mark}
        〈    {Open Single (Section) Title Mark}
        〉    {Close Single (Section) Title Mark}
        》    {Close Double Title Mark}
        ……    {Ellipsis} 
        ——    {Em Dash}
        —    {En Dash}
        ~    {Wavy Dash}
    } }

    set tb [gnocl::toolBar -style text ]
       
    foreach {mark tip} $marks {
        $tb add item \
            -icon %#New \
            -text $mark \
            -tooltip $tip \
            -onClicked "do_chn_punc $mark"
    }
   
    return $tb
}


set box [gnocl::box -orientation vertical]
set txt(1) [gnocl::text -baseFont {Serif 12}]
set txt(2) [gnocl::text -baseColor #D1FEFF -baseFont {Serif 14}]
gnocl::window -child $box
$box add [chn_punc]
$box add [chn_punc fanti]
$box add $txt(1)
$box add $txt(2)

# list of available texts
set texts [list $txt(1) $txt(2)]
puts $texts

$txt(1) grabFocus





Tuesday, January 22, 2013

gnocl::cairo - moving forward

Spent some time last night working on the proposed gnocl::cairo package. I've got most of the innards working now and the next step will be wrapping the gdk core api into the mixture. Cairo is certainly fast even on my old 32-bit workstation. By default, my development code wants always to work on bitmaps but, of course, Cairo will work on a variety of surfaces. Apart from pixbufs, the additional objects include svn, png, ps, pdf, some other windows, mac and X formats and gdk drawables such as windows. The code is expanding rapidly but, it's getting there!

Wednesday, January 16, 2013

Today's Added Functionality

This is the summary of today's work on the Gnocl sourcecode.

2013-01:
    gnocl::winfo
        o new subcommand
            exists
    gnocl::notebook
        o new options
            -startWidget
            -endWidget
            -onDestroy
    gnocl::label
        o new option
            -onDestroy

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