file_dialog_save.cpp 2.5 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_save.h"
  9. #include <cstdio>
  10. #ifdef _WIN32
  11. #include <Commdlg.h>
  12. #endif
  13. IGL_INLINE std::string igl::file_dialog_save()
  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. // There is currently a bug in Applescript that strips extensions off
  20. // of chosen existing files in the "choose file name" dialog
  21. // I'm assuming that will be fixed soon
  22. FILE * output = popen(
  23. "osascript -e \""
  24. " tell application \\\"System Events\\\"\n"
  25. " activate\n"
  26. " set existing_file to choose file name\n"
  27. " end tell\n"
  28. " set existing_file_path to (POSIX path of (existing_file))\n"
  29. "\" 2>/dev/null | tr -d '\n' ","r");
  30. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
  31. {
  32. }
  33. #elif _WIN32
  34. // Use native windows file dialog box
  35. // (code contributed by Tino Weinkauf)
  36. OPENFILENAME ofn; // common dialog box structure
  37. char szFile[260]; // buffer for file name
  38. HWND hwnd; // owner window
  39. HANDLE hf; // file handle
  40. // Initialize OPENFILENAME
  41. ZeroMemory(&ofn, sizeof(ofn));
  42. ofn.lStructSize = sizeof(ofn);
  43. ofn.hwndOwner = NULL;//hwnd;
  44. ofn.lpstrFile = new wchar_t[100];
  45. // Set lpstrFile[0] to '\0' so that GetOpenFileName does not
  46. // use the contents of szFile to initialize itself.
  47. ofn.lpstrFile[0] = '\0';
  48. ofn.nMaxFile = sizeof(szFile);
  49. ofn.lpstrFilter = L"";
  50. ofn.nFilterIndex = 1;
  51. ofn.lpstrFileTitle = NULL;
  52. ofn.nMaxFileTitle = 0;
  53. ofn.lpstrInitialDir = NULL;
  54. ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
  55. // Display the Open dialog box.
  56. int pos = 0;
  57. if (GetSaveFileName(&ofn)==TRUE)
  58. {
  59. while(ofn.lpstrFile[pos] != '\0')
  60. {
  61. buffer[pos] = (char)ofn.lpstrFile[pos];
  62. pos++;
  63. }
  64. buffer[pos] = 0;
  65. }
  66. #else
  67. // For every other machine type use zenity
  68. FILE * output = popen("/usr/bin/zenity --file-selection --save","r");
  69. while ( fgets(buffer, FILE_DIALOG_MAX_BUFFER, output) != NULL )
  70. {
  71. }
  72. if (strlen(buffer) > 0)
  73. {
  74. buffer[strlen(buffer)-1] = 0;
  75. }
  76. #endif
  77. return std::string(buffer);
  78. }