1 | # -*- mode: tcl; indent-tabs-mode: nil -*- |
---|
2 | # ---------------------------------------------------------------------- |
---|
3 | # COMPONENT: periodictable - drop-down list of items |
---|
4 | # |
---|
5 | # This is a drop-down listbox, which might be used in conjunction |
---|
6 | # with a combobox. |
---|
7 | # |
---|
8 | # ====================================================================== |
---|
9 | # AUTHOR: George Howlett, Purdue University |
---|
10 | # Copyright (c) 2004-2012 HUBzero Foundation, LLC |
---|
11 | # |
---|
12 | # See the file "license.terms" for information on usage and |
---|
13 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | # ====================================================================== |
---|
15 | package require Itk |
---|
16 | |
---|
17 | option add *PeriodicTable.textBackground white widgetDefault |
---|
18 | option add *PeriodicTable.outline black widgetDefault |
---|
19 | option add *PeriodicTable.borderwidth 1 widgetDefault |
---|
20 | option add *PeriodicTable.relief flat widgetDefault |
---|
21 | option add *PeriodicTable.font "Arial 8" |
---|
22 | |
---|
23 | itcl::class Rappture::PeriodicTable { |
---|
24 | inherit Rappture::Dropdown |
---|
25 | |
---|
26 | constructor {args} { # defined below } |
---|
27 | |
---|
28 | public method active { list } |
---|
29 | public method inactive { list } |
---|
30 | public method get {args} |
---|
31 | public method select {name} |
---|
32 | public method value { {name ""} } |
---|
33 | |
---|
34 | public proc FindElement { string } |
---|
35 | public proc ElementIsType { string args } |
---|
36 | |
---|
37 | private variable _dispatcher "" |
---|
38 | private variable _current "" |
---|
39 | private variable _state |
---|
40 | |
---|
41 | protected method _adjust {{widget ""}} |
---|
42 | |
---|
43 | protected method _react {} |
---|
44 | protected method Redraw {} |
---|
45 | protected method Activate { widget id x y } |
---|
46 | protected method Deactivate { widget id } |
---|
47 | private common _colors |
---|
48 | array set _colors { |
---|
49 | actinoid-activebackground \#cd679a |
---|
50 | actinoid-activeforeground white |
---|
51 | actinoid-disabledbackground \#ff99cc |
---|
52 | actinoid-disabledforeground \#D97DAB |
---|
53 | actinoid-background \#ff99cc |
---|
54 | actinoid-foreground black |
---|
55 | alkali-metal-activebackground \#cd3434 |
---|
56 | alkali-metal-activeforeground white |
---|
57 | alkali-metal-disabledbackground \#ff6666 |
---|
58 | alkali-metal-disabledforeground \#D04747 |
---|
59 | alkali-metal-background \#ff6666 |
---|
60 | alkali-metal-foreground black |
---|
61 | alkaline-earth-metal-activebackground \#cdac7b |
---|
62 | alkaline-earth-metal-activeforeground white |
---|
63 | alkaline-earth-metal-disabledbackground \#ffdead |
---|
64 | alkaline-earth-metal-disabledforeground \#C19A64 |
---|
65 | alkaline-earth-metal-background \#ffdead |
---|
66 | alkaline-earth-metal-foreground black |
---|
67 | halogen-activebackground \#cdcd67 |
---|
68 | halogen-activeforeground white |
---|
69 | halogen-disabledbackground \#ffff99 |
---|
70 | halogen-disabledforeground \#D5D562 |
---|
71 | halogen-background \#ffff99 |
---|
72 | halogen-foreground black |
---|
73 | lanthanoid-activebackground \#cd8dcd |
---|
74 | lanthanoid-activeforeground white |
---|
75 | lanthanoid-disabledbackground \#ffbfff |
---|
76 | lanthanoid-disabledforeground \#D884D8 |
---|
77 | lanthanoid-background \#ffbfff |
---|
78 | lanthanoid-foreground black |
---|
79 | metalloid-activebackground \#9a9a67 |
---|
80 | metalloid-activeforeground white |
---|
81 | metalloid-disabledbackground \#cccc99 |
---|
82 | metalloid-disabledforeground \#92922C |
---|
83 | metalloid-background \#cccc99 |
---|
84 | metalloid-foreground black |
---|
85 | noble-gas-activebackground \#8ecdcd |
---|
86 | noble-gas-activeforeground white |
---|
87 | noble-gas-disabledbackground \#c0ffff |
---|
88 | noble-gas-disabledforeground \#7FC1C1 |
---|
89 | noble-gas-background \#c0ffff |
---|
90 | noble-gas-foreground black |
---|
91 | other-non-metal-activebackground \#6ecd6e |
---|
92 | other-non-metal-activeforeground white |
---|
93 | other-non-metal-disabledbackground \#a0ffa0 |
---|
94 | other-non-metal-disabledforeground \#6ACD6A |
---|
95 | other-non-metal-background \#a0ffa0 |
---|
96 | other-non-metal-foreground black |
---|
97 | post-transition-metal-activebackground \#9a9a9a |
---|
98 | post-transition-metal-activeforeground white |
---|
99 | post-transition-metal-disabledbackground \#cccccc |
---|
100 | post-transition-metal-disabledforeground \#999999 |
---|
101 | post-transition-metal-background \#cccccc |
---|
102 | post-transition-metal-foreground black |
---|
103 | transition-metal-activebackground \#cd8e8e |
---|
104 | transition-metal-activeforeground white |
---|
105 | transition-metal-disabledbackground \#ffc0c0 |
---|
106 | transition-metal-disabledforeground \#C77E7E |
---|
107 | transition-metal-background \#ffc0c0 |
---|
108 | transition-metal-foreground black |
---|
109 | unknown-activebackground \#cdcdcd |
---|
110 | unknown-activeforeground white |
---|
111 | unknown-disabledbackground \#ffffff |
---|
112 | unknown-disabledforeground \#B9B9B9 |
---|
113 | unknown-background \#ffffff |
---|
114 | unknown-foreground black |
---|
115 | } |
---|
116 | private common _tableData { |
---|
117 | Hydrogen 1 H 1.0079 1 1 other-non-metal |
---|
118 | Helium 2 He 4.0026 1 18 noble-gas |
---|
119 | Lithium 3 Li 6.941(2) 2 1 alkali-metal |
---|
120 | Beryllium 4 Be 9.0122 2 2 alkaline-earth-metal |
---|
121 | Boron 5 B 10.811(7) 2 13 metalloid |
---|
122 | Carbon 6 C 12.011 2 14 other-non-metal |
---|
123 | Nitrogen 7 N 14.007 2 15 other-non-metal |
---|
124 | Oxygen 8 O 15.999 2 16 other-non-metal |
---|
125 | Fluorine 9 F 18.998 2 17 halogen |
---|
126 | Neon 10 Ne 20.180 2 18 noble-gas |
---|
127 | |
---|
128 | Sodium 11 Na 22.990 3 1 alkali-metal |
---|
129 | Magnesium 12 Mg 24.305 3 2 alkaline-earth-metal |
---|
130 | Aluminium 13 Al 26.982 3 13 post-transition-metal |
---|
131 | Silicon 14 Si 28.086 3 14 metalloid |
---|
132 | Phosphorus 15 P 30.974 3 15 other-non-metal |
---|
133 | Sulfur 16 S 32.066(6) 3 16 other-non-metal |
---|
134 | Chlorine 17 Cl 35.453 3 17 halogen |
---|
135 | Argon 18 Ar 39.948(1) 3 18 noble-gas |
---|
136 | |
---|
137 | Potassium 19 K 39.098 4 1 alkali-metal |
---|
138 | Calcium 20 Ca 40.078(4) 4 2 alkaline-earth-metal |
---|
139 | Scandium 21 Sc 44.956 4 3 transition-metal |
---|
140 | Titanium 22 Ti 47.867(1) 4 4 transition-metal |
---|
141 | Vanadium 23 V 50.942(1) 4 5 transition-metal |
---|
142 | Chromium 24 Cr 51.996 4 6 transition-metal |
---|
143 | Manganese 25 Mn 54.938 4 7 transition-metal |
---|
144 | Iron 26 Fe 55.845(2) 4 8 transition-metal |
---|
145 | Cobalt 27 Co 58.933 4 9 transition-metal |
---|
146 | Nickel 28 Ni 58.693 4 10 transition-metal |
---|
147 | Copper 29 Cu 63.546(3) 4 11 transition-metal |
---|
148 | Zinc 30 Zn 65.39(2) 4 12 transition-metal |
---|
149 | Gallium 31 Ga 69.723(1) 4 13 post-transition-metal |
---|
150 | Germanium 32 Ge 72.61(2) 4 14 metalloid |
---|
151 | Arsenic 33 As 74.922 4 15 metalloid |
---|
152 | Selenium 34 Se 78.96(3) 4 16 other-non-metal |
---|
153 | Bromine 35 Br 79.904(1) 4 17 halogen |
---|
154 | Krypton 36 Kr 83.80(1) 4 18 noble-gas |
---|
155 | |
---|
156 | Rubidium 37 Rb 85.468 5 1 alkali-metal |
---|
157 | Strontium 38 Sr 87.62(1) 5 2 alkaline-earth-metal |
---|
158 | Yttrium 39 Y 88.906 5 3 transition-metal |
---|
159 | Zirconium 40 Zr 91.224(2) 5 4 transition-metal |
---|
160 | Niobium 41 Nb 92.906 5 5 transition-metal |
---|
161 | Molybdenum 42 Mo 95.94(1) 5 6 transition-metal |
---|
162 | Technetium 43 Tc [97.907] 5 7 transition-metal |
---|
163 | Ruthenium 44 Ru 101.07(2) 5 8 transition-metal |
---|
164 | Rhodium 45 Rh 102.906 5 9 transition-metal |
---|
165 | Palladium 46 Pd 106.42(1) 5 10 transition-metal |
---|
166 | Silver 47 Ag 107.868 5 11 transition-metal |
---|
167 | Cadmium 48 Cd 112.411(8) 5 12 transition-metal |
---|
168 | Indium 49 In 114.818(3) 5 13 post-transition-metal |
---|
169 | Tin 50 Sn 118.710(7) 5 14 post-transition-metal |
---|
170 | Antimony 51 Sb 121.760(1) 5 15 metalloid |
---|
171 | Tellurium 52 Te 127.60(3) 5 16 metalloid |
---|
172 | Iodine 53 I 126.904 5 17 halogen |
---|
173 | Xenon 54 Xe 131.29(2) 5 18 noble-gas |
---|
174 | |
---|
175 | Cesium 55 Cs 132.905 6 1 alkali-metal |
---|
176 | Barium 56 Ba 137.327(7) 6 2 alkaline-earth-metal |
---|
177 | Lanthanides 57-71 * * 6 3 lanthanoid |
---|
178 | Hafnium 72 Hf 178.49(2) 6 4 transition-metal |
---|
179 | Tantalum 73 Ta 180.948 6 5 transition-metal |
---|
180 | Tungsten 74 W 183.84(1) 6 6 transition-metal |
---|
181 | Rhenium 75 Re 186.207(1) 6 7 transition-metal |
---|
182 | Osmium 76 Os 190.23(3) 6 8 transition-metal |
---|
183 | Iridium 77 Ir 192.217(3) 6 9 transition-metal |
---|
184 | Platinum 78 Pt 195.084(9) 6 10 transition-metal |
---|
185 | Gold 79 Au 196.967 6 11 transition-metal |
---|
186 | Mercury 80 Hg 200.59(2) 6 12 transition-metal |
---|
187 | Thallium 81 Tl 204.383 6 13 post-transition-metal |
---|
188 | Lead 82 Pb 207.2(1) 6 14 post-transition-metal |
---|
189 | Bismuth 83 Bi 208.980 6 15 post-transition-metal |
---|
190 | Polonium 84 Po [208.982] 6 16 metalloid |
---|
191 | Astatine 85 At [209.987] 6 17 halogen |
---|
192 | Radon 86 Rn [222.018] 6 18 noble-gas |
---|
193 | |
---|
194 | Francium 87 Fr [223.020] 7 1 alkali-metal |
---|
195 | Radium 88 Ra [226.0254] 7 2 alkaline-earth-metal |
---|
196 | Actinides 89-103 * * 7 3 actinoid |
---|
197 | Rutherfordium 104 Rf [263.113] 7 4 transition-metal |
---|
198 | Dubnium 105 Db [262.114] 7 5 transition-metal |
---|
199 | Seaborgium 106 Sg [266.122] 7 6 transition-metal |
---|
200 | Bohrium 107 Bh [264.1247] 7 7 transition-metal |
---|
201 | Hassium 108 Hs [269.134] 7 8 transition-metal |
---|
202 | Meitnerium 109 Mt [268.139] 7 9 transition-metal |
---|
203 | Darmstadtium 110 Ds [272.146] 7 10 transition-metal |
---|
204 | Roentgenium 111 Rg [272.154] 7 11 transition-metal |
---|
205 | Ununbium 112 Uub [277] 7 12 transition-metal |
---|
206 | Ununtrium 113 Uut [284] 7 13 post-transition-metal |
---|
207 | Ununquadium 114 Uuq [289] 7 14 post-transition-metal |
---|
208 | Ununpentium 115 Uup [288] 7 15 post-transition-metal |
---|
209 | Ununhexium 116 Uuh [292] 7 16 post-transition-metal |
---|
210 | Ununseptium 117 Uus ? 7 17 unknown |
---|
211 | Ununoctium 118 Uuo [294] 7 18 unknown |
---|
212 | |
---|
213 | Lanthanum 57 La 138.905 8 3 lanthanoid |
---|
214 | Cerium 58 Ce 140.116(1) 8 4 lanthanoid |
---|
215 | Praseodymium 59 Pr 140.908 8 5 lanthanoid |
---|
216 | Neodymium 60 Nd 144.242(3) 8 6 lanthanoid |
---|
217 | Promethium 61 Pm [144.913] 8 7 lanthanoid |
---|
218 | Samarium 62 Sm 150.36(2) 8 8 lanthanoid |
---|
219 | Europium 63 Eu 151.964(1) 8 9 lanthanoid |
---|
220 | Gadolinium 64 Gd 157.25(3) 8 10 lanthanoid |
---|
221 | Terbium 65 Tb 158.925 8 11 lanthanoid |
---|
222 | Dysprosium 66 Dy 162.500(1) 8 12 lanthanoid |
---|
223 | Holmium 67 Ho 164.930 8 13 lanthanoid |
---|
224 | Erbium 68 Er 167.259(3) 8 14 lanthanoid |
---|
225 | Thulium 69 Tm 168.934 8 15 lanthanoid |
---|
226 | Ytterbium 70 Yb 173.04(3) 8 16 lanthanoid |
---|
227 | Lutetium 71 Lu 174.967(1) 8 17 lanthanoid |
---|
228 | |
---|
229 | Actinium 89 Ac [227.027] 9 3 actinoid |
---|
230 | Thorium 90 Th 232.038 9 4 actinoid |
---|
231 | Protactinium 91 Pa 231.036 9 5 actinoid |
---|
232 | Uranium 92 U 238.029 9 6 actinoid |
---|
233 | Neptunium 93 Np [237.048] 9 7 actinoid |
---|
234 | Plutonium 94 Pu [244.064] 9 8 actinoid |
---|
235 | Americium 95 Am [243.061] 9 9 actinoid |
---|
236 | Curium 96 Cm [247.070] 9 10 actinoid |
---|
237 | Berkelium 97 Bk [247.070] 9 11 actinoid |
---|
238 | Californium 98 Cf [251.080] 9 12 actinoid |
---|
239 | Einsteinium 99 Es [252.083] 9 13 actinoid |
---|
240 | Fermium 100 Fm [257.095] 9 14 actinoid |
---|
241 | Mendelevium 101 Md [258.098] 9 15 actinoid |
---|
242 | Nobelium 102 No [259.101] 9 16 actinoid |
---|
243 | Lawrencium 103 Lr [262.110] 9 17 actinoid |
---|
244 | } |
---|
245 | |
---|
246 | private common _table |
---|
247 | foreach { name number symbol weight row column type } $_tableData { |
---|
248 | set _table($name) [list name $name number $number symbol $symbol \ |
---|
249 | weight $weight row $row column $column type $type] |
---|
250 | } |
---|
251 | |
---|
252 | private common _types |
---|
253 | array set _types { |
---|
254 | actinoid { |
---|
255 | Actinides Actinium Americium Berkelium Californium Curium |
---|
256 | Einsteinium Fermium Mendelevium Neptunium Plutonium Protactinium |
---|
257 | Thorium Uranium Lawrencium Nobelium |
---|
258 | } |
---|
259 | alkali-metal { |
---|
260 | Cesium Francium Lithium Potassium Rubidium Sodium |
---|
261 | } |
---|
262 | alkaline-earth-metal { |
---|
263 | Barium Beryllium Calcium Magnesium Radium Strontium |
---|
264 | } |
---|
265 | halogen { |
---|
266 | Astatine Bromine Chlorine Fluorine Iodine |
---|
267 | } |
---|
268 | lanthanoid { |
---|
269 | Cerium Erbium Europium Gadolinium Holmium Lanthanides Lanthanum |
---|
270 | Lutetium Neodymium Praseodymium Promethium Samarium Terbium |
---|
271 | Thulium Ytterbium Dysprosium |
---|
272 | } |
---|
273 | metalloid { |
---|
274 | Arsenic Boron Germanium Polonium Silicon Tellurium Antimony |
---|
275 | } |
---|
276 | noble-gas { |
---|
277 | Argon Helium Krypton Neon Radon Xenon |
---|
278 | } |
---|
279 | other-non-metal { |
---|
280 | Carbon Hydrogen Nitrogen Sulfur Oxygen Phosphorus Selenium |
---|
281 | } |
---|
282 | post-transition-metal { |
---|
283 | Aluminium Bismuth Gallium Indium Lead Thallium Tin Ununhexium |
---|
284 | Ununpentium Ununquadium Ununtrium |
---|
285 | } |
---|
286 | transition-metal { |
---|
287 | Chromium Cobalt Copper Dubnium Gold Hafnium Hassium Iridium |
---|
288 | Iron Manganese Meitnerium Mercury Molybdenum Nickel Niobium |
---|
289 | Osmium Palladium Rhenium Rhodium Roentgenium Ruthenium |
---|
290 | Rutherfordium Scandium Seaborgium Silver Tantalum Technetium |
---|
291 | Titanium Tungsten Ununbium Vanadium Yttrium Zinc Zirconium |
---|
292 | Bohrium Cadmium Darmstadtium Platinum |
---|
293 | } |
---|
294 | unknown { |
---|
295 | Ununoctium |
---|
296 | Ununseptium |
---|
297 | } |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | itk::usual PeriodicTable { |
---|
302 | keep -background -outline -cursor |
---|
303 | } |
---|
304 | |
---|
305 | # ---------------------------------------------------------------------- |
---|
306 | # CONSTRUCTOR |
---|
307 | # ---------------------------------------------------------------------- |
---|
308 | itcl::body Rappture::PeriodicTable::constructor {args} { |
---|
309 | Rappture::dispatcher _dispatcher |
---|
310 | $_dispatcher register !rebuild |
---|
311 | $_dispatcher dispatch $this !rebuild "[itcl::code $this Redraw]; list" |
---|
312 | |
---|
313 | foreach name [array names _table] { |
---|
314 | set _state($name) "normal" |
---|
315 | } |
---|
316 | |
---|
317 | itk_component add scroller { |
---|
318 | Rappture::Scroller $itk_interior.sc \ |
---|
319 | -xscrollmode off -yscrollmode auto |
---|
320 | } |
---|
321 | pack $itk_component(scroller) -expand yes -fill both |
---|
322 | |
---|
323 | itk_component add table { |
---|
324 | canvas $itk_component(scroller).table \ |
---|
325 | -highlightthickness 0 |
---|
326 | } { |
---|
327 | usual |
---|
328 | rename -background -textbackground textBackground Background |
---|
329 | ignore -highlightthickness -highlightbackground -highlightcolor |
---|
330 | keep -relief |
---|
331 | } |
---|
332 | $itk_component(scroller) contents $itk_component(table) |
---|
333 | |
---|
334 | # add bindings so the table can react to selections |
---|
335 | bind RappturePeriodicTable-$this <ButtonRelease-1> [itcl::code $this _react] |
---|
336 | bind RappturePeriodicTable-$this <Return> [itcl::code $this _react] |
---|
337 | bind RappturePeriodicTable-$this <KP_Enter> [itcl::code $this _react] |
---|
338 | bind RappturePeriodicTable-$this <space> [itcl::code $this _react] |
---|
339 | bind RappturePeriodicTable-$this <Escape> [itcl::code $this unpost] |
---|
340 | |
---|
341 | set btags [bindtags $itk_component(table)] |
---|
342 | set i [lsearch $btags [winfo class $itk_component(table)]] |
---|
343 | if {$i < 0} { |
---|
344 | set i end |
---|
345 | } |
---|
346 | set btags [linsert $btags [expr {$i+1}] RappturePeriodicTable-$this] |
---|
347 | bindtags $itk_component(table) $btags |
---|
348 | |
---|
349 | eval itk_initialize $args |
---|
350 | Redraw |
---|
351 | } |
---|
352 | |
---|
353 | # |
---|
354 | # active <list of elements> |
---|
355 | # |
---|
356 | # Enables zero or more elements in the periodic table so that |
---|
357 | # they can be selected. All elements are first disabled. Each |
---|
358 | # argument can one of the following forms: |
---|
359 | # 1. element name. |
---|
360 | # 2. element symbol. |
---|
361 | # 3. element number. |
---|
362 | # 4. type of element. The argument is expanded into all |
---|
363 | # elements of that type. |
---|
364 | # |
---|
365 | itcl::body Rappture::PeriodicTable::active { list } { |
---|
366 | set c $itk_component(table) |
---|
367 | foreach elem [array names _table] { |
---|
368 | set _state($elem) "disabled" |
---|
369 | $c bind $elem <Enter> {} |
---|
370 | $c bind $elem <Leave> {} |
---|
371 | $c bind $elem <ButtonRelease-1> {} |
---|
372 | } |
---|
373 | # Expand any arguments that represent a group of elements. |
---|
374 | set arglist {} |
---|
375 | foreach arg $list { |
---|
376 | if { [info exists _types($arg)] } { |
---|
377 | set arglist [concat $arglist $_types($arg)] |
---|
378 | } else { |
---|
379 | lappend arglist $arg |
---|
380 | } |
---|
381 | } |
---|
382 | foreach arg $arglist { |
---|
383 | set elem [FindElement $arg] |
---|
384 | if { $elem == "" } { |
---|
385 | puts stderr "unknown element \"$arg\"" |
---|
386 | } else { |
---|
387 | set _state($elem) "normal" |
---|
388 | $c bind $elem <Enter> \ |
---|
389 | [itcl::code $this Activate %W $elem %X %Y] |
---|
390 | $c bind $elem <Leave> [itcl::code $this Deactivate %W $elem] |
---|
391 | $c bind $elem <ButtonRelease-1> [itcl::code $this value $elem] |
---|
392 | } |
---|
393 | } |
---|
394 | $_dispatcher event -idle !rebuild |
---|
395 | } |
---|
396 | |
---|
397 | # |
---|
398 | # inactive <list of elements> |
---|
399 | # |
---|
400 | # Disables zero or more elements in the periodic table so that |
---|
401 | # they can't be selected. All elements are first enabled. Each |
---|
402 | # argument can one of the following forms: |
---|
403 | # 1. element name. |
---|
404 | # 2. element symbol. |
---|
405 | # 3. element number. |
---|
406 | # 4. type of element. The argument is expanded into all |
---|
407 | # elements of that type. |
---|
408 | # |
---|
409 | itcl::body Rappture::PeriodicTable::inactive { list } { |
---|
410 | set c $itk_component(table) |
---|
411 | foreach elem [array names _table] { |
---|
412 | set _state($elem) "normal" |
---|
413 | $c bind $elem <Enter> \ |
---|
414 | [itcl::code $this Activate %W $elem %X %Y] |
---|
415 | $c bind $elem <Leave> [itcl::code $this Deactivate %W $elem] |
---|
416 | $c bind $elem <ButtonRelease-1> [itcl::code $this value $elem] |
---|
417 | } |
---|
418 | # Expand any arguments that represent a group of elements. |
---|
419 | set arglist {} |
---|
420 | foreach arg $list { |
---|
421 | if { [info exists _types($arg)] } { |
---|
422 | set arglist [concat $arglist $_types($arg)] |
---|
423 | } else { |
---|
424 | lappend arglist $arg |
---|
425 | } |
---|
426 | } |
---|
427 | foreach arg $arglist { |
---|
428 | set elem [FindElement $arg] |
---|
429 | if { $elem == "" } { |
---|
430 | puts stderr "unknown element \"$arg\"" |
---|
431 | } else { |
---|
432 | set _state($elem) "disabled" |
---|
433 | $c bind $elem <Enter> {} |
---|
434 | $c bind $elem <Leave> {} |
---|
435 | $c bind $elem <ButtonRelease-1> {} |
---|
436 | } |
---|
437 | } |
---|
438 | $_dispatcher event -idle !rebuild |
---|
439 | } |
---|
440 | |
---|
441 | # ---------------------------------------------------------------------- |
---|
442 | # USAGE: get ?-symbol|-name|-all? ?name? |
---|
443 | # |
---|
444 | # Queries one or more values from the drop-down list. With no args, |
---|
445 | # it returns a list of all values and labels in the list. If the |
---|
446 | # index is specified, then it returns the value or label (or both) |
---|
447 | # for the specified index. |
---|
448 | # ---------------------------------------------------------------------- |
---|
449 | itcl::body Rappture::PeriodicTable::get { args } { |
---|
450 | set first [lindex $args 0] |
---|
451 | set choices {-symbol -weight -number -name -all} |
---|
452 | set format "-name" |
---|
453 | if {[string index $first 0] == "-"} { |
---|
454 | if {[lsearch $choices $first] < 0} { |
---|
455 | error "bad option \"$first\": should be [join [lsort $choices] {, }]" |
---|
456 | } |
---|
457 | set format $first |
---|
458 | set args [lrange $args 1 end] |
---|
459 | } |
---|
460 | # return the whole list or just a single value |
---|
461 | if {[llength $args] > 1} { |
---|
462 | error "wrong # args: should be [join [lsort $choices] {, }]" |
---|
463 | } |
---|
464 | if {[llength $args] == 0} { |
---|
465 | set name $_current |
---|
466 | } else { |
---|
467 | set name [lindex $args 0] |
---|
468 | } |
---|
469 | set elem [FindElement $name] |
---|
470 | if { $elem == "" || $_state($elem) == "disabled" } { |
---|
471 | if { $elem != "" } { |
---|
472 | puts stderr "element $elem is disabled" |
---|
473 | } |
---|
474 | return "" |
---|
475 | } |
---|
476 | array set info $_table($elem) |
---|
477 | # scan through and build up the return list |
---|
478 | switch -- $format { |
---|
479 | -name { set value $info(name) } |
---|
480 | -symbol { set value $info(symbol) } |
---|
481 | -weight { set value $info(weight) } |
---|
482 | -number { set value $info(number) } |
---|
483 | -all { |
---|
484 | foreach key { symbol name number weight } { |
---|
485 | lappend value $key $info($key) |
---|
486 | } |
---|
487 | } |
---|
488 | } |
---|
489 | return $value |
---|
490 | } |
---|
491 | |
---|
492 | # ---------------------------------------------------------------------- |
---|
493 | # USAGE: select <name> |
---|
494 | # |
---|
495 | # Used to manipulate the selection in the table. |
---|
496 | # |
---|
497 | # ---------------------------------------------------------------------- |
---|
498 | itcl::body Rappture::PeriodicTable::select { what } { |
---|
499 | set elem [FindElement $what] |
---|
500 | if { $elem == "" } { |
---|
501 | set elem "Hydrogen" |
---|
502 | } |
---|
503 | set _current $elem |
---|
504 | $_dispatcher event -idle !rebuild |
---|
505 | } |
---|
506 | |
---|
507 | # ---------------------------------------------------------------------- |
---|
508 | # USAGE: FindElement <what> |
---|
509 | # |
---|
510 | # Checks to see if the given string <what> is recognized as an element |
---|
511 | # name, symbol, or number. Returns the corresponding element name |
---|
512 | # or "" if the element is not recognized. |
---|
513 | # ---------------------------------------------------------------------- |
---|
514 | itcl::body Rappture::PeriodicTable::FindElement { what } { |
---|
515 | foreach name [array names _table] { |
---|
516 | array unset info |
---|
517 | array set info $_table($name) |
---|
518 | if { $what eq $info(name) || $what eq $info(number) || |
---|
519 | $what eq $info(symbol) } { |
---|
520 | return $info(name) |
---|
521 | } |
---|
522 | } |
---|
523 | return "" |
---|
524 | } |
---|
525 | |
---|
526 | # ---------------------------------------------------------------------- |
---|
527 | # USAGE: ElementIsType <name> <type> <type>... |
---|
528 | # |
---|
529 | # Checks to see if the given element <name> is one of the specified |
---|
530 | # <type> values. Returns true if the element is one of the types |
---|
531 | # (logical or), or false if it is not. |
---|
532 | # ---------------------------------------------------------------------- |
---|
533 | itcl::body Rappture::PeriodicTable::ElementIsType { name args } { |
---|
534 | foreach type $args { |
---|
535 | if {[info exists _types($type)]} { |
---|
536 | if {[lsearch -exact $_types($type) $name] >= 0} { |
---|
537 | return 1 |
---|
538 | } |
---|
539 | } |
---|
540 | } |
---|
541 | return 0 |
---|
542 | } |
---|
543 | |
---|
544 | # ---------------------------------------------------------------------- |
---|
545 | # USAGE: _adjust ?<widget>? |
---|
546 | # |
---|
547 | # This method is invoked each time the dropdown is posted to adjust |
---|
548 | # its size and contents. If the <widget> is specified, it is the |
---|
549 | # controlling widget. |
---|
550 | # ---------------------------------------------------------------------- |
---|
551 | itcl::body Rappture::PeriodicTable::_adjust {{widget ""}} { |
---|
552 | chain $widget |
---|
553 | |
---|
554 | focus $itk_component(table) |
---|
555 | } |
---|
556 | |
---|
557 | # ---------------------------------------------------------------------- |
---|
558 | # USAGE: _react |
---|
559 | # |
---|
560 | # Invoked automatically when the user has selected something from |
---|
561 | # the listbox. Unposts the drop-down and generates an event letting |
---|
562 | # the client know that the selection has changed. |
---|
563 | # ---------------------------------------------------------------------- |
---|
564 | itcl::body Rappture::PeriodicTable::_react {} { |
---|
565 | unpost |
---|
566 | event generate $itk_component(hull) <<PeriodicTableSelect>> |
---|
567 | } |
---|
568 | |
---|
569 | itcl::body Rappture::PeriodicTable::Activate { canvas id x y } { |
---|
570 | set c $itk_component(table) |
---|
571 | array set info $_table($id) |
---|
572 | set type $info(type) |
---|
573 | set fg $_colors($type-activeforeground) |
---|
574 | set bg $_colors($type-activebackground) |
---|
575 | $c itemconfigure $id-rect -outline black -width 1 -fill $bg |
---|
576 | $c itemconfigure $id-number -fill white |
---|
577 | $c itemconfigure $id-symbol -fill white |
---|
578 | ::Rappture::Tooltip::text $c \ |
---|
579 | "$info(name) ($info(symbol))\nNumber: $info(number)\nWeight: $info(weight)" |
---|
580 | ::Rappture::Tooltip::tooltip pending $c @$x,$y |
---|
581 | } |
---|
582 | |
---|
583 | itcl::body Rappture::PeriodicTable::Deactivate { canvas id } { |
---|
584 | set c $itk_component(table) |
---|
585 | array set info $_table($id) |
---|
586 | set type $info(type) |
---|
587 | set fg $_colors($type-foreground) |
---|
588 | set bg $_colors($type-background) |
---|
589 | $c itemconfigure $id-rect -outline $fg -width 1 -fill $bg |
---|
590 | $c itemconfigure $id-number -fill $fg |
---|
591 | $c itemconfigure $id-symbol -fill $fg |
---|
592 | ::Rappture::Tooltip::tooltip cancel |
---|
593 | } |
---|
594 | |
---|
595 | itcl::body Rappture::PeriodicTable::value {{value "" }} { |
---|
596 | if { $value != "" } { |
---|
597 | set _current $value |
---|
598 | } |
---|
599 | } |
---|
600 | |
---|
601 | # ---------------------------------------------------------------------- |
---|
602 | # USAGE: Redraw |
---|
603 | # |
---|
604 | # Invoked automatically when the user has selected something from |
---|
605 | # the canvas. Unposts the drop-down and generates an event letting |
---|
606 | # the client know that the selection has changed. |
---|
607 | # ---------------------------------------------------------------------- |
---|
608 | itcl::body Rappture::PeriodicTable::Redraw {} { |
---|
609 | set sqwidth [winfo pixels . 0.22i] |
---|
610 | set sqheight [winfo pixels . 0.22i] |
---|
611 | set xoffset 4 |
---|
612 | set yoffset 4 |
---|
613 | set last "" |
---|
614 | set c $itk_component(table) |
---|
615 | $c delete all |
---|
616 | foreach name [array names _table] { |
---|
617 | array set info $_table($name) |
---|
618 | set x1 [expr ($info(column)-1)*$sqwidth+$xoffset] |
---|
619 | set y1 [expr ($info(row)-1)*$sqheight+$yoffset] |
---|
620 | set x2 [expr ($info(column)*$sqwidth)-2+$xoffset] |
---|
621 | set y2 [expr ($info(row)*$sqheight)-2+$yoffset] |
---|
622 | set type $info(type) |
---|
623 | if { $_state($name) == "disabled" } { |
---|
624 | set fg $_colors($type-disabledforeground) |
---|
625 | set bg $_colors($type-disabledbackground) |
---|
626 | } else { |
---|
627 | set fg $_colors($type-foreground) |
---|
628 | set bg $_colors($type-background) |
---|
629 | } |
---|
630 | $c create rectangle $x1 $y1 $x2 $y2 -outline $fg -fill $bg \ |
---|
631 | -tags $info(name)-rect |
---|
632 | if { $info(symbol) != "*" } { |
---|
633 | $c create text [expr ($x2+$x1)/2+1] [expr ($y2+$y1)/2+4] \ |
---|
634 | -anchor c -fill $fg \ |
---|
635 | -text [string range $info(symbol) 0 4] \ |
---|
636 | -font "Arial 8 bold" -tags $info(name)-symbol |
---|
637 | $c create text [expr $x2-2] [expr $y1+2] -anchor ne \ |
---|
638 | -text $info(number) -fill $fg \ |
---|
639 | -font "math1 6" -tags $info(name)-number |
---|
640 | } |
---|
641 | $c create rectangle $x1 $y1 $x2 $y2 -outline "" -fill "" \ |
---|
642 | -tags $info(name) |
---|
643 | if { $_state($name) == "normal" } { |
---|
644 | $c bind $info(name) <Enter> \ |
---|
645 | [itcl::code $this Activate %W $info(name) %X %Y] |
---|
646 | $c bind $info(name) <Leave> [itcl::code $this Deactivate %W $info(name)] |
---|
647 | $c bind $info(name) <ButtonRelease-1> [itcl::code $this value $info(name)] |
---|
648 | } |
---|
649 | } |
---|
650 | update |
---|
651 | foreach { x1 y1 x2 y2 } [$c bbox all] break |
---|
652 | set width [expr $x2-$x1+$xoffset*2] |
---|
653 | set height [expr $y2-$y1+$yoffset*2] |
---|
654 | $c configure -height $height -width $width -background white |
---|
655 | } |
---|