qcap/tests: Add media tests for the SmartTee filter.
[wine/multimedia.git] / programs / view / view.c
blob0adade711189b4c9b89bff3156588f94746820e9
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 GlobalFree(lpData);
171 return 0;
175 width = APMHeader.bbox.Right - APMHeader.bbox.Left;
176 height = APMHeader.bbox.Bottom - APMHeader.bbox.Top;
178 /* printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch); */
179 hdc = GetDC(hMainWnd);
180 width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch;
181 height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch;
182 ReleaseDC(hMainWnd, hdc);
184 deltax = 0;
185 deltay = 0 ;
186 GlobalFree(lpData);
187 return hmf;
190 static void DoOpenFile(LPCWSTR filename)
192 if (!filename) return;
194 isAldus = FileIsPlaceable(filename);
195 if (isAldus) {
196 hmf = GetPlaceableMetaFile(filename);
197 } else {
198 RECT r;
199 isEnhanced = FileIsEnhanced(filename);
200 if (isEnhanced)
201 enhmf = GetEnhMetaFileW(filename);
202 else
203 hmf = GetMetaFileW(filename);
204 GetClientRect(hMainWnd, &r);
205 width = r.right - r.left;
206 height = r.bottom - r.top;
208 InvalidateRect( hMainWnd, NULL, TRUE );
211 static void UpdateWindowCaption(void)
213 WCHAR szCaption[MAX_PATH];
214 WCHAR szView[MAX_PATH];
215 static const WCHAR hyphenW[] = { ' ','-',' ',0 };
217 LoadStringW(hInst, IDS_DESCRIPTION, szView, sizeof(szView)/sizeof(WCHAR));
219 if (szFileTitle[0] != '\0')
221 lstrcpyW(szCaption, szFileTitle);
222 LoadStringW(hInst, IDS_DESCRIPTION, szView, sizeof(szView)/sizeof(WCHAR));
223 lstrcatW(szCaption, hyphenW);
224 lstrcatW(szCaption, szView);
226 else
227 lstrcpyW(szCaption, szView);
229 SetWindowTextW(hMainWnd, szCaption);
232 static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
234 switch (uMessage)
236 case WM_PAINT:
238 PAINTSTRUCT ps;
239 BeginPaint(hwnd, &ps);
240 SetMapMode(ps.hdc, MM_ANISOTROPIC);
241 /* Set the window extent to a sane value in case the metafile doesn't */
242 SetWindowExtEx(ps.hdc, width, height, NULL);
243 SetViewportExtEx(ps.hdc, width, height, NULL);
244 SetViewportOrgEx(ps.hdc, deltax, deltay, NULL);
245 if (isEnhanced && enhmf)
247 RECT r;
248 GetClientRect(hwnd, &r);
249 PlayEnhMetaFile(ps.hdc, enhmf, &r);
251 else if (hmf)
252 PlayMetaFile(ps.hdc, hmf);
253 EndPaint(hwnd, &ps);
255 break;
257 case WM_COMMAND: /* message: command from application menu */
258 switch (LOWORD(wparam))
260 case IDM_OPEN:
262 WCHAR filename[MAX_PATH];
263 if (FileOpen(hwnd, filename, sizeof(filename)/sizeof(WCHAR)))
265 szFileTitle[0] = 0;
266 GetFileTitleW(filename, szFileTitle, sizeof(szFileTitle)/sizeof(WCHAR));
267 DoOpenFile(filename);
268 UpdateWindowCaption();
271 break;
273 case IDM_SET_EXT_TO_WIN:
275 RECT r;
276 GetClientRect(hwnd, &r);
277 width = r.right - r.left;
278 height = r.bottom - r.top;
279 deltax = deltay = 0;
280 InvalidateRect( hwnd, NULL, TRUE );
282 break;
285 case IDM_LEFT:
286 deltax += 100;
287 InvalidateRect( hwnd, NULL, TRUE );
288 break;
289 case IDM_RIGHT:
290 deltax -= 100;
291 InvalidateRect( hwnd, NULL, TRUE );
292 break;
293 case IDM_UP:
294 deltay += 100;
295 InvalidateRect( hwnd, NULL, TRUE );
296 break;
297 case IDM_DOWN:
298 deltay -= 100;
299 InvalidateRect( hwnd, NULL, TRUE );
300 break;
302 case IDM_EXIT:
303 DestroyWindow(hwnd);
304 break;
306 default:
307 return DefWindowProcW(hwnd, uMessage, wparam, lparam);
309 break;
311 case WM_DESTROY: /* message: window being destroyed */
312 PostQuitMessage(0);
313 break;
315 default: /* Passes it on if unprocessed */
316 return DefWindowProcW(hwnd, uMessage, wparam, lparam);
318 return 0;
321 static BOOL InitApplication(HINSTANCE hInstance)
323 WNDCLASSEXW wc;
325 /* Load the application description strings */
326 LoadStringW(hInstance, IDS_DESCRIPTION, szTitle, sizeof(szTitle)/sizeof(WCHAR));
328 /* Fill in window class structure with parameters that describe the
329 main window */
331 wc.cbSize = sizeof(WNDCLASSEXW);
332 wc.style = CS_HREDRAW | CS_VREDRAW; /* Class style(s) */
333 wc.lpfnWndProc = WndProc; /* Window Procedure */
334 wc.cbClsExtra = 0; /* No per-class extra data */
335 wc.cbWndExtra = 0; /* No per-window extra data */
336 wc.hInstance = hInstance; /* Owner of this class */
337 wc.hIcon = NULL;
338 wc.hIconSm = NULL;
339 wc.hCursor = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
340 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); /* Default color */
341 wc.lpszMenuName = szAppName; /* Menu name from .rc */
342 wc.lpszClassName = szAppName; /* Name to register as */
344 if (!RegisterClassExW(&wc)) return FALSE;
346 /* Call module specific initialization functions here */
348 return TRUE;
351 static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
353 /* Save the instance handle in a global variable for later use */
354 hInst = hInstance;
356 /* Create main window */
357 hMainWnd = CreateWindowW(szAppName, /* See RegisterClass() call */
358 szTitle, /* window title */
359 WS_OVERLAPPEDWINDOW, /* Window style */
360 CW_USEDEFAULT, 0, /* positioning */
361 CW_USEDEFAULT, 0, /* size */
362 NULL, /* Overlapped has no parent */
363 NULL, /* Use the window class menu */
364 hInstance,
365 NULL);
367 if (!hMainWnd)
368 return FALSE;
370 /* Call module specific instance initialization functions here */
372 /* show the window, and paint it for the first time */
373 ShowWindow(hMainWnd, nCmdShow);
374 UpdateWindow(hMainWnd);
376 return TRUE;
379 static void HandleCommandLine(LPWSTR cmdline)
381 /* skip white space */
382 while (*cmdline == ' ') cmdline++;
384 if (*cmdline)
386 /* file name is passed on the command line */
387 if (cmdline[0] == '"')
389 cmdline++;
390 cmdline[lstrlenW(cmdline) - 1] = 0;
392 szFileTitle[0] = 0;
393 GetFileTitleW(cmdline, szFileTitle, sizeof(szFileTitle)/sizeof(WCHAR));
394 DoOpenFile(cmdline);
395 UpdateWindowCaption();
399 int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
401 MSG msg;
403 /* Other instances of app running? */
404 if (!hPrevInstance)
406 /* stuff to be done once */
407 if (!InitApplication(hInstance))
409 return FALSE; /* exit */
413 /* stuff to be done every time */
414 if (!InitInstance(hInstance, nCmdShow))
416 return FALSE;
419 HandleCommandLine(lpCmdLine);
421 /* Main loop */
422 /* Acquire and dispatch messages until a WM_QUIT message is received */
423 while (GetMessageW(&msg, NULL, 0, 0))
425 TranslateMessage(&msg);
426 DispatchMessageW(&msg);
429 return msg.wParam;