filtering file types by extension in open/save dialogs, in carbon

Discussion in 'Mac Programming' started by printf, Nov 15, 2008.

  1. printf macrumors regular

    Aug 27, 2008
    for NavCreateGetFileDialog/NavCreatePutFileDialog dialogs, how can i set filters on which items are visible, or at least enabled? for instance, i might wish to only allow the user to select files of the .xyz and .zyx types.

    popupExtension looked like a start, but i couldn't find sufficient documentation on how to actually set it up.

    i tried the following with arr containing test strings (not sure what they would actually be):
    CFStringRef arr[] = {CFSTR("test1"),CFSTR("test2")};
    dialogOptions.popupExtension = CFArrayCreate(NULL, (const void**) &arr, 2, NULL);

    but it would cause a crash at NavDialogRun, which returned a -50 status.

    i've scoured the web and found zilch, so if you have full source on a open or save function with the aforementioned filtering that compiles and works, i'd be grateful if you could post it.
  2. idelovski macrumors regular

    Sep 11, 2008
    I don't think popupExtension is what you need.

    If you have the book "Carbon Programming" or if you check the online version you'll see that popupExtension is "a handle to one or more structures of type NavMenuItemSpec used to add extra menu items to an Open dialog box's Show pop-up menu or a Save dialog box's Format pop-up menu."

    Maybe a filterProc you pass to Nav functions?
  3. printf thread starter macrumors regular

    Aug 27, 2008
    kainjow, NavDialogSetFilterTypeIdentifiers looks like it covers the part of what i need! thanks for that link! i would still like to display a 'Show' or 'Format' list in the dialog however, to allow the user to further filter the selection.

    Yes, i want this as well. Here let me explain further. When a user chooses the open dialog, I want to pre-filter what they can select from a list of filetypes. So, they might be able to select .abc .xyz, and .zyx. kainjow's answer showed me how to do that.

    BUT, in addition to that, they can use that 'Show' pop-up menu you mentioned, to further filter THAT list that I created down to one type. And for saving, I need the format pop-up to allow the user to choose 'Format' for their desired save type.

    This is what my windows app does, so I somehow have to duplicate that.
  4. idelovski macrumors regular

    Sep 11, 2008
    Have you looked up that link I supplied above? Everything is described in "The Show Pop-up Menu" section. Kainjow's link has few words about it too.
  5. printf thread starter macrumors regular

    Aug 27, 2008
    yeah, the article kainjow linked to only mentions going about it using NavDialogSetFilterTypeIdentifiers which takes uniform type identifiers (UTI's). i don't think this will work for me because my application opens and saves several different proprietary file types which os x isn't aware of, and i'm not sure how to 'register' them if there is such a process.

    your link, while seemingly on target with the 'Show pop-up menu' section mentions passing in a NavTypeList structure handle, but provides no example of how to populate the structure, particularly the OSType members, which again, look proprietary-type-unfriendly... sorry, guess i need a little more explanation here, because googling isn't providing what i need either.
  6. kainjow Moderator emeritus


    Jun 15, 2000
  7. printf thread starter macrumors regular

    Aug 27, 2008
    kainjow, thanks again for your help. i've finally got the 'Show' pop-up menu to display custom items, and i've created a Format pop-up button to display at the bottom of the dialog.

    the ONLY outstanding issue, is capturing the change event of that pop-up button. The kNavCBEvent event is different from standard events i'm used to working with, so i don't know if or how i can use GetEventParameter to verify the control via cmd.source.control and check the selected item via cmd.commandID.

    i've found some examples of capturing mouseDown using FindControlUnderMouse to confirm the control receiving the event, however i don't know how to get this to work for a pop-up button type which uses a MenuRef to make selections.
  8. kainjow Moderator emeritus


    Jun 15, 2000
    I would assume that handling the event for when the popup changes wouldn't be any different than normal. Have you tried installing normal event handlers for it (I'm not sure what those would be for popup buttons).

    Alternatively, if you still have a reference to the control after the nav dialog is closed, couldn't you just get access to the selected item?
  9. idelovski macrumors regular

    Sep 11, 2008
    Maybe googling for kNavCBEvent and kNavCBPopupMenuSelect could help.

    I have never used that popUp menu on Nav dialogs so I can't send any of my own code. I have only that filter proc that checks file extensions first and then fyleTypes. I still don't understand why is Open button disabled in OpenDialog when folders are selected in lists, even though Return key works fine and pressing it would open selected folder. Bug or feature?

    Have you ever used Google Code Search or or similar sites?

    This MacTech article (from 1998) should point you somewhere.

    I remember that when I decided to start using Nav services I wasted maybe whole week with it. I have no idea why they made it so complicated. So, don't give up and post some code back when you're done.
  10. printf thread starter macrumors regular

    Aug 27, 2008
    i finally got it working. my initial approach was correct, but my execution was flawed. the problems was that i was setting up the popupExtension member without yet supplying NavCreatePutFileDialog with a callback proc or client data.

    once i added those, it was as simple as catching the kNavCBPopupMenuSelect event and getting the selected index of the pop-up menu.

    that was far too much work for what it yielded. apple needs to work on providing better documentation for it's api, and rally legions of developers to write for their os. it's really sad that most of my googling for this issue resulted in 20-40 results compared to the thousands i'd get searching win32-based counterparts.

    having said that, thank you kainjow and idelovski for your help with this!
  11. kainjow Moderator emeritus


    Jun 15, 2000
    Great to see you got it working.

    Well the fact of the matter is, virtually all new developers are using Cocoa instead of Carbon for their GUIs, and the majority of those who already are using Carbon know how to do this stuff, so making their APIs simpler and implementing better documentation for Carbon I doubt is on Apple's todo list.

    One interesting tidbit is in Leopard Apple actually rewrote their navigation dialog API for Carbon to be a wrapper around the Cocoa classes so they both have the same look and feel. If you called CFShow() and passed in your NavDialogRef, Console would spit out the name of a Cocoa class.

    From the Leopard release notes:
  12. EricHuang macrumors newbie

    Dec 3, 2008

Share This Page