From 3bdb18f5362eade49288e2101c6d19c65c0f98eb Mon Sep 17 00:00:00 2001 From: richard Date: Mon, 14 Dec 1998 08:58:53 +0000 Subject: [PATCH] Completed alert panel functions git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@3453 72102866-910b-0410-8b05-ffd578937521 --- ChangeLog | 5 + Headers/gnustep/gui/NSPanel.h | 31 ++++ Source/NSPanel.m | 260 +++++++++++++++++++++++++++++----- 3 files changed, 259 insertions(+), 37 deletions(-) diff --git a/ChangeLog b/ChangeLog index 852fc33ee..2c9e9feef 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Dec 14 9:10:00 1998 Richard Frith-Macdonald + + * NSPanel.m: Implemented all the alert panel functions + * NSPanel.h: Added prototypes for MacOS-X alert panel functions. + Sat Dec 12 10:10:00 1998 Richard Frith-Macdonald * NSView.m: Fixed bug and added comment about the (deliberately) diff --git a/Headers/gnustep/gui/NSPanel.h b/Headers/gnustep/gui/NSPanel.h index c385382f0..5564e0276 100644 --- a/Headers/gnustep/gui/NSPanel.h +++ b/Headers/gnustep/gui/NSPanel.h @@ -87,6 +87,20 @@ id NSGetAlertPanel(NSString *title, NSString *alternateButton, NSString *otherButton, ...); +#ifndef STRICT_OPENSTEP +id NSGetCriticalAlertPanel(NSString *title, + NSString *msg, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, ...); + +id NSGetInformationalAlertPanel(NSString *title, + NSString *msg, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, ...); +#endif + // // Create and Run an Attention Panel // @@ -95,12 +109,29 @@ int NSRunAlertPanel(NSString *title, NSString *defaultButton, NSString *alternateButton, NSString *otherButton, ...); + +#ifndef STRICT_OPENSTEP +int NSRunCriticalAlertPanel(NSString *title, + NSString *msg, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, ...); + +int NSRunInformationalAlertPanel(NSString *title, + NSString *msg, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, ...); +#endif + +#ifndef STRICT_MACOS_X int NSRunLocalizedAlertPanel(NSString *table, NSString *title, NSString *msg, NSString *defaultButton, NSString *alternateButton, NSString *otherButton, ...); +#endif // // Release an Attention Panel diff --git a/Source/NSPanel.m b/Source/NSPanel.m index 092b14797..15b48e05b 100644 --- a/Source/NSPanel.m +++ b/Source/NSPanel.m @@ -33,6 +33,8 @@ #include +#include +#include #include #include #include @@ -43,6 +45,7 @@ #include #include #include +#include #include @@ -83,13 +86,32 @@ // // Determining the Panel's Behavior // -- (BOOL)isFloatingPanel { return _isFloatingPanel; } -- (void)setFloatingPanel:(BOOL)flag { _isFloatingPanel = flag; } -- (BOOL)worksWhenModal { return _worksWhenModal; } -- (void)setWorksWhenModal:(BOOL)flag { _worksWhenModal = flag; } -- (BOOL)becomesKeyOnlyIfNeeded { return _becomesKeyOnlyIfNeeded; } +- (BOOL) isFloatingPanel +{ + return _isFloatingPanel; +} -- (void)setBecomesKeyOnlyIfNeeded:(BOOL)flag +- (void) setFloatingPanel: (BOOL)flag +{ + _isFloatingPanel = flag; +} + +- (BOOL) worksWhenModal +{ + return _worksWhenModal; +} + +- (void) setWorksWhenModal: (BOOL)flag +{ + _worksWhenModal = flag; +} + +- (BOOL) becomesKeyOnlyIfNeeded +{ + return _becomesKeyOnlyIfNeeded; +} + +- (void)setBecomesKeyOnlyIfNeeded: (BOOL)flag { _becomesKeyOnlyIfNeeded = flag; } @@ -97,16 +119,16 @@ // // NSCoding protocol // -- (void)encodeWithCoder:aCoder +- (void) encodeWithCoder: (NSCoder*)aCoder { - [super encodeWithCoder:aCoder]; + [super encodeWithCoder:aCoder]; } -- initWithCoder:aDecoder +- initWithCoder: (NSCoder*)aDecoder { - [super initWithCoder:aDecoder]; + [super initWithCoder:aDecoder]; - return self; + return self; } @end /* NSPanel */ @@ -123,7 +145,9 @@ @class GSAlertPanel; static GSAlertPanel *standardAlertPanel = nil; -static GSAlertPanel *reusableAlertPanel = nil; +static GSAlertPanel *informationalAlertPanel = nil; +static GSAlertPanel *criticalAlertPanel = nil; +static GSAlertPanel *gmodelAlertPanel = nil; @interface GSAlertPanel : NSPanel { @@ -136,7 +160,7 @@ static GSAlertPanel *reusableAlertPanel = nil; int result; BOOL active; } -- (void) buttonAction: (id)sender; +- (void) buttonAction: (id) sender; - (int) result; - (int) runModal; - (void) setTitle: (NSString*)title @@ -159,6 +183,20 @@ static GSAlertPanel *reusableAlertPanel = nil; } } ++ (id) createObjectForModelUnarchiver: (GMUnarchiver*)unarchiver +{ + unsigned backingType = [unarchiver decodeUnsignedIntWithName: + @"backingType"]; + unsigned styleMask = [unarchiver decodeUnsignedIntWithName:@"styleMask"]; + NSRect aRect = [unarchiver decodeRectWithName:@"frame"]; + NSPanel* panel = [[[GSAlertPanel allocWithZone:[unarchiver objectZone]] + initWithContentRect:aRect + styleMask:styleMask backing:backingType defer:YES] + autorelease]; + + return panel; +} + - (void) buttonAction: (id)sender { if (active == NO) @@ -190,6 +228,12 @@ static GSAlertPanel *reusableAlertPanel = nil; - (void) dealloc { + if (self == standardAlertPanel) + standardAlertPanel = nil; + if (self == informationalAlertPanel) + informationalAlertPanel = nil; + if (self == criticalAlertPanel) + criticalAlertPanel = nil; [defButton release]; [altButton release]; [othButton release]; @@ -201,8 +245,13 @@ static GSAlertPanel *reusableAlertPanel = nil; - (void) encodeWithModelArchiver: (GMArchiver *)archiver { - if (standardAlertPanel) - [archiver encodeObject: standardAlertPanel withName: @"AlertPanel"]; + [super encodeWithModelArchiver: archiver]; + [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) initWithContentRect: (NSRect)r @@ -322,10 +371,15 @@ static GSAlertPanel *reusableAlertPanel = nil; - (id) initWithModelUnarchiver: (GMUnarchiver*)unarchiver { - if (!standardAlertPanel) - standardAlertPanel = [unarchiver decodeObjectWithName: @"AlertPanel"]; - [self release]; - return standardAlertPanel; + self = [super initWithModelUnarchiver: unarchiver]; + defButton = [[unarchiver decodeObjectWithName: @"DefaultButton"] retain]; + altButton = [[unarchiver decodeObjectWithName: @"AlternateButton"] retain]; + othButton = [[unarchiver decodeObjectWithName: @"OtherButton"] retain]; + icoButton = [[unarchiver decodeObjectWithName: @"IconButton"] retain]; + messageField = [[unarchiver decodeObjectWithName: @"MessageField"] retain]; + titleField = [[unarchiver decodeObjectWithName: @"TitleField"] retain]; + gmodelAlertPanel = self; + return gmodelAlertPanel; } - (int) result @@ -483,16 +537,31 @@ NSGetCriticalAlertPanel(NSString *title, NSString *otherButton, ...) { va_list ap; - NSPanel *panel; + NSString *message; + GSAlertPanel *panel; if (title == nil) title = @"Warning"; va_start (ap, otherButton); - panel = NSGetAlertPanel(title, msg, defaultButton, - alternateButton, otherButton, ap); + message = [NSString stringWithFormat: msg arguments: ap]; va_end (ap); - return [panel retain]; + if (criticalAlertPanel == nil) + { + panel = NSGetAlertPanel(title, msg, defaultButton, + alternateButton, otherButton, ap); + criticalAlertPanel = panel; + } + else + panel = criticalAlertPanel; + + [panel setTitle: @"Critical"]; + [panel setTitle: title + message: message + def: defaultButton + alt: alternateButton + other: otherButton]; + return panel; } id @@ -503,22 +572,40 @@ NSGetInformationalAlertPanel(NSString *title, NSString *otherButton, ...) { va_list ap; - NSPanel *panel; + NSString *message; + GSAlertPanel *panel; if (title == nil) title = @"Information"; va_start (ap, otherButton); - panel = NSGetAlertPanel(title, msg, defaultButton, - alternateButton, otherButton, ap); + message = [NSString stringWithFormat: msg arguments: ap]; va_end (ap); - return [panel retain]; + if (informationalAlertPanel == nil) + { + panel = NSGetAlertPanel(title, msg, defaultButton, + alternateButton, otherButton, ap); + informationalAlertPanel = panel; + } + else + panel = informationalAlertPanel; + + [panel setTitle: @"Information"]; + [panel setTitle: title + message: message + def: defaultButton + alt: alternateButton + other: otherButton]; + return panel; } void NSReleaseAlertPanel(id alertPanel) { - [alertPanel release]; + if (alertPanel != standardAlertPanel && + alertPanel != informationalAlertPanel && + alertPanel != criticalAlertPanel) + [alertPanel release]; } int @@ -542,10 +629,9 @@ NSRunAlertPanel(NSString *title, message = [NSString stringWithFormat: msg arguments: ap]; va_end (ap); - if (reusableAlertPanel) + if (standardAlertPanel) { - panel = reusableAlertPanel; - reusableAlertPanel = nil; + panel = standardAlertPanel; [panel setTitle: title message: message def: defaultButton @@ -556,15 +642,115 @@ NSRunAlertPanel(NSString *title, { panel = NSGetAlertPanel(title, message, defaultButton, alternateButton, otherButton, ap); + standardAlertPanel = panel; } result = [panel runModal]; - - if (reusableAlertPanel == nil) - reusableAlertPanel = panel; - else - NSReleaseAlertPanel(panel); - + NSReleaseAlertPanel(panel); + return result; +} + +int +NSRunCriticalAlertPanel(NSString *title, + NSString *msg, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, ...) +{ + va_list ap; + GSAlertPanel *panel; + int result; + + va_start (ap, otherButton); + panel = NSGetCriticalAlertPanel(title, msg, + defaultButton, alternateButton, otherButton, ap); + va_end (ap); + + result = [panel runModal]; + NSReleaseAlertPanel(panel); + return result; +} + +int +NSRunInformationalAlertPanel(NSString *title, + NSString *msg, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, ...) +{ + va_list ap; + GSAlertPanel *panel; + int result; + + va_start (ap, otherButton); + panel = NSGetInformationalAlertPanel(title, msg, + defaultButton, alternateButton, otherButton, ap); + va_end (ap); + + result = [panel runModal]; + NSReleaseAlertPanel(panel); + return result; +} + +int +NSRunLocalizedAlertPanel(NSString *table, + NSString *title, + NSString *msg, + NSString *defaultButton, + NSString *alternateButton, + NSString *otherButton, ...) +{ + va_list ap; + GSAlertPanel *panel; + NSString *message; + int result; + NSBundle *bundle = [NSBundle mainBundle]; + + if (title == nil) + title = @"Alert"; + + title = [bundle localizedStringForKey: title + value: title + table: table]; + if (defaultButton) + defaultButton = [bundle localizedStringForKey: defaultButton + value: defaultButton + table: table]; + if (alternateButton) + alternateButton = [bundle localizedStringForKey: alternateButton + value: alternateButton + table: table]; + if (otherButton) + otherButton = [bundle localizedStringForKey: otherButton + value: otherButton + table: table]; + if (msg) + msg = [bundle localizedStringForKey: msg + value: msg + table: table]; + + va_start (ap, otherButton); + message = [NSString stringWithFormat: msg arguments: ap]; + va_end (ap); + + if (standardAlertPanel) + { + panel = standardAlertPanel; + [panel setTitle: title + message: message + def: defaultButton + alt: alternateButton + other: otherButton]; + } + else + { + panel = NSGetAlertPanel(title, message, defaultButton, + alternateButton, otherButton, ap); + standardAlertPanel = panel; + } + + result = [panel runModal]; + NSReleaseAlertPanel(panel); return result; }