home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_tcl_tk8.3_tk.tcl < prev    next >
Encoding:
Text File  |  2001-07-26  |  10.8 KB  |  366 lines

  1. # tk.tcl --
  2. #
  3. # Initialization script normally executed in the interpreter for each
  4. # Tk-based application.  Arranges class bindings for widgets.
  5. #
  6. # RCS: @(#) $Id: tk.tcl,v 1.20.2.1 2001/04/04 07:57:17 hobbs Exp $
  7. #
  8. # Copyright (c) 1992-1994 The Regents of the University of California.
  9. # Copyright (c) 1994-1996 Sun Microsystems, Inc.
  10. # Copyright (c) 1998-2000 Scriptics Corporation.
  11. #
  12. # See the file "license.terms" for information on usage and redistribution
  13. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  14.  
  15. # Insist on running with compatible versions of Tcl and Tk.
  16.  
  17. package require -exact Tk 8.3
  18. package require -exact Tcl 8.3
  19.  
  20. # Add Tk's directory to the end of the auto-load search path, if it
  21. # isn't already on the path:
  22.  
  23. if {[info exists auto_path] && [string compare {} $tk_library] && \
  24.     [lsearch -exact $auto_path $tk_library] < 0} {
  25.     lappend auto_path $tk_library
  26. }
  27.  
  28. # Turn off strict Motif look and feel as a default.
  29.  
  30. set tk_strictMotif 0
  31.  
  32. # Turn on useinputmethods (X Input Methods) by default.
  33. # We catch this because safe interpreters may not allow the call.
  34.  
  35. catch {tk useinputmethods 1}
  36.  
  37. # Create a ::tk namespace
  38.  
  39. namespace eval ::tk {
  40. }
  41.  
  42. # ::tk::PlaceWindow --
  43. #   place a toplevel at a particular position
  44. # Arguments:
  45. #   toplevel    name of toplevel window
  46. #   ?placement?    pointer ?center? ; places $w centered on the pointer
  47. #        widget widgetPath ; centers $w over widget_name
  48. #        defaults to placing toplevel in the middle of the screen
  49. #   ?anchor?    center or widgetPath
  50. # Results:
  51. #   Returns nothing
  52. #
  53. proc ::tk::PlaceWindow {w {place ""} {anchor ""}} {
  54.     wm withdraw $w
  55.     update idletasks
  56.     set checkBounds 1
  57.     if {[string equal -len [string length $place] $place "pointer"]} {
  58.     ## place at POINTER (centered if $anchor == center)
  59.     if {[string equal -len [string length $anchor] $anchor "center"]} {
  60.         set x [expr {[winfo pointerx $w]-[winfo reqwidth $w]/2}]
  61.         set y [expr {[winfo pointery $w]-[winfo reqheight $w]/2}]
  62.     } else {
  63.         set x [winfo pointerx $w]
  64.         set y [winfo pointery $w]
  65.     }
  66.     } elseif {[string equal -len [string length $place] $place "widget"] && \
  67.         [winfo exists $anchor] && [winfo ismapped $anchor]} {
  68.     ## center about WIDGET $anchor, widget must be mapped
  69.     set x [expr {[winfo rootx $anchor] + \
  70.         ([winfo width $anchor]-[winfo reqwidth $w])/2}]
  71.     set y [expr {[winfo rooty $anchor] + \
  72.         ([winfo height $anchor]-[winfo reqheight $w])/2}]
  73.     } else {
  74.     set x [expr {([winfo screenwidth $w]-[winfo reqwidth $w])/2}]
  75.     set y [expr {([winfo screenheight $w]-[winfo reqheight $w])/2}]
  76.     set checkBounds 0
  77.     }
  78.     if {$checkBounds} {
  79.     if {$x < 0} {
  80.         set x 0
  81.     } elseif {$x > ([winfo screenwidth $w]-[winfo reqwidth $w])} {
  82.         set x [expr {[winfo screenwidth $w]-[winfo reqwidth $w]}]
  83.     }
  84.     if {$y < 0} {
  85.         set y 0
  86.     } elseif {$y > ([winfo screenheight $w]-[winfo reqheight $w])} {
  87.         set y [expr {[winfo screenheight $w]-[winfo reqheight $w]}]
  88.     }
  89.     }
  90.     wm geometry $w +$x+$y
  91.     wm deiconify $w
  92. }
  93.  
  94. # ::tk::SetFocusGrab --
  95. #   swap out current focus and grab temporarily (for dialogs)
  96. # Arguments:
  97. #   grab    new window to grab
  98. #   focus    window to give focus to
  99. # Results:
  100. #   Returns nothing
  101. #
  102. proc ::tk::SetFocusGrab {grab {focus {}}} {
  103.     set index "$grab,$focus"
  104.     upvar ::tk::FocusGrab($index) data
  105.  
  106.     lappend data [focus]
  107.     set oldGrab [grab current $grab]
  108.     lappend data $oldGrab
  109.     if {[winfo exists $oldGrab]} {
  110.     lappend data [grab status $oldGrab]
  111.     }
  112.     # The "grab" command will fail if another application
  113.     # already holds the grab.  So catch it.
  114.     catch {grab $grab}
  115.     if {[winfo exists $focus]} {
  116.     focus $focus
  117.     }
  118. }
  119.  
  120. # ::tk::RestoreFocusGrab --
  121. #   restore old focus and grab (for dialogs)
  122. # Arguments:
  123. #   grab    window that had taken grab
  124. #   focus    window that had taken focus
  125. #   destroy    destroy|withdraw - how to handle the old grabbed window
  126. # Results:
  127. #   Returns nothing
  128. #
  129. proc ::tk::RestoreFocusGrab {grab focus {destroy destroy}} {
  130.     set index "$grab,$focus"
  131.     foreach {oldFocus oldGrab oldStatus} $::tk::FocusGrab($index) { break }
  132.     unset ::tk::FocusGrab($index)
  133.  
  134.     catch {focus $oldFocus}
  135.     grab release $grab
  136.     if {[string equal $destroy "withdraw"]} {
  137.     wm withdraw $grab
  138.     } else {
  139.     destroy $grab
  140.     }
  141.     if {[winfo exists $oldGrab] && [winfo ismapped $oldGrab]} {
  142.     if {[string equal $oldStatus "global"]} {
  143.         grab -global $oldGrab
  144.     } else {
  145.         grab $oldGrab
  146.     }
  147.     }
  148. }
  149.  
  150. # tkScreenChanged --
  151. # This procedure is invoked by the binding mechanism whenever the
  152. # "current" screen is changing.  The procedure does two things.
  153. # First, it uses "upvar" to make global variable "tkPriv" point at an
  154. # array variable that holds state for the current display.  Second,
  155. # it initializes the array if it didn't already exist.
  156. #
  157. # Arguments:
  158. # screen -        The name of the new screen.
  159.  
  160. proc tkScreenChanged screen {
  161.     set x [string last . $screen]
  162.     if {$x > 0} {
  163.     set disp [string range $screen 0 [expr {$x - 1}]]
  164.     } else {
  165.     set disp $screen
  166.     }
  167.  
  168.     uplevel #0 upvar #0 tkPriv.$disp tkPriv
  169.     global tkPriv
  170.     global tcl_platform
  171.  
  172.     if {[info exists tkPriv]} {
  173.     set tkPriv(screen) $screen
  174.     return
  175.     }
  176.     array set tkPriv {
  177.     activeMenu    {}
  178.     activeItem    {}
  179.     afterId        {}
  180.     buttons        0
  181.     buttonWindow    {}
  182.     dragging    0
  183.     focus        {}
  184.     grab        {}
  185.     initPos        {}
  186.     inMenubutton    {}
  187.     listboxPrev    {}
  188.     menuBar        {}
  189.     mouseMoved    0
  190.     oldGrab        {}
  191.     popup        {}
  192.     postedMb    {}
  193.     pressX        0
  194.     pressY        0
  195.     prevPos        0
  196.     selectMode    char
  197.     }
  198.     set tkPriv(screen) $screen
  199.     set tkPriv(tearoff) [string equal $tcl_platform(platform) "unix"]
  200.     set tkPriv(window) {}
  201. }
  202.  
  203. # Do initial setup for tkPriv, so that it is always bound to something
  204. # (otherwise, if someone references it, it may get set to a non-upvar-ed
  205. # value, which will cause trouble later).
  206.  
  207. tkScreenChanged [winfo screen .]
  208.  
  209. # tkEventMotifBindings --
  210. # This procedure is invoked as a trace whenever tk_strictMotif is
  211. # changed.  It is used to turn on or turn off the motif virtual
  212. # bindings.
  213. #
  214. # Arguments:
  215. # n1 - the name of the variable being changed ("tk_strictMotif").
  216.  
  217. proc tkEventMotifBindings {n1 dummy dummy} {
  218.     upvar $n1 name
  219.     
  220.     if {$name} {
  221.     set op delete
  222.     } else {
  223.     set op add
  224.     }
  225.  
  226.     event $op <<Cut>> <Control-Key-w>
  227.     event $op <<Copy>> <Meta-Key-w> 
  228.     event $op <<Paste>> <Control-Key-y>
  229. }
  230.  
  231. #----------------------------------------------------------------------
  232. # Define common dialogs on platforms where they are not implemented 
  233. # using compiled code.
  234. #----------------------------------------------------------------------
  235.  
  236. if {[string equal [info commands tk_chooseColor] ""]} {
  237.     proc tk_chooseColor {args} {
  238.     return [eval tkColorDialog $args]
  239.     }
  240. }
  241. if {[string equal [info commands tk_getOpenFile] ""]} {
  242.     proc tk_getOpenFile {args} {
  243.     if {$::tk_strictMotif} {
  244.         return [eval tkMotifFDialog open $args]
  245.     } else {
  246.         return [eval ::tk::dialog::file::tkFDialog open $args]
  247.     }
  248.     }
  249. }
  250. if {[string equal [info commands tk_getSaveFile] ""]} {
  251.     proc tk_getSaveFile {args} {
  252.     if {$::tk_strictMotif} {
  253.         return [eval tkMotifFDialog save $args]
  254.     } else {
  255.         return [eval ::tk::dialog::file::tkFDialog save $args]
  256.     }
  257.     }
  258. }
  259. if {[string equal [info commands tk_messageBox] ""]} {
  260.     proc tk_messageBox {args} {
  261.     return [eval tkMessageBox $args]
  262.     }
  263. }
  264. if {[string equal [info command tk_chooseDirectory] ""]} {
  265.     proc tk_chooseDirectory {args} {
  266.     return [eval ::tk::dialog::file::chooseDir::tkChooseDirectory $args]
  267.     }
  268. }
  269.     
  270. #----------------------------------------------------------------------
  271. # Define the set of common virtual events.
  272. #----------------------------------------------------------------------
  273.  
  274. switch $tcl_platform(platform) {
  275.     "unix" {
  276.     event add <<Cut>> <Control-Key-x> <Key-F20> 
  277.     event add <<Copy>> <Control-Key-c> <Key-F16>
  278.     event add <<Paste>> <Control-Key-v> <Key-F18>
  279.     event add <<PasteSelection>> <ButtonRelease-2>
  280.     # Some OS's define a goofy (as in, not <Shift-Tab>) keysym
  281.     # that is returned when the user presses <Shift-Tab>.  In order for
  282.     # tab traversal to work, we have to add these keysyms to the 
  283.     # PrevWindow event.
  284.     # The info exists is necessary, because tcl_platform(os) doesn't
  285.     # exist in safe interpreters.
  286.     if {[info exists tcl_platform(os)]} {
  287.         switch $tcl_platform(os) {
  288.         "IRIX"  -
  289.         "Linux" { event add <<PrevWindow>> <ISO_Left_Tab> }
  290.         "HP-UX" {
  291.             # This seems to be correct on *some* HP systems.
  292.             catch { event add <<PrevWindow>> <hpBackTab> }
  293.         }
  294.         }
  295.     }
  296.     trace variable tk_strictMotif w tkEventMotifBindings
  297.     set tk_strictMotif $tk_strictMotif
  298.     }
  299.     "windows" {
  300.     event add <<Cut>> <Control-Key-x> <Shift-Key-Delete>
  301.     event add <<Copy>> <Control-Key-c> <Control-Key-Insert>
  302.     event add <<Paste>> <Control-Key-v> <Shift-Key-Insert>
  303.     event add <<PasteSelection>> <ButtonRelease-2>
  304.     }
  305.     "macintosh" {
  306.     event add <<Cut>> <Control-Key-x> <Key-F2> 
  307.     event add <<Copy>> <Control-Key-c> <Key-F3>
  308.     event add <<Paste>> <Control-Key-v> <Key-F4>
  309.     event add <<PasteSelection>> <ButtonRelease-2>
  310.     event add <<Clear>> <Clear>
  311.     }
  312. }
  313.  
  314. # ----------------------------------------------------------------------
  315. # Read in files that define all of the class bindings.
  316. # ----------------------------------------------------------------------
  317.  
  318. if {[string compare $tcl_platform(platform) "macintosh"] && \
  319.     [string compare {} $tk_library]} {
  320.     source [file join $tk_library button.tcl]
  321.     source [file join $tk_library entry.tcl]
  322.     source [file join $tk_library listbox.tcl]
  323.     source [file join $tk_library menu.tcl]
  324.     source [file join $tk_library scale.tcl]
  325.     source [file join $tk_library scrlbar.tcl]
  326.     source [file join $tk_library text.tcl]
  327. }
  328.  
  329. # ----------------------------------------------------------------------
  330. # Default bindings for keyboard traversal.
  331. # ----------------------------------------------------------------------
  332.  
  333. event add <<PrevWindow>> <Shift-Tab>
  334. bind all <Tab> {tkTabToWindow [tk_focusNext %W]}
  335. bind all <<PrevWindow>> {tkTabToWindow [tk_focusPrev %W]}
  336.  
  337. # tkCancelRepeat --
  338. # This procedure is invoked to cancel an auto-repeat action described
  339. # by tkPriv(afterId).  It's used by several widgets to auto-scroll
  340. # the widget when the mouse is dragged out of the widget with a
  341. # button pressed.
  342. #
  343. # Arguments:
  344. # None.
  345.  
  346. proc tkCancelRepeat {} {
  347.     global tkPriv
  348.     after cancel $tkPriv(afterId)
  349.     set tkPriv(afterId) {}
  350. }
  351.  
  352. # tkTabToWindow --
  353. # This procedure moves the focus to the given widget.  If the widget
  354. # is an entry, it selects the entire contents of the widget.
  355. #
  356. # Arguments:
  357. # w - Window to which focus should be set.
  358.  
  359. proc tkTabToWindow {w} {
  360.     if {[string equal [winfo class $w] Entry]} {
  361.     $w selection range 0 end
  362.     $w icursor end
  363.     }
  364.     focus $w
  365. }
  366.