virtual.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: virtual.cpp
  3. // Purpose: wxHtml sample: demonstrates virtual file systems feature
  4. // Author: ?
  5. // Modified by:
  6. // Created: ?
  7. // Copyright: (c) wxWidgets team
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // For compilers that support precompilation, includes "wx/wx.h".
  11. #include "wx/wxprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. // for all others, include the necessary headers (this file is usually all you
  16. // need because it includes almost all "standard" wxWidgets headers
  17. #ifndef WX_PRECOMP
  18. #include "wx/wx.h"
  19. #endif
  20. #include "wx/html/htmlwin.h"
  21. #ifndef wxHAS_IMAGES_IN_RESOURCES
  22. #include "../../sample.xpm"
  23. #endif
  24. // new handler class:
  25. #include "wx/wfstream.h"
  26. #include "wx/mstream.h"
  27. // ----------------------------------------------------------------------------
  28. // MyVFS
  29. // ----------------------------------------------------------------------------
  30. class MyVFS : public wxFileSystemHandler
  31. {
  32. public:
  33. MyVFS() : wxFileSystemHandler() {}
  34. wxFSFile* OpenFile(wxFileSystem& fs, const wxString& location);
  35. bool CanOpen(const wxString& location);
  36. };
  37. bool MyVFS::CanOpen(const wxString& location)
  38. {
  39. return (GetProtocol(location) == wxT("myVFS"));
  40. }
  41. wxFSFile* MyVFS::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location)
  42. {
  43. wxFSFile *f;
  44. wxInputStream *str;
  45. static char buf[1024];
  46. const wxWX2MBbuf loc = location.ToAscii();
  47. sprintf(buf, "<html><body><h2><i>You're in Node <u>%s</u></i></h2><p>"
  48. "Where do you want to go?<br><blockquote>"
  49. "<a href=\"%s-1\">sub-1</a><br>"
  50. "<a href=\"%s-2\">sub-2</a><br>"
  51. "<a href=\"%s-3\">sub-3</a><br>"
  52. "</blockquote></body></html>",
  53. (const char*)loc, (const char*)loc, (const char*)loc,
  54. (const char*)loc);
  55. // NB: There's a terrible hack involved: we fill 'buf' with new data every
  56. // time this method is called and return new wxMemoryInputStream pointing to it.
  57. // This won't work as soon as there are 2+ myVFS files opened. Fortunately,
  58. // this won't happen because wxHTML keeps only one "page" file opened at the
  59. // time.
  60. str = new wxMemoryInputStream(buf, strlen(buf));
  61. f = new wxFSFile(str, location, wxT("text/html"), wxEmptyString, wxDateTime::Today());
  62. return f;
  63. }
  64. // ----------------------------------------------------------------------------
  65. // private classes
  66. // ----------------------------------------------------------------------------
  67. // Define a new application type, each program should derive a class from wxApp
  68. class MyApp : public wxApp
  69. {
  70. public:
  71. // override base class virtuals
  72. // ----------------------------
  73. // this one is called on application startup and is a good place for the app
  74. // initialization (doing it here and not in the ctor allows to have an error
  75. // return: if OnInit() returns false, the application terminates)
  76. virtual bool OnInit();
  77. };
  78. // Define a new frame type: this is going to be our main frame
  79. class MyFrame : public wxFrame
  80. {
  81. public:
  82. // ctor(s)
  83. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  84. // event handlers (these functions should _not_ be virtual)
  85. void OnQuit(wxCommandEvent& event);
  86. void OnBack(wxCommandEvent& event);
  87. void OnForward(wxCommandEvent& event);
  88. private:
  89. // any class wishing to process wxWidgets events must use this macro
  90. wxDECLARE_EVENT_TABLE();
  91. };
  92. // ----------------------------------------------------------------------------
  93. // constants
  94. // ----------------------------------------------------------------------------
  95. // IDs for the controls and the menu commands
  96. enum
  97. {
  98. // menu items
  99. Minimal_Quit = 1,
  100. Minimal_Back,
  101. Minimal_Forward,
  102. // controls start here (the numbers are, of course, arbitrary)
  103. Minimal_Text = 1000
  104. };
  105. // ----------------------------------------------------------------------------
  106. // event tables and other macros for wxWidgets
  107. // ----------------------------------------------------------------------------
  108. // the event tables connect the wxWidgets events with the functions (event
  109. // handlers) which process them. It can be also done at run-time, but for the
  110. // simple menu events like this the static method is much simpler.
  111. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  112. EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
  113. EVT_MENU(Minimal_Back, MyFrame::OnBack)
  114. EVT_MENU(Minimal_Forward, MyFrame::OnForward)
  115. wxEND_EVENT_TABLE()
  116. // Create a new application object: this macro will allow wxWidgets to create
  117. // the application object during program execution (it's better than using a
  118. // static object for many reasons) and also declares the accessor function
  119. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  120. // not wxApp)
  121. IMPLEMENT_APP(MyApp)
  122. // ============================================================================
  123. // implementation
  124. // ============================================================================
  125. // ----------------------------------------------------------------------------
  126. // the application class
  127. // ----------------------------------------------------------------------------
  128. // `Main program' equivalent: the program execution "starts" here
  129. bool MyApp::OnInit()
  130. {
  131. if ( !wxApp::OnInit() )
  132. return false;
  133. // Create the main application window
  134. MyFrame *frame = new MyFrame(_("wxHtmlWindow testing application"),
  135. wxDefaultPosition, wxSize(640, 480));
  136. // Show it
  137. frame->Show(true);
  138. wxFileSystem::AddHandler(new MyVFS);
  139. // success: wxApp::OnRun() will be called which will enter the main message
  140. // loop and the application will run. If we returned false here, the
  141. // application would exit immediately.
  142. return true;
  143. }
  144. // ----------------------------------------------------------------------------
  145. // main frame
  146. // ----------------------------------------------------------------------------
  147. wxHtmlWindow *html;
  148. // frame constructor
  149. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  150. : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size)
  151. {
  152. SetIcon(wxICON(sample));
  153. // create a menu bar
  154. wxMenu *menuFile = new wxMenu;
  155. wxMenu *menuNav = new wxMenu;
  156. menuFile->Append(Minimal_Quit, _("E&xit"));
  157. menuNav->Append(Minimal_Back, _("Go &BACK"));
  158. menuNav->Append(Minimal_Forward, _("Go &FORWARD"));
  159. // now append the freshly created menu to the menu bar...
  160. wxMenuBar *menuBar = new wxMenuBar;
  161. menuBar->Append(menuFile, _("&File"));
  162. menuBar->Append(menuNav, _("&Navigate"));
  163. // ... and attach this menu bar to the frame
  164. SetMenuBar(menuBar);
  165. #if wxUSE_STATUSBAR
  166. CreateStatusBar(2);
  167. #endif // wxUSE_STATUSBAR
  168. html = new wxHtmlWindow(this);
  169. html -> SetRelatedFrame(this, _("VFS Demo: '%s'"));
  170. #if wxUSE_STATUSBAR
  171. html -> SetRelatedStatusBar(1);
  172. #endif // wxUSE_STATUSBAR
  173. html -> LoadPage(wxT("start.htm"));
  174. }
  175. // event handlers
  176. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  177. {
  178. // true is to force the frame to close
  179. Close(true);
  180. }
  181. void MyFrame::OnBack(wxCommandEvent& WXUNUSED(event))
  182. {
  183. if (!html -> HistoryBack()) wxMessageBox(_("You reached prehistory era!"));
  184. }
  185. void MyFrame::OnForward(wxCommandEvent& WXUNUSED(event))
  186. {
  187. if (!html -> HistoryForward()) wxMessageBox(_("No more items in history!"));
  188. }