Menu

[0b4401]: / PythonScript / src / HelpController.cpp  Maximize  Restore  History

Download this file

139 lines (114 with data), 4.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "stdafx.h"
#include "HelpController.h"
#include "Scintilla.h"
#include "Notepad_plus_msgs.h"
#include "WcharMbcsConverter.h"
#include "PythonScriptVersion.h"
HelpController::HelpController(HWND hNotepad, HWND hScintilla)
: m_hNotepad(hNotepad),
m_hScintilla(hScintilla)
{
}
void HelpController::callHelp()
{
//::HtmlHelp(m_hNotepad, getFilename(), HH_DISPLAY_TOPIC, getTopicUrl().c_str());
std::string helpFile = getFilename();
bool useWeb = false;
if (helpFile.empty())
{
helpFile = "http://npppythonscript.sourceforge.net/docs/";
helpFile.append(PYSCR_VERSION_STRING);
helpFile.append("/");
useWeb = true;
}
if (useWeb)
{
helpFile.append(getTopicUrl());
::ShellExecuteA(m_hNotepad, "open", helpFile.c_str(), NULL, NULL, SW_SHOWDEFAULT);
}
else
{
::HtmlHelpA(m_hNotepad, helpFile.c_str(), HH_DISPLAY_TOPIC, reinterpret_cast<DWORD_PTR>(getTopicUrl().c_str()));
}
}
std::string HelpController::getFilename()
{
TCHAR helpPath[MAX_PATH];
::SendMessage(m_hNotepad, NPPM_GETNPPDIRECTORY, MAX_PATH, reinterpret_cast<LPARAM>(helpPath));
_tcscat_s(helpPath, MAX_PATH, _T("\\plugins\\doc\\PythonScript\\PythonScript.chm"));
if (::PathFileExists(helpPath))
{
return std::string(WcharMbcsConverter::tchar2char(helpPath).get());
}
else
{
return std::string();
}
}
std::string HelpController::getTopicUrl()
{
size_t length = (size_t)SendMessage(m_hScintilla, SCI_GETCURLINE, 0, 0);
char *buffer = new char[length + 1];
SendMessage(m_hScintilla, SCI_GETCURLINE, length, reinterpret_cast<LPARAM>(buffer));
idx_t position = (idx_t)SendMessage(m_hScintilla, SCI_GETCURRENTPOS, 0, 0);
idx_t lineNumber = (idx_t)SendMessage(m_hScintilla, SCI_LINEFROMPOSITION, position, 0);
idx_t lineStartPos = (idx_t)SendMessage(m_hScintilla, SCI_POSITIONFROMLINE, lineNumber, 0);
position = position - lineStartPos;
// position is now the character offset in buffer of the caret
// Go forwards until we reach a non-acceptable char
bool foundDot = false;
idx_t dotPosition = 0;
idx_t endPosition;
for(endPosition = position;
( (buffer[endPosition] >= 'A' && buffer[endPosition] <= 'Z')
|| (buffer[endPosition] >= '0' && buffer[endPosition] <= '9')
|| (buffer[endPosition] >= 'a' && buffer[endPosition] <= 'z')
|| (!foundDot && buffer[endPosition] == '.'))
&& endPosition < length; ++endPosition)
{
if (buffer[endPosition] == '.')
{
foundDot = true;
dotPosition = endPosition;
}
}
// Go backwards until we reach a non-acceptable char
idx_t startPosition = position > 0 ? position - 1 : 0;
for(;
( (buffer[startPosition] >= 'A' && buffer[startPosition] <= 'Z')
|| (buffer[startPosition] >= '0' && buffer[startPosition] <= '9')
|| (buffer[startPosition] >= 'a' && buffer[startPosition] <= 'z')
|| (!foundDot && buffer[startPosition] == '.'))
&& startPosition != IDX_MAX; --startPosition)
{
if (buffer[startPosition] == '.')
{
foundDot = true;
dotPosition = startPosition;
}
}
startPosition++;
buffer[endPosition] = '\0';
std::string url;
if (foundDot && endPosition > startPosition)
{
if (dotPosition - startPosition == 7 && 0 == _strnicmp((buffer + startPosition), "notepad", dotPosition - startPosition))
{
url = "notepad.html#Notepad.";
url.append(buffer + dotPosition + 1);
}
if ((dotPosition - startPosition == 6 && 0 == _strnicmp((buffer + startPosition), "editor", dotPosition - startPosition))
|| (dotPosition - startPosition == 7 && 0 == _strnicmp((buffer + startPosition), "editor1", dotPosition - startPosition))
|| (dotPosition - startPosition == 7 && 0 == _strnicmp((buffer + startPosition), "editor2", dotPosition - startPosition)))
{
url = "scintilla.html#Editor.";
url.append(buffer + dotPosition + 1);
}
if (dotPosition - startPosition == 7 && 0 == _strnicmp((buffer + startPosition), "console", dotPosition - startPosition))
{
url = "console.html#Console.";
url.append(buffer + dotPosition + 1);
}
}
return url;
}