pull/1/head
pipi 1 year ago
parent ff45e725cd
commit 8fd36839c5

@ -61,17 +61,18 @@ void Notepad_plus_Window::setStartupBgColor(COLORREF BgColor)
ReleaseDC(_hSelf, hdc); ReleaseDC(_hSelf, hdc);
} }
void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR* cmdLine, CmdLineParams* cmdLineParams) void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR* cmdLine, CmdLineParams* cmdLineParams)
{ {
// 初始化时间戳
time_t timestampBegin = 0; time_t timestampBegin = 0;
if (cmdLineParams->_showLoadingTime) if (cmdLineParams->_showLoadingTime)
timestampBegin = time(NULL); timestampBegin = time(NULL);
// 初始化窗口
Window::init(hInst, parent); Window::init(hInst, parent);
WNDCLASS nppClass{}; WNDCLASS nppClass{};
// 设置窗口类属性
nppClass.style = CS_BYTEALIGNWINDOW | CS_DBLCLKS; nppClass.style = CS_BYTEALIGNWINDOW | CS_DBLCLKS;
nppClass.lpfnWndProc = Notepad_plus_Proc; nppClass.lpfnWndProc = Notepad_plus_Proc;
nppClass.cbClsExtra = 0; nppClass.cbClsExtra = 0;
@ -85,49 +86,56 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
_isPrelaunch = cmdLineParams->_isPreLaunch; _isPrelaunch = cmdLineParams->_isPreLaunch;
// 注册窗口类
if (!::RegisterClass(&nppClass)) if (!::RegisterClass(&nppClass))
{ {
throw std::runtime_error("Notepad_plus_Window::init : RegisterClass() function failed"); throw std::runtime_error("Notepad_plus_Window::init : RegisterClass() function failed");
} }
// 获取 Notepad++ 参数实例
NppParameters& nppParams = NppParameters::getInstance(); NppParameters& nppParams = NppParameters::getInstance();
NppGUI& nppGUI = nppParams.getNppGUI(); NppGUI& nppGUI = nppParams.getNppGUI();
// 根据命令行参数禁用插件
if (cmdLineParams->_isNoPlugin) if (cmdLineParams->_isNoPlugin)
_notepad_plus_plus_core._pluginsManager.disable(); _notepad_plus_plus_core._pluginsManager.disable();
// 设置命令行参数的会话状态
nppGUI._isCmdlineNosessionActivated = cmdLineParams->_isNoSession; nppGUI._isCmdlineNosessionActivated = cmdLineParams->_isNoSession;
// 加载图标
_hIconAbsent = ::LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICONABSENT)); _hIconAbsent = ::LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICONABSENT));
// 创建窗口
_hSelf = ::CreateWindowEx( _hSelf = ::CreateWindowEx(
WS_EX_ACCEPTFILES | (_notepad_plus_plus_core._nativeLangSpeaker.isRTL() ? WS_EX_LAYOUTRTL : 0), WS_EX_ACCEPTFILES | (_notepad_plus_plus_core._nativeLangSpeaker.isRTL() ? WS_EX_LAYOUTRTL : 0),
_className, _className,
TEXT("Notepad++"), TEXT("Notepad++"),
(WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN), (WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN),
// CreateWindowEx bug : set all 0 to walk around the pb
0, 0, 0, 0, 0, 0, 0, 0,
_hParent, nullptr, _hInst, _hParent, nullptr, _hInst,
(LPVOID) this); // pass the ptr of this instantiated object (LPVOID)this);
// for retrieve it in Notepad_plus_Proc from
// the CREATESTRUCT.lpCreateParams afterward.
if (NULL == _hSelf) if (NULL == _hSelf)
throw std::runtime_error("Notepad_plus_Window::init : CreateWindowEx() function return null"); throw std::runtime_error("Notepad_plus_Window::init : CreateWindowEx() function return null");
// 锁定绘制
PaintLocker paintLocker{ _hSelf }; PaintLocker paintLocker{ _hSelf };
// 静态检查菜单和工具栏
_notepad_plus_plus_core.staticCheckMenuAndTB(); _notepad_plus_plus_core.staticCheckMenuAndTB();
// 设置全局窗口句柄
gNppHWND = _hSelf; gNppHWND = _hSelf;
// 移动窗口位置或设置默认位置
if (cmdLineParams->isPointValid()) if (cmdLineParams->isPointValid())
{ {
::MoveWindow(_hSelf, cmdLineParams->_point.x, cmdLineParams->_point.y, nppGUI._appPos.right, nppGUI._appPos.bottom, TRUE); ::MoveWindow(_hSelf, cmdLineParams->_point.x, cmdLineParams->_point.y, nppGUI._appPos.right, nppGUI._appPos.bottom, TRUE);
} }
else else
{ {
// 设置窗口位置信息
WINDOWPLACEMENT posInfo{}; WINDOWPLACEMENT posInfo{};
posInfo.length = sizeof(WINDOWPLACEMENT); posInfo.length = sizeof(WINDOWPLACEMENT);
posInfo.flags = 0; posInfo.flags = 0;
@ -136,6 +144,7 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
else else
posInfo.showCmd = nppGUI._isMaximized ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL; posInfo.showCmd = nppGUI._isMaximized ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL;
// 设置窗口位置
posInfo.ptMinPosition.x = (LONG)-1; posInfo.ptMinPosition.x = (LONG)-1;
posInfo.ptMinPosition.y = (LONG)-1; posInfo.ptMinPosition.y = (LONG)-1;
posInfo.ptMaxPosition.x = (LONG)-1; posInfo.ptMaxPosition.x = (LONG)-1;
@ -145,36 +154,43 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
posInfo.rcNormalPosition.bottom = nppGUI._appPos.top + nppGUI._appPos.bottom; posInfo.rcNormalPosition.bottom = nppGUI._appPos.top + nppGUI._appPos.bottom;
posInfo.rcNormalPosition.right = nppGUI._appPos.left + nppGUI._appPos.right; posInfo.rcNormalPosition.right = nppGUI._appPos.left + nppGUI._appPos.right;
//SetWindowPlacement will take care of situations, where saved position was in no longer available monitor // 设置窗口位置
::SetWindowPlacement(_hSelf, &posInfo); ::SetWindowPlacement(_hSelf, &posInfo);
// 绘制暗色背景
if (NppDarkMode::isEnabled()) if (NppDarkMode::isEnabled())
setStartupBgColor(NppDarkMode::getBackgroundColor()); //draw dark background when opening Npp without position data setStartupBgColor(NppDarkMode::getBackgroundColor());
} }
// 处理选项卡显示模式
if ((nppGUI._tabStatus & TAB_MULTILINE) != 0) if ((nppGUI._tabStatus & TAB_MULTILINE) != 0)
::SendMessage(_hSelf, WM_COMMAND, IDM_VIEW_DRAWTABBAR_MULTILINE, 0); ::SendMessage(_hSelf, WM_COMMAND, IDM_VIEW_DRAWTABBAR_MULTILINE, 0);
// 隐藏/显示菜单栏
if (!nppGUI._menuBarShow) if (!nppGUI._menuBarShow)
::SetMenu(_hSelf, NULL); ::SetMenu(_hSelf, NULL);
// 隐藏选项卡栏
if (cmdLineParams->_isNoTab || (nppGUI._tabStatus & TAB_HIDE)) if (cmdLineParams->_isNoTab || (nppGUI._tabStatus & TAB_HIDE))
{ {
const int tabStatusOld = nppGUI._tabStatus; const int tabStatusOld = nppGUI._tabStatus;
::SendMessage(_hSelf, NPPM_HIDETABBAR, 0, TRUE); ::SendMessage(_hSelf, NPPM_HIDETABBAR, 0, TRUE);
if (cmdLineParams->_isNoTab) if (cmdLineParams->_isNoTab)
{ {
// Restore old settings when tab bar has been hidden from tab bar. // 从选项卡栏隐藏后恢复旧设置
nppGUI._tabStatus = tabStatusOld; nppGUI._tabStatus = tabStatusOld;
} }
} }
// 设置窗口置顶
if (cmdLineParams->_alwaysOnTop) if (cmdLineParams->_alwaysOnTop)
::SendMessage(_hSelf, WM_COMMAND, IDM_VIEW_ALWAYSONTOP, 0); ::SendMessage(_hSelf, WM_COMMAND, IDM_VIEW_ALWAYSONTOP, 0);
// 加载上次会话
if (nppGUI._rememberLastSession && !nppGUI._isCmdlineNosessionActivated) if (nppGUI._rememberLastSession && !nppGUI._isCmdlineNosessionActivated)
_notepad_plus_plus_core.loadLastSession(); _notepad_plus_plus_core.loadLastSession();
// 导出功能列表或打印并退出
if (nppParams.doFunctionListExport() || nppParams.doPrintAndExit()) if (nppParams.doFunctionListExport() || nppParams.doPrintAndExit())
{ {
::ShowWindow(_hSelf, SW_HIDE); ::ShowWindow(_hSelf, SW_HIDE);
@ -188,35 +204,33 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
} }
else else
{ {
// 加载托盘图标
HICON icon = nullptr; HICON icon = nullptr;
loadTrayIcon(_hInst, &icon); loadTrayIcon(_hInst, &icon);
_notepad_plus_plus_core._pTrayIco = new trayIconControler(_hSelf, IDI_M30ICON, NPPM_INTERNAL_MINIMIZED_TRAY, icon, TEXT("")); _notepad_plus_plus_core._pTrayIco = new trayIconControler(_hSelf, IDI_M30ICON, NPPM_INTERNAL_MINIMIZED_TRAY, icon, TEXT(""));
_notepad_plus_plus_core._pTrayIco->doTrayIcon(ADD); _notepad_plus_plus_core._pTrayIco->doTrayIcon(ADD);
} }
// 根据命令行参数和暗黑模式设置启动背景颜色
if (cmdLineParams->isPointValid() && NppDarkMode::isEnabled()) if (cmdLineParams->isPointValid() && NppDarkMode::isEnabled())
setStartupBgColor(NppDarkMode::getBackgroundColor()); //draw dark background when opening Npp through cmd with position data setStartupBgColor(NppDarkMode::getBackgroundColor());
// 加载本地化文件
std::vector<generic_string> fileNames; std::vector<generic_string> fileNames;
std::vector<generic_string> patterns; std::vector<generic_string> patterns;
patterns.push_back(TEXT("*.xml")); patterns.push_back(TEXT("*.xml"));
generic_string nppDir = nppParams.getNppPath(); generic_string nppDir = nppParams.getNppPath();
LocalizationSwitcher& localizationSwitcher = nppParams.getLocalizationSwitcher(); LocalizationSwitcher& localizationSwitcher = nppParams.getLocalizationSwitcher();
std::wstring localizationDir = nppDir; std::wstring localizationDir = nppDir;
pathAppend(localizationDir, TEXT("localization\\")); pathAppend(localizationDir, TEXT("localization\\"));
_notepad_plus_plus_core.getMatchedFileNames(localizationDir.c_str(), 0, patterns, fileNames, false, false); _notepad_plus_plus_core.getMatchedFileNames(localizationDir.c_str(), 0, patterns, fileNames, false, false);
for (size_t i = 0, len = fileNames.size(); i < len; ++i) for (size_t i = 0, len = fileNames.size(); i < len; ++i)
localizationSwitcher.addLanguageFromXml(fileNames[i]); localizationSwitcher.addLanguageFromXml(fileNames[i]);
// 加载主题文件
fileNames.clear(); fileNames.clear();
ThemeSwitcher& themeSwitcher = nppParams.getThemeSwitcher(); ThemeSwitcher& themeSwitcher = nppParams.getThemeSwitcher();
// 获取应用数据主题目录
// Get themes from both npp install themes dir and app data themes dir with the per user
// overriding default themes of the same name.
generic_string appDataThemeDir = nppParams.isCloud() ? nppParams.getUserPath() : nppParams.getAppDataNppDir(); generic_string appDataThemeDir = nppParams.isCloud() ? nppParams.getUserPath() : nppParams.getAppDataNppDir();
if (!appDataThemeDir.empty()) if (!appDataThemeDir.empty())
{ {
@ -228,14 +242,11 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
} }
} }
// 加载主题文件
fileNames.clear(); fileNames.clear();
generic_string nppThemeDir = nppDir.c_str();
generic_string nppThemeDir = nppDir.c_str(); // <- should use the pointer to avoid the constructor of copy
pathAppend(nppThemeDir, TEXT("themes\\")); pathAppend(nppThemeDir, TEXT("themes\\"));
// Set theme directory to their installation directory
themeSwitcher.setThemeDirPath(nppThemeDir); themeSwitcher.setThemeDirPath(nppThemeDir);
_notepad_plus_plus_core.getMatchedFileNames(nppThemeDir.c_str(), 0, patterns, fileNames, false, false); _notepad_plus_plus_core.getMatchedFileNames(nppThemeDir.c_str(), 0, patterns, fileNames, false, false);
for (size_t i = 0, len = fileNames.size(); i < len; ++i) for (size_t i = 0, len = fileNames.size(); i < len; ++i)
{ {
@ -247,7 +258,6 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
if (!appDataThemeDir.empty()) if (!appDataThemeDir.empty())
{ {
generic_string appDataThemePath = appDataThemeDir; generic_string appDataThemePath = appDataThemeDir;
if (!::PathFileExists(appDataThemePath.c_str())) if (!::PathFileExists(appDataThemePath.c_str()))
{ {
::CreateDirectory(appDataThemePath.c_str(), NULL); ::CreateDirectory(appDataThemePath.c_str(), NULL);
@ -260,6 +270,7 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
} }
} }
// 根据暗黑模式设置主题
if (NppDarkMode::isWindowsModeEnabled()) if (NppDarkMode::isWindowsModeEnabled())
{ {
generic_string themePath; generic_string themePath;
@ -293,42 +304,54 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
} }
} }
// Restore all dockable panels from the last session // 恢复上次会话中的可停靠面板
for (size_t i = 0, len = _notepad_plus_plus_core._internalFuncIDs.size(); i < len; ++i) for (size_t i = 0, len = _notepad_plus_plus_core._internalFuncIDs.size(); i < len; ++i)
::SendMessage(_hSelf, WM_COMMAND, _notepad_plus_plus_core._internalFuncIDs[i], 0); ::SendMessage(_hSelf, WM_COMMAND, _notepad_plus_plus_core._internalFuncIDs[i], 0);
// 加载命令行参数中的文件
std::vector<generic_string> fns; std::vector<generic_string> fns;
if (cmdLine) if (cmdLine)
fns = _notepad_plus_plus_core.loadCommandlineParams(cmdLine, cmdLineParams); fns = _notepad_plus_plus_core.loadCommandlineParams(cmdLine, cmdLineParams);
// Launch folder as workspace after all this dockable panel being restored from the last session // 如果是快照模式,则检查修改过的文档并启动备份任务
// To avoid dockable panel toggle problem. bool isSnapshotMode = nppGUI.isSnapshotMode();
if (cmdLineParams->_openFoldersAsWorkspace) if (isSnapshotMode)
{ {
generic_string emptyStr; _notepad_plus_plus_core.checkModifiedDocument(false);
_notepad_plus_plus_core.launchFileBrowser(fns, emptyStr, true); // 启动备份任务
_notepad_plus_plus_core.launchDocumentBackupTask();
} }
::SendMessage(_hSelf, WM_ACTIVATE, WA_ACTIVE, 0);
::SendMessage(_hSelf, NPPM_INTERNAL_CRLFFORMCHANGED, 0, 0);
::SendMessage(_hSelf, NPPM_INTERNAL_NPCFORMCHANGED, 0, 0); // 设置单词字符和 NPC
::SendMessage(_hSelf, NPPM_INTERNAL_SETWORDCHARS, 0, 0);
::SendMessage(_hSelf, NPPM_INTERNAL_SETNPC, 0, 0);
::SendMessage(_hSelf, NPPM_INTERNAL_ENABLECHANGEHISTORY, 0, 0); // 导出功能列表并退出
if (nppParams.doFunctionListExport())
::SendMessage(_hSelf, NPPM_INTERNAL_EXPORTFUNCLISTANDQUIT, 0, 0);
// 打印并退出
if (nppParams.doPrintAndExit())
::SendMessage(_hSelf, NPPM_INTERNAL_PRNTANDQUIT, 0, 0);
if (nppGUI._newDocDefaultSettings._addNewDocumentOnStartup && nppGUI._rememberLastSession) // 计算加载时间并显示消息框
if (cmdLineParams->_showLoadingTime)
{ {
::SendMessage(_hSelf, WM_COMMAND, IDM_FILE_NEW, 0); time_t timestampEnd = time(NULL);
double loadTime = difftime(timestampEnd, timestampBegin);
char dest[256];
sprintf(dest, "Loading time : %.0lf seconds", loadTime);
::MessageBoxA(NULL, dest, "", MB_OK);
} }
// Notify plugins that Notepad++ is ready // 通知插件 Notepad++ 已准备就绪
SCNotification scnN{}; SCNotification scnN{};
scnN.nmhdr.code = NPPN_READY; scnN.nmhdr.code = NPPN_READY;
scnN.nmhdr.hwndFrom = _hSelf; scnN.nmhdr.hwndFrom = _hSelf;
scnN.nmhdr.idFrom = 0; scnN.nmhdr.idFrom = 0;
_notepad_plus_plus_core._pluginsManager.notify(&scnN); _notepad_plus_plus_core._pluginsManager.notify(&scnN);
// 处理命令行参数中的彩蛋
if (!cmdLineParams->_easterEggName.empty()) if (!cmdLineParams->_easterEggName.empty())
{ {
if (cmdLineParams->_quoteType == 0) // Easter Egg Name if (cmdLineParams->_quoteType == 0) // Easter Egg Name
@ -353,10 +376,9 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
_quoteParams._speed = QuoteParams::rapid; _quoteParams._speed = QuoteParams::rapid;
else if (cmdLineParams->_ghostTypingSpeed == 3) else if (cmdLineParams->_ghostTypingSpeed == 3)
_quoteParams._speed = QuoteParams::speedOfLight; _quoteParams._speed = QuoteParams::speedOfLight;
_notepad_plus_plus_core.showQuote(&_quoteParams); _notepad_plus_plus_core.showQuote(&_quoteParams);
} }
else if (cmdLineParams->_quoteType == 2) // content drom file else if (cmdLineParams->_quoteType == 2) // content from file
{ {
if (::PathFileExists(cmdLineParams->_easterEggName.c_str())) if (::PathFileExists(cmdLineParams->_easterEggName.c_str()))
{ {
@ -376,40 +398,11 @@ void Notepad_plus_Window::init(HINSTANCE hInst, HWND parent, const TCHAR *cmdLin
_quoteParams._speed = QuoteParams::rapid; _quoteParams._speed = QuoteParams::rapid;
else if (cmdLineParams->_ghostTypingSpeed == 3) else if (cmdLineParams->_ghostTypingSpeed == 3)
_quoteParams._speed = QuoteParams::speedOfLight; _quoteParams._speed = QuoteParams::speedOfLight;
_notepad_plus_plus_core.showQuote(&_quoteParams); _notepad_plus_plus_core.showQuote(&_quoteParams);
} }
} }
} }
} }
if (cmdLineParams->_showLoadingTime)
{
time_t timestampEnd = time(NULL);
double loadTime = difftime(timestampEnd, timestampBegin);
char dest[256];
sprintf(dest, "Loading time : %.0lf seconds", loadTime);
::MessageBoxA(NULL, dest, "", MB_OK);
}
bool isSnapshotMode = nppGUI.isSnapshotMode();
if (isSnapshotMode)
{
_notepad_plus_plus_core.checkModifiedDocument(false);
// Lauch backup task
_notepad_plus_plus_core.launchDocumentBackupTask();
}
// Make this call later to take effect
::SendMessage(_hSelf, NPPM_INTERNAL_SETWORDCHARS, 0, 0);
::SendMessage(_hSelf, NPPM_INTERNAL_SETNPC, 0, 0);
if (nppParams.doFunctionListExport())
::SendMessage(_hSelf, NPPM_INTERNAL_EXPORTFUNCLISTANDQUIT, 0, 0);
if (nppParams.doPrintAndExit())
::SendMessage(_hSelf, NPPM_INTERNAL_PRNTANDQUIT, 0, 0);
} }

@ -55,10 +55,10 @@ filePath : file or folder name to open (absolute or relative path name)\r\
"); ");
class Notepad_plus_Window : public Window class Notepad_plus_Window : public Window//用作程序的主窗口
{ {
public: public:
void init(HINSTANCE, HWND, const TCHAR *cmdLine, CmdLineParams *cmdLineParams); void init(HINSTANCE, HWND, const TCHAR *cmdLine, CmdLineParams *cmdLineParams);//创建窗口
bool isDlgsMsg(MSG *msg) const; bool isDlgsMsg(MSG *msg) const;

Loading…
Cancel
Save