Implemented coding for NSPanel. For GSAlertPanel reoranised the

initXXX code. Moved the GModel methods into a separate category,
removing [createObjectForModelUnarchiver:] as this was (incorrectly)
duplication code from the super class.
Implemented NSBeginAlertSheet(), NSBeginCriticalAlertSheet() and
NSBeginInformationalAlertSheet().


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@11000 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Fred Kiefer 2001-09-24 23:30:36 +00:00
parent ccf62d14f7
commit bce2688968

View file

@ -167,12 +167,28 @@ static NSString *defaultTitle = @" ";
*/ */
- (void) encodeWithCoder: (NSCoder*)aCoder - (void) encodeWithCoder: (NSCoder*)aCoder
{ {
BOOL flag;
[super encodeWithCoder: aCoder]; [super encodeWithCoder: aCoder];
flag = _becomesKeyOnlyIfNeeded;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _isFloatingPanel;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
flag = _worksWhenModal;
[aCoder encodeValueOfObjCType: @encode(BOOL) at: &flag];
} }
- (id) initWithCoder: (NSCoder*)aDecoder - (id) initWithCoder: (NSCoder*)aDecoder
{ {
BOOL flag;
[super initWithCoder: aDecoder]; [super initWithCoder: aDecoder];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
[self setBecomesKeyOnlyIfNeeded: flag];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
[self setFloatingPanel: flag];
[aDecoder decodeValueOfObjCType: @encode(BOOL) at: &flag];
[self setWorksWhenModal: flag];
return self; return self;
} }
@ -288,7 +304,6 @@ static NSString *defaultTitle = @" ";
static GSAlertPanel *standardAlertPanel = nil; static GSAlertPanel *standardAlertPanel = nil;
static GSAlertPanel *informationalAlertPanel = nil; static GSAlertPanel *informationalAlertPanel = nil;
static GSAlertPanel *criticalAlertPanel = nil; static GSAlertPanel *criticalAlertPanel = nil;
static GSAlertPanel *gmodelAlertPanel = nil;
@interface GSAlertPanel: NSPanel @interface GSAlertPanel: NSPanel
{ {
@ -301,15 +316,9 @@ static GSAlertPanel *gmodelAlertPanel = nil;
NSScrollView *scroll; NSScrollView *scroll;
int result; int result;
BOOL isGreen; // we were unarchived and not resized. BOOL isGreen; // we were unarchived and not resized.
//PJB: I removed the active flag. Please see the isActivePanel method below.
} }
- (id) initWithContentRect: (NSRect)r - (id) _initWithoutGModel;
styleMask: (unsigned)m
backing: (NSBackingStoreType)b
defer: (BOOL)d
screen: (NSScreen*)s;
- (id) initWithModelUnarchiver: (GMUnarchiver*)unarchiver;
- (int) runModal; - (int) runModal;
- (void) setTitle: (NSString*)title - (void) setTitle: (NSString*)title
message: (NSString*)message message: (NSString*)message
@ -358,16 +367,16 @@ static const float ButtonMinWidth = 72.0;
} }
} }
+ (id) createObjectForModelUnarchiver: (GMUnarchiver*)unarchiver - (id) init
{ {
unsigned backingType = [unarchiver decodeUnsignedIntWithName: /*
@"backingType"]; if (![NSBundle loadNibNamed: @"AlertPanel" owner: self])
unsigned styleMask = [unarchiver decodeUnsignedIntWithName: @"styleMask"]; {
NSRect aRect = [unarchiver decodeRectWithName: @"frame"]; NSLog(@"cannot open alert panel model file\n");
NSPanel* panel = AUTORELEASE([[GSAlertPanel allocWithZone: return nil;
[unarchiver objectZone]] initWithContentRect: aRect }
styleMask: styleMask backing: backingType defer: YES]); */
return panel; return [self _initWithoutGModel];
} }
- (void) dealloc - (void) dealloc
@ -394,18 +403,6 @@ static const float ButtonMinWidth = 72.0;
[super dealloc]; [super dealloc];
} }
- (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"];
}
static NSScrollView* static NSScrollView*
makeScrollViewWithRect(NSRect rect) makeScrollViewWithRect(NSRect rect)
{ {
@ -422,7 +419,6 @@ makeScrollViewWithRect(NSRect rect)
return scroll; return scroll;
} }
- (NSButton*) _makeButtonWithRect: (NSRect)rect - (NSButton*) _makeButtonWithRect: (NSRect)rect
{ {
NSButton *button = [[NSButton alloc] initWithFrame: rect]; NSButton *button = [[NSButton alloc] initWithFrame: rect];
@ -463,25 +459,25 @@ setControl(NSView* content, id control, NSString *title)
} }
} }
- (id) initWithContentRect: (NSRect)r - (id) _initWithoutGModel
styleMask: (unsigned)m
backing: (NSBackingStoreType)b
defer: (BOOL)d
screen: (NSScreen*)s
{
self = [super initWithContentRect: r
styleMask: m
backing: b
defer: d
screen: s];
if (self != nil)
{ {
NSRect rect; NSRect rect;
NSImage *image; NSImage *image;
NSBox *box; NSBox *box;
NSView *content = [self contentView]; NSView *content;
NSRect r = NSMakeRect(0.0, 0.0, WinMinWidth, WinMinHeight);
self = [self initWithContentRect: r
styleMask: NSTitledWindowMask
backing: NSBackingStoreRetained
defer: YES
screen: nil];
if (self == nil)
return nil;
[self setTitle: @" "]; [self setTitle: @" "];
content = [self contentView];
// we're an ATTENTION panel, therefore: // we're an ATTENTION panel, therefore:
[self setHidesOnDeactivate: NO]; [self setHidesOnDeactivate: NO];
@ -501,7 +497,6 @@ setControl(NSView* content, id control, NSString *title)
[icoButton setImage: image]; [icoButton setImage: image];
[content addSubview: icoButton]; [content addSubview: icoButton];
rect.size.height = 20.0; // will be sized to fit anyway. rect.size.height = 20.0; // will be sized to fit anyway.
rect.size.width = 80.0; // will be sized to fit anyway. rect.size.width = 80.0; // will be sized to fit anyway.
rect.origin.y = r.origin.y + r.size.height + IconBottom; rect.origin.y = r.origin.y + r.size.height + IconBottom;
@ -527,7 +522,6 @@ setControl(NSView* content, id control, NSString *title)
RELEASE(box); RELEASE(box);
// Then, make the subviews that'll be sized by sizePanelToFit; // Then, make the subviews that'll be sized by sizePanelToFit;
rect.size.height = 20.0; rect.size.height = 20.0;
rect.size.width = 80.0; rect.size.width = 80.0;
rect.origin.y = 0.0; rect.origin.y = 0.0;
@ -563,34 +557,10 @@ setControl(NSView* content, id control, NSString *title)
result = NSAlertErrorReturn; result = NSAlertErrorReturn;
isGreen = YES; isGreen = YES;
}
return self; return self;
} }
- (id) initWithModelUnarchiver: (GMUnarchiver*)unarchiver
{
self = [super initWithModelUnarchiver: unarchiver];
if (self != nil)
{
// TODO: Remove the following line when NSPanel archiver method will be implemented.
[self setBecomesKeyOnlyIfNeeded: NO];
(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;
}
gmodelAlertPanel = self;
return gmodelAlertPanel;
}
- (void) sizePanelToFit - (void) sizePanelToFit
{ {
NSRect bounds; NSRect bounds;
@ -853,7 +823,7 @@ setControl(NSView* content, id control, NSString *title)
NSLog(@"alert panel buttonAction: from unknown sender - x%x\n", NSLog(@"alert panel buttonAction: from unknown sender - x%x\n",
(unsigned)sender); (unsigned)sender);
} }
[[NSApplication sharedApplication] stopModal]; [NSApp stopModalWithCode: result];
} }
- (int) result - (int) result
@ -863,7 +833,7 @@ setControl(NSView* content, id control, NSString *title)
- (BOOL) isActivePanel - (BOOL) isActivePanel
{ {
return [[NSApplication sharedApplication]modalWindow] == self; return [NSApp modalWindow] == self;
} }
- (int) runModal - (int) runModal
@ -872,6 +842,7 @@ setControl(NSView* content, id control, NSString *title)
{ {
[self sizePanelToFit]; [self sizePanelToFit];
} }
[NSApp runModalForWindow: self]; [NSApp runModalForWindow: self];
[self orderOut: self]; [self orderOut: self];
return result; return result;
@ -886,10 +857,13 @@ setControl(NSView* content, id control, NSString *title)
NSView *content = [self contentView]; NSView *content = [self contentView];
setControl(content, titleField, title); setControl(content, titleField, title);
if (useControl(scroll))
{
// TODO: Remove the following line once NSView is corrected. // TODO: Remove the following line once NSView is corrected.
[scroll setDocumentView: nil]; [scroll setDocumentView: nil];
[scroll removeFromSuperview]; [scroll removeFromSuperview];
[messageField removeFromSuperview]; [messageField removeFromSuperview];
}
setControl(content, messageField, message); setControl(content, messageField, message);
setControl(content, defButton, defaultButton); setControl(content, defButton, defaultButton);
setControl(content, altButton, alternateButton); setControl(content, altButton, alternateButton);
@ -961,6 +935,42 @@ setControl(NSView* content, id control, NSString *title)
@end /* GSAlertPanel */ @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 */
/* /*
These functions may be called "recursively". For example, from a These functions may be called "recursively". For example, from a
@ -981,28 +991,10 @@ setControl(NSView* content, id control, NSString *title)
*/ */
#define NEW_PANEL [[GSAlertPanel alloc] \
initWithContentRect: NSMakeRect(0.0, 0.0, WinMinWidth, WinMinHeight) \
styleMask: NSTitledWindowMask \
backing: NSBackingStoreRetained \
defer: YES \
screen: nil]
/*
if (![GMModel loadIMFile: @"AlertPanel" owner: [GSAlertPanel alloc]])
{
NSLog(@"cannot open alert panel model file\n");
return nil;
}
*/
/* /*
TODO: Check if this discrepancy is wanted and needed. TODO: Check if this discrepancy is wanted and needed.
If not, we could merge these parameters, even If not, we could merge these parameters, even
for the alert panel, setting its window title to "Alert". for the alert panel, setting its window title to "Alert".
*/ */
static GSAlertPanel* static GSAlertPanel*
@ -1021,7 +1013,7 @@ getSomePanel(
{ {
if ([*instance isActivePanel]) if ([*instance isActivePanel])
{ // c: { // c:
panel = NEW_PANEL; panel = [[GSAlertPanel alloc] init];
} }
else else
{ // b: { // b:
@ -1030,7 +1022,7 @@ getSomePanel(
} }
else else
{ // a: { // a:
panel = NEW_PANEL; panel = [[GSAlertPanel alloc] init];
*instance = panel; *instance = panel;
} }
@ -1266,7 +1258,30 @@ void NSBeginAlertSheet(NSString *title,
void *contextInfo, void *contextInfo,
NSString *msg, ...) NSString *msg, ...)
{ {
// TODO 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 = getSomePanel(&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: didEndSelector
contextInfo: contextInfo];
[panel close];
NSReleaseAlertPanel(panel);
} }
void NSBeginCriticalAlertSheet(NSString *title, void NSBeginCriticalAlertSheet(NSString *title,
@ -1280,7 +1295,25 @@ void NSBeginCriticalAlertSheet(NSString *title,
void *contextInfo, void *contextInfo,
NSString *msg, ...) NSString *msg, ...)
{ {
// TODO va_list ap;
NSString *message;
GSAlertPanel *panel;
va_start(ap, msg);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
panel = getSomePanel(&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: didEndSelector
contextInfo: contextInfo];
[panel close];
NSReleaseAlertPanel(panel);
} }
void NSBeginInformationalAlertSheet(NSString *title, void NSBeginInformationalAlertSheet(NSString *title,
@ -1294,6 +1327,26 @@ void NSBeginInformationalAlertSheet(NSString *title,
void *contextInfo, void *contextInfo,
NSString *msg, ...) NSString *msg, ...)
{ {
// TODO va_list ap;
NSString *message;
GSAlertPanel *panel;
va_start(ap, msg);
message = [NSString stringWithFormat: msg arguments: ap];
va_end(ap);
panel = getSomePanel(&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: didEndSelector
contextInfo: contextInfo];
[panel close];
NSReleaseAlertPanel(panel);
} }