namespace eval ::TextCopy {
namespace export textCopy
variable tags
# -------
# copy all of a text box
# -------
proc textCopy {source dest} {
variable tags
# copy tags
set tagconfig [$source tag config sel]
set tagopts {}
foreach item $tagconfig { lappend tagopts [lindex $item 0] }
foreach tag [$source tag names] {
foreach opt $tagopts {
set value [$source tag cget $tag $opt]
if {"$value" != ""} { $dest tag config $tag $opt $value }
}
}
# copy marks
foreach mark [$source mark names] {
set value [$source mark gravity $mark]
if {"$value" != ""} { $dest mark gravity $mark $value }
}
# get image options list
# copy text & embedded
proc copyItem {source dest key val1 val2} {
variable tags
switch $key {
image {
# copy an image
set imageconfig [$source image config $val1]
set imageopts {}
foreach item $imageconfig { lappend imageopts [lindex $item 0] }
set opts {}
foreach opt $imageopts {
set value [$source image cget $val1 $opt]
if {"$value" != ""} { lappend opts $opt $value }
}
if {[catch { eval $dest image create end -name $val1 $opts } msg] != 0} { error $msg }
}
mark {
# copy a mark
$dest mark set $val1
}
tagon {
lappend tags $val1
}
tagoff {
set n [lsearch $tags $val1]
set tags [lreplace $tags $n $n]
}
text {
$dest insert end $val1 $tags
}
window {
# copy a window
set windowconfig [$source window config $val2]
set windowopts {}
foreach item $windowconfig { lappend windowopts [lindex $item 0] }
set opts {}
foreach opt $windowopts {
set value [$source window cget $val2 $opt]
if {"$value" != ""} { lappend opts $opt $value }
}
if {[catch { eval $dest window create end $opts } msg] != 0} { error $msg }
}
}
}
set tags {}
$source dump -command [list copyItem $source $dest] 1.0 end
}
}
# -------
# demo
# -------
# create images
set data "
#define v_width 8
#define v_height 10
static unsigned char v_bits[] = { 0xff, 0xff, 0x7e, 0x7e, 0x3c, 0x3c, 0x18, 0x18, 0x00, 0x00 };"
set pop [image create bitmap -data $data]
set data "
#define v_width 8
#define v_height 10
static unsigned char v_bits[] = { 0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18, 0x00, 0x00 };"
set nop [image create bitmap -data $data]
# create text boxes
set ::n 0
pack [text .text1 -height 4] -pady 5
pack [text .text2 -height 4] -pady 5
.text1 tag config underline -underline 1
.text1 tag config bold -font {Courier 10 bold}
.text1 tag config red -foreground red
.text1 window create end -create { label .label[incr ::n] -text label$::n }
.text1 insert end "this text is underlined " underline
.text1 image create end -image $pop
.text1 insert end " and "
.text1 image create end -image $nop
.text1 insert end " this text is bold and red" {bold red}
# copy the first text box to the second text box
namespace import ::TextCopy::textCopy
textCopy .text1 .text2