-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlisting53.html
executable file
·1176 lines (1006 loc) · 56.5 KB
/
listing53.html
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
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<!-- BEGIN META TAG INFO -->
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="home" href="http://developer.apple.com/">
<link rel="find" href="http://developer.apple.com/search/">
<link rel="stylesheet" type="text/css" href="../../documentation/css/adcstyle.css" title="fonts">
<script language="JavaScript" src="../../documentation/js/adc.js" type="text/javascript"></script>
<!-- END META TAG INFO -->
<!-- BEGIN TITLE -->
<title>Sketch-112 - /Sketch Before/DocumentModel.subproj/SKTDrawDocument.m</title>
<!-- END TITLE -->
<script language="JavaScript">
function JumpToNewPage() {
window.location=document.scpopupmenu.gotop.value;
return true;
}
</script>
</head>
<!-- BEGIN BODY OPEN -->
<body>
<!--END BODY OPEN -->
<!-- START CENTER OPEN -->
<center>
<!-- END CENTER OPEN -->
<!-- BEGIN LOGO AND SEARCH -->
<!--#include virtual="/includes/adcnavbar"-->
<!-- END LOGO AND SEARCH -->
<!-- START BREADCRUMB -->
<div id="breadcrumb">
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr>
<td scope="row"><img width="340" height="10" src="images/1dot.gif" alt=""></td>
<td><img width="340" height="10" src="images/1dot.gif" alt=""></td>
</tr>
<tr valign="middle">
<td align="left" colspan="2">
<a href="http://developer.apple.com/">ADC Home</a> > <a href="../../referencelibrary/index.html">Reference Library</a> > <a href="../../samplecode/index.html">Sample Code</a> > <a href="../../samplecode/ScriptingAutomation/index.html">Scripting & Automation</a> > <a href="../../samplecode/ScriptingAutomation/idxCocoa-date.html">Cocoa</a> > <A HREF="javascript:location.replace('index.html');">Sketch-112</A> >
</td>
</tr>
<tr>
<td colspan="2" scope="row"><img width="680" height="35" src="images/1dot.gif" alt=""></td>
</tr>
</table>
</div>
<!-- END BREADCRUMB -->
<!-- START MAIN CONTENT -->
<!-- START TITLE GRAPHIC AND INTRO-->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td><h1><div id="pagehead">Sketch-112</div></h1></td>
</tr>
</table>
<!-- END TITLE GRAPHIC AND INTRO -->
<!-- START WIDE COLUMN -->
<table width="680" border="0" cellpadding="0" cellspacing="0">
<tr align="left" valign="top">
<td id="scdetails">
<h2>/Sketch Before/DocumentModel.subproj/SKTDrawDocument.m</h2>
<form name="scpopupmenu" onSubmit="return false;" method=post>
<p><strong>View Source Code:</strong>
<select name="gotop" onChange="JumpToNewPage();" style="width:340px"><option selected value="ingnore">Select File</option>
<option value="listing1.html">/Sketch After/DocumentModel.subproj/my.h</option>
<option value="listing2.html">/Sketch After/DocumentModel.subproj/SKTAlignCommand.h</option>
<option value="listing3.html">/Sketch After/DocumentModel.subproj/SKTAlignCommand.m</option>
<option value="listing4.html">/Sketch After/DocumentModel.subproj/SKTCircle.h</option>
<option value="listing5.html">/Sketch After/DocumentModel.subproj/SKTCircle.m</option>
<option value="listing6.html">/Sketch After/DocumentModel.subproj/SKTDrawDocument.h</option>
<option value="listing7.html">/Sketch After/DocumentModel.subproj/SKTDrawDocument.m</option>
<option value="listing8.html">/Sketch After/DocumentModel.subproj/SKTGraphic.h</option>
<option value="listing9.html">/Sketch After/DocumentModel.subproj/SKTGraphic.m</option>
<option value="listing10.html">/Sketch After/DocumentModel.subproj/SKTImage.h</option>
<option value="listing11.html">/Sketch After/DocumentModel.subproj/SKTImage.m</option>
<option value="listing12.html">/Sketch After/DocumentModel.subproj/SKTLine.h</option>
<option value="listing13.html">/Sketch After/DocumentModel.subproj/SKTLine.m</option>
<option value="listing14.html">/Sketch After/DocumentModel.subproj/SKTRectangle.h</option>
<option value="listing15.html">/Sketch After/DocumentModel.subproj/SKTRectangle.m</option>
<option value="listing16.html">/Sketch After/DocumentModel.subproj/SKTRectangleOrientation.h</option>
<option value="listing17.html">/Sketch After/DocumentModel.subproj/SKTRectangleOrientation.m</option>
<option value="listing18.html">/Sketch After/DocumentModel.subproj/SKTRectangleRotate.h</option>
<option value="listing19.html">/Sketch After/DocumentModel.subproj/SKTRectangleRotate.m</option>
<option value="listing20.html">/Sketch After/DocumentModel.subproj/SKTRenderingView.h</option>
<option value="listing21.html">/Sketch After/DocumentModel.subproj/SKTRenderingView.m</option>
<option value="listing22.html">/Sketch After/DocumentModel.subproj/SKTScriptingExtras.h</option>
<option value="listing23.html">/Sketch After/DocumentModel.subproj/SKTTextArea.h</option>
<option value="listing24.html">/Sketch After/DocumentModel.subproj/SKTTextArea.m</option>
<option value="listing25.html">/Sketch After/English.lproj/Sketch.scriptTerminology</option>
<option value="listing26.html">/Sketch After/Sketch.scriptSuite</option>
<option value="listing27.html">/Sketch After/Sketch.sdef</option>
<option value="listing28.html">/Sketch After/Sketch_main.m</option>
<option value="listing29.html">/Sketch After/SKT_Prefix.h</option>
<option value="listing30.html">/Sketch After/SKTDrawAppDelegate.h</option>
<option value="listing31.html">/Sketch After/SKTDrawAppDelegate.m</option>
<option value="listing32.html">/Sketch After/SKTDrawWindowController.h</option>
<option value="listing33.html">/Sketch After/SKTDrawWindowController.m</option>
<option value="listing34.html">/Sketch After/SKTFoundationExtras.h</option>
<option value="listing35.html">/Sketch After/SKTFoundationExtras.m</option>
<option value="listing36.html">/Sketch After/SKTGraphicView.h</option>
<option value="listing37.html">/Sketch After/SKTGraphicView.m</option>
<option value="listing38.html">/Sketch After/SKTGridPanelController.h</option>
<option value="listing39.html">/Sketch After/SKTGridPanelController.m</option>
<option value="listing40.html">/Sketch After/SKTGridView.h</option>
<option value="listing41.html">/Sketch After/SKTGridView.m</option>
<option value="listing42.html">/Sketch After/SKTInspectorController.h</option>
<option value="listing43.html">/Sketch After/SKTInspectorController.m</option>
<option value="listing44.html">/Sketch After/SKTToolPaletteController.h</option>
<option value="listing45.html">/Sketch After/SKTToolPaletteController.m</option>
<option value="listing46.html">/Sketch After/ToDo.txt</option>
<option value="listing47.html">/Sketch Before/DocumentModel.subproj/my.h</option>
<option value="listing48.html">/Sketch Before/DocumentModel.subproj/SKTAlignCommand.h</option>
<option value="listing49.html">/Sketch Before/DocumentModel.subproj/SKTAlignCommand.m</option>
<option value="listing50.html">/Sketch Before/DocumentModel.subproj/SKTCircle.h</option>
<option value="listing51.html">/Sketch Before/DocumentModel.subproj/SKTCircle.m</option>
<option value="listing52.html">/Sketch Before/DocumentModel.subproj/SKTDrawDocument.h</option>
<option value="listing53.html">/Sketch Before/DocumentModel.subproj/SKTDrawDocument.m</option>
<option value="listing54.html">/Sketch Before/DocumentModel.subproj/SKTGraphic.h</option>
<option value="listing55.html">/Sketch Before/DocumentModel.subproj/SKTGraphic.m</option>
<option value="listing56.html">/Sketch Before/DocumentModel.subproj/SKTImage.h</option>
<option value="listing57.html">/Sketch Before/DocumentModel.subproj/SKTImage.m</option>
<option value="listing58.html">/Sketch Before/DocumentModel.subproj/SKTLine.h</option>
<option value="listing59.html">/Sketch Before/DocumentModel.subproj/SKTLine.m</option>
<option value="listing60.html">/Sketch Before/DocumentModel.subproj/SKTRectangle.h</option>
<option value="listing61.html">/Sketch Before/DocumentModel.subproj/SKTRectangle.m</option>
<option value="listing62.html">/Sketch Before/DocumentModel.subproj/SKTRectangleOrientation.h</option>
<option value="listing63.html">/Sketch Before/DocumentModel.subproj/SKTRectangleOrientation.m</option>
<option value="listing64.html">/Sketch Before/DocumentModel.subproj/SKTRectangleRotate.h</option>
<option value="listing65.html">/Sketch Before/DocumentModel.subproj/SKTRectangleRotate.m</option>
<option value="listing66.html">/Sketch Before/DocumentModel.subproj/SKTRenderingView.h</option>
<option value="listing67.html">/Sketch Before/DocumentModel.subproj/SKTRenderingView.m</option>
<option value="listing68.html">/Sketch Before/DocumentModel.subproj/SKTScriptingExtras.h</option>
<option value="listing69.html">/Sketch Before/DocumentModel.subproj/SKTTextArea.h</option>
<option value="listing70.html">/Sketch Before/DocumentModel.subproj/SKTTextArea.m</option>
<option value="listing71.html">/Sketch Before/English.lproj/Sketch.scriptTerminology</option>
<option value="listing72.html">/Sketch Before/Sketch.scriptSuite</option>
<option value="listing73.html">/Sketch Before/Sketch.sdef</option>
<option value="listing74.html">/Sketch Before/Sketch_main.m</option>
<option value="listing75.html">/Sketch Before/SKT_Prefix.h</option>
<option value="listing76.html">/Sketch Before/SKTDrawAppDelegate.h</option>
<option value="listing77.html">/Sketch Before/SKTDrawAppDelegate.m</option>
<option value="listing78.html">/Sketch Before/SKTDrawWindowController.h</option>
<option value="listing79.html">/Sketch Before/SKTDrawWindowController.m</option>
<option value="listing80.html">/Sketch Before/SKTFoundationExtras.h</option>
<option value="listing81.html">/Sketch Before/SKTFoundationExtras.m</option>
<option value="listing82.html">/Sketch Before/SKTGraphicView.h</option>
<option value="listing83.html">/Sketch Before/SKTGraphicView.m</option>
<option value="listing84.html">/Sketch Before/SKTGridPanelController.h</option>
<option value="listing85.html">/Sketch Before/SKTGridPanelController.m</option>
<option value="listing86.html">/Sketch Before/SKTGridView.h</option>
<option value="listing87.html">/Sketch Before/SKTGridView.m</option>
<option value="listing88.html">/Sketch Before/SKTInspectorController.h</option>
<option value="listing89.html">/Sketch Before/SKTInspectorController.m</option>
<option value="listing90.html">/Sketch Before/SKTToolPaletteController.h</option>
<option value="listing91.html">/Sketch Before/SKTToolPaletteController.m</option>
<option value="listing92.html">/Sketch Before/ToDo.txt</option>
<option value="listing93.html">/test scripts/test01.applescript</option>
<option value="listing94.html">/test scripts/test02.applescript</option>
<option value="listing95.html">/test scripts/test03.applescript</option></select>
</p>
</form>
<p><strong><a href="Sketch-112.zip">Download Sample</a></strong> (“Sketch-112.zip”, 378.8K)<BR>
<strong><a href="Sketch-112.dmg">Download Sample</a></strong> (“Sketch-112.dmg”, 595.4K)</p>
<!--
<p><strong><a href="#">Download Sample</a></strong> (“filename.sit”, 500K)</p>
-->
</td>
</tr>
<tr>
<td scope="row"><img width="680" height="10" src="images/1dot.gif" alt=""><br>
<img height="1" width="680" src="images/1dot_919699.gif" alt=""><br>
<img width="680" height="20" src="images/1dot.gif" alt=""></td>
</tr>
<tr>
<td scope="row">
<!--googleon: index -->
<pre class="sourcecodebox">// SKTDrawDocument.m
// Sketch Example
//
#import "SKTDrawDocument.h"
#import "SKTDrawWindowController.h"
#import "SKTGraphic.h"
#import "SKTRenderingView.h"
#import "SKTRectangle.h"
#import "SKTCircle.h"
#import "SKTLine.h"
#import "SKTTextArea.h"
#import "SKTImage.h"
//#import "my.h" //ME search for: // myLog1
// Sketch establishes an NSError domain and some error codes. In a bigger app this stuff would of course be declared in a header. Also, in a bigger app the lookup of error description and failure reasons would probably be centralized somewhere instead of scattered all over the source code like in this file.
NSString *const SKTErrorDomain = @"SketchErrorDomain";
enum {
SKTReadUnknownError = 1,
SKTWriteCouldntMakeTIFFError = 2
};
NSString *SKTDrawDocumentType = @"Apple Sketch Graphic Format";
@implementation SKTDrawDocument
- (id)init {
self = [super init];
if (self) {
_graphics = [[NSMutableArray allocWithZone:[self zone]] init];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_graphics release];
[super dealloc];
}
- (void)makeWindowControllers {
SKTDrawWindowController *myController = [[SKTDrawWindowController allocWithZone:[self zone]] init];
[self addWindowController:myController];
[myController release];
}
static NSString *SKTGraphicsListKey = @"GraphicsList";
static NSString *SKTDrawDocumentVersionKey = @"DrawDocumentVersion";
static int SKTCurrentDrawDocumentVersion = 1;
static NSString *SKTPrintInfoKey = @"PrintInfo";
- (NSDictionary *)drawDocumentDictionaryForGraphics:(NSArray *)graphics {
NSMutableDictionary *doc = [NSMutableDictionary dictionary];
unsigned i, c = [graphics count];
NSMutableArray *graphicDicts = [NSMutableArray arrayWithCapacity:c];
for (i=0; i<c; i++) {
[graphicDicts addObject:[[graphics objectAtIndex:i] propertyListRepresentation]];
}
[doc setObject:graphicDicts forKey:SKTGraphicsListKey];
[doc setObject:[NSString stringWithFormat:@"%d", SKTCurrentDrawDocumentVersion] forKey:SKTDrawDocumentVersionKey];
[doc setObject:[NSArchiver archivedDataWithRootObject:[self printInfo]] forKey:SKTPrintInfoKey];
return doc;
}
- (NSData *)drawDocumentDataForGraphics:(NSArray *)graphics {
NSDictionary *doc = [self drawDocumentDictionaryForGraphics:graphics];
NSString *string = [doc description];
return [string dataUsingEncoding:NSASCIIStringEncoding];
}
- (NSDictionary *)drawDocumentDictionaryFromData:(NSData *)data error:(NSError **)outError {
// If property list parsing fails we have no choice but to admit that we don't know what went wrong. The error description returned by +[NSPropertyListSerialization propertyListFromData:mutabilityOption:format:errorDescription:] would be pretty technical, and not the sort of thing that we should show to a user.
NSDictionary *properties = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:NULL errorDescription:NULL];
if (!properties && outError) {
// An NSError has a bunch of parameters that determine how it's presented to the user. We just specify two of them here.
NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
// This localized description won't be presented to the user, except maybe by -[SKTGraphicView paste:]. It's a good idea to always provide a decent description that's a full sentence.
NSLocalizedStringFromTable(@"Sketch document data could not be read for an unknown reason.", @"ErrorStrings", @"Description of can't-read-Sketch error."), NSLocalizedDescriptionKey,
// This localized failure reason will be presented to the user if we're trying to open a document. NSDocumentController will take it and tack it onto the end of a "The document "so-and-so" could not be opened." message and use the whole thing as an error description. Full sentence!
NSLocalizedStringFromTable(@"An unknown error occured.", @"ErrorStrings", @"Reason for can't-read-Sketch error."), NSLocalizedFailureReasonErrorKey,
nil];
// In this simple example we know that no one's going to be paying attention to the domain and code that we use here, but don't just fill in junk here. Certainly don't just use NSCocoaErrorDomain and some random error code.
*outError = [NSError errorWithDomain:SKTErrorDomain code:SKTReadUnknownError userInfo:errorUserInfo];
}
return properties;
}
- (NSArray *)graphicsFromDrawDocumentDictionary:(NSDictionary *)doc {
NSArray *graphicDicts = [doc objectForKey:SKTGraphicsListKey];
unsigned i, c = [graphicDicts count];
NSMutableArray *graphics = [NSMutableArray arrayWithCapacity:c];
for (i=0; i<c; i++) {
[graphics addObject:[SKTGraphic graphicWithPropertyListRepresentation:[graphicDicts objectAtIndex:i]]];
}
return graphics;
}
- (NSRect)boundsForGraphics:(NSArray *)graphics {
NSRect rect = NSZeroRect;
unsigned i, c = [graphics count];
for (i=0; i<c; i++) {
if (i==0) {
rect = [[graphics objectAtIndex:i] bounds];
} else {
rect = NSUnionRect(rect, [[graphics objectAtIndex:i] bounds]);
}
}
return rect;
}
- (NSRect)drawingBoundsForGraphics:(NSArray *)graphics {
NSRect rect = NSZeroRect;
unsigned i, c = [graphics count];
for (i=0; i<c; i++) {
if (i==0) {
rect = [[graphics objectAtIndex:i] drawingBounds];
} else {
rect = NSUnionRect(rect, [[graphics objectAtIndex:i] drawingBounds]);
}
}
return rect;
}
- (NSData *)TIFFRepresentationForGraphics:(NSArray *)graphics error:(NSError **)outError {
// How big a of a TIFF are we going to make?
NSData *tiffData;
NSRect bounds = [self drawingBoundsForGraphics:graphics];
if (!NSIsEmptyRect(bounds)) {
// Create a new image and prepare to draw in it. Get the graphics context for it after we lock focus, not before.
NSImage *image = [[NSImage alloc] initWithSize:bounds.size];
[image setFlipped:YES];
[image lockFocus];
NSGraphicsContext *currentContext = [NSGraphicsContext currentContext];
// We're not drawing a page image here, just the rectangle that contains the graphics being drawn, so make sure they get drawn in the right place.
NSAffineTransform *transform = [NSAffineTransform transform];
[transform translateXBy:(0.0f - bounds.origin.x) yBy:(0.0f - bounds.origin.y)];
[transform concat];
// Draw the graphics back to front.
unsigned int graphicIndex = [graphics count];
while (graphicIndex-->0) {
// The only reason a Sketch graphic knows what view it is drawing in is so that it can draw differently when being created or edited or selected. Specify a nil view to tell it to draw in the standard way.
SKTGraphic *graphic = [graphics objectAtIndex:graphicIndex];
[currentContext saveGraphicsState];
[NSBezierPath clipRect:[graphic drawingBounds]];
[graphic drawInView:nil isSelected:NO];
[currentContext restoreGraphicsState];
}
// We're done drawing.
[image unlockFocus];
tiffData = [image TIFFRepresentation];
[image release];
} else {
// Regardless of what NSImage supports, Sketch doesn't support the creation of TIFFs that are 0 by 0 pixels. (We have to demonstrate a custom saving error somewhere, and this is an easy place to do it...)
tiffData = nil;
// Return an error that will be presented to the user by NSDocument if the user was attempting to save to a TIFF file. Notice that we're not allowed to assume that outError!=NULL.
// There are lots of places to catch this situation earlier. For example, we could have overridden -writableTypesForSaveOperation: and made it not return NSTIFFPboardType, but then the user would have no idea why TIFF isn't showing up in the save panel's File Format popup. This way we can present a nice descriptive errror message.
if (outError) {
// An NSError has a bunch of parameters that determine how it's presented to the user. We just specify two of them here.
NSDictionary *errorUserInfo = [NSDictionary dictionaryWithObjectsAndKeys:
// This localized description won't be presented to the user. In code that's more reusable it might be though, so it's a good idea to always provide a decent one that's a full sentence.
NSLocalizedStringFromTable(@"A TIFF image could not be made because it would be empty.", @"ErrorStrings", @"Description of can't-make-TIFF error."), NSLocalizedDescriptionKey,
// This localized failure reason _will_ be presented to the user. NSDocument will take it and tack it onto the end of a "The document "so-and-so" could not be saved." message and use the whole thing as an error description. Full sentence!
NSLocalizedStringFromTable(@"The TIFF image would be empty.", @"ErrorStrings", @"Reason for can't-make-TIFF error."), NSLocalizedFailureReasonErrorKey,
nil];
// In this simple example we know that no one's going to be paying attention to the domain and code that we use here, but don't just fill in junk here. Certainly don't just use NSCocoaErrorDomain and some random error code.
*outError = [NSError errorWithDomain:SKTErrorDomain code:SKTWriteCouldntMakeTIFFError userInfo:errorUserInfo];
}
}
return tiffData;
}
- (NSData *)PDFRepresentationForGraphics:(NSArray *)graphics {
// Create a view that will be used just for making PDF.
NSRect bounds = [self drawingBoundsForGraphics:graphics];
SKTRenderingView *view = [[SKTRenderingView alloc] initWithFrame:bounds graphics:graphics];
NSData *pdfData = [view dataWithPDFInsideRect:bounds];
[view release];
return pdfData;
}
// This method will only be invoked on Mac OS 10.4 and later. If you're writing an application that has to run on 10.3.x and earlier you should override -dataRepresentationOfType: instead.
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError {
// This method must be prepared for typeName to be any value that might be in the array returned by any invocation of -writableTypesForSaveOperation:. Because this class:
// doesn't - override -writableTypesForSaveOperation:, and
// doesn't - override +writableTypes and +isNativeType: (which the default implementation of -writableTypesForSaveOperation: invokes),
// and because:
// - Sketch has a "Save a Copy As..." file menu item that results in NSSaveToOperations,
// we know that that the type names we have to handle here include:
// - SKTDrawDocumentType, because this application's Info.plist file declares that instances of this class can play the "editor" role for it, and
// - NSPDFPboardType and NSTIFFPboardType, because according to the Info.plist an SKTDrawDocumentType document is exportable as them.
// If we had reason to believe that -PDFRepresentationForGraphics or -drawDocumentDataForGraphics could return nil we would have to arrange for *outError to be set to a real value in that case. If you signal failure in a method that takes an error: parameter and outError!=NULL you must set *outError to something decent.
NSData *data;
NSArray *graphics = [self graphics];
if ([typeName isEqualToString:NSPDFPboardType]) {
data = [self PDFRepresentationForGraphics:graphics];
} else if ([typeName isEqualToString:NSTIFFPboardType]) {
data = [self TIFFRepresentationForGraphics:graphics error:outError];
} else {
NSParameterAssert([typeName isEqualToString:SKTDrawDocumentType]);
data = [self drawDocumentDataForGraphics:graphics];
}
return data;
}
// This method will only be invoked on Mac 10.4 and later. If you're writing an application that has to run on 10.3.x and earlier you should override -loadDataRepresentation:ofType: instead.
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError {
// This application's Info.plist only declares one document type, SKTDrawDocumentType, for which it can play the "editor" role, and none for which it can play the "viewer" role, so the type better be SKTDrawDocumentType.
NSParameterAssert([typeName isEqualToString:SKTDrawDocumentType]);
// Read in the property list.
NSDictionary *properties = [self drawDocumentDictionaryFromData:data error:outError];
if (properties) {
// Get the graphics and set them. Strictly speaking the property list of an empty document should have an empty graphics array, not no graphics array, but we cope easily with either. It wouldn't be good practice to invoke [self setGraphics:nil] though (passing or returning nil collection pointers rarely is).
NSArray *graphics = [self graphicsFromDrawDocumentDictionary:properties];
if (!graphics) {
graphics = [NSArray array];
}
[self setGraphics:graphics];
// There's no point in considering the opening of the document to have failed" if we can't get print info. A more finished app might present a panel warning the user that something's fishy though.
NSData *printInfoData = [properties objectForKey:SKTPrintInfoKey];
if (printInfoData) {
NSPrintInfo *printInfo = [NSUnarchiver unarchiveObjectWithData:printInfoData];
if (printInfo) {
[self setPrintInfo:printInfo];
// -[NSDocument setPrintInfo:] registered an undo action, but that wasn't appropriate in this case.
[[self undoManager] removeAllActions];
}
}
} // else it was -drawDocumentDictionaryFromData:error:'s responsibility to set *outError to something good.
return properties ? YES : NO;
}
- (NSSize)documentSize {
NSPrintInfo *printInfo = [self printInfo];
NSSize paperSize = [printInfo paperSize];
paperSize.width -= ([printInfo leftMargin] + [printInfo rightMargin]);
paperSize.height -= ([printInfo topMargin] + [printInfo bottomMargin]);
return paperSize;
}
// This method will only be invoked on Mac 10.4 and later. If you're writing an application that has to run on 10.3.x and earlier you should override -printShowingPrintPanel: instead.
- (NSPrintOperation *)printOperationWithSettings:(NSDictionary *)printSettings error:(NSError **)outError {
// Create a view that will be used just for printing.
NSSize documentSize = [self documentSize];
SKTRenderingView *renderingView = [[SKTRenderingView alloc] initWithFrame:NSMakeRect(0.0, 0.0, documentSize.width, documentSize.height) graphics:[self graphics]];
// Create a print operation.
NSPrintOperation *printOperation = [NSPrintOperation printOperationWithView:renderingView printInfo:[self printInfo]];
[renderingView release];
// Specify that the print operation can run in a separate thread. This will cause the print progress panel to appear as a sheet on the document window.
[printOperation setCanSpawnSeparateThread:YES];
// Set any print settings that might have been specified in a Print Document Apple event. We do it this way because we shouldn't be mutating the result of [self printInfo] here, and using the result of [printOperation printInfo], a copy of the original print info, means we don't have to make yet another temporary copy of [self printInfo].
[[[printOperation printInfo] dictionary] addEntriesFromDictionary:printSettings];
// We don't have to autorelease the print operation because +[NSPrintOperation printOperationWithView:printInfo:] of course already autoreleased it. Nothing in this method can fail, so we never return nil, so we don't have to worry about setting *outError.
return printOperation;
}
- (void)setPrintInfo:(NSPrintInfo *)printInfo {
// Do the regular Cocoa thing...
[super setPrintInfo:printInfo];
// ...and then make sure that all of the graphic views know the new document size, if it changed.
[[self windowControllers] makeObjectsPerformSelector:@selector(setUpGraphicView)];
}
- (NSArray *)graphics {
// myLog1(@"ME SKTDrawDocument graphics");
return _graphics;
}
- (void)setGraphics:(NSArray *)graphics {
unsigned i = [_graphics count];
while (i-- > 0) {
[self removeGraphicAtIndex:i];
}
i = [graphics count];
while (i-- > 0) {
[self insertGraphic:[graphics objectAtIndex:i] atIndex:0];
}
}
- (void)invalidateGraphic:(SKTGraphic *)graphic {
NSArray *windowControllers = [self windowControllers];
[windowControllers makeObjectsPerformSelector:@selector(invalidateGraphic:) withObject:graphic];
}
- (void)insertGraphic:(SKTGraphic *)graphic atIndex:(unsigned)index {
[[[self undoManager] prepareWithInvocationTarget:self] removeGraphicAtIndex:index];
[_graphics insertObject:graphic atIndex:index];
[graphic setDocument:self];
[self invalidateGraphic:graphic];
}
- (void)removeGraphicAtIndex:(unsigned)index {
id graphic = [[_graphics objectAtIndex:index] retain];
[_graphics removeObjectAtIndex:index];
[self invalidateGraphic:graphic];
[[[self undoManager] prepareWithInvocationTarget:self] insertGraphic:graphic atIndex:index];
[graphic release];
}
- (void)removeGraphic:(SKTGraphic *)graphic {
unsigned index = [_graphics indexOfObjectIdenticalTo:graphic];
if (index != NSNotFound) {
[self removeGraphicAtIndex:index];
}
}
- (void)moveGraphic:(SKTGraphic *)graphic toIndex:(unsigned)newIndex {
unsigned curIndex = [_graphics indexOfObjectIdenticalTo:graphic];
if (curIndex != newIndex) {
[[[self undoManager] prepareWithInvocationTarget:self] moveGraphic:graphic toIndex:((curIndex > newIndex) ? curIndex+1 : curIndex)];
if (curIndex < newIndex) {
newIndex--;
}
[graphic retain];
[_graphics removeObjectAtIndex:curIndex];
[_graphics insertObject:graphic atIndex:newIndex];
[graphic release];
[self invalidateGraphic:graphic];
}
}
@end
@implementation SKTDrawDocument (SKTScriptingExtras)
// These are methods that we probably wouldn't bother with if we weren't scriptable.
// graphics and setGraphics: are already implemented above.
- (void)addInGraphics:(SKTGraphic *)graphic {
[self insertGraphic:graphic atIndex:[[self graphics] count]];
}
- (void)insertInGraphics:(SKTGraphic *)graphic atIndex:(unsigned)index {
[self insertGraphic:graphic atIndex:index];
}
- (void)removeFromGraphicsAtIndex:(unsigned)index {
[self removeGraphicAtIndex:index];
}
- (void)replaceInGraphics:(SKTGraphic *)graphic atIndex:(unsigned)index {
[self removeGraphicAtIndex:index];
[self insertGraphic:graphic atIndex:index];
}
- (NSArray *)graphicsWithClass:(Class)theClass {
NSArray *graphics = [self graphics];
NSMutableArray *result = [NSMutableArray array];
unsigned i, c = [graphics count];
id curGraphic;
for (i=0; i<c; i++) {
curGraphic = [graphics objectAtIndex:i];
if ([curGraphic isKindOfClass:theClass]) {
[result addObject:curGraphic];
}
}
return result;
}
- (NSArray *)rectangles {
// myLog1(@"ME SKTDrawDocument rectangles");
return [self graphicsWithClass:[SKTRectangle class]];
}
- (NSArray *)circles {
return [self graphicsWithClass:[SKTCircle class]];
}
- (NSArray *)lines {
return [self graphicsWithClass:[SKTLine class]];
}
- (NSArray *)textAreas {
return [self graphicsWithClass:[SKTTextArea class]];
}
- (NSArray *)images {
return [self graphicsWithClass:[SKTImage class]];
}
- (void)setRectangles:(NSArray *)rects {
// We won't allow wholesale setting of these subset keys.
[NSException raise:NSOperationNotSupportedForKeyException format:@"Setting 'rectangles' key is not supported."];
}
- (void)addInRectangles:(SKTGraphic *)graphic {
[self addInGraphics:graphic];
}
- (void)insertInRectangles:(SKTGraphic *)graphic atIndex:(unsigned)index {
// MF:!!! This is not going to be ideal. If we are being asked to, say, "make a new rectangle at after rectangle 2", we will be after rectangle 2, but we may be after some other stuff as well since we will be asked to insertInRectangles:atIndex:3...
// myLog1(@"ME SKTDrawDocument insertInRectangles:atIndex:");
NSArray *rects = [self rectangles];
if (index == [rects count]) {
[self addInGraphics:graphic];
} else {
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[rects objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given rectangle in the graphics."];
}
}
}
- (void)removeFromRectanglesAtIndex:(unsigned)index {
// myLog1(@"ME SKTDrawDocument removeFromRectanglesAtIndex:");
NSArray *rects = [self rectangles];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[rects objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given rectangle in the graphics."];
}
}
- (void)replaceInRectangles:(SKTGraphic *)graphic atIndex:(unsigned)index {
// myLog1(@"ME SKTDrawDocument replaceInRectangles:atIndex:");
NSArray *rects = [self rectangles];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[rects objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given rectangle in the graphics."];
}
}
- (void)setCircles:(NSArray *)circles {
// We won't allow wholesale setting of these subset keys.
[NSException raise:NSOperationNotSupportedForKeyException format:@"Setting 'circles' key is not supported."];
}
- (void)addInCircles:(SKTGraphic *)graphic {
[self addInGraphics:graphic];
}
- (void)insertInCircles:(SKTGraphic *)graphic atIndex:(unsigned)index {
// MF:!!! This is not going to be ideal. If we are being asked to, say, "make a new rectangle at after rectangle 2", we will be after rectangle 2, but we may be after some other stuff as well since we will be asked to insertInCircles:atIndex:3...
NSArray *circles = [self circles];
if (index == [circles count]) {
[self addInGraphics:graphic];
} else {
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[circles objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given circle in the graphics."];
}
}
}
- (void)removeFromCirclesAtIndex:(unsigned)index {
NSArray *circles = [self circles];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[circles objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given circle in the graphics."];
}
}
- (void)replaceInCircles:(SKTGraphic *)graphic atIndex:(unsigned)index {
NSArray *circles = [self circles];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[circles objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given circle in the graphics."];
}
}
- (void)setLines:(NSArray *)lines {
// We won't allow wholesale setting of these subset keys.
[NSException raise:NSOperationNotSupportedForKeyException format:@"Setting 'lines' key is not supported."];
}
- (void)addInLines:(SKTGraphic *)graphic {
[self addInGraphics:graphic];
}
- (void)insertInLines:(SKTGraphic *)graphic atIndex:(unsigned)index {
// MF:!!! This is not going to be ideal. If we are being asked to, say, "make a new rectangle at after rectangle 2", we will be after rectangle 2, but we may be after some other stuff as well since we will be asked to insertInLines:atIndex:3...
NSArray *lines = [self lines];
if (index == [lines count]) {
[self addInGraphics:graphic];
} else {
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[lines objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given line in the graphics."];
}
}
}
- (void)removeFromLinesAtIndex:(unsigned)index {
NSArray *lines = [self lines];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[lines objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given line in the graphics."];
}
}
- (void)replaceInLines:(SKTGraphic *)graphic atIndex:(unsigned)index {
NSArray *lines = [self lines];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[lines objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given line in the graphics."];
}
}
- (void)setTextAreas:(NSArray *)textAreas {
// We won't allow wholesale setting of these subset keys.
[NSException raise:NSOperationNotSupportedForKeyException format:@"Setting 'textAreas' key is not supported."];
}
- (void)addInTextAreas:(SKTGraphic *)graphic {
[self addInGraphics:graphic];
}
- (void)insertInTextAreas:(SKTGraphic *)graphic atIndex:(unsigned)index {
// MF:!!! This is not going to be ideal. If we are being asked to, say, "make a new rectangle at after rectangle 2", we will be after rectangle 2, but we may be after some other stuff as well since we will be asked to insertInTextAreas:atIndex:3...
NSArray *textAreas = [self textAreas];
if (index == [textAreas count]) {
[self addInGraphics:graphic];
} else {
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[textAreas objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given text area in the graphics."];
}
}
}
- (void)removeFromTextAreasAtIndex:(unsigned)index {
NSArray *textAreas = [self textAreas];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[textAreas objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given text area in the graphics."];
}
}
- (void)replaceInTextAreas:(SKTGraphic *)graphic atIndex:(unsigned)index {
NSArray *textAreas = [self textAreas];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[textAreas objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given text area in the graphics."];
}
}
- (void)setImages:(NSArray *)images {
// We won't allow wholesale setting of these subset keys.
[NSException raise:NSOperationNotSupportedForKeyException format:@"Setting 'images' key is not supported."];
}
- (void)addInImages:(SKTGraphic *)graphic {
[self addInGraphics:graphic];
}
- (void)insertInImages:(SKTGraphic *)graphic atIndex:(unsigned)index {
// MF:!!! This is not going to be ideal. If we are being asked to, say, "make a new rectangle at after rectangle 2", we will be after rectangle 2, but we may be after some other stuff as well since we will be asked to insertInImages:atIndex:3...
NSArray *images = [self images];
if (index == [images count]) {
[self addInGraphics:graphic];
} else {
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[images objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given image in the graphics."];
}
}
}
- (void)removeFromImagesAtIndex:(unsigned)index {
NSArray *images = [self images];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[images objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given image in the graphics."];
}
}
- (void)replaceInImages:(SKTGraphic *)graphic atIndex:(unsigned)index {
NSArray *images = [self images];
NSArray *graphics = [self graphics];
int newIndex = [graphics indexOfObjectIdenticalTo:[images objectAtIndex:index]];
if (newIndex != NSNotFound) {
[self removeGraphicAtIndex:newIndex];
[self insertGraphic:graphic atIndex:newIndex];
} else {
// Shouldn't happen.
[NSException raise:NSRangeException format:@"Could not find the given image in the graphics."];
}
}
// The following "indicesOf..." methods are in support of scripting. They allow more flexible range and relative specifiers to be used with the different graphic keys of a SKTDrawDocument.
// The scripting engine does not know about the fact that the "rectangles" key is really just a subset of the "graphics" key, so script code like "rectangles from circle 1 to line 4" don't make sense to it. But Sketch does know and can answer such questions itself, with a little work.
- (NSArray *)indicesOfObjectsByEvaluatingRangeSpecifier:(NSRangeSpecifier *)rangeSpec {
NSString *key = [rangeSpec key];
if ([key isEqual:@"graphics"] || [key isEqual:@"rectangles"] || [key isEqual:@"circles"] || [key isEqual:@"lines"] || [key isEqual:@"textAreas"] || [key isEqual:@"images"]) {
// This is one of the keys we might want to deal with.
NSScriptObjectSpecifier *startSpec = [rangeSpec startSpecifier];
NSScriptObjectSpecifier *endSpec = [rangeSpec endSpecifier];
NSString *startKey = [startSpec key];
NSString *endKey = [endSpec key];
NSArray *graphics = [self graphics];
if ((startSpec == nil) && (endSpec == nil)) {
// We need to have at least one of these...
return nil;
}
if ([graphics count] == 0) {
// If there are no graphics, there can be no match. Just return now.
return [NSArray array];
}
if ((!startSpec || [startKey isEqual:@"graphics"] || [startKey isEqual:@"rectangles"] || [startKey isEqual:@"circles"] || [startKey isEqual:@"lines"] || [startKey isEqual:@"textAreas"] || [startKey isEqual:@"images"]) && (!endSpec || [endKey isEqual:@"graphics"] || [endKey isEqual:@"rectangles"] || [endKey isEqual:@"circles"] || [endKey isEqual:@"lines"] || [endKey isEqual:@"textAreas"] || [endKey isEqual:@"images"])) {
int startIndex;
int endIndex;
// The start and end keys are also ones we want to handle.
// The strategy here is going to be to find the index of the start and stop object in the full graphics array, regardless of what its key is. Then we can find what we're looking for in that range of the graphics key (weeding out objects we don't want, if necessary).
// First find the index of the first start object in the graphics array
if (startSpec) {
id startObject = [startSpec objectsByEvaluatingSpecifier];
if ([startObject isKindOfClass:[NSArray class]]) {
if ([startObject count] == 0) {
startObject = nil;
} else {
startObject = [startObject objectAtIndex:0];
}
}
if (!startObject) {
// Oops. We could not find the start object.
return nil;
}
startIndex = [graphics indexOfObjectIdenticalTo:startObject];
if (startIndex == NSNotFound) {
// Oops. We couldn't find the start object in the graphics array. This should not happen.
return nil;
}
} else {
startIndex = 0;
}
// Now find the index of the last end object in the graphics array
if (endSpec) {
id endObject = [endSpec objectsByEvaluatingSpecifier];
if ([endObject isKindOfClass:[NSArray class]]) {
unsigned endObjectsCount = [endObject count];
if (endObjectsCount == 0) {
endObject = nil;
} else {
endObject = [endObject objectAtIndex:(endObjectsCount-1)];
}
}
if (!endObject) {
// Oops. We could not find the end object.
return nil;
}
endIndex = [graphics indexOfObjectIdenticalTo:endObject];
if (endIndex == NSNotFound) {
// Oops. We couldn't find the end object in the graphics array. This should not happen.
return nil;
}
} else {
endIndex = [graphics count] - 1;
}
if (endIndex < startIndex) {
// Accept backwards ranges gracefully
int temp = endIndex;
endIndex = startIndex;
startIndex = temp;
}
{
// Now startIndex and endIndex specify the end points of the range we want within the graphics array.
// We will traverse the range and pick the objects we want.
// We do this by getting each object and seeing if it actually appears in the real key that we are trying to evaluate in.
NSMutableArray *result = [NSMutableArray array];
BOOL keyIsGraphics = [key isEqual:@"graphics"];
NSArray *rangeKeyObjects = (keyIsGraphics ? nil : [self valueForKey:key]);
id curObj;
unsigned curKeyIndex, i;
for (i=startIndex; i<=endIndex; i++) {
if (keyIsGraphics) {
[result addObject:[NSNumber numberWithInt:i]];
} else {
curObj = [graphics objectAtIndex:i];
curKeyIndex = [rangeKeyObjects indexOfObjectIdenticalTo:curObj];
if (curKeyIndex != NSNotFound) {
[result addObject:[NSNumber numberWithInt:curKeyIndex]];
}
}
}
return result;
}
}
}
return nil;
}
- (NSArray *)indicesOfObjectsByEvaluatingRelativeSpecifier:(NSRelativeSpecifier *)relSpec {
NSString *key = [relSpec key];
if ([key isEqual:@"graphics"] || [key isEqual:@"rectangles"] || [key isEqual:@"circles"] || [key isEqual:@"lines"] || [key isEqual:@"textAreas"] || [key isEqual:@"images"]) {
// This is one of the keys we might want to deal with.
NSScriptObjectSpecifier *baseSpec = [relSpec baseSpecifier];
NSString *baseKey = [baseSpec key];
NSArray *graphics = [self graphics];