Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I open a Open File dialog in OS X using C++?

I'm working on an application using OpenGL and C++ that parses some structured input from a file and displays it graphically. I'd like to start an Open File dialog when the application loads to allow the user to choose the file they want to have displayed. I haven't been able to find what I need on the web. Is there a way to achieve this in C++? If so, how? Thank you in advance.

like image 629
Lyudmil Avatar asked Jun 20 '10 00:06

Lyudmil


2 Answers

You have two choices, a quick one, and a good one:

  • Quick and pretty simple, use the Navigation Services framework from Carbon and NavCreateGetFileDialog(). You'll be done quick, and you'll have to learn almost nothing new, but your code won't run in 64-bit (which Apple is pushing everyone towards) and you'll have to link the Carbon framework. Navigation Services is officially removed in 64-bit, and is generally deprecated going forward (though I expect it to linger in 32-bit for quite a while).

  • A little more work the first time you do it (because you need to learn some Objective-C), but much more powerful and fully supported, wrap up NSOpenPanel in an Objective-C++ class and expose that to your C++. This is my Wrapping C++ pattern, just backwards. If you go this way and have trouble, drop a note and I'll try to speed up posting a blog entry on it.

like image 85
Rob Napier Avatar answered Oct 11 '22 12:10

Rob Napier


To add to what Rob wrote:

Unfortunately, there's no simple equivalent to Windows's GetOpenFileName.

  1. If you use Carbon: I don't really think NavCreatGetFileDialog is easy to use... you can use this code in the CarbonDev to see how to use it. The code there returns CFURLRef. To get the POSIX path, use CFURLGetFileSystemReprestnation.

  2. That said, I recommend you to use Cocoa. Rob will write a blog post how to use NSOpenPanel from GLUT :)

like image 25
Yuji Avatar answered Oct 11 '22 10:10

Yuji