Gnocl Cookbook‎ > ‎

Cloning Widgets

A while ago I saw a StackoverFlow request about cloning widgets. Ok, the request was about cloning Gtk widgets in C, but here's the solution in Tcl using the Gnocl package. For a some time now its been possible to query Gnocl about the various options and commands available to each widget and its the ability combined with the cget (configuration get) command that allows property and data values to be extracted from a widget. Here's a very simple example based upon a button.
#!/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" "$@" if { [catch { package present Gnocl } ] } { package require Gnocl } ## clone specified widget #/param wid the widget to clone #/returns wid of newly created clone object proc gnocl::clone { wid } { foreach item [split [string trim [gnocl::[$wid class] options]] \n] { # get list of options from internal help, trim away option information set item [string range $item 0 [string first " " $item]-1] lappend res $item [$wid cget $item] } return [gnocl::[$wid class] {*}$res] } ## test proc proc main { } { set but [gnocl::button -text %#New -onClicked { puts "HIDIHI" } ] gnocl::window -child $but -x 500 -y 500 -width 300 -title ORIGINAL gnocl::window -child [gnocl::clone $but] -x 850 -y 500 -width 300 -title CLONE } main