dialog.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright 2010-2025 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 && !BX_PLATFORM_VISIONOS
  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(
  79. m_token.getTerm()+1
  80. , bx::strFind(bx::StringView(m_token.getTerm()+1, m_str.getTerm() ), m_ch).getPtr()
  81. )
  82. , " \t\n"
  83. );
  84. return result;
  85. }
  86. bool isDone() const
  87. {
  88. return m_token.isEmpty();
  89. }
  90. private:
  91. const bx::StringView& m_str;
  92. bx::StringView m_token;
  93. char m_ch;
  94. };
  95. #if BX_PLATFORM_WINDOWS
  96. extern "C" typedef bool(__stdcall* OPENFILENAMEFUNCTION)(OPENFILENAMEA* _ofn);
  97. static const struct { OPENFILENAMEFUNCTION m_function; uint32_t m_flags; }
  98. s_getFileNameA[] =
  99. {
  100. { GetOpenFileNameA, /* OFN_EXPLORER */ 0x00080000 | /* OFN_DONTADDTORECENT */ 0x02000000 | /* OFN_FILEMUSTEXIST */ 0x00001000 },
  101. { GetSaveFileNameA, /* OFN_EXPLORER */ 0x00080000 | /* OFN_DONTADDTORECENT */ 0x02000000 },
  102. };
  103. static_assert(BX_COUNTOF(s_getFileNameA) == FileSelectionDialogType::Count);
  104. #endif
  105. #if !BX_PLATFORM_OSX
  106. bool openFileSelectionDialog(
  107. bx::FilePath& _inOutFilePath
  108. , FileSelectionDialogType::Enum _type
  109. , const bx::StringView& _title
  110. , const bx::StringView& _filter
  111. )
  112. {
  113. bx::Error err;
  114. char tmp[4096];
  115. bx::StaticMemoryBlockWriter writer(tmp, sizeof(tmp) );
  116. #if BX_PLATFORM_LINUX
  117. bx::write(&writer, &err
  118. , "--file-selection%s --title \"%.*s\" --filename \"%s\""
  119. , FileSelectionDialogType::Save == _type ? " --save" : ""
  120. , _title.getLength()
  121. , _title.getPtr()
  122. , _inOutFilePath.getCPtr()
  123. );
  124. for (bx::LineReader lr(_filter); !lr.isDone();)
  125. {
  126. const bx::StringView line = lr.next();
  127. bx::write(&writer, &err
  128. , " --file-filter \"%.*s\""
  129. , line.getLength()
  130. , line.getPtr()
  131. );
  132. }
  133. bx::write(&writer, '\0', &err);
  134. if (err.isOk() )
  135. {
  136. bx::ProcessReader pr;
  137. if (bx::open(&pr, "zenity", tmp, &err) )
  138. {
  139. char buffer[1024];
  140. int32_t total = bx::read(&pr, buffer, sizeof(buffer), &err);
  141. bx::close(&pr);
  142. if (0 == pr.getExitCode() )
  143. {
  144. _inOutFilePath.set(bx::strRTrim(bx::StringView(buffer, total), "\n\r") );
  145. return true;
  146. }
  147. }
  148. }
  149. #elif BX_PLATFORM_WINDOWS
  150. if (_type < 0
  151. || _type >= BX_COUNTOF(s_getFileNameA) )
  152. {
  153. return false;
  154. }
  155. char out[bx::kMaxFilePath] = { '\0' };
  156. OPENFILENAMEA ofn;
  157. bx::memSet(&ofn, 0, sizeof(ofn) );
  158. ofn.structSize = sizeof(OPENFILENAMEA);
  159. ofn.initialDir = _inOutFilePath.getCPtr();
  160. ofn.file = out;
  161. ofn.maxFile = sizeof(out);
  162. ofn.flags = s_getFileNameA[_type].m_flags;
  163. ofn.title = tmp;
  164. bx::write(&writer, &err, "%.*s", _title.getLength(), _title.getPtr() );
  165. bx::write(&writer, '\0', &err);
  166. ofn.filter = tmp + uint32_t(bx::seek(&writer) );
  167. for (bx::LineReader lr(_filter); !lr.isDone() && err.isOk();)
  168. {
  169. const bx::StringView line = lr.next();
  170. const bx::StringView sep = bx::strFind(line, '|');
  171. if (!sep.isEmpty() )
  172. {
  173. bx::write(&writer, bx::strTrim(bx::StringView(line.getPtr(), sep.getPtr() ), " "), &err);
  174. bx::write(&writer, '\0', &err);
  175. bool first = true;
  176. for (Split split(bx::strTrim(bx::StringView(sep.getPtr()+1, line.getTerm() ), " "), ' '); !split.isDone() && err.isOk();)
  177. {
  178. const bx::StringView token = split.next();
  179. if (!first)
  180. {
  181. bx::write(&writer, ';', &err);
  182. }
  183. first = false;
  184. bx::write(&writer, token, &err);
  185. }
  186. bx::write(&writer, '\0', &err);
  187. }
  188. else
  189. {
  190. bx::write(&writer, line, &err);
  191. bx::write(&writer, '\0', &err);
  192. bx::write(&writer, '\0', &err);
  193. }
  194. }
  195. bx::write(&writer, '\0', &err);
  196. if (err.isOk()
  197. && s_getFileNameA[_type].m_function(&ofn) )
  198. {
  199. _inOutFilePath.set(ofn.file);
  200. return true;
  201. }
  202. #else
  203. BX_UNUSED(_inOutFilePath, _type, _title, _filter, err, tmp, writer);
  204. #endif // BX_PLATFORM_LINUX
  205. return false;
  206. }
  207. #endif // !BX_PLATFORM_OSX