tags:

views:

55

answers:

2

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.

+2  A: 

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.

Rob Napier
+2  A: 

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

Yuji
hahahaha.... Actually, poking around a little more, Apple has provided some nice sample code for GLUT. Most useful for this problem is GLUTWindow, since it gives you the handle you the objects you'll need to run a sheet. Apple also has a least one Technote on using GLUT; it's worth searching their site for more stuff. http://developer.apple.com/mac/library/samplecode/glut
Rob Napier