fs_mem.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: fs_mem.h
  3. // Purpose: interface of wxMemoryFSHandler
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxMemoryFSHandler
  9. This wxFileSystem handler can store arbitrary data in memory stream and make
  10. them accessible via an URL.
  11. It is particularly suitable for storing bitmaps from resources or included XPM
  12. files so that they can be used with wxHTML or wxWebView.
  13. Filenames are prefixed with @c "memory:", e.g. @c "memory:myfile.html".
  14. Example:
  15. @code
  16. #ifndef __WXMSW__
  17. #include "logo.xpm"
  18. #endif
  19. void MyFrame::OnAbout(wxCommandEvent&)
  20. {
  21. wxFileSystem::AddHandler(new wxMemoryFSHandler);
  22. wxMemoryFSHandler::AddFile("logo.png", wxBITMAP(logo), wxBITMAP_TYPE_PNG);
  23. wxMemoryFSHandler::AddFile("about.htm",
  24. "<html><body>About: "
  25. "<img src=\"memory:logo.png\"></body></html>");
  26. wxDialog dlg(this, -1, wxString(_("About")));
  27. wxBoxSizer *topsizer;
  28. topsizer = new wxBoxSizer(wxVERTICAL);
  29. #ifdef USE_WEBVIEW
  30. wxWebView* browser = wxWebView::New(&dlg, wxID_ANY, wxWebViewDefaultURLStr,
  31. wxDefaultPosition, wxSize(380, 160));
  32. browser->RegisterHandler(wxSharedPtr<wxWebViewHandler>(new wxWebViewFSHandler("memory")));
  33. browser->LoadURL("memory:about.htm");
  34. #else // Use wxHtml
  35. wxHtmlWindow *browser;
  36. browser = new wxHtmlWindow(&dlg, -1, wxDefaultPosition,
  37. wxSize(380, 160), wxHW_SCROLLBAR_NEVER);
  38. browser->SetBorders(0);
  39. browser->LoadPage("memory:about.htm");
  40. browser->SetSize(browser->GetInternalRepresentation()->GetWidth(),
  41. browser->GetInternalRepresentation()->GetHeight());
  42. #endif
  43. topsizer->Add(browser, 1, wxALL, 10);
  44. topsizer->Add(new wxStaticLine(&dlg, -1), 0, wxEXPAND | wxLEFT | wxRIGHT, 10);
  45. topsizer->Add(new wxButton(&dlg, wxID_OK, "Ok"),
  46. 0, wxALL | wxALIGN_RIGHT, 15);
  47. dlg.SetAutoLayout(true);
  48. dlg.SetSizer(topsizer);
  49. topsizer->Fit(&dlg);
  50. dlg.Centre();
  51. dlg.ShowModal();
  52. wxMemoryFSHandler::RemoveFile("logo.png");
  53. wxMemoryFSHandler::RemoveFile("about.htm");
  54. }
  55. @endcode
  56. @library{wxbase}
  57. @category{vfs}
  58. @see wxMemoryFSHandler::AddFileWithMimeType
  59. */
  60. class wxMemoryFSHandler : public wxFileSystemHandler
  61. {
  62. public:
  63. /**
  64. Constructor.
  65. */
  66. wxMemoryFSHandler();
  67. //@{
  68. /**
  69. Adds a file to the list of the files stored in memory.
  70. Stored data (bitmap, text or raw data) will be copied into private memory
  71. stream and available under name @c "memory:" + @e filename.
  72. @note you must use a @a type value (aka image format) that wxWidgets
  73. can save (e.g. JPG, PNG, see wxImage documentation)!
  74. @see AddFileWithMimeType()
  75. */
  76. static void AddFile(const wxString& filename, wxImage& image, wxBitmapType type);
  77. static void AddFile(const wxString& filename, const wxBitmap& bitmap, wxBitmapType type);
  78. //@}
  79. //@{
  80. /**
  81. Like AddFile(), but lets you explicitly specify added file's MIME type.
  82. This version should be used whenever you know the MIME type, because it
  83. makes accessing the files faster.
  84. @since 2.8.5
  85. @see AddFile()
  86. */
  87. static void AddFileWithMimeType(const wxString& filename,
  88. const wxString& textdata,
  89. const wxString& mimetype);
  90. static void AddFileWithMimeType(const wxString& filename,
  91. const void* binarydata,
  92. size_t size,
  93. const wxString& mimetype);
  94. //@}
  95. /**
  96. Removes a file from memory FS and frees the occupied memory.
  97. */
  98. static void RemoveFile(const wxString& filename);
  99. };