3 * PROJECT: ReactOS Picture and Fax Viewer
4 * FILE: dll/win32/shimgvw/shimgvw.c
6 * PROGRAMMER: Dmitry Chapyshev (dmitry@reactos.org)
12 #define WIN32_NO_STATUS
14 #define COM_NO_WINDOWS_H
35 SHIMGVW_SETTINGS shiSettings
;
37 WNDPROC PrevProc
= NULL
;
39 HWND hDispWnd
, hToolBar
;
42 static const TBBUTTON Buttons
[] =
43 { /* iBitmap, idCommand, fsState, fsStyle, bReserved[2], dwData, iString */
44 {TBICON_PREV
, IDC_PREV
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
45 {TBICON_NEXT
, IDC_NEXT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
46 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
47 {TBICON_ZOOMP
, IDC_ZOOMP
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
48 {TBICON_ZOOMM
, IDC_ZOOMM
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
49 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
50 {TBICON_ROT1
, IDC_ROT1
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
51 {TBICON_ROT2
, IDC_ROT2
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
52 {15, 0, TBSTATE_ENABLED
, BTNS_SEP
, {0}, 0, 0},
53 {TBICON_SAVE
, IDC_SAVE
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
54 {TBICON_PRINT
, IDC_PRINT
, TBSTATE_ENABLED
, BTNS_BUTTON
, {0}, 0, 0},
57 static void pLoadImage(LPWSTR szOpenFileName
)
59 if (GetFileAttributesW(szOpenFileName
) == 0xFFFFFFFF)
61 DPRINT1("File %s not found!\n", szOpenFileName
);
65 GdipLoadImageFromFile(szOpenFileName
, &image
);
68 DPRINT1("GdipLoadImageFromFile() failed\n");
73 ImageView_DrawImage(HWND hwnd
)
76 UINT uImgWidth
, uImgHeight
;
77 UINT height
= 0, width
= 0, x
= 0, y
= 0;
82 hdc
= BeginPaint(hwnd
, &ps
);
85 DPRINT1("BeginPaint() failed\n");
89 GdipCreateFromHDC(hdc
, &graphics
);
92 DPRINT1("GdipCreateFromHDC() failed\n");
96 GdipGetImageWidth(image
, &uImgWidth
);
97 GdipGetImageHeight(image
, &uImgHeight
);
99 if (GetClientRect(hwnd
, &rect
))
101 FillRect(hdc
, &rect
, (HBRUSH
)GetStockObject(WHITE_BRUSH
));
103 if ((rect
.right
== uImgWidth
)&&(rect
.bottom
== uImgHeight
))
105 x
= 0, y
= 0, width
= rect
.right
, height
= rect
.bottom
;
107 else if ((rect
.right
>= uImgWidth
)&&(rect
.bottom
>= uImgHeight
))
109 x
= (rect
.right
/2)-(uImgWidth
/2);
110 y
= (rect
.bottom
/2)-(uImgHeight
/2);
114 else if ((rect
.right
< uImgWidth
)||(rect
.bottom
< uImgHeight
))
116 if (rect
.bottom
< uImgHeight
)
118 height
= rect
.bottom
;
119 width
= uImgWidth
*(UINT
)rect
.bottom
/uImgHeight
;
120 x
= (rect
.right
/2)-(width
/2);
121 y
= (rect
.bottom
/2)-(height
/2);
123 if (rect
.right
< uImgWidth
)
126 height
= uImgHeight
*(UINT
)rect
.right
/uImgWidth
;
127 x
= (rect
.right
/2)-(width
/2);
128 y
= (rect
.bottom
/2)-(height
/2);
130 if ((height
> rect
.bottom
)||(width
> rect
.right
))
134 if (((int)width
- 1 < 0)||((int)height
- 1 < 0)) break;
137 y
= (rect
.bottom
/2)-(height
/2);
138 x
= (rect
.right
/2)-(width
/2);
139 if ((height
< rect
.bottom
)&&(width
< rect
.right
)) break;
143 else if ((rect
.right
<= uImgWidth
)&&(rect
.bottom
<= uImgHeight
))
145 height
= uImgHeight
*(UINT
)rect
.right
/uImgWidth
;
146 y
= (rect
.bottom
/2)-(height
/2);
149 if ((height
> rect
.bottom
)||(width
> rect
.right
))
153 if (((int)width
- 1 < 0)||((int)height
- 1 < 0)) break;
156 y
= (rect
.bottom
/2)-(height
/2);
157 x
= (rect
.right
/2)-(width
/2);
158 if ((height
< rect
.bottom
)&&(width
< rect
.right
)) break;
163 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
);
164 Rectangle(hdc
, x
- 1, y
- 1, x
+ width
+ 1, y
+ height
+ 1);
165 GdipDrawImageRect(graphics
, image
, x
, y
, width
, height
);
167 GdipDeleteGraphics(graphics
);
172 ImageView_LoadSettings()
177 if (RegOpenKeyEx(HKEY_CURRENT_USER
, _T("Software\\ReactOS\\shimgvw"), 0, KEY_READ
, &hKey
) == ERROR_SUCCESS
)
179 dwSize
= sizeof(SHIMGVW_SETTINGS
);
180 if (RegQueryValueEx(hKey
, _T("Settings"), NULL
, NULL
, (LPBYTE
)&shiSettings
, &dwSize
) == ERROR_SUCCESS
)
193 ImageView_SaveSettings(HWND hwnd
)
198 ShowWindow(hwnd
, SW_HIDE
);
199 wp
.length
= sizeof(WINDOWPLACEMENT
);
200 GetWindowPlacement(hwnd
, &wp
);
202 shiSettings
.Left
= wp
.rcNormalPosition
.left
;
203 shiSettings
.Top
= wp
.rcNormalPosition
.top
;
204 shiSettings
.Right
= wp
.rcNormalPosition
.right
;
205 shiSettings
.Bottom
= wp
.rcNormalPosition
.bottom
;
206 shiSettings
.Maximized
= (IsZoomed(hwnd
) || (wp
.flags
& WPF_RESTORETOMAXIMIZED
));
208 if (RegCreateKeyEx(HKEY_CURRENT_USER
, _T("Software\\ReactOS\\shimgvw"), 0, NULL
,
209 REG_OPTION_NON_VOLATILE
, KEY_WRITE
, NULL
, &hKey
, NULL
) == ERROR_SUCCESS
)
211 RegSetValueEx(hKey
, _T("Settings"), 0, REG_BINARY
, (LPBYTE
)&shiSettings
, sizeof(SHIMGVW_SETTINGS
));
217 ImageView_CreateToolBar(HWND hwnd
)
219 INT numButtons
= sizeof(Buttons
) / sizeof(Buttons
[0]);
221 hToolBar
= CreateWindowEx(0, TOOLBARCLASSNAME
, NULL
,
222 WS_CHILD
| WS_VISIBLE
| TBSTYLE_FLAT
| CCS_BOTTOM
| TBSTYLE_TOOLTIPS
,
227 HIMAGELIST hImageList
;
229 SendMessage(hToolBar
, TB_SETEXTENDEDSTYLE
,
230 0, TBSTYLE_EX_HIDECLIPPEDBUTTONS
);
232 SendMessage(hToolBar
, TB_BUTTONSTRUCTSIZE
,
233 sizeof(Buttons
[0]), 0);
235 hImageList
= ImageList_Create(TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, ILC_MASK
| ILC_COLOR24
, 1, 1);
237 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_PREVICON
), IMAGE_BITMAP
,
238 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
240 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_NEXTICON
), IMAGE_BITMAP
,
241 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
243 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ZOOMPICON
), IMAGE_BITMAP
,
244 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
246 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ZOOMMICON
), IMAGE_BITMAP
,
247 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
249 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_SAVEICON
), IMAGE_BITMAP
,
250 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
252 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_PRINTICON
), IMAGE_BITMAP
,
253 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
255 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ROT1ICON
), IMAGE_BITMAP
,
256 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
258 ImageList_AddMasked(hImageList
, LoadImage(hInstance
, MAKEINTRESOURCE(IDB_ROT2ICON
), IMAGE_BITMAP
,
259 TB_IMAGE_WIDTH
, TB_IMAGE_HEIGHT
, LR_DEFAULTCOLOR
), RGB(255, 255, 255));
261 if (hImageList
== NULL
) return FALSE
;
263 ImageList_Destroy((HIMAGELIST
)SendMessage(hToolBar
, TB_SETIMAGELIST
,
264 0, (LPARAM
)hImageList
));
266 SendMessage(hToolBar
, TB_ADDBUTTONS
,
267 numButtons
, (LPARAM
)Buttons
);
276 ImageView_DispWndProc(HWND hwnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
282 ImageView_DrawImage(hwnd
);
286 return CallWindowProc(PrevProc
, hwnd
, Message
, wParam
, lParam
);
290 ImageView_InitControls(HWND hwnd
)
292 MoveWindow(hwnd
, shiSettings
.Left
, shiSettings
.Top
,
293 shiSettings
.Right
- shiSettings
.Left
,
294 shiSettings
.Bottom
- shiSettings
.Top
, TRUE
);
296 if (shiSettings
.Maximized
) ShowWindow(hwnd
, SW_MAXIMIZE
);
298 hDispWnd
= CreateWindowEx(0, _T("STATIC"), _T(""),
299 WS_CHILD
| WS_VISIBLE
,
300 0, 0, 0, 0, hwnd
, NULL
, hInstance
, NULL
);
302 SetClassLongPtr(hDispWnd
, GCL_STYLE
, CS_HREDRAW
| CS_VREDRAW
);
303 PrevProc
= (WNDPROC
) SetWindowLongPtr(hDispWnd
, GWL_WNDPROC
, (LPARAM
) ImageView_DispWndProc
);
305 ImageView_CreateToolBar(hwnd
);
309 ImageView_WndProc(HWND hwnd
, UINT Message
, WPARAM wParam
, LPARAM lParam
)
315 ImageView_InitControls(hwnd
);
352 LPNMHDR pnmhdr
= (LPNMHDR
)lParam
;
354 switch (pnmhdr
->code
)
356 case TTN_GETDISPINFO
:
361 lpttt
= (LPTOOLTIPTEXT
)lParam
;
362 idButton
= (UINT
)lpttt
->hdr
.idFrom
;
367 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PREV_PIC
);
370 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_NEXT_PIC
);
373 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ZOOM_IN
);
376 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ZOOM_OUT
);
379 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_SAVEAS
);
382 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_PRINT
);
385 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ROT_COUNCW
);
388 lpttt
->lpszText
= MAKEINTRESOURCE(IDS_TOOLTIP_ROT_CLOCKW
);
398 LPRECT pRect
= (LPRECT
)lParam
;
399 if (pRect
->right
-pRect
->left
< 350)
400 pRect
->right
= pRect
->left
+ 350;
402 if (pRect
->bottom
-pRect
->top
< 290)
403 pRect
->bottom
= pRect
->top
+ 290;
409 SendMessage(hToolBar
, TB_AUTOSIZE
, 0, 0);
410 SendMessage(hToolBar
, TB_GETITEMRECT
, 1, (LPARAM
)&rc
);
411 MoveWindow(hDispWnd
, 1, 1, LOWORD(lParam
)-1, HIWORD(lParam
)-rc
.bottom
, TRUE
);
416 ImageView_SaveSettings(hwnd
);
417 SetWindowLongPtr(hDispWnd
, GWL_WNDPROC
, (LPARAM
) PrevProc
);
423 return DefWindowProc(hwnd
, Message
, wParam
, lParam
);
427 ImageView_CreateWindow(HWND hwnd
, LPWSTR szFileName
)
429 struct GdiplusStartupInput gdiplusStartupInput
;
430 ULONG_PTR gdiplusToken
;
431 WNDCLASS WndClass
= {0};
436 if (!ImageView_LoadSettings())
438 shiSettings
.Maximized
= FALSE
;
439 shiSettings
.Left
= 0;
441 shiSettings
.Right
= 520;
442 shiSettings
.Bottom
= 400;
446 gdiplusStartupInput
.GdiplusVersion
= 1;
447 gdiplusStartupInput
.DebugEventCallback
= NULL
;
448 gdiplusStartupInput
.SuppressBackgroundThread
= FALSE
;
449 gdiplusStartupInput
.SuppressExternalCodecs
= FALSE
;
451 GdiplusStartup(&gdiplusToken
, &gdiplusStartupInput
, NULL
);
452 pLoadImage(szFileName
);
455 WndClass
.lpszClassName
= _T("shimgvw_window");
456 WndClass
.lpfnWndProc
= (WNDPROC
)ImageView_WndProc
;
457 WndClass
.hInstance
= hInstance
;
458 WndClass
.style
= CS_HREDRAW
| CS_VREDRAW
;
459 WndClass
.hIcon
= LoadIcon(hInstance
, MAKEINTRESOURCE(IDI_APPICON
));
460 WndClass
.hCursor
= LoadCursor(hInstance
, IDC_ARROW
);
461 WndClass
.hbrBackground
= (HBRUSH
)COLOR_WINDOW
;
463 if (!RegisterClass(&WndClass
)) return -1;
465 LoadString(hInstance
, IDS_APPTITLE
, szBuf
, sizeof(szBuf
) / sizeof(TCHAR
));
466 hMainWnd
= CreateWindow(_T("shimgvw_window"), szBuf
,
467 WS_OVERLAPPEDWINDOW
| WS_VISIBLE
| WS_CAPTION
,
468 CW_USEDEFAULT
, CW_USEDEFAULT
,
469 0, 0, NULL
, NULL
, hInstance
, NULL
);
472 ShowWindow(hMainWnd
, SW_SHOW
);
473 UpdateWindow(hMainWnd
);
476 while(GetMessage(&msg
,NULL
,0,0))
478 TranslateMessage(&msg
);
479 DispatchMessageW(&msg
);
483 GdipDisposeImage(image
);
484 GdiplusShutdown(gdiplusToken
);
489 ImageView_FullscreenW(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
491 ImageView_CreateWindow(hwnd
, (LPWSTR
)path
);
495 ImageView_Fullscreen(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
497 ImageView_CreateWindow(hwnd
, (LPWSTR
)path
);
501 ImageView_FullscreenA(HWND hwnd
, HINSTANCE hInst
, LPCSTR path
, int nShow
)
503 WCHAR szFile
[MAX_PATH
];
505 if (MultiByteToWideChar(CP_ACP
, 0, (char*)path
, strlen((char*)path
)+1, szFile
, MAX_PATH
))
507 ImageView_CreateWindow(hwnd
, (LPWSTR
)szFile
);
512 ImageView_PrintTo(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
514 DPRINT("ImageView_PrintTo() not implemented\n");
518 ImageView_PrintToA(HWND hwnd
, HINSTANCE hInst
, LPCSTR path
, int nShow
)
520 DPRINT("ImageView_PrintToA() not implemented\n");
524 ImageView_PrintToW(HWND hwnd
, HINSTANCE hInst
, LPCWSTR path
, int nShow
)
526 DPRINT("ImageView_PrintToW() not implemented\n");
530 DllMain(IN HINSTANCE hinstDLL
,
532 IN LPVOID lpvReserved
)
536 case DLL_PROCESS_ATTACH
:
537 case DLL_THREAD_ATTACH
:
538 hInstance
= hinstDLL
;