-
Notifications
You must be signed in to change notification settings - Fork 0
/
shimgvw.c
1803 lines (1525 loc) · 48.7 KB
/
shimgvw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PROJECT: ReactOS Picture and Fax Viewer
* LICENSE: GPL-2.0 (https://spdx.org/licenses/GPL-2.0)
* PURPOSE: Image file browsing and manipulation
* COPYRIGHT: Copyright Dmitry Chapyshev (dmitry@reactos.org)
* Copyright 2018-2023 Katayama Hirofumi MZ (katayama.hirofumi.mz@gmail.com)
*/
#include "shimgvw.h"
#include <windowsx.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shlobj.h>
#include <shellapi.h>
/* Toolbar image size */
#define TB_IMAGE_WIDTH 16
#define TB_IMAGE_HEIGHT 16
/* Slide show timer */
#define SLIDESHOW_TIMER_ID 0xFACE
#define SLIDESHOW_TIMER_INTERVAL 5000 /* 5 seconds */
HINSTANCE g_hInstance = NULL;
HWND g_hMainWnd = NULL;
HWND g_hwndFullscreen = NULL;
SHIMGVW_FILENODE * g_pCurrentFile = NULL;
GpImage * g_pImage = NULL;
SHIMGVW_SETTINGS g_Settings;
static const UINT s_ZoomSteps[] =
{
5, 10, 25, 50, 100, 200, 300, 500, 1000, 2000, 4000
};
#define MIN_ZOOM s_ZoomSteps[0]
#define MAX_ZOOM s_ZoomSteps[_countof(s_ZoomSteps) - 1]
/* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
#define DEFINE_BTN_INFO(_name) \
{ TBICON_##_name, IDC_##_name, TBSTATE_ENABLED, BTNS_BUTTON, {0}, 0, 0 }
#define DEFINE_BTN_SEPARATOR \
{ -1, 0, TBSTATE_ENABLED, BTNS_SEP, {0}, 0, 0 }
/* ToolBar Buttons */
static const TBBUTTON s_Buttons[] =
{
DEFINE_BTN_INFO(PREV_PIC),
DEFINE_BTN_INFO(NEXT_PIC),
DEFINE_BTN_SEPARATOR,
DEFINE_BTN_INFO(BEST_FIT),
DEFINE_BTN_INFO(REAL_SIZE),
DEFINE_BTN_INFO(SLIDE_SHOW),
DEFINE_BTN_SEPARATOR,
DEFINE_BTN_INFO(ZOOM_IN),
DEFINE_BTN_INFO(ZOOM_OUT),
DEFINE_BTN_SEPARATOR,
DEFINE_BTN_INFO(ROT_CLOCKW),
DEFINE_BTN_INFO(ROT_COUNCW),
DEFINE_BTN_SEPARATOR,
DEFINE_BTN_INFO(ROT_CWSAVE),
DEFINE_BTN_INFO(ROT_CCWSAVE),
DEFINE_BTN_SEPARATOR,
DEFINE_BTN_INFO(DELETE),
DEFINE_BTN_INFO(PRINT),
DEFINE_BTN_INFO(SAVEAS),
DEFINE_BTN_INFO(MODIFY),
DEFINE_BTN_SEPARATOR,
DEFINE_BTN_INFO(HELP_TOC)
};
/* ToolBar Button configuration */
typedef struct
{
DWORD idb; /* Index to bitmap */
DWORD ids; /* Index to tooltip */
} TB_BUTTON_CONFIG;
#define DEFINE_BTN_CONFIG(_name) { IDB_##_name, IDS_TOOLTIP_##_name }
static const TB_BUTTON_CONFIG s_ButtonConfig[] =
{
DEFINE_BTN_CONFIG(PREV_PIC),
DEFINE_BTN_CONFIG(NEXT_PIC),
DEFINE_BTN_CONFIG(BEST_FIT),
DEFINE_BTN_CONFIG(REAL_SIZE),
DEFINE_BTN_CONFIG(SLIDE_SHOW),
DEFINE_BTN_CONFIG(ZOOM_IN),
DEFINE_BTN_CONFIG(ZOOM_OUT),
DEFINE_BTN_CONFIG(ROT_CLOCKW),
DEFINE_BTN_CONFIG(ROT_COUNCW),
DEFINE_BTN_CONFIG(ROT_CWSAVE),
DEFINE_BTN_CONFIG(ROT_CCWSAVE),
DEFINE_BTN_CONFIG(DELETE),
DEFINE_BTN_CONFIG(PRINT),
DEFINE_BTN_CONFIG(SAVEAS),
DEFINE_BTN_CONFIG(MODIFY),
DEFINE_BTN_CONFIG(HELP_TOC),
};
typedef struct tagPREVIEW_DATA
{
HWND m_hwnd;
HWND m_hwndZoom;
HWND m_hwndToolBar;
INT m_nZoomPercents;
ANIME m_Anime; /* Animation */
INT m_xScrollOffset;
INT m_yScrollOffset;
UINT m_nMouseDownMsg;
POINT m_ptOrigin;
IStream *m_pMemStream;
WCHAR m_szFile[MAX_PATH];
} PREVIEW_DATA, *PPREVIEW_DATA;
static inline PPREVIEW_DATA
Preview_GetData(HWND hwnd)
{
return (PPREVIEW_DATA)GetWindowLongPtrW(hwnd, GWLP_USERDATA);
}
static inline BOOL
Preview_IsMainWnd(HWND hwnd)
{
return hwnd == g_hMainWnd;
}
static VOID
Preview_RestartTimer(HWND hwnd)
{
if (!Preview_IsMainWnd(hwnd))
{
KillTimer(hwnd, SLIDESHOW_TIMER_ID);
SetTimer(hwnd, SLIDESHOW_TIMER_ID, SLIDESHOW_TIMER_INTERVAL, NULL);
}
}
static VOID
ZoomWnd_UpdateScroll(PPREVIEW_DATA pData, HWND hwnd, BOOL bResetPos)
{
RECT rcClient;
UINT ImageWidth, ImageHeight, ZoomedWidth, ZoomedHeight;
SCROLLINFO si;
BOOL bShowHorz, bShowVert;
if (bResetPos)
pData->m_xScrollOffset = pData->m_yScrollOffset = 0;
if (!g_pImage)
{
ShowScrollBar(hwnd, SB_BOTH, FALSE);
InvalidateRect(hwnd, NULL, TRUE);
return;
}
GdipGetImageWidth(g_pImage, &ImageWidth);
GdipGetImageHeight(g_pImage, &ImageHeight);
ZoomedWidth = (ImageWidth * pData->m_nZoomPercents) / 100;
ZoomedHeight = (ImageHeight * pData->m_nZoomPercents) / 100;
GetClientRect(hwnd, &rcClient);
bShowHorz = (rcClient.right < ZoomedWidth);
bShowVert = (rcClient.bottom < ZoomedHeight);
ShowScrollBar(hwnd, SB_HORZ, bShowHorz);
ShowScrollBar(hwnd, SB_VERT, bShowVert);
GetClientRect(hwnd, &rcClient);
ZeroMemory(&si, sizeof(si));
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
if (bShowHorz)
{
GetScrollInfo(hwnd, SB_HORZ, &si);
si.nPage = rcClient.right;
si.nMin = 0;
si.nMax = ZoomedWidth;
si.nPos = (ZoomedWidth - rcClient.right) / 2 + pData->m_xScrollOffset;
si.nPos = max(min(si.nPos, si.nMax - (INT)si.nPage), si.nMin);
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
pData->m_xScrollOffset = si.nPos - (ZoomedWidth - rcClient.right) / 2;
}
else
{
pData->m_xScrollOffset = 0;
}
if (bShowVert)
{
GetScrollInfo(hwnd, SB_VERT, &si);
si.nPage = rcClient.bottom;
si.nMin = 0;
si.nMax = ZoomedHeight;
si.nPos = (ZoomedHeight - rcClient.bottom) / 2 + pData->m_yScrollOffset;
si.nPos = max(min(si.nPos, si.nMax - (INT)si.nPage), si.nMin);
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
pData->m_yScrollOffset = si.nPos - (ZoomedHeight - rcClient.bottom) / 2;
}
else
{
pData->m_yScrollOffset = 0;
}
InvalidateRect(hwnd, NULL, TRUE);
}
static VOID
Preview_UpdateZoom(PPREVIEW_DATA pData, UINT NewZoom, BOOL bEnableBestFit, BOOL bEnableRealSize)
{
BOOL bEnableZoomIn, bEnableZoomOut;
HWND hToolBar = pData->m_hwndToolBar;
pData->m_nZoomPercents = NewZoom;
/* Check if a zoom button of the toolbar must be grayed */
bEnableZoomIn = (NewZoom < MAX_ZOOM);
bEnableZoomOut = (NewZoom > MIN_ZOOM);
/* Update toolbar buttons */
PostMessageW(hToolBar, TB_ENABLEBUTTON, IDC_ZOOM_OUT, bEnableZoomOut);
PostMessageW(hToolBar, TB_ENABLEBUTTON, IDC_ZOOM_IN, bEnableZoomIn);
PostMessageW(hToolBar, TB_ENABLEBUTTON, IDC_BEST_FIT, bEnableBestFit);
PostMessageW(hToolBar, TB_ENABLEBUTTON, IDC_REAL_SIZE, bEnableRealSize);
/* Redraw the display window */
InvalidateRect(pData->m_hwndZoom, NULL, TRUE);
/* Restart timer if necessary */
Preview_RestartTimer(pData->m_hwnd);
/* Update scroll info */
ZoomWnd_UpdateScroll(pData, pData->m_hwndZoom, FALSE);
}
static VOID
Preview_ZoomInOrOut(PPREVIEW_DATA pData, BOOL bZoomIn)
{
UINT i, NewZoom;
if (g_pImage == NULL)
return;
if (bZoomIn) /* zoom in */
{
/* find next step */
for (i = 0; i < _countof(s_ZoomSteps); ++i)
{
if (pData->m_nZoomPercents < s_ZoomSteps[i])
break;
}
NewZoom = ((i >= _countof(s_ZoomSteps)) ? MAX_ZOOM : s_ZoomSteps[i]);
}
else /* zoom out */
{
/* find previous step */
for (i = _countof(s_ZoomSteps); i > 0; )
{
--i;
if (s_ZoomSteps[i] < pData->m_nZoomPercents)
break;
}
NewZoom = ((i < 0) ? MIN_ZOOM : s_ZoomSteps[i]);
}
/* Update toolbar and refresh screen */
Preview_UpdateZoom(pData, NewZoom, TRUE, TRUE);
}
static VOID
Preview_ResetZoom(PPREVIEW_DATA pData)
{
RECT Rect;
UINT ImageWidth, ImageHeight, NewZoom;
if (g_pImage == NULL)
return;
/* get disp window size and image size */
GetClientRect(pData->m_hwndZoom, &Rect);
GdipGetImageWidth(g_pImage, &ImageWidth);
GdipGetImageHeight(g_pImage, &ImageHeight);
/* compare two aspect rates. same as
(ImageHeight / ImageWidth < Rect.bottom / Rect.right) in real */
if (ImageHeight * Rect.right < Rect.bottom * ImageWidth)
{
if (Rect.right < ImageWidth)
{
/* it's large, shrink it */
NewZoom = (Rect.right * 100) / ImageWidth;
}
else
{
/* it's small. show as original size */
NewZoom = 100;
}
}
else
{
if (Rect.bottom < ImageHeight)
{
/* it's large, shrink it */
NewZoom = (Rect.bottom * 100) / ImageHeight;
}
else
{
/* it's small. show as original size */
NewZoom = 100;
}
}
Preview_UpdateZoom(pData, NewZoom, FALSE, TRUE);
}
static VOID
Preview_UpdateTitle(PPREVIEW_DATA pData, LPCWSTR FileName)
{
WCHAR szText[MAX_PATH + 100];
LPWSTR pchFileTitle;
LoadStringW(g_hInstance, IDS_APPTITLE, szText, _countof(szText));
pchFileTitle = PathFindFileNameW(FileName);
if (pchFileTitle && *pchFileTitle)
{
StringCchCatW(szText, _countof(szText), L" - ");
StringCchCatW(szText, _countof(szText), pchFileTitle);
}
SetWindowTextW(pData->m_hwnd, szText);
}
static VOID
Preview_pFreeImage(PPREVIEW_DATA pData)
{
Anime_FreeInfo(&pData->m_Anime);
if (g_pImage)
{
GdipDisposeImage(g_pImage);
g_pImage = NULL;
}
if (pData->m_pMemStream)
{
pData->m_pMemStream->lpVtbl->Release(pData->m_pMemStream);
pData->m_pMemStream = NULL;
}
pData->m_szFile[0] = UNICODE_NULL;
}
IStream* MemStreamFromFile(LPCWSTR pszFileName)
{
HANDLE hFile;
DWORD dwFileSize, dwRead;
LPBYTE pbMemFile = NULL;
IStream *pStream;
hFile = CreateFileW(pszFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return NULL;
dwFileSize = GetFileSize(hFile, NULL);
pbMemFile = QuickAlloc(dwFileSize, FALSE);
if (!dwFileSize || (dwFileSize == INVALID_FILE_SIZE) || !pbMemFile)
{
CloseHandle(hFile);
return NULL;
}
if (!ReadFile(hFile, pbMemFile, dwFileSize, &dwRead, NULL) || (dwRead != dwFileSize))
{
QuickFree(pbMemFile);
CloseHandle(hFile);
return NULL;
}
CloseHandle(hFile);
#ifdef __RSHIMGVW__
{
HINSTANCE hSHLWAPI = LoadLibraryA("shlwapi");
FN_SHCreateMemStream fn = (FN_SHCreateMemStream)GetProcAddress(hSHLWAPI, MAKEINTRESOURCEA(12));
pStream = fn(pbMemFile, dwFileSize);
FreeLibrary(hSHLWAPI);
}
#else
pStream = SHCreateMemStream(pbMemFile, dwFileSize);
#endif
QuickFree(pbMemFile);
return pStream;
}
static VOID
Preview_pLoadImage(PPREVIEW_DATA pData, LPCWSTR szOpenFileName)
{
Preview_pFreeImage(pData);
pData->m_pMemStream = MemStreamFromFile(szOpenFileName);
if (!pData->m_pMemStream)
{
DPRINT1("MemStreamFromFile() failed\n");
Preview_UpdateTitle(pData, NULL);
return;
}
/* NOTE: GdipLoadImageFromFile locks the file.
Avoid file locking by using GdipLoadImageFromStream and memory stream. */
GdipLoadImageFromStream(pData->m_pMemStream, &g_pImage);
if (!g_pImage)
{
DPRINT1("GdipLoadImageFromStream() failed\n");
Preview_pFreeImage(pData);
Preview_UpdateTitle(pData, NULL);
return;
}
Anime_LoadInfo(&pData->m_Anime);
SHAddToRecentDocs(SHARD_PATHW, szOpenFileName);
GetFullPathNameW(szOpenFileName, _countof(pData->m_szFile), pData->m_szFile, NULL);
/* Reset zoom and redraw display */
Preview_ResetZoom(pData);
Preview_UpdateTitle(pData, szOpenFileName);
}
static VOID
Preview_pLoadImageFromNode(PPREVIEW_DATA pData, SHIMGVW_FILENODE *pNode)
{
Preview_pLoadImage(pData, (pNode ? pNode->FileName : NULL));
}
static BOOL
Preview_pSaveImage(PPREVIEW_DATA pData, LPCWSTR pszFile)
{
ImageCodecInfo *codecInfo;
GUID rawFormat;
UINT j, num, nFilterIndex, size;
BOOL ret = FALSE;
if (g_pImage == NULL)
return FALSE;
GdipGetImageEncodersSize(&num, &size);
codecInfo = QuickAlloc(size, FALSE);
if (!codecInfo)
{
DPRINT1("QuickAlloc() failed in pSaveImage()\n");
return FALSE;
}
GdipGetImageEncoders(num, size, codecInfo);
GdipGetImageRawFormat(g_pImage, &rawFormat);
if (IsEqualGUID(&rawFormat, &ImageFormatMemoryBMP))
rawFormat = ImageFormatBMP;
nFilterIndex = 0;
for (j = 0; j < num; ++j)
{
if (IsEqualGUID(&rawFormat, &codecInfo[j].FormatID))
{
nFilterIndex = j + 1;
break;
}
}
Anime_Pause(&pData->m_Anime);
ret = (nFilterIndex > 0) &&
(GdipSaveImageToFile(g_pImage, pszFile, &codecInfo[nFilterIndex - 1].Clsid, NULL) == Ok);
if (!ret)
DPRINT1("GdipSaveImageToFile() failed\n");
Anime_Start(&pData->m_Anime, 0);
QuickFree(codecInfo);
return ret;
}
static VOID
Preview_pSaveImageAs(PPREVIEW_DATA pData)
{
OPENFILENAMEW sfn;
ImageCodecInfo *codecInfo;
WCHAR szSaveFileName[MAX_PATH];
WCHAR *szFilterMask;
GUID rawFormat;
UINT num, size, j;
size_t sizeRemain;
WCHAR *c;
HWND hwnd = pData->m_hwnd;
if (g_pImage == NULL)
return;
GdipGetImageEncodersSize(&num, &size);
codecInfo = QuickAlloc(size, FALSE);
if (!codecInfo)
{
DPRINT1("QuickAlloc() failed in pSaveImageAs()\n");
return;
}
GdipGetImageEncoders(num, size, codecInfo);
GdipGetImageRawFormat(g_pImage, &rawFormat);
if (IsEqualGUID(&rawFormat, &ImageFormatMemoryBMP))
rawFormat = ImageFormatBMP;
sizeRemain = 0;
for (j = 0; j < num; ++j)
{
// Every pair needs space for the Description, twice the Extensions, 1 char for the space, 2 for the braces and 2 for the NULL terminators.
sizeRemain = sizeRemain + (((wcslen(codecInfo[j].FormatDescription) + (wcslen(codecInfo[j].FilenameExtension) * 2) + 5) * sizeof(WCHAR)));
}
/* Add two more chars for the last terminator */
sizeRemain += (sizeof(WCHAR) * 2);
szFilterMask = QuickAlloc(sizeRemain, FALSE);
if (!szFilterMask)
{
DPRINT1("cannot allocate memory for filter mask in pSaveImageAs()");
QuickFree(codecInfo);
return;
}
ZeroMemory(szSaveFileName, sizeof(szSaveFileName));
ZeroMemory(szFilterMask, sizeRemain);
ZeroMemory(&sfn, sizeof(sfn));
sfn.lStructSize = sizeof(sfn);
sfn.hwndOwner = hwnd;
sfn.lpstrFile = szSaveFileName;
sfn.lpstrFilter = szFilterMask;
sfn.nMaxFile = _countof(szSaveFileName);
sfn.Flags = OFN_EXPLORER | OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY;
sfn.lpstrDefExt = L"png";
c = szFilterMask;
for (j = 0; j < num; ++j)
{
StringCbPrintfExW(c, sizeRemain, &c, &sizeRemain, 0, L"%ls (%ls)", codecInfo[j].FormatDescription, codecInfo[j].FilenameExtension);
/* Skip the NULL character */
c++;
sizeRemain -= sizeof(*c);
StringCbPrintfExW(c, sizeRemain, &c, &sizeRemain, 0, L"%ls", codecInfo[j].FilenameExtension);
/* Skip the NULL character */
c++;
sizeRemain -= sizeof(*c);
if (IsEqualGUID(&rawFormat, &codecInfo[j].FormatID))
{
sfn.nFilterIndex = j + 1;
}
}
if (GetSaveFileNameW(&sfn) && sfn.nFilterIndex > 0)
{
Anime_Pause(&pData->m_Anime);
if (GdipSaveImageToFile(g_pImage, szSaveFileName, &codecInfo[sfn.nFilterIndex - 1].Clsid, NULL) != Ok)
{
DPRINT1("GdipSaveImageToFile() failed\n");
}
Anime_Start(&pData->m_Anime, 0);
}
QuickFree(szFilterMask);
QuickFree(codecInfo);
}
static VOID
Preview_pPrintImage(PPREVIEW_DATA pData)
{
/* FIXME */
}
static VOID
Preview_UpdateUI(PPREVIEW_DATA pData)
{
BOOL bEnable = (g_pImage != NULL);
PostMessageW(pData->m_hwndToolBar, TB_ENABLEBUTTON, IDC_SAVEAS, bEnable);
PostMessageW(pData->m_hwndToolBar, TB_ENABLEBUTTON, IDC_PRINT, bEnable);
}
static VOID
Preview_UpdateImage(PPREVIEW_DATA pData)
{
if (!Preview_IsMainWnd(pData->m_hwnd))
Preview_ResetZoom(pData);
ZoomWnd_UpdateScroll(pData, pData->m_hwndZoom, TRUE);
}
static SHIMGVW_FILENODE*
pBuildFileList(LPCWSTR szFirstFile)
{
HANDLE hFindHandle;
WCHAR *extension;
WCHAR szSearchPath[MAX_PATH];
WCHAR szSearchMask[MAX_PATH];
WCHAR szFileTypes[MAX_PATH];
WIN32_FIND_DATAW findData;
SHIMGVW_FILENODE *currentNode = NULL;
SHIMGVW_FILENODE *root = NULL;
SHIMGVW_FILENODE *conductor = NULL;
ImageCodecInfo *codecInfo;
UINT num;
UINT size;
UINT j;
StringCbCopyW(szSearchPath, sizeof(szSearchPath), szFirstFile);
PathRemoveFileSpecW(szSearchPath);
GdipGetImageDecodersSize(&num, &size);
codecInfo = QuickAlloc(size, FALSE);
if (!codecInfo)
{
DPRINT1("QuickAlloc() failed in pLoadFileList()\n");
return NULL;
}
GdipGetImageDecoders(num, size, codecInfo);
root = QuickAlloc(sizeof(SHIMGVW_FILENODE), FALSE);
if (!root)
{
DPRINT1("QuickAlloc() failed in pLoadFileList()\n");
QuickFree(codecInfo);
return NULL;
}
conductor = root;
for (j = 0; j < num; ++j)
{
StringCbCopyW(szFileTypes, sizeof(szFileTypes), codecInfo[j].FilenameExtension);
extension = wcstok(szFileTypes, L";");
while (extension != NULL)
{
PathCombineW(szSearchMask, szSearchPath, extension);
hFindHandle = FindFirstFileW(szSearchMask, &findData);
if (hFindHandle != INVALID_HANDLE_VALUE)
{
do
{
PathCombineW(conductor->FileName, szSearchPath, findData.cFileName);
// compare the name of the requested file with the one currently found.
// if the name matches, the current node is returned by the function.
if (_wcsicmp(szFirstFile, conductor->FileName) == 0)
{
currentNode = conductor;
}
conductor->Next = QuickAlloc(sizeof(SHIMGVW_FILENODE), FALSE);
// if QuickAlloc fails, make circular what we have and return it
if (!conductor->Next)
{
DPRINT1("QuickAlloc() failed in pLoadFileList()\n");
conductor->Next = root;
root->Prev = conductor;
FindClose(hFindHandle);
QuickFree(codecInfo);
return conductor;
}
conductor->Next->Prev = conductor;
conductor = conductor->Next;
}
while (FindNextFileW(hFindHandle, &findData) != 0);
FindClose(hFindHandle);
}
extension = wcstok(NULL, L";");
}
}
// we now have a node too much in the list. In case the requested file was not found,
// we use this node to store the name of it, otherwise we free it.
if (currentNode == NULL)
{
StringCchCopyW(conductor->FileName, MAX_PATH, szFirstFile);
currentNode = conductor;
}
else
{
conductor = conductor->Prev;
QuickFree(conductor->Next);
}
// link the last node with the first one to make the list circular
conductor->Next = root;
root->Prev = conductor;
conductor = currentNode;
QuickFree(codecInfo);
return conductor;
}
static VOID
pFreeFileList(SHIMGVW_FILENODE *root)
{
SHIMGVW_FILENODE *conductor;
if (!root)
return;
root->Prev->Next = NULL;
root->Prev = NULL;
while (root)
{
conductor = root;
root = conductor->Next;
QuickFree(conductor);
}
}
static HBRUSH CreateCheckerBoardBrush(VOID)
{
static const CHAR pattern[] =
"\x28\x00\x00\x00\x10\x00\x00\x00\x10\x00\x00\x00\x01\x00\x04\x00\x00\x00"
"\x00\x00\x80\x00\x00\x00\x23\x2E\x00\x00\x23\x2E\x00\x00\x10\x00\x00\x00"
"\x00\x00\x00\x00\x99\x99\x99\x00\xCC\xCC\xCC\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11"
"\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00"
"\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00"
"\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11"
"\x11\x11\x00\x00\x00\x00\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00"
"\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11"
"\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11"
"\x00\x00\x00\x00\x11\x11\x11\x11\x00\x00\x00\x00\x11\x11\x11\x11";
return CreateDIBPatternBrushPt(pattern, DIB_RGB_COLORS);
}
static VOID
ZoomWnd_OnDraw(
PPREVIEW_DATA pData,
HDC hdc,
LPRECT prcPaint,
LPRECT prcClient)
{
GpGraphics *graphics;
INT ZoomedWidth, ZoomedHeight;
RECT rect, rcClient = *prcClient;
HDC hdcMem;
HBRUSH hBrush;
HPEN hPen;
HGDIOBJ hbrOld, hbmOld, hPenOld;
UINT uFlags;
HBITMAP hbmMem;
SIZE paintSize = { prcPaint->right - prcPaint->left, prcPaint->bottom - prcPaint->top };
COLORREF color0, color1;
GpImageAttributes *imageAttributes;
/* We use a memory bitmap to reduce flickering */
hdcMem = CreateCompatibleDC(hdc);
hbmMem = CreateCompatibleBitmap(hdc, paintSize.cx, paintSize.cy);
hbmOld = SelectObject(hdcMem, hbmMem);
/* Choose colors */
if (Preview_IsMainWnd(pData->m_hwnd))
{
color0 = GetSysColor(COLOR_WINDOW);
color1 = GetSysColor(COLOR_WINDOWTEXT);
}
else
{
color0 = RGB(0, 0, 0);
color1 = RGB(255, 255, 255);
}
hBrush = CreateSolidBrush(color0);
SetBkColor(hdcMem, color0);
hPen = CreatePen(PS_SOLID, 1, color1);
SetTextColor(hdcMem, color1);
/* Fill background */
SetRect(&rect, 0, 0, paintSize.cx, paintSize.cy);
FillRect(hdcMem, &rect, hBrush);
DeleteObject(hBrush);
if (g_pImage == NULL)
{
WCHAR szText[128];
LoadStringW(g_hInstance, IDS_NOPREVIEW, szText, _countof(szText));
SelectObject(hdcMem, GetStockFont(DEFAULT_GUI_FONT));
OffsetRect(&rcClient, -prcPaint->left, -prcPaint->top);
DrawTextW(hdcMem, szText, -1, &rcClient, DT_SINGLELINE | DT_CENTER | DT_VCENTER |
DT_NOPREFIX);
}
else
{
UINT ImageWidth, ImageHeight;
GdipGetImageWidth(g_pImage, &ImageWidth);
GdipGetImageHeight(g_pImage, &ImageHeight);
ZoomedWidth = (ImageWidth * pData->m_nZoomPercents) / 100;
ZoomedHeight = (ImageHeight * pData->m_nZoomPercents) / 100;
GdipCreateFromHDC(hdcMem, &graphics);
if (!graphics)
{
DPRINT1("error: GdipCreateFromHDC\n");
return;
}
GdipGetImageFlags(g_pImage, &uFlags);
if (pData->m_nZoomPercents % 100 == 0)
{
GdipSetInterpolationMode(graphics, InterpolationModeNearestNeighbor);
GdipSetSmoothingMode(graphics, SmoothingModeNone);
}
else
{
GdipSetInterpolationMode(graphics, InterpolationModeHighQualityBilinear);
GdipSetSmoothingMode(graphics, SmoothingModeHighQuality);
}
rect.left = (rcClient.right - ZoomedWidth ) / 2;
rect.top = (rcClient.bottom - ZoomedHeight) / 2;
rect.right = rect.left + ZoomedWidth;
rect.bottom = rect.top + ZoomedHeight;
OffsetRect(&rect,
-prcPaint->left - pData->m_xScrollOffset,
-prcPaint->top - pData->m_yScrollOffset);
InflateRect(&rect, +1, +1); /* Add Rectangle() pen width */
/* Draw a rectangle. Fill by checker board if necessary */
if (uFlags & (ImageFlagsHasAlpha | ImageFlagsHasTranslucent))
hbrOld = SelectObject(hdcMem, CreateCheckerBoardBrush());
else
hbrOld = SelectObject(hdcMem, GetStockBrush(NULL_BRUSH));
hPenOld = SelectObject(hdcMem, hPen);
Rectangle(hdcMem, rect.left, rect.top, rect.right, rect.bottom);
DeleteObject(SelectObject(hdcMem, hbrOld));
DeleteObject(SelectObject(hdcMem, hPenOld));
InflateRect(&rect, -1, -1); /* Subtract Rectangle() pen width */
/* Image attributes are required to draw image correctly */
GdipCreateImageAttributes(&imageAttributes);
GdipSetImageAttributesWrapMode(imageAttributes, WrapModeTile,
GetBkColor(hdcMem) | 0xFF000000, TRUE);
/* Draw image. -0.5f is used for interpolation */
GdipDrawImageRectRect(graphics, g_pImage,
rect.left, rect.top,
rect.right - rect.left, rect.bottom - rect.top,
-0.5f, -0.5f, ImageWidth, ImageHeight,
UnitPixel, imageAttributes, NULL, NULL);
GdipDisposeImageAttributes(imageAttributes);
GdipDeleteGraphics(graphics);
}
BitBlt(hdc, prcPaint->left, prcPaint->top, paintSize.cx, paintSize.cy, hdcMem, 0, 0, SRCCOPY);
DeleteObject(SelectObject(hdcMem, hbmOld));
DeleteDC(hdcMem);
}
static VOID
ZoomWnd_OnPaint(PPREVIEW_DATA pData, HWND hwnd)
{
PAINTSTRUCT ps;
HDC hDC;
RECT rcClient;
hDC = BeginPaint(hwnd, &ps);
if (hDC)
{
GetClientRect(hwnd, &rcClient);
ZoomWnd_OnDraw(pData, hDC, &ps.rcPaint, &rcClient);
EndPaint(hwnd, &ps);
}
}
static VOID
ImageView_ResetSettings(VOID)
{
g_Settings.Maximized = FALSE;
g_Settings.X = CW_USEDEFAULT;
g_Settings.Y = CW_USEDEFAULT;
g_Settings.Width = 520;
g_Settings.Height = 400;
}
static BOOL
ImageView_LoadSettings(VOID)
{
HKEY hKey;
DWORD dwSize;
LSTATUS nError;
nError = RegOpenKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\shimgvw", 0, KEY_READ, &hKey);
if (nError != ERROR_SUCCESS)
return FALSE;
dwSize = sizeof(g_Settings);
nError = RegQueryValueExW(hKey, L"Settings", NULL, NULL, (LPBYTE)&g_Settings, &dwSize);
RegCloseKey(hKey);
return ((nError == ERROR_SUCCESS) && (dwSize == sizeof(g_Settings)));
}
static VOID
ImageView_SaveSettings(VOID)
{
HKEY hKey;
LSTATUS nError;
nError = RegCreateKeyExW(HKEY_CURRENT_USER, L"Software\\ReactOS\\shimgvw",
0, NULL, 0, KEY_WRITE, NULL, &hKey, NULL);
if (nError != ERROR_SUCCESS)
return;
RegSetValueExW(hKey, L"Settings", 0, REG_BINARY, (LPBYTE)&g_Settings, sizeof(g_Settings));
RegCloseKey(hKey);
}
static BOOL
Preview_CreateToolBar(PPREVIEW_DATA pData)
{
HWND hwndToolBar;
HIMAGELIST hImageList, hOldImageList;
DWORD style = WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT | TBSTYLE_TOOLTIPS;
if (!Preview_IsMainWnd(pData->m_hwnd))
return TRUE; /* FIXME */
style |= CCS_BOTTOM;
hwndToolBar = CreateWindowExW(0, TOOLBARCLASSNAMEW, NULL, style,
0, 0, 0, 0, pData->m_hwnd, NULL, g_hInstance, NULL);
if (!hwndToolBar)
return FALSE;
pData->m_hwndToolBar = hwndToolBar;
SendMessageW(hwndToolBar, TB_BUTTONSTRUCTSIZE, sizeof(s_Buttons[0]), 0);
SendMessageW(hwndToolBar, TB_SETEXTENDEDSTYLE, 0, TBSTYLE_EX_HIDECLIPPEDBUTTONS);
hImageList = ImageList_Create(TB_IMAGE_WIDTH, TB_IMAGE_HEIGHT, ILC_MASK | ILC_COLOR24, 1, 1);
if (hImageList == NULL)
return FALSE;
for (UINT n = 0; n < _countof(s_ButtonConfig); n++)
{
HBITMAP hBitmap = LoadBitmapW(g_hInstance, MAKEINTRESOURCEW(s_ButtonConfig[n].idb));
ImageList_AddMasked(hImageList, hBitmap, RGB(255, 255, 255));
DeleteObject(hBitmap);
}
hOldImageList = (HIMAGELIST)SendMessageW(hwndToolBar, TB_SETIMAGELIST, 0, (LPARAM)hImageList);
ImageList_Destroy(hOldImageList);
SendMessageW(hwndToolBar, TB_ADDBUTTONS, _countof(s_Buttons), (LPARAM)s_Buttons);
return TRUE;
}
static VOID
Preview_EndSlideShow(HWND hwnd)
{
if (Preview_IsMainWnd(hwnd))
return;
KillTimer(hwnd, SLIDESHOW_TIMER_ID);
ShowWindow(hwnd, SW_HIDE);
ShowWindow(g_hMainWnd, SW_SHOWNORMAL);
Preview_ResetZoom(Preview_GetData(g_hMainWnd));
}