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 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018
|
<?xml version="1.0"?>
<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
"http://docbook.org/docbook/xml/4.5/docbookx.dtd" [
<!ENTITY url_refdocs_base_glib_html "http://developer.gnome.org/glibmm/unstable/">
<!ENTITY url_refdocs_base_glib "&url_refdocs_base_glib_html;classGlib_1_1">
<!ENTITY url_refdocs_base_gtk_html "http://developer.gnome.org/gtkmm/unstable/">
<!ENTITY url_refdocs_base_gtk "&url_refdocs_base_gtk_html;classGtk_1_1">
<!ENTITY url_refdocs_base_gtk_namespace "&url_refdocs_base_gtk_html;namespaceGtk_1_1">
<!ENTITY url_figures_base "figures/">
<!ENTITY url_examples_base "http://git.gnome.org/browse/gtkmm-documentation/tree/examples/book/">
<!ENTITY url_examples_branchsuffix "master">
<!ENTITY gtkmm "<application>gtkmm</application>">
<!ENTITY uuml "ü" >
<!ENTITY szlig "ß" >
<!ENTITY verbar "|" >
<!ENTITY copy "©" >
<!ENTITY nbsp " " >
]>
<!--
NOTE TO TUTORIAL DOCUMENTATION AUTHORS:
When referring to the gtkmm project in this document, please use the form
>kmm; so that the name is consistent throughout the document. This will wrap
gtkmm with <application></application> tags which can then be styled by CSS if
desired (e.g. boldface, monospace, etc) to make it stand out as the project
name
-->
<!-- The XSL for developer.gnome.org requires this id. -->
<book id="index" lang="en">
<bookinfo>
<title>Programming with >kmm; 3</title>
<authorgroup>
<author>
<firstname>Murray</firstname>
<surname>Cumming</surname>
</author>
<author>
<firstname>Bernhard</firstname>
<surname>Rieder</surname>
<contrib>Chapter on "Timeouts".</contrib>
</author>
<author>
<firstname>Jonathon</firstname>
<surname>Jongsma</surname>
<contrib>Chapter on "Drawing with Cairo".</contrib>
<contrib>Chapter on "Working with gtkmm's Source Code".</contrib>
<contrib>Chapter on "Recent Files".</contrib>
</author>
<author>
<firstname>Ole</firstname>
<surname>Laursen</surname>
<contrib>Parts of chapter on "Internationalization".</contrib>
</author>
<author>
<firstname>Marko</firstname>
<surname>Anastasov</surname>
<contrib>Chapter on "Printing".</contrib>
<contrib>Parts of chapter on "Internationalization".</contrib>
</author>
<author>
<firstname>Daniel</firstname>
<surname>Elstner</surname>
<contrib>Section "Build Structure" of chapter
on "Wrapping C Libraries with gmmproc".</contrib>
</author>
<author>
<firstname>Chris</firstname>
<surname>Vine</surname>
<contrib>Chapter on "Multi-threaded programs".</contrib>
</author>
<author>
<firstname>David</firstname>
<surname>King</surname>
<contrib>Section on Gtk::Grid.</contrib>
</author>
<author>
<firstname>Pedro</firstname>
<surname>Ferreira</surname>
<contrib>Chapter on Keyboard Events.</contrib>
</author>
<author>
<firstname>Kjell</firstname>
<surname>Ahlstedt</surname>
<contrib>Parts of the update from gtkmm 2 to gtkmm 3.</contrib>
</author>
</authorgroup>
<abstract>
<!-- This text is copied from the introduction. -->
<para>This book explains key concepts of the >kmm; C++ API for creating user interfaces. It also introduces the main user interface elements ("widgets").
</para>
</abstract>
<copyright>
<year>2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010</year>
<holder>Murray Cumming</holder>
</copyright>
<legalnotice>
<para>
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.2
or any later version published by the Free Software Foundation;
with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
You may obtain a copy of the GNU Free Documentation License from the Free Software Foundation by visiting their Web site or by writing to: Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
</para>
</legalnotice>
</bookinfo>
<chapter id="chapter-introduction">
<title>Introduction</title>
<sect1 id="sec-this-book">
<title>This book</title>
<para>This book explains key concepts of the >kmm; C++ API for creating user interfaces. It also introduces the main user interface elements ("widgets"). Although it mentions classes, constructors, and methods, it does not go into great detail. Therefore, for full API information you should follow the links into the reference documentation.</para>
<para>This book assumes a good understanding of C++, and how to create C++ programs.</para>
<para>
We would very much like to hear of any problems you have learning >kmm;
with this document, and would appreciate input regarding improvements. Please see the <link linkend="chapter-contributing">Contributing</link> section for further information.
</para>
</sect1>
<sect1 id="sec-gtkmm">
<title>gtkmm</title>
<para>
>kmm; is a C++ wrapper for
<ulink url="http://www.gtk.org/">GTK+</ulink>,
a library used to create graphical user
interfaces. It is licensed using the LGPL license, so you can develop
open software, free software, or even commercial non-free software
using >kmm; without purchasing licenses.
</para>
<para>>kmm; was originally named gtk-- because GTK+ already has a + in the name. However, as -- is not easily indexed by search engines the package generally went by the name >kmm;, and that's what we stuck with.</para>
<sect2 id="why-use-gtkmm">
<title>Why use >kmm; instead of GTK+?</title>
<para>>kmm; allows you to write code using normal C++ techniques such as encapsulation, derivation, and polymorphism. As a C++ programmer you probably already realise that this leads to clearer and better organized code.</para>
<para>>kmm; is more type-safe, so the compiler can detect errors that would only be detected at run time when using C. This use of specific types also makes the API clearer because you can see what types should be used just by looking at a method's declaration.</para>
<para>Inheritance can be used to derive new widgets. The derivation of new widgets in GTK+ C code is so complicated and error prone that almost no C coders do it. As a C++ developer you know that derivation is an essential Object Orientated technique.</para>
<para>Member instances can be used, simplifying memory management. All GTK+ C widgets are dealt with by use of pointers. As a C++ coder you know that pointers should be avoided where possible.</para>
<para>>kmm; involves less code compared to GTK+, which uses prefixed function names and lots of cast macros.</para>
</sect2>
<sect2 id="gtkmm-vs-qt">
<title>>kmm; compared to Qt</title>
<para>Trolltech's Qt is the closest competition to >kmm;, so it deserves discussion.</para>
<para>>kmm; developers tend to prefer >kmm; to Qt because >kmm; does things in a more C++ way. Qt originates from a time when C++ and the standard library were not standardised or well supported by compilers. It therefore duplicates a lot of stuff that is now in the standard library, such as containers and type information. Most significantly, Trolltech modified the C++ language to provide signals, so that Qt classes cannot be used easily with non-Qt classes. >kmm; was able to use standard C++ to provide signals without changing the C++ language.
See the <ulink url="https://wiki.gnome.org/Projects/gtkmm/FAQ">FAQ</ulink> for more detailed differences.</para>
</sect2>
<sect2 id="gtkmm-is-a-wrapper">
<title>>kmm; is a wrapper</title>
<para>
>kmm; is not a native C++ toolkit, but a C++ wrapper of a C toolkit. This separation of interface and implementation has advantages. The >kmm; developers spend most of their time talking about how >kmm; can present the clearest API, without awkward compromises due to obscure technical details. We contribute a little to the underlying GTK+ code base, but so do the C coders, and the Perl coders and the Python coders, etc. Therefore GTK+ benefits from a broader user base than language-specific toolkits - there are more implementers, more developers, more testers, and more users.</para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-installation">
<title>Installation</title>
<sect1 id="sec-installation-dependencies">
<title>Dependencies</title>
<para>
Before attempting to install >kmm; 3.0, you might first need to install these other
packages.
</para>
<itemizedlist>
<listitem><para><application>libsigc++ 2.0</application></para></listitem>
<listitem><para><application>GTK+ 3.0</application></para></listitem>
<listitem><para><application>glibmm</application></para></listitem>
<listitem><para><application>cairomm</application></para></listitem>
<listitem><para><application>pangomm</application></para></listitem>
<listitem><para><application>atkmm</application></para></listitem>
</itemizedlist>
<para>
These dependencies have their own dependencies, including the following
applications and libraries:
</para>
<itemizedlist>
<listitem><para><application>pkg-config</application></para></listitem>
<listitem><para><application>glib</application></para></listitem>
<listitem><para><application>ATK</application></para></listitem>
<listitem><para><application>Pango</application></para></listitem>
<listitem><para><application>cairo</application></para></listitem>
<listitem><para><application>gdk-pixbuf</application></para></listitem>
</itemizedlist>
</sect1>
<sect1 id="sec-install-unix-and-linux">
<title>Unix and Linux</title>
<sect2 id="sec-linux-install-from-packages">
<title>Prebuilt Packages</title>
<para>
Recent versions of >kmm; are packaged by nearly every major Linux
distribution these days. So, if you use Linux, you can probably get
started with >kmm; by installing the package from the official repository
for your distribution. Distributions that include >kmm; in their
repositories include Debian, Ubuntu, Red Hat, Fedora, Mandriva, Suse, and
many others.
</para>
<para>
The names of the >kmm; packages vary from distribution to distribution
(e.g. <application>libgtkmm-3.0-dev</application> on Debian and Ubuntu or
<application>gtkmm30-devel</application> on Red Hat Fedora), so check with
your distribution's package management program for the correct package name
and install it like you would any other package.
</para>
<note>
<para>
The package names will not change when new API/ABI-compatible versions of >kmm;
are released. Otherwise they would not be API/ABI-compatible. So don't be
surprised, for instance, to find >kmm; 3.8 supplied by Debian's
<application>libgtkmm-3.0-dev</application> package.
</para>
</note>
</sect2>
<sect2 id="sec-install-from-source">
<title>Installing From Source</title>
<para>
If your distribution does not provide a pre-built >kmm; package, or if you
want to install a different version than the one provided by your distribution,
you can also install >kmm; from source. The source code for >kmm; can
be downloaded from <ulink url="http://www.gtkmm.org/"></ulink>.
</para>
<para>
After you've installed all of the dependencies, download the >kmm; source
code, unpack it, and change to the newly created directory. >kmm; can be
built and installed with the following sequence of commands:
</para>
<screen>
# ./configure
# make
# make install
</screen>
<note>
<para>
Remember that on a Unix or Linux operating system, you will probably need to
be <literal>root</literal> to install software. The <command>su</command> or <command>sudo</command>
command will allow you to enter the <literal>root</literal> password and have
<literal>root</literal> status temporarily.
</para>
</note>
<para>
The <filename>configure</filename> script will check to make sure all of
the required dependencies are already installed. If you are missing any
dependencies, it will exit and display an error.
</para>
<para>
By default, >kmm; will be installed under the
<filename>/usr/local</filename> directory. On some systems you may need to
install to a different location. For instance, on Red Hat Linux systems
you might use the <literal>--prefix</literal> option with configure, like
so:
<screen>
# ./configure --prefix=/usr
</screen>
</para>
<warning>
<para>
You should be very careful when installing to standard system prefixes
such as <filename>/usr</filename>. Linux distributions install software
packages to <filename>/usr</filename>, so installing a source package
to this prefix could corrupt or conflict with software installed using
your distribution's package-management system. Ideally, you should use
a separate prefix for all software you install from source.
</para>
</warning>
<para>
If you want to help develop >kmm; or experiment with new features, you can
also install >kmm; from git. Most users will never need to do this, but if
you're interested in helping with >kmm; development, see the <link
linkend="chapter-working-with-source">Working with gtkmm's Source Code</link> appendix.
</para>
</sect2>
</sect1>
<sect1 id="sec-packages-windows">
<title>Microsoft Windows</title>
<para>GTK+ and >kmm; were designed to work well with Microsoft Windows, and the developers encourage its use on the win32 platform. However, Windows has no standard installation system for development libraries. Please see the <ulink url="https://wiki.gnome.org/Projects/gtkmm/MSWindows">Windows Installation</ulink>
page for Windows-specific installation instructions and notes.</para>
</sect1>
</chapter>
<chapter id="chapter-basics">
<title>Basics</title>
<para>
This chapter will introduce some of the most important aspects of >kmm; coding. These will be demonstrated with simple working example code. However, this is just a taster, so you need to look at the other chapters for more substantial information.
</para>
<para>
Your existing knowledge of C++ will help you with >kmm; as it would with any library. Unless we state otherwise, you can expect >kmm; classes to behave like any other C++ class, and you can expect to use your existing C++ techniques with >kmm; classes.
</para>
<sect1 id="sec-basics-simple-example">
<title>Simple Example</title>
<para>
To begin our introduction to >kmm;, we'll start with the simplest
program possible. This program will create an empty 200 x 200 pixel window.
</para>
<para><ulink url="&url_examples_base;base">Source Code</ulink></para>
<para>We will now explain each line of the example</para>
<programlisting>#include <gtkmm.h></programlisting>
<para>
All >kmm; programs must include certain >kmm; headers; <literal>gtkmm.h</literal>
includes the entire >kmm; kit. This is usually not a good idea, because
it includes a megabyte or so of headers, but for simple programs, it
suffices.
</para>
<para>
The next statement:
<programlisting>Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.examples.base");</programlisting>
creates a <classname>Gtk::Application</classname> object, stored in a <classname>RefPtr</classname> smartpointer. This is needed in all >kmm;
applications. The <methodname>create()</methodname> method for this object initializes >kmm;, and checks the
arguments passed to your application on the command line, looking for
standard options such as <literal>--display</literal>. It takes these from the argument list, leaving anything it does not
recognize for your application to parse or ignore. This ensures
that all >kmm; applications accept the same set of standard arguments.
</para>
<para>
The next two lines of code create a window and set its default (initial) size:
</para>
<programlisting>Gtk::Window window;
window.set_default_size(200, 200);</programlisting>
<para>
The last line shows the window and enters the >kmm; main processing loop, which will finish when the window is closed.
Your <function>main()</function> function will then return with an appropriate success or error code.
</para>
<programlisting>return app->run(window);</programlisting>
<para>
After putting the source code in <literal>simple.cc</literal> you can compile
the above program with <application>gcc</application> using:
<programlisting>g++ simple.cc -o simple `pkg-config gtkmm-3.0 --cflags --libs`</programlisting>
Note that you must surround the <literal>pkg-config</literal> invocation with backquotes.
Backquotes cause the shell to execute the command inside them, and to use
the command's output as part of the command line.
Note also that <literal>simple.cc</literal> must come before the <literal>pkg-config</literal>
invocation on the command line.
</para>
</sect1>
<sect1 id="sec-headers-and-linking">
<title>Headers and Linking</title>
<para>
Although we have shown the compilation command for the simple example, you really should use the automake and autoconf tools, as described in "Autoconf, Automake, Libtool", by G. V. Vaughan et al. The examples used in this book are included in the <application>gtkmm-documentation</application> package, with appropriate build files, so we won't show the build commands in future. You'll just need to find the appropriate directory and type <literal>make</literal>.
</para>
<para>
To simplify compilation, we use <literal>pkg-config</literal>, which
is present in all (properly installed) >kmm; installations. This
program 'knows' what compiler switches are needed to compile programs
that use >kmm;. The <literal>--cflags</literal> option causes
<literal>pkg-config</literal> to output a list of include directories for the
compiler to look in; the <literal>--libs</literal> option requests the
list of libraries for the compiler to link with and the directories to
find them in. Try running it from your shell-prompt to see the results on your system.
</para>
<para>
However, this is even simpler when using the <function>PKG_CHECK_MODULES()</function> macro in a standard configure.ac file with autoconf and automake.
For instance:
<programlisting>PKG_CHECK_MODULES([MYAPP], [gtkmm-3.0 >= 3.8.0])</programlisting>
This checks for the presence of gtkmm and defines MYAPP_LIBS and MYAPP_CFLAGS for use in your Makefile.am files.
</para>
<para>gtkmm-3.0 is the name of the current stable API. There was an older API called gtkmm-2-4 which installs in parallel when it is available. There were several versions of gtkmm-2.4, such as gtkmm 2.10 and there are several versions of the gtkmm-3.0 API. Note that the API name does not change for every version because that would be an incompatible API and ABI break. Theoretically, there might be a future gtkmm-4.0 API which would install in parallel with gtkmm-3.0 without affecting existing applications.
</para>
<para>Note that if you mention extra modules in addition to gtkmm-3.0, they should be separated by spaces, not commas.
</para>
<para>
Openismus has more <ulink url="http://www.openismus.com/documents/linux/automake/automake.shtml">basic help with automake and autoconf</ulink>.
</para>
</sect1>
<sect1 id="sec-widgets-overview">
<title>Widgets</title>
<para>>kmm; applications consist of windows containing widgets, such as buttons and text boxes. In some other systems, widgets are called "controls". For each widget in your application's windows, there is a C++ object in your application's code. So you just need to call a method of the widget's class to affect the visible widget.</para>
<para>Widgets are arranged inside container widgets such as frames and notebooks, in a hierarchy of widgets within widgets. Some of these container widgets, such as <classname>Gtk::Grid</classname>, are not visible - they exist only to arrange other widgets. Here is some example code that adds 2 <classname>Gtk::Button</classname> widgets to a <classname>Gtk::Box</classname> container widget:
<programlisting>m_box.pack_start(m_Button1);
m_box.pack_start(m_Button2);</programlisting>
and here is how to add the <classname>Gtk::Box</classname>, containing those buttons, to a <classname>Gtk::Frame</classname>, which has a visible frame and title:
<programlisting>m_frame.add(m_box);</programlisting>
</para>
<para>
Most of the chapters in this book deal with specific widgets. See the <link linkend="chapter-container-widgets">Container Widgets</link> section for more details about adding widgets to container widgets.
</para>
<para>Although you can specify the layout and appearance of windows and widgets with C++ code, you will probably find it more convenient to design your user interfaces with <literal>Glade</literal> and load them at runtime with <literal>Gtk::Builder</literal>. See the <link linkend="chapter-builder">Glade and Gtk::Builder</link> chapter.
</para>
<para>Although >kmm; widget instances have lifetimes and scopes just like those of other C++ classes, >kmm; has an optional time-saving feature that you will see in some of the examples. <function>Gtk::manage()</function> allows you to say that a child widget is owned by the container into which you place it. This allows you to <function>new</function> the widget, add it to the container and forget about deleting it. You can learn more about >kmm; memory management techniques in the <link linkend="chapter-memory">Memory Management chapter</link>.
</para>
</sect1>
<sect1 id="sec-signals-overview">
<title>Signals</title>
<para>
>kmm;, like most GUI toolkits, is <emphasis>event-driven</emphasis>. When an event occurs, such as the press of a mouse
button, the appropriate signal will be <emphasis>emitted</emphasis> by the Widget
that was pressed. Each Widget has a different set of signals that it can emit. To make a
button click result in an action, we set up a
<emphasis>signal handler</emphasis> to catch the button's "clicked" signal.
</para>
<para>>kmm; uses the libsigc++ library to implement signals. Here is an example line of code that connects a Gtk::Button's "clicked" signal with a signal handler called "on_button_clicked":
<programlisting>m_button1.signal_clicked().connect( sigc::mem_fun(*this,
&HelloWorld::on_button_clicked) );</programlisting>
</para>
<para>For more detailed information about signals, see the <link linkend="chapter-signals">appendix</link>.</para>
<para>For information about implementing your own signals rather than
just connecting to the existing >kmm; signals, see the <link linkend="chapter-custom-signals">appendix</link>.</para>
</sect1>
<sect1 id="sec-basics-ustring">
<title>Glib::ustring</title>
<para>You might be surprised to learn that >kmm; doesn't use <classname>std::string</classname> in its interfaces. Instead it uses <classname>Glib::ustring</classname>, which is so similar and unobtrusive that you could actually pretend that each <classname>Glib::ustring</classname> is a <classname>std::string</classname> and ignore the rest of this section. But read on if you want to use languages other than English in your application.</para>
<para>std::string uses 8 bit per character, but 8 bits aren't enough to encode languages such as Arabic, Chinese, and Japanese. Although the encodings for these languages have now been specified by the Unicode Consortium, the C and C++ languages do not yet provide any standardised Unicode support. GTK+ and GNOME chose to implement Unicode using UTF-8, and that's what is wrapped by Glib::ustring. It provides almost exactly the same interface as std::string, along with automatic conversions to and from std::string.</para>
<para>One of the benefits of UTF-8 is that you don't need to use it unless you want to, so you don't need to retrofit all of your code at once. <classname>std::string</classname> will still work for 7-bit ASCII strings. But when you try to localize your application for languages like Chinese, for instance, you will start to see strange errors, and possible crashes. Then all you need to do is start using <classname>Glib::ustring</classname> instead.</para>
<para>Note that UTF-8 isn't compatible with 8-bit encodings like ISO-8859-1. For instance, German umlauts are not in the ASCII range and need more than 1 byte in the UTF-8 encoding. If your code contains 8-bit string literals, you have to convert them to UTF-8 (e.g. the Bavarian greeting "Grüß Gott" would be "Gr\xC3\xBC\xC3\x9F Gott").</para>
<para>You should avoid C-style pointer arithmetic, and functions such as strlen(). In UTF-8, each character might need anywhere from 1 to 6 bytes, so it's not possible to assume that the next byte is another character. <classname>Glib::ustring</classname> worries about the details of this for you so you can use methods such as Glib::ustring::substr() while still thinking in terms of characters instead of bytes.</para>
<para>Unlike the Windows UCS-2 Unicode solution, this does not require any special compiler options to process string literals, and it does not result in Unicode executables and libraries which are incompatible with ASCII ones.</para>
<para><ulink url="&url_refdocs_base_glib;ustring.html">Reference</ulink></para>
<para>See the <link linkend="chapter-internationalization">Internationalization</link> section for information about providing the UTF-8 string literals.</para>
</sect1>
<sect1 id="sec-intermediate-types">
<title>Intermediate types</title>
<para>Some API related to gtkmm uses intermediate data containers, such as <classname>Glib::StringArrayHandle</classname>, instead of a specific Standard C++ container such as <classname>std::vector</classname> or <classname>std::list</classname>, though >kmm; itself now uses just <classname>std::vector</classname> since >kmm; 3.0.</para>
<para>You should not declare these types yourself. You should instead use whatever Standard C++ container you prefer. glibmm will do the conversion for you. Here are some of these intermediate types:
<itemizedlist>
<listitem><para><classname>Glib::StringArrayHandle</classname> or <classname>Glib::ArrayHandle<Glib::ustring></classname>: Use <classname>std::vector<Glib::ustring></classname>, <classname>std::list<Glib::ustring></classname>, <type>const char*[]</type>, etc.</para></listitem>
<listitem><para><classname>Glib::ListHandle<Gtk::Widget*></classname>: Use <classname>std::vector<Gtk::Widget*></classname>, <classname>std::list<Gtk::Widget*></classname>, etc.</para></listitem>
<listitem><para><classname>Glib::SListHandle<Gtk::Widget*></classname>: Use <classname>std::vector<Gtk::Widget*></classname>, <classname>std::list<Gtk::Widget*></classname>, etc.</para></listitem>
</itemizedlist>
</para>
</sect1>
<sect1 id="sec-basics-gobj-and-wrap">
<title>Mixing C and C++ APIs</title>
<para>You can use C APIs which do not yet have convenient C++ interfaces. It is generally not a problem to use C APIs from C++, and >kmm; helps by providing access to the underlying C object, and providing an easy way to create a C++ wrapper object from a C object, provided that the C API is also based on the GObject system.</para>
<para>To use a >kmm; instance with a C function that requires a C GObject instance, use the <function>gobj()</function> function to obtain a pointer to the underlying GObject instance. For instance</para>
<para>
<programlisting>
Gtk::Button* button = new Gtk::Button("example");
gtk_button_do_something_new(button->gobj());
</programlisting>
</para>
<para>To obtain a >kmm; instance from a C GObject instance, use the Glib::wrap() function. For instance</para>
<para>
<programlisting>
GtkButton* cbutton = get_a_button();
Gtk::Button* button = Glib::wrap(cbutton);
</programlisting>
</para>
</sect1>
<sect1 id="sec-helloworld">
<title>Hello World in >kmm;</title>
<para>
We've now learned enough to look at a real example. In accordance with an ancient
tradition of computer science, we now introduce Hello World, a la >kmm;:
</para>
<para><ulink url="&url_examples_base;helloworld">Source Code</ulink></para>
<para>
Try to compile and run it before going on. You should see something like this:
</para>
<figure id="figure-helloworld">
<title>Hello World</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;helloworld.png"/>
</screenshot>
</figure>
<para>
Pretty thrilling, eh? Let's examine the code. First, the
<classname>HelloWorld</classname> class:
</para>
<programlisting>class HelloWorld : public Gtk::Window
{
public:
HelloWorld();
virtual ~HelloWorld();
protected:
//Signal handlers:
virtual void on_button_clicked();
//Member widgets:
Gtk::Button m_button;
};</programlisting>
<para>
This class implements the "Hello World" window. It's derived from
<classname>Gtk::Window</classname>, and has a single <classname>Gtk::Button</classname> as a member.
We've chosen to use the
constructor to do all of the initialisation work for the window,
including setting up the signals. Here it is, with the comments
omitted:
</para>
<programlisting>HelloWorld::HelloWorld()
:
m_button ("Hello World")
{
set_border_width(10);
m_button.signal_clicked().connect(sigc::mem_fun(*this,
&HelloWorld::on_button_clicked));
add(m_button);.
m_button.show();
}</programlisting>
<para>
Notice that we've used an initialiser statement to give the <literal>m_button</literal>
object the label "Hello World".
</para>
<para>
Next we call the Window's <methodname>set_border_width()</methodname> method. This sets
the amount of space between the sides of the window and the widget it
contains.
</para>
<para>
We then hook up a signal handler to <literal>m_button</literal>'s <literal>clicked</literal> signal.
This prints our friendly greeting to <literal>stdout</literal>.
</para>
<para>
Next, we use the Window's <methodname>add()</methodname> method to put <literal>m_button</literal> in
the Window. (<methodname>add()</methodname> comes from <classname>Gtk::Container</classname>, which is
described in the chapter on container widgets.) The <methodname>add()</methodname> method
places the Widget in the Window, but it doesn't display
the widget. >kmm; widgets are always invisible when you create them - to display them, you must call their <methodname>show()</methodname> method, which
is what we do in the next line.
</para>
<para>
Now let's look at our program's <function>main()</function> function. Here it is,
without comments:
</para>
<programlisting>int main(int argc, char** argv)
{
Glib::RefPtr<Gtk::Application> app = Gtk::Application::create(argc, argv, "org.gtkmm.example");
HelloWorld helloworld;
return app->run(helloworld);
}</programlisting>
<para>
First we instantiate an object stored in a <classname>RefPtr</classname> smartpointer called <literal>app</literal>. This is of type
<classname>Gtk::Application</classname>. Every >kmm; program must have one of these. We pass
our command-line arguments to its create() method. It takes the arguments
it wants, and leaves you the rest, as we described earlier.
</para>
<para>
Next we make an object of our <classname>HelloWorld</classname> class, whose constructor
takes no arguments, but it isn't visible yet. When we call <methodname>Gtk::Application::run()</methodname>, giving it the helloworld Window, it shows the Window and starts the >kmm; <emphasis>event loop</emphasis>. During the event loop >kmm; idles, waiting for actions from the user, and responding appropriately. When the user closes the Window, run() will return, causing the final line of our main() function be to executed. The application will then finish.
</para>
</sect1>
</chapter>
<chapter id="changes-gtkmm3">
<title>Changes in >kmm; 3</title>
<para>>kmm;-3.0 is a new version of the >kmm; API that installs in parallel with the older >kmm;-2.4 API. The last version of the >kmm;-2.4 API was >kmm; 2.24. >kmm; 3 has no major fundamental differences to >kmm; 2 but does make several small changes that were not possible while maintaining binary compatibility. If you never used the >kmm;-2.4 API then you can safely ignore this chapter.</para>
<para>>kmm; 3's library is called <literal>libgtkmm-3.0</literal> rather than <literal>libgtkmm-2.4</literal> and installs its headers in a similarly-versioned directory, so your pkg-config check should ask for <literal>gtkmm-3.0</literal> rather than <literal>gtkmm-2.4</literal>.</para>
<para>>kmm; 3 added some new classes:</para>
<orderedlist>
<listitem><simpara><classname>Gtk::AppChooser</classname>, <classname>Gtk::AppChooserButton</classname>, <classname>Gtk::AppChooserDialog</classname> allow the user to select an installed application to open a particular type of content.</simpara></listitem>
<listitem><simpara><classname>Gtk::Grid</classname> is a new container widget that will eventually replace <classname>Gtk::Box</classname> and <classname>Gtk::Table</classname>. It arranges its children according to properties of those children rather than its own layout details.</simpara></listitem>
<listitem><simpara><classname>Gtk::Switch</classname> displays On/Off states more explictly than <classname>Gtk::CheckBox</classname>. It may be useful, for instance, when allowing users to activate hardware.</simpara></listitem>
</orderedlist>
<para>>kmm; 3 also made several small changes to the API, which you will probably encounter when porting code that used >kmm;-2.4. Here is a short list:</para>
<para>
<orderedlist>
<listitem><simpara><classname>Gtk::CellLayout</classname>, used by <classname>Gtk::IconView</classname>, <classname>Gtk::TreeView::Column</classname> and <classname>Gtk::ComboBox</classname>, now has a <classname>Gtk::CellArea</classname> which can be used to specify more details of how the <classname>CellRenderer</classname>s are arranged and aligned.</simpara></listitem>
<listitem><simpara>Gtk::ComboBox now derives from CellLayout, allowing easier layout and alignment of its <classname>Gtk::CellRenderer</classname>s.</simpara></listitem>
<listitem><simpara><classname>Gtk::Adjustment</classname> and <classname>IconSet</classname> and <classname>Gdk::Cursor</classname> are now used via <classname>Glib::RefPtr</classname>.</simpara></listitem>
<listitem><simpara><classname>Gtk::Box</classname>, <classname>Gtk::ButtonBox</classname>, <classname>Gtk::IconView</classname>, <classname>Gtk::Paned</classname>, <classname>Gtk::ProgressBar</classname>, <classname>Gtk::ScaleButton</classname>, <classname>Gtk::Scrollbar</classname> and <classname>Gtk::Separator</classname> now derive from <classname>Gtk::Orientable</classname>, allowing their
orientation (vertical or horizontal) to be specified without requiring the use of a derived class such as <classname>Gtk::HBox</classname>.</simpara></listitem>
<listitem><simpara><classname>Gtk::IconView</classname>, <classname>Gtk::TextView</classname>, <classname>Gtk::TreeView</classname> and other widgets derive from Scrollable instead of having their own methods such as <methodname>get_vadjustment()</methodname> and instead of having their own set_scroll_adjustments signal.</simpara></listitem>
<listitem><simpara><classname>Gtk::Style</classname> and <classname>Gtk::Rc</classname> were removed, replaced by <classname>Gtk::StyleContext</classname>, and <classname>Gtk::StyleProvider</classname>s, such as <classname>Gtk::CssProvider</classname>.</simpara></listitem>
<listitem><simpara>Widget::on_expose_event() was replaced by Widget::on_draw(), which assumes that cairomm is used for drawing, via the provided <classname>Cairo::Context</classname> and does not require you to call <methodname>Cairo::Context::clip()</methodname>.</simpara></listitem>
<listitem><simpara><classname>Gdk::RGBA</classname> replaces <classname>Color</classname>, adding an alpha component for opacity. <classname>Colormap</classname> was removed, along with its awkward use to allocate colors.</simpara></listitem>
<listitem><simpara><classname>Gdk::Pixmap</classname> and <classname>Gdk::Bitmap</classname> were removed in favour of <classname>Gdk::Pixbuf</classname>.</simpara></listitem>
<listitem><simpara><classname>Gdk::Drawable</classname> was removed, with its methods moving into <classname>Gdk::Window</classname>.</simpara></listitem>
<listitem><simpara>We now use std::vector in several methods instead of the intermediate *Handle types to make the API clearer.</simpara></listitem>
</orderedlist>
</para>
<para>All deprecated API was removed in >kmm; 3.0, though there will be new deprecations in future versions.</para>
<para>As a first step to porting your source code to >kmm;-3.0 you should probably ensure that your application builds with the deprecated >kmm;-2.4 API disabled, by defining macro such as GTKMM_DISABLE_DEPRECATED. There are some autotools macros that can help with this by defining them optionally at build time. See the <ulink url="https://wiki.gnome.org/Projects/gtkmm/PortingToGtkmm3">gtkmm 3 porting wiki page</ulink> for more details.</para>
</chapter>
<chapter id="chapter-button-widget">
<title>Buttons</title>
<para>
>kmm; provides four basic types of buttons:
</para>
<variablelist>
<varlistentry>
<term>Push-Buttons</term>
<listitem>
<para>
<ulink url="&url_refdocs_base_gtk;Button.html"><classname>Gtk::Button</classname></ulink>. Standard buttons, usually
marked with a label or picture. Pushing one triggers an action. See the <link linkend="sec-pushbuttons">Button</link> section.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Toggle buttons</term>
<listitem>
<para>
<ulink url="&url_refdocs_base_gtk;ToggleButton.html"><classname>Gtk::ToggleButton</classname></ulink>.
Unlike a normal Button, which springs back up, a ToggleButton stays down until you
press it again. It might be useful as an on/off switch. See the <link linkend="sec-toggle-buttons">ToggleButton</link> section.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Checkboxes</term>
<listitem>
<para>
<ulink url="&url_refdocs_base_gtk;CheckButton.html"><classname>Gtk::CheckButton</classname></ulink>.
These act like ToggleButtons, but show their state in small squares,
with their label at the side. They should be used in most situations
which require an on/off setting.
See the <link linkend="sec-checkboxes">CheckButton</link> section.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term>Radio buttons</term>
<listitem>
<para>
<ulink url="&url_refdocs_base_gtk;RadioButton.html"><classname>Gtk::RadioButton</classname></ulink>.
Named after the station selectors on old car
radios, these buttons are used in groups for options which are
mutually exclusive. Pressing one causes all the
others in its group to turn off. They are similar to
CheckBoxes (a small widget with a label at the side), but usually
look different.
See the <link linkend="sec-radio-buttons">RadioButton</link> section.
</para>
</listitem>
</varlistentry>
</variablelist>
<para>
Note that, due to GTK+'s theming system, the appearance of these
widgets will vary. In the case of checkboxes and radio buttons, they
may vary considerably.
</para>
<sect1 id="sec-pushbuttons">
<title>Button</title>
<sect2 id="pushbutton-constructors"><title>Constructors</title>
<para>
There are two ways to create a Button. You can specify a label
string in the <classname>Gtk::Button</classname> constructor,
or set it later with <methodname>set_label()</methodname>.
</para>
<para>To define an accelerator key for keyboard navigation, place an underscore before one of the label's characters and specify <literal>true</literal> for the optional <literal>mnemonic</literal> parameter. For instance:
</para>
<programlisting>Gtk::Button* pButton = new Gtk::Button("_Something", true);</programlisting>
<para>
Stock items have been recommended for use in buttons. From >kmm;-3.10 they are deprecated.
They should not be used in newly-written code. However, the documentation of
<ulink url="&url_refdocs_base_gtk_namespace;Stock.html">namespace Gtk::Stock</ulink>
shows recommended labels and named icons to show in buttons.
</para>
<para>
<classname>Gtk::Button</classname> is also
a container so you could put any other widget, such as a
<classname>Gtk::Image</classname> into it.
</para>
<para><ulink url="&url_refdocs_base_gtk;Button.html">Reference</ulink></para>
</sect2>
<sect2 id="pushbutton-example"><title>Example</title>
<para>
This example creates a button with a picture and a label.
</para>
<figure id="figure-buttons">
<title>buttons example</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;buttons.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;buttons/button">Source Code</ulink></para>
</sect2>
<sect2 id="pushbutton-signals"><title>Signals</title>
<para>
The <classname>Gtk::Button</classname> widget has the following signals, but most of the time you will just handle the <literal>clicked</literal> signal:
</para>
<para>
<variablelist>
<varlistentry>
<term><literal>pressed</literal></term>
<listitem>
<para>
Emitted when the button is pressed.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>released</literal></term>
<listitem>
<para>
Emitted when the button is released.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>clicked</literal></term>
<listitem>
<para>
Emitted when the button is pressed and released.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>enter</literal></term>
<listitem>
<para>
Emitted when the mouse pointer moves over the button's window.
</para>
</listitem>
</varlistentry>
<varlistentry>
<term><literal>leave</literal></term>
<listitem>
<para>
Emitted when the mouse pointer leaves the button's window.
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</sect2>
</sect1>
<sect1 id="sec-toggle-buttons">
<title>ToggleButton</title>
<para><classname>ToggleButton</classname>s are like normal <classname>Button</classname>s, but when clicked they remain activated, or pressed, until clicked again.</para>
<para>
To retrieve the state of the <classname>ToggleButton</classname>, you can use the
<methodname>get_active()</methodname> method. This returns <literal>true</literal> if the button
is "down". You can also set the toggle button's state, with <methodname>set_active()</methodname>. Note that, if you do this, and the state actually changes, it causes the
"clicked" signal to be emitted. This is usually what you want.
</para>
<para>
You can use the <methodname>toggled()</methodname> method to toggle the button, rather than
forcing it to be up or down: This switches the button's state, and causes the <literal>toggled</literal> signal to be emitted.
</para>
<para>
<classname>Gtk::ToggleButton</classname> is most useful as a base class for the
<classname>Gtk::CheckButton</classname> and
<classname>Gtk::RadioButton</classname> classes.
</para>
<para><ulink url="&url_refdocs_base_gtk;ToggleButton.html">Reference</ulink></para>
</sect1>
<sect1 id="sec-checkboxes">
<title>CheckButton</title>
<para>
<classname>Gtk::CheckButton</classname> inherits from
<classname>Gtk::ToggleButton</classname>. The only real difference between the
two is <classname>Gtk::CheckButton</classname>'s
appearance. You can check, set, and toggle a checkbox using the same
member methods as for <classname>Gtk::ToggleButton</classname>.
</para>
<para><ulink url="&url_refdocs_base_gtk;CheckButton.html">Reference</ulink></para>
<sect2 id="checkbutton-example"><title>Example</title>
<figure id="figure-checkbutton">
<title>CheckButton</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;checkbutton.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;buttons/checkbutton">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-radio-buttons">
<title>RadioButton</title>
<para>
Like checkboxes, radio buttons also inherit from
<classname>Gtk::ToggleButton</classname>, but these work in groups, and only
one RadioButton in a group can be selected at any one time.
</para>
<sect2 id="radiobutton-groups"><title>Groups</title>
<para>
There are two ways to set up a group of radio buttons. The first way
is to create the buttons, and set up their groups afterwards. Only
the first two constructors are used. In the following example, we
make a new window class called <classname>RadioButtons</classname>, and then
put three radio buttons in it:
</para>
<programlisting>class RadioButtons : public Gtk::Window
{
public:
RadioButtons();
protected:
Gtk::RadioButton m_rb1, m_rb2, m_rb3;
};
RadioButtons::RadioButtons()
: m_rb1("button1"),
m_rb2("button2"),
m_rb3("button3")
{
Gtk::RadioButton::Group group = m_rb1.get_group();
m_rb2.set_group(group);
m_rb3.set_group(group);
}</programlisting>
<para>
We told >kmm; to put all three <classname>RadioButton</classname>s in the
same group by obtaining the group with <methodname>get_group()</methodname> and using
<methodname>set_group()</methodname> to tell the other
<classname>RadioButton</classname>s to share that group.
</para>
<para>
Note that you can't just do
<programlisting>m_rb2.set_group(m_rb1.get_group()); //doesn't work</programlisting>
because the group is modified by <methodname>set_group()</methodname> and therefore
non-const.
</para>
<para>
The second way to set up radio buttons is to make a group first, and
then add radio buttons to it. Here's an example:
</para>
<programlisting>class RadioButtons : public Gtk::Window
{
public:
RadioButtons();
};
RadioButtons::RadioButtons()
{
Gtk::RadioButton::Group group;
Gtk::RadioButton *m_rb1 = Gtk::manage(
new Gtk::RadioButton(group,"button1"));
Gtk::RadioButton *m_rb2 = manage(
new Gtk::RadioButton(group,"button2"));
Gtk::RadioButton *m_rb3 = manage(
new Gtk::RadioButton(group,"button3"));
}</programlisting>
<para>
We made a new group by simply declaring a variable, <literal>group</literal>,
of type <classname>Gtk::RadioButton::Group</classname>. Then we made three radio
buttons, using a constructor to make each of them part of
<literal>group</literal>.
</para>
</sect2>
<sect2 id="radiobutton-methods"><title>Methods</title>
<para>
<classname>RadioButtons</classname> are "off" when created; this means that
when you first make a group of them, they will all be off. Don't forget to turn
one of them on using <methodname>set_active()</methodname>:
</para>
<para><ulink url="&url_refdocs_base_gtk;RadioButton.html">Reference</ulink></para>
</sect2>
<sect2 id="radiobutton-example"><title>Example</title>
<para>
The following example demonstrates the use of
<classname>RadioButton</classname>s:
</para>
<figure id="figure-radiobutton">
<title>RadioButton</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;radiobuttons.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;buttons/radiobutton">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-range-widgets">
<title>Range Widgets</title>
<para>
<classname>Gtk::Scale</classname> and <classname>Gtk::Scrollbar</classname>
both inherit from <classname>Gtk::Range</classname> and share much
functionality. They contain a "trough" and a "slider" (sometimes called a
"thumbwheel" in other GUI environments). Dragging the slider with the pointer
moves it within the trough, while clicking in the trough advances the slider
towards the location of the click, either completely, or by a designated
amount, depending on which mouse button is used. This should be familiar
scrollbar behaviour.
</para>
<para>
As will be explained in the <link linkend="chapter-adjustment">Adjustment</link>
section, all Range widgets are associated with a
<classname>Adjustment</classname> object. To change the lower, upper, and
current values used by the widget you need to use the methods of its
<classname>Adjustment</classname>, which you can get with the
<methodname>get_adjustment()</methodname> method. The <classname>Range</classname>
widgets' default constructors create an <classname>Adjustment</classname>
automatically, or you can specify an existing
<classname>Adjustment</classname>, maybe to share it with another widget. See
the <link linkend="chapter-adjustment">Adjustments</link> section for further
details.
</para>
<para><ulink url="&url_refdocs_base_gtk;Range.html">Reference</ulink></para>
<sect1 id="sec-scrollbar-widgets">
<title>Scrollbar Widgets</title>
<para>
These are standard scrollbars. They should be used only to scroll another
widget, such as, a <classname>Gtk::Entry</classname>, or a
<classname>Gtk::Viewport</classname>, though it's usually easier to use the
<classname>Gtk::ScrolledWindow</classname> widget in most cases.
</para>
<para>
The orientation of a <classname>Gtk::Scrollbar</classname> can be either
horizontal or vertical.
</para>
<para><ulink url="&url_refdocs_base_gtk;Scrollbar.html">Reference</ulink></para>
</sect1>
<sect1 id="sec-scale-widgets">
<title>Scale Widgets</title>
<para>
<classname>Gtk::Scale</classname> widgets (or "sliders") allow the user to
visually select and manipulate a value within a specific range. You
might use one, for instance, to adjust the
magnification level on a zoomed preview of a picture, or to control
the brightness of a colour, or to specify the number of minutes of
inactivity before a screensaver takes over the screen.
</para>
<para>
As with <classname>Scrollbar</classname>s, the orientation can be either
horizontal or vertical. The default constructor creates an
<classname>Adjustment</classname> with all of its values set to
<literal>0.0</literal>. This isn't useful so you will need to set some
<classname>Adjustment</classname> details to get meaningful behaviour.
</para>
<sect2 id="scale-useful-methods">
<title>Useful methods</title>
<para>
<classname>Scale</classname> widgets can display their current value as a number
next to the trough. By default they show the value, but you can change this
with the <methodname>set_draw_value()</methodname> method.
</para>
<para>
The value displayed by a scale widget is rounded to one decimal point
by default, as is the <literal>value</literal> field in its
<classname>Gtk::Adjustment</classname>. You can change this with the
<methodname>set_digits()</methodname> method.
</para>
<para>
Also, the value can be drawn in different positions relative to the trough,
specified by the <methodname>set_value_pos()</methodname> method.
</para>
<para><ulink url="&url_refdocs_base_gtk;Scale.html">Reference</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-range-example">
<title>Example</title>
<para>
This example displays a window with three range widgets all connected
to the same adjustment, along with a couple of controls for adjusting
some of the parameters mentioned above and in the section on
adjustments, so you can see how they affect the way these widgets work
for the user.
</para>
<figure id="figure-range-widgets">
<title>Range Widgets</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;range_widgets.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;range_widgets">Source Code</ulink></para>
</sect1>
</chapter>
<chapter id="chapter-misc-widgets">
<title>Miscellaneous Widgets</title>
<sect1 id="sec-labels">
<title>Label</title>
<para>
Labels are the main method of placing non-editable text in windows, for
instance to place a title next to a <classname>Entry</classname> widget. You
can specify the text in the constructor, or later with the
<methodname>set_text()</methodname> or <methodname>set_markup()</methodname> methods.
</para>
<para>
The width of the label will be adjusted automatically. You can produce multi-line labels by putting line breaks ("\n") in the label string.
</para>
<para>
The label text can be justified using the <methodname>set_justify()</methodname>
method. The widget is also capable of word-wrapping, which can be activated
with <methodname>set_line_wrap()</methodname>.
</para>
<para>
Gtk::Label support some simple formatting, for instance allowing you to make some
text bold, colored, or larger. You can do this by providing a string to
<methodname>set_markup()</methodname>, using the <ulink url="http://developer.gnome.org/pango/unstable/PangoMarkupFormat.html">Pango Markup syntax</ulink>. For instance,
<code>
<b>bold text</b> and <s>strikethrough text</s>
</code>
.</para>
<para><ulink url="&url_refdocs_base_gtk;Label.html">Reference</ulink></para>
<sect2 id="label-example"><title>Example</title>
<para>
Below is a short example to illustrate these functions. This example
makes use of the Frame widget to better demonstrate the label styles.
(The Frame widget is explained in the <link linkend="sec-frame">Frame</link> section.)
It is possible that the first character in <literal>m_Label_Normal</literal> is shown
underlined only when you press the <keycap>Alt</keycap> key.
</para>
<figure id="figure-label">
<title>Label</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;label.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;label">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-text-entry">
<title>Entry</title>
<sect2 id="sec-text-entry-simple">
<title>Simple Use</title>
<para>
Entry widgets allow the user to enter text. You can change the contents with the <methodname>set_text()</methodname> method,
and read the current contents with the <methodname>get_text()</methodname> method.
</para>
<para>
Occasionally you might want to make an <classname>Entry</classname> widget
read-only. This can be done by passing <literal>false</literal> to the
<methodname>set_editable()</methodname> method.
</para>
<para>
For the input of passwords, passphrases and other information you don't want
echoed on the screen, calling <methodname>set_visibility()</methodname> with
<literal>false</literal> will cause the text to be hidden.
</para>
<para>
You might want to be notified whenever the user types in a text entry widget.
<classname>Gtk::Entry</classname> provides two signals,
<literal>activate</literal> and <literal>changed</literal>, for this purpose.
<literal>activate</literal> is emitted when the user presses the Enter key in
a text-entry widget; <literal>changed</literal> is emitted when the text in
the widget changes. You can use these, for instance, to validate or filter
the text the user types. Moving the keyboard focus to another widget may also
signal that the user has finished entering text. The <literal>focus_out_event</literal>
signal that <classname>Gtk::Entry</classname> inherits from
<classname>Gtk::Widget</classname> can notify you when that happens.
The <link linkend="sec-comboboxentry">ComboBox with an Entry</link> section
contains example programs that use these signals.
</para>
<para>
If you pass <literal>true</literal> to the <methodname>set_activates_default()</methodname>
method, pressing Enter in the <classname>Gtk::Entry</classname> will activate
the default widget for the window containing the <classname>Gtk::Entry</classname>.
This is especially useful in dialog boxes. The default widget is usually one of
the dialog buttons, which e.g. will close the dialog box. To set a widget as the
default widget, use <methodname>Gtk::Widget::set_can_default()</methodname> and
<methodname>Gtk::Widget::grab_default()</methodname>.
</para>
<para><ulink url="&url_refdocs_base_gtk;Entry.html">Reference</ulink></para>
<sect3 id="entry-example"><title>Simple Entry Example</title>
<para>
This example uses <classname>Gtk::Entry</classname>. It also has two
<classname>CheckButton</classname>s, with which you can toggle the editable and
visible flags.
</para>
<figure id="figure-entry-simple">
<title>Entry</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;entry.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;entry/simple">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-text-entry-completion">
<title>Entry Completion</title>
<para>A <classname>Entry</classname> widget can offer a drop-down list of
pre-existing choices based on the first few characters typed by the user. For
instance, a search dialog could suggest text from previous searches.
</para>
<para>To enable this functionality, you must create a
<classname>EntryCompletion</classname> object, and provide it to the
<classname>Entry</classname> widget via the
<methodname>set_completion()</methodname> method.</para>
<para>The <classname>EntryCompletion</classname> may use a
<classname>TreeModel</classname> containing possible entries, specified with
<methodname>set_model()</methodname>. You should then call
<methodname>set_text_column()</methodname> to specify which of your model columns
should be used to match possible text entries.</para>
<para>Alternatively, if a complete list of possible entries
would be too large or too inconvenient to generate, a callback slot may instead
be specified with <methodname>set_match_func()</methodname>.
This is also useful if you wish to match on a part of the string other
than the start.</para>
<para><ulink url="&url_refdocs_base_gtk;EntryCompletion.html">Reference</ulink></para>
<sect3 id="entry-completion-example"><title>Entry Completion Example</title>
<para>
This example creates a <classname>Gtk::EntryCompletion</classname> and associates
it with a <classname>Gtk::Entry</classname> widget. The completion uses a
<classname>Gtk::TreeModel</classname> of possible entries, and some additional
actions.
</para>
<figure id="figure-entry-completion">
<title>Entry Completion</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;entry_completion.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;entry/completion">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-text-entry-icons">
<title>Entry Icons</title>
<para>An <classname>Entry</classname> widget can show an icon at the start or
end of the text area. The icon can be specifed by methods such as
<methodname>set_icon_from_pixbuf()</methodname> or
<methodname>set_icon_from_icon_name()</methodname>. An application can respond to the
user pressing the icon by handling the
<methodname>signal_icon_press</methodname> signal.</para>
<sect3 id="entry-icon-example"><title>Entry Icon Example</title>
<para>
This example shows a <classname>Gtk::Entry</classname> widget with a named
search icon, and prints text to the terminal when the icon is pressed.
</para>
<figure id="figure-entry-icon">
<title>Entry with Icon</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;entry_icon.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;entry/icon">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-text-entry-progress">
<title>Entry Progress</title>
<para>An <classname>Entry</classname> widget can show a progress bar inside the
text area, under the entered text. The progress bar will be shown if the
<methodname>set_progress_fraction()</methodname> or
<methodname>set_progress_pulse_step()</methodname> methods are called.</para>
<sect3 id="entry-progress-example"><title>Entry Progress Example</title>
<para>
This example shows a <classname>Gtk::Entry</classname> widget with a progress
bar.
</para>
<figure id="figure-entry-progress">
<title>Entry with Progress Bar</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;entry_progress.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;entry/progress">Source Code</ulink></para>
</sect3>
</sect2>
</sect1>
<sect1 id="sec-spinbutton">
<title>SpinButton</title>
<para>
A <classname>SpinButton</classname> allows the user to select a value from a
range of numeric values. It has an <classname>Entry</classname> widget with increment and decrement buttons
at the side. Clicking the buttons causes the value to 'spin' up and down across
the range of possible values. The <classname>Entry</classname> widget may also
be used to enter a value directly.
</para>
<para>
The value can have an adjustable number of decimal places, and the step size is
configurable. <classname>SpinButton</classname>s have an 'auto-repeat' feature
as well: holding down the increment or decrement button can optionally cause the value to
change more quickly the longer the button is held down.
</para>
<para>
<classname>SpinButton</classname>s use an <link
linkend="chapter-adjustment">Adjustment</link> object to hold information about
the range of values. These Adjustment attributes are used by the Spin Button
like so:
<itemizedlist>
<listitem>
<para>
<literal>value</literal>: value for the Spin Button
</para>
</listitem>
<listitem>
<para>
<literal>lower</literal>: lower range value
</para>
</listitem>
<listitem>
<para>
<literal>upper</literal>: upper range value
</para>
</listitem>
<listitem>
<para>
<literal>step_increment</literal>: value to increment/decrement when pressing
mouse button 1 on a button
</para>
</listitem>
<listitem>
<para>
<literal>page_increment</literal>: value to increment/decrement when pressing
mouse button 2 on a button
</para>
</listitem>
<listitem>
<para>
<literal>page_size</literal>: unused
</para>
</listitem>
</itemizedlist>
</para>
<para>
Additionally, mouse button 3 can be used to jump directly to the
<literal>upper</literal> or <literal>lower</literal> values.
</para>
<para>
The <classname>SpinButton</classname> can create a default
<classname>Adjustment</classname>, which you can access via the
<methodname>get_adjustment()</methodname> method, or you can specify an existing
<classname>Adjustment</classname> in the constructor.
</para>
<sect2 id="spinbutton-methods"><title>Methods</title>
<para>
The number of decimal places can be altered using the
<methodname>set_digits()</methodname> method.
</para>
<para>
You can set the spinbutton's value using the <methodname>set_value()</methodname>
method, and retrieve it with <methodname>get_value()</methodname>.
</para>
<para>
The <methodname>spin()</methodname> method 'spins' the
<classname>SpinButton</classname>, as if its increment or decrement button had been clicked.
You need to specify a <classname>Gtk::SpinType</classname> to specify the
direction or new position.
</para>
<para>
To prevent the user from typing non-numeric characters into the entry box, pass
<literal>true</literal> to the <methodname>set_numeric()</methodname> method.
</para>
<para>
To make the <classname>SpinButton</classname> 'wrap' between its upper and
lower bounds, use the <methodname>set_wrap()</methodname> method.
</para>
<para>
To force it to snap to the nearest <literal>step_increment</literal>,
use <methodname>set_snap_to_ticks()</methodname>.
</para>
<para><ulink url="&url_refdocs_base_gtk;SpinButton.html">Reference</ulink></para>
</sect2>
<sect2 id="spinbutton-example"><title>Example</title>
<para>
Here's an example of a <classname>SpinButton</classname> in action:
</para>
<figure id="figure-spinbutton">
<title>SpinButton</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;spinbutton.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;spinbutton">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-progressbar">
<title>ProgressBar</title>
<para>
Progress bars are used to show the status of an ongoing operation. For
instance, a <classname>ProgressBar</classname> can show how much of a task has
been completed.
</para>
<para>
To change the value shown, use the <methodname>set_fraction()</methodname> method,
passing a <type>double</type> between 0.0 and 1.0 to provide the new percentage.
</para>
<para>
A <classname>ProgressBar</classname> is horizontal and left-to-right by default,
but you can change it to a vertical progress bar by using the
<methodname>set_orientation()</methodname> method.
</para>
<para><ulink url="&url_refdocs_base_gtk;ProgressBar.html">Reference</ulink></para>
<sect2 id="progressbar-activity-mode">
<title>Activity Mode</title>
<para>
Besides indicating the amount of progress that has occured, the
progress bar can also be used to indicate that there is some activity;
this is done by placing the progress bar in <emphasis>activity mode</emphasis>. In
this mode, the progress bar displays a small rectangle which moves
back and forth. Activity mode is useful in situations where the
progress of an operation cannot be calculated as a value range (e.g.,
receiving a file of unknown length).
</para>
<para>
To do this, you need to call the <methodname>pulse()</methodname> method at regular
intervals. You can also choose the step size, with the
<methodname>set_pulse_step()</methodname> method.
</para>
<para>
The progress bar can also display a configurable text
string within its trough, using the <methodname>set_text()</methodname> method.
</para>
</sect2>
<sect2 id="progressbar-example"><title>Example</title>
<figure id="figure-progressbar">
<title>ProgressBar</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;progressbar.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;progressbar">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-infobar">
<title>InfoBar</title>
<para>
An <classname>InfoBar</classname> may show small items of information or ask brief questions. Unlike a <classname>Dialog</classname>, it appears at the top of the current window instead of opening a new window. Its API is very similar to the <link linkend="chapter-dialogs">Gtk::Dialog</link> API.</para>
<para><ulink url="&url_refdocs_base_gtk;InfoBar.html">Reference</ulink></para>
<sect2 id="infobar-example"><title>Example</title>
<figure id="figure-infobar">
<title>InfoBar</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;infobar.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;infobar">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-tooltips">
<title>Tooltips</title>
<para>
Tooltips are the little information windows that pop up when you leave your
pointer over a widget for a few seconds. Use
<methodname>set_tooltip_text()</methodname> to set a text string as a tooltip
on any <classname>Widget</classname>. <classname>Gtk::ToolItem</classname>s are
not <classname>Widget</classname>s, but have the same method for convenience.
<classname>Gtk::Tooltip</classname> is used for more advanced tooltip usage,
such as showing an image as well as text.
</para>
<para><ulink url="&url_refdocs_base_gtk;Widget.html">Widget Reference</ulink></para>
<para><ulink url="&url_refdocs_base_gtk;Tooltip.html">Tooltip Reference</ulink></para>
<sect2 id="tooltip-example"><title>Example</title>
<figure id="figure-tooltip">
<title>Tooltip</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;tooltip.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;tooltips">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-container-widgets">
<title>Container Widgets</title>
<para>
All container widgets derive from <classname>Gtk::Container</classname>, not
always directly. Some container widgets, such as
<classname>Gtk::Grid</classname> can hold many child widgets, so these
typically have more complex interfaces. Others, such as
<classname>Gtk::Frame</classname> contain only one child widget.
</para>
<sect1 id="sec-single-item-containers">
<title>Single-item Containers</title>
<para>
The single-item container widgets derive from <classname>Gtk::Bin</classname>,
which provides the <methodname>add()</methodname> and <methodname>remove()</methodname>
methods for the child widget. Note that <classname>Gtk::Button</classname> and
<classname>Gtk::Window</classname> are technically single-item containers, but
we have discussed them already elsewhere.
</para>
<para>
We also discuss the <classname>Gtk::Paned</classname> widget, which allows you
to divide a window into two separate "panes". This widget actually contains
two child widgets, but the number is fixed so it seems appropriate.
</para>
<sect2 id="sec-frame">
<title>Frame</title>
<para>
Frames can enclose one or a group of widgets within a box, optionally with a
title. For instance, you might place a group of
<classname>RadioButton</classname>s or <classname>CheckButton</classname>s in a
<classname>Frame</classname>.
</para>
<para><ulink url="&url_refdocs_base_gtk;Frame.html">Reference</ulink></para>
<sect3 id="frame-example"><title>Example</title>
<figure id="figure-frame">
<title>Frame</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;frame.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;frame">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-paned">
<title>Paned</title>
<para>
Panes divide a widget into two halves, separated by a moveable divider.
The two halves (panes) can be oriented either horizontally (side by side) or
vertically (one above the other).
</para>
<para>
Unlike the other widgets in this section, pane widgets contain not one but two
child widgets, one in each pane. Therefore, you should use
<methodname>add1()</methodname> and <methodname>add2()</methodname> instead of the
<methodname>add()</methodname> method.
</para>
<para>
You can adjust the position of the divider using the
<methodname>set_position()</methodname> method, and you will probably need to do
so.
</para>
<para><ulink url="&url_refdocs_base_gtk;Paned.html">Reference</ulink></para>
<sect3 id="paned-example"><title>Example</title>
<figure id="figure-paned">
<title>Paned</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;paned.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;paned">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-scrolledwindow">
<title>ScrolledWindow</title>
<para>
<classname>ScrolledWindow</classname> widgets create a scrollable
area. You can insert any type of widget into a
<classname>ScrolledWindow</classname> window, and it will be accessible
regardless of its size by using the scrollbars. Note that
<classname>ScrolledWindow</classname> is not a
<classname>Gtk::Window</classname> despite the slightly misleading name.
</para>
<para>
Scrolled windows have <emphasis>scrollbar policies</emphasis> which determine
whether the <classname>Scrollbar</classname>s will be displayed. The policies
can be set with the <methodname>set_policy()</methodname> method. The policy may be
one of <literal>Gtk::POLICY_AUTOMATIC</literal> or
<literal>Gtk::POLICY_ALWAYS</literal>.
<literal>Gtk::POLICY_AUTOMATIC</literal> will cause the scrolled window
to display the scrollbar only if the contained widget is larger than the
visible area. <literal>Gtk::POLICY_ALWAYS</literal> will cause the
scrollbar to be displayed always.
</para>
<para><ulink url="&url_refdocs_base_gtk;ScrolledWindow.html">Reference</ulink></para>
<sect3 id="scrolledwindow-example"><title>Example</title>
<para>
Here is a simple example that packs 100 toggle buttons into a ScrolledWindow. Try resizing the window to see the scrollbars react.
</para>
<figure id="figure-scrolledwindow">
<title>ScrolledWindow</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;scrolledwindow.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;scrolledwindow">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-aspectframe">
<title>AspectFrame</title>
<para>
The <classname>AspectFrame</classname> widget looks like a
<classname>Frame</classname> widget, but it also enforces the <emphasis>aspect
ratio</emphasis> (the ratio of the width to the height) of the child
widget, adding extra space if necessary. For instance, this would allow you to
display a photograph without allowing the user to distort it horizontally or
vertically while resizing.
</para>
<para><ulink url="&url_refdocs_base_gtk;AspectFrame.html">Reference</ulink></para>
<sect3 id="aspectframe-example">
<title>Example</title>
<para>
The following program uses a <classname>Gtk::AspectFrame</classname> to present a
drawing area whose aspect ratio will always be 2:1, no matter how the user
resizes the top-level window.
</para>
<figure id="figure-aspectframe">
<title>AspectFrame</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;aspectframe.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;aspectframe">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-alignment">
<title>Alignment</title>
<para>
The <classname>Alignment</classname> widget allows you to place a widget at a
position and size relative to the size of the <classname>Alignment</classname>
widget itself. For instance, it might be used to center a widget.
</para>
<para>
You need to specify the <classname>Alignment</classname>'s characteristics to
the constructor, or to the <methodname>set()</methodname> method. In particular, you
won't notice much effect unless you specify a number other than 1.0 for the
<literal>xscale</literal> and <literal>yscale</literal> parameters, because 1.0
simply means that the child widget will expand to fill all available space.
</para>
<para><ulink url="&url_refdocs_base_gtk;Alignment.html">Reference</ulink></para>
<sect3 id="alignment-example">
<title>Example</title>
<para>
This example right-aligns a button in a window by using an
<classname>Alignment</classname> widget.
</para>
<figure id="figure-alignment">
<title>Alignment</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;alignment.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;alignment">Source Code</ulink></para>
<para>
See the <link linkend="sec-progressbar">ProgressBar</link> section for another
example that uses an <classname>Alignment</classname>.
</para>
</sect3>
</sect2>
</sect1>
<sect1 id="sec-multi-item-containers">
<title>Multiple-item widgets </title>
<para>
Multiple-item widgets inherit from <classname>Gtk::Container</classname>; just
as with <classname>Gtk::Bin</classname>, you use the <methodname>add()</methodname>
and <methodname>remove()</methodname> methods to add and remove contained widgets.
Unlike <methodname>Gtk::Bin::remove()</methodname>, however, the
<methodname>remove()</methodname> method for <classname>Gtk::Container</classname>
takes an argument, specifiying which widget to remove.
</para>
<sect2 id="container-packing">
<title>Packing</title>
<para>
You've probably noticed that >kmm; windows seem "elastic" - they can usually be stretched in many different ways. This is due to the <emphasis>widget packing</emphasis>
system.
</para>
<para>
Many GUI toolkits require you to precisely place widgets in a window, using absolute positioning, often using a visual editor. This leads to several problems:
</para>
<itemizedlist>
<listitem>
<para>The widgets don't rearrange themselves when the window is resized. Some widgets are hidden when the window is made smaller, and lots of useless space appears when the window is made larger.</para>
</listitem>
<listitem>
<para>It's impossible to predict the amount of space necessary for text after it has been translated to other languages, or displayed in a different font. On Unix it is also impossible to anticipate the effects of every theme and window manager.</para>
</listitem>
<listitem>
<para>
Changing the layout of a window "on the fly", to make some extra widgets appear, for instance, is complex. It requires tedious recalculation of every widget's position.</para>
</listitem>
</itemizedlist>
<para>
>kmm; uses the packing system to solve these problems. Rather than specifying the position and size of each widget in the window,
you can arrange your widgets in rows, columns,
and/or grids. >kmm; can size your window automatically, based on the
sizes of the widgets it contains. And the sizes of the widgets are, in turn, determined by the amount of text they contain, or the minimum and maximum sizes that you specify, and/or how you have requested that the available space should be shared between sets of widgets.
You can perfect your layout by
specifying padding distance and centering values for each of your widgets. >kmm; then uses
all this information to resize and reposition everything sensibly and smoothly when the user manipulates the window. </para>
<para>
>kmm; arranges widgets hierarchically, using <emphasis>containers</emphasis>.
A Container widget contains other widgets. Most >kmm; widgets are
containers. Windows, Notebook tabs, and Buttons are all container widgets.
There are two flavours of containers: single-child containers, which are all
descendants of <classname>Gtk::Bin</classname>, and multiple-child containers,
which are descendants of <classname>Gtk::Container</classname>. Most widgets
in >kmm; are descendants of <classname>Gtk::Bin</classname>, including
<classname>Gtk::Window</classname>.
</para>
<para>
Yes, that's correct: a Window can contain at most one widget. How, then, can
we use a window for anything useful? By placing a multiple-child container in
the window. The most useful container widgets are
<classname>Gtk::Grid</classname> and <classname>Gtk::Box</classname>.
</para>
<itemizedlist>
<listitem>
<para>
<classname>Gtk::Grid</classname> arranges its child widgets in rows and
columns. Use <methodname>attach()</methodname>,
<methodname>attach_next_to()</methodname> and <methodname>add()</methodname> to
insert child widgets.
</para>
</listitem>
<listitem>
<para>
<classname>Gtk::Box</classname> arranges its child widgets vertically or horizontally. Use
<methodname>pack_start()</methodname> and <methodname>pack_end()</methodname> to insert
child widgets.
</para>
</listitem>
</itemizedlist>
<para>
There are several other containers, which we will also discuss.
</para>
<para>
If you've never used a packing toolkit before, it can take some
getting used to. You'll probably find, however, that you don't
need to rely on visual form editors quite as much as you might with
other toolkits.
</para>
</sect2>
<sect2 id="sec-helloworld2">
<title>An improved Hello World</title>
<para>
Let's take a look at a slightly improved <literal>helloworld</literal>, showing what we've learnt.
</para>
<figure id="figure-helloworld2">
<title>Hello World 2</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;helloworld2.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;helloworld2">Source Code</ulink></para>
<para>
After building and running this program, try resizing the window to see the
behaviour. Also, try playing with the options to
<methodname>pack_start()</methodname> while reading the <link
linkend="sec-boxes">Boxes</link> section.
</para>
</sect2>
<sect2 id="sec-boxes">
<title>Boxes</title>
<para>
Most packing uses boxes as in the above example. These
are invisible containers into which we can pack our widgets. When
packing widgets into a horizontal box, the objects are inserted
horizontally from left to right or right to left depending on whether
<methodname>pack_start()</methodname> or <methodname>pack_end()</methodname> is used.
In a vertical box, widgets are packed from top to bottom or vice
versa. You may use any combination of boxes inside or beside other
boxes to create the desired effect.
</para>
<sect3 id="boxes-adding-widgets"><title>Adding widgets</title>
<sect4 id="per-child-packing-options"><title>Per-child packing options</title>
<para>
The <methodname>pack_start()</methodname> and
<methodname>pack_end()</methodname> methods place widgets inside these
containers. The <methodname>pack_start()</methodname> method will start at
the top and work its way down in a <classname>Box</classname> with vertical
orientation, or pack left to right in a <classname>Box</classname> with horizontal
orientation. <methodname>pack_end()</methodname> will do the opposite, packing from
bottom to top or from right to left. Using these methods allows us to right justify or
left justify our widgets. We will use <methodname>pack_start()</methodname>
in most of our examples.
</para>
<para>
There are several options governing how widgets are to be packed, and this can
be confusing at first. If you have difficulties then it is sometimes a good
idea to play with the <application>glade</application> GUI designer to see what
is possible. You might even decide to use the
<application>Gtk::Builder</application> API to load your GUI at runtime.
</para>
<para>
There are basically five
different styles, as shown in this picture:
</para>
<figure id="figure-box-packing1">
<title>Box Packing 1</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;box_packing1.png"/>
</screenshot>
</figure>
<para>
Each line contains one horizontal <classname>Box</classname> with
several buttons. Each of the buttons on a line is packed into the
<classname>Box</classname> with the same arguments to the
<methodname>pack_start()</methodname> method.
</para>
<para>
This is the declaration of the <methodname>pack_start()</methodname> method:
</para>
<programlisting>void pack_start(Gtk::Widget& child,
Gtk::PackOptions options = Gtk::PACK_EXPAND_WIDGET,
guint padding = 0);</programlisting>
<para>
The first argument is the widget you're packing. In our example these are all <classname>Button</classname>s.
</para>
<para>
The <parameter>options</parameter> argument can take one of these three options:
<itemizedlist>
<listitem><para><literal>Gtk::PACK_SHRINK</literal>: Space is contracted to the child widget size. The widget will take up just-enough space and never expand.</para></listitem>
<listitem><para><literal>Gtk::PACK_EXPAND_PADDING</literal>: Extra space is filled with padding. The widgets will be spaced out evenly, but their sizes won't change - there will be empty space between the widgets instead. </para></listitem>
<listitem><para><literal>Gtk::PACK_EXPAND_WIDGET</literal>: Extra space is taken up by increasing the child widget size, without changing the amount of space between widgets.</para></listitem>
</itemizedlist>
</para>
<para>
The <parameter>padding</parameter> argument specifies the width of an extra
border area to leave around the packed widget.
</para>
<para><ulink url="&url_refdocs_base_gtk;Box.html">Reference</ulink></para>
</sect4>
<sect4 id="per-container-packing-options"><title>Per-container packing options</title>
<para>
Here's the constructor for the <classname>Box</classname> widget,
and methods that set per-container packing options:
<programlisting>Gtk::Box(Gtk::Orientation orientation = Gtk::ORIENTATION_HORIZONTAL, int spacing = 0);
void set_spacing(int spacing);
void set_homogeneous(bool homogeneous = true);</programlisting>
Passing <literal>true</literal> to <methodname>set_homogeneous()</methodname> will
cause all of the contained widgets to be the same size.
<parameter>spacing</parameter> is a (minimum) number of pixels to leave between
each widget.
</para>
<para>
What's the difference between spacing (set when the box is created)
and padding (set when elements are packed)? Spacing is added between
objects, and padding is added on either side of a widget. The following
figure should make it clearer:
</para>
<figure id="figure-box-packing2">
<title>Box Packing 2</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;box_packing2.png"/>
</screenshot>
</figure>
</sect4>
</sect3>
<sect3 id="boxes-command-line-options">
<title>Gtk::Application and command-line options</title>
<para>The following example program requires a command-line option.
The source code shows two ways of handling command-line options in combination
with <classname>Gtk::Application</classname>.
</para>
<itemizedlist>
<listitem><para>
Handle the options in <function>main()</function> and hide them from
<classname>Gtk::Application</classname> by setting <literal>argc = 1</literal>
in the call to <methodname>Gtk::Application::create()</methodname>.
</para></listitem>
<listitem><para>
Give all command-line options to <methodname>Gtk::Application::create()</methodname>
and add the flag <literal>Gio::APPLICATION_HANDLES_COMMAND_LINE</literal>.
Connect a signal handler to the <literal>command_line</literal> signal, and
handle the command-line options in the signal handler.</para>
<para>You must set the optional parameter <literal>after = false</literal> in
the call to <literal>signal_command_line().connect()</literal>, because your signal
handler must be called before the default signal handler. You must also call
<methodname>Gio::Application::activate()</methodname> in the signal handler,
unless you want your application to exit without showing its main window.
(<classname>Gio::Application</classname> is a base class of
<classname>Gtk::Application</classname>.)
</para></listitem>
</itemizedlist>
</sect3>
<sect3 id="box-packing-example">
<title>Example</title>
<para>
Here is the source code for the example that produced the screenshots above. When you run this example, provide a number between 1 and 3 as a command-line option, to see different packing options in use.</para>
<para><ulink url="&url_examples_base;box">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-buttonbox">
<title>ButtonBoxes</title>
<para>
Button boxes are a convenient way to quickly arrange a group of buttons. Their
orientation can be either horizontal or vertical.
</para>
<para>
<classname>ButtonBox</classname>es help to make applications appear consistent
because they use standard settings, such as inter-button spacing and packing.
</para>
<para>
Buttons are added to a <classname>ButtonBox</classname> with the
<methodname>add()</methodname> method.
</para>
<para>
Button boxes support several layout styles. The style can be retrieved and
changed using <methodname>get_layout()</methodname> and
<methodname>set_layout()</methodname>.
</para>
<para><ulink url="&url_refdocs_base_gtk;ButtonBox.html">Reference</ulink></para>
<sect3 id="buttonbox-example">
<title>Example</title>
<figure id="figure-buttonbox">
<title>ButtonBox</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;buttonbox.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;buttonbox">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-grid">
<title>Grid</title>
<para>
A <classname>Grid</classname> dynamically lays out child widgets in rows and
columns. The dimensions of the grid do not need to be specified in the constructor.
</para>
<para>
Child widgets can span multiple rows or columns, using
<methodname>attach()</methodname>, or added next to an existing widget inside
the grid with <methodname>attach_next_to()</methodname>. Individual rows and columns of the grid can be set to have uniform height or width with
<methodname>set_row_homogeneous()</methodname> and
<methodname>set_column_homogeneous()</methodname>.
</para>
<para>You can set the <emphasis>margin</emphasis> and <emphasis>expand</emphasis> properties of the
child <classname>Widget</classname>s to control their spacing and their behaviour when the Grid is resized.
</para>
<para><ulink url="&url_refdocs_base_gtk;Grid.html">Reference</ulink></para>
<sect3 id="grid-example"><title>Example</title>
<para>
This example creates a window with three buttons in a grid.
The first two buttons are in the upper row, from left to right. A
third button is attached underneath the first button, in a new lower row,
spanning two columns.
</para>
<figure id="figure-grid">
<title>Grid</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;grid.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;grid">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-table">
<title>Table</title>
<para>
<classname>Gtk::Table</classname> allows us to place widgets in a grid,
similar to <classname>Gtk::Grid</classname>.
</para>
<para>
<classname>Gtk::Table</classname> is deprecated from >kmm; version 3.4 and should
not be used in newly-written code. Use <classname>Gtk::Grid</classname> instead.
</para>
</sect2>
<sect2 id="sec-notebook">
<title>Notebook</title>
<para>
A <classname>Notebook</classname> has a set of stacked
<literal>pages</literal>, each of which contains widgets. Labelled
<literal>tabs</literal> allow the user to select the pages.
<classname>Notebook</classname>s allow several sets of widgets to be placed in a
small space, by only showing one page at a time. For instance, they are often
used in preferences dialogs.
</para>
<para>
Use the <methodname>append_page()</methodname>, <methodname>prepend_page()</methodname>
and <methodname>insert_page()</methodname> methods to add tabbed pages to the
<literal>Notebook</literal>, supplying the child widget and the name for the
tab.
</para>
<para>
To discover the currently visible page, use the
<methodname>get_current_page()</methodname> method. This returns the page number,
and then calling <methodname>get_nth_page()</methodname> with that number will give
you a pointer to the actual child widget.
</para>
<para>
To programmatically change the selected page, use the
<methodname>set_current_page()</methodname> method.
</para>
<para><ulink url="&url_refdocs_base_gtk;Notebook.html">Reference</ulink></para>
<sect3 id="notebook-example"><title>Example</title>
<figure id="figure-notebook">
<title>Notebook</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;notebook.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;notebook/">Source Code</ulink></para>
</sect3>
</sect2>
<sect2 id="sec-assistant">
<title>Assistant</title>
<para>
An <classname>Assistant</classname> splits a complex operation into steps. Each step is a page, containing a header, a child widget and an action area. The Assistant's action area has navigation buttons which update automatically depending on the type of the page, set with <methodname>set_page_type()</methodname>.
</para>
<para>
Use the <methodname>append_page()</methodname>, <methodname>prepend_page</methodname> and <methodname>insert_page()</methodname> methods to add pages to the <classname>Assistant</classname>, supplying the child widget for each page.
</para>
<para>
To determine the currently-visible page, use the <methodname>get_current_page()</methodname> method, and pass the result to <methodname>get_nth_page()</methodname>, which returns a pointer to the actual widget. To programmatically change the current page, use the <methodname>set_current_page()</methodname> method.
</para>
<para>
To set the title of a page, use the <methodname>set_page_title()</methodname> method. The header and side images of a page can be set with the <methodname>set_page_header_image()</methodname> and <methodname>set_page_side_image()</methodname> methods.
</para>
<para>
To add widgets to the action area, use the <methodname>add_action_widget()</methodname> method. They will be packed alongside the default buttons. Use the <methodname>remove_action_widget()</methodname> method to remove widgets.
</para>
<para><ulink url="&url_refdocs_base_gtk;Assistant.html">Reference</ulink></para>
<sect3 id="assistant-example"><title>Example</title>
<figure id="figure-assistant">
<title>Assistant</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;assistant.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;assistant/">Source Code</ulink></para>
</sect3>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-treeview">
<title>The TreeView widget</title>
<para>
The <classname>Gtk::TreeView</classname> widget can contain lists or trees of
data, in columns.
</para>
<sect1 id="sec-treeview-model">
<title>The Model</title>
<para>
Each <classname>Gtk::TreeView</classname> has an associated
<classname>Gtk::TreeModel</classname>, which contains the data displayed by the
<classname>TreeView</classname>. Each <classname>Gtk::TreeModel</classname> can
be used by more than one <classname>Gtk::TreeView</classname>. For instance,
this allows the same underlying data to be displayed and edited in 2 different
ways at the same time. Or the 2 Views might display different columns from the
same Model data, in the same way that 2 SQL queries (or "views") might
show different fields from the same database table.
</para>
<para>
Although you can theoretically implement your own Model, you will normally use
either the <classname>ListStore</classname> or <classname>TreeStore</classname>
model classes.
</para>
<para><ulink url="&url_refdocs_base_gtk;TreeModel.html">Reference</ulink></para>
<sect2 id="treeview-model-liststore">
<title>ListStore, for rows</title>
<para>
The <classname>ListStore</classname> contains simple rows of data, and each row
has no children.
</para>
<figure id="figure-treeview-liststore-model">
<title>TreeView - ListStore</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;treeview_list.png"/>
</screenshot>
</figure>
<para><ulink url="&url_refdocs_base_gtk;ListStore.html">Reference</ulink></para>
</sect2>
<sect2 id="treeview-model-treestore">
<title>TreeStore, for a hierarchy</title>
<para>
The <classname>TreeStore</classname> contains rows of data, and each row may
have child rows.
</para>
<figure id="figure-treeview-treestore-model">
<title>TreeView - TreeStore</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;treeview_tree.png"/>
</screenshot>
</figure>
<para><ulink url="&url_refdocs_base_gtk;TreeStore.html">Reference</ulink></para>
</sect2>
<sect2 id="treeview-model-columns">
<title>Model Columns</title>
<para>
The <classname>TreeModelColumnRecord</classname> class is used to keep track
of the columns and their data types. You add
<classname>TreeModelColumn</classname> instances to the
<classname>ColumnRecord</classname> and then use those
<classname>TreeModelColumns</classname> when getting and setting the data in
model rows. You will probably find it convenient to derive a new
<classname>TreeModelColumnRecord</classname> which has your
<classname>TreeModelColumn</classname> instances as member data.
</para>
<programlisting>class ModelColumns : public Gtk::TreeModelColumnRecord
{
public:
ModelColumns()
{ add(m_col_text); add(m_col_number); }
Gtk::TreeModelColumn<Glib::ustring> m_col_text;
Gtk::TreeModelColumn<int> m_col_number;
};
ModelColumns m_Columns;</programlisting>
<para>
You specify the <classname>ColumnRecord</classname> when creating the Model,
like so:
</para>
<programlisting>Glib::RefPtr<Gtk::ListStore> refListStore =
Gtk::ListStore::create(m_Columns);</programlisting>
<para>Note that the instance (such as m_Columns here) should usually not be static, because it often needs to be instantiated after
glibmm has been instantiated.</para>
</sect2>
<sect2 id="treeview-adding-rows">
<title>Adding Rows</title>
<para>
Add rows to the model with the <methodname>append()</methodname>,
<methodname>prepend()</methodname>, or <methodname>insert()</methodname> methods.
</para>
<programlisting>Gtk::TreeModel::iterator iter = m_refListStore->append();</programlisting>
<para>You can dereference the iterator to get the Row:
</para>
<programlisting>Gtk::TreeModel::Row row = *iter;</programlisting>
<sect3 id="treeview-adding-child-rows"><title>Adding child rows</title>
<para>
<classname>Gtk::TreeStore</classname> models can have child items. Add them
with the <methodname>append()</methodname>, <methodname>prepend()</methodname>, or
<methodname>insert()</methodname> methods, like so:
</para>
<programlisting>Gtk::TreeModel::iterator iter_child =
m_refTreeStore->append(row.children());</programlisting>
</sect3>
</sect2>
<sect2 id="treeview-setting-values">
<title>Setting values</title>
<para>
You can use the <methodname>operator[]</methodname> override to set the data for a
particular column in the row, specifying the
<classname>TreeModelColumn</classname> used to create the model.
</para>
<programlisting>row[m_Columns.m_col_text] = "sometext";</programlisting>
</sect2>
<sect2 id="treeview-getting-values">
<title>Getting values</title>
<para>
You can use the <methodname>operator[]</methodname> override to get the data in a
particular column in a row, specifiying the
<classname>TreeModelColumn</classname> used to create the model.
</para>
<programlisting>Glib::ustring strText = row[m_Columns.m_col_text];
int number = row[m_Columns.m_col_number];</programlisting>
<para>
The compiler will complain if you use an inappropriate type. For
instance, this would generate a compiler error:
</para>
<programlisting>//compiler error - no conversion from ustring to int.
int number = row[m_Columns.m_col_text];</programlisting>
</sect2>
<sect2 id="treeview-hidden-columns">
<title>"Hidden" Columns</title>
<para>
You might want to associate extra data with each row. If so, just add
it as a Model column, but don't add it to the View.
</para>
</sect2>
</sect1>
<sect1 id="sec-treeview">
<title>The View</title>
<para>
The View is the actual widget (<classname>Gtk::TreeView</classname>) that
displays the model (<classname>Gtk::TreeModel</classname>) data and allows the
user to interact with it. The View can show all of the model's columns, or just
some, and it can show them in various ways.
</para>
<para><ulink url="&url_refdocs_base_gtk;TreeView.html">Reference</ulink></para>
<sect2 id="sec-treeview-using-a-model">
<title>Using a Model</title>
<para>
You can specify a <classname>Gtk::TreeModel</classname> when constructing the
<classname>Gtk::TreeView</classname>, or you can use the
<methodname>set_model()</methodname> method, like so:
</para>
<programlisting>m_TreeView.set_model(m_refListStore);</programlisting>
</sect2>
<sect2 id="treeview-adding-view-columns">
<title>Adding View Columns</title>
<para>
You can use the <methodname>append_column()</methodname> method to tell the View
that it should display certain Model columns, in a certain order, with a
certain column title.
</para>
<programlisting>m_TreeView.append_column("Messages", m_Columns.m_col_text);</programlisting>
<para>
When using this simple <methodname>append_column()</methodname> override, the
<classname>TreeView</classname> will display the model data with an appropriate
<classname>CellRenderer</classname>. For instance, strings and numbers are
shown in a simple <classname>Gtk::Entry</classname> widget, and booleans are
shown in a <classname>Gtk::CheckButton</classname>. This is usually what you
need. For other column types you must either connect a callback that converts
your type into a string representation, with
<methodname>TreeViewColumn::set_cell_data_func()</methodname>, or derive a custom
<classname>CellRenderer</classname>. Note that (unsigned) short is not
supported by default - You could use (unsigned) int or (unsigned) long as the
column type instead.
</para>
</sect2>
<sect2 id="treeview-multiple-model-columns-per-view-column">
<title>More than one Model Column per View Column</title>
<para>
To render more than one model column in a view column, you need to create the
<classname>TreeView::Column</classname> widget manually, and use
<methodname>pack_start()</methodname> to add the model columns to it.
</para>
<para>
Then use <methodname>append_column()</methodname> to add the view Column to the
View. Notice that <methodname>Gtk::TreeView::append_column()</methodname> is overridden
to accept either a prebuilt <classname>Gtk::TreeView::Column</classname> widget, or
just the <classname>TreeModelColumn</classname> from which it generates an
appropriate <classname>Gtk::TreeView::Column</classname> widget.
</para>
<para>
Here is some example code from
<filename>gtkmm/demos/gtk-demo/example_icontheme.cc</filename>, which has a pixbuf
icon and a text name in the same column:
</para>
<programlisting>Gtk::TreeView::Column* pColumn =
Gtk::manage(new Gtk::TreeView::Column("Icon Name"));
// m_columns.icon and m_columns.iconname are columns in the model.
// pColumn is the column in the TreeView:
pColumn->pack_start(m_columns.icon, /* expand= */ false);
pColumn->pack_start(m_columns.iconname);
m_TreeView.append_column(*pColumn);</programlisting>
</sect2>
<sect2 id="treeview-cellrenderer-details">
<title>Specifying CellRenderer details</title>
<para>
The default <classname>CellRenderers</classname> and their default behaviour
will normally suffice, but you might occasionally need finer control. For
instance, this example code from
<filename>gtkmm/demos/gtk-demo/example_treeview_treestore.cc</filename>, appends a
<classname>Gtk::CellRenderer</classname> widget and instructs it to render the
data from various model columns through various aspects of its appearance.
</para>
<programlisting>int cols_count = m_TreeView.append_column_editable("Alex", m_columns.alex);
Gtk::TreeViewColumn* pColumn = m_TreeView.get_column(cols_count-1);
if(pColumn)
{
Gtk::CellRendererToggle* pRenderer =
static_cast<Gtk::CellRendererToggle*>(pColumn->get_first_cell());
pColumn->add_attribute(pRenderer->property_visible(), m_columns.visible);
pColumn->add_attribute(pRenderer->property_activatable(), m_columns.world);</programlisting>
<para>
You can also connect to <classname>CellRenderer</classname> signals to detect user
actions. For instance:
</para>
<programlisting>Gtk::CellRendererToggle* pRenderer =
Gtk::manage( new Gtk::CellRendererToggle() );
pRenderer->signal_toggled().connect(
sigc::bind( sigc::mem_fun(*this,
&Example_TreeView_TreeStore::on_cell_toggled), m_columns.dave)
);</programlisting>
</sect2>
<sect2 id="treeview-editable-cells">
<title>Editable Cells</title>
<sect3 id="treeview-editable-cells-automatic">
<title>Automatically-stored editable cells.</title>
<para>
Cells in a <classname>TreeView</classname> can be edited in-place by the user.
To allow this, use the <classname>Gtk::TreeView</classname>
<methodname>insert_column_editable()</methodname> and
<methodname>append_column_editable()</methodname> methods instead of
<methodname>insert_column()</methodname> and <methodname>append_column()</methodname>.
When these cells are edited the new values will be stored immediately in the
Model. Note that these methods are templates which can only be instantiated for
simple column types such as <classname>Glib::ustring</classname>, int, and
long.
</para>
</sect3>
<sect3 id="treeview-editable-cells-custom">
<title>Implementing custom logic for editable cells.</title>
<para>
However, you might not want the new values to be stored
immediately. For instance, maybe you want to restrict the input to
certain characters or ranges of values.
</para>
<para>
To achieve this, you should use the normal <classname>Gtk::TreeView</classname>
<methodname>insert_column()</methodname> and <methodname>append_column()</methodname>
methods, then use <methodname>get_column_cell_renderer()</methodname> to get the
<classname>Gtk::CellRenderer</classname> used by that column.
</para>
<para>
You should then cast that <classname>Gtk::CellRenderer*</classname> to the
specific <classname>CellRenderer</classname> that you expect, so you can use specific API.
</para>
<para>For instance, for a CellRendererText, you would set the cell's <emphasis>editable</emphasis> property to true, like
so:
</para>
<programlisting>cell.property_editable() = true;</programlisting>
<para>
For a CellRendererToggle, you would set the <emphasis>activatable</emphasis>
property instead.
</para>
<para>You can then connect
to the appropriate "edited" signal. For instance, connect to
<methodname>Gtk::CellRendererText::signal_edited()</methodname>, or
<methodname>Gtk::CellRendererToggle::signal_toggled()</methodname>. If the column
contains more than one <classname>CellRenderer</classname> then you will need
to use <methodname>Gtk::TreeView::get_column()</methodname> and then call
<methodname>get_cell_renderers()</methodname> on that view Column.
</para>
<para>
In your signal handler, you should examine the new value and then
store it in the Model if that is appropriate for your application.
</para>
</sect3>
</sect2>
</sect1>
<sect1 id="sec-iterating-over-model-rows">
<title>Iterating over Model Rows</title>
<para>
<classname>Gtk::TreeModel</classname> provides a C++ Standard Library-style container of its
children, via the <methodname>children()</methodname> method. You can use the
familiar <methodname>begin()</methodname> and <methodname>end()</methodname> methods
iterator incrementing, like so:
</para>
<programlisting>typedef Gtk::TreeModel::Children type_children; //minimise code length.
type_children children = refModel->children();
for(type_children::iterator iter = children.begin();
iter != children.end(); ++iter)
{
Gtk::TreeModel::Row row = *iter;
//Do something with the row - see above for set/get.
}</programlisting>
<sect2 id="treeview-row-children">
<title>Row children</title>
<para>
When using a <classname>Gtk::TreeStore</classname>, the rows can have child
rows, which can have their own children in turn. Use
<methodname>Gtk::TreeModel::Row::children()</methodname> to get the container of child <classname>Row</classname>s:
<programlisting>Gtk::TreeModel::Children children = row.children();</programlisting>
</para>
</sect2>
</sect1>
<sect1 id="sec-treeview-selection">
<title>The Selection</title>
<para>
To find out what rows the user has selected, get the
<classname>Gtk::TreeView::Selection</classname> object from the
<classname>TreeView</classname>, like so:
</para>
<programlisting>Glib::RefPtr<Gtk::TreeSelection> refTreeSelection =
m_TreeView.get_selection();</programlisting>
<sect2 id="treeview-selection-mode">
<title>Single or multiple selection</title>
<para>
By default, only single rows can be selected, but you can allow
multiple selection by setting the mode, like so:
<programlisting>refTreeSelection->set_mode(Gtk::SELECTION_MULTIPLE);</programlisting>
</para>
</sect2>
<sect2 id="treeview-selected-rows">
<title>The selected rows</title>
<para>
For single-selection, you can just call <methodname>get_selected()</methodname>,
like so:
</para>
<programlisting>TreeModel::iterator iter = refTreeSelection->get_selected();
if(iter) //If anything is selected
{
TreeModel::Row row = *iter;
//Do something with the row.
}</programlisting>
<para>
For multiple-selection, you need to define a callback, and give it to
<methodname>selected_foreach()</methodname>,
<methodname>selected_foreach_path()</methodname>, or
<methodname>selected_foreach_iter()</methodname>, like so:
</para>
<programlisting>refTreeSelection->selected_foreach_iter(
sigc::mem_fun(*this, &TheClass::selected_row_callback) );
void TheClass::selected_row_callback(
const Gtk::TreeModel::iterator& iter)
{
TreeModel::Row row = *iter;
//Do something with the row.
}</programlisting>
</sect2>
<sect2 id="treeview-selection-changed-signal">
<title>The "changed" signal</title>
<para>
To respond to the user clicking on a row or range of rows, connect to the
signal like so:
</para>
<programlisting>refTreeSelection->signal_changed().connect(
sigc::mem_fun(*this, &Example_IconTheme::on_selection_changed)
);</programlisting>
</sect2>
<sect2 id="treeview-selection-preventing">
<title>Preventing row selection</title>
<para>
Maybe the user should not be able to select every item in your list or tree.
For instance, in the gtk-demo, you can select a demo to see the source code,
but it doesn't make any sense to select a demo category.
</para>
<para>
To control which rows can be selected, use the
<methodname>set_select_function()</methodname> method, providing a
<classname>sigc::slot</classname> callback. For instance:
</para>
<programlisting>m_refTreeSelection->set_select_function( sigc::mem_fun(*this,
&DemoWindow::select_function) );</programlisting>
<para>
and then
</para>
<programlisting>bool DemoWindow::select_function(
const Glib::RefPtr<Gtk::TreeModel>& model,
const Gtk::TreeModel::Path& path, bool)
{
const Gtk::TreeModel::iterator iter = model->get_iter(path);
return iter->children().empty(); // only allow leaf nodes to be selected
}</programlisting>
</sect2>
<sect2 id="treeview-selection-changing">
<title>Changing the selection</title>
<para>
To change the selection, specify a
<classname>Gtk::TreeModel::iterator</classname> or
<classname>Gtk::TreeModel::Row</classname>, like so:
</para>
<programlisting>Gtk::TreeModel::Row row = m_refModel->children()[5]; //The fifth row.
if(row)
refTreeSelection->select(row);</programlisting>
<para>
or
</para>
<programlisting>Gtk::TreeModel::iterator iter = m_refModel->children().begin()
if(iter)
refTreeSelection->select(iter);</programlisting>
</sect2>
</sect1>
<sect1 id="sec-treeview-sort">
<title>Sorting</title>
<para>
The standard tree models (<classname>TreeStore</classname> and <classname>ListStore</classname>) derive from <classname>TreeSortable</classname>, so they offer sorting functionality. For instance, call <methodname>set_sort_column()</methodname>, to sort the model by the specified column. Or supply a callback function to <methodname>set_sort_func()</methodname> to implement a more complicated sorting algorithm.
</para>
<para><ulink url="&url_refdocs_base_gtk;TreeSortable.html">TreeSortable Reference</ulink></para>
<sect2 id="treeview-sort-headers">
<title>Sorting by clicking on columns</title>
<para>
So that a user can click on a <classname>TreeView</classname>'s column header to sort the <classname>TreeView</classname>'s contents, call <methodname>Gtk::TreeView::Column::set_sort_column()</methodname>, supplying the model column on which model should be sorted when the header is clicked. For instance:
</para>
<programlisting>Gtk::TreeView::Column* pColumn = treeview.get_column(0);
if(pColumn)
pColumn->set_sort_column(m_columns.m_col_id);</programlisting>
</sect2>
<sect2 id="treeview-sort-independent-views">
<title>Independently sorted views of the same model</title>
<para>
The <classname>TreeView</classname> already allows you to show the same <classname>TreeModel</classname> in two <classname>TreeView</classname> widgets. If you need one of these TreeViews to sort the model differently than the other then you should use a <classname>TreeModelSort</classname> instead of just, for instance, <methodname>Gtk::TreeViewModel::set_sort_column()</methodname>. <classname>TreeModelSort</classname> is a model that contains another model, presenting a sorted version of that model. For instance, you might add a sorted version of a model to a <classname>TreeView</classname> like so:
</para>
<programlisting>Glib::RefPtr<Gtk::TreeModelSort> sorted_model =
Gtk::TreeModelSort::create(model);
sorted_model->set_sort_column(columns.m_col_name, Gtk::SORT_ASCENDING);
treeview.set_model(sorted_model);</programlisting>
<para>Note, however, that the TreeView will provide iterators to the sorted model. You must convert them to iterators to the underlying child model in order to perform actions on that model. For instance:
</para>
<programlisting>void ExampleWindow::on_button_delete()
{
Glib::RefPtr<Gtk::TreeSelection> refTreeSelection =
m_treeview.get_selection();
if(refTreeSelection)
{
Gtk::TreeModel::iterator sorted_iter =
m_refTreeSelection->get_selected();
if(sorted_iter)
{
Gtk::TreeModel::iterator iter =
m_refModelSort->convert_iter_to_child_iter(sorted_iter);
m_refModel->erase(iter);
}
}
}</programlisting>
<para><ulink url="&url_refdocs_base_gtk;TreeModelSort.html">TreeModelSort Reference</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-treeview-draganddrop">
<title>Drag and Drop</title>
<para>
<classname>Gtk::TreeView</classname> already implements simple drag-and-drop
when used with the <classname>Gtk::ListStore</classname> or
<classname>Gtk::TreeStore</classname> models. If necessary, it also allows you
to implement more complex behaviour when items are dragged and dropped, using
the normal <link linkend="chapter-draganddrop">Drag and Drop</link> API.
</para>
<sect2 id="treeview-reorderable-rows">
<title>Reorderable rows</title>
<para>
If you call <methodname>Gtk::TreeView::set_reorderable()</methodname> then your
TreeView's items can be moved within the treeview itself. This is demonstrated
in the <classname>TreeStore</classname> example.
</para>
<para>However, this does not allow you any control of which items can be dragged, and where they can be dropped. If you need that extra control then you might create a derived <literal>Gtk::TreeModel</literal> from <literal>Gtk::TreeStore</literal> or <literal>Gtk::ListStore</literal> and override the <literal>Gtk::TreeDragSource::row_draggable()</literal> and <literal>Gdk::TreeDragDest::row_drop_possible()</literal> virtual methods. You can examine the <literal>Gtk::TreeModel::Path</literal>s provided and allow or disallow dragging or dropping by returning <literal>true</literal> or <literal>false</literal>.</para>
<para>This is demonstrated in the drag_and_drop example.</para>
</sect2>
</sect1>
<sect1 id="sec-treeview-contextmenu">
<title>Popup Context Menu</title>
<para>
Lots of people need to implement right-click context menus for
<classname>TreeView</classname>'s so we will explain how to do that here to
save you some time. Apart from one or two points, it's much the same as a
normal context menu, as described in the <link linkend="sec-menus-popup">menus
chapter</link>.
</para>
<sect2 id="treeview-button-press-event">
<title>Handling <literal>button_press_event</literal></title>
<para>
To detect a click of the right mouse button, you need to handle the
<literal>button_press_event</literal> signal, and check exactly which button
was pressed. Because the <classname>TreeView</classname> normally handles this
signal completely, you need to either override the default signal handler in a
derived <classname>TreeView</classname> class, or use
<methodname>connect_notify()</methodname> instead of <methodname>connect()</methodname>.
You probably also want to call the default handler before doing anything else,
so that the right-click will cause the row to be selected first.
</para>
<para>This is demonstrated in the Popup Context Menu example.</para>
</sect2>
</sect1>
<sect1 id="sec-treeview-examples"><title>Examples</title>
<sect2 id="liststore-example"><title>ListStore</title>
<para>
This example has a <classname>Gtk::TreeView</classname> widget, with a
<classname>Gtk::ListStore</classname> model.
</para>
<figure id="figure-treeview-liststore">
<title>TreeView - ListStore</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;treeview_list.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;treeview/list/">Source Code</ulink></para>
</sect2>
<sect2 id="treestore-example"><title>TreeStore</title>
<para>
This example is very similar to the <classname>ListStore</classname> example,
but uses a <classname>Gtk::TreeStore</classname> model instead, and adds
children to the rows.
</para>
<figure id="figure-treeview-treestore">
<title>TreeView - TreeStore</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;treeview_tree.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;treeview/tree/">Source Code</ulink></para>
</sect2>
<sect2 id="sec-editable-cells-example"><title>Editable Cells</title>
<para>
This example is identical to the <classname>ListStore</classname> example, but
it uses <methodname>TreeView::append_column_editable()</methodname> instead of
<methodname>TreeView::append_column()</methodname>.
</para>
<figure id="figure-treeview-editablecells">
<title>TreeView - Editable Cells</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;treeview_editablecells.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;treeview/editable_cells/">Source Code</ulink></para>
</sect2>
<sect2 id="treeview-dnd-example"><title>Drag and Drop</title>
<para>
This example is much like the <classname>TreeStore</classname> example, but has
2 extra columns to indicate whether the row can be dragged, and whether it can
receive drag-and-dropped rows. It uses a derived
<classname>Gtk::TreeStore</classname> which overrides the virtual functions as
described in the <link linkend="sec-treeview-draganddrop">TreeView Drag and
Drop</link> section.
</para>
<figure id="figure-treeview-draganddrop">
<title>TreeView - Drag And Drop</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;treeview_draganddrop.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;treeview/drag_and_drop/">Source Code</ulink></para>
</sect2>
<sect2 id="treeview-popup-menu-example"><title>Popup Context Menu</title>
<para>
This example is much like the <classname>ListStore</classname> example, but
derives a custom <classname>TreeView</classname> in order to override the
<literal>button_press_event</literal>, and also to encapsulate the tree model
code in our derived class. See the <link
linkend="sec-treeview-contextmenu">TreeView Popup Context Menu</link>
section.
</para>
<figure id="figure-treeview-popup">
<title>TreeView - Popup Context Menu</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;treeview_popup.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;treeview/popup/">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-combobox">
<title>Combo Boxes</title>
<para>The <classname>ComboBox</classname> widget offers a list (or tree) of choices in a dropdown menu. If appropriate, it can show extra information about each item, such as text, a picture, a checkbox, or a progress bar. The <classname>ComboBox</classname> widget usually restricts the user to the available choices, but it can optionally have an <classname>Entry</classname>, allowing the user to enter arbitrary text if none of the available choices are suitable.
</para>
<para>The list is provided via a <classname>TreeModel</classname>, and columns from this model are added to the ComboBox's view with the <methodname>ComboBox::pack_start()</methodname> method. This provides flexibility and compile-time type-safety, but the <classname>ComboBoxText</classname> class provides a simpler text-based specialization in case that flexibility is not required.
</para>
<para><ulink url="&url_refdocs_base_gtk;ComboBox.html">Reference</ulink></para>
<sect1 id="sec-combobox-model">
<title>The model</title>
<para>The model for a ComboBox can be defined and filled exactly as for a <classname>TreeView</classname>. For instance, you might derive a ComboBox class with one integer and one text column, like so:
</para>
<programlisting>ModelColumns()
{ add(m_col_id); add(m_col_name); }
Gtk::TreeModelColumn<int> m_col_id;
Gtk::TreeModelColumn<Glib::ustring> m_col_name;
};
ModelColumns m_columns;</programlisting>
<para>After appending rows to this model, you should provide the model to the <classname>ComboBox</classname> with the <methodname>set_model()</methodname> method. Then use the <methodname>pack_start()</methodname> or <methodname>pack_end()</methodname> methods to specify what columns will be displayed in the ComboBox. As with the TreeView you may either use the default cell renderer by passing the <classname>TreeModelColumn</classname> to the pack methods, or you may instantiate a specific <classname>CellRenderer</classname> and specify a particular mapping with either <methodname>add_attribute()</methodname> or <methodname>set_cell_data_func()</methodname>. Note that these methods are in the <classname>CellLayout</classname> base class.</para>
</sect1>
<sect1 id="sec-combobox-get">
<title>The chosen item</title>
<para>To discover what item, if any, the user has chosen from the ComboBox, call <methodname>ComboBox::get_active()</methodname>. This returns a <classname>TreeModel::iterator</classname> that you can dereference to a <classname>Row</classname> in order to read the values in your columns. For instance, you might read an integer ID value from the model, even though you have chosen only to show the human-readable description in the ComboBox. For instance:
</para>
<programlisting>Gtk::TreeModel::iterator iter = m_Combo.get_active();
if(iter)
{
Gtk::TreeModel::Row row = *iter;
//Get the data for the selected row, using our knowledge
//of the tree model:
int id = row[m_Columns.m_col_id];
set_something_id_chosen(id); //Your own function.
}
else
set_nothing_chosen(); //Your own function.</programlisting>
</sect1>
<sect1 id="sec-combobox-changes">
<title>Responding to changes</title>
<para>
You might need to react to every change of selection in the ComboBox, for instance to update other widgets. To do so, you should handle the <literal>changed</literal> signal. For instance:
</para>
<programlisting>m_combo.signal_changed().connect( sigc::mem_fun(*this,
&ExampleWindow::on_combo_changed) );</programlisting>
</sect1>
<sect1 id="combobox-example-full"><title>Full Example</title>
<figure id="figure-combobox-complex">
<title>ComboBox</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;combobox_complex.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;combobox/complex">Source Code</ulink></para>
</sect1>
<sect1 id="combobox-example-simple"><title>Simple Text Example</title>
<figure id="figure-combobox-text">
<title>ComboBoxText</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;combobox_text.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;combobox/text">Source Code</ulink></para>
</sect1>
<sect1 id="sec-comboboxentry">
<title>ComboBox with an Entry</title>
<para>A <classname>ComboBox</classname> may contain an <classname>Entry</classname> widget for entering of arbitrary text, by specifying <literal>true</literal> for the constructor's <literal>has_entry</literal> parameter.</para>
<sect2 id="sec-comboboxentry-text-column">
<title>The text column</title>
<para>So that the <classname>Entry</classname> can interact with the drop-down list of choices, you must specify which of your model columns is the text column, with <methodname>set_entry_text_column()</methodname>. For instance:
<programlisting>m_combo.set_entry_text_column(m_columns.m_col_name);</programlisting>
</para>
<para>
When you select a choice from the drop-down menu, the value from this column will be placed in the <classname>Entry</classname>.
</para>
</sect2>
<sect2 id="sec-comboboxentry-model">
<title>The entry</title>
<para>Because the user may enter arbitrary text, an active model row isn't enough to tell us what text the user has entered. Therefore, you should retrieve the <classname>Entry</classname> widget with the <methodname>ComboBox::get_entry()</methodname> method and call <methodname>get_text()</methodname> on that.
</para>
</sect2>
<sect2 id="sec-comboboxentry-changes">
<title>Responding to changes</title>
<para>
When the user enters arbitrary text, it may not be enough to connect to the
<literal>changed</literal> signal, which is emitted for every typed character.
It is not emitted when the user presses the Enter key. Pressing the Enter key or
moving the keyboard focus to another widget may signal that the user has finished
entering text. To be notified of these events, connect to the
<classname>Entry</classname>'s <literal>activate</literal> and
<literal>focus_out_event</literal> signals, like so
<programlisting>Gtk::Entry* entry = m_Combo.get_entry();
if (entry)
{
// The Entry shall receive focus-out events.
entry->add_events(Gdk::FOCUS_CHANGE_MASK);
// Alternatively you can connect to m_Combo.signal_changed().
entry->signal_changed().connect(sigc::mem_fun(*this,
&ExampleWindow::on_entry_changed) );
entry->signal_activate().connect(sigc::mem_fun(*this,
&ExampleWindow::on_entry_activate) );
entry->signal_focus_out_event().connect(sigc::mem_fun(*this,
&ExampleWindow::on_entry_focus_out_event) );
}</programlisting>
The <literal>changed</literal> signals of <classname>ComboBox</classname> and
<classname>Entry</classname> are both emitted for every change. It doesn't matter
which one you connect to. But only <classname>Entry</classname>'s
<literal>focus_out_event</literal> signal is useful here.
</para>
<para>
X events are described in more detail in the
<link linkend="sec-xeventsignals">X Event signals</link> section in the appendix.
</para>
</sect2>
<sect2 id="comboboxentry-example-full"><title>Full Example</title>
<figure id="figure-comboboxentry-complex">
<title>ComboBox with Entry</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;comboboxentry_complex.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;combobox/entry_complex">Source Code</ulink></para>
</sect2>
<sect2 id="comboboxentry-example-simple"><title>Simple Text Example</title>
<figure id="figure-comboboxentry-text">
<title>ComboBoxText with Entry</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;comboboxentry_text.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;combobox/entry_text">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-textview">
<title>TextView</title>
<para>
The <classname>TextView</classname> widget can be used to display and edit
large amounts of formatted text. Like the <classname>TreeView</classname>, it
has a model/view design. In this case the <classname>TextBuffer</classname> is
the model.
</para>
<sect1 id="sec-textview-buffer">
<title>The Buffer</title>
<para>
<classname>Gtk::TextBuffer</classname> is a model containing the data for the
<classname>Gtk::TextView</classname>, like the
<classname>Gtk::TreeModel</classname> used by <classname>Gtk::TreeView</classname>.
This allows two or more <classname>Gtk::TextView</classname>s to share the same
<classname>TextBuffer</classname>, and allows those TextBuffers to be displayed
slightly differently. Or you could maintain several
<classname>Gtk::TextBuffer</classname>s and choose to display each one at different
times in the same <classname>Gtk::TextView</classname> widget.
</para>
<para>
The <classname>TextView</classname> creates its own default
<classname>TextBuffer</classname>, which you can access via the
<methodname>get_buffer()</methodname> method.
</para>
<para><ulink url="&url_refdocs_base_gtk;TextBuffer.html">Reference</ulink></para>
<sect2 id="textview-iterators">
<title>Iterators</title>
<para>
</para>
</sect2>
<sect2 id="textview-formatting">
<title>Tags and Formatting</title>
<sect3 id="textview-formatting-tags">
<title>Tags</title>
<para>
To specify that some text in the buffer should have specific formatting, you must define a tag to hold that formatting information, and then apply that tag to the region of text. For instance, to define the tag and its properties:
</para>
<programlisting>Glib::RefPtr<Gtk::TextBuffer::Tag> refTagMatch =
Gtk::TextBuffer::Tag::create();
refTagMatch->property_background() = "orange";</programlisting>
<para>
You can specify a name for the <classname>Tag</classname> when using the
<methodname>create()</methodname> method, but it is not necessary.
</para>
<para>
The <classname>Tag</classname> class has many other properties.
</para>
<para><ulink url="&url_refdocs_base_gtk;TextTag.html">Reference</ulink></para>
</sect3>
<sect3 id="textview-formatting-tagtable">
<title>TagTable</title>
<para>
Each <classname>Gtk::TextBuffer</classname> uses a
<classname>Gtk::TextBuffer::TagTable</classname>, which contains the
<classname>Tag</classname>s for that buffer. 2 or more
<classname>TextBuffer</classname>s may share the same
<classname>TagTable</classname>. When you create <classname>Tag</classname>s
you should add them to the <classname>TagTable</classname>. For instance:
</para>
<programlisting>Glib::RefPtr<Gtk::TextBuffer::TagTable> refTagTable =
Gtk::TextBuffer::TagTable::create();
refTagTable->add(refTagMatch);
//Hopefully a future version of >kmm; will have a set_tag_table() method,
//for use after creation of the buffer.
Glib::RefPtr<Gtk::TextBuffer> refBuffer =
Gtk::TextBuffer::create(refTagTable);</programlisting>
<para>
You can also use <methodname>get_tag_table()</methodname> to get, and maybe modify,
the <classname>TextBuffer</classname>'s default <classname>TagTable</classname>
instead of creating one explicitly.
</para>
<para><ulink url="&url_refdocs_base_gtk;TextTagTable.html">Reference</ulink></para>
</sect3>
<sect3 id="textview-formatting-applying-tags">
<title>Applying Tags</title>
<para>
If you have created a <classname>Tag</classname> and added it to the
<classname>TagTable</classname>, you may apply that tag to part of the
<classname>TextBuffer</classname> so that some of the text is displayed with that
formatting. You define the start and end of the range of text by specifying
<classname>Gtk::TextBuffer::iterator</classname>s. For instance:
</para>
<programlisting>refBuffer->apply_tag(refTagMatch, iterRangeStart, iterRangeStop);</programlisting>
<para>
Or you could specify the tag when first inserting the text:
<programlisting>refBuffer->insert_with_tag(iter, "Some text", refTagMatch);</programlisting>
</para>
<para>
You can apply more than one <classname>Tag</classname> to the same text, by
using <methodname>apply_tag()</methodname> more than once, or by using
<methodname>insert_with_tags()</methodname>. The <classname>Tag</classname>s might
specify different values for the same properties, but you can resolve these
conflicts by using <methodname>Tag::set_priority()</methodname>.
</para>
</sect3>
</sect2>
<sect2 id="textview-marks">
<title>Marks</title>
<para>
<classname>TextBuffer</classname> iterators are generally invalidated when the
text changes, but you can use a <classname>Gtk::TextBuffer::Mark</classname> to
remember a position in these situations. For instance,
</para>
<programlisting>Glib::RefPtr<Gtk::TextBuffer::Mark> refMark =
refBuffer->create_mark(iter);</programlisting>
<para>
You can then use the <methodname>get_iter()</methodname> method later to create an
iterator for the <classname>Mark</classname>'s new position.
</para>
<para>
There are two built-in <classname>Mark</classname>s - <literal>insert</literal>
and <literal>selection_bound</literal>, which you can access with
<classname>TextBuffer</classname>'s <methodname>get_insert()</methodname> and
<methodname>get_selection_bound()</methodname> methods.
</para>
<para><ulink url="&url_refdocs_base_gtk;TextMark.html">Reference</ulink></para>
</sect2>
<sect2 id="textview-view">
<title>The View</title>
<para>
As mentioned above, each <classname>TextView</classname> has a
<classname>TextBuffer</classname>, and one or more
<classname>TextView</classname>s can share the same
<classname>TextBuffer</classname>.
</para>
<para>
Like the <classname>TreeView</classname>, you should probably put your
<classname>TextView</classname> inside a <classname>ScrolledWindow</classname>
to allow the user to see and move around the whole text area with
scrollbars.
</para>
<para><ulink url="&url_refdocs_base_gtk;TextView.html">Reference</ulink></para>
<sect3 id="textview-default-formatting">
<title>Default formatting</title>
<para>
<classname>TextView</classname> has various methods which allow you to change
the presentation of the buffer for this particular view. Some of these may be
overridden by the <classname>Gtk::TextTag</classname>s in the buffer, if they
specify the same things. For instance, <methodname>set_left_margin()</methodname>,
<methodname>set_right_margin()</methodname>, <methodname>set_indent()</methodname>,
etc.
</para>
</sect3>
<sect3 id="textview-scrolling">
<title>Scrolling</title>
<para>
<classname>Gtk::TextView</classname> has various
<methodname>scroll_to_*()</methodname> methods. These allow you to ensure that a
particular part of the text buffer is visible. For instance, your application's
Find feature might use <methodname>Gtk::TextView::scroll_to_iter()</methodname> to
show the found text.
</para>
</sect3>
</sect2>
</sect1>
<sect1 id="sec-widgets-and-childanchors">
<title>Widgets and ChildAnchors</title>
<para>
You can embed widgets, such as <classname>Gtk::Button</classname>s, in the
text. Each such child widget needs a <classname>ChildAnchor</classname>.
ChildAnchors are associated with <classname>iterators</classname>. For
instance, to create a child anchor at a particular position, use
<methodname>Gtk::TextBuffer::create_child_anchor()</methodname>:
</para>
<programlisting>Glib::RefPtr<Gtk::TextChildAnchor> refAnchor =
refBuffer->create_child_anchor(iter);</programlisting>
<para>
Then, to add a widget at that position, use
<methodname>Gtk::TextView::add_child_at_anchor()</methodname>:
</para>
<programlisting>m_TextView.add_child_at_anchor(m_Button, refAnchor);</programlisting>
<para><ulink url="&url_refdocs_base_gtk;TextChildAnchor.html">Reference</ulink></para>
</sect1>
<sect1 id="sec-textview-examples"><title>Examples</title>
<sect2 id="textview-example-simple"><title>Simple Example</title>
<figure id="figure-textview">
<title>TextView</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;textview.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;textview/">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-menus-and-toolbars">
<title>Menus and Toolbars</title>
<para>
There are specific APIs for Menus and toolbars, but you should usually deal
with them together, using the <classname>UIManager</classname> to define
<classname>Action</classname>s which you can then arrange in menus and toolbars.
In this way you can handle activation of the action instead of responding to
the menu and toolbar items separately. And you can enable or disable both the
menu and toolbar item via the action.
</para>
<para>
This involves the use of the <classname>Gtk::ActionGroup</classname>,
<classname>Gtk::Action</classname>, and <classname>UIManager</classname>
classes, all of which should be instantiated via their
<methodname>create()</methodname> methods, which return
<classname>RefPtr</classname>s.
</para>
<sect1 id="sec-actions">
<title>Actions</title>
<para>
First create the <classname>Action</classname>s and add them to an
<classname>ActionGroup</classname>, with
<methodname>ActionGroup::add()</methodname>.
</para>
<para>
The arguments to <methodname>Action::create()</methodname> specify the action's
name and how it will appear in menus and toolbars.
</para>
<para>
You can also specify a signal handler when calling
<methodname>ActionGroup::add()</methodname>. This signal handler will be called
when the action is activated via either a menu item or a toolbar button.
</para>
<para>Note that you must specify actions for sub menus as well as menu items.</para>
<para>For instance:
</para>
<programlisting>m_refActionGroup = Gtk::ActionGroup::create();
m_refActionGroup->add( Gtk::Action::create("MenuFile", "_File") );
m_refActionGroup->add( Gtk::Action::create("New", "_New"),
sigc::mem_fun(*this, &ExampleWindow::on_action_file_new) );
m_refActionGroup->add( Gtk::Action::create("ExportData", "Export Data"),
sigc::mem_fun(*this, &ExampleWindow::on_action_file_open) );
m_refActionGroup->add( Gtk::Action::create("Quit", "_Quit"),
sigc::mem_fun(*this, &ExampleWindow::on_action_file_quit) );</programlisting>
<para>Note that this is where we specify the names of the actions as they will be seen by users in menus and toolbars. Therefore, this is where you should make strings translatable, by putting them inside the _() macro.</para>
</sect1>
<sect1 id="sec-uimanager">
<title>UIManager</title>
<para>
Next you should create a <classname>UIManager</classname> and add the
<classname>ActionGroup</classname> to the <classname>UIManager</classname> with
<methodname>insert_action_group()</methodname> At this point is also a good idea to
tell the parent window to respond to the specified keyboard shortcuts, by using
<methodname>add_accel_group()</methodname>.
</para>
<para>For instance,
</para>
<programlisting>Glib::RefPtr<Gtk::UIManager> m_refUIManager =
Gtk::UIManager::create();
m_refUIManager->insert_action_group(m_refActionGroup);
add_accel_group(m_refUIManager->get_accel_group());</programlisting>
<para>
Then, you can define the actual visible layout of the menus and toolbars, and
add the UI layout to the <classname>UIManager</classname>. This "ui
string" uses an XML format, in which you should mention the names of the
actions that you have already created. For instance:
</para>
<programlisting>Glib::ustring ui_info =
"<ui>"
" <menubar name='MenuBar'>"
" <menu action='MenuFile'>"
" <menuitem action='New'/>"
" <menuitem action='Open'/>"
" <separator/>"
" <menuitem action='Quit'/>"
" </menu>"
" <menu action='MenuEdit'>"
" <menuitem action='Cut'/>"
" <menuitem action='Copy'/>"
" <menuitem action='Paste'/>"
" </menu>"
" </menubar>"
" <toolbar name='ToolBar'>"
" <toolitem action='Open'/>"
" <toolitem action='Quit'/>"
" </toolbar>"
"</ui>";
m_refUIManager->add_ui_from_string(ui_info);</programlisting>
<para>Remember that these names are just the identifiers that we used when creating the actions. They are not the text that the user will see in the menus and toolbars. We provided those human-readable names when we created the actions.</para>
<para>
To instantiate a <classname>Gtk::MenuBar</classname> or
<classname>Gtk::Toolbar</classname> which you can actually show, you should use
the <methodname>UIManager::get_widget()</methodname> method, and then add the widget
to a container. For instance:
</para>
<programlisting>Gtk::Widget* pMenubar = m_refUIManager->get_widget("/MenuBar");
pBox->add(*pMenuBar, Gtk::PACK_SHRINK);</programlisting>
</sect1>
<sect1 id="sec-menus-popup"><title>Popup Menus</title>
<para>
<classname>Menus</classname> are normally just added to a window, but they can
also be displayed temporarily as the result of a mouse button click. For
instance, a context menu might be displayed when the user clicks their right
mouse button.
</para>
<para>The UI layout for a popup menu should use the <literal>popup</literal> node. For instance:
</para>
<programlisting>Glib::ustring ui_info =
"<ui>"
" <popup name='PopupMenu'>"
" <menuitem action='ContextEdit'/>"
" <menuitem action='ContextProcess'/>"
" <menuitem action='ContextRemove'/>"
" </popup>"
"</ui>";
m_refUIManager->add_ui_from_string(ui_info);</programlisting>
<para>
To show the popup menu, use <classname>Gtk::Menu</classname>'s
<methodname>popup()</methodname> method, providing the button identifier and the
time of activation, as provided by the <literal>button_press_event</literal>
signal, which you will need to handle anyway. For instance:
</para>
<programlisting>bool ExampleWindow::on_button_press_event(GdkEventButton* event)
{
if( (event->type == GDK_BUTTON_PRESS) &&
(event->button == 3) )
{
m_Menu_Popup->popup(event->button, event->time);
return true; //It has been handled.
}
else
return false;
}</programlisting>
</sect1>
<sect1 id="sec-menus-examples">
<title>Examples</title>
<sect2 id="menu-example-main"><title>Main Menu example</title>
<figure id="figure-menus-mainmenu">
<title>Main Menu</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;main_menu.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;menus/main_menu/">Source Code</ulink></para>
</sect2>
<sect2 id="menu-example-popup"><title>Popup Menu example</title>
<figure id="figure-menus-popup">
<title>Popup Menu</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;menu_popup.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;menus/popup/">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-toolpalette">
<title>ToolPalette</title>
<para>A <classname>ToolPalette</classname> is similar to a <classname>Toolbar</classname> but can contain a grid of items, categorized into groups. The user may hide or expand each group. As in a toolbar, the items may be displayed as only icons, as only text, or as icons with text.
</para>
<para>The <classname>ToolPalette</classname>'s items might be dragged or simply activated. For instance, the user might drag objects to a canvas to create new items there. Or the user might click an item to activate a certain brush size in a drawing application.</para>
<para><classname>ToolItemGroup</classname>s should be added to the tool palette via the base class's <function>Gtk::Container::add()</function> method, for instance like so:
</para>
<para>
<programlisting>
Gtk::ToolItemGroup* group_brushes =
Gtk::manage(new Gtk::ToolItemGroup("Brushes"));
m_ToolPalette.add(*group_brushes);
</programlisting>
</para>
<para>
<classname>Gtk::ToolItem</classname>s can then be added to the group. For instance, like so:
</para>
<para>
<programlisting>
Gtk::ToolButton* button = Gtk::manage(new Gtk::ToolButton(icon, "Big"));
button->set_tooltip_text("Big Brush);
group_brushes->insert(*button);
</programlisting>
</para>
<para>You might then handle the <classname>ToolButton</classname>'s <literal>clicked</literal> signal. Alternatively, you could allow the item to be dragged to another widget, by calling <methodname>Gtk::ToolPalette::add_drag_dest()</methodname> and then using <methodname>Gtk::ToolPalette::get_drag_item()</methodname> in the other widget's <literal>drag_data_received</literal> signal handler.</para>
<para><ulink url="&url_refdocs_base_gtk;ToolPalette.html">ToolPalette Reference</ulink></para>
<para><ulink url="&url_refdocs_base_gtk;ToolItemGroup.html">ToolItemGroup Reference</ulink></para>
<para><ulink url="&url_refdocs_base_gtk;ToolItem.html">ToolItem Reference</ulink></para>
<sect1 id="toolpallete-dranganddrop">
<title>Drag and Drop</title>
<para>Call <methodname>add_drag_dest()</methodname> to allow items or groups to be dragged from the tool palette to a particular destination widget. You can then use <methodname>get_drag_item()</methodname> to discover which ToolItem or ToolItemGroup is being dragged. You can use <literal>dynamic_cast</literal> to discover whether it is an item or a group. For instance, you might use this in your <literal>drag_data_received</literal> signal handler, to add a dropped item, or to show a suitable icon while dragging.</para>
<para>See the <link linkend="chapter-draganddrop">Drag and Drop</link> chapter for general advice about Drag and Drop with gtkmm.</para>
</sect1>
<sect1 id="toolpalette-example"><title>ToolPalette Example</title>
<para>This example adds a <classname>ToolPalette</classname> and a <classname>DrawingArea</classname> to a window and allows the user to drag icons from the tool palette to the drawing area. The tool palette contains several groups of items. The combo boxes allow the user to change the style and orientation of the tool palette.</para>
<figure id="figure-toolpalette">
<title>ToolPalette</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;toolpalette.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;toolpalette/">Source Code</ulink></para>
</sect1>
</chapter>
<chapter id="chapter-adjustment">
<title>Adjustments</title>
<para>
>kmm; has various widgets that can be visually adjusted using the mouse or
the keyboard, such as the <classname>Range</classname> widgets (described in
the <link linkend="chapter-range-widgets">Range Widgets</link> section). There are
also a few widgets that display some adjustable part of a larger area, such as
the <classname>Viewport</classname> widget. These widgets have
<classname>Gtk::Adjustment</classname> objects that express this common part of
their API.
</para>
<para>
So that applications can react to changes, for instance when a user moves a
scrollbar, <classname>Gtk::Adjustment</classname> has a
<literal>value_changed</literal> signal. You can then use the
<methodname>get_value()</methodname> method to discover the new value.
</para>
<sect1 id="sec-creating-adjustment">
<title>Creating an Adjustment</title>
<para>
The <classname>Gtk::Adjustment</classname> is created by its
<methodname>create()</methodname> method which is as follows:
</para>
<programlisting>Glib::RefPtr<Gtk::Adjustment> Gtk::Adjustment::create(
double value,
double lower,
double upper,
double step_increment = 1,
double page_increment = 10,
double page_size = 0);</programlisting>
<para>
The <parameter>value</parameter> argument is the initial value of the
adjustment, usually corresponding to the topmost or leftmost position of an
adjustable widget. The <parameter>lower</parameter> and
<parameter>upper</parameter> arguments specify the possible range of values
which the adjustment can hold. The
<parameter>step_increment</parameter> argument specifies the smaller of
the two increments by which the user can change the value, while the
<parameter>page_increment</parameter> is the larger one. The
<parameter>page_size</parameter> argument usually corresponds somehow to
the visible area of a panning widget. The <parameter>upper</parameter> argument
is used to represent the bottommost or rightmost coordinate in a panning
widget's child.
<!-- TODO: Investigate the upper argument properly. There was some unclear stuff about it not always being the upper value. -->
</para>
</sect1>
<sect1 id="sec-adjustments-easy">
<title>Using Adjustments the Easy Way</title>
<para>
The adjustable widgets can be roughly divided into those which use and
require specific units for these values, and those which treat them as
arbitrary numbers.
</para>
<para>
The group which treats the values as arbitrary numbers includes the
<classname>Range</classname> widgets (<classname>Scrollbar</classname> and
<classname>Scale</classname>), the <classname>ScaleButton</classname> widget,
and the <classname>SpinButton</classname> widget. These widgets are typically
"adjusted" directly by the user with the mouse or keyboard. They will treat the
<parameter>lower</parameter> and <parameter>upper</parameter> values of an
adjustment as a range within which the user can manipulate the adjustment's
<parameter>value</parameter>. By default, they will only modify the
<parameter>value</parameter> of an adjustment.
</para>
<para>
The other group includes the <classname>Viewport</classname> widget and the
<classname>ScrolledWindow</classname> widget. All of these widgets use pixel
values for their adjustments. These are also typically adjusted indirectly
using scrollbars. While all widgets which use adjustments can either create
their own adjustments or use ones you supply, you'll generally want to let this
particular category of widgets create its own adjustments.
</para>
<para>
If you share an adjustment object between a Scrollbar and a TextView
widget, manipulating the scrollbar will automagically adjust the TextView
widget. You can set it up like this:
</para>
<programlisting>// creates its own adjustments
Gtk::TextView textview;
// uses the newly-created adjustment for the scrollbar as well
Gtk::Scrollbar vscrollbar (textview.get_vadjustment(), Gtk::ORIENTATION_VERTICAL);</programlisting>
</sect1>
<sect1 id="sec-adjustment-internals">
<title>Adjustment Internals</title>
<para>
OK, you say, that's nice, but what if I want to create my own handlers to
respond when the user adjusts a <classname>Range</classname> widget or a
<classname>SpinButton</classname>. To access the value of a
<classname>Gtk::Adjustment</classname>, you can use the
<methodname>get_value()</methodname> and <methodname>set_value()</methodname> methods:
</para>
<para>
As mentioned earlier, <classname>Gtk::Adjustment</classname> can emit signals.
This is, of course, how updates happen automatically when you share an
<classname>Adjustment</classname> object between a
<classname>Scrollbar</classname> and another adjustable widget; all adjustable
widgets connect signal handlers to their adjustment's
<literal>value_changed</literal> signal, as can your program.
</para>
<para>
So, for example, if you have a <classname>Scale</classname> widget, and you
want to change the rotation of a picture whenever its value changes, you would
create a signal handler like this:
</para>
<programlisting>void cb_rotate_picture (MyPicture* picture)
{
picture->set_rotation(adj->get_value());
...</programlisting>
<para>
and connect it to the scale widget's adjustment like this:
</para>
<programlisting>adj->signal_value_changed().connect(sigc::bind<MyPicture*>(sigc::mem_fun(*this,
&cb_rotate_picture), picture));</programlisting>
<para>
What if a widget reconfigures the <parameter>upper</parameter> or
<parameter>lower</parameter> fields of its <classname>Adjustment</classname>,
such as when a user adds more text to a text widget? In this case, it emits
the <literal>changed</literal> signal.
</para>
<para>
<classname>Range</classname> widgets typically connect a handler to this
signal, which changes their appearance to reflect the change - for example, the
size of the slider in a scrollbar will grow or shrink in inverse proportion to
the difference between the <parameter>lower</parameter> and
<parameter>upper</parameter> values of its
<classname>Adjustment</classname>.
</para>
<para>
You probably won't ever need to attach a handler to this signal, unless you're
writing a new type of range widget.
</para>
<programlisting>adjustment->signal_changed();</programlisting>
</sect1>
</chapter>
<chapter id="chapter-widgets-without-xwindows">
<title>Widgets Without X-Windows</title>
<para>
Some Widgets do not have an associated X-Window, so they therefore do not
receive X events. This means that the signals described in the <link
linkend="sec-xeventsignals">X event signals</link> section will not be
emitted. If you want to capture events for these widgets you can use a special
container called <classname>Gtk::EventBox</classname>, which is described in
the <link linkend="sec-eventbox">EventBox</link> section.
</para>
<para>
Here is a list of some of these Widgets:
</para>
<programlisting>Gtk::Alignment
Gtk::Arrow
Gtk::AspectFrame
Gtk::Bin
Gtk::Box
Gtk::Button
Gtk::CheckButton
Gtk::Fixed
Gtk::Frame
Gtk::Grid
Gtk::Image
Gtk::Label
Gtk::MenuItem
Gtk::Notebook
Gtk::Paned
Gtk::RadioButton
Gtk::Range
Gtk::ScrolledWindow
Gtk::Separator
Gtk::Table (deprecated from >kmm; version 3.4)
Gtk::Toolbar</programlisting>
<para>
These widgets are mainly used for decoration or layout, so you won't often need
to capture events on them. They are intended to have no X-Window in order to improve performance.
</para>
<sect1 id="sec-eventbox">
<title>EventBox</title>
<para>
Some >kmm; widgets don't have associated X windows; they draw on
their parents' windows. Because of this, they cannot receive events.
Also, if they are incorrectly sized, they don't clip, so you can get
messy overwriting etc. To receive events on one of these widgets, you can place it
inside an <classname>EventBox</classname> widget and then call
<methodname>Gtk::Widget::set_events()</methodname> on the EventBox before showing it.</para>
<para>Although the name
<classname>EventBox</classname> emphasises the event-handling method, the
widget can also be used for clipping (and more; see the example below).
</para>
<!--
<para>TODO: Why don't they have X Windows - explain clipping.
Also, how does this affect platform such as Windows and MacOS that don't use X.
</para>
-->
<para>
The constructor for <classname>Gtk::EventBox</classname> is:
</para>
<programlisting>Gtk::EventBox();</programlisting>
<para>
A child widget can be added to the <classname>EventBox</classname> using:
</para>
<programlisting>event_box.add(child_widget);</programlisting>
<para><ulink url="&url_refdocs_base_gtk;EventBox.html">Reference</ulink></para>
<sect2 id="eventbox-example">
<title>Example</title>
<para>
The following example demonstrates both uses of an
<classname>EventBox</classname> - a label is created that is clipped to a small
box, and set up so that a mouse-click on the label causes the program to exit.
Resizing the window reveals varying amounts of the label.
</para>
<figure id="figure-eventbox">
<title>EventBox</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;eventbox.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;eventbox">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-dialogs">
<title>Dialogs</title>
<para>
Dialogs are used as secondary windows, to provide specific information or to
ask questions. <classname>Gtk::Dialog</classname> windows contain a few pre-packed
widgets to ensure consistency, and a <methodname>run()</methodname> method which
blocks until the user dismisses the dialog.
</para>
<para>
There are several derived <classname>Dialog</classname> classes which you might
find useful. <classname>Gtk::MessageDialog</classname> is used for most simple
notifications. But at other times you might need to derive your own dialog
class to provide more complex functionality.
</para>
<para>
To pack widgets into a custom dialog, you should pack them into the
<classname>Gtk::Box</classname>, available via
<methodname>get_content_area()</methodname>. To just add a <classname>Button</classname>
to the bottom of the <classname>Dialog</classname>, you could use the
<methodname>add_button()</methodname> method.
</para>
<para>
The <methodname>run()</methodname> method returns an <literal>int</literal>. This
may be a value from the <literal>Gtk::ResponseType</literal> if the user
closed the dialog by clicking a standard button, or it could be the custom
response value that you specified when using <methodname>add_button()</methodname>.
</para>
<para><ulink url="&url_refdocs_base_gtk;Dialog.html">Reference</ulink></para>
<sect1 id="sec-dialogs-messagedialog"><title>MessageDialog</title>
<para>
<classname>MessageDialog</classname> is a convenience class, used to create
simple, standard message dialogs, with a message, an icon, and buttons for user
response. You can specify the type of message and the text in the constructor,
as well as specifying standard buttons via the
<literal>Gtk::ButtonsType</literal> enum.
</para>
<para><ulink url="&url_refdocs_base_gtk;MessageDialog.html">Reference</ulink></para>
<sect2 id="messagedialog-example">
<title>Example</title>
<figure id="figure-dialogs-messagedialog">
<title>MessageDialog</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;dialogs_messagedialog.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;dialogs/messagedialog">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-dialogs-filechooserdialog"><title>FileChooserDialog</title>
<para>
The <classname>FileChooserDialog</classname> is suitable for use with
"Open" or "Save" menu items.
</para>
<para>
Most of the useful member methods for this class are actually in the
<classname>Gtk::FileChooser</classname> base class.
</para>
<para><ulink url="&url_refdocs_base_gtk;FileChooserDialog.html">Reference</ulink></para>
<sect2 id="filechooserdialog-example">
<title>Example</title>
<figure id="figure-dialogs-filechooser">
<title>FileChooser</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;dialogs_filechooser.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;dialogs/filechooserdialog">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-color-selection-dialog"><title>ColorChooserDialog</title>
<para>
The <classname>ColorChooserDialog</classname> allows the user to choose a
color. The <classname>ColorButton</classname> opens a color selection dialog
when it is clicked.
</para>
<para><ulink url="&url_refdocs_base_gtk;ColorChooserDialog.html">Reference</ulink></para>
<sect2 id="colorchooserdialog-example">
<title>Example</title>
<figure id="figure-dialogs-colorchooserdialog">
<title>ColorChooserDialog</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;dialogs_colorchooserdialog.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;dialogs/colorchooserdialog">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-font-chooser-dialog"><title>FontChooserDialog</title>
<para>
The <classname>FontChooserDialog</classname> allows the user to choose a
font. The <classname>FontButton</classname> opens a font chooser dialog
when it is clicked.
</para>
<para><ulink url="&url_refdocs_base_gtk;FontChooserDialog.html">Reference</ulink></para>
<sect2 id="fontchooserdialog-example">
<title>Example</title>
<figure id="figure-dialogs-fontchooserdialog">
<title>FontChooserDialog</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;dialogs_fontchooserdialog.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;dialogs/fontchooserdialog">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-about-dialog"><title>Non-modal AboutDialog</title>
<para>
The <classname>AboutDialog</classname> offers a simple way to display information
about a program, like its logo, name, copyright, website and license.
</para>
<para>
Most dialogs in this chapter are modal, that is, they freeze the rest of
the application while they are shown. It's also possible to create a non-modal
dialog, which does not freeze other windows in the application.
The following example shows a non-modal <classname>AboutDialog</classname>. This is
perhaps not the kind of dialog you would normally make non-modal, but non-modal
dialogs can be useful in other cases. E.g. <application>gedit</application>'s
search-and-replace dialog is non-modal.
</para>
<para><ulink url="&url_refdocs_base_gtk;AboutDialog.html">Reference</ulink></para>
<sect2 id="aboutdialog-example">
<title>Example</title>
<figure id="figure-dialogs-about">
<title>AboutDialog</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;dialogs_about.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;dialogs/aboutdialog">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-drawingarea">
<title>The Drawing Area Widget</title>
<para>
The <classname>DrawingArea</classname> widget is a blank window that gives
you the freedom to create any graphic you desire. Along with that freedom
comes the responsibility to handle draw signals on the widget. When a
widget is first shown, or when it is covered and then uncovered again it
needs to redraw itself. Most widgets have code to do this, but the
DrawingArea does not, allowing you to write your own draw signal
handler to determine how the contents of the widget will be drawn. This is
most often done by overriding the virtual
<methodname>on_draw()</methodname> member function.
</para>
<para>
GTK+ uses the <ulink url="http://cairographics.org">Cairo</ulink> drawing API.
With >kmm;, you may use the <ulink url="http://www.cairographics.org/cairomm/">cairomm</ulink> C++ API for cairo.
</para>
<para>
You can draw very sophisticated shapes using Cairo, but the methods to do
so are quite basic. Cairo provides methods for drawing straight lines,
curved lines, and arcs (including circles). These basic shapes can be
combined to create more complex shapes and paths which can be filled with
solid colors, gradients, patterns, and other things. In addition, Cairo
can perform complex transformations, do compositing of images, and render
antialiased text.
</para>
<note>
<title>Cairo and Pango</title>
<para>Although Cairo can render text, it's not meant to be a replacement for
Pango. Pango is a better choice if you need to perform more advanced
text rendering such as wrapping or ellipsizing text. Drawing text with
Cairo should only be done if the text is part of a graphic.</para>
</note>
<para>
In this section of the tutorial, we'll cover the basic Cairo drawing
model, describe each of the basic drawing elements in some detail (with
examples), and then present a simple application that uses Cairo to draw
a custom clock widget.
</para>
<sect1 id="sec-cairo-drawing-model">
<title>The Cairo Drawing Model</title>
<para>
The basic concept of drawing in Cairo involves defining 'invisible'
paths and then stroking or filling them to make them visible.
</para>
<para>
To do any drawing in >kmm; with Cairo, you must first create a
<classname>Cairo::Context</classname> object. This class holds all of the graphics state parameters that
describe how drawing is to be done. This includes information such as
line width, color, the surface to draw to, and many other things. This
allows the actual drawing functions to take fewer arguments to simplify
the interface. In >kmm;, a <classname>Cairo::Context</classname> is
created by calling the
<methodname>Gdk::Window::create_cairo_context()</methodname> function.
Since Cairo contexts are reference-counted objects, this function
returns a <classname>Cairo::RefPtr<Cairo::Context></classname>
object.
</para>
<para>
The following example shows how to set up a Cairo context with a
foreground color of red and a width of 2. Any drawing functions that
use this context will use these settings.
</para>
<programlisting>Gtk::DrawingArea myArea;
Cairo::RefPtr<Cairo::Context> myContext = myArea.get_window()->create_cairo_context();
myContext->set_source_rgb(1.0, 0.0, 0.0);
myContext->set_line_width(2.0);</programlisting>
<para>
Each <classname>Cairo::Context</classname> is associated with a
particular <classname>Gdk::Window</classname>, so the first line of the
above example creates a <classname>Gtk::DrawingArea</classname> widget
and the second line uses its associated
<classname>Gdk::Window</classname> to create a
<classname>Cairo::Context</classname> object. The final two lines
change the graphics state of the context.
</para>
<para>
There are a number of graphics state variables that can be set for a
Cairo context. The most common context attributes are color (using
<methodname>set_source_rgb()</methodname> or
<methodname>set_source_rgba()</methodname> for translucent colors), line
width (using <methodname>set_line_width()</methodname>), line dash pattern
(using <methodname>set_dash()</methodname>), line cap style (using
<methodname>set_line_cap()</methodname>), and line join style (using
<methodname>set_line_join()</methodname>), and font styles (using
<methodname>set_font_size()</methodname>,
<methodname>set_font_face()</methodname> and others).
There are many other settings as well, such as transformation matrices,
fill rules, whether to perform antialiasing, and others. For further
information, see the <ulink url="http://www.cairographics.org/cairomm/">cairomm</ulink> API documentation.
</para>
<para>
The current state of a <classname>Cairo::Context</classname> can be
saved to an internal stack of saved states and later be restored to the
state it was in when you saved it. To do this, use the
<methodname>save()</methodname>
method and the <methodname>restore()</methodname> method. This can be
useful if you need to temporarily change the line width and color (or
any other graphics setting) in order to draw something and then return
to the previous settings. In this situation, you could call
<methodname>Cairo::Context::save()</methodname>, change the graphics
settings, draw the lines, and then call
<methodname>Cairo::Context::restore()</methodname> to restore the original
graphics state. Multiple calls to <methodname>save()</methodname> and
<methodname>restore()</methodname> can be nested; each call to
<methodname>restore()</methodname> restores the state from the
matching paired <methodname>save()</methodname>.
<tip>
<para>It is good practice to put all modifications to the graphics state
between <methodname>save()</methodname>/<methodname>restore()</methodname>
function calls. For example, if you have a function that takes a
<classname>Cairo::Context</classname> reference as an argument, you
might implement it as follows:
</para>
<programlisting>void doSomething(const Cairo::RefPtr<Cairo::Context>& context, int x)
{
context->save();
// change graphics state
// perform drawing operations
context->restore();
}</programlisting>
</tip>
</para>
<para>
The virtual <methodname>on_draw()</methodname> method provides a
Cairo context that you shall use for drawing in the
<classname>Gtk::DrawingArea</classname> widget. It is not necessary to
save and restore this Cairo context in <methodname>on_draw()</methodname>.
</para>
</sect1>
<sect1 id="sec-cairo-drawing-lines">
<title>Drawing Straight Lines</title>
<para>
Now that we understand the basics of the Cairo graphics library, we're
almost ready to start drawing. We'll start with the simplest of
drawing elements: the straight line. But first you need to know a
little bit about Cairo's coordinate system. The origin of the Cairo
coordinate system is located in the upper-left corner of the window
with positive x values to the right and positive y values going down.
<tip>
<para>Since the Cairo graphics library was written with support for
multiple output targets (the X window system, PNG images, OpenGL,
etc), there is a distinction between user-space and device-space
coordinates. The mapping between these two coordinate systems
defaults to one-to-one so that integer values map roughly to pixels
on the screen, but this setting can be adjusted if desired.
Sometimes it may be useful to scale the coordinates so that the
full width and height of a window both range from 0 to 1 (the 'unit
square') or some other mapping that works for your application.
This can be done with the
<methodname>Cairo::Context::scale()</methodname> function.</para>
</tip>
</para>
<sect2 id="cairo-example-lines"><title>Example</title>
<para>
In this example, we'll construct a small but fully functional >kmm;
program and draw some lines into the window. The lines are drawn by
creating a path and then stroking it. A path is created using the
functions <methodname>Cairo::Context::move_to()</methodname> and
<methodname>Cairo::Context::line_to()</methodname>. The function
<methodname>move_to()</methodname> is similar to the act of lifting your
pen off of the paper and placing it somewhere else -- no line is drawn
between the point you were at and the point you moved to. To draw a
line between two points, use the <methodname>line_to()</methodname>
function.
</para>
<para>
After you've finished creating your path, you still haven't
drawn anything visible yet. To make the path visible, you must use the
function <methodname>stroke()</methodname> which will stroke the current
path with the line width and style specified in your
<classname>Cairo::Context</classname> object. After stroking, the
current path will be cleared so that you can start on your next path.
</para>
<tip>
<para>Many Cairo drawing functions have a <methodname>_preserve()</methodname>
variant. Normally drawing functions such as
<methodname>clip()</methodname>, <methodname>fill()</methodname>, or
<methodname>stroke()</methodname> will clear the current path. If you
use the <methodname>_preserve()</methodname> variant, the current path
will be retained so that you can use the same path with the next
drawing function.</para>
</tip>
<figure id="figure-drawingarea-lines">
<title>Drawing Area - Lines</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;drawingarea_lines.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;drawingarea/simple">Source Code</ulink></para>
<para>
This program contains a single class, <classname>MyArea</classname>,
which is a subclass of <classname>Gtk::DrawingArea</classname> and
contains an <methodname>on_draw()</methodname> member function.
This function is called whenever the image in the drawing area needs to
be redrawn. It is passed a <classname>Cairo::RefPtr</classname>
pointer to a <classname>Cairo::Context</classname> that we use
for the drawing.
The actual drawing code sets the color we want to use for drawing by
using <methodname>set_source_rgb()</methodname> which takes arguments
defining the Red, Green, and Blue components of the desired color
(valid values are between 0 and 1). After setting the color, we
created a new path using the functions <methodname>move_to()</methodname>
and <methodname>line_to()</methodname>, and then stroked this path with
<methodname>stroke()</methodname>.
</para>
<tip>
<title>Drawing with relative coordinates</title>
<para>In the example above we drew everything using absolute coordinates. You can also draw using
relative coordinates. For a straight line, this is done with the
function <methodname>Cairo::Context::rel_line_to()</methodname>.</para>
</tip>
</sect2>
<sect2 id="cairo-line-styles">
<title>Line styles</title>
<para>
In addition to drawing basic straight lines, there are a number of
things that you can customize about a line. You've already seen
examples of setting a line's color and width, but there are others
as well.
</para>
<para>
If you've drawn a series of lines that form a path, you may
want them to join together in a certain way. Cairo offers
three different ways to join lines together: Miter, Bevel, and
Round. These are show below:
</para>
<figure id="figure-cairo-joins">
<title>Different join types in Cairo</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;cairo_joins.png"/>
</screenshot>
</figure>
<para>
The line join style is set using the function
<methodname>Cairo::Context::set_line_join()</methodname>.
</para>
<para>
Line ends can have different styles as well. The default style
is for the line to start and stop exactly at the destination
points of the line. This is called a Butt cap. The other
options are Round (uses a round ending, with the center of the
circle at the end point) or Square (uses a squared ending, with
the center of the square at the end point). This setting is set
using the function
<methodname>Cairo::Context::set_line_cap()</methodname>.
</para>
<para>
There are other things you can customize as well, including
creating dashed lines and other things. For more information, see
the Cairo API documentation.
</para>
</sect2>
<sect2 id="sec-cairo-thin-lines">
<title>Drawing thin lines</title>
<para>
If you try to draw one pixel wide lines, you may notice that the line
sometimes comes up blurred and wider than it ought to be.
This happens because Cairo will try to draw from the selected position,
to both sides (half to each), so if you're positioned right on the
intersection of the pixels, and want a one pixel wide line, Cairo will try
to use half of each adjacent pixel, which isn't possible (a pixel is the
smallest unit possible). This happens when the width of the line is an
odd number of pixels (not just one pixel).
</para>
<para>
The trick is to position in the middle of the pixel where you want the
line to be drawn, and thus guaranteeing you get the desired results.
See <ulink url="http://cairographics.org/FAQ/#sharp_lines">Cairo FAQ</ulink>.
</para>
<figure id="figure-drawingarea-thin-lines">
<title>Drawing Area - Thin Lines</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;drawingarea_thin_lines.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;drawingarea/thin_lines">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-cairo-curved-lines">
<title>Drawing Curved Lines</title>
<para>
In addition to drawing straight lines Cairo allows you to easily
draw curved lines (technically a cubic Bézier spline) using the
<methodname>Cairo::Context::curve_to()</methodname> and
<methodname>Cairo::Context::rel_curve_to()</methodname> functions.
These functions take coordinates for a destination point as well as
coordinates for two 'control' points. This is best explained using
an example, so let's dive in.
</para>
<sect2 id="cairo-example-curves">
<title>Example</title>
<para>
This simple application draws a curve with Cairo and displays
the control points for each end of the curve.
</para>
<figure id="figure-drawingarea-curve">
<title>Drawing Area - Lines</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;drawingarea_curve.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;drawingarea/curve">Source Code</ulink></para>
<para>
The only difference between this example and the straight line
example is in the <methodname>on_draw()</methodname> function,
but there are a few new concepts and functions introduced here, so
let's examine them briefly.
</para>
<para>
We make a call to
<methodname>Cairo::Context::scale()</methodname>, passing in the width
and height of the drawing area. This scales the user-space
coordinate system such that the width and height of the widget
are both equal to 1.0 'units'. There's no particular reason to
scale the coordinate system in this case, but sometimes it can make
drawing operations easier.
</para>
<para>
The call to <methodname>Cairo::Context::curve_to()</methodname> should
be fairly self-explanatory. The first pair of coordinates define
the control point for the beginning of the curve. The second set
of coordinates define the control point for the end of the curve,
and the last set of coordinates define the destination point. To
make the concept of control points a bit easier to visualize, a
line has been drawn from each control point to the end-point on the
curve that it is associated with. Note that these control point
lines are both translucent. This is achieved with a variant of
<methodname>set_source_rgb()</methodname> called
<methodname>set_source_rgba()</methodname>. This function takes a
fourth argument specifying the alpha value of the color (valid
values are between 0 and 1).
</para>
</sect2>
</sect1>
<sect1 id="sec-cairo-drawing-arcs">
<title>Drawing Arcs and Circles</title>
<para>
With Cairo, the same function is used to draw arcs, circles, or
ellipses: <methodname>Cairo::Context::arc()</methodname>. This function
takes five arguments. The first two are the coordinates of the
center point of the arc, the third argument is the radius of the arc,
and the final two arguments define the start and end angle of the
arc. All angles are defined in radians, so drawing a circle is the
same as drawing an arc from 0 to 2 * M_PI radians.
An angle of 0 is in the direction of the positive X axis (in user-space). An
angle of M_PI/2 radians (90 degrees) is in the direction of the positive Y axis
(in user-space). Angles increase in the direction from the positive X axis
toward the positive Y axis. So with the default transformation matrix, angles
increase in a clockwise direction. (Remember that the positive Y axis
points downwards.)
</para>
<para>
To draw an ellipse, you can scale the current transformation matrix
by different amounts in the X and Y directions. For example, to draw
an ellipse with center at <varname>x</varname>, <varname>y</varname>
and size <varname>width</varname>, <varname>height</varname>:
<programlisting>context->save();
context->translate(x, y);
context->scale(width / 2.0, height / 2.0);
context->arc(0.0, 0.0, 1.0, 0.0, 2 * M_PI);
context->restore();</programlisting>
</para>
<sect2 id="cairo-example-arcs">
<title>Example</title>
<para>
Here's an example of a simple program that draws an arc, a circle
and an ellipse into a drawing area.
</para>
<figure id="figure-drawingarea-arc">
<title>Drawing Area - Arcs</title>
<screenshot>
<graphic format="PNG"
fileref="&url_figures_base;drawingarea_arcs.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;drawingarea/arcs">Source Code</ulink></para>
<para>
There are a couple of things to note about this example code.
Again, the only real difference between this example and the
previous ones is the <methodname>on_draw()</methodname>
function, so we'll limit our focus to that function. In
addition, the first part of the function is nearly identical to
the previous examples, so we'll skip that portion.
</para>
<para>
Note that in this case, we've expressed nearly everything in
terms of the height and width of the window, including the width
of the lines. Because of this, when you resize the window,
everything scales with the window. Also note that there are
three drawing sections in the function and each is wrapped with a
<methodname>save()</methodname>/<methodname>restore()</methodname> pair
so that we're back at a known state after each drawing.
</para>
<para>
The section for drawing an arc introduces one new function,
<methodname>close_path()</methodname>. This function will in effect
draw a straight line from the current point back to the first
point in the path. There is a significant difference between
calling <methodname>close_path()</methodname> and manually drawing a
line back to the starting point, however. If you use
<methodname>close_path()</methodname>, the lines will be nicely
joined together. If you use <methodname>line_to()</methodname>
instead, the lines will end at the same point, but Cairo won't do
any special joining.
</para>
<note>
<title>Drawing counter-clockwise</title>
<para>
The function
<methodname>Cairo::Context::arc_negative()</methodname> is
exactly the same as
<methodname>Cairo::Context::arc()</methodname> but the angles go
the opposite direction.
</para>
</note>
</sect2>
</sect1>
<sect1 id="sec-drawing-text">
<title>Drawing Text</title>
<sect2 id="drawing-text-pango">
<title>Drawing Text with Pango</title>
<para>
Text is drawn via Pango Layouts. The easiest way to create a
<classname>Pango::Layout</classname> is to use
<methodname>Gtk::Widget::create_pango_layout()</methodname>.
Once created, the layout can be manipulated in various ways,
including changing the text, font, etc. Finally, the layout can
be rendered using the
<methodname>Pango::Layout::show_in_cairo_context()</methodname> method.
</para>
</sect2>
<sect2 id="pango-text-example">
<title>Example</title>
<para>
Here is an example of a program that draws some text, some of it
upside-down. The Printing chapter contains another
<link linkend="sec-printing-example">example</link> of drawing text.
</para>
<figure id="figure-drawingarea-pango-text">
<title>Drawing Area - Text</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;drawingarea_pango_text.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;drawingarea/pango_text">Source Code</ulink></para>
</sect2>
<!--
<sect2 id="drawing-text-cairo">
<title>Drawing Text with Cairo</title>
<warning>TODO: Add Cairo content.</warning>
</sect2>
-->
</sect1>
<sect1 id="sec-draw-images">
<title>Drawing Images</title>
<para>
There is a method for drawing from a
<classname>Gdk::Pixbuf</classname> to a <classname>Cairo::Context</classname>.
A <classname>Gdk::Pixbuf</classname> buffer is a useful wrapper
around a collection of pixels, which can be read from files, and
manipulated in various ways.
</para>
<para>
Probably the most common way of creating
<classname>Gdk::Pixbuf</classname>s is to use
<methodname>Gdk::Pixbuf::create_from_file()</methodname>, which can
read an image file, such as a png file into a pixbuf ready for
rendering.
</para>
<para>
The <classname>Gdk::Pixbuf</classname> can be rendered by setting
it as the source pattern of the Cairo context with
<methodname>Gdk::Cairo::set_source_pixbuf()</methodname>.
Then draw the image with either <methodname>Cairo::Context::paint()</methodname>
(to draw the whole image), or <methodname>Cairo::Context::rectangle()</methodname>
and <methodname>Cairo::Context::fill()</methodname> (to fill the
specified rectangle). <methodname>set_source_pixbuf()</methodname>
is not a member of <classname>Cairo::Context</classname>. It takes
a <classname>Cairo::Context</classname> as its first parameter.
</para>
<para>
Here is a small bit of code to tie it all together: (Note that
usually you wouldn't load the image every time in the draw
signal handler! It's just shown here to keep it all together.)
</para>
<programlisting>bool MyArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
{
Glib::RefPtr<Gdk::Pixbuf> image = Gdk::Pixbuf::create_from_file("myimage.png");
// Draw the image at 110, 90, except for the outermost 10 pixels.
Gdk::Cairo::set_source_pixbuf(cr, image, 100, 80);
cr->rectangle(110, 90, image->get_width()-20, image->get_height()-20);
cr->fill();
return true;
}</programlisting>
<sect2 id="cairo-example-image">
<title>Example</title>
<para>
Here is an example of a simple program that draws an image.
</para>
<figure id="figure-drawingarea-image">
<title>Drawing Area - Image</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;drawingarea_image.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;drawingarea/image">Source Code</ulink></para>
</sect2>
</sect1>
<!--
<sect1 id="sec-drawing-fill">
<title>Gradients and other fill techniques</title>
<warning>TODO: Add content.</warning>
</sect1>
<sect1 id="sec-drawing-transformations">
<title>Transformations with Cairo</title>
<warning>TODO: Add content.</warning>
</sect1>
-->
<sect1 id="sec-drawing-clock-example">
<title>Example Application: Creating a Clock with Cairo</title>
<para>
Now that we've covered the basics of drawing with Cairo, let's try to
put it all together and create a simple application that actually
does something. The following example uses Cairo to create a custom
<classname>Clock</classname> widget. The clock has a second hand, a
minute hand, and an hour hand, and updates itself every second.
</para>
<screenshot>
<graphic format="PNG"
fileref="&url_figures_base;cairo_clock.png"/>
</screenshot>
<para><ulink url="&url_examples_base;drawingarea/clock">Source Code</ulink></para>
<para>
As before, almost all of the interesting stuff is done in the draw
signal handler <methodname>on_draw()</methodname>. Before we dig
into the draw signal handler, notice that the constructor for the
<classname>Clock</classname> widget connects a handler function
<methodname>on_timeout()</methodname> to a timer with a timeout
period of 1000 milliseconds (1 second). This means that
<methodname>on_timeout()</methodname> will get called once per
second. The sole responsibility of this function is to invalidate
the window so that >kmm; will be forced to redraw it.
</para>
<para>
Now let's take a look at the code that performs the actual drawing.
The first section of <methodname>on_draw()</methodname> should be
pretty familiar by now. This example again scales the coordinate system
to be a unit square so that it's easier to draw the clock as a
percentage of window size so that it will automatically scale when
the window size is adjusted. Furthermore, the coordinate system is
scaled over and down so that the (0, 0) coordinate is in the very
center of the window.
</para>
<para>
The function <methodname>Cairo::Context::paint()</methodname> is used here
to set the background color of the window. This function takes no
arguments and fills the current surface (or the clipped portion of
the surface) with the source color currently active. After setting
the background color of the window, we draw a circle for the clock
outline, fill it with white, and then stroke the outline in black.
Notice that both of these actions use the
<methodname>_preserve</methodname> variant to preserve the current path,
and then this same path is clipped to make sure that our next lines
don't go outside the outline of the clock.
</para>
<para>
After drawing the outline, we go around the clock and draw ticks for
every hour, with a larger tick at 12, 3, 6, and 9. Now we're finally
ready to implement the time-keeping functionality of the clock, which
simply involves getting the current values for hours, minutes and
seconds, and drawing the hands at the correct angles.
</para>
</sect1>
</chapter>
<chapter id="chapter-draganddrop">
<title>Drag and Drop</title>
<para>
<classname>Gtk::Widget</classname> has several methods and signals which are
prefixed with "drag_". These are used for Drag and Drop.
</para>
<sect1 id="sec-dnd-sources-destinations">
<title>Sources and Destinations</title>
<para>
Things are dragged from <literal>sources</literal> to be dropped on
<literal>destinations</literal>. Each source and destination has infomation
about the data formats that it can send or receive, provided by
<classname>Gtk::TargetEntry</classname> items. A drop destination will only
accept a dragged item if they both share a compatible
<classname>Gtk::TargetEntry</classname> item. Appropriate signals will then be
emitted, telling the signal handlers which
<classname>Gtk::TargetEntry</classname> was used.
</para>
<para>
<classname>Gtk::TargetEntry</classname> objects contain this information:
<itemizedlist>
<listitem><para>target: A name, such as "STRING"</para></listitem>
<listitem><para>info: An identifier which will be sent to your signals to tell you which TargetEntry was used.</para></listitem>
<listitem><para>flags: Used only for drag and drop, this specifies whether the data may be dragged to other widgets and applications, or only to the same ones.</para></listitem>
</itemizedlist>
</para>
</sect1>
<sect1 id="sec-dnd-methods">
<title>Methods</title>
<para>
<classname>Widget</classname>s can be identified as sources or destinations
using these <classname>Gtk::Widget</classname> methods:
</para>
<programlisting>void drag_source_set(const std::vector<Gtk::TargetEntry>& targets,
Gdk::ModifierType start_button_mask, Gdk::DragAction actions);</programlisting>
<itemizedlist>
<listitem>
<para>
<literal>targets</literal> is a vector of
<classname>Gtk::TargetEntry</classname> elements.
</para>
</listitem>
<listitem>
<para>
<literal>start_button_mask</literal> is an ORed combination of values,
which specify which modifier key or mouse button must be pressed to
start the drag.
</para>
</listitem>
<listitem>
<para>
<literal>actions</literal> is an ORed combination of values, which
specified which Drag and Drop operations will be possible from this
source - for instance, copy, move, or link. The user can choose between
the actions by using modifier keys, such as <keycap>Shift</keycap> to
change from <literal>copy</literal> to <literal>move</literal>, and
this will be shown by a different cursor.
</para>
</listitem>
</itemizedlist>
<programlisting>void drag_dest_set(const std::vector<Gtk::TargetEntry>& targets,
Gtk::DestDefaults flags, Gdk::DragAction actions);</programlisting>
<itemizedlist>
<listitem>
<para>
<literal>flags</literal> is an ORed combination of values which
indicates how the widget will respond visually to Drag and Drop items.
</para>
</listitem>
<listitem>
<para>
<literal>actions</literal> indicates the Drag and Drop actions which
this destination can receive - see the description above.
</para>
</listitem>
</itemizedlist>
</sect1>
<sect1 id="sec-dnd-signals">
<title>Signals</title>
<para>
When a drop destination has accepted a dragged item, certain signals will be
emitted, depending on what action has been selected. For instance, the user
might have held down the <keycap>Shift</keycap> key to specify a
<literal>move</literal> rather than a <literal>copy</literal>. Remember that
the user can only select the actions which you have specified in your calls to
<methodname>drag_dest_set()</methodname> and
<methodname>drag_source_set()</methodname>.
</para>
<sect2 id="sec-dnd-signals-copy">
<title>Copy</title>
<para>
The source widget will emit these signals, in this order:
<itemizedlist>
<listitem><para><literal>drag_begin</literal>: Provides DragContext.</para></listitem>
<listitem><para><literal>drag_data_get</literal>: Provides <literal>info</literal> about the dragged data format, and a <literal>Gtk::SelectionData</literal> structure, in which you should put the requested data.</para></listitem>
<listitem><para><literal>drag_end</literal>: Provides DragContext.</para></listitem>
</itemizedlist>
</para>
<para>
The destination widget will emit these signals, in this order:
<itemizedlist>
<listitem><para><literal>drag_motion</literal>: Provides DragContext and coordinates.
You can call the <methodname>drag_status()</methodname> method of the DragContext
to indicate which action will be accepted.</para></listitem>
<listitem><para><literal>drag_drop</literal>: Provides DragContext and coordinates.
You can call <methodname>drag_get_data()</methodname>, which triggers the
<literal>drag_data_get</literal> signal in the source widget, and then the
<literal>drag_data_received</literal> signal in the destination widget.</para></listitem>
<listitem>
<para>
<literal>drag_data_received</literal>: Provides <literal>info</literal>
about the dragged data format, and a
<literal>Gtk::SelectionData</literal> structure which contains the
dropped data. You should call the <methodname>drag_finish()</methodname>
method of the <literal>DragContext</literal> to indicate whether the
operation was successful.
</para>
</listitem>
</itemizedlist>
</para>
</sect2>
<sect2 id="dnd-signal-move">
<title>Move</title>
<para>During a <literal>move</literal>, the source widget will also emit this signal:
<itemizedlist>
<listitem><para><literal>drag_data_delete</literal>: Gives the source the opportunity to delete the original data if that's appropriate.</para></listitem>
</itemizedlist>
</para>
</sect2>
<!--
<sect2 id="dnd-signal-link">
<title>Link</title>
<para>TODO: Find an example or documentation.</para>
</sect2>
-->
</sect1>
<sect1 id="sec-dragcontext">
<title>DragContext</title>
<para>
The drag and drop signals provide a DragContext, which contains some
information about the drag and drop operation and can be used to influence the
process. For instance, you can discover the source widget, or change the drag
and drop icon, by using the <methodname>set_icon()</methodname> methods. More
importantly, you should call the <methodname>drag_finish()</methodname> method from
your <literal>drag_data_received</literal> signal handler to indicate whether
the drop was successful.
</para>
</sect1>
<sect1 id="sec-dnd-example">
<title>Example</title>
<para>Here is a very simple example, demonstrating a drag and drop <literal>Copy</literal> operation:</para>
<figure id="figure-drag-and-drop">
<title>Drag and Drop</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;drag_and_drop.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;drag_and_drop">Source Code</ulink></para>
<para>
There is a more complex example in examples/others/dnd.
</para>
</sect1>
</chapter>
<chapter id="chapter-clipboard">
<title>The Clipboard</title>
<para>Simple text copy-paste functionality is provided for free by widgets such as
<classname>Gtk::Entry</classname> and <classname>Gtk::TextView</classname>,
but you might need special code to deal with your own data formats. For instance,
a drawing program would need special code to allow copy and paste within a view,
or between documents.</para>
<para>
You can usually pretend that <classname>Gtk::Clipboard</classname> is a singleton.
You can get the default clipboard instance with <methodname>Gtk::Clipboard::get()</methodname>.
This is probably the only clipboard you will ever need.
</para>
<para>
Your application doesn't need to wait for clipboard operations, particularly
between the time when the user chooses Copy and then later chooses Paste. Most
<classname>Gtk::Clipboard</classname> methods take
<classname>sigc::slot</classname>s which specify callback methods. When
<classname>Gtk::Clipboard</classname> is ready, it will call these methods,
either providing the requested data, or asking for data.
</para>
<para><ulink url="&url_refdocs_base_gtk;Clipboard.html">Reference</ulink></para>
<sect1 id="sec-clipboard-targets">
<title>Targets</title>
<para>
Different applications contain different types of data, and they might make that data available in
a variety of formats. >kmm; calls these data types <literal>target</literal>s.</para>
<para>
For instance, <application>gedit</application> can supply and receive the <literal>"UTF8_STRING"</literal>
target, so you can paste data into <application>gedit</application> from any application that supplies that target.
Or two different image editing applications might supply and receive a variety of image formats as targets.
As long as one application can receive one of the targets that the other supplies then you will be able to copy data from one to the other.
</para>
<para>
A target can be in a variety of binary formats. This chapter, and the examples,
assume that the data is 8-bit text. This would allow us to use an XML format
for the clipboard data. However this would probably not be appropriate for
binary data such as images. <classname>Gtk::Clipboard</classname> provides
overloads that allow you to specify the format in more detail if
necessary.
</para>
<para>The <link linkend="chapter-draganddrop">Drag and Drop</link> API uses the same mechanism.
You should probably use the same data targets and formats for both Clipboard and Drag and Drop operations.</para>
</sect1>
<sect1 id="sec-clipboard-copy">
<title>Copy</title>
<para>
When the user asks to copy some data, you should tell the
<classname>Clipboard</classname> what targets are available, and provide the
callback methods that it can use to get the data. At this point you should
store a copy of the data, to be provided when the clipboard calls your callback
method in response to a paste.
</para>
<para>For instance,
</para>
<programlisting>Glib::RefPtr<Gtk::Clipboard> refClipboard = Gtk::Clipboard::get();
//Targets:
std::vector<Gtk::TargetEntry> targets;
targets.push_back( Gtk::TargetEntry("example_custom_target") );
targets.push_back( Gtk::TargetEntry("UTF8_STRING") );
refClipboard->set( targets,
sigc::mem_fun(*this, &ExampleWindow::on_clipboard_get),
sigc::mem_fun(*this, &ExampleWindow::on_clipboard_clear) );</programlisting>
<para>Your callback will then provide the stored data when the user chooses to paste the data. For instance:
</para>
<programlisting>void ExampleWindow::on_clipboard_get(
Gtk::SelectionData& selection_data, guint /* info */)
{
const std::string target = selection_data.get_target();
if(target == "example_custom_target")
selection_data.set("example_custom_target", m_ClipboardStore);
}</programlisting>
<para>
The <literal>ideal</literal> example below can supply more than one clipboard target.
</para>
<para>The clear callback allows you to free the memory used by your stored data when the clipboard replaces its data with something else.
</para>
</sect1>
<sect1 id="sec-clipboard-paste">
<title>Paste</title>
<para>
When the user asks to paste data from the <classname>Clipboard</classname>, you
should request a specific format and provide a callback method which will be
called with the actual data. For instance:
</para>
<programlisting>refClipboard->request_contents("example_custom_target",
sigc::mem_fun(*this, &ExampleWindow::on_clipboard_received) );</programlisting>
<para>Here is an example callback method:
</para>
<programlisting>void ExampleWindow::on_clipboard_received(
const Gtk::SelectionData& selection_data)
{
Glib::ustring clipboard_data = selection_data.get_data_as_string();
//Do something with the pasted data.
}</programlisting>
<sect2 id="dnd-discovering-targets">
<title>Discovering the available targets</title>
<para>
To find out what targets are currently available on the
<classname>Clipboard</classname> for pasting, call the
<methodname>request_targets()</methodname> method, specifying a method to be called
with the information. For instance:
</para>
<programlisting>refClipboard->request_targets( sigc::mem_fun(*this,
&ExampleWindow::on_clipboard_received_targets) );</programlisting>
<para>
In your callback, compare the vector of available targets with those that your application supports for pasting. You could enable or disable a Paste menu item, depending on whether pasting is currently possible. For instance:
</para>
<programlisting>void ExampleWindow::on_clipboard_received_targets(
const std::vector<Glib::ustring>& targets)
{
const bool bPasteIsPossible =
std::find(targets.begin(), targets.end(),
example_target_custom) != targets.end();
// Enable/Disable the Paste button appropriately:
m_Button_Paste.set_sensitive(bPasteIsPossible);
}</programlisting>
</sect2>
</sect1>
<sect1 id="sec-clipboard-examples"><title>Examples</title>
<sect2 id="sec-clipboard-example-simple"><title>Simple</title>
<para>
This example allows copy and pasting of application-specific data, using the
standard text target. Although this is simple, it's not ideal because it does
not identify the <classname>Clipboard</classname> data as being of a particular
type.
</para>
<figure id="figure-clipboard-simple">
<title>Clipboard - Simple</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;clipboard_simple.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;clipboard/simple/">Source Code</ulink></para>
</sect2>
<sect2 id="sec-clipboard-example-ideal"><title>Ideal</title>
<para>This is like the simple example, but it
<orderedlist>
<listitem><simpara>Defines a custom clipboard target, though the format of that target is still text.</simpara></listitem>
<listitem><simpara>It supports pasting of 2 targets - both the custom one and a text one that creates an arbitrary text representation of the custom data.</simpara></listitem>
<listitem><simpara>It uses <methodname>request_targets()</methodname> and the <literal>owner_change</literal> signal and disables the Paste button if it can't use anything on the clipboard.</simpara></listitem>
</orderedlist>
</para>
<figure id="figure-clipboard-ideal">
<title>Clipboard - Ideal</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;clipboard_ideal.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;clipboard/ideal/">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-printing">
<title>Printing</title>
<para>
At the application development level, >kmm;'s printing API
provides dialogs that are consistent across applications and allows use of Cairo's common drawing API, with Pango-driven text rendering. In the implementation of this common API, platform-specific backends and printer-specific drivers are used.
</para>
<sect1 id="sec-printoperation">
<title>PrintOperation</title>
<para>
The primary object is <classname>Gtk::PrintOperation</classname>, allocated
for each print operation. To handle page drawing connect to its signals,
or inherit from it and override the default virtual signal handlers.
<classname>PrintOperation</classname> automatically handles all the settings
affecting the print loop.
</para>
<sect2 id="sec-printoperation-signals">
<title>Signals</title>
<para>
The <methodname>PrintOperation::run()</methodname> method starts the print loop,
during which various signals are emitted:
<itemizedlist>
<listitem>
<para>
<literal>begin_print</literal>:
You must handle this signal, because this is where you
create and set up a <classname>Pango::Layout</classname> using the
provided <classname>Gtk::PrintContext</classname>, and break up your
printing output into pages.
</para>
</listitem>
<listitem>
<para>
<literal>paginate</literal>: Pagination is potentially slow so if you
need to monitor it you can call the
<methodname>PrintOperation::set_show_progress()</methodname> method and
handle this signal.
</para>
</listitem>
<listitem>
<para>
For each page that needs to be rendered, the following signals
are emitted:
<itemizedlist>
<listitem>
<para>
<literal>request_page_setup</literal>: Provides a
<classname>PrintContext</classname>, page number and
<classname>Gtk::PageSetup</classname>. Handle this signal if you
need to modify page setup on a per-page basis.
</para>
</listitem>
<listitem>
<para>
<literal>draw_page</literal>: You must handle this signal, which provides a
<classname>PrintContext</classname> and a page number.
The <classname>PrintContext</classname> should be used
to create a <classname>Cairo::Context</classname> into which
the provided page should be drawn. To render text, iterate over
the <classname>Pango::Layout</classname> you created in the
<literal>begin_print</literal> handler.
</para>
</listitem>
</itemizedlist>
</para>
</listitem>
<listitem>
<para>
<literal>end_print</literal>: A handler for it is a safe place to free
any resources related to a <classname>PrintOperation</classname>.
If you have your custom class that inherits from
<classname>PrintOperation</classname>, it is naturally simpler to do it
in the destructor.
</para>
</listitem>
<listitem>
<para>
<literal>done</literal>: This signal is emitted when printing is finished, meaning when the
print data is spooled. Note that the provided
<literal>Gtk::PrintOperationResult</literal> may indicate that
an error occurred. In any case you probably want to notify the user
about the final status.
</para>
</listitem>
<listitem>
<para>
<literal>status_changed</literal>: Emitted whenever a print job's
status changes, until it is finished. Call the
<methodname>PrintOperation::set_track_print_status()</methodname> method to
monitor the job status after spooling. To see the status, use
<methodname>get_status()</methodname> or
<methodname>get_status_string()</methodname>.
</para>
</listitem>
</itemizedlist>
</para>
<para>
<ulink url="&url_refdocs_base_gtk;PrintOperation.html">Reference</ulink>
</para>
</sect2>
</sect1>
<sect1 id="sec-page-setup">
<title>Page setup</title>
<para>
The <classname>PrintOperation</classname> class has a method called
<methodname>set_default_page_setup()</methodname> which selects the default
paper size, orientation and margins. To show a page setup dialog from your
application, use the <methodname>Gtk::run_page_setup_dialog()</methodname> method,
which returns a <classname>Gtk::PageSetup</classname> object with the chosen
settings. Use this object to update a <classname>PrintOperation</classname>
and to access the selected <classname>Gtk::PaperSize</classname>,
<literal>Gtk::PageOrientation</literal> and printer-specific margins.
</para>
<para>You should save the chosen <classname>Gtk::PageSetup</classname>
so you can use it again if the page setup dialog is shown again.</para>
<para>For instance,
<programlisting>
//Within a class that inherits from Gtk::Window and keeps m_refPageSetup and m_refSettings as members...
Glib::RefPtr<Gtk::PageSetup> new_page_setup = Gtk::run_page_setup_dialog(*this, m_refPageSetup, m_refSettings);
m_refPageSetup = new_page_setup;
</programlisting>
</para>
<para>
<ulink url="&url_refdocs_base_gtk;PageSetup.html">Reference</ulink>
</para>
<para>
The Cairo coordinate system, in the <literal>draw_page</literal> handler,
is automatically rotated to the current page orientation. It is normally
within the printer margins, but you can change that via the
<methodname>PrintOperation::set_use_full_page()</methodname>
method. The default measurement unit is device pixels. To select other units,
use the <methodname>PrintOperation::set_unit()</methodname> method.
</para>
</sect1>
<sect1 id="sec-printing-rendering-text">
<title>Rendering text</title>
<para>
Text rendering is done using Pango.
The <classname>Pango::Layout</classname> object for printing should be created by calling
the <methodname>PrintContext::create_pango_layout()</methodname> method.
The <classname>PrintContext</classname> object also provides the page metrics,
via <methodname>get_width()</methodname> and <methodname>get_height()</methodname>.
The number of pages can be set with
<methodname>PrintOperation::set_n_pages()</methodname>. To actually render the
Pango text in <literal>on_draw_page</literal>, get a
<classname>Cairo::Context</classname> with
<methodname>PrintContext::get_cairo_context()</methodname> and show the
<classname>Pango::LayoutLine</classname>s that appear within the requested
page number.
</para>
<para>
See <link linkend="sec-printing-example-simple">an example</link>
of exactly how this can be done.
</para>
</sect1>
<sect1 id="sec-async-printing-ops">
<title>Asynchronous operations</title>
<para>
By default, <methodname>PrintOperation::run()</methodname> returns when a print
operation is completed. If you need to run a non-blocking print operation,
call <methodname>PrintOperation::set_allow_async()</methodname>. Note that <methodname>set_allow_async()</methodname> is not supported
on all platforms, however the <literal>done</literal> signal will still be emitted.
</para>
<para>
<methodname>run()</methodname> may return
<literal>PRINT_OPERATION_RESULT_IN_PROGRESS</literal>. To track status
and handle the result or error you need to implement signal handlers for
the <literal>done</literal> and <literal>status_changed</literal> signals:
</para>
<para>For instance,
<programlisting>
// in class ExampleWindow's method...
Glib::RefPtr<PrintOperation> op = PrintOperation::create();
// ...set up op...
op->signal_done().connect(sigc::bind(sigc::mem_fun(*this, &ExampleWindow::on_printoperation_done), op));
// run the op
</programlisting>
</para>
<para>Second, check for an error and connect to the <literal>status_changed</literal> signal. For instance:
<programlisting>
void ExampleWindow::on_printoperation_done(Gtk::PrintOperationResult result, const Glib::RefPtr<PrintOperation>& op)
{
if (result == Gtk::PRINT_OPERATION_RESULT_ERROR)
//notify user
else if (result == Gtk::PRINT_OPERATION_RESULT_APPLY)
//Update PrintSettings with the ones used in this PrintOperation
if (! op->is_finished())
op->signal_status_changed().connect(sigc::bind(sigc::mem_fun(*this, &ExampleWindow::on_printoperation_status_changed), op));
}
</programlisting>
</para>
<para>Finally, check the status. For instance,
<programlisting>
void ExampleWindow::on_printoperation_status_changed(const Glib::RefPtr<PrintFormOperation>& op)
{
if (op->is_finished())
//the print job is finished
else
//get the status with get_status() or get_status_string()
//update UI
}
</programlisting>
</para>
</sect1>
<sect1 id="sec-printing-export-to-pdf">
<title>Export to PDF</title>
<para>
The 'Print to file' option is available in the print dialog, without the need for extra implementation. However, it is sometimes useful to generate a pdf file directly from code. For instance,
<programlisting>
Glib::RefPtr<Gtk::PrintOperation> op = Gtk::PrintOperation::create();
// ...set up op...
op->set_export_filename("test.pdf");
Gtk::PrintOperationResult res = op->run(Gtk::PRINT_OPERATION_ACTION_EXPORT);
</programlisting>
</para>
</sect1>
<sect1 id="sec-extending-print-dialog">
<title>Extending the print dialog</title>
<para>
You may add a custom tab to the print dialog:
<itemizedlist>
<listitem>
<para>
Set the title of the tab via
<methodname>PrintOperation::set_custom_tab_label()</methodname>,
create a new widget and return it from the
<literal>create_custom_widget</literal> signal handler. You'll probably
want this to be a container widget, packed with some others.
</para>
</listitem>
<listitem>
<para>
Get the data from the widgets in the
<literal>custom_widget_apply</literal> signal handler.
</para>
</listitem>
</itemizedlist>
</para>
<para>
Although the <literal>custom_widget_apply</literal> signal provides the widget you
previously created, to simplify things you can keep the widgets you expect
to contain some user input as class members. For example, let's say you have
a <classname>Gtk::Entry</classname> called <literal>m_Entry</literal> as
a member of your <classname>CustomPrintOperation</classname> class:
<programlisting>
Gtk::Widget* CustomPrintOperation::on_create_custom_widget()
{
set_custom_tab_label("My custom tab");
Gtk::Box* hbox = new Gtk::Box(Gtk::ORIENTATION_HORIZONTAL, 8);
hbox->set_border_width(6);
Gtk::Label* label = Gtk::manage(new Gtk::Label("Enter some text: "));
hbox->pack_start(*label, false, false);
label->show();
hbox->pack_start(m_Entry, false, false);
m_Entry.show();
return hbox;
}
void CustomPrintOperation::on_custom_widget_apply(Gtk::Widget* /* widget */)
{
Glib::ustring user_input = m_Entry.get_text();
//...
}
</programlisting>
</para>
<para>
The example in examples/book/printing/advanced demonstrates this.
</para>
</sect1>
<sect1 id="sec-printing-preview">
<title>Preview</title>
<para>
The native GTK+ print dialog has a preview button, but you may also start
a preview directly from an application:
<programlisting>
// in a class that inherits from Gtk::Window...
Glib::RefPtr<PrintOperation> op = PrintOperation::create();
// ...set up op...
op->run(Gtk::PRINT_OPERATION_ACTION_PREVIEW, *this);
</programlisting>
</para>
<para>
On Unix, the default preview handler uses an external viewer program.
On Windows, the native preview dialog will be shown. If necessary you may
override this behaviour and provide a custom preview dialog. See the example
located in /examples/book/printing/advanced.
</para>
</sect1>
<sect1 id="sec-printing-example">
<title>Example</title>
<sect2 id="sec-printing-example-simple">
<title>Simple</title>
<para>
The following example demonstrates how to print some input from a user
interface. It shows how to implement <literal>on_begin_print</literal>
and <literal>on_draw_page</literal>, as well as how to track print status
and update the print settings.
</para>
<figure id="figure-printing-simple">
<title>Printing - Simple</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;printing.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;printing/simple/">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-recent-documents">
<title>Recently Used Documents</title>
<para>
>kmm; provides an easy way to manage recently used documents. The classes
involved in implementing this functionality are
<classname>RecentManager</classname>,
<classname>RecentChooserDialog</classname>,
<classname>RecentChooserMenu</classname>,
<classname>RecentChooserWidget</classname>,
<classname>RecentAction</classname>, and
<classname>RecentFilter</classname>.
</para>
<para>
Each item in the list of recently used files is identified by its URI, and
can have associated metadata. The metadata can be used to specify how the
file should be displayed, a description of the file, its mime type, which
application registered it, whether it's private to the registering
application, and several other things.
</para>
<sect1 id="sec-recentmanager">
<title>RecentManager</title>
<para>
<classname>RecentManager</classname> acts as a database of
recently used files. You use this class to register new files, remove
files from the list, or look up recently used files. There is one list
of recently used files per user.
</para>
<para>
You can create a new <classname>RecentManager</classname>, but you'll most
likely just want to use the default one. You can get a reference to the
default <classname>RecentManager</classname> with
<methodname>get_default()</methodname>.
</para>
<para>
<classname>RecentManager</classname> is the model of a model-view pattern,
where the view is a class that implements the
<classname>RecentChooser</classname> interface.
</para>
<sect2 id="recent-files-adding">
<title>Adding Items to the List of Recent Files</title>
<para>
To add a new file to the list of recent documents, in the simplest case,
you only need to provide the URI. For example:
</para>
<programlisting>Glib::RefPtr<Gtk::RecentManager> recent_manager = Gtk::RecentManager::get_default();
recent_manager->add_item(uri);</programlisting>
<para>
If you want to register a file with metadata, you can pass a
<classname>RecentManager::Data</classname> parameter to
<methodname>add_item()</methodname>. The metadata that can be set on a
particular file item is as follows:
</para>
<itemizedlist id="list-file-metadata">
<listitem>
<para><varname>app_exec</varname>: The command line to be used to launch
this resource. This string may contain the "f" and "u" escape
characters which will be expanded to the resource file path and URI
respectively</para>
</listitem>
<listitem>
<para><varname>app_name</varname>: The name of the application that
registered the resource</para>
</listitem>
<listitem>
<para><varname>description</varname>: A short description of the
resource as a UTF-8 encoded string</para>
</listitem>
<listitem>
<para><varname>display_name</varname>: The name of the resource to be
used for display as a UTF-8 encoded string</para>
</listitem>
<listitem>
<para><varname>groups</varname>: A list of groups associated with this
item. Groups are essentially arbitrary strings associated with a
particular resource. They can be thought of as 'categories' (such
as "email", "graphics", etc) or tags for the resource.</para>
</listitem>
<listitem>
<para><varname>is_private</varname>: Whether this resource should be
visible only to applications that have registered it or not</para>
</listitem>
<listitem>
<para><varname>mime_type</varname>: The MIME type of the resource</para>
</listitem>
</itemizedlist>
<para>
In addition to adding items to the list, you can also look up items from
the list and modify or remove items.
</para>
</sect2>
<sect2 id="recent-files-lookup">
<title>Looking up Items in the List of Recent Files</title>
<para>
To look up recently used files, <classname>RecentManager</classname>
provides several functions. To look up a specific item by its URI, you
can use the <methodname>lookup_item()</methodname> function, which will
return a <classname>RecentInfo</classname> class. If the specified URI
did not exist in the list of recent files,
<methodname>lookup_item()</methodname> throws a
<classname>RecentManagerError</classname> exception. For example:
</para>
<programlisting>Glib::RefPtr<Gtk::RecentInfo> info;
try
{
info = recent_manager->lookup_item(uri);
}
catch(const Gtk::RecentManagerError& ex)
{
std::cerr << "RecentManagerError: " << ex.what() << std::endl;
}
if (info)
{
// item was found
}</programlisting>
<para>
A <classname>RecentInfo</classname> object is essentially an object
containing all of the metadata about a single recently-used file. You
can use this object to look up any of the properties listed
<link linkend="list-file-metadata">above</link>.
</para>
<para>
If you don't want to look for a specific URI, but instead want to get a
list of all recently used items, <classname>RecentManager</classname>
provides the <methodname>get_items()</methodname> function. The return
value of this function is a <classname>std::vector</classname> of all
recently used files. The following code demonstrates how you might get a
list of recently used files:
</para>
<programlisting>std::vector< Glib::RefPtr<Gtk::RecentInfo> > info_list = recent_manager->get_items();</programlisting>
<para>
The maximum age of items in the recently used files list can be set with
<methodname>Gtk::Settings::property_gtk_recent_files_max_age()</methodname>.
Default value: 30 days.
</para>
</sect2>
<sect2 id="recent-files-modifying">
<title>Modifying the List of Recent Files</title>
<para>
There may be times when you need to modify the list of recent files.
For instance, if a file is moved or renamed, you may need to update the
file's location in the recent files list so that it doesn't point to an
incorrect location. You can update an item's location by using
<methodname>move_item()</methodname>.
</para>
<para>
In addition to changing a file's URI, you can also remove items from the
list, either one at a time or by clearing them all at once. The former
is accomplished with <methodname>remove_item()</methodname>, the latter with
<methodname>purge_items()</methodname>.
</para>
<note>
<para>
The functions <methodname>move_item()</methodname>,
<methodname>remove_item()</methodname> and
<methodname>purge_items()</methodname> have no effect on the actual files
that are referred to by the URIs, they only modify the list of recent
files.
</para>
</note>
</sect2>
</sect1>
<sect1 id="sec-recentchooser">
<title>RecentChooser</title>
<para>
<classname>RecentChooser</classname> is an interface that can be
implemented by widgets displaying the list of recently used files.
>kmm; provides four built-in implementations for choosing recent files:
<classname>RecentChooserWidget</classname>,
<classname>RecentChooserDialog</classname>,
<classname>RecentChooserMenu</classname>, and
<classname>RecentAction</classname>.
</para>
<para>
<classname>RecentChooserWidget</classname> is a simple widget for
displaying a list of recently used files.
<classname>RecentChooserWidget</classname> is the basic building block for
<classname>RecentChooserDialog</classname>, but you can embed it into your
user interface if you want to.
</para>
<para>
<classname>RecentChooserMenu</classname> and
<classname>RecentAction</classname> allow you to list recently used files
as a menu.
</para>
<sect2 id="recentchooserdialog-example">
<title>Simple RecentChooserDialog example</title>
<para>
Shown below is a simple example of how to use the
<classname>RecentChooserDialog</classname> and the
<classname>RecentAction</classname> classes in a program.
This simple program has a menubar with a
<guimenuitem>Recent Files Dialog</guimenuitem> menu item.
When you select this menu item, a dialog pops up showing the list of
recently used files.
</para>
<note>
<para>
If this is the first time you're using a program that uses the Recent
Files framework, the dialog may be empty at first. Otherwise it
should show the list of recently used documents registered by other
applications.
</para>
</note>
<para>
After selecting the <guimenuitem>Recent Files Dialog</guimenuitem> menu
item, you should see something similar to the following window.
</para>
<screenshot>
<graphic format="PNG"
fileref="&url_figures_base;recentchooserdialog.png"/>
</screenshot>
<para><ulink url="&url_examples_base;recent_files">Source Code</ulink></para>
<para>
The constructor for <classname>ExampleWindow</classname> creates the
menu using <classname>UIManager</classname> (see <xref
linkend="chapter-menus-and-toolbars"/> for more information). It then adds
the menu and the toolbar to the window.
</para>
</sect2>
<sect2 id="recent-files-filtering">
<title>Filtering Recent Files</title>
<para>
For any of the <classname>RecentChooser</classname> classes, if you
don't wish to display all of the items in the list of recent files, you
can filter the list to show only those that you want. You can filter
the list with the help of the <classname>RecentFilter</classname> class.
This class allows you to filter recent files by their name
(<methodname>add_pattern()</methodname>), their mime type
(<methodname>add_mime_type()</methodname>), the application that registered
them (<methodname>add_application()</methodname>), or by a custom filter
function (<methodname>add_custom()</methodname>). It also provides the
ability to filter based on how long ago the file was modified and which
groups it belongs to.
</para>
<para>
After you've created and set up the filter to match only the items you
want, you can apply a filter to a chooser widget with the
<methodname>RecentChooser::add_filter()</methodname> function.
</para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-plugs-sockets">
<title>Plugs and Sockets</title>
<sect1 id="sec-plugs-sockets-overview">
<title>Overview</title>
<para>
From time to time, it may be useful to be able to embed a widget from
another application within your application. >kmm; allows you to do
this with the <classname>Gtk::Socket</classname> and
<classname>Gtk::Plug</classname> classes. It is not anticipated that very
many applications will need this functionality, but in the rare case that
you need to display a widget that is running in a completely different
process, these classes can be very helpful.
</para>
<para>
The communication between a <classname>Socket</classname> and a
<classname>Plug</classname> follows the XEmbed protocol. This protocol has
also been implemented in other toolkits (e.g. Qt), which allows the same
level of integration when embedding a Qt widget in GTK+ or vice versa.
</para>
<para>
The way that <classname>Sockets</classname> and
<classname>Plugs</classname> work together is through their window ids.
Both a <classname>Socket</classname> and a <classname>Plug</classname>
have IDs that can be retrieved with their <methodname>get_id()</methodname>
member functions. The use of these IDs will be explained below in <xref
linkend="sec-connecting-plugs-sockets"/>.
</para>
<sect2 id="sec-sockets">
<title>Sockets</title>
<para>
A <classname>Socket</classname> is a special kind of container widget that
provides the ability to embed widgets from one process into another
process in a way that is transparent to the user.
</para>
</sect2>
<sect2 id="sec-plugs">
<title>Plugs</title>
<para>
A <classname>Plug</classname> is a special kind of Window that can be
plugged into a <classname>Socket</classname>. Besides the normal
properties and methods of <classname>Gtk::Window</classname>, a
<classname>Plug</classname> provides a constructor that takes the ID of
a <classname>Socket</classname>, which will automatically embed the
<classname>Plug</classname> into the <classname>Socket</classname> that
matches that ID.
</para>
<para>
Since a <classname>Plug</classname> is just a special type of
<classname>Gtk::Window</classname> class, you can add containers or
widgets to it like you would to any other window.
</para>
</sect2>
<sect2 id="sec-connecting-plugs-sockets">
<title>Connecting Plugs and Sockets</title>
<para>
After a <classname>Socket</classname> or <classname>Plug</classname>
object is realized, you can obtain its ID with its
<methodname>get_id()</methodname> function. This ID can then be shared with
other processes so that other processes know how to connect to
each other.
</para>
<para>
There are two basic strategies that can be used:
<itemizedlist>
<listitem>
<para>
Create a <classname>Socket</classname> object in one process and
pass the ID of that <classname>Socket</classname> to another
process so that it can create a <classname>Plug</classname> object
by specifying the given <classname>Socket</classname> ID in its
constructor. There is no way to assign a
<classname>Plug</classname> to a particular
<classname>Socket</classname> after creation, so you must pass the
<classname>Socket</classname> ID to the
<classname>Plug</classname>'s constructor.
</para>
</listitem>
<listitem>
<para>
Create a <classname>Plug</classname> independantly from any
particular <classname>Socket</classname> and pass the ID of the
<classname>Plug</classname> to other processes that need to use
it. The ID of the <classname>Plug</classname> can be associated
with a particular <classname>Socket</classname> object using the
<methodname>Socket::add_id()</methodname> function. This is the
approach used in the example below.
</para>
</listitem>
</itemizedlist>
</para>
</sect2>
</sect1>
<sect1 id="sec-plugs-sockets-example">
<title>Plugs and Sockets Example</title>
<para>
The following is a simple example of using sockets and plugs. The method
of communication between processes is deliberately kept very simple: The
<classname>Plug</classname> writes its ID out to a text file named
<filename>plug.id</filename> and the process with the socket reads the ID
from this file. In a real program, you may want to use a more
sophisticated method of inter-process communication.
</para>
<para><ulink url="&url_examples_base;socket/">Source Code</ulink></para>
<para>
This example creates two executable programs: <filename>socket</filename>
and <filename>plug</filename>. The idea is that
<filename>socket</filename> has an application window that will embed a
widget from the <filename>plug</filename> program. The way this example
is designed, <filename>plug</filename> must be running first before
starting <filename>socket</filename>. To see the example in action,
execute the following commands in order from within the example directory:
</para>
<para>
Start the <filename>plug</filename> program and send it to the background
(or just use a different terminal).
</para>
<screen>$ ./plug &</screen>
<para>
After which you should see something like the following:
</para>
<screen>The window ID is: 69206019</screen>
<para>Then start the <filename>socket</filename> program:</para>
<screen>$ ./socket</screen>
<para>
After starting <filename>socket</filename>, you should see the following
output in the terminal:
</para>
<screen>I've been embedded.
A plug was added</screen>
<para>
The first line of output is from <filename>plug</filename>, after it has
been notified that it has been embedded inside of a
<classname>Socket</classname>. The second line was emitted by
<filename>socket</filename> in response to its
<methodname>plug_added</methodname> signal. If everything was done as
described above, the <filename>socket</filename> window should look
roughly like the following:
</para>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;socket.png"/>
</screenshot>
<para>
If for some reason the <classname>Socket</classname> couldn't attach the
<classname>Plug</classname>, the window would look something like this:
</para>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;socket-fail.png"/>
</screenshot>
</sect1>
</chapter>
<chapter id="chapter-keyboardevents">
<title>Keyboard Events</title>
<para>
X events differ in some ways from other signals. These differences are described
in the <link linkend="sec-xeventsignals">X Event signals</link> section in
the appendix. Here we will use keyboard events to show how X events can be
used in a program.
</para>
<sect1 id="sec-keyboardevents-overview">
<title>Overview</title>
<para>
Whenever you press or release a key, an event is emitted. You can connect
a signal handler to handle such events.
</para>
<para>
To receive the keyboard events, you must first call the
<methodname>Gtk::Widget::add_events()</methodname> function with a bit
mask of the events you're interested in. The event signal handler will
receive an argument that depends on the type of event. For keyboard
events it's a <type>GdkEventKey*</type>. As discribed in the
<link linkend="sec-xeventsignals">appendix</link>, the event signal handler
returns a <type>bool</type> value, to indicate that the signal is fully
handled (<literal>true</literal>) or allow event propagation
(<literal>false</literal>).
</para>
<para>
To determine which key was pressed or released, you read the value of
<varname>GdkEventKey::keyval</varname> and compare it with a constant in the
<filename><gdk/gdkkeysyms.h></filename> header file. The states of
modifier keys (shift, ctrl, etc.) are available as bit-flags in
<varname>GdkEventKey::state</varname>.
</para>
<para>
Here's a simple example:
<programlisting>
bool on_key_press_or_release_event(GdkEventKey* event)
{
if (event->type == GDK_KEY_PRESS &&
event->keyval == GDK_KEY_1 &&
(event->state & (GDK_SHIFT_MASK | GDK_CONTROL_MASK | GDK_MOD1_MASK)) == GDK_MOD1_MASK)
{
handle_alt_1_press(); // GDK_MOD1_MASK is normally the Alt key
return true;
}
return false;
}
Gtk::Entry m_entry; // in a class definition
// in the class constructor
m_entry.signal_key_press_event().connect( sigc::ptr_fun(&on_key_press_or_release_event) );
m_entry.signal_key_release_event().connect( sigc::ptr_fun(&on_key_press_or_release_event) );
m_entry.add_events(Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK);
</programlisting>
</para>
<sect2 id="keyboardevents-simple-example">
<title>Example</title>
<para>
In this example there are three keyboard shortcuts:
<keycap>Alt</keycap>+<keycap>1</keycap> selects the first radio button,
<keycap>Alt</keycap>+<keycap>2</keycap> selects the second one, and the
<keycap>Esc</keycap> key hides (closes) the window.
The default event signal handler is overridden, as described in the
<link linkend="sec-overriding-default-signal-handlers">Overriding default signal handlers</link>
section in the appendix.
</para>
<figure id="figure-keyboardevents-simple">
<title>Keyboard Events - Simple</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;keyboardevents_simple.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;keyboard_events/simple/">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-keyboardevents-propagation">
<title>Event Propagation</title>
<para>
Event propagation means that, when an event is emitted on a particular
widget, it can be passed to its parent widget (and that widget can pass
it to its parent, and so on) and, if the parent has an event handler,
that handler will be called.
</para>
<para>
Contrary to other events, keyboard events are first sent to the toplevel window
(<classname>Gtk::Window</classname>), where it will be checked
for any keyboard shortcuts that may be set (accelerator keys and mnemonics,
used for selecting menu items from the keyboard). After this (and assuming
the event wasn't handled), it is sent to the widget which has focus,
and the propagation begins from there.
</para>
<para>
The event will propagate until it reaches the top-level widget, or until
you stop the propagation by returning <literal>true</literal> from an
event handler.
</para>
<para>
Notice, that after canceling an event, no other function will be called
(even if it is from the same widget).
</para>
<sect2 id="keyboardevents-propagation-example">
<title>Example</title>
<para>
In this example there are three event handlers that are called after
<classname>Gtk::Window</classname>'s default event handler, one in the
<classname>Gtk::Entry</classname>, one in the <classname>Gtk::Grid</classname>
and one in the <classname>Gtk::Window</classname>.
</para>
<para>
In the <classname>Gtk::Window</classname>, we have also the default handler
overridden (<methodname>on_key_release_event()</methodname>), and
another handler being called before the default handler
(<methodname>windowKeyReleaseBefore()</methodname>).
</para>
<para>
The purpose of this example is to show the steps the event takes when it is emitted.
</para>
<para>
When you write in the entry, a key release event will be emitted,
which will go first to the toplevel window (<classname>Gtk::Window</classname>),
since we have one event handler set to be called before, that's what is
called first (<methodname>windowKeyReleaseBefore()</methodname>).
Then the default handler is called (which we have overridden), and after
that the event is sent to the widget that has focus,
the <classname>Entry</classname> in our example and, depending on whether we let
it propagate, it can reach the <classname>Grid</classname>'s and the
<classname>Window</classname>'s event handlers. If it propagates,
the text you're writing will appear in the <classname>Label</classname>
above the <classname>Entry</classname>.
</para>
<figure id="figure-keyboardevents-propagation">
<title>Keyboard Events - Event Propagation</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;keyboardevents_propagation.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;keyboard_events/propagation/">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-chapter-timeouts">
<title>Timeouts, I/O and Idle Functions </title>
<sect1 id="sec-timeouts">
<title>Timeouts</title>
<para>
You may be wondering how to make >kmm; do useful work while it's idling along. Happily,
you have several options. Using the following methods you can create a timeout
method that will be called every few milliseconds.
</para>
<para>
<programlisting>
sigc::connection Glib::SignalTimeout::connect(const sigc::slot<bool>& slot,
unsigned int interval, int priority = Glib::PRIORITY_DEFAULT);
</programlisting>
</para>
<para>
The first argument is a <classname>slot</classname> you wish to have called
when the timeout occurs. The second argument is the number of milliseconds
between calls to that method. You receive a
<classname>sigc::connection</classname> object that can be used to deactivate
the connection using its <methodname>disconnect()</methodname> method:
</para>
<para>
<programlisting>
my_connection.disconnect();
</programlisting>
</para>
<para>
Another way of destroying the connection is your signal handler.
It has to be of the type <classname>sigc::slot<bool></classname>.
As you see from the definition your signal handler has to return a value of
the type <literal>bool</literal>. A definition of a sample method might
look like this:
<programlisting>
bool MyCallback() { std::cout << "Hello World!\n" << std::endl; return true; }
</programlisting>
</para>
<para>
You can stop the timeout method by returning <literal>false</literal> from
your signal handler. Therefore, if you want your
method to be called repeatedly, it should return <literal>true</literal>.
</para>
<para>
Here's an example of this technique:
</para>
<para><ulink url="&url_examples_base;timeout/">Source Code</ulink></para>
</sect1>
<sect1 id="sec-monitoring-io">
<title>Monitoring I/O</title>
<para>
A nifty feature of Glib (one of the libraries underlying
>kmm;) is the ability to have it check for data on a file descriptor
for you. This is especially useful for networking applications. The
following method is used to do this:
</para>
<para>
<programlisting>
sigc::connection Glib::SignalIO::connect(const sigc::slot<bool,Glib::IOCondition>& slot,
int fd, Glib::IOCondition condition,
int priority = Glib::PRIORITY_DEFAULT);
</programlisting>
</para>
<para>
The first argument is a slot you wish to have called when
the specified event (see argument 3) occurs on the file descriptor you specify
using argument two. Argument three may be one or more (using
<literal>|</literal>) of:
</para>
<itemizedlist>
<listitem>
<para>
Glib::IO_IN - Call your method when there is data ready for
reading on your file descriptor.
</para>
</listitem>
<listitem>
<para>
Glib::IO_OUT - Call your method when the file descriptor is
ready for writing.
</para>
</listitem>
<listitem>
<para>
Glib::IO_PRI - Call your method when the file descriptor has urgent data to be read.
</para>
</listitem>
<listitem>
<para>
Glib::IO_ERR - Call your method when an error has occurred on the file descriptor.
</para>
</listitem>
<listitem>
<para>
Glib::IO_HUP - Call your method when hung up (the connection has been broken usually for pipes and sockets).
</para>
</listitem>
</itemizedlist>
<para>
The return value is a <classname>sigc::connection</classname> that may be used to stop monitoring
this file descriptor using its <methodname>disconnect()</methodname> method. The
<parameter>slot</parameter> signal handler should be declared as follows:
</para>
<para>
<programlisting>
bool input_callback(Glib::IOCondition condition);
</programlisting>
</para>
<para>
where <parameter>condition</parameter> is as
specified above. As usual the slot is created with
<function>sigc::mem_fun()</function> (for a member method of an object), or
<function>sigc::ptr_fun()</function> (for a function).
</para>
<para>
A little example follows. To use the example just execute it from a terminal;
it doesn't create a window. It will create a pipe named
<literal>testfifo</literal> in the current directory. Then start another shell
and execute <literal>echo "Hello" > testfifo</literal>. The example will
print each line you enter until you execute <literal>echo "Q" >
testfifo</literal>.
</para>
<para><ulink url="&url_examples_base;input/">Source Code</ulink></para>
</sect1>
<sect1 id="sec-idle-functions">
<title>Idle Functions</title>
<para>
If you want to specify a method that gets called when nothing else is happening, use the following:
</para>
<para>
<programlisting>
sigc::connection Glib::SignalIdle::connect(const sigc::slot<bool>& slot,
int priority = Glib::PRIORITY_DEFAULT_IDLE);
</programlisting>
</para>
<para>
This causes >kmm; to call the specified method whenever nothing else is
happening. You can add a priority (lower numbers are higher priorities). There are two ways to remove the signal handler: calling
<methodname>disconnect()</methodname> on the
<classname>sigc::connection</classname> object, or returning
<literal>false</literal> in the signal handler, which should be declared
as follows:
</para>
<para>
<programlisting>
bool idleFunc();
</programlisting>
</para>
<para>
Since this is very similar to the methods above this explanation should
be sufficient to understand what's going on. However, here's a little example:
</para>
<para><ulink url="&url_examples_base;idle/">Source Code</ulink></para>
<para>
This example points out the difference of idle and timeout methods a
little. If you need methods that are called periodically, and speed
is not very important, then you want timeout methods. If
you want methods that are called as often as possible (like
calculating a fractal in background), then use idle methods.
</para>
<para>
Try executing the example and increasing the system load. The upper
progress bar will increase steadily; the lower one will slow down.
</para>
</sect1>
</chapter>
<chapter id="chapter-memory">
<title>Memory management</title>
<sect1 id="sec-memory-widgets">
<title>Widgets</title>
<sect2 id="memory-normal">
<title>Normal C++ memory management</title>
<para>
>kmm; allows the programmer to control the lifetime (that is, the construction
and destruction) of any widget in the same manner as any other C++ object.
This flexibility allows you to use <literal>new</literal> and
<literal>delete</literal> to create and destroy objects dynamically
or to use regular class members (that are destroyed automatically when the
class is destroyed) or to use local instances (that are destroyed when the
instance goes out of scope). This flexibility is not present in some C++ GUI
toolkits, which restrict the programmer to only a subset of C++'s memory
management features.
</para>
<para>Here are some examples of normal C++ memory management:</para>
<sect3 id="memory-class-scope">
<title>Class Scope widgets</title>
<para>
If a programmer does not need dynamic memory allocation, automatic widgets in class
scope may be used. One advantage of automatic widgets in class scope is that
memory management is grouped in one place. The programmer does not
risk memory leaks from failing to <literal>delete</literal> a widget.
</para>
<para>
The primary disadvantage of using class scope widgets is revealing
the class implementation rather than the class interface in the class header.
</para>
<para>
<programlisting>
#include <gtkmm/button.h>
#include <gtkmm/window.h>
class Foo : public Gtk::Window
{
private:
Gtk::Button theButton;
// will be destroyed when the Foo object is destroyed
};
</programlisting>
</para>
</sect3>
<sect3 id="memory-function-scope">
<title>Function scope widgets</title>
<para>
If a programmer does not need a class scope widget, a function scope widget
may also be used. The advantages to function scope over class scope are the
increased data hiding and reduced dependencies.
<programlisting>
{
Gtk::Button aButton;
aButton.show();
...
app->run();
}
</programlisting>
</para>
</sect3>
<sect3 id="memory-dynamic-allocation">
<title>Dynamic allocation with new and delete</title>
<para>
Although, in most cases, the programmer will prefer to allow containers to
automatically destroy their children using <function>Gtk::manage()</function> (see
below), the programmer is not required to use <function>Gtk::manage()</function>.
The traditional <literal>new</literal> and <literal>delete</literal> operators
may also be used.
<programlisting>
Gtk::Button* pButton = new Gtk::Button("Test");
// do something useful with pButton
delete pButton;
</programlisting>
Here, the programmer deletes <varname>pButton</varname> to prevent a memory leak.
</para>
</sect3>
</sect2>
<sect2 id="memory-managed-widgets">
<title>Managed Widgets</title>
<para>
Alternatively, you can let a widget's container control when the widget is
destroyed. In most cases, you want a widget to last only as long as the
container it is in. To delegate the management of a widget's lifetime to its
container, first create it with <function>Gtk::manage()</function> and
pack it into its container with <methodname>Gtk::Container::add()</methodname>,
<methodname>Gtk::Box::pack_start()</methodname>, or a similar method. Now the
widget will be destroyed whenever its container is destroyed.
</para>
<sect3 id="memory-managed-dynamic">
<title>Dynamic allocation with manage() and add()</title>
<para>
>kmm; provides the <function>manage()</function> function and
<methodname>add()</methodname> methods to create and destroy widgets. Every widget
except a top-level window must be added or packed into a container in order to
be displayed. The <function>manage()</function> function marks a widget
so that when the widget is added to a container, the container becomes
responsible for deleting the widget.
</para>
<para>
<programlisting>
MyContainer::MyContainer()
{
Gtk::Button* pButton = Gtk::manage(new Gtk::Button("Test"));
add(*pButton); //add *pButton to MyContainer
}
</programlisting>
Now, when objects of type <classname>MyContainer</classname> are destroyed, the
button will also be deleted. It is no longer necessary to delete <varname>pButton</varname>
to free the button's memory; its deletion has been delegated to the
<classname>MyContainer</classname> object.
</para>
<para>
Of course, a top-level container will not be added to another container. The
programmer is responsible for destroying the top-level container using one of
the traditional C++ techniques. For instance, your top-level Window might just
be an instance in your <function>main()</function> function.
</para>
</sect3>
</sect2>
</sect1>
<sect1 id="sec-memory-shared-resources">
<title>Shared resources</title>
<para>
Some objects, such as <classname>Gdk::Pixbuf</classname>s and
<classname>Pango::Font</classname>s, are obtained from a shared store.
Therefore you cannot instantiate your own instances. These classes typically
inherit from <classname>Glib::Object</classname>. Rather than requiring you to
reference and unreference these objects, >kmm; uses the
<classname>Glib::RefPtr<></classname> smartpointer. Cairomm has its own
smartpointer, <classname>Cairo::RefPtr<></classname>.
</para>
<para>
Objects such as <classname>Gdk::Pixbuf</classname> can only be instantiated
with a <methodname>create()</methodname> function. For instance,
<programlisting>
Glib::RefPtr<Gdk::Pixbuf> pixbuf = Gdk::Pixbuf::create_from_file(filename);
</programlisting>
</para>
<para>
You have no way of getting a bare <classname>Gdk::Pixbuf</classname>. In the
example, <varname>pixbuf</varname> is a smart pointer, so you can do this, much
like a normal pointer:
<programlisting>
int width = 0;
if(pixbuf)
{
width = pixbuf->get_width();
}
</programlisting>
</para>
<para>
When <varname>pixbuf</varname> goes out of scope an
<methodname>unref()</methodname> will happen in the background and you don't need
to worry about it anymore. There's no <literal>new</literal> so there's no
<literal>delete</literal>.
</para>
<para>
If you copy a <classname>RefPtr</classname>, for instance
<programlisting>
Glib::RefPtr<Gdk::Pixbuf> pixbuf2 = pixbuf;
</programlisting>
, or if you pass it as a method argument or a return type, then
<classname>RefPtr</classname> will do any necessary referencing to ensure that
the instance will not be destroyed until the last <classname>RefPtr</classname>
has gone out of scope.
</para>
<para>See the <link linkend="chapter-refptr">appendix</link> for detailed information about RefPtr.</para>
<para>
If you wish to learn more about smartpointers, you might look in these
books:
<itemizedlist>
<listitem><para>
Bjarne Stroustrup, "The C++ Programming Language" Forth Edition - section 34.3
</para></listitem>
<listitem><para>
Nicolai M. Josuttis, "The C++ Standard Library" - section 4.2
</para></listitem>
</itemizedlist>
</para>
</sect1>
</chapter>
<chapter id="chapter-builder">
<title>Glade and Gtk::Builder</title>
<para>
Although you can use C++ code to instantiate and arrange widgets, this
can soon become tedious and repetitive. And it requires a recompilation to show
changes. The <application>Glade</application> application allows you to layout
widgets on screen and then save an XML description of the arrangement. Your
application can then use the <application>Gtk::Builder</application> API to load
that XML file at runtime and obtain a pointer to specifically named widget
instances.
</para>
<para>
This has the following advantages:
<orderedlist>
<listitem><simpara>Less C++ code is required.</simpara></listitem>
<listitem><simpara>UI changes can be seen more quickly, so UIs are able to improve.</simpara></listitem>
<listitem><simpara>Designers without programming skills can create and edit UIs.</simpara></listitem>
</orderedlist>
</para>
<para>
You still need C++ code to deal with User Interface changes triggered by user
actions, but using <application>Gtk::Builder</application> for the widget
layout allows you to focus on implementing that functionality.
</para>
<sect1 id="sec-builder-loading-glade-file">
<title>Loading the .glade file</title>
<para>
<classname>Gtk::Builder</classname> must be used via a
<classname>Glib::RefPtr</classname>. Like all such classes, you need to use a
<methodname>create()</methodname> method to instantiate it. For instance,
<programlisting>
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("basic.glade");
</programlisting>
This will instantiate the windows defined in the .glade file, though they will
not be shown immediately unless you have specified that via the <guilabel>Properties</guilabel>
window in <application>Glade</application>.
</para>
<para>To instantiate just one window, or just one of the child widgets, you can specify the name of a widget as the second parameter. For instance,
<programlisting>
Glib::RefPtr<Gtk::Builder> builder = Gtk::Builder::create_from_file("basic.glade", "treeview_products");
</programlisting>
</para>
</sect1>
<sect1 id="sec-builder-accessing-widgets">
<title>Accessing widgets</title>
<para>
To access a widget, for instance to <methodname>show()</methodname> a dialog, use
the <methodname>get_widget()</methodname> method, providing the widget's name. This
name should be specified in the <application>Glade</application> Properties
window. If the widget could not be found, or is of the wrong type, then the
pointer will be set to 0.
<programlisting>
Gtk::Dialog* pDialog = 0;
builder->get_widget("DialogBasic", pDialog);
</programlisting>
</para>
<para>
<application>Gtk::Builder</application> checks for a null pointer, and checks
that the widget is of the expected type, and will show warnings on the command
line about these.
</para>
<para>
Remember that you are not instantiating a widget with
<methodname>get_widget()</methodname>, you are just obtaining a pointer to one that
already exists. You will always receive a pointer to the same instance when you
call <methodname>get_widget()</methodname> on the same
<classname>Gtk::Builder</classname>, with the same widget name. The
widgets are instantiated during <methodname>Gtk::Builder::create_from_file()</methodname>.
</para>
<para>
<methodname>get_widget()</methodname> returns child widgets that are
<function>manage()</function>ed (see the <link linkend="chapter-memory">Memory
Management</link> chapter), so they will be deleted when their parent
container is deleted. So, if you get only a child widget from
<application>Gtk::Builder</application>, instead of a whole window, then you must
either put it in a <classname>Container</classname> or delete it.
<classname>Windows</classname> (such as <classname>Dialogs</classname>) cannot
be managed because they have no parent container, so you must delete them at
some point.
</para>
<sect2 id="builder-example-loading">
<title>Example</title>
<para>
This simple example shows how to load a <application>Glade</application> file at runtime and access the widgets with
<application>Gtk::Builder</application>.
</para>
<para><ulink url="&url_examples_base;builder/basic">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-builder-using-derived-widgets">
<title>Using derived widgets</title>
<para>
You can use <application>Glade</application> to layout your own custom widgets
derived from >kmm; widget classes. This keeps your code organized and
encapsulated. Of course you won't see the exact appearance and properties of
your derived widget in <application>Glade</application>, but you can specify
its location and child widgets and the properties of its >kmm; base class.
</para>
<para>Use <methodname>Gtk::Builder::get_widget_derived()</methodname> like so:
<programlisting>
DerivedDialog* pDialog = 0;
builder->get_widget_derived("DialogBasic", pDialog);
</programlisting>
</para>
<para>
Your derived class must have a constructor that takes a pointer to the
underlying C type, and the <classname>Gtk::Builder</classname> instance.
All relevant classes of >kmm; typedef their underlying C type as
<classname>BaseObjectType</classname> (<classname>Gtk::Dialog</classname>
typedefs <classname>BaseObjectType</classname> as <type>GtkDialog</type>, for instance).
</para>
<para>
You must call the base class's constructor in the initialization list, providing the C pointer. For
instance,
<programlisting>
DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::Dialog(cobject)
{
}
</programlisting>
</para>
<para>
You could then encapsulate the manipulation of the child widgets in the
constructor of the derived class, maybe using <methodname>get_widget()</methodname>
or <methodname>get_widget_derived()</methodname> again. For instance,
<programlisting>
DerivedDialog::DerivedDialog(BaseObjectType* cobject, const Glib::RefPtr<Gtk::Builder>& builder)
: Gtk::Dialog(cobject),
m_builder(builder),
m_pButton(0)
{
//Get the Glade-instantiated Button, and connect a signal handler:
m_builder->get_widget("quit_button", m_pButton);
if(m_pButton)
{
m_pButton->signal_clicked().connect( sigc::mem_fun(*this, &DerivedDialog::on_button_quit) );
}
}
</programlisting>
</para>
<sect2 id="builder-example-accessing">
<title>Example</title>
<para>
This example shows how to load a <application>Glade</application> file at runtime and access the widgets via a derived class.
</para>
<para><ulink url="&url_examples_base;builder/derived">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-internationalization">
<title>Internationalization and Localization</title>
<para>
>kmm; applications can easily support multiple languages, including
non-European languages such as Chinese and right-to-left languages such as
Arabic. An appropriately-written and translated >kmm; application will use
the appropriate language at runtime based on the user's environment.
</para>
<para>
You might not anticipate the need to support additional languages, but
you can never rule it out. And it's easier to develop the application
properly in the first place rather than retrofitting later.
</para>
<para>
The process of writing source code that allows for translation is called
<literal>internationalization</literal>, often abbreviated to
<literal>i18n</literal>. The <literal>Localization</literal> process,
sometimes abbreviated as <literal>l10n</literal>, provides translated text
for other languages, based on that source code.
</para>
<para>
The main activity in the internationalization process is finding strings
seen by users and marking them for translation. You do not need to do it all
at once - if you set up the necessary project infrastructure correctly then
your application will work normally regardless of how many strings you've
covered.
</para>
<para>
String literals should be typed in the source code in English, but
surrounded by a macro. The <application>gettext</application> (or intltool)
utility can then extract the marked strings for translation, and substitute
the translated text at runtime.
</para>
<sect1 id="sec-internationalization-intro">
<title>Preparing your project</title>
<note>
<para>
In the instructions below we will assume that you will not be using
<application>gettext</application> directly, but
<application>intltool</application>, which was written specifically for
<literal>GNOME</literal>. <application>intltool</application> uses
<function>gettext()</function>, which extracts strings from source code,
but <application>intltool</application> can also combine strings from
other files, for example from desktop menu details, and GUI resource
files such as <application>Glade</application> files, into standard
<application>gettext</application> <filename>.pot/.po</filename> files.
</para>
<para>
We also assume that you are using autotools (e.g.
<application>automake</application> and
<application>autoconf</application>) to build your project, and
that you are using <ulink
url="http://git.gnome.org/browse/gnome-common/tree/autogen.sh">
<literal>./autogen.sh</literal> from
<application>gnome-common</application></ulink>, which, among other
things, takes care of some <application>intltool</application>
initialization.
</para>
</note>
<para>
Create a sub-directory named <literal>po</literal> in your project's root
directory. This directory will eventually contain all of your
translations. Within it, create a file named <literal>LINGUAS</literal>
and a file named <literal>POTFILES.in</literal>. It is common practice to
also create a <literal>ChangeLog</literal> file in the
<literal>po</literal> directory so that translators can keep track of
translation changes.
</para>
<para>
<literal>LINGUAS</literal> contains an alphabetically sorted list of codes
identifying the languages for which your program is translated (comment
lines starting with a <literal>#</literal> are ignored). Each language
code listed in the <literal>LINGUAS</literal> file must have a
corresponding <literal>.po</literal> file. So, if your program has German
and Japanese translations, your <literal>LINGUAS</literal> file would
look like this:
</para>
<programlisting># keep this file sorted alphabetically, one language code per line
de
ja</programlisting>
<para>
(In addition, you'd have the files <literal>ja.po</literal> and
<literal>de.po</literal> in your
<literal>po</literal> directory which contain the German and Japanese
translations, respectively.)
</para>
<para>
<literal>POTFILES.in</literal> is a list of paths to all files which
contain strings marked up for translation, starting from the project root
directory. So for example, if your project sources were located in a
subdirectory named <literal>src</literal>, and you had two files that
contained strings that should be translated, your
<literal>POTFILES.in</literal> file might look like this:
</para>
<programlisting>src/main.cc
src/other.cc</programlisting>
<para>
If you are using <application>gettext</application> directly, you can only
mark strings for translation if they are in source code file. However, if
you use <application>intltool</application>, you can mark strings for
translation in a variety of other file formats, including
<application>Glade</application> UI files, xml, <ulink
url="http://standards.freedesktop.org/desktop-entry-spec/latest/">.desktop
files</ulink> and several more. So, if you have designed some of the
application UI in <application>Glade</application> then also add your
<filename>.glade</filename> files to the list in
<literal>POTFILES.in</literal>.
</para>
<para>
Now that there is a place to put your translations, you need to initialize
<application>intltool</application> and <application>gettext</application>.
Add the following code to your <literal>configure.ac</literal>,
substituting 'programname' with the name of your program:
</para>
<programlisting>IT_PROG_INTLTOOL([0.35.0])
GETTEXT_PACKAGE=programname
AC_SUBST(GETTEXT_PACKAGE)
AC_DEFINE_UNQUOTED([GETTEXT_PACKAGE], ["$GETTEXT_PACKAGE"],
[The domain to use with gettext])
AM_GLIB_GNU_GETTEXT
PROGRAMNAME_LOCALEDIR=[${datadir}/locale]
AC_SUBST(PROGRAMNAME_LOCALEDIR)</programlisting>
<para>
This <varname>PROGRAMNAME_LOCALEDIR</varname> variable will be used later
in the <literal>Makefile.am</literal> file, to define a macro that will be
used when you initialize <application>gettext</application> in your source
code.
</para>
<para>
In the top-level Makefile.am:
<itemizedlist>
<listitem>
<para>Add <literal>po</literal> to the <literal>SUBDIRS</literal>
variable. Without this, your translations won't get built and
installed when you build the program</para>
</listitem>
<listitem>
<para>
Define <literal>INTLTOOL_FILES</literal> as:
<programlisting>INTLTOOL_FILES = intltool-extract.in \
intltool-merge.in \
intltool-update.in</programlisting>
</para>
</listitem>
<listitem>
<para>
Add <literal>INTLTOOL_FILES</literal> to the
<literal>EXTRA_DIST</literal> list of files. This ensures that when
you do a <command>make dist</command>, these commands will be
included in the source tarball.
</para>
</listitem>
<listitem>
<para>
Update your <literal>DISTCLEANFILES</literal>:
<programlisting>DISTCLEANFILES = ... intltool-extract \
intltool-merge \
intltool-update \
po/.intltool-merge-cache</programlisting>
</para>
</listitem>
</itemizedlist>
</para>
<para>
In your <literal>src/Makefile.am</literal>, update your
<literal>AM_CPPFLAGS</literal> to add the following preprocessor macro
definition:
</para>
<programlisting>AM_CPPFLAGS = ... -DPROGRAMNAME_LOCALEDIR=\"${PROGRAMNAME_LOCALEDIR}\"</programlisting>
<para>
This macro will be used when you initialize <literal>gettext</literal> in
your source code.
</para>
</sect1>
<sect1 id="sec-i18n-marking-strings">
<title>Marking strings for translation</title>
<para>
String literals should be typed in the source code in English, but
they should be surrounded by a call to the <function>gettext()</function>
function. These strings will be extracted for translation and the
translations may be used at runtime instead of the original English
strings.
</para>
<para>
The <application>GNU gettext</application> package allows you to mark
strings in source code, extract those strings for translation, and use
the translated strings in your application.
</para>
<para>
However, <application>Glib</application> defines
<function>gettext()</function>
support macros which are shorter wrappers in an easy-to-use form.
To use these macros, include <literal><glibmm/i18n.h></literal>,
and then, for example, substitute:
<programlisting>display_message("Getting ready for i18n.");</programlisting>
with:
<programlisting>display_message(_("Getting ready for i18n."));</programlisting>
</para>
<para>
For reference, it is possible to generate a file which contains all
strings which appear in your code, even if they are not marked for translation,
together with file name and line
number references. To generate such a file named
<literal>my-strings</literal>, execute the following command,
within the source code directory:
<programlisting>xgettext -a -o my-strings --omit-header *.cc *.h</programlisting>
</para>
<para>
Finally, to let your program use the translation for the current locale,
add this code to the beginning of your <filename>main.cc</filename> file, to initialize gettext.
<programlisting>bindtextdomain(GETTEXT_PACKAGE, PROGRAMNAME_LOCALEDIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);</programlisting>
</para>
<sect2 id="sec-i18n-gettext">
<title>How gettext works</title>
<para>
<application>intltool</application> /
<application>xgettext</application> script extracts the strings
and puts them in a <filename>mypackage.pot</filename> file.
The translators of your application create their translations by
first copying this <filename>.pot</filename> file to a
<filename>localename.po</filename> file. A locale identifies a
language and an encoding for that language, including date and numerical
formats. Later, when the text in your source code has changed, the
<literal>msmerge</literal> script is used to update the
<filename>localename.po</filename> files from the regenerated
<filename>.pot</filename> file.
</para>
<para>
At install time, the <filename>.po</filename> files are converted to
a binary format (with the extension <filename>.mo</filename>) and
placed in a system-wide directory for locale files, for example
<filename>/usr/share/locale/</filename>.
</para>
<para>
When the application runs, the <application>gettext</application>
library checks the system-wide directory to see if there is a
<filename>.mo</filename> file for the user's locale environment
(you can set the locale with, for instance, "export LANG=de_DE.UTF-8"
from a bash console). Later, when the program reaches a
<literal>gettext</literal> call, it looks for a translation of a
particular string. If none is found, the original string is used.
</para>
</sect2>
<sect2 id="sec-i18n-testing">
<title>Testing and adding translations</title>
<para>
To convince yourself that you've done well, you may wish to add a
translation for a new locale. In order to do that, go to the
<filename>po</filename> subdirectory of your project and
execute the following command:
<programlisting>intltool-update --pot</programlisting>
</para>
<para>
That will create a file named <filename>programname.pot</filename>.
Now copy that file to <filename>languagecode.po</filename>, such as
<filename>de.po</filename> or <filename>hu.po</filename>. Also add
that language code to <literal>LINGUAS</literal>. The
<filename>.po</filename> file contains a header and a list of English strings,
with space for the translated strings to be entered. Make sure you set the
encoding of the <filename>.po</filename> file (specified in the header, but
also as content) to <literal>UTF-8</literal>.
</para>
<!-- TODO: This need more explanation. What's the point of the fuzzy tag then? murrayc -->
<note>
<para>
It's possible that certain strings will be marked as
<literal>fuzzy</literal> in the <filename>.po</filename> file.
These translations will not substitute the original string. To make
them appear, simply remove the <literal>fuzzy</literal> tag.
</para>
</note>
</sect2>
<sect2 id="sec-i18n-resources">
<title>Resources</title>
<para>
More information about what lies behind the internationalization and localization process
is presented and demonstrated in:
<itemizedlist>
<listitem>
<para>
<ulink url="https://wiki.gnome.org/TranslationProject/DevGuidelines">
L10N Guidelines for Developers</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="http://bazaar.launchpad.net/~intltool/intltool/trunk/view/head:/README">Intltool README</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="https://wiki.gnome.org/TranslationProject/GitHowTo">How to use Git for GNOME translators</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="http://www.gnu.org/software/gettext/manual/gettext.html">gettext manual</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="http://ftp.gnome.org/pub/GNOME/sources/gtkmm_hello/"><literal>gtkmm_hello</literal> example package</ulink>
</para>
</listitem>
<listitem>
<para>
<ulink url="http://ftp.gnome.org/pub/GNOME/sources/gnomemm_hello/"><literal>gnomemm_hello</literal> example package</ulink>
</para>
</listitem>
</itemizedlist>
</para>
</sect2>
</sect1>
<sect1 id="sec-i18n-expecting-utf8">
<title>Expecting UTF8</title>
<para>
A properly internationalized application will not make assumptions about the
number of bytes in a character. That means that you shouldn't use pointer
arithmetic to step through the characters in a string, and it means you
shouldn't use <classname>std::string</classname> or standard C functions such
as <function>strlen()</function> because they make the same assumption.
</para>
<para>
However, you probably already avoid bare char* arrays and pointer arithmetic by
using <classname>std::string</classname>, so you just need to start using
<classname>Glib::ustring</classname> instead. See the <link
linkend="sec-basics-ustring">Basics</link> chapter about
<classname>Glib::ustring</classname>.
</para>
<sect2 id="i18n-ustring-iostreams"><title>Glib::ustring and std::iostreams</title>
<!-- <para>TODO: This section is not clear - it needs to spell things out more clearly and obviously.</para> -->
<para>
Unfortunately, the integration with the standard iostreams is not completely
foolproof. >kmm; converts <classname>Glib::ustring</classname>s to a
locale-specific encoding (which usually is not UTF-8) if you output them to an
<classname>ostream</classname> with <function>operator<<</function>.
Likewise, retrieving <classname>Glib::ustrings</classname> from
<classname>istream</classname> with <function>operator>></function>
causes a conversion in the opposite direction. But this scheme breaks down if
you go through a <classname>std::string</classname>, e.g. by inputting text
from a stream to a <classname>std::string</classname> and then implicitly
converting it to a <classname>Glib::ustring</classname>. If the string
contained non-ASCII characters and the current locale is not UTF-8 encoded, the
result is a corrupted <classname>Glib::ustring</classname>. You can work around
this with a manual conversion. For instance, to retrieve the
<classname>std::string</classname> from a <classname>ostringstream</classname>:
<programlisting>std::ostringstream output;
output.imbue(std::locale("")); // use the user's locale for this stream
output << percentage << " % done";
label->set_text(Glib::locale_to_utf8(output.str()));</programlisting>
</para>
</sect2>
</sect1>
<sect1 id="sec-i18n-pitfalls">
<title>Pitfalls</title>
<para>There are a few common mistakes that you would discover eventually yourself. But this section might help you to avoid them.</para>
<sect2 id="i18n-string-semantics">
<title>Same strings, different semantics</title>
<para>Sometimes two english strings are identical but have different meanings in
different contexts, so they would probably not be identical when translated. Since the English strings are
used as look-up keys, this causes problems.</para>
<para>
In these cases, you should add extra characters to the strings. For instance,
use <literal>"jumps[noun]"</literal> and <literal>"jumps[verb]"</literal>
instead of just <literal>"jumps"</literal> and strip them again outside the
<function>gettext</function> call. If you add extra characters you should also
add a comment for the translators before the <function>gettext</function> call.
Such comments will be shown in the <filename>.po</filename> files. For
instance:
</para>
<programlisting>// note to translators: don't translate the "[noun]" part - it is
// just here to distinguish the string from another "jumps" string
text = strip(gettext("jumps[noun]"), "[noun]");</programlisting>
</sect2>
<sect2 id="i18n-composition">
<title>Composition of strings</title>
<para>
C programmers use <function>sprintf()</function> to compose and concatenate
strings. C++ favours streams, but unfortunately, this approach makes
translation difficult, because each fragment of text is translated separately,
without allowing the translators to rearrange them according to the grammar of
the language.</para>
<para>For instance, this code would be problematic:</para>
<programlisting>std::cout << _("Current amount: ") << amount
<< _(" Future: ") << future << std::endl;
label.set_text(_("Really delete ") + filename + _(" now?"));</programlisting>
<para>
So you should either avoid this situation or use
<ulink url="&url_refdocs_base_glib;ustring.html"><function>Glib::ustring::compose()</function></ulink>
which supports syntax such as:
<programlisting>std::cout << Glib::ustring::compose(
_("Current amount: %1 Future: %2"), amount, future) << std::endl;
label.set_text(Glib::ustring::compose(_("Really delete %1 now?"), filename));</programlisting>
</para>
</sect2>
<sect2 id="i18n-display-size">
<title>Assuming the displayed size of strings</title>
<para>You never know how much space a string will take on screen when translated. It might very possibly be twice the size of the original English string. Luckily, most >kmm; widgets will expand at runtime to the required size.</para>
</sect2>
<sect2 id="i18n-unusual-words">
<title>Unusual words</title>
<para>You should avoid cryptic abbreviations, slang, or jargon.
They are usually difficult to translate, and are often difficult
for even native speakers to understand. For instance, prefer "application" to "app"</para>
</sect2>
<sect2 id="i18n-non-ascii-characters">
<title>Using non-ASCII characters in strings</title>
<para>
Currently, <application>gettext</application> does not support non-ASCII
characters (i.e. any characters with a code above 127) in source code. For
instance, you cannot use the copyright sign (©).
</para>
<para>To work around this, you could write a comment in the
source code just before the string, telling the translators to
use the special character if it is available in their languages. For english, you could then make an American English
<filename>en_US.po</filename> translation which used that special character.</para>
</sect2>
</sect1>
<sect1 id="sec-i18n-getting-help-with-translations">
<title>Getting help with translations</title>
<para>If your program is free software, there is a whole <literal>GNOME</literal>
subproject devoted to helping you make translations, the
<ulink url="https://wiki.gnome.org/TranslationProject/"><literal>GNOME</literal>
Translation Project</ulink>.</para>
<para>The way it works is that you upload your source code to a git
repository where translators can access it, then contact the gnome-i18n
mailing list and ask to have your program added to the
<ulink url="http://l10n.gnome.org/module/">list of modules to translate</ulink>.</para>
<para>Then you make sure you update the file
<filename>POTFILES.in</filename> in the
<filename>po/</filename> subdirectory
(<command>intltool-update -M</command> can help with this) so
that the translators always access updated
<filename>myprogram.pot</filename> files, and simply freeze
the strings at least a couple of days before you make a new
release, announcing it on gnome-i18n. Depending on the number
of strings your program contains and how popular it is, the
translations will then start to tick in as
<filename>languagename.po</filename> files.</para>
<para>Note that most language teams only consist of 1-3 persons,
so if your program contains a lot of strings, it might last a
while before anyone has the time to look at it. Also, most
translators do not want to waste their time (translating is
a very time-consuming task) so if they do not assess your
project as being really serious (in the sense that it is
polished and being maintained) they may decide to spend their
time on some other project.</para>
</sect1>
</chapter>
<chapter id="chapter-customwidgets">
<title>Custom Widgets</title>
<para>>kmm; makes it very easy to derive new widgets by inheriting from an
existing widget class, either by deriving from a container and adding child
widgets, or by deriving from a single-item widget, and changing its behaviour.
But you might occasionally find that no suitable starting point already exists.
In this case, you can implement a widget from scratch.</para>
<sect1 id="sec-custom-containers">
<title>Custom Containers</title>
<para>When deriving from <classname>Gtk::Container</classname>, you should override the following virtual methods:
<itemizedlist>
<listitem><para><methodname>get_request_mode_vfunc()</methodname>: Return what <literal>Gtk::SizeRequestMode</literal> is preferred by the container.</para></listitem>
<listitem><para><methodname>get_preferred_width_vfunc()</methodname>: Calculate the minimum and natural width of the container.</para></listitem>
<listitem><para><methodname>get_preferred_height_vfunc()</methodname>: Calculate the minimum and natural height of the container.</para></listitem>
<listitem><para><methodname>get_preferred_width_for_height_vfunc()</methodname>: Calculate the minimum and natural width of the container, if it would be given the specified height.</para></listitem>
<listitem><para><methodname>get_preferred_height_for_width_vfunc()</methodname>: Calculate the minimum and natural height of the container, if it would be given the specified width.</para></listitem>
<listitem><para><methodname>on_size_allocate()</methodname>: Position the child widgets, given the height and width that the container has actually been given.</para></listitem>
<listitem><para><methodname>forall_vfunc()</methodname>: Call the same callback for each of the children.</para></listitem>
<listitem><para><methodname>on_add()</methodname>: Add a child widget to the container.</para></listitem>
<listitem><para><methodname>on_remove()</methodname>: Remove a child widget from the container.</para></listitem>
<listitem><para><methodname>child_type_vfunc()</methodname>: Return what type of child can be added.</para></listitem>
</itemizedlist>
</para>
<para>The <methodname>get_request_mode_vfunc()</methodname>,
<methodname>get_preferred_width_vfunc()</methodname>,
<methodname>get_preferred_height_vfunc()</methodname>,
<methodname>get_preferred_width_for_height_vfunc()</methodname>,
<methodname>get_preferred_height_for_width_vfunc()</methodname>, and
<methodname>on_size_allocate()</methodname> virtual methods control the
layout of the child widgets. For instance, if your container has 2
child widgets, with one below the other, your
<methodname>get_request_mode_vfunc()</methodname> might request
height-for-width layout. Then your
<methodname>get_preferred_width_vfunc()</methodname>
might report the maximum of the widths of the child widgets, and
<methodname>get_preferred_height_for_width_vfunc()</methodname>
might report the sum of their heights. If you want padding between
the child widgets then you would add that to the width and height too.
Your widget's container will use this result to ensure that your widget
gets enough space, and not less. By examining each widget's parent, and
its parent, this logic will eventually decide the size of the top-level
window.</para>
<para>You are not guaranteed to get the <literal>Gtk::SizeRequestMode</literal>
that you request. Therefore all four of the
<methodname>get_preferred_xxx_vfunc()</methodname> methods must return
sensible values.</para>
<para><methodname>on_size_allocate()</methodname> receives the actual
height and width that the parent container has decided to give to your
widget. This might be more than the minimum, or even more than the natural
size, for instance if the
top-level window has been expanded. You might choose to ignore the extra
space and leave a blank area, or you might choose to expand your child
widgets to fill the space, or you might choose to expand the padding
between your widgets. It's your container, so you decide. Don't forget to
call <methodname>set_allocation()</methodname> inside your
<methodname>on_size_allocate()</methodname> implementation to actually use the
allocated space that has been offered by the parent container.</para>
<para>Unless your container is a top-level window that derives from
<classname>Gtk::Window</classname>, you should probably also call
<methodname>Gtk::Widget::set_has_window(false)</methodname> in your
constructor. This means that your container does not create its own
<classname>Gdk::Window</classname>, but uses its parent's
window. (Note the difference between <classname>Gtk::Window</classname>
and <classname>Gdk::Window</classname>.) If your container does need
its own <classname>Gdk::Window</classname>, and does not derive from
<classname>Gtk::Window</classname>, you must also override the
<methodname>on_realize()</methodname> method as described in the
<link linkend="sec-custom-widgets">Custom Widgets</link> section.
And unless your container draws directly onto the underlying
<classname>Gdk::Window</classname>, you should probably call
<methodname>set_redraw_on_allocate(false)</methodname> to improve
performance.</para>
<para>By overriding <methodname>forall_vfunc()</methodname> you can allow
applications to operate on all of the container's child widgets. For
instance, <methodname>show_all_children()</methodname> uses this to find all
the child widgets and show them.</para>
<para>Although your container might have its own method to set the child
widgets, you should still provide an implementation for the virtual
<methodname>on_add()</methodname> and <methodname>on_remove()</methodname>
methods from the base class, so that the add() and remove() methods will
do something appropriate if they are called.</para>
<para>Your implementation of the <methodname>child_type_vfunc()</methodname>
method should report the type of widget that may be added to your
container, if it is not yet full. This is usually
<methodname>Gtk::Widget::get_type()</methodname> to indicate that the
container may contain any class derived from
<classname>Gtk::Widget</classname>. If the container may not contain any
more widgets, then this method should return
<literal>G_TYPE_NONE</literal>.</para>
<sect2 id="custom-container-example"><title>Example</title>
<para>This example implements a container with two child widgets, one above
the other. Of course, in this case it would be far simpler just to use
a vertical <classname>Gtk::Box</classname>.</para>
<figure id="figure-custom-container">
<title>Custom Container</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;custom_container.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;custom/custom_container/">Source Code</ulink></para>
</sect2>
</sect1>
<sect1 id="sec-custom-widgets">
<title>Custom Widgets</title>
<para>By deriving directly from <classname>Gtk::Widget</classname> you can
do all the drawing for your widget directly, instead of just arranging
child widgets. For instance, a <classname>Gtk::Label</classname> draws
the text of the label, but does not do this by using other
widgets.</para>
<para>When deriving from <classname>Gtk::Widget</classname>, you should
override the following virtual methods. The methods marked (optional)
need not be overridden in all custom widgets. The base class's methods
may be appropriate.
<itemizedlist>
<listitem><para><methodname>get_request_mode_vfunc()</methodname>: (optional) Return what <literal>Gtk::SizeRequestMode</literal> is preferred by the widget.</para></listitem>
<listitem><para><methodname>get_preferred_width_vfunc()</methodname>: Calculate the minimum and natural width of the widget.</para></listitem>
<listitem><para><methodname>get_preferred_height_vfunc()</methodname>: Calculate the minimum and natural height of the widget.</para></listitem>
<listitem><para><methodname>get_preferred_width_for_height_vfunc()</methodname>: Calculate the minimum and natural width of the widget, if it would be given the specified height.</para></listitem>
<listitem><para><methodname>get_preferred_height_for_width_vfunc()</methodname>: Calculate the minimum and natural height of the widget, if it would be given the specified width.</para></listitem>
<listitem><para><methodname>on_size_allocate()</methodname>: Position the widget, given the height and width that it has actually been given.</para></listitem>
<listitem><para><methodname>on_realize()</methodname>: Associate a <classname>Gdk::Window</classname> with the widget.</para></listitem>
<listitem><para><methodname>on_unrealize()</methodname>: (optional) Break the association with the <classname>Gdk::Window</classname>. </para></listitem>
<listitem><para><methodname>on_map()</methodname>: (optional)</para></listitem>
<listitem><para><methodname>on_unmap()</methodname>: (optional)</para></listitem>
<listitem><para><methodname>on_draw()</methodname>: Draw on the supplied <classname>Cairo::Context</classname>.</para></listitem>
</itemizedlist>
</para>
<para>The first 6 methods in the previous table are also overridden in custom
containers. They are briefly described in the
<link linkend="sec-custom-containers">Custom Containers</link> section.
</para>
<para>Most custom widgets need their own <classname>Gdk::Window</classname>
to draw on. Then you can call
<methodname>Gtk::Widget::set_has_window(true)</methodname> in your
constructor. (This is the default value.) If you do not call
<methodname>set_has_window(false)</methodname>, you must override
<methodname>on_realize()</methodname> and call
<methodname>Gtk::Widget::set_realized()</methodname> and
<methodname>Gtk::Widget::set_window()</methodname> from there.</para>
<sect2 id="custom-widget-example"><title>Example</title>
<para>This example implements a widget which draws a Penrose triangle.</para>
<figure id="figure-custom-widget">
<title>Custom Widget</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;custom_widget.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;custom/custom_widget/">Source Code</ulink></para>
</sect2>
</sect1>
</chapter>
<chapter id="chapter-multi-threaded-programs">
<title>Multi-threaded programs</title>
<sect1 id="sec-the-constraints">
<title>The constraints</title>
<para>
<application>glibmm</application> provides the normal set of thread
launching functions, mutexes, condition variables and scoped locking
classes required for writing multi-threaded programs using C++.
</para>
<para>
However, care is required when writing programs based on >kmm; using
multiple threads of execution, arising from the fact that
<application>libsigc++</application>, and in particular
<classname>sigc::trackable</classname>, are not thread-safe. That's
because none of the complex interactions that occur behind the scenes
when using <application>libsigc++</application> are protected by a
mutex or other means of synchronization.
<footnote>
<para>
These interactions arise from the fact that, amongst other things, a
class inheriting from <classname>sigc::trackable</classname> will, via
that inheritance, have a <classname>std::list</classname> object
keeping track of slots created by calls to
<function>sigc::mem_fun()</function> representing any of its
non-static methods (more particularly it keeps a list of callbacks
which will null the connected slots on its destruction). Each
<classname>sigc::slot</classname> object also keeps, via
<classname>sigc::slot_rep</classname>, its own
<classname>sigc::trackable</classname> object to track any
<classname>sigc::connection</classname> objects which it needs to
inform about its demise, and also has a function to deregister itself
from any <classname>sigc::trackable</classname> on disconnection or
destruction. <classname>sigc::signal</classname> objects also keep
lists of slots, which will be updated by a call to their
<methodname>connect()</methodname> method or calls to any
<classname>sigc::connection</classname> object relating to such a
connection.
</para>
</footnote>
</para>
<sect2 id="the-rules">
<title>The rules</title>
<para>
This requires a number of rules to be observed when writing
multi-threaded programs using >kmm;. These are set out below, but
one point to note is that extra care is required when deriving classes
from <classname>sigc::trackable</classname>, because the effects are
unintuitive (see particularly points 4 and 5 below).
</para>
<orderedlist>
<listitem>
<para>
Use <classname>Glib::Dispatcher</classname> to invoke >kmm; functions
from worker threads (this is dealt with in more detail in the next
section).
</para>
</listitem>
<listitem>
<para>
A <classname>sigc::signal</classname> object should be regarded as
owned by the thread which created it. Only that thread should connect
a <classname>sigc::slot</classname> object to the signal object, and
only that thread should <methodname>emit()</methodname> or call
<methodname>operator()()</methodname> on the signal, or null any
connected <classname>sigc::slot</classname> object. It follows
(amongst other things) that any signal object provided by a >kmm;
widget should only be operated on in the main GUI thread and any
object deriving from <classname>sigc::trackable</classname> having its
non-static methods referenced by slots connected to the signal object
should only be destroyed in that thread.
</para>
</listitem>
<listitem>
<para>
Any <classname>sigc::connection</classname> object should be regarded
as owned by the thread in which the method returning the
<classname>sigc::connection</classname> object was called. Only that
thread should call <classname>sigc::connection</classname> methods on
the object.
</para>
</listitem>
<listitem>
<para>
A <classname>sigc::slot</classname> object created by a call to
<function>sigc::mem_fun()</function> which references a method of a
class deriving from <classname>sigc::trackable</classname> should
never be copied to another thread, nor destroyed by a different thread
than the one which created it. (One consequence of this is that
<methodname>Glib::Threads::Thread::create()</methodname> should not be
called with a slot argument created by a call to
<function>sigc::mem_fun()</function> which represents a method of such
a class. It is however safe to pass
<methodname>Glib::Threads::Thread::create()</methodname> a function
object representing such a method by using, say,
<function>boost::bind()</function> or, in C++11,
<function>std::bind()</function> or a C++11 lambda expression.)
</para>
</listitem>
<listitem>
<para>
If a particular class object derives from
<classname>sigc::trackable</classname>, only one thread should create
<classname>sigc::slot</classname> objects representing any of the
class's non-static methods by calling
<function>sigc::mem_fun()</function>. The first thread to create such
a slot should be regarded as owning the relevant object for the
purpose of creating further slots referencing <emphasis>any</emphasis>
of its non-static methods using that function, or nulling those slots
by disconnecting them or destroying the trackable object.
</para>
</listitem>
<listitem>
<para>
Although <application>glib</application> is itself thread-safe, any
<application>glibmm</application> wrappers which use
<application>libsigc++</application> will not be. So for example, only
the thread in which a main loop runs should call
<methodname>Glib::SignalIdle::connect()</methodname>,
<methodname>Glib::SignalIO::connect()</methodname>,
<methodname>Glib::SignalTimeout::connect()</methodname>,
<methodname>Glib::SignalTimeout::connect_seconds</methodname>
for that main loop, or manipulate any
<classname>sigc::connection</classname> object returned by them.
</para>
<para>
The connect*_once() variants,
<methodname>Glib::SignalIdle::connect_once()</methodname>,
<methodname>Glib::SignalTimeout::connect_once()</methodname>,
<methodname>Glib::SignalTimeout::connect_seconds_once()</methodname>,
are thread-safe for any case where the slot is not created by a call to
<function>sigc::mem_fun()</function> which represents a method of a class
deriving from <classname>sigc::trackable</classname>. This is similar to
<methodname>Glib::Threads::Thread::create()</methodname> as mentioned in point 4.
</para>
</listitem>
</orderedlist>
</sect2>
</sect1>
<sect1 id="sec-using-glib-dispatcher">
<title>Using Glib::Dispatcher</title>
<para>
The slots connected to <classname>sigc::signal</classname> objects
execute in the thread which calls <methodname>emit()</methodname> or
<methodname>operator()()</methodname> on the signal.
<classname>Glib::Dispatcher</classname> does not behave this way:
instead its connected slots execute in the thread in which the
<classname>Glib::Dispatcher</classname> object was constructed (which
must have a glib main loop). If a
<classname>Glib::Dispatcher</classname> object is constructed in the
main GUI thread (which will therefore be the receiver thread), any
worker thread can emit on it and have the connected slots safely
execute >kmm; functions.
</para>
<para>
Some thread safety rules on the use of
<classname>Glib::Dispatcher</classname> still apply. As mentioned, a
<classname>Glib::Dispatcher</classname> object must be constructed in
the receiver thread (the thread in whose main loop it will execute its
connected slots). By default this is the main program thread, although
there is a <classname>Glib::Dispatcher</classname> constructor which
can take the <classname>Glib::MainContext</classname> object of any
thread which has a main loop. Only the receiver thread should call
<methodname>connect()</methodname> on the
<classname>Glib::Dispatcher</classname> object, or manipulate any
related <classname>sigc::connection</classname> object, unless
additional synchronization is employed. However, any worker thread can
safely emit on the <classname>Glib::Dispatcher</classname> object
without any locking once the receiver thread has connected the slots,
provided that it is constructed before the worker thread is started
(if it is constructed after the thread has started, additional
synchronization will normally be required to ensure visibility).
</para>
<para>
Aside from the fact that connected slots always execute in the
receiver thread, <classname>Glib::Dispatcher</classname> objects are
similar to <classname>sigc::signal<void></classname> objects.
They therefore cannot pass unbound arguments nor return a value. The
best way to pass unbound arguments is with a thread-safe
(asynchronous) queue. At the time of writing
<application>glibmm</application> does not have one, although most
people writing multi-threaded code will have one available to them
(they are relatively easy to write although there are subtleties in
combining thread safety with strong exception safety).
</para>
<para>
A <classname>Glib::Dispatcher</classname> object can be emitted on by
the receiver thread as well as by a worker thread, although this
should be done within reasonable bounds. On unix-like systems
<classname>Glib::Dispatcher</classname> objects share a single common
pipe, which could in theory at least fill up on a very heavily loaded
system running a program with a very large number of
<classname>Dispatcher</classname> objects in use. Were the pipe to
fill up before the receiver thread's main loop has had an opportunity
to read from it to empty it, and the receiver thread attempt to emit
and so write to it when it is in that condition, the receiver thread
would block on the write, so deadlocking. Where the receiver thread is
to emit, a normal <classname>sigc::signal<void></classname>
object could of course be used instead.
</para>
</sect1>
<sect1 id="sec-multithread-example">
<title>Example</title>
<para>
This is an example program with two threads, one GUI thread, like in all
>kmm; programs, and one worker thread. The worker thread is created when you
press the <literal>Start work</literal> button. It is deleted when the work is
finished, when you press the <literal>Stop work</literal> button, or when you
press the <literal>Quit</literal> button.
</para>
<para>
A <classname>Glib::Dispatcher</classname> is used for sending notifications
from the worker thread to the GUI thread. The <classname>ExampleWorker</classname>
class contains data which is accessed by both threads. This data is protected
by a <classname>Glib::Threads::Mutex</classname>.
Only the GUI thread updates the GUI.
</para>
<figure id="figure-multithread">
<title>Multi-Threaded Program</title>
<screenshot>
<graphic format="PNG" fileref="&url_figures_base;multithread.png"/>
</screenshot>
</figure>
<para><ulink url="&url_examples_base;multithread">Source Code</ulink></para>
</sect1>
</chapter>
<chapter id="chapter-recommended-techniques">
<title>Recommended Techniques</title>
<para>This section is simply a gathering of wisdom, general style guidelines
and hints for creating >kmm; applications.
</para>
<para>Use GNU <application>autoconf</application> and
<application>automake</application>! They are your friends :)
<application>Automake</application> examines C files, determines how they
depend on each other, and generates a <filename>Makefile</filename> so the
files can be compiled in the correct order.
<application>Autoconf</application> permits automatic configuration of
software installation, handling a large number of system quirks to increase
portability.
</para>
<para>Subclass Widgets to better organize your code. You should probably
subclass your main <classname>Window</classname> at least. Then you can
make your child Widgets and signal handlers members of that class.
</para>
<para>Create your own signals instead of passing pointers around. Objects can
communicate with each other via signals and signal handlers. This is much
simpler than objects holding pointers to each other and calling each
other's methods. >kmm;'s classes uses special versions of
<classname>sigc::signal</classname>, but you should use normal
<classname>sigc::signal</classname>s, as described in the
<application>libsigc++</application> documentation.</para>
<sect1 id="sec-application-lifetime">
<title>Application Lifetime</title>
<para>Most applications will have only one <classname>Window</classname>, or
only one main window. These applications can use the
<methodname>Gtk::Application::run(Gtk::Window&)</methodname> overload. It shows
the window and returns when the window has been hidden. This might happen
when the user closes the window, or when your code decides to
<methodname>hide()</methodname> the window. You can prevent the user from
closing the window (for instance, if there are unsaved changes) by
overriding <methodname>Gtk::Window::on_delete_event()</methodname>.</para>
<para>Most of our examples use this technique.</para>
</sect1>
<sect1 id="sec-using-a-gtkmm-widget">
<title>Using a >kmm; widget</title>
<para>
Our examples all tend to have the same structure. They follow these steps
for using a <classname>Widget</classname>:
</para>
<para>
<orderedlist>
<listitem>
<para>
Declare a variable of the type of <classname>Widget</classname> you wish to
use, generally as member variable of a derived container class. You could also
declare a pointer to the widget type, and then create it with
<literal>new</literal> in your code. Even when using the widget via a pointer,
it's still probably best to make that pointer a member variable of a container
class so that you can access it later.
</para>
</listitem>
<listitem>
<para>
Set the attributes of the widget. If the widget has no default constructor, then you will need to initialize the widget in the initalizer list of your container class's constructor.
</para>
</listitem>
<listitem>
<para>
Connect any signals you wish to use to the appropriate handlers.
</para>
</listitem>
<listitem>
<para>
Pack the widget into a container using the appropriate call,
e.g. <methodname>Gtk::Container::add()</methodname> or
<methodname>pack_start()</methodname>.
</para>
</listitem>
<listitem>
<para>
Call <methodname>show()</methodname> to display the widget.
</para>
</listitem>
</orderedlist>
</para>
<para>
<methodname>Gtk::Widget::show()</methodname> lets >kmm; know that we have
finished setting the attributes of the widget, and that it is ready to be
displayed. You can use <methodname>Gtk::Widget::hide()</methodname> to make it
disappear again. The order in which you show the widgets is not important, but
we do suggest that you show the top-level window last; this way, the whole
window will appear with its contents already drawn. Otherwise, the user will
first see a blank window, into which the widgets will be gradually drawn.
</para>
</sect1>
</chapter>
<chapter id="chapter-contributing">
<title>Contributing</title>
<para>
This document, like so much other great software out there, was
created for free by volunteers. If you are at all knowledgeable about
any aspect of >kmm; that does not already have documentation, please
consider contributing to this document.
</para>
<para>
Ideally, we would like you to <ulink url="http://www.gtkmm.org/bugs.shtml">provide a patch</ulink> to the
<filename>docs/tutorial/C/gtkmm-tutorial-in.xml</filename> file. This file is currently
in the <literal>gtkmm-documentation</literal> module in GNOME git.
</para>
<para>
If you do decide to contribute, please post your contribution to the
>kmm; mailing list at <ulink url="mailto:gtkmm-list@gnome.org"><gtkmm-list@gnome.org></ulink>. Also, be aware that
the entirety of this document is free, and any addition you provide
must also be free. That is, people must be able to use any portion of
your examples in their programs, and copies of this document
(including your contribution) may be distributed freely.
</para>
</chapter>
<appendix id="chapter-refptr">
<title>The RefPtr smartpointer</title>
<para>
<classname>Glib::RefPtr</classname> is a smartpointer. Specifically, it is a
reference-counting smartpointer. You might be familiar with
<classname>std::auto_ptr<></classname>, <classname>std::unique_ptr<></classname>
and <classname>std::shared_ptr<></classname>, which are also smartpointers.
<classname>Glib::RefPtr<></classname> is similar to <classname>std::shared_ptr<></classname>,
which is also reference-counting. <classname>Glib::RefPtr<></classname> was introduced
long before there was a reference-counting smartpointer in the C++ Standard Library.
</para>
<para><ulink url="&url_refdocs_base_glib;RefPtr.html">Reference</ulink></para>
<para>A smartpointer acts much like a normal pointer. Here are a few examples.</para>
<sect1 id="sec-refptr-copying">
<title>Copying</title>
<para>
You can copy <classname>RefPtr</classname>s, just like normal pointers. But
unlike normal pointers, you don't need to worry about deleting the underlying
instance.
</para>
<para>
<programlisting>
Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
Glib::RefPtr<Gdk::Pixbuf> refPixbuf2 = refPixbuf;
</programlisting>
</para>
<para>
Of course this means that you can store <classname>RefPtr</classname>s in
standard containers, such as <classname>std::vector</classname> or
<classname>std::list</classname>.</para>
<para>
<programlisting>
std::list< Glib::RefPtr<Gdk::Pixbuf> > listPixbufs;
Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
listPixbufs.push_back(refPixbuf);
</programlisting>
</para>
</sect1>
<sect1 id="sec-refptr-dereferencing"><title>Dereferencing</title>
<para>You can dereference a smartpointer with the -> operator, to
call the methods of the underlying instance, just like a normal pointer.
</para>
<para>
<programlisting>
Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
int width = refPixbuf->get_width();
</programlisting>
</para>
<para>But unlike most smartpointers, you can't use the * operator to
access the underlying instance.
</para>
<para>
<programlisting>
Glib::RefPtr<Gdk::Pixbuf> refPixbuf = Gdk::Pixbuf::create_from_file(filename);
Gdk::Pixbuf& underlying = *refPixbuf; //Syntax error - will not compile.
</programlisting>
</para>
</sect1>
<sect1 id="sec-refptr-casting"><title>Casting</title>
<para>
You can cast <classname>RefPtr</classname>s to base types, just like normal
pointers.
</para>
<para>
<programlisting>
Glib::RefPtr<Gtk::TreeStore> refStore = Gtk::TreeStore::create(columns);
Glib::RefPtr<Gtk::TreeModel> refModel = refStore;
</programlisting>
</para>
<para>This means that any method which takes a <type>const
Glib::RefPtr<BaseType></type> argument can also take a
<type>const Glib::RefPtr<DerivedType></type>. The cast is
implicit, just as it would be for a normal pointer.</para>
<para>You can also cast to a derived type, but the syntax is
a little different than with a normal pointer.
</para>
<para>
<programlisting>
Glib::RefPtr<Gtk::TreeStore> refStore =
Glib::RefPtr<Gtk::TreeStore>::cast_dynamic(refModel);
Glib::RefPtr<Gtk::TreeStore> refStore2 =
Glib::RefPtr<Gtk::TreeStore>::cast_static(refModel);
</programlisting>
</para>
</sect1>
<sect1 id="sec-refptr-checking-for-null"><title>Checking for null</title>
<para>
Just like normal pointers, you can check whether a
<classname>RefPtr</classname> points to anything.
</para>
<para>
<programlisting>
Glib::RefPtr<Gtk::TreeModel> refModel = m_TreeView.get_model();
if(refModel)
{
int cols_count = refModel->get_n_columns();
...
}
</programlisting>
</para>
<para>
But unlike normal pointers, <classname>RefPtr</classname>s are automatically
initialized to null so you don't need to remember to do that yourself.
</para>
</sect1>
<sect1 id="sec-refptr-constness"><title>Constness</title>
<para>
The use of the <literal>const</literal> keyword in C++ is not always clear. You
might not realise that <type>const Something*</type> declares a pointer to a
<type>const Something</type>. The pointer can be changed, but not the
<type>Something</type> that it points to.
</para>
<para>
Therefore, the <classname>RefPtr</classname> equivalent of
<type>Something*</type> for a method parameter is <type>const
Glib::RefPtr<Something>&</type>, and the equivalent of
<type>const Something*</type> is <type>const Glib::RefPtr<const
Something>&</type>.
</para>
<para>
The <literal>const ... &</literal> around both is just for efficiency, like
using <classname>const std::string&</classname> instead of
<classname>std::string</classname> for a method parameter to avoid unnecessary
copying.
</para>
</sect1>
</appendix>
<appendix id="chapter-signals">
<title>Signals</title>
<sect1 id="sec-connecting-signal-handlers">
<title>Connecting signal handlers</title>
<para>
>kmm; widget classes have signal accessor methods, such as
<methodname>Gtk::Button::signal_clicked()</methodname>, which allow you to connect
your signal handler. Thanks to the flexibility of
<application>libsigc++</application>, the callback library used by >kmm;, the
signal handler can be almost any kind of function, but you will probably want
to use a class method. Among <application>GTK+</application> C coders, these
signal handlers are often named callbacks.
</para>
<para>
Here's an example of a signal handler being connected to a signal:
</para>
<para>
<programlisting>
#include <gtkmm/button.h>
void on_button_clicked()
{
std::cout << "Hello World" << std::endl;
}
main()
{
Gtk::Button button("Hello World");
button.signal_clicked().connect(sigc::ptr_fun(&on_button_clicked));
}
</programlisting>
</para>
<para>
There's rather a lot to think about in this (non-functional) code.
First let's identify the parties involved:
</para>
<itemizedlist>
<listitem>
<para>
The signal handler is <methodname>on_button_clicked()</methodname>.
</para>
</listitem>
<listitem>
<para>
We're hooking it up to the <classname>Gtk::Button</classname> object called
<varname>button</varname>.
</para>
</listitem>
<listitem>
<para>
When the Button emits its <literal>clicked</literal> signal,
<methodname>on_button_clicked()</methodname> will be called.
</para>
</listitem>
</itemizedlist>
<para>
Now let's look at the connection again:
</para>
<para>
<programlisting>
...
button.signal_clicked().connect(sigc::ptr_fun(&on_button_clicked));
...
</programlisting>
</para>
<para>
Note that we don't pass a pointer to <methodname>on_button_clicked()</methodname>
directly to the signal's <methodname>connect()</methodname> method. Instead, we
call <function>sigc::ptr_fun()</function>, and pass the result to
<methodname>connect()</methodname>.
</para>
<para>
<function>sigc::ptr_fun()</function> generates a <classname>sigc::slot</classname>.
A slot is an object which
looks and feels like a function, but is actually an object. These are also
known as function objects, or functors.
<function>sigc::ptr_fun()</function> generates a slot for a standalone function or static method.
<function>sigc::mem_fun()</function> generates a slot for a member method of a particular instance.
</para>
<para>
Here's a slightly larger example of slots in action:
</para>
<para>
<programlisting>
void on_button_clicked();
class some_class
{
void on_button_clicked();
};
some_class some_object;
main()
{
Gtk::Button button;
button.signal_clicked().connect( sigc::ptr_fun(&on_button_clicked) );
button.signal_clicked().connect( sigc::mem_fun(some_object, &some_class::on_button_clicked) );
}
</programlisting>
</para>
<para>
The first call to <methodname>connect()</methodname> is just like the one we saw
last time; nothing new here.</para>
<para>The next is more interesting.
<function>sigc::mem_fun()</function> is called with two arguments. The first
argument is <parameter>some_object</parameter>, which is the object that our
new slot will be pointing at. The second argument is a pointer to one of its
methods. This particular version of <function>sigc::mem_fun()</function>
creates a slot which will, when "called", call the pointed-to method of the
specified object, in this case
<methodname>some_object.on_button_clicked()</methodname>.
</para>
<para>
Another thing to note about this example is that we made the call to
<methodname>connect()</methodname> twice for the same signal object. This is
perfectly fine - when the button is clicked, both signal handlers will be
called.
</para>
<para>
We just told you that the button's <literal>clicked</literal> signal is expecting
to call a method with no arguments. All signals have
requirements like this - you can't hook a function with two arguments
to a signal expecting none (unless you use an adapter, such as
<function>sigc::bind()</function>, of course). Therefore, it's important to
know what type of signal handler you'll be expected to connect to a given
signal.
</para>
</sect1>
<sect1 id="sec-writing-signal-handlers">
<title>Writing signal handlers</title>
<para>
To find out what type of signal handler you can connect to a signal, you can
look it up in the reference documentation or the header file. Here's an example of a signal declaration you
might see in the >kmm; headers:
</para>
<para>
<programlisting>
Glib::SignalProxy1<bool, Gtk::DirectionType> signal_focus()
</programlisting>
</para>
<para>
Other than the signal's name (<literal>focus</literal>), two things are
important to note here: the number following the word
<classname>SignalProxy</classname> at the beginning (1, in this case), and the
types in the list (<type>bool</type> and <type>Gtk::DirectionType</type>). The
number indicates how many arguments the signal handler should have; the first
type, <type>bool</type>, is the type that the signal handler should return; and
the next type, <type>Gtk::DirectionType</type>, is the type of this signal's
first, and only, argument. By looking at the reference documentation, you can
see the names of the arguments too.
</para>
<para>
The same principles apply for signals which have more arguments. Here's one
with three (taken from <filename><gtkmm/textbuffer.h></filename>):
</para>
<para>
<programlisting>
Glib::SignalProxy3<void, const TextBuffer::iterator&, const Glib::ustrin&, int> signal_insert();
</programlisting>
</para>
<para>
It follows the same form. The number 3 at the end of the type's name indicates
that our signal handler will need three arguments. The first type in the type
list is <type>void</type>, so that should be our signal handler's return type.
The following three types are the argument types, in order. Our signal
handler's prototype could look like this:
</para>
<para>
<programlisting>
void on_insert(const TextBuffer::iterator& pos, const Glib::ustring& text, int bytes)
</programlisting>
</para>
</sect1>
<sect1 id="sec-disconnecting-signal-handlers">
<title>Disconnecting signal handlers</title>
<para>
Let's take another look at a Signal's <literal>connect</literal> method:
</para>
<para>
<programlisting>
sigc::signal<void,int>::iterator signal<void,int>::connect( const sigc::slot<void,int>& );
</programlisting>
</para>
<para>
Notice that the return value is of type
<classname>sigc::signal<void,int>::iterator</classname>. This can be
implicitly converted into a <classname>sigc::connection</classname> which in
turn can be used to control the connection. By keeping a connection object you
can disconnect its associated signal handler using the method
<methodname>sigc::connection::disconnect()</methodname>.
</para>
</sect1>
<sect1 id="sec-overriding-default-signal-handlers">
<title>Overriding default signal handlers</title>
<para>
So far we've told you to perform actions in
response to button-presses and the like by handling signals.
That's certainly a good way to do things, but it's not the only
way.
</para>
<para>
Instead of laboriously connecting signal handlers to signals,
you can simply make a new class which inherits from a widget - say, a
Button - and then override the default signal handler, such as Button::on_clicked(). This can be a
lot simpler than hooking up signal handlers for everything.
</para>
<para>
Subclassing isn't always the best way to accomplish
things. It is only useful when you want the widget to handle its own signal by itself. If you want some other class to handle the signal then you'll need to connect a separate handler. This is even more true if you want several objects to handle the same signal, or if you want one signal handler to respond to the same signal from different objects.
</para>
<para>
>kmm; classes are designed with overriding in mind; they contain
virtual member methods specifically intended to be overridden.
</para>
<para>
Let's look at an example of overriding:
</para>
<para>
<programlisting>
#include <gtkmm/button.h>
class OverriddenButton : public Gtk::Button
{
protected:
virtual void on_clicked();
}
void OverriddenButton::on_clicked()
{
std::cout << "Hello World" << std::endl;
// call the base class's version of the method:
Gtk::Button::on_clicked();
}
</programlisting>
</para>
<para>
Here we define a new class called <classname>OverriddenButton</classname>,
which inherits from <classname>Gtk::Button</classname>. The only thing we
change is the <methodname>on_clicked()</methodname> method, which is called
whenever <classname>Gtk::Button</classname> emits the
<literal>clicked</literal> signal. This method prints "Hello World" to
<literal>stdout</literal>, and then calls the original, overridden method, to
let <classname>Gtk::Button</classname> do what it would have done had we not
overridden.
</para>
<para>
You don't always need to call the parent's method; there are times
when you might not want to. Note that we called the parent method
<emphasis>after</emphasis> writing "Hello World", but we could have called it before.
In this simple example, it hardly matters much, but there are times
when it will. With signals, it's not quite so easy to change details
like this, and you can do something here which you can't do at all
with connected signal handlers: you can call the parent method in the <emphasis>middle</emphasis> of
your custom code.
</para>
</sect1>
<sect1 id="sec-binding-extra-arguments">
<title>Binding extra arguments</title>
<para>
If you use one signal handler to catch the same signal from several widgets,
you might like that signal handler to receive some extra information. For
instance, you might want to know which button was clicked. You can do this with
<function>sigc::bind()</function>. Here's some code from the <link
linkend="sec-helloworld2">helloworld2</link> example.
<programlisting>
m_button1.signal_clicked().connect( sigc::bind<Glib::ustring>( sigc::mem_fun(*this, &HelloWorld::on_button_clicked), "button 1") );
</programlisting>
This says that we want the signal to send an extra
<classname>Glib::ustring</classname> argument to the signal handler, and that
the value of that argument should be "button 1". Of course we will need to add
that extra argument to the declaration of our signal handler:
<programlisting>
virtual void on_button_clicked(Glib::ustring data);
</programlisting>
Of course, a normal "clicked" signal handler would have no arguments.
</para>
<para>
<function>sigc::bind()</function> is not commonly used, but you might find it
helpful sometimes. If you are familiar with <application>GTK+</application>
programming then you have probably noticed that this is similar to the extra
<literal>gpointer data</literal> arguments which all GTK+ callbacks have. This
is generally overused in <application>GTK+</application> to pass information
that should be stored as member data in a derived widget, but widget derivation
is very difficult in C. We have far less need of this hack in >kmm;.
</para>
</sect1>
<sect1 id="sec-xeventsignals">
<title>X Event signals</title>
<para>
The <classname>Widget</classname> class has some special signals which
correspond to the underlying X-Windows events. These are suffixed by
<literal>_event</literal>; for instance,
<methodname>Widget::signal_button_press_event()</methodname>.
</para>
<para>
You might occasionally find it useful to handle X events when there's something
you can't accomplish with normal signals. <classname>Gtk::Button</classname>,
for example, does not send mouse-pointer coordinates with its
<literal>clicked</literal> signal, but you could handle
<literal>button_press_event</literal> if you needed this
information. X events are also often used to handle key-presses.
</para>
<para>
These signals behave slightly differently. The value returned from the signal handler indicates whether it has fully "handled"
the event. If the value is <literal>false</literal> then >kmm; will pass the event on to the next signal handler. If the value is <literal>true</literal> then no other signal handlers will need to be called.
</para>
<para>
Handling an X event doesn't affect the Widget's other signals. If you handle
<literal>button_press_event</literal> for
<classname>Gtk::Button</classname>, you'll still be able to get the
<literal>clicked</literal> signal. They are emitted at (nearly) the same time.
</para>
<para>Note also that not all widgets receive all X events by default. To receive additional
X events, you can use <methodname>Gtk::Widget::set_events()</methodname> before showing the
widget, or <methodname>Gtk::Widget::add_events()</methodname> after showing the widget. However,
some widgets must first be placed inside an <classname>EventBox</classname> widget. See
the <link linkend="chapter-widgets-without-xwindows">Widgets Without X-Windows</link> chapter.
</para>
<para>
Here's a simple example:
<programlisting>
bool on_button_press(GdkEventButton* event);
Gtk::Button button("label");
button.signal_button_press_event().connect( sigc::ptr_fun(&on_button_press) );
</programlisting>
</para>
<para>
When the mouse is over the button and a mouse button is pressed,
<methodname>on_button_press()</methodname> will be called.
</para>
<para>
<type>GdkEventButton</type> is a structure containing the event's parameters,
such as the coordinates of the mouse pointer at the time the button was
pressed. There are several different types of <type>GdkEvent</type> structures
for the various events.
</para>
<sect2 id="signal-handler-sequence">
<title>Signal Handler sequence</title>
<para>By default, your signal handlers are called after any previously-connected
signal handlers. However, this can be a problem with the X Event signals. For instance,
the existing signal handlers, or the default signal handler, might return <literal>true</literal>
to stop other signal handlers from being called. To specify that your signal handler
should be called before the other signal handlers, so that it will always be called,
you can specify <literal>false</literal> for the optional <literal>after</literal>
parameter. For instance,
<programlisting>
button.signal_button_press_event().connect( sigc::ptr_fun(&on_mywindow_button_press), false );
</programlisting>
</para>
<para>The event is delivered first to the widget the event occurred in. If all
signal handlers in that widget return <literal>false</literal> (indicating that
the event has not been handled), then the signal will be propagated to the parent
widget and emitted there. This continues all the way up to the top-level widget
if no one handles the event.
</para>
</sect2>
</sect1>
<sect1 id="sec-exceptions-in-signal-handlers">
<title>Exceptions in signal handlers</title>
<para>
When a program is aborted because of an unhandled C++ exception, it's sometimes
possible to use a debugger to find the location where the exception was thrown.
This is more difficult than usual if the exception was thrown from a signal handler.
</para>
<para>
This section describes primarily what you can expect on a Linux system, when you
use <ulink url="http://www.gnu.org/software/gdb/">the gdb debugger</ulink>.
</para>
<para>
First, let's look at a simple example where an exception is thrown from a normal
function (no signal handler).
<programlisting>
// without_signal.cc
#include <gtkmm.h>
bool throwSomething()
{
throw "Something";
return true;
}
int main(int argc, char** argv)
{
throwSomething();
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create(argc, argv, "org.gtkmm.without_signal");
return app->run();
}
</programlisting>
</para>
<para>
Here is an excerpt from a <application>gdb</application> session. Only the most
interesting parts of the output are shown.
<programlisting>
> gdb without_signal
(gdb) run
terminate called after throwing an instance of 'char const*'
Program received signal SIGABRT, Aborted.
(gdb) backtrace
#7 0x08048864 in throwSomething () at without_signal.cc:6
#8 0x0804887d in main (argc=1, argv=0xbfffecd4) at without_signal.cc:12
</programlisting>
You can see that the exception was thrown from <filename>without_signal.cc</filename>,
line 6 (<code>throw "Something";</code>).
</para>
<para>
Now let's see what happens when an exception is thrown from a signal handler.
Here's the source code.
<programlisting>
// with_signal.cc
#include <gtkmm.h>
bool throwSomething()
{
throw "Something";
return true;
}
int main(int argc, char** argv)
{
Glib::signal_timeout().connect(sigc::ptr_fun(throwSomething), 500);
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create(argc, argv, "org.gtkmm.with_signal");
app->hold();
return app->run();
}
</programlisting>
</para>
<para>
And here's an excerpt from a <application>gdb</application> session.
<programlisting>
> gdb with_signal
(gdb) run
(with_signal:2703): glibmm-ERROR **:
unhandled exception (type unknown) in signal handler
Program received signal SIGTRAP, Trace/breakpoint trap.
(gdb) backtrace
#2 0x0063c6ab in glibmm_unexpected_exception () at exceptionhandler.cc:77
#3 Glib::exception_handlers_invoke () at exceptionhandler.cc:150
#4 0x0063d370 in glibmm_source_callback (data=0x804d620) at main.cc:212
#13 0x002e1b31 in Gtk::Application::run (this=0x804f300) at application.cc:178
#14 0x08048ccc in main (argc=1, argv=0xbfffecd4) at with_signal.cc:16
</programlisting>
The exception is caught in <application>glibmm</application>, and the program
ends with a call to <function>g_error()</function>. Other exceptions may result
in different behaviour, but in any case the exception from a signal handler is
caught in <application>glibmm</application> or >kmm;, and
<application>gdb</application> can't see where it was thrown.
</para>
<para>
To see where the exception is thrown, you can use the <application>gdb</application>
command <userinput>catch throw</userinput>.
<programlisting>
> gdb with_signal
(gdb) catch throw
Catchpoint 1 (throw)
(gdb) run
Catchpoint 1 (exception thrown), 0x00714ff0 in __cxa_throw ()
(gdb) backtrace
#0 0x00714ff0 in __cxa_throw () from /usr/lib/i386-linux-gnu/libstdc++.so.6
#1 0x08048bd4 in throwSomething () at with_signal.cc:6
(gdb) continue
Continuing.
(with_signal:2375): glibmm-ERROR **
unhandled exception (type unknown) in signal handler
Program received signal SIGTRAP, Trace/breakpoint trap.
</programlisting>
</para>
<para>
If there are many caught exceptions before the interesting uncaught one, this
method can be tedious. It can be automated with the following
<application>gdb</application> commands.
<programlisting>
(gdb) catch throw
(gdb) commands
(gdb) backtrace
(gdb) continue
(gdb) end
(gdb) set pagination off
(gdb) run
</programlisting>
These commands will print a backtrace from each <code>throw</code> and continue.
The backtrace from the last (or possibly the last but one) <code>throw</code>
before the program stops, is the interesting one.
</para>
</sect1>
</appendix>
<appendix id="chapter-custom-signals">
<title>Creating your own signals</title>
<para>
Now that you've seen signals and signal handlers in >kmm;, you
might like to use the same technique to allow interaction between your
own classes. That's actually very simple by using the
<application>libsigc++</application> library directly.
</para>
<para>
This isn't purely a >kmm; or GUI issue. >kmm; uses
<application>libsigc++</application> to implement its proxy wrappers for the
<application>GTK+</application> signal system, but for new,
non-GTK+ signals, you can create pure C++ signals, using the
<classname>sigc::signal<></classname> template.
</para>
<para>
For instance, to create a signal that sends 2 parameters, a <type>bool</type>
and an <type>int</type>, just declare a <classname>sigc::signal</classname>,
like so:
<programlisting>
sigc::signal<void, bool, int> signal_something;
</programlisting>
</para>
<para>
You could just declare that signal as a public member variable, but
some people find that distasteful and prefer to make it available via
an accessor method, like so:
<programlisting>
class Server
{
public:
//signal accessor:
typedef sigc::signal<void, bool, int> type_signal_something;
type_signal_something signal_something();
protected:
type_signal_something m_signal_something;
};
Server::type_signal_something Server::signal_something()
{
return m_signal_something;
}
</programlisting>
</para>
<para>
You can then connect to the signal using the same syntax used when
connecting to >kmm; signals. For instance,
<programlisting>
server.signal_something().connect(
sigc::mem_fun(client, &Client::on_server_something) );
</programlisting>
</para>
<sect1 id="chapter-custom-signals-example"><title>Example</title>
<para>
This is a full working example that defines and uses custom signals.
</para>
<para><ulink url="&url_examples_base;signals/custom/">Source Code</ulink></para>
</sect1>
</appendix>
<appendix id="sec-signals-comparison">
<title>Comparison with other signalling systems</title>
<para>
<!-- TODO: Rewrite this paragraph and talk about Qt's moc. -->
(An aside: <application>GTK+</application> calls this scheme "signalling"; the
sharp-eyed reader with GUI toolkit experience will note that this same design
is often
seen under the name of "broadcaster-listener" (e.g., in Metrowerks'
PowerPlant framework for the Macintosh). It works in much the same
way: one sets up <literal>broadcasters</literal>, and then connects
<literal>listeners</literal> to them; the broadcaster keeps a list of the
objects listening to it, and when someone gives the broadcaster a
message, it calls all of its objects in its list with the message. In
>kmm;, signal objects play the role of broadcasters, and slots
play the role of listeners - sort of. More on this later.)
</para>
<para>
>kmm; signal handlers are strongly-typed, whereas
<application>GTK+</application> C code allows you to connect a callback with
the wrong number and type of arguments, leading to a segfault at runtime. And,
unlike <application>Qt</application>, >kmm; achieves this without modifying
the C++ language.</para>
<para>
Re. Overriding signal handlers: You can do this in the straight-C world of GTK+ too; that's what GTK's
object system is for. But in GTK+, you have to go through some
complicated procedures to get object-oriented features like
inheritance and overloading. In C++, it's simple, since those
features are supported in the language itself; you can let the
compiler do the dirty work.
</para>
<para>
This is one of the places where the beauty of C++ really comes out.
One wouldn't think of subclassing a GTK+ widget simply to override its
action method; it's just too much trouble. In GTK+, you almost always
use signals to get things done, unless you're writing a new widget.
But because overriding methods is so easy in C++, it's entirely
practical - and sensible - to subclass a button for that purpose.
</para>
</appendix>
<appendix id="sec-windows-installation">
<title>>kmm; and Win32</title>
<para>
One of the major advantages of >kmm; is that it is crossplatform. >kmm; programs written on other platforms such as
GNU/Linux can generally be transferred to Windows (and vice
versa) with few modifications to the source.
</para>
<para>
>kmm; currently works with the <ulink
url="http://mingw.org/">MingW/GCC3.4 compiler</ulink> and Microsoft
Visual C++ 2005 or later (including the freely available express
editions) on the Windows platform. There is an
<ulink url="ftp://ftp.gnome.org/pub/GNOME/binaries/win32/gtkmm">
installer</ulink> available for gtkmm on Microsoft Windows. Refer to
<ulink url="https://wiki.gnome.org/Projects/gtkmm/MSWindows/">
https://wiki.gnome.org/Projects/gtkmm/MSWindows</ulink> for instructions how to
use it.
</para>
<sect1 id="sec-building-on-win32">
<title>Building >kmm; on Win32</title>
<para>Please see <ulink url="https://wiki.gnome.org/Projects/gtkmm/MSWindows/BuildingGtkmm">
https://wiki.gnome.org/Projects/gtkmm/MSWindows/BuildingGtkmm</ulink> for instructions on how to build gtkmm on Windows.
</para>
</sect1>
</appendix>
<appendix id="chapter-working-with-source">
<title>Working with gtkmm's Source Code</title>
<para>
If you are interested in helping out with the development of >kmm;, or
fixing a bug in >kmm;, you'll probably need to build the development
version of >kmm;. However, you should not install a development version over
your stable version. Instead, you should install it alongside your existing >kmm;
installation, in a separate path.
</para>
<para>
The easiest way to do this is using <ulink
url="https://wiki.gnome.org/Projects/Jhbuild">jhbuild</ulink>.
<application>jhbuild</application> is a program that makes building GNOME
software much easier by calculating dependencies and building things in the
correct order. This section will give a brief explanation of how to set up
<application>jhbuild</application> to build and install >kmm; from the
source repository (git). For up-to-date information
on <application>jhbuild</application>, please refer to the <ulink
url="http://developer.gnome.org/jhbuild/unstable/">jhbuild manual</ulink>.
If you need assistance using <application>jhbuild</application>, you should
ask for help on the <ulink
url="http://mail.gnome.org/mailman/listinfo/gnome-love">gnome-love
mailing list</ulink>.
</para>
<note>
<para>
Note that to build >kmm; from git, you'll often need to build many of its
dependencies from git as well. <application>jhbuild</application> makes
this easier than it would normally be, but it will take quite a while to
build and install them all. You will probably encounter build problems,
though these will usually be corrected quickly if you report them.
</para>
</note>
<sect1 id="sec-setting-up-jhbuild">
<title>Setting up jhbuild</title>
<para>
To set up <application>jhbuild</application>, follow the basic
installation instructions from the <ulink
url="http://developer.gnome.org/jhbuild/unstable/">jhbuild manual</ulink>.
After you have installed <application>jhbuild</application>, you
should copy the sample <application>jhbuild</application> configuration
file into your home directory by executing the following command from the
<application>jhbuild</application> directory:
<screen>$ cp examples/sample.jhbuildrc ~/.jhbuildrc</screen>
</para>
<para>
The >kmm; module is defined in the
<filename>gnome-suites-core-deps-3.x.modules</filename> moduleset, so edit your
<filename>.jhbuildrc</filename> file and set your moduleset setting to the
latest version e.g. like so:
<programlisting>moduleset = 'gnome-suites-core-deps-3.12'</programlisting>
</para>
<para>
After setting the correct moduleset, you need to tell
<application>jhbuild</application> which module or modules to build. To
build >kmm; and all of its dependencies, set <varname>modules</varname>
like so:
<programlisting>modules = [ 'gtkmm' ]</programlisting>
</para>
<para>
You can build several modules by setting the
<varname>modules</varname> variable to a meta-package, e.g.
<literal>meta-gnome-core</literal>, or listing more than one module name.
The <varname>modules</varname> variable specifies which modules will be
built when you don't explicitly specify anything on the command line. You
can always build a different moduleset later by specifying it on the
commandline (e.g. <command>jhbuild build gtkmm</command>).
</para>
<important>
<title>Setting a prefix</title>
<para>
By default, <application>jhbuild</application>'s configuration is
configured to install all software built with
<application>jhbuild</application> under the
<filename>/opt/gnome</filename> prefix. You can choose a different
prefix, but it is recommended that you keep this prefix different from
other software that you've installed (don't set it to
<filename>/usr</filename>!) If you've followed the jhbuild instructions
then this prefix belongs to your user, so you don't need to run jhbuild
as <literal>root</literal>.
</para>
</important>
<para>
When you downloaded <application>jhbuild</application> from the git repository,
you got a number of <filename>.modules</filename> files, specifying
dependencies between modules. By default <application>jhbuild</application>
does not use the downloaded versions of these files, but reads the
latest versions in the git repository. This is usually what you want.
If you don't want it, use the <varname>use_local_modulesets</varname>
variable in <filename>.jhbuildrc</filename>.
</para>
</sect1>
<sect1 id="sec-installing-jhbuild">
<title>Installing and Using the git version of >kmm;</title>
<para>
Once you've configured <application>jhbuild</application> as described
above, building >kmm; should be relatively straightforward. The first
time you run <application>jhbuild</application>, you should run the
following sequence of commands to ensure that
<application>jhbuild</application> has the required tools and verify that
it is set up correctly:
<screen>$ jhbuild bootstrap
$ jhbuild sanitycheck</screen>
</para>
<sect2 id="jhbuild-installing-gtkmm">
<title>Installing >kmm; with <application>jhbuild</application></title>
<para>
If everything worked correctly, you should be able to build >kmm; and
all of its dependencies from git by executing <command>jhbuild
build</command> (or, if you didn't specify >kmm; in the
<varname>modules</varname> variable, with the command <command>jhbuild
build gtkmm</command>).
</para>
<para>
This command will build and install a series of modules and will probably
take quite a long time the first time through. After the first time,
however, it should go quite a bit faster since it only needs to rebuild
files that changed since the last build. Alternatively, after you've
built and installed >kmm; the first time, you can rebuild >kmm; by
itself (without rebuilding all of its dependencies) with the command
<command>jhbuild buildone gtkmm</command>.
</para>
</sect2>
<sect2 id="jhbuild-using-gtkmm">
<title>Using the git version of >kmm;</title>
<para>
After you've installed the git version of >kmm;, you're ready to start
using and experimenting with it. In order to use the new version of
>kmm; you've just installed, you need to set some environment
variables so that your <filename>configure</filename> script knows where
to find the new libraries. Fortunately,
<application>jhbuild</application> offers an easy solution to this
problem. Executing the command <command>jhbuild shell</command> will
start a new shell with all of the correct environment variables set.
Now if you re-configure and build your project just as you usually do,
it should link against the newly installed libraries. To return to your
previous environment, simply exit the <application>jhbuild</application>
shell.
</para>
<para>
Once you've built your software, you'll need to run your program within
the jhbuild environment as well. To do this, you can again use the
<command>jhbuild shell</command> command to start a new shell with the
<application>jhbuild</application> environment set up. Alternatively,
you can execute a one-off command in the
<application>jhbuild</application> environment using the following
command: <command>jhbuild run command-name</command>. In this case,
the command will be run with the correct environment variables set, but
will return to your previous environment after the program exits.
</para>
</sect2>
</sect1>
</appendix>
<appendix id="chapter-wrapping-c-libraries">
<title>Wrapping C Libraries with gmmproc</title>
<para>>kmm; uses the <command>gmmproc</command> tool to generate most of its
source code, using .defs files that define the APIs of
<classname>GObject</classname>-based libraries. So it's quite easy to create
additional gtkmm-style wrappers of other glib/GObject-based
libraries.</para>
<para>This involves a variety of tools, some of them crufty, but at least
they work, and has been used successfully by several
projects.</para>
<sect1 id="sec-wrapping-build-structure">
<title>The build structure</title>
<para>Generation of the source code for a gtkmm-style wrapper API requires use
of tools such as <command>gmmproc</command> and
<filename>generate_wrap_init.pl</filename>. In theory you could write your
own build files to use these appropriately, but a much better option is to
make use of the build infrastructure provided by the mm-common module. To
get started, it helps a lot to pick an existing binding module as an example
to look at.</para>
<para>For instance, let's pretend that we are wrapping a C library called
libsomething. It provides a <classname>GObject</classname>-based API with
types named, for instance, <classname>SomeWidget</classname> and
<classname>SomeStuff</classname>.</para>
<sect2 id="copying-skeleton-project">
<title>Copying the skeleton project</title>
<para>Typically our wrapper library would be called libsomethingmm. We can start by
copying the <ulink url="http://git.gnome.org/browse/mm-common/tree/skeletonmm">skeleton
source tree</ulink> from the mm-common module.
<programlisting>
$ git clone git://git.gnome.org/mm-common
$ cp -a mm-common/skeletonmm libsomethingmm
</programlisting>
</para>
<para>This provides a directory structure for the source .hg and .ccg files and the generated .h
and .cc files, with <filename>filelist.am</filename> Automake include files that can specify the
various files in use, in terms of generic Automake variables. The directory structure usually
looks like this, after we have renamed the directories appropriately:
<itemizedlist>
<listitem><para><filename>libsomethingmm</filename>: The top-level directory.</para>
<itemizedlist>
<listitem><para><filename>libsomething</filename>: Contains the main include file and the pkg-config .pc file.</para>
<itemizedlist>
<listitem><para><filename>src</filename>: Contains .hg and .ccg source files.</para></listitem>
<listitem><para><filename>libsomethingmm</filename>: Contains generated and hand-written .h and .cc files.</para>
<itemizedlist>
<listitem><para><filename>private</filename>: Contains generated <filename>*_p.h</filename> files.</para></listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
</para>
<para>As well as renaming the directories, we should rename some of the source
files. For instance:
<programlisting>
$ for f in $(find libsomethingmm -depth -name '*skeleton*'); do \
d="${f%/*}"; b="${f##*/}"; mv "$f" "$d/${b//skeleton/libsomething}"; \
done
</programlisting>
A number of the skeleton files must still be filled in with project-specific content later.
</para>
<para>Note that files ending in <filename>.in</filename> will be used to generate
files with the same name but without the <filename>.in</filename> suffix, by
replacing some variables with actual values during the configure stage.</para>
</sect2>
<sect2 id="modifying-build-files">
<title>Modifying build files</title>
<para>Now we edit the files to adapt them to our needs. You might prefer to use a multiple-file
search-replace utility for this, such as <command>regexxer</command>. Note that nearly all of the
files provided with the skeleton source tree contain placeholder text. Thus, the substitutions
should be performed globally, and not be limited to the Automake and Autoconf files.</para>
<para>All mentions of <varname>skeleton</varname> should be replaced by the correct name of the C
library you are wrapping, such as "something" or "libsomething". In the same manner, all
instances of <varname>SKELETON</varname> should be replaced by "SOMETHING" or "LIBSOMETHING", and
all occurrences of <varname>Skeleton</varname> changed to "Something".</para>
<para>Likewise, replace all instances of <varname>Joe Hacker</varname> by the name of the intended
copyright holder, which is probably you. Do the same for the <varname>joe@example.com</varname>
email address.</para>
<sect3 id="modifying-configure.ac">
<title>configure.ac</title>
<para>In <filename>configure.ac</filename>,
<itemizedlist>
<listitem><para>The <function>AC_CONFIG_SRCDIR()</function> line must mention a file
in our source tree. We can edit this later if we don't yet know the
names of any of the files that we will create.</para></listitem>
<listitem><para>It is common for binding modules to track the version number
of the library they are wrapping. So, for instance, if the C library is
at version 1.23.4, then the initial version of the binding module would
be 1.23.0. However, avoid starting with an even minor version number as
that usually indicates a stable release.</para></listitem>
<listitem><para>The <function>AC_CONFIG_HEADERS()</function> line is used to
generate two or more configuration header files. The first header file
in the list contains all configuration macros which are set during the
configure run. The remaining headers in the list contain only a subset
of configuration macros and their corresponding <filename>config.h.in</filename>
file will not be autogenerated. The reason for this separation is that
the namespaced configuration headers are installed with your library and
define publically visible macros.</para></listitem>
<listitem><para>The <function>AC_SUBST([SOMETHINGMM_MODULES], ['...'])</function>
line may need to be modified to check for the correct dependencies.</para></listitem>
<listitem><para>The <function>AC_CONFIG_FILES()</function> block must mention
the correct directory names, as described above.</para></listitem>
</itemizedlist>
</para>
</sect3>
<sect3 id="modifying-makefile.am">
<title>Makefile.am files</title>
<para>Next we must adapt the various <filename>Makefile.am</filename> files:
<itemizedlist>
<listitem><para>In <filename>skeleton/src/Makefile.am</filename> we
must mention the correct values for the generic variables that are used
elsewhere in the build system:</para>
<variablelist>
<varlistentry>
<term><varname>binding_name</varname></term>
<listitem><para>The name of the library, such as
libsomethingmm.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>wrap_init_flags</varname></term>
<listitem><para>Additional command-line flags passed to the
<filename>generate_wrap_init.pl</filename> script, such
as the C++ namespace and the parent directory prefix of
include files.</para></listitem>
</varlistentry>
</variablelist>
</listitem>
<listitem><para>In <filename>skeleton/skeletonmm/Makefile.am</filename> we
must mention the correct values for the generic variables that are used
elsewhere in the build system:</para>
<variablelist>
<varlistentry>
<term><varname>lib_LTLIBRARIES</varname></term>
<listitem><para>This variable must mention the correct library
name, and this library name must be used to form the
<varname>_SOURCES</varname>, <varname>_LDFLAGS</varname>, and
<varname>_LIBADD</varname> variable names. It is permissible to
use variables substituted by <filename>configure</filename> like
<varname>@SOMETHINGMM_API_VERSION@</varname> as part of the
variable names.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>AM_CPPFLAGS</varname></term>
<listitem><para>The command line options passed to the C
preprocessor.</para></listitem>
</varlistentry>
<varlistentry>
<term><varname>AM_CXXFLAGS</varname></term>
<listitem><para>The command line options passed to the C++
compiler.</para></listitem>
</varlistentry>
</variablelist>
</listitem>
</itemizedlist>
</para>
</sect3>
<sect3 id="creating-hg-ccg">
<title>Creating .hg and .ccg files</title>
<para>We should now create our first <filename>.hg</filename> and <filename>.ccg</filename> files,
to wrap one of the objects in the C library. One pair of example source files already exists:
<filename>skeleton.ccg</filename> and <filename>skeleton.hg</filename>. Create copies of these
files as necessary.</para>
<para>We must mention all of our <filename>.hg</filename> and
<filename>.ccg</filename> files in the
<filename>skeleton/src/filelist.am</filename> file, typically in the
<varname>files_hg</varname> variable.</para>
<para>Any additional non-generated <filename>.h</filename> and
<filename>.cc</filename> source files may be placed in
<filename>skeleton/skeletonmm/</filename> and listed in
<filename>skeleton/skeletonmm/filelist.am</filename>, typically in the
<varname>files_extra_h</varname> and <varname>files_extra_cc</varname>
variables.</para>
<para>In the <link linkend="sec-wrapping-hg-files">.hg and .ccg files</link>
section you can learn about the syntax used in these files.</para>
</sect3>
</sect2>
</sect1>
<sect1 id="sec-wrapping-defs-files">
<title>Generating the .defs files.</title>
<para>The <filename>.defs</filename> files are text files, in a lisp format, that describe the API
of a C library, including its
<itemizedlist>
<listitem><para>objects (GObjects, widgets, interfaces, boxed-types and plain structs)</para></listitem>
<listitem><para>functions</para></listitem>
<listitem><para>enums</para></listitem>
<listitem><para>signals</para></listitem>
<listitem><para>properties</para></listitem>
<listitem><para>vfuncs</para></listitem>
</itemizedlist>
</para>
<para>At the moment, we have separate tools for generating different parts of
these <filename>.defs</filename>, so we split them up into separate files.
For instance, in the <filename>gtk/src</filename> directory of the >kmm;
sources, you will find these files:
<variablelist>
<varlistentry>
<term><filename>gtk.defs</filename></term>
<listitem><para>Includes the other files.</para></listitem>
</varlistentry>
<varlistentry>
<term><filename>gtk_methods.defs</filename></term>
<listitem><para>Objects and functions.</para></listitem>
</varlistentry>
<varlistentry>
<term><filename>gtk_enums.defs</filename></term>
<listitem><para>Enumerations.</para></listitem>
</varlistentry>
<varlistentry>
<term><filename>gtk_signals.defs</filename></term>
<listitem><para>Signals and properties.</para></listitem>
</varlistentry>
<varlistentry>
<term><filename>gtk_vfuncs.defs</filename></term>
<listitem><para>vfuncs (function pointer member fields in structs), written by hand.</para></listitem>
</varlistentry>
</variablelist>
</para>
<para>The <filename>skeletonmm/codegen/generate_defs_and_docs.sh</filename> script
generates all <filename>.defs</filename> files and the <filename>*_docs.xml</filename> file,
described in the <link linkend="sec-wrapping-documentation">Documentation</link> section.
</para>
<sect2 id="generating-defs-methods">
<title>Generating the methods .defs</title>
<para>This <filename>.defs</filename> file describes objects and their functions.
It is generated by the <command>h2def.py</command> script which you can find in
glibmm's <filename>tools/defs_gen</filename> directory. For instance,
<programlisting>
$ ./h2def.py /usr/include/gtk-3.0/gtk/*.h > gtk_methods.defs
</programlisting>
</para>
</sect2>
<sect2 id="generating-defs-enums">
<title>Generating the enums .defs</title>
<para>This <filename>.defs</filename> file describes enum types and their possible
values. It is generated by the <filename>enum.pl</filename> script which you can
find in glibmm's <filename>tools</filename> directory. For instance,
<programlisting>
$ ./enum.pl /usr/include/gtk-3.0/gtk/*.h > gtk_enums.defs
</programlisting>
</para>
</sect2>
<sect2 id="generating-defs-signals-properties">
<title>Generating the signals and properties .defs</title>
<para>This <filename>.defs</filename> file describes signals and properties. It is
generated by the special <filename>generate_extra_defs</filename> utility that is in every
wrapping project, such as <filename>gtkmm/tools/extra_defs_gen/</filename>.
For instance
<programlisting>
$ cd tools/extra_defs_gen
$ ./generate_extra_defs > gtk_signals.defs
</programlisting>
</para>
<para>You must edit the source code of your own <filename>generate_extra_defs</filename> tool
in order to generate the <filename>.defs</filename> for the GObject C types that you wish to
wrap. In the skeleton source tree, the source file is named
<filename>codegen/extradefs/generate_extra_defs_skeleton.cc</filename>. If not done so
already, the file should be renamed, with the basename of your new binding substituted
for the <varname>skeleton</varname> placeholder. The <filename>codegen/Makefile.am</filename>
file should also mention the new source filename.</para>
<para>Then edit the <filename>.cc</filename> file to specify the correct types.
For instance, your <function>main()</function> function might look like this:
<programlisting>
#include <libsomething.h>
int main(int, char**)
{
something_init();
std::cout << get_defs(SOME_TYPE_WIDGET)
<< get_defs(SOME_TYPE_STUFF);
return 0;
}
</programlisting>
</para>
</sect2>
<sect2 id="writing-defs-vfuncs">
<title>Writing the vfuncs .defs</title>
<para>
This <filename>.defs</filename> file describes virtual functions (vfuncs).
It must be written by hand. There is the skeleton file
<filename>skeleton/src/skeleton_vfunc.defs</filename> to start from. You can also look
at >kmm;'s <filename>gtk/src/gtk_vfuncs.defs</filename> file.
</para>
</sect2>
</sect1>
<sect1 id="sec-wrapping-hg-files">
<title>The .hg and .ccg files</title>
<para>The .hg and .ccg source files are very much like
.h and .cc C++ source files, but they contain extra macros, such as
<function>_CLASS_GOBJECT()</function> and
<function>_WRAP_METHOD()</function>, from which
<command>gmmproc</command> generates appropriate C++ source code,
usually at the same position in the header. Any additional C++ source
code will be copied verbatim into the corresponding
.h or .cc file.</para>
<para>A .hg file will typically include some headers
and then declare a class, using some macros to add API or behaviour to
this class. For instance, >kmm;'s <filename>button.hg</filename> looks
roughly like this:
<programlisting>
#include <gtkmm/bin.h>
#include <gtkmm/activatable.h>
_DEFS(gtkmm,gtk)
_PINCLUDE(gtkmm/private/bin_p.h)
namespace Gtk
{
class Button
: public Bin,
public Activatable
{
_CLASS_GTKOBJECT(Button,GtkButton,GTK_BUTTON,Gtk::Bin,GtkBin)
_IMPLEMENTS_INTERFACE(Activatable)
public:
_CTOR_DEFAULT
explicit Button(const Glib::ustring& label, bool mnemonic = false);
_WRAP_METHOD(void set_label(const Glib::ustring& label), gtk_button_set_label)
...
_WRAP_SIGNAL(void clicked(), "clicked")
...
_WRAP_PROPERTY("label", Glib::ustring)
};
} // namespace Gtk
</programlisting>
</para>
<para>The macros in this example do the following:
<variablelist>
<varlistentry>
<term><function>_DEFS()</function></term>
<listitem><para>Specifies the destination directory for generated sources, and the name of the main .defs file that <command>gmmproc</command> should parse.</para></listitem>
</varlistentry>
<varlistentry>
<term><function>_PINCLUDE()</function></term>
<listitem><para>Tells <command>gmmproc</command> to include a header in the generated private/button_p.h file.</para></listitem>
</varlistentry>
<varlistentry>
<term><function>_CLASS_GTKOBJECT()</function></term>
<listitem><para>Tells <command>gmmproc</command> to add some typedefs, constructors, and standard methods to this class, as appropriate when wrapping a widget.</para></listitem>
</varlistentry>
<varlistentry>
<term><function>_IMPLEMENTS_INTERFACE()</function></term>
<listitem><para>Tells <command>gmmproc</command> to add initialization code for the interface.</para></listitem>
</varlistentry>
<varlistentry>
<term><function>_CTOR_DEFAULT</function></term>
<listitem><para>Add a default constructor.</para></listitem>
</varlistentry>
<varlistentry>
<term><function>_WRAP_METHOD()</function>,
<function>_WRAP_SIGNAL()</function>, and
<function>_WRAP_PROPERTY()</function></term>
<listitem><para>Add methods to wrap parts of the C API.</para></listitem>
</varlistentry>
</variablelist>
</para>
<para>The .h and .cc files will be generated from the .hg and .ccg files by
processing them with <command>gmmproc</command> like so, though this happens
automatically when using the above build structure:
<programlisting>
$ cd gtk/src
$ /usr/lib/glibmm-2.4/proc/gmmproc -I ../../tools/m4 --defs . button . ./../gtkmm
</programlisting>
</para>
<para>Notice that we provided <command>gmmproc</command> with the path to the
.m4 convert files, the path to the .defs file, the name of a .hg file, the
source directory, and the destination directory.</para>
<para>You should avoid including the C header from your C++ header, to avoid
polluting the global namespace, and to avoid exporting unnecessary public
API. But you will need to include the necessary C headers from your
.ccg file.</para>
<para>The macros are explained in more detail in the following sections.</para>
<sect2 id="gmmproc-m4-conversions">
<title>m4 Conversions</title>
<para>The macros that you use in the .hg and .ccg files often need to know how
to convert a C++ type to a C type, or vice-versa. gmmproc takes this information
from an .m4 file in your <literal>tools/m4/</literal> directory. This allows it
to call a C function in the implementation of your C++ method, passing the
appropriate parameters to that C functon. For instance, this
tells gmmproc how to convert a GtkTreeView pointer to a Gtk::TreeView pointer:
<programlisting>
_CONVERSION(`GtkTreeView*',`TreeView*',`Glib::wrap($3)')
</programlisting>
</para>
<para><literal>$3</literal> will be replaced by the parameter name when this
conversion is used by gmmproc.
</para>
<para>
Some extra macros make this easier and consistent. Look in gtkmm's .m4 files
for examples. For instance:
<programlisting>
_CONVERSION(`PrintSettings&',`GtkPrintSettings*',__FR2P)
_CONVERSION(`const PrintSettings&',`GtkPrintSettings*',__FCR2P)
_CONVERSION(`const Glib::RefPtr<Printer>&',`GtkPrinter*',__CONVERT_REFPTR_TO_P($3))
</programlisting>
</para>
</sect2>
<sect2 id="gmmproc-m4-initializations">
<title>m4 Initializations</title>
<para>
Often when wrapping methods, it is desirable to store the return of the C
function in what is called an output parameter. In this case, the C++ method
returns <type>void</type> but an output parameter in which to store the value
of the C function is included in the argument list of the C++ method.
gmmproc allows such functionality, but appropriate initialization macros must
be included to tell gmmproc how to initialize the C++ parameter from the
return of the C function.
</para>
<para>
For example, if there was a C function that returned a
<type>GtkWidget*</type> and for some reason, instead of having the C++ method
also return the widget, it was desirable to have the C++ method place the
widget in a specified output parameter, an initialization macro such as the
following would be necessary:
<programlisting>
_INITIALIZATION(`Gtk::Widget&',`GtkWidget*',`$3 = Glib::wrap($4)')
</programlisting>
</para>
<para>
<literal>$3</literal> will be replaced by the output parameter name of the
C++ method and <literal>$4</literal> will be replaced by the return of the C
function when this initialization is used by gmmproc. For convenience,
<literal>$1</literal> will also be replaced by the C++ type without the
ampersand (&) and <literal>$2</literal> will be replaced by the C type.
</para>
</sect2>
<sect2 id="gmmproc-class-macros">
<title>Class macros</title>
<para>The class macro declares the class itself and its relationship with the
underlying C type. It generates some internal constructors, the member
<varname>gobject_</varname>, typedefs, the <function>gobj()</function>
accessors, type registration, and the <function>Glib::wrap()</function>
method, among other things.</para>
<para>Other macros, such as <function>_WRAP_METHOD()</function> and
<function>_WRAP_SIGNAL()</function> may only be used after a call to a
<function>_CLASS_*</function> macro.</para>
<sect3 id="gmmproc-class-gobject">
<title>_CLASS_GOBJECT</title>
<para>This macro declares a wrapper for a type that is derived from
<classname>GObject</classname>, but whose wrapper is not derived from
<classname>Gtk::Object</classname>.</para>
<para><function>_CLASS_GOBJECT( C++ class, C class, C casting macro, C++ base class, C base class )</function></para>
<para>For instance, from <filename>accelgroup.hg</filename>:
<programlisting>
_CLASS_GOBJECT(AccelGroup, GtkAccelGroup, GTK_ACCEL_GROUP, Glib::Object, GObject)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-class-gtkobject">
<title>_CLASS_GTKOBJECT</title>
<para>This macro declares a wrapper for a type whose wrapper is derived from
<classname>Gtk::Object</classname>, such as a widget or dialog.</para>
<para><function>_CLASS_GTKOBJECT( C++ class, C class, C casting macro, C++ base class, C base class )</function></para>
<para>For instance, from <filename>button.hg</filename>:
<programlisting>
_CLASS_GTKOBJECT(Button, GtkButton, GTK_BUTTON, Gtk::Bin, GtkBin)
</programlisting>
</para>
<para>You will typically use this macro when the class already derives from Gtk::Object. For instance, you will use it when wrapping a GTK+ Widget, because Gtk::Widget derives from Gtk::Object.</para>
<para>You might also derive non-widget classes from Gtk::Object so they can be used without <classname>Glib::RefPtr</classname>. For instance, they could then be instantiated with <function>Gtk::manage()</function> or on the stack as a member variable. This is convenient, but you should use this only when you are sure that true reference-counting is not needed. We consider it useful for widgets.</para>
</sect3>
<sect3 id="gmmproc-class-boxedtype">
<title>_CLASS_BOXEDTYPE</title>
<para>This macro declares a wrapper for a non-<classname>GObject</classname>
struct, registered with
<function>g_boxed_type_register_static()</function>.</para>
<para><function>_CLASS_BOXEDTYPE( C++ class, C class, new function, copy function, free function )</function></para>
<para>For instance, from <classname>Gdk::RGBA</classname>:
<programlisting>
_CLASS_BOXEDTYPE(RGBA, GdkRGBA, NONE, gdk_rgba_copy, gdk_rgba_free)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-class-boxedtype-static">
<title>_CLASS_BOXEDTYPE_STATIC</title>
<para>This macro declares a wrapper for a simple assignable struct such as
<classname>GdkRectangle</classname>. It is similar to
<function>_CLASS_BOXEDTYPE</function>, but the C struct is not allocated
dynamically.</para>
<para><function>_CLASS_BOXEDTYPE_STATIC( C++ class, C class )</function></para>
<para>For instance, for <classname>Gdk::Rectangle</classname>:
<programlisting>
_CLASS_BOXEDTYPE_STATIC(Rectangle, GdkRectangle)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-class-opaque-copyable">
<title>_CLASS_OPAQUE_COPYABLE</title>
<para>This macro declares a wrapper for an opaque struct that has copy and free
functions. The new, copy and free functions will be used to instantiate the
default constructor, copy constructor and destructor.</para>
<para><function>_CLASS_OPAQUE_COPYABLE( C++ class, C class, new function, copy function, free function )</function></para>
<para>For instance, from <classname>Glib::Checksum</classname>:
<programlisting>
_CLASS_OPAQUE_COPYABLE(Checksum, GChecksum, NONE, g_checksum_copy, g_checksum_free)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-class-opaque-refcounted">
<title>_CLASS_OPAQUE_REFCOUNTED</title>
<para>This macro declares a wrapper for a reference-counted opaque struct. The
C++ wrapper cannot be directly instantiated and can only be used with
<classname>Glib::RefPtr</classname>.</para>
<para><function>_CLASS_OPAQUE_REFCOUNTED( C++ class, C class, new function, ref function, unref function )</function></para>
<para>For instance, for <classname>Pango::Coverage</classname>:
<programlisting>
_CLASS_OPAQUE_REFCOUNTED(Coverage, PangoCoverage, pango_coverage_new, pango_coverage_ref, pango_coverage_unref)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-class-generic">
<title>_CLASS_GENERIC</title>
<para>This macro can be used to wrap structs which don't fit into any
specialized category.</para>
<para><function>_CLASS_GENERIC( C++ class, C class )</function></para>
<para>For instance, for <classname>Pango::AttrIter</classname>:
<programlisting>
_CLASS_GENERIC(AttrIter, PangoAttrIterator)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-class-interface">
<title>_CLASS_INTERFACE</title>
<para>This macro declares a wrapper for a type that is derived from
<classname>GTypeInterface</classname>.
</para>
<para><function>_CLASS_INTERFACE( C++ class, C class, C casting macro, C interface struct, Base C++ class (optional), Base C class (optional) )</function></para>
<para>
For instance, from <filename>celleditable.hg</filename>:
<programlisting>
_CLASS_INTERFACE(CellEditable, GtkCellEditable, GTK_CELL_EDITABLE, GtkCellEditableIface)
</programlisting>
</para>
<para>Two extra parameters are optional, for the case that the interface derives from another interface,
which should be the case when the GInterface has another GInterface as a prerequisite.
For instance, from <filename>loadableicon.hg</filename>:
<programlisting>
_CLASS_INTERFACE(LoadableIcon, GLoadableIcon, G_LOADABLE_ICON, GLoadableIconIface, Icon, GIcon)
</programlisting>
</para>
</sect3>
</sect2>
<sect2 id="gmmproc-constructor-macros">
<title>Constructor macros</title>
<para>The <function>_CTOR_DEFAULT()</function> and
<function>_WRAP_CTOR()</function> macros add constructors, wrapping the
specified <function>*_new()</function> C functions. These macros assume that
the C object has properties with the same names as the function parameters,
as is usually the case, so that it can supply the parameters directly to a
<function>g_object_new()</function> call. These constructors never actually
call the <function>*_new()</function> C functions,
because gtkmm must actually instantiate derived GTypes, and the
<function>*_new()</function> C functions are meant only as convenience
functions for C programmers.</para>
<para>When using <function>_CLASS_GOBJECT()</function>, the constructors should
be protected (rather than public) and each constructor should have a
corresponding <function>_WRAP_CREATE()</function> in the public section.
This prevents the class from being instantiated without using a
<classname>RefPtr</classname>. For instance:
<programlisting>
class TextMark : public Glib::Object
{
_CLASS_GOBJECT(TextMark, GtkTextMark, GTK_TEXT_MARK, Glib::Object, GObject)
protected:
_WRAP_CTOR(TextMark(const Glib::ustring& name, bool left_gravity = true), gtk_text_mark_new)
public:
_WRAP_CREATE(const Glib::ustring& name, bool left_gravity = true)
</programlisting>
</para>
<sect3 id="gmmproc-ctor-default">
<title>_CTOR_DEFAULT</title>
<para>This macro creates a default constructor with no arguments.
</para>
</sect3>
<sect3 id="gmmproc-wrap-ctor">
<title>_WRAP_CTOR</title>
<para>This macro creates a constructor with arguments, equivalent to a
<function>*_new()</function> C function. It won't actually call the
<function>*_new()</function> function, but will simply create an equivalent
constructor with the same argument types. It takes a C++ constructor
signature, and a C function name.
</para>
<para>It also takes an optional extra argument:
<variablelist>
<varlistentry>
<term>errthrow</term>
<listitem>
<para>This tells gmmproc that the C <function>*_new()</function> has
a final GError** parameter which should be ignored.</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</sect3>
<sect3 id="gmmproc-ctor-manual">
<title>Hand-coding constructors</title>
<para>When a constructor must be partly hand written because, for instance, the
<function>*_new()</function> C function's parameters do not correspond
directly to object properties, or because the <function>*_new()</function> C
function does more than call <function>g_object_new()</function>, the
<function>_CONSTRUCT()</function> macro may be used in the
.ccg file to save some work. The <function>_CONSTRUCT</function> macro takes
a series of property names and values. For instance, from
<filename>button.ccg</filename>:
<programlisting>
Button::Button(const Glib::ustring& label, bool mnemonic)
:
_CONSTRUCT("label", label.c_str(), "use_underline", gboolean(mnemonic))
{}
</programlisting>
</para>
</sect3>
</sect2>
<sect2 id="gmmproc-method-macros">
<title>Method macros</title>
<sect3 id="gmmproc-wrap-method">
<title>_WRAP_METHOD</title>
<para>This macro generates the C++ method to wrap a C function.</para>
<para><function>_WRAP_METHOD( C++ method signature, C function name)</function></para>
<para>For instance, from <filename>entry.hg</filename>:
<programlisting>
_WRAP_METHOD(void set_text(const Glib::ustring& text), gtk_entry_set_text)
</programlisting>
</para>
<para>The C function (e.g. <function>gtk_entry_set_text</function>) is described
more fully in the .defs file, and the <filename>convert*.m4</filename> files
contain the necessary conversion from the C++ parameter type to the C
parameter type. This macro also generates doxygen documentation comments
based on the <filename>*_docs.xml</filename> and
<filename>*_docs_override.xml</filename> files.</para>
<para>There are some optional extra arguments:
<variablelist>
<varlistentry>
<term>refreturn</term>
<listitem>
<para>Do an extra <function>reference()</function> on the return value,
in case the C function does not provide a reference.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>errthrow</term>
<listitem>
<para>Use the last GError** parameter of the C function to
throw an exception.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>deprecated</term>
<listitem>
<para>Puts the generated code in #ifdef blocks. Text about the
deprecation can be specified as an optional
parameter.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>constversion</term>
<listitem>
<para>Just call the non-const version of the same function,
instead of generating almost duplicate code.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ifdef</term>
<listitem>
<para>Puts the generated code in #ifdef blocks.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>slot_name</term>
<listitem>
<para>Specifies the name of the slot parameter of the method, if it
has one. This enables <command>gmmproc</command> to generate code
to copy the slot and pass the copy on to the C function in its
final <literal>gpointer user_data</literal> parameter. The
<literal>slot_callback</literal> option must also be used to
specify the name of the glue callback function to also pass on to
the C function.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>slot_callback</term>
<listitem>
<para>Used in conjunction with the <literal>slot_name</literal>
option to specify the name of the glue callback function that
handles extracting the slot and then calling it. The address of
this callback is also passed on to the C function that the method
wraps.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>no_slot_copy</term>
<listitem>
<para>Tells <command>gmmproc</command> not to pass a copy of the slot
to the C function, if the method has one. Instead the slot itself
is passed. The slot parameter name and the glue callback function
must have been specified with the <literal>slot_name</literal> and
<literal>slot_callbback</literal> options respectively.</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>Selecting which C++ types should be used is also important when wrapping
C API. Though it's usually obvious what C++ types should be used in the C++
method, here are some hints:
<itemizedlist>
<listitem><para>Objects used via <classname>RefPtr</classname>: Pass the
<classname>RefPtr</classname> as a const reference. For instance,
<code>const Glib::RefPtr<Gtk::FileFilter>&
filter</code>.</para></listitem>
<listitem><para>Const Objects used via <classname>RefPtr</classname>: If the
object should not be changed by the function, then make sure that
the object is const, even if the <classname>RefPtr</classname> is
already const. For instance, <code>const Glib::RefPtr<const
Gtk::FileFilter>& filter</code>.</para></listitem>
<listitem><para>Wrapping <classname>GList*</classname> and
<classname>GSList*</classname> parameters: First, you need to discover
what objects are contained in the list's data field for each item,
usually by reading the documentation for the C function. The list can
then be wrapped by a <classname>std::vector</classname> type.
For instance, <code>std::vector<
Glib::RefPtr<Gdk::Pixbuf> ></code>.
You may need to define a Traits type to specify how the C
and C++ types should be converted.</para></listitem>
<listitem><para>Wrapping <classname>GList*</classname> and
<classname>GSList*</classname> return types: You must discover whether
the caller should free the list and whether it should release the items
in the list, again by reading the documentation of the C function. With
this information you can choose the ownership (none, shallow or deep)
for the m4 conversion rule, which you should probably put directly into
the .hg file because the ownership depends on the
function rather than the type. For instance:
<programlisting>#m4 _CONVERSION(`GSList*',`std::vector<Widget*>',`Glib::SListHandler<Widget*>::slist_to_vector($3, Glib::OWNERSHIP_SHALLOW)')</programlisting></para></listitem>
</itemizedlist>
</para>
</sect3>
<sect3 id="gmmproc-wrap-method-docs-only">
<title>_WRAP_METHOD_DOCS_ONLY</title>
<para>This macro is like <function>_WRAP_METHOD()</function>, but it generates
only the documentation for a C++ method that wraps a C function. Use this
when you must hand-code the method, but you want to use the documentation
that would be generated if the method was generated.</para>
<para><function>_WRAP_METHOD_DOCS_ONLY(C function name)</function></para>
<para>For instance, from <filename>container.hg</filename>:
<programlisting>
_WRAP_METHOD_DOCS_ONLY(gtk_container_remove)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-ignore">
<title>_IGNORE / _IGNORE_SIGNAL</title>
<para><command>gmmproc</command> will warn you on stdout about functions and signals that
you have forgotten to wrap, helping to ensure that you are wrapping the
complete API. But if you don't want to wrap some functions or signals, or if you chose
to hand-code some methods then you can use the _IGNORE() or _IGNORE_SIGNAL() macro to make
<command>gmmproc</command> stop complaining.</para>
<para><function>_IGNORE(C function name 1, C function name2, etc)</function></para>
<para><function>_IGNORE_SIGNAL(C signal name 1, C signal name2, etc)</function></para>
<para>For instance, from <filename>buttonbox.hg</filename>:
<programlisting>
_IGNORE(gtk_button_box_set_spacing, gtk_button_box_get_spacing)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-wrap-signal">
<title>_WRAP_SIGNAL</title>
<para>This macro generates the C++ libsigc++-style signal to wrap a C GObject
signal. It actually generates a public accessor method, such as
<function>signal_clicked()</function>, which returns a proxy object.
<command>gmmproc</command> uses the .defs file to discover the C parameter
types and the .m4 convert files to discover appropriate type
conversions.</para>
<para><function>_WRAP_SIGNAL( C++ signal handler signature, C signal name)</function></para>
<para>For instance, from <filename>button.hg</filename>:
<programlisting>
_WRAP_SIGNAL(void clicked(),"clicked")
</programlisting>
</para>
<para>Signals usually have function pointers in the GTK struct, with a
corresponding enum value and a <function>g_signal_new()</function> in the
.c file.</para>
<para>There are some optional extra arguments:
<variablelist>
<varlistentry>
<term>no_default_handler</term>
<listitem>
<para>Do not generate an <function>on_something()</function> virtual
method to allow easy overriding of the default signal handler.
Use this when adding a signal with a default signal handler
would break the ABI by increasing the size of the class's
virtual function table.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>custom_default_handler</term>
<listitem>
<para>Generate a declaration of the <function>on_something()</function>
virtual method in the <filename>.h</filename> file, but do not
generate a definition in the <filename>.cc</filename> file.
Use this when you must generate the definition by hand.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>custom_c_callback</term>
<listitem>
<para>Do not generate a C callback function for the signal.
Use this when you must generate the callback function by hand.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>refreturn</term>
<listitem>
<para>Do an extra <function>reference()</function> on the return value
of the <function>on_something()</function> virtual method, in
case the C function does not provide a reference.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ifdef</term>
<listitem>
<para>Puts the generated code in #ifdef blocks.</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</sect3>
<sect3 id="gmmproc-wrap-property">
<title>_WRAP_PROPERTY</title>
<para>This macro generates the C++ method to wrap a C GObject property. You must
specify the property name and the wanted C++ type for the property. <command>gmmproc</command>
uses the .defs file to discover the C type and the .m4 convert files to
discover appropriate type conversions.</para>
<para><function>_WRAP_PROPERTY(C property name, C++ type)</function></para>
<para>For instance, from <filename>button.hg</filename>:
<programlisting>
_WRAP_PROPERTY("label", Glib::ustring)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-wrap-vfunc">
<title>_WRAP_VFUNC</title>
<para>This macro generates the C++ method to wrap a virtual C function.</para>
<para><function>_WRAP_VFUNC( C++ method signature, C function name)</function></para>
<para>For instance, from <filename>widget.hg</filename>:
<programlisting>
_WRAP_VFUNC(SizeRequestMode get_request_mode() const, get_request_mode)
</programlisting>
</para>
<para>The C function (e.g. <function>get_request_mode</function>) is described
more fully in the <filename>*_vfuncs.defs</filename> file, and the
<filename>convert*.m4</filename> files contain the necessary conversion from
the C++ parameter type to the C parameter type.</para>
<para>There are some optional extra arguments:
<variablelist>
<varlistentry>
<term>refreturn</term>
<listitem>
<para>Do an extra <function>reference()</function> on the return value
of the <function>something_vfunc()</function> function,
in case the virtual C function does not provide a reference.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>refreturn_ctype</term>
<listitem>
<para>Do an extra <function>reference()</function> on the return value
of an overridden <function>something_vfunc()</function> function
in the C callback function, in case the calling C function
expects it to provide a reference.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>errthrow</term>
<listitem>
<para>Use the last GError** parameter of the C virtual function (if
there is one) to throw an exception.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>custom_vfunc</term>
<listitem>
<para>Do not generate a definition of the vfunc in the
<filename>.cc</filename> file. Use this when you must generate
the vfunc by hand.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>custom_vfunc_callback</term>
<listitem>
<para>Do not generate a C callback function for the vfunc.
Use this when you must generate the callback function by hand.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>ifdef</term>
<listitem>
<para>Puts the generated code in #ifdef blocks.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>slot_name</term>
<listitem>
<para>Specifies the name of the slot parameter of the method, if it
has one. This enables <command>gmmproc</command> to generate code
to copy the slot and pass the copy on to the C function in its
final <literal>gpointer user_data</literal> parameter. The
<literal>slot_callback</literal> option must also be used to
specify the name of the glue callback function to also pass on to
the C function.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>slot_callback</term>
<listitem>
<para>Used in conjunction with the <literal>slot_name</literal>
option to specify the name of the glue callback function that
handles extracting the slot and then calling it. The address of
this callback is also passed on to the C function that the method
wraps.</para>
</listitem>
</varlistentry>
<varlistentry>
<term>no_slot_copy</term>
<listitem>
<para>Tells <command>gmmproc</command> not to pass a copy of the slot
to the C function, if the method has one. Instead the slot itself
is passed. The slot parameter name and the glue callback function
must have been specified with the <literal>slot_name</literal> and
<literal>slot_callbback</literal> options respectively.</para>
</listitem>
</varlistentry>
</variablelist>
</para>
<para>A rule to which there may be exceptions: If the virtual C function returns
a pointer to an object derived from <classname>GObject</classname>, i.e. a
reference-counted object, then the virtual C++ function shall return a
<classname>Glib::RefPtr<></classname> object. One of the extra
arguments <parameter>refreturn</parameter> or
<parameter>refreturn_ctype</parameter> is required.</para>
</sect3>
</sect2>
<sect2 id="gmmproc-other-macros">
<title>Other macros</title>
<sect3 id="gmmproc-implements-interface">
<title>_IMPLEMENTS_INTERFACE</title>
<para>This macro generates initialization code for the interface.</para>
<para><function>_IMPLEMENTS_INTERFACE(C++ interface name)</function></para>
<para>For instance, from <filename>button.hg</filename>:
<programlisting>
_IMPLEMENTS_INTERFACE(Activatable)
</programlisting>
</para>
<para>There is one optional extra argument:
<variablelist>
<varlistentry>
<term>ifdef</term>
<listitem>
<para>Puts the generated code in #ifdef blocks.</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</sect3>
<sect3 id="gmmproc-wrap-enum">
<title>_WRAP_ENUM</title>
<para>This macro generates a C++ enum to wrap a C enum. You must specify the desired C++ name and
the name of the underlying C enum.</para>
<para>For instance, from <filename>enums.hg</filename>:
<programlisting>
_WRAP_ENUM(WindowType, GtkWindowType)
</programlisting>
</para>
<para>If the enum is not a <classname>GType</classname>, you must pass a third parameter NO_GTYPE.
This is the case when there is no <function>*_get_type()</function> function for the C enum, but
be careful that you don't just need to include an extra header for that function. You should also
file a bug against the C API, because all enums should be registered as GTypes.</para>
<para>For example, from <filename>icontheme.hg</filename>:
<programlisting>
_WRAP_ENUM(IconLookupFlags, GtkIconLookupFlags, NO_GTYPE)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-wrap-enum-docs-only">
<title>_WRAP_ENUM_DOCS_ONLY</title>
<para>This macro just generates a Doxygen documentationn block for the enum.
This is useful for enums that can't be wrapped with
<function>_WRAP_ENUM()</function> because they are complexly defined (maybe
using C macros) but including the generated enum documentation is still
desired. It is used with the same syntax as
<function>_WRAP_ENUM()</function> and also process the same options (though
NO_GTYPE is just ignored because it makes no difference when just generating
the enum's documentation).
</para>
</sect3>
<sect3 id="gmmproc-wrap-gerror">
<title>_WRAP_GERROR</title>
<para>This macro generates a C++ exception class, derived from Glib::Error, with
a Code enum and a code() method. You must specify the desired C++ name, the name
of the corresponding C enum, and the prefix for the C enum values.</para>
<para>This exception can then be thrown by methods which are generated from _WRAP_METHOD() with the errthrow option.</para>
<para>For instance, from <filename>pixbuf.hg</filename>:
<programlisting>
_WRAP_GERROR(PixbufError, GdkPixbufError, GDK_PIXBUF_ERROR)
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-member-set-get">
<title>_MEMBER_GET / _MEMBER_SET</title>
<para>
Use these macros if you're wrapping a simple struct or boxed type that provides
direct access to its data members, to create getters and setters for the data members.
</para>
<para><function>_MEMBER_GET(C++ name, C name, C++ type, C type)</function></para>
<para><function>_MEMBER_SET(C++ name, C name, C++ type, C type)</function></para>
<para>
For example, in <filename>rectangle.hg</filename>:
<programlisting>_MEMBER_GET(x, x, int, int)</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-member-get-set-ptr">
<title>_MEMBER_GET_PTR / _MEMBER_SET_PTR</title>
<para>
Use these macros to automatically provide getters and setters for a data
member that is a pointer type. For the getter function, it will
create two methods, one const and one non-const.
</para>
<para><function>_MEMBER_GET_PTR(C++ name, C name, C++ type, C type)</function></para>
<para><function>_MEMBER_SET_PTR(C++ name, C name, C++ type, C type)</function></para>
<para>For example, for <classname>Pango::Analysis</classname> in <filename>item.hg</filename>:
<programlisting>
// _MEMBER_GET_PTR(engine_lang, lang_engine, EngineLang*, PangoEngineLang*)
// It's just a comment. It's difficult to find a real-world example.
</programlisting>
</para>
</sect3>
<sect3 id="gmmproc-member-get-set-gobject">
<title>_MEMBER_GET_GOBJECT / _MEMBER_SET_GOBJECT</title>
<para>
Use these macros to provide getters and setters for a data member that is a
<classname>GObject</classname> type that must be referenced before being
returned.
</para>
<para><function>_MEMBER_GET_GOBJECT(C++ name, C name, C++ type, C type)</function></para>
<para><function>_MEMBER_SET_GOBJECT(C++ name, C name, C++ type, C type)</function></para>
<para>For example, in Pangomm, <filename>layoutline.hg</filename>:
<programlisting>
_MEMBER_GET_GOBJECT(layout, layout, Pango::Layout, PangoLayout*)
</programlisting>
</para>
</sect3>
</sect2>
<sect2 id="gmmproc-parameter-processing">
<title>gmmproc Parameter Processing</title>
<para><command>gmmproc</command> allows processing the parameters in a method
signature for the macros that process method signatures (like
<function>_WRAP_METHOD()</function>, <function>_WRAP_CTOR()</function> and
<function>_WRAP_CREATE()</function>) in a variety of ways:
</para>
<sect3 id="gmmproc-parameter-reordering">
<title>Parameter Reordering</title>
<para>
For all the macros that process method signatures, it is possible to
specify a different order for the C++ parameters than the existing order
in the C function, virtual function or signal. For example, say that the
following C function were being wrapped as a C++ method for the
<classname>Gtk::Widget</classname> class:
<programlisting>
void gtk_widget_set_device_events(GtkWidget* widget, GdkDevice* device,
GdkEventMask events);
</programlisting>
However, changing the order of the C++ method's two parameters is
necessary. Something like the following would wrap the function as a C++
method with a different order for the two parameters:
<programlisting>
_WRAP_METHOD(void set_device_events(Gdk::EventMask events{events},
const Glib::RefPtr<const Gdk::Device>& device{device}),
gtk_widget_set_device_events)
</programlisting>
The <literal>{c_param_name}</literal> following the method parameter
names tells <command>gmmproc</command> to map the C++ parameter to the
specified C parameter within the <literal>{}</literal>. Since the C++
parameter names correspond to the C ones, the above could be re-written
as:
<programlisting>
_WRAP_METHOD(void set_device_events(Gdk::EventMask events{.}, const
Glib::RefPtr<const Gdk::Device>& device{.}),
gtk_widget_set_device_events)
</programlisting>
</para>
<warning>
<para>
Please note that when reordering parameters for a
<function>_WRAP_SIGNAL()</function> method signature, the C parameter
names would always be <literal>p0</literal>, <literal>p1</literal>,
etc. because the <filename>generate_extra_defs</filename> utility uses those
parameter names no matter what the C API's parameter names may be.
It's how the utility is written presently.
</para>
</warning>
</sect3>
<sect3 id="gmmproc-optional-parameter-processing">
<title>Optional Parameter Processing</title>
<para>
For all macros processing method signatures except
<function>_WRAP_SIGNAL()</function> and
<function>_WRAP_VFUNC()</function> it is also possible to make the
parameters optional so that extra C++ methods are generated without the
specified optional parameter. For example, say that the following
<function>*_new()</function> function were being wrapped as a constructor
in the <classname>Gtk::ToolButton</classname> class:
<programlisting>
GtkToolItem* gtk_tool_button_new(GtkWidget* icon_widget, const gchar*
label);
</programlisting>
Also, say that the C API allowed NULL for the function's
<parameter>label</parameter> parameter so that that parameter is optional.
It would be possible to have <command>gmmproc</command> generate the
original constructor (with all the parameters) along with an additional
constructor without that optional parameter by appending a
<literal>{?}</literal> to the parameter name like so:
<programlisting>
_WRAP_CTOR(ToolButton(Widget& icon_widget, const Glib::ustring&
label{?}), gtk_tool_button_new)
</programlisting>
In this case, two constructors would be generated: One with the optional
parameter and one without it.
</para>
</sect3>
<sect3 id="gmmproc-output-parameter-processing">
<title>Output Parameter Processing</title>
<para>
With <function>_WRAP_METHOD()</function> it is also possible for the
return of the wrapped C function (if it has one) to be placed in an
output parameter of the C++ method instead of having the C++ method also
return a value like the C function does. To do that, simply include the
output parameter in the C++ method parameter list appending a
<literal>{OUT}</literal> to the output parameter name. For example, if
<function>gtk_widget_get_request_mode()</function> is declared as the
following:
<programlisting>
GtkSizeRequestMode gtk_widget_get_request_mode(GtkWidget* widget);
</programlisting>
And having the C++ method set an output parameter is desired instead of
returning a <type>SizeRequestMode</type>, something like the following
could be used:
<programlisting>
_WRAP_METHOD(void get_request_mode(SizeRequestMode& mode{OUT})
const, gtk_widget_get_request_mode)
</programlisting>
The <literal>{OUT}</literal> appended to the name of the
<parameter>mode</parameter> output parameter tells
<command>gmmproc</command> to place the return of the C function in that
output parameter. In this case, however, a necessary initialization
macro like the following would also have to be specified:
<programlisting>
_INITIALIZATION(`SizeRequestMode&',`GtkSizeRequestMode',`$3 =
(SizeRequestMode)($4)')
</programlisting>
Which could also be written as:
<programlisting>
_INITIALIZATION(`SizeRequestMode&',`GtkSizeRequestMode',`$3 =
($1)($4)')
</programlisting>
</para>
<para>
<function>_WRAP_METHOD()</function> also supports setting C++ output
parameters from C output parameters if the C function being wrapped has
any. Suppose, for example, that we want to wrap the following C function
that returns a value in its C output parameter
<parameter>rect</parameter>:
<programlisting>
gboolean gtk_icon_view_get_cell_rect(GtkIconView* icon_view,
GtkTreePath* path, GtkCellRenderer* cell, GdkRectangle* rect);
</programlisting>
To have <command>gmmproc</command> place the value returned in the C++
<parameter>rect</parameter> output parameter, something like the
following <function>_WRAP_METHOD()</function> directive could be used:
<programlisting>
_WRAP_METHOD(bool get_cell_rect(const TreeModel::Path& path, const
CellRenderer& cell, Gdk::Rectangle& rect{>>}) const,
gtk_icon_view_get_cell_rect)
</programlisting>
The <literal>{>>}</literal> following the <parameter>rect</parameter>
parameter name indicates that the C++ output parameter should be set from
the value returned in the C parameter from the C function.
<command>gmmproc</command> will generate a declaration of a temporary
variable in which to store the value of the C output parameter and a
statement that sets the C++ output parameter from the temporary variable.
In this case it may be necessary to have an
<function>_INITIALIZATION()</function> describing how to set a
<classname>Gdk::Rectangle&</classname> from a
<classname>GdkRectangle*</classname> such as the following:
<programlisting>
_INITIALIZATION(`Gdk::Rectangle&',`GdkRectangle', `$3 =
Glib::wrap(&($4))')
</programlisting>
</para>
</sect3>
</sect2>
<sect2 id="gmmproc-basic-types">
<title>Basic Types</title>
<para>Some of the basic types that are used in C APIs have better alternatives
in C++. For example, there's no need for a <type>gboolean</type> type since
C++ has <type>bool</type>. The following list shows some commonly-used
types in C APIs and what you might convert them to in a C++ wrapper library.
</para>
<segmentedlist><title>Basic Type equivalents</title>
<?dbhtml list-presentation="table"?>
<segtitle>C type</segtitle>
<segtitle>C++ type</segtitle>
<seglistitem><seg><type>gboolean</type></seg><seg><type>bool</type></seg></seglistitem>
<seglistitem><seg><type>gint</type></seg><seg><type>int</type></seg></seglistitem>
<seglistitem><seg><type>guint</type></seg><seg><type>guint</type></seg></seglistitem>
<seglistitem><seg><type>gdouble</type></seg><seg><type>double</type></seg></seglistitem>
<seglistitem><seg><type>gunichar</type></seg><seg><type>gunichar</type></seg></seglistitem>
<seglistitem><seg><type>gchar*</type></seg><seg><classname>Glib::ustring</classname> (or <classname>std::string</classname> for filenames)</seg></seglistitem>
</segmentedlist>
</sect2>
</sect1>
<sect1 id="sec-wrapping-hand-coded-files">
<title>Hand-coded source files</title>
<para>You might want to include additional source files that will not be
generated by <command>gmmproc</command> from <filename>.hg</filename> and
<filename>.ccg</filename> files. You can simply place these in your
<filename>libsomething/libsomethingmm</filename> directory and mention them
in the <filename>Makefile.am</filename> in the
<varname>files_extra_h</varname> and <varname>files_extra_cc</varname>
variables.</para>
</sect1>
<sect1 id="sec-wrapping-initialization">
<title>Initialization</title>
<para>Your library must be initialized before it can be used, to register the
new types that it makes available. Also, the C library that you are wrapping
might have its own initialization function that you should call. You can do
this in an <function>init()</function> function that you can place in
hand-coded <filename>init.h</filename> and <filename>init.cc</filename>
files. This function should initialize your dependencies (such as the C
function, and >kmm;) and call your generated
<function>wrap_init()</function> function. For instance:
<programlisting>
void init()
{
Gtk::Main::init_gtkmm_internals(); //Sets up the g type system and the Glib::wrap() table.
wrap_init(); //Tells the Glib::wrap() table about the libsomethingmm classes.
}
</programlisting>
</para>
<para>The implementation of the <function>wrap_init()</function> method in
<filename>wrap_init.cc</filename> is generated by
<filename>generate_wrap_init.pl</filename>, but the declaration in
<filename>wrap_init.h</filename> is hand-coded, so you will need to adjust
<filename>wrap_init.h</filename> so that the <function>wrap_init()</function>
function appears in the correct C++ namespace.</para>
</sect1>
<sect1 id="sec-wrapping-problems">
<title>Problems in the C API.</title>
<para>You are likely to encounter some problems in the library that you are wrapping, particularly if it is a new project. Here are some common problems, with solutions.</para>
<sect2 id="wrapping-predeclare-structs">
<title>Unable to predeclare structs</title>
<para>By convention, structs are declared in glib/GTK+-style headers like so:
<programlisting>
typedef struct _ExampleWidget ExampleWidget;
struct _ExampleWidget
{
...
};
</programlisting>
</para>
<para>The extra typedef allows the struct to be used in a header without including
its full definition, simply by predeclaring it, by repeating that typedef.
This means that you don't have to include the C library's header in your C++ header,
thus keeping it out of your public API. <command>gmmproc</command> assumes that
this technique was used, so you will see compiler errors if that is not the case.</para>
<para>
This compiler error might look like this:
<programlisting>
example-widget.h:56: error: using typedef-name 'ExampleWidget' after 'struct'
../../libexample/libexamplemm/example-widget.h:34: error: 'ExampleWidget' has a previous declaration here
make[4]: *** [example-widget.lo] Error 1
</programlisting>
or this:
<programlisting>
example-widget.h:60: error: '_ExampleWidget ExampleWidget' redeclared as different kind of symbol
../../libexample/libexamplemm/example-widget.h:34: error: previous declaration of 'typedef struct _ExampleWidget ExampleWidget'
</programlisting>
</para>
<para>This is easy to correct in the C library, so do send a patch to the relevant maintainer.</para>
</sect2>
<sect2 id="wrapping-no-properties">
<title>Lack of properties</title>
<para>By convention, glib/GTK+-style objects have <function>*_new()</function>
functions, such as <function>example_widget_new()</function> that do nothing
more than call <function>g_object_new()</function> and return the result.
The input parameters are supplied to <function>g_object_new()</function>
along with the names of the properties for which they are values. For
instance,
<programlisting>
GtkWidget* example_widget_new(int something, const char* thing)
{
return g_object_new (EXAMPLE_TYPE_WIDGET, "something", something, "thing", thing, NULL);
}
</programlisting>
</para>
<para>This allows language bindings to implement their own equivalents (such as
C++ constructors), without using the <function>*_new()</function> function.
This is often necessary so that they can actually instantiate a derived
GType, to add their own hooks for signal handlers and vfuncs.</para>
<para>At the least, the <function>_new()</function> function should not use any
private API (functions that are only in a .c file). Even when there are no
functions, we can sometimes reimplement 2 or 3 lines of code in a
<function>_new()</function> function as long as those lines of code use API
that is available to us.</para>
<para>Another workaround is to add a <function>*_construct()</function> function
that the C++ constructor can call after instantiating its own type. For
instance,
<programlisting>
GtkWidget* example_widget_new(int something, const char* thing)
{
ExampleWidget* widget;
widget = g_object_new (EXAMPLE_TYPE_WIDGET, NULL);
example_widget_construct(widget, "something", something, "thing", thing);
}
void example_widget_construct(ExampleWidget* widget, int something, const char* thing)
{
//Do stuff that uses private API:
widget->priv->thing = thing;
do_something(something);
}
</programlisting>
</para>
<para>Adding properties, and ensuring that they interact properly with each
other, is relatively difficult to correct in the C library, but it is
possible, so do file a bug and try to send a patch to the relevant
maintainer.</para>
</sect2>
</sect1>
<sect1 id="sec-wrapping-documentation">
<title>Documentation</title>
<para>In general, gtkmm-style projects use Doxygen, which reads specially formatted C++ comments and generates HTML documentation. You may write these doxygen comments directly in the header files.</para>
<sect2 id="wrapping-reusing-c-documentation">
<title>Reusing C documentation</title>
<para>You might wish to reuse documentation that exists for the C library that
you are wrapping. GTK-style C libraries typically use gtk-doc and therefore
have source code comments formatted for gtk-doc and some extra documentation
in .sgml and .xml files. The docextract_to_xml.py script, from glibmm's
<filename>tools/defs_gen</filename> directory, can read these files and
generate an .xml file that <command>gmmproc</command> can use to generate
doxygen comments. <command>gmmproc</command> will even try to transform the
documentation to make it more appropriate for a C++ API.</para>
<para>
For instance,
<programlisting>./docextract_to_xml.py -s ~/checkout/gnome/gtk+/gtk/ > gtk_docs.xml
</programlisting>
</para>
<para>Because this automatic transformation is not always appropriate, you might
want to provide hand-written text for a particular method. You can do this
by copying the XML node for the function from your
<filename>something_docs.xml</filename> file to the
<filename>something_docs_override.xml</filename> file and changing the
contents.</para>
</sect2>
<sect2 id="wrapping-documentation-build-structure">
<title>Documentation build structure</title>
<para>If you copied the skeleton source tree in mm-common and substituted the
placeholder text, then you will already have suitable <filename>Makefile.am</filename>
and <filename>Doxyfile.in</filename> files. With the mm-common build setup, the list
of Doxygen input files is not defined in the Doxygen configuration file, but passed
along from <command>make</command> to the standard input of <command>doxygen</command>.
The input file list is defined by the <varname>doc_input</varname> variable in the
<filename>Makefile.am</filename> file.
</para>
</sect2>
</sect1>
</appendix>
</book>
<!-- some vim settings
vim:ts=2 sw=2 et
-->
|