// NppPluginClass.cpp
// This file is part of the Notepad++ External Lexers Plugin.
// Copyright 2008 - 2009 Thell Fowler (thell@almostautomated.com)
//
// This program is free software; you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software Foundation; either version
// 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with this program;
// if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
///////////////////////////////////////////////////////////////////////////////////////////////
// Class for Plugin Object Data Storage
// for use with Notepad++ Plugins
///////////////////////////////////////////////////////////////////////////////////////////////
#include <algorithm>
#include <iterator>
#include "NppPluginClass.h"
void NppPlugin::init(const TCHAR* name, HANDLE hModule) // Initialize Plugin.
{
/*
* NppPlugin is a singleton class and should only have init() called once!
* Fail if called again.
*
*/
_ASSERT(!_isInitialized); // Assert that the class hasn't already been initialized.
_Name = name;
_hModule = hModule;
_isInitialized = true;
}
void NppPlugin::setNppData(NppData NppData) // Store Notepad++ handle details.
{
_hNpp = NppData._nppHandle;
_hScintillaMain = NppData._scintillaMainHandle;
_hScintillaSecond = NppData._scintillaSecondHandle;
}
const TCHAR* NppPlugin::Name() // Plugin module's name, for internal Notepad++ use.
{
return _Name;
}
HANDLE NppPlugin::hModule() // Plugin module's handle.
{
return _hModule;
}
HWND NppPlugin::hNpp() // Returns the main Notepad++ handle.
{
return _hNpp;
}
HWND NppPlugin::hScintillaMain() // Returns handle used in messaging the main view.
{
return _hScintillaMain;
}
HWND NppPlugin::hScintillaSecond() // Returns handle used in messaging the secondary view.
{
return _hScintillaSecond;
}
HWND NppPlugin::hCurrView() // Returns handle for the currently focused view.
{
if ( bRefreshCurrViewH || !_isReady ) {
_hCurrView = NppPlugin::updateCurrViewHandle();
}
return _hCurrView;
}
HWND NppPlugin::updateCurrViewHandle() // Updates handle pointer to the currently active view.
{
int currentEdit;
::SendMessage(_hNpp, NPPM_GETCURRENTSCINTILLA, 0, (LPARAM)¤tEdit);
bRefreshCurrViewH = false;
return ( currentEdit == 0 ) ? ( _hScintillaMain ): ( _hScintillaSecond );
};
bool NppPlugin::isReady() // Returns Notepad++ initialization state.
{
return _isReady;
}
void NppPlugin::setLexerFuncItem(const TCHAR* szName, PFUNCPLUGINCMD pFunc)
{
/*
* This function stores Function Items to the FuncItem vector.
* Using a vector instead of array to allow for easier sorting.
*
* The _cmdID values are assigned after sorting in getFunctionsArray().
*
*/
FuncItem thisFunction;
thisFunction._cmdID = NULL;
lstrcpy(thisFunction._itemName, szName);
thisFunction._pFunc = pFunc;
thisFunction._init2Check = false;
thisFunction._pShKey = NULL;
_FunctionsLexer.push_back(thisFunction);
}
void NppPlugin::setPluginFuncItem(const TCHAR* szName, PFUNCPLUGINCMD pFunc)
{
/*
* This function stores Function Items to the FuncItem vector.
* Using a vector instead of array to allow for easier sorting.
*
* The _cmdID values are assigned after sorting in getFunctionsArray().
*
*/
FuncItem thisFunction;
thisFunction._cmdID = NULL;
lstrcpy(thisFunction._itemName, szName);
thisFunction._pFunc = pFunc;
thisFunction._init2Check = false;
thisFunction._pShKey = NULL;
_FunctionsPlugin.push_back(thisFunction);
}
FuncItem * NppPlugin::getFunctionsArray() // Return this plugin's sorted FuncItem array.
{
/*
* In order to have the function items show up in the Notepad++ plugins menu with
* this plugins function items at the bottom a new vector is created and the existing
* vectors are copied into it with the lexer functions first.
*
*/
// <--- Local function for sorting criteria predicate. --->
struct sortByName
{
bool operator()(FuncItem& f1, FuncItem& f2)
{
return f1._itemName < f2._itemName;
}
};
if ( _FunctionsLexer.empty() ) {
// Doesn't look like there are any lexer function items so just send the plugin's.
ReturnVector = _FunctionsPlugin;
}
else {
// Sort the lexer function items vector.
std::sort(_FunctionsLexer.begin(), _FunctionsLexer.end(), sortByName());
// Copy it to a new vector that will be returned to Notepad++'s PluginsManager.
ReturnVector = _FunctionsLexer;
// Append the base plugin's function items (ie 'Help', 'About', etc...
std::copy(_FunctionsPlugin.begin(), _FunctionsPlugin.end(), std::back_inserter(ReturnVector));
}
return ( &ReturnVector[0] );
}
int NppPlugin::getFunctionsCount()
{
int count = _FunctionsLexer.size();
count += _FunctionsPlugin.size();
return count;
}