From 67133145627bdacf5443d1ef47387ff6b17dd7b6 Mon Sep 17 00:00:00 2001 From: Ekopalypse Date: Sat, 28 Dec 2024 08:18:17 +0100 Subject: [PATCH 1/4] Convert loaded .ico files to bitmap handles; fixes #195 --- PythonScript/src/ConfigFile.cpp | 36 ++++++++++++++++++++++++++++---- PythonScript/src/ShortcutDlg.cpp | 32 +++++++++++++++++++++++++++- 2 files changed, 63 insertions(+), 5 deletions(-) diff --git a/PythonScript/src/ConfigFile.cpp b/PythonScript/src/ConfigFile.cpp index 2f6b7f70..77674eae 100644 --- a/PythonScript/src/ConfigFile.cpp +++ b/PythonScript/src/ConfigFile.cpp @@ -58,7 +58,7 @@ void ConfigFile::readConfig() char buffer[500]; - HBITMAP hIcon; + HBITMAP hBitmap; while (startupFile.good()) { @@ -90,17 +90,45 @@ void ConfigFile::readConfig() char *iconPath = strtok_s(NULL, "/", &context); if (!iconPath || !(*iconPath)) { - hIcon = static_cast(LoadImage(m_hInst, MAKEINTRESOURCE(IDB_PYTHON), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE)); + hBitmap = static_cast(LoadImage(m_hInst, MAKEINTRESOURCE(IDB_PYTHON), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE)); iconPath = NULL; } else { iconFullPath = expandPathIfNeeded(iconPath); - hIcon = static_cast(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE)); + hBitmap = static_cast(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE)); + if (hBitmap == NULL) { + HICON hIcon = (HICON)LoadImage(NULL, iconFullPath.c_str(), IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE); + if (hIcon) { + ICONINFO iconInfo; + if (GetIconInfo(hIcon, &iconInfo)) { + HDC hdc = GetDC(NULL); + if (hdc) { + HDC hdcMem = CreateCompatibleDC(hdc); + if (hdcMem) { + BITMAP bm{}; + if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) { + hBitmap = CreateCompatibleBitmap(hdc, 16, 16); + if (hBitmap) { + HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap); + DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, NULL, DI_NORMAL); + SelectObject(hdcMem, hbmOld); + } + } + DeleteDC(hdcMem); + } + ReleaseDC(NULL, hdc); + } + DeleteObject(iconInfo.hbmColor); + DeleteObject(iconInfo.hbmMask); + } + DestroyIcon(hIcon); + } + } } if (scriptFullPath != L"") { - m_toolbarItems.push_back(std::pair >(scriptFullPath, std::pair(hIcon, iconPath ? iconFullPath : tstring()))); + m_toolbarItems.push_back(std::pair >(scriptFullPath, std::pair(hBitmap, iconPath ? iconFullPath : tstring()))); } } else if (0 == strcmp(element, "SETTING")) diff --git a/PythonScript/src/ShortcutDlg.cpp b/PythonScript/src/ShortcutDlg.cpp index babb3450..292d826e 100644 --- a/PythonScript/src/ShortcutDlg.cpp +++ b/PythonScript/src/ShortcutDlg.cpp @@ -602,8 +602,38 @@ void ShortcutDlg::toolbarSetIcon() if (GetOpenFileName(&ofn)) { ConfigFile::ToolbarItemsTD::iterator it = m_toolbarItems.begin() + index; - it->second.first = static_cast(LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE)); + HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE); + if (hBitmap == NULL) { + HICON hIcon = (HICON)LoadImage(NULL, ofn.lpstrFile, IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE); + if (hIcon) { + ICONINFO iconInfo; + if (GetIconInfo(hIcon, &iconInfo)) { + HDC hdc = GetDC(NULL); + if (hdc) { + HDC hdcMem = CreateCompatibleDC(hdc); + if (hdcMem) { + BITMAP bm{}; + if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) { + hBitmap = CreateCompatibleBitmap(hdc, 16, 16); + if (hBitmap) { + HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap); + DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, NULL, DI_NORMAL); + SelectObject(hdcMem, hbmOld); + } + } + DeleteDC(hdcMem); + } + ReleaseDC(NULL, hdc); + } + DeleteObject(iconInfo.hbmColor); + DeleteObject(iconInfo.hbmMask); + } + DestroyIcon(hIcon); + } + } + it->second.first = hBitmap; it->second.second = ofn.lpstrFile; + int imageIndex = ImageList_Add(m_hImageList, it->second.first, NULL); LVITEM lvItem{}; lvItem.mask = LVIF_IMAGE; From 6faf9ca30f124ea676b6ea610b47155d9fb34b5b Mon Sep 17 00:00:00 2001 From: Ekopalypse Date: Sat, 28 Dec 2024 14:51:46 +0100 Subject: [PATCH 2/4] Make 16x16 .ico files possible for toolbar --- PythonScript/src/ConfigFile.cpp | 28 ++------------------------- PythonScript/src/ShortcutDlg.cpp | 29 ++-------------------------- PythonScript/src/Utility.cpp | 33 ++++++++++++++++++++++++++++++++ PythonScript/src/Utility.h | 7 +++++++ 4 files changed, 44 insertions(+), 53 deletions(-) create mode 100644 PythonScript/src/Utility.cpp create mode 100644 PythonScript/src/Utility.h diff --git a/PythonScript/src/ConfigFile.cpp b/PythonScript/src/ConfigFile.cpp index 77674eae..45c32446 100644 --- a/PythonScript/src/ConfigFile.cpp +++ b/PythonScript/src/ConfigFile.cpp @@ -3,6 +3,7 @@ #include "ConfigFile.h" #include "resource.h" #include "WcharMbcsConverter.h" +#include "Utility.h" ConfigFile* ConfigFile::s_instance; @@ -98,32 +99,7 @@ void ConfigFile::readConfig() iconFullPath = expandPathIfNeeded(iconPath); hBitmap = static_cast(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE)); if (hBitmap == NULL) { - HICON hIcon = (HICON)LoadImage(NULL, iconFullPath.c_str(), IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE); - if (hIcon) { - ICONINFO iconInfo; - if (GetIconInfo(hIcon, &iconInfo)) { - HDC hdc = GetDC(NULL); - if (hdc) { - HDC hdcMem = CreateCompatibleDC(hdc); - if (hdcMem) { - BITMAP bm{}; - if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) { - hBitmap = CreateCompatibleBitmap(hdc, 16, 16); - if (hBitmap) { - HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap); - DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, NULL, DI_NORMAL); - SelectObject(hdcMem, hbmOld); - } - } - DeleteDC(hdcMem); - } - ReleaseDC(NULL, hdc); - } - DeleteObject(iconInfo.hbmColor); - DeleteObject(iconInfo.hbmMask); - } - DestroyIcon(hIcon); - } + hBitmap = ConvertIconToBitmap(const_cast(iconFullPath.c_str())); } } if (scriptFullPath != L"") diff --git a/PythonScript/src/ShortcutDlg.cpp b/PythonScript/src/ShortcutDlg.cpp index 292d826e..89474416 100644 --- a/PythonScript/src/ShortcutDlg.cpp +++ b/PythonScript/src/ShortcutDlg.cpp @@ -5,7 +5,7 @@ #include "resource.h" #include "MenuManager.h" #include "Notepad_plus_msgs.h" - +#include "Utility.h" ShortcutDlg::ShortcutDlg(HINSTANCE hInst, NppData& nppData, const TCHAR *scriptDirAppend) : m_hTree(NULL), @@ -604,32 +604,7 @@ void ShortcutDlg::toolbarSetIcon() ConfigFile::ToolbarItemsTD::iterator it = m_toolbarItems.begin() + index; HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE); if (hBitmap == NULL) { - HICON hIcon = (HICON)LoadImage(NULL, ofn.lpstrFile, IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE); - if (hIcon) { - ICONINFO iconInfo; - if (GetIconInfo(hIcon, &iconInfo)) { - HDC hdc = GetDC(NULL); - if (hdc) { - HDC hdcMem = CreateCompatibleDC(hdc); - if (hdcMem) { - BITMAP bm{}; - if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) { - hBitmap = CreateCompatibleBitmap(hdc, 16, 16); - if (hBitmap) { - HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap); - DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, NULL, DI_NORMAL); - SelectObject(hdcMem, hbmOld); - } - } - DeleteDC(hdcMem); - } - ReleaseDC(NULL, hdc); - } - DeleteObject(iconInfo.hbmColor); - DeleteObject(iconInfo.hbmMask); - } - DestroyIcon(hIcon); - } + hBitmap = ConvertIconToBitmap(ofn.lpstrFile); } it->second.first = hBitmap; it->second.second = ofn.lpstrFile; diff --git a/PythonScript/src/Utility.cpp b/PythonScript/src/Utility.cpp new file mode 100644 index 00000000..4c97ce06 --- /dev/null +++ b/PythonScript/src/Utility.cpp @@ -0,0 +1,33 @@ +#include "stdafx.h" +#include "Utility.h" + +HBITMAP ConvertIconToBitmap(LPCWSTR file_path) { + HBITMAP hBitmap = nullptr; + HICON hIcon = (HICON)LoadImage(nullptr, file_path, IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE); + if (hIcon) { + ICONINFO iconInfo; + if (GetIconInfo(hIcon, &iconInfo)) { + HDC hdc = GetDC(nullptr); + if (hdc) { + HDC hdcMem = CreateCompatibleDC(hdc); + if (hdcMem) { + BITMAP bm{}; + if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) { + hBitmap = CreateCompatibleBitmap(hdc, 16, 16); + if (hBitmap) { + HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap); + DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, nullptr, DI_NORMAL); + SelectObject(hdcMem, hbmOld); + } + } + DeleteDC(hdcMem); + } + ReleaseDC(nullptr, hdc); + } + DeleteObject(iconInfo.hbmColor); + DeleteObject(iconInfo.hbmMask); + } + DestroyIcon(hIcon); + } + return hBitmap; +} diff --git a/PythonScript/src/Utility.h b/PythonScript/src/Utility.h new file mode 100644 index 00000000..41eae045 --- /dev/null +++ b/PythonScript/src/Utility.h @@ -0,0 +1,7 @@ +#ifndef _PYTHONSCRIPT_UTILITY_H +#define _PYTHONSCRIPT_UTILITY_H +#endif + +#include + +HBITMAP ConvertIconToBitmap(LPCWSTR file_path); From 90373a110a421d5f7ba0b9a6550ee30a9e41e167 Mon Sep 17 00:00:00 2001 From: Ekopalypse <47723516+Ekopalypse@users.noreply.github.com> Date: Sun, 29 Dec 2024 09:36:02 +0100 Subject: [PATCH 3/4] Add utility files to PythonScript.vcxproj --- PythonScript/project/PythonScript.vcxproj | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/PythonScript/project/PythonScript.vcxproj b/PythonScript/project/PythonScript.vcxproj index f38a9722..8b246e41 100644 --- a/PythonScript/project/PythonScript.vcxproj +++ b/PythonScript/project/PythonScript.vcxproj @@ -481,6 +481,7 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py + @@ -535,6 +536,7 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py + @@ -553,4 +555,4 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py - \ No newline at end of file + From 74c5c2a6499ad83a81909650a80543668de0ef0a Mon Sep 17 00:00:00 2001 From: Ekopalypse <47723516+Ekopalypse@users.noreply.github.com> Date: Sun, 29 Dec 2024 09:38:25 +0100 Subject: [PATCH 4/4] Add utility.cpp to PythonScript.Tests.vcxproj --- PythonScript.Tests/PythonScript.Tests.vcxproj | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/PythonScript.Tests/PythonScript.Tests.vcxproj b/PythonScript.Tests/PythonScript.Tests.vcxproj index 796297d3..4b79c7df 100644 --- a/PythonScript.Tests/PythonScript.Tests.vcxproj +++ b/PythonScript.Tests/PythonScript.Tests.vcxproj @@ -296,6 +296,7 @@ + Create @@ -321,4 +322,4 @@ - \ No newline at end of file +