Control.tcl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*-mode: tcl; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: Control.tcl,v 1.3 2001/12/09 05:31:07 idiscovery Exp $
  4. #
  5. # Tix Demostration Program
  6. #
  7. # This sample program is structured in such a way so that it can be
  8. # executed from the Tix demo program "widget": it must have a
  9. # procedure called "RunSample". It should also have the "if" statment
  10. # at the end of this file so that it can be run as a standalone
  11. # program using tixwish.
  12. # This file demonstrates the use of the tixControl widget -- it is an
  13. # entry widget with up/down arrow buttons. You can use the arrow buttons
  14. # to adjust the value inside the entry widget.
  15. #
  16. # This example program uses three Control widgets. One lets you select
  17. # integer values; one lets you select floating point values and the last
  18. # one lets you select a few names.
  19. #
  20. proc RunSample {w} {
  21. # Create the tixControls on the top of the dialog box
  22. #
  23. frame $w.top -border 1 -relief raised
  24. # $w.top.a allows only integer values
  25. #
  26. # [Hint] The -options switch sets the options of the subwidgets.
  27. # [Hint] We set the label.width subwidget option of the Controls to
  28. # be 16 so that their labels appear to be aligned.
  29. #
  30. global demo_maker demo_thrust demo_num_engins
  31. set demo_maker P&W
  32. set demo_thrust 20000.0
  33. set demo_num_engins 2
  34. tixControl $w.top.a -label "Number of Engines: " -integer true \
  35. -variable demo_num_engins -min 1 -max 4\
  36. -options {
  37. entry.width 10
  38. label.width 20
  39. label.anchor e
  40. }
  41. tixControl $w.top.b -label "Thrust: " -integer false \
  42. -min 10000.0 -max 60000.0 -step 500\
  43. -variable demo_thrust \
  44. -options {
  45. entry.width 10
  46. label.width 20
  47. label.anchor e
  48. }
  49. tixControl $w.top.c -label "Engin Maker: " \
  50. -incrcmd "ctl:adjust_maker $w.top.c +1" \
  51. -decrcmd "ctl:adjust_maker $w.top.c -1" \
  52. -validatecmd "ctl:validate_maker $w.top.c" \
  53. -value "P&W" \
  54. -options {
  55. entry.width 10
  56. label.width 20
  57. label.anchor e
  58. }
  59. pack $w.top.a $w.top.b $w.top.c -side top -anchor w
  60. # Use a ButtonBox to hold the buttons.
  61. #
  62. tixButtonBox $w.box -orientation horizontal
  63. $w.box add ok -text Ok -underline 0 -command "ctl:okcmd $w" \
  64. -width 6
  65. $w.box add cancel -text Cancel -underline 0 -command "destroy $w" \
  66. -width 6
  67. pack $w.box -side bottom -fill x
  68. pack $w.top -side top -fill both -expand yes
  69. }
  70. set ctl_makers {GE P&W "Rolls Royce"}
  71. # This procedure gets called when the user presses the up/down arrow buttons.
  72. # We return the "previous" or "next" engin maker according to the "$by"
  73. # argument
  74. #
  75. proc ctl:adjust_maker {w by value} {
  76. global ctl_makers
  77. set index [lsearch $ctl_makers $value]
  78. set len [llength $ctl_makers]
  79. set index [expr $index $by]
  80. if {$index < 0} {
  81. set index [expr $len -1]
  82. }
  83. if {$index >= $len} {
  84. set index 0
  85. }
  86. return [lindex $ctl_makers $index]
  87. }
  88. proc ctl:validate_maker {w value} {
  89. global ctl_makers
  90. if {[lsearch $ctl_makers $value] == -1} {
  91. return [lindex $ctl_makers 0]
  92. } else {
  93. return $value
  94. }
  95. }
  96. proc ctl:okcmd {w} {
  97. global demo_maker demo_thrust demo_num_engins
  98. tixDemo:Status "You selected $demo_num_engins engin(s) of thrust $demo_thrust made \
  99. by $demo_maker"
  100. destroy $w
  101. }
  102. # This "if" statement makes it possible to run this script file inside or
  103. # outside of the main demo program "widget".
  104. #
  105. if {![info exists tix_demo_running]} {
  106. wm withdraw .
  107. set w .demo
  108. toplevel $w; wm transient $w ""
  109. RunSample $w
  110. bind $w <Destroy> exit
  111. }