PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/02-development/uDrawGraph-3.1/lib/BWidget-1.7.0/entry.tcl

https://bitbucket.org/jmelo_lyncode/thesis
TCL | 470 lines | 347 code | 55 blank | 68 comment | 109 complexity | 1792166e0dca8f683683a6aca6251e3f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0
  1. # ------------------------------------------------------------------------------
  2. # entry.tcl
  3. # This file is part of Unifix BWidget Toolkit
  4. # $Id: entry.tcl,v 1.20 2003/11/17 20:15:59 hobbs Exp $
  5. # ------------------------------------------------------------------------------
  6. # Index of commands:
  7. # - Entry::create
  8. # - Entry::configure
  9. # - Entry::cget
  10. # - Entry::_destroy
  11. # - Entry::_init_drag_cmd
  12. # - Entry::_end_drag_cmd
  13. # - Entry::_drop_cmd
  14. # - Entry::_over_cmd
  15. # - Entry::_auto_scroll
  16. # - Entry::_scroll
  17. # ------------------------------------------------------------------------------
  18. namespace eval Entry {
  19. Widget::define Entry entry DragSite DropSite DynamicHelp
  20. # Note: -textvariable is pulled off of the tk entry and put onto the
  21. # BW Entry so that we avoid the TkResource test for it, which screws up
  22. # the existance/non-existance bits of the -textvariable.
  23. Widget::tkinclude Entry entry :cmd \
  24. remove { -state -background -foreground -textvariable
  25. -disabledforeground -disabledbackground }
  26. set declare [list \
  27. [list -background TkResource "" 0 entry] \
  28. [list -foreground TkResource "" 0 entry] \
  29. [list -state Enum normal 0 [list normal disabled]] \
  30. [list -text String "" 0] \
  31. [list -textvariable String "" 0] \
  32. [list -editable Boolean 1 0] \
  33. [list -command String "" 0] \
  34. [list -relief TkResource "" 0 entry] \
  35. [list -borderwidth TkResource "" 0 entry] \
  36. [list -fg Synonym -foreground] \
  37. [list -bg Synonym -background] \
  38. [list -bd Synonym -borderwidth] \
  39. ]
  40. if {![package vsatisfies [package provide Tk] 8.4]} {
  41. ## If we're not running version 8.4 or higher, get our
  42. ## disabled resources from the button widget.
  43. lappend declare [list -disabledforeground TkResource "" 0 button]
  44. lappend declare [list -disabledbackground TkResource "" 0 \
  45. {button -background}]
  46. } else {
  47. lappend declare [list -disabledforeground TkResource "" 0 entry]
  48. lappend declare [list -disabledbackground TkResource "" 0 entry]
  49. }
  50. Widget::declare Entry $declare
  51. Widget::addmap Entry "" :cmd { -textvariable {} }
  52. DynamicHelp::include Entry balloon
  53. DragSite::include Entry "" 3
  54. DropSite::include Entry {
  55. TEXT {move {}}
  56. FGCOLOR {move {}}
  57. BGCOLOR {move {}}
  58. COLOR {move {}}
  59. }
  60. foreach event [bind Entry] {
  61. bind BwEntry $event [bind Entry $event]
  62. }
  63. # Copy is kind of a special event. It should be enabled when the
  64. # widget is editable but not disabled, and not when the widget is disabled.
  65. # To make this a bit easier to manage, we will handle it separately.
  66. bind BwEntry <<Copy>> {}
  67. bind BwEditableEntry <<Copy>> [bind Entry <<Copy>>]
  68. bind BwEntry <Return> [list Entry::invoke %W]
  69. bind BwEntry <Destroy> [list Entry::_destroy %W]
  70. bind BwDisabledEntry <Destroy> [list Entry::_destroy %W]
  71. }
  72. # ------------------------------------------------------------------------------
  73. # Command Entry::create
  74. # ------------------------------------------------------------------------------
  75. proc Entry::create { path args } {
  76. variable $path
  77. upvar 0 $path data
  78. array set maps [list Entry {} :cmd {}]
  79. array set maps [Widget::parseArgs Entry $args]
  80. set data(afterid) ""
  81. eval [list entry $path] $maps(:cmd)
  82. Widget::initFromODB Entry $path $maps(Entry)
  83. set state [Widget::getMegawidgetOption $path -state]
  84. set editable [Widget::getMegawidgetOption $path -editable]
  85. set text [Widget::getMegawidgetOption $path -text]
  86. if { $editable && [string equal $state "normal"] } {
  87. bindtags $path [list $path BwEntry [winfo toplevel $path] all]
  88. $path configure -takefocus 1
  89. } else {
  90. bindtags $path [list $path BwDisabledEntry [winfo toplevel $path] all]
  91. $path configure -takefocus 0
  92. }
  93. if { $editable == 0 } {
  94. $path configure -cursor left_ptr
  95. }
  96. if { [string equal $state "disabled"] } {
  97. $path configure \
  98. -foreground [Widget::getMegawidgetOption $path -disabledforeground] \
  99. -background [Widget::getMegawidgetOption $path -disabledbackground]
  100. } else {
  101. $path configure \
  102. -foreground [Widget::getMegawidgetOption $path -foreground] \
  103. -background [Widget::getMegawidgetOption $path -background]
  104. bindtags $path [linsert [bindtags $path] 2 BwEditableEntry]
  105. }
  106. if { [string length $text] } {
  107. set varName [$path cget -textvariable]
  108. if { ![string equal $varName ""] } {
  109. uplevel \#0 [list set $varName [Widget::cget $path -text]]
  110. } else {
  111. set validateState [$path cget -validate]
  112. $path configure -validate none
  113. $path delete 0 end
  114. $path configure -validate $validateState
  115. $path insert 0 [Widget::getMegawidgetOption $path -text]
  116. }
  117. }
  118. DragSite::setdrag $path $path Entry::_init_drag_cmd Entry::_end_drag_cmd 1
  119. DropSite::setdrop $path $path Entry::_over_cmd Entry::_drop_cmd 1
  120. DynamicHelp::sethelp $path $path 1
  121. Widget::create Entry $path
  122. proc ::$path { cmd args } \
  123. "return \[Entry::_path_command $path \$cmd \$args\]"
  124. return $path
  125. }
  126. # ------------------------------------------------------------------------------
  127. # Command Entry::configure
  128. # ------------------------------------------------------------------------------
  129. proc Entry::configure { path args } {
  130. # Cheat by setting the -text value to the current contents of the entry
  131. # This might be better hidden behind a function in ::Widget.
  132. set Widget::Entry::${path}:opt(-text) [$path:cmd get]
  133. set res [Widget::configure $path $args]
  134. # Extract the modified bits that we are interested in.
  135. set vars [list chstate cheditable chfg chdfg chbg chdbg chtext]
  136. set opts [list -state -editable -foreground -disabledforeground \
  137. -background -disabledbackground -text]
  138. foreach $vars [eval Widget::hasChangedX $path $opts] { break }
  139. if { $chstate || $cheditable } {
  140. set state [Widget::getMegawidgetOption $path -state]
  141. set editable [Widget::getMegawidgetOption $path -editable]
  142. set btags [bindtags $path]
  143. if { $editable && [string equal $state "normal"] } {
  144. set idx [lsearch $btags BwDisabledEntry]
  145. if { $idx != -1 } {
  146. bindtags $path [lreplace $btags $idx $idx BwEntry]
  147. }
  148. $path:cmd configure -takefocus 1
  149. } else {
  150. set idx [lsearch $btags BwEntry]
  151. if { $idx != -1 } {
  152. bindtags $path [lreplace $btags $idx $idx BwDisabledEntry]
  153. }
  154. $path:cmd configure -takefocus 0
  155. if { [string equal [focus] $path] } {
  156. focus .
  157. }
  158. }
  159. }
  160. if { $chstate || $chfg || $chdfg || $chbg || $chdbg } {
  161. set state [Widget::getMegawidgetOption $path -state]
  162. if { [string equal $state "disabled"] } {
  163. $path:cmd configure \
  164. -fg [Widget::cget $path -disabledforeground] \
  165. -bg [Widget::cget $path -disabledbackground]
  166. } else {
  167. $path:cmd configure \
  168. -fg [Widget::cget $path -foreground] \
  169. -bg [Widget::cget $path -background]
  170. }
  171. }
  172. if { $chstate } {
  173. if { [string equal $state "disabled"] } {
  174. set idx [lsearch -exact [bindtags $path] BwEditableEntry]
  175. if { $idx != -1 } {
  176. bindtags $path [lreplace [bindtags $path] $idx $idx]
  177. }
  178. } else {
  179. set idx [expr {[lsearch [bindtags $path] Bw*Entry] + 1}]
  180. bindtags $path [linsert [bindtags $path] $idx BwEditableEntry]
  181. }
  182. }
  183. if { $cheditable } {
  184. if { $editable } {
  185. $path:cmd configure -cursor xterm
  186. } else {
  187. $path:cmd configure -cursor left_ptr
  188. }
  189. }
  190. if { $chtext } {
  191. # Oh my lordee-ba-goordee
  192. # Do some magic to prevent multiple validation command firings.
  193. # If there is a textvariable, set that to the right value; if not,
  194. # disable validation, delete the old text, enable, then set the text.
  195. set varName [$path:cmd cget -textvariable]
  196. if { ![string equal $varName ""] } {
  197. uplevel \#0 [list set $varName \
  198. [Widget::getMegawidgetOption $path -text]]
  199. } else {
  200. set validateState [$path:cmd cget -validate]
  201. $path:cmd configure -validate none
  202. $path:cmd delete 0 end
  203. $path:cmd configure -validate $validateState
  204. $path:cmd insert 0 [Widget::getMegawidgetOption $path -text]
  205. }
  206. }
  207. DragSite::setdrag $path $path Entry::_init_drag_cmd Entry::_end_drag_cmd
  208. DropSite::setdrop $path $path Entry::_over_cmd Entry::_drop_cmd
  209. DynamicHelp::sethelp $path $path
  210. return $res
  211. }
  212. # ------------------------------------------------------------------------------
  213. # Command Entry::cget
  214. # ------------------------------------------------------------------------------
  215. proc Entry::cget { path option } {
  216. if { [string equal "-text" $option] } {
  217. return [$path:cmd get]
  218. }
  219. Widget::cget $path $option
  220. }
  221. # ------------------------------------------------------------------------------
  222. # Command Entry::invoke
  223. # ------------------------------------------------------------------------------
  224. proc Entry::invoke { path } {
  225. if { [set cmd [Widget::getMegawidgetOption $path -command]] != "" } {
  226. uplevel \#0 $cmd
  227. }
  228. }
  229. # ------------------------------------------------------------------------------
  230. # Command Entry::_path_command
  231. # ------------------------------------------------------------------------------
  232. proc Entry::_path_command { path cmd larg } {
  233. if {[string equal $cmd "configure"] || [string equal $cmd "cget"]} {
  234. return [eval [list Entry::$cmd $path] $larg]
  235. } else {
  236. return [eval [list $path:cmd $cmd] $larg]
  237. }
  238. }
  239. # ------------------------------------------------------------------------------
  240. # Command Entry::_init_drag_cmd
  241. # ------------------------------------------------------------------------------
  242. proc Entry::_init_drag_cmd { path X Y top } {
  243. variable $path
  244. upvar 0 $path data
  245. if { [set cmd [Widget::getoption $path -draginitcmd]] != "" } {
  246. return [uplevel \#0 $cmd [list $path $X $Y $top]]
  247. }
  248. set type [Widget::getoption $path -dragtype]
  249. if { $type == "" } {
  250. set type "TEXT"
  251. }
  252. if { [set drag [$path get]] != "" } {
  253. if { [$path:cmd selection present] } {
  254. set idx [$path:cmd index @[expr {$X-[winfo rootx $path]}]]
  255. set sel0 [$path:cmd index sel.first]
  256. set sel1 [expr {[$path:cmd index sel.last]-1}]
  257. if { $idx >= $sel0 && $idx <= $sel1 } {
  258. set drag [string range $drag $sel0 $sel1]
  259. set data(dragstart) $sel0
  260. set data(dragend) [expr {$sel1+1}]
  261. if { ![Widget::getoption $path -editable] ||
  262. [Widget::getoption $path -state] == "disabled" } {
  263. return [list $type {copy} $drag]
  264. } else {
  265. return [list $type {copy move} $drag]
  266. }
  267. }
  268. } else {
  269. set data(dragstart) 0
  270. set data(dragend) end
  271. if { ![Widget::getoption $path -editable] ||
  272. [Widget::getoption $path -state] == "disabled" } {
  273. return [list $type {copy} $drag]
  274. } else {
  275. return [list $type {copy move} $drag]
  276. }
  277. }
  278. }
  279. }
  280. # ------------------------------------------------------------------------------
  281. # Command Entry::_end_drag_cmd
  282. # ------------------------------------------------------------------------------
  283. proc Entry::_end_drag_cmd { path target op type dnddata result } {
  284. variable $path
  285. upvar 0 $path data
  286. if { [set cmd [Widget::getoption $path -dragendcmd]] != "" } {
  287. return [uplevel \#0 $cmd [list $path $target $op $type $dnddata $result]]
  288. }
  289. if { $result && $op == "move" && $path != $target } {
  290. $path:cmd delete $data(dragstart) $data(dragend)
  291. }
  292. }
  293. # ------------------------------------------------------------------------------
  294. # Command Entry::_drop_cmd
  295. # ------------------------------------------------------------------------------
  296. proc Entry::_drop_cmd { path source X Y op type dnddata } {
  297. variable $path
  298. upvar 0 $path data
  299. if { $data(afterid) != "" } {
  300. after cancel $data(afterid)
  301. set data(afterid) ""
  302. }
  303. if { [set cmd [Widget::getoption $path -dropcmd]] != "" } {
  304. set idx [$path:cmd index @[expr {$X-[winfo rootx $path]}]]
  305. return [uplevel \#0 $cmd [list $path $source $idx $op $type $dnddata]]
  306. }
  307. if { $type == "COLOR" || $type == "FGCOLOR" } {
  308. configure $path -foreground $dnddata
  309. } elseif { $type == "BGCOLOR" } {
  310. configure $path -background $dnddata
  311. } else {
  312. $path:cmd icursor @[expr {$X-[winfo rootx $path]}]
  313. if { $op == "move" && $path == $source } {
  314. $path:cmd delete $data(dragstart) $data(dragend)
  315. }
  316. set sel0 [$path index insert]
  317. $path:cmd insert insert $dnddata
  318. set sel1 [$path index insert]
  319. $path:cmd selection range $sel0 $sel1
  320. }
  321. return 1
  322. }
  323. # ------------------------------------------------------------------------------
  324. # Command Entry::_over_cmd
  325. # ------------------------------------------------------------------------------
  326. proc Entry::_over_cmd { path source event X Y op type dnddata } {
  327. variable $path
  328. upvar 0 $path data
  329. set x [expr {$X-[winfo rootx $path]}]
  330. if { [string equal $event "leave"] } {
  331. if { [string length $data(afterid)] } {
  332. after cancel $data(afterid)
  333. set data(afterid) ""
  334. }
  335. } elseif { [_auto_scroll $path $x] } {
  336. return 2
  337. }
  338. if { [set cmd [Widget::getoption $path -dropovercmd]] != "" } {
  339. set x [expr {$X-[winfo rootx $path]}]
  340. set idx [$path:cmd index @$x]
  341. set res [uplevel \#0 $cmd [list $path $source $event $idx $op $type $dnddata]]
  342. return $res
  343. }
  344. if { [string equal $type "COLOR"] ||
  345. [string equal $type "FGCOLOR"] ||
  346. [string equal $type "BGCOLOR"] } {
  347. DropSite::setcursor based_arrow_down
  348. return 1
  349. }
  350. if { [Widget::getoption $path -editable]
  351. && [string equal [Widget::getoption $path -state] "normal"] } {
  352. if { ![string equal $event "leave"] } {
  353. $path:cmd selection clear
  354. $path:cmd icursor @$x
  355. DropSite::setcursor based_arrow_down
  356. return 3
  357. }
  358. }
  359. DropSite::setcursor dot
  360. return 0
  361. }
  362. # ------------------------------------------------------------------------------
  363. # Command Entry::_auto_scroll
  364. # ------------------------------------------------------------------------------
  365. proc Entry::_auto_scroll { path x } {
  366. variable $path
  367. upvar 0 $path data
  368. set xmax [winfo width $path]
  369. if { $x <= 10 && [$path:cmd index @0] > 0 } {
  370. if { $data(afterid) == "" } {
  371. set data(afterid) [after 100 "Entry::_scroll $path -1 $x $xmax"]
  372. DropSite::setcursor sb_left_arrow
  373. }
  374. return 1
  375. } else {
  376. if { $x >= $xmax-10 && [$path:cmd index @$xmax] < [$path:cmd index end] } {
  377. if { $data(afterid) == "" } {
  378. set data(afterid) [after 100 "Entry::_scroll $path 1 $x $xmax"]
  379. DropSite::setcursor sb_right_arrow
  380. }
  381. return 1
  382. } else {
  383. if { $data(afterid) != "" } {
  384. after cancel $data(afterid)
  385. set data(afterid) ""
  386. }
  387. }
  388. }
  389. return 0
  390. }
  391. # ------------------------------------------------------------------------------
  392. # Command Entry::_scroll
  393. # ------------------------------------------------------------------------------
  394. proc Entry::_scroll { path dir x xmax } {
  395. variable $path
  396. upvar 0 $path data
  397. $path:cmd xview scroll $dir units
  398. $path:cmd icursor @$x
  399. if { ($dir == -1 && [$path:cmd index @0] > 0) ||
  400. ($dir == 1 && [$path:cmd index @$xmax] < [$path:cmd index end]) } {
  401. set data(afterid) [after 100 "Entry::_scroll $path $dir $x $xmax"]
  402. } else {
  403. set data(afterid) ""
  404. DropSite::setcursor dot
  405. }
  406. }
  407. # ------------------------------------------------------------------------------
  408. # Command Entry::_destroy
  409. # ------------------------------------------------------------------------------
  410. proc Entry::_destroy { path } {
  411. variable $path
  412. upvar 0 $path data
  413. Widget::destroy $path
  414. unset data
  415. }