From: <pst...@us...> - 2013-03-20 15:36:13
|
Revision: 985 http://sourceforge.net/p/jazzplusplus/code/985 Author: pstieber Date: 2013-03-20 15:36:05 +0000 (Wed, 20 Mar 2013) Log Message: ----------- 1. Made the help class a Singleton for global access instead of using a global instance. 2. Moved functions for finding, registering, and using the help file from the application class to this class. Modified Paths: -------------- trunk/jazz/src/Help.cpp trunk/jazz/src/Help.h Modified: trunk/jazz/src/Help.cpp =================================================================== --- trunk/jazz/src/Help.cpp 2013-03-20 15:33:33 UTC (rev 984) +++ trunk/jazz/src/Help.cpp 2013-03-20 15:36:05 UTC (rev 985) @@ -22,9 +22,12 @@ #include "Help.h" +#include <wx/filedlg.h> #include <wx/html/helpctrl.h> +#include <wx/msgdlg.h> +#include <wx/stdpaths.h> -#include <iostream> +#include <fstream> using namespace std; @@ -34,34 +37,187 @@ //***************************************************************************** //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -JZHelp::JZHelp(const char* pHelpFleName) - : mpHelp(0), - mHelpFile(pHelpFleName) +wxString JZHelp::mHelpFileName = "jazz.hhp"; + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +JZHelp::JZHelp() + : mpHelp(0) { - mpHelp = new wxHtmlHelpController(); - mpHelp->Initialize(mHelpFile); - cout << "JZHelp::JZHelp " << mHelpFile << endl; + mpHelp = new wxHtmlHelpController(wxHF_DEFAULT_STYLE | wxHF_OPEN_FILES); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- JZHelp::~JZHelp() { + CloseHelp(); delete mpHelp; } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -void JZHelp::ShowTopic(const char* pTopic) +void JZHelp::ShowTopic(const wxString& TopicString) { - mpHelp->LoadFile(mHelpFile.c_str()); - mpHelp->KeywordSearch(pTopic); + mpHelp->LoadFile(mHelpFileName); + mpHelp->KeywordSearch(TopicString); } //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- -void JZHelp::DisplayContents() +void JZHelp::DisplayHelpContents() { - mpHelp->LoadFile(mHelpFile.c_str()); + mpHelp->LoadFile(mHelpFileName); mpHelp->DisplayContents(); } + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZHelp::CloseHelp() +{ + // GetFrame returns NULL if there is no help frame active. + if (mpHelp->GetFrame()) + { + // Close the help frame; this will cause the config data to get written. + mpHelp->GetFrame()->Close(true); + } +} + +//----------------------------------------------------------------------------- +//----------------------------------------------------------------------------- +void JZHelp::ConfigureHelp() +{ + wxConfigBase* pConfig = wxConfigBase::Get(); + + // Let the help system store the Jazz++ help configuration info. + mpHelp->UseConfig(pConfig); + + // This code should be distributed with a HelpFiles subdirectory under + // the directory the executable is stored in on Windows and under the + // ${prefix}/shared/${appname} on Linux. + wxString HelpFileDirectoryGuess = + wxStandardPaths::Get().GetDataDir() + + wxFileName::GetPathSeparator() + + "HelpFiles" + + wxFileName::GetPathSeparator(); + + // Attempt to obtain the path to the help file from configuration data. + wxString HelpFilePath; + bool WasHelpPathRead = false; + if (pConfig) + { + WasHelpPathRead = pConfig->Read( + "/Paths/Help", + &HelpFilePath, + HelpFileDirectoryGuess); + } + + // Construct a full file name. + wxString HelpFileNameAndPath = HelpFilePath + mHelpFileName; + + // Test for the existence of the help file. + bool HelpFileFound = false; + ifstream Is; + Is.open(HelpFileNameAndPath.mb_str()); + if (!Is) + { + // Ask the user to find the help file. + if (FindAndRegisterHelpFilePath(HelpFilePath)) + { + HelpFileNameAndPath = HelpFilePath + mHelpFileName; + + // Try one more time. + Is.close(); + Is.clear(); + Is.open(HelpFileNameAndPath.mb_str()); + if (!Is) + { + wxString Message = "Failed to add the Jazz++ book " + mHelpFileName; + ::wxMessageBox(Message); + } + else + { + HelpFileFound = true; + } + } + } + else + { + HelpFileFound = true; + } + + // GetUserDataDir returns the directory for the user-dependent application + // data files. The value is $HOME/.appname on Linux, + // c:\Documents and Settings\username\Application Data\appname on + // Windows, and ~/Library/Application Support/appname on the Mac. + // The cached version of the help file will be placed in this location. + mpHelp->SetTempDir(wxStandardPaths::Get().GetUserDataDir()); + + if (HelpFileFound) + { + // Add the Jazz++ help file the the help system. + mpHelp->AddBook(HelpFileNameAndPath); + + if (!WasHelpPathRead && pConfig) + { + // Register the help path. + pConfig->Write("/Paths/Help", HelpFilePath); + } + } +} + +//----------------------------------------------------------------------------- +// Description: +// This function walks the user through a top-level help file search. If +// the help file is found, create a configuration entry so the code can find +// the help file path the next time the application starts. +// +// Outputs: +// wxString& HelpFilePath: +// A user-selected path to the help file. The calling code should check +// to insure the help file is actually in this path. +//----------------------------------------------------------------------------- +bool JZHelp::FindAndRegisterHelpFilePath(wxString& HelpFilePath) const +{ + wxString Message; + Message = + "Unable to find " + mHelpFileName + "\n" + + "Would you like to locate this file?"; + int Response = ::wxMessageBox( + Message, + "Cannnot Find Help File", + wxOK | wxCANCEL); + + if (Response == wxOK) + { + // Use an open dialog to find the help file. + wxFileDialog OpenDialog( + 0, + "Open the Help File", + HelpFilePath, + mHelpFileName, + "*.hhp", + wxFD_OPEN); + + if (OpenDialog.ShowModal() == wxID_OK) + { + // Generate a string that contains a path to the help file. + wxString TempHelpFilePath; + TempHelpFilePath = ::wxPathOnly(OpenDialog.GetPath()); + TempHelpFilePath += ::wxFileName::GetPathSeparator(); + + wxConfigBase* pConfig = wxConfigBase::Get(); + if (pConfig) + { + pConfig->Write("/Paths/Help", TempHelpFilePath); + } + + // Return the user selected help file path. + HelpFilePath = TempHelpFilePath; + + return true; + } + } + + return false; +} Modified: trunk/jazz/src/Help.h =================================================================== --- trunk/jazz/src/Help.h 2013-03-20 15:33:33 UTC (rev 984) +++ trunk/jazz/src/Help.h 2013-03-20 15:36:05 UTC (rev 985) @@ -35,17 +35,36 @@ { public: - JZHelp(const char* pHelpFleName); + static JZHelp& Instance(); - ~JZHelp(); + void ConfigureHelp(); - void ShowTopic(const char* pTopic); + void ShowTopic(const wxString& TopicString); - void DisplayContents(); + void DisplayHelpContents(); + void CloseHelp(); + private: + JZHelp(); + + ~JZHelp(); + + bool FindAndRegisterHelpFilePath(wxString& HelpFilePath) const; + + private: + + static wxString mHelpFileName; + wxHtmlHelpController* mpHelp; +}; - wxString mHelpFile; -}; +//***************************************************************************** +//***************************************************************************** +inline +JZHelp& JZHelp::Instance() +{ + static JZHelp JazzHelp; + return JazzHelp; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |