Skip to content

Commit f1ae189

Browse files
authored
Merge pull request #371 from gnustep/NSAlert_branch
Update to include function to open an alert panel relative to a window
2 parents a651830 + f0a39a7 commit f1ae189

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

Headers/AppKit/NSPanel.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,15 @@ APPKIT_EXPORT NSInteger NSRunAlertPanel(NSString *title,
123123
NSString *alternateButton,
124124
NSString *otherButton, ...);
125125

126+
// Synchronous alert that is displayed as a sheet attached to the provided
127+
// window. If docWindow is nil it falls back to NSRunAlertPanel.
128+
APPKIT_EXPORT NSInteger NSRunAlertPanelRelativeToWindow(NSString *title,
129+
NSString *msg,
130+
NSString *defaultButton,
131+
NSString *alternateButton,
132+
NSString *otherButton,
133+
NSWindow *docWindow, ...);
134+
126135
#if OS_API_VERSION(GS_API_MACOSX, GS_API_LATEST)
127136
APPKIT_EXPORT NSInteger NSRunCriticalAlertPanel(NSString *title,
128137
NSString *msg,

Source/NSAlert.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,6 +1770,64 @@ void NSBeginInformationalAlertSheet(NSString *title,
17701770
NSReleaseAlertPanel(panel);
17711771
}
17721772

1773+
// Synchronous sheet-aware alert. If docWindow is nil, fall back to
1774+
// NSRunAlertPanel which shows a modal alert. Otherwise display a sheet
1775+
// attached to docWindow and run a modal loop until it completes.
1776+
NSInteger
1777+
NSRunAlertPanelRelativeToWindow(NSString *title,
1778+
NSString *msg,
1779+
NSString *defaultButton,
1780+
NSString *alternateButton,
1781+
NSString *otherButton,
1782+
NSWindow *docWindow, ...)
1783+
{
1784+
va_list ap;
1785+
NSString *message = nil;
1786+
GSAlertPanel *panel = nil;
1787+
NSInteger result = 0;
1788+
1789+
va_start(ap, docWindow);
1790+
message = [NSString stringWithFormat: msg arguments: ap];
1791+
va_end(ap);
1792+
1793+
// Set the default button...
1794+
if (defaultButton == nil)
1795+
{
1796+
defaultButton = @"OK";
1797+
}
1798+
1799+
// Show a panel or a sheet depending on if we have a window...
1800+
if (docWindow == nil)
1801+
{
1802+
panel = getSomePanel(&standardAlertPanel,
1803+
defaultTitle,
1804+
title,
1805+
message,
1806+
defaultButton, alternateButton, otherButton);
1807+
1808+
result = [panel runModal];
1809+
}
1810+
else
1811+
{
1812+
panel = getSomeSheet(&informationalAlertPanel,
1813+
defaultTitle,
1814+
title,
1815+
message,
1816+
defaultButton, alternateButton, otherButton);
1817+
1818+
[NSApp beginSheet: panel
1819+
modalForWindow: docWindow
1820+
modalDelegate: nil
1821+
didEndSelector: NULL
1822+
contextInfo: NULL];
1823+
1824+
result = [panel result];
1825+
}
1826+
1827+
NSReleaseAlertPanel(panel);
1828+
return result;
1829+
}
1830+
17731831

17741832
@implementation NSAlert
17751833

0 commit comments

Comments
 (0)