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 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
|
/*
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 of the
* License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
#include "grt_python_debugger.h"
#include "grt_shell_window.h"
#include "base/file_functions.h"
#include "base/string_utilities.h"
#include "base/wb_iterators.h"
#include "base/notifications.h"
#include "base/log.h"
#include <errno.h>
DEFAULT_LOG_DOMAIN("grtshell")
#include "grt/common.h"
#include "grt_code_editor.h"
#include "grt_plugin_wizard.h"
#include "workbench/wb_context.h"
#include "mforms/app.h"
#include "mforms/imagebox.h"
#include <glib/gstdio.h>
using namespace base;
using namespace bec;
using namespace mforms;
#define EDITOR_TAB_OFFSET 2
//--------------------------------------------------------------------------------------------------
GRTShellWindow::GRTShellWindow(wb::WBContext* context)
: mforms::Form(mforms::Form::main_form(), (mforms::FormFlag)(mforms::FormResizable | mforms::FormMinimizable | mforms::FormHideOnClose)),
_context(context),
_toolbar(true),
_content(false), _padding_box(false), _hsplitter(true), _side_tab(mforms::TabViewPalette),
_main_tab(mforms::TabViewDocumentClosable),
_global_box1(false), _global_box2(false), _global_splitter(false), _global_combo(mforms::SelectorCombobox),
_global_tree(mforms::TreeDefault), _global_list(mforms::TreeFlatList),
_classes_box(false), _classes_splitter(false), _classes_tree(mforms::TreeNoBorder),
_classes_text(mforms::VerticalScrollBar),
_modules_splitter(false), _modules_tree(mforms::TreeNoBorder), _modules_text(mforms::VerticalScrollBar),
_notifs_splitter(false), _notifs_tree(mforms::TreeNoBorder), _notifs_text(mforms::VerticalScrollBar),
_right_splitter(false),
_shell_box(false), _shell_text(mforms::VerticalScrollBar), _shell_hbox(true),
#ifdef _WIN32
_side_header_panel(mforms::FilledHeaderPanel),
_lower_tab(mforms::TabViewPalette),
_lower_header_panel(mforms::FilledHeaderPanel),
#else
_lower_tab(mforms::TabViewDocument),
#endif
_output_text(mforms::VerticalScrollBar),
_snippet_splitter(false),
_snippet_text()
{
set_title(("Workbench Scripting Shell"));
set_name("shell_window");
set_content(&_content);
set_menubar(&_menu);
scoped_connect(signal_closed(),boost::bind(&GRTShellWindow::shell_closed, this));
set_on_close(boost::bind(&GRTShellWindow::can_close, this));
_content.add(&_toolbar, false, true);
// setup the menubar
{
mforms::MenuItem *menu = mforms::manage(new mforms::MenuItem("File"));
mforms::MenuItem *item;
_menu.add_submenu(menu);
item = menu->add_item_with_title("New...", boost::bind(&GRTShellWindow::add_new_script, this));
item->set_shortcut("Modifier+N");
menu->add_item_with_title("New Script", boost::bind(&GRTShellWindow::add_editor, this, true, "python"));
item = menu->add_item_with_title("Open...", boost::bind(&GRTShellWindow::open_script_file, this));
item->set_shortcut("Modifier+O");
menu->add_separator();
item = menu->add_item_with_title("Save", boost::bind(&GRTShellWindow::save_file, this, false));
item->set_shortcut("Modifier+S");
item = menu->add_item_with_title("Save As...", boost::bind(&GRTShellWindow::save_file, this, true));
item->set_shortcut("Modifier+Shift+S");
menu->add_separator();
item = menu->add_item_with_title("Close Script", boost::bind(&GRTShellWindow::close_tab, this));
item->set_shortcut("Modifier+W");
item = menu->add_item_with_title("Close Window", boost::bind(&GRTShellWindow::close, this));
#ifdef _WIN32
item->set_shortcut("Control+F4");
#else
item->set_shortcut("Modifier+Shift+W");
#endif
menu = mforms::manage(new mforms::MenuItem("Edit"));
_menu.add_submenu(menu);
item = menu->add_item_with_title("Cut", boost::bind(&GRTShellWindow::cut, this));
item->set_shortcut("Modifier+X");
item = menu->add_item_with_title("Copy", boost::bind(&GRTShellWindow::copy, this));
item->set_shortcut("Modifier+C");
item = menu->add_item_with_title("Paste", boost::bind(&GRTShellWindow::paste, this));
item->set_shortcut("Modifier+V");
item = menu->add_item_with_title("Select All", boost::bind(&GRTShellWindow::select_all, this));
item->set_shortcut("Modifier+A");
menu->add_separator();
item = menu->add_item_with_title("Find...", boost::bind(&GRTShellWindow::show_find_panel, this));
item->set_shortcut("Modifier+F");
menu = mforms::manage(new mforms::MenuItem("Script"));
_menu.add_submenu(menu);
item = menu->add_item_with_title("Run", boost::bind(&GRTShellWindow::execute_file, this));
item->set_shortcut("Modifier+R");
item->set_name("run");
}
#ifdef _WIN32
_content.add(&_padding_box, true, true);
_padding_box.add(&_hsplitter, true, true);
_padding_box.set_padding(6);
set_back_color("#283752");
_hsplitter.set_back_color("#283752");
_side_header_panel.add(&_side_tab);
_side_header_panel.set_back_color("#283752");
_hsplitter.add(&_side_header_panel);
#else
_content.add(&_hsplitter, true, true);
_hsplitter.add(&_side_tab);
#endif
scoped_connect(_side_tab.signal_tab_changed(),boost::bind(&GRTShellWindow::side_tab_changed, this));
_hsplitter.add(&_right_splitter);
// Side bar consists of 4 pages: files, globals tree, classes tree and modules tree.
// Setup toolbar.
#ifdef _WIN32
_toolbar.set_size(-1, 24);
_toolbar.set_back_color("#BCC7D8");
#endif
_toolbar.set_padding(2);
_toolbar.set_spacing(4);
add_tool_button("tiny_new_doc.png", boost::bind(&GRTShellWindow::add_new_script, this),
_("Create a new file from a template"));
add_tool_button("tiny_load.png", boost::bind(&GRTShellWindow::open_script_file, this),
_("Open a script file"));
add_tool_separator();
_save_button = add_tool_button("tiny_save.png", boost::bind(&GRTShellWindow::save_file, this, false),
_("Save file"));
_save_as_button = add_tool_button("tiny_saveas.png", boost::bind(&GRTShellWindow::save_file, this, true),
_("Save file with a new name"));
add_tool_separator();
// if (_editing_module)
{
// add_tool_button("tiny_refresh.png", boost::bind(&GRTCodeEditor::execute, this),
// "Refresh the module");
}
// else
{
_run_button = add_tool_button("qe_sql-editor-tb-icon_execute.png", boost::bind(&GRTShellWindow::execute_file, this),
_("Execute script"));
_continue_button = add_tool_button("debug_continue.png", boost::bind(&GRTShellWindow::debug_continue, this),
_("Continue execution until next breakpoint"));
_pause_button = add_tool_button("debug_pause.png", boost::bind(&GRTShellWindow::debug_pause, this),
"Pause script execution");
_step_button = add_tool_button("debug_step.png", boost::bind(&GRTShellWindow::debug_step, this),
_("Step to next statement"));
_step_into_button = add_tool_button("debug_step_into.png", boost::bind(&GRTShellWindow::debug_step_into, this),
_("Step into next function"));
_step_out_button = add_tool_button("debug_step_out.png", boost::bind(&GRTShellWindow::debug_step_out, this),
_("Finish execution of current function"));
_stop_button = add_tool_button("debug_stop.png", boost::bind(&GRTShellWindow::debug_stop, this),
_("Stop executing script"));
_continue_button->show(false);
_step_button->set_enabled(false);
_step_into_button->set_enabled(false);
_step_out_button->set_enabled(false);
_continue_button->set_enabled(false);
_stop_button->set_enabled(false);
_pause_button->set_enabled(false);
}
#if !defined(_WIN32) && !defined(__APPLE__)
// TODO: remove as soon as all platforms support closable tabs.
_close_script_tab_button = add_tool_button("Discard.png", boost::bind(&GRTShellWindow::close_tab, this),
_("Close this script tab"), false);
#else
_close_script_tab_button = NULL;
#endif
add_tool_separator();
_clear_script_output_button = add_tool_button("clear_output.png", boost::bind(&mforms::TextBox::set_value, &_output_text, ""),
_("Clear script output"), false);
add_tool_button("snippet_add.png", boost::bind(&GRTShellWindow::add_snippet, this),
_("Add a new snippet"));
_snippet_delete_button = add_tool_button("snippet_del.png", boost::bind(&GRTShellWindow::del_snippet, this),
_("Delete the selected snippet"));
_snippet_copy_button = add_tool_button("snippet_clipboard.png", boost::bind(&GRTShellWindow::copy_snippet, this),
_("Copy snippet text to the clipboard"));
add_tool_separator();
_show_find_button = add_tool_button("qe_sql-editor-tb-icon_find.png", boost::bind(&GRTShellWindow::show_find_panel, this),
_("Show the Find panel for the editor"));
_show_find_button->set_enabled(false);
// Files
mforms::Box *files_box= mforms::manage(new mforms::Box(false));
#ifdef _WIN32
_files_tree = mforms::manage(new mforms::TreeNodeView(mforms::TreeNoBorder|mforms::TreeNoHeader));
#else
_files_tree = mforms::manage(new mforms::TreeNodeView(mforms::TreeDefault|mforms::TreeNoHeader));
#endif
_files_menu.add_item_with_title("Add New File", boost::bind(&GRTShellWindow::file_menu_activate, this, "file-from-template"));
_files_menu.add_item_with_title("Open Script File", boost::bind(&GRTShellWindow::file_menu_activate, this, "open-script"));
_files_menu.add_separator();
_files_menu.add_item_with_title("Delete Script", boost::bind(&GRTShellWindow::file_menu_activate, this, "delete-script"));
_files_tree->set_context_menu(&_files_menu);
_files_tree->add_column(IconStringColumnType, "", 400, false);
_files_tree->end_columns();
scoped_connect(_files_tree->signal_node_activated(),boost::bind(&GRTShellWindow::file_list_activated, this, _1, _2));
files_box->add(_files_tree, true, true);
#ifdef _WIN32
files_box->set_back_color("#FFFFFF");
files_box->set_padding(0, 0, 0, 2);
#endif
_side_tab.add_page(files_box, "Files");
// 1) Globals tree
#ifdef _WIN32
_global_splitter.set_back_color("#FFFFFF");
_hsplitter.set_back_color("#283752");
//_global_splitter.set_padding(0, 0, 0, 2); TODO: might require work around since we removed padding from View.
#endif
_side_tab.add_page(&_global_splitter, "Globals");
_global_splitter.add(&_global_box1, 0);
_global_splitter.add(&_global_box2, 0);
#ifndef _WIN32
_global_box1.set_spacing(4);
_global_box2.set_spacing(4);
#endif
_global_box1.add(&_global_combo, false, false);
_global_box1.add(&_global_tree, true, true);
_global_box2.add(&_global_entry, false, true);
_global_entry.set_read_only(true);
#if defined(_WIN32) | defined(__APPLE__)
_global_entry.set_back_color("#FFFFFF");
#endif
_global_box2.add(&_global_list, true, true);
_global_list.add_column(mforms::IconStringColumnType, "Name", 100);
_global_list.add_column(mforms::StringColumnType, "Value", 100);
_global_list.end_columns();
_global_tree.add_column(mforms::IconStringColumnType, "GRT Globals", 200, false);
_global_tree.add_column(mforms::StringColumnType, "Type", 100, false);
_global_tree.end_columns();
scoped_connect(_global_tree.signal_expand_toggle(), boost::bind(&GRTShellWindow::globals_expand_toggle, this, _1, _2));
scoped_connect(_global_tree.signal_changed(),boost::bind(&GRTShellWindow::global_selected, this));
scoped_connect(_global_combo.signal_changed(),boost::bind(&GRTShellWindow::refresh_globals_tree, this));
_inspector= 0;
_global_menu.add_item_with_title(_("Copy Value"), boost::bind(&GRTShellWindow::handle_global_menu, this, "copy_value"));
_global_menu.add_item_with_title(_("Copy Path"), boost::bind(&GRTShellWindow::handle_global_menu, this, "copy_path"));
_global_menu.add_item_with_title(_("Copy Path for Python"), boost::bind(&GRTShellWindow::handle_global_menu, this, "copy_path_py"));
_global_tree.set_context_menu(&_global_menu);
// 2) Classes tree
#ifdef _WIN32
_classes_splitter.set_back_color("#FFFFFF");
//_classes_splitter.set_padding(0, 0, 0, 2); TODO: might require workaround.
#endif
_side_tab.add_page(&_classes_splitter, "Classes");
_classes_splitter.add(&_classes_box, 0);
_classes_box.set_spacing(4);
_classes_box.add(&_classes_sorting, false, true);
_classes_box.add(&_classes_tree, true, true);
_classes_splitter.add(&_classes_text, 0);
_classes_text.set_read_only(true);
_classes_text.set_back_color("#FFFFFF");
_classes_tree.add_column(mforms::IconStringColumnType, "Name", 150, false);
_classes_tree.add_column(mforms::StringColumnType, "Type", 100, false);
_classes_tree.add_column(mforms::StringColumnType, "Caption", 100, false);
_classes_tree.end_columns();
scoped_connect(_classes_tree.signal_changed(),boost::bind(&GRTShellWindow::class_selected, this));
_classes_sorting.add_item("Group by Name");
_classes_sorting.add_item("Group by Hierarchy");
_classes_sorting.add_item("Group by Package");
scoped_connect(_classes_sorting.signal_changed(),boost::bind(&GRTShellWindow::refresh_classes_tree, this));
// 3) Modules tree
#ifdef _WIN32
_modules_splitter.set_back_color("#FFFFFF");
//_modules_splitter.set_padding(0, 0, 0, 2); TODO: might require workaround.
#endif
_side_tab.add_page(&_modules_splitter, "Modules");
_modules_splitter.add(&_modules_tree, 0);
_modules_splitter.add(&_modules_text, 0);
_modules_text.set_read_only(true);
_modules_text.set_back_color("#FFFFFF");
_modules_tree.add_column(mforms::IconStringColumnType, "GRT Modules", 220, false);
_modules_tree.end_columns();
scoped_connect(_modules_tree.signal_changed(),boost::bind(&GRTShellWindow::module_selected, this));
_right_splitter.add(&_main_tab);
// 4) Notifications tree
#ifdef _WIN32
_notifs_splitter.set_back_color("#FFFFFF");
//_modules_splitter.set_padding(0, 0, 0, 2); TODO: might require workaround.
#endif
_side_tab.add_page(&_notifs_splitter, "Notifications");
_notifs_splitter.add(&_notifs_tree, 0);
_notifs_splitter.add(&_notifs_text, 0);
_notifs_text.set_read_only(true);
_notifs_text.set_back_color("#FFFFFF");
_notifs_tree.add_column(mforms::IconStringColumnType, "Notifications", 220, false);
_notifs_tree.end_columns();
scoped_connect(_notifs_tree.signal_changed(),boost::bind(&GRTShellWindow::notif_selected, this));
#ifdef _WIN32
_right_splitter.set_back_color("#283752");
_lower_header_panel.add(&_lower_tab);
_right_splitter.add(&_lower_header_panel);
_lower_header_panel.set_title(_("Debugging"));
_lower_header_panel.set_back_color("#283752");
#else
_right_splitter.add(&_lower_tab);
#endif
// setup shell
#ifdef _WIN32
_snippet_list = mforms::manage(new TreeNodeView(mforms::TreeNoBorder|mforms::TreeFlatList));
#else
_snippet_list = mforms::manage(new TreeNodeView(mforms::TreeDefault|mforms::TreeFlatList));
#endif
_shell_box.add(&_shell_text, true, true);
_shell_text.set_monospaced(true);
_shell_text.set_read_only(true);
#if defined(_WIN32) | defined(__APPLE__)
_shell_text.set_front_color("#FFFFFF");
_shell_text.set_back_color("#000000");
#endif
_shell_text.set_padding(2);
_shell_box.add(&_shell_hbox, false, true);
_shell_hbox.add(&_shell_prompt, false, true);
_shell_hbox.add(&_shell_entry, true, true);
_main_tab.add_page(&_shell_box, "Shell");
scoped_connect(_shell_entry.signal_action(),boost::bind(&GRTShellWindow::shell_action, this, _1));
// snippets tab
#ifdef _WIN32
_snippet_splitter.set_back_color("#283752");
#else
_snippet_splitter.set_padding(8);
#endif
//_snippet_box.set_spacing(8);
_snippet_splitter.add(_snippet_list, 50, true);
_snippet_splitter.add(&_snippet_text, 50, false);
_snippet_menu.add_item_with_title("Execute Snippet", boost::bind(&GRTShellWindow::snippet_menu_activate, this, "execute"));
_snippet_menu.add_item_with_title("Send to Script Editor", boost::bind(&GRTShellWindow::snippet_menu_activate, this, "new_with_snippet"));
_snippet_menu.add_separator();
_snippet_menu.add_item_with_title("Copy to Clipboard", boost::bind(&GRTShellWindow::snippet_menu_activate, this, "copy_clipboard"));
_snippet_menu.add_separator();
_snippet_menu.add_item_with_title("Delete Snippet", boost::bind(&GRTShellWindow::snippet_menu_activate, this, "delete"));
_snippet_list->set_context_menu(&_snippet_menu);
scoped_connect(_snippet_list->signal_changed(),boost::bind(&GRTShellWindow::snippet_selected, this));
_snippet_text.set_language(LanguagePython);
scoped_connect(_snippet_text.signal_changed(),boost::bind(&GRTShellWindow::snippet_changed, this, _1, _2));
_snippet_list->add_column(mforms::StringColumnType, "Snippet", 500, false);
_snippet_list->end_columns();
_main_tab.add_page(&_snippet_splitter, "Snippets");
scoped_connect(_main_tab.signal_tab_closing(),boost::bind(&GRTShellWindow::on_tab_closing, this, _1));
scoped_connect(_main_tab.signal_tab_changed(),boost::bind(&GRTShellWindow::on_tab_changed, this));
//
_output_text.set_read_only(true);
_output_text.set_monospaced(true);
_lower_tab.add_page(&_output_text, "Output");
_debugger = new PythonDebugger(this, &_lower_tab);
try
{
_debugger->init_pdb();
}
catch (const std::exception &exc)
{
log_error("Could not initialize the debugger: %s\n", exc.what());
add_output("Could not initialize the debugger\n");
delete _debugger;
_debugger= 0;
}
// Minimum size for the entire window.
set_size(800, 600);
grtm()->run_once_when_idle(boost::bind(&GRTShellWindow::set_splitter_positions, this));
grtm()->get_shell()->set_ready_handler(boost::bind(&GRTShellWindow::handle_prompt, this, _1));
grtm()->get_shell()->set_output_handler(boost::bind(&GRTShellWindow::handle_output, this, _1));
on_tab_changed();
snippet_selected();
side_tab_changed();
}
bool GRTShellWindow::can_close()
{
// Because there's no other way to know if the window is about to close, we'll use this event to stop debugger.
if (_stop_button->is_enabled() && _debugger)
_debugger->stop();
// GRTShellWindow is about to close so we ask each editor if it's fine to quit.
return request_quit();
}
void GRTShellWindow::set_splitter_positions()
{
_hsplitter.set_position(300);
_global_splitter.set_position(400);
_modules_splitter.set_position(400);
_classes_splitter.set_position(400);
_notifs_splitter.set_position(400);
_snippet_splitter.set_position(200);
}
//--------------------------------------------------------------------------------------------------
GRTManager *GRTShellWindow::grtm()
{
return _context->get_grt_manager();
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::shell_action(mforms::TextEntryAction action)
{
switch (action)
{
case mforms::EntryActivate:
{
std::string command= _shell_entry.get_string_value();
_shell_entry.set_value("");
// _completion->add_completion_text(command);
command += '\n';
grtm()->get_shell()->write(grtm()->get_grt()->get_shell()->get_prompt()+" "+command);
grtm()->get_shell()->process_line_async(command);
break;
}
case mforms::EntryKeyUp:
{
std::string line;
if (grtm()->get_shell()->previous_history_line(_shell_entry.get_string_value(), line))
_shell_entry.set_value(line);
break;
}
case mforms::EntryCKeyUp:
break;
case mforms::EntryKeyDown:
{
std::string line;
if (grtm()->get_shell()->next_history_line(line))
_shell_entry.set_value(line);
break;
}
case mforms::EntryCKeyDown:
break;
case mforms::EntryEscape:
break;
}
}
void GRTShellWindow::show(bool flag)
{
if (flag)
refresh_all();
load_state();
mforms::Form::show(flag);
}
void GRTShellWindow::refresh_all()
{
refresh_files();
int idx= 0;
std::string root = _global_tree.root_node()->get_tag();
std::vector<std::string> l = grtm()->get_shell()->get_grt_tree_bookmarks();
_global_combo.clear();
for (std::vector<std::string>::const_iterator i = l.begin(); i != l.end(); ++i, ++idx)
{
_global_combo.add_item(*i);
if (root == *i)
_global_combo.set_selected(idx);
}
//refresh values
refresh_globals_tree();
global_selected();
//refresh _struct
refresh_classes_tree();
// refresh modules
refresh_modules_tree();
refresh_notifs_list();
_script_extension = ".py";
_comment_prefix = "# ";
refresh_snippets();
}
//--------------------------------------------------------------------------------------------------
bool GRTShellWindow::capture_output(const grt::Message &msg, void *sender, bool send_to_output)
{
if (msg.type == grt::OutputMsg)
{
if (grtm()->in_main_thread())
{
if (send_to_output)
add_output(msg.text);
else
handle_output(msg.text); // sends to shell window
}
else
{
if (send_to_output)
grtm()->run_once_when_idle(boost::bind(&GRTShellWindow::add_output, this, msg.text));
else
grtm()->run_once_when_idle(boost::bind(&GRTShellWindow::handle_output, this, msg.text));
}
return true;
}
return false;
}
void GRTShellWindow::execute_file()
{
GRTCodeEditor *editor = get_active_editor();
if (!editor) return;
grtm()->get_grt()->push_message_handler(boost::bind(&GRTShellWindow::capture_output, this, _1, _2, true));
if (_debugger && g_str_has_suffix(editor->get_path().c_str(), ".py"))
{
_run_button->show(false);
_continue_button->show(true);
_pause_button->set_enabled(true);
_debugger->run(editor);
_run_button->show(true);
_continue_button->show(false);
_step_button->set_enabled(false);
_step_into_button->set_enabled(false);
_step_out_button->set_enabled(false);
_continue_button->set_enabled(false);
_stop_button->set_enabled(false);
_pause_button->set_enabled(false);
}
else
try
{
editor->execute();
}
catch (const std::exception &exc)
{
log_error("Error during execution of script: %s\n", exc.what());
add_output("There were errors during execution. Please review log messages.\n");
}
grtm()->get_grt()->pop_message_handler();
}
void GRTShellWindow::debug_step()
{
GRTCodeEditor *editor = get_active_editor();
if (editor && _debugger && g_str_has_suffix(editor->get_path().c_str(), ".py"))
{
if (_debugger->program_stopped())
_debugger->step();
else
{
// start the program stopping at the 1st line
grtm()->get_grt()->push_message_handler(boost::bind(&GRTShellWindow::capture_output, this, _1, _2, true));
_run_button->show(false);
_continue_button->show(true);
_pause_button->set_enabled(true);
_debugger->run(editor, true);
_run_button->show(true);
_continue_button->show(false);
_step_button->set_enabled(true);
_step_into_button->set_enabled(false);
_step_out_button->set_enabled(false);
_continue_button->set_enabled(false);
_stop_button->set_enabled(false);
_pause_button->set_enabled(false);
grtm()->get_grt()->pop_message_handler();
}
}
}
void GRTShellWindow::debug_step_into()
{
if (_debugger)
_debugger->step_into();
}
void GRTShellWindow::debug_step_out()
{
if (_debugger)
_debugger->step_out();
}
void GRTShellWindow::debug_continue()
{
if (_debugger)
_debugger->continue_();
}
void GRTShellWindow::debug_stop()
{
if (_debugger)
_debugger->stop();
}
void GRTShellWindow::debug_pause()
{
if (_debugger)
_debugger->pause();
}
void GRTShellWindow::save_file(bool save_as)
{
GRTCodeEditor *editor = get_active_editor();
if (editor)
editor->save(save_as);
}
void GRTShellWindow::close_tab()
{
GRTCodeEditor *editor = get_active_editor();
if (editor)
{
if (editor->can_close())
close_editor(editor);
}
}
void GRTShellWindow::show_find_panel()
{
GRTCodeEditor *editor = get_active_editor();
if (editor)
{
editor->get_editor()->show_find_panel(false);
}
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::side_tab_changed()
{
#ifdef _WIN32
static std::string side_bar_titles[] = {
_("File Browser"),
_("Globals Tree"),
_("Classes List"),
_("Modules List"),
_("Notifications")
};
_side_header_panel.set_title(side_bar_titles[_side_tab.get_active_tab()]);
#endif
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::handle_output(const std::string &text)
{
_shell_text.append_text(text, true);
}
void GRTShellWindow::handle_error(const std::string &text, const std::string &detail)
{
_shell_text.append_text(text);
_shell_text.append_text(detail);
}
void GRTShellWindow::handle_prompt(const std::string &text)
{
_shell_prompt.set_text(text);
}
void GRTShellWindow::global_selected()
{
if (_inspector)
{
delete _inspector;
_inspector= 0;
}
mforms::TreeNodeRef selected;
try
{
if ((selected = _global_tree.get_selected_node()))
{
grt::ValueRef value(get_global_at_node(selected));
if (value.is_valid())
{
_inspector= ValueInspectorBE::create(grtm()->get_grt(), value, false, false);
refresh_global_list();
}
_global_entry.set_value(get_global_path_at_node(selected));
}
else
_global_entry.set_value("");
}
catch (std::exception &exc)
{
log_error("Exception when selecting item in globals tree: %s\n", exc.what());
}
}
void GRTShellWindow::class_selected()
{
mforms::TreeNodeRef selected;
if ((selected = _classes_tree.get_selected_node()))
_classes_text.set_value(get_class_node_description(selected));
else
_classes_text.set_value("");
}
void GRTShellWindow::module_selected()
{
mforms::TreeNodeRef selected;
if ((selected = _modules_tree.get_selected_node()))
{
std::string text(get_module_node_description(selected));
_modules_text.set_value(text);
}
else
_modules_text.set_value("");
}
void GRTShellWindow::notif_selected()
{
mforms::TreeNodeRef selected;
if ((selected = _notifs_tree.get_selected_node()) && selected->get_parent() != _notifs_tree.root_node())
{
std::string text;
std::string name = selected->get_string(0);
base::NotificationCenter::NotificationHelp info = base::NotificationCenter::get()->get_registered_notification(name);
text = base::strfmt("%s (%s)\n"
"%s\n\n"
"Sender: %s\n\n"
"Extra Info Dictionary:\n%s",
name.c_str(), info.context.c_str(),
info.summary.c_str(),
info.sender.empty() ? "NULL" : info.sender.c_str(),
info.info.empty() ? "No additional info is sent" : info.info.c_str());
_notifs_text.set_value(text);
}
else
_notifs_text.set_value("");
}
void GRTShellWindow::handle_global_menu(const std::string &action)
{
mforms::TreeNodeRef selected;
if ((selected = _global_tree.get_selected_node()))
{
if (action == "copy_value")
{
grt::ValueRef value(get_global_at_node(selected));
mforms::Utilities::set_clipboard_text(value.debugDescription());
}
else if (action == "copy_path")
{
mforms::Utilities::set_clipboard_text(get_global_path_at_node(selected));
}
else if (action == "copy_path_py")
{
std::string path = "grt.root";
std::vector<std::string> parts;
parts = base::split(get_global_path_at_node(selected), "/");
for (base::const_range<std::vector<std::string> > p(parts); p; ++p)
{
if (p->empty()) continue;
if (isdigit(p->at(0)))
path.append("[").append(*p).append("]");
else
path.append(".").append(*p);
}
mforms::Utilities::set_clipboard_text(path);
}
}
}
void GRTShellWindow::save_snippets()
{
std::string path = make_path(grtm()->get_user_datadir(), "shell_snippets"+_script_extension);
FILE *f = base_fopen(path.c_str(), "w+");
if (!f)
{
_shell_text.append_text(base::strfmt("Cannot save snippets to %s: %s",
path.c_str(), g_strerror(errno)));
return;
}
int c = _snippet_list->root_node()->count();
for (int i = _global_snippet_count; i < c; i++)
{
std::string snippet = _snippet_list->root_node()->get_child(i)->get_tag();
std::string::size_type p = 0, l = snippet.size();
while (p < l)
{
std::string::size_type eol = snippet.find('\n', p);
if (eol == std::string::npos)
eol = l;
else
eol++;
fwrite(" ", 1, 1, f);
fwrite(snippet.data()+p, 1, eol-p, f);
p = eol;
}
fwrite("\n", 1, 1, f);
}
fclose(f);
}
void GRTShellWindow::load_snippets_from(const std::string &path)
{
FILE *f = base_fopen(path.c_str(), "r");
if (f)
{
char line[4096];
while (fgets(line, sizeof(line), f))
{
std::string script = line+1;
char *ptr = strchr(line, '\n');
if (ptr)
*ptr= 0;
std::string name = line+1;
while (fgets(line, sizeof(line)-1, f) && line[0] == ' ')
{
script.append(line+1);
}
// Remove the last line break, we added that, not the user.
if (script.size() > 0)
script.erase(script.size() - 1, 1);
mforms::TreeNodeRef node = _snippet_list->add_node();
node->set_string(0, name);
node->set_tag(script);
}
fclose(f);
}
}
void GRTShellWindow::refresh_snippets()
{
_snippet_list->clear();
load_snippets_from(grtm()->get_data_file_path("shell_snippets"+_script_extension+".txt"));
_global_snippet_count = _snippet_list->root_node()->count();
load_snippets_from(make_path(grtm()->get_user_datadir(), "shell_snippets"+_script_extension));
snippet_selected();
}
void GRTShellWindow::open_script_file()
{
mforms::FileChooser chooser(mforms::OpenFile);
chooser.set_title(_("Open GRT Script"));
if (chooser.run_modal())
{
open_file_in_editor(chooser.get_path(), true);
}
}
bool GRTShellWindow::execute_script(const std::string &script, const std::string &language)
{
bool result = grtm()->get_shell()->run_script(script, language);
save_state();
return result;
}
void GRTShellWindow::add_snippet()
{
std::string snippet = _comment_prefix + " new snippet\n";
mforms::TreeNodeRef node = _snippet_list->add_node();
node->set_tag(snippet);
_snippet_list->select_node(node);
snippet_selected(); // force snippet to be displayed
snippet_changed(0, 0); // force setting of the snippet name
save_state();
}
void GRTShellWindow::del_snippet()
{
mforms::TreeNodeRef node = _snippet_list->get_selected_node();
if (node)
{
node->remove_from_parent();
snippet_selected();
save_snippets();
}
}
void GRTShellWindow::copy_snippet()
{
mforms::TreeNodeRef node = _snippet_list->get_selected_node();
if (node)
mforms::Utilities::set_clipboard_text(node->get_tag());
}
void GRTShellWindow::scriptize_snippet()
{
mforms::TreeNodeRef node = _snippet_list->get_selected_node();
if (node)
{
std::string snippet = node->get_tag();
std::string language = "python";
GRTCodeEditor *editor= add_editor(true, language);
editor->set_text(snippet);
}
}
bool run_return_true(boost::function<void (const std::string&)> f, const std::string& param)
{
f(param);
return true;
}
void GRTShellWindow::run_snippet()
{
mforms::TreeNodeRef node = _snippet_list->get_selected_node();
if (node)
{
std::string script = node->get_tag();
// auto-select the tab where output goes
_main_tab.set_active_tab(0);
handle_output("Running snippet...\n");
// redirect snippet output to the shell
grtm()->get_grt()->push_message_handler(boost::bind(&GRTShellWindow::capture_output, this, _1, _2, false));
try
{
std::string language = "python";
bool ret = execute_script(script, language);
grtm()->get_grt()->pop_message_handler();
if (!ret)
handle_output("Snippet execution finished with an error\n");
}
catch (const std::exception &exc)
{
grtm()->get_grt()->pop_message_handler();
handle_output("Exception caught while executing snippet:\n");
handle_output(std::string(exc.what()).append("\n"));
}
}
save_state();
}
void GRTShellWindow::snippet_selected()
{
bool read_only = false;
_snippet_text.set_features(mforms::FeatureReadOnly, false); // Necessary to be able to change the text.
int sel = _snippet_list->get_selected_row();
if (sel < 0)
{
_snippet_delete_button->set_enabled(false);
_snippet_copy_button->set_enabled(false);
_snippet_text.set_value("");
read_only = true;
for (int i= 0; i < 6; i++)
_snippet_menu.get_item(i)->set_enabled(false);
}
else
{
if (sel < _global_snippet_count)
{
read_only = true;
_snippet_delete_button->set_enabled(false);
for (int i= 0; i < 6; i++)
{
if (i != 5)
_snippet_menu.get_item(i)->set_enabled(true);
else
_snippet_menu.get_item(i)->set_enabled(false); // 5 is delete
}
}
else
{
_snippet_delete_button->set_enabled(true);
for (int i= 0; i < 6; i++)
_snippet_menu.get_item(i)->set_enabled(true);
}
mforms::TreeNodeRef node(_snippet_list->get_selected_node());
if (node)
_snippet_text.set_value(node->get_tag());
_snippet_copy_button->set_enabled(true);
}
_snippet_text.set_features(mforms::FeatureReadOnly, read_only);
}
void GRTShellWindow::snippet_changed(int line, int linesAdded)
{
std::string snippet = _snippet_text.get_string_value();
mforms::TreeNodeRef node = _snippet_list->get_selected_node();
if (node)
{
node->set_tag(snippet);
std::string::size_type p = snippet.find('\n');
if (p != std::string::npos)
snippet = snippet.substr(0, p);
node->set_string(0, snippet);
save_snippets();
}
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::snippet_menu_activate(const std::string &action)
{
if (action == "execute")
run_snippet();
else if (action == "new_with_snippet")
scriptize_snippet();
else if (action == "copy_clipboard")
copy_snippet();
else if (action == "delete")
del_snippet();
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::file_menu_activate(const std::string &action)
{
if (action == "file-from-template")
add_new_script();
else if (action == "open-script")
open_script_file();
else if (action == "delete-script")
delete_selected_file();
}
//--------------------------------------------------------------------------------------------------
GRTCodeEditor *GRTShellWindow::add_editor(bool is_script, const std::string &language)
{
GRTCodeEditor *editor = manage(new GRTCodeEditor(this, !is_script, language));
_editors.push_back(editor);
int page = _main_tab.add_page(editor, editor->get_title());
_main_tab.set_active_tab(page);
save_state();
if (language == "python" && _debugger)
_debugger->editor_added(editor);
return editor;
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::close_editor(GRTCodeEditor *editor)
{
for (std::vector<GRTCodeEditor*>::iterator iter= _editors.begin(); iter != _editors.end(); ++iter)
{
if ((*iter) == editor)
{
_editors.erase(iter);
break;
}
}
if (_debugger)
_debugger->editor_closed(editor);
_main_tab.remove_page(editor);
save_state();
}
void GRTShellWindow::open_file_in_editor(const std::string &path, bool is_script)
{
if (get_editor_for(path, true) != NULL)
return;
std::string language = ""; // No syntax highlighting if file extension is unknown.
if (g_str_has_suffix(path.c_str(), ".py"))
language = "python"; // Python script
else
if (g_str_has_suffix(path.c_str(), ".sql") || g_str_has_suffix(path.c_str(), ".qbquery"))
language = "sql";
// Show warning messages if applicable...
if (language == "")
{
std::string text= base::strfmt(_("The file %s has an unsupported extension for this script editor."), path.c_str());
if (Utilities::show_message_and_remember(_("Unsupported File Format"), text, _("OK"), _("Cancel"), "", "ShellWindowUnknownLanguageFile", "") == mforms::ResultCancel)
return;
}
else if (language == "sql")
{
if (Utilities::show_message_and_remember(_("Unsupported Execution"), _("This script editor is meant for developing Workbench plugins and scripts. SQL scripts should be opened and executed in the SQL Editor."), _("OK"), _("Cancel"), "", "ShellWindowSqlLanguageFile", "") == mforms::ResultCancel)
return;
}
GRTCodeEditor *editor= add_editor(is_script, language);
if (!editor->load(path))
{
close_editor(editor);
return;
}
#ifdef _DEBUG
editor->test_markup();
#endif
}
GRTCodeEditor *GRTShellWindow::show_file_at_line(const std::string &path, int line)
{
open_file_in_editor(path, true);
GRTCodeEditor *editor = get_editor_for(path, true);
if (!editor)
add_output(base::strfmt("Cannot open file %s", path.c_str()));
else
{
ssize_t start, length;
editor->get_editor()->get_range_of_line(line, start, length);
editor->get_editor()->set_selection(start, 0);
}
return editor;
}
void GRTShellWindow::add_new_script()
{
NewPluginDialog wizard(this, grtm()->get_data_file_path("script_templates"));
std::string path;
std::string code;
bool is_script;
std::string language;
if (wizard.run(path, code, is_script, language))
{
GRTCodeEditor *editor= add_editor(is_script, language);
if (!path.empty() && base::basename(path) == path)
path= make_path(grtm()->get_user_script_path(), path);
editor->set_path(path);
editor->set_text(code);
}
save_state();
}
bool GRTShellWindow::add_output(const std::string &text)
{
_output_text.append_text(text, true);
return true;
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::set_editor_title(GRTCodeEditor *editor, const std::string &title)
{
int index= _main_tab.get_page_index(editor);
if (index >= 0)
_main_tab.set_tab_title(index, editor->get_title());
}
//--------------------------------------------------------------------------------------------------
/**
* Called from the UI context when WB is about to quit. Check if we have pending changes.
* Return true if we are clear, false otherwise.
*/
bool GRTShellWindow::request_quit()
{
std::vector<GRTCodeEditor*>::reverse_iterator editor;
while ((editor = _editors.rbegin()) != _editors.rend())
{
if (!(*editor)->can_close())
return false;
else
close_editor(*editor);
}
return true;
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::add_files_from_dir(mforms::TreeNodeRef parent, const std::string &dirname, bool is_script)
{
GDir *dir= g_dir_open(dirname.c_str(), 0, NULL);
if (!dir)
return;
while (const gchar *name= g_dir_read_name(dir))
{
if (g_str_has_suffix(name, ".py"))
{
mforms::TreeNodeRef node = parent->add_child();
node->set_string(0, name);
if (is_script)
node->set_tag(std::string("s").append(dirname).append(G_DIR_SEPARATOR_S).append(name));
else
node->set_tag(std::string("m").append(dirname).append(G_DIR_SEPARATOR_S).append(name));
}
}
g_dir_close(dir);
}
void GRTShellWindow::refresh_files()
{
mforms::TreeNodeRef node;
_files_tree->clear();
node= _files_tree->root_node()->add_child();
node->set_string(0, "User Scripts");
node->set_icon_path(0, "folder");
add_files_from_dir(node, grtm()->get_user_script_path(), true);
node->expand();
node= _files_tree->root_node()->add_child();
node->set_string(0, "User Modules");
node->set_icon_path(0, "folder");
add_files_from_dir(node, grtm()->get_user_module_path(), false);
node->expand();
node= _files_tree->root_node()->add_child();
node->set_string(0, "User Libraries");
node->set_icon_path(0, "folder");
add_files_from_dir(node, grtm()->get_user_library_path(), true);
node->expand();
}
void GRTShellWindow::file_list_activated(mforms::TreeNodeRef node, int column)
{
if (node)
{
std::string path= node->get_tag();
if (!path.empty())
{
open_file_in_editor(path.substr(1), path[0] == 's');
}
}
}
void GRTShellWindow::on_file_save(const std::string &file)
{
refresh_files();
if (_debugger)
_debugger->refresh_file(file);
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::delete_selected_file()
{
mforms::TreeNodeRef node(_files_tree->get_selected_node());
if (node)
{
std::string path= node->get_tag();
if (!path.empty())
{
std::string fn = path.substr(1);
if (mforms::Utilities::show_message(_("Delete File"),
base::strfmt(_("Really delete '%s' from disk? This operation cannot be undone."),
fn.c_str()),
_("Delete"), _("Cancel")) == mforms::ResultOk)
{
::g_remove(fn.c_str());
refresh_files();
}
}
}
}
//--------------------------------------------------------------------------------------------------
mforms::Button *GRTShellWindow::add_tool_button(const std::string &image,
const boost::function<void ()> &action,
const std::string &tooltip,
bool left)
{
App *app= App::get();
Button *b = manage(new Button(ToolButton));
b->set_icon(app->get_resource_path(image));
b->set_tooltip(tooltip);
scoped_connect(b->signal_clicked(),action);
if (left)
_toolbar.add(b, false, true);
else
_toolbar.add_end(b, false, true);
return b;
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::add_tool_separator()
{
App *app= App::get();
ImageBox* image = manage(new ImageBox());
image->set_image(app->get_resource_path("statusbar_separator.png"));
image->set_image_align(MiddleCenter);
_toolbar.add(image, false, true);
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::load_state()
{
int x = _context->read_state("left", "scripting-shell", 100);
int y = _context->read_state("top", "scripting-shell", 100);
int width = _context->read_state("width", "scripting-shell", 800);
int height = _context->read_state("height", "scripting-shell", 600);
set_size(width, height);
set_position(x, y);
// Restore divider positions.
_hsplitter.set_position(_context->read_state("main-splitter", "scripting-shell", 250));
_global_splitter.set_position(_context->read_state("global-splitter", "scripting-shell", 400));
_modules_splitter.set_position(_context->read_state("modules-splitter", "scripting-shell", 400));
_classes_splitter.set_position(_context->read_state("classes-splitter", "scripting-shell", 400));
_snippet_splitter.set_position(_context->read_state("snippets-splitter", "scripting-shell", 400));
_shell_text.set_font(grtm()->get_app_option_string("workbench.scripting.ScriptingShell:Font"));
_snippet_text.set_font(grtm()->get_app_option_string("workbench.scripting.ScriptingEditor:Font"));
for (std::vector<GRTCodeEditor*>::iterator editor = _editors.begin(); editor != _editors.end(); editor++)
(*editor)->set_font(grtm()->get_app_option_string("workbench.scripting.ScriptingEditor:Font"));
_lower_tab_height = _context->read_state("editor-splitter", "scripting-shell", 400);
on_tab_changed();
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::save_state()
{
// Store form's size and position.
_context->save_state("left", "scripting-shell", get_x());
_context->save_state("top", "scripting-shell", get_y());
_context->save_state("width", "scripting-shell", get_width());
_context->save_state("height", "scripting-shell", get_height());
// Store all divider positions.
_context->save_state("main-splitter", "scripting-shell", _hsplitter.get_position());
_context->save_state("global-splitter", "scripting-shell", _global_splitter.get_position());
_context->save_state("modules-splitter", "scripting-shell", _modules_splitter.get_position());
_context->save_state("classes-splitter", "scripting-shell", _classes_splitter.get_position());
_context->save_state("snippet-splitter", "scripting-shell", _snippet_splitter.get_position());
}
//--------------------------------------------------------------------------------------------------
/**
* Triggered when the shell window was closed by the user. We can use this event to store our state.
*/
void GRTShellWindow::shell_closed()
{
save_state();
}
//--------------------------------------------------------------------------------------------------
/**
* Triggered when a tab is about to close. Don't allow shell and snippets to close and check if
* editors are dirty.
*/
bool GRTShellWindow::on_tab_closing(int index)
{
if (index == 0 || index == 1)
return false;
GRTCodeEditor* editor = _editors[index - EDITOR_TAB_OFFSET];
if (editor->can_close())
{
close_editor(editor);
return true;
}
return false;
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::on_tab_changed()
{
GRTCodeEditor *editor = get_active_editor();
mforms::MenuItem* _run = _menu.find_item("run");
if (editor)
{
bool exec_enabled = (editor->get_language() == "python");
_save_button->set_enabled(true);
_save_as_button->set_enabled(true);
_run_button->set_enabled(exec_enabled);
if(_run)
_run->set_enabled(exec_enabled);
_step_button->set_enabled(exec_enabled);
_clear_script_output_button->set_enabled(true);
if (_close_script_tab_button)
_close_script_tab_button->set_enabled(true);
_show_find_button->set_enabled(true);
_right_splitter.set_expanded(false, true);
}
else
{
_save_button->set_enabled(false);
_save_as_button->set_enabled(false);
_run_button->set_enabled(false);
if(_run)
_run->set_enabled(false);
_step_button->set_enabled(false);
_clear_script_output_button->set_enabled(false);
if (_close_script_tab_button)
_close_script_tab_button->set_enabled(false);
_show_find_button->set_enabled(false);
_right_splitter.set_expanded(false, false);
}
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::activate_output_tab()
{
_lower_tab.set_active_tab(0);
}
//--------------------------------------------------------------------------------------------------
/**
* Returns the editor which is currently editing the given file.
*/
GRTCodeEditor* GRTShellWindow::get_editor_for(const std::string& path, bool select_tab)
{
#ifdef _WIN32
// We probably would need g_utf8_normalize too if we want it really good, but since this is
// supposed to be a temporary solution...
gchar* path1 = g_utf8_strdown(path.c_str(), -1);
for (std::vector<GRTCodeEditor*>::iterator editor = _editors.begin(); editor != _editors.end(); editor++)
{
gchar* path2 = g_utf8_strdown((*editor)->get_path().c_str(), -1);
if (g_utf8_collate(path1, path2) == 0)
{
if (select_tab)
_main_tab.set_active_tab((int)(editor - _editors.begin() + EDITOR_TAB_OFFSET));
g_free(path2);
return *editor;
}
g_free(path2);
}
g_free(path1);
#else
for (std::vector<GRTCodeEditor*>::iterator editor = _editors.begin(); editor != _editors.end(); editor++)
{
if ((*editor)->get_path() == path)
{
if (select_tab)
_main_tab.set_active_tab(editor - _editors.begin() + EDITOR_TAB_OFFSET);
return *editor;
}
}
#endif
return NULL;
}
GRTCodeEditor *GRTShellWindow::get_active_editor()
{
int index = _main_tab.get_active_tab() - EDITOR_TAB_OFFSET;
if (index >= 0 && index < (int)_editors.size())
return _editors[index];
return 0;
}
//--------------------------------------------------------------------------------------------------
template <class C>
struct CompareNamedObject
{
bool operator()(C *a, C *b)
{
return a->name() < b->name();
}
};
void GRTShellWindow::refresh_modules_tree()
{
grt::GRT *grt = _context->get_grt();
IconManager *im = IconManager::get_instance();
std::string mod_icon = im->get_icon_path("grt_module.png");;
std::string fun_icon = im->get_icon_path("grt_function.png");;
_modules_tree.clear();
std::vector<grt::Module*> modules(grt->get_modules());
std::sort(modules.begin(), modules.end(), CompareNamedObject<grt::Module>());
for (std::vector<grt::Module*>::const_iterator m = modules.begin(); m != modules.end(); ++m)
{
mforms::TreeNodeRef mod_node = _modules_tree.add_node();
const std::vector<grt::Module::Function> functions((*m)->get_functions());
if ((*m)->description().empty())
mod_node->set_string(0, (*m)->name());
else
mod_node->set_string(0, (*m)->name()+" *");
mod_node->set_icon_path(0, mod_icon);
mod_node->set_tag("m");
for (std::vector<grt::Module::Function>::const_iterator f = functions.begin(); f != functions.end(); ++f)
{
mforms::TreeNodeRef fun_node = mod_node->add_child();
fun_node->set_string(0, f->name);
fun_node->set_icon_path(0, fun_icon);
}
}
}
std::string GRTShellWindow::get_module_node_description(const mforms::TreeNodeRef &node)
{
grt::GRT *grt = _context->get_grt();
std::string value;
if (node->get_parent() == _modules_tree.root_node())
{
std::string name = node->get_string(0);
if (!name.empty() && name[name.size()-1] == '*')
name = name.substr(0, name.size()-2);
grt::Module *module= grt->get_module(name);
if (module)
{
std::string descr;
descr.append("Module: "+module->name()+"\n");
descr.append("Path: "+module->path()+"\n");
descr.append("Language: "+module->get_loader()->get_loader_name()+"\n");
descr.append("Extends: "+module->extends()+"\n");
descr.append("Implements: ");
for (std::vector<std::string>::const_iterator iter= module->get_interfaces().begin();
iter != module->get_interfaces().end(); ++iter)
{
descr.append(*iter).append("\n");
}
descr.append("\n\n").append(module->description());
value = descr;
}
}
else
{
std::string name = node->get_parent()->get_string(0);
if (!name.empty() && name[name.size()-1] == '*')
name = name.substr(0, name.size()-2);
grt::Module *module= grt->get_module(name);
if (module)
{
const grt::Module::Function* func= module->get_function(node->get_string(0));
value= base::strfmt("Function:\n %s %s(%s)\n\n", fmt_type_spec(func->ret_type).c_str(), func->name.c_str(), fmt_arg_spec_list(func->arg_types).c_str());
value.append("Arguments:\n");
std::string args;
for (grt::ArgSpecList::const_iterator arg= func->arg_types.begin();
arg != func->arg_types.end(); ++arg)
{
if (!arg->name.empty())
args.append(" - ").append(arg->name).append(": ").append(arg->doc).append("\n");
else
args.append(" - ").append(fmt_type_spec(arg->type)).append("\n");
}
value.append(args);
value.append("\n").append(func->description);
}
}
return value;
}
//--------------------------------------------------------------------------------------------------
void GRTShellWindow::refresh_classes_tree()
{
_classes_tree.clear();
switch (_classes_sorting.get_selected_index())
{
case 0:
refresh_classes_tree_by_name();
break;
case 1:
refresh_classes_tree_by_hierarchy();
break;
case 2:
refresh_classes_tree_by_package();
break;
}
}
static std::string struct_member_icon(grt::TypeSpec type)
{
IconManager *im = IconManager::get_instance();
switch (type.base.type)
{
case grt::ListType:
return im->get_icon_path("grt_list.png");
case grt::DictType:
return im->get_icon_path("grt_dict.png");
case grt::ObjectType:
return im->get_icon_path("grt_object.png");
default:
return im->get_icon_path("grt_simple_type.png");
}
return im->get_icon_path("grt_simple_type.png");
}
struct SortableClassMember {
std::string name;
std::string caption;
std::string type;
std::string icon;
std::string tag;
bool operator < (const SortableClassMember &o) const
{
return name < o.name;
}
};
static void scan_class_members(mforms::TreeNodeRef node, grt::MetaClass *gstruct)
{
IconManager *im = IconManager::get_instance();
std::vector<SortableClassMember> members;
for (grt::MetaClass::MethodList::const_iterator mem= gstruct->get_methods_partial().begin();
mem != gstruct->get_methods_partial().end(); ++mem)
{
SortableClassMember m;
m.name = mem->second.name;
m.caption = gstruct->get_member_attribute(mem->second.name, "caption");
m.type = mem->second.ret_type.base.type == grt::AnyType ? "void" : grt::fmt_type_spec(mem->second.ret_type);
m.icon = im->get_icon_path("grt_function.png");
std::string value;
value= base::strfmt("Function:\n %s %s(%s)\n",
m.type.c_str(),
mem->second.name.c_str(), fmt_arg_spec_list(mem->second.arg_types).c_str());
value.append(gstruct->get_member_attribute(mem->second.name, "caption")).append("\n");
value.append("Arguments:\n");
std::string args;
for (grt::ArgSpecList::const_iterator arg= mem->second.arg_types.begin();
arg != mem->second.arg_types.end(); ++arg)
{
if (!arg->name.empty())
args.append(" - ").append(arg->name).append(": ").append(arg->doc).append("\n");
else
args.append(" - ").append(fmt_type_spec(arg->type)).append("\n");
}
value.append(args);
value.append("\n").append(gstruct->get_member_attribute(mem->second.name, "desc"));
m.tag = value;
members.push_back(m);
}
for (grt::MetaClass::MemberList::const_iterator mem= gstruct->get_members_partial().begin();
mem != gstruct->get_members_partial().end(); ++mem)
{
SortableClassMember m;
m.name = mem->second.name;
m.caption = gstruct->get_member_attribute(mem->second.name, "caption");
m.type = grt::fmt_type_spec(mem->second.type);
m.icon = struct_member_icon(mem->second.type);
m.tag = base::strfmt("Member:\n %s %s\n%s\n\n", m.type.c_str(), m.name.c_str(),
gstruct->get_member_attribute(mem->second.name, "desc").c_str());
members.push_back(m);
}
std::sort(members.begin(), members.end());
for (std::vector<SortableClassMember>::const_iterator i = members.begin(); i != members.end(); ++i)
{
mforms::TreeNodeRef mnode = node->add_child();
mnode->set_string(0, i->name);
mnode->set_string(1, i->type);
mnode->set_string(2, i->caption);
mnode->set_icon_path(0, i->icon);
mnode->set_tag(i->tag);
}
}
void GRTShellWindow::refresh_classes_tree_by_name()
{
std::list<grt::MetaClass*> metaclasses(_context->get_grt()->get_metaclasses());
std::string struct_icon = IconManager::get_instance()->get_icon_path("grt_struct.png");
metaclasses.sort(CompareNamedObject<grt::MetaClass>());
for (std::list<grt::MetaClass*>::const_iterator iter= metaclasses.begin();
iter != metaclasses.end(); ++iter)
{
grt::MetaClass *gstruct= *iter;
mforms::TreeNodeRef node;
node = _classes_tree.add_node();
node->set_tag(base::strfmt("Class:\n %s %s\n\n%s", gstruct->name().c_str(),
gstruct->parent() ? base::strfmt("(%s)", gstruct->parent()->name().c_str()).c_str() : "",
(*iter)->get_attribute("desc").c_str()));
node->set_string(0, gstruct->name());
node->set_string(2, gstruct->get_attribute("caption"));
node->set_icon_path(0, struct_icon);
scan_class_members(node, gstruct);
}
}
static void scan_subclasses(const std::list<grt::MetaClass*> &metaclasses, mforms::TreeNodeRef parnode, grt::MetaClass *parent)
{
std::string struct_icon = IconManager::get_instance()->get_icon_path("grt_struct.png");
for (std::list<grt::MetaClass*>::const_iterator iter= metaclasses.begin();
iter != metaclasses.end(); ++iter)
{
mforms::TreeNodeRef node;
if ((*iter)->parent() != parent)
continue;
node = parnode->add_child();
node->set_tag((*iter)->get_attribute("desc"));
node->set_string(0, (*iter)->name());
node->set_string(2, (*iter)->get_attribute("caption"));
node->set_icon_path(0, struct_icon);
scan_class_members(node, *iter);
// add child structs
scan_subclasses(metaclasses, node, *iter);
}
}
void GRTShellWindow::refresh_classes_tree_by_hierarchy()
{
std::list<grt::MetaClass*> metaclasses(_context->get_grt()->get_metaclasses());
metaclasses.sort(CompareNamedObject<grt::MetaClass>());
scan_subclasses(metaclasses, _classes_tree.root_node(), _context->get_grt()->get_metaclass(grt::internal::Object::static_class_name()));
}
void GRTShellWindow::refresh_classes_tree_by_package()
{
IconManager *im = IconManager::get_instance();
std::map<std::string, mforms::TreeNodeRef> package_nodes;
std::list<grt::MetaClass*> metaclasses(_context->get_grt()->get_metaclasses());
metaclasses.sort(CompareNamedObject<grt::MetaClass>());
std::string struct_icon = im->get_icon_path("grt_struct.png");
for (std::list<grt::MetaClass*>::const_iterator iter= metaclasses.begin();
iter != metaclasses.end(); ++iter)
{
std::string pkgname= (*iter)->name();
std::string::size_type p= pkgname.rfind('.');
if (p != std::string::npos)
pkgname= pkgname.substr(0, p);
else
pkgname= "";
mforms::TreeNodeRef pkgnode= package_nodes[pkgname];
if (!pkgnode)
{
pkgnode= _classes_tree.add_node();
pkgnode->set_string(0, pkgname);
pkgnode->set_icon_path(0, "folder");
package_nodes[pkgname]= pkgnode;
}
mforms::TreeNodeRef node= pkgnode->add_child();
node->set_tag((*iter)->get_attribute("desc"));
node->set_string(0, (*iter)->name());
node->set_string(2, (*iter)->get_attribute("caption"));
node->set_icon_path(0, struct_icon);
scan_class_members(node, *iter);
}
}
std::string GRTShellWindow::get_class_node_description(const mforms::TreeNodeRef& selected)
{
return selected->get_tag();
}
//--------------------------------------------------------------------------------------------------
static bool find_expandable_member(const grt::MetaClass::Member *member, bool *expandable)
{
if (!grt::is_simple_type(member->type.base.type))
*expandable = true;
return !*expandable;
}
static void globals_get_node_info(const grt::ValueRef &value,
std::string &type,
std::string &icon,
bool &expandable)
{
IconManager *im = IconManager::get_instance();
type= grt::type_to_str(value.type());
expandable = false;
switch (value.type())
{
case grt::ListType:
{
grt::BaseListRef l(grt::BaseListRef::cast_from(value));
std::string struct_name;
if (l.content_type() != grt::AnyType)
{
type+= " [";
if (l.content_type() == grt::ObjectType)
{
if (l.content_class_name().empty())
{
type+= "object";
struct_name= "";
}
else
{
type+= "object:"+l.content_class_name();
struct_name= l.content_class_name();
}
}
else
{
if (l.content_type() == grt::AnyType)
type+= "*";
else
type+= grt::type_to_str(l.content_type());
}
type+= "]";
}
if (!struct_name.empty())
icon= im->get_icon_path(im->get_icon_id(l.get_grt()->get_metaclass(struct_name),
Icon16, "many_$"));
if (icon.empty())
icon= im->get_icon_path("grt_list.png");
for (size_t c= l.count(), i= 0; i < c; i++)
{
if (!grt::is_simple_type(l[i].type()))
{
expandable = true;
break;
}
}
}
break;
case grt::DictType:
{
grt::DictRef d(grt::DictRef::cast_from(value));
if (d.content_type() != grt::AnyType)
{
type+= " [";
if (d.content_type() == grt::ObjectType)
{
type+= "object:"+d.content_class_name();
icon= im->get_icon_path(im->get_icon_id(d.get_grt()->get_metaclass(d.content_class_name()), Icon16));
}
else
type+= grt::type_to_str(d.content_type());
type+= "]";
}
if (icon.empty())
icon= im->get_icon_path("grt_dict.png");
for (grt::DictRef::const_iterator iter= d.begin(); iter != d.end(); ++iter)
{
if (!grt::is_simple_type(iter->second.type()))
{
expandable = true;
break;
}
}
}
break;
case grt::ObjectType:
{
grt::ObjectRef o(grt::ObjectRef::cast_from(value));
type+= ":" + std::string(o.class_name());
icon= im->get_icon_path(im->get_icon_id(o, Icon16));
if (icon.empty())
icon= im->get_icon_path("grt_object.png");
grt::MetaClass *meta= o.get_metaclass();
meta->foreach_member(boost::bind(&find_expandable_member, _1, &expandable));
}
break;
default:
icon= im->get_icon_path("grt_simple_type.png");
break;
}
}
static void globals_rescan_list(mforms::TreeNodeRef &node,
const std::string &path,
const grt::BaseListRef &value)
{
char buffer[30];
node->remove_children();
for (size_t i = 0; i < value.count(); i++)
{
grt::ValueRef v= value.get(i);
std::string label;
sprintf(buffer, "%lu", (long unsigned)i);
if (v.is_valid() && !grt::is_simple_type(v.type()))
{
mforms::TreeNodeRef child = node->add_child();
std::string type;
std::string icon;
bool expandable;
globals_get_node_info(v, type, icon, expandable);
child->set_tag(buffer);
child->set_string(0, label.empty() ? buffer : label);
child->set_string(1, type);
child->set_icon_path(0, icon);
if (v.type() == grt::ObjectType && label.empty())
{
grt::ObjectRef o(grt::ObjectRef::cast_from(v));
std::string s = std::string("[")+buffer+"]";
try
{
if (o.has_member("name") && o.get_string_member("name") != "")
s.append(" ").append(o.get_string_member("name"));
}
catch (grt::type_error)
{
s.append(" ").append("?");
}
child->set_string(0, s);
}
if (expandable)
child->add_child();
}
}
}
static void globals_rescan_dict(mforms::TreeNodeRef &node,
const std::string &path,
const grt::DictRef &value)
{
node->remove_children();
for (grt::DictRef::const_iterator item= value.begin(); item != value.end(); ++item)
{
std::string key(item->first);
grt::ValueRef v(item->second);
std::string label;
if (v.is_valid() && !grt::is_simple_type(v.type()))
{
mforms::TreeNodeRef child= node->add_child();
std::string type;
std::string icon;
bool expandable;
globals_get_node_info(v, type, icon, expandable);
child->set_tag(key);
child->set_string(0, label.empty() ? key : label);
child->set_string(1, type);
child->set_icon_path(0, icon);
if (v.type() == grt::ObjectType && label.empty())
{
grt::ObjectRef o(grt::ObjectRef::cast_from(v));
if (o.has_member("name") && o.get_string_member("name") != "")
child->set_string(0, o.get_string_member("name"));
else
child->set_string(0, "["+child->get_tag()+"]");
}
if (expandable)
child->add_child();
}
}
}
static bool globals_rescan_member(const grt::MetaClass::Member *mem, mforms::TreeNodeRef &node, const grt::ObjectRef &value)
{
std::string name(mem->name);
grt::ValueRef v(value.get_member(name));
std::string label;
if (v.is_valid() && !is_simple_type(v.type()))
{
mforms::TreeNodeRef child = node->add_child();
std::string type;
std::string icon;
bool expandable;
globals_get_node_info(v, type, icon, expandable);
child->set_tag(name);
child->set_string(0, label.empty() ? name : label);
child->set_string(1, type);
child->set_icon_path(0, icon);
if (expandable)
child->add_child();
}
return true;
}
static void globals_rescan_object(mforms::TreeNodeRef &node,
const std::string &path,
const grt::ObjectRef &value)
{
grt::MetaClass *meta= value.get_metaclass();
node->remove_children();
meta->foreach_member(boost::bind(&globals_rescan_member, _1, node, value));
}
static void globals_rescan_value(mforms::TreeNodeRef &node,
const std::string &path,
const grt::ValueRef &value)
{
switch (value.type())
{
case grt::ListType:
globals_rescan_list(node, path, grt::BaseListRef::cast_from(value));
break;
case grt::DictType:
globals_rescan_dict(node, path, grt::DictRef::cast_from(value));
break;
case grt::ObjectType:
globals_rescan_object(node, path, grt::ObjectRef::cast_from(value));
break;
default:
break;
}
}
void GRTShellWindow::refresh_globals_tree()
{
std::string path = _global_combo.get_string_value();
if (path.empty())
path = "/";
try
{
grt::ValueRef value = _context->get_grt()->get(path);
if (value.is_valid())
{
_global_tree.clear();
mforms::TreeNodeRef root = _global_tree.add_node();
std::string type;
std::string icon;
bool expandable;
globals_get_node_info(value, type, icon, expandable);
root->set_string(0, path);
root->set_string(1, type);
root->set_icon_path(0, icon);
root->set_tag(path);
globals_rescan_value(root, path, value);
// root->expand();
}
}
catch (const grt::bad_item)
{
// ignore
}
}
void GRTShellWindow::globals_expand_toggle(const mforms::TreeNodeRef &node, bool expanded)
{
if (expanded)
{
grt::ValueRef value = get_global_at_node(node);
if (value.is_valid())
{
mforms::TreeNodeRef mnode = node;
globals_rescan_value(mnode, mnode->get_tag(), value);
}
}
}
grt::ValueRef GRTShellWindow::get_global_at_node(const mforms::TreeNodeRef &node)
{
return _context->get_grt()->get(get_global_path_at_node(node));
}
std::string GRTShellWindow::get_global_path_at_node(const mforms::TreeNodeRef &node)
{
std::string path;
mforms::TreeNodeRef parent = node;
while (parent != _global_tree.root_node())
{
if (parent->get_tag() == "/")
path = "/"+path;
else
{
if (path.empty())
path = parent->get_tag();
else
path = parent->get_tag()+"/"+path;
}
parent = parent->get_parent();
}
return path;
}
void GRTShellWindow::refresh_global_list()
{
_global_list.clear();
if (_inspector)
{
for (size_t c = _inspector->count(), i = 0; i < c; i++)
{
mforms::TreeNodeRef node = _global_list.add_node();
std::string value;
_inspector->get_field(i, 0, value);
node->set_string(0, value);
_inspector->get_field(i, 1, value);
node->set_string(1, value);
value = IconManager::get_instance()->get_icon_path(_inspector->get_field_icon(i, 0, Icon16));
node->set_icon_path(0, value);
}
}
}
void GRTShellWindow::refresh_notifs_list()
{
const std::map<std::string, base::NotificationCenter::NotificationHelp> &info = base::NotificationCenter::get()->get_registered_notifications();
std::map<std::string, std::vector<std::string> > contexts;
_notifs_tree.clear();
for (std::map<std::string, base::NotificationCenter::NotificationHelp>::const_iterator i = info.begin();
i != info.end(); ++i)
contexts[i->second.context].push_back(i->first);
for (std::map<std::string, std::vector<std::string> > ::const_iterator iter = contexts.begin();
iter != contexts.end(); ++iter)
{
mforms::TreeNodeRef node = _notifs_tree.add_node();
node->set_string(0, iter->first);
node->set_icon_path(0, "folder");
for (std::vector<std::string>::const_iterator n = iter->second.begin(); n != iter->second.end(); ++n)
{
mforms::TreeNodeRef nnode = node->add_child();
nnode->set_string(0, *n);
}
node->expand();
}
}
void GRTShellWindow::cut()
{
GRTCodeEditor *editor = get_active_editor();
if (editor)
editor->get_editor()->cut();
else if (_shell_entry.has_focus())
_shell_entry.cut();
}
void GRTShellWindow::copy()
{
GRTCodeEditor *editor = get_active_editor();
if (editor)
editor->get_editor()->copy();
else if (_shell_entry.has_focus())
_shell_entry.copy();
}
void GRTShellWindow::paste()
{
GRTCodeEditor *editor = get_active_editor();
if (editor)
editor->get_editor()->paste();
else if (_shell_entry.has_focus())
_shell_entry.paste();
}
void GRTShellWindow::select_all()
{
GRTCodeEditor *editor = get_active_editor();
if (editor)
editor->get_editor()->select_all();
else if (_shell_entry.has_focus())
_shell_entry.select(base::Range(0, (size_t)-1));
}
|