help.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: help.cpp
  3. // Purpose: wxHtml sample: help test
  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/image.h"
  21. #include "wx/html/helpfrm.h"
  22. #include "wx/html/helpctrl.h"
  23. #include "wx/filesys.h"
  24. #include "wx/fs_zip.h"
  25. #ifndef wxHAS_IMAGES_IN_RESOURCES
  26. #include "../../sample.xpm"
  27. #endif
  28. // ----------------------------------------------------------------------------
  29. // private classes
  30. // ----------------------------------------------------------------------------
  31. // Define a new application type, each program should derive a class from wxApp
  32. class MyApp : public wxApp
  33. {
  34. public:
  35. // override base class virtuals
  36. // ----------------------------
  37. // this one is called on application startup and is a good place for the app
  38. // initialization (doing it here and not in the ctor allows to have an error
  39. // return: if OnInit() returns false, the application terminates)
  40. virtual bool OnInit();
  41. };
  42. // Define a new frame type: this is going to be our main frame
  43. class MyFrame : public wxFrame
  44. {
  45. public:
  46. // ctor(s)
  47. MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
  48. // event handlers (these functions should _not_ be virtual)
  49. void OnQuit(wxCommandEvent& event);
  50. void OnHelp(wxCommandEvent& event);
  51. void OnClose(wxCloseEvent& event);
  52. private:
  53. wxHtmlHelpController help;
  54. // any class wishing to process wxWidgets events must use this macro
  55. wxDECLARE_EVENT_TABLE();
  56. };
  57. // ----------------------------------------------------------------------------
  58. // constants
  59. // ----------------------------------------------------------------------------
  60. // IDs for the controls and the menu commands
  61. enum
  62. {
  63. // menu items
  64. Minimal_Quit = 1,
  65. Minimal_Help
  66. };
  67. // ----------------------------------------------------------------------------
  68. // event tables and other macros for wxWidgets
  69. // ----------------------------------------------------------------------------
  70. // the event tables connect the wxWidgets events with the functions (event
  71. // handlers) which process them. It can be also done at run-time, but for the
  72. // simple menu events like this the static method is much simpler.
  73. wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
  74. EVT_MENU(Minimal_Quit, MyFrame::OnQuit)
  75. EVT_MENU(Minimal_Help, MyFrame::OnHelp)
  76. EVT_CLOSE(MyFrame::OnClose)
  77. wxEND_EVENT_TABLE()
  78. // Create a new application object: this macro will allow wxWidgets to create
  79. // the application object during program execution (it's better than using a
  80. // static object for many reasons) and also declares the accessor function
  81. // wxGetApp() which will return the reference of the right type (i.e. MyApp and
  82. // not wxApp)
  83. IMPLEMENT_APP(MyApp)
  84. // ============================================================================
  85. // implementation
  86. // ============================================================================
  87. // ----------------------------------------------------------------------------
  88. // the application class
  89. // ----------------------------------------------------------------------------
  90. // `Main program' equivalent: the program execution "starts" here
  91. bool MyApp::OnInit()
  92. {
  93. if ( !wxApp::OnInit() )
  94. return false;
  95. wxInitAllImageHandlers();
  96. #if wxUSE_STREAMS && wxUSE_ZIPSTREAM && wxUSE_ZLIB
  97. wxFileSystem::AddHandler(new wxZipFSHandler);
  98. #endif
  99. SetVendorName(wxT("wxWidgets"));
  100. SetAppName(wxT("wxHTMLHelp"));
  101. // Create the main application window
  102. MyFrame *frame = new MyFrame(_("HTML Help Sample"),
  103. wxDefaultPosition, wxDefaultSize);
  104. // Show it
  105. frame->Show(true);
  106. // success: wxApp::OnRun() will be called which will enter the main message
  107. // loop and the application will run. If we returned false here, the
  108. // application would exit immediately.
  109. return true;
  110. }
  111. // ----------------------------------------------------------------------------
  112. // main frame
  113. // ----------------------------------------------------------------------------
  114. // frame constructor
  115. MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
  116. : wxFrame((wxFrame *)NULL, wxID_ANY, title, pos, size),
  117. help(wxHF_DEFAULT_STYLE | wxHF_OPEN_FILES)
  118. {
  119. SetIcon(wxICON(sample));
  120. // create a menu bar
  121. wxMenu *menuFile = new wxMenu;
  122. menuFile->Append(Minimal_Help, _("&Help"));
  123. menuFile->Append(Minimal_Quit, _("E&xit"));
  124. // now append the freshly created menu to the menu bar...
  125. wxMenuBar *menuBar = new wxMenuBar;
  126. menuBar->Append(menuFile, _("&File"));
  127. // ... and attach this menu bar to the frame
  128. SetMenuBar(menuBar);
  129. help.UseConfig(wxConfig::Get());
  130. bool ret;
  131. help.SetTempDir(wxT("."));
  132. ret = help.AddBook(wxFileName(wxT("helpfiles/testing.hhp"), wxPATH_UNIX));
  133. if (! ret)
  134. wxMessageBox(wxT("Failed adding book helpfiles/testing.hhp"));
  135. ret = help.AddBook(wxFileName(wxT("helpfiles/another.hhp"), wxPATH_UNIX));
  136. if (! ret)
  137. wxMessageBox(_("Failed adding book helpfiles/another.hhp"));
  138. }
  139. // event handlers
  140. void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
  141. {
  142. // true is to force the frame to close
  143. Close(true);
  144. }
  145. void MyFrame::OnHelp(wxCommandEvent& WXUNUSED(event))
  146. {
  147. help.Display(wxT("Test HELPFILE"));
  148. }
  149. void MyFrame::OnClose(wxCloseEvent& event)
  150. {
  151. // Close the help frame; this will cause the config data to
  152. // get written.
  153. if ( help.GetFrame() ) // returns NULL if no help frame active
  154. help.GetFrame()->Close(true);
  155. // now we can safely delete the config pointer
  156. event.Skip();
  157. delete wxConfig::Set(NULL);
  158. }