optMenu.tcl 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # optMenu.tcl --
  2. #
  3. # This file defines the procedure tk_optionMenu, which creates
  4. # an option button and its associated menu.
  5. #
  6. # Copyright (c) 1994 The Regents of the University of California.
  7. # Copyright (c) 1994 Sun Microsystems, Inc.
  8. #
  9. # See the file "license.terms" for information on usage and redistribution
  10. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11. #
  12. # ::tk_optionMenu --
  13. # This procedure creates an option button named $w and an associated
  14. # menu. Together they provide the functionality of Motif option menus:
  15. # they can be used to select one of many values, and the current value
  16. # appears in the global variable varName, as well as in the text of
  17. # the option menubutton. The name of the menu is returned as the
  18. # procedure's result, so that the caller can use it to change configuration
  19. # options on the menu or otherwise manipulate it.
  20. #
  21. # Arguments:
  22. # w - The name to use for the menubutton.
  23. # varName - Global variable to hold the currently selected value.
  24. # firstValue - First of legal values for option (must be >= 1).
  25. # args - Any number of additional values.
  26. proc ::tk_optionMenu {w varName firstValue args} {
  27. upvar #0 $varName var
  28. if {![info exists var]} {
  29. set var $firstValue
  30. }
  31. menubutton $w -textvariable $varName -indicatoron 1 -menu $w.menu \
  32. -relief raised -highlightthickness 1 -anchor c \
  33. -direction flush
  34. menu $w.menu -tearoff 0
  35. $w.menu add radiobutton -label $firstValue -variable $varName
  36. foreach i $args {
  37. $w.menu add radiobutton -label $i -variable $varName
  38. }
  39. return $w.menu
  40. }