jscript: Removed unused do_*_tag_format arguments.
[wine/multimedia.git] / programs / view / view.c
blobd32a10b8f12d53e6bb4b8b07e74a5ea96883d68d
1 /*
2 * Copyright 1998 Douglas Ridgway
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <windows.h>
20 #include <commdlg.h>
21 #include "resource.h"
23 #include <stdio.h>
25 static HINSTANCE hInst;
26 static HWND hMainWnd;
27 static WCHAR szAppName[5] = {'V','i','e','w',0};
28 static WCHAR szTitle[MAX_PATH];
29 static WCHAR szFileTitle[MAX_PATH];
31 static HMETAFILE hmf;
32 static HENHMETAFILE enhmf;
33 static int deltax = 0, deltay = 0;
34 static int width = 0, height = 0;
35 static BOOL isAldus, isEnhanced;
37 #include "pshpack1.h"
38 typedef struct
40 DWORD key;
41 WORD hmf;
42 SMALL_RECT bbox;
43 WORD inch;
44 DWORD reserved;
45 WORD checksum;
46 } APMFILEHEADER;
47 #include "poppack.h"
49 #define APMHEADER_KEY 0x9AC6CDD7l
52 static BOOL FileOpen(HWND hWnd, WCHAR *fn, int fnsz)
54 static const WCHAR filter[] = {'M','e','t','a','f','i','l','e','s','\0','*','.','w','m','f',';','*','.','e','m','f','\0',0};
55 OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
56 0, 0, NULL, NULL, 0, 0, NULL,
57 fnsz, NULL, 0, NULL, NULL,
58 OFN_SHOWHELP, 0, 0, NULL, 0, NULL };
59 ofn.lpstrFilter = filter;
60 ofn.hwndOwner = hWnd;
61 ofn.lpstrFile = fn;
62 if( fnsz < 1 )
63 return FALSE;
64 *fn = 0;
65 return GetOpenFileNameW(&ofn);
68 static BOOL FileIsEnhanced( LPCWSTR szFileName )
70 ENHMETAHEADER enh;
71 HANDLE handle;
72 DWORD size;
74 handle = CreateFileW( szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
75 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
76 if (handle == INVALID_HANDLE_VALUE)
77 return FALSE;
79 if (!ReadFile( handle, &enh, sizeof(ENHMETAHEADER), &size, NULL ) || size != sizeof(ENHMETAHEADER) )
81 CloseHandle( handle );
82 return FALSE;
84 CloseHandle( handle );
86 /* Is it enhanced? */
87 return (enh.dSignature == ENHMETA_SIGNATURE);
90 static BOOL FileIsPlaceable( LPCWSTR szFileName )
92 APMFILEHEADER apmh;
93 HANDLE handle;
94 DWORD size;
96 handle = CreateFileW( szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
97 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
98 if (handle == INVALID_HANDLE_VALUE)
99 return FALSE;
101 if (!ReadFile( handle, &apmh, sizeof(APMFILEHEADER), &size, NULL ) || size != sizeof(APMFILEHEADER))
103 CloseHandle( handle );
104 return FALSE;
106 CloseHandle( handle );
108 /* Is it placeable? */
109 return (apmh.key == APMHEADER_KEY);
112 static HMETAFILE GetPlaceableMetaFile( LPCWSTR szFileName )
114 LPBYTE lpData;
115 METAHEADER mfHeader;
116 APMFILEHEADER APMHeader;
117 HANDLE handle;
118 DWORD size;
119 HMETAFILE hmf;
120 WORD checksum, *p;
121 HDC hdc;
122 int i;
124 handle = CreateFileW( szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
125 NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
126 if (handle == INVALID_HANDLE_VALUE)
127 return 0;
129 if (!ReadFile( handle, &APMHeader, sizeof(APMFILEHEADER), &size, NULL ) || size != sizeof(APMFILEHEADER))
131 CloseHandle( handle );
132 return 0;
134 checksum = 0;
135 p = (WORD *) &APMHeader;
137 for(i=0; i<10; i++)
138 checksum ^= *p++;
139 if (checksum != APMHeader.checksum) {
140 char msg[128];
141 sprintf(msg, "Computed checksum %04x != stored checksum %04x\n",
142 checksum, APMHeader.checksum);
143 MessageBoxA(hMainWnd, msg, "Checksum failed", MB_OK);
144 CloseHandle( handle );
145 return 0;
148 if (!ReadFile( handle, &mfHeader, sizeof(METAHEADER), &size, NULL) || size != sizeof(METAHEADER))
150 CloseHandle( handle );
151 return 0;
154 if (!(lpData = GlobalAlloc(GPTR, (mfHeader.mtSize * 2L))))
156 CloseHandle( handle );
157 return 0;
160 SetFilePointer( handle, sizeof(APMFILEHEADER), NULL, FILE_BEGIN );
161 if (!ReadFile(handle, lpData, mfHeader.mtSize * 2, &size, NULL ) || size != mfHeader.mtSize * 2)
163 GlobalFree(lpData);
164 CloseHandle( handle );
165 return 0;
167 CloseHandle( handle );
169 if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData)))
170 return 0;
173 width = APMHeader.bbox.Right - APMHeader.bbox.Left;
174 height = APMHeader.bbox.Bottom - APMHeader.bbox.Top;
176 /* printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch); */
177 hdc = GetDC(hMainWnd);
178 width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch;
179 height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch;
180 ReleaseDC(hMainWnd, hdc);
182 deltax = 0;
183 deltay = 0 ;
184 return hmf;
187 static void DoOpenFile(LPCWSTR filename)
189 if (!filename) return;
191 isAldus = FileIsPlaceable(filename);
192 if (isAldus) {
193 hmf = GetPlaceableMetaFile(filename);
194 } else {
195 RECT r;
196 isEnhanced = FileIsEnhanced(filename);
197 if (isEnhanced)
198 enhmf = GetEnhMetaFileW(filename);
199 else
200 hmf = GetMetaFileW(filename);
201 GetClientRect(hMainWnd, &r);
202 width = r.right - r.left;
203 height = r.bottom - r.top;
205 InvalidateRect( hMainWnd, NULL, TRUE );
208 static void UpdateWindowCaption(void)
210 WCHAR szCaption[MAX_PATH];
211 WCHAR szView[MAX_PATH];
212 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
214 LoadStringW(hInst, IDS_DESCRIPTION, szView, sizeof(szView)/sizeof(WCHAR));
216 if (szFileTitle[0] != '\0')
218 lstrcpyW(szCaption, szFileTitle);
219 LoadStringW(hInst, IDS_DESCRIPTION, szView, sizeof(szView)/sizeof(WCHAR));
220 lstrcatW(szCaption, hyphenW);
221 lstrcatW(szCaption, szView);
223 else
224 lstrcpyW(szCaption, szView);
226 SetWindowTextW(hMainWnd, szCaption);
229 static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
231 switch (uMessage)
233 case WM_PAINT:
235 PAINTSTRUCT ps;
236 BeginPaint(hwnd, &ps);
237 SetMapMode(ps.hdc, MM_ANISOTROPIC);
238 /* Set the window extent to a sane value in case the metafile doesn't */
239 SetWindowExtEx(ps.hdc, width, height, NULL);
240 SetViewportExtEx(ps.hdc, width, height, NULL);
241 SetViewportOrgEx(ps.hdc, deltax, deltay, NULL);
242 if (isEnhanced && enhmf)
244 RECT r;
245 GetClientRect(hwnd, &r);
246 PlayEnhMetaFile(ps.hdc, enhmf, &r);
248 else if (hmf)
249 PlayMetaFile(ps.hdc, hmf);
250 EndPaint(hwnd, &ps);
252 break;
254 case WM_COMMAND: /* message: command from application menu */
255 switch (LOWORD(wparam))
257 case IDM_OPEN:
259 WCHAR filename[MAX_PATH];
260 if (FileOpen(hwnd, filename, sizeof(filename)/sizeof(WCHAR)))
262 szFileTitle[0] = 0;
263 GetFileTitleW(filename, szFileTitle, sizeof(szFileTitle));
264 DoOpenFile(filename);
265 UpdateWindowCaption();
268 break;
270 case IDM_SET_EXT_TO_WIN:
272 RECT r;
273 GetClientRect(hwnd, &r);
274 width = r.right - r.left;
275 height = r.bottom - r.top;
276 deltax = deltay = 0;
277 InvalidateRect( hwnd, NULL, TRUE );
279 break;
282 case IDM_LEFT:
283 deltax += 100;
284 InvalidateRect( hwnd, NULL, TRUE );
285 break;
286 case IDM_RIGHT:
287 deltax -= 100;
288 InvalidateRect( hwnd, NULL, TRUE );
289 break;
290 case IDM_UP:
291 deltay += 100;
292 InvalidateRect( hwnd, NULL, TRUE );
293 break;
294 case IDM_DOWN:
295 deltay -= 100;
296 InvalidateRect( hwnd, NULL, TRUE );
297 break;
299 case IDM_EXIT:
300 DestroyWindow(hwnd);
301 break;
303 default:
304 return DefWindowProcW(hwnd, uMessage, wparam, lparam);
306 break;
308 case WM_DESTROY: /* message: window being destroyed */
309 PostQuitMessage(0);
310 break;
312 default: /* Passes it on if unprocessed */
313 return DefWindowProcW(hwnd, uMessage, wparam, lparam);
315 return 0;
318 static BOOL InitApplication(HINSTANCE hInstance)
320 WNDCLASSEXW wc;
322 /* Load the application description strings */
323 LoadStringW(hInstance, IDS_DESCRIPTION, szTitle, sizeof(szTitle)/sizeof(WCHAR));
325 /* Fill in window class structure with parameters that describe the
326 main window */
328 wc.cbSize = sizeof(WNDCLASSEXW);
329 wc.style = CS_HREDRAW | CS_VREDRAW; /* Class style(s) */
330 wc.lpfnWndProc = WndProc; /* Window Procedure */
331 wc.cbClsExtra = 0; /* No per-class extra data */
332 wc.cbWndExtra = 0; /* No per-window extra data */
333 wc.hInstance = hInstance; /* Owner of this class */
334 wc.hIcon = NULL;
335 wc.hIconSm = NULL;
336 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
337 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); /* Default color */
338 wc.lpszMenuName = szAppName; /* Menu name from .rc */
339 wc.lpszClassName = szAppName; /* Name to register as */
341 if (!RegisterClassExW(&wc)) return FALSE;
343 /* Call module specific initialization functions here */
345 return TRUE;
348 static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
350 /* Save the instance handle in a global variable for later use */
351 hInst = hInstance;
353 /* Create main window */
354 hMainWnd = CreateWindowW(szAppName, /* See RegisterClass() call */
355 szTitle, /* window title */
356 WS_OVERLAPPEDWINDOW, /* Window style */
357 CW_USEDEFAULT, 0, /* positioning */
358 CW_USEDEFAULT, 0, /* size */
359 NULL, /* Overlapped has no parent */
360 NULL, /* Use the window class menu */
361 hInstance,
362 NULL);
364 if (!hMainWnd)
365 return FALSE;
367 /* Call module specific instance initialization functions here */
369 /* show the window, and paint it for the first time */
370 ShowWindow(hMainWnd, nCmdShow);
371 UpdateWindow(hMainWnd);
373 return TRUE;
376 static void HandleCommandLine(LPWSTR cmdline)
378 /* skip white space */
379 while (*cmdline == ' ') cmdline++;
381 if (*cmdline)
383 /* file name is passed on the command line */
384 if (cmdline[0] == '"')
386 cmdline++;
387 cmdline[lstrlenW(cmdline) - 1] = 0;
389 szFileTitle[0] = 0;
390 GetFileTitleW(cmdline, szFileTitle, sizeof(szFileTitle));
391 DoOpenFile(cmdline);
392 UpdateWindowCaption();
396 int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
398 MSG msg;
400 /* Other instances of app running? */
401 if (!hPrevInstance)
403 /* stuff to be done once */
404 if (!InitApplication(hInstance))
406 return FALSE; /* exit */
410 /* stuff to be done every time */
411 if (!InitInstance(hInstance, nCmdShow))
413 return FALSE;
416 HandleCommandLine(lpCmdLine);
418 /* Main loop */
419 /* Acquire and dispatch messages until a WM_QUIT message is received */
420 while (GetMessageW(&msg, NULL, 0, 0))
422 TranslateMessage(&msg);
423 DispatchMessageW(&msg);
426 return msg.wParam;