mirror of
https://github.com/gnustep/libs-gui.git
synced 2025-06-01 20:01:56 +00:00
Added code to resize GSAlertPanel's buttons and panel to deal with
long strings passed as messages or button strings git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/gui/trunk@5760 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
parent
ba3d56a887
commit
edcc3ebd8a
1 changed files with 118 additions and 26 deletions
144
Source/NSPanel.m
144
Source/NSPanel.m
|
@ -181,6 +181,7 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
|
|
||||||
@interface GSAlertPanel : NSPanel
|
@interface GSAlertPanel : NSPanel
|
||||||
{
|
{
|
||||||
|
NSSize originalSize;
|
||||||
NSButton *defButton;
|
NSButton *defButton;
|
||||||
NSButton *altButton;
|
NSButton *altButton;
|
||||||
NSButton *othButton;
|
NSButton *othButton;
|
||||||
|
@ -275,6 +276,7 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
- (void) encodeWithModelArchiver: (GMArchiver *)archiver
|
- (void) encodeWithModelArchiver: (GMArchiver *)archiver
|
||||||
{
|
{
|
||||||
[super encodeWithModelArchiver: archiver];
|
[super encodeWithModelArchiver: archiver];
|
||||||
|
[archiver encodeSize: originalSize withName: @"OriginalSize"];
|
||||||
[archiver encodeObject: defButton withName: @"DefaultButton"];
|
[archiver encodeObject: defButton withName: @"DefaultButton"];
|
||||||
[archiver encodeObject: altButton withName: @"AlternateButton"];
|
[archiver encodeObject: altButton withName: @"AlternateButton"];
|
||||||
[archiver encodeObject: othButton withName: @"OtherButton"];
|
[archiver encodeObject: othButton withName: @"OtherButton"];
|
||||||
|
@ -304,6 +306,7 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
NSRect rect;
|
NSRect rect;
|
||||||
NSBox *box;
|
NSBox *box;
|
||||||
|
|
||||||
|
originalSize = r.size;
|
||||||
[self setMaxSize: r.size];
|
[self setMaxSize: r.size];
|
||||||
[self setMinSize: r.size];
|
[self setMinSize: r.size];
|
||||||
[self setTitle: @" "];
|
[self setTitle: @" "];
|
||||||
|
@ -368,11 +371,11 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
|
|
||||||
rect.size.height = 36.0;
|
rect.size.height = 36.0;
|
||||||
rect.size.width = 344.0;
|
rect.size.width = 344.0;
|
||||||
rect.origin.y = 46.0;
|
rect.origin.y = 55.0;
|
||||||
rect.origin.x = 8.0;
|
rect.origin.x = 8.0;
|
||||||
messageField = [[NSTextField alloc] initWithFrame: rect];
|
messageField = [[NSTextField alloc] initWithFrame: rect];
|
||||||
[messageField setAutoresizingMask:
|
[messageField setAutoresizingMask:
|
||||||
NSViewWidthSizable | NSViewHeightSizable | NSViewMaxYMargin];
|
NSViewMinYMargin | NSViewMaxYMargin];
|
||||||
[messageField setEditable: NO];
|
[messageField setEditable: NO];
|
||||||
[messageField setSelectable: NO];
|
[messageField setSelectable: NO];
|
||||||
[messageField setBezeled: NO];
|
[messageField setBezeled: NO];
|
||||||
|
@ -386,7 +389,7 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
rect.origin.y = 121.0;
|
rect.origin.y = 121.0;
|
||||||
rect.origin.x = 64.0;
|
rect.origin.x = 64.0;
|
||||||
titleField = [[NSTextField alloc] initWithFrame: rect];
|
titleField = [[NSTextField alloc] initWithFrame: rect];
|
||||||
[titleField setAutoresizingMask: NSViewWidthSizable | NSViewMinYMargin];
|
[titleField setAutoresizingMask: NSViewMinYMargin];
|
||||||
[titleField setEditable: NO];
|
[titleField setEditable: NO];
|
||||||
[titleField setSelectable: NO];
|
[titleField setSelectable: NO];
|
||||||
[titleField setBezeled: NO];
|
[titleField setBezeled: NO];
|
||||||
|
@ -401,6 +404,7 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
- (id) initWithModelUnarchiver: (GMUnarchiver*)unarchiver
|
- (id) initWithModelUnarchiver: (GMUnarchiver*)unarchiver
|
||||||
{
|
{
|
||||||
self = [super initWithModelUnarchiver: unarchiver];
|
self = [super initWithModelUnarchiver: unarchiver];
|
||||||
|
originalSize = [unarchiver decodeSizeWithName: @"OriginalSize"];
|
||||||
defButton = [[unarchiver decodeObjectWithName: @"DefaultButton"] retain];
|
defButton = [[unarchiver decodeObjectWithName: @"DefaultButton"] retain];
|
||||||
altButton = [[unarchiver decodeObjectWithName: @"AlternateButton"] retain];
|
altButton = [[unarchiver decodeObjectWithName: @"AlternateButton"] retain];
|
||||||
othButton = [[unarchiver decodeObjectWithName: @"OtherButton"] retain];
|
othButton = [[unarchiver decodeObjectWithName: @"OtherButton"] retain];
|
||||||
|
@ -431,27 +435,33 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
other: (NSString*)otherButton
|
other: (NSString*)otherButton
|
||||||
{
|
{
|
||||||
NSView *content = [self contentView];
|
NSView *content = [self contentView];
|
||||||
float maxX = NSMaxX([content bounds]) - 5;
|
float button_origin;
|
||||||
float diff;
|
float excessWidth = 0;
|
||||||
|
float width;
|
||||||
NSRect bRect;
|
NSRect bRect;
|
||||||
|
NSSize buttonSize = NSMakeSize (72, 24);
|
||||||
|
int numberOfButtons = 0;
|
||||||
|
|
||||||
if ([icoButton superview] == nil)
|
/* First step, get minimum size needed for objects */
|
||||||
|
/* Put all the objects in the panel now, otherwise resizing the panel
|
||||||
|
won't do the vertical position computations for us */
|
||||||
|
if ([icoButton superview] == nil)
|
||||||
[content addSubview: icoButton];
|
[content addSubview: icoButton];
|
||||||
|
|
||||||
|
/* NB: Need to make all the buttons of the same size */
|
||||||
if (defaultButton != nil)
|
if (defaultButton != nil)
|
||||||
{
|
{
|
||||||
|
numberOfButtons++;
|
||||||
[defButton setTitle: defaultButton];
|
[defButton setTitle: defaultButton];
|
||||||
[defButton sizeToFit];
|
[defButton sizeToFit];
|
||||||
|
bRect = [defButton frame];
|
||||||
|
if (bRect.size.height > buttonSize.height)
|
||||||
|
buttonSize.height = bRect.size.height;
|
||||||
|
if (bRect.size.width > buttonSize.width)
|
||||||
|
buttonSize.width = bRect.size.width;
|
||||||
|
|
||||||
if ([defButton superview] == nil)
|
if ([defButton superview] == nil)
|
||||||
[content addSubview: defButton];
|
[content addSubview: defButton];
|
||||||
|
|
||||||
bRect = [defButton frame];
|
|
||||||
diff = maxX - NSMaxX(bRect);
|
|
||||||
bRect.origin.x += diff;
|
|
||||||
[defButton setFrame: bRect];
|
|
||||||
maxX = NSMinX(bRect) - 10;
|
|
||||||
|
|
||||||
[self makeFirstResponder: defButton];
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -461,16 +471,17 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
|
|
||||||
if (alternateButton != nil)
|
if (alternateButton != nil)
|
||||||
{
|
{
|
||||||
|
numberOfButtons++;
|
||||||
[altButton setTitle: alternateButton];
|
[altButton setTitle: alternateButton];
|
||||||
[altButton sizeToFit];
|
[altButton sizeToFit];
|
||||||
|
bRect = [altButton frame];
|
||||||
|
if (bRect.size.height > buttonSize.height)
|
||||||
|
buttonSize.height = bRect.size.height;
|
||||||
|
if (bRect.size.width > buttonSize.width)
|
||||||
|
buttonSize.width = bRect.size.width;
|
||||||
|
|
||||||
if ([altButton superview] == nil)
|
if ([altButton superview] == nil)
|
||||||
[content addSubview: altButton];
|
[content addSubview: altButton];
|
||||||
|
|
||||||
bRect = [altButton frame];
|
|
||||||
diff = maxX - NSMaxX(bRect);
|
|
||||||
bRect.origin.x += diff;
|
|
||||||
[altButton setFrame: bRect];
|
|
||||||
maxX = NSMinX(bRect) - 10;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -480,16 +491,17 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
|
|
||||||
if (otherButton != nil)
|
if (otherButton != nil)
|
||||||
{
|
{
|
||||||
|
numberOfButtons++;
|
||||||
[othButton setTitle: otherButton];
|
[othButton setTitle: otherButton];
|
||||||
[othButton sizeToFit];
|
[othButton sizeToFit];
|
||||||
|
bRect = [othButton frame];
|
||||||
|
if (bRect.size.height > buttonSize.height)
|
||||||
|
buttonSize.height = bRect.size.height;
|
||||||
|
if (bRect.size.width > buttonSize.width)
|
||||||
|
buttonSize.width = bRect.size.width;
|
||||||
|
|
||||||
if ([othButton superview] == nil)
|
if ([othButton superview] == nil)
|
||||||
[content addSubview: othButton];
|
[content addSubview: othButton];
|
||||||
|
|
||||||
bRect = [othButton frame];
|
|
||||||
diff = maxX - NSMaxX(bRect);
|
|
||||||
bRect.origin.x += diff;
|
|
||||||
[othButton setFrame: bRect];
|
|
||||||
maxX = NSMinX(bRect) - 10;
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -497,10 +509,31 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
[othButton removeFromSuperview];
|
[othButton removeFromSuperview];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (numberOfButtons > 0)
|
||||||
|
{
|
||||||
|
// Compute total width taken up by buttons
|
||||||
|
// buttonSize: size of a button
|
||||||
|
// 10: spacing between buttons
|
||||||
|
// 5: spacing between last button and window border
|
||||||
|
width = (10 + buttonSize.width) * numberOfButtons;
|
||||||
|
|
||||||
|
// If needed, take note that we have to resize the panel
|
||||||
|
if (width - originalSize.width > excessWidth)
|
||||||
|
excessWidth = width - originalSize.width;
|
||||||
|
|
||||||
|
// Secondary TODO: Check also height.
|
||||||
|
}
|
||||||
|
|
||||||
if (message != nil)
|
if (message != nil)
|
||||||
{
|
{
|
||||||
[messageField setStringValue: message];
|
[messageField setStringValue: message];
|
||||||
[messageField sizeToFit];
|
[messageField sizeToFit];
|
||||||
|
// 5: spacing between text and window borders
|
||||||
|
width = [messageField frame].size.width + 10;
|
||||||
|
|
||||||
|
if (width - originalSize.width > excessWidth)
|
||||||
|
excessWidth = width - originalSize.width;
|
||||||
|
|
||||||
if ([messageField superview] == nil)
|
if ([messageField superview] == nil)
|
||||||
[content addSubview: messageField];
|
[content addSubview: messageField];
|
||||||
}
|
}
|
||||||
|
@ -514,6 +547,8 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
{
|
{
|
||||||
[titleField setStringValue: title];
|
[titleField setStringValue: title];
|
||||||
[titleField sizeToFit];
|
[titleField sizeToFit];
|
||||||
|
// TODO: Check title dimension
|
||||||
|
|
||||||
if ([titleField superview] == nil)
|
if ([titleField superview] == nil)
|
||||||
[content addSubview: titleField];
|
[content addSubview: titleField];
|
||||||
}
|
}
|
||||||
|
@ -523,6 +558,63 @@ static GSAlertPanel *gmodelAlertPanel = nil;
|
||||||
[titleField removeFromSuperview];
|
[titleField removeFromSuperview];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Second step, resize the panel to comfortable size */
|
||||||
|
if (excessWidth > 0)
|
||||||
|
{
|
||||||
|
NSSize s = NSMakeSize (originalSize.width + excessWidth,
|
||||||
|
originalSize.height + (excessWidth/2));
|
||||||
|
[self setMaxSize: s];
|
||||||
|
[self setMinSize: s];
|
||||||
|
[self setContentSize: s];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Resize the panel to standard dimension
|
||||||
|
[self setMaxSize: originalSize];
|
||||||
|
[self setMinSize: originalSize];
|
||||||
|
[self setContentSize: originalSize];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Third step, now that the panel size is OK, move objects
|
||||||
|
in the right position */
|
||||||
|
button_origin = [content bounds].size.width - 5 - buttonSize.width;
|
||||||
|
|
||||||
|
if (defaultButton != nil)
|
||||||
|
{
|
||||||
|
bRect.size = buttonSize;
|
||||||
|
bRect.origin.x = button_origin;
|
||||||
|
bRect.origin.y = [defButton frame].origin.y;
|
||||||
|
[defButton setFrame: bRect];
|
||||||
|
button_origin -= 10 + buttonSize.width;
|
||||||
|
|
||||||
|
[self makeFirstResponder: defButton];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (alternateButton != nil)
|
||||||
|
{
|
||||||
|
bRect.size = buttonSize;
|
||||||
|
bRect.origin.x = button_origin;
|
||||||
|
bRect.origin.y = [altButton frame].origin.y;
|
||||||
|
[altButton setFrame: bRect];
|
||||||
|
button_origin -= 10 + buttonSize.width;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (otherButton != nil)
|
||||||
|
{
|
||||||
|
bRect.size = buttonSize;
|
||||||
|
bRect.origin.x = button_origin;
|
||||||
|
bRect.origin.y = [othButton frame].origin.y;
|
||||||
|
[othButton setFrame: bRect];
|
||||||
|
}
|
||||||
|
|
||||||
|
if (message != nil)
|
||||||
|
{
|
||||||
|
// Center the message
|
||||||
|
bRect = [messageField frame];
|
||||||
|
bRect.origin.x = ([content frame].size.width - bRect.size.width) / 2;
|
||||||
|
[messageField setFrameOrigin: NSMakePoint (bRect.origin.x, bRect.origin.y)];
|
||||||
|
}
|
||||||
|
|
||||||
result = NSAlertErrorReturn; /* If no button was pressed */
|
result = NSAlertErrorReturn; /* If no button was pressed */
|
||||||
[content display];
|
[content display];
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue