| 1 |
/* |
| 2 |
Copyright (C) 2009-2010 wxLauncher Team |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "apis/HelpManager.h" |
| 20 |
#include "generated/configure_launcher.h" |
| 21 |
#include <wx/filename.h> |
| 22 |
#include <wx/html/helpctrl.h> |
| 23 |
#include "global/ids.h" |
| 24 |
|
| 25 |
#include "global/MemoryDebugging.h" |
| 26 |
|
| 27 |
using namespace HelpManager; |
| 28 |
|
| 29 |
namespace HelpManager { |
| 30 |
struct helpLink { |
| 31 |
WindowIDS id; |
| 32 |
wxString location; |
| 33 |
}; |
| 34 |
helpLink helpLinks[] = { |
| 35 |
#include "generated/helplinks.cpp" |
| 36 |
}; |
| 37 |
bool initialized = false; |
| 38 |
wxHtmlHelpController *controller = NULL; |
| 39 |
size_t numberOfHelpLinks = sizeof(HelpManager::helpLinks)/sizeof(HelpManager::helpLink); |
| 40 |
class ExternLinkHandler: public wxEvtHandler { |
| 41 |
public: |
| 42 |
void LinkClicked(wxHtmlLinkEvent &event); |
| 43 |
}; |
| 44 |
ExternLinkHandler* externLinkHandler; |
| 45 |
class HtmlHelpController: public wxHtmlHelpController { |
| 46 |
public: |
| 47 |
virtual wxHtmlHelpFrame* CreateHelpFrame(wxHtmlHelpData *data); |
| 48 |
}; |
| 49 |
}; |
| 50 |
|
| 51 |
bool HelpManager::Initialize() { |
| 52 |
if (HelpManager::IsInitialized()) { |
| 53 |
return false; |
| 54 |
} |
| 55 |
|
| 56 |
externLinkHandler = new ExternLinkHandler(); |
| 57 |
controller = new HtmlHelpController(); |
| 58 |
wxFileName file(_T(HELP_HTB_LOCATION)); |
| 59 |
if ( file.FileExists() ) { |
| 60 |
controller->AddBook(file, false); |
| 61 |
} else { |
| 62 |
wxLogWarning(_T("Unable to find help file %s"), file.GetFullName().c_str()); |
| 63 |
delete controller; |
| 64 |
controller = NULL; |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
HelpManager::initialized = true; |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
bool HelpManager::DeInitialize() { |
| 73 |
if ( HelpManager::IsInitialized()) { |
| 74 |
HelpManager::initialized = false; |
| 75 |
delete controller; |
| 76 |
delete externLinkHandler; |
| 77 |
} |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
bool HelpManager::IsInitialized() { |
| 82 |
return HelpManager::initialized; |
| 83 |
} |
| 84 |
|
| 85 |
void HelpManager::OpenHelpById(WindowIDS id) { |
| 86 |
wxCHECK_RET( HelpManager::IsInitialized(), _("Help manager is not initialized")); |
| 87 |
|
| 88 |
// find id if it exists |
| 89 |
for (size_t i = 0; i < HelpManager::numberOfHelpLinks; i++) { |
| 90 |
if (HelpManager::helpLinks[i].id == id) { |
| 91 |
HelpManager::controller->Display( |
| 92 |
HelpManager::helpLinks[i].location); |
| 93 |
return; |
| 94 |
} |
| 95 |
} |
| 96 |
wxLogInfo(_(" ID %d does not have more specific help"), id); |
| 97 |
} |
| 98 |
|
| 99 |
void HelpManager::OpenMainHelpPage() { |
| 100 |
wxCHECK_RET( HelpManager::IsInitialized(), _T("Help is not initialized")); |
| 101 |
// Hacky way to get the help tree to expand |
| 102 |
wxString faq(_T("jfaq.htm")); |
| 103 |
wxString index(_T("index.htm")); |
| 104 |
HelpManager::OpenHelpByString(faq); |
| 105 |
HelpManager::OpenHelpByString(index); |
| 106 |
} |
| 107 |
|
| 108 |
/** Opens the help file passing str to the help controller. |
| 109 |
The help controller tries to find a page in the manual in 4 ways: |
| 110 |
\li as a direct filename of the document in the manual |
| 111 |
\li as a chapter name (based on the page title displayed in the contents) |
| 112 |
\li a word from the index |
| 113 |
\li any word (will open search pane and do a search) |
| 114 |
|
| 115 |
\note Capitalization matters. |
| 116 |
*/ |
| 117 |
void HelpManager::OpenHelpByString(wxString& str) { |
| 118 |
wxCHECK_RET( HelpManager::IsInitialized(), _("Help manager is not initialized")); |
| 119 |
|
| 120 |
HelpManager::controller->Display(str); |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
void HelpManager::ExternLinkHandler::LinkClicked(wxHtmlLinkEvent &event) { |
| 125 |
wxHtmlLinkInfo info(event.GetLinkInfo()); |
| 126 |
wxString href(info.GetHref()); |
| 127 |
if ( href.StartsWith(_T("http://")) ) { |
| 128 |
::wxLaunchDefaultBrowser(href); |
| 129 |
} else { |
| 130 |
event.Skip(); // not external, so I don't want it. |
| 131 |
} |
| 132 |
} |
| 133 |
wxHtmlHelpFrame* HelpManager::HtmlHelpController::CreateHelpFrame(wxHtmlHelpData *data) { |
| 134 |
wxHtmlHelpFrame* frame = new wxHtmlHelpFrame(data); |
| 135 |
frame->SetController(this); |
| 136 |
frame->Create(m_parentWindow, -1, wxEmptyString, m_FrameStyle, m_Config, m_ConfigRoot); |
| 137 |
frame->SetTitleFormat(m_titleFormat); |
| 138 |
frame->GetEventHandler() // hook in to allow veto on the links so we can open external browsers |
| 139 |
->Connect(wxEVT_COMMAND_HTML_LINK_CLICKED, wxHtmlLinkEventHandler(ExternLinkHandler::LinkClicked), NULL, HelpManager::externLinkHandler); |
| 140 |
m_helpFrame = frame; |
| 141 |
return frame; |
| 142 |
} |