file_dialog_open.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. buffer[FILE_DIALOG_MAX_BUFFER - 1] = 'x'; // Initialize last character with a char != '\0'
  23. #ifdef __APPLE__
  24. // For apple use applescript hack
  25. FILE * output = popen(
  26. "osascript -e \""
  27. " tell application \\\"System Events\\\"\n"
  28. " activate\n"
  29. " set existing_file to choose file\n"
  30. " end tell\n"
  31. " set existing_file_path to (POSIX path of (existing_file))\n"
  32. "\" 2>/dev/null | tr -d '\n' ","r");
  33. if (output)
  34. {
  35. auto ret = fgets(buffer, FILE_DIALOG_MAX_BUFFER, output);
  36. if (ret == NULL || ferror(output))
  37. {
  38. // I/O error
  39. buffer[0] = '\0';
  40. }
  41. if (buffer[FILE_DIALOG_MAX_BUFFER - 1] == '\0')
  42. {
  43. // File name too long, buffer has been filled, so we return empty string instead
  44. buffer[0] = '\0';
  45. }
  46. }
  47. #elif defined _WIN32
  48. // Use native windows file dialog box
  49. // (code contributed by Tino Weinkauf)
  50. OPENFILENAME ofn; // common dialog box structure
  51. char szFile[260]; // buffer for file name
  52. // Initialize OPENFILENAME
  53. ZeroMemory(&ofn, sizeof(ofn));
  54. ofn.lStructSize = sizeof(ofn);
  55. ofn.hwndOwner = NULL;
  56. ofn.lpstrFile = new char[100];
  57. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  58. // use the contents of szFile to initialize itself.
  59. ofn.lpstrFile[0] = '\0';
  60. ofn.nMaxFile = sizeof(szFile);
  61. ofn.lpstrFilter = "*.*\0";//off\0*.off\0obj\0*.obj\0mp\0*.mp\0";
  62. ofn.nFilterIndex = 1;
  63. ofn.lpstrFileTitle = NULL;
  64. ofn.nMaxFileTitle = 0;
  65. ofn.lpstrInitialDir = NULL;
  66. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  67. // Display the Open dialog box.
  68. int pos = 0;
  69. if (GetOpenFileName(&ofn)==TRUE)
  70. {
  71. while(ofn.lpstrFile[pos] != '\0')
  72. {
  73. buffer[pos] = (char)ofn.lpstrFile[pos];
  74. pos++;
  75. }
  76. }
  77. buffer[pos] = 0;
  78. #else
  79. // For linux use zenity
  80. FILE * output = popen("/usr/bin/zenity --file-selection","r");
  81. if (output)
  82. {
  83. auto ret = fgets(buffer, FILE_DIALOG_MAX_BUFFER, output);
  84. if (ret == NULL || ferror(output))
  85. {
  86. // I/O error
  87. buffer[0] = '\0';
  88. }
  89. if (buffer[FILE_DIALOG_MAX_BUFFER - 1] == '\0')
  90. {
  91. // File name too long, buffer has been filled, so we return empty string instead
  92. buffer[0] = '\0';
  93. }
  94. }
  95. // Replace last '\n' by '\0'
  96. if(strlen(buffer) > 0)
  97. {
  98. buffer[strlen(buffer)-1] = '\0';
  99. }
  100. #endif
  101. return std::string(buffer);
  102. }