comparison win/dw.c @ 1251:7809be48de7d

Enable GDI Plus on Windows to see how well it works. Had to include a bunch of stuff in the source... since the shipped headers are not compatible with C (only C++).
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 24 Oct 2011 03:10:27 +0000
parents ed2119fc210d
children 575663435272
comparison
equal deleted inserted replaced
1250:ed2119fc210d 1251:7809be48de7d
17 #include <stdlib.h> 17 #include <stdlib.h>
18 #include <string.h> 18 #include <string.h>
19 #include <stdio.h> 19 #include <stdio.h>
20 #include <process.h> 20 #include <process.h>
21 #include <time.h> 21 #include <time.h>
22 #ifdef GDIPLUS
23 #include <gdiplustypes.h>
24 #include <gdiplusflat.h>
25 #endif
26 #include "dw.h" 22 #include "dw.h"
27 #include "XBrowseForFolder.h" 23 #include "XBrowseForFolder.h"
24
25 #ifdef GDIPLUS
26 /* GDI+ Headers are not C compatible... so define what we need here instead */
27 struct GdiplusStartupInput
28 {
29 UINT32 GdiplusVersion;
30 void *DebugEventCallback;
31 BOOL SuppressBackgroundThread;
32 BOOL SuppressExternalCodecs;
33 };
34
35 typedef enum {
36 Ok = 0,
37 GenericError = 1,
38 InvalidParameter = 2,
39 OutOfMemory = 3,
40 ObjectBusy = 4,
41 InsufficientBuffer = 5,
42 NotImplemented = 6,
43 Win32Error = 7,
44 WrongState = 8,
45 Aborted = 9,
46 FileNotFound = 10,
47 ValueOverflow = 11,
48 AccessDenied = 12,
49 UnknownImageFormat = 13,
50 FontFamilyNotFound = 14,
51 FontStyleNotFound = 15,
52 NotTrueTypeFont = 16,
53 UnsupportedGdiplusVersion = 17,
54 GdiplusNotInitialized = 18,
55 PropertyNotFound = 19,
56 PropertyNotSupported = 20,
57 ProfileNotFound = 21
58 } Status;
59
60 Status WINAPI GdipCreateBitmapFromFile(const WCHAR* filename, void **bitmap);
61 Status WINAPI GdipCreateHBITMAPFromBitmap(void *bitmap, HBITMAP* hbmReturn, DWORD background);
62 Status WINAPI GdipCreateHICONFromBitmap(void *bitmap, HICON *hbmReturn);
63 Status WINAPI GdipDisposeImage(void *image);
64 Status WINAPI GdipGetImagePixelFormat(void *image, INT *format);
65 Status WINAPI GdiplusStartup(ULONG_PTR *token, const struct GdiplusStartupInput *input, void *output);
66 VOID WINAPI GdiplusShutdown(ULONG_PTR token);
67
68 /* Pixel format information */
69 #define PixelFormatIndexed 0x00010000
70 #define PixelFormatGDI 0x00020000
71 #define PixelFormatAlpha 0x00040000
72 #define PixelFormatPAlpha 0x00080000
73 #define PixelFormatExtended 0x00100000
74 #define PixelFormatCanonical 0x00200000
75
76 #define PixelFormatUndefined 0
77 #define PixelFormatDontCare 0
78
79 #define PixelFormat1bppIndexed (1 | ( 1 << 8) | PixelFormatIndexed | PixelFormatGDI)
80 #define PixelFormat4bppIndexed (2 | ( 4 << 8) | PixelFormatIndexed | PixelFormatGDI)
81 #define PixelFormat8bppIndexed (3 | ( 8 << 8) | PixelFormatIndexed | PixelFormatGDI)
82 #define PixelFormat16bppGrayScale (4 | (16 << 8) | PixelFormatExtended)
83 #define PixelFormat16bppRGB555 (5 | (16 << 8) | PixelFormatGDI)
84 #define PixelFormat16bppRGB565 (6 | (16 << 8) | PixelFormatGDI)
85 #define PixelFormat16bppARGB1555 (7 | (16 << 8) | PixelFormatAlpha | PixelFormatGDI)
86 #define PixelFormat24bppRGB (8 | (24 << 8) | PixelFormatGDI)
87 #define PixelFormat32bppRGB (9 | (32 << 8) | PixelFormatGDI)
88 #define PixelFormat32bppARGB (10 | (32 << 8) | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical)
89 #define PixelFormat32bppPARGB (11 | (32 << 8) | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatGDI)
90 #define PixelFormat48bppRGB (12 | (48 << 8) | PixelFormatExtended)
91 #define PixelFormat64bppARGB (13 | (64 << 8) | PixelFormatAlpha | PixelFormatCanonical | PixelFormatExtended)
92 #define PixelFormat64bppPARGB (14 | (64 << 8) | PixelFormatAlpha | PixelFormatPAlpha | PixelFormatExtended)
93 #define PixelFormat32bppCMYK (15 | (32 << 8))
94
95 /* Token to the GDI+ Instance */
96 ULONG_PTR gdiplusToken;
97 #endif
28 98
29 /* 99 /*
30 * MinGW (as at 3.2.3) doesn't have MIM_MENUDATA 100 * MinGW (as at 3.2.3) doesn't have MIM_MENUDATA
31 * so #define it here 101 * so #define it here
32 */ 102 */
315 ".tiff", 385 ".tiff",
316 ".bmp" 386 ".bmp"
317 }; 387 };
318 388
319 /* Section for loading files of types besides BMP and ICO and return HBITMAP or HICON */ 389 /* Section for loading files of types besides BMP and ICO and return HBITMAP or HICON */
320 GpBitmap *_dw_load_gpbitmap( char *filename ) 390 void *_dw_load_gpbitmap( char *filename )
321 { 391 {
322 int found_ext = 0,i, wclen = (strlen(filename) + 6) * sizeof(wchar_t); 392 int found_ext = 0,i, wclen = (strlen(filename) + 6) * sizeof(wchar_t);
323 char *file = _alloca(strlen(filename) + 6); 393 char *file = _alloca(strlen(filename) + 6);
324 wchar_t *wfile = _alloca(wclen); 394 wchar_t *wfile = _alloca(wclen);
325 GpBitmap *image; 395 void *image;
326 396
327 /* Try various extentions */ 397 /* Try various extentions */
328 for ( i = 0; i < NUM_EXTS; i++ ) 398 for ( i = 0; i < NUM_EXTS; i++ )
329 { 399 {
330 strcpy( file, filename ); 400 strcpy( file, filename );
331 strcat( file, image_exts[i] ); 401 strcat( file, image_exts[i] );
332 if ( access( file, 04 ) == 0 ) 402 if ( access( file, 04 ) == 0 )
333 { 403 {
334 /* Convert to wide format */ 404 /* Convert to wide format */
335 MultiByteToWideChar(CP_ACP, 0, file, strlen(file), wfile, wclen); 405 MultiByteToWideChar(CP_ACP, 0, file, strlen(file), wfile, wclen);
336 if(GdipCreateBitmapFromFile(wfile, &image)) 406 if(!GdipCreateBitmapFromFile(wfile, &image))
337 return image; 407 return image;
338 } 408 }
339 } 409 }
340 return NULL; 410 return NULL;
341 } 411 }
342 412
343 /* Try to load the appropriate image and return the HBITMAP handle */ 413 /* Try to load the appropriate image and return the HBITMAP handle */
344 HBITMAP _dw_load_bitmap(char *filename, unsigned long *depth) 414 HBITMAP _dw_load_bitmap(char *filename, unsigned long *depth)
345 { 415 {
346 GpBitmap *bitmap = _dw_load_gpbitmap(filename); 416 void *bitmap = _dw_load_gpbitmap(filename);
347 if(bitmap) 417 if(bitmap)
348 { 418 {
349 HBITMAP hbm; 419 HBITMAP hbm = NULL;
350 420
351 if(GdipCreateHBITMAPFromBitmap(bitmap, &hbm, 0)) 421 if(!GdipCreateHBITMAPFromBitmap(bitmap, &hbm, 0))
352 { 422 {
353 if(depth) 423 if(depth)
354 { 424 {
355 /* TODO: Actually query the color depth here */ 425 INT pf;
356 *depth = 32; 426
427 /* Default to 0 */
428 *depth = 0;
429
430 /* Query the pixel format so we can determine the depth */
431 if(!GdipGetImagePixelFormat(bitmap, &pf))
432 {
433 switch(pf)
434 {
435 case PixelFormat1bppIndexed:
436 *depth = 1;
437 break;
438 case PixelFormat4bppIndexed:
439 *depth = 4;
440 break;
441 case PixelFormat8bppIndexed:
442 *depth = 8;
443 break;
444 case PixelFormat16bppGrayScale:
445 case PixelFormat16bppRGB555:
446 case PixelFormat16bppRGB565:
447 case PixelFormat16bppARGB1555:
448 *depth = 16;
449 break;
450 case PixelFormat24bppRGB:
451 *depth = 24;
452 break;
453 case PixelFormat32bppRGB:
454 case PixelFormat32bppARGB:
455 case PixelFormat32bppPARGB:
456 case PixelFormat32bppCMYK:
457 *depth = 32;
458 break;
459 case PixelFormat48bppRGB:
460 *depth = 48;
461 break;
462 case PixelFormat64bppARGB:
463 case PixelFormat64bppPARGB:
464 *depth = 64;
465 break;
466 }
467 }
357 } 468 }
358 return hbm;
359 } 469 }
470 GdipDisposeImage(bitmap);
471 return hbm;
360 } 472 }
361 return NULL; 473 return NULL;
362 } 474 }
363 475
364 /* Try to load the appropriate image and return the HICON handle */ 476 /* Try to load the appropriate image and return the HICON handle */
365 HICON _dw_load_icon(char *filename) 477 HICON _dw_load_icon(char *filename)
366 { 478 {
367 GpBitmap *bitmap = _dw_load_gpbitmap(filename); 479 void *bitmap = _dw_load_gpbitmap(filename);
368 if(bitmap) 480 if(bitmap)
369 { 481 {
370 HICON hicon; 482 HICON hicon = NULL;
371 483
372 if(GdipCreateHICONFromBitmap(bitmap, &hicon)) 484 GdipCreateHICONFromBitmap(bitmap, &hicon);
373 return hicon; 485 GdipDisposeImage(bitmap);
486 return hicon;
374 } 487 }
375 return NULL; 488 return NULL;
376 } 489 }
377 #endif 490 #endif
378 491
3505 WNDCLASS wc; 3618 WNDCLASS wc;
3506 int z; 3619 int z;
3507 INITCOMMONCONTROLSEX icc; 3620 INITCOMMONCONTROLSEX icc;
3508 char *fname, *alttmpdir; 3621 char *fname, *alttmpdir;
3509 HFONT oldfont; 3622 HFONT oldfont;
3623 struct GdiplusStartupInput si;
3510 3624
3511 /* Initialize our thread local storage */ 3625 /* Initialize our thread local storage */
3512 _foreground = TlsAlloc(); 3626 _foreground = TlsAlloc();
3513 _background = TlsAlloc(); 3627 _background = TlsAlloc();
3514 _hPen = TlsAlloc(); 3628 _hPen = TlsAlloc();
3646 * and dw_screen-height() quicker, but to alos limit the 3760 * and dw_screen-height() quicker, but to alos limit the
3647 * default size of windows. 3761 * default size of windows.
3648 */ 3762 */
3649 screenx = GetSystemMetrics(SM_CXSCREEN); 3763 screenx = GetSystemMetrics(SM_CXSCREEN);
3650 screeny = GetSystemMetrics(SM_CYSCREEN); 3764 screeny = GetSystemMetrics(SM_CYSCREEN);
3765
3766 #ifdef GDIPLUS
3767 /* Setup GDI+ */
3768 si.GdiplusVersion = 1;
3769 si.DebugEventCallback = NULL;
3770 si.SuppressBackgroundThread = FALSE;
3771 si.SuppressExternalCodecs = FALSE;
3772 GdiplusStartup(&gdiplusToken, &si, NULL);
3773 #endif
3651 return 0; 3774 return 0;
3652 } 3775 }
3653 3776
3654 /* 3777 /*
3655 * Runs a message loop for Dynamic Windows. 3778 * Runs a message loop for Dynamic Windows.
8994 if ( fp != NULL ) 9117 if ( fp != NULL )
8995 { 9118 {
8996 fwrite( data, 1, len, fp ); 9119 fwrite( data, 1, len, fp );
8997 fclose( fp ); 9120 fclose( fp );
8998 #ifdef GDIPLUS 9121 #ifdef GDIPLUS
8999 pixmap->hbm = _dw_load_bitmap(file); 9122 pixmap->hbm = _dw_load_bitmap(file, NULL);
9000 #else 9123 #else
9001 pixmap->hbm = (HBITMAP)LoadImage( NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE ); 9124 pixmap->hbm = (HBITMAP)LoadImage( NULL, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE );
9002 pixmap->depth = _read_bitmap_header(file); 9125 pixmap->depth = _read_bitmap_header(file);
9003 #endif 9126 #endif
9004 } 9127 }