eglib.tcl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ###########################################################
  2. # Name: eglib.tcl
  3. # Author: Daniele Bonini (posta@elettronica.lol)
  4. # Date: 05/12/2023
  5. # Desc: Code library code.
  6. #
  7. # Code Library scaffolding and most of the code
  8. # here presented and distributed contains excerpts
  9. # from "Practical Programming in Tcl and Tk, 4th Ed."
  10. # by Brent B. Welch, Ken Jones, Jeffrey Hebbs.
  11. # The original code of these excerpts could be
  12. # borrowed from other sources which the author
  13. # and the contributors to RADXIDE have no
  14. # knowledge about.
  15. #
  16. # License: MIT. Copyrights 5 Mode (Last implementation and adaptations.)
  17. # Copyrights © 2003 Pearson Education Inc. (original excerpts.)
  18. #
  19. ###########################################################
  20. namespace eval eglib {
  21. namespace upvar ::radxide dan dan
  22. set browse(list) [list]
  23. set browse(dir) $dan(WORKDIR)/.snippets
  24. set browse(curix) -1
  25. set browse(current) ""
  26. set browse(label) {}
  27. set browse(cookie) ""
  28. # ____________________ Scrolled_Text ____________________ #
  29. proc Scrolled_Text { f args } {
  30. # Create the text to display the example
  31. frame $f
  32. eval {text $f.text -wrap none \
  33. -xscrollcommand [list $f.xscroll set] \
  34. -yscrollcommand [list $f.yscroll set]} $args
  35. scrollbar $f.xscroll -orient horizontal \
  36. -command [list $f.text xview]
  37. scrollbar $f.yscroll -orient vertical \
  38. -command [list $f.text yview]
  39. grid $f.text $f.yscroll -sticky news
  40. grid $f.xscroll -sticky news
  41. grid rowconfigure $f 0 -weight 1
  42. grid columnconfigure $f 0 -weight 1
  43. return $f.text
  44. }
  45. # ____________________ create ____________________________ #
  46. proc create {wframe} {
  47. # Create the Code Library window
  48. namespace upvar ::radxide dan dan
  49. variable browse
  50. #wm minsize $wframe 30 5
  51. #wm title $wframe "Tcl Example Browser"
  52. # Create a row of buttons along the top
  53. #set f $wframe
  54. set f [frame $wframe.menubar]
  55. pack $f -fill x; #-side left -fill both -expand 1 ;#-fill x
  56. # Create the menubutton and menu
  57. menubutton $f.ex -text Snippets -menu $f.ex.m
  58. pack $f.ex -side left
  59. set m [menu $f.ex.m]
  60. button $f.next -text Next -command ::radxide::eglib::Next
  61. button $f.prev -text Previous -command ::radxide::eglib::Previous
  62. # The Run and Reset buttons use EvalEcho that
  63. # is defined by the Tcl shell in Example 24–4 on page 389
  64. button $f.load -text Copy -command ::radxide::eglib::Copy ;#-state disabled
  65. button $f.reset -text Reset -command ::radxide::eglib::Reset
  66. # A label identifies the current example
  67. set browse(label) [set l [label $f.label]]
  68. pack $f.reset $f.load $f.next $f.prev $l -side left
  69. set browse(text) [Scrolled_Text $wframe.body \
  70. -width 40 -height 10\
  71. -setgrid false]
  72. pack $wframe.body -side left -expand 1 -fill both -anchor nw ;#-fill both -expand true
  73. # Look through the example files for their ID number.
  74. catch {
  75. foreach fpath [lsort -dictionary [glob [file join $browse(dir) *]]] {
  76. if [catch {open $fpath} in] {
  77. puts stderr "Cannot open $f: $in"
  78. continue
  79. }
  80. set ex [expr 0]
  81. while {[gets $in line] >= 0} {
  82. #if [regexp {^# Example ([0-9]+)-([0-9]+)} $line x chap ex] {
  83. #regexp {^\/\/\w+} $line x
  84. set fname [file tail $fpath]
  85. set chap [string range $fname [expr [string first \[ $fname]+1] [expr [string last \] $fname]-1]]
  86. if {[string length $chap] <=1} {
  87. set chap "Various"
  88. }
  89. set ex [incr $ex]
  90. lappend snippets($chap) $ex
  91. lappend browse(list) $fpath
  92. # Read example title
  93. #gets $in line
  94. set title($chap-$ex) [string trim $line "# "]
  95. #tk_messageBox -title radxide -icon error -message $title($chap-$ex)
  96. set file($chap-$ex) $fpath
  97. close $in
  98. break
  99. #}
  100. }
  101. }
  102. }
  103. # Create two levels of cascaded menus.
  104. # The first level divides up the chapters into chunks.
  105. # The second level has an entry for each example.
  106. option add *Menu.tearOff 0
  107. set limit 8
  108. set c 0; set i 0
  109. foreach chap [lsort [array names snippets]] {
  110. $m add cascade -label "$chap..." \
  111. -menu $m.$c
  112. set sub1(chap) [menu $m.$c]
  113. incr c
  114. set i [expr ($i +1) % $limit]
  115. foreach ex [lsort $snippets($chap)] {
  116. $sub1(chap) add command -label $title($chap-$ex) \
  117. -command [list ::radxide::eglib::Browse $file($chap-$ex)]
  118. }
  119. }
  120. }
  121. # ___________________ Browse ____________________ #
  122. proc Browse { file } {
  123. # Display a specified file. The label is updated to
  124. # reflect what is displayed, and the text is left
  125. # in a read-only mode after the example is inserted.
  126. variable browse
  127. # update the descriptive label with the filename
  128. #$browse(label) configure -text [set browse(current) [file tail $file]]
  129. set t $browse(text)
  130. $t config -state normal
  131. $t delete 1.0 end
  132. if [catch {open $file} in] {
  133. $t insert end $in
  134. } else {
  135. $t insert end [set browse(cookie) [read $in]]
  136. close $in
  137. }
  138. #$t config -state disabled
  139. }
  140. # ___________________ Next ____________________ #
  141. proc Next {} {
  142. # Browse the next files in the list
  143. variable browse
  144. if {$browse(curix) < ([llength $browse(list)] - 1)} {
  145. incr browse(curix)
  146. }
  147. ::radxide::eglib::Browse [lindex $browse(list) $browse(curix)]
  148. }
  149. # ___________________ Previous ____________________ #
  150. proc Previous {} {
  151. # Browse the previous files in the list
  152. variable browse
  153. if {$browse(curix) > 0} {
  154. incr browse(curix) -1
  155. }
  156. ::radxide::eglib::Browse [lindex $browse(list) $browse(curix)]
  157. }
  158. # ___________________ Run ____________________ #
  159. proc Copy {} {
  160. # Run the example in the shell
  161. variable browse
  162. set t $browse(text)
  163. tk_textCopy $t
  164. }
  165. # ___________________ Reset ____________________ #
  166. proc Reset {} {
  167. # Reset the slave in the eval server
  168. variable browse
  169. set t $browse(text)
  170. $t config -state normal
  171. $t delete 1.0 end
  172. #Next
  173. #Previous
  174. #$t insert end ""
  175. $t insert end $browse(cookie)
  176. #$t config -state disabled
  177. }
  178. #_______________________
  179. }
  180. # _________________________________ EOF _________________________________ #