dialog.tcl 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. # ------------------------------------------------------------------------------
  2. # dialog.tcl
  3. # This file is part of Unifix BWidget Toolkit
  4. # $Id$
  5. # ------------------------------------------------------------------------------
  6. # Index of commands:
  7. # - Dialog::create
  8. # - Dialog::configure
  9. # - Dialog::cget
  10. # - Dialog::getframe
  11. # - Dialog::add
  12. # - Dialog::itemconfigure
  13. # - Dialog::itemcget
  14. # - Dialog::invoke
  15. # - Dialog::setfocus
  16. # - Dialog::enddialog
  17. # - Dialog::draw
  18. # - Dialog::withdraw
  19. # - Dialog::_destroy
  20. # ------------------------------------------------------------------------------
  21. namespace eval Dialog {
  22. ButtonBox::use
  23. Widget::bwinclude Dialog ButtonBox .bbox \
  24. remove {-orient} \
  25. initialize {-spacing 10 -padx 10}
  26. Widget::declare Dialog {
  27. {-title String "" 0}
  28. {-modal Enum local 0 {none local global}}
  29. {-bitmap TkResource "" 1 label}
  30. {-image TkResource "" 1 label}
  31. {-separator Boolean 0 1}
  32. {-cancel Int -1 0 {=-1 ""}}
  33. {-parent String "" 0}
  34. {-side Enum bottom 1 {bottom left top right}}
  35. {-anchor Enum c 1 {n e w s c}}
  36. }
  37. Widget::addmap Dialog "" :cmd {-background {}}
  38. Widget::addmap Dialog "" .frame {-background {}}
  39. proc ::Dialog { path args } { return [eval Dialog::create $path $args] }
  40. proc use {} {}
  41. bind BwDialog <Destroy> {Dialog::enddialog %W -1; Dialog::_destroy %W}
  42. variable _widget
  43. }
  44. # ------------------------------------------------------------------------------
  45. # Command Dialog::create
  46. # ------------------------------------------------------------------------------
  47. proc Dialog::create { path args } {
  48. global tcl_platform
  49. variable _widget
  50. Widget::init Dialog $path $args
  51. set bg [Widget::getoption $path -background]
  52. if { ![string compare $tcl_platform(platform) "unix"] } {
  53. toplevel $path -relief raised -borderwidth 1 -background $bg
  54. } else {
  55. toplevel $path -relief flat -borderwidth 0 -background $bg
  56. }
  57. bindtags $path [list $path BwDialog all]
  58. wm overrideredirect $path 1
  59. wm title $path [Widget::getoption $path -title]
  60. set parent [Widget::getoption $path -parent]
  61. if { ![winfo exists $parent] } {
  62. set parent [winfo parent $path]
  63. }
  64. wm transient $path [winfo toplevel $parent]
  65. wm withdraw $path
  66. set side [Widget::getoption $path -side]
  67. if { ![string compare $side "left"] || ![string compare $side "right"] } {
  68. set orient vertical
  69. } else {
  70. set orient horizontal
  71. }
  72. set bbox [eval ButtonBox::create $path.bbox [Widget::subcget $path .bbox] \
  73. -orient $orient]
  74. set frame [frame $path.frame -relief flat -borderwidth 0 -background $bg]
  75. if { [set bitmap [Widget::getoption $path -image]] != "" } {
  76. set label [label $path.label -image $bitmap -background $bg]
  77. } elseif { [set bitmap [Widget::getoption $path -bitmap]] != "" } {
  78. set label [label $path.label -bitmap $bitmap -background $bg]
  79. }
  80. if { [Widget::getoption $path -separator] } {
  81. Separator::create $path.sep -orient $orient -background $bg
  82. }
  83. set _widget($path,realized) 0
  84. set _widget($path,nbut) 0
  85. bind $path <Escape> "ButtonBox::invoke $path.bbox [Widget::getoption $path -cancel]"
  86. bind $path <Return> "ButtonBox::invoke $path.bbox default"
  87. rename $path ::$path:cmd
  88. proc ::$path { cmd args } "return \[eval Dialog::\$cmd $path \$args\]"
  89. return $path
  90. }
  91. # ------------------------------------------------------------------------------
  92. # Command Dialog::configure
  93. # ------------------------------------------------------------------------------
  94. proc Dialog::configure { path args } {
  95. set res [Widget::configure $path $args]
  96. if { [Widget::hasChanged $path -title title] } {
  97. wm title $path $title
  98. }
  99. if { [Widget::hasChanged $path -background bg] } {
  100. if { [winfo exists $path.label] } {
  101. $path.label configure -background $bg
  102. }
  103. if { [winfo exists $path.sep] } {
  104. Separator::configure $path.sep -background $bg
  105. }
  106. }
  107. return $res
  108. }
  109. # ------------------------------------------------------------------------------
  110. # Command Dialog::cget
  111. # ------------------------------------------------------------------------------
  112. proc Dialog::cget { path option } {
  113. return [Widget::cget $path $option]
  114. }
  115. # ------------------------------------------------------------------------------
  116. # Command Dialog::getframe
  117. # ------------------------------------------------------------------------------
  118. proc Dialog::getframe { path } {
  119. return $path.frame
  120. }
  121. # ------------------------------------------------------------------------------
  122. # Command Dialog::add
  123. # ------------------------------------------------------------------------------
  124. proc Dialog::add { path args } {
  125. variable _widget
  126. set res [eval ButtonBox::add $path.bbox \
  127. -command [list "Dialog::enddialog $path $_widget($path,nbut)"] $args]
  128. incr _widget($path,nbut)
  129. return $res
  130. }
  131. # ------------------------------------------------------------------------------
  132. # Command Dialog::itemconfigure
  133. # ------------------------------------------------------------------------------
  134. proc Dialog::itemconfigure { path index args } {
  135. return [eval ButtonBox::itemconfigure $path.bbox $index $args]
  136. }
  137. # ------------------------------------------------------------------------------
  138. # Command Dialog::itemcget
  139. # ------------------------------------------------------------------------------
  140. proc Dialog::itemcget { path index option } {
  141. return [ButtonBox::itemcget $path.bbox $index $option]
  142. }
  143. # ------------------------------------------------------------------------------
  144. # Command Dialog::invoke
  145. # ------------------------------------------------------------------------------
  146. proc Dialog::invoke { path index } {
  147. ButtonBox::invoke $path.bbox $index
  148. }
  149. # ------------------------------------------------------------------------------
  150. # Command Dialog::setfocus
  151. # ------------------------------------------------------------------------------
  152. proc Dialog::setfocus { path index } {
  153. ButtonBox::setfocus $path.bbox $index
  154. }
  155. # ------------------------------------------------------------------------------
  156. # Command Dialog::enddialog
  157. # ------------------------------------------------------------------------------
  158. proc Dialog::enddialog { path result } {
  159. variable _widget
  160. set _widget($path,result) $result
  161. }
  162. # ------------------------------------------------------------------------------
  163. # Command Dialog::draw
  164. # ------------------------------------------------------------------------------
  165. proc Dialog::draw { path {focus ""}} {
  166. variable _widget
  167. set parent [Widget::getoption $path -parent]
  168. if { !$_widget($path,realized) } {
  169. set _widget($path,realized) 1
  170. if { [llength [winfo children $path.bbox]] } {
  171. set side [Widget::getoption $path -side]
  172. if { ![string compare $side "left"] || ![string compare $side "right"] } {
  173. set pad -padx
  174. set fill y
  175. } else {
  176. set pad -pady
  177. set fill x
  178. }
  179. pack $path.bbox -side $side -anchor [Widget::getoption $path -anchor] -padx 1m -pady 1m
  180. if { [winfo exists $path.sep] } {
  181. pack $path.sep -side $side -fill $fill $pad 2m
  182. }
  183. }
  184. if { [winfo exists $path.label] } {
  185. pack $path.label -side left -anchor n -padx 3m -pady 3m
  186. }
  187. pack $path.frame -padx 1m -pady 1m -fill both -expand yes
  188. }
  189. if { [winfo exists $parent] } {
  190. BWidget::place $path 0 0 center $parent
  191. } else {
  192. BWidget::place $path 0 0 center
  193. }
  194. update idletasks
  195. wm overrideredirect $path 0
  196. wm deiconify $path
  197. tkwait visibility $path
  198. BWidget::focus set $path
  199. if { [winfo exists $focus] } {
  200. focus -force $focus
  201. } else {
  202. ButtonBox::setfocus $path.bbox default
  203. }
  204. if { [set grab [Widget::getoption $path -modal]] != "none" } {
  205. BWidget::grab $grab $path
  206. catch {unset _widget($path,result)}
  207. tkwait variable Dialog::_widget($path,result)
  208. if { [info exists _widget($path,result)] } {
  209. set res $_widget($path,result)
  210. unset _widget($path,result)
  211. } else {
  212. set res -1
  213. }
  214. withdraw $path
  215. return $res
  216. }
  217. return ""
  218. }
  219. # ------------------------------------------------------------------------------
  220. # Command Dialog::withdraw
  221. # ------------------------------------------------------------------------------
  222. proc Dialog::withdraw { path } {
  223. BWidget::grab release $path
  224. BWidget::focus release $path
  225. if { [winfo exists $path] } {
  226. wm withdraw $path
  227. }
  228. }
  229. # ------------------------------------------------------------------------------
  230. # Command Dialog::_destroy
  231. # ------------------------------------------------------------------------------
  232. proc Dialog::_destroy { path } {
  233. variable _widget
  234. BWidget::grab release $path
  235. BWidget::focus release $path
  236. catch {unset _widget($path,result)}
  237. unset _widget($path,realized)
  238. unset _widget($path,nbut)
  239. Widget::destroy $path
  240. rename $path {}
  241. }