3 * PROJECT: ReactOS Picture and Fax Viewer
4 * FILE: dll/win32/shimgvw/shimgvw.c
6 * PROGRAMMER: Dmitry Chapyshev (dmitry@reactos.org)
28 SHIMGVW_SETTINGS shiSettings
;
30 WNDPROC PrevProc
= NULL
;
32 HWND hDispWnd
, hToolBar
;
35 static const TBBUTTON Buttons
[] =
36 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
37 {TBICON_PREV
, IDC_PREV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
38 {TBICON_NEXT
, IDC_NEXT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
39 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
40 {TBICON_ZOOMP
, IDC_ZOOMP
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
41 {TBICON_ZOOMM
, IDC_ZOOMM
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
42 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
43 {TBICON_ROT1
, IDC_ROT1
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
44 {TBICON_ROT2
, IDC_ROT2
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
45 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
46 {TBICON_SAVE
, IDC_SAVE
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
47 {TBICON_PRINT
, IDC_PRINT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
50 static void pLoadImage(LPWSTR szOpenFileName
)
52 if (GetFileAttributesW(szOpenFileName
) == 0xFFFFFFFF)
54 DPRINT1("File %s not found!\n", szOpenFileName
);
58 GdipLoadImageFromFile(szOpenFileName
, &image
);
61 DPRINT1("GdipLoadImageFromFile() failed\n");
66 ImageView_DrawImage(HWND hwnd
)
69 UINT uImgWidth
, uImgHeight
;
70 UINT height
= 0, width
= 0, x
= 0, y
= 0;
75 hdc
= BeginPaint(hwnd
, &ps
);
78 DPRINT1("BeginPaint() failed\n");
82 GdipCreateFromHDC(hdc
, &graphics
);
85 DPRINT1("GdipCreateFromHDC() failed\n");
89 GdipGetImageWidth(image
, &uImgWidth
);
90 GdipGetImageHeight(image
, &uImgHeight
);
92 if (GetClientRect(hwnd
, &rect
))
94 FillRect(hdc
, &rect
, (HBRUSH
)GetStockObject(WHITE_BRUSH
));
96 if ((rect
.right
== uImgWidth
)&&(rect
.bottom
== uImgHeight
))
98 x
= 0, y
= 0, width
= rect
.right
, height
= rect
.bottom
;
100 else if ((rect
.right
>= uImgWidth
)&&(rect
.bottom
>= uImgHeight
))
102 x
= (rect
.right
/2)-(uImgWidth
/2);
103 y
= (rect
.bottom
/2)-(uImgHeight
/2);
107 else if ((rect
.right
< uImgWidth
)||(rect
.bottom
< uImgHeight
))
109 if (rect
.bottom
< uImgHeight
)
111 height
= rect
.bottom
;
112 width
= uImgWidth
*(UINT
)rect
.bottom
/uImgHeight
;
113 x
= (rect
.right
/2)-(width
/2);
114 y
= (rect
.bottom
/2)-(height
/2);
116 if (rect
.right
< uImgWidth
)
119 height
= uImgHeight
*(UINT
)rect
.right
/uImgWidth
;
120 x
= (rect
.right
/2)-(width
/2);
121 y
= (rect
.bottom
/2)-(height
/2);
123 if ((height
> rect
.bottom
)||(width
> rect
.right
))
127 if (((int)width
- 1 < 0)||((int)height
- 1 < 0)) break;
130 y
= (rect
.bottom
/2)-(height
/2);
131 x
= (rect
.right
/2)-(width
/2);
132 if ((height
< rect
.bottom
)&&(width
< rect
.right
)) break;
136 else if ((rect
.right
<= uImgWidth
)&&(rect
.bottom
<= uImgHeight
))
138 height
= uImgHeight
*(UINT
)rect
.right
/uImgWidth
;
139 y
= (rect
.bottom
/2)-(height
/2);
142 if ((height
> rect
.bottom
)||(width
> rect
.right
))
146 if (((int)width
- 1 < 0)||((int)height
- 1 < 0)) break;
149 y
= (rect
.bottom
/2)-(height
/2);
150 x
= (rect
.right
/2)-(width
/2);
151 if ((height
< rect
.bottom
)&&(width
< rect
.right
)) break;
156 DPRINT("x = %d\ny = %d\nWidth = %d\nHeight = %d\n\nrect.right = %d\nrect.bottom = %d\n\nuImgWidth = %d\nuImgHeight = %d\n", x
, y
, width
, height
, rect
.right
, rect
.bottom
, uImgWidth
, uImgHeight
);
157 Rectangle(hdc
, x
- 1, y
- 1, x
+ width
+ 1, y
+ height
+ 1);
158 GdipDrawImageRect(graphics
, image
, x
, y
, width
, height
);
160 GdipDeleteGraphics(graphics
);
165 ImageView_LoadSettings()
170 if (RegOpenKeyEx(HKEY_CURRENT_USER
, _T("Software\\ReactOS\\shimgvw"), 0, KEY_READ
, &hKey
) == ERROR_SUCCESS
)
172 dwSize
= sizeof(SHIMGVW_SETTINGS
);
173 if (RegQueryValueEx(hKey
, _T("Settings"), NULL
, NULL
, (LPBYTE
)&shiSettings
, &dwSize
) == ERROR_SUCCESS
)
186 ImageView_SaveSettings(HWND hwnd
)
191 ShowWindow(hwnd
, SW_HIDE
);
192 wp
.length
= sizeof(WINDOWPLACEMENT
);
193 GetWindowPlacement(hwnd
, &wp
);
195 shiSettings
.Left
= wp
.rcNormalPosition
.left
;
196 shiSettings
.Top
= wp
.rcNormalPosition
.top
;
197 shiSettings
.Right
= wp
.rcNormalPosition
.right
;
198 shiSettings
.Bottom
= wp
.rcNormalPosition
.bottom
;
199 shiSettings
.Maximized
= (IsZoomed(hwnd
) || (wp
.flags
& WPF_RESTORETOMAXIMIZED
));
201 if (RegCreateKeyEx(HKEY_CURRENT_USER
, _T("Software\\ReactOS\\shimgvw"), 0, NULL
,
202 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, NULL
) == ERROR_SUCCESS
)
204 RegSetValueEx(hKey
, _T("Settings"), 0, REG_BINARY
, (LPBYTE
)&shiSettings
, sizeof(SHIMGVW_SETTINGS
));
210 ImageView_CreateToolBar(HWND hwnd
)
212 INT numButtons
= sizeof(Buttons
) / sizeof(Buttons
[0]);
214 hToolBar
= CreateWindowEx(0, TOOLBARCLASSNAME
, NULL
,
215 WS_CHILD
| WS_VISIBLE
| TBSTYLE_FLAT
| CCS_BOTTOM
| TBSTYLE_TOOLTIPS
,
220 HIMAGELIST hImageList
;
222 SendMessage(hToolBar
, TB_SETEXTENDEDSTYLE
,
223 0, TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
225 SendMessage(hToolBar
, TB_BUTTONSTRUCTSIZE
,
226 sizeof(Buttons
[0]), 0);
228 hImageList
= ImageList_Create(TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, ILC_MASK
| ILC_COLOR24
, 1, 1);
230 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_PREVICON
), IMAGE_BITMAP
,
231 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
233 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_NEXTICON
), IMAGE_BITMAP
,
234 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
236 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ZOOMPICON
), IMAGE_BITMAP
,
237 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
239 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ZOOMMICON
), IMAGE_BITMAP
,
240 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
242 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_SAVEICON
), IMAGE_BITMAP
,
243 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
245 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_PRINTICON
), IMAGE_BITMAP
,
246 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
248 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ROT1ICON
), IMAGE_BITMAP
,
249 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
251 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ROT2ICON
), IMAGE_BITMAP
,
252 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
254 if (hImageList
== NULL
) return FALSE
;
256 ImageList_Destroy((HIMAGELIST
)SendMessage(hToolBar
, TB_SETIMAGELIST
,
257 0, (LPARAM
)hImageList
));
259 SendMessage(hToolBar
, TB_ADDBUTTONS
,
260 numButtons
, (LPARAM
)Buttons
);
269 ImageView_DispWndProc(HWND hwnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
275 ImageView_DrawImage(hwnd
);
279 return CallWindowProc(PrevProc
, hwnd
, Message
, wParam
, lParam
);
283 ImageView_InitControls(HWND hwnd
)
285 MoveWindow(hwnd
, shiSettings
.Left
, shiSettings
.Top
,
286 shiSettings
.Right
- shiSettings
.Left
,
287 shiSettings
.Bottom
- shiSettings
.Top
, TRUE
);
289 if (shiSettings
.Maximized
) ShowWindow(hwnd
, SW_MAXIMIZE
);
291 hDispWnd
= CreateWindowEx(0, _T("STATIC"), _T(""),
292 WS_CHILD
| WS_VISIBLE
,
293 0, 0, 0, 0, hwnd
, NULL
, hInstance
, NULL
);
295 SetClassLongPtr(hDispWnd
, GCL_STYLE
, CS_HREDRAW
| CS_VREDRAW
);
296 PrevProc
= (WNDPROC
) SetWindowLongPtr(hDispWnd
, GWL_WNDPROC
, (LPARAM
) ImageView_DispWndProc
);
298 ImageView_CreateToolBar(hwnd
);
302 ImageView_WndProc(HWND hwnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
308 ImageView_InitControls(hwnd
);
345 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
347 switch (pnmhdr
->code
)
349 case TTN_GETDISPINFO
:
354 lpttt
= (LPTOOLTIPTEXT
)lParam
;
355 idButton
= (UINT
)lpttt
->hdr
.idFrom
;
360 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PREV_PIC
);
363 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_NEXT_PIC
);
366 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ZOOM_IN
);
369 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ZOOM_OUT
);
372 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_SAVEAS
);
375 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PRINT
);
378 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ROT_COUNCW
);
381 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ROT_CLOCKW
);
391 LPRECT pRect
= (LPRECT
)lParam
;
392 if (pRect
->right
-pRect
->left
< 350)
393 pRect
->right
= pRect
->left
+ 350;
395 if (pRect
->bottom
-pRect
->top
< 290)
396 pRect
->bottom
= pRect
->top
+ 290;
402 SendMessage(hToolBar
, TB_AUTOSIZE
, 0, 0);
403 SendMessage(hToolBar
, TB_GETITEMRECT
, 1, (LPARAM
)&rc
);
404 MoveWindow(hDispWnd
, 1, 1, LOWORD(lParam
)-1, HIWORD(lParam
)-rc
.bottom
, TRUE
);
409 ImageView_SaveSettings(hwnd
);
410 SetWindowLongPtr(hDispWnd
, GWL_WNDPROC
, (LPARAM
) PrevProc
);
416 return DefWindowProc(hwnd
, Message
, wParam
, lParam
);
420 ImageView_CreateWindow(HWND hwnd
, LPWSTR szFileName
)
422 struct GdiplusStartupInput gdiplusStartupInput
;
423 ULONG_PTR gdiplusToken
;
424 WNDCLASS WndClass
= {0};
429 if (!ImageView_LoadSettings())
431 shiSettings
.Maximized
= FALSE
;
432 shiSettings
.Left
= 0;
434 shiSettings
.Right
= 520;
435 shiSettings
.Bottom
= 400;
439 gdiplusStartupInput
.GdiplusVersion
= 1;
440 gdiplusStartupInput
.DebugEventCallback
= NULL
;
441 gdiplusStartupInput
.SuppressBackgroundThread
= FALSE
;
442 gdiplusStartupInput
.SuppressExternalCodecs
= FALSE
;
444 GdiplusStartup(&gdiplusToken
, &gdiplusStartupInput
, NULL
);
445 pLoadImage(szFileName
);
448 WndClass
.lpszClassName
= _T("shimgvw_window");
449 WndClass
.lpfnWndProc
= (WNDPROC
)ImageView_WndProc
;
450 WndClass
.hInstance
= hInstance
;
451 WndClass
.style
= CS_HREDRAW
| CS_VREDRAW
;
452 WndClass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_APPICON
));
453 WndClass
.hCursor
= LoadCursor(hInstance
, IDC_ARROW
);
454 WndClass
.hbrBackground
= (HBRUSH
)COLOR_WINDOW
;
456 if (!RegisterClass(&WndClass
)) return -1;
458 LoadString(hInstance
, IDS_APPTITLE
, szBuf
, sizeof(szBuf
) / sizeof(TCHAR
));
459 hMainWnd
= CreateWindow(_T("shimgvw_window"), szBuf
,
460 WS_OVERLAPPEDWINDOW
| WS_VISIBLE
| WS_CAPTION
,
461 CW_USEDEFAULT
, CW_USEDEFAULT
,
462 0, 0, NULL
, NULL
, hInstance
, NULL
);
465 ShowWindow(hMainWnd
, SW_SHOW
);
466 UpdateWindow(hMainWnd
);
469 while(GetMessage(&msg
,NULL
,0,0))
471 TranslateMessage(&msg
);
472 DispatchMessageW(&msg
);
476 GdipDisposeImage(image
);
477 GdiplusShutdown(gdiplusToken
);
482 ImageView_FullscreenW(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
484 ImageView_CreateWindow(hwnd
, (LPWSTR
)path
);
488 ImageView_Fullscreen(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
490 ImageView_CreateWindow(hwnd
, (LPWSTR
)path
);
494 ImageView_FullscreenA(HWND hwnd
, HINSTANCE hInst
, LPCSTR path
, int nShow
)
496 WCHAR szFile
[MAX_PATH
];
498 if (MultiByteToWideChar(CP_ACP
, 0, (char*)path
, strlen((char*)path
)+1, szFile
, MAX_PATH
))
500 ImageView_CreateWindow(hwnd
, (LPWSTR
)szFile
);
505 ImageView_PrintTo(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
507 DPRINT("ImageView_PrintTo() not implemented\n");
511 ImageView_PrintToA(HWND hwnd
, HINSTANCE hInst
, LPCSTR path
, int nShow
)
513 DPRINT("ImageView_PrintToA() not implemented\n");
517 ImageView_PrintToW(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
519 DPRINT("ImageView_PrintToW() not implemented\n");
523 imageview_fullscreenW(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
525 DPRINT("ImageView_fullscreenW() not implemented\n");
529 DllMain(IN HINSTANCE hinstDLL
,
531 IN LPVOID lpvReserved
)
535 case DLL_PROCESS_ATTACH
:
536 case DLL_THREAD_ATTACH
:
537 hInstance
= hinstDLL
;