filechooser: use the thumbnails Explorer and Quick Look already have, on Windows and macOS

This commit is contained in:
Daemonratte 2026-10-16 09:12:16 +02:00
commit c2ff30b474
No known key found for this signature in database

View file

@ -32,6 +32,12 @@
#ifdef G_OS_WIN32
#include <windows.h>
#include <objbase.h>
#endif
#ifdef GDK_WINDOWING_QUARTZ
#include <dlfcn.h>
#include <ApplicationServices/ApplicationServices.h>
#import <Foundation/Foundation.h> /* libgtk is Objective-C there */
#endif
#ifdef __linux__
#include <sys/sysmacros.h>
@ -444,6 +450,249 @@ static const struct { const gchar *dir; gint size; } sizes[] = {
{ "normal", 128 }, { "large", 256 }, { "x-large", 512 }, { "xx-large", 1024 }
};
/* The picture of a thumbnail of the native file manager, at most @size
* big: one Explorer has in its cache (Vista and later), one of Quick Look
* (10.5 and later), which has its own cache */
#ifdef G_OS_WIN32
/* IShellItemImageFactory, declared here, as not all headers have it */
typedef struct _ImageFactory ImageFactory;
typedef struct
{
HRESULT (STDMETHODCALLTYPE *QueryInterface) (ImageFactory *, REFIID, void **);
ULONG (STDMETHODCALLTYPE *AddRef) (ImageFactory *);
ULONG (STDMETHODCALLTYPE *Release) (ImageFactory *);
HRESULT (STDMETHODCALLTYPE *GetImage) (ImageFactory *, SIZE, int, HBITMAP *);
} ImageFactoryVtbl;
struct _ImageFactory { const ImageFactoryVtbl *lpVtbl; };
#define THUMBNAIL_ONLY 0x08 /* SIIGBF_THUMBNAILONLY */
#define IN_CACHE_ONLY 0x10 /* SIIGBF_INCACHEONLY */
static const GUID iid_image_factory =
{ 0xbcc18b79, 0xba16, 0x442f, { 0x80, 0xc4, 0x8a, 0x59, 0xc3, 0x0c, 0x46, 0x3b } };
typedef HRESULT (WINAPI *CreateItemFunc) (PCWSTR, void *, REFIID, void **);
/* A 32-bit DIB, with alpha premultiplied, or none at all */
static GdkPixbuf *
pixbuf_of_bitmap (HBITMAP bitmap)
{
BITMAPINFOHEADER header;
GdkPixbuf *pixbuf = NULL;
guchar *bits, *row;
gboolean alpha = FALSE;
gint width, height, x, y;
BITMAP info;
HDC dc;
if (!GetObjectW (bitmap, sizeof info, &info) || info.bmWidth <= 0 || info.bmHeight == 0)
return NULL;
width = info.bmWidth;
height = ABS (info.bmHeight);
memset (&header, 0, sizeof header);
header.biSize = sizeof header;
header.biWidth = width;
header.biHeight = -height; /* top row first */
header.biPlanes = 1;
header.biBitCount = 32;
header.biCompression = BI_RGB;
bits = g_try_malloc ((gsize) width * height * 4);
if (bits == NULL)
return NULL;
dc = GetDC (NULL);
if (GetDIBits (dc, bitmap, 0, height, bits, (BITMAPINFO *) &header, DIB_RGB_COLORS) == height)
pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
ReleaseDC (NULL, dc);
for (x = 0; pixbuf && x < width * height; x++)
alpha |= bits[x * 4 + 3] != 0;
for (y = 0; pixbuf && y < height; y++)
{
guchar *src = bits + (gsize) y * width * 4;
row = gdk_pixbuf_get_pixels (pixbuf) + (gsize) y * gdk_pixbuf_get_rowstride (pixbuf);
for (x = 0; x < width; x++, src += 4, row += 4)
{
guint a = alpha ? src[3] : 255;
/* B, G, R, A to R, G, B, A, not premultiplied */
row[0] = a ? MIN (255, src[2] * 255 / a) : 0;
row[1] = a ? MIN (255, src[1] * 255 / a) : 0;
row[2] = a ? MIN (255, src[0] * 255 / a) : 0;
row[3] = a;
}
}
g_free (bits);
return pixbuf;
}
static GdkPixbuf *
native_thumbnail (Job *job)
{
static CreateItemFunc create_item;
static gsize looked_up;
ImageFactory *factory;
GdkPixbuf *pixbuf = NULL;
wchar_t *wpath;
HRESULT init;
if (g_once_init_enter (&looked_up))
{
HMODULE shell = LoadLibraryW (L"shell32.dll");
if (shell)
create_item = (CreateItemFunc) GetProcAddress (shell, "SHCreateItemFromParsingName");
g_once_init_leave (&looked_up, 1);
}
if (create_item == NULL)
return NULL; /* Windows XP */
wpath = g_utf8_to_utf16 (job->path, -1, NULL, NULL, NULL);
if (wpath == NULL)
return NULL;
init = CoInitializeEx (NULL, COINIT_MULTITHREADED);
if (SUCCEEDED (create_item (wpath, NULL, &iid_image_factory, (void **) &factory)))
{
SIZE size = { job->thumbnail_size, job->thumbnail_size };
HBITMAP bitmap;
if (SUCCEEDED (factory->lpVtbl->GetImage (factory, size, THUMBNAIL_ONLY | IN_CACHE_ONLY, &bitmap)))
{
pixbuf = pixbuf_of_bitmap (bitmap);
DeleteObject (bitmap);
}
factory->lpVtbl->Release (factory);
}
if (SUCCEEDED (init))
CoUninitialize ();
g_free (wpath);
return pixbuf;
}
#elif defined (GDK_WINDOWING_QUARTZ)
typedef CGImageRef (*ThumbnailCreateFunc) (CFAllocatorRef, CFURLRef, CGSize, CFDictionaryRef);
static GdkPixbuf *
native_thumbnail (Job *job)
{
static ThumbnailCreateFunc create_thumbnail;
static gsize looked_up;
CGColorSpaceRef space;
CGContextRef context;
GdkPixbuf *pixbuf;
CGImageRef image;
CFURLRef url;
gint width, height, x, y;
if (g_once_init_enter (&looked_up))
{
void *quick_look = dlopen ("/System/Library/Frameworks/QuickLook.framework/QuickLook",
RTLD_LAZY | RTLD_LOCAL);
if (quick_look)
create_thumbnail = (ThumbnailCreateFunc) dlsym (quick_look, "QLThumbnailImageCreate");
g_once_init_leave (&looked_up, 1);
}
if (create_thumbnail == NULL)
return NULL; /* before 10.5 */
url = CFURLCreateFromFileSystemRepresentation (NULL, (const UInt8 *) job->path,
strlen (job->path), false);
if (url == NULL)
return NULL;
{
/* Quick Look autoreleases; this thread of the pool has no pool of
* its own */
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
image = create_thumbnail (kCFAllocatorDefault, url,
CGSizeMake (job->thumbnail_size, job->thumbnail_size), NULL);
[pool release];
}
CFRelease (url);
if (image == NULL)
return NULL;
width = CGImageGetWidth (image);
height = CGImageGetHeight (image);
pixbuf = width > 0 && height > 0 ?
gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height) : NULL;
if (pixbuf)
{
/* drawn as R, G, B, A, premultiplied */
gdk_pixbuf_fill (pixbuf, 0);
space = CGColorSpaceCreateDeviceRGB ();
context = CGBitmapContextCreate (gdk_pixbuf_get_pixels (pixbuf), width, height, 8,
gdk_pixbuf_get_rowstride (pixbuf), space,
kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease (space);
if (context)
{
CGContextDrawImage (context, CGRectMake (0, 0, width, height), image);
CGContextRelease (context);
for (y = 0; y < height; y++)
{
guchar *p = gdk_pixbuf_get_pixels (pixbuf) + (gsize) y * gdk_pixbuf_get_rowstride (pixbuf);
for (x = 0; x < width; x++, p += 4)
if (p[3] && p[3] < 255)
{
p[0] = MIN (255, p[0] * 255 / p[3]);
p[1] = MIN (255, p[1] * 255 / p[3]);
p[2] = MIN (255, p[2] * 255 / p[3]);
}
}
}
else
{
g_object_unref (pixbuf);
pixbuf = NULL;
}
}
CGImageRelease (image);
return pixbuf;
}
#endif
#if defined (G_OS_WIN32) || defined (GDK_WINDOWING_QUARTZ)
/* Not bigger than @size, keeping the shape */
static GdkPixbuf *
fit (GdkPixbuf *pixbuf,
gint size)
{
gint width = gdk_pixbuf_get_width (pixbuf), height = gdk_pixbuf_get_height (pixbuf);
GdkPixbuf *scaled;
if (width <= size && height <= size)
return pixbuf;
if (width > height)
{
height = MAX (1, (gint) ((gint64) height * size / width));
width = size;
}
else
{
width = MAX (1, (gint) ((gint64) width * size / height));
height = size;
}
scaled = gdk_pixbuf_scale_simple (pixbuf, width, height, GDK_INTERP_BILINEAR);
g_object_unref (pixbuf);
return scaled;
}
#endif
static gchar *
make_thumbnail (Job *job)
{
@ -489,6 +738,18 @@ make_thumbnail (Job *job)
if (thumbnail_matches (fail, job, NULL, NULL) || on_remote_filesystem (job))
goto none;
#if defined (G_OS_WIN32) || defined (GDK_WINDOWING_QUARTZ)
pixbuf = native_thumbnail (job);
if (pixbuf)
{
pixbuf = fit (pixbuf, job->thumbnail_size);
if (!save (pixbuf, path, job, 0, 0))
goto none;
g_object_unref (pixbuf);
goto done;
}
#endif
format = gdk_pixbuf_get_file_info (job->path, &width, &height);
if (format == NULL || width <= 0 || height <= 0)
{