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