FileEnt.tcl 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. # -*- mode: TCL; fill-column: 75; tab-width: 8; coding: iso-latin-1-unix -*-
  2. #
  3. # $Id: FileEnt.tcl,v 1.7 2004/03/28 02:44:57 hobbs Exp $
  4. #
  5. # FileEnt.tcl --
  6. #
  7. # TixFileEntry Widget: an entry box for entering filenames.
  8. #
  9. # Copyright (c) 1993-1999 Ioi Kim Lam.
  10. # Copyright (c) 2000-2001 Tix Project Group.
  11. # Copyright (c) 2004 ActiveState
  12. #
  13. # See the file "license.terms" for information on usage and redistribution
  14. # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  15. #
  16. tixWidgetClass tixFileEntry {
  17. -classname TixFileEntry
  18. -superclass tixLabelWidget
  19. -method {
  20. invoke filedialog update
  21. }
  22. -flag {
  23. -activatecmd -command -dialogtype -disablecallback -disabledforeground
  24. -filebitmap -selectmode -state -validatecmd -value -variable
  25. }
  26. -forcecall {
  27. -variable
  28. }
  29. -static {
  30. -filebitmap
  31. }
  32. -configspec {
  33. {-activatecmd activateCmd ActivateCmd ""}
  34. {-command command Command ""}
  35. {-dialogtype dialogType DialogType ""}
  36. {-disablecallback disableCallback DisableCallback 0 tixVerifyBoolean}
  37. {-disabledforeground disabledForeground DisabledForeground #303030}
  38. {-filebitmap fileBitmap FileBitmap ""}
  39. {-selectmode selectMode SelectMode normal}
  40. {-state state State normal}
  41. {-validatecmd validateCmd ValidateCmd ""}
  42. {-value value Value ""}
  43. {-variable variable Variable ""}
  44. }
  45. -default {
  46. {*frame.borderWidth 2}
  47. {*frame.relief sunken}
  48. {*Button.highlightThickness 0}
  49. {*Entry.highlightThickness 0}
  50. {*Entry.borderWidth 0}
  51. }
  52. }
  53. proc tixFileEntry:InitWidgetRec {w} {
  54. upvar #0 $w data
  55. tixChainMethod $w InitWidgetRec
  56. set data(varInited) 0
  57. if {$data(-filebitmap) eq ""} {
  58. set data(-filebitmap) [tix getbitmap openfile]
  59. }
  60. }
  61. proc tixFileEntry:ConstructFramedWidget {w frame} {
  62. upvar #0 $w data
  63. tixChainMethod $w ConstructFramedWidget $frame
  64. set data(w:entry) [entry $frame.entry]
  65. set data(w:button) [button $frame.button -bitmap $data(-filebitmap) \
  66. -takefocus 0]
  67. set data(entryfg) [$data(w:entry) cget -fg]
  68. pack $data(w:button) -side right -fill both
  69. pack $data(w:entry) -side left -expand yes -fill both
  70. }
  71. proc tixFileEntry:SetBindings {w} {
  72. upvar #0 $w data
  73. tixChainMethod $w SetBindings
  74. $data(w:button) config -command [list tixFileEntry:OpenFile $w]
  75. tixSetMegaWidget $data(w:entry) $w
  76. # If user press <return>, verify the value and call the -command
  77. #
  78. bind $data(w:entry) <Return> [list tixFileEntry:invoke $w]
  79. bind $data(w:entry) <KeyPress> {
  80. if {[set [tixGetMegaWidget %W](-selectmode)] eq "immediate"} {
  81. tixFileEntry:invoke [tixGetMegaWidget %W]
  82. }
  83. }
  84. bind $data(w:entry) <FocusOut> {
  85. if {"%d" eq "NotifyNonlinear" || "%d" eq "NotifyNonlinearVirtual"} {
  86. tixFileEntry:invoke [tixGetMegaWidget %W]
  87. }
  88. }
  89. bind $w <FocusIn> [list focus $data(w:entry)]
  90. }
  91. #----------------------------------------------------------------------
  92. # CONFIG OPTIONS
  93. #----------------------------------------------------------------------
  94. proc tixFileEntry:config-state {w value} {
  95. upvar #0 $w data
  96. if {$value eq "normal"} {
  97. $data(w:button) config -state $value
  98. $data(w:entry) config -state $value -fg $data(entryfg)
  99. catch {$data(w:label) config -fg $data(entryfg)}
  100. } else {
  101. $data(w:button) config -state $value
  102. $data(w:entry) config -state $value -fg $data(-disabledforeground)
  103. catch {$data(w:label) config -fg $data(-disabledforeground)}
  104. }
  105. return ""
  106. }
  107. proc tixFileEntry:config-value {w value} {
  108. tixFileEntry:SetValue $w $value
  109. }
  110. proc tixFileEntry:config-variable {w arg} {
  111. upvar #0 $w data
  112. if {[tixVariable:ConfigVariable $w $arg]} {
  113. # The value of data(-value) is changed if tixVariable:ConfigVariable
  114. # returns true
  115. tixFileEntry:SetValue $w $data(-value)
  116. }
  117. catch {
  118. unset data(varInited)
  119. }
  120. set data(-variable) $arg
  121. }
  122. #----------------------------------------------------------------------
  123. # User Commands
  124. #----------------------------------------------------------------------
  125. proc tixFileEntry:invoke {w} {
  126. upvar #0 $w data
  127. if {![catch {$data(w:entry) index sel.first}]} {
  128. # THIS ENTRY OWNS SELECTION --> TURN IT OFF
  129. #
  130. $data(w:entry) select from end
  131. $data(w:entry) select to end
  132. }
  133. tixFileEntry:SetValue $w [$data(w:entry) get]
  134. }
  135. proc tixFileEntry:filedialog {w args} {
  136. upvar #0 $w data
  137. if {[llength $args]} {
  138. return [eval [tix filedialog $data(-dialogtype)] $args]
  139. } else {
  140. return [tix filedialog $data(-dialogtype)]
  141. }
  142. }
  143. proc tixFileEntry:update {w} {
  144. upvar #0 $w data
  145. if {[$data(w:entry) get] ne $data(-value)} {
  146. tixFileEntry:invoke $w
  147. }
  148. }
  149. #----------------------------------------------------------------------
  150. # Internal Commands
  151. #----------------------------------------------------------------------
  152. proc tixFileEntry:OpenFile {w} {
  153. upvar #0 $w data
  154. if {$data(-activatecmd) != ""} {
  155. uplevel #0 $data(-activatecmd)
  156. }
  157. switch -- $data(-dialogtype) tk_chooseDirectory {
  158. set args [list -parent [winfo toplevel $w]]
  159. if {[set initial $data(-value)] != ""} {
  160. lappend args -initialdir $data(value)
  161. }
  162. set retval [eval [linsert $args 0 tk_chooseDirectory]]
  163. if {$retval != ""} {tixFileEntry:SetValue $w [tixFSNative $retval]}
  164. } tk_getOpenFile - tk_getSaveFile {
  165. set args [list -parent [winfo toplevel $w]]
  166. if {[set initial [$data(w:entry) get]] != ""} {
  167. switch -glob -- $initial *.py {
  168. set types [list {"Python Files" {.py .pyw}} {"All Files" *}]
  169. } *.txt {
  170. set types [list {"Text Files" .txt} {"All Files" *}]
  171. } *.tcl {
  172. set types [list {"Tcl Files" .tcl} {"All Files" *}]
  173. } * - default {
  174. set types [list {"All Files" *}]
  175. }
  176. if {[file isfile $initial]} {
  177. lappend args -initialdir [file dir $initial] \
  178. -initialfile $initial
  179. } elseif {[file isdir $initial]} {
  180. lappend args -initialdir $initial
  181. }
  182. } else {
  183. set types [list {"All Files" *}]
  184. }
  185. lappend args -filetypes $types
  186. set retval [eval $data(-dialogtype) $args]
  187. if {$retval != ""} {tixFileEntry:SetValue $w [tixFSNative $retval]}
  188. } default {
  189. set filedlg [tix filedialog $data(-dialogtype)]
  190. $filedlg config -parent [winfo toplevel $w] \
  191. -command [list tixFileEntry:FileDlgCallback $w]
  192. focus $data(w:entry)
  193. $filedlg popup
  194. }
  195. }
  196. proc tixFileEntry:FileDlgCallback {w args} {
  197. set filename [tixEvent flag V]
  198. tixFileEntry:SetValue $w $filename
  199. }
  200. proc tixFileEntry:SetValue {w value} {
  201. upvar #0 $w data
  202. if {[llength $data(-validatecmd)]} {
  203. set value [tixEvalCmdBinding $w $data(-validatecmd) "" $value]
  204. }
  205. if {$data(-state) eq "normal"} {
  206. $data(w:entry) delete 0 end
  207. $data(w:entry) insert 0 $value
  208. $data(w:entry) xview end
  209. }
  210. set data(-value) $value
  211. tixVariable:UpdateVariable $w
  212. if {[llength $data(-command)] && !$data(-disablecallback)} {
  213. if {![info exists data(varInited)]} {
  214. set bind(specs) ""
  215. tixEvalCmdBinding $w $data(-command) bind $value
  216. }
  217. }
  218. }
  219. proc tixFileEntry:Destructor {w} {
  220. upvar #0 $w data
  221. tixUnsetMegaWidget $data(w:entry)
  222. tixVariable:DeleteVariable $w
  223. # Chain this to the superclass
  224. #
  225. tixChainMethod $w Destructor
  226. }