2
0

file_dialog_open.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <[email protected]>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "file_dialog_open.h"
  9. #include <cstdio>
  10. #include <cstring>
  11. #ifdef _WIN32
  12. #include <windows.h>
  13. #undef max
  14. #undef min
  15. #include <Commdlg.h>
  16. #endif
  17. IGL_INLINE std::string igl::file_dialog_open()
  18. {
  19. const int FILE_DIALOG_MAX_BUFFER = 1024;
  20. char buffer[FILE_DIALOG_MAX_BUFFER];
  21. buffer[0] = '\0';
  22. #ifdef __APPLE__
  23. // For apple use applescript hack
  24. FILE * output = popen(
  25. "osascript -e \""
  26. " tell application \\\"System Events\\\"\n"
  27. " activate\n"
  28. " set existing_file to choose file\n"
  29. " end tell\n"
  30. " set existing_file_path to (POSIX path of (existing_file))\n"
  31. "\" 2>/dev/null | tr -d '\n' ","r");
  32. if( !output || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) == NULL || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL)
  33. {
  34. buffer[0] = '\0';
  35. }
  36. #elif defined _WIN32
  37. // Use native windows file dialog box
  38. // (code contributed by Tino Weinkauf)
  39. OPENFILENAME ofn; // common dialog box structure
  40. char szFile[260]; // buffer for file name
  41. // Initialize OPENFILENAME
  42. ZeroMemory(&ofn, sizeof(ofn));
  43. ofn.lStructSize = sizeof(ofn);
  44. ofn.hwndOwner = NULL;
  45. ofn.lpstrFile = new char[100];
  46. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  47. // use the contents of szFile to initialize itself.
  48. ofn.lpstrFile[0] = '\0';
  49. ofn.nMaxFile = sizeof(szFile);
  50. ofn.lpstrFilter = "*.*\0";//off\0*.off\0obj\0*.obj\0mp\0*.mp\0";
  51. ofn.nFilterIndex = 1;
  52. ofn.lpstrFileTitle = NULL;
  53. ofn.nMaxFileTitle = 0;
  54. ofn.lpstrInitialDir = NULL;
  55. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  56. // Display the Open dialog box.
  57. int pos = 0;
  58. if (GetOpenFileName(&ofn)==TRUE)
  59. {
  60. while(ofn.lpstrFile[pos] != '\0')
  61. {
  62. buffer[pos] = (char)ofn.lpstrFile[pos];
  63. pos++;
  64. }
  65. }
  66. buffer[pos] = 0;
  67. #else
  68. // For linux use zenity
  69. FILE * output = popen("/usr/bin/zenity --file-selection","r");
  70. if( !output || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) == NULL || fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL)
  71. {
  72. buffer[0] = '\0';
  73. }
  74. if(strlen(buffer) > 0)
  75. {
  76. buffer[strlen(buffer)-1] = '\0';
  77. }
  78. #endif
  79. return std::string(buffer);
  80. }