Advertisement
Zgragselus

FileDialog.cpp

Dec 20th, 2023
832
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // FileDialog.cpp
  4. //
  5. ///////////////////////////////////////////////////////////////////////////////////////////////////
  6.  
  7. ///////////////////////////////////////////////////////////////////////////////////////////////////
  8. // Header section
  9.  
  10. #include "FileDialog.h"
  11.  
  12. ///////////////////////////////////////////////////////////////////////////////////////////////////
  13. // Declaration section
  14.  
  15. using namespace Engine;
  16.  
  17. ///////////////////////////////////////////////////////////////////////////////////////////////////
  18. // Definition section
  19.  
  20. /// <summary>
  21. /// Open and show file dialog. Note that "OFN_NOCHANGEDIR" is used to reset current working
  22. /// directory back to the executable file.
  23. /// </summary>
  24. /// <param name="log">Log for storing errors</param>
  25. /// <param name="title">Title for dialog</param>
  26. /// <param name="filter">Filter for specific file formats (Windows format - e.g. Image\0*.bmp\0All Files\0*.*\0</param>
  27. /// <param name="type">Dialog type (Open, Save, ...)</param>
  28. /// <param name="filename">Resulting filename</param>
  29. /// <return>True if file selected, false otherwise</return>
  30. bool FileDialog::Show(Log* log, const std::string& title, const char* filter, Type type, std::string& filename)
  31. {
  32.     char result[MAX_PATH] = { 0 };
  33.  
  34.     OPENFILENAME ofn = { 0 };
  35.     ofn.lStructSize = sizeof(ofn);
  36.     ofn.hwndOwner = nullptr;
  37.     ofn.lpstrFilter = filter;
  38.     ofn.lpstrFile = result;
  39.     ofn.nMaxFile = MAX_PATH;
  40.     ofn.lpstrTitle = title.c_str();
  41.     ofn.Flags = OFN_DONTADDTORECENT | OFN_NOCHANGEDIR | (type == FileDialog::Type::OPEN_FILE_DIALOG ? OFN_FILEMUSTEXIST : 0);
  42.  
  43.     if (type == FileDialog::Type::SAVE_FILE_DIALOG ? GetSaveFileName(&ofn) : GetOpenFileName(&ofn))
  44.     {
  45.         filename = result;
  46.         return true;
  47.     }
  48.     else
  49.     {
  50.         switch (CommDlgExtendedError())
  51.         {
  52.         case CDERR_DIALOGFAILURE:   log->Print("Engine::FileDialog", "The dialog box could not be created."); break;
  53.         case CDERR_FINDRESFAILURE:  log->Print("Engine::FileDialog", "The common dialog box function failed to find a specified resource."); break;
  54.         case CDERR_INITIALIZATION:  log->Print("Engine::FileDialog", "The common dialog box function failed during initialization. This error often occurs when sufficient memory is not available."); break;
  55.         case CDERR_LOADRESFAILURE:  log->Print("Engine::FileDialog", "The common dialog box function failed to load a specified resource."); break;
  56.         case CDERR_LOADSTRFAILURE:  log->Print("Engine::FileDialog", "The common dialog box function failed to load a specified string."); break;
  57.         case CDERR_LOCKRESFAILURE:  log->Print("Engine::FileDialog", "The common dialog box function failed to lock a specified resource."); break;
  58.         case CDERR_MEMALLOCFAILURE: log->Print("Engine::FileDialog", "The common dialog box function was unable to allocate memory for internal structures."); break;
  59.         case CDERR_MEMLOCKFAILURE:  log->Print("Engine::FileDialog", "The common dialog box function was unable to lock the memory associated with a handle."); break;
  60.         case CDERR_NOHINSTANCE:     log->Print("Engine::FileDialog", "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding instance handle."); break;
  61.         case CDERR_NOHOOK:          log->Print("Engine::FileDialog", "The ENABLEHOOK flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a pointer to a corresponding hook procedure."); break;
  62.         case CDERR_NOTEMPLATE:      log->Print("Engine::FileDialog", "The ENABLETEMPLATE flag was set in the Flags member of the initialization structure for the corresponding common dialog box, but you failed to provide a corresponding template."); break;
  63.         case CDERR_STRUCTSIZE:      log->Print("Engine::FileDialog", "The RegisterWindowMessage function returned an error code when it was called by the common dialog box function."); break;
  64.         case FNERR_BUFFERTOOSMALL:  log->Print("Engine::FileDialog", "The lStructSize member of the initialization structure for the corresponding common dialog box is invalid."); break;
  65.         case FNERR_INVALIDFILENAME: log->Print("Engine::FileDialog", "A file name is invalid."); break;
  66.         case FNERR_SUBCLASSFAILURE: log->Print("Engine::FileDialog", "An attempt to subclass a list box failed because sufficient memory was not available."); break;
  67.         default:                    break;
  68.         }
  69.  
  70.         return false;
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement