file_dialog_open.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Daniele Panozzo <daniele.panozzo@gmail.com>
  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. #ifdef __APPLE__
  22. // For apple use applescript hack
  23. FILE * output = popen(
  24. "osascript -e \""
  25. " tell application \\\"System Events\\\"\n"
  26. " activate\n"
  27. " set existing_file to choose file\n"
  28. " end tell\n"
  29. " set existing_file_path to (POSIX path of (existing_file))\n"
  30. "\" 2>/dev/null | tr -d '\n' ","r");
  31. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
  32. {
  33. }
  34. #elif defined _WIN32
  35. // Use native windows file dialog box
  36. // (code contributed by Tino Weinkauf)
  37. OPENFILENAME ofn; // common dialog box structure
  38. char szFile[260]; // buffer for file name
  39. // Initialize OPENFILENAME
  40. ZeroMemory(&ofn, sizeof(ofn));
  41. ofn.lStructSize = sizeof(ofn);
  42. ofn.hwndOwner = NULL;
  43. ofn.lpstrFile = new char[100];
  44. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  45. // use the contents of szFile to initialize itself.
  46. ofn.lpstrFile[0] = '\0';
  47. ofn.nMaxFile = sizeof(szFile);
  48. ofn.lpstrFilter = "*.*\0";//off\0*.off\0obj\0*.obj\0mp\0*.mp\0";
  49. ofn.nFilterIndex = 1;
  50. ofn.lpstrFileTitle = NULL;
  51. ofn.nMaxFileTitle = 0;
  52. ofn.lpstrInitialDir = NULL;
  53. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  54. // Display the Open dialog box.
  55. int pos = 0;
  56. if (GetOpenFileName(&ofn)==TRUE)
  57. {
  58. while(ofn.lpstrFile[pos] != '\0')
  59. {
  60. buffer[pos] = (char)ofn.lpstrFile[pos];
  61. pos++;
  62. }
  63. }
  64. buffer[pos] = 0;
  65. #else
  66. // For linux use zenity
  67. FILE * output = popen("/usr/bin/zenity --file-selection","r");
  68. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
  69. {
  70. }
  71. if (strlen(buffer) > 0)
  72. {
  73. buffer[strlen(buffer)-1] = 0;
  74. }
  75. #endif
  76. return std::string(buffer);
  77. }