home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / linux / puppy-barebones-2.01r2.iso / pup_201.sfs / usr / lib / tk8.5 / tearoff.tcl < prev    next >
Encoding:
Text File  |  2006-06-17  |  4.6 KB  |  171 lines

  1. # tearoff.tcl --
  2. #
  3. # This file contains procedures that implement tear-off menus.
  4. #
  5. # RCS: @(#) $Id: tearoff.tcl,v 1.10 2005/07/25 09:06:00 dkf Exp $
  6. #
  7. # Copyright (c) 1994 The Regents of the University of California.
  8. # Copyright (c) 1994-1997 Sun Microsystems, Inc.
  9. #
  10. # See the file "license.terms" for information on usage and redistribution
  11. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12. #
  13.  
  14. # ::tk::TearoffMenu --
  15. # Given the name of a menu, this procedure creates a torn-off menu
  16. # that is identical to the given menu (including nested submenus).
  17. # The new torn-off menu exists as a toplevel window managed by the
  18. # window manager.  The return value is the name of the new menu.
  19. # The window is created at the point specified by x and y
  20. #
  21. # Arguments:
  22. # w -            The menu to be torn-off (duplicated).
  23. # x -            x coordinate where window is created
  24. # y -            y coordinate where window is created
  25.  
  26. proc ::tk::TearOffMenu {w {x 0} {y 0}} {
  27.     # Find a unique name to use for the torn-off menu.  Find the first
  28.     # ancestor of w that is a toplevel but not a menu, and use this as
  29.     # the parent of the new menu.  This guarantees that the torn off
  30.     # menu will be on the same screen as the original menu.  By making
  31.     # it a child of the ancestor, rather than a child of the menu, it
  32.     # can continue to live even if the menu is deleted;  it will go
  33.     # away when the toplevel goes away.
  34.  
  35.     if {$x == 0} {
  36.         set x [winfo rootx $w]
  37.     }
  38.     if {$y == 0} {
  39.         set y [winfo rooty $w]
  40.     if {[tk windowingsystem] eq "aqua"} {
  41.         # Avoid the native menu bar which sits on top of everything.
  42.         if {$y < 20} { set y 20 }
  43.     }
  44.     }
  45.  
  46.     set parent [winfo parent $w]
  47.     while {[winfo toplevel $parent] ne $parent \
  48.         || [winfo class $parent] eq "Menu"} {
  49.     set parent [winfo parent $parent]
  50.     }
  51.     if {$parent eq "."} {
  52.     set parent ""
  53.     }
  54.     for {set i 1} 1 {incr i} {
  55.     set menu $parent.tearoff$i
  56.     if {![winfo exists $menu]} {
  57.         break
  58.     }
  59.     }
  60.  
  61.     $w clone $menu tearoff
  62.  
  63.     # Pick a title for the new menu by looking at the parent of the
  64.     # original: if the parent is a menu, then use the text of the active
  65.     # entry.  If it's a menubutton then use its text.
  66.  
  67.     set parent [winfo parent $w]
  68.     if {[$menu cget -title] ne ""} {
  69.         wm title $menu [$menu cget -title]
  70.     } else {
  71.         switch -- [winfo class $parent] {
  72.         Menubutton {
  73.             wm title $menu [$parent cget -text]
  74.         }
  75.         Menu {
  76.             wm title $menu [$parent entrycget active -label]
  77.         }
  78.     }
  79.     }
  80.  
  81.     $menu post $x $y
  82.  
  83.     if {[winfo exists $menu] == 0} {
  84.     return ""
  85.     }
  86.  
  87.     # Set tk::Priv(focus) on entry:  otherwise the focus will get lost
  88.     # after keyboard invocation of a sub-menu (it will stay on the
  89.     # submenu).
  90.  
  91.     bind $menu <Enter> {
  92.     set tk::Priv(focus) %W
  93.     }
  94.  
  95.     # If there is a -tearoffcommand option for the menu, invoke it
  96.     # now.
  97.  
  98.     set cmd [$w cget -tearoffcommand]
  99.     if {$cmd ne ""} {
  100.     uplevel #0 $cmd [list $w $menu]
  101.     }
  102.     return $menu
  103. }
  104.  
  105. # ::tk::MenuDup --
  106. # Given a menu (hierarchy), create a duplicate menu (hierarchy)
  107. # in a given window.
  108. #
  109. # Arguments:
  110. # src -            Source window.  Must be a menu.  It and its
  111. #            menu descendants will be duplicated at dst.
  112. # dst -            Name to use for topmost menu in duplicate
  113. #            hierarchy.
  114.  
  115. proc ::tk::MenuDup {src dst type} {
  116.     set cmd [list menu $dst -type $type]
  117.     foreach option [$src configure] {
  118.     if {[llength $option] == 2} {
  119.         continue
  120.     }
  121.     if {[lindex $option 0] eq "-type"} {
  122.         continue
  123.     }
  124.     lappend cmd [lindex $option 0] [lindex $option 4]
  125.     }
  126.     eval $cmd
  127.     set last [$src index last]
  128.     if {$last eq "none"} {
  129.     return
  130.     }
  131.     for {set i [$src cget -tearoff]} {$i <= $last} {incr i} {
  132.     set cmd [list $dst add [$src type $i]]
  133.     foreach option [$src entryconfigure $i]  {
  134.         lappend cmd [lindex $option 0] [lindex $option 4]
  135.     }
  136.     eval $cmd
  137.     }
  138.  
  139.     # Duplicate the binding tags and bindings from the source menu.
  140.  
  141.     set tags [bindtags $src]
  142.     set srcLen [string length $src]
  143.  
  144.     # Copy tags to x, replacing each substring of src with dst.
  145.  
  146.     while {[set index [string first $src $tags]] != -1} {
  147.     append x [string range $tags 0 [expr {$index - 1}]]$dst
  148.     set tags [string range $tags [expr {$index + $srcLen}] end]
  149.     }
  150.     append x $tags
  151.  
  152.     bindtags $dst $x
  153.  
  154.     foreach event [bind $src] {
  155.     unset x
  156.     set script [bind $src $event]
  157.     set eventLen [string length $event]
  158.  
  159.     # Copy script to x, replacing each substring of event with dst.
  160.  
  161.     while {[set index [string first $event $script]] != -1} {
  162.         append x [string range $script 0 [expr {$index - 1}]]
  163.         append x $dst
  164.         set script [string range $script [expr {$index + $eventLen}] end]
  165.     }
  166.     append x $script
  167.  
  168.     bind $dst $event $x
  169.     }
  170. }
  171.