Lyudmil
Lyudmil

Reputation: 1163

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.

Upvotes: 6

Views: 6021

Answers (3)

Fedor
Fedor

Reputation: 21291

Probably the best way to open file dialog on macOS is to use AppKit framework, which a part of Cocoa API.

You will need the class NSOpenPanel from AppKit, and create appropriate Objective-C++ wrapping around to use it from an ordinary C++ code. It is not an easy task for a person without much macOS development experience. Fortunately, it is not necessary nowadays, since existing wrappers already can be found in GitHub. Please take a look, for example, at

Upvotes: -1

Rob Napier
Rob Napier

Reputation: 299355

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.

Upvotes: 2

Yuji
Yuji

Reputation: 34185

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 :)

Upvotes: 3

Related Questions