form.tcl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. lappend auto_path $env(GISBASE)/bwidget
  2. package require -exact BWidget 1.2.1
  3. #package require http
  4. set formpath $env(GISBASE)/etc/form
  5. source $formpath/html_library.tcl
  6. proc create_submit_msg { formid } {
  7. global submit_result submit_msg formf
  8. destroy $formf($formid).sbw
  9. destroy $formf($formid).sbt
  10. if { $submit_result == 1 } { set color "green" } else { set color "red" }
  11. set sbw [ScrolledWindow $formf($formid).sbw -relief sunken -borderwidth 2]
  12. set sbt [text $formf($formid).sbt -height 3 -width 20 -foreground $color ]
  13. pack $sbw $sbt -fill x
  14. $sbw setwidget $sbt
  15. $sbt insert end $submit_msg
  16. $sbt configure -state disabled
  17. }
  18. proc add_form { formid title } {
  19. global nb formf html
  20. set form($formid) [$nb insert end $formid -text $title]
  21. $nb raise $formid
  22. set formf($formid) [ frame $form($formid).frm ]
  23. set formsw($formid) [ScrolledWindow $formf($formid).sw -relief sunken -borderwidth 2]
  24. set formtxt($formid) [ text $formf($formid).txt -height 5 -width 20 ]
  25. pack $formf($formid) $formsw($formid) $formtxt($formid) -fill both -expand yes
  26. $formsw($formid) setwidget $formtxt($formid)
  27. HMinit_win $formtxt($formid)
  28. HMparse_html $html "HMrender $formtxt($formid)"
  29. $formtxt($formid) configure -state disabled
  30. }
  31. proc clear_nb { } {
  32. global submit_msg
  33. set submit_msg ""
  34. foreach frm [ .nb pages ] {
  35. .nb delete $frm
  36. }
  37. }
  38. proc HMsubmit_form {win param query} {
  39. global submit_result submit_msg
  40. regexp -- {\.nb\.f(.+)\.frm\.txt} $win r formid
  41. #puts "win = $win formid = $formid"
  42. reset_values
  43. foreach {col val} $query {
  44. #puts "$col : $val"
  45. set_value $col $val
  46. }
  47. submit $formid
  48. #puts "result = $submit_result msg = $submit_msg"
  49. create_submit_msg $formid
  50. }
  51. proc make_form {} {
  52. global nb
  53. set nb [NoteBook .nb]
  54. $nb configure -width 300 -height 500
  55. pack .nb -fill both -expand yes
  56. }
  57. proc close_form {} {
  58. global form_open
  59. wm withdraw .
  60. set form_open false
  61. }
  62. proc process_command {} {
  63. global env
  64. global child_recv child_send
  65. global form_open encoding_val frmid
  66. global html
  67. if {[eof $child_recv]} {
  68. exit 0
  69. }
  70. set cmd [read $child_recv 1]
  71. switch $cmd {
  72. O {
  73. if {! $form_open} {
  74. wm state . normal
  75. set form_open true
  76. }
  77. # Read title
  78. set length [gets $child_recv]
  79. set child_title [read $child_recv $length]
  80. # Read html
  81. set length [gets $child_recv]
  82. set child_html [read $child_recv $length]
  83. set child_html [encoding convertfrom $encoding_val $child_html]
  84. # Insert new page
  85. set html $child_html
  86. add_form $frmid $child_title
  87. puts -nonewline $child_send O
  88. flush $child_send
  89. incr frmid
  90. }
  91. C { # clear old forms
  92. clear_nb
  93. puts -nonewline $child_send O
  94. flush $child_send
  95. }
  96. D { # done!
  97. clear_nb
  98. puts -nonewline $child_send O
  99. flush $child_send
  100. destroy .
  101. exit 0
  102. }
  103. }
  104. }
  105. make_form
  106. wm protocol . WM_DELETE_WINDOW close_form
  107. bind . <Destroy> { if { "%W" == "."} { close_form } }
  108. set submit_result ""
  109. set submit_msg ""
  110. set html ""
  111. set frmid 0
  112. set form_open true
  113. set child_recv stdin
  114. set child_send stdout
  115. set encoding_val [exec g.gisenv GRASS_DB_ENCODING]
  116. if {$encoding_val == ""} {
  117. set encoding_val [encoding system]
  118. }
  119. if {[catch {encoding system $encoding_val}]} {
  120. puts stderr "Could not set Tcl system encoding to $encoding_val"
  121. }
  122. fconfigure $child_recv -buffering none -encoding binary -translation binary
  123. fileevent $child_recv readable process_command