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 <Commdlg.h>
  13. #endif
  14. IGL_INLINE std::string igl::file_dialog_open()
  15. {
  16. const int FILE_DIALOG_MAX_BUFFER = 1024;
  17. char buffer[FILE_DIALOG_MAX_BUFFER];
  18. #ifdef __APPLE__
  19. // For apple use applescript hack
  20. FILE * output = popen(
  21. "osascript -e \""
  22. " tell application \\\"System Events\\\"\n"
  23. " activate\n"
  24. " set existing_file to choose file\n"
  25. " end tell\n"
  26. " set existing_file_path to (POSIX path of (existing_file))\n"
  27. "\" 2>/dev/null | tr -d '\n' ","r");
  28. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
  29. {
  30. }
  31. #elif _WIN32
  32. // Use native windows file dialog box
  33. // (code contributed by Tino Weinkauf)
  34. OPENFILENAME ofn; // common dialog box structure
  35. char szFile[260]; // buffer for file name
  36. HWND hwnd; // owner window
  37. HANDLE hf; // file handle
  38. // Initialize OPENFILENAME
  39. ZeroMemory(&ofn, sizeof(ofn));
  40. ofn.lStructSize = sizeof(ofn);
  41. ofn.hwndOwner = NULL;//hwnd;
  42. ofn.lpstrFile = new wchar_t[100];
  43. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  44. // use the contents of szFile to initialize itself.
  45. ofn.lpstrFile[0] = '\0';
  46. ofn.nMaxFile = sizeof(szFile);
  47. ofn.lpstrFilter = L"*.*\0";//off\0*.off\0obj\0*.obj\0mp\0*.mp\0";
  48. ofn.nFilterIndex = 1;
  49. ofn.lpstrFileTitle = NULL;
  50. ofn.nMaxFileTitle = 0;
  51. ofn.lpstrInitialDir = NULL;
  52. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  53. // Display the Open dialog box.
  54. int pos = 0;
  55. if (GetOpenFileName(&ofn)==TRUE)
  56. {
  57. while(ofn.lpstrFile[pos] != '\0')
  58. {
  59. buffer[pos] = (char)ofn.lpstrFile[pos];
  60. pos++;
  61. }
  62. buffer[pos] = 0;
  63. }
  64. #else
  65. // For linux use zenity
  66. FILE * output = popen("/usr/bin/zenity --file-selection","r");
  67. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
  68. {
  69. }
  70. if (strlen(buffer) > 0)
  71. {
  72. buffer[strlen(buffer)-1] = 0;
  73. }
  74. #endif
  75. return std::string(buffer);
  76. }