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
|
/* gtkbuilderparser.c
* Copyright (C) 2006-2007 Async Open Source,
* Johan Dahlin <jdahlin@async.com.br>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gtkbuilderprivate.h"
#include "gtkbuildableprivate.h"
#include "gtkbuilderscopeprivate.h"
#include "gtkdebug.h"
#include "gtktypebuiltins.h"
#include "gtkversion.h"
#include "gdkprofilerprivate.h"
#include "gtkprivate.h"
#include <gio/gio.h>
#include <string.h>
typedef struct
{
const GtkBuildableParser *last_parser;
gpointer last_user_data;
int last_depth;
} GtkBuildableParserStack;
static void
pop_subparser_stack (GtkBuildableParseContext *context)
{
GtkBuildableParserStack *stack = &g_array_index (context->subparser_stack, GtkBuildableParserStack,
context->subparser_stack->len - 1);
context->awaiting_pop = TRUE;
context->held_user_data = context->user_data;
context->user_data = stack->last_user_data;
context->parser = stack->last_parser;
g_array_set_size (context->subparser_stack, context->subparser_stack->len - 1);
}
static void
possibly_finish_subparser (GtkBuildableParseContext *context)
{
GtkBuildableParserStack *stack;
if (!context->subparser_stack ||
context->subparser_stack->len == 0)
return;
stack = &g_array_index (context->subparser_stack, GtkBuildableParserStack,
context->subparser_stack->len - 1);
if (stack->last_depth == context->tag_stack->len)
pop_subparser_stack (context);
}
static void
proxy_start_element (GMarkupParseContext *gm_context,
const char *element_name,
const char **attribute_names,
const char **attribute_values,
gpointer user_data,
GError **error)
{
GtkBuildableParseContext *context = user_data;
// Due to the way GMarkup works we're sure this will live until the end_element callback
g_ptr_array_add (context->tag_stack, (char *)element_name);
if (context->parser->start_element)
context->parser->start_element (context, element_name,
attribute_names, attribute_values,
context->user_data, error);
}
static void
proxy_end_element (GMarkupParseContext *gm_context,
const char *element_name,
gpointer user_data,
GError **error)
{
GtkBuildableParseContext *context = user_data;
possibly_finish_subparser (context);
if (context->parser->end_element)
context->parser->end_element (context, element_name, context->user_data, error);
g_ptr_array_set_size (context->tag_stack, context->tag_stack->len - 1);
}
static void
proxy_text (GMarkupParseContext *gm_context,
const char *text,
gsize text_len,
gpointer user_data,
GError **error)
{
GtkBuildableParseContext *context = user_data;
if (context->parser->text)
context->parser->text (context, text, text_len, context->user_data, error);
}
static void
proxy_error (GMarkupParseContext *gm_context,
GError *error,
gpointer user_data)
{
GtkBuildableParseContext *context = user_data;
if (context->parser->error)
context->parser->error (context, error, context->user_data);
/* report the error all the way up to free all the user-data */
if (!context->subparser_stack)
return;
while (context->subparser_stack->len > 0)
{
pop_subparser_stack (context);
context->awaiting_pop = FALSE; /* already been freed */
if (context->parser->error)
context->parser->error (context, error, context->user_data);
}
}
static const GMarkupParser gmarkup_parser = {
proxy_start_element,
proxy_end_element,
proxy_text,
NULL,
proxy_error,
};
static void
gtk_buildable_parse_context_init (GtkBuildableParseContext *context,
const GtkBuildableParser *parser,
gpointer user_data)
{
context->internal_callbacks = &gmarkup_parser;
context->ctx = NULL;
context->parser = parser;
context->user_data = user_data;
context->subparser_stack = NULL;
context->tag_stack = g_ptr_array_new ();
context->held_user_data = NULL;
context->awaiting_pop = FALSE;
}
static void
gtk_buildable_parse_context_free (GtkBuildableParseContext *context)
{
if (context->subparser_stack)
g_array_unref (context->subparser_stack);
g_ptr_array_unref (context->tag_stack);
}
static gboolean
gtk_buildable_parse_context_parse (GtkBuildableParseContext *context,
const char *text,
gssize text_len,
GError **error)
{
gboolean res;
if (_gtk_buildable_parser_is_precompiled (text, text_len))
{
res = _gtk_buildable_parser_replay_precompiled (context, text, text_len, error);
}
else
{
context->ctx = g_markup_parse_context_new (context->internal_callbacks,
G_MARKUP_TREAT_CDATA_AS_TEXT,
context, NULL);
res = g_markup_parse_context_parse (context->ctx, text, text_len, error);
g_markup_parse_context_free (context->ctx);
}
return res;
}
/**
* gtk_buildable_parse_context_push:
* @context: a `GtkBuildableParseContext`
* @parser: a `GtkBuildableParser`
* @user_data: user data to pass to `GtkBuildableParser` functions
*
* Temporarily redirects markup data to a sub-parser.
*
* This function may only be called from the start_element handler of
* a `GtkBuildableParser`. It must be matched with a corresponding call to
* gtk_buildable_parse_context_pop() in the matching end_element handler
* (except in the case that the parser aborts due to an error).
*
* All tags, text and other data between the matching tags is
* redirected to the subparser given by @parser. @user_data is used
* as the user_data for that parser. @user_data is also passed to the
* error callback in the event that an error occurs. This includes
* errors that occur in subparsers of the subparser.
*
* The end tag matching the start tag for which this call was made is
* handled by the previous parser (which is given its own user_data)
* which is why gtk_buildable_parse_context_pop() is provided to allow "one
* last access" to the @user_data provided to this function. In the
* case of error, the @user_data provided here is passed directly to
* the error callback of the subparser and gtk_buildable_parse_context_pop()
* should not be called. In either case, if @user_data was allocated
* then it ought to be freed from both of these locations.
*
* This function is not intended to be directly called by users
* interested in invoking subparsers. Instead, it is intended to be
* used by the subparsers themselves to implement a higher-level
* interface.
*
* For an example of how to use this, see g_markup_parse_context_push() which
* has the same kind of API.
**/
void
gtk_buildable_parse_context_push (GtkBuildableParseContext *context,
const GtkBuildableParser *parser,
gpointer user_data)
{
GtkBuildableParserStack stack = { 0 };
stack.last_parser = context->parser;
stack.last_user_data = context->user_data;
stack.last_depth = context->tag_stack->len; // If at end_element time we're this deep, then pop it
context->parser = parser;
context->user_data = user_data;
if (!context->subparser_stack)
context->subparser_stack = g_array_new (FALSE, FALSE, sizeof (GtkBuildableParserStack));
g_array_append_val (context->subparser_stack, stack);
}
/**
* gtk_buildable_parse_context_pop:
* @context: a `GtkBuildableParseContext`
*
* Completes the process of a temporary sub-parser redirection.
*
* This function exists to collect the user_data allocated by a
* matching call to gtk_buildable_parse_context_push(). It must be called
* in the end_element handler corresponding to the start_element
* handler during which gtk_buildable_parse_context_push() was called.
* You must not call this function from the error callback -- the
* @user_data is provided directly to the callback in that case.
*
* This function is not intended to be directly called by users
* interested in invoking subparsers. Instead, it is intended to
* be used by the subparsers themselves to implement a higher-level
* interface.
*
* Returns: the user data passed to gtk_buildable_parse_context_push()
*/
gpointer
gtk_buildable_parse_context_pop (GtkBuildableParseContext *context)
{
gpointer user_data;
if (!context->awaiting_pop)
possibly_finish_subparser (context);
g_assert (context->awaiting_pop);
context->awaiting_pop = FALSE;
user_data = context->held_user_data;
context->held_user_data = NULL;
return user_data;
}
/**
* gtk_buildable_parse_context_get_element:
* @context: a `GtkBuildablParseContext`
*
* Retrieves the name of the currently open element.
*
* If called from the start_element or end_element handlers this will
* give the element_name as passed to those functions. For the parent
* elements, see gtk_buildable_parse_context_get_element_stack().
*
* Returns: (nullable): the name of the currently open element
*/
const char *
gtk_buildable_parse_context_get_element (GtkBuildableParseContext *context)
{
if (context->tag_stack->len > 0)
return g_ptr_array_index (context->tag_stack, context->tag_stack->len - 1);
return NULL;
}
/**
* gtk_buildable_parse_context_get_element_stack:
* @context: a `GtkBuildableParseContext`
*
* Retrieves the element stack from the internal state of the parser.
*
* The returned `GPtrArray` is an array of strings where the last item is
* the currently open tag (as would be returned by
* gtk_buildable_parse_context_get_element()) and the previous item is its
* immediate parent.
*
* This function is intended to be used in the start_element and
* end_element handlers where gtk_buildable_parse_context_get_element()
* would merely return the name of the element that is being
* processed.
*
* Returns: (transfer none) (element-type utf8): the element stack, which must not be modified
*/
GPtrArray *
gtk_buildable_parse_context_get_element_stack (GtkBuildableParseContext *context)
{
return context->tag_stack;
}
/**
* gtk_buildable_parse_context_get_position:
* @context: a `GtkBuildableParseContext`
* @line_number: (out) (optional): return location for a line number
* @char_number: (out) (optional): return location for a char-on-line number
*
* Retrieves the current line number and the number of the character on
* that line. Intended for use in error messages; there are no strict
* semantics for what constitutes the "current" line number other than
* "the best number we could come up with for error messages."
*/
void
gtk_buildable_parse_context_get_position (GtkBuildableParseContext *context,
int *line_number,
int *char_number)
{
if (context->ctx)
g_markup_parse_context_get_position (context->ctx, line_number, char_number);
else
{
if (line_number)
*line_number = 0;
if (char_number)
*char_number = 0;
}
}
static void free_property_info (PropertyInfo *info);
static void free_object_info (ObjectInfo *info);
static inline void
state_push (ParserData *data, gpointer info)
{
g_ptr_array_add (data->stack, info);
}
static inline gpointer
state_peek (ParserData *data)
{
if (!data->stack ||
data->stack->len == 0)
return NULL;
return g_ptr_array_index (data->stack, data->stack->len - 1);
}
static inline gpointer
state_pop (ParserData *data)
{
gpointer old = NULL;
g_assert (data->stack);
old = state_peek (data);
g_assert (old);
data->stack->len --;
return old;
}
#define state_peek_info(data, st) ((st*)state_peek(data))
#define state_pop_info(data, st) ((st*)state_pop(data))
static void
error_missing_attribute (ParserData *data,
const char *tag,
const char *attribute,
GError **error)
{
int line, col;
gtk_buildable_parse_context_get_position (&data->ctx, &line, &col);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
"%s:%d:%d <%s> requires attribute '%s'",
data->filename, line, col, tag, attribute);
}
static void
error_invalid_tag (ParserData *data,
const char *tag,
const char *expected,
GError **error)
{
int line, col;
gtk_buildable_parse_context_get_position (&data->ctx, &line, &col);
if (expected)
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_TAG,
"%s:%d:%d <%s> is not a valid tag here, expected a <%s> tag",
data->filename, line, col, tag, expected);
else
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_TAG,
"%s:%d:%d <%s> is not a valid tag here",
data->filename, line, col, tag);
}
static void
error_unhandled_tag (ParserData *data,
const char *tag,
GError **error)
{
int line, col;
gtk_buildable_parse_context_get_position (&data->ctx, &line, &col);
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_UNHANDLED_TAG,
"%s:%d:%d Unhandled tag: <%s>",
data->filename, line, col, tag);
}
static GObject *
builder_construct (ParserData *data,
ObjectInfo *object_info,
GError **error)
{
GObject *object;
g_assert (object_info != NULL);
if (object_info->object == NULL)
{
object = _gtk_builder_construct (data->builder, object_info, error);
if (!object)
return NULL;
}
else
{
/* We're building a template, the object is already set and
* we just want to resolve the properties at the right time
*/
object = object_info->object;
_gtk_builder_apply_properties (data->builder, object_info, error);
}
g_assert (G_IS_OBJECT (object));
object_info->object = object;
return object;
}
static void
parse_requires (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
RequiresInfo *req_info;
const char *library = NULL;
const char *version = NULL;
char **split;
int version_major = 0;
int version_minor = 0;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "lib", &library,
G_MARKUP_COLLECT_STRING, "version", &version,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (!(split = g_strsplit (version, ".", 2)) || !split[0] || !split[1])
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"'version' attribute has malformed value '%s'", version);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
version_major = g_ascii_strtoll (split[0], NULL, 10);
version_minor = g_ascii_strtoll (split[1], NULL, 10);
g_strfreev (split);
req_info = g_new0 (RequiresInfo, 1);
req_info->library = g_strdup (library);
req_info->major = version_major;
req_info->minor = version_minor;
state_push (data, req_info);
req_info->tag_type = TAG_REQUIRES;
}
static gboolean
is_requested_object (const char *object,
ParserData *data)
{
int i;
for (i = 0; data->requested_objects[i]; ++i)
{
if (g_strcmp0 (data->requested_objects[i], object) == 0)
return TRUE;
}
return FALSE;
}
static void
parse_object (GtkBuildableParseContext *context,
ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
ObjectInfo *object_info;
ChildInfo* child_info;
GType object_type = G_TYPE_INVALID;
const char *object_class = NULL;
const char *constructor = NULL;
const char *type_func = NULL;
const char *object_id = NULL;
char *internal_id = NULL;
int line;
gpointer line_ptr;
gboolean has_duplicate;
child_info = state_peek_info (data, ChildInfo);
if (child_info && child_info->tag_type == TAG_OBJECT)
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
/* Even though 'class' is a mandatory attribute, we don't flag its
* absence here because it's supposed to throw
* GTK_BUILDER_ERROR_MISSING_ATTRIBUTE, not
* G_MARKUP_ERROR_MISSING_ATTRIBUTE. It's handled immediately
* afterwards.
*/
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "class", &object_class,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "constructor", &constructor,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "type-func", &type_func,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "id", &object_id,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (!object_class)
{
error_missing_attribute (data, element_name, "class", error);
return;
}
if (type_func)
{
/* Call the GType function, and return the GType, it's guaranteed afterwards
* that g_type_from_name on the name will return our GType
*/
object_type = gtk_builder_scope_get_type_from_function (gtk_builder_get_scope (data->builder), data->builder, type_func);
if (object_type == G_TYPE_INVALID)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_TYPE_FUNCTION,
"Invalid type function '%s'", type_func);
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
}
else
{
g_assert_nonnull (object_class);
object_type = gtk_builder_get_type_from_name (data->builder, object_class);
if (object_type == G_TYPE_INVALID)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Invalid object type '%s'", object_class);
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
}
if (!object_id)
{
internal_id = g_strdup_printf ("___object_%d___", ++data->object_counter);
object_id = internal_id;
}
++data->cur_object_level;
/* check if we reached a requested object (if it is specified) */
if (data->requested_objects && !data->inside_requested_object)
{
if (is_requested_object (object_id, data))
{
data->requested_object_level = data->cur_object_level;
GTK_DEBUG (BUILDER_TRACE, "requested object \"%s\" found at level %d",
object_id, data->requested_object_level);
data->inside_requested_object = TRUE;
}
else
{
g_free (internal_id);
return;
}
}
object_info = g_new0 (ObjectInfo, 1);
object_info->tag_type = TAG_OBJECT;
object_info->type = object_type;
object_info->oclass = g_type_class_ref (object_type);
object_info->id = (internal_id) ? internal_id : g_strdup (object_id);
object_info->constructor = g_strdup (constructor);
object_info->parent = (CommonInfo*)child_info;
state_push (data, object_info);
has_duplicate = g_hash_table_lookup_extended (data->object_ids, object_id, NULL, &line_ptr);
if (has_duplicate != 0)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_DUPLICATE_ID,
"Duplicate object ID '%s' (previously on line %d)",
object_id, GPOINTER_TO_INT (line_ptr));
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
gtk_buildable_parse_context_get_position (context, &line, NULL);
g_hash_table_insert (data->object_ids, g_strdup (object_id), GINT_TO_POINTER (line));
}
static void
parse_template (GtkBuildableParseContext *context,
ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
ObjectInfo *object_info;
const char *object_class = NULL;
const char *parent_class = NULL;
int line;
gpointer line_ptr;
gboolean has_duplicate, allow_parents;
GType template_type;
GType parsed_type;
template_type = gtk_builder_get_template_type (data->builder, &allow_parents);
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "class", &object_class,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "parent", &parent_class,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (template_type == 0)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_UNHANDLED_TAG,
"Template declaration (class '%s', parent '%s') where templates aren't supported",
object_class, parent_class ? parent_class : "GtkWidget");
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
else if (state_peek (data) != NULL)
{
error_invalid_tag (data, "template", NULL, error);
return;
}
parsed_type = g_type_from_name (object_class);
if (template_type != parsed_type &&
(!allow_parents || !g_type_is_a (template_type, parsed_type)))
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_TEMPLATE_MISMATCH,
"Parsed template definition for type '%s', expected type '%s'",
object_class, g_type_name (template_type));
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
if (parent_class)
{
GType parent_type = g_type_from_name (parent_class);
GType expected_type = g_type_parent (parsed_type);
if (parent_type == G_TYPE_INVALID)
{
g_set_error (error, GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Invalid template parent type '%s'", parent_class);
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
if (parent_type != expected_type)
{
g_set_error (error, GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_TEMPLATE_MISMATCH,
"Template parent type '%s' does not match instance parent type '%s'.",
parent_class, g_type_name (expected_type));
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
}
++data->cur_object_level;
object_info = g_new0 (ObjectInfo, 1);
object_info->tag_type = TAG_TEMPLATE;
object_info->object = gtk_builder_get_object (data->builder, object_class);
object_info->type = template_type;
object_info->oclass = g_type_class_ref (template_type);
object_info->id = g_strdup (object_class);
g_assert (object_info->object);
state_push (data, object_info);
has_duplicate = g_hash_table_lookup_extended (data->object_ids, object_class, NULL, &line_ptr);
if (has_duplicate != 0)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_DUPLICATE_ID,
"Duplicate object ID '%s' (previously on line %d)",
object_class, GPOINTER_TO_INT (line_ptr));
_gtk_builder_prefix_error (data->builder, context, error);
return;
}
gtk_buildable_parse_context_get_position (context, &line, NULL);
g_hash_table_insert (data->object_ids, g_strdup (object_class), GINT_TO_POINTER (line));
}
static void
free_object_info (ObjectInfo *info)
{
/* Do not free the signal items, which GtkBuilder takes ownership of */
g_type_class_unref (info->oclass);
if (info->signals)
g_ptr_array_free (info->signals, TRUE);
if (info->properties)
g_ptr_array_free (info->properties, TRUE);
g_free (info->constructor);
g_free (info->id);
g_free (info);
}
static void
parse_child (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
ObjectInfo* object_info;
ChildInfo *child_info;
const char *type = NULL;
const char *internal_child = NULL;
object_info = state_peek_info (data, ObjectInfo);
if (!object_info ||
!(object_info->tag_type == TAG_OBJECT ||
object_info->tag_type == TAG_TEMPLATE))
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "type", &type,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "internal-child", &internal_child,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
child_info = g_new0 (ChildInfo, 1);
child_info->tag_type = TAG_CHILD;
child_info->type = g_strdup (type);
child_info->internal_child = g_strdup (internal_child);
child_info->parent = (CommonInfo*)object_info;
state_push (data, child_info);
object_info->object = builder_construct (data, object_info, error);
}
static void
free_child_info (ChildInfo *info)
{
g_free (info->type);
g_free (info->internal_child);
g_free (info);
}
static void
parse_property (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
PropertyInfo *info;
const char *name = NULL;
const char *context = NULL;
const char *bind_source = NULL;
const char *bind_property = NULL;
const char *bind_flags_str = NULL;
GBindingFlags bind_flags = G_BINDING_DEFAULT;
gboolean translatable = FALSE;
const char *translatable_string = NULL;
ObjectInfo *object_info;
GParamSpec *pspec = NULL;
int line, col;
object_info = state_peek_info (data, ObjectInfo);
if (!object_info ||
!(object_info->tag_type == TAG_OBJECT ||
object_info->tag_type == TAG_TEMPLATE))
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "name", &name,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "translatable", &translatable_string,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "comments", NULL,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "context", &context,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "bind-source", &bind_source,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "bind-property", &bind_property,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "bind-flags", &bind_flags_str,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
pspec = g_object_class_find_property (object_info->oclass, name);
if (!pspec)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_PROPERTY,
"Invalid property: %s.%s",
g_type_name (object_info->type), name);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (translatable_string &&
!gtk_builder_parse_translatable (translatable_string, &translatable, error))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (bind_flags_str)
{
guint flags;
if (!_gtk_builder_flags_from_string (G_TYPE_BINDING_FLAGS, bind_flags_str, &flags, error))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
bind_flags = flags;
}
gtk_buildable_parse_context_get_position (&data->ctx, &line, &col);
if (bind_source)
{
BindingInfo *binfo;
binfo = g_new0 (BindingInfo, 1);
binfo->tag_type = TAG_BINDING;
binfo->target = NULL;
binfo->target_pspec = pspec;
binfo->source = g_strdup (bind_source);
binfo->source_property = bind_property ? g_strdup (bind_property) : g_strdup (name);
binfo->flags = bind_flags;
binfo->line = line;
binfo->col = col;
object_info->bindings = g_slist_prepend (object_info->bindings, binfo);
}
else if (bind_property)
{
error_missing_attribute (data, element_name,
"bind-source",
error);
return;
}
info = g_new0 (PropertyInfo, 1);
info->tag_type = TAG_PROPERTY;
info->pspec = pspec;
info->text = g_string_new ("");
info->translatable = translatable;
info->bound = bind_source != NULL;
info->context = g_strdup (context);
info->line = line;
info->col = col;
state_push (data, info);
}
static void
parse_binding (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
BindingExpressionInfo *info;
const char *name = NULL;
const char *object_name = NULL;
ObjectInfo *object_info;
GParamSpec *pspec = NULL;
object_info = state_peek_info (data, ObjectInfo);
if (!object_info ||
!(object_info->tag_type == TAG_OBJECT ||
object_info->tag_type == TAG_TEMPLATE))
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "name", &name,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "object", &object_name,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
pspec = g_object_class_find_property (object_info->oclass, name);
if (!pspec)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_PROPERTY,
"Invalid property: %s.%s",
g_type_name (object_info->type), name);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
else if (pspec->flags & G_PARAM_CONSTRUCT_ONLY)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_PROPERTY,
"%s.%s is a construct-only property",
g_type_name (object_info->type), name);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
else if (!(pspec->flags & G_PARAM_WRITABLE))
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_PROPERTY,
"%s.%s is a non-writable property",
g_type_name (object_info->type), name);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
info = g_new0 (BindingExpressionInfo, 1);
info->tag_type = TAG_BINDING_EXPRESSION;
info->target = NULL;
info->target_pspec = pspec;
info->object_name = g_strdup (object_name);
gtk_buildable_parse_context_get_position (&data->ctx, &info->line, &info->col);
state_push (data, info);
}
static void
free_property_info (PropertyInfo *info)
{
if (info->value)
{
if (G_PARAM_SPEC_VALUE_TYPE (info->pspec) == GTK_TYPE_EXPRESSION)
gtk_expression_unref (info->value);
else
g_assert_not_reached();
}
g_string_free (info->text, TRUE);
g_free (info->context);
g_free (info);
}
static void
free_expression_info (ExpressionInfo *info)
{
switch (info->expression_type)
{
case EXPRESSION_EXPRESSION:
g_clear_pointer (&info->expression, gtk_expression_unref);
break;
case EXPRESSION_CONSTANT:
g_string_free (info->constant.text, TRUE);
g_free (info->constant.context);
break;
case EXPRESSION_CLOSURE:
g_free (info->closure.function_name);
g_free (info->closure.object_name);
g_slist_free_full (info->closure.params, (GDestroyNotify) free_expression_info);
break;
case EXPRESSION_PROPERTY:
g_clear_pointer (&info->property.expression, free_expression_info);
g_free (info->property.property_name);
break;
default:
g_assert_not_reached ();
break;
}
g_free (info);
}
static gboolean
check_expression_parent (ParserData *data)
{
CommonInfo *common_info = state_peek_info (data, CommonInfo);
if (common_info == NULL)
return FALSE;
if (common_info->tag_type == TAG_PROPERTY)
{
PropertyInfo *prop_info = (PropertyInfo *) common_info;
return G_PARAM_SPEC_VALUE_TYPE (prop_info->pspec) == GTK_TYPE_EXPRESSION;
}
else if (common_info->tag_type == TAG_BINDING_EXPRESSION)
{
BindingExpressionInfo *expr_info = (BindingExpressionInfo *) common_info;
return expr_info->expr == NULL;
}
else if (common_info->tag_type == TAG_EXPRESSION)
{
ExpressionInfo *expr_info = (ExpressionInfo *) common_info;
switch (expr_info->expression_type)
{
case EXPRESSION_CLOSURE:
return TRUE;
case EXPRESSION_CONSTANT:
return FALSE;
case EXPRESSION_PROPERTY:
return expr_info->property.expression == NULL;
case EXPRESSION_EXPRESSION:
default:
g_assert_not_reached ();
return FALSE;
}
}
return FALSE;
}
static void
parse_constant_expression (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
ExpressionInfo *info;
const char *type_name = NULL;
GType type;
gboolean translatable = FALSE;
const char *translatable_string = NULL;
const char *context = NULL;
if (!check_expression_parent (data))
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "type", &type_name,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "translatable", &translatable_string,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "comments", NULL,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "context", &context,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (type_name == NULL)
type = G_TYPE_INVALID;
else
{
type = gtk_builder_get_type_from_name (data->builder, type_name);
if (type == G_TYPE_INVALID)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Invalid type '%s'", type_name);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
}
if (translatable_string &&
!gtk_builder_parse_translatable (translatable_string, &translatable, error))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
info = g_new0 (ExpressionInfo, 1);
info->tag_type = TAG_EXPRESSION;
info->expression_type = EXPRESSION_CONSTANT;
info->constant.type = type;
info->constant.text = g_string_new (NULL);
info->constant.translatable = translatable;
info->constant.context = g_strdup (context);
state_push (data, info);
}
static void
parse_closure_expression (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
ExpressionInfo *info;
const char *type_name;
const char *function_name;
const char *object_name = NULL;
gboolean swapped = -1;
GType type;
if (!check_expression_parent (data))
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "type", &type_name,
G_MARKUP_COLLECT_STRING, "function", &function_name,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "object", &object_name,
G_MARKUP_COLLECT_TRISTATE|G_MARKUP_COLLECT_OPTIONAL, "swapped", &swapped,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
type = gtk_builder_get_type_from_name (data->builder, type_name);
if (type == G_TYPE_INVALID)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Invalid type '%s'", type_name);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
/* Swapped defaults to FALSE except when object is set */
if (swapped == -1)
{
if (object_name)
swapped = TRUE;
else
swapped = FALSE;
}
info = g_new0 (ExpressionInfo, 1);
info->tag_type = TAG_EXPRESSION;
info->expression_type = EXPRESSION_CLOSURE;
info->closure.type = type;
info->closure.swapped = swapped;
info->closure.function_name = g_strdup (function_name);
info->closure.object_name = g_strdup (object_name);
state_push (data, info);
}
static void
parse_lookup_expression (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
ExpressionInfo *info;
const char *property_name;
const char *type_name = NULL;
GType type;
if (!check_expression_parent (data))
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "type", &type_name,
G_MARKUP_COLLECT_STRING, "name", &property_name,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (type_name == NULL)
{
type = G_TYPE_INVALID;
}
else
{
type = gtk_builder_get_type_from_name (data->builder, type_name);
if (type == G_TYPE_INVALID)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_VALUE,
"Invalid type '%s'", type_name);
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
}
info = g_new0 (ExpressionInfo, 1);
info->tag_type = TAG_EXPRESSION;
info->expression_type = EXPRESSION_PROPERTY;
info->property.this_type = type;
info->property.property_name = g_strdup (property_name);
state_push (data, info);
}
GtkExpression *
expression_info_construct (GtkBuilder *builder,
const char *domain,
ExpressionInfo *info,
GError **error)
{
switch (info->expression_type)
{
case EXPRESSION_EXPRESSION:
break;
case EXPRESSION_CONSTANT:
{
GtkExpression *expr;
if (info->constant.translatable && info->constant.text->len)
{
const char *translated;
translated = _gtk_builder_parser_translate (domain,
info->constant.context,
info->constant.text->str);
g_string_assign (info->constant.text, translated);
}
if (info->constant.type == G_TYPE_INVALID)
{
GObject *o = gtk_builder_lookup_object (builder, info->constant.text->str, 0, 0, error);
if (o == NULL)
return NULL;
expr = gtk_object_expression_new (o);
}
else
{
GValue value = G_VALUE_INIT;
if (!gtk_builder_value_from_string_type (builder,
info->constant.type,
info->constant.text->str,
&value,
error))
return NULL;
if (G_VALUE_HOLDS_OBJECT (&value))
expr = gtk_object_expression_new (g_value_get_object (&value));
else
expr = gtk_constant_expression_new_for_value (&value);
g_value_unset (&value);
}
g_string_free (info->constant.text, TRUE);
info->expression_type = EXPRESSION_EXPRESSION;
info->expression = expr;
}
break;
case EXPRESSION_CLOSURE:
{
GObject *object;
GClosure *closure;
guint i, n_params;
GtkExpression **params;
GtkExpression *expression;
GSList *l;
if (info->closure.object_name)
{
object = gtk_builder_lookup_object (builder, info->closure.object_name, 0, 0, error);
if (object == NULL)
return NULL;
}
else
{
object = NULL;
}
closure = gtk_builder_create_closure (builder,
info->closure.function_name,
info->closure.swapped,
object,
error);
if (closure == NULL)
return NULL;
n_params = g_slist_length (info->closure.params);
params = g_newa (GtkExpression *, n_params);
i = n_params;
for (l = info->closure.params; l; l = l->next)
{
params[--i] = expression_info_construct (builder, domain, l->data, error);
if (params[i] == NULL)
return NULL;
}
expression = gtk_closure_expression_new (info->closure.type, closure, n_params, params);
g_free (info->closure.function_name);
g_free (info->closure.object_name);
g_slist_free_full (info->closure.params, (GDestroyNotify) free_expression_info);
info->expression_type = EXPRESSION_EXPRESSION;
info->expression = expression;
}
break;
case EXPRESSION_PROPERTY:
{
GtkExpression *expression;
GType type;
GParamSpec *pspec;
if (info->property.expression)
{
expression = expression_info_construct (builder, domain, info->property.expression, error);
if (expression == NULL)
return NULL;
g_clear_pointer (&info->property.expression, free_expression_info);
}
else
expression = NULL;
if (info->property.this_type != G_TYPE_INVALID)
type = info->property.this_type;
else if (expression != NULL)
type = gtk_expression_get_value_type (expression);
else
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
"Lookups require a type attribute if they don't have an expression.");
return NULL;
}
if (g_type_fundamental (type) == G_TYPE_OBJECT)
{
GObjectClass *class = g_type_class_ref (type);
pspec = g_object_class_find_property (class, info->property.property_name);
g_type_class_unref (class);
}
else if (g_type_fundamental (type) == G_TYPE_INTERFACE)
{
GTypeInterface *iface = g_type_default_interface_ref (type);
pspec = g_object_interface_find_property (iface, info->property.property_name);
g_type_default_interface_unref (iface);
}
else
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
"Type `%s` does not support properties",
g_type_name (type));
return NULL;
}
if (pspec == NULL)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
"Type `%s` does not have a property name `%s`",
g_type_name (type), info->property.property_name);
return NULL;
}
expression = gtk_property_expression_new_for_pspec (expression, pspec);
g_free (info->property.property_name);
info->expression_type = EXPRESSION_EXPRESSION;
info->expression = expression;
}
break;
default:
g_return_val_if_reached (NULL);
}
return gtk_expression_ref (info->expression);
}
static void
parse_signal (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
SignalInfo *info;
const char *name;
const char *handler = NULL;
const char *object = NULL;
gboolean after = FALSE;
gboolean swapped = -1;
ObjectInfo *object_info;
guint id = 0;
GQuark detail = 0;
object_info = state_peek_info (data, ObjectInfo);
if (!object_info ||
!(object_info->tag_type == TAG_OBJECT||
object_info->tag_type == TAG_TEMPLATE))
{
error_invalid_tag (data, element_name, NULL, error);
return;
}
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING, "name", &name,
G_MARKUP_COLLECT_STRING, "handler", &handler,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "object", &object,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "last_modification_time", NULL,
G_MARKUP_COLLECT_BOOLEAN|G_MARKUP_COLLECT_OPTIONAL, "after", &after,
G_MARKUP_COLLECT_TRISTATE|G_MARKUP_COLLECT_OPTIONAL, "swapped", &swapped,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (!g_signal_parse_name (name, object_info->type, &id, &detail, TRUE))
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_SIGNAL,
"Invalid signal '%s' for type '%s'",
name, g_type_name (object_info->type));
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
/* Swapped defaults to FALSE except when object is set */
if (swapped == -1)
{
if (object)
swapped = TRUE;
else
swapped = FALSE;
}
info = g_new0 (SignalInfo, 1);
info->id = id;
info->detail = detail;
info->handler = g_strdup (handler);
if (after)
info->flags |= G_CONNECT_AFTER;
if (swapped)
info->flags |= G_CONNECT_SWAPPED;
info->connect_object_name = g_strdup (object);
state_push (data, info);
info->tag_type = TAG_SIGNAL;
}
/* Called by GtkBuilder */
void
_free_signal_info (SignalInfo *info,
gpointer user_data)
{
g_free (info->handler);
g_free (info->connect_object_name);
g_free (info->object_name);
g_free (info);
}
void
_free_binding_info (BindingInfo *info,
gpointer user)
{
g_free (info->source);
g_free (info->source_property);
g_free (info);
}
void
free_binding_expression_info (BindingExpressionInfo *info)
{
if (info->expr)
free_expression_info (info->expr);
g_free (info->object_name);
g_free (info);
}
static void
free_requires_info (RequiresInfo *info,
gpointer user_data)
{
g_free (info->library);
g_free (info);
}
static void
parse_interface (ParserData *data,
const char *element_name,
const char **names,
const char **values,
GError **error)
{
const char *domain = NULL;
if (!g_markup_collect_attributes (element_name, names, values, error,
G_MARKUP_COLLECT_STRING|G_MARKUP_COLLECT_OPTIONAL, "domain", &domain,
G_MARKUP_COLLECT_INVALID))
{
_gtk_builder_prefix_error (data->builder, &data->ctx, error);
return;
}
if (domain)
{
if (data->domain && strcmp (data->domain, domain) != 0)
{
g_warning ("%s: interface domain '%s' overrides programmatic value '%s'",
data->filename, domain, data->domain);
g_free (data->domain);
}
data->domain = g_strdup (domain);
gtk_builder_set_translation_domain (data->builder, data->domain);
}
}
static SubParser *
create_subparser (GObject *object,
GObject *child,
const char *element_name,
GtkBuildableParser *parser,
gpointer user_data)
{
SubParser *subparser;
subparser = g_new0 (SubParser, 1);
subparser->object = object;
subparser->child = child;
subparser->tagname = g_strdup (element_name);
subparser->level = 1;
subparser->start = element_name;
subparser->parser = g_memdup2 (parser, sizeof (GtkBuildableParser));
subparser->data = user_data;
return subparser;
}
static void
free_subparser (SubParser *subparser)
{
g_free (subparser->tagname);
g_free (subparser);
}
static gboolean
subparser_start (GtkBuildableParseContext *context,
const char *element_name,
const char **names,
const char **values,
ParserData *data,
GError **error)
{
SubParser *subparser = data->subparser;
if (!subparser->start &&
strcmp (element_name, subparser->tagname) == 0)
subparser->start = element_name;
if (subparser->start)
{
subparser->level++;
if (subparser->parser->start_element)
subparser->parser->start_element (context,
element_name, names, values,
subparser->data, error);
return FALSE;
}
return TRUE;
}
static void
subparser_end (GtkBuildableParseContext *context,
const char *element_name,
ParserData *data,
GError **error)
{
data->subparser->level--;
if (data->subparser->parser->end_element)
data->subparser->parser->end_element (context, element_name,
data->subparser->data, error);
if (*error)
return;
if (data->subparser->level > 0)
return;
g_assert (strcmp (data->subparser->start, element_name) == 0);
gtk_buildable_custom_tag_end (GTK_BUILDABLE (data->subparser->object),
data->builder,
data->subparser->child,
element_name,
data->subparser->data);
g_clear_pointer (&data->subparser->parser, g_free);
if (_gtk_builder_lookup_failed (data->builder, error))
return;
if (GTK_BUILDABLE_GET_IFACE (data->subparser->object)->custom_finished)
data->custom_finalizers = g_slist_prepend (data->custom_finalizers,
data->subparser);
else
free_subparser (data->subparser);
data->subparser = NULL;
}
static gboolean
parse_custom (GtkBuildableParseContext *context,
const char *element_name,
const char **names,
const char **values,
ParserData *data,
GError **error)
{
CommonInfo* parent_info;
GtkBuildableParser parser;
gpointer subparser_data;
GObject *object;
GObject *child;
parent_info = state_peek_info (data, CommonInfo);
if (!parent_info)
return FALSE;
if (parent_info->tag_type == TAG_OBJECT ||
parent_info->tag_type == TAG_TEMPLATE)
{
ObjectInfo* object_info = (ObjectInfo*)parent_info;
if (!object_info->object)
{
object_info->object = builder_construct (data, object_info, error);
if (!object_info->object)
return TRUE; /* A GError is already set */
}
g_assert (object_info->object);
object = object_info->object;
child = NULL;
}
else if (parent_info->tag_type == TAG_CHILD)
{
ChildInfo* child_info = (ChildInfo*)parent_info;
_gtk_builder_add (data->builder, child_info);
object = ((ObjectInfo*)child_info->parent)->object;
child = child_info->object;
}
else
return FALSE;
if (!gtk_buildable_custom_tag_start (GTK_BUILDABLE (object),
data->builder,
child,
element_name,
&parser,
&subparser_data))
return FALSE;
data->subparser = create_subparser (object, child, element_name,
&parser, subparser_data);
if (parser.start_element)
parser.start_element (context,
element_name, names, values,
subparser_data, error);
return TRUE;
}
static void
start_element (GtkBuildableParseContext *context,
const char *element_name,
const char **names,
const char **values,
gpointer user_data,
GError **error)
{
ParserData *data = (ParserData*)user_data;
if (GTK_DEBUG_CHECK (BUILDER_TRACE))
{
GString *tags = g_string_new ("");
int i;
for (i = 0; names[i]; i++)
g_string_append_printf (tags, "%s=\"%s\" ", names[i], values[i]);
if (i)
{
g_string_insert_c (tags, 0, ' ');
g_string_truncate (tags, tags->len - 1);
}
g_message ("<%s%s>", element_name, tags->str);
g_string_free (tags, TRUE);
}
if (!data->last_element && strcmp (element_name, "interface") != 0)
{
error_unhandled_tag (data, element_name, error);
return;
}
data->last_element = element_name;
if (data->subparser)
{
if (!subparser_start (context, element_name, names, values, data, error))
return;
}
if (strcmp (element_name, "object") == 0)
parse_object (context, data, element_name, names, values, error);
else if (data->requested_objects && !data->inside_requested_object)
{
/* If outside a requested object, simply ignore this tag */
}
else if (strcmp (element_name, "property") == 0)
parse_property (data, element_name, names, values, error);
else if (strcmp (element_name, "binding") == 0)
parse_binding (data, element_name, names, values, error);
else if (strcmp (element_name, "child") == 0)
parse_child (data, element_name, names, values, error);
else if (strcmp (element_name, "signal") == 0)
parse_signal (data, element_name, names, values, error);
else if (strcmp (element_name, "template") == 0)
parse_template (context, data, element_name, names, values, error);
else if (strcmp (element_name, "requires") == 0)
parse_requires (data, element_name, names, values, error);
else if (strcmp (element_name, "interface") == 0)
parse_interface (data, element_name, names, values, error);
else if (strcmp (element_name, "constant") == 0)
parse_constant_expression (data, element_name, names, values, error);
else if (strcmp (element_name, "closure") == 0)
parse_closure_expression (data, element_name, names, values, error);
else if (strcmp (element_name, "lookup") == 0)
parse_lookup_expression (data, element_name, names, values, error);
else if (strcmp (element_name, "menu") == 0)
_gtk_builder_menu_start (data, element_name, names, values, error);
else if (strcmp (element_name, "placeholder") == 0)
{
/* placeholder has no special treatmeant, but it needs an
* if clause to avoid an error below.
*/
}
else if (!parse_custom (context, element_name, names, values, data, error))
error_unhandled_tag (data, element_name, error);
}
const char *
_gtk_builder_parser_translate (const char *domain,
const char *context,
const char *text)
{
const char *s;
if (context)
s = g_dpgettext2 (domain, context, text);
else
s = g_dgettext (domain, text);
return s;
}
static void
end_element (GtkBuildableParseContext *context,
const char *element_name,
gpointer user_data,
GError **error)
{
ParserData *data = (ParserData*)user_data;
GTK_DEBUG (BUILDER_TRACE, "</%s>", element_name);
if (data->subparser && data->subparser->start)
{
subparser_end (context, element_name, data, error);
return;
}
if (data->requested_objects && !data->inside_requested_object)
{
/* If outside a requested object, simply ignore this tag */
}
else if (strcmp (element_name, "property") == 0)
{
PropertyInfo *prop_info = state_pop_info (data, PropertyInfo);
CommonInfo *info = state_peek_info (data, CommonInfo);
g_assert (info != NULL);
/* Normal properties */
if (info->tag_type == TAG_OBJECT ||
info->tag_type == TAG_TEMPLATE)
{
ObjectInfo *object_info = (ObjectInfo*)info;
if (prop_info->translatable && prop_info->text->len)
{
const char *translated;
translated = _gtk_builder_parser_translate (data->domain,
prop_info->context,
prop_info->text->str);
g_string_assign (prop_info->text, translated);
}
if (G_UNLIKELY (!object_info->properties))
object_info->properties = g_ptr_array_new_with_free_func ((GDestroyNotify)free_property_info);
g_ptr_array_add (object_info->properties, prop_info);
}
else
g_assert_not_reached ();
}
else if (strcmp (element_name, "binding") == 0)
{
BindingExpressionInfo *binfo = state_pop_info (data, BindingExpressionInfo);
CommonInfo *info = state_peek_info (data, CommonInfo);
g_assert (info != NULL);
if (binfo->expr == NULL)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_INVALID_TAG,
"Binding tag requires an expression");
free_binding_expression_info (binfo);
}
else if (info->tag_type == TAG_OBJECT ||
info->tag_type == TAG_TEMPLATE)
{
ObjectInfo *object_info = (ObjectInfo*)info;
object_info->bindings = g_slist_prepend (object_info->bindings, binfo);
}
else
g_assert_not_reached ();
}
else if (strcmp (element_name, "object") == 0 ||
strcmp (element_name, "template") == 0)
{
ObjectInfo *object_info = state_pop_info (data, ObjectInfo);
ChildInfo* child_info = state_peek_info (data, ChildInfo);
PropertyInfo* prop_info = state_peek_info (data, PropertyInfo);
if (child_info && child_info->tag_type != TAG_CHILD)
child_info = NULL;
if (prop_info && prop_info->tag_type != TAG_PROPERTY)
prop_info = NULL;
if (data->requested_objects && data->inside_requested_object &&
(data->cur_object_level == data->requested_object_level))
{
GTK_DEBUG (BUILDER_TRACE, "requested object end found at level %d",
data->requested_object_level);
data->inside_requested_object = FALSE;
}
--data->cur_object_level;
g_assert (data->cur_object_level >= 0);
object_info->object = builder_construct (data, object_info, error);
if (!object_info->object)
{
free_object_info (object_info);
return;
}
if (child_info)
child_info->object = object_info->object;
if (prop_info)
g_string_assign (prop_info->text, object_info->id);
if (GTK_IS_BUILDABLE (object_info->object) &&
GTK_BUILDABLE_GET_IFACE (object_info->object)->parser_finished)
g_ptr_array_add (data->finalizers, object_info->object);
if (object_info->signals)
{
_gtk_builder_add_signals (data->builder, object_info->signals);
object_info->signals = NULL;
}
if (object_info->bindings)
{
gtk_builder_take_bindings (data->builder, object_info->object, object_info->bindings);
object_info->bindings = NULL;
}
free_object_info (object_info);
}
else if (strcmp (element_name, "child") == 0)
{
ChildInfo *child_info = state_pop_info (data, ChildInfo);
_gtk_builder_add (data->builder, child_info);
free_child_info (child_info);
}
else if (strcmp (element_name, "signal") == 0)
{
SignalInfo *signal_info = state_pop_info (data, SignalInfo);
ObjectInfo *object_info = (ObjectInfo*)state_peek_info (data, CommonInfo);
g_assert (object_info != NULL);
signal_info->object_name = g_strdup (object_info->id);
if (G_UNLIKELY (!object_info->signals))
object_info->signals = g_ptr_array_new ();
g_ptr_array_add (object_info->signals, signal_info);
}
else if (strcmp (element_name, "constant") == 0 ||
strcmp (element_name, "closure") == 0 ||
strcmp (element_name, "lookup") == 0)
{
ExpressionInfo *expression_info = state_pop_info (data, ExpressionInfo);
CommonInfo *parent_info = state_peek_info (data, CommonInfo);
g_assert (parent_info != NULL);
if (parent_info->tag_type == TAG_BINDING_EXPRESSION)
{
BindingExpressionInfo *expr_info = (BindingExpressionInfo *) parent_info;
expr_info->expr = expression_info;
}
else if (parent_info->tag_type == TAG_PROPERTY)
{
PropertyInfo *prop_info = (PropertyInfo *) parent_info;
prop_info->value = expression_info_construct (data->builder, data->domain, expression_info, error);
free_expression_info (expression_info);
}
else if (parent_info->tag_type == TAG_EXPRESSION)
{
ExpressionInfo *expr_info = (ExpressionInfo *) parent_info;
switch (expr_info->expression_type)
{
case EXPRESSION_CLOSURE:
expr_info->closure.params = g_slist_prepend (expr_info->closure.params, expression_info);
break;
case EXPRESSION_PROPERTY:
expr_info->property.expression = expression_info;
break;
case EXPRESSION_EXPRESSION:
case EXPRESSION_CONSTANT:
default:
g_assert_not_reached ();
break;
}
}
else
{
g_assert_not_reached ();
}
}
else if (strcmp (element_name, "requires") == 0)
{
RequiresInfo *req_info = state_pop_info (data, RequiresInfo);
/* TODO: Allow third party widget developers to check their
* required versions, possibly throw a signal allowing them
* to check their library versions here.
*/
if (!strcmp (req_info->library, "gtk"))
{
if (req_info->major == 4 && req_info->minor == 0)
{
/* We allow 3.99.x to pass as 4.0 */
}
else if (gtk_check_version (req_info->major, req_info->minor, 0) != NULL)
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_VERSION_MISMATCH,
"Required GTK version %d.%d, current version is %d.%d",
req_info->major, req_info->minor,
GTK_MAJOR_VERSION, GTK_MINOR_VERSION);
_gtk_builder_prefix_error (data->builder, context, error);
}
}
free_requires_info (req_info, NULL);
}
else if (strcmp (element_name, "interface") == 0)
{
}
else if (strcmp (element_name, "menu") == 0)
{
PropertyInfo *prop_info;
char *id;
id = _gtk_builder_menu_end (data);
prop_info = state_peek_info (data, PropertyInfo);
if (prop_info && prop_info->tag_type == TAG_PROPERTY)
g_string_assign (prop_info->text, id);
g_free (id);
}
else if (strcmp (element_name, "placeholder") == 0)
{
}
else
{
g_set_error (error,
GTK_BUILDER_ERROR,
GTK_BUILDER_ERROR_UNHANDLED_TAG,
"Unhandled tag: <%s>", element_name);
_gtk_builder_prefix_error (data->builder, context, error);
}
}
/* Called for character data */
/* text is not nul-terminated */
static void
text (GtkBuildableParseContext *context,
const char *text,
gsize text_len,
gpointer user_data,
GError **error)
{
ParserData *data = (ParserData*)user_data;
CommonInfo *info;
if (data->subparser && data->subparser->start)
{
GError *tmp_error = NULL;
if (data->subparser->parser->text)
data->subparser->parser->text (context, text, text_len,
data->subparser->data, &tmp_error);
if (tmp_error)
g_propagate_error (error, tmp_error);
return;
}
if (!data->stack || data->stack->len == 0)
return;
info = state_peek_info (data, CommonInfo);
g_assert (info != NULL);
if (strcmp (gtk_buildable_parse_context_get_element (context), "property") == 0)
{
PropertyInfo *prop_info = (PropertyInfo*)info;
g_string_append_len (prop_info->text, text, text_len);
}
else if (strcmp (gtk_buildable_parse_context_get_element (context), "constant") == 0)
{
ExpressionInfo *expr_info = (ExpressionInfo *) info;
g_string_append_len (expr_info->constant.text, text, text_len);
}
else if (strcmp (gtk_buildable_parse_context_get_element (context), "lookup") == 0)
{
ExpressionInfo *expr_info = (ExpressionInfo *) info;
while (g_ascii_isspace (*text) && text_len > 0)
{
text++;
text_len--;
}
while (text_len > 0 && g_ascii_isspace (text[text_len - 1]))
text_len--;
if (expr_info->property.expression == NULL && text_len > 0)
{
ExpressionInfo *constant = g_new0 (ExpressionInfo, 1);
constant->tag_type = TAG_EXPRESSION;
constant->expression_type = EXPRESSION_CONSTANT;
constant->constant.type = G_TYPE_INVALID;
constant->constant.text = g_string_new_len (text, text_len);
expr_info->property.expression = constant;
}
}
}
static const GtkBuildableParser parser = {
start_element,
end_element,
text,
NULL,
};
void
_gtk_builder_parser_parse_buffer (GtkBuilder *builder,
const char *filename,
const char *buffer,
gssize length,
const char **requested_objs,
GError **error)
{
const char * domain;
ParserData data;
GSList *l;
gint64 before = GDK_PROFILER_CURRENT_TIME;
/* Store the original domain so that interface domain attribute can be
* applied for the builder and the original domain can be restored after
* parsing has finished. This allows subparsers to translate elements with
* gtk_builder_get_translation_domain() without breaking the ABI or API
*/
domain = gtk_builder_get_translation_domain (builder);
memset (&data, 0, sizeof (ParserData));
data.builder = builder;
data.filename = filename;
data.domain = g_strdup (domain);
data.object_ids = g_hash_table_new_full (g_str_hash, g_str_equal,
(GDestroyNotify)g_free, NULL);
data.stack = g_ptr_array_new ();
data.finalizers = g_ptr_array_new ();
if (requested_objs)
{
data.inside_requested_object = FALSE;
data.requested_objects = requested_objs;
}
else
{
/* get all the objects */
data.inside_requested_object = TRUE;
}
gtk_buildable_parse_context_init (&data.ctx, &parser, &data);
if (!gtk_buildable_parse_context_parse (&data.ctx, buffer, length, error))
goto out;
if (_gtk_builder_lookup_failed (builder, error))
goto out;
if (!_gtk_builder_finish (builder, error))
goto out;
/* Custom parser_finished */
data.custom_finalizers = g_slist_reverse (data.custom_finalizers);
for (l = data.custom_finalizers; l; l = l->next)
{
SubParser *sub = (SubParser*)l->data;
gtk_buildable_custom_finished (GTK_BUILDABLE (sub->object),
builder,
sub->child,
sub->tagname,
sub->data);
if (_gtk_builder_lookup_failed (builder, error))
goto out;
}
/* Common parser_finished, for all created objects */
for (guint i = 0; i < data.finalizers->len; i++)
{
GtkBuildable *buildable = g_ptr_array_index (data.finalizers, i);
gtk_buildable_parser_finished (GTK_BUILDABLE (buildable), builder);
if (_gtk_builder_lookup_failed (builder, error))
goto out;
}
out:
g_slist_free_full (data.custom_finalizers, (GDestroyNotify)free_subparser);
g_free (data.domain);
g_hash_table_destroy (data.object_ids);
g_ptr_array_free (data.stack, TRUE);
g_ptr_array_free (data.finalizers, TRUE);
gtk_buildable_parse_context_free (&data.ctx);
/* restore the original domain */
gtk_builder_set_translation_domain (builder, domain);
if (GDK_PROFILER_IS_RUNNING)
{
guint64 after = GDK_PROFILER_CURRENT_TIME;
if (after - before > 500000) /* half a millisecond */
{
gdk_profiler_add_mark (before, after - before, "Builder load", filename);
}
}
}
|