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
|
/** <title>NSAlert</title>
<abstract>Encapsulate an alert panel</abstract>
Copyright <copy>(C) 1998, 2000, 2004 Free Software Foundation, Inc.</copy>
Author: Fred Kiefer <FredKiefer@gmx.de>
Date: July 2004
GSAlertPanel and alert panel functions implementation
Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
Date: 1998
GSAlertPanel and alert panel functions cleanup and improvements (scroll view)
Author: Pascal J. Bourguignon <pjb@imaginet.fr>>
Date: 2000-03-08
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 Lesser 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; see the file COPYING.LIB.
If not, see <http://www.gnu.org/licenses/> or write to the
Free Software Foundation, 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#import "config.h"
#import <Foundation/NSDebug.h>
#import <Foundation/NSBundle.h>
#import <Foundation/NSError.h>
#import <Foundation/NSNotification.h>
#import <Foundation/NSString.h>
#import <Foundation/NSThread.h>
#import "AppKit/NSAlert.h"
#import "AppKit/NSApplication.h"
#import "AppKit/NSAttributedString.h"
#import "AppKit/NSBox.h"
#import "AppKit/NSBrowser.h"
#import "AppKit/NSBrowserCell.h"
#import "AppKit/NSButton.h"
#import "AppKit/NSEvent.h"
#import "AppKit/NSFont.h"
#import "AppKit/NSHelpManager.h"
#import "AppKit/NSImage.h"
#import "AppKit/NSMatrix.h"
#import "AppKit/NSPanel.h"
#import "AppKit/NSScreen.h"
#import "AppKit/NSScroller.h"
#import "AppKit/NSScrollView.h"
#import "AppKit/NSStringDrawing.h"
#import "AppKit/NSTextField.h"
#import "AppKit/NSTextView.h"
#import "GNUstepGUI/GMAppKit.h"
#import "GNUstepGUI/GMArchiver.h"
#import "GSGuiPrivate.h"
extern NSThread *GSAppKitThread;
static NSNotificationCenter *nc = nil;
#ifdef ALERT_TITLE
static NSString *defaultTitle = @"Alert";
#else
static NSString *defaultTitle = @" ";
#endif
/*
+--------------------------------------------------------------------------+
|############################Title bar#####################################|
+--------------------------------------------------------------------------+
| | | | | |
| ........... | | | |
| : | : | | | |
|--: Icon | :----Title | | |
| :-------|-: | | |
| :.........: | | |
| | | |
|-===========================|=============~~~~~~=========================-|
| s | |
| s | |
| ...................s....|......................................... |
| : Message s s ; |
| : s s : |
| : s s : |
|----: s s :----|
| :~~~~~~~~~~~~~~~~~~s~~~~~~~~~~~~~~~~~~~~s~~~~~~~~~~~~~~~~~~~~~~~~: |
| : s s : |
| :..................s.............................................: |
| | s |
| | s +-----------+ +-----------+ +-----------+ |
| | s | Altern |---| Cancel |---| OK |----|
| | s +-----------+ +-----------+ +-----------+ |
| | s | | | |
+--------------------------------------------------------------------------+
Apart from the buttons and window borders, '|' and '-' mean not
flexible, while '~' and 's' mean flexible.
The global window size is determined by the message text field
size. which is computed with sizeToFit. However, if the window
would become larger than the screen, then the message text field
is replaced with a scroll view containing the text.
The strategy taken in this new version of GSAlertPanel is to let
the icon, the title field and the line (box view) being placed
automatically by resizing the window, but to always place
explicitely the message field (or scroll view when it's needed)
and the buttons (that may change of width), the whole in the
sizeToFitIfNeeded method. We're doing it separately from the
setting of the elements (setTitle: ...) because we also need to
recompute the size of the window and position of the elements when
dearchiving a panel, because it could be dearchived and displayed
on a much smaller screen than originaly created, in which case we
needed to embed the message field into a scroll view, and reduce
the size of the window.
Some rules (which are implemented in sizePanelToFit):
=====================================================
IF the messageField is too big either vertically or horizontally and
would make the window greater than the screen in any direction,
THEN use a scroll view.
The width of the window content rect is the minimum of:
= the width of the content rect of a window filling the screen
= the maximum of:
= a minimum width of 362,
= the messageField width + 2 = MessageHorzMargin,
= the sum of sizes of the buttons and their interspaces and margins,
= the sum of sizes of the icon, title and their interspaces
and margin.
The height of the window content rect is the minimum of:
= the height of the content rect of a window filling the screen
= the maximum of:
= a minimum height of 161.
= the sum of:
= The height of the icon, the line and their interspaces.
= The height of the messageField and its interspaces, if present.
= The height of the buttons and their interspaces, if present.
The size of the scroll view must be at minimum ScrollMinSize
in each direction.
The size of the messageField is a given (sizeToFit).
The height of the scroll is the rest of the height of the content rect
minus the height of the other elements.
The width of the scroll is the rest of the width of the content rect
minus the margins.
In order to prevent alert panels from obscuring the whole screen, we
limit the width and height of a panel to at most a factor of SIZE_SCALE
of the screen size. At present, we use 60% for SIZE_SCALE, which means
that the limit is greater than the minimum width and height even on a
640x400 screen.
((wsize.width <= ssize.width * SIZE_SCALE)
and ([messageField frame].size.width+2*MessageHorzMargin <= wsize.width))
or ((wsize.width == ssize.width * SIZE_SCALE)
and ([scroll frame].size.width = wsize.width-2*MessageHorzMargin));
...
*/
@class GSAlertPanel;
static GSAlertPanel *standardAlertPanel = nil;
static GSAlertPanel *informationalAlertPanel = nil;
static GSAlertPanel *criticalAlertPanel = nil;
@interface GSAlertPanel: NSPanel
{
NSButton *defButton;
NSButton *altButton;
NSButton *othButton;
NSButton *icoButton;
NSTextField *titleField;
NSTextField *messageField;
NSScrollView *scroll;
NSInteger result;
BOOL isGreen; // we were unarchived and not resized.
}
- (id) _initWithoutGModel;
- (NSInteger) runModal;
- (void) setTitleBar: (NSString*)titleBar
icon: (NSImage*)icon
title: (NSString*)title
message: (NSString*)message;
- (void) setTitleBar: (NSString*)titleBar
icon: (NSImage*)icon
title: (NSString*)title
message: (NSString*)message
def: (NSString*)defaultButton
alt: (NSString*)alternateButton
other: (NSString*)otherButton;
- (void) setButtons: (NSArray *)buttons;
- (void) sizePanelToFit;
- (void) buttonAction: (id)sender;
- (NSInteger) result;
- (BOOL) isActivePanel;
@end
@implementation GSAlertPanel
//static const float WTitleHeight = 0.0; // TODO: Check this value.
static const float WinMinWidth = 362.0;
static const float WinMinHeight = 161.0;
static const float IconSide = 48.0;
static const float IconBottom = -56.0; // from the top of the window.
static const float IconLeft = 8.0;
static const float TitleLeft = 64.0;
static const float TitleMinRight = 8.0;
static const float LineHeight = 2.0;
static const float LineBottom = -66.0; // from the top of the window.
static const float LineLeft = 0.0;
//static const float ScrollMinSize = 48.0; // in either direction.
static const float MessageHorzMargin = 8.0; // 5 is too little margin.
//static const float MessageMinHeight = 20.0;
static const float MessageVertMargin = 6.0; // from the top of the buttons.
static const float MessageTop = -72; // from the top of the window;
static const float ButtonBottom = 8.0; // from the bottom of the window.
static const float ButtonMargin = 8.0;
static const float ButtonInterspace = 10.0;
static const float ButtonMinHeight = 24.0;
static const float ButtonMinWidth = 72.0;
#define SIZE_SCALE 0.6
#define MessageFont [NSFont messageFontOfSize: 14]
+ (void) initialize
{
if (self == [GSAlertPanel class])
{
[self setVersion: 1];
}
}
- (id) init
{
/*
if (![NSBundle loadNibNamed: @"AlertPanel" owner: self])
{
NSLog(@"cannot open alert panel model file\n");
return nil;
}
*/
return [self _initWithoutGModel];
}
- (void) dealloc
{
if (self == standardAlertPanel)
{
standardAlertPanel = nil;
}
if (self == informationalAlertPanel)
{
informationalAlertPanel = nil;
}
if (self == criticalAlertPanel)
{
criticalAlertPanel = nil;
}
RELEASE(defButton);
RELEASE(altButton);
RELEASE(othButton);
RELEASE(icoButton);
RELEASE(titleField);
RELEASE(messageField);
RELEASE(scroll);
[super dealloc];
}
static NSScrollView*
makeScrollViewWithRect(NSRect rect)
{
float lineHeight = [MessageFont boundingRectForFont].size.height;
NSScrollView *scroll = [[NSScrollView alloc]initWithFrame: rect];
[scroll setBorderType: NSLineBorder];
[scroll setBackgroundColor: [NSColor controlBackgroundColor]];
[scroll setHasHorizontalScroller: NO];
[scroll setHasVerticalScroller: YES];
[scroll setScrollsDynamically: YES];
[scroll setLineScroll: lineHeight];
[scroll setPageScroll: lineHeight*10.0];
return scroll;
}
- (NSButton*) _makeButtonWithRect: (NSRect)rect tag: (NSInteger)tag
{
NSButton *button = [[NSButton alloc] initWithFrame: rect];
[button setAutoresizingMask: NSViewMinXMargin | NSViewMaxYMargin];
[button setButtonType: NSMomentaryPushInButton];
[button setTitle: @""];
[button setTarget: self];
[button setAction: @selector(buttonAction:)];
[button setTag: tag];
[button setFont: [NSFont systemFontOfSize: 0]];
return button;
}
#define useControl(control) ([control superview] != nil)
static void
setControl(NSView* content, id control, NSString *title)
{
if (title != nil)
{
if ([control respondsToSelector: @selector(setTitle:)])
{
[control setTitle: title];
}
else if ([control respondsToSelector: @selector(setStringValue:)])
{
[control setStringValue: title];
}
[control sizeToFit];
if (!useControl(control))
{
[content addSubview: control];
}
}
else if (useControl(control))
{
[control removeFromSuperview];
}
}
static void
setButton(NSView* content, NSButton *control, NSButton *template)
{
if (template != nil)
{
[control setTitle: [template title]];
[control setKeyEquivalent: [template keyEquivalent]];
[control setKeyEquivalentModifierMask:
[template keyEquivalentModifierMask]];
[control setTag: [template tag]];
[control sizeToFit];
if (!useControl(control))
{
[content addSubview: control];
}
}
else if (useControl(control))
{
[control removeFromSuperview];
}
}
static void
setKeyEquivalent(NSButton *button)
{
NSString *title = [button title];
if (![[button keyEquivalent] isEqualToString: @"\r"])
{
if ([title isEqualToString: _(@"Cancel")])
{
[button setKeyEquivalent: @"\e"];
[button setKeyEquivalentModifierMask: 0];
}
else if ([title isEqualToString: _(@"Don't Save")])
{
[button setKeyEquivalent: @"d"];
[button setKeyEquivalentModifierMask: NSCommandKeyMask];
}
else
{
[button setKeyEquivalent: @""];
[button setKeyEquivalentModifierMask: 0];
}
}
}
- (id) _initWithoutGModel
{
NSRect rect;
NSImage *image;
NSBox *box;
NSView *content;
NSRect r = NSMakeRect(0.0, 0.0, WinMinWidth, WinMinHeight);
NSFont *titleFont = [NSFont systemFontOfSize: 18.0];
float titleHeight = [titleFont boundingRectForFont].size.height;
NSText *fieldEditor = [self fieldEditor: YES forObject: messageField];
NSDictionary *selectedAttrs;
self = [self initWithContentRect: r
styleMask: NSTitledWindowMask
backing: NSBackingStoreRetained
defer: YES];
if (self == nil)
return nil;
[self setTitle: @" "];
[self setLevel: NSModalPanelWindowLevel];
content = [self contentView];
// we're an ATTENTION panel, therefore:
[self setHidesOnDeactivate: NO];
[self setBecomesKeyOnlyIfNeeded: NO];
// First, the subviews that will be positioned automatically.
rect.size.height = IconSide;
rect.size.width = IconSide;
rect.origin.y = r.origin.y + r.size.height + IconBottom;
rect.origin.x = IconLeft;
icoButton = [[NSButton alloc] initWithFrame: rect];
[icoButton setAutoresizingMask: NSViewMaxXMargin|NSViewMinYMargin];
[icoButton setBordered: NO];
[icoButton setEnabled: NO];
[[icoButton cell] setImageDimsWhenDisabled: NO];
[[icoButton cell] setImageScaling: NSImageScaleProportionallyUpOrDown];
[icoButton setImagePosition: NSImageOnly];
image = [[NSApplication sharedApplication] applicationIconImage];
[icoButton setImage: image];
[content addSubview: icoButton];
// Title
rect.size.height = 0.0; // will be sized to fit anyway.
rect.size.width = 0.0; // will be sized to fit anyway.
rect.origin.y = r.origin.y + r.size.height
+ IconBottom + (IconSide - titleHeight)/2;;
rect.origin.x = TitleLeft;
titleField = [[NSTextField alloc] initWithFrame: rect];
[titleField setAutoresizingMask: NSViewMinYMargin];
[titleField setEditable: NO];
[titleField setSelectable: NO];
[titleField setBezeled: NO];
[titleField setDrawsBackground: NO];
[titleField setStringValue: @""];
[titleField setFont: titleFont];
// Horizontal line
rect.size.height = LineHeight;
rect.size.width = r.size.width;
rect.origin.y = r.origin.y + r.size.height + LineBottom;
rect.origin.x = LineLeft;
box = [[NSBox alloc] initWithFrame: rect];
[box setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
[box setTitlePosition: NSNoTitle];
[box setBorderType: NSGrooveBorder];
[content addSubview: box];
RELEASE(box);
// Then, make the subviews that'll be sized by sizePanelToFit;
rect.size.height = 0.0;
rect.size.width = 0.0;
rect.origin.y = 0.0;
rect.origin.x = 0.0;
messageField = [[NSTextField alloc] initWithFrame: rect];
[messageField setEditable: NO];
/*
PJB:
How do you want the user to report an error message if it is
not selectable? Any text visible on the screen should always
be selectable for a copy-and-paste. Hence, setSelectable: YES.
*/
selectedAttrs = [NSDictionary dictionaryWithObjectsAndKeys:
[NSColor controlLightHighlightColor],
NSBackgroundColorAttributeName,
nil];
[(NSTextView *)fieldEditor setSelectedTextAttributes: selectedAttrs];
[messageField setSelectable: YES];
[messageField setBezeled: NO];
[messageField setDrawsBackground: NO];
[messageField setAlignment: NSCenterTextAlignment];
[messageField setStringValue: @""];
[messageField setFont: MessageFont];
defButton = [self _makeButtonWithRect: rect tag: NSAlertDefaultReturn];
[defButton setKeyEquivalent: @"\r"];
[defButton setHighlightsBy: NSPushInCellMask | NSChangeGrayCellMask
| NSContentsCellMask];
[defButton setImagePosition: NSImageRight];
[defButton setImage: [NSImage imageNamed: @"common_ret"]];
[defButton setAlternateImage: [NSImage imageNamed: @"common_retH"]];
altButton = [self _makeButtonWithRect: rect tag: NSAlertAlternateReturn];
othButton = [self _makeButtonWithRect: rect tag: NSAlertOtherReturn];
rect.size.height = 80.0;
scroll = makeScrollViewWithRect(rect);
result = NSAlertErrorReturn;
isGreen = YES;
return self;
}
- (void) sizePanelToFit
{
NSRect bounds;
NSSize ssize; // screen size (corrected).
NSSize bsize; // button size (max of the three).
NSSize wsize = {0.0, 0.0}; // window size (computed).
NSScreen *screen;
NSView *content;
NSButton *buttons[3];
float position = 0.0;
int numberOfButtons;
int i;
BOOL needsScroll;
BOOL couldNeedScroll;
NSUInteger mask = [self styleMask];
/*
* Set size to the size of a content rectangle of a panel
* that completely fills the screen.
*/
screen = [self screen];
if (screen == nil)
{
screen = [NSScreen mainScreen];
}
bounds = [screen frame];
bounds = [NSWindow contentRectForFrameRect: bounds styleMask: mask];
ssize = bounds.size;
/* Do not let a panel grow beyond a factor of SIZE_SCALE of the screen */
ssize.width = SIZE_SCALE * ssize.width;
ssize.height = SIZE_SCALE * ssize.height;
// Let's size the title.
if (useControl(titleField))
{
NSRect rect = [titleField frame];
float width = TitleLeft + rect.size.width + TitleMinRight;
if (wsize.width < width)
{
wsize.width = width;
// ssize.width < width = > the title will be silently clipped.
}
}
wsize.height = -LineBottom;
// Let's count the buttons.
bsize.width = ButtonMinWidth;
bsize.height = ButtonMinHeight;
buttons[0] = defButton;
buttons[1] = altButton;
buttons[2] = othButton;
numberOfButtons = 0;
for (i = 0; i < 3; i++)
{
if (useControl(buttons[i]))
{
NSRect rect = [buttons[i] frame];
if (bsize.width < rect.size.width)
{
bsize.width = rect.size.width;
}
if (bsize.height < rect.size.height)
{
bsize.height = rect.size.height;
}
numberOfButtons++;
}
}
if (numberOfButtons > 0)
{
// (with NSGetAlertPanel, there could be zero buttons).
float width = (bsize.width + ButtonInterspace) * numberOfButtons
-ButtonInterspace + ButtonMargin * 2;
/*
* If the buttons are too wide or too high to fit in the screen,
* then too bad! Thought it would be simple enough to put them
* in the scroll view with the messageField.
* TODO: See if we raise an exception here or if we let the
* QA people detect this kind of problem.
*/
if (wsize.width < width)
{
wsize.width = width;
}
wsize.height += ButtonBottom + bsize.height;
}
// Let's see the size of the messageField and how to place it.
needsScroll = NO;
couldNeedScroll = useControl(messageField);
if (couldNeedScroll)
{
NSRect rect = [messageField frame];
float width = rect.size.width + 2*MessageHorzMargin;
if (wsize.width < width)
{
wsize.width = width;
}
/* If the message is too wide, wrap its text. Apparently, we cannot
use -sizeToFit to compute the height of the message. */
[messageField setAlignment: NSLeftTextAlignment];
width = ssize.width - 2*MessageHorzMargin;
rect.size =
[[messageField attributedStringValue]
boundingRectWithSize: NSMakeSize(width, 1e6)
options: 0].size;
[messageField setFrame: rect];
/*
* But only the messageField can impose a great height, therefore
* we check it along in the next paragraph.
*/
wsize.height += rect.size.height + 2 * MessageVertMargin;
}
else
{
wsize.height += MessageVertMargin;
}
// Strategically placed here, we resize the window.
if (ssize.height < wsize.height)
{
wsize.height = ssize.height;
needsScroll = couldNeedScroll;
}
else if (wsize.height < WinMinHeight)
{
wsize.height = WinMinHeight;
}
if (needsScroll)
{
wsize.width += [NSScroller scrollerWidth] + 4.0;
}
if (ssize.width < wsize.width)
{
wsize.width = ssize.width;
}
else if (wsize.width < WinMinWidth)
{
wsize.width = WinMinWidth;
}
bounds = NSMakeRect(0, 0, wsize.width, wsize.height);
bounds = [NSWindow frameRectForContentRect: bounds styleMask: mask];
[self setMaxSize: bounds.size];
[self setMinSize: bounds.size];
[self setContentSize: wsize];
content = [self contentView];
bounds = [content bounds];
// Now we can place the buttons.
if (numberOfButtons > 0)
{
position = bounds.origin.x + bounds.size.width - ButtonMargin;
for (i = 0; i < 3; i++)
{
if (useControl(buttons[i]))
{
NSRect rect;
position -= bsize.width;
rect.origin.x = position;
rect.origin.y = bounds.origin.y + ButtonBottom;
rect.size.width = bsize.width;
rect.size.height = bsize.height;
[buttons[i] setFrame: rect];
position -= ButtonInterspace;
}
}
}
// Finaly, place the message.
if (useControl(messageField))
{
NSRect mrect = [messageField frame];
if (needsScroll)
{
NSRect srect;
float width;
// The scroll view takes all the space that is available.
srect.origin.x = bounds.origin.x + MessageHorzMargin;
if (numberOfButtons > 0)
{
srect.origin.y = bounds.origin.y + ButtonBottom
+ bsize.height + MessageVertMargin;
}
else
{
srect.origin.y = bounds.origin.y + MessageVertMargin;
}
srect.size.width = bounds.size.width - 2 * MessageHorzMargin;
srect.size.height = bounds.origin.y + bounds.size.height
+ MessageTop - srect.origin.y;
[scroll setFrame: srect];
if (!useControl(scroll))
{
[content addSubview: scroll];
}
/* Adjust the message field's width again so that it does not
exceed the scroll view's visible rectangle and we do not
need a horizontal scroller. */
[messageField removeFromSuperview];
width =
[NSScrollView contentSizeForFrameSize: srect.size
hasHorizontalScroller: NO
hasVerticalScroller: YES
borderType: [scroll borderType]].width;
mrect.origin = NSZeroPoint;
mrect.size =
[[messageField attributedStringValue]
boundingRectWithSize: NSMakeSize(width, 1e6)
options: 0].size;
[messageField setFrame: mrect];
[scroll setDocumentView: messageField];
}
else
{
float vmargin;
/*
* We must center vertically the messageField because
* the window has a minimum size, thus may be greater
* than expected.
*/
mrect.origin.x = (wsize.width - mrect.size.width)/2;
vmargin = bounds.size.height + LineBottom-mrect.size.height;
if (numberOfButtons > 0)
{
vmargin -= ButtonBottom + bsize.height;
}
vmargin/= 2.0; // if negative, it'll bite up and down.
mrect.origin.y = bounds.origin.y + vmargin;
if (numberOfButtons > 0)
{
mrect.origin.y += ButtonBottom + bsize.height;
}
[messageField setFrame: mrect];
}
}
else if (useControl(scroll))
{
[scroll removeFromSuperview];
}
isGreen = NO;
[content display];
}
- (void) buttonAction: (id)sender
{
if (![self isActivePanel])
{
NSLog(@"alert panel buttonAction: when not in modal loop\n");
return;
}
result = [sender tag];
[NSApp stopModalWithCode: result];
}
- (NSInteger) result
{
return result;
}
- (BOOL) isActivePanel
{
return [NSApp modalWindow] == self;
}
- (NSInteger) runModal
{
if (GSCurrentThread() != GSAppKitThread)
{
[self performSelectorOnMainThread: _cmd
withObject: nil
waitUntilDone: YES];
}
else
{
if (isGreen)
{
[self sizePanelToFit];
}
[NSApp runModalForWindow: self];
[self orderOut: self];
}
return result;
}
- (void) setTitleBar: (NSString*)titleBar
icon: (NSImage*)icon
title: (NSString*)title
message: (NSString*)message
{
NSView *content = [self contentView];
if (titleBar != nil)
{
[self setTitle: titleBar];
}
if (icon != nil)
{
[icoButton setImage: icon];
}
if (title == nil)
{
title = titleBar; // Fall back to the same text as the title bar
}
setControl(content, titleField, title);
if (useControl(scroll))
{
// TODO: Remove the following line once NSView is corrected.
[scroll setDocumentView: nil];
[scroll removeFromSuperview];
[messageField removeFromSuperview];
}
setControl(content, messageField, message);
/* If the message contains a newline character then align the
* message to the left side, as it is quite undesirable for a long
* message to appear aligned in the center
*/
if ([message rangeOfString: @"\n"].location != NSNotFound)
{
[messageField setAlignment: NSLeftTextAlignment];
}
else
{
[messageField setAlignment: NSCenterTextAlignment];
}
}
- (void) setTitleBar: (NSString*)titleBar
icon: (NSImage*)icon
title: (NSString*)title
message: (NSString*)message
def: (NSString*)defaultButton
alt: (NSString*)alternateButton
other: (NSString*)otherButton
{
NSView *content = [self contentView];
[self setTitleBar: titleBar icon: icon title: title message: message];
setControl(content, defButton, defaultButton);
setControl(content, altButton, alternateButton);
setControl(content, othButton, otherButton);
if (useControl(defButton))
{
[self makeFirstResponder: defButton];
}
else
{
[self makeFirstResponder: self];
}
if (useControl(altButton))
{
setKeyEquivalent(altButton);
}
if (useControl(othButton))
{
setKeyEquivalent(othButton);
}
/* a *working* nextKeyView chain:
the trick is that the 3 buttons are not always used (displayed)
so we have to set the nextKeyView *each* time.
Maybe some optimisation in the logic of this block will be good,
however it seems too risky for a (so) small reward
*/
{
BOOL ud, ua, uo;
ud = useControl(defButton);
ua = useControl(altButton);
uo = useControl(othButton);
if (ud)
{
if (uo)
[defButton setNextKeyView: othButton];
else if (ua)
[defButton setNextKeyView: altButton];
else
{
[defButton setPreviousKeyView: nil];
[defButton setNextKeyView: nil];
}
}
if (uo)
{
if (ua)
[othButton setNextKeyView: altButton];
else if (ud)
[othButton setNextKeyView: defButton];
else
{
[othButton setPreviousKeyView: nil];
[othButton setNextKeyView: nil];
}
}
if (ua)
{
if (ud)
[altButton setNextKeyView: defButton];
else if (uo)
[altButton setNextKeyView: othButton];
else
{
[altButton setPreviousKeyView: nil];
[altButton setNextKeyView: nil];
}
}
}
[self sizePanelToFit];
isGreen = YES;
result = NSAlertErrorReturn; /* If no button was pressed */
}
- (void) setButtons: (NSArray *)buttons;
{
NSView *content = [self contentView];
NSUInteger count = [buttons count];
setButton(content, defButton, count > 0 ? [buttons objectAtIndex: 0] : nil);
setButton(content, altButton, count > 1 ? [buttons objectAtIndex: 1] : nil);
setButton(content, othButton, count > 2 ? [buttons objectAtIndex: 2] : nil);
if (useControl(defButton))
{
[self makeFirstResponder: defButton];
}
else
{
[self makeFirstResponder: self];
}
/* a *working* nextKeyView chain:
the trick is that the 3 buttons are not always used (displayed)
so we have to set the nextKeyView *each* time.
*/
if (count > 2)
{
[defButton setNextKeyView: othButton];
[othButton setNextKeyView: altButton];
[altButton setNextKeyView: defButton];
}
else if (count > 1)
{
[defButton setNextKeyView: altButton];
[altButton setNextKeyView: defButton];
}
else if (count > 0)
{
[defButton setPreviousKeyView: nil];
[defButton setNextKeyView: nil];
}
[self sizePanelToFit];
isGreen = YES;
result = NSAlertErrorReturn; /* If no button was pressed */
}
@end /* GSAlertPanel */
@implementation GSAlertPanel (GMArchiverMethods)
// Reuse createObjectForModelUnarchiver: from super class
- (void) encodeWithModelArchiver: (GMArchiver*)archiver
{
[super encodeWithModelArchiver: archiver];
[archiver encodeSize: [self frame].size withName: @"OriginalSize"];
[archiver encodeObject: defButton withName: @"DefaultButton"];
[archiver encodeObject: altButton withName: @"AlternateButton"];
[archiver encodeObject: othButton withName: @"OtherButton"];
[archiver encodeObject: icoButton withName: @"IconButton"];
[archiver encodeObject: messageField withName: @"MessageField"];
[archiver encodeObject: titleField withName: @"TitleField"];
}
- (id) initWithModelUnarchiver: (GMUnarchiver*)unarchiver
{
self = [super initWithModelUnarchiver: unarchiver];
if (self != nil)
{
(void)[unarchiver decodeSizeWithName: @"OriginalSize"];
defButton = RETAIN([unarchiver decodeObjectWithName: @"DefaultButton"]);
altButton = RETAIN([unarchiver decodeObjectWithName: @"AlternateButton"]);
othButton = RETAIN([unarchiver decodeObjectWithName: @"OtherButton"]);
icoButton = RETAIN([unarchiver decodeObjectWithName: @"IconButton"]);
messageField = RETAIN([unarchiver decodeObjectWithName: @"MessageField"]);
titleField = RETAIN([unarchiver decodeObjectWithName: @"TitleField"]);
scroll = makeScrollViewWithRect(NSMakeRect(0.0, 0.0, 80.0, 80.0));
result = NSAlertErrorReturn;
isGreen = YES;
}
return self;
}
@end /* GSAlertPanel GMArchiverMethods */
/*
GSAlertSheet. This class provides a borderless window which is
attached to the parent window.
*/
@interface GSAlertSheet : GSAlertPanel
@end
@implementation GSAlertSheet
+ (void) initialize
{
if (self == [GSAlertSheet class])
{
if (nc == nil)
{
nc = [NSNotificationCenter defaultCenter];
}
[self setVersion: 0];
}
}
- (id) initWithContentRect: (NSRect)contentRect
styleMask: (NSUInteger)aStyle
backing: (NSBackingStoreType)bufferingType
defer: (BOOL)flag
{
if (NSIsEmptyRect(contentRect))
{
contentRect = NSMakeRect(0,0,100,100);
}
self = [super initWithContentRect: contentRect
styleMask: NSBorderlessWindowMask
backing: bufferingType
defer: flag];
if (self != nil)
{
// FIXME
}
return self;
}
- (NSRect) frameFromParentWindowFrame
{
id parent = [self parentWindow];
NSRect frame = [self frame];
NSRect newFrame = NSZeroRect; // return zero rect, if parent isn't set.
if(parent != nil)
{
NSRect contentRect = [[parent contentView] frame];
//
// The calculation is based on the contentRect of the parent window
// since we want the sheet to appear just inside of it.
//
newFrame = [parent frame];
newFrame.origin.x += ((newFrame.size.width - frame.size.width) / 2);
newFrame.origin.y += (contentRect.size.height - frame.size.height) + 5;
}
return newFrame;
}
- (void) resetWindow
{
NSRect frame = [self frameFromParentWindowFrame];
NSWindow *parent = nil;
if((parent = [self parentWindow]) != nil)
{
[self setBackgroundColor:
[[parent backgroundColor]
highlightWithLevel: 0.4]];
}
[self setFrame: frame display: YES];
}
- (void) setParentWindow: (NSWindow *)window
{
[super setParentWindow: window];
[self resetWindow];
/*
[nc removeObserver: self];
if (parent != nil)
{
// add observers....
[nc addObserver: self
selector: @selector(handleWindowClose:)
name: NSWindowWillCloseNotification
object: parent];
[nc addObserver: self
selector: @selector(handleWindowMiniaturize:)
name: NSWindowWillMiniaturizeNotification
object: parent];
[nc addObserver: self
selector: @selector(handleWindowMove:)
name: NSWindowWillMoveNotification
object: parent];
[nc addObserver: self
selector: @selector(handleWindowMove:)
name: NSWindowDidResizeNotification
object: parent];
[nc addObserver: self
selector: @selector(handleWindowDidBecomeKey:)
name: NSWindowDidBecomeKeyNotification
object: parent];
}
*/
}
/*
- (void) handleWindowClose: (NSNotification *)notification
{
[self close];
}
- (void) handleWindowMiniaturize: (NSNotification *)notification
{
[self close];
}
- (void) handleWindowMove: (NSNotification *)notification
{
[self _resetWindowPosition];
}
- (void) handleWindowDidBecomeKey: (NSNotification *)notification
{
[self _resetWindowPosition];
}
- (void) dealloc
{
[nc removeObserver: self];
[super dealloc];
}
*/
@end
/*
These functions may be called "recursively". For example, from a
timed event. Therefore, there may be several alert panel active
at the same time, but only the first one will be THE
standardAlertPanel, which will not be released once finished
with, but which will be kept for future use.
+---------+---------+---------+---------+---------+
| std !=0 | std act | pan=std | pan=new | std=new |
+---------+---------+---------+---------+---------+
a: | F | N/A | | X | X |
+---------+---------+---------+---------+---------+
b: | V | F | X | | |
+---------+---------+---------+---------+---------+
c: | V | V | | X | |
+---------+---------+---------+---------+---------+
*/
/*
TODO: Check if this discrepancy is wanted and needed.
If not, we could merge these parameters, even
for the alert panel, setting its window title to "Alert".
*/
@interface _GSAlertCreation : NSObject
{
GSAlertPanel **instance;
NSString *defaultTitle;
NSString *title;
NSString *message;
NSString *defaultButton;
NSString *alternateButton;
NSString *otherButton;
GSAlertPanel *panel;
}
- (id) initWithInstance: (GSAlertPanel**)_instance
defaultTitle: (NSString*)_defaultTitle
title: (NSString*)_title
message: (NSString*)_message
defaultButton: (NSString*)_defaultButton
alternateButton: (NSString*)_alternateButton
otherButton: (NSString*)_otherButton;
- (void) makePanel;
- (void) makeSheet;
- (GSAlertPanel*) panel;
@end
@implementation _GSAlertCreation
- (void) dealloc
{
RELEASE(defaultTitle);
RELEASE(title);
RELEASE(defaultButton);
RELEASE(alternateButton);
RELEASE(otherButton);
[super dealloc];
}
- (id) initWithInstance: (GSAlertPanel**)_instance
defaultTitle: (NSString*)_defaultTitle
title: (NSString*)_title
message: (NSString*)_message
defaultButton: (NSString*)_defaultButton
alternateButton: (NSString*)_alternateButton
otherButton: (NSString*)_otherButton
{
instance = _instance;
ASSIGNCOPY(defaultTitle, _defaultTitle);
ASSIGNCOPY(title, _title);
ASSIGNCOPY(message, _message);
ASSIGNCOPY(defaultButton, _defaultButton);
ASSIGNCOPY(alternateButton, _alternateButton);
ASSIGNCOPY(otherButton, _otherButton);
return self;
}
- (void) makePanel
{
if (*instance != 0 && [*instance isMemberOfClass: [GSAlertPanel class]])
{
if ([*instance isActivePanel])
{ // c:
panel = [[GSAlertPanel alloc] init];
}
else
{ // b:
panel = *instance;
}
}
else
{ // a:
panel = [[GSAlertPanel alloc] init];
*instance = panel;
}
[panel setTitleBar: defaultTitle
icon: nil
title: title
message: message
def: defaultButton
alt: alternateButton
other: otherButton];
}
- (void) makeSheet
{
if (*instance != 0 && [*instance isMemberOfClass: [GSAlertSheet class]])
{
if ([*instance isActivePanel])
{ // c:
panel = [[GSAlertSheet alloc] init];
}
else
{ // b:
panel = *instance;
}
}
else
{ // a:
panel = [[GSAlertSheet alloc] init];
*instance = panel;
}
[panel setTitleBar: defaultTitle
icon: nil
title: title
message: message
def: defaultButton
alt: alternateButton
other: otherButton];
}
- (GSAlertPanel*) panel
{
return panel;
}
@end
static GSAlertPanel*
getSomePanel(
GSAlertPanel **instance,
NSString *defaultTitle,
NSString *title,
NSString *message,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton)
{
GSAlertPanel *panel;
if (GSCurrentThread() != GSAppKitThread)
{
_GSAlertCreation *c;
NSWarnFLog(@"Alert Panel functionality called from a thread other than"
@" the main one, this may not work on MacOS-X and could therefore be"
@" a portability problem in your code");
c = [_GSAlertCreation alloc];
c = [c initWithInstance: instance
defaultTitle: defaultTitle
title: title
message: message
defaultButton: defaultButton
alternateButton: alternateButton
otherButton: otherButton];
[c performSelectorOnMainThread: @selector(makePanel)
withObject: nil
waitUntilDone: YES];
panel = [c panel];
RELEASE(c);
}
else
{
if (*instance != 0 && [*instance isMemberOfClass: [GSAlertPanel class]])
{
if ([*instance isActivePanel])
{ // c:
panel = [[GSAlertPanel alloc] init];
}
else
{ // b:
panel = *instance;
}
}
else
{ // a:
panel = [[GSAlertPanel alloc] init];
*instance = panel;
}
[panel setTitleBar: defaultTitle
icon: nil
title: title
message: message
def: defaultButton
alt: alternateButton
other: otherButton];
}
return panel;
}
static GSAlertPanel*
getSomeSheet(
GSAlertPanel **instance,
NSString *defaultTitle,
NSString *title,
NSString *message,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton)
{
GSAlertSheet *panel;
if (GSCurrentThread() != GSAppKitThread)
{
_GSAlertCreation *c;
NSWarnFLog(@"Alert Sheet functionality called from a thread other than"
@" the main one, this may not work on MacOS-X and could therefore be"
@" a portability problem in your code");
c = [_GSAlertCreation alloc];
c = [c initWithInstance: instance
defaultTitle: defaultTitle
title: title
message: message
defaultButton: defaultButton
alternateButton: alternateButton
otherButton: otherButton];
[c performSelectorOnMainThread: @selector(makeSheet)
withObject: nil
waitUntilDone: YES];
panel = (GSAlertSheet *)[c panel];
RELEASE(c);
}
else
{
if (*instance != 0 && [*instance isMemberOfClass: [GSAlertSheet class]])
{
if ([*instance isActivePanel])
{ // c:
panel = [[GSAlertSheet alloc] init];
}
else
{ // b:
panel = (GSAlertSheet *)*instance;
}
}
else
{ // a:
panel = [[GSAlertSheet alloc] init];
*instance = panel;
}
[panel setTitleBar: defaultTitle
icon: nil
title: title
message: message
def: defaultButton
alt: alternateButton
other: otherButton];
}
return panel;
}
id
NSGetAlertPanel(
NSString *title,
NSString *msg,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton, ...)
{
va_list ap;
NSString *message;
va_start(ap, otherButton);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
return getSomePanel(&standardAlertPanel, defaultTitle, title, message,
defaultButton, alternateButton, otherButton);
}
NSInteger
NSRunAlertPanel(
NSString *title,
NSString *msg,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton, ...)
{
va_list ap;
NSString *message;
GSAlertPanel *panel;
NSInteger result;
va_start(ap, otherButton);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
if (NSApp == nil)
{
// No NSApp ... not running in a gui application so just log.
NSLog(@"%@", message);
return NSAlertDefaultReturn;
}
if (defaultButton == nil)
{
defaultButton = @"OK";
}
panel = getSomePanel(&standardAlertPanel, defaultTitle, title, message,
defaultButton, alternateButton, otherButton);
result = [panel runModal];
NSReleaseAlertPanel(panel);
return result;
}
NSInteger
NSRunLocalizedAlertPanel(
NSString *table,
NSString *title,
NSString *msg,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton, ...)
{
va_list ap;
NSString *message;
GSAlertPanel *panel;
NSInteger result;
NSBundle *bundle = [NSBundle mainBundle];
if (title == nil)
{
title = defaultTitle;
}
#define localize(string) if (string != nil) \
string = [bundle localizedStringForKey: string value: string table: table]
localize(title);
localize(defaultButton);
localize(alternateButton);
localize(otherButton);
localize(msg);
#undef localize
va_start(ap, otherButton);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
if (defaultButton == nil)
{
defaultButton = @"OK";
}
panel = getSomePanel(&standardAlertPanel, @"Alert", title, message,
defaultButton, alternateButton, otherButton);
result = [panel runModal];
NSReleaseAlertPanel(panel);
return result;
}
id
NSGetCriticalAlertPanel(
NSString *title,
NSString *msg,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton, ...)
{
va_list ap;
NSString *message;
va_start(ap, otherButton);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
return getSomePanel(&criticalAlertPanel, @"Critical", title, message,
defaultButton, alternateButton, otherButton);
}
NSInteger
NSRunCriticalAlertPanel(
NSString *title,
NSString *msg,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton, ...)
{
va_list ap;
NSString *message;
GSAlertPanel *panel;
NSInteger result;
va_start(ap, otherButton);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
panel = getSomePanel(&criticalAlertPanel, @"Critical", title, message,
defaultButton, alternateButton, otherButton);
result = [panel runModal];
NSReleaseAlertPanel(panel);
return result;
}
id
NSGetInformationalAlertPanel(
NSString *title,
NSString *msg,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton, ...)
{
va_list ap;
NSString *message;
va_start(ap, otherButton);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
return getSomePanel(&informationalAlertPanel, @"Information", title, message,
defaultButton, alternateButton, otherButton);
}
NSInteger
NSRunInformationalAlertPanel(
NSString *title,
NSString *msg,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton, ...)
{
va_list ap;
NSString *message;
GSAlertPanel *panel;
NSInteger result;
va_start(ap, otherButton);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
panel = getSomePanel(&informationalAlertPanel,
@"Information",
title, message,
defaultButton, alternateButton, otherButton);
result = [panel runModal];
NSReleaseAlertPanel(panel);
return result;
}
void
NSReleaseAlertPanel(id panel)
{
if ((panel != standardAlertPanel)
&& (panel != informationalAlertPanel)
&& (panel != criticalAlertPanel))
{
RELEASE(panel);
}
}
//
// New alert interface of Mac OS X
//
void NSBeginAlertSheet(NSString *title,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton,
NSWindow *docWindow,
id modalDelegate,
SEL willEndSelector,
SEL didEndSelector,
void *contextInfo,
NSString *msg, ...)
{
va_list ap;
NSString *message;
GSAlertPanel *panel;
va_start(ap, msg);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
if (defaultButton == nil)
{
defaultButton = @"OK";
}
panel = getSomeSheet(&standardAlertPanel, defaultTitle, title, message,
defaultButton, alternateButton, otherButton);
// FIXME: We should also change the button action to call endSheet:
[NSApp beginSheet: panel
modalForWindow: docWindow
modalDelegate: modalDelegate
didEndSelector: willEndSelector
contextInfo: contextInfo];
[panel close];
if (modalDelegate && [modalDelegate respondsToSelector: didEndSelector])
{
void (*didEnd)(id, SEL, id, NSInteger, void*);
didEnd = (void (*)(id, SEL, id, NSInteger, void*))[modalDelegate
methodForSelector: didEndSelector];
didEnd(modalDelegate, didEndSelector, panel, [panel result], contextInfo);
}
NSReleaseAlertPanel(panel);
}
void NSBeginCriticalAlertSheet(NSString *title,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton,
NSWindow *docWindow,
id modalDelegate,
SEL willEndSelector,
SEL didEndSelector,
void *contextInfo,
NSString *msg, ...)
{
va_list ap;
NSString *message;
GSAlertPanel *panel;
va_start(ap, msg);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
panel = getSomeSheet(&criticalAlertPanel, @"Critical", title, message,
defaultButton, alternateButton, otherButton);
// FIXME: We should also change the button action to call endSheet:
[NSApp beginSheet: panel
modalForWindow: docWindow
modalDelegate: modalDelegate
didEndSelector: willEndSelector
contextInfo: contextInfo];
[panel close];
if (modalDelegate && [modalDelegate respondsToSelector: didEndSelector])
{
void (*didEnd)(id, SEL, id, NSInteger, void*);
didEnd = (void (*)(id, SEL, id, NSInteger, void*))[modalDelegate
methodForSelector: didEndSelector];
didEnd(modalDelegate, didEndSelector, panel, [panel result], contextInfo);
}
NSReleaseAlertPanel(panel);
}
void NSBeginInformationalAlertSheet(NSString *title,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton,
NSWindow *docWindow,
id modalDelegate,
SEL willEndSelector,
SEL didEndSelector,
void *contextInfo,
NSString *msg, ...)
{
va_list ap;
NSString *message;
GSAlertPanel *panel;
va_start(ap, msg);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
panel = getSomeSheet(&informationalAlertPanel,
@"Information",
title, message,
defaultButton, alternateButton, otherButton);
// FIXME: We should also change the button action to call endSheet:
[NSApp beginSheet: panel
modalForWindow: docWindow
modalDelegate: modalDelegate
didEndSelector: willEndSelector
contextInfo: contextInfo];
[panel close];
if (modalDelegate && [modalDelegate respondsToSelector: didEndSelector])
{
void (*didEnd)(id, SEL, id, NSInteger, void*);
didEnd = (void (*)(id, SEL, id, NSInteger, void*))[modalDelegate
methodForSelector: didEndSelector];
didEnd(modalDelegate, didEndSelector, panel, [panel result], contextInfo);
}
NSReleaseAlertPanel(panel);
}
@implementation NSAlert
/*
* Class methods
*/
+ (void) initialize
{
if (self == [NSAlert class])
{
[self setVersion: 1];
}
}
+ (NSAlert *) alertWithError: (NSError *)error
{
NSArray *options;
NSUInteger count;
NSString *errorText;
errorText = [error localizedFailureReason];
if (errorText == nil)
{
errorText = [error localizedDescription];
}
options = [error localizedRecoveryOptions];
count = [options count];
return [self alertWithMessageText: errorText
defaultButton: (count > 0) ? [options objectAtIndex: 0] : nil
alternateButton: (count > 1) ? [options objectAtIndex: 1] : nil
otherButton: (count > 2) ? [options objectAtIndex: 2] : nil
informativeTextWithFormat: [error localizedRecoverySuggestion]];
}
+ (NSAlert *) alertWithMessageText: (NSString *)messageTitle
defaultButton: (NSString *)defaultButtonTitle
alternateButton: (NSString *)alternateButtonTitle
otherButton: (NSString *)otherButtonTitle
informativeTextWithFormat: (NSString *)format, ...
{
va_list ap;
NSAlert *alert = [[self alloc] init];
NSButton *but;
NSString *text;
va_start(ap, format);
if (format != nil)
{
text = [[NSString alloc] initWithFormat: format arguments: ap];
[alert setInformativeText: text];
RELEASE(text);
}
va_end(ap);
[alert setMessageText: messageTitle];
if (defaultButtonTitle != nil)
{
but = [alert addButtonWithTitle: defaultButtonTitle];
}
else
{
but = [alert addButtonWithTitle: _(@"OK")];
}
[but setTag: NSAlertDefaultReturn];
if (alternateButtonTitle != nil)
{
but = [alert addButtonWithTitle: alternateButtonTitle];
[but setTag: NSAlertAlternateReturn];
}
if (otherButtonTitle != nil)
{
but = [alert addButtonWithTitle: otherButtonTitle];
[but setTag: NSAlertOtherReturn];
}
return AUTORELEASE(alert);
}
- (id) init
{
_buttons = [[NSMutableArray alloc] init];
_style = NSWarningAlertStyle;
return self;
}
- (void) dealloc
{
RELEASE(_informative_text);
RELEASE(_message_text);
RELEASE(_icon);
RELEASE(_buttons);
RELEASE(_help_anchor);
RELEASE(_window);
[super dealloc];
}
- (void) setInformativeText: (NSString *)informativeText
{
ASSIGN(_informative_text, informativeText);
}
- (NSString *) informativeText
{
return _informative_text;
}
- (void) setMessageText: (NSString *)messageText
{
ASSIGN(_message_text, messageText);
}
- (NSString *) messageText
{
return _message_text;
}
- (void) setIcon: (NSImage *)icon
{
ASSIGN(_icon, icon);
}
- (NSImage *) icon
{
return _icon;
}
- (NSButton *) addButtonWithTitle: (NSString *)aTitle
{
NSButton *button = [[NSButton alloc] init];
NSUInteger count = [_buttons count];
[button setTitle: aTitle];
[button setAutoresizingMask: NSViewMinXMargin | NSViewMaxYMargin];
[button setButtonType: NSMomentaryPushButton];
[button setTarget: self];
[button setAction: @selector(buttonAction:)];
[button setFont: [NSFont systemFontOfSize: 0]];
if (count == 0)
{
[button setTag: NSAlertFirstButtonReturn];
[button setKeyEquivalent: @"\r"];
}
else
{
[button setTag: NSAlertFirstButtonReturn + count];
setKeyEquivalent(button);
}
[_buttons addObject: button];
RELEASE(button);
return button;
}
- (NSArray *) buttons
{
return _buttons;
}
- (void) setShowsHelp: (BOOL)showsHelp
{
_shows_help = showsHelp;
}
- (BOOL) showsHelp
{
return _shows_help;
}
- (void) setHelpAnchor: (NSString *)anchor
{
ASSIGN(_help_anchor, anchor);
}
- (NSString *) helpAnchor
{
return _help_anchor;
}
- (void) setAlertStyle: (NSAlertStyle)style
{
_style = style;
}
- (NSAlertStyle) alertStyle
{
return _style;
}
- (void) setDelegate: (id)delegate
{
_delegate = delegate;
}
- (id) delegate
{
return _delegate;
}
- (void) _setupPanel
{
if (GSCurrentThread() != GSAppKitThread)
{
[self performSelectorOnMainThread: _cmd
withObject: nil
waitUntilDone: YES];
}
else
{
GSAlertPanel *panel;
NSString *title;
panel = [[GSAlertPanel alloc] init];
_window = panel;
switch (_style)
{
case NSCriticalAlertStyle:
title = @"Critical";
break;
case NSInformationalAlertStyle:
title = @"Information";
break;
case NSWarningAlertStyle:
default:
title = @"Alert";
break;
}
[panel setTitleBar: title
icon: _icon
title: _message_text != nil ? _message_text : _(@"Alert")
message: _informative_text != nil ? _informative_text : _(@"No information")];
[panel setButtons: _buttons];
}
}
- (NSInteger) runModal
{
if (GSCurrentThread() != GSAppKitThread)
{
[self performSelectorOnMainThread: _cmd
withObject: nil
waitUntilDone: YES];
return _result;
}
else
{
[self _setupPanel];
[NSApp runModalForWindow: _window];
[_window orderOut: self];
_result = [(GSAlertPanel*)_window result];
DESTROY(_window);
return _result;
}
}
- (void) beginSheetModalForWindow: (NSWindow *)window
modalDelegate: (id)delegate
didEndSelector: (SEL)didEndSelector
contextInfo: (void *)contextInfo
{
[self _setupPanel];
_modalDelegate = delegate;
_didEndSelector = didEndSelector;
[NSApp beginSheet: _window
modalForWindow: window
modalDelegate: self
didEndSelector: @selector(_alertDidEnd:returnCode:contextInfo:)
contextInfo: contextInfo];
DESTROY(_window);
}
- (void) _alertDidEnd: (NSWindow *)sheet
returnCode: (NSInteger)returnCode
contextInfo: (void *)contextInfo
{
if ([_modalDelegate respondsToSelector: _didEndSelector])
{
void (*didEnd)(id, SEL, id, NSInteger, void *);
didEnd = (void (*)(id, SEL, id, NSInteger, void *))[_modalDelegate
methodForSelector: _didEndSelector];
didEnd(_modalDelegate, _didEndSelector, self, returnCode, contextInfo);
}
}
- (id) window
{
return _window;
}
@end
@interface GSExceptionPanel : GSAlertPanel
{
NSBrowser *_browser;
NSDictionary *_userInfo;
NSPanel *_userInfoPanel;
}
- (void) setUserInfo: (NSDictionary *)userInfo;
- (NSPanel *) userInfoPanel;
@end
NSInteger GSRunExceptionPanel(
NSString *title,
NSException *exception,
NSString *defaultButton,
NSString *alternateButton,
NSString *otherButton)
{
NSString *message;
GSExceptionPanel *panel;
NSInteger result;
message = [NSString stringWithFormat: @"%@: %@",
[exception name],
[exception reason]];
if (defaultButton == nil)
{
defaultButton = @"OK";
}
panel = [[GSExceptionPanel alloc] init];
if (title == nil)
{
title = @"Exception";
}
[panel setTitleBar: nil
icon: nil
title: title
message: message
def: defaultButton
alt: alternateButton
other: otherButton];
[panel setUserInfo: [exception userInfo]];
result = [panel runModal];
[[panel userInfoPanel] orderOut: nil];
[panel setUserInfo: nil];
RELEASE(panel);
return result;
}
@implementation GSExceptionPanel
- (void) dealloc
{
RELEASE(_userInfo);
RELEASE(_browser);
RELEASE(_userInfoPanel);
[super dealloc];
}
- (id) init
{
if ((self = [super init]))
{
[icoButton setEnabled: YES];
[icoButton setTarget: self];
[icoButton setAction: @selector(_icoAction:)];
}
return self;
}
- (NSPanel *) userInfoPanel
{
return _userInfoPanel;
}
- (void) setUserInfo: (NSDictionary *)userInfo;
{
ASSIGN(_userInfo, userInfo);
[_browser reloadColumn: 0];
}
- (void) _icoAction: (id)sender
{
NSRect fr;
if (_userInfoPanel)
{
[_browser reloadColumn: 0];
return;
}
fr = NSMakeRect(_frame.origin.x, _frame.origin.y + _frame.size.height + 15,
_frame.size.width, 108);
_userInfoPanel = [[NSPanel alloc] initWithContentRect: fr
styleMask: NSTitledWindowMask | NSResizableWindowMask
backing: NSBackingStoreBuffered
defer: NO];
[_userInfoPanel setTitle: @"User Info Inspector"];
[_userInfoPanel setWorksWhenModal: YES];
fr = NSMakeRect(8, 8, _frame.size.width - 16, 100);
_browser = [[NSBrowser alloc] initWithFrame: fr];
[_browser setMaxVisibleColumns: 2];
[_browser setDelegate: self];
[_browser setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable];
[_browser reloadColumn: 0];
[[_userInfoPanel contentView] addSubview:_browser];
[_userInfoPanel makeKeyAndOrderFront: self];
}
- (NSInteger) browser: (id)browser
numberOfRowsInColumn: (NSInteger)col
{
if (col == 0)
return [[_userInfo allKeys] count];
else
{
id val;
volatile id foo = nil;
val = [[(NSCell *)[browser selectedCellInColumn: col - 1]
representedObject] description];
NS_DURING
foo = [val propertyList];
val = foo;
NS_HANDLER
NS_ENDHANDLER
if ([val isKindOfClass: [NSArray class]])
return [val count];
else if ([val isKindOfClass: [NSDictionary class]])
return [[val allKeys] count];
else return val != nil;
}
return 0;
}
- (void) browser: (NSBrowser *)browser
willDisplayCell: (NSBrowserCell *)cell
atRow: (NSInteger)row
column: (NSInteger)column
{
if (column == 0)
{
id key = [[_userInfo allKeys] objectAtIndex: row];
id val = [_userInfo objectForKey: key];
[cell setLeaf: NO];
[cell setStringValue: [key description]];
[cell setRepresentedObject: val];
}
else
{
volatile id val;
BOOL flag;
val = [(NSCell *)[browser selectedCellInColumn: column - 1]
representedObject];
if (!([val isKindOfClass: [NSArray class]]
|| [val isKindOfClass: [NSArray class]]))
{
volatile id foo = nil;
val = [val description];
NS_DURING
foo = [val propertyList];
val = foo;
NS_HANDLER
NS_ENDHANDLER
}
flag = (!([val isKindOfClass: [NSArray class]]
|| [val isKindOfClass: [NSDictionary class]]));
[cell setLeaf: flag];
if ([val isKindOfClass: [NSArray class]])
{
volatile id obj = [val objectAtIndex: row];
if (!([obj isKindOfClass: [NSArray class]]
|| [obj isKindOfClass: [NSArray class]]))
{
volatile id foo;
obj = [[obj description] propertyList];
NS_DURING
foo = [obj propertyList];
obj = foo;
NS_HANDLER
NS_ENDHANDLER
}
if ([obj isKindOfClass: [NSArray class]])
{
[cell setRepresentedObject: obj];
[cell setLeaf: NO];
[cell setStringValue:
[NSString stringWithFormat: @"%@ %p", [obj class], obj]];
}
else if ([obj isKindOfClass: [NSDictionary class]])
{
[cell setRepresentedObject: obj];
[cell setLeaf: NO];
[cell setStringValue:
[NSString stringWithFormat: @"%@ %p", [obj class], obj]];
}
else
{
[cell setLeaf: YES];
[cell setStringValue: [obj description]];
[cell setRepresentedObject: nil];
}
}
else if ([val isKindOfClass: [NSDictionary class]])
{
id key = [[val allKeys] objectAtIndex: row];
volatile id it = [(NSDictionary *)val objectForKey: key];
volatile id foo;
foo = [it description];
NS_DURING
foo = [it propertyList];
it = foo;
NS_HANDLER
NS_ENDHANDLER
[cell setStringValue: [key description]];
[cell setRepresentedObject: it];
}
else
{
[cell setLeaf: YES];
[cell setStringValue: [val description]];
}
}
}
- (id) browser: (NSBrowser *)browser titleOfColumn: (NSInteger)column
{
id val;
NSString *title;
if (column == 0)
return @"userInfo";
val = [(NSCell *)[browser selectedCellInColumn: column - 1]
representedObject];
title = [NSString stringWithFormat: @"%@ %p", [val class], val];
return title;
}
@end
|