1  proc updateWidget { widget xml_node args } {
2    variable tagCollapse
3    variable IMAGE
4    set a_args(-index) {}
5    array set a_args $args
6
7    set css        [widgetData $widget get css]
8    set xml        [widgetData $widget get xml]
9    set xml_root   [widgetData $widget get xml_root]
10    set show_tag   [widgetData $widget get show-tag]
11
12    if { [string equal $xml_root ""] } {  set xml_root    [simxml::root get $xml] } ;# if (xml_root eq "")
13    if { [string equal $xml_node ""] } {  set xml_node    $xml_root  } ;# if (xml_node eq "")
14    if { $xml_node == $xml_root      } {  set node_index  1.0 } ;# if NODE is ROOT
15
16  if { ![string equal $a_args(-index) {}] } {
17    set node_index $a_args(-index)
18  } else {
19    # BUG tag may be ELEMENT
20    set open_tag_index  [lindex [tagIndex $widget $xml_node -type open ] 0]
21    set close_tag_index [lindex [tagIndex $widget $xml_node -type close] 1]
22    if { [llength $open_tag_index] != 0 } {
23      set open_tag_index  [$widget index $open_tag_index ]
24      set close_tag_index [$widget index $close_tag_index]
25      set node_index  [$widget index [lindex $open_tag_index 0]]
26      gui::msgDebug "$open_tag_index|$close_tag_index"
27      foreach { type mark_name mark_index } [$widget dump -mark $open_tag_index $close_tag_index]  {
28        $widget mark unset $mark_name
29      } ;# foreach all MARK
30      $widget delete $open_tag_index $close_tag_index
31     } else {
32      set open_tag_index  [lindex [tagIndex $widget $xml_node -type text ] 0]
33      if { [llength $open_tag_index] != 0 } {
34        set close_tag_index [lindex [tagIndex $widget $xml_node -type text ] 1]
35        gui::msgDebug "THIS IS A TEXT: $open_tag_index | $close_tag_index"
36
37        set open_tag_index  [$widget index $open_tag_index ]
38        set close_tag_index [$widget index $close_tag_index]
39        set node_index  [$widget index [lindex $open_tag_index 0]]
40        foreach { type mark_name mark_index } [$widget dump -mark $open_tag_index $close_tag_index]  {
41          $widget mark unset $mark_name
42        } ;# foreach all MARK
43        $widget delete $open_tag_index $close_tag_index
44      } ;# if CHECK TEXT
45    } ;# if
46
47# Needed check before printed tag
48    if { ! [info exists node_index] } {
49      set par_child [simxml::nodeInfo $xml [simxml::nodeInfo $xml $xml_node parent] children]
50      if { ([set child_index [lsearch $par_child $xml_node ] ] != -1) &&
51           ($child_index != 0) } {
52        set before_tag [ lindex $par_child [expr $child_index - 1] ]
53        set close_before_index [lindex [tagIndex $widget $before_tag -type close ] 1]
54        set node_index [$widget index $close_before_index]
55        # BUG !!!! need more coding
56      } ;# if-else
57    } ;# if
58
59
60    if { ! [info exists node_index] } {
61      gui::msgDebug "ERROR: $xml_node ([simxml::nodeInfo $xml $xml_node name]) $open_tag_index"
62    } ;# if
63
64  } ;# if-else  -index exists
65
66    set indentTag    [llength [simxml::listNodePath $xml $xml_node -only-parent true]]
67    set indent_value [expr {[widgetData $widget get auto-indent] * $indentTag}]
68
69
70
71    $widget config -cursor watch
72    switch -- [simxml::nodeInfo $xml $xml_node type] {
73      TAG {
74# START TAG RENDER ==================================================
75        catch { foreach { key } [array names style] { unset style($key) } }
76        array set style [simcss::getStyle $css $xml_node -xml $xml]
77        if { $style(display) == "none" } { return $node_index } ;# if DISPLAY is none
78        if { $indent_value != 0 } { set style(margin-left) $indent_value }
79
80        set tag tag
81        set node_text $style(tag-openTag)
82
83        if { $show_tag == "OFF" } { set node_text "" }
84        switch -- $style(display) {
85          block {
86            if { [$widget compare $node_index != "$node_index linestart"] } {
87              set node_text "\n$node_text"
88            } ;# if
89          }
90          list-item {
91            set node_text "* "
92            if {  [lsearch -exact $style(list-style) inside]  != -1 } { set style(margin-left) [expr $style(margin-left) + 15] } ;# if LIST-STYLE
93            if { ([lsearch -exact $style(list-style) decimal] != -1) || \
94                 ([lsearch -exact $style(list-style-type) decimal] != -1) } {
95              set node_text "[expr [lsearch -exact \
96                                     [simxml::getNodes $xml \
97                                     [simxml::nodeInfo $xml $xml_node parent] \
98                                     name [simxml::nodeInfo $xml $xml_node name] \
99                                     -one-level-child true] \
100                                     $xml_node] \
101                             + 1]. "
102            } ;# if LIST-STYLE
103            if { [$widget compare $node_index != "$node_index linestart"] } {
104              set node_text "\n$node_text"
105            } ;# if
106          }
107        } ;# switch $style(display)
108
109        catch { set node_text "$node_text$style(content-before)" }
110
111        if { [string length $node_text] == 0 } {
112          set node_text " "
113          set tag emptyTag
114        } ;# if length $node_text == 0
115
116        set old_index $node_index
117        set node_index [insNode $widget $node_index \
118                                ${xml_node} \
119                                $node_text -tag $tag -type open]
120        if { [regexp {^(\s*<)(.+?)(\s+)(.+?)/?>\s*$} $node_text -> space1 first_word space2 attr] } {
121          set attrStart [expr [string length $space1] + [string length $first_word] + [string length $space2]]
122          set attrEnd   [expr $attrStart + [string length $attr]]
123          $widget tag add tagAttr "$old_index + $attrStart chars" "$old_index + $attrEnd chars"
124        } ;# if
125
126        $widget tag add indent$indentTag $old_index $node_index
127        eval $widget tag configure $xml_node:Style \
128                                     -rmargin  $style(margin-right) \
129                                     -lmargin1 $style(margin-left) \
130                                     -lmargin2 $style(margin-left) \
131                                     -spacing1 $style(margin-top) \
132                                     -spacing3 $style(margin-bottom) \
133                                     -justify  $style(text-align)
134        $widget tag add $xml_node:Style $old_index $node_index
135
136# _[simxml::nodeInfo $xml $xml_node name]
137
138# DO RECURSE IF NEEDED
139        set node_children  [simxml::nodeInfo $xml $xml_node children]
140        if { [llength $node_children] > 0 } {
141          foreach { children } $node_children {
142            set node_index [updateWidget $widget $children -index $node_index]
143          } ;# foreach RECURSE
144        } else {
145# OTHERWISE CHECK FOR EMPTY TEXT IN TAG
146          set text_font_style  [simcss::getFontStyle [array get style] -param true -for text]
147          set text_tag_style   [simcss::getTagStyle [array get style] -param true -for text]
148          eval {$widget tag configure $xml_node:empty} -font {$text_font_style} \
149                                                       $text_tag_style \
150                                                       -bgstipple gray12 \
151                                                       -background $style(color)
152          set node_index [insNode $widget $node_index ${xml_node} "   " \
153                                  -tag $xml_node:empty -type empty]
154        } ;# if have children node
155
156# END TAG RENDER ==================================================
157        set tag tag
158        set node_text $style(tag-closeTag)
159        if { $show_tag == "OFF" } { set node_text "" }
160        switch -- $style(display) {
161          block {
162            if { [$widget compare $node_index != "$node_index linestart"] } {
163              set node_text "$node_text\n"
164            } ;# if
165          }
166          list-item {
167            set node_text " "
168          }
169        } ;# switch
170
171        catch { set node_text "$style(content-after)$node_text" }
172
173        if { [string equal $node_text {}] } {
174          set node_text " "
175          set tag       emptyTag
176        } ;# if
177
178        set old_index  $node_index
179        set node_index [insNode $widget $node_index \
180                                ${xml_node} \
181                                $node_text -tag $tag -type close]
182# _[simxml::nodeInfo $xml $xml_node name]
183        $widget tag add indent$indentTag $old_index $node_index
184      }
185      ELEMENT {
186        array set style [simcss::getStyle $css $xml_node -xml $xml]
187        if { $style(display) == "none" } { return $node_index } ;# if DISPLAY is none
188        if { $indent_value != 0 }        { set style(margin-left) $indent_value }
189
190
191        if { [info exists style(image)] } {
192          set fileName  [file join [file dirname [simxml::xmlInfo $xml get file]] \
193                        [simxml::attribute $xml get $xml_node -name $style(image)]]
194          if { [file isfile $fileName] } {
195            set image     [image create photo -file $fileName]
196          } else {
197            set image     [image create photo -data $IMAGE(notFound)]
198          } ;# if IMAGE exists
199
200          set old_index  $node_index
201          set node_index [insNode $widget $node_index ${xml_node} \
202                                          $image -tag {} -type image]
203          if { $style(display) == "block" } {
204            $widget tag add indent$indentTag $old_index $node_index
205            eval $widget tag configure $xml_node:Style \
206                                         -rmargin  $style(margin-right) \
207                                         -lmargin1 $style(margin-left) \
208                                         -lmargin2 $style(margin-left) \
209                                         -spacing1 $style(margin-top) \
210                                         -spacing3 $style(margin-bottom) \
211                                         -justify  $style(text-align)
212            $widget tag add $xml_node:Style "$old_index linestart" $node_index
213            $widget tag raise $xml_node:Style
214          } ;# if $style(display) == block
215          return $node_index
216        } ;# if EXISTS IMAGE attribute
217
218        if { $show_tag == "OFF" } { set node_text {} }
219        set node_text $style(tag-elementTag)
220        set tag tag
221        switch -- $style(display) {
222          block {
223            if { [$widget compare $node_index != "$node_index linestart"] } {
224              set node_text "\n$node_text\n"
225            } ;# if
226          }
227        } ;# switch
228
229        if { [string equal $node_text {}] } {
230          if { $style(display) == "inline" } {
231            set node_text " "
232          } else {
233            set node_text "\n"
234          }
235          set tag emptyTag
236        } ;# if
237
238        set old_index  $node_index
239        set node_index [insNode $widget $node_index \
240                                ${xml_node} \
241                                $node_text -tag $tag -type element]
242        $widget tag add indent$indentTag $old_index $node_index
243        if { [regexp {^(\s*<)(.+?)(\s+)(.+?)/?>\s*$} $node_text -> space1 first_word space2 attr] } {
244          set attrStart [expr [string length $space1] + [string length $first_word] + [string length $space2]]
245          set attrEnd   [expr $attrStart + [string length $attr]]
246          $widget tag add tagAttr "$old_index + $attrStart chars" "$old_index + $attrEnd chars"
247        } ;# if
248      }
249      TEXT {
250        array set style [simcss::getStyle $css [simxml::nodeInfo $xml $xml_node parent] -xml $xml]
251        if { $style(display) == "none" } { return $node_index } ;# if DISPLAY is none
252        if { $indent_value != 0 } { set style(margin-left) $indent_value }
253        if { $style(list-style) == "inside" } {
254          set style(margin-left) [expr $style(margin-left) + 15]
255        } ;# if LIST-STYLE
256
257        set font_style  [simcss::getFontStyle [array get style] -param true \
258                               -for text -fontSize [widgetData $widget get fontSize]]
259        set tag_style   [simcss::getTagStyle [array get style] -param true -for text]
260
261if { 1 == 1 } {
262          set lmargin2 $style(margin-left);
263          if { $style(text-indent) != "NULL" } {
264            set lmargin1 [expr $style(margin-left) + $style(text-indent)]
265          } ;# if text-indent
266          eval $widget tag configure $xml_node:Style \
267                                      -rmargin  $style(margin-right) \
268                                      -lmargin1 $style(margin-left) \
269                                      -lmargin2 $lmargin2 \
270                                      -spacing1 $style(margin-top) \
271                                      -spacing3 $style(margin-bottom) \
272                                      -justify  $style(text-align)
273} ;# if COMMENT
274        set node_text [simxml::nodeInfo $xml $xml_node text]
275        if { [string equal $node_text {}] } {
276          set node_text "   "
277          set TAG $xml_node:empty
278          eval {$widget tag configure $TAG} -font {$font_style} $tag_style -bgstipple gray12 -background $style(color)
279          $widget tag raise $TAG
280        } else {
281          set TAG $xml_node:text
282          #set TAG $style(styleName)
283          eval {$widget tag configure $TAG} -font {$font_style} $tag_style
284          #set TAG [simcss::getStyleName $css $widget $style(styleName) $font_style $tag_style]
285          $widget tag raise $TAG
286        } ;# if-else
287
288        set old_node_index $node_index
289        set node_index [insNode $widget $node_index $xml_node $node_text -tag $TAG -type text]
290#        $widget tag add $xml_node:Style "$old_node_index linestart" "$node_index lineend"
291#        $widget tag raise $xml_node:Style
292      }
293    } ;# switch NODE TYPE
294
295    $widget tag raise sel
296    $widget config -cursor xterm
297    return $node_index
298  } ;# proc updateWidget
299
300