file_dialog_open.cpp 2.3 KB

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