Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text Box Copying

Status
Not open for further replies.

SmallProgram

Programmer
Feb 6, 2002
42
IN
Hello Friends,
I have an Interesting Question for you people, which is plaguaing me from days.
How can I copy a Text with all its Tags set (Like apart having Bold,a part being colored etc...) into an another text box with out losing the text tag properties.
Thanks In Advance
Mallik. Programmer,Amatuer
 
Yes, it was interesting.
Here is the code:
Code:
  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 insert end "this text is underlined" underline
  .text1 insert end " and " bold
  .text1 insert end "this text is bold and red" {bold red}
# get info from first text box
  # get text and associated tags
  set dump [.text1 dump -tag -text 1.0 end]
  # get tag names
  set tags [.text1 tag names]
  # get tags possible options
  set config [.text1 tag config sel]
  set opts {}
  foreach item $config { lappend opts [lindex $item 0] }
# set second text box tags from first text box tags
  foreach tag $tags   {
    foreach opt $opts     {
      set value [.text1 tag cget $tag $opt]
      if {"$value" != ""} { .text2 tag config $tag $opt $value }
    }
  }
# set second text box text from first text box dump
  set tlist {}
  foreach {key val1 val2} $dump   {
    switch $key     {
      tagon   { lappend tlist $val1 }
      tagoff  { set n [lsearch $tlist $val1]; set tlist [lreplace $tlist $n $n] }
      text    { .text2 insert end $val1 $tlist }
    }
  }
Some explanations now.
You can inspect text and associated tags with:
Code:
<widget> dump -tag -text <begin> <end>
You can inspect tags name with:
Code:
<widget> tag names
You can know all the config options for a tag with:
Code:
<widget> tag config <tag>
You can get the value of a tag option with:
Code:
<widget> tag cget <tag> <option>

ulis
 
Thank You Ulis The Answer was great.
It worked well for me.
Mallik Programmer,Amatuer
 
Thanks for the thanks :)
I have now a fonction that make a complete copy: text, tags, marks, windows & images.
I named it TextCopy:
Code:
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 {&quot;$value&quot; != &quot;&quot;} { $dest tag config $tag $opt $value }
      }
    }
    # copy marks
    foreach mark [$source mark names]     {
      set value [$source mark gravity $mark]
      if {&quot;$value&quot; != &quot;&quot;} { $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 {&quot;$value&quot; != &quot;&quot;} { 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 {&quot;$value&quot; != &quot;&quot;} { 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 &quot;
  #define v_width 8
  #define v_height 10
  static unsigned char v_bits[] = { 0xff, 0xff, 0x7e, 0x7e, 0x3c, 0x3c, 0x18, 0x18, 0x00, 0x00 };&quot;
set pop [image create bitmap -data $data]
set data &quot;
  #define v_width 8
  #define v_height 10
  static unsigned char v_bits[] = { 0x18, 0x3c, 0x7e, 0xff, 0xff, 0x7e, 0x3c, 0x18, 0x00, 0x00 };&quot;
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 &quot;this text is underlined &quot; underline
.text1 image create end -image $pop
.text1 insert end &quot; and &quot;
.text1 image create end -image $nop
.text1 insert end &quot; this text is bold and red&quot; {bold red}
# copy the first text box to the second text box
namespace import ::TextCopy::textCopy

textCopy .text1 .text2
ulis
 
Hey U seem to Help Me in the same way I want.
Thank you And With Your Earlier Reply I have designed a similar Function and Am I using it.
Thank you once again for the reply.
Programmer,Amatuer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top