[Drawkit] any help with read/write
Brad Larson
larson at sonoplot.com
Fri Aug 8 08:06:47 PDT 2008
On Aug 7, 2008, at 9:50 PM, James Maxwell wrote:
> In particular, I'm wondering how to get a DKDrawingDocument-based
> app to save to my own file type, extension, and all that. I have it
> working, for now, just using the defaults of "Drawing" and
> ".drawing", but I'd like to get my own file type and extension
> implemented.
>
> A quick list of steps would be a huge help.
>
First, you'll want to create a category on DKDrawing that will handle
your custom file format. Within that category, create two methods, a
class method that takes in an NSData object and returns a new
DKDrawing, and an instance method that returns an NSData object. The
first is your loading method, the second your saving one.
Next, you'll need to set up the file extension or UTI to use in your
application's Info.plist. To set up a simple file extension, go to
your Xcode project, click on the Targets tab, click on your
application's target, and click the Get Info button. In the
inspector, go to the Properties tab and add a new document type. Give
it a name (for example "Pattern"), leave the UTI field blank, set the
appropriate extension (or extensions) for this file format, set the
class to be your new custom document class, give it an icon, and set
the type to Editor or Viewer as appropriate.
UTIs are a little more involved, and require you to edit the
Info.plist. Find the Info.plist in your source files, right-click on
it, and choose to open it as a plain-text file. You'll need to add
XML to export your UTI definition similar to the following:
<key>UTExportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array>
<string>public.xml</string>
</array>
<key>UTTypeDescription</key>
<string>SonoPlot's proprietary XML pattern file type</string>
<key>UTTypeIdentifier</key>
<string>com.sonoplot.pattern</string>
<key>UTTypeTagSpecification</key>
<dict>
<key>public.filename-extension</key>
<array>
<string>pattern</string>
</array>
</dict>
</dict>
</array>
This example is for our custom XML-based file format. Repeat the same
steps as for setting up a new file extension type, only this time fill
in the UTI using the one you exported in your Info.plist.
Unfortunately, UTIs don't always work the way you want, such as when
another application on your system has claimed the same file extension
as your application.
Finally, you'll want to call the DKDrawingDocument class methods
bindFileExportType:toSelector: and bindFileImportType:toSelector: to
associate your file loading and saving methods from your custom
category with the new filetype or UTI. For example, I do
NSString* kSPPatternDocumentType = @"Pattern";
NSString* kSPPatternDocumentUTI = @"com.sonoplot.pattern";
[[self class] bindFileExportType:kSPPatternDocumentType
toSelector:@selector(dataForPattern)];
[[self class] bindFileExportType:kSPPatternDocumentUTI
toSelector:@selector(dataForPattern)];
[[self class] bindFileImportType:kSPPatternDocumentType
toSelector:@selector(drawingWithPattern:)];
[[self class] bindFileImportType:kSPPatternDocumentUTI
toSelector:@selector(drawingWithPattern:)];
in my document's init: method. I'm going to move that out of there,
because it only needs to be called once on the start of the program,
not with each new document.
For a working example of this, I direct you to the code for the beta
of SonoDraw that we have out there. I posted this earlier, but I can
repeat:
The source code is available for download from
http://www.sonoplot.com/sites/default/files/SonoDraw/SonoDrawBeta1.zip
and a compiled binary can be downloaded from
http://www.sonoplot.com/sites/default/files/SonoDraw/SonoDrawBeta1-Binary.zip
Example .pattern files that you can load with the software are also
available at
http://www.sonoplot.com/sites/default/files/SonoDraw/SamplePatterns.zip
I'm getting close to releasing the final version of this, and I'll be
sure to post that when it's done.
______________________
Brad Larson
SonoPlot, Inc.
3030 Laura Lane, Suite 120
Middleton, WI 53562
More information about the Drawkit
mailing list