1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
//
// MDialogs.m
// MySQLGUICommon
//
// Created by Alfredo Kojima on Sat Jul 10 2004.
// Copyright (c) 2004 MySQL AB. All rights reserved.
//
#import "MDialogs.h"
@implementation MStringRequestSheet
+ (MStringRequestSheet*)sheetWithTitle:(NSString*)title labels:(NSArray*)labels;
{
return [[[MStringRequestSheet alloc] initWithTitle:title labels:labels] autorelease];
}
+ (MStringRequestSheet*)sheetWithTitle:(NSString*)title label:(NSString*)label
{
return [[[MStringRequestSheet alloc] initWithTitle:title labels:[NSArray arrayWithObject:label]] autorelease];
}
- (id)initWithTitle:(NSString*)title labels:(NSArray*)labels
{
return [self initWithTitle:title labels:labels buttons:nil];
}
- (id)initWithTitle:(NSString*)title labels:(NSArray*)labels buttons:(NSArray*)buttons
{
NSRect contentRect;
contentRect= NSMakeRect(0.0, 0.0, 420.0, 128.0+([labels count]-1)*(22.0+8.0));
self= [super initWithContentRect:contentRect
styleMask:NSTitledWindowMask
backing:NSBackingStoreBuffered
defer:YES];
if (self)
{
NSView *contentView= [[NSView alloc] initWithFrame:contentRect];
id w;
unsigned int i;
[self setContentView:contentView];
[contentView release];
[self setHasShadow:YES];
w= [[NSTextField alloc] initWithFrame:NSMakeRect(17.0, contentRect.size.height-34.0, 387.0, 17.0)];
[w setStringValue:title];
[contentView addSubview:w];
[w release];
[w setFont:[NSFont boldSystemFontOfSize:[NSFont systemFontSize]]];
[w setAlignment:NSCenterTextAlignment];
[w setEditable:NO];
[w setDrawsBackground:NO];
[w setBordered:NO];
form= [[NSForm alloc] initWithFrame:NSMakeRect(20.0, 59.0,
380.0, 22+([labels count]-1)*(22.0+8.0))];
[form setCellSize:NSMakeSize([form cellSize].width, 22.0)];
[contentView addSubview:form];
[form release];
for (i= 0; i < [labels count]; i++)
{
[form addEntry:[labels objectAtIndex:i]];
}
if (!buttons)
{
w= [[NSButton alloc] initWithFrame:NSMakeRect(316.0, 11.0, 90.0, 32.0)];
[w setTag:0];
[contentView addSubview:w];
[w setTitle:@"OK"];
[w release];
[w setAction:@selector(buttonClicked:)];
[w setTarget:self];
[[w cell] setBezelStyle:NSRoundedBezelStyle];
[self setDefaultButtonCell:[w cell]];
w= [[NSButton alloc] initWithFrame:NSMakeRect(224.0, 11.0, 90.0, 32.0)];
[w setTag:-1];
[contentView addSubview:w];
[w setTitle:@"Cancel"];
[w setKeyEquivalent:@"\E"];
[w release];
[w setAction:@selector(buttonClicked:)];
[w setTarget:self];
[[w cell] setBezelStyle:NSRoundedBezelStyle];
}
else
{
unsigned int i;
float x= 420-14;
NSFont *font= [NSFont systemFontOfSize:[NSFont systemFontSize]];
for (i= 0; i < [buttons count]; i++)
{
float size;
size= [font widthOfString:[buttons objectAtIndex:i]];
if (size < 90.0-20.0)
size= 90.0;
else
size+= 35.0;
w= [[NSButton alloc] initWithFrame:NSMakeRect(x-size, 11.0, size, 32.0)];
[w setTag:i];
[contentView addSubview:w];
[w setTitle:[buttons objectAtIndex:i]];
[w release];
[w setAction:@selector(buttonClicked:)];
[w setTarget:self];
[[w cell] setBezelStyle:NSRoundedBezelStyle];
x-= size+2;
}
// Last is supposed to be Escape
[w setKeyEquivalent:@"\E"];
}
}
return self;
}
- (void)buttonClicked:(id)sender
{
[NSApp endSheet:self];
button= [sender tag];
}
- (void)sheetDidEnd:(NSWindow *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo
{
[NSApp abortModal];
[sheet orderOut:self];
}
- (BOOL)acceptsFirstResponder
{
return YES;
}
- (NSArray*)runModal:(NSWindow*)window
{
NSMutableArray *array;
[NSApp beginSheet:self
modalForWindow:window
modalDelegate:self
didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:)
contextInfo:self];
[NSApp runModalForWindow:self];
if (button == -1)
return nil;
else
{
unsigned int i;
array= [NSMutableArray arrayWithCapacity:[form numberOfRows]];
for (i= 0; i < [form numberOfRows]; i++)
[array addObject:[[form cellAtIndex:i] stringValue]];
return array;
}
}
- (void)setDefaultValues:(NSArray*)array
{
unsigned int i;
for (i= 0; i < [form numberOfRows]; i++)
[[form cellAtIndex:i] setStringValue:[array objectAtIndex:i]];
}
- (int)clickedButton
{
return button;
}
@end
|