text.tcl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # text.tcl --
  2. #
  3. # This demonstration script creates a text widget that describes
  4. # the basic editing functions.
  5. if {![info exists widgetDemo]} {
  6. error "This script should be run from the \"widget\" demo."
  7. }
  8. package require Tk
  9. set w .text
  10. catch {destroy $w}
  11. toplevel $w
  12. wm title $w "Text Demonstration - Basic Facilities"
  13. wm iconname $w "text"
  14. positionWindow $w
  15. ## See Code / Dismiss buttons
  16. set btns [addSeeDismiss $w.buttons $w {} \
  17. {ttk::button $w.buttons.fontchooser -command fontchooserToggle}]
  18. pack $btns -side bottom -fill x
  19. text $w.text -yscrollcommand [list $w.scroll set] -setgrid 1 \
  20. -height 30 -undo 1 -autosep 1
  21. ttk::scrollbar $w.scroll -command [list $w.text yview]
  22. pack $w.scroll -side right -fill y
  23. pack $w.text -expand yes -fill both
  24. # TIP 324 Demo: [tk fontchooser]
  25. proc fontchooserToggle {} {
  26. tk fontchooser [expr {[tk fontchooser configure -visible] ?
  27. "hide" : "show"}]
  28. }
  29. proc fontchooserVisibility {w} {
  30. $w configure -text [expr {[tk fontchooser configure -visible] ?
  31. "Hide Font Dialog" : "Show Font Dialog"}]
  32. }
  33. proc fontchooserFocus {w} {
  34. tk fontchooser configure -font [$w cget -font] \
  35. -command [list fontchooserFontSel $w]
  36. }
  37. proc fontchooserFontSel {w font args} {
  38. $w configure -font [font actual $font]
  39. }
  40. tk fontchooser configure -parent $w
  41. bind $w.text <FocusIn> [list fontchooserFocus $w.text]
  42. fontchooserVisibility $w.buttons.fontchooser
  43. bind $w <<TkFontchooserVisibility>> [list \
  44. fontchooserVisibility $w.buttons.fontchooser]
  45. focus $w.text
  46. $w.text insert 0.0 \
  47. {This window is a text widget. It displays one or more lines of text
  48. and allows you to edit the text. Here is a summary of the things you
  49. can do to a text widget:
  50. 1. Scrolling. Use the scrollbar to adjust the view in the text window.
  51. 2. Scanning. Press the middle mouse button in the text window and drag up
  52. or down. This will drag the text at high speed to allow you to scan its
  53. contents.
  54. 3. Insert text. Press mouse button 1 to set the insertion cursor, then
  55. type text. What you type will be added to the widget.
  56. 4. Select. Press mouse button 1 and drag to select a range of characters.
  57. Once you've released the button, you can adjust the selection by pressing
  58. button 1 with the shift key down. This will reset the end of the
  59. selection nearest the mouse cursor and you can drag that end of the
  60. selection by dragging the mouse before releasing the mouse button.
  61. You can double-click to select whole words or triple-click to select
  62. whole lines.
  63. 5. Delete and replace. To delete text, select the characters you'd like
  64. to delete and type Backspace or Delete. Alternatively, you can type new
  65. text, in which case it will replace the selected text.
  66. 6. Copy the selection. To copy the selection into this window, select
  67. what you want to copy (either here or in another application), then
  68. click the middle mouse button to copy the selection to the point of the
  69. mouse cursor.
  70. 7. Edit. Text widgets support the standard Motif editing characters
  71. plus many Emacs editing characters. Backspace and Control-h erase the
  72. character to the left of the insertion cursor. Delete and Control-d
  73. erase the character to the right of the insertion cursor. Meta-backspace
  74. deletes the word to the left of the insertion cursor, and Meta-d deletes
  75. the word to the right of the insertion cursor. Control-k deletes from
  76. the insertion cursor to the end of the line, or it deletes the newline
  77. character if that is the only thing left on the line. Control-o opens
  78. a new line by inserting a newline character to the right of the insertion
  79. cursor. Control-t transposes the two characters on either side of the
  80. insertion cursor. Control-z undoes the last editing action performed,
  81. and }
  82. switch [tk windowingsystem] {
  83. "aqua" - "x11" {
  84. $w.text insert end "Control-Shift-z"
  85. }
  86. "win32" {
  87. $w.text insert end "Control-y"
  88. }
  89. }
  90. $w.text insert end { redoes undone edits.
  91. 7. Resize the window. This widget has been configured with the "setGrid"
  92. option on, so that if you resize the window it will always resize to an
  93. even number of characters high and wide. Also, if you make the window
  94. narrow you can see that long lines automatically wrap around onto
  95. additional lines so that all the information is always visible.}
  96. $w.text mark set insert 0.0