-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagramView.m
executable file
·705 lines (542 loc) · 19.6 KB
/
DiagramView.m
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
#import "DiagramView.h"
#import "SelData.h"
#import "Analysis.h"
static void *PropertyObservationContext = (void *)1091;
static void *GraphicsObservationContext = (void *)1092;
static void *SelectionIndexesObservationContext = (void *)1093;
static NSString *const GRAPHICS_BINDING_NAME = @"analysis";
static NSString * const SELECTIONINDEXES_BINDING_NAME = @"selectionIndexes";
@implementation DiagramView
+ (void)initialize
{
[self exposeBinding:GRAPHICS_BINDING_NAME];
[self exposeBinding:SELECTIONINDEXES_BINDING_NAME];
}
-(void)awakeFromNib
{
[self setBoundsOrigin:NSMakePoint(-48.0,-30.0)];
[self prepareAttributes];
bindingInfo = [[NSMutableDictionary alloc] init];
selArray = [[NSMutableArray alloc] init];
}
/*
nicht bei Objekten aus dem Nibfile 28/Jan/09
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
[self setBoundsOrigin:NSMakePoint(-50.0,-30.0)];
// selArray = [[NSMutableArray alloc] init];
[self prepareAttributes];
}
return self;
}
*/
- (void)prepareAttributes
{
attributes = [[NSMutableDictionary alloc] init];
[attributes setObject:[NSFont fontWithName:@"Helvetica" size:11]
forKey:NSFontAttributeName];
[attributes setObject:[NSColor blackColor]
forKey:NSForegroundColorAttributeName];
}
-(void)setXMin:(float)x
{
xMin = x;
}
-(void)setXMax:(float)x
{
xMax = x;
}
-(void)setYMax:(float)x
{
yMax = x;
}
-(void)setSelectedData:(NSArray *)theData
{
if (theData == selectedData)
return;
[selectedData release];
[theData retain];
selectedData = theData;
}
-(NSArray *)selectedData {return selectedData;}
- (void)setSelArray:(NSMutableArray *)aSelArray
{
if (selArray != aSelArray) {
[selArray release];
selArray = [aSelArray mutableCopy];
// [selTable reloadData];
}
}
/*
-(void)setSelArray:(NSMutableArray*)array
{
if (array == selArray)
return;
[selArray release];
[array retain];
selArray = array;
}
*/
// bindings-related -- infoForBinding and convenience methods
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
// NSLog(@"observeValueForKeyPath" );
if (context == GraphicsObservationContext)
{
/*
Should be able to use
NSArray *oldGraphics = [change objectForKey:NSKeyValueChangeOldKey];
etc. but the dictionary doesn't contain old and new arrays.
*/
NSArray *newAnalysis = [object valueForKeyPath:[self analysisKeyPath]];
NSMutableArray *onlyNew = [newAnalysis mutableCopy];
[onlyNew removeObjectsInArray:oldAnalysis];
[self startObservingAnalysis:onlyNew];
[onlyNew release];
NSMutableArray *removed = [oldAnalysis mutableCopy];
[removed removeObjectsInArray:newAnalysis];
[self stopObservingAnalysis:removed];
[removed release];
[self setOldAnalysis:newAnalysis];
// could check drawingBounds of old and new, but...
[self setNeedsDisplay:YES];
return;
}
if (context == PropertyObservationContext)
{
NSRect updateRect;
if ([keyPath isEqualToString:@"drawingBounds"])
{
NSRect newBounds = [[change objectForKey:NSKeyValueChangeNewKey] rectValue];
NSRect oldBounds = [[change objectForKey:NSKeyValueChangeOldKey] rectValue];
updateRect = NSUnionRect(newBounds,oldBounds);
}
else
{
updateRect = [(Analysis *)object drawingBounds];
}
updateRect = NSMakeRect(updateRect.origin.x-1.0,
updateRect.origin.y-1.0,
updateRect.size.width+2.0,
updateRect.size.height+2.0);
[self setNeedsDisplayInRect:updateRect];
return;
}
if (context == SelectionIndexesObservationContext)
{
[self setNeedsDisplay:YES];
return;
}
}
- (void)startObservingAnalysis:(NSArray *)analysis
{
if ([analysis isEqual:[NSNull null]])
{
return;
}
/*
Register to observe each of the new graphics, and each of their observable properties -- we need old and new values for drawingBounds to figure out what our dirty rect
*/
NSEnumerator *analysisEnumerator = [analysis objectEnumerator];
/*
Declare newGraphic as NSObject * to get key value observing methods
Add Graphic protocol for drawing
*/
Analysis *newAnalysis;
/*
Register as observer for all the drawing-related properties
*/
while (newAnalysis = [analysisEnumerator nextObject])
{
[newAnalysis addObserver:self
forKeyPath:@"drawingBounds"
options:(NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld)
context:PropertyObservationContext];
[newAnalysis addObserver:self
forKeyPath:@"drawingBounds"
options:0
context:PropertyObservationContext];
}
}
- (void)stopObservingAnalysis:(NSArray *)analysis
{
if ([analysis isEqual:[NSNull null]])
{
return;
}
NSEnumerator *graphicsEnumerator = [analysis objectEnumerator];
id oldGraphic;
while (oldAnalysis = [graphicsEnumerator nextObject])
{
[oldAnalysis removeObserver:self forKeyPath:@"drawingBounds"];
[oldAnalysis removeObserver:self forKeyPath:@"drawingContents"];
}
}
- (NSArray *)oldAnalysis
{
return oldAnalysis;
}
- (void)setOldAnalysis:(NSArray *)anOldAnalysis
{
if (oldAnalysis != anOldAnalysis) {
[oldAnalysis release];
oldAnalysis = [anOldAnalysis mutableCopy];
}
}
- (void)bind:(NSString *)bindingName
toObject:(id)observableObject
withKeyPath:(NSString *)observableKeyPath
options:(NSDictionary *)options
{
if ([bindingName isEqualToString:GRAPHICS_BINDING_NAME])
{
if ([bindingInfo objectForKey:GRAPHICS_BINDING_NAME] != nil)
{
// [self unbind:GRAPHICS_BINDING_NAME];
}
/*
observe the controller for changes -- note, pass binding identifier as the context, so we get that back in observeValueForKeyPath:... -- that way we can determine what needs to be updated
*/
NSDictionary *bindingsData = [NSDictionary dictionaryWithObjectsAndKeys:
observableObject, NSObservedObjectKey,
[[observableKeyPath copy] autorelease], NSObservedKeyPathKey,
[[options copy] autorelease], NSOptionsKey, nil];
[bindingInfo setObject:bindingsData forKey:GRAPHICS_BINDING_NAME];
[observableObject addObserver:self
forKeyPath:observableKeyPath
options:(NSKeyValueObservingOptionNew |
NSKeyValueObservingOptionOld)
context:GraphicsObservationContext];
[self startObservingAnalysis:[observableObject valueForKeyPath:observableKeyPath]];
}
else
if ([bindingName isEqualToString:SELECTIONINDEXES_BINDING_NAME])
{
if ([bindingInfo objectForKey:SELECTIONINDEXES_BINDING_NAME] != nil)
{
[self unbind:SELECTIONINDEXES_BINDING_NAME];
}
/*
observe the controller for changes -- note, pass binding identifier as the context, so we get that back in observeValueForKeyPath:... -- that way we can determine what needs to be updated
*/
NSDictionary *bindingsData = [NSDictionary dictionaryWithObjectsAndKeys:
observableObject, NSObservedObjectKey,
[[observableKeyPath copy] autorelease], NSObservedKeyPathKey,
[[options copy] autorelease], NSOptionsKey, nil];
[bindingInfo setObject:bindingsData forKey:SELECTIONINDEXES_BINDING_NAME];
[observableObject addObserver:self
forKeyPath:observableKeyPath
options:0
context:SelectionIndexesObservationContext];
}
else
{
/*
For every binding except "graphics" and "selectionIndexes" just use NSObject's default implementation. It will start observing the bound-to property. When a KVO notification is sent for the bound-to property, this object will be sent a [self setValue:theNewValue forKey:theBindingName] message, so this class just has to be KVC-compliant for a key that is the same as the binding name. Also, NSView supports a few simple bindings of its own, and there's no reason to get in the way of those.
*/
[super bind:bindingName toObject:observableObject withKeyPath:observableKeyPath options:options];
}
[self setNeedsDisplay:YES];
}
- (NSDictionary *)infoForBinding:(NSString *)bindingName
{
NSDictionary *info = [bindingInfo objectForKey:bindingName];
if (info == nil) {
info = [super infoForBinding:bindingName];
}
return info;
}
- (id)analysisContainer
{
return [[self infoForBinding:GRAPHICS_BINDING_NAME] objectForKey:NSObservedObjectKey];
}
- (NSString *)analysisKeyPath {
return [[self infoForBinding:GRAPHICS_BINDING_NAME] objectForKey:NSObservedKeyPathKey];
}
- (id)selectionIndexesContainer
{
return [[self infoForBinding:SELECTIONINDEXES_BINDING_NAME] objectForKey:NSObservedObjectKey];
}
- (NSString *)selectionIndexesKeyPath {
return [[self infoForBinding:SELECTIONINDEXES_BINDING_NAME] objectForKey:NSObservedKeyPathKey];
}
- (NSArray *)analysis
{
return [[self analysisContainer] valueForKeyPath:[self analysisKeyPath]];
}
- (NSIndexSet *)selectionIndexes
{
return [[self selectionIndexesContainer] valueForKeyPath:[self selectionIndexesKeyPath]];
}
- (void)drawRect:(NSRect)rect
{
NSPoint a, b, c;
float xStep, yStep;
int index, i, j;
NSRect bounds = [self bounds];
float tickLength;
float xAxisLength;
float yAxisLength;
tickLength = 3;
xAxisLength = 500.0;
yAxisLength = 350.0;
NSString *xAxisLabel = [NSString stringWithString:@"age [Ma]"];
NSString *yAxisLabel = [NSString stringWithString:@"relative Probability"];
[[NSColor whiteColor] set];
[NSBezierPath fillRect:bounds];
Analysis *analysis;
NSArray *analysisArray = [self analysis];
[[NSColor blackColor] set];
NSEraseRect(rect);
// NSLog(@"histoView %@", self);
// Draw x-y axis
NSBezierPath *path;
path = [[NSBezierPath alloc] init];
[path setLineWidth: 1.0];
a.x = 0.0;
a.y = 0.0;
b.x = a.x + xAxisLength;
b.y = a.y;
c.x = a.x;
c.y = a.y + yAxisLength;
[path moveToPoint: c];
[path lineToPoint: a];
[path lineToPoint: b];
// [path closePath];
[path stroke];
[path release];
float yaw = [yAxisLabel sizeWithAttributes:attributes].width/2;
float yah = [yAxisLabel sizeWithAttributes:attributes].height/2;
float xx = -40 - yaw, yy = yAxisLength/2-yah;
[[NSGraphicsContext currentContext] saveGraphicsState];
NSRect rcWhereToDraw = NSMakeRect(xx, yy, [yAxisLabel sizeWithAttributes:attributes].width, [yAxisLabel sizeWithAttributes:attributes].height);
float dx = NSMidX(rcWhereToDraw), dy = NSMidY(rcWhereToDraw);
NSAffineTransform *tr = [NSAffineTransform transform];
[tr translateXBy:dx yBy:dy]; // center or rect will be the center of rotation
[tr rotateByDegrees:90]; // rotate it
[tr translateXBy:-dx yBy:-dy]; // move it back
[tr concat];
[yAxisLabel drawInRect:rcWhereToDraw withAttributes:attributes];
[[NSGraphicsContext currentContext] restoreGraphicsState];
[xAxisLabel drawAtPoint:NSMakePoint(xAxisLength/2 - [xAxisLabel sizeWithAttributes:attributes].width/2, -25.0) withAttributes:attributes];
// Draw axis ticks
//mc = [self getMaxCount:intervallCounts];
xStep = xAxisLength/(xMax-xMin);
yStep = yAxisLength/yMax;
/*
[histoPath moveToPoint: a];
yMax = 1;
for (index=5; index < [intervallCounts count]; index++) {
y = [[intervallCounts objectAtIndex:index] intValue];
NSLog(@"counts at index %i %i", y, index );
b.x = xStep * (index-5) * 20;
b.y = yStep * y;
[histoPath lineToPoint:b];
if ([[intervallCounts objectAtIndex:index] intValue] > yMax) {
yMax = [[intervallCounts objectAtIndex:index] intValue];
}
}
*/
// Draw the gausscurves
float age_, err, gy, stepSize, lowerX, x;
id dat;
// NSLog(@"age count %i", [age count]);
NSIndexSet *currentSelectionIndexes = [self selectionIndexes];
NSEnumerator *dataEnum = [analysisArray objectEnumerator];
index = 0;
while (dat = [dataEnum nextObject])
{
index++;
age_ = [[dat valueForKey:@"age"] floatValue];
err = [[dat valueForKey:@"ageErr"] floatValue];
// for(index = 0; index < [age count]; index++) {
NSBezierPath *gaussPath = [[NSBezierPath alloc] init];
// age_ = [[age objectAtIndex: index] floatValue];
// err = [[sigma objectAtIndex: index] floatValue];
// NSLog(@"age err %f %f %i", age_, err, index);
lowerX = (age_ - 3*err) > xMin ? (age_ - 3*err) : xMin;
stepSize = err/10; // *6 = 60 is the numberOfSteps (3 sigma * 2 sides)
[gaussPath moveToPoint:NSMakePoint((lowerX-xMin)*xStep,0)];
for ( i = 0; i < 60; i++) {
x = lowerX + i*stepSize;
gy = 1/(err * sqrt(2*3.14159265)) * exp(-pow((x-age_),2)/2/pow(err,2));
[gaussPath lineToPoint:NSMakePoint((x-xMin) * xStep, gy * yStep)];
// NSLog(@"gy x %f %f %i", gy, x, i);
}
[[NSColor grayColor] set];
[gaussPath stroke];
[gaussPath release];
}
// NSLog(@"gy x %f %f %i", gy, x, i);
// NSLog(@"currentselinx %@", [self selectionIndexes]);
// if ( [selectedData containsObject:dat])
if (currentSelectionIndexes != nil)
{
NSUInteger indx = [currentSelectionIndexes firstIndex];
while (indx != NSNotFound)
{
analysis = [analysisArray objectAtIndex:indx];
indx = [currentSelectionIndexes indexGreaterThanIndex:indx];
NSBezierPath *selPath = [[NSBezierPath alloc] init];
age_ = [[analysis valueForKey:@"age"] floatValue];
err = [[analysis valueForKey:@"ageErr"] floatValue];
lowerX = (age_ - 3*err) > xMin ? (age_ - 3*err) : xMin;
stepSize = err/10; // *6 = 60 is the numberOfSteps (3 sigma * 2 sides)
[selPath moveToPoint:NSMakePoint((lowerX-xMin)*xStep,0)];
for ( i = 0; i < 60; i++)
{
x = lowerX + i*stepSize;
gy = 1/(err * sqrt(2*3.14159265)) * exp(-pow((x-age_),2)/2/pow(err,2));
[selPath lineToPoint:NSMakePoint((x-xMin) * xStep, gy * yStep)];
// NSLog(@"gy x %f %f %i", gy, x, i);
}
[[NSColor greenColor] set];
[selPath stroke];
[selPath release];
}
}
int numberOfXTicks = 5;
float delta = (xMax - xMin)/numberOfXTicks;
float xLabel;
NSString *stringXLabel;
NSBezierPath *tickPath = [[NSBezierPath alloc] init];
for (index = 0; index < (numberOfXTicks+1); index++) {
xLabel = delta * index + xMin;
stringXLabel = [NSString stringWithFormat:@"%4.0f", xLabel];
b.x = xStep * (xLabel-xMin);
[stringXLabel drawAtPoint:NSMakePoint(b.x - [stringXLabel sizeWithAttributes:attributes].width/2, -15.0) withAttributes:attributes];
if (index > 0) {
[tickPath moveToPoint:NSMakePoint( b.x, 0.0)];
[tickPath lineToPoint:NSMakePoint( b.x, tickLength)];
[tickPath stroke];
}
}
// NSLog(@"ymax %i", yMax);
delta = yMax/5.0;
int numberOfYTicks = rint(yMax/delta);
float yLabel;
NSString *stringYLabel;
//NSBezierPath *tickPath = [[NSBezierPath alloc] init];
for (index = 0; index < (numberOfYTicks+1); index++) {
yLabel = delta * index;
stringYLabel = [NSString stringWithFormat:@"%1.2f", yLabel];
b.y = yStep * yLabel;
[stringYLabel drawAtPoint:NSMakePoint(-5.0 - [stringYLabel sizeWithAttributes:attributes].width, b.y - [stringYLabel sizeWithAttributes:attributes].height/2) withAttributes:attributes];
if (index > 0) {
[tickPath moveToPoint:NSMakePoint(0.0, b.y)];
[tickPath lineToPoint:NSMakePoint(tickLength, b.y)];
[tickPath stroke];
}
}
[tickPath release];
// Draw Sum Curves ,jetztwirds schwierig
if ([selArray count] > 0) {
for (index = 0; index < [selArray count]; index++) {
NSBezierPath *sumGaussPath = [[NSBezierPath alloc] init];
SelData *aSel = [selArray objectAtIndex:index];
// zuerst obere und untere Limits bestimmen
// dazu die selektierten Daten durchloopen
// [aSel sel] gibt den NSSet mit indizes der Analysis
float upLimit = xMin;
float lowLimit = xMax;
// float lowLimit, upLimit;
// Erstmal den Bereich der x-Werte ermittlen, ber den die Summenkurven gebildet werden
// sollen. Dies sind upLimit und lowLimit.
NSEnumerator *enumerator = [[aSel sel] objectEnumerator];
id obj;
while (obj = [enumerator nextObject]) {
/* code that acts on the setÕs values */
float compValue = [[obj valueForKey:@"age"] floatValue];
// NSLog(@"age %f index %i", [[obj valueForKey:@"age"] floatValue], index);
float compErr = [[obj valueForKey:@"ageErr"] floatValue];
upLimit = (((compValue + 3*compErr) > upLimit) && ((compValue + 3*compErr) < xMax)) ? (compValue + 3*compErr) : upLimit ;
lowLimit = (((compValue - 3*compErr) < lowLimit) && ((compValue - 3*compErr) > xMin)) ? (compValue - 3*compErr) : lowLimit ;
}
// NSLog(@"lowLimit %f upLimit %f", lowLimit, upLimit);
// Jetzt zum Startpunkt gehen und den Pfad beginnen
[sumGaussPath moveToPoint:NSMakePoint((lowLimit-xMin)*xStep,0)];
// Die Kurve soll in 60 Schritten aufgebaut werden. Bestimmung der Schrittweite:
stepSize = (upLimit - lowLimit)/100;
// Jetzt wird die eigentlichen Summenkurve bestimmt. Dazu wird fr jeden der 60 Einzelschritte
// fr jedes Alter in der Auswahl (_age) der Wert y an der Stelle x berechnet und diese Einzelwerte
// werden aufsummiert.
// ussere Schleife fr jeden Einzelschritt
for ( j = 0; j < 100; j++) {
x = lowLimit + j*stepSize;
gy = 0;
enumerator = [[aSel sel] objectEnumerator];
// Innere Schleife: Aufsummierung der y - Werte
while (obj = [enumerator nextObject]) {
age_ = [[obj valueForKey:@"age"] floatValue];
err = [[obj valueForKey:@"ageErr"] floatValue];
gy = gy + 1/(err * sqrt(2*3.14159265)) * exp(-pow((x-age_),2)/2/pow(err,2));
// NSLog(@"gy x %f %f %i", gy, x, k);
}
[sumGaussPath lineToPoint:NSMakePoint((x-xMin) * xStep, gy * yStep)];
}
[[NSColor blueColor] set];
[sumGaussPath stroke];
[sumGaussPath release];
//draw the bestimate-curve
err = [aSel errbestimate];
lowerX = [aSel bestimate] - 3*err;
stepSize = err/10; // *6 = 60 is the numberOfSteps (3 sigma * 2 sides)
NSBezierPath *bestimatePath = [[NSBezierPath alloc] init];
[bestimatePath moveToPoint:NSMakePoint((lowerX-xMin)*xStep,0)];
for ( i = 0; i < 60; i++) {
x = lowerX + i*stepSize;
gy = 1/(err * sqrt(2*3.14159265)) * exp(-pow((x-[aSel bestimate]),2)/2/pow(err,2));
[bestimatePath lineToPoint:NSMakePoint((x-xMin) * xStep, gy * yStep)];
// NSLog(@"gy x %f %f %i", gy, x, i);
}
[[NSColor redColor] set];
[bestimatePath stroke];
[bestimatePath release];
}
} // if
[self retain];
}
//
// The action for saving to PDF file. Just do a save panel and
// use -didEnd to handle results.
//
-(IBAction) savePDF:(id)sender
{
NSSavePanel * panel = [NSSavePanel savePanel];
// NSLog(@"savePDF",self);
[panel setRequiredFileType: @"pdf"];
[panel beginSheetForDirectory: nil
file: nil
modalForWindow: [self window]
modalDelegate: self
didEndSelector: @selector(didEnd:returnCode:contextInfo:)
contextInfo: nil];
}
//
// Do this after the PDF file target has been set.
//
-(void) didEnd: (NSSavePanel *)sheet returnCode:(int)code
contextInfo: (void *)contextInfo
{
if(code == NSOKButton)
{
// NSLog(@"didEnd called for %@", self);
NSRect bounds = [self bounds];
NSSize frameSize = [self frame].size;
/* frameSize.width = bounds.size.width;
frameSize.height = bounds.size.height;
[self setFrameSize:frameSize];
[self setBounds:bounds];
*/
NSRect r = NSMakeRect(bounds.origin.x, bounds.origin.y, frameSize.width, frameSize.height);
// NSRect r = [self bounds];
NSData *data = [self dataWithPDFInsideRect:r];
[data writeToFile: [sheet filename] atomically: YES];
}
}
@end