2014-08-16 13:02:00 -05:00
|
|
|
// 16 august 2014
|
|
|
|
|
|
|
|
#include "winapi_windows.h"
|
|
|
|
#include "_cgo_export.h"
|
|
|
|
|
|
|
|
HBITMAP unscaledBitmap(void *i, intptr_t dx, intptr_t dy)
|
|
|
|
{
|
|
|
|
BITMAPINFO bi;
|
|
|
|
VOID *ppvBits;
|
|
|
|
HBITMAP bitmap;
|
|
|
|
|
|
|
|
ZeroMemory(&bi, sizeof (BITMAPINFO));
|
|
|
|
bi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
|
|
|
|
bi.bmiHeader.biWidth = (LONG) dx;
|
|
|
|
bi.bmiHeader.biHeight = -((LONG) dy); // negative height to force top-down drawing;
|
|
|
|
bi.bmiHeader.biPlanes = 1;
|
|
|
|
bi.bmiHeader.biBitCount = 32;
|
|
|
|
bi.bmiHeader.biCompression = BI_RGB;
|
|
|
|
bi.bmiHeader.biSizeImage = (DWORD) (dx * dy * 4);
|
|
|
|
bitmap = CreateDIBSection(NULL, &bi, DIB_RGB_COLORS, &ppvBits, 0, 0);
|
|
|
|
if (bitmap == NULL)
|
|
|
|
xpanic("error creating HBITMAP for unscaled ImageList image copy", GetLastError());
|
|
|
|
dotoARGB(i, (void *) ppvBits);
|
|
|
|
return bitmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
HIMAGELIST newImageList(int width, int height)
|
|
|
|
{
|
|
|
|
HIMAGELIST il;
|
|
|
|
|
|
|
|
// TODO does this strip alpha?
|
|
|
|
// sinni800 in irc.freenode.net/#go-nuts suggests our use of *image.RGBA makes this not so much of an issue
|
|
|
|
il = (*fv_ImageList_Create)(width, height, ILC_COLOR32, 20, 20); // should be reasonable
|
|
|
|
if (il == NULL)
|
|
|
|
xpanic("error creating image list", GetLastError());
|
|
|
|
return il;
|
|
|
|
}
|
|
|
|
|
2014-08-16 16:37:21 -05:00
|
|
|
void addImage(HIMAGELIST il, HWND hwnd, HBITMAP bitmap, int origwid, int oright, int width, int height)
|
2014-08-16 13:02:00 -05:00
|
|
|
{
|
|
|
|
BOOL wasScaled = FALSE;
|
2014-08-16 16:37:21 -05:00
|
|
|
HDC winDC, scaledDC, origDC;
|
2014-08-16 13:02:00 -05:00
|
|
|
HBITMAP scaled;
|
|
|
|
HBITMAP prevscaled, prevorig;
|
|
|
|
|
|
|
|
// first we need to scale the bitmap
|
|
|
|
if (origwid == width && oright == height) {
|
|
|
|
scaled = bitmap;
|
|
|
|
goto noscale;
|
|
|
|
}
|
|
|
|
wasScaled = TRUE;
|
2014-08-16 16:37:21 -05:00
|
|
|
winDC = GetDC(hwnd);
|
|
|
|
if (winDC == NULL)
|
|
|
|
xpanic("error getting DC for window", GetLastError());
|
|
|
|
origDC = CreateCompatibleDC(winDC);
|
|
|
|
if (winDC == NULL)
|
|
|
|
xpanic("error getting DC for original ImageList bitmap", GetLastError());
|
|
|
|
prevorig = SelectObject(origDC, bitmap);
|
|
|
|
if (prevorig == NULL)
|
|
|
|
xpanic("error selecting original ImageList bitmap into DC", GetLastError());
|
|
|
|
scaledDC = CreateCompatibleDC(origDC);
|
2014-08-16 13:02:00 -05:00
|
|
|
if (scaledDC == NULL)
|
2014-08-16 16:37:21 -05:00
|
|
|
xpanic("error getting DC for scaled ImageList bitmap", GetLastError());
|
|
|
|
scaled = CreateCompatibleBitmap(origDC, width, height);
|
2014-08-16 13:02:00 -05:00
|
|
|
if (scaled == NULL)
|
|
|
|
xpanic("error creating scaled ImageList bitmap", GetLastError());
|
|
|
|
prevscaled = SelectObject(scaledDC, scaled);
|
|
|
|
if (prevscaled == NULL)
|
2014-08-16 16:37:21 -05:00
|
|
|
xpanic("error selecting scaled ImageList bitmap into DC", GetLastError());
|
2014-08-16 13:02:00 -05:00
|
|
|
if (StretchBlt(scaledDC, 0, 0, width, height,
|
|
|
|
origDC, 0, 0, origwid, oright,
|
|
|
|
SRCCOPY) == 0)
|
|
|
|
xpanic("error scaling ImageList bitmap down", GetLastError());
|
|
|
|
if (SelectObject(origDC, prevorig) != bitmap)
|
2014-08-16 16:37:21 -05:00
|
|
|
xpanic("error selecting previous bitmap into original image's DC", GetLastError());
|
2014-08-16 13:02:00 -05:00
|
|
|
if (DeleteDC(origDC) == 0)
|
2014-08-16 16:37:21 -05:00
|
|
|
xpanic("error deleting original image's DC", GetLastError());
|
2014-08-16 13:02:00 -05:00
|
|
|
if (SelectObject(scaledDC, prevscaled) != scaled)
|
2014-08-16 16:37:21 -05:00
|
|
|
xpanic("error selecting previous bitmap into scaled image's DC", GetLastError());
|
2014-08-16 13:02:00 -05:00
|
|
|
if (DeleteDC(scaledDC) == 0)
|
2014-08-16 16:37:21 -05:00
|
|
|
xpanic("error deleting scaled image's DC", GetLastError());
|
|
|
|
if (DeleteDC(winDC) == 0)
|
|
|
|
xpanic("error deleting window DC", GetLastError());
|
2014-08-16 13:02:00 -05:00
|
|
|
|
|
|
|
noscale:
|
|
|
|
if ((*fv_ImageList_Add)(il, scaled, NULL) == -1)
|
|
|
|
xpanic("error adding ImageList image to image list", GetLastError());
|
|
|
|
if (wasScaled) // clean up
|
|
|
|
if (DeleteObject(scaled) == 0)
|
|
|
|
xpanic("error deleting scaled bitmap", GetLastError());
|
|
|
|
}
|
2014-08-16 16:37:21 -05:00
|
|
|
|
|
|
|
void applyImageList(HWND hwnd, UINT uMsg, WPARAM wParam, HIMAGELIST il)
|
|
|
|
{
|
|
|
|
if (SendMessageW(hwnd, uMsg, wParam, (LPARAM) il) == (LRESULT) NULL)
|
|
|
|
;// xpanic("error setting image list", GetLastError());
|
|
|
|
// TODO free old one here if any
|
|
|
|
}
|