file dialog??

it is possible to open a system file dialog to select a video file to load?

This is something that has to be done on a per-platform basis I believe. Here’s how to do it:

Windows:

  
char szFileName[MAX_PATH] = "";  
  
	OPENFILENAME ofn;  
	ZeroMemory(&ofn, sizeof(ofn));  
	ofn.lStructSize = sizeof(ofn);  
	ofn.hwndOwner = hwnd;  
	ofn.lpstrFilter = "All Files (*.*)\0*.*\0";  
	ofn.lpstrFile = szFileName;  
	ofn.nMaxFile = MAX_PATH;  
	ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;  
	ofn.lpstrDefExt = 0;  
  
	if(GetOpenFileName(&ofn)) {  
		loadfile(szFileName);  
	}  

OSX:

  
NSOpenPanel *opanel = [NSOpenPanel openPanel];  
	[opanel setAllowsMultipleSelection:NO];  
	int returnCode = [opanel runModalForDirectory:nil file:nil types:nil];  
  
	if(returnCode == NSOKButton) {  
		NSArray *filenames = [opanel filenames];  
		NSString *file = [filenames objectAtIndex:0];  
		[self loadFile:file];  
	}  

Linux (GTK)

  
GtkWidget *dialog;  
  
    dialog = gtk_file_chooser_dialog_new ("Open File",  
                          NULL,  
                          GTK_FILE_CHOOSER_ACTION_OPEN,  
                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,  
                          GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,  
                          NULL);  
  
    if(gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {  
        char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));  
        fileopen (filename);  
        g_free (filename);  
    }  
    gtk_widget_destroy(dialog);  

Hmm… Might make a nice little library addon… :slight_smile:

The os x stuff is in cocoa so unless you are working in obj-c++ you might want to take a look at carbon and this article i found quickly in google:
http://www.peachpit.com/articles/article.aspx?p=24467

[EDIT] here is another from http://paste.lisp.org/display/18561 :

  
//---------------------------------------------------------------------  
// Gets a file to open from the user. Caller must release the CFURLRef.  
CFURLRef GetOpenFileFromUser(void)  
{  
        NavDialogCreationOptions dialogOptions;  
        NavDialogRef dialog;  
        NavReplyRecord replyRecord;  
        CFURLRef fileAsCFURLRef = NULL;  
        FSRef fileAsFSRef;  
        OSStatus status;  
  
        // Get the standard set of defaults  
        status = NavGetDefaultDialogCreationOptions(&dialogOptions);  
        require_noerr( status, CantGetNavOptions );  
  
        // Make the window app-wide modal  
        dialogOptions.modality = kWindowModalityAppModal;  
  
        // Create the dialog  
        status = NavCreateGetFileDialog(&dialogOptions, NULL, NULL, NULL, NULL, NULL, &dialog);  
        require_noerr( status, CantCreateDialog );  
  
        // Show it  
        status = NavDialogRun(dialog);  
        require_noerr( status, CantRunDialog );  
          
        // Get the reply  
        status = NavDialogGetReply(dialog, &replyRecord);  
        require( ((status == noErr) || (status == userCanceledErr)), CantGetReply );  
  
        // If the user clicked "Cancel", just bail  
        if ( status == userCanceledErr ) goto UserCanceled;  
  
        // Get the file  
        status = AEGetNthPtr(&(replyRecord.selection), 1, typeFSRef, NULL, NULL, &fileAsFSRef, sizeof(FSRef), NULL);  
        require_noerr( status, CantExtractFSRef );  
          
        // Convert it to a CFURL  
        fileAsCFURLRef = CFURLCreateFromFSRef(NULL, &fileAsFSRef);  
  
        // Cleanup  
CantExtractFSRef:  
UserCanceled:  
        verify_noerr( NavDisposeReply(&replyRecord) );  
CantGetReply:  
CantRunDialog:  
        NavDialogDispose(dialog);  
CantCreateDialog:  
CantGetNavOptions:  
    return fileAsCFURLRef;  
}  

I hate to use goto but if you hate them too you could find a good workaround. To find out more look up NavCreateGetFileDialog or Carbon Navigation Services in google.

ding

just out of curiosity, does anybody have a safe file dialog working in osx?

thanks,

jeremy

try this as a starting point:

http://www.frey.co.nz/blog/2008/05/02/s-…-osxcarbon/

not sure how working it is, though… OSX file dialog stuff is ridiculously complicated.

Or if cocoa is more your style there’s:
http://developer.apple.com/documentation/Cocoa/Conceptual/AppFileMgmt/Tasks/UsingASavePanel.html

thanks, damian, that code proved to be helpful (is that a sarcastic comment at the bottom there?).

here’s a cleaned up version which takes the CFString and FSRef and converts them into friendly c++ strings.

and i removed some of the stuff at the bottom so that this code does not create a file, just a file name.

  
  
  
short fRefNumOut;  
	FSRef output_file;  
	OSStatus err;  
	  
	NavDialogCreationOptions options;  
	NavGetDefaultDialogCreationOptions( &options );  
	options.modality = kWindowModalityAppModal;  
	  
	NavDialogRef dialog;  
	err = NavCreatePutFileDialog(&options, '.mov', 'Moov', NULL, NULL, &dialog);  
	printf("NavCreatePutFileDialog returned %i\n", err );  
	  
	err = NavDialogRun(dialog);  
	printf("NavDialogRun returned %i\n", err );  
	  
	NavUserAction action;  
	action = NavDialogGetUserAction( dialog );  
	printf("got action %i\n", action);  
	if (action == kNavUserActionNone || action == kNavUserActionCancel) {  
		  
		return 2;  
	}  
	  
	// get dialog reply  
	NavReplyRecord reply;  
	err = NavDialogGetReply(dialog, &reply);  
	if ( err != noErr )  
		return 2;  
	  
	if ( reply.replacing )  
	{  
		printf("need to replace\n");  
	}  
	  
	AEKeyword keyword;  
	DescType actual_type;  
	Size actual_size;  
	FSRef output_dir;  
	err = AEGetNthPtr(&(reply.selection), 1, typeFSRef, &keyword, &actual_type,  
						  &output_dir, sizeof(output_file), &actual_size);  
	  
	//printf("AEGetNthPtr returned %i\n", err );  
	  
	  
	CFURLRef cfUrl = CFURLCreateFromFSRef( kCFAllocatorDefault, &output_dir );  
	CFStringRef cfString = NULL;  
	if ( cfUrl != NULL )  
	{  
	  cfString = CFURLCopyFileSystemPath( cfUrl, kCFURLPOSIXPathStyle );  
	  CFRelease( cfUrl );  
	}  
	  
	 // copy from a CFString into a local c string ([http://www.carbondev.com/site/?page=CStrings+)](http://www.carbondev.com/site/?page=CStrings+))  
	const int kBufferSize = 255;  
	  
	char folderURL[kBufferSize];  
	Boolean bool1 = CFStringGetCString(cfString,folderURL,kBufferSize,kCFStringEncodingMacRoman);  
	  
	char fileName[kBufferSize];  
	Boolean bool2 = CFStringGetCString(reply.saveFileName,fileName,kBufferSize,kCFStringEncodingMacRoman);  
	  
	// append strings together  
	  
	string url1 = folderURL;  
	string url2 = fileName;  
	string finalURL = url1 + "/" + url2;  
	  
	printf("url %s\n", finalURL.c_str());  
  
	// cleanup dialog  
	NavDialogDispose(dialog);  

one more thing:

if you want to set the default file name, call this before caling NavCreatePutFileDialog.

  
  
  
options.saveFileName = CFStringCreateWithCString(NULL, "Untitled.mov", kCFStringEncodingASCII);  
  
  

jeremy

where am I supposed to put that code for the save dialog?

hi moka

right after you declare the options variable.

  
  
  
NavDialogCreationOptions options;  
	NavGetDefaultDialogCreationOptions( &options );  
	options.modality = kWindowModalityAppModal;  
	  
	options.saveFileName = CFStringCreateWithCString(NULL, "Untitled.mov", kCFStringEncodingASCII);  
  
  

thanks, got it :slight_smile:

hi.

otherside, where do i need to put the windows code you provided?
i have several warnings in my build messages window

(of course i’m a beginner)

thanks,

krisztian

Hi,

I’ve created an addon to open a file dialog in OSX. (one for saving , one for opening)
[attachment=0:27i85ghp]ofxFileDialogOSX.zip[/attachment:27i85ghp]

Everything seems to work nicely, but when I run it with Leaks, I get a lot of leaked bytes every time I open a dialog.
Does anyone know how to solve this?

thanks!
tim.

ofxFileDialogOSX.zip

ok! I think I solved the leaks problem. I think you need to call

  
 CFRelease( cfString );   

after doing

  
   
cfString = CFURLCopyFileSystemPath( cfUrl, kCFURLPOSIXPathStyle ); // LEAK?  
  

If anyone feels like checking if that makes sense, please feel free to do so!
anyway, here is the update version: when I run it with leaks, it reports 0 leaked bytes! yaay!
[attachment=0:2wqewoia]ofxFileDialogOSX.zip[/attachment:2wqewoia]

best,
tim

ofxFileDialogOSX.zip