Tutorial Pages‎ > ‎

Gnocl - Glade Integration

Contents

    No headings.

    The loading of xml UI description files created with Glade [1] is an important part of  Gtk+/Gnome applications development. The inclusion and use such files into a Tcl/Gnocl application is significant step forward.

    This series of three tutorials shows how basic application development with Glade is done. Demos made by Henry Kroll.










    #---------------
    # gladeXMLTest1.tcl
    #---------------
    # William J Giddings
    # 30-Nov-2009
    #---------------

    #!/bin/sh
    # the next line restarts using tclsh \
    exec tclsh "$0" "$@"

    package require Gnocl

    # load a simple glade file, containing three buttons in a horizontal row
    # the variable glade1_widgets contains a list of all widgets created
    # by loading this file, they will include the names given in the glade file
    # itself along with the names assigned when registering these with Tcl

    set glade1 button1.glade
    set glade1_widgets [gnocl::glade new $glade1]

    # create a list of aliases for these new widgets
    # we can use the glade widget names to do this, to make things manageable
    # when switching between Tcl and Glade

    foreach item $glade1_widgets {
    foreach {gnocl glade} $item {}
    set $glade $gnocl
    }

    # in the glade file the toplevel window is named window1
    # let us resize, rename and centre it on screen

    $window1 configure -title "Glade Test" -width 320 -height 200
    $window1 centre ;# or 'center' if you wish

    # we can now access the widget attributes as these are registered with gnocl!
    $button1 configure -icon "%#New" -onClicked { puts "NEW! from %w" }
    $button2 configure -icon "%#Open" -onClicked { puts "Open! from %w" }
    $button3 configure -icon "%#Save" -onClicked { puts "Save! from %w" }

    # as there needs to be a container for these widgets we can get its name with
    set box [$window1 cget -child]

    # and, of course, add new widgets too!
    $box add [gnocl::toggleButton -text "CLICK ME!"]

    gnocl::mainLoop


    Incorporating a glade/builder UI description in a script.


    #!/bin/sh
    # the next line restarts using tclsh \
    exec tclsh "$0" "$@"

    package require Gnocl

    #---------------
    # Create a simple layout, a widget with a button.
    #---------------
    set gladeBuffer {<?xml version="1.0"?>
    <glade-interface>
    <requires-version lib="gtk+" version="2.12"/>
    <widget class="GtkWindow" id="window1">
    <property name="window_position">GTK_WIN_POS_CENTER</property>
    <property name="default_width">300</property>
    <property name="default_height">100</property>
    <child>
    <widget class="GtkButton" id="button1">
    <property name="visible">True</property>
    <property name="can_focus">True</property>
    <property name="receives_default">True</property>
    <property name="label" translatable="yes">Close Me!</property>
    <property name="response_id">0</property>
    </widget>
    </child>
    </widget>
    </glade-interface>}

    #---------------
    # Load the buffer.
    #---------------
    set glade1_widgets [gnocl::glade buffer $gladeBuffer]

    #---------------
    # Register the widgets using their glade names.
    #---------------
    foreach item $glade1_widgets {
    foreach {gnocl glade} $item {}
    set $glade $gnocl
    }


    #---------------
    # Tweak settings, and display.
    #---------------
    $button1 configure -onClicked {gnocl::beep}
    $window1 show -onDestroy {exit}
    gnocl::mainLoop




    Comments