Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Created September 27, 2013 09:17
Show Gist options
  • Save ruby0x1/6726059 to your computer and use it in GitHub Desktop.
Save ruby0x1/6726059 to your computer and use it in GitHub Desktop.
rushed/quick Cocoa File dialogs. import cocoa.h.
std::string FileDialogOpen( const std::string &title, const std::string &text, const std::vector<std::string> &fileTypes ) {
// NSArray *fileTypes = [NSArray arrayWithObjects:@"jpg",@"jpeg",nil];
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:NO];
[panel setCanChooseFiles:YES];
[panel setFloatingPanel:YES];
[panel setTitle: [NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]] ];
NSInteger result = [ panel runModalForDirectory:NSHomeDirectory() file:nil types:nil ];
if(result == NSOKButton) {
NSString *pathfile = [[panel URL] path];
std::string _path = std::string( [pathfile UTF8String] );
[pathfile release];
return _path;
}
return std::string();
} //FileDialogOpen
std::string FileDialogSave( const std::string &title, const std::string &text, const std::vector<std::string> &fileTypes ) {
NSSavePanel *panel = [NSSavePanel savePanel];
[panel setAllowsOtherFileTypes:YES];
[panel setExtensionHidden:YES];
[panel setCanCreateDirectories:YES];
// [panel setNameFieldStringValue:filename];
[panel setTitle: [NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]] ];
NSInteger result = [panel runModal];
NSError *error = nil;
if (result == NSOKButton) {
NSString *pathfile = [[panel URL] path];
std::string _path = std::string( [pathfile UTF8String] );
[pathfile release];
return _path;
}
return std::string();
} //FileDialogSave
std::string FileDialogFolder( const std::string &title, const std::string &text ) {
NSOpenPanel * panel = [NSOpenPanel openPanel];
[panel setAllowsMultipleSelection:NO];
[panel setCanChooseDirectories:YES];
[panel setCanChooseFiles:NO];
[panel setFloatingPanel:YES];
[panel setTitle: [NSString stringWithCString:title.c_str() encoding:[NSString defaultCStringEncoding]] ];
NSInteger result = [ panel runModalForDirectory:NSHomeDirectory() file:nil types:nil ];
if(result == NSOKButton) {
NSString *pathfile = [[panel URL] path];
std::string _path = std::string( [pathfile UTF8String] );
[pathfile release];
return _path;
} //result
return std::string();
} //FileDialogFolder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment