Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / June 2006



Tip: Looking for answers? Try searching our database.

setAllowedFileTypes and NSOpenPanel

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
andrewbogott@gmail.com - 19 Jun 2006 15:15 GMT
I'm trying to create an open panel that changes the allowed file types
dynamically.  The panel includes an accessory view which, when
appropriate, calls setAllowedFileTypes on the panel.

I'm getting nowhere.  Calling setAllowedFileTypes after an NSOpenPanel
is visible seems to have no effect.  For example:

   openPanel = [[NSOpenPanel openPanel] retain];

   [openPanel setCanChooseFiles:TRUE];
   <snip>
   [openPanel beginForDirectory:nil
                       file:nil
                       types:[NSArray array] (1)
           modelessDelegate:nil

didEndSelector:@selector(openDocumentPanelDidEnd:returnCode:contextInfo:)
                contextInfo:NULL];

   [openPanel setAllowedFileTypes: nil]; (2)

In that case, the panel acts as though no file types are allowed --
appropriate behavior for the first types list ([NSArray array]) but not
for the second (nil.).  So I'm thinking, fair enough, it doesn't
support dynamic changing of the file types.

But... then why does the documentation describe setAllowedFileTypes as
a valid method for an NSOpenPanel?  Here are all the functions that
might invoke an NSOpenPanel:

-
beginForDirectory:file:types:modelessDelegate:didEndSelector:contextInfo:

-
beginSheetForDirectory:file:types:modalForWindow:modalDelegate:didEndSelector:contextInfo:

-  runModalForDirectory:file:types:
-  runModalForTypes:

Each takes a 'types' argument.  So in each case a previous call to
setAllowedFileTypes would be meaningless, as the allowedFileTypes would
be clobbered by the types: arg specified on invocation.  So
setAllowedFileTypes _must_ serve some use after the dialog is
visible... or the documentation is wrong.

Does anyone have any ideas about what's going on here?  Am I making a
foolish mistake?  If not, does anyone have an idea for another way to
make an open panel with a changeable type filter?

-Andrew
Simon Slavin - 21 Jun 2006 23:07 GMT
On 19/06/2006, andrewbogott@gmail.com wrote in message
<1150726519.766830.218680@g10g2000cwb.googlegroups.com>:

> I'm trying to create an open panel that changes the allowed file types
> dynamically.  The panel includes an accessory view which, when
> appropriate, calls setAllowedFileTypes on the panel.
>
> I'm getting nowhere.  Calling setAllowedFileTypes after an NSOpenPanel
> is visible seems to have no effect.

Sorry, but that method is not suitable when the user can already see the
panel: the list you supply is only consulted when the panel is created.
Instead, implement your own filter that looks at each file and decides
whether it should be allowed or not -- in your case, by simply examining the filetype.

Look in the definition for NSSavePanel for the delegate method

- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename

You have to set up a delegate to make it work, but it's not too hard to do and does exactly what you want done.  Although it's documented in NSSavePanel, NSOpenPanel inherits from that, so it works in both.

I've never used it so I'm not sure how to make it rescan the files already showing if the user changes the current file format selection.  I suspect it's something easy, like sending a refresh message to the panel.

Simon.
Signature

http://www.hearsay.demon.co.uk

Andrew - 23 Jun 2006 19:30 GMT
Thanks, Simon.  That turns out to work just fine.  And, indeed, it's
enough to just call [window update] to refresh the state of each file.

- (BOOL)panel:(id)sender shouldShowFilename:(NSString *)filename
{
   NSString* ext = [filename pathExtension];

   NSEnumerator* tagEnumerator = [[(NSOpenPanel *) [BottleSelection
window] allowedFileTypes] objectEnumerator];
   NSString* allowedExt;
   while ((allowedExt = [tagEnumerator nextObject]))
   {
       if ([ext caseInsensitiveCompare:allowedExt] == NSOrderedSame)
       {
           return TRUE;
       }
   }

   return FALSE;
}

(I'm cheating, and using the 'allowedFileTypes' member to store my
types list anyway.  That allows me to pretend, elsewhere, that setting
it actually works.)

So, that answers the 'what should I do?' question.  It leaves
unanswered the 'why did apple implement these functions when they can
never be usefully called?' question.  But I put in a bug report to
apple about that, so now I feel free to ignore that question as long as
they do.

Thanks again!

-Andrew

> On 19/06/2006, andrewbogott@gmail.com wrote in message
> <1150726519.766830.218680@g10g2000cwb.googlegroups.com>:
[quoted text clipped - 20 lines]
>
> Simon.
Simon Slavin - 26 Jun 2006 23:02 GMT
On 23/06/2006, Andrew wrote in message
<1151087427.246146.159470@r2g2000cwb.googlegroups.com>:

> So, that answers the 'what should I do?' question.

Glad you got it working.

> It leaves
> unanswered the 'why did apple implement these functions when they can
> never be usefully called?' question.

I think that those functions are there for another model of working.
Almost all applications don't have such flexible ways of opening files:
the application can only open certain kinds of file, and there's nothing
within the 'open' dialog that can change those kinds.  Under those
circumstances, the functions are fine: you set them once before you open
the panel and they tell the panel what to do.  Your program is more
sophisticated in that you want to change which kinds of file the panel
wants to work with when the panel has already been opened.

I agree that either the functions could be changed to work the way you
want, or that the documentation for them should state that those are the
ones to use before the panel is open and that they don't work after that.

Happy to see that you solved your problem.

Simon.
Signature

http://www.hearsay.demon.co.uk

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.