Skip to content

Commit d14c8be

Browse files
authored
Make 16x16 .ico files possible for toolbar (bruderstein#366)
* Convert loaded .ico files to bitmap handles; fixes bruderstein#195 * Make 16x16 .ico files possible for toolbar * Add utility files to PythonScript.vcxproj * Add utility.cpp to PythonScript.Tests.vcxproj
1 parent b08c46e commit d14c8be

File tree

6 files changed

+60
-8
lines changed

6 files changed

+60
-8
lines changed

PythonScript.Tests/PythonScript.Tests.vcxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
<ClCompile Include="..\PythonScript\src\UTF8Iterator.cpp" />
297297
<ClCompile Include="..\PythonScript\src\UtfConversion.cpp" />
298298
<ClCompile Include="..\PythonScript\src\WcharMbcsConverter.cpp" />
299+
<ClCompile Include="..\PythonScript\src\Utility.cpp" />
299300
<ClCompile Include="TestRunner.cpp" />
300301
<ClCompile Include="stdafx.cpp">
301302
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
@@ -321,4 +322,4 @@
321322
<Error Condition="!Exists('..\packages\boost.1.87.0\build\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\boost.1.87.0\build\boost.targets'))" />
322323
<Error Condition="!Exists('..\packages\boost_python312-vc143.1.87.0\build\boost_python312-vc143.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\boost_python312-vc143.1.87.0\build\boost_python312-vc143.targets'))" />
323324
</Target>
324-
</Project>
325+
</Project>

PythonScript/project/PythonScript.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py
481481
<ClCompile Include="..\src\UTF8Iterator.cpp" />
482482
<ClCompile Include="..\src\UtfConversion.cpp" />
483483
<ClCompile Include="..\src\WcharMbcsConverter.cpp" />
484+
<ClCompile Include="..\src\Utility.cpp" />
484485
</ItemGroup>
485486
<ItemGroup>
486487
<ClInclude Include="..\include\PythonScript\NppPythonScript.h" />
@@ -535,6 +536,7 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py
535536
<ClInclude Include="..\src\UTF8Iterator.h" />
536537
<ClInclude Include="..\src\UtfConversion.h" />
537538
<ClInclude Include="..\src\WcharMbcsConverter.h" />
539+
<ClInclude Include="..\src\Utility.h" />
538540
<ClInclude Include="..\res\resource1.h" />
539541
</ItemGroup>
540542
<ItemGroup>
@@ -553,4 +555,4 @@ xcopy $(ProjectDir)..\python_tests\*.* "e:\notepadtest\unicode\plugins\config\py
553555
<Error Condition="!Exists('..\..\packages\boost.1.87.0\build\boost.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\boost.1.87.0\build\boost.targets'))" />
554556
<Error Condition="!Exists('..\..\packages\boost_python312-vc143.1.87.0\build\boost_python312-vc143.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\boost_python312-vc143.1.87.0\build\boost_python312-vc143.targets'))" />
555557
</Target>
556-
</Project>
558+
</Project>

PythonScript/src/ConfigFile.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "ConfigFile.h"
44
#include "resource.h"
55
#include "WcharMbcsConverter.h"
6+
#include "Utility.h"
67

78
ConfigFile* ConfigFile::s_instance;
89

@@ -58,7 +59,7 @@ void ConfigFile::readConfig()
5859
char buffer[500];
5960

6061

61-
HBITMAP hIcon;
62+
HBITMAP hBitmap;
6263

6364
while (startupFile.good())
6465
{
@@ -90,17 +91,20 @@ void ConfigFile::readConfig()
9091
char *iconPath = strtok_s(NULL, "/", &context);
9192
if (!iconPath || !(*iconPath))
9293
{
93-
hIcon = static_cast<HBITMAP>(LoadImage(m_hInst, MAKEINTRESOURCE(IDB_PYTHON), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE));
94+
hBitmap = static_cast<HBITMAP>(LoadImage(m_hInst, MAKEINTRESOURCE(IDB_PYTHON), IMAGE_BITMAP, 0, 0, LR_DEFAULTSIZE));
9495
iconPath = NULL;
9596
}
9697
else
9798
{
9899
iconFullPath = expandPathIfNeeded(iconPath);
99-
hIcon = static_cast<HBITMAP>(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE));
100+
hBitmap = static_cast<HBITMAP>(LoadImage(NULL, iconFullPath.c_str(), IMAGE_BITMAP, 16, 16, LR_LOADMAP3DCOLORS | LR_LOADFROMFILE));
101+
if (hBitmap == NULL) {
102+
hBitmap = ConvertIconToBitmap(const_cast<LPWSTR>(iconFullPath.c_str()));
103+
}
100104
}
101105
if (scriptFullPath != L"")
102106
{
103-
m_toolbarItems.push_back(std::pair<tstring, std::pair<HBITMAP, tstring> >(scriptFullPath, std::pair<HBITMAP, tstring>(hIcon, iconPath ? iconFullPath : tstring())));
107+
m_toolbarItems.push_back(std::pair<tstring, std::pair<HBITMAP, tstring> >(scriptFullPath, std::pair<HBITMAP, tstring>(hBitmap, iconPath ? iconFullPath : tstring())));
104108
}
105109
}
106110
else if (0 == strcmp(element, "SETTING"))

PythonScript/src/ShortcutDlg.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "resource.h"
66
#include "MenuManager.h"
77
#include "Notepad_plus_msgs.h"
8-
8+
#include "Utility.h"
99

1010
ShortcutDlg::ShortcutDlg(HINSTANCE hInst, NppData& nppData, const TCHAR *scriptDirAppend)
1111
: m_hTree(NULL),
@@ -602,8 +602,13 @@ void ShortcutDlg::toolbarSetIcon()
602602
if (GetOpenFileName(&ofn))
603603
{
604604
ConfigFile::ToolbarItemsTD::iterator it = m_toolbarItems.begin() + index;
605-
it->second.first = static_cast<HBITMAP>(LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE));
605+
HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 16, 16, LR_COLOR | LR_LOADFROMFILE);
606+
if (hBitmap == NULL) {
607+
hBitmap = ConvertIconToBitmap(ofn.lpstrFile);
608+
}
609+
it->second.first = hBitmap;
606610
it->second.second = ofn.lpstrFile;
611+
607612
int imageIndex = ImageList_Add(m_hImageList, it->second.first, NULL);
608613
LVITEM lvItem{};
609614
lvItem.mask = LVIF_IMAGE;

PythonScript/src/Utility.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "stdafx.h"
2+
#include "Utility.h"
3+
4+
HBITMAP ConvertIconToBitmap(LPCWSTR file_path) {
5+
HBITMAP hBitmap = nullptr;
6+
HICON hIcon = (HICON)LoadImage(nullptr, file_path, IMAGE_ICON, 16, 16, LR_COLOR | LR_LOADFROMFILE);
7+
if (hIcon) {
8+
ICONINFO iconInfo;
9+
if (GetIconInfo(hIcon, &iconInfo)) {
10+
HDC hdc = GetDC(nullptr);
11+
if (hdc) {
12+
HDC hdcMem = CreateCompatibleDC(hdc);
13+
if (hdcMem) {
14+
BITMAP bm{};
15+
if (GetObject(iconInfo.hbmColor, sizeof(BITMAP), &bm)) {
16+
hBitmap = CreateCompatibleBitmap(hdc, 16, 16);
17+
if (hBitmap) {
18+
HBITMAP hbmOld = (HBITMAP)SelectObject(hdcMem, hBitmap);
19+
DrawIconEx(hdcMem, 0, 0, hIcon, 16, 16, 0, nullptr, DI_NORMAL);
20+
SelectObject(hdcMem, hbmOld);
21+
}
22+
}
23+
DeleteDC(hdcMem);
24+
}
25+
ReleaseDC(nullptr, hdc);
26+
}
27+
DeleteObject(iconInfo.hbmColor);
28+
DeleteObject(iconInfo.hbmMask);
29+
}
30+
DestroyIcon(hIcon);
31+
}
32+
return hBitmap;
33+
}

PythonScript/src/Utility.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef _PYTHONSCRIPT_UTILITY_H
2+
#define _PYTHONSCRIPT_UTILITY_H
3+
#endif
4+
5+
#include <wtypes.h>
6+
7+
HBITMAP ConvertIconToBitmap(LPCWSTR file_path);

0 commit comments

Comments
 (0)