dialog_darwin.mm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright 2019-2019 Attila Kocsis. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include "entry_p.h"
  6. #if BX_PLATFORM_OSX
  7. #include <bx/allocator.h>
  8. #include <bx/filepath.h>
  9. #include <bx/string.h>
  10. #include <bx/readerwriter.h>
  11. #include <bx/process.h>
  12. #include <bx/semaphore.h>
  13. #import <AppKit/AppKit.h>
  14. #include "dialog.h"
  15. class Split
  16. {
  17. public:
  18. Split(const bx::StringView& _str, char _ch)
  19. : m_str(_str)
  20. , m_token(_str.getPtr(), bx::strFind(_str, _ch).getPtr() )
  21. , m_ch(_ch)
  22. {
  23. }
  24. bx::StringView next()
  25. {
  26. bx::StringView result = m_token;
  27. m_token = bx::strTrim(
  28. bx::StringView(m_token.getTerm()+1, bx::strFind(bx::StringView(m_token.getTerm()+1, m_str.getTerm() ), m_ch).getPtr())
  29. , " \t\n"
  30. );
  31. return result;
  32. }
  33. bool isDone() const
  34. {
  35. return m_token.isEmpty();
  36. }
  37. private:
  38. const bx::StringView& m_str;
  39. bx::StringView m_token;
  40. char m_ch;
  41. };
  42. bool openFileSelectionDialog(
  43. bx::FilePath& _inOutFilePath
  44. , FileSelectionDialogType::Enum _type
  45. , const bx::StringView& _title
  46. , const bx::StringView& _filter
  47. )
  48. {
  49. NSMutableArray* fileTypes = [NSMutableArray arrayWithCapacity:10];
  50. for (bx::LineReader lr(_filter); !lr.isDone();)
  51. {
  52. const bx::StringView line = lr.next();
  53. const bx::StringView sep = bx::strFind(line, '|');
  54. if (!sep.isEmpty() )
  55. {
  56. for (Split split(bx::strTrim(bx::StringView(sep.getPtr()+1, line.getTerm() ), " "), ' ')
  57. ; !split.isDone()
  58. ;
  59. )
  60. {
  61. const bx::StringView token = split.next();
  62. if (token.getLength() >= 3
  63. && token.getPtr()[0] == '*'
  64. && token.getPtr()[1] == '.'
  65. && bx::isAlphaNum(token.getPtr()[2]) )
  66. {
  67. NSString* extension = [[NSString alloc] initWithBytes:token.getPtr()+2 length:token.getLength()-2 encoding:NSASCIIStringEncoding];
  68. [fileTypes addObject: extension];
  69. }
  70. }
  71. }
  72. }
  73. __block NSString* fileName = nil;
  74. void (^invokeDialog)(void) =
  75. ^{
  76. NSSavePanel* panel = nil;
  77. if (FileSelectionDialogType::Open == _type)
  78. {
  79. NSOpenPanel* openPanel = [NSOpenPanel openPanel];
  80. openPanel.canChooseFiles = TRUE;
  81. openPanel.allowsMultipleSelection = FALSE;
  82. openPanel.canChooseDirectories = FALSE;
  83. panel = openPanel;
  84. }
  85. else
  86. {
  87. panel = [NSSavePanel savePanel];
  88. }
  89. panel.message = [[NSString alloc] initWithBytes:_title.getPtr() length:_title.getLength() encoding:NSASCIIStringEncoding];
  90. panel.directoryURL = [NSURL URLWithString:@(_inOutFilePath.getCPtr())];
  91. panel.allowedFileTypes = fileTypes;
  92. if ([panel runModal] == NSModalResponseOK)
  93. {
  94. NSURL* url = [panel URL];
  95. if (nil != url)
  96. {
  97. fileName = [url path];
  98. [fileName retain];
  99. }
  100. }
  101. [panel close];
  102. };
  103. if ([NSThread isMainThread])
  104. {
  105. invokeDialog();
  106. }
  107. else
  108. {
  109. bx::Semaphore semaphore;
  110. bx::Semaphore* psemaphore = &semaphore;
  111. CFRunLoopPerformBlock(
  112. [[NSRunLoop mainRunLoop] getCFRunLoop]
  113. , kCFRunLoopCommonModes
  114. , ^{
  115. invokeDialog();
  116. psemaphore->post();
  117. });
  118. semaphore.wait();
  119. }
  120. if (fileName != nil)
  121. {
  122. _inOutFilePath.set([fileName UTF8String]);
  123. [fileName release];
  124. return true;
  125. }
  126. return false;
  127. }
  128. #endif // BX_PLATFORM_OSX