file_dialog.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #define FILE_DIALOG_MAX_BUFFER 1024
  9. std::string IGL_INLINE igl::file_dialog_open()
  10. {
  11. char buffer[FILE_DIALOG_MAX_BUFFER];
  12. #ifdef __APPLE__
  13. // For apple use applescript hack
  14. FILE * output = popen(
  15. "osascript -e \""
  16. " tell application \\\"System Events\\\"\n"
  17. " activate\n"
  18. " set existing_file to choose file\n"
  19. " end tell\n"
  20. " set existing_file_path to (POSIX path of (existing_file))\n"
  21. "\" 2>/dev/null | tr -d '\n' ","r");
  22. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
  23. }
  24. #elif _WIN32
  25. // Use native windows file dialog box
  26. // (code contributed by Tino Weinkauf)
  27. OPENFILENAME ofn; // common dialog box structure
  28. char szFile[260]; // buffer for file name
  29. HWND hwnd; // owner window
  30. HANDLE hf; // file handle
  31. // Initialize OPENFILENAME
  32. ZeroMemory(&ofn, sizeof(ofn));
  33. ofn.lStructSize = sizeof(ofn);
  34. ofn.hwndOwner = NULL;//hwnd;
  35. ofn.lpstrFile = new wchar_t[100];
  36. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  37. // use the contents of szFile to initialize itself.
  38. ofn.lpstrFile[0] = '\0';
  39. ofn.nMaxFile = sizeof(szFile);
  40. ofn.lpstrFilter = L"*.*\0";//off\0*.off\0obj\0*.obj\0mp\0*.mp\0";
  41. ofn.nFilterIndex = 1;
  42. ofn.lpstrFileTitle = NULL;
  43. ofn.nMaxFileTitle = 0;
  44. ofn.lpstrInitialDir = NULL;
  45. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  46. // Display the Open dialog box.
  47. int pos = 0;
  48. if (GetOpenFileName(&ofn)==TRUE)
  49. {
  50. while(ofn.lpstrFile[pos] != '\0')
  51. {
  52. buffer[pos] = (char)ofn.lpstrFile[pos];
  53. pos++;
  54. }
  55. buffer[pos] = 0;
  56. }
  57. #else
  58. // For linux use zenity
  59. FILE * output = popen("/usr/bin/zenity --file-selection","r");
  60. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
  61. }
  62. if (strlen(buffer) > 0)
  63. buffer[strlen(buffer)-1] = 0;
  64. #endif
  65. return std::string(buffer);
  66. }
  67. std::string IGL_INLINE igl::file_dialog_save()
  68. {
  69. char buffer[FILE_DIALOG_MAX_BUFFER];
  70. #ifdef __APPLE__
  71. // For apple use applescript hack
  72. // There is currently a bug in Applescript that strips extensions off
  73. // of chosen existing files in the "choose file name" dialog
  74. // I'm assuming that will be fixed soon
  75. FILE * output = popen(
  76. "osascript -e \""
  77. " tell application \\\"System Events\\\"\n"
  78. " activate\n"
  79. " set existing_file to choose file name\n"
  80. " end tell\n"
  81. " set existing_file_path to (POSIX path of (existing_file))\n"
  82. "\" 2>/dev/null | tr -d '\n' ","r");
  83. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
  84. }
  85. #elif _WIN32
  86. // Use native windows file dialog box
  87. // (code contributed by Tino Weinkauf)
  88. OPENFILENAME ofn; // common dialog box structure
  89. char szFile[260]; // buffer for file name
  90. HWND hwnd; // owner window
  91. HANDLE hf; // file handle
  92. // Initialize OPENFILENAME
  93. ZeroMemory(&ofn, sizeof(ofn));
  94. ofn.lStructSize = sizeof(ofn);
  95. ofn.hwndOwner = NULL;//hwnd;
  96. ofn.lpstrFile = new wchar_t[100];
  97. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  98. // use the contents of szFile to initialize itself.
  99. ofn.lpstrFile[0] = '\0';
  100. ofn.nMaxFile = sizeof(szFile);
  101. ofn.lpstrFilter = L"";
  102. ofn.nFilterIndex = 1;
  103. ofn.lpstrFileTitle = NULL;
  104. ofn.nMaxFileTitle = 0;
  105. ofn.lpstrInitialDir = NULL;
  106. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  107. // Display the Open dialog box.
  108. int pos = 0;
  109. if (GetSaveFileName(&ofn)==TRUE)
  110. {
  111. while(ofn.lpstrFile[pos] != '\0')
  112. {
  113. buffer[pos] = (char)ofn.lpstrFile[pos];
  114. pos++;
  115. }
  116. buffer[pos] = 0;
  117. }
  118. #else
  119. // For every other machine type use zenity
  120. FILE * output = popen("/usr/bin/zenity --file-selection --save","r");
  121. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL ){
  122. }
  123. if (strlen(buffer) > 0)
  124. buffer[strlen(buffer)-1] = 0;
  125. #endif
  126. }