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
|
/** <title>NSApplication</title>
<abstract>The one and only application class</abstract>
Copyright (C) 1996,1999 Free Software Foundation, Inc.
Author: Scott Christley <scottc@net-community.com>
Date: 1996
Author: Felipe A. Rodriguez <far@ix.netcom.com>
Date: August 1998
Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
Date: December 1998
This file is part of the GNUstep GUI Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; see the file COPYING.LIB.
If not, write to the Free Software Foundation,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <gnustep/gui/config.h>
#include <stdio.h>
#include <Foundation/NSArray.h>
#include <Foundation/NSSet.h>
#include <Foundation/NSDictionary.h>
#include <Foundation/NSException.h>
#include <Foundation/NSNotification.h>
#include <Foundation/NSObject.h>
#include <Foundation/NSRunLoop.h>
#include <Foundation/NSAutoreleasePool.h>
#include <Foundation/NSTimer.h>
#include <Foundation/NSProcessInfo.h>
#include <Foundation/NSFileManager.h>
#include <Foundation/NSUserDefaults.h>
#include <Foundation/NSBundle.h>
#ifndef LIB_FOUNDATION_LIBRARY
# include <Foundation/NSConnection.h>
#endif
#include <AppKit/AppKitExceptions.h>
#include <AppKit/NSGraphicsContext.h>
#include <AppKit/NSApplication.h>
#include <AppKit/NSDocumentController.h>
#include <AppKit/NSPopUpButton.h>
#include <AppKit/NSPasteboard.h>
#include <AppKit/NSColorPanel.h>
#include <AppKit/NSPanel.h>
#include <AppKit/NSEvent.h>
#include <AppKit/NSImage.h>
#include <AppKit/NSMenu.h>
#include <AppKit/NSMenuItem.h>
#include <AppKit/NSCursor.h>
#include <AppKit/NSWorkspace.h>
#include <AppKit/GSServicesManager.h>
#include <AppKit/NSNibLoading.h>
#include <AppKit/IMLoading.h>
#include <AppKit/DPSOperators.h>
#include <AppKit/NSPageLayout.h>
#include <AppKit/NSDataLinkPanel.h>
#include <AppKit/NSHelpManager.h>
#include <AppKit/GSGuiPrivate.h>
/*
* Base library exception handler
*/
static NSUncaughtExceptionHandler *defaultUncaughtExceptionHandler;
/*
* Gui library user friendly exception handler
*/
static void
_NSAppKitUncaughtExceptionHandler (NSException *exception)
{
int retVal;
#ifdef DEBUG
#define DEBUG_BUTTON @"Debug"
#else
#define DEBUG_BUTTON nil
#endif
/* Reset the exception handler to the Base library's one, to prevent
recursive calls to the gui one. */
NSSetUncaughtExceptionHandler (defaultUncaughtExceptionHandler);
/*
* If there is no graphics context to run the alert panel in or
* its a sever error, use a non-graphical exception handler
*/
if (GSCurrentContext() == nil
|| [[exception name] isEqual: NSWindowServerCommunicationException])
{
/* The following will raise again the exception using the base
library exception handler */
[exception raise];
}
retVal = NSRunCriticalAlertPanel
([NSString stringWithFormat:
GSGuiLocalizedString (@"Critical Error in %@", @""),
[[NSProcessInfo processInfo] processName]],
@"%@: %@",
GSGuiLocalizedString (@"Abort", @""),
GSGuiLocalizedString (@"Ignore", @""),
GSGuiLocalizedString (DEBUG_BUTTON, @""),
[exception name],
[exception reason]);
/* The user wants to abort */
if (retVal == NSAlertDefault)
{
/* The following will raise again the exception using the base
library exception handler */
[exception raise];
}
else if (retVal == NSAlertOther)
{
/* Debug button: abort so we can trace the error in gdb */
abort();
}
/* The user said to go on - more fun I guess - turn the AppKit
exception handler on again */
NSSetUncaughtExceptionHandler (_NSAppKitUncaughtExceptionHandler);
}
/* This is the bundle from where we load localization of messages. */
static NSBundle *guiBundle = nil;
/* Get the bundle. */
NSBundle *GSGuiBundle ()
{
return guiBundle;
}
@interface GSBackend : NSGraphicsContext
{}
+ (void) initializeBackend;
@end
BOOL
initialize_gnustep_backend(void)
{
static int first = 1;
if (first)
{
Class backend;
first = 0;
#ifdef BACKEND_BUNDLE
{
NSBundle *theBundle;
NSEnumerator *benum;
NSString *path, *bundleName;
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
/* What backend ? */
bundleName = [defs stringForKey: @"GSBackend"];
if ( bundleName == nil )
bundleName = @"libgnustep-xgps.bundle";
else
bundleName = [bundleName stringByAppendingString: @".bundle"];
NSDebugFLLog(@"BackendBundle", @"Looking for %@", bundleName);
/* Find the backend bundle */
benum = [NSStandardLibraryPaths() objectEnumerator];
while ((path = [benum nextObject]))
{
path = [path stringByAppendingPathComponent: @"Bundles"];
path = [path stringByAppendingPathComponent: bundleName];
if ([[NSFileManager defaultManager] fileExistsAtPath: path])
break;
path = nil;
}
NSCAssert(path != nil,
GSGuiLocalizedString (@"Unable to load backend, aborting",
nil));
NSDebugLog(@"Loading Backend from %@", path);
theBundle = [NSBundle bundleWithPath: path];
NSCAssert(theBundle != nil,
GSGuiLocalizedString (@"Can't init backend bundle", nil));
backend = [theBundle classNamed: @"GSBackend"];
NSCAssert(backend,
GSGuiLocalizedString (@"Can't load backend bundle", nil));
[backend initializeBackend];
}
#else
/* GSBackend will be in a separate library, so use the runtime
to find the class and avoid an unresolved reference problem */
backend = [[NSBundle gnustepBundle] classNamed: @"GSBackend"];
NSCAssert(backend, GSGuiLocalizedString (@"Can't find backend context",
nil));
[backend initializeBackend];
#endif
}
return YES;
}
/*
* Types
*/
struct _NSModalSession {
int runState;
int entryLevel;
NSWindow *window;
NSModalSession previous;
};
@interface NSApplication (Private)
- _appIconInit;
- (void) _openDocument: (NSString*)name;
- (void) _windowDidBecomeKey: (NSNotification*) notification;
- (void) _windowDidBecomeMain: (NSNotification*) notification;
- (void) _windowDidResignKey: (NSNotification*) notification;
- (void) _windowWillClose: (NSNotification*) notification;
@end
@interface NSIconWindow : NSWindow
@end
@interface NSAppIconView : NSView
- (void) setImage: (NSImage *)anImage;
@end
/*
* Class variables
*/
static NSEvent *null_event;
static Class arpClass;
static NSNotificationCenter *nc;
NSApplication *NSApp = nil;
@implementation NSIconWindow
- (BOOL) canBecomeMainWindow
{
return NO;
}
- (BOOL) canBecomeKeyWindow
{
return NO;
}
- (BOOL) worksWhenModal
{
return YES;
}
- (void) orderWindow: (NSWindowOrderingMode)place relativeTo: (int)otherWin
{
if ((place == NSWindowOut) && [NSApp isRunning])
{
NSLog (@"Argh - icon window ordered out");
}
else
{
[super orderWindow: place relativeTo: otherWin];
}
}
- (void) _initDefaults
{
[super _initDefaults];
/* Set the title of the window to the process name. Even as the
window shows no title bar, the window manager may show it. */
[self setTitle: [[NSProcessInfo processInfo] processName]];
[self setExcludedFromWindowsMenu: YES];
[self setReleasedWhenClosed: NO];
_windowLevel = NSDockWindowLevel;
}
@end
@implementation NSAppIconView
// Class variables
static NSCell* dragCell = nil;
static NSCell* tileCell = nil;
+ (void) initialize
{
NSImage *defImage = [NSImage imageNamed: @"GNUstep"];
NSImage *tileImage = [NSImage imageNamed: @"common_Tile"];
dragCell = [[NSCell alloc] initImageCell: defImage];
[dragCell setBordered: NO];
tileCell = [[NSCell alloc] initImageCell: tileImage];
[tileCell setBordered: NO];
}
- (BOOL) acceptsFirstMouse: (NSEvent*)theEvent
{
return YES;
}
- (void) concludeDragOperation: (id<NSDraggingInfo>)sender
{
}
- (unsigned) draggingEntered: (id<NSDraggingInfo>)sender
{
return NSDragOperationGeneric;
}
- (void) draggingExited: (id<NSDraggingInfo>)sender
{
}
- (unsigned) draggingUpdated: (id<NSDraggingInfo>)sender
{
return NSDragOperationGeneric;
}
- (void) drawRect: (NSRect)rect
{
[tileCell drawWithFrame: NSMakeRect(0,0,64,64) inView: self];
[dragCell drawWithFrame: NSMakeRect(8,8,48,48) inView: self];
}
- (id) initWithFrame: (NSRect)frame
{
self = [super initWithFrame: frame];
[self registerForDraggedTypes: [NSArray arrayWithObjects:
NSFilenamesPboardType, nil]];
return self;
}
- (void) mouseDown: (NSEvent*)theEvent
{
if ([theEvent clickCount] >= 2)
{
[NSApp unhide: self];
}
else
{
NSPoint lastLocation;
NSPoint location;
unsigned eventMask = NSLeftMouseDownMask | NSLeftMouseUpMask
| NSPeriodicMask | NSOtherMouseUpMask | NSRightMouseUpMask;
NSDate *theDistantFuture = [NSDate distantFuture];
BOOL done = NO;
lastLocation = [theEvent locationInWindow];
[NSEvent startPeriodicEventsAfterDelay: 0.02 withPeriod: 0.02];
while (!done)
{
theEvent = [NSApp nextEventMatchingMask: eventMask
untilDate: theDistantFuture
inMode: NSEventTrackingRunLoopMode
dequeue: YES];
switch ([theEvent type])
{
case NSRightMouseUp:
case NSOtherMouseUp:
case NSLeftMouseUp:
/* any mouse up means we're done */
done = YES;
break;
case NSPeriodic:
location = [_window mouseLocationOutsideOfEventStream];
if (NSEqualPoints(location, lastLocation) == NO)
{
NSPoint origin = [_window frame].origin;
origin.x += (location.x - lastLocation.x);
origin.y += (location.y - lastLocation.y);
[_window setFrameOrigin: origin];
}
break;
default:
break;
}
}
[NSEvent stopPeriodicEvents];
}
}
- (BOOL) prepareForDragOperation: (id<NSDraggingInfo>)sender
{
return YES;
}
- (BOOL) performDragOperation: (id<NSDraggingInfo>)sender
{
NSArray *types;
NSPasteboard *dragPb;
dragPb = [sender draggingPasteboard];
types = [dragPb types];
if ([types containsObject: NSFilenamesPboardType] == YES)
{
NSArray *names = [dragPb propertyListForType: NSFilenamesPboardType];
unsigned index;
[NSApp activateIgnoringOtherApps: YES];
for (index = 0; index < [names count]; index++)
{
[NSApp _openDocument: [names objectAtIndex: index]];
}
return YES;
}
return NO;
}
- (void) setImage: (NSImage *)anImage
{
[tileCell drawWithFrame: NSMakeRect(0,0,64,64) inView: self];
[dragCell setImage: anImage];
[dragCell drawWithFrame: NSMakeRect(8,8,48,48) inView: self];
[_window flushWindow];
}
@end
@implementation NSApplication
/*
* Class methods
*/
+ (void) initialize
{
if (self == [NSApplication class])
{
CREATE_AUTORELEASE_POOL(pool);
/*
* Dummy functions to fool linker into linking files that contain
* only catagories - static libraries seem to have problems here.
*/
extern void GSStringDrawingDummyFunction();
GSStringDrawingDummyFunction();
NSDebugLog(@"Initialize NSApplication class\n");
[self setVersion: 1];
/* Create the gui bundle we use to localize messages. */
guiBundle = [NSBundle bundleForLibrary: @"gnustep-gui"];
RETAIN(guiBundle);
/* Save the base library exception handler */
defaultUncaughtExceptionHandler = NSGetUncaughtExceptionHandler ();
/* Cache the NSAutoreleasePool class */
arpClass = [NSAutoreleasePool class];
nc = [NSNotificationCenter defaultCenter];
RELEASE(pool);
}
}
+ (void)detachDrawingThread:(SEL)selector toTarget:(id)target withObject:(id)argument
{
// TODO: This is not fully defined by Apple
}
+ (NSApplication *) sharedApplication
{
/* If the global application does not yet exist then create it */
if (!NSApp)
{
/*
* Don't combine the following two statements into one to avoid
* problems with some classes' initialization code that tries
* to get the shared application.
*/
NSApp = [self alloc];
[NSApp init];
}
return NSApp;
}
/*
* Instance methods
*/
- (id) init
{
if (NSApp != nil && NSApp != self)
{
RELEASE(self);
return [NSApplication sharedApplication];
}
// Initialization must be enclosed in an autorelease pool
{
CREATE_AUTORELEASE_POOL (_app_init_pool);
self = [super init];
NSApp = self;
if (NSApp == nil)
{
NSLog(GSGuiLocalizedString
(@"Cannot allocate the application instance!\n", nil));
RELEASE (_app_init_pool);
return nil;
}
NSDebugLog(@"Begin of NSApplication -init\n");
/* Initialize the backend here. */
initialize_gnustep_backend();
/* Create our context. This is equivalent to connecting to
our window server, so if someone wants to query information that might
require the backend, they just need to instantiate a sharedApplication
*/
_default_context = [NSGraphicsContext graphicsContextWithAttributes: nil];
[NSGraphicsContext setCurrentContext: _default_context];
/* Initialize font manager */
[NSFontManager sharedFontManager];
_hidden = [[NSMutableArray alloc] init];
_inactive = [[NSMutableArray alloc] init];
_unhide_on_activation = YES;
_app_is_hidden = YES;
/* Ivar already automatically initialized to NO when the app is created */
//_app_is_active = NO;
//_main_menu = nil;
_windows_need_update = YES;
/* Set a new exception handler for the gui library */
NSSetUncaughtExceptionHandler (_NSAppKitUncaughtExceptionHandler);
_listener = [GSServicesManager newWithApplication: self];
/* NSEvent doesn't use -init so we use +alloc instead of +new */
_current_event = [NSEvent alloc]; // no current event
null_event = [NSEvent alloc]; // create dummy event
/* We are the end of responder chain */
[self setNextResponder: nil];
RELEASE (_app_init_pool);
}
return self;
}
- (void) finishLaunching
{
NSBundle *mainBundle = [NSBundle mainBundle];
NSDictionary *infoDict = [mainBundle infoDictionary];
NSString *mainModelFile;
NSString *appIconFile;
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSString *filePath;
NSDictionary *userInfo;
NSArray *windows_list;
unsigned count;
unsigned i;
BOOL hadDuplicates = NO;
appIconFile = [infoDict objectForKey: @"NSIcon"];
if (appIconFile && ![appIconFile isEqual: @""])
{
NSImage *image = [NSImage imageNamed: appIconFile];
if (image != nil)
{
[self setApplicationIconImage: image];
}
}
[self _appIconInit];
mainModelFile = [infoDict objectForKey: @"NSMainNibFile"];
if (mainModelFile != nil && [mainModelFile isEqual: @""] == NO)
{
if ([NSBundle loadNibNamed: mainModelFile owner: self] == NO)
{
NSLog (GSGuiLocalizedString (@"Cannot load the main model file '%@'",
nil), mainModelFile);
}
}
/* post notification that launch will finish */
[nc postNotificationName: NSApplicationWillFinishLaunchingNotification
object: self];
/* Register our listener to incoming services requests etc. */
[_listener registerAsServiceProvider];
/*
* Establish the current key and main windows. We need to do this in case
* the windows were created and set to be key/main earlier - before the
* app was active.
*/
windows_list = [self windows];
count = [windows_list count];
for (i = 0; i < count; i++)
{
NSWindow *win = [windows_list objectAtIndex: i];
if ([win isKeyWindow] == YES)
{
if (_key_window == nil)
{
_key_window = win;
}
else
{
hadDuplicates = YES;
NSDebugLog(@"Duplicate keyWindow ignored");
[win resignKeyWindow];
}
}
if ([win isMainWindow] == YES)
{
if (_main_window == nil)
{
_main_window = win;
}
else
{
hadDuplicates = YES;
NSDebugLog(@"Duplicate mainWindow ignored");
[win resignMainWindow];
}
}
}
/*
* If there is no main or key window, we need to make the main menu key
* so it can respond to menu shortcuts and deactivate the app properly
* when it looses focus.
*/
if (_key_window == nil && _main_window == nil)
{
_key_window = [[self mainMenu] window];
[_key_window becomeKeyWindow];
}
/*
* If there was more than one window set as key or main, we must make sure
* that the one we have recorded is the real one by making it become key/main
* again.
*/
if (hadDuplicates)
{
[_main_window resignMainWindow];
[_main_window becomeMainWindow];
[_main_window orderFrontRegardless];
[_key_window resignKeyWindow];
[_key_window becomeKeyWindow];
[_key_window orderFrontRegardless];
}
/* Register self as observer to window events. */
[nc addObserver: self selector: @selector(_windowWillClose:)
name: NSWindowWillCloseNotification object: nil];
[nc addObserver: self selector: @selector(_windowDidBecomeKey:)
name: NSWindowDidBecomeKeyNotification object: nil];
[nc addObserver: self selector: @selector(_windowDidBecomeMain:)
name: NSWindowDidBecomeMainNotification object: nil];
[nc addObserver: self selector: @selector(_windowDidResignKey:)
name: NSWindowDidResignKeyNotification object: nil];
[nc addObserver: self selector: @selector(_windowDidResignMain:)
name: NSWindowDidResignMainNotification object: nil];
[self activateIgnoringOtherApps: YES];
/*
* Now check to see if we were launched with arguments asking to
* open a file. We permit some variations on the default name.
*/
if ((filePath = [defs stringForKey: @"GSFilePath"]) != nil ||
(filePath = [defs stringForKey: @"NSOpen"]) != nil)
{
[self _openDocument: filePath];
}
else if ((filePath = [defs stringForKey: @"GSTempPath"]) != nil)
{
if ([_delegate respondsToSelector: @selector(application:openTempFile:)])
{
[_delegate application: self openTempFile: filePath];
}
else
{
// FIXME: Should remember that this is a temp file
[[NSDocumentController sharedDocumentController]
openDocumentWithContentsOfFile: filePath display: YES];
}
}
// TODO: Should also support printing of a file here.
/* finish the launching post notification that launching has finished */
[nc postNotificationName: NSApplicationDidFinishLaunchingNotification
object: self];
userInfo = [NSDictionary dictionaryWithObject:
[[NSProcessInfo processInfo] processName] forKey: @"NSApplicationName"];
NS_DURING
[[workspace notificationCenter]
postNotificationName: NSWorkspaceDidLaunchApplicationNotification
object: workspace
userInfo: userInfo];
NS_HANDLER
NSLog(GSGuiLocalizedString (@"Problem during launch app notification: %@",
nil),
[localException reason]);
[localException raise];
NS_ENDHANDLER
}
- (void) dealloc
{
NSDebugLog(@"Freeing NSApplication\n");
[nc removeObserver: self];
RELEASE(_hidden);
RELEASE(_inactive);
RELEASE(_listener);
RELEASE(null_event);
RELEASE(_current_event);
/* We may need to tidy up nested modal session structures. */
while (_session != 0)
{
NSModalSession tmp = _session;
_session = tmp->previous;
NSZoneFree(NSDefaultMallocZone(), tmp);
}
/* Release the menus, then set them to nil so we don't try updating
them after they have been deallocated. */
DESTROY(_main_menu);
DESTROY(_windows_menu);
TEST_RELEASE(_app_icon);
TEST_RELEASE(_app_icon_window);
TEST_RELEASE(_infoPanel);
/* Destroy the default context, this will free it */
[_default_context destroyContext];
[super dealloc];
}
/*
* Changing the active application
*/
- (void) activateIgnoringOtherApps: (BOOL)flag
{
// TODO: Currently the flag is ignored
if (_app_is_active == NO)
{
unsigned count = [_inactive count];
unsigned i;
/*
* Menus should observe this notification in order to make themselves
* visible when the application is active.
*/
[nc postNotificationName: NSApplicationWillBecomeActiveNotification
object: self];
NSDebugLog(@"activateIgnoringOtherApps start.");
_app_is_active = YES;
for (i = 0; i < count; i++)
{
[[_inactive objectAtIndex: i] orderFrontRegardless];
}
[_inactive removeAllObjects];
if (_hidden_key != nil
&& [[self windows] indexOfObjectIdenticalTo: _hidden_key] != NSNotFound)
{
[_hidden_key makeKeyWindow];
_hidden_key = nil;
}
[_main_menu update];
[_main_menu display];
if (_unhide_on_activation)
{
[self unhide: nil];
}
if ([self keyWindow] != nil)
{
[[self keyWindow] orderFront: self];
}
else if ([self mainWindow] != nil)
{
[[self mainWindow] orderFront: self];
}
NSDebugLog(@"activateIgnoringOtherApps end.");
[nc postNotificationName: NSApplicationDidBecomeActiveNotification
object: self];
}
}
- (void) deactivate
{
if (_app_is_active == YES)
{
NSArray *windows_list = [self windows];
unsigned count = [windows_list count];
unsigned i;
[nc postNotificationName: NSApplicationWillResignActiveNotification
object: self];
_app_is_active = NO;
if ([self keyWindow] != nil)
{
_hidden_key = [self keyWindow];
[_hidden_key resignKeyWindow];
DPSsetinputfocus(GSCurrentContext(), [_app_icon_window windowNumber]);
}
for (i = 0; i < count; i++)
{
NSModalSession theSession;
NSWindow *win = [windows_list objectAtIndex: i];
if ([win isVisible] == NO)
{
continue; /* Already invisible */
}
if (win == _app_icon_window)
{
continue; /* can't hide the app icon. */
}
/* Don't order out modal windows */
theSession = _session;
while (theSession != 0)
{
if (win == theSession->window)
break;
theSession = theSession->previous;
}
if (theSession)
continue;
if ([win hidesOnDeactivate] == YES)
{
[_inactive addObject: win];
[win orderOut: self];
}
}
[nc postNotificationName: NSApplicationDidResignActiveNotification
object: self];
}
}
- (BOOL) isActive
{
return _app_is_active;
}
- (void) hideOtherApplications: (id)sender
{
// FIXME Currently does nothing
}
- (void) unhideAllApplications: (id)sender
{
// FIXME Currently does nothing
}
/*
* Running the main event loop
*/
- (void) run
{
NSEvent *e;
id distantFuture = [NSDate distantFuture]; /* Cache this, safe */
NSDebugLog(@"NSApplication -run\n");
if (_runLoopPool != nil)
{
[NSException raise: NSInternalInconsistencyException
format: @"NSApp's run called recursively"];
}
IF_NO_GC(_runLoopPool = [arpClass new]);
/*
* Set this flag here in case the application is actually terminated
* inside -finishLaunching.
*/
_app_is_running = YES;
[self finishLaunching];
[_listener updateServicesMenu];
[_main_menu update];
DESTROY(_runLoopPool);
while (_app_is_running)
{
IF_NO_GC(_runLoopPool = [arpClass new]);
e = [self nextEventMatchingMask: NSAnyEventMask
untilDate: distantFuture
inMode: NSDefaultRunLoopMode
dequeue: YES];
if (e != nil && e != null_event)
{
NSEventType type = [e type];
[self sendEvent: e];
// update (en/disable) the services menu's items
if (type != NSPeriodic && type != NSMouseMoved)
{
[_listener updateServicesMenu];
[_main_menu update];
}
}
// send an update message to all visible windows
if (_windows_need_update)
{
[self updateWindows];
}
DESTROY (_runLoopPool);
}
/* Every single non trivial line of code must be enclosed into an
autorelease pool. Create an autorelease pool here to wrap
synchronize and the NSDebugLog. */
IF_NO_GC(_runLoopPool = [arpClass new]);
[[NSUserDefaults standardUserDefaults] synchronize];
NSDebugLog(@"NSApplication end of run loop\n");
DESTROY (_runLoopPool);
}
- (BOOL) isRunning
{
return _app_is_running;
}
/*
* Running modal event loops
*/
- (void) abortModal
{
if (_session == 0)
{
[NSException raise: NSAbortModalException
format: @"abortModal called while not in a modal session"];
}
[NSException raise: NSAbortModalException format: @"abortModal"];
}
- (NSModalSession) beginModalSessionForWindow: (NSWindow*)theWindow
{
NSModalSession theSession;
theSession = (NSModalSession)NSZoneMalloc(NSDefaultMallocZone(),
sizeof(struct _NSModalSession));
theSession->runState = NSRunContinuesResponse;
theSession->entryLevel = [theWindow level];
theSession->window = theWindow;
theSession->previous = _session;
_session = theSession;
/*
* The NSWindow documentation says runModalForWindow centers panels.
* Here would seem the best place to do it.
*/
if ([theWindow isKindOfClass: [NSPanel class]])
{
[theWindow center];
[theWindow setLevel: NSModalPanelWindowLevel];
}
[theWindow orderFrontRegardless];
if ([self isActive] == YES)
{
if ([theWindow canBecomeKeyWindow] == YES)
{
[theWindow makeKeyWindow];
}
else if ([theWindow canBecomeMainWindow] == YES)
{
[theWindow makeMainWindow];
}
}
return theSession;
}
- (void) endModalSession: (NSModalSession)theSession
{
NSModalSession tmp = _session;
NSArray *windows = [self windows];
if (theSession == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"null pointer passed to endModalSession:"];
}
/* Remove this session from linked list of sessions. */
while (tmp != 0 && tmp != theSession)
{
tmp = tmp->previous;
}
if (tmp == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"unknown session passed to endModalSession:"];
}
while (_session != theSession)
{
tmp = _session;
_session = tmp->previous;
if ([windows indexOfObjectIdenticalTo: tmp->window] != NSNotFound)
{
[tmp->window setLevel: tmp->entryLevel];
}
NSZoneFree(NSDefaultMallocZone(), tmp);
}
_session = _session->previous;
if ([windows indexOfObjectIdenticalTo: theSession->window] != NSNotFound)
{
[theSession->window setLevel: theSession->entryLevel];
}
NSZoneFree(NSDefaultMallocZone(), theSession);
}
- (int) runModalForWindow: (NSWindow*)theWindow
{
NSModalSession theSession = 0;
int code = NSRunContinuesResponse;
NS_DURING
{
theSession = [self beginModalSessionForWindow: theWindow];
while (code == NSRunContinuesResponse)
{
code = [self runModalSession: theSession];
}
[self endModalSession: theSession];
}
NS_HANDLER
{
if (theSession != 0)
{
NSWindow *win_to_close = theSession->window;
[self endModalSession: theSession];
[win_to_close close];
}
if ([[localException name] isEqual: NSAbortModalException] == NO)
{
[localException raise];
}
code = NSRunAbortedResponse;
}
NS_ENDHANDLER
return code;
}
/**
<p>
Processes one event for a modal session described by the theSession
variable. Before processing the event, it makes the session window key
and orders the window front, so there is no need to do this
separately. When finished, it returns the state of the session (i.e.
whether it is still running or has been stopped, etc)
<p>
</p>
See Also: -runModalForWindow:
</p>
*/
- (int) runModalSession: (NSModalSession)theSession
{
NSAutoreleasePool *pool;
NSGraphicsContext *ctxt;
BOOL found = NO;
NSEvent *event;
NSDate *limit;
if (theSession != _session)
{
[NSException raise: NSInvalidArgumentException
format: @"runModalSession: with wrong session"];
}
IF_NO_GC(pool = [arpClass new]);
[theSession->window orderFrontRegardless];
if ([theSession->window canBecomeKeyWindow] == YES)
{
[theSession->window makeKeyWindow];
}
else if ([theSession->window canBecomeMainWindow] == YES)
{
[theSession->window makeMainWindow];
}
// Use the default context for all events.
ctxt = _default_context;
/*
* Set a limit date in the distant future so we wait until we get an
* event. We discard events that are not for this window. When we
* find one for this window, we push it back at the start of the queue.
*/
limit = [NSDate distantFuture];
do
{
event = DPSGetEvent(ctxt, NSAnyEventMask, limit, NSDefaultRunLoopMode);
if (event != nil)
{
NSWindow *eventWindow = [event window];
if (eventWindow == theSession->window || [eventWindow worksWhenModal])
{
DPSPostEvent(ctxt, event, YES);
found = YES;
}
else if ([event type] == NSAppKitDefined)
{
/* Handle resize and other window manager events now */
[self sendEvent: event];
}
}
}
while (found == NO && theSession->runState == NSRunContinuesResponse);
RELEASE (pool);
/*
* Deal with the events in the queue.
*/
while (found == YES && theSession->runState == NSRunContinuesResponse)
{
IF_NO_GC(pool = [arpClass new]);
event = DPSGetEvent(ctxt, NSAnyEventMask, limit, NSDefaultRunLoopMode);
if (event != nil)
{
NSWindow *eventWindow = [event window];
if (eventWindow == theSession->window || [eventWindow worksWhenModal])
{
ASSIGN(_current_event, event);
}
else
{
found = NO;
}
}
else
{
found = NO;
}
if (found == YES)
{
NSEventType type = [_current_event type];
[self sendEvent: _current_event];
// update (en/disable) the services menu's items
if (type != NSPeriodic && type != NSMouseMoved)
{
[_listener updateServicesMenu];
[_main_menu update];
}
/*
* Check to see if the window has gone away - if so, end session.
*/
if ([[self windows] indexOfObjectIdenticalTo: _session->window] ==
NSNotFound)
{
[self stopModal];
}
if (_windows_need_update)
{
[self updateWindows];
}
}
RELEASE (pool);
}
NSAssert(_session == theSession, @"Session was changed while running");
return theSession->runState;
}
/**
<p>
Returns the window that is part of the current modal session, if any.
<p>
</p>
See -runModalForWindow:
</p>
*/
- (NSWindow *) modalWindow
{
if (_session != 0)
return (_session->window);
else
return nil;
}
- (void) stop: (id)sender
{
if (_session != 0)
[self stopModal];
else
{
_app_is_running = NO;
/*
* add dummy event to queue to assure loop cycles
* at least one more time
*/
DPSPostEvent(_default_context, null_event, NO);
}
}
- (void) stopModal
{
[self stopModalWithCode: NSRunStoppedResponse];
}
- (void) stopModalWithCode: (int)returnCode
{
if (_session == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"stopModalWithCode: when not in a modal session"];
}
else if (returnCode == NSRunContinuesResponse)
{
[NSException raise: NSInvalidArgumentException
format: @"stopModalWithCode: with NSRunContinuesResponse"];
}
_session->runState = returnCode;
}
- (int) runModalForWindow: (NSWindow *)theWindow
relativeToWindow: (NSWindow *)docWindow
{
// FIXME
return [self runModalForWindow: theWindow];
}
- (void) beginSheet: (NSWindow *)sheet
modalForWindow: (NSWindow *)docWindow
modalDelegate: (id)modalDelegate
didEndSelector: (SEL)didEndSelector
contextInfo: (void *)contextInfo
{
// FIXME
int ret;
ret = [self runModalForWindow: sheet
relativeToWindow: docWindow];
if ([modalDelegate respondsToSelector: didEndSelector])
// FIXME Those this work on all platforms???
[modalDelegate performSelector: didEndSelector
withObject: (NSObject*)ret
withObject: contextInfo];
}
- (void) endSheet: (NSWindow *)sheet
{
// FIXME
[self stopModal];
}
- (void) endSheet: (NSWindow *)sheet
returnCode: (int)returnCode
{
// FIXME
[self stopModalWithCode: returnCode];
}
/*
* Getting, removing, and posting events
*/
- (void) sendEvent: (NSEvent *)theEvent
{
NSEventType type;
type = [theEvent type];
switch (type)
{
case NSPeriodic: /* NSApplication traps the periodic events */
break;
case NSKeyDown:
{
NSDebugLLog(@"NSEvent", @"send key down event\n");
if ([theEvent modifierFlags] & NSCommandKeyMask)
{
NSArray *window_list = [self windows];
unsigned i;
unsigned count = [window_list count];
for (i = 0; i < count; i++)
{
NSWindow *window = [window_list objectAtIndex: i];
if ([window performKeyEquivalent: theEvent] == YES)
break;
}
}
else
[[theEvent window] sendEvent: theEvent];
break;
}
case NSKeyUp:
{
NSDebugLLog(@"NSEvent", @"send key up event\n");
[[theEvent window] sendEvent: theEvent];
break;
}
default: /* pass all other events to the event's window */
{
NSWindow *window = [theEvent window];
if (!theEvent)
NSDebugLLog(@"NSEvent", @"NSEvent is nil!\n");
if (type == NSMouseMoved)
NSDebugLLog(@"NSMotionEvent", @"Send move (%d) to window %@",
type, ((window != nil) ? [window description]
: @"No window"));
else
NSDebugLLog(@"NSEvent", @"Send NSEvent type: %d to window %@",
type, ((window != nil) ? [window description]
: @"No window"));
if (window)
[window sendEvent: theEvent];
else if (type == NSRightMouseDown)
[self rightMouseDown: theEvent];
}
}
}
- (NSEvent*) currentEvent
{
return _current_event;
}
- (void) discardEventsMatchingMask: (unsigned int)mask
beforeEvent: (NSEvent *)lastEvent
{
DPSDiscardEvents(_default_context, mask, lastEvent);
}
- (NSEvent*) nextEventMatchingMask: (unsigned int)mask
untilDate: (NSDate*)expiration
inMode: (NSString*)mode
dequeue: (BOOL)flag
{
NSEvent *event;
if (!expiration)
expiration = [NSDate distantFuture];
if (flag)
event = DPSGetEvent(_default_context, mask, expiration, mode);
else
event = DPSPeekEvent(_default_context, mask, expiration, mode);
if (event)
{
IF_NO_GC(NSAssert([event retainCount] > 0, NSInternalInconsistencyException));
/*
* If we are not in a tracking loop, we may want to unhide a hidden
* because the mouse has been moved.
*/
if (mode != NSEventTrackingRunLoopMode)
{
if ([NSCursor isHiddenUntilMouseMoves])
{
NSEventType type = [event type];
if ((type == NSLeftMouseDown) || (type == NSLeftMouseUp)
|| (type == NSOtherMouseDown) || (type == NSOtherMouseUp)
|| (type == NSRightMouseDown) || (type == NSRightMouseUp)
|| (type == NSMouseMoved))
{
[NSCursor unhide];
}
}
}
ASSIGN(_current_event, event);
}
return event;
}
- (void) postEvent: (NSEvent *)event atStart: (BOOL)flag
{
DPSPostEvent(_default_context, event, flag);
}
/*
* Sending action messages
*/
- (BOOL) sendAction: (SEL)aSelector to: aTarget from: sender
{
/*
* If target responds to the selector then have it perform it.
*/
if (aTarget && [aTarget respondsToSelector: aSelector])
{
[aTarget performSelector: aSelector withObject: sender];
return YES;
}
else
{
id resp = [self targetForAction: aSelector];
if (resp)
{
[resp performSelector: aSelector withObject: sender];
return YES;
}
}
return NO;
}
- (id)targetForAction:(SEL)theAction to:(id)theTarget from:(id)sender
{
// TODO: This is not fully documented, if it ever gets, we should
// call this in sendAction:to:from:
if (theTarget && [theTarget respondsToSelector: theAction])
{
return theTarget;
}
else
{
return [self targetForAction: theAction];
}
}
/**
<p>
Returns the target object that will respond to aSelector, if any. The
method first checks if any of the key window's first responders, the
key window or its delegate responds. Next it checks the main window in
the same way. Finally it checks the receiver (NSApplication) and it's
delegate.
</p>
*/
- (id) targetForAction: (SEL)aSelector
{
NSWindow *keyWindow;
NSWindow *mainWindow;
id resp;
keyWindow = [self keyWindow];
if (keyWindow != nil)
{
resp = [keyWindow firstResponder];
while (resp != nil && resp != keyWindow)
{
if ([resp respondsToSelector: aSelector])
{
return resp;
}
resp = [resp nextResponder];
}
if ([keyWindow respondsToSelector: aSelector])
{
return keyWindow;
}
resp = [keyWindow delegate];
if (resp != nil && [resp respondsToSelector: aSelector])
{
return resp;
}
}
if (_session != 0)
return nil;
mainWindow = [self mainWindow];
if (keyWindow != mainWindow && mainWindow != nil)
{
resp = [mainWindow firstResponder];
while (resp != nil && resp != mainWindow)
{
if ([resp respondsToSelector: aSelector])
{
return resp;
}
resp = [resp nextResponder];
}
if ([mainWindow respondsToSelector: aSelector])
{
return mainWindow;
}
resp = [mainWindow delegate];
if (resp != nil && [resp respondsToSelector: aSelector])
{
return resp;
}
}
if ([self respondsToSelector: aSelector])
{
return self;
}
if (_delegate != nil && [_delegate respondsToSelector: aSelector])
{
return _delegate;
}
return nil;
}
- (BOOL) tryToPerform: (SEL)aSelector with: (id)anObject
{
if ([super tryToPerform: aSelector with: anObject] == YES)
{
return YES;
}
if (_delegate != nil && [_delegate respondsToSelector: aSelector])
{
[_delegate performSelector: aSelector withObject: anObject];
return YES;
}
return NO;
}
// Set the app's icon
- (void) setApplicationIconImage: (NSImage*)anImage
{
[_app_icon setName: nil];
[anImage setName: @"NSApplicationIcon"];
ASSIGN(_app_icon, anImage);
if (_app_icon_window != nil)
{
[[_app_icon_window contentView] setImage: anImage];
}
}
- (NSImage*) applicationIconImage
{
return _app_icon;
}
- (NSWindow*) iconWindow
{
return _app_icon_window;
}
/*
* Hiding and arranging windows
*/
- (void) hide: (id)sender
{
if (_app_is_hidden == NO)
{
NSArray *windows_list = [self windows];
unsigned count = [windows_list count];
unsigned i;
[nc postNotificationName: NSApplicationWillHideNotification
object: self];
if ([self keyWindow] != nil)
{
_hidden_key = [self keyWindow];
[_hidden_key resignKeyWindow];
DPSsetinputfocus(GSCurrentContext(), [_app_icon_window windowNumber]);
}
for (i = 0; i < count; i++)
{
NSWindow *win = [windows_list objectAtIndex: i];
if ([win isVisible] == NO)
{
continue; /* Already invisible */
}
if (win == _app_icon_window)
{
continue; /* can't hide the app icon. */
}
if (_app_is_active == YES && [win hidesOnDeactivate] == YES)
{
continue; /* Will be hidden by deactivation */
}
[_hidden addObject: win];
[win orderOut: self];
}
_app_is_hidden = YES;
/*
* On hiding we also deactivate the application which will make the menus
* go away too.
*/
[self deactivate];
_unhide_on_activation = YES;
[nc postNotificationName: NSApplicationDidHideNotification
object: self];
}
}
- (BOOL) isHidden
{
return _app_is_hidden;
}
- (void) unhide: (id)sender
{
if (_app_is_hidden)
{
[self unhideWithoutActivation];
_unhide_on_activation = NO;
}
if (_app_is_active == NO)
{
/*
* Activation should make the applications menus visible.
*/
[self activateIgnoringOtherApps: YES];
}
}
- (void) unhideWithoutActivation
{
if (_app_is_hidden == YES)
{
unsigned count;
unsigned i;
[nc postNotificationName: NSApplicationWillUnhideNotification
object: self];
count = [_hidden count];
for (i = 0; i < count; i++)
{
[[_hidden objectAtIndex: i] orderFrontRegardless];
}
[_hidden removeAllObjects];
if (_hidden_key != nil
&& [[self windows] indexOfObjectIdenticalTo: _hidden_key] != NSNotFound)
{
[_hidden_key makeKeyAndOrderFront: self];
_hidden_key = nil;
}
_app_is_hidden = NO;
[nc postNotificationName: NSApplicationDidUnhideNotification
object: self];
}
}
- (void) arrangeInFront: (id)sender
{
NSMenu *menu;
menu = [self windowsMenu];
if (menu)
{
NSArray *itemArray;
unsigned count;
unsigned i;
itemArray = [menu itemArray];
count = [itemArray count];
for (i = 0; i < count; i++)
{
id win = [[itemArray objectAtIndex: i] target];
if ([win isKindOfClass: [NSWindow class]])
{
[win orderFront: sender];
}
}
}
}
/*
* Managing windows
*/
- (NSWindow*) keyWindow
{
return _key_window;
}
- (NSWindow*) mainWindow
{
return _main_window;
}
- (NSWindow*) makeWindowsPerform: (SEL)aSelector inOrder: (BOOL)flag
{
NSArray *window_list = [self windows];
unsigned i;
if (flag)
{
unsigned count = [window_list count];
for (i = 0; i < count; i++)
{
NSWindow *window = [window_list objectAtIndex: i];
if ([window performSelector: aSelector] != nil)
{
return window;
}
}
}
else
{
i = [window_list count];
while (i-- > 0)
{
NSWindow *window = [window_list objectAtIndex: i];
if ([window performSelector: aSelector] != nil)
{
return window;
}
}
}
return nil;
}
- (void) miniaturizeAll: sender
{
NSArray *window_list = [self windows];
unsigned i, count;
for (i = 0, count = [window_list count]; i < count; i++)
[[window_list objectAtIndex: i] miniaturize: sender];
}
- (void) preventWindowOrdering
{
//TODO
}
- (void) setWindowsNeedUpdate: (BOOL)flag
{
_windows_need_update = flag;
}
- (void) updateWindows
{
NSArray *window_list = [self windows];
unsigned count = [window_list count];
unsigned i;
_windows_need_update = NO;
[nc postNotificationName: NSApplicationWillUpdateNotification object: self];
for (i = 0; i < count; i++)
{
NSWindow *win = [window_list objectAtIndex: i];
if ([win isVisible])
[win update];
}
[nc postNotificationName: NSApplicationDidUpdateNotification object: self];
}
- (NSArray*) windows
{
return GSAllWindows();
}
- (NSWindow *) windowWithWindowNumber: (int)windowNum
{
return GSWindowWithNumber(windowNum);
}
/*
* Showing Standard Panels
*/
- (void) orderFrontColorPanel: sender
{
NSColorPanel *colorPanel = [NSColorPanel sharedColorPanel];
if (colorPanel)
[colorPanel orderFront: nil];
else
NSBeep();
}
- (void) orderFrontDataLinkPanel: sender
{
NSDataLinkPanel *dataLinkPanel = [NSDataLinkPanel sharedDataLinkPanel];
if (dataLinkPanel)
[dataLinkPanel orderFront: nil];
else
NSBeep();
}
- (void) orderFrontHelpPanel: sender
{
// This is implemented in NSHelpManager.m
[self showHelp: sender];
}
- (void) runPageLayout: sender
{
[[NSPageLayout pageLayout] runModal];
}
/* infoPanel, macosx API -- Deprecated */
- (void) orderFrontStandardAboutPanel: sender
{
[self orderFrontStandardInfoPanel: sender];
}
- (void) orderFrontStandardAboutPanelWithOptions: (NSDictionary *)dictionary
{
[self orderFrontStandardInfoPanelWithOptions: dictionary];
}
/* infoPanel, GNUstep API */
- (void) orderFrontStandardInfoPanel: sender
{
[self orderFrontStandardInfoPanelWithOptions: nil];
}
- (void) orderFrontStandardInfoPanelWithOptions: (NSDictionary *)dictionary
{
if (_infoPanel == nil)
_infoPanel = [[GSInfoPanel alloc] initWithDictionary: dictionary];
[_infoPanel setTitle: GSGuiLocalizedString (@"Info",
@"Title of the Info Panel")];
[_infoPanel orderFront: self];
}
/*
* Getting the main menu
*/
- (NSMenu*) mainMenu
{
return _main_menu;
}
- (void) setMainMenu: (NSMenu*)aMenu
{
if (_main_menu != nil && _main_menu != aMenu)
{
[_main_menu close];
[[_main_menu window] setLevel: NSSubmenuWindowLevel];
}
ASSIGN(_main_menu, aMenu);
[_main_menu setTitle: [[NSProcessInfo processInfo] processName]];
// Set the title of the window also.
// This wont be displayed, but the window manager may need it.
[[_main_menu window] setTitle: [[NSProcessInfo processInfo] processName]];
[[_main_menu window] setLevel: NSMainMenuWindowLevel];
[_main_menu sizeToFit];
if ([self isActive])
{
[_main_menu update];
[_main_menu display];
}
}
- (void) rightMouseDown: (NSEvent*)theEvent
{
// On right mouse down display the main menu transient
if (_main_menu != nil)
[_main_menu _rightMouseDisplay: theEvent];
else
[super rightMouseDown: theEvent];
}
- (void) setAppleMenu: (NSMenu*)aMenu
{
//TODO: Unclear, what this should do.
}
/*
* Managing the Windows menu
*/
- (void) addWindowsItem: (NSWindow*)aWindow
title: (NSString*)aString
filename: (BOOL)isFilename
{
[self changeWindowsItem: aWindow title: aString filename: isFilename];
}
- (void) changeWindowsItem: (NSWindow*)aWindow
title: (NSString*)aString
filename: (BOOL)isFilename
{
NSArray *itemArray;
unsigned count;
unsigned i;
id item;
if (![aWindow isKindOfClass: [NSWindow class]])
[NSException raise: NSInvalidArgumentException
format: @"Object of bad type passed as window"];
if (isFilename)
{
NSRange r = [aString rangeOfString: @" -- "];
if (r.length > 0)
{
aString = [aString substringToIndex: r.location];
}
}
/*
* If there is no menu and nowhere to put one, we can't do anything.
*/
if (_windows_menu == nil)
return;
/*
* Check if the window is already in the menu.
*/
itemArray = [_windows_menu itemArray];
count = [itemArray count];
for (i = 0; i < count; i++)
{
id item = [itemArray objectAtIndex: i];
if ([item target] == aWindow)
{
/*
* If our menu item already exists and with the correct
* title, we need not continue.
*/
if ([[item title] isEqualToString: aString])
{
return;
}
else
{
/*
* Else, we need to remove the old item and add it again
* with the new title. Then new item might be located
* somewhere else in the menu than the old one (because
* items in the menu are sorted by title) ... this is
* why we remove the old one and then insert it again.
*/
[_windows_menu removeItem: item];
break;
}
}
}
/*
* Can't permit an untitled window in the window menu ... so if the
* window has not title, we don't add it to the menu.
*/
if (aString == nil || [aString isEqualToString: @""])
return;
/*
* Now we insert a menu item for the window in the correct order.
* Make special allowance for menu entries to 'arrangeInFront: '
* 'performMiniaturize: ' and 'performClose: '. If these exist the
* window entries should stay after the first one and before the
* other two.
*/
itemArray = [_windows_menu itemArray];
count = [itemArray count];
i = 0;
if (count > 0 && sel_eq([[itemArray objectAtIndex: 0] action],
@selector(arrangeInFront:)))
i++;
if (count > i && sel_eq([[itemArray objectAtIndex: count-1] action],
@selector(performClose:)))
count--;
if (count > i && sel_eq([[itemArray objectAtIndex: count-1] action],
@selector(performMiniaturize:)))
count--;
while (i < count)
{
item = [itemArray objectAtIndex: i];
if ([[item title] compare: aString] == NSOrderedDescending)
break;
i++;
}
item = [_windows_menu insertItemWithTitle: aString
action: @selector(makeKeyAndOrderFront:)
keyEquivalent: @""
atIndex: i];
[item setTarget: aWindow];
// TODO: When changing for a window with a file, we should also set the image.
}
- (void) removeWindowsItem: (NSWindow*)aWindow
{
if (_windows_menu)
{
NSArray *itemArray;
unsigned count;
itemArray = [_windows_menu itemArray];
count = [itemArray count];
while (count-- > 0)
{
id item = [itemArray objectAtIndex: count];
if ([item target] == aWindow)
{
[_windows_menu removeItemAtIndex: count];
return;
}
}
}
}
- (void) setWindowsMenu: (NSMenu*)aMenu
{
if (_windows_menu == aMenu)
{
return;
}
/*
* Remove all the windows from the old windows menu.
*/
if (_windows_menu != nil)
{
NSArray *itemArray = [_windows_menu itemArray];
unsigned i, count = [itemArray count];
for (i = 0; i < count; i++)
{
NSMenuItem *anItem = [itemArray objectAtIndex: i];
id win = [anItem target];
if ([win isKindOfClass: [NSWindow class]])
{
[_windows_menu removeItem: anItem];
}
}
}
/* Set the new _windows_menu. */
ASSIGN (_windows_menu, aMenu);
{
/*
* Now use [-changeWindowsItem:title:filename:] to build the new menu.
*/
NSArray * windows = [self windows];
unsigned i, count = [windows count];
for (i = 0; i < count; i++)
{
NSWindow *win = [windows objectAtIndex: i];
if ([win isExcludedFromWindowsMenu] == NO)
{
NSString *t = [win title];
NSString *f = [win representedFilename];
[self changeWindowsItem: win
title: t
filename: [t isEqual: f]];
}
}
}
}
- (void) updateWindowsItem: (NSWindow*)aWindow
{
NSMenu *menu;
NSMenuView *view;
menu = [self windowsMenu];
if (menu != nil)
{
NSArray *itemArray;
unsigned count;
unsigned i;
BOOL found = NO;
view = [menu menuRepresentation];
itemArray = [menu itemArray];
count = [itemArray count];
for (i = 0; i < count; i++)
{
id item = [itemArray objectAtIndex: i];
if ([item target] == aWindow)
{
NSMenuItemCell *cell;
NSCellImagePosition oldPos;
NSImage *oldImage;
NSImage *newImage;
BOOL changed;
found = YES;
cell = [view menuItemCellForItemAtIndex: i];
oldPos = [cell imagePosition];
oldImage = [cell image];
newImage = oldImage;
changed = NO;
if (oldPos != NSImageLeft)
{
[cell setImagePosition: NSImageLeft];
changed = YES;
}
if ([aWindow isDocumentEdited])
{
newImage = [NSImage imageNamed: @"common_WMCloseBroken"];
}
else
{
newImage = [NSImage imageNamed: @"common_WMClose"];
}
if (newImage != oldImage)
{
[item setImage: newImage];
[cell setImage: newImage];
changed = YES;
}
if (changed)
{
[menu sizeToFit];
[view setNeedsDisplayForItemAtIndex: i];
}
break;
}
}
if (found == NO)
{
NSString *t = [aWindow title];
NSString *f = [aWindow representedFilename];
[self changeWindowsItem: aWindow
title: t
filename: [t isEqual: f]];
}
}
}
- (NSMenu*) windowsMenu
{
return _windows_menu;
}
/*
* Managing the Service menu
*/
- (void) registerServicesMenuSendTypes: (NSArray *)sendTypes
returnTypes: (NSArray *)returnTypes
{
[_listener registerSendTypes: sendTypes
returnTypes: returnTypes];
}
- (NSMenu *) servicesMenu
{
return [_listener servicesMenu];
}
- (id) servicesProvider
{
return [_listener servicesProvider];
}
- (void) setServicesMenu: (NSMenu *)aMenu
{
[_listener setServicesMenu: aMenu];
}
- (void) setServicesProvider: (id)anObject
{
[_listener setServicesProvider: anObject];
}
- (id) validRequestorForSendType: (NSString *)sendType
returnType: (NSString *)returnType
{
if (_delegate != nil && ![_delegate isKindOfClass: [NSResponder class]] &&
[_delegate respondsToSelector: @selector(validRequestorForSendType:returnType:)])
return [_delegate validRequestorForSendType: sendType
returnType: returnType];
return nil;
}
- (NSGraphicsContext *) context
{
return _default_context;
}
- (void) reportException: (NSException *)anException
{
if (anException)
NSLog(GSGuiLocalizedString (@"reported exception - %@", nil), anException);
}
/*
* Terminating the application
*/
- (void) terminate: (id)sender
{
BOOL shouldTerminate = YES;
if ([_delegate respondsToSelector: @selector(applicationShouldTerminate:)])
{
shouldTerminate = [_delegate applicationShouldTerminate: sender];
}
else
{
shouldTerminate = [[NSDocumentController sharedDocumentController]
reviewUnsavedDocumentsWithAlertTitle:
GSGuiLocalizedString (@"Quit", nil)
cancellable:YES];
}
if (shouldTerminate)
{
NSDictionary *userInfo;
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[nc postNotificationName: NSApplicationWillTerminateNotification
object: self];
_app_is_running = NO;
[[self windows] makeObjectsPerformSelector: @selector(close)];
/* Store our user information. */
[[NSUserDefaults standardUserDefaults] synchronize];
/* Tell the Workspace that we really did terminate. */
userInfo = [NSDictionary dictionaryWithObject:
[[NSProcessInfo processInfo] processName] forKey:
@"NSApplicationName"];
[[workspace notificationCenter]
postNotificationName: NSWorkspaceDidTerminateApplicationNotification
object: workspace
userInfo: userInfo];
/* Destroy the main run loop pool (this also destroys any nested
pools which might have been created inside this one). */
DESTROY (_runLoopPool);
/* Now free the NSApplication object. Enclose the operation
into an autorelease pool, in case some -dealloc method needs
to use any temporary object. */
{
NSAutoreleasePool *pool;
IF_NO_GC(pool = [arpClass new]);
DESTROY(NSApp);
DESTROY(pool);
}
/* And finally, stop the program. */
exit(0);
}
}
- (id) delegate
{
return _delegate;
}
- (void) setDelegate: (id)anObject
{
if (_delegate)
[nc removeObserver: _delegate name: nil object: self];
_delegate = anObject;
#define SET_DELEGATE_NOTIFICATION(notif_name) \
if ([_delegate respondsToSelector: @selector(application##notif_name:)]) \
[nc addObserver: _delegate \
selector: @selector(application##notif_name:) \
name: NSApplication##notif_name##Notification object: self]
SET_DELEGATE_NOTIFICATION(DidBecomeActive);
SET_DELEGATE_NOTIFICATION(DidFinishLaunching);
SET_DELEGATE_NOTIFICATION(DidHide);
SET_DELEGATE_NOTIFICATION(DidResignActive);
SET_DELEGATE_NOTIFICATION(DidUnhide);
SET_DELEGATE_NOTIFICATION(DidUpdate);
SET_DELEGATE_NOTIFICATION(WillBecomeActive);
SET_DELEGATE_NOTIFICATION(WillFinishLaunching);
SET_DELEGATE_NOTIFICATION(WillHide);
SET_DELEGATE_NOTIFICATION(WillResignActive);
SET_DELEGATE_NOTIFICATION(WillTerminate);
SET_DELEGATE_NOTIFICATION(WillUnhide);
SET_DELEGATE_NOTIFICATION(WillUpdate);
}
/*
* NSCoding protocol
*/
- (void) encodeWithCoder: (NSCoder*)aCoder
{
[super encodeWithCoder: aCoder];
[aCoder encodeConditionalObject: _delegate];
[aCoder encodeObject: _main_menu];
[aCoder encodeConditionalObject: _windows_menu];
}
- (id) initWithCoder: (NSCoder*)aDecoder
{
id obj;
[super initWithCoder: aDecoder];
obj = [aDecoder decodeObject];
[self setDelegate: obj];
obj = [aDecoder decodeObject];
[self setMainMenu: obj];
obj = [aDecoder decodeObject];
[self setWindowsMenu: obj];
return self;
}
@end /* NSApplication */
@implementation NSApplication (Private)
- _appIconInit
{
NSAppIconView *iv;
if (_app_icon == nil)
_app_icon = RETAIN([NSImage imageNamed: @"GNUstep"]);
_app_icon_window = [[NSIconWindow alloc] initWithContentRect:
NSMakeRect(0,0,64,64)
styleMask: NSIconWindowMask
backing: NSBackingStoreRetained
defer: NO
screen: nil];
iv = [[NSAppIconView alloc] initWithFrame: NSMakeRect(0,0,64,64)];
[iv setImage: _app_icon];
[_app_icon_window setContentView: iv];
RELEASE(iv);
[_app_icon_window orderFrontRegardless];
DPSsetinputfocus(GSCurrentContext(), [_app_icon_window windowNumber]);
return self;
}
- (void) _openDocument: (NSString*)filePath
{
if ([_delegate respondsToSelector: @selector(application:openFile:)])
{
[_delegate application: self openFile: filePath];
}
else
{
[[NSDocumentController sharedDocumentController]
openDocumentWithContentsOfFile: filePath display: YES];
}
}
- (void) _windowDidBecomeKey: (NSNotification*) notification
{
id obj = [notification object];
if (_key_window == nil && [obj isKindOfClass: [NSWindow class]])
{
_key_window = obj;
}
else
{
NSLog(@"Bogus attempt to set key window");
}
}
- (void) _windowDidBecomeMain: (NSNotification*) notification
{
id obj = [notification object];
if (_main_window == nil && [obj isKindOfClass: [NSWindow class]])
{
_main_window = obj;
}
else
{
NSLog(@"Bogus attempt to set main window");
}
}
- (void) _windowDidResignKey: (NSNotification*) notification
{
id obj = [notification object];
if (_key_window == obj)
{
_key_window = nil;
}
else
{
NSLog(@"Bogus attempt to resign key window");
}
}
- (void) _windowDidResignMain: (NSNotification*) notification
{
id obj = [notification object];
if (_main_window == obj)
{
_main_window = nil;
}
else
{
NSLog(@"Bogus attempt to resign key window");
}
}
- (void) _windowWillClose: (NSNotification*) notification
{
NSWindow *win = [notification object];
NSArray *windows_list = [self windows];
unsigned count = [windows_list count];
unsigned i;
NSMutableArray *list = [NSMutableArray arrayWithCapacity: count];
BOOL wasKey = [win isKeyWindow];
BOOL wasMain = [win isMainWindow];
for (i = 0; i < count; i++)
{
NSWindow *tmp = [windows_list objectAtIndex: i];
if ([tmp canBecomeMainWindow] == YES && [tmp isVisible] == YES)
{
[list addObject: tmp];
}
}
[list removeObjectIdenticalTo: win];
count = [list count];
/* If there's only one window left, and that's the one being closed,
then we ask the delegate if the app is to be terminated. */
if (wasMain && count == 0 && _app_is_running)
{
NSDebugLog(@"asking delegate whether to terminate app...");
if ([_delegate respondsToSelector:
@selector(applicationShouldTerminateAfterLastWindowClosed:)])
{
if ([_delegate applicationShouldTerminateAfterLastWindowClosed: self])
{
[self terminate: self];
}
}
}
if (wasMain == YES)
{
[win resignMainWindow];
}
if (wasKey == YES)
{
[win resignKeyWindow];
}
if (_app_is_running)
{
/*
* If we are not quitting, we may need to find a new key/main window.
*/
if (wasKey == YES && [self keyWindow] == nil)
{
win = [self mainWindow];
if (win != nil && [win canBecomeKeyWindow] == YES)
{
/*
* We have a main window that can become key, so do it.
*/
[win makeKeyAndOrderFront: self];
}
else if (win != nil)
{
/*
* We have a main window that can't become key, so we just
* find a new window to make into our key window.
*/
for (i = 0; i < count; i++)
{
win = [list objectAtIndex: i];
if ([win canBecomeKeyWindow] == YES)
{
[win makeKeyAndOrderFront: self];
}
}
}
else
{
/*
* Find a window that can be made key and main - and do it.
*/
for (i = 0; i < count; i++)
{
win = [list objectAtIndex: i];
if ([win canBecomeKeyWindow] && [win canBecomeMainWindow])
{
break;
}
}
if (i < count)
{
[win makeMainWindow];
[win makeKeyAndOrderFront: self];
}
else
{
/*
* No window we can use, so just find any candidate to
* be main window and another to be key window.
*/
for (i = 0; i < count; i++)
{
win = [list objectAtIndex: i];
if ([win canBecomeMainWindow] == YES)
{
[win makeMainWindow];
break;
}
}
for (i = 0; i < count; i++)
{
win = [list objectAtIndex: i];
if ([win canBecomeKeyWindow] == YES)
{
[win makeKeyAndOrderFront: self];
break;
}
}
}
}
}
else if ([self mainWindow] == nil)
{
win = [self keyWindow];
if ([win canBecomeMainWindow] == YES)
{
[win makeMainWindow];
}
else
{
for (i = 0; i < count; i++)
{
win = [list objectAtIndex: i];
if ([win canBecomeMainWindow] == YES)
{
[win makeMainWindow];
break;
}
}
}
}
/*
* If the app has no key window - we must make sure the icon window
* has keyboard focus, even though it doesn't actually use kb events.
*/
if ([self keyWindow] == nil)
{
DPSsetinputfocus(GSCurrentContext(), [_app_icon_window windowNumber]);
}
}
}
@end // NSApplication (Private)
|