text.tcl 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. pack $btns -side bottom -fill x
  18. text $w.text -yscrollcommand [list $w.scroll set] -setgrid 1 \
  19. -height 30 -undo 1 -autosep 1
  20. scrollbar $w.scroll -command [list $w.text yview]
  21. pack $w.scroll -side right -fill y
  22. pack $w.text -expand yes -fill both
  23. $w.text insert 0.0 \
  24. {This window is a text widget. It displays one or more lines of text
  25. and allows you to edit the text. Here is a summary of the things you
  26. can do to a text widget:
  27. 1. Scrolling. Use the scrollbar to adjust the view in the text window.
  28. 2. Scanning. Press mouse button 2 in the text window and drag up or down.
  29. This will drag the text at high speed to allow you to scan its contents.
  30. 3. Insert text. Press mouse button 1 to set the insertion cursor, then
  31. type text. What you type will be added to the widget.
  32. 4. Select. Press mouse button 1 and drag to select a range of characters.
  33. Once you've released the button, you can adjust the selection by pressing
  34. button 1 with the shift key down. This will reset the end of the
  35. selection nearest the mouse cursor and you can drag that end of the
  36. selection by dragging the mouse before releasing the mouse button.
  37. You can double-click to select whole words or triple-click to select
  38. whole lines.
  39. 5. Delete and replace. To delete text, select the characters you'd like
  40. to delete and type Backspace or Delete. Alternatively, you can type new
  41. text, in which case it will replace the selected text.
  42. 6. Copy the selection. To copy the selection into this window, select
  43. what you want to copy (either here or in another application), then
  44. click button 2 to copy the selection to the point of the mouse cursor.
  45. 7. Edit. Text widgets support the standard Motif editing characters
  46. plus many Emacs editing characters. Backspace and Control-h erase the
  47. character to the left of the insertion cursor. Delete and Control-d
  48. erase the character to the right of the insertion cursor. Meta-backspace
  49. deletes the word to the left of the insertion cursor, and Meta-d deletes
  50. the word to the right of the insertion cursor. Control-k deletes from
  51. the insertion cursor to the end of the line, or it deletes the newline
  52. character if that is the only thing left on the line. Control-o opens
  53. a new line by inserting a newline character to the right of the insertion
  54. cursor. Control-t transposes the two characters on either side of the
  55. insertion cursor. Control-z undoes the last editing action performed,
  56. and }
  57. switch [tk windowingsystem] {
  58. "aqua" - "x11" {
  59. $w.text insert end "Control-Shift-z"
  60. }
  61. "win32" {
  62. $w.text insert end "Control-y"
  63. }
  64. }
  65. $w.text insert end { redoes undone edits.
  66. 7. Resize the window. This widget has been configured with the "setGrid"
  67. option on, so that if you resize the window it will always resize to an
  68. even number of characters high and wide. Also, if you make the window
  69. narrow you can see that long lines automatically wrap around onto
  70. additional lines so that all the information is always visible.}
  71. $w.text mark set insert 0.0