Fix NSAlert implementation so that buttons are assigned tags starting

with NSAlertFirstButtonReturn, except if the panel was created with
the backward compatibility method +alertWithMessageText:defaultButton:...
Also assign appropriate key equivalents to buttons in panels created
with the old OpenStep alert panel functions.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@30515 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
wlux 2010-05-31 22:20:38 +00:00
parent 57cc668254
commit c8a398925b
2 changed files with 141 additions and 41 deletions

View file

@ -1,3 +1,12 @@
2010-06-01 Wolfgang Lux <wolfgang.lux@gmail.com>
* Source/NSAlert.m: Fix NSAlert implementation so that buttons are
assigned tags starting with NSAlertFirstButtonReturn, except if the
panel was created with the backward compatibility method
+alertWithMessageText:defaultButton:... Also assign appropriate
key equivalents to buttons in panels created with the old OpenStep
alert panel functions.
2010-06-01 Fred Kiefer <FredKiefer@gmx.de> 2010-06-01 Fred Kiefer <FredKiefer@gmx.de>
* Headers/AppKit/NSOutlineView.h, * Headers/AppKit/NSOutlineView.h,

View file

@ -205,6 +205,10 @@ static GSAlertPanel *criticalAlertPanel = nil;
- (id) _initWithoutGModel; - (id) _initWithoutGModel;
- (int) runModal; - (int) runModal;
- (void) setTitleBar: (NSString*)titleBar
icon: (NSImage*)icon
title: (NSString*)title
message: (NSString*)message;
- (void) setTitleBar: (NSString*)titleBar - (void) setTitleBar: (NSString*)titleBar
icon: (NSImage*)icon icon: (NSImage*)icon
title: (NSString*)title title: (NSString*)title
@ -212,6 +216,7 @@ static GSAlertPanel *criticalAlertPanel = nil;
def: (NSString*)defaultButton def: (NSString*)defaultButton
alt: (NSString*)alternateButton alt: (NSString*)alternateButton
other: (NSString*)otherButton; other: (NSString*)otherButton;
- (void) setButtons: (NSArray *)buttons;
- (void) sizePanelToFit; - (void) sizePanelToFit;
- (void) buttonAction: (id)sender; - (void) buttonAction: (id)sender;
- (int) result; - (int) result;
@ -306,7 +311,7 @@ makeScrollViewWithRect(NSRect rect)
return scroll; return scroll;
} }
- (NSButton*) _makeButtonWithRect: (NSRect)rect - (NSButton*) _makeButtonWithRect: (NSRect)rect tag: (int)tag
{ {
NSButton *button = [[NSButton alloc] initWithFrame: rect]; NSButton *button = [[NSButton alloc] initWithFrame: rect];
@ -315,6 +320,7 @@ makeScrollViewWithRect(NSRect rect)
[button setTitle: @""]; [button setTitle: @""];
[button setTarget: self]; [button setTarget: self];
[button setAction: @selector(buttonAction:)]; [button setAction: @selector(buttonAction:)];
[button setTag: tag];
[button setFont: [NSFont systemFontOfSize: 0]]; [button setFont: [NSFont systemFontOfSize: 0]];
return button; return button;
} }
@ -346,6 +352,53 @@ setControl(NSView* content, id control, NSString *title)
} }
} }
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 - (id) _initWithoutGModel
{ {
NSRect rect; NSRect rect;
@ -443,7 +496,7 @@ setControl(NSView* content, id control, NSString *title)
[messageField setStringValue: @""]; [messageField setStringValue: @""];
[messageField setFont: MessageFont]; [messageField setFont: MessageFont];
defButton = [self _makeButtonWithRect: rect]; defButton = [self _makeButtonWithRect: rect tag: NSAlertDefaultReturn];
[defButton setKeyEquivalent: @"\r"]; [defButton setKeyEquivalent: @"\r"];
[defButton setHighlightsBy: NSPushInCellMask | NSChangeGrayCellMask [defButton setHighlightsBy: NSPushInCellMask | NSChangeGrayCellMask
| NSContentsCellMask]; | NSContentsCellMask];
@ -451,8 +504,8 @@ setControl(NSView* content, id control, NSString *title)
[defButton setImage: [NSImage imageNamed: @"common_ret"]]; [defButton setImage: [NSImage imageNamed: @"common_ret"]];
[defButton setAlternateImage: [NSImage imageNamed: @"common_retH"]]; [defButton setAlternateImage: [NSImage imageNamed: @"common_retH"]];
altButton = [self _makeButtonWithRect: rect]; altButton = [self _makeButtonWithRect: rect tag: NSAlertAlternateReturn];
othButton = [self _makeButtonWithRect: rect]; othButton = [self _makeButtonWithRect: rect tag: NSAlertOtherReturn];
rect.size.height = 80.0; rect.size.height = 80.0;
scroll = makeScrollViewWithRect(rect); scroll = makeScrollViewWithRect(rect);
@ -727,22 +780,7 @@ setControl(NSView* content, id control, NSString *title)
NSLog(@"alert panel buttonAction: when not in modal loop\n"); NSLog(@"alert panel buttonAction: when not in modal loop\n");
return; return;
} }
else if (sender == defButton) result = [sender tag];
{
result = NSAlertDefaultReturn;
}
else if (sender == altButton)
{
result = NSAlertAlternateReturn;
}
else if (sender == othButton)
{
result = NSAlertOtherReturn;
}
else
{
NSLog(@"alert panel buttonAction: from unknown sender - x%p\n", sender);
}
[NSApp stopModalWithCode: result]; [NSApp stopModalWithCode: result];
} }
@ -780,9 +818,6 @@ setControl(NSView* content, id control, NSString *title)
icon: (NSImage*)icon icon: (NSImage*)icon
title: (NSString*)title title: (NSString*)title
message: (NSString*)message message: (NSString*)message
def: (NSString*)defaultButton
alt: (NSString*)alternateButton
other: (NSString*)otherButton
{ {
NSView *content = [self contentView]; NSView *content = [self contentView];
@ -820,7 +855,19 @@ setControl(NSView* content, id control, NSString *title)
{ {
[messageField setAlignment: NSCenterTextAlignment]; [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, defButton, defaultButton);
setControl(content, altButton, alternateButton); setControl(content, altButton, alternateButton);
setControl(content, othButton, otherButton); setControl(content, othButton, otherButton);
@ -832,6 +879,14 @@ setControl(NSView* content, id control, NSString *title)
{ {
[self makeFirstResponder: self]; [self makeFirstResponder: self];
} }
if (useControl(altButton))
{
setKeyEquivalent(altButton);
}
if (useControl(othButton))
{
setKeyEquivalent(othButton);
}
/* a *working* nextKeyView chain: /* a *working* nextKeyView chain:
the trick is that the 3 buttons are not always used (displayed) the trick is that the 3 buttons are not always used (displayed)
@ -889,6 +944,49 @@ setControl(NSView* content, id control, NSString *title)
result = NSAlertErrorReturn; /* If no button was pressed */ 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 */ @end /* GSAlertPanel */
@implementation GSAlertPanel (GMArchiverMethods) @implementation GSAlertPanel (GMArchiverMethods)
@ -1712,6 +1810,7 @@ void NSBeginInformationalAlertSheet(NSString *title,
{ {
va_list ap; va_list ap;
NSAlert *alert = [[self alloc] init]; NSAlert *alert = [[self alloc] init];
NSButton *but;
NSString *text; NSString *text;
va_start(ap, format); va_start(ap, format);
@ -1727,21 +1826,24 @@ void NSBeginInformationalAlertSheet(NSString *title,
if (defaultButtonTitle != nil) if (defaultButtonTitle != nil)
{ {
[alert addButtonWithTitle: defaultButtonTitle]; but = [alert addButtonWithTitle: defaultButtonTitle];
} }
else else
{ {
[alert addButtonWithTitle: _(@"OK")]; but = [alert addButtonWithTitle: _(@"OK")];
} }
[but setTag: NSAlertDefaultReturn];
if (alternateButtonTitle != nil) if (alternateButtonTitle != nil)
{ {
[alert addButtonWithTitle: alternateButtonTitle]; but = [alert addButtonWithTitle: alternateButtonTitle];
[but setTag: NSAlertAlternateReturn];
} }
if (otherButtonTitle != nil) if (otherButtonTitle != nil)
{ {
[alert addButtonWithTitle: otherButtonTitle]; but = [alert addButtonWithTitle: otherButtonTitle];
[but setTag: NSAlertOtherReturn];
} }
return AUTORELEASE(alert); return AUTORELEASE(alert);
@ -1814,15 +1916,7 @@ void NSBeginInformationalAlertSheet(NSString *title,
else else
{ {
[button setTag: NSAlertFirstButtonReturn + count]; [button setTag: NSAlertFirstButtonReturn + count];
if ([aTitle isEqualToString: _(@"Cancel")]) setKeyEquivalent(button);
{
[button setKeyEquivalent: @"\e"];
}
else if ([aTitle isEqualToString: _(@"Don't Save")])
{
[button setKeyEquivalent: @"D"];
[button setKeyEquivalentModifierMask: NSCommandKeyMask];
}
} }
[_buttons addObject: button]; [_buttons addObject: button];
@ -1887,7 +1981,6 @@ void NSBeginInformationalAlertSheet(NSString *title,
{ {
GSAlertPanel *panel; GSAlertPanel *panel;
NSString *title; NSString *title;
unsigned nbut = [_buttons count];
panel = [[GSAlertPanel alloc] init]; panel = [[GSAlertPanel alloc] init];
_window = panel; _window = panel;
@ -1908,10 +2001,8 @@ void NSBeginInformationalAlertSheet(NSString *title,
[panel setTitleBar: title [panel setTitleBar: title
icon: _icon icon: _icon
title: _message_text != nil ? _message_text : _(@"Alert") title: _message_text != nil ? _message_text : _(@"Alert")
message: _informative_text message: _informative_text];
def: (nbut > 0) ? [[_buttons objectAtIndex: 0] title] : (NSString*)nil [panel setButtons: _buttons];
alt: (nbut > 1) ? [[_buttons objectAtIndex: 1] title] : (NSString*)nil
other: (nbut > 2) ? [[_buttons objectAtIndex: 2] title] : (NSString*)nil];
} }
} }