browser-window.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #if defined(Hiro_BrowserWindow)
  2. namespace hiro {
  3. auto pBrowserWindow::directory(BrowserWindow::State& state) -> string {
  4. string result;
  5. @autoreleasepool {
  6. NSOpenPanel* panel = [NSOpenPanel openPanel];
  7. if(state.title) [panel setTitle:[NSString stringWithUTF8String:state.title]];
  8. [panel setCanChooseDirectories:YES];
  9. [panel setCanChooseFiles:NO];
  10. if([panel runModalForDirectory:[NSString stringWithUTF8String:state.path] file:nil] == NSOKButton) {
  11. NSArray* names = [panel filenames];
  12. const char* name = [[names objectAtIndex:0] UTF8String];
  13. if(name) result = name;
  14. }
  15. }
  16. return result;
  17. }
  18. auto pBrowserWindow::open(BrowserWindow::State& state) -> string {
  19. string result;
  20. @autoreleasepool {
  21. NSMutableArray* filters = [[NSMutableArray alloc] init];
  22. for(auto& rule : state.filters) {
  23. string pattern = rule.split("|", 1L)(1).transform(":", ";");
  24. if(pattern) [filters addObject:[NSString stringWithUTF8String:pattern]];
  25. }
  26. NSOpenPanel* panel = [NSOpenPanel openPanel];
  27. if(state.title) [panel setTitle:[NSString stringWithUTF8String:state.title]];
  28. [panel setCanChooseDirectories:NO];
  29. [panel setCanChooseFiles:YES];
  30. [panel setAllowedFileTypes:filters];
  31. if([panel runModalForDirectory:[NSString stringWithUTF8String:state.path] file:nil] == NSOKButton) {
  32. NSArray* names = [panel filenames];
  33. const char* name = [[names objectAtIndex:0] UTF8String];
  34. if(name) result = name;
  35. }
  36. [filters release];
  37. }
  38. return result;
  39. }
  40. auto pBrowserWindow::save(BrowserWindow::State& state) -> string {
  41. string result;
  42. @autoreleasepool {
  43. NSMutableArray* filters = [[NSMutableArray alloc] init];
  44. for(auto& rule : state.filters) {
  45. string pattern = rule.split("|", 1L)(1).transform(":", ";");
  46. if(pattern) [filters addObject:[NSString stringWithUTF8String:pattern]];
  47. }
  48. NSSavePanel* panel = [NSSavePanel savePanel];
  49. if(state.title) [panel setTitle:[NSString stringWithUTF8String:state.title]];
  50. [panel setAllowedFileTypes:filters];
  51. if([panel runModalForDirectory:[NSString stringWithUTF8String:state.path] file:nil] == NSOKButton) {
  52. NSArray* names = [panel filenames];
  53. const char* name = [[names objectAtIndex:0] UTF8String];
  54. if(name) result = name;
  55. }
  56. [filters release];
  57. }
  58. return result;
  59. }
  60. }
  61. #endif