Tutorial Pages‎ > ‎

    Working with text buffers.

    Gnocl text widgets are capable of working with multiple buffers, here's how to do it.


    There are a number of approaches that a programmer can take in deciding how to work with multiple documents in a single application. These range from the opening unique windows, tabbed widgets or, as is shown below, swapping between specific data buffers.




    The default implementation of the gnocl::text widget is to create a scrolling window with a textview and a unique textbuffer. The way in which Gtk+ handles the text issue is actually to treat the text and the means of displaying it as two separate items. Under program control it is feasible then, to open multiple text files and to switch between them in a single single, common display area. This imlies, of course, that for any given textBuffer it is possible to share it between multiple display widgets. Both these features are supported within gnocl.

    First of all lets take a look on displaying different textBuffers. The example code below shows how its done. First of all, create some distinct, separate pieces of text. Here we have the three verses of the poem "Come to me in the silence of the night" by Christina Rossetti. See how each of the three verses is
    held as a separate entries within an array. These, of course could be files loaded from disk, or sections of a parsed larger document.
    set verse(1) {Come to me in the silence of the night;
    Come to me in the speaking silence of a dream;
    Come with soft rounded cheeks and eyes as bright
    As sunlight on a stream;
    Come back in tears,
    O memory, hope, love of finished years.}

    set verse(2) {O dream how sweet, too sweet, too bitter sweet,
    Whose wakening should have been in Paradise,
    Where souls brimfull of love abide and meet;
    Where thirsting longing eyes
    Watch the slow door
    That opening, letting in, lets out no more.}

    set verse(3) {Yet come to me in dreams that I may live
    My very life again cold in death:
    Come back to me in dreams, that I may give
    Pulse for pulse, breath for breath:
    Speak low, lean low,
    As long ago. my love, how long ago.}
    Next, simply create the buffers specifying the text content with the -text option.
    set tbuf(0) [gnocl::textBuffer -text $verse(0)]
    set tbuf(1) [gnocl::textBuffer -text $verse(1)]
    set tbuf(2) [gnocl::textBuffer -text $verse(2)]
    set tbuf(3) [gnocl::textBuffer -text $verse(3)]
    This process is quite straightforward. A further great feature of this piece of functionality is that any rich text formatting, tags, marks and so on, are retained. Whenever there need arises to swap the content of the textview to another buffer, this again is easily achieved with a simple reconfiguration of the widget setting. Assuming that a gnocl::text widget $txt has already been created and the variable $buff is the index of the buffer to be displayed, one line of script will implement the necessary changes.
    $txt configure -buffer $tbuf($buff)
    This is the completed test script, see 'attachments' below to download and run. It requires gnocl version 0.9.94 to run.

    #---------------
    # test-text-buffer.tcl
    #---------------
    # William J Giddings
    # 16/Jan/2010
    #---------------
    #!/bin/sh
    # the next line restarts using tclsh \
    exec tclsh "$0" "$@"
    #---------------

    package require Gnocl

    set box1 [gnocl::box -orientation vertical]
    set box2 [gnocl::box ]
    $box1 add $box2

    set txt [gnocl::text \
    -baseFont {Sans 14} \
    -editable no \
    -text "Come to me in the silence of the night\nChristina Rossetti (1830-1894)" \
    -scrollbar always]

    foreach {i j} {0 Title 1 Verse-1 2 Verse-2 3 Verse-3} {
    set rad($i) [gnocl::radioButton \
    -text $j \
    -variable buff \
    -onValue $i]
    $rad($i) configure -onToggled { $txt configure -buffer $tbuf($buff) }
    $box2 add [set rad($i)]
    }

    $box1 add $txt -fill {1 1} -expand 1

    gnocl::window -child $box1 -defaultWidth 480 -defaultHeight 240 -onDestroy {exit}

    set verse(0) [$txt get start end]

    set verse(1) {Come to me in the silence of the night;
    Come to me in the speaking silence of a dream;
    Come with soft rounded cheeks and eyes as bright
    As sunlight on a stream;
    Come back in tears,
    O memory, hope, love of finished years.}

    set verse(2) {O dream how sweet, too sweet, too bitter sweet,
    Whose wakening should have been in Paradise,
    Where souls brimfull of love abide and meet;
    Where thirsting longing eyes
    Watch the slow door
    That opening, letting in, lets out no more.}

    set verse(3) {Yet come to me in dreams that I may live
    My very life again cold in death:
    Come back to me in dreams, that I may give
    Pulse for pulse, breath for breath:
    Speak low, lean low,
    As long ago. my love, how long ago.}

    set tbuf(0) [gnocl::textBuffer -text $verse(0)]
    set tbuf(1) [gnocl::textBuffer -text $verse(1)]
    set tbuf(2) [gnocl::textBuffer -text $verse(2)]
    set tbuf(3) [gnocl::textBuffer -text $verse(3)]

    gnocl::mainLoop

    Č
    ċ
    ď
    test-text-buffer.tcl
    (2k)
    William Giddings,
    13 Mar 2010 04:16
    Comments

    Sections