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
26 static HINSTANCE hInst
;
28 static const WCHAR szAppName
[] = L
"View";
29 static WCHAR szTitle
[MAX_PATH
];
30 static WCHAR szFileTitle
[MAX_PATH
];
33 static HENHMETAFILE enhmf
;
34 static int deltax
= 0, deltay
= 0;
35 static int width
= 0, height
= 0;
36 static BOOL isAldus
, isEnhanced
;
50 #define APMHEADER_KEY 0x9AC6CDD7l
53 static BOOL
FileOpen(HWND hWnd
, WCHAR
*fn
, int fnsz
)
55 WCHAR filter
[120], metafileFilter
[100];
56 OPENFILENAMEW ofn
= { sizeof(OPENFILENAMEW
),
57 0, 0, NULL
, NULL
, 0, 0, NULL
,
58 fnsz
, NULL
, 0, NULL
, NULL
,
59 OFN_SHOWHELP
, 0, 0, NULL
, 0, NULL
};
61 LoadStringW( hInst
, IDS_OPEN_META_STRING
, metafileFilter
, ARRAY_SIZE(metafileFilter
) );
62 swprintf( filter
, ARRAY_SIZE(filter
), L
"%s%c*.wmf;*.emf%c", metafileFilter
, 0, 0 );
64 ofn
.lpstrFilter
= filter
;
70 return GetOpenFileNameW(&ofn
);
73 static BOOL
FileIsEnhanced( LPCWSTR szFileName
)
79 handle
= CreateFileW( szFileName
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
80 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0 );
81 if (handle
== INVALID_HANDLE_VALUE
)
84 if (!ReadFile( handle
, &enh
, sizeof(ENHMETAHEADER
), &size
, NULL
) || size
!= sizeof(ENHMETAHEADER
) )
86 CloseHandle( handle
);
89 CloseHandle( handle
);
92 return (enh
.dSignature
== ENHMETA_SIGNATURE
);
95 static BOOL
FileIsPlaceable( LPCWSTR szFileName
)
101 handle
= CreateFileW( szFileName
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
102 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0 );
103 if (handle
== INVALID_HANDLE_VALUE
)
106 if (!ReadFile( handle
, &apmh
, sizeof(APMFILEHEADER
), &size
, NULL
) || size
!= sizeof(APMFILEHEADER
))
108 CloseHandle( handle
);
111 CloseHandle( handle
);
113 /* Is it placeable? */
114 return (apmh
.key
== APMHEADER_KEY
);
117 static HMETAFILE
GetPlaceableMetaFile( LPCWSTR szFileName
)
121 APMFILEHEADER APMHeader
;
129 handle
= CreateFileW( szFileName
, GENERIC_READ
, FILE_SHARE_READ
| FILE_SHARE_WRITE
,
130 NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_NORMAL
, 0 );
131 if (handle
== INVALID_HANDLE_VALUE
)
134 if (!ReadFile( handle
, &APMHeader
, sizeof(APMFILEHEADER
), &size
, NULL
) || size
!= sizeof(APMFILEHEADER
))
136 CloseHandle( handle
);
140 p
= (WORD
*) &APMHeader
;
144 if (checksum
!= APMHeader
.checksum
) {
146 sprintf(msg
, "Computed checksum %04x != stored checksum %04x\n",
147 checksum
, APMHeader
.checksum
);
148 MessageBoxA(hMainWnd
, msg
, "Checksum failed", MB_OK
);
149 CloseHandle( handle
);
153 if (!ReadFile( handle
, &mfHeader
, sizeof(METAHEADER
), &size
, NULL
) || size
!= sizeof(METAHEADER
))
155 CloseHandle( handle
);
159 if (!(lpData
= GlobalAlloc(GPTR
, (mfHeader
.mtSize
* 2L))))
161 CloseHandle( handle
);
165 SetFilePointer( handle
, sizeof(APMFILEHEADER
), NULL
, FILE_BEGIN
);
166 if (!ReadFile(handle
, lpData
, mfHeader
.mtSize
* 2, &size
, NULL
) || size
!= mfHeader
.mtSize
* 2)
169 CloseHandle( handle
);
172 CloseHandle( handle
);
174 if (!(hmf
= SetMetaFileBitsEx(mfHeader
.mtSize
*2, lpData
))) {
180 width
= APMHeader
.bbox
.Right
- APMHeader
.bbox
.Left
;
181 height
= APMHeader
.bbox
.Bottom
- APMHeader
.bbox
.Top
;
183 /* printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch); */
184 hdc
= GetDC(hMainWnd
);
185 width
= width
* GetDeviceCaps(hdc
, LOGPIXELSX
)/APMHeader
.inch
;
186 height
= height
* GetDeviceCaps(hdc
,LOGPIXELSY
)/APMHeader
.inch
;
187 ReleaseDC(hMainWnd
, hdc
);
195 static void DoOpenFile(LPCWSTR filename
)
197 if (!filename
) return;
199 isAldus
= FileIsPlaceable(filename
);
201 hmf
= GetPlaceableMetaFile(filename
);
204 isEnhanced
= FileIsEnhanced(filename
);
206 enhmf
= GetEnhMetaFileW(filename
);
208 hmf
= GetMetaFileW(filename
);
209 GetClientRect(hMainWnd
, &r
);
210 width
= r
.right
- r
.left
;
211 height
= r
.bottom
- r
.top
;
213 InvalidateRect( hMainWnd
, NULL
, TRUE
);
216 static void UpdateWindowCaption(void)
218 WCHAR szCaption
[MAX_PATH
];
219 WCHAR szView
[MAX_PATH
];
221 LoadStringW(hInst
, IDS_DESCRIPTION
, szView
, ARRAY_SIZE(szView
));
223 if (szFileTitle
[0] != '\0')
225 lstrcpyW(szCaption
, szFileTitle
);
226 LoadStringW(hInst
, IDS_DESCRIPTION
, szView
, ARRAY_SIZE(szView
));
227 lstrcatW(szCaption
, L
" - ");
228 lstrcatW(szCaption
, szView
);
231 lstrcpyW(szCaption
, szView
);
233 SetWindowTextW(hMainWnd
, szCaption
);
236 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT uMessage
, WPARAM wparam
, LPARAM lparam
)
243 BeginPaint(hwnd
, &ps
);
244 SetMapMode(ps
.hdc
, MM_ANISOTROPIC
);
245 /* Set the window extent to a sane value in case the metafile doesn't */
246 SetWindowExtEx(ps
.hdc
, width
, height
, NULL
);
247 SetViewportExtEx(ps
.hdc
, width
, height
, NULL
);
248 SetViewportOrgEx(ps
.hdc
, deltax
, deltay
, NULL
);
249 if (isEnhanced
&& enhmf
)
252 GetClientRect(hwnd
, &r
);
253 PlayEnhMetaFile(ps
.hdc
, enhmf
, &r
);
256 PlayMetaFile(ps
.hdc
, hmf
);
261 case WM_COMMAND
: /* message: command from application menu */
262 switch (LOWORD(wparam
))
266 WCHAR filename
[MAX_PATH
];
267 if (FileOpen(hwnd
, filename
, ARRAY_SIZE(filename
)))
270 GetFileTitleW(filename
, szFileTitle
, ARRAY_SIZE(szFileTitle
));
271 DoOpenFile(filename
);
272 UpdateWindowCaption();
277 case IDM_SET_EXT_TO_WIN
:
280 GetClientRect(hwnd
, &r
);
281 width
= r
.right
- r
.left
;
282 height
= r
.bottom
- r
.top
;
284 InvalidateRect( hwnd
, NULL
, TRUE
);
291 InvalidateRect( hwnd
, NULL
, TRUE
);
295 InvalidateRect( hwnd
, NULL
, TRUE
);
299 InvalidateRect( hwnd
, NULL
, TRUE
);
303 InvalidateRect( hwnd
, NULL
, TRUE
);
311 return DefWindowProcW(hwnd
, uMessage
, wparam
, lparam
);
315 case WM_DESTROY
: /* message: window being destroyed */
319 default: /* Passes it on if unprocessed */
320 return DefWindowProcW(hwnd
, uMessage
, wparam
, lparam
);
325 static BOOL
InitApplication(HINSTANCE hInstance
)
329 /* Load the application description strings */
330 LoadStringW(hInstance
, IDS_DESCRIPTION
, szTitle
, ARRAY_SIZE(szTitle
));
332 /* Fill in window class structure with parameters that describe the
335 wc
.cbSize
= sizeof(WNDCLASSEXW
);
336 wc
.style
= CS_HREDRAW
| CS_VREDRAW
; /* Class style(s) */
337 wc
.lpfnWndProc
= WndProc
; /* Window Procedure */
338 wc
.cbClsExtra
= 0; /* No per-class extra data */
339 wc
.cbWndExtra
= 0; /* No per-window extra data */
340 wc
.hInstance
= hInstance
; /* Owner of this class */
343 wc
.hCursor
= LoadCursorW(NULL
, (LPWSTR
)IDC_ARROW
);
344 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1); /* Default color */
345 wc
.lpszMenuName
= szAppName
; /* Menu name from .rc */
346 wc
.lpszClassName
= szAppName
; /* Name to register as */
348 if (!RegisterClassExW(&wc
)) return FALSE
;
350 /* Call module specific initialization functions here */
355 static BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
357 /* Save the instance handle in a global variable for later use */
360 /* Create main window */
361 hMainWnd
= CreateWindowW(szAppName
, /* See RegisterClass() call */
362 szTitle
, /* window title */
363 WS_OVERLAPPEDWINDOW
, /* Window style */
364 CW_USEDEFAULT
, 0, /* positioning */
365 CW_USEDEFAULT
, 0, /* size */
366 NULL
, /* Overlapped has no parent */
367 NULL
, /* Use the window class menu */
374 /* Call module specific instance initialization functions here */
376 /* show the window, and paint it for the first time */
377 ShowWindow(hMainWnd
, nCmdShow
);
378 UpdateWindow(hMainWnd
);
383 static void HandleCommandLine(LPWSTR cmdline
)
385 /* skip white space */
386 while (*cmdline
== ' ') cmdline
++;
390 /* file name is passed on the command line */
391 if (cmdline
[0] == '"')
394 cmdline
[lstrlenW(cmdline
) - 1] = 0;
397 GetFileTitleW(cmdline
, szFileTitle
, ARRAY_SIZE(szFileTitle
));
399 UpdateWindowCaption();
403 int APIENTRY
wWinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPWSTR lpCmdLine
, int nCmdShow
)
407 InitCommonControls();
409 /* Other instances of app running? */
412 /* stuff to be done once */
413 if (!InitApplication(hInstance
))
415 return FALSE
; /* exit */
419 /* stuff to be done every time */
420 if (!InitInstance(hInstance
, nCmdShow
))
425 HandleCommandLine(lpCmdLine
);
428 /* Acquire and dispatch messages until a WM_QUIT message is received */
429 while (GetMessageW(&msg
, NULL
, 0, 0))
431 TranslateMessage(&msg
);
432 DispatchMessageW(&msg
);