Skip to content

Instantly share code, notes, and snippets.

@gszauer
Created June 25, 2014 21:04
Show Gist options
  • Save gszauer/d8c87317cec667037acf to your computer and use it in GitHub Desktop.
Save gszauer/d8c87317cec667037acf to your computer and use it in GitHub Desktop.
Dialogs
#import "OpenFile.h"
#if __APPLE__
#include <AppKit/AppKit.h>
#endif
/*
#ifndef FileOpen_OpenFile_h
#define FileOpen_OpenFile_h
#include <string>
#include <vector>
std::vector<std::string> OpenFile(bool multiple = false, std::vector<std::string>* extensions = 0);
#endif
*/
std::vector<std::string> OpenFile(bool multiple, std::vector<std::string>* extensions) {
std::vector<std::string> result;
#if __APPLE__ // Link to: AppKit
// http://cyborgdino.com/2012/02/nsopenpanel-displaying-a-file-open-dialog-in-os-x-10-7/
// http://iosdevelopertips.com/c/converting-between-c-and-objective-c-strings.html
// http://stackoverflow.com/questions/2335151/how-can-i-create-an-empty-array-in-objective-c-and-assign-value-into-it-one-by
// Create a File Open Dialog class.
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
if (extensions != 0) { // Set array of file types
NSMutableArray *fileTypesArray = [NSMutableArray array];
for (int i = 0; i < extensions->size(); ++i)
[fileTypesArray addObject: [NSString stringWithCString:extensions->at(i).c_str() encoding:NSUTF8StringEncoding]];
[openDlg setAllowedFileTypes:fileTypesArray];
}
[openDlg setCanChooseFiles:YES];
if (multiple)
[openDlg setAllowsMultipleSelection:TRUE];
else
[openDlg setAllowsMultipleSelection:FALSE];
if ( [openDlg runModal] == NSOKButton ) {
NSArray *files = [openDlg URLs];
for(int i = 0; i < [files count]; i++ ) {
NSString *str = [[files objectAtIndex:i] path];
const char *ptr = [str cStringUsingEncoding:NSUTF8StringEncoding];
result.push_back(ptr);
}
}
#endif
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment