dialog.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright 2010-2023 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #include <bx/allocator.h>
  6. #include <bx/filepath.h>
  7. #include <bx/string.h>
  8. #include <bx/readerwriter.h>
  9. #include <bx/process.h>
  10. #include "dialog.h"
  11. #if BX_PLATFORM_WINDOWS
  12. typedef uintptr_t (__stdcall *LPOFNHOOKPROC)(void*, uint32_t, uintptr_t, uint64_t);
  13. struct OPENFILENAMEA
  14. {
  15. uint32_t structSize;
  16. void* hwndOwner;
  17. void* hinstance;
  18. const char* filter;
  19. const char* customFilter;
  20. uint32_t maxCustomFilter;
  21. uint32_t filterIndex;
  22. const char* file;
  23. uint32_t maxFile;
  24. const char* fileTitle;
  25. uint32_t maxFileTitle;
  26. const char* initialDir;
  27. const char* title;
  28. uint32_t flags;
  29. uint16_t fileOffset;
  30. uint16_t fileExtension;
  31. const char* defExt;
  32. uintptr_t customData;
  33. LPOFNHOOKPROC hook;
  34. const char* templateName;
  35. void* reserved0;
  36. uint32_t reserved1;
  37. uint32_t flagsEx;
  38. };
  39. extern "C" bool __stdcall GetOpenFileNameA(OPENFILENAMEA* _ofn);
  40. extern "C" bool __stdcall GetSaveFileNameA(OPENFILENAMEA * _ofn);
  41. extern "C" void* __stdcall GetModuleHandleA(const char* _moduleName);
  42. extern "C" uint32_t __stdcall GetModuleFileNameA(void* _module, char* _outFilePath, uint32_t _size);
  43. extern "C" void* __stdcall ShellExecuteA(void* _hwnd, void* _operation, void* _file, void* _parameters, void* _directory, int32_t _showCmd);
  44. #endif // BX_PLATFORM_WINDOWS
  45. void openUrl(const bx::StringView& _url)
  46. {
  47. char tmp[4096];
  48. #if BX_PLATFORM_WINDOWS
  49. # define OPEN ""
  50. #elif BX_PLATFORM_OSX
  51. # define OPEN "open "
  52. #else
  53. # define OPEN "xdg-open "
  54. #endif // BX_PLATFORM_OSX
  55. bx::snprintf(tmp, BX_COUNTOF(tmp), OPEN "%.*s", _url.getLength(), _url.getPtr() );
  56. #undef OPEN
  57. #if BX_PLATFORM_WINDOWS
  58. void* result = ShellExecuteA(NULL, NULL, tmp, NULL, NULL, false);
  59. BX_UNUSED(result);
  60. #elif !BX_PLATFORM_IOS
  61. int32_t result = system(tmp);
  62. BX_UNUSED(result);
  63. #endif // BX_PLATFORM_*
  64. }
  65. class Split
  66. {
  67. public:
  68. Split(const bx::StringView& _str, char _ch)
  69. : m_str(_str)
  70. , m_token(_str.getPtr(), bx::strFind(_str, _ch).getPtr() )
  71. , m_ch(_ch)
  72. {
  73. }
  74. bx::StringView next()
  75. {
  76. bx::StringView result = m_token;
  77. m_token = bx::strTrim(
  78. bx::StringView(m_token.getTerm()+1, bx::strFind(bx::StringView(m_token.getTerm()+1, m_str.getTerm() ), m_ch).getPtr() )
  79. , " \t\n"
  80. );
  81. return result;
  82. }
  83. bool isDone() const
  84. {
  85. return m_token.isEmpty();
  86. }
  87. private:
  88. const bx::StringView& m_str;
  89. bx::StringView m_token;
  90. char m_ch;
  91. };
  92. #if BX_PLATFORM_WINDOWS
  93. extern "C" typedef bool(__stdcall* OPENFILENAMEFUNCTION)(OPENFILENAMEA* _ofn);
  94. static const struct { OPENFILENAMEFUNCTION m_function; uint32_t m_flags; }
  95. s_getFileNameA[] =
  96. {
  97. { GetOpenFileNameA, /* OFN_EXPLORER */ 0x00080000 | /* OFN_DONTADDTORECENT */ 0x02000000 | /* OFN_FILEMUSTEXIST */ 0x00001000 },
  98. { GetSaveFileNameA, /* OFN_EXPLORER */ 0x00080000 | /* OFN_DONTADDTORECENT */ 0x02000000 },
  99. };
  100. BX_STATIC_ASSERT(BX_COUNTOF(s_getFileNameA) == FileSelectionDialogType::Count);
  101. #endif
  102. #if !BX_PLATFORM_OSX
  103. bool openFileSelectionDialog(
  104. bx::FilePath& _inOutFilePath
  105. , FileSelectionDialogType::Enum _type
  106. , const bx::StringView& _title
  107. , const bx::StringView& _filter
  108. )
  109. {
  110. #if BX_PLATFORM_LINUX
  111. char tmp[4096];
  112. bx::StaticMemoryBlockWriter writer(tmp, sizeof(tmp) );
  113. bx::Error err;
  114. bx::write(&writer, &err
  115. , "--file-selection%s --title \"%.*s\" --filename \"%s\""
  116. , FileSelectionDialogType::Save == _type ? " --save" : ""
  117. , _title.getLength()
  118. , _title.getPtr()
  119. , _inOutFilePath.getCPtr()
  120. );
  121. for (bx::LineReader lr(_filter); !lr.isDone();)
  122. {
  123. const bx::StringView line = lr.next();
  124. bx::write(&writer, &err
  125. , " --file-filter \"%.*s\""
  126. , line.getLength()
  127. , line.getPtr()
  128. );
  129. }
  130. bx::write(&writer, '\0', &err);
  131. if (err.isOk() )
  132. {
  133. bx::ProcessReader pr;
  134. if (bx::open(&pr, "zenity", tmp, &err) )
  135. {
  136. char buffer[1024];
  137. int32_t total = bx::read(&pr, buffer, sizeof(buffer), &err);
  138. bx::close(&pr);
  139. if (0 == pr.getExitCode() )
  140. {
  141. _inOutFilePath.set(bx::strRTrim(bx::StringView(buffer, total), "\n\r") );
  142. return true;
  143. }
  144. }
  145. }
  146. #elif BX_PLATFORM_WINDOWS
  147. if (_type < 0 || _type >= BX_COUNTOF(s_getFileNameA))
  148. return false;
  149. char out[bx::kMaxFilePath] = { '\0' };
  150. OPENFILENAMEA ofn;
  151. bx::memSet(&ofn, 0, sizeof(ofn) );
  152. ofn.structSize = sizeof(OPENFILENAMEA);
  153. ofn.initialDir = _inOutFilePath.getCPtr();
  154. ofn.file = out;
  155. ofn.maxFile = sizeof(out);
  156. ofn.flags = s_getFileNameA[_type].m_flags;
  157. char tmp[4096];
  158. bx::StaticMemoryBlockWriter writer(tmp, sizeof(tmp) );
  159. bx::Error err;
  160. ofn.title = tmp;
  161. bx::write(&writer, &err, "%.*s", _title.getLength(), _title.getPtr() );
  162. bx::write(&writer, '\0', &err);
  163. ofn.filter = tmp + uint32_t(bx::seek(&writer) );
  164. for (bx::LineReader lr(_filter); !lr.isDone() && err.isOk();)
  165. {
  166. const bx::StringView line = lr.next();
  167. const bx::StringView sep = bx::strFind(line, '|');
  168. if (!sep.isEmpty() )
  169. {
  170. bx::write(&writer, bx::strTrim(bx::StringView(line.getPtr(), sep.getPtr() ), " "), &err);
  171. bx::write(&writer, '\0', &err);
  172. bool first = true;
  173. for (Split split(bx::strTrim(bx::StringView(sep.getPtr()+1, line.getTerm() ), " "), ' '); !split.isDone() && err.isOk();)
  174. {
  175. const bx::StringView token = split.next();
  176. if (!first)
  177. {
  178. bx::write(&writer, ';', &err);
  179. }
  180. first = false;
  181. bx::write(&writer, token, &err);
  182. }
  183. bx::write(&writer, '\0', &err);
  184. }
  185. else
  186. {
  187. bx::write(&writer, line, &err);
  188. bx::write(&writer, '\0', &err);
  189. bx::write(&writer, '\0', &err);
  190. }
  191. }
  192. bx::write(&writer, '\0', &err);
  193. if (err.isOk()
  194. && s_getFileNameA[_type].m_function(&ofn))
  195. {
  196. _inOutFilePath.set(ofn.file);
  197. return true;
  198. }
  199. #else
  200. BX_UNUSED(_inOutFilePath, _type, _title, _filter);
  201. #endif // BX_PLATFORM_LINUX
  202. return false;
  203. }
  204. #endif // !BX_PLATFORM_OSX