console.tcl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #-----------------------------------------------------
  2. #
  3. # console.tcl --
  4. #
  5. # Command console for Tk GUI applications.
  6. #
  7. # This package provides a wrapper around TkCon - a highly
  8. # functional and very portable Tcl command console. This
  9. # allows application users to type Tcl commands directly
  10. # into the executing application's Tcl interpreter. It
  11. # can also serve as a log window, catching output from
  12. # [puts] statements.
  13. #
  14. #
  15. # See the README file for operating instructions..
  16. #
  17. #
  18. # Bob Techentin
  19. # January 22, 2004
  20. #
  21. # Copyright 2004 Mayo Foundation. All rights reserved.
  22. # $Id: console.tcl,v 1.8 2004/02/12 20:54:19 techenti Exp $
  23. #
  24. #-----------------------------------------------------
  25. package require Tk
  26. package require tkcon
  27. package provide console 1.1
  28. namespace eval console {
  29. namespace export create options
  30. namespace export show
  31. namespace export hide
  32. # Define options and default values
  33. variable options
  34. array set options {
  35. -showOnStartup 0
  36. -root .console
  37. -protocol {console::hide}
  38. -showMenu 0
  39. -title "Command Console"
  40. -variable {}
  41. }
  42. }
  43. #-----------------------------------------------------
  44. # console::create --
  45. #
  46. # Create a console window - always a toplevel
  47. # but doesn't show it.
  48. #
  49. # Arguments:
  50. # None.
  51. #
  52. # Side Effects:
  53. # Creates a toplevel
  54. #
  55. # Results:
  56. # A list containing the names of all known levels,
  57. # alphabetically sorted.
  58. #------------------------------------------------------
  59. proc console::create {args} {
  60. variable options
  61. #------------------------------------------------------
  62. # process options
  63. #------------------------------------------------------
  64. for {set i 0} {$i < [llength $args]} {incr i} {
  65. set arg [lindex $args $i]
  66. set val [lindex $args [incr i]]
  67. # Copy values directly into options array
  68. if { [info exists options($arg)]} {
  69. set options($arg) $val
  70. } else {
  71. set names [lsort [array names options]]
  72. return -code error \
  73. -errorinfo "bad option \"$arg\": must be $names"
  74. }
  75. }
  76. #------------------------------------------------------
  77. # Create the console with our options.
  78. # Note that tkcon forces itself into the global
  79. # namespace, so we use fully qualified names.
  80. #------------------------------------------------------
  81. set ::tkcon::PRIV(showOnStartup) $options(-showOnStartup)
  82. set ::tkcon::PRIV(root) $options(-root)
  83. set ::tkcon::PRIV(protocol) $options(-protocol)
  84. set ::tkcon::OPT(showmenu) $options(-showMenu)
  85. set ::tkcon::OPT(exec) ""
  86. ::tkcon::Init
  87. ::tkcon title $options(-title)
  88. #------------------------------------------------------
  89. # If we have a variable name, then we need to set
  90. # up a trace to make sure that we show/hide when
  91. # that variable changes.
  92. #------------------------------------------------------
  93. if { "$options(-variable)" != "" } {
  94. # First we have to translate the variable name
  95. # into a fully qualified name (namespace which)
  96. # in the context of the caller (uplevel).
  97. set options(-variable) [uplevel namespace which -variable $options(-variable)]
  98. # Set initial value
  99. set $options(-variable) $options(-showOnStartup)
  100. # Now set the trace
  101. trace add variable $options(-variable) write \
  102. [namespace code variableChange]
  103. }
  104. }
  105. #-----------------------------------------------------
  106. # console::show --
  107. #
  108. # Show (deiconify) the console window
  109. # and optionally set the -variable.
  110. #
  111. # Arguments:
  112. # None.
  113. #
  114. # Side Effects:
  115. # shows the console window
  116. #
  117. # Results:
  118. # none.
  119. #------------------------------------------------------
  120. proc console::show {} {
  121. variable options
  122. ::tkcon show
  123. # If a variable is defined, set the varible
  124. if { $options(-variable) != "" } {
  125. # but only if we're not already in a trace
  126. catch {info level -1} level
  127. if {![string match "variableChange*" $level]} {
  128. set [set options(-variable)] 1
  129. }
  130. }
  131. }
  132. #-----------------------------------------------------
  133. # console::hide --
  134. #
  135. # Hide (iconify) the console window
  136. # and optionally set the -variable.
  137. #
  138. # Arguments:
  139. # None.
  140. #
  141. # Side Effects:
  142. # hides the console window
  143. #
  144. # Results:
  145. # none.
  146. #------------------------------------------------------
  147. proc console::hide {} {
  148. variable options
  149. ::tkcon hide
  150. # If a variable is defined, set the varible
  151. if { $options(-variable) != "" } {
  152. # but only if we're not already in a trace
  153. catch {info level -1} level
  154. if {![string match "variableChange*" $level]} {
  155. set [set options(-variable)] 0
  156. }
  157. }
  158. }
  159. #-----------------------------------------------------
  160. # console::variableChange --
  161. #
  162. # If the console was created with a -variable
  163. # that controls its visibility, then this procedure
  164. # is attached to a variable trace. It shows or
  165. # hides the console, depending on the trueness or
  166. # falseness of the variable.
  167. #
  168. # Arguments:
  169. # name1 name2 op (as is the case for variable traces)
  170. #
  171. # Side Effects:
  172. # Calls console show or console hide
  173. #
  174. # Results:
  175. # none
  176. #------------------------------------------------------
  177. proc console::variableChange {name1 name2 op} {
  178. # We _could_ assemble the right variable name in
  179. # the context of the name1, name2, and upvar,
  180. # but we already did that once...
  181. variable options
  182. if { [set $options(-variable)] } {
  183. show
  184. } else {
  185. hide
  186. }
  187. }