Custom accessory view for NSSavePanel of NSDocument
Asked Answered
F

1

6

Consider a Cocoa NSDocument that supports two document types (e.g. BMP and JPEG). While one type has no save options (e.g. BMP), the other does (e.g. compression level for JPEG).

How do you implement this?

I understand that you should override prepareSavePanel: and set the accessory view of the given NSSavePanel. However, doing this replaces the default accessory view with the document type pop-up.

Is it necessary to recreate the document type pop-up if when using a custom accessory view?

If yes, how can pass the selected document type and the additional save options to the NSDocument write methods?

If no, how can I show the additional save options (e.g. compression level) only if the corresponding document (e.g. JPEG) type has been selected? Is there a delegate method for document type changes in the NSSavePanel?

Fuss answered 12/3, 2013 at 9:35 Comment(0)
I
2

I'm pretty certain that yes, if you want a custom accessory view like this, you have to provide the entire thing. It's a shame — but there's no harm in filing a radar!

It might be possible to provide your own accessory view in -prepareSavePanel: and then override -fileTypeFromLastRunSavePanel to return the type selected. However, NSDocument's docs make no promises that it'll respect that, annoyingly. Try it and see!

if that doesn't work, it looks like you need to provide your own implementation of -runModalSavePanelForSaveOperation:delegate:didSaveSelector:contextInfo:. Follow Apple's description of the method:

The default implementation of this method first makes sure that any editor registered using Cocoa Bindings' NSEditorRegistration informal protocol has committed its changes, then creates a save panel, adds a standard "file format" accessory view if there is more than one file type for the user to choose from and [self shouldRunSavePanelWithAccessoryView] returns YES, sets various attributes of the panel, invokes [self prepareSavePanel:theSavePanel] to provide an opportunity for customization, then presents the panel. If the user OKs the panel -saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo: is invoked.

The docs for -writableTypesForSaveOperation: do note:

You can invoke this method when creating a custom save panel accessory view to easily present the same set of types that NSDocument would in its standard file format popup menu.

Incumbent answered 8/4, 2013 at 15:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.