Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 2427 lines (2072 sloc) 56.28 kb
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
/*
* See Licensing and Copyright notice in naev.h
*/

/**
* @file toolkit.c
*
* @brief Handles windows and widgets.
*/


#include "toolkit.h"

#include "naev.h"

#include <stdarg.h>

#include "tk/toolkit_priv.h"

#include "log.h"
#include "pause.h"
#include "opengl.h"
#include "input.h"
#include "nstd.h"
#include "dialogue.h"
#include "conf.h"


#define INPUT_DELAY conf.repeat_delay /**< Delay before starting to repeat. */
#define INPUT_FREQ conf.repeat_freq /**< Interval between repetition. */


static unsigned int genwid = 0; /**< Generates unique window ids, > 0 */


static int toolkit_open = 0; /**< 1 if toolkit is in use, 0 else. */
static int toolkit_delayCounter = 0; /**< Horrible hack around secondary loop. */


/*
* window stuff
*/
#define MIN_WINDOWS 3 /**< Minimum windows to prealloc. */
static Window *windows = NULL; /**< Window linked list, not to be confused with MS windows. */
static int window_dead = 0; /**< There are dead windows lying around. */


/*
* simulate keypresses when holding
*/
static SDLKey input_key = 0; /**< Current pressed key. */
static SDLMod input_mod = 0; /**< Current pressed modifier. */
static unsigned int input_keyTime = 0; /**< Tick pressed. */
static int input_keyCounter = 0; /**< Number of repetitions. */
static char input_text = 0; /**< Current character. */


/*
* default outline colours
*/
const glColour* toolkit_colLight = &cGrey90; /**< Light outline colour. */
const glColour* toolkit_col = &cGrey70; /**< Normal outline colour. */
const glColour* toolkit_colDark = &cGrey30; /**< Dark outline colour. */


/*
* VBO
*/
static gl_vbo *toolkit_vbo; /**< Toolkit VBO. */
static GLsizei toolkit_vboColourOffset; /**< Colour offset. */


/*
* static prototypes
*/
/* input */
static int toolkit_mouseEvent( Window *w, SDL_Event* event );
static int toolkit_mouseEventWidget( Window *w, Widget *wgt,
      Uint32 type, Uint8 button, int x, int y, int rx, int ry );
static int toolkit_keyEvent( Window *wdw, SDL_Event* event );
/* focus */
static void toolkit_focusClear( Window *wdw );
static int toolkit_isFocusable( Widget *wgt );
static Widget* toolkit_getFocus( Window *wdw );
/* render */
static void window_renderBorder( Window* w );
/* Death. */
static void widget_kill( Widget *wgt );
static void window_kill( Window *wdw );
static void toolkit_purgeDead (void);


/**
* @brief Checks to see if the toolkit is open.
*
* @return 1 if the toolkit is open.
*/
int toolkit_isOpen (void)
{
   return !!toolkit_open;
}


/**
* @brief Delays the toolkit purge by an iteration, useful for dialogues.
*/
void toolkit_delay (void)
{
   toolkit_delayCounter++;
}


/**
* @brief Sets the internal widget position.
*
* @param wdw Window to which the widget belongs.
* @param wgt Widget to set position of.
* @param x X position to use.
* @param y Y position to use.
*/
void toolkit_setPos( Window *wdw, Widget *wgt, int x, int y )
{
   /* X position. */
   if (x < 0)
      wgt->x = wdw->w - wgt->w + x;
   else
      wgt->x = (double) x;

   /* Y position. */
   if (y < 0)
      wgt->y = wdw->h - wgt->h + y;
   else
      wgt->y = (double) y;
}


/**
* @brief Allocates room for a new widget.
*
* @param w Window to create widget in.
* @return Newly allocated widget.
*/
Widget* window_newWidget( Window* w, const char *name )
{
   Widget *wgt, *wlast, *wtmp;
   char *saved_name = NULL;

   /* NULL protection. */
   if (w==NULL)
      return NULL;

   /* Try to find one with the same name first. */
   wlast = NULL;
   for (wgt=w->widgets; wgt!=NULL; wgt=wgt->next) {

      /* Must match name. */
      if (strcmp(name, wgt->name)!=0) {
         wlast = wgt;
         continue;
      }

      /* Should be destroyed. */
      if (!wgt_isFlag( wgt, WGT_FLAG_KILL )) {
         WARN("Trying to create widget '%s' over existing one that hasn't been destroyed",
               name );
         return NULL;
      }

      /* Relink. */
      if (wlast==NULL)
         w->widgets = wgt->next;
      else
         wlast->next = wgt->next;

      /* Prepare and return this widget. */
      saved_name = wgt->name;
      wgt->name = NULL;
      widget_cleanup(wgt);
      break;
   }

   /* Must grow widgets. */
   if (wgt == NULL)
      wgt = malloc( sizeof(Widget) );

   /* Sane defaults. */
   memset( wgt, 0, sizeof(Widget) );
   wgt->type = WIDGET_NULL;
   wgt->status = WIDGET_STATUS_NORMAL;
   wgt->wdw = w->id;
   if (saved_name != NULL) /* Hack to avoid frees so _getFocus works in the same frame. */
      wgt->name = saved_name;
   else
      wgt->name = strdup(name);
   wgt->id = ++w->idgen;

   /* Set up. */
   wlast = NULL;
   for (wtmp=w->widgets; wtmp!=NULL; wtmp=wtmp->next)
      wlast = wtmp;
   if (wlast == NULL)
      w->widgets = wgt;
   else
      wlast->next = wgt;

   return wgt;
}


/**
* @brief Gets a Window by ID.
*
* @param wid ID of the window to get.
* @return Window matching wid.
*/
Window* window_wget( const unsigned int wid )
{
   Window *w;
   if (windows == NULL)
      return NULL;
   for (w = windows; w != NULL; w = w->next)
      if (w->id == wid)
         return w;
   return NULL;
}


/**
* @brief Gets a widget from window id and widgetname.
*
* @param wid ID of the window to get widget from.
* @param name Name of the widget to get.
* @return Widget matching name in the window.
*/
Widget* window_getwgt( const unsigned int wid, const char* name )
{
   Window *wdw;
   Widget *wgt;

   /* Get the window. */
   wdw = window_wget(wid);
   if (wdw == NULL)
      return NULL;

   /* Find the widget. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next)
      if (strcmp(wgt->name, name)==0)
         return wgt;

   WARN("Widget '%s' not found in window '%u'!", name, wid );
   return NULL;
}


/**
* @brief Gets the dimensions of a window.
*
* @param wid ID of the window to get dimension of.
* @param[out] w Width of the window or -1 on error.
* @param[out] h Height of the window or -1 on error.
*/
void window_dimWindow( const unsigned int wid, int *w, int *h )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget(wid);
   if (wdw == NULL) {
      *w = -1;
      *h = -1;
      return;
   }

   /* Set dimensions. */
   *w = wdw->w;
   *h = wdw->h;
}

/**
* @brief Gets a widget's position.
*
* @param wid ID of the window to get widget from.
* @param name Name of the widget to get position of.
* @param[out] x X position of the widget.
* @param[out] y Y position of the widget.
*/
void window_posWidget( const unsigned int wid,
      char* name, int *x, int *y )
{
   Widget *wgt;

   /* Get widget. */
   wgt = window_getwgt(wid,name);
   if (wgt == NULL)
      return;

   /* Return position. */
   if (x != NULL)
      (*x) = wgt->x;
   if (y != NULL)
      (*y) = wgt->y;
}


/**
* @brief Moves a widget.
*
* @param wid ID of the window to get widget from.
* @param name Name of the widget to set position to.
* @param x New X position to set widget to.
* @param y New Y position to set widget to.
*/
void window_moveWidget( const unsigned int wid,
      char* name, int x, int y )
{
   Window *wdw;
   Widget *wgt;

   /* Get window. */
   wdw = window_wget(wid);
   if (wdw == NULL)
      return;

   /* Get widget. */
   wgt = window_getwgt(wid,name);
   if (wgt == NULL)
      return;

   /* Set position. */
   toolkit_setPos( wdw, wgt, x, y );
}


/**
* @brief Checks to see if a window exists.
*
* @param wdwname Name of the window to check.
* @return 1 if it exists, 0 if it doesn't.
*/
int window_exists( const char* wdwname )
{
   Window *w;
   if (windows == NULL)
      return 0;
   for (w = windows; w != NULL; w = w->next)
      if ((strcmp(w->name,wdwname)==0) && !window_isFlag(w, WINDOW_KILL))
         return 1;
   return 0; /* doesn't exist */
}


/**
* @brief Checks to see if a window with a certain ID exists.
*
* @param wdwname Name of the window to check.
* @return 1 if it exists, 0 if it doesn't.
*/
int window_existsID( const unsigned int wid )
{
   Window *w;
   if (windows == NULL)
      return 0;
   for (w = windows; w != NULL; w = w->next)
      if ((w->id==wid) && !window_isFlag(w, WINDOW_KILL))
         return 1;
   return 0; /* doesn't exist */
}


/**
* @brief Gets the ID of a window.
*
* @param wdwname Name of the window to get ID of.
* @return ID of the window.
*/
unsigned int window_get( const char* wdwname )
{
   Window *w;
   if (windows == NULL)
      return 0;
   for (w = windows; w != NULL; w = w->next)
      if ((strcmp(w->name,wdwname)==0) && !window_isFlag(w, WINDOW_KILL))
         return w->id;
   return 0;
}


/**
* @brief Creates a window.
*
* @param name Name of the window to create.
* @param x X position of the window (-1 centers).
* @param y Y position of the window (-1 centers).
* @param w Width of the window (-1 fullscreen).
* @param h Height of the window (-1 fullscreen).
* @return Newly created window's ID.
*/
unsigned int window_create( const char* name,
      const int x, const int y, const int w, const int h )
{
   Window *wcur, *wlast, *wdw;

   /* Allocate memory. */
   wdw = calloc( 1, sizeof(Window) );

   const int wid = (++genwid); /* unique id */

   /* Create the window. */

   wdw->id = wid;
   wdw->name = strdup(name);

   /* Sane defaults. */
   wdw->idgen = -1;
   wdw->focus = -1;

   /* Dimensions. */
   wdw->w = (w == -1) ? SCREEN_W : (double) w;
   wdw->h = (h == -1) ? SCREEN_H : (double) h;
   if ((w == -1) && (h == -1)) {
      window_setFlag( wdw, WINDOW_FULLSCREEN );
      wdw->x = 0.;
      wdw->y = 0.;
   }
   else {
      /* x pos */
      if (x==-1) /* center */
         wdw->x = (SCREEN_W - wdw->w)/2.;
      else if (x < 0)
         wdw->x = SCREEN_W - wdw->w + (double) x;
      else wdw->x = (double) x;
      /* y pos */
      if (y==-1) /* center */
         wdw->y = (SCREEN_H - wdw->h)/2.;
      else if (y < 0)
         wdw->y = SCREEN_H - wdw->h + (double) y;
      else wdw->y = (double) y;
   }

   if (toolkit_open==0) { /* toolkit is on */
      input_mouseShow();
      toolkit_open = 1; /* enable toolkit */
      pause_game();
      gl_defViewport(); /* Reset the default viewport */
   }

   /* Clear key repeat. */
   toolkit_clearKey();

   /* Add to list. */
   wdw->next = NULL;
   if (windows == NULL)
      windows = wdw;
   else {
      for (wcur = windows; wcur != NULL; wcur = wcur->next) {
         if ((strcmp(wcur->name,name)==0) && !window_isFlag(wcur, WINDOW_KILL) &&
               !window_isFlag(wcur, WINDOW_NOFOCUS))
            WARN("Window with name '%s' already exists!",wcur->name);
         wlast = wcur;
      }
      wlast->next = wdw;
   }

   return wid;
}


/**
* @brief Sets a window as a window's parent.
*
* When a window's parent closes, it closes the window also.
*
* @param wid Window to set as child.
* @param parent Window to set as parent.
*/
void window_setParent( unsigned int wid, unsigned int parent )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Set the parent. */
   wdw->parent = parent;
}


/**
* @brief Gets the window's parent.
*
* @param wid Window to get parent of.
* @return Parent of the window or 0 on error.
*/
unsigned int window_getParent( unsigned int wid )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return 0;

   /* Get the parent. */
   return wdw->parent;
}


/**
* @brief Sets the default close function of the window.
*
* This function is called when the window is closed.
*
* @param wid Window to set close function of.
* @param Function to trigger when window is closed, parameter is window id
* and name.
*/
void window_onClose( unsigned int wid, void (*fptr)(unsigned int,char*) )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Set the close function. */
   wdw->close_fptr = fptr;
}


/**
* @brief Sets the default accept function of the window.
*
* This function is called whenever 'enter' is pressed and the current widget
* does not catch it. NULL disables the accept function.
*
* @param wid ID of the window to set the accept function.
* @param accept Function to trigger when window is "accepted". Parameter
* passed is window name.
*/
void window_setAccept( const unsigned int wid, void (*accept)(unsigned int,char*) )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Set the accept function. */
   wdw->accept_fptr = accept;
}


/**
* @brief Sets the default cancel function of the window.
*
* This function is called whenever 'escape' is hit and the current widget
* does not catch it. NULL disables the cancel function.
*
* @param wid ID of the window to set cancel function.
* @param cancel Function to trigger when window is "cancelled". Parameter
* passed is window name.
*/
void window_setCancel( const unsigned int wid, void (*cancel)(unsigned int,char*) )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Set the cancel function. */
   wdw->cancel_fptr = cancel;
}


/**
* @brief Sets custom data for a window.
*
* @param wid Window to set custom data of.
* @param data Data to set.
*/
void window_setData( unsigned int wid, void *data )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Set data. */
   wdw->udata = data;
}


/**
* @brief Gets the custom data of a window.
*
* @param wid Window to get custom data of.
* @return The custom data or NULL if not applicable.
*/
void* window_getData( unsigned int wid )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return NULL;

   /* Get data. */
   return wdw->udata;
}


/**
* @brief Sets or removes the border of a window.
*
* Default is enabled.
*
* @param wid ID of the window to enable/disable border.
* @param enable Whether or not to enable rendering of the border.
*/
void window_setBorder( unsigned int wid, int enable )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   if (enable)
      window_rmFlag( wdw, WINDOW_NOBORDER );
   else
      window_setFlag( wdw, WINDOW_NOBORDER );
}


/**
* @brief Sets the key handler for the window.
*
* This function is only called if neither the active widget nor the window
* itself grabs the input.
*/
void window_handleKeys( const unsigned int wid,
      int (*keyhandler)(unsigned int,SDLKey,SDLMod) )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Set key event handler function. */
   wdw->keyevent = keyhandler;
}


/**
* @brief Sets the event handler for the window.
*
* This function is called every time the window receives input.
*/
void window_handleEvents( const unsigned int wid,
      int (*eventhandler)(unsigned int,SDL_Event*) )
{
   Window *wdw;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Set key event handler function. */
   wdw->eventevent = eventhandler;
}


/**
* @brief Destroys a widget.
*
* @param widget Widget to destroy.
*/
void widget_cleanup( Widget *widget )
{
   /* Type specific clean up. */
   if (widget->cleanup != NULL)
      widget->cleanup(widget);

   /* General freeing. */
   free(widget->name);
}


/**
* @brief Helper function to automatically close the window calling it.
*
* @param wid Window to close.
* @param str Unused.
*/
void window_close( unsigned int wid, char *str )
{
   (void) str;

   window_destroy( wid );
}


/**
* @brief Kills the window.
*
* @param wid ID of window to destroy.
* @return 1 if windows still need killing.
*/
void window_destroy( const unsigned int wid )
{
   Window *wdw, *w;
   if (windows == NULL)
      return;
   /* Destroy the window */
   for (wdw = windows; wdw != NULL; wdw = wdw->next) {

      /* Not the window we're looking for. */
      if (wdw->id != wid)
         continue;

      /* Already being killed, skip. */
      if (window_isFlag( wdw, WINDOW_KILL ))
         continue;

      /* Mark children for death. */
      for (w = windows; w != NULL; w = w->next)
         if (w->parent == wid)
            window_destroy( w->id );

      /* Mark for death. */
      window_setFlag( wdw, WINDOW_KILL );
      window_dead = 1;

      /* Run the close function first. */
      if (wdw->close_fptr != NULL)
         wdw->close_fptr( wdw->id, wdw->name );
      wdw->close_fptr = NULL;
      break;
   }
}


/**
* @brief Kills the window.
*
* @param wid ID of window to destroy.
*/
static void window_kill( Window *wdw )
{
   Widget *wgt, *wgtkill;

   /* Run the close function first. */
   if (wdw->close_fptr != NULL)
      wdw->close_fptr( wdw->id, wdw->name );
   wdw->close_fptr = NULL;

   /* Destroy the window. */
   if (wdw->name)
      free(wdw->name);
   wgt = wdw->widgets;
   while (wgt != NULL) {
      wgtkill = wgt;
      wgt = wgtkill->next;
      widget_kill(wgtkill);
   }
   free(wdw);

   /* Clear key repeat, since toolkit could miss the keyup event. */
   toolkit_clearKey();
}


/**
* @brief Checks to see if a widget exists.
*
* @param wid Window to check widget in.
* @param wgtname Name of the widget to check;
* @return 1 if the widget exists.
*/
int widget_exists( const unsigned int wid, const char* wgtname )
{
   Window *w = window_wget(wid);
   Widget *wgt;

   /* Get window. */
   if (w==NULL) {
      WARN("window '%d' does not exist", wid);
      return 0;
   }

   /* Check for widget. */
   for (wgt=w->widgets; wgt!=NULL; wgt=wgt->next)
      if (strcmp(wgtname, wgt->name)==0)
         return !wgt_isFlag(wgt, WGT_FLAG_KILL);

   return 0;
}


/**
* @brief Destroys a widget in a window.
*
* @param wid Window to destroy widget in.
* @param wgtname Name of the widget to destroy.
*/
void window_destroyWidget( unsigned int wid, const char* wgtname )
{
   Window *wdw;
   Widget *wgt;

   /* Get the window. */
   wdw = window_wget( wid );
   if (wdw == NULL)
      return;

   /* Get the widget. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next)
      if (strcmp(wgt->name, wgtname)==0)
         break;

   if (wgt == NULL) {
      WARN("Widget '%s' not found in window '%s'", wgtname, wdw->name );
      return;
   }

   /* Defocus. */
   if (wdw->focus == wgt->id)
      wdw->focus = -1;

   /* There's dead stuff now. */
   window_dead = 1;
   wgt_rmFlag( wgt, WGT_FLAG_FOCUSED );
   wgt_setFlag( wgt, WGT_FLAG_KILL );
}


/**
* @brief Destroy a widget really.
*/
static void widget_kill( Widget *wgt )
{
   /* Clean up. */
   widget_cleanup(wgt);
   free(wgt);
}


/**
* @brief Draws an outline.
*
* If lc is NULL, colour will be flat.
*
* @param x X position to draw at.
* @param y Y position to draw at.
* @param w Width.
* @param h Height.
* @param b Border width.
* @param thick Thickness of the border.
* @param c Colour.
* @param lc Light colour.
*/
void toolkit_drawOutlineThick( int x, int y, int w, int h, int b,
      int thick, const glColour* c, const glColour* lc )
{
   GLshort tri[5][4];
   glColour colours[10];

   /* Set shade model. */
   glShadeModel( (lc==NULL) ? GL_FLAT : GL_SMOOTH );

   x -= b - thick;
   w += 2 * (b - thick);
   y -= b - thick;
   h += 2 * (b - thick);
   lc = lc ? lc : c;

   /* Left-up. */
   tri[0][0] = x; /* Inner */
   tri[0][1] = y;
   tri[0][2] = x-thick; /* Outer */
   tri[0][3] = y-thick;
   colours[0] = *lc;
   colours[1] = *lc;

   /* Left-down. */
   tri[1][0] = x; /* Inner. */
   tri[1][1] = y + h;
   tri[1][2] = x-thick; /* Outer. */
   tri[1][3] = y + h+thick;
   colours[2] = *c;
   colours[3] = *c;

   /* Right-down. */
   tri[2][0] = x + w; /* Inner. */
   tri[2][1] = y + h;
   tri[2][2] = x + w+thick; /* Outer. */
   tri[2][3] = y + h+thick;
   colours[4] = *c;
   colours[5] = *c;

   /* Right-up. */
   tri[3][0] = x + w; /* Inner. */
   tri[3][1] = y;
   tri[3][2] = x + w+thick; /* Outer. */
   tri[3][3] = y-thick;
   colours[6] = *lc;
   colours[7] = *lc;

   /* Left-up. */
   tri[4][0] = x; /* Inner */
   tri[4][1] = y;
   tri[4][2] = x-thick; /* Outer */
   tri[4][3] = y-thick;
   colours[8] = *lc;
   colours[9] = *lc;

   /* Upload to the VBO. */
   gl_vboSubData( toolkit_vbo, 0, sizeof(tri), tri );
   gl_vboSubData( toolkit_vbo, toolkit_vboColourOffset, sizeof(colours), colours );

   /* Set up the VBO. */
   gl_vboActivateOffset( toolkit_vbo, GL_VERTEX_ARRAY, 0, 2, GL_SHORT, 0 );
   gl_vboActivateOffset( toolkit_vbo, GL_COLOR_ARRAY,
         toolkit_vboColourOffset, 4, GL_FLOAT, 0 );

   /* Draw the VBO. */
   glDrawArrays( GL_TRIANGLE_STRIP, 0, 10 );

   /* Deactivate VBO. */
   gl_vboDeactivate();
}


/**
* @brief Draws an outline.
*
* If lc is NULL, colour will be flat.
*
* @param x X position to draw at.
* @param y Y position to draw at.
* @param w Width.
* @param h Height.
* @param b Border width.
* @param c Colour.
* @param lc Light colour.
*/
void toolkit_drawOutline( int x, int y, int w, int h, int b,
      const glColour* c, const glColour* lc )
{
   GLshort lines[4][2];
   glColour colours[4];

   /* Set shade model. */
   glShadeModel( (lc==NULL) ? GL_FLAT : GL_SMOOTH );

   x -= b, w += 2 * b;
   y -= b, h += 2 * b;
   lc = lc ? lc : c;

   /* Lines. */
   lines[0][0] = x - 1; /* left-up */
   lines[0][1] = y;
   colours[0] = *lc;

   lines[1][0] = x; /* left-down */
   lines[1][1] = y + h;
   colours[1] = *c;

   lines[2][0] = x + w; /* right-down */
   lines[2][1] = y + h;
   colours[2] = *c;

   lines[3][0] = x + w; /* right-up */
   lines[3][1] = y;
   colours[3] = *lc;

   /* Upload to the VBO. */
   gl_vboSubData( toolkit_vbo, 0, sizeof(lines), lines );
   gl_vboSubData( toolkit_vbo, toolkit_vboColourOffset, sizeof(colours), colours );

   /* Set up the VBO. */
   gl_vboActivateOffset( toolkit_vbo, GL_VERTEX_ARRAY, 0, 2, GL_SHORT, 0 );
   gl_vboActivateOffset( toolkit_vbo, GL_COLOR_ARRAY,
         toolkit_vboColourOffset, 4, GL_FLOAT, 0 );

   /* Draw the VBO. */
   glDrawArrays( GL_LINE_LOOP, 0, 4 );

   /* Deactivate VBO. */
   gl_vboDeactivate();
}
/**
* @brief Draws a rectangle.
*
* If lc is NULL, colour will be flat.
*
* @param x X position to draw at.
* @param y Y position to draw at.
* @param w Width.
* @param h Height.
* @param c Colour.
* @param lc Light colour.
*/
void toolkit_drawRect( int x, int y, int w, int h,
      const glColour* c, const glColour* lc )
{
   GLshort vertex[4][2];
   glColour colours[4];

   /* Set shade model. */
   glShadeModel( (lc) ? GL_SMOOTH : GL_FLAT );

   lc = lc == NULL ? c : lc;

   /* Set up vertices and colours. */
   vertex[0][0] = x; /* left-up */
   vertex[0][1] = y;
   colours[0] = *c;

   vertex[1][0] = x; /* left-down */
   vertex[1][1] = y + h;
   colours[1] = *lc;

   vertex[2][0] = x + w; /* right-up */
   vertex[2][1] = y;
   colours[2] = *c;

   vertex[3][0] = x + w; /* right-down */
   vertex[3][1] = y + h;
   colours[3] = *lc;

   /* Upload to the VBO. */
   gl_vboSubData( toolkit_vbo, 0, sizeof(vertex), vertex );
   gl_vboSubData( toolkit_vbo, toolkit_vboColourOffset, sizeof(colours), colours );

   /* Set up the VBO. */
   gl_vboActivateOffset( toolkit_vbo, GL_VERTEX_ARRAY,
         0, 2, GL_SHORT, 0 );
   gl_vboActivateOffset( toolkit_vbo, GL_COLOR_ARRAY,
         toolkit_vboColourOffset, 4, GL_FLOAT, 0 );

   /* Draw the VBO. */
   glDrawArrays( GL_TRIANGLE_STRIP, 0, 4 );

   /* Deactivate VBO. */
   gl_vboDeactivate();
}
/**
* @brief Draws an alt text.
*
* @param bx X position to draw at.
* @param by Y position to draw at.
* @param alt Text to draw.
*/
void toolkit_drawAltText( int bx, int by, const char *alt )
{
   double w, h;
   double x, y, o;
   glColour c;
   glColour c2;

   /* Get dimensions. */
   w = 160.;
   h = gl_printHeightRaw( &gl_smallFont, w, alt );
   /* One check to make bigger. */
   if (h > 160.) {
      w = 200;
      h = gl_printHeightRaw( &gl_smallFont, w, alt );
   }

   /* Choose position. */
   x = bx + 10.;
   y = by - h - gl_smallFont.h - 10.;
   if (y < -SCREEN_H/2+10) {
      o = -(SCREEN_H/2 + y) + 10;
      y += o;
   }

   /* Set colours. */
   c.r = cGrey80.r;
   c.g = cGrey80.g;
   c.b = cGrey80.b;
   c.a = 0.8;
   c2.r = cGrey30.r;
   c2.g = cGrey30.g;
   c2.b = cGrey30.b;
   c2.a = 0.7;
   toolkit_drawRect( x-1, y-5, w+6, h+6, &c2, NULL );
   toolkit_drawRect( x-3, y-3, w+6, h+6, &c, NULL );
   gl_printTextRaw( &gl_smallFont, w, h, x, y, &cBlack, alt );
}


/**
* @brief Renders a window border.
*
* @param w Window to render
*/
static void window_renderBorder( Window* w )
{
   int i;
   GLshort cx, cy;
   double x, y;
   const glColour *lc, *c, *dc, *oc;
   GLshort vertex[31*4];
   GLfloat colours[31*4];

   /* position */
   x = w->x;
   y = w->y;

   /* colours */
   lc = &cGrey90;
   c = &cGrey70;
   dc = &cGrey50;
   oc = &cGrey30;

   /*
* Case fullscreen.
*/
   if (window_isFlag( w, WINDOW_FULLSCREEN )) {
      /* Background. */
      toolkit_drawRect( x, y, w->w, 0.6*w->h, dc, c );
      toolkit_drawRect( x, y+0.6*w->h, w->w, 0.4*w->h, c, NULL );
      /* Name. */
      gl_printMidRaw( &gl_defFont, w->w,
            x,
            y + w->h - 20.,
            &cBlack, w->name );
      return;
   }

   /*
* window shaded bg
*/
   /* main body */
   toolkit_drawRect( x+21, y, w->w-42., 0.6*w->h, dc, c );
   toolkit_drawRect( x+21, y+0.6*w->h, w->w-42., 0.4*w->h, c, NULL );

   glShadeModel(GL_SMOOTH);
   /* Both sides. */
   gl_vboActivateOffset( toolkit_vbo, GL_COLOR_ARRAY,
         toolkit_vboColourOffset, 4, GL_FLOAT, 0 );
   gl_vboActivateOffset( toolkit_vbo, GL_VERTEX_ARRAY, 0, 2, GL_SHORT, 0 );
   /* Colour is shared. */
   colours[0] = c->r;
   colours[1] = c->g;
   colours[2] = c->b;
   colours[3] = c->a;
   for (i=0; i<7; i++) {
      colours[4 + 4*i + 0] = dc->r;
      colours[4 + 4*i + 1] = dc->g;
      colours[4 + 4*i + 2] = dc->b;
      colours[4 + 4*i + 3] = dc->a;
   }
   for (i=0; i<8; i++) {
      colours[32 + 4*i + 0] = c->r;
      colours[32 + 4*i + 1] = c->g;
      colours[32 + 4*i + 2] = c->b;
      colours[32 + 4*i + 3] = c->a;
   }
   gl_vboSubData( toolkit_vbo, toolkit_vboColourOffset,
         sizeof(GLfloat) * 4*16, colours );
   /* Left side vertex. */
   cx = x;
   cy = y;
   vertex[0] = cx + 21;
   vertex[1] = cy + 0.6*w->h;
   vertex[2] = cx + 21;
   vertex[3] = cy;
   vertex[4] = cx + 15;
   vertex[5] = cy + 1;
   vertex[6] = cx + 10;
   vertex[7] = cy + 3;
   vertex[8] = cx + 6;
   vertex[9] = cy + 6;
   vertex[10] = cx + 3;
   vertex[11] = cy + 10;
   vertex[12] = cx + 1;
   vertex[13] = cy + 15;
   vertex[14] = cx;
   vertex[15] = cy + 21;
   vertex[16] = cx;
   vertex[17] = cy + 0.6*w->h;
   vertex[18] = cx;
   cy = y + w->h;
   vertex[19] = cy - 21;
   vertex[20] = cx + 1;
   vertex[21] = cy - 15;
   vertex[22] = cx + 3;
   vertex[23] = cy - 10;
   vertex[24] = cx + 6;
   vertex[25] = cy - 6;
   vertex[26] = cx + 10;
   vertex[27] = cy - 3;
   vertex[28] = cx + 15;
   vertex[29] = cy - 1;
   vertex[30] = cx + 21;
   vertex[31] = cy;
   gl_vboSubData( toolkit_vbo, 0, sizeof(GLshort) * 2*16, vertex );
   glDrawArrays( GL_TRIANGLE_FAN, 0, 16 );
   /* Right side vertex. */
   cx = x + w->w;
   cy = y;
   vertex[0] = cx - 21;
   vertex[1] = cy + 0.6*w->h;
   vertex[2] = cx - 21;
   vertex[3] = cy;
   vertex[4] = cx - 15;
   vertex[5] = cy + 1;
   vertex[6] = cx - 10;
   vertex[7] = cy + 3;
   vertex[8] = cx - 6;
   vertex[9] = cy + 6;
   vertex[10] = cx - 3;
   vertex[11] = cy + 10;
   vertex[12] = cx - 1;
   vertex[13] = cy + 15;
   vertex[14] = cx;
   vertex[15] = cy + 21;
   vertex[16] = cx;
   vertex[17] = cy + 0.6*w->h;
   vertex[18] = cx;
   cy = y + w->h;
   vertex[19] = cy - 21;
   vertex[20] = cx - 1;
   vertex[21] = cy - 15;
   vertex[22] = cx - 3;
   vertex[23] = cy - 10;
   vertex[24] = cx - 6;
   vertex[25] = cy - 6;
   vertex[26] = cx - 10;
   vertex[27] = cy - 3;
   vertex[28] = cx - 15;
   vertex[29] = cy - 1;
   vertex[30] = cx - 21;
   vertex[31] = cy;
   gl_vboSubData( toolkit_vbo, 0, sizeof(GLshort) * 2*16, vertex );
   glDrawArrays( GL_TRIANGLE_FAN, 0, 16 );


   /*
* inner outline
*/
   /* Colour. */
   for (i=0; i<7; i++) {
      colours[4*i + 0] = c->r;
      colours[4*i + 1] = c->g;
      colours[4*i + 2] = c->b;
      colours[4*i + 3] = c->a;
   }
   for (; i<7+16; i++) {
      colours[4*i + 0] = lc->r;
      colours[4*i + 1] = lc->g;
      colours[4*i + 2] = lc->b;
      colours[4*i + 3] = lc->a;
   }
   for (; i<7+16+8; i++) {
      colours[4*i + 0] = c->r;
      colours[4*i + 1] = c->g;
      colours[4*i + 2] = c->b;
      colours[4*i + 3] = c->a;
   }
   gl_vboSubData( toolkit_vbo, toolkit_vboColourOffset,
         sizeof(GLfloat) * 4*31, colours );
   /* Vertex. */
   /* Left side. */
   cx = x + 1;
   cy = y + 1;
   vertex[0] = cx + 21;
   vertex[1] = cy;
   vertex[2] = cx + 15;
   vertex[3] = cy + 1;
   vertex[4] = cx + 10;
   vertex[5] = cy + 3;
   vertex[6] = cx + 6;
   vertex[7] = cy + 6;
   vertex[8] = cx + 3;
   vertex[9] = cy + 10;
   vertex[10] = cx + 1;
   vertex[11] = cy + 15;
   vertex[12] = cx;
   vertex[13] = cy + 21;
   vertex[14] = cx;
   vertex[15] = cy + 0.6*w->h - 1;
   cy = y + w->h - 1;
   vertex[16] = cx;
   vertex[17] = cy - 21;
   vertex[18] = cx + 1;
   vertex[19] = cy - 15;
   vertex[20] = cx + 3;
   vertex[21] = cy - 10;
   vertex[22] = cx + 6;
   vertex[23] = cy - 6;
   vertex[24] = cx + 10;
   vertex[25] = cy - 3;
   vertex[26] = cx + 15;
   vertex[27] = cy - 1;
   vertex[28] = cx + 21;
   vertex[29] = cy;
   /* Right side via top. */
   cx = x + w->w - 1;
   cy = y + w->h - 1;
   vertex[30] = cx - 21;
   vertex[31] = cy;
   vertex[32] = cx - 15;
   vertex[33] = cy - 1;
   vertex[34] = cx - 10;
   vertex[35] = cy - 3;
   vertex[36] = cx - 6;
   vertex[37] = cy - 6;
   vertex[38] = cx - 3;
   vertex[39] = cy - 10;
   vertex[40] = cx - 1;
   vertex[41] = cy - 15;
   vertex[42] = cx;
   vertex[43] = cy - 21;
   cy = y + 1;
   vertex[44] = cx;
   vertex[45] = cy + 0.6*w->h - 1;
   vertex[46] = cx;
   vertex[47] = cy + 21;
   vertex[48] = cx - 1;
   vertex[49] = cy + 15;
   vertex[50] = cx - 3;
   vertex[51] = cy + 10;
   vertex[52] = cx - 6;
   vertex[53] = cy + 6;
   vertex[54] = cx - 10;
   vertex[55] = cy + 3;
   vertex[56] = cx - 15;
   vertex[57] = cy + 1;
   vertex[58] = cx - 21;
   vertex[59] = cy;
   cx = x + 1;
   cy = y + 1;
   vertex[60] = cx + 21;
   vertex[61] = cy;
   gl_vboSubData( toolkit_vbo, 0, sizeof(GLshort) * 2*31, vertex );
   glDrawArrays( GL_LINE_LOOP, 0, 31 );


   /*
* outer outline
*/
   glShadeModel(GL_FLAT);
   /* Colour. */
   for (i=0; i<31; i++) {
      colours[4*i + 0] = oc->r;
      colours[4*i + 1] = oc->g;
      colours[4*i + 2] = oc->b;
      colours[4*i + 3] = oc->a;
   }
   gl_vboSubData( toolkit_vbo, toolkit_vboColourOffset,
         sizeof(GLfloat) * 4*31, colours );
   /* Vertex. */
   /* Left side. */
   cx = x;
   cy = y;
   vertex[0] = cx + 21;
   vertex[1] = cy;
   vertex[2] = cx + 15;
   vertex[3] = cy + 1;
   vertex[4] = cx + 10;
   vertex[5] = cy + 3;
   vertex[6] = cx + 6;
   vertex[7] = cy + 6;
   vertex[8] = cx + 3;
   vertex[9] = cy + 10;
   vertex[10] = cx + 1;
   vertex[11] = cy + 15;
   vertex[12] = cx;
   vertex[13] = cy + 21;
   vertex[14] = cx;
   vertex[15] = cy + 0.6*w->h;
   cy = y + w->h;
   vertex[16] = cx;
   vertex[17] = cy - 21;
   vertex[18] = cx + 1;
   vertex[19] = cy - 15;
   vertex[20] = cx + 3;
   vertex[21] = cy - 10;
   vertex[22] = cx + 6;
   vertex[23] = cy - 6;
   vertex[24] = cx + 10;
   vertex[25] = cy - 3;
   vertex[26] = cx + 15;
   vertex[27] = cy - 1;
   vertex[28] = cx + 21;
   vertex[29] = cy;
   /* Right side via top. */
   cx = x + w->w;
   cy = y + w->h;
   vertex[30] = cx - 21;
   vertex[31] = cy;
   vertex[32] = cx - 15;
   vertex[33] = cy - 1;
   vertex[34] = cx - 10;
   vertex[35] = cy - 3;
   vertex[36] = cx - 6;
   vertex[37] = cy - 6;
   vertex[38] = cx - 3;
   vertex[39] = cy - 10;
   vertex[40] = cx - 1;
   vertex[41] = cy - 15;
   vertex[42] = cx;
   vertex[43] = cy - 21;
   cy = y;
   vertex[44] = cx;
   vertex[45] = cy + 0.6*w->h;
   vertex[46] = cx;
   vertex[47] = cy + 21;
   vertex[48] = cx - 1;
   vertex[49] = cy + 15;
   vertex[50] = cx - 3;
   vertex[51] = cy + 10;
   vertex[52] = cx - 6;
   vertex[53] = cy + 6;
   vertex[54] = cx - 10;
   vertex[55] = cy + 3;
   vertex[56] = cx - 15;
   vertex[57] = cy + 1;
   vertex[58] = cx - 21;
   vertex[59] = cy;
   cx = x;
   cy = y;
   vertex[60] = cx + 21;
   vertex[61] = cy;
   gl_vboSubData( toolkit_vbo, 0, sizeof(GLshort) * 2*31, vertex );
   glDrawArrays( GL_LINE_LOOP, 0, 31 );

   /* Clean up. */
   gl_vboDeactivate();

   /*
* render window name
*/
   gl_printMidRaw( &gl_defFont, w->w,
         x,
         y + w->h - 20.,
         &cBlack, w->name );
}


/**
* @brief Renders a window.
*
* @param w Window to render.
*/
void window_render( Window *w )
{
   double x, y, wid, hei;
   Widget *wgt;

   /* Do not render dead windows. */
   if (window_isFlag( w, WINDOW_KILL ))
      return;

   /* position */
   x = w->x;
   y = w->y;

   /* See if needs border. */
   if (!window_isFlag( w, WINDOW_NOBORDER ))
      window_renderBorder(w);

   /*
* widgets
*/
   for (wgt=w->widgets; wgt!=NULL; wgt=wgt->next)
      if (wgt->render != NULL)
         wgt->render( wgt, x, y );

   /*
* focused widget
*/
   if (w->focus != -1) {
      wgt = toolkit_getFocus( w );
      if (wgt == NULL)
         return;
      x += wgt->x;
      y += wgt->y;
      wid = wgt->w;
      hei = wgt->h;
      toolkit_drawOutline( x, y, wid, hei, 3, &cBlack, NULL );
   }
}


/**
* @brief Renders the window overlays.
*
* @param w Window to render overlays of.
*/
void window_renderOverlay( Window *w )
{
   double x, y;
   Widget *wgt;

   /* Do not render dead windows. */
   if (window_isFlag( w, WINDOW_KILL ))
      return;

   /* position */
   x = w->x;
   y = w->y;

   /*
* overlays
*/
   for (wgt=w->widgets; wgt!=NULL; wgt=wgt->next)
      if (wgt->renderOverlay != NULL)
         wgt->renderOverlay( wgt, x, y );
}


/**
* @brief Draws a scrollbar.
*
* @param x X position of scrollbar.
* @param y Y position of scrollbar.
* @param w Width of the scrollbar.
* @param h Height of the scrollbar.
* @param pos Position at [0:1].
*/
void toolkit_drawScrollbar( int x, int y, int w, int h, double pos )
{
   double sy;

   /* scrollbar background */
   toolkit_drawRect( x, y, w, h, toolkit_colDark, toolkit_col );
   /* toolkit_drawOutline( x, y, w, h, 0., toolkit_colDark, NULL ); */
   /* toolkit_drawOutline( x, y, w, h, 0., toolkit_colLight, toolkit_col ); */

   /* Bar itself. */
   sy = y + (h - 30.) * (1.-pos);
   toolkit_drawRect( x, sy, w, 30., toolkit_colLight, toolkit_col );
   toolkit_drawOutline( x + 1, sy, w - 1, 30., 0., toolkit_colDark, NULL );
}


/**
* @brief Renders the windows.
*/
void toolkit_render (void)
{
   Window *w;

   /* Render base. */
   for (w = windows; w!=NULL; w = w->next) {
      if (!window_isFlag(w, WINDOW_NORENDER) &&
            !window_isFlag(w, WINDOW_KILL)) {
         window_render(w);
         window_renderOverlay(w);
      }
   }
}


/**
* @brief Toolkit input handled here.
*
* @param event Event to handle.
* @return 1 if input was used, 0 if it wasn't.
*/
int toolkit_input( SDL_Event* event )
{
   Window *wdw;

   /* Get window that can be focused. */
   wdw = toolkit_getActiveWindow();
   if (wdw == NULL)
      return 0;

   /* Pass event to window. */
   return toolkit_inputWindow( wdw, event, 1 );
}


/**
* @brief Toolkit window input is handled here.
*/
int toolkit_inputWindow( Window *wdw, SDL_Event *event, int purge )
{
   int ret;
   Widget *wgt;
   ret = 0;

   /* See if widget needs event. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next) {
      if (wgt_isFlag( wgt, WGT_FLAG_RAWINPUT )) {
         if (wgt->rawevent != NULL) {
            ret = wgt->rawevent( wgt, event );
            if (ret != 0)
               return ret;
         }
      }
   }

   /* Event handler. */
   if (wdw->eventevent != NULL)
      wdw->eventevent( wdw->id, event );

   /* Hack in case window got destroyed in eventevent. */
   if (!window_isFlag(wdw, WINDOW_KILL)) {

      /* Pass it on. */
      switch (event->type) {
         case SDL_MOUSEMOTION:
         case SDL_MOUSEBUTTONDOWN:
         case SDL_MOUSEBUTTONUP:
            ret |= toolkit_mouseEvent(wdw, event);
            break;

         case SDL_KEYDOWN:
         case SDL_KEYUP:
            ret |= toolkit_keyEvent(wdw, event);
            break;
      }
   }

   /* Clean up the dead if needed. */
   if (purge && !dialogue_isOpen()) { /* Hack, since dialogues use secondary loop. */
      if (toolkit_delayCounter > 0)
         toolkit_delayCounter--;
      else
         toolkit_purgeDead();
   }

   return ret; /* don't block input */
}


/**
* @brief Translates the mouse coordinates.
*
* @param w Window to translate coords for.
* @param event Event to translate coords.
* @param[out] x Resulting X coord in window space.
* @param[out] y Resulting Y coord in window space.
* @param[out] rx Relative X movement (only valid for motion).
* @param[out] ry Relative Y movement (only valid for motion).
* @return The type of the event.
*/
Uint32 toolkit_inputTranslateCoords( Window *w, SDL_Event *event,
      int *x, int *y, int *rx, int *ry )
{
   /* Extract the position as event. */
   if (event->type==SDL_MOUSEMOTION) {
      *x = event->motion.x;
      *y = event->motion.y;
   }
   else if ((event->type==SDL_MOUSEBUTTONDOWN) || (event->type==SDL_MOUSEBUTTONUP)) {
      *x = event->button.x;
      *y = event->button.y;
   }

   /* Translate offset. */
   gl_windowToScreenPos( x, y, *x, *y );

   /* Transform to relative to window. */
   *x -= w->x;
   *y -= w->y;

   /* Relative only matter if mouse motion. */
   if (event->type==SDL_MOUSEMOTION) {
      *ry = (double)event->motion.yrel * gl_screen.mxscale;
      *rx = (double)event->motion.xrel * gl_screen.myscale;
   }
   else {
      *ry = 0;
      *rx = 0;
   }

   return event->type;
}


/**
* @brief Handles the mouse events.
*
* @param wdw Window receiving the mouse event.
* @param event Mouse event to handle.
*/
static int toolkit_mouseEvent( Window *w, SDL_Event* event )
{
   Widget *wgt;
   Uint32 type;
   Uint8 button;
   int x, y, rx, ry, ret;

   /* Translate mouse coords. */
   type = toolkit_inputTranslateCoords( w, event, &x, &y, &rx, &ry );

   /* Check each widget. */
   ret = 0;
   for (wgt=w->widgets; wgt!=NULL; wgt=wgt->next) {

      /* custom widgets take it from here */
      if (wgt->type==WIDGET_CUST) {
         if (wgt->dat.cst.mouse)
            ret |= wgt->dat.cst.mouse( w->id, event, x-wgt->x, y-wgt->y, wgt->w, wgt->h,
                  wgt->dat.cst.userdata );
      }
      else {
         /* Handle mouse event. */
         if (type == SDL_MOUSEMOTION)
            button = event->motion.state;
         else
            button = event->button.button;
         ret |= toolkit_mouseEventWidget( w, wgt, type, button, x, y, rx, ry );
      }
   }

   return ret;
}


/**
* @brief Handle widget mouse input.
*
* @param w Window to which widget belongs.
* @param wgt Widget receiving event.
* @param event Event received by the window.
*/
static int toolkit_mouseEventWidget( Window *w, Widget *wgt,
      Uint32 type, Uint8 button, int x, int y, int rx, int ry )
{
   int ret, inbounds;

   /* Widget translations. */
   x -= wgt->x;
   y -= wgt->y;

   /* Check inbounds. */
   inbounds = !((x < 0) || (x >= wgt->w) || (y < 0) || (y >= wgt->h));

   /* Regular widgets. */
   ret = 0;
   switch (type) {
      case SDL_MOUSEMOTION:
         /* Change the status of the widget if mouse isn't down. */

         /* Not scrolling. */
         if (wgt->status != WIDGET_STATUS_SCROLLING) {
            if (inbounds) {
               if (wgt->status != WIDGET_STATUS_MOUSEDOWN)
                  wgt->status = WIDGET_STATUS_MOUSEOVER;
            }
            else
               wgt->status = WIDGET_STATUS_NORMAL;
         }
         else
            inbounds = 1; /* Scrolling is always inbounds. */

         /* If always gets the event. */
         if (wgt_isFlag( wgt, WGT_FLAG_ALWAYSMMOVE ))
            inbounds = 1;

         /* Try to give the event to the widget. */
         if (inbounds && (wgt->mmoveevent != NULL))
            ret |= (*wgt->mmoveevent)( wgt, x, y, rx, ry );

         break;

      case SDL_MOUSEBUTTONDOWN:
         if (!inbounds)
            break;

         /* Update the status. */
         if (button == SDL_BUTTON_LEFT)
            wgt->status = WIDGET_STATUS_MOUSEDOWN;

         if (toolkit_isFocusable(wgt)) {
            toolkit_focusClear( w );
            w->focus = wgt->id;
            wgt_setFlag( wgt, WGT_FLAG_FOCUSED );
         }

         /* Try to give the event to the widget. */
         if (wgt->mclickevent != NULL)
            ret |= (*wgt->mclickevent)( wgt, button, x, y );
         break;

      case SDL_MOUSEBUTTONUP:
         /* Since basically only buttons are handled here, we ignore
* it all except the left mouse button. */
         if (button != SDL_BUTTON_LEFT)
            break;

         if (wgt->status==WIDGET_STATUS_MOUSEDOWN) {
            /* Soft-disabled buttons will run anyway. */
            if ((wgt->type==WIDGET_BUTTON) && ((wgt->dat.btn.disabled==0) ||
                     (wgt->dat.btn.softdisable))) {
               if (wgt->dat.btn.fptr==NULL)
                  DEBUG("Toolkit: Button '%s' of Window '%s' "
                        "doesn't have a function trigger",
                        wgt->name, w->name );
               else {
                  (*wgt->dat.btn.fptr)(w->id, wgt->name);
                  ret = 1;
               }
            }
         }

         /* Signal scroll done if necessary. */
         if ((wgt->status == WIDGET_STATUS_SCROLLING) && (wgt->scrolldone != NULL))
            wgt->scrolldone( wgt );

         /* Always goes normal unless is below mouse. */
         if (inbounds)
            wgt->status = WIDGET_STATUS_MOUSEOVER;
         else
            wgt->status = WIDGET_STATUS_NORMAL;

         break;
   }

   return ret;
}


/**
* @brief Maps modifier keysyms (ctrl, alt, shift) to SDLMods.
*
* @param key Key to convert.
* @return The SDLMod corresponding to the key, or 0 if none correspond.
*/
static SDLMod toolkit_mapMod( SDLKey key )
{
   switch(key) {
      case SDLK_LCTRL:
         return KMOD_LCTRL;
      case SDLK_RCTRL:
         return KMOD_RCTRL;
      case SDLK_LALT:
         return KMOD_LALT;
      case SDLK_RALT:
         return KMOD_RALT;
      case SDLK_LSHIFT:
         return KMOD_LSHIFT;
      case SDLK_RSHIFT:
         return KMOD_RSHIFT;
      default:
         return 0;
   }
}

/**
* @brief Registers a key as down (for key repetition).
*
* @param key Key to register as down.
*/
static void toolkit_regKey( SDLKey key, SDLKey c )
{
   SDLMod mod;
   
   /* See if our key is in fact a modifier key, and if it is, convert it to a mod.
* If it is indeed a mod, do not register a new key but add the modifier to the mod mask instead.
*/
   mod = toolkit_mapMod(key);
   if (mod)
      input_mod |= mod;
   else {
      input_key = key;
      input_keyTime = SDL_GetTicks();
      input_keyCounter = 0;
      input_text = nstd_checkascii(c) ? c : 0;
   }
}

/**
* @brief Unregisters a key.
*
* @param key Key to unregister.
*/
static void toolkit_unregKey( SDLKey key )
{
   SDLMod mod;
   
   /* See if our key is in fact a modifier key, and if it is, convert it to a mod.
* If it is indeed a mod, do not unregister the key but subtract the modifier from the mod mask instead.
*/
   mod = toolkit_mapMod(key);
   if (mod)
      input_mod &= ~mod;
   else
      toolkit_clearKey();
}

/**
* @brief Clears the registered keys.
*/
void toolkit_clearKey (void)
{
   input_key = 0;
   input_keyTime = 0;
   input_keyCounter = 0;
   input_text = 0;
}
/**
* @brief Handles keyboard events.
*
* @param wdw Window receiving the key event.
* @param event Keyboard event to handle.
* @return 1 if the event is used, 0 if it isn't.
*/
static int toolkit_keyEvent( Window *wdw, SDL_Event* event )
{
   Widget *wgt;
   SDLKey key;
   SDLMod mod;
   char buf[2];

   /* Event info. */
   key = event->key.keysym.sym;
   mod = event->key.keysym.mod;

   /* Hack to simulate key repetition */
   if (event->type == SDL_KEYDOWN)
#if SDL_VERSION_ATLEAST(2,0,0)
      toolkit_regKey(key, key);
#else /* SDL_VERSION_ATLEAST(2,0,0) */
      toolkit_regKey(key, event->key.keysym.unicode);
#endif /* SDL_VERSION_ATLEAST(2,0,0) */
   else if (event->type == SDL_KEYUP)
      toolkit_unregKey(key);

   /* See if window is valid. */
   if (wdw == NULL)
      return 0;

   /* Get widget. */
   wgt = toolkit_getFocus( wdw );

   /* We only want keydown from now on. */
   if (event->type != SDL_KEYDOWN)
      return 0;

   /* Trigger event function if exists. */
   if (wgt != NULL) {
      if (wgt->keyevent != NULL) {
         if (wgt->keyevent( wgt, input_key, input_mod ))
            return 1;
      }
      if (wgt->textevent != NULL) {
#if SDL_VERSION_ATLEAST(2,0,0)
         buf[0] = key & 0x7f;
#else /* SDL_VERSION_ATLEAST(2,0,0) */
         buf[0] = event->key.keysym.unicode & 0x7f;
#endif /* SDL_VERSION_ATLEAST(2,0,0) */
         buf[1] = '\0';
         if ((*wgt->textevent)( wgt, buf ))
            return 1;
      }
   }

   /* Handle button hotkeys. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next)
      if ((wgt->type == WIDGET_BUTTON) && (wgt->dat.btn.key != 0) &&
            (wgt->dat.btn.key == input_key))
         return (wgt->keyevent( wgt, SDLK_RETURN, input_mod ));

   /* Handle other cases where event might be used by the window. */
   switch (key) {
      case SDLK_TAB:
         if (mod & (KMOD_LSHIFT | KMOD_RSHIFT))
            toolkit_prevFocus( wdw );
         else
            toolkit_nextFocus( wdw );
         break;

      case SDLK_RETURN:
      case SDLK_KP_ENTER:
         if (wdw->accept_fptr != NULL) {
            wdw->accept_fptr( wdw->id, wdw->name );
            return 1;
         }
         break;

      case SDLK_ESCAPE:
         if (wdw->cancel_fptr != NULL) {
            wdw->cancel_fptr( wdw->id, wdw->name );
            return 1;
         }
         break;

      default:
         break;
   }

   /* Finally the stuff gets passed to the custom key handler if it's defined. */
   if (wdw->keyevent != NULL)
      (*wdw->keyevent)( wdw->id, input_key, input_mod );

   return 0;
}


/**
* @brief Purges the dead windows.
*/
static void toolkit_purgeDead (void)
{
   Window *wdw, *wlast, *wkill;
   Widget *wgt, *wgtlast, *wgtkill;

   /* Only clean up if necessary. */
   if (!window_dead)
      return;

   /* Must be windows. */
   if (windows == NULL)
      return;

   /* Destroy what is needed. */
   wlast = NULL;
   wdw = windows;
   while (wdw != NULL) {
      if (window_isFlag( wdw, WINDOW_KILL )) {
         /* Save target. */
         wkill = wdw;
         /* Reattach linked list. */
         if (wlast == NULL)
            windows = wdw->next;
         else
            wlast->next = wdw->next;
         wdw = wlast;
         /* Kill target. */
         wkill->next = NULL;
         window_kill( wkill );
      }
      else {
         wgtlast = NULL;
         wgt = wdw->widgets;
         while (wgt != NULL) {
            if (wgt_isFlag( wgt, WGT_FLAG_KILL )) {
               /* Save target. */
               wgtkill = wgt;
               /* Reattach linked list. */
               if (wgtlast == NULL)
                  wdw->widgets = wgt->next;
               else
                  wgtlast->next = wgt->next;
               wgt = wgtlast;
               /* Kill target. */
               wgtkill->next = NULL;
               widget_kill( wgtkill );
            }
            /* Save position. */
            wgtlast = wgt;
            if (wgt == NULL)
               wgt = wdw->widgets;
            else
               wgt = wgt->next;
         }
      }
      /* Save position. */
      wlast = wdw;
      if (wdw == NULL)
         wdw = windows;
      else
         wdw = wdw->next;
   }

   /* Nothing left to purge. */
   window_dead = 0;
}


/**
* @brief Updates the toolkit input for repeating keys.
*/
void toolkit_update (void)
{
   unsigned int t;
   Window *wdw;
   Widget *wgt;
   char buf[2];
   SDL_Event event;
   int ret;

   /* Clean up the dead if needed. */
   if (!dialogue_isOpen()) { /* Hack, since dialogues use secondary loop. */
      if (toolkit_delayCounter > 0)
         toolkit_delayCounter--;
      else
         toolkit_purgeDead();
   }

   /* Killed all the windows. */
   if (windows == NULL) {
      input_mouseHide();
      toolkit_open = 0; /* disable toolkit */
      if (paused)
         unpause_game();
      return; /* No need to handle anything else. */
   }

   /* Must have a key pressed. */
   if (input_key == 0 && input_mod == 0)
      return;

   t = SDL_GetTicks();

   /* Should be repeating. */
   if (input_keyTime + INPUT_DELAY + input_keyCounter*INPUT_FREQ > t)
      return;

   /* Increment counter. */
   input_keyCounter++;

   /* Get the window. */
   wdw = toolkit_getActiveWindow();
   if (wdw == NULL)
      return;

   /* See if widget needs event. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next) {
      if (wgt_isFlag( wgt, WGT_FLAG_RAWINPUT )) {
         if (wgt->rawevent != NULL) {
            event.type = SDL_KEYDOWN;
            event.key.state = SDL_PRESSED;
            event.key.keysym.sym = input_key;
            event.key.keysym.mod = input_mod;
            ret = wgt->rawevent( wgt, &event );
            if (ret != 0)
               return;
         }
      }
   }

   /* Handle the focused widget. */
   wgt = toolkit_getFocus( wdw );
   if ((wgt != NULL) && (wgt->keyevent != NULL))
      wgt->keyevent( wgt, input_key, input_mod );

   if ((input_text != 0) && (wgt != NULL) && (wgt->textevent != NULL)) {
      buf[0] = input_text;
      buf[1] = '\0';
      wgt->textevent( wgt, buf );
   }
}


/**
* @brief Clears the window focus.
*/
static void toolkit_focusClear( Window *wdw )
{
   Widget *wgt;
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next)
      wgt_rmFlag( wgt, WGT_FLAG_FOCUSED );
}


/**
* @brief Sanitizes the focus of a window.
*
* Makes sure the window has a focusable widget focused.
*/
void toolkit_focusSanitize( Window *wdw )
{
   Widget *wgt;

   /* Clear focus. */
   toolkit_focusClear( wdw );

   /* No focus is always sane. */
   if (wdw->focus == -1)
      return;

   /* Check focused widget. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next) {
      if (wdw->focus == wgt->id) {
         /* Not focusable. */
         if (!toolkit_isFocusable(wgt)) {
            wdw->focus = -1;
            toolkit_nextFocus( wdw ); /* Get first focus. */
         }
         else
            wgt_setFlag( wgt, WGT_FLAG_FOCUSED );
         return;
      }
   }
}


/**
* @brief Focus next widget.
*/
void toolkit_nextFocus( Window *wdw )
{
   Widget *wgt;
   int next;

   /* Clear focus. */
   toolkit_focusClear( wdw );

   /* See what to focus. */
   next = (wdw->focus == -1);
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next) {
      if (!toolkit_isFocusable(wgt))
         continue;

      if (next) {
         wdw->focus = wgt->id;
         wgt_setFlag( wgt, WGT_FLAG_FOCUSED );
         return;
      }
      else if (wdw->focus == wgt->id)
         next = 1;
   }

   /* Focus nothing. */
   wdw->focus = -1;
   return;
}


/**
* @brief Focus previous widget.
*/
void toolkit_prevFocus( Window *wdw )
{
   Widget *wgt, *prev;

   /* Clear focus. */
   toolkit_focusClear( wdw );

   /* See what to focus. */
   prev = NULL;
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next) {
      if (!toolkit_isFocusable(wgt))
         continue;

      /* See if we found the current one. */
      if (wdw->focus == wgt->id) {
         if (prev == NULL)
            wdw->focus = -1;
         else {
            wdw->focus = prev->id;
            wgt_setFlag( prev, WGT_FLAG_FOCUSED );
         }
         return;
      }

      /* Store last focusable widget. */
      prev = wgt;
   }

   /* Focus nothing. */
   if (prev == NULL)
      wdw->focus = -1;
   else {
      wdw->focus = prev->id;
      wgt_setFlag( prev, WGT_FLAG_FOCUSED );
   }
   return;
}


/**
* @brief Checks to see if a widget is focusable.
*
* @param wgt Widget to check if is focusable.
* @return 1 if it's focusable, 0 if it isn't.
*/
static int toolkit_isFocusable( Widget *wgt )
{
   if (wgt==NULL)
      return 0;

   return wgt_isFlag(wgt, WGT_FLAG_CANFOCUS);
}


/**
* @brief Gets the active window in the toolkit.
*
* @return The active window in the toolkit.
*/
Window* toolkit_getActiveWindow (void)
{
   Window *wdw, *wlast;

   /* Get window that can be focused. */
   wlast = NULL;
   for (wdw = windows; wdw!=NULL; wdw = wdw->next)
      if (!window_isFlag(wdw, WINDOW_NOFOCUS) &&
            !window_isFlag(wdw, WINDOW_KILL))
         wlast = wdw;
   return wlast;
}


/**
* @brief Gets the focused widget in a window.
*
* @param wdw The window to get the focused widget from.
* @return The focused widget.
*/
static Widget* toolkit_getFocus( Window *wdw )
{
   Widget *wgt;

   /* No focus. */
   if (wdw->focus == -1)
      return NULL;

   /* Find focus. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next)
      if (wdw->focus == wgt->id)
         return wgt;

   /* Not found. */
   toolkit_focusClear( wdw );
   wdw->focus = -1;
   return NULL;
}


/**
* @brief Sets the focused widget in a window.
*
* @param wid ID of the window to get widget from.
* @param name Name of the widget to set focus to.
*/
void window_setFocus( const unsigned int wid, const char* wgtname )
{
   Window *wdw;
   Widget *wgt;

   /* Get window. */
   wdw = window_wget(wid);
   if (wdw == NULL)
      return;

   /* Get widget. */
   wgt = window_getwgt(wid,wgtname);
   if (wgt == NULL)
      return;

   wdw->focus = wgt->id;
}


/**
* @brief Gets the focused widget in a window.
*
* @param wid ID of the window to get widget from.
* @return The focused widget's name.
*/
char* window_getFocus( const unsigned int wid )
{
   Window *wdw;
   Widget *wgt;

   /* Get window. */
   wdw = window_wget(wid);
   if (wdw == NULL)
      return NULL;

   /* Find focused widget. */
   for (wgt=wdw->widgets; wgt!=NULL; wgt=wgt->next)
      if (wgt->id == wdw->focus)
         return wgt->name;

   return NULL;
}


/**
* @brief Initializes the toolkit.
*
* @return 0 on success.
*/
int toolkit_init (void)
{
   GLsizei size;

   /* Create the VBO. */
   toolkit_vboColourOffset = sizeof(GLshort) * 2 * 31;
   size = (sizeof(GLshort)*2 + sizeof(GLfloat)*4) * 31;
   toolkit_vbo = gl_vboCreateStream( size, NULL );

   /* Disable the cursor. */
   input_mouseHide();

   return 0;
}


/**
* @brief Exits the toolkit.
*/
void toolkit_exit (void)
{
   Window *wdw;

   /* Destroy the windows. */
   while (windows!=NULL) {
      wdw = windows;
      windows = windows->next;
      window_kill(wdw);
   }
   free(windows);

   /* Free the VBO. */
   gl_vboDestroy( toolkit_vbo );
   toolkit_vbo = NULL;
}

Something went wrong with that request. Please try again.