textpeer.tcl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # textpeer.tcl --
  2. #
  3. # This demonstration script creates a pair of text widgets that can edit a
  4. # single logical buffer. This is particularly useful when editing related text
  5. # in two (or more) parts of the same file.
  6. if {![info exists widgetDemo]} {
  7. error "This script should be run from the \"widget\" demo."
  8. }
  9. package require Tk
  10. set w .textpeer
  11. catch {destroy $w}
  12. toplevel $w
  13. wm title $w "Text Widget Peering Demonstration"
  14. wm iconname $w "textpeer"
  15. positionWindow $w
  16. set count 0
  17. ## Define a widget that we peer from; it won't ever actually be shown though
  18. set first [text $w.text[incr count]]
  19. $first insert end "This is a coupled pair of text widgets; they are peers to "
  20. $first insert end "each other. They have the same underlying data model, but "
  21. $first insert end "can show different locations, have different current edit "
  22. $first insert end "locations, and have different selections. You can also "
  23. $first insert end "create additional peers of any of these text widgets using "
  24. $first insert end "the Make Peer button beside the text widget to clone, and "
  25. $first insert end "delete a particular peer widget using the Delete Peer "
  26. $first insert end "button."
  27. ## Procedures to make and kill clones; most of this is just so that the demo
  28. ## looks nice...
  29. proc makeClone {w parent} {
  30. global count
  31. set t [$parent peer create $w.text[incr count] -yscroll "$w.sb$count set"\
  32. -height 10 -wrap word]
  33. set sb [ttk::scrollbar $w.sb$count -command "$t yview" -orient vertical]
  34. set b1 [button $w.clone$count -command "makeClone $w $t" \
  35. -text "Make Peer"]
  36. set b2 [button $w.kill$count -command "killClone $w $count" \
  37. -text "Delete Peer"]
  38. set row [expr {$count * 2}]
  39. grid $t $sb $b1 -sticky nsew -row $row
  40. grid ^ ^ $b2 -row [incr row]
  41. grid configure $b1 $b2 -sticky new
  42. grid rowconfigure $w $b2 -weight 1
  43. }
  44. proc killClone {w count} {
  45. destroy $w.text$count $w.sb$count
  46. destroy $w.clone$count $w.kill$count
  47. }
  48. ## Now set up the GUI
  49. makeClone $w $first
  50. makeClone $w $first
  51. destroy $first
  52. ## See Code / Dismiss buttons
  53. grid [addSeeDismiss $w.buttons $w] - - -sticky ew -row 5000
  54. grid columnconfigure $w 0 -weight 1