/usr/share/tcltk/tklib0.6/menubar/menubar.tcl is in tklib 0.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 | # menubar.tcl --
#
# Package that defines the menubar class. The menubar class
# encapsulates the definition, installation and dynamic behavior
# of a menubar.
#
# Copyright (c) 2009 Tom Krehbiel <tomk@users.sourceforge.net>
#
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# RCS: @(#) $Id: menubar.tcl,v 1.6 2010/01/09 20:41:29 tomk Exp $
package require Tk
package require TclOO
package require menubar::tree
package provide menubar 0.5
# --------------------------------------------------
# DESIGN NOTES
# --------------------------------------------------
# STRUCTURE: mtree
#
# <node>
# M - <cascade>
# S - <separator>
# C - <command>
# X - <checkbutton>
# R - {radiogroup}
# <radiobutton>
# G - {commandgroup} <separator>
# <command>
#
# STRUCTURE: installs
#
# <wtop>
# <node-tag>
# +pathname
# <pathname>
# +callback_ns
# <namespace>
# --------------------------------------------------
oo::class create ::menubar {
# ------------------------------------------------------------
#
# new --
#
# Create an instance of the menubar class.
#
# Arguments:
# args - zero or more option/value pairs
#
# Results:
# An instance of the menubar class is returned.
#
# Side effects:
# none
#
# ------------------------------------------------------------
constructor { args } {
variable mtree
variable next_id
variable installs
variable tearoffpathnames
variable first_install
variable wtop
variable mtop
# The tagVal array holds the current value
# for all checkbutton and radiobutton items
variable tagVal
# This dict holds the widget specific value store
variable notebookVals
# create font used by labelseperator
menu .temp
set font_def [font actual [.temp cget -font]]
set font_def [dict replace ${font_def} -weight bold -slant italic]
font create sfont {*}${font_def}
destroy .temp
set next_id 0
set mtree [menubar::tree new]
${mtree} rename root menubar
${mtree} key.set menubar +type cascade
${mtree} key.set menubar +is_defined 0
# add menubar settings
foreach {opt value} ${args} {
${mtree} key.set menubar ${opt} ${value}
}
set installs [dict create]
set tearoffpathnames [dict create]
set first_install ""
set notebookVals [dict create]
return
}
# ------------------------------------------------------------
#
# loadDebugMethods --
#
# Determine the pathname of the toplevel window for a widget.
#
# Arguments:
# none
#
# Results:
# node
#
# Side effects:
# Debug package is loaded
#
# ------------------------------------------------------------
method loadDebugMethod { } {
package require menubar::debug
}
# ------------------------------------------------------------
#
# getTopLevel --
#
# Determine the pathname of the toplevel window for a widget.
#
# Arguments:
# w - window
#
# Results:
# Returns the path name of the top-of-hierarchy window
# containing window.
#
# Side effects:
# none
#
# ------------------------------------------------------------
method getTopLevel { w } {
return [winfo toplevel ${w}]
}
# ------------------------------------------------------------
#
# ScrubString --
#
# Convert users menubar description into a tcl list, also
# remove blank and comment lines from the description.
#
# Arguments:
# str - character string
#
# Results:
# list
#
# Side effects:
# none
#
# ------------------------------------------------------------
method ScrubString { str } {
foreach line [split ${str} \n] {
set line [string trim ${line}]
if { ${line} eq "" || [string index ${line} 0] eq "#" } { continue }
lappend result ${line}
}
return [join ${result} \n]
}
# ------------------------------------------------------------
#
# define --
#
# This is the user interface to the menubar description parser.
#
# Arguments:
# definition - menubar description
#
# Results:
# none
#
# Side effects:
# Entries are added to the mtree structure.
#
# ------------------------------------------------------------
method define { definition } {
variable mtree
if { [${mtree} key.get menubar +is_defined] == 1 } { return }
my MenuAdd menubar ${definition}
${mtree} key.set menubar +is_defined 1
return
}
# ------------------------------------------------------------
#
# MenuAdd --
#
# Parse one menu description and add it to mtree.
# The new menu is a branch off the parent.
#
# Arguments:
# parent - parent tag name
# desc - menu description
#
# Results:
# none
#
# Side effects:
# Entries are added to the menu tree (mtree) structure.
#
# ------------------------------------------------------------
method MenuAdd { parent desc } {
variable mtree
if { ${parent} ni [${mtree} nodes] } {
error "error: MenuAdd - tag '${parent}' doesn't exist."
}
set desc [my ScrubString ${desc}]
if { ${desc} eq "" } { return 0 }
foreach {name istype more} ${desc} {
set node [my ParseItem ${parent} ${name} ${istype} end ${more}]
}
return
}
# ------------------------------------------------------------
#
# TagCheck --
#
# Check the format of a tag string.
#
# Arguments:
# tag - tag name to be checked
# unique - flag that indecates if the tag should be globally
# unique. Default is true.
#
# Results:
# none
#
# Side effects:
# error is thrown if tag check fails
#
# ------------------------------------------------------------
method TagCheck { tag {unique 1} } {
variable mtree
set tags [${mtree} nodes]
if { ${unique} == 1 && ${tag} in ${tags} } {
error "bug: tag '${tag}' already used."
}
if { [regexp {^[^+\"\[\]\$\\]{1,}$} ${tag}] == 0 } {
error "bug: tag '${tag}' not a simple string."
}
return
}
# ------------------------------------------------------------
#
# CreateWidgetPath --
#
# Construct a unique child pathname for a parent pathname.
#
# Arguments:
# pathname - parent tag name
#
# Results:
# child widget pathname
#
# Side effects:
# none
#
# ------------------------------------------------------------
method CreateWidgetPath { pathname } {
variable next_id
incr next_id
return [join [list ${pathname} "m${next_id}"] "."]
}
# ------------------------------------------------------------
#
# ParseItem --
#
# Parse one line from the user's menubar description and add the
# item to mtree.
#
# Arguments:
# parent - parent tag name
# name - tag name of new item
# istype - type identifier (oneof: M,S,C,X,R,G)
# index - position in parent's list
# more - rest of item definition; varies based on istype value
#
# Results:
# fully qualified tag name of new item
#
# Side effects:
# an item is added to mtree
#
# ------------------------------------------------------------
method ParseItem { parent name istype index more } {
variable mtree
variable tagVal
variable next_id
switch -regexp -- ${istype} {
"M.*" {
# create a new sub-menu
set opts [dict create +type cascade]
set def ${more}
set tearoff 0
set tearoff [expr { [string index ${istype} end] eq "+" ? 1 : 0 }]
dict set opts +tearoff ${tearoff}
dict set opts +tearoffpathname {}
dict set opts +hide 0
set istype [string trimright ${istype} "+"]
lassign [split ${istype} ":"] - tag
if { ${tag} eq "" } {
error "bug: menu (${name} ${istype}) has no tag name."
}
my TagCheck ${tag}
${mtree} insert ${parent} ${index} ${tag}
dict set opts -label ${name}
dict set opts -underline 0
dict set opts -hidemargin 0
dict for {opt val} ${opts} {
${mtree} key.set ${tag} ${opt} ${val}
}
my MenuAdd ${tag} ${def}
}
"S" {
# add a separator
set tag ${more}
set opts [dict create +type separator]
my TagCheck ${tag}
${mtree} insert ${parent} ${index} ${tag}
dict set opts -label "${name}"
dict for {opt val} ${opts} {
${mtree} key.set ${tag} ${opt} ${val}
}
}
"C" {
# add a command
set tag ${more}
set opts [dict create +type command]
my TagCheck ${tag}
${mtree} insert ${parent} ${index} ${tag}
${mtree} key.set ${tag} +scope global
dict set opts +command {}
dict set opts +bind {}
dict set opts -label "${name}"
dict set opts -underline 0
dict for {opt val} ${opts} {
${mtree} key.set ${tag} ${opt} ${val}
}
}
{X[@=]?} {
# add a checkbutton
set tag ${more}
set opts [dict create +type checkbutton]
set value [expr {([string index ${tag} end] eq "+") ? 1 : 0}]
set tag [string trimright ${tag} +]
my TagCheck ${tag}
${mtree} insert ${parent} ${index} ${tag}
${mtree} key.set ${tag} +initval ${value}
dict set opts +command {}
dict set opts +bind {}
# determine the scope of the button variable
if { [string index ${istype} end] eq "@" } {
# toplevel single-valued local
${mtree} key.set ${tag} +scope local
set tagVal(${tag}%%) ${value}
dict set opts +variable [self namespace]::tagVal(${tag}%%)
} elseif { [string index ${istype} end] eq "=" } {
# toplevel multi-valued local
${mtree} key.set ${tag} +scope multival
set tagVal(${tag}%%) ${value}
dict set opts +variable [self namespace]::tagVal(${tag}%%)
} else {
# application global
${mtree} key.set ${tag} +scope global
set tagVal(${tag}) ${value}
dict set opts +variable [self namespace]::tagVal(${tag})
}
dict set opts -label "${name}"
dict set opts -underline 0
dict set opts -onvalue 1
dict set opts -offvalue 0
dict for {opt val} ${opts} {
${mtree} key.set ${tag} ${opt} ${val}
}
}
{R[@=]?} {
# add a radiobutton
set tag ${more}
set value [expr {([string index ${tag} end] eq "+") ? 1 : 0}]
set tag [string trimright ${tag} +]
my TagCheck ${tag} 0
if { ${tag} ni [${mtree} nodes] } {
${mtree} insert ${parent} ${index} ${tag}
${mtree} key.set ${tag} +type radiogroup
${mtree} key.set ${tag} +command {}
${mtree} key.set ${tag} +value {}
${mtree} key.set ${tag} +initval {}
# determine the scope of the button variable
if { [string index ${istype} end] eq "@" } {
# toplevel single-valued local
${mtree} key.set ${tag} +scope local
set tagVal(${tag}%%) ${value}
${mtree} key.set ${tag} +variable [self namespace]::tagVal(${tag}%%)
} elseif { [string index ${istype} end] eq "=" } {
# toplevel multi-valued local
${mtree} key.set ${tag} +scope multival
set tagVal(${tag}%%) ${value}
${mtree} key.set ${tag} +variable [self namespace]::tagVal(${tag}%%)
} else {
# application global
${mtree} key.set ${tag} +scope global
set tagVal(${tag}) ${value}
${mtree} key.set ${tag} +variable [self namespace]::tagVal(${tag})
}
}
set opts [dict create +type radiobutton]
# tag name for radiobutton node
incr next_id
set _tag "${tag}_${next_id}"
${mtree} insert ${tag} ${index} ${_tag}
if { ${value} == 1 } {
${mtree} key.set ${tag} +initval ${name}
}
dict set opts -label "${name}"
dict set opts -underline 0
dict for {opt val} ${opts} {
${mtree} key.set ${_tag} ${opt} ${val}
}
}
"G" {
# add a command group
set tag ${more}
set opts [dict create +type commandgroup]
my TagCheck ${tag}
${mtree} insert ${parent} ${index} ${tag}
dict for {opt val} ${opts} {
${mtree} key.set ${tag} ${opt} ${val}
}
}
default {
error "bug: '${name}' - unknown item type (${istype})"
}}
return ${tag}
}
# ------------------------------------------------------------
#
# tag.add --
#
# Add a user defined tag
#
# Arguments:
# tag - tag name
# value - the tag's value
#
# Results:
# none
#
# Side effects:
# a user defined tag/value pair is added to the installs
# structure for the current toplevel window
#
# ------------------------------------------------------------
method tag.add { tag value } {
variable wtop
variable installs
variable first_install
# Only set during an install
if { ${first_install} ne "" } {
dict set installs ${wtop} +tags ${tag} ${value}
} else {
error "error: tag - command used outside install"
}
return
}
# ------------------------------------------------------------
#
# tag.cget --
#
# Return an option value given a window and tag
#
# Arguments:
# wtop - toplevel pathname
# tag - tag name
# opt - option name
#
# Results:
# value of option
#
# Side effects:
# none
#
# ------------------------------------------------------------
method tag.cget { wtop tag {opt ""} } {
variable mtree
variable installs
if { [dict size ${installs}] == 0 } {
error "error: tag.cget - '${mtree}' not installed."
}
# if opt isn't included then return the value of an
# user defined (install) tag (not a menu tag)
if { ${opt} eq "" } {
if { [dict exists ${installs} ${wtop} +tags ${tag}] } {
set value [dict get ${installs} ${wtop} +tags ${tag}]
} else {
error "error: tag(${tag}) - not defined for toplevel(${wtop})"
}
return ${value}
}
if { [${mtree} exists ${tag}] ne "" } {
error "error: tag.cget - tag '${tag}' not found"
}
set parent_node [${mtree} parent ${tag}]
switch -exact -- [${mtree} key.get ${tag} +type] {
"cascade" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${tag} -label]
set value [${parent_path} entrycget ${name} ${opt}]
return ${value}
}
"separator" {
return "separator"
}
"command" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${tag} -label]
set value [${parent_path} entrycget ${name} ${opt}]
return ${value}
}
"checkbutton" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${tag} -label]
set value [${parent_path} entrycget ${name} ${opt}]
return ${value}
}
"radiogroup" {
# only state information is availible for radio buttons
# because the buttons share a tag name
if { ${opt} eq "-state" } {
set value [${mtree} key.get ${tag} +value]
if { ${value} eq "" } {
set value [${mtree} key.get ${tag} +initval]
}
return ${value}
}
}
"radiobutton" {
}
"commandgroup" {
}
"groupcommand" {
}
default {
}}
return
}
# ------------------------------------------------------------
#
# menu.namespace --
#
# Set the namespace for a sub-tree of the menubar starting at
# the entry with tag name.
#
# Arguments:
# tag - tag name
# ns - new namespace
#
# Results:
# none
#
# Side effects:
# mtree is modified or an error is thrown if the command is
# used outside the context of the install method
#
# ------------------------------------------------------------
method menu.namespace { tag ns } {
variable mtree
variable wtop
variable installs
variable first_install
# Only set during an install
if { ${first_install} ne "" } {
if { [${mtree} key.get ${tag} +type] ni {commandgroup cascade} } {
puts stderr "menu.namespace: tag (${tag}) not a commandgroup or cascade"
return
}
dict set installs ${wtop} ${tag} +callback_ns ${ns}
} else {
error "error: menu.namespace - command used outside install"
}
return
}
# ------------------------------------------------------------
#
# install --
#
# Install an initial Tk menu in a toplevel window and install the
# rest of the menubar using the MenuInstall method. After the menubar
# has been installed, configure option values for all the menubar entries.
#
# Arguments:
# win - pathname of window where menubar will be installed
# config - user supplied code to configure the items on
# the installed menubar
#
# Results:
# none
#
# Side effects:
# A menubar is installed and displayed.
#
# ------------------------------------------------------------
method install { win config } {
variable mtree
variable wtop
variable mtop
variable installs
variable first_install ;# oneof: "yes", "no" or ""
variable ns
if { [${mtree} key.get menubar +is_defined] != 1 } {
error "error: install - '${mtree}' not defined."
}
# determine the actual path of the top level window
# (the path can be "hidden" by megawidget code)
set wtop [my getTopLevel ${win}]
if { ${wtop} eq "." } {
set mtop ".m0"
} else {
set mtop [join [list ${wtop} "m0"] "."]
}
if { [dict keys ${installs} ${wtop}] eq "" } {
# puts a Destroy binding on the new toplevel
bind ${wtop} <Destroy> [namespace code [list my WindowCleanup %W ${wtop}]]
# check to see if this is the first install
set first_install "no"
if { [llength [dict keys ${installs}]] == 0 } {
set first_install "yes"
}
# create a menu and install it as a menubar
menu ${mtop}
${wtop} configure -menu ${mtop}
dict set installs ${wtop} menubar +pathname ${mtop}
dict set installs ${wtop} menubar +callback_ns [uplevel {namespace current}]
# configure the menubar
${mtop} configure {*}[${mtree} key.getall menubar -*]
# create sub-menus
foreach node [${mtree} children menubar] {
my MenuInstall ${wtop} ${mtop} ${node}
}
# configure the new menubar
uplevel ${config}
set first_install ""
}
return
}
# ------------------------------------------------------------
#
# WindowCleanup --
#
# Cleanup internal data structures associated with a toplevel window
# when it is destroyed. Note that is will get called for each
#
# Arguments:
# w - the window being destroyed
# wtop - pathname of toplevel window
#
# Results:
# none
#
# Side effects:
# Data is removed from internal data structures
#
# ------------------------------------------------------------
method WindowCleanup { w wtop } {
variable mtree
variable installs
variable tearoffpathnames
variable notebookVals
if { ${w} eq ${wtop} && ${wtop} in [dict keys ${installs}] } {
dict unset installs ${wtop}
dict unset tearoffpathnames ${wtop}
set notebookVals [dict remove ${notebookVals} ${wtop}]
#puts "tearoffpathnames: ${tearoffpathnames}"
}
return
}
# ------------------------------------------------------------
#
# MenuInstall --
#
# Create Tk menu widget or menu widget entry from a definition found in
# the mtree structure.
#
# Arguments:
# win - pathname of window where tk menu will be added
# parent_path - pathname of parent menu
# node - mtree node containing the item to be created
#
# Results:
# none
#
# Side effects:
# A Tk menu or menu entry is created.
#
# ------------------------------------------------------------
method MenuInstall { wtop parent_path node } {
variable mtree
variable installs
variable first_install
variable next_id
variable notebookVals
set ns [dict get ${installs} ${wtop} menubar +callback_ns]
switch -glob -- [${mtree} key.get ${node} +type] {
"cascade" {
# don't render hidden menus
if { [${mtree} key.exists ${node} +hide] && [${mtree} key.get ${node} +hide] == 1 } {
return
}
set name [${mtree} key.get ${node} -label]
if { [string tolower ${name}] eq "help" } {
# If menu is a help menu then the pathname must end in "help"
# so the menu will be right justified on the menubar
set child_path [join [list ${parent_path} "help"] "."]
} else {
set child_path [my CreateWidgetPath ${parent_path}]
}
dict set installs ${wtop} ${node} +pathname ${child_path}
if { ${first_install} ne "" } {
dict set installs ${wtop} ${node} +callback_ns ${ns}
}
menu ${child_path} \
-tearoff [${mtree} key.get ${node} +tearoff] \
-tearoffcommand [namespace code [list my AppendTearoffPathname ${node}]]
set index [expr [${mtree} index ${node}]+1]
${parent_path} insert ${index} cascade -label ${name} -menu ${child_path}
my InstallSubTree ${wtop} ${child_path} ${node}
my RenderTag ${wtop} ${node}
}
"separator" {
set name [${mtree} key.get ${node} -label]
if { ${name} eq "--" } {
${parent_path} add separator
} else {
${parent_path} add command -label "-- ${name} --" \
-state active \
-command {} \
-foreground black \
-background lightgray \
-activebackground lightgray \
-activeforeground black \
-font sfont
}
#my RenderTag ${wtop} ${node}
}
"groupcommand" {
set name [${mtree} key.get ${node} -label]
${parent_path} add command -label ${name}
my RenderTag ${wtop} ${node}
}
"command" {
set name [${mtree} key.get ${node} -label]
${parent_path} add command -label ${name}
my RenderTag ${wtop} ${node}
}
"checkbutton" {
set name [${mtree} key.get ${node} -label]
${parent_path} add checkbutton -label ${name}
# create the checkbutton variable
incr next_id
set varname [string map [list "%%" ${next_id}] [${mtree} key.get ${node} +variable]]
${parent_path} entryconfigure ${name} -variable ${varname}
set scope [${mtree} key.get ${node} +scope]
if { ${scope} eq "global" } {
if { ${first_install} eq "yes" } {
set ${varname} [${mtree} key.get ${node} +initval]
}
} elseif { ${scope} eq "local" } {
set ${varname} [${mtree} key.get ${node} +initval]
} elseif { ${scope} eq "multival" } {
set ${varname} [${mtree} key.get ${node} +initval]
dict set notebookVals ${wtop} +var ${node} ${varname}
}
dict set installs ${wtop} +btnvars ${node} ${varname}
# process the rest of the option settings
my RenderTag ${wtop} ${node} ${varname}
}
"radiogroup" {
dict set installs ${wtop} ${node} +pathname ${parent_path}
# create a variable for radiogroup
incr next_id
set varname [string map [list "%%" ${next_id}] [${mtree} key.get ${node} +variable]]
set scope [${mtree} key.get ${node} +scope]
if { ${scope} eq "global" } {
if { ${first_install} eq "yes" } {
set ${varname} [${mtree} key.get ${node} +initval]
}
} elseif { ${scope} eq "local" } {
set ${varname} [${mtree} key.get ${node} +initval]
} elseif { ${scope} eq "multival" } {
set ${varname} [${mtree} key.get ${node} +initval]
dict set notebookVals ${wtop} +var ${node} ${varname}
}
dict set installs ${wtop} +btnvars ${node} ${varname}
# process the rest of the option settings
my InstallSubTree ${wtop} ${parent_path} ${node}
}
"radiobutton" {
set varname [${mtree} key.get [${mtree} parent ${node}] +variable]
set name [${mtree} key.get ${node} -label]
${parent_path} add radiobutton -label ${name} -variable ${varname}
my RenderTag ${wtop} ${node}
}
"commandgroup" {
${parent_path} add separator
dict set installs ${wtop} ${node} +pathname ${parent_path}
my RenderTag ${wtop} ${node}
my InstallSubTree ${wtop} ${parent_path} ${node}
}
default {
}}
return
}
# ------------------------------------------------------------
#
# AppendTearoffPathname --
#
# This proceedure is called after a tearoff menu has been created.
# Tearoff menus are toplevel windows but we need to keep track of
# the association between the toplevel window containing the menubar
# and the tearoff menu.
#
# Arguments:
# node - mtree node containing the item to be created
# from_pathname - menubar path where tearoff occured
# tearoff_pathname - pathname of torn off menu
#
# Results:
# none
#
# Side effects:
# Information about the tearoff menu is saved. A WM_DELETE_WINDOW
# protocol handler is added to the tearoff menu. The tearoff menu
# geometry is ajusted and then resizing is turned off.
#
# ------------------------------------------------------------
method AppendTearoffPathname { node from_pathname tearoff_pathname } {
my variable mtree
my variable tearoffpathnames
# get the toplevel that contains the menubar
set tearoff_wtop [winfo toplevel ${tearoff_pathname}]
my DeleteTearoff ${from_pathname} ${node}
dict set tearoffpathnames ${from_pathname} ${node} ${tearoff_wtop}
switch -exact -- [tk windowingsystem] {
win32 {
wm attributes ${tearoff_pathname} -toolwindow 1
}
x11 {
wm transient ${tearoff_pathname}
}
osx {
}}
wm protocol ${tearoff_pathname} WM_DELETE_WINDOW [namespace code [list my DeleteTearoff ${from_pathname} ${node}]]
lassign [winfo pointerxy .] xx yy
regexp {([0-9]+)[Xx]([0-9]+)([+-][0-9]+)([+-][0-9]+)} [wm geometry ${tearoff_wtop}] - width height x y
wm geometry ${tearoff_wtop} +${xx}+${yy}
if { ${width} < 120 } { set width 120 }
wm minsize ${tearoff_wtop} ${width} 40
update
wm resizable ${tearoff_wtop} 0 0
return
}
# ------------------------------------------------------------
#
# DeleteTearoff --
#
# This proceedure is called when a tearoff menu is destroyed.
#
# Arguments:
# from_path - menubar path where tearoff occured
# node - mtree node that defines the menu
#
# Results:
# none
#
# Side effects:
# The tornoff menu is destroyed and its pathname is removed from
# the list of menus that have been tornoff.
#
# ------------------------------------------------------------
method DeleteTearoff { from_path node } {
my variable mtree
my variable tearoffpathnames
if { [dict exists ${tearoffpathnames} ${from_path} ${node}] } {
destroy [dict get ${tearoffpathnames} ${from_path} ${node}]
dict unset tearoffpathnames ${from_path} ${node}
}
return
}
# ------------------------------------------------------------
#
# InstallSubTree --
#
# Install all the child nodes for a given parent node.
#
# Arguments:
# wtop - toplevel window for install
# parent_path - pathname of parent node
# parent_node - mtree node name of parent
#
# Results:
# none
#
# Side effects:
# The MenuInstall method is executed on all the child nodes
# of parent_node.
#
# ------------------------------------------------------------
method InstallSubTree { wtop parent_path parent_node } {
variable mtree
foreach node [${mtree} children ${parent_node}] {
my MenuInstall ${wtop} ${parent_path} ${node}
}
return
}
# ------------------------------------------------------------
#
# menu.configure --
#
# Add any number of option/value pairs for multiple item in the mtree
# stucture then update the visible rendering of the menubar.
#
# Arguments:
# args - a list of option/body pairs. Option is any of the legal
# option names for a menubar. The body part of the pair is
# a line oriented text definition of tag/value pairs where
# the item associated with the tag will have the option set
# to the specified value.
#
# Results:
# none
#
# Side effects:
# The option values for items in the menubar are modified.
#
# ------------------------------------------------------------
method menu.configure { args } {
if { [llength ${args}] < 2 } {
error "error: menu.configure - to few arguments."
}
foreach {opt settings} ${args} {
set lines [split ${settings} \n]
foreach line ${lines} {
set line [string trim ${line}]
if { ${line} eq "" || [string index ${line} 0] eq "#" } { continue }
lassign ${line} tag value
my tag.configure group ${tag} ${opt} ${value}
}
}
return
}
# ------------------------------------------------------------
#
# tag.configure --
#
# Set any number of option/value pairs for one item in the mtree structure
# then update the visible rendering of the item.
#
# Arguments:
# wtop - toplevel window containing the menubar item
# node - mtree node to be configured
# args - a list of option/value pairs
#
# Results:
# none
#
# Side effects:
# The option values of a menubar item are modified.
#
# ------------------------------------------------------------
method tag.configure { wtop node args } {
variable mtree
variable installs
variable first_install
if { [dict size ${installs}] == 0 } {
error "error: tag.configure - '${mtree}' not installed."
}
if { [${mtree} exists ${node}] ne "" } {
error "error: tag.configure - tag '${node}' not found"
}
# put option info in tree structure
foreach {opt value} ${args} {
switch -exact -- ${opt} {
"-command" {
set value [string trim ${value}]
switch -exact -- [${mtree} key.get ${node} +type] {
"radiogroup" {
${mtree} key.set ${node} +command ${value}
}
"radiobutton" {
# command is stored in
}
default {
${mtree} key.set ${node} +command ${value}
}}
}
-state {
if { ${first_install} ne "no" } {
${mtree} key.set ${node} ${opt} ${value}
}
}
-bind {
lassign ${value} uline accel sequence
if { ${uline} eq "" || [string is integer ${uline}] } {
${mtree} key.set ${node} -underline [expr {(${uline} eq "") ? -1 : ${uline}}]
} else {
error "tag.configure: underline value for tag (${node}) not positive integer."
}
${mtree} key.set ${node} -accelerator ${accel}
${mtree} key.set ${node} +bind ${sequence}
}
default {
# don't process these options
if { ${opt} in {-accelerator -menu -offvalue -onvalue -value -variable -underline} } { continue }
${mtree} key.set ${node} ${opt} ${value}
}}
}
# update the node in all top level windows
foreach wtop [dict keys ${installs}] {
my RenderTag ${wtop} ${node}
}
return
}
# ------------------------------------------------------------
#
# getButtonVars --
#
# Set any number of option/value pairs for one item in the mtree structure
# then update the visible rendering of the item.
#
# Arguments:
# wtop - toplevel window containing the menubar item
#
# Results:
# dict of tag name/button variables
#
# Side effects:
# none
#
# ------------------------------------------------------------
method getButtonVars { wtop } {
variable installs
return [dict get ${installs} ${wtop} +btnvars]
}
# ------------------------------------------------------------
#
# IsHidden --
#
# Determine if a node is visible.
#
# Arguments:
# node - mtree node to be checked
#
# Results:
# returns 1 if the node is hiden else 0
#
# Side effects:
# none
#
# ------------------------------------------------------------
method IsHidden { node } {
variable mtree
if { [${mtree} key.exists ${node} +hide] && [${mtree} key.get ${node} +hide] == 1 } {
return 1
}
foreach ancestor [${mtree} ancestors ${node}] {
if { [${mtree} key.exists ${ancestor} +hide] && [${mtree} key.get ${ancestor} +hide] == 1 } {
return 1
}
}
return 0
}
# ------------------------------------------------------------
#
# RenderTag --
#
# Perform Tk configure commands on a menubar item using the option
# settings found in the mtree structure.
#
# Arguments:
# wtop - toplevel window containing the menubar item
# node - mtree node for item
# varname - (optional) shared variable name for a radiobutton group
#
# Results:
# none
#
# Side effects:
# The visible rendering of a menubar is updated.
#
# ------------------------------------------------------------
method RenderTag { wtop node {varname {}} } {
variable mtree
variable installs
variable next_id
# don't configure hidden items
if { [my IsHidden ${node}] == 1 } {
return
}
set parent_node [${mtree} parent ${node}]
switch -exact -- [${mtree} key.get ${node} +type] {
"cascade" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${node} -label]
${parent_path} entryconfigure ${name} {*}[${mtree} key.getall ${node} -*]
}
"separator" {
}
"command" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${node} -label]
if { [${mtree} key.get ${node} +command] eq "" } {
${parent_path} entryconfigure ${name} -command {}
} else {
${parent_path} entryconfigure ${name} -command [list [self object] commandCallback ${wtop} ${node}]
}
${parent_path} entryconfigure ${name} {*}[${mtree} key.getall ${node} -*]
set sequence [${mtree} key.get ${node} +bind]
if { ${sequence} eq "" } {
bind ${wtop} <${sequence}> {}
} else {
bind ${wtop} <${sequence}> [list ${parent_path} invoke ${name}]
}
}
"checkbutton" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${node} -label]
if { [${mtree} key.get ${node} +command] eq "" } {
${parent_path} entryconfigure ${name} -command {}
} else {
${parent_path} entryconfigure ${name} -command [list [self object] commandCallback ${wtop} ${node}]
}
${parent_path} entryconfigure ${name} {*}[${mtree} key.getall ${node} -*]
set sequence [${mtree} key.get ${node} +bind]
if { ${sequence} eq "" } {
bind ${wtop} <${sequence}> {}
} else {
bind ${wtop} <${sequence}> [list ${parent_path} invoke ${name}]
}
}
"radiogroup" {
set varname [dict get ${installs} ${wtop} +btnvars ${node}]
foreach child [${mtree} children ${node}] {
my RenderTag ${wtop} ${child} ${varname}
}
}
"radiobutton" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${node} -label]
if { [${mtree} key.get ${parent_node} +command] eq "" } {
${parent_path} entryconfigure ${name} -command {}
} else {
${parent_path} entryconfigure ${name} -command [list [self object] commandCallback ${wtop} ${node}]
}
${parent_path} entryconfigure ${name} -variable ${varname}
${parent_path} entryconfigure ${name} {*}[${mtree} key.getall ${node} -*]
}
"commandgroup" {
}
"groupcommand" {
set parent_path [dict get ${installs} ${wtop} ${parent_node} +pathname]
set name [${mtree} key.get ${node} -label]
${parent_path} entryconfigure ${name} {*}[${mtree} key.getall ${node} -*]
if { [${mtree} key.get ${node} +command] eq "" } {
${parent_path} entryconfigure ${name} -command {}
} else {
${parent_path} entryconfigure ${name} -command [list [self object] commandCallback ${wtop} ${node}]
}
set sequence [${mtree} key.get ${node} +bind]
if { ${sequence} eq "" } {
bind ${wtop} <${sequence}> {}
} else {
bind ${wtop} <${sequence}> [list ${parent_path} invoke ${name}]
}
}
default {
}}
return
}
# ------------------------------------------------------------
#
# commandCallback --
#
# Execute a command callback
#
# Arguments:
# wtop - toplevel window containing the menubar item
# that triggered the callback
# node - mtree node for item
#
# Results:
# none
#
# Side effects:
# The callback code associated with node is executed in
# the context of the wtop window.
#
# ------------------------------------------------------------
method commandCallback { wtop node } {
variable mtree
variable installs
# set namespace for callbacks
set parent [${mtree} parent ${node}]
set parent_path [dict get ${installs} ${wtop} ${parent} +pathname]
set name [${mtree} key.get ${node} -label]
# don't execute callback if item is disabled
set state [${parent_path} entrycget ${name} -state]
if { ${state} eq "normal" } {
switch -glob -- [${mtree} key.get ${node} +type] {
"command" {
set ns [dict get ${installs} ${wtop} ${parent} +callback_ns]
set cmd [${mtree} key.get ${node} +command]
if { [string equal -length 2 "::" ${cmd}] } {
eval ${cmd} ${wtop}
} else {
namespace eval ${ns} ${cmd} ${wtop}
}
}
"groupcommand" {
set ns [dict get ${installs} ${wtop} ${parent} +callback_ns]
set cmd [${mtree} key.get ${node} +command]
if { [string equal -length 2 "::" ${cmd}] } {
#puts "eval $cmd"
eval ${cmd} ${wtop}
} else {
#puts "namespace eval ${ns} ${cmd}"
namespace eval ${ns} ${cmd} ${wtop}
}
}
"checkbutton" {
set ns [dict get ${installs} ${wtop} ${parent} +callback_ns]
set cmd [${mtree} key.get ${node} +command]
set value [set [${parent_path} entrycget ${name} -variable]]
if { [string equal -length 2 "::" ${cmd}] } {
eval [list {*}${cmd} ${wtop} ${node} ${value}]
} else {
namespace eval ${ns} [list {*}${cmd} ${wtop} ${node} ${value}]
}
}
"radiobutton" {
set ns [dict get ${installs} ${wtop} [${mtree} parent ${parent}] +callback_ns]
set parent_node [${mtree} parent ${node}]
set cmd [${mtree} key.get ${parent_node} +command]
set cur_value [${mtree} key.get ${parent_node} +value]
set value [set [${parent_path} entrycget ${name} -variable]]
${mtree} key.set ${parent_node} +value ${value}
if { [string equal -length 2 "::" ${cmd}] } {
eval [list {*}${cmd} ${wtop} ${parent_node} ${value}]
} else {
namespace eval ${ns} [list {*}${cmd} ${wtop} ${parent_node} ${value}]
}
}
default {
}}
}
return
}
# ------------------------------------------------------------
#
# menu.show --
#
# Render (i.e. show) a menubar item that is hidden.
#
# Arguments:
# node - mtree node for item
#
# Results:
# none
#
# Side effects:
# The item is added to the menubar of all installed
# toplevel windows.
#
# ------------------------------------------------------------
method menu.show { node } {
variable mtree
variable installs
variable first_install
if { ${node} ni [${mtree} nodes] } {
error "error: menu.show - tag '${node}' doesn't exist."
}
if { [${mtree} key.get ${node} +type] ne "cascade" } {
error "error: menu.show - tag '${node}' not a menu."
}
if { ${first_install} eq "no" } { return }
if { [${mtree} key.get ${node} +hide] == 1 } {
${mtree} key.set ${node} +hide 0
foreach wtop [dict keys ${installs}] {
set parent_path [dict get ${installs} ${wtop} [${mtree} parent ${node}] +pathname]
my MenuInstall ${wtop} ${parent_path} ${node}
}
}
return
}
# ------------------------------------------------------------
#
# menu.hide --
#
# Hide a menubar item that is visible.
#
# Arguments:
# node - mtree node for item
#
# Results:
# none
#
# Side effects:
# The item is removed from the menubar of all installed
# toplevel windows.
#
# ------------------------------------------------------------
method menu.hide { node } {
variable mtree
variable installs
variable first_install
if { ${node} ni [${mtree} nodes] } {
error "error: menu.hide - tag '${node}' doesn't exist."
}
if { [${mtree} key.get ${node} +type] ne "cascade" } {
error "error: menu.hide - tag '${node}' not a menu."
}
if { ${first_install} eq "no" } { return }
if { [${mtree} key.get ${node} +hide] == 0 } {
${mtree} key.set ${node} +hide 1
foreach wtop [dict keys ${installs}] {
set parent_path [dict get ${installs} ${wtop} [${mtree} parent ${node}]]
my DeleteMenu ${wtop} ${node}
}
}
return
}
# ------------------------------------------------------------
#
# DeleteMenu --
#
# Delete a Tk menu.
#
# Arguments:
# wtop - toplevel window
# node - mtree node for item
#
# Results:
# none
#
# Side effects:
# The Tk menu defined by node is deleted from wtop.
#
# ------------------------------------------------------------
method DeleteMenu { wtop node } {
variable mtree
variable installs
set type [${mtree} key.get ${node} +type]
switch -exact -- ${type} {
"cascade" {
# delete submenu entries
set pathname [dict get ${installs} ${wtop} ${node} +pathname]
my DeleteTearoff ${wtop} ${node}
foreach child [${mtree} children ${node}] {
my DeleteMenu ${wtop} ${child}
}
# delete the menu content
${pathname} delete 0 end
# delete menu
set parent_path [dict get ${installs} ${wtop} [${mtree} parent ${node}] +pathname]
set name [${mtree} key.get ${node} -label]
${parent_path} delete ${name}
}
default {
}}
return
}
# ===== GROUP COMMANDS ====================================
# ------------------------------------------------------------
#
# group.add --
#
# Add a command definition to a group and
# append the command to the end of the group menu.
#
# Arguments:
# parent - parent node in mtree
# args - list of items that define the command
# (i.e. name cmd accel sequence state)
#
# Results:
# Returns a 0 on success or 1 on failure.
#
# Side effects:
# A command definition is added to the commandgroup and
# the new command is displayed (i.e. rendered) on all
# installed toplevel windows.
#
# ------------------------------------------------------------
method group.add { parent args } {
variable mtree
variable installs
if { [${mtree} key.get ${parent} +type] ne "commandgroup" } {
#puts stderr "group.add: tag (${parent}) not a command group"
return 1
}
lassign ${args} name cmd accel sequence state
# don't add item if name already exists
if { ${name} in [${mtree} children ${parent}] } {
#puts stderr "warning: command '${name}' already used in command group '${parent}'"
return 1
}
# add command to tree
my TagCheck ${name}
${mtree} insert ${parent} end ${name}
${mtree} key.set ${name} +type groupcommand
set opts [dict create]
dict set opts -label ${name}
dict set opts -underline 0
if { ${state} eq "" || ${state} ni {normal disabled active} } {
dict set opts -state normal
} else {
dict set opts -state ${state}
}
if { ${cmd} eq "" } {
dict set opts -state disabled
}
dict set opts +command ${cmd}
if { ${accel} ne "" } {
dict set opts -accelerator ${accel}
}
if { ${sequence} ne "" } {
dict set opts +bind ${sequence}
}
dict for {opt val} ${opts} {
${mtree} key.set ${name} ${opt} ${val}
}
# update the node in all top level windows
foreach wtop [dict keys ${installs}] {
if { [my IsHidden ${parent}] != 1 } {
set grandparent_node [${mtree} parent ${parent}]
set grandparent_path [dict get ${installs} ${wtop} ${grandparent_node} +pathname]
if { [catch {${grandparent_path} index ${name}} msg] } {
${grandparent_path} add command -label ${name}
}
}
my RenderTag ${wtop} ${name}
}
return 0
}
# ------------------------------------------------------------
#
# group.entries --
#
# Return a list of all entries in a group.
#
# Arguments:
# parent - the mtree commandgroup node of interest
#
# Results:
# Returns a list of mtree node names or 1 if parent isn't
# a commandgroup node.
#
# Side effects:
# none
#
# ------------------------------------------------------------
method group.entries { parent } {
variable mtree
variable installs
if { [${mtree} key.get ${parent} +type] ne "commandgroup" } {
#puts stderr "group.add: tag (${parent}) not a command group"
return 1
}
return [${mtree} children ${parent}]
}
# ------------------------------------------------------------
#
# group.delete --
#
# Delete a command from a commandgroup.
#
# Arguments:
# parent - the mtree commandgroup node of interest
# name - name of item to be removed from the commandgroup
#
# Results:
# Returns 0 on success or 1 on failure.
#
# Side effects:
# none
#
# ------------------------------------------------------------
method group.delete { parent name } {
variable mtree
variable installs
if { [${mtree} key.get ${parent} +type] ne "commandgroup" } {
#puts stderr "group.add: tag (${parent}) not a command group"
return 1
}
# don't delete item if name doesn't exists
if { ${name} ni [${mtree} children ${parent}] } {
#puts stderr "warning: command '${name}' not found in command group '${parent}'"
return 1
}
# update the node in all top level windows
foreach wtop [dict keys ${installs}] {
# delete menu item
set grandparent_node [${mtree} parent ${parent}]
if { [my IsHidden ${grandparent_node}] != 1 } {
set grandparent_path [dict get ${installs} ${wtop} ${grandparent_node} +pathname]
if { [catch {${grandparent_path} index ${name}} idx] } {
#puts stderr "warning: command '${name}' not found in command group '${parent}'"
return 1
}
${grandparent_path} delete ${idx}
}
# delete binding, if one exists
if { [${mtree} key.exists ${name} +bind] } {
set sequence [${mtree} key.get ${name} +bind]
bind ${wtop} <${sequence}> {}
}
}
# delete the node from the menu tree
${mtree} delete ${name}
return 0
}
# ------------------------------------------------------------
#
# group.move --
#
# Move a group command up/down one location within a group menu.
#
# Arguments:
# direction - oneof: up, down
# parent - the mtree commandgroup node of interest
# name - name of item to be moved
#
# Results:
# Returns 0 on success or 1 on failure.
#
# Side effects:
# A menu items is move up or down on all installed menubar.
#
# ------------------------------------------------------------
method group.move { direction parent name } {
variable mtree
variable installs
if { [${mtree} key.get ${parent} +type] ne "commandgroup" } {
#puts stderr "group.add: tag (${parent}) not a command group"
return 1
}
# don't delete item if name doesn't exists
if { ${name} ni [${mtree} children ${parent}] } {
#puts stderr "warning: command '${name}' not found in command group '${parent}'"
return 1
}
if { ${direction} eq "up" } {
set neighbor "previous"
set sign "-"
} elseif { ${direction} eq "down" } {
set neighbor "next"
set sign "+"
} else {
return 1
}
set node ${name}
set neighbor_node [${mtree} ${neighbor} ${node}]
if { ${neighbor_node} eq "" } {
# item is at top/bottom of list
return 0
}
# update mtree structure
${mtree} swap ${name} ${neighbor_node}
# get menu index information for the move
set grandparent_node [${mtree} parent ${parent}]
# update the item in all top level windows
foreach wtop [dict keys ${installs}] {
if { [my IsHidden ${grandparent_node}] != 1 } {
set grandparent_path [dict get ${installs} ${wtop} ${grandparent_node} +pathname]
if { ![catch {${grandparent_path} index ${name}} old_idx] } {
# compute new command location within group menu
set new_idx [expr ${old_idx}${sign}1]
# remove the command from its current location
${grandparent_path} delete ${old_idx}
# insert command in the new location
${grandparent_path} insert ${new_idx} command -label "${name}"
my RenderTag ${wtop} ${name}
}
}
}
return 0
}
# ------------------------------------------------------------
#
# group.configure --
#
# Modify the configuration of a command in a commandgroup.
#
# Arguments:
# parent - the mtree commandgroup node of interest
# name - name of item to be moved
# args - a list of option/value pairs used to configure
# the named commandgroup item.
#
# Results:
# Returns 0 on success or 1 on failure.
#
# Side effects:
# A menu items is move up or down on all installed menubar.
#
# ------------------------------------------------------------
method group.configure { parent name args } {
variable mtree
variable first_install
variable installs
if { [${mtree} key.get ${parent} +type] ne "commandgroup" } {
#puts stderr "group.configure: tag (${parent}) not a command group"
return 1
}
# don't configure item if it doesn't exists
if { ${name} ni [${mtree} children ${parent}] } {
#puts stderr "group.configure: command '${name}' doesn't exist in command group '${parent}'"
return 1
}
# put option info in tree structure
foreach {opt value} ${args} {
switch -exact -- ${opt} {
"-command" {
set value [string trim ${value}]
${mtree} key.set ${name} +command ${value}
}
-state {
if { ${first_install} ne "no" } {
${mtree} key.set ${name} ${opt} ${value}
}
}
-bind {
lassign ${value} uline accel sequence
if { ${uline} eq "" || [string is integer ${uline}] } {
${mtree} key.set ${name} -underline [expr {(${uline} eq "") ? -1 : ${uline}}]
} else {
error "tag.configure: underline value for tag (${node}) not positive integer."
}
${mtree} key.set ${name} -accelerator ${accel}
${mtree} key.set ${name} +bind ${sequence}
}
default {
${mtree} key.set ${name} ${opt} ${value}
}}
}
# update the node in all top level windows
foreach wtop [dict keys ${installs}] {
my RenderTag ${wtop} ${name}
}
return 0
}
# ------------------------------------------------------------
#
# group.serialize --
#
# Create a serialized representation of a commandgroup.
#
# Arguments:
# node - node name of the commandgroup
#
# Results:
# Returns a string serialization or a 1 on failure.
#
# Side effects:
# none
#
# ------------------------------------------------------------
method group.serialize { node } {
variable mtree
if { [${mtree} key.get ${node} +type] ne "commandgroup" } {
#puts stderr "group.serialize: tag (${parent}) not a command group"
return 1
}
return [${mtree} serialize ${node}]
}
# ------------------------------------------------------------
#
# group.deserialize --
#
# Replace the items in a commandgroup from the definitions
# found in a serialized stream
#
# Arguments:
# node - node name of the commandgroup
# stream - a commandgroup serialization string
#
# Results:
# Returns 0 on success or 1 on failure
#
# Side effects:
# All the items in the commandgroup are deleted and replaced
# with new items defined by the serialization string.
#
# ------------------------------------------------------------
method group.deserialize { node stream } {
variable mtree
variable installs
if { [${mtree} key.get ${node} +type] ne "commandgroup" } {
#puts stderr "group.serialize: tag (${parent}) not a command group"
return 1
}
# delete the existing entries for node
foreach name [my group.entries ${node}] {
my group.delete ${node} ${name}
}
# replace the node from the serialized stream
${mtree} deserialize ${node} ${stream}
set parent ${node}
# update the node in all top level windows
foreach wtop [dict keys ${installs}] {
if { [my IsHidden ${parent}] != 1 } {
set grandparent_node [${mtree} parent ${parent}]
set grandparent_path [dict get ${installs} ${wtop} ${grandparent_node} +pathname]
foreach name [${mtree} children ${parent}] {
if { [catch {${grandparent_path} index ${name}} msg] } {
${grandparent_path} add command -label ${name}
}
my RenderTag ${wtop} ${name}
}
}
}
return 0
}
# ===== COMMANDS THAT SUPPORT NOTEBOOK TABS =========================
# ------------------------------------------------------------
#
# notebook.addTabStore --
#
# Add a new set of values to the notebookVals dict
#
# Arguments:
# pathname - tab window pathname
#
# Results:
# none
#
# Side effects:
# A new tab is initialized in the notebookVals dict
#
# ------------------------------------------------------------
method notebook.addTabStore { pathname } {
variable wtop
variable notebookVals
dict for {tag var} [dict get ${notebookVals} ${wtop} +var] {
dict set notebookVals ${wtop} ${pathname} ${tag} [set ${var}]
}
}
# ------------------------------------------------------------
#
# notebook.deleteTabStore --
#
# Remove a set of values from the notebookVals dict
#
# Arguments:
# pathname - window pathname
#
# Results:
# none
#
# Side effects:
# A tab is deleted from the notebookVals dict
#
# ------------------------------------------------------------
method notebook.deleteTabStore { pathname } {
variable wtop
variable notebookVals
set notebookVals [dict remove ${notebookVals} ${wtop} ${pathname}]
}
# ------------------------------------------------------------
#
# notebook.setTabValue --
#
# Set a value in the notebookVals dict
#
# Arguments:
# pathname - tab window pathname
# tag - menubar tag name
#
# Results:
# none
#
# Side effects:
# A value in the notebookVals dict is set
# from a menu item variable.
#
# ------------------------------------------------------------
method notebook.setTabValue { pathname tag } {
set wtop [winfo toplevel ${pathname}]
variable notebookVals
if { [dict exists ${notebookVals} ${wtop} ${pathname} ${tag}] } {
set val [set [dict get ${notebookVals} ${wtop} +var ${tag}]]
dict set notebookVals ${wtop} ${pathname} ${tag} ${val}
}
}
# ------------------------------------------------------------
#
# notebook.restoreTabValues --
#
# Restore the value for a notebook tab.
#
# Arguments:
# pathname - tab window pathname
#
# Results:
# none
#
# Side effects:
# Values in notebookVals associated with pathname are
# assigned to menubar variables.
#
# ------------------------------------------------------------
method notebook.restoreTabValues { pathname } {
variable notebookVals
set wtop [winfo toplevel ${pathname}]
dict for {tag var} [dict get ${notebookVals} ${wtop} +var] {
set val [dict get ${notebookVals} ${wtop} ${pathname} ${tag}]
set ${var} ${val}
}
}
}
|