| 1 |
#include "HelpPluginMessageDlg.h" |
| 2 |
#include "HelpPluginSettings.h" |
| 3 |
#include "HelpPluginSettingsDlg.h" |
| 4 |
#include "clKeyboardManager.h" |
| 5 |
#include "codelite_events.h" |
| 6 |
#include "event_notifier.h" |
| 7 |
#include "file_logger.h" |
| 8 |
#include "fileextmanager.h" |
| 9 |
#include "fileutils.h" |
| 10 |
#include "helpplugin.h" |
| 11 |
#include <wx/msgdlg.h> |
| 12 |
#include <wx/stc/stc.h> |
| 13 |
#include <wx/uri.h> |
| 14 |
#include <wx/xrc/xmlres.h> |
| 15 |
|
| 16 |
static HelpPlugin* thePlugin = NULL; |
| 17 |
|
| 18 |
// Define the plugin entry point |
| 19 |
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager) |
| 20 |
{ |
| 21 |
if(thePlugin == 0) { thePlugin = new HelpPlugin(manager); } |
| 22 |
return thePlugin; |
| 23 |
} |
| 24 |
|
| 25 |
CL_PLUGIN_API PluginInfo* GetPluginInfo() |
| 26 |
{ |
| 27 |
static PluginInfo info; |
| 28 |
info.SetAuthor(wxT("Eran Ifrah")); |
| 29 |
info.SetName(wxT("HelpPlugin")); |
| 30 |
info.SetDescription(_("Provide help based on selected words")); |
| 31 |
info.SetVersion(wxT("v1.0")); |
| 32 |
return &info; |
| 33 |
} |
| 34 |
|
| 35 |
CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; } |
| 36 |
|
| 37 |
HelpPlugin::HelpPlugin(IManager* manager) |
| 38 |
: IPlugin(manager) |
| 39 |
{ |
| 40 |
m_longName = _("Provide help based on selected words"); |
| 41 |
m_shortName = _("HelpPlugin"); |
| 42 |
wxTheApp->Bind(wxEVT_MENU, &HelpPlugin::OnHelp, this, XRCID("ID_ZEAL_HELP")); |
| 43 |
Bind(wxEVT_MENU, &HelpPlugin::OnHelpSettings, this, XRCID("ID_ZEAL_SETTINGS")); |
| 44 |
EventNotifier::Get()->Bind(wxEVT_CONTEXT_MENU_EDITOR, &HelpPlugin::OnEditorContextMenu, this); |
| 45 |
} |
| 46 |
|
| 47 |
HelpPlugin::~HelpPlugin() {} |
| 48 |
|
| 49 |
void HelpPlugin::CreateToolBar(clToolBarGeneric* toolbar) { wxUnusedVar(toolbar); } |
| 50 |
|
| 51 |
void HelpPlugin::CreatePluginMenu(wxMenu* pluginsMenu) |
| 52 |
{ |
| 53 |
wxMenu* menu = new wxMenu; |
| 54 |
menu->Append(XRCID("ID_ZEAL_SETTINGS"), _("Settings...")); |
| 55 |
pluginsMenu->Append(wxID_ANY, _("Help Plugin"), menu); |
| 56 |
menu->SetNextHandler(this); |
| 57 |
this->SetPreviousHandler(menu); |
| 58 |
} |
| 59 |
|
| 60 |
void HelpPlugin::UnPlug() |
| 61 |
{ |
| 62 |
wxTheApp->Unbind(wxEVT_MENU, &HelpPlugin::OnHelp, this, XRCID("ID_ZEAL_HELP")); |
| 63 |
Unbind(wxEVT_MENU, &HelpPlugin::OnHelpSettings, this, XRCID("ID_ZEAL_SETTINGS")); |
| 64 |
EventNotifier::Get()->Unbind(wxEVT_CONTEXT_MENU_EDITOR, &HelpPlugin::OnEditorContextMenu, this); |
| 65 |
} |
| 66 |
|
| 67 |
void HelpPlugin::OnEditorContextMenu(clContextMenuEvent& event) |
| 68 |
{ |
| 69 |
event.Skip(); |
| 70 |
IEditor* editor = m_mgr->GetActiveEditor(); |
| 71 |
CHECK_PTR_RET(editor); |
| 72 |
|
| 73 |
if(!editor->GetCtrl()->HasSelection()) return; |
| 74 |
wxString selection = editor->GetCtrl()->GetSelectedText(); |
| 75 |
wxString modSelection = selection.BeforeFirst('\n'); |
| 76 |
modSelection.Trim().Trim(false); |
| 77 |
if(modSelection.IsEmpty()) return; |
| 78 |
|
| 79 |
// Ensure we only use 15 chars of the selected text, otherwise the menu label |
| 80 |
// will overflow |
| 81 |
if(modSelection.length() > 15) { modSelection = modSelection.Mid(0, 15); } |
| 82 |
if(selection.Contains("\n")) { modSelection << "..."; } |
| 83 |
|
| 84 |
// Get the context menu |
| 85 |
wxMenu* menu = event.GetMenu(); |
| 86 |
wxBitmap helpBitmap = wxXmlResource::Get()->LoadBitmap("svn_info"); |
| 87 |
menu->AppendSeparator(); |
| 88 |
wxString label; |
| 89 |
label << _("Search the docs for '") << modSelection << "'"; |
| 90 |
menu->Append(XRCID("ID_ZEAL_HELP"), label)->SetBitmap(helpBitmap); |
| 91 |
menu->AppendSeparator(); |
| 92 |
} |
| 93 |
|
| 94 |
void HelpPlugin::OnHelp(wxCommandEvent& event) |
| 95 |
{ |
| 96 |
wxUnusedVar(event); |
| 97 |
CallAfter(&HelpPlugin::DoHelp); |
| 98 |
} |
| 99 |
|
| 100 |
wxString HelpPlugin::DoBuildQueryString() |
| 101 |
{ |
| 102 |
IEditor* editor = m_mgr->GetActiveEditor(); |
| 103 |
CHECK_PTR_RET_EMPTY_STRING(editor); |
| 104 |
|
| 105 |
// if no selection is available, just launch the help with an empty query |
| 106 |
// so Zeal will come to front |
| 107 |
if(!editor->GetCtrl()->HasSelection()) return "dash-plugin://"; |
| 108 |
|
| 109 |
wxString selection = editor->GetCtrl()->GetSelectedText(); |
| 110 |
|
| 111 |
HelpPluginSettings settings; |
| 112 |
settings.Load(); |
| 113 |
|
| 114 |
// Auto detect the language |
| 115 |
wxString language; |
| 116 |
wxString label; |
| 117 |
FileExtManager::FileType type = FileExtManager::GetType(editor->GetFileName().GetFullName()); |
| 118 |
switch(type) { |
| 119 |
case FileExtManager::TypeCMake: |
| 120 |
language << settings.GetCmakeDocset(); |
| 121 |
break; |
| 122 |
case FileExtManager::TypeHeader: |
| 123 |
case FileExtManager::TypeSourceC: |
| 124 |
case FileExtManager::TypeSourceCpp: |
| 125 |
language << settings.GetCxxDocset(); |
| 126 |
break; |
| 127 |
case FileExtManager::TypeHtml: |
| 128 |
language << settings.GetHtmlDocset(); |
| 129 |
break; |
| 130 |
case FileExtManager::TypeCSS: |
| 131 |
language << settings.GetCssDocset(); |
| 132 |
break; |
| 133 |
case FileExtManager::TypeJS: |
| 134 |
language << settings.GetJsDocset(); |
| 135 |
break; |
| 136 |
case FileExtManager::TypePhp: |
| 137 |
language << settings.GetPhpDocset(); |
| 138 |
break; |
| 139 |
case FileExtManager::TypeJava: |
| 140 |
language << settings.GetJavaDocset(); |
| 141 |
break; |
| 142 |
default: |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
wxString q; |
| 147 |
if(!language.IsEmpty()) { |
| 148 |
// Build context aware search string |
| 149 |
q << "dash-plugin://keys=" << language << "&query=" << selection; |
| 150 |
} else { |
| 151 |
q << "dash-plugin://query=" << selection; |
| 152 |
} |
| 153 |
q = FileUtils::EncodeURI(q); |
| 154 |
return q; |
| 155 |
} |
| 156 |
|
| 157 |
void HelpPlugin::OnHelpSettings(wxCommandEvent& event) |
| 158 |
{ |
| 159 |
wxUnusedVar(event); |
| 160 |
HelpPluginSettingsDlg dlg(EventNotifier::Get()->TopFrame()); |
| 161 |
dlg.ShowModal(); |
| 162 |
} |
| 163 |
|
| 164 |
void HelpPlugin::DoHelp() |
| 165 |
{ |
| 166 |
wxString query = DoBuildQueryString(); |
| 167 |
if(query.IsEmpty()) return; |
| 168 |
#ifdef __WXGTK__ |
| 169 |
wxFileName fnZeal("/usr/bin", "zeal"); |
| 170 |
if(!fnZeal.Exists()) { |
| 171 |
HelpPluginMessageDlg dlg(EventNotifier::Get()->TopFrame()); |
| 172 |
dlg.ShowModal(); |
| 173 |
} |
| 174 |
wxString command; |
| 175 |
command << fnZeal.GetFullPath() << " " |
| 176 |
<< "\"" << query << "\""; |
| 177 |
clDEBUG() << "Help Plugin:" << command << clEndl; |
| 178 |
::wxExecute(command); |
| 179 |
#else |
| 180 |
clDEBUG() << "Help Plugin:" << query << clEndl; |
| 181 |
if(!::wxLaunchDefaultBrowser(query)) { |
| 182 |
HelpPluginMessageDlg dlg(EventNotifier::Get()->TopFrame()); |
| 183 |
dlg.ShowModal(); |
| 184 |
} |
| 185 |
#endif |
| 186 |
} |