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
25 static HINSTANCE hInst
;
27 static WCHAR szAppName
[5] = {'V','i','e','w',0};
28 static WCHAR szTitle
[MAX_PATH
];
29 static WCHAR szFileTitle
[MAX_PATH
];
32 static HENHMETAFILE enhmf
;
33 static int deltax
= 0, deltay
= 0;
34 static int width
= 0, height
= 0;
35 static BOOL isAldus
, isEnhanced
;
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
;
65 return GetOpenFileNameW(&ofn
);
68 static BOOL
FileIsEnhanced( LPCWSTR szFileName
)
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
)
79 if (!ReadFile( handle
, &enh
, sizeof(ENHMETAHEADER
), &size
, NULL
) || size
!= sizeof(ENHMETAHEADER
) )
81 CloseHandle( handle
);
84 CloseHandle( handle
);
87 return (enh
.dSignature
== ENHMETA_SIGNATURE
);
90 static BOOL
FileIsPlaceable( LPCWSTR szFileName
)
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
)
101 if (!ReadFile( handle
, &apmh
, sizeof(APMFILEHEADER
), &size
, NULL
) || size
!= sizeof(APMFILEHEADER
))
103 CloseHandle( handle
);
106 CloseHandle( handle
);
108 /* Is it placeable? */
109 return (apmh
.key
== APMHEADER_KEY
);
112 static HMETAFILE
GetPlaceableMetaFile( LPCWSTR szFileName
)
116 APMFILEHEADER APMHeader
;
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
)
129 if (!ReadFile( handle
, &APMHeader
, sizeof(APMFILEHEADER
), &size
, NULL
) || size
!= sizeof(APMFILEHEADER
))
131 CloseHandle( handle
);
135 p
= (WORD
*) &APMHeader
;
139 if (checksum
!= APMHeader
.checksum
) {
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
);
148 if (!ReadFile( handle
, &mfHeader
, sizeof(METAHEADER
), &size
, NULL
) || size
!= sizeof(METAHEADER
))
150 CloseHandle( handle
);
154 if (!(lpData
= GlobalAlloc(GPTR
, (mfHeader
.mtSize
* 2L))))
156 CloseHandle( handle
);
160 SetFilePointer( handle
, sizeof(APMFILEHEADER
), NULL
, FILE_BEGIN
);
161 if (!ReadFile(handle
, lpData
, mfHeader
.mtSize
* 2, &size
, NULL
) || size
!= mfHeader
.mtSize
* 2)
164 CloseHandle( handle
);
167 CloseHandle( handle
);
169 if (!(hmf
= SetMetaFileBitsEx(mfHeader
.mtSize
*2, lpData
))) {
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
);
190 static void DoOpenFile(LPCWSTR filename
)
192 if (!filename
) return;
194 isAldus
= FileIsPlaceable(filename
);
196 hmf
= GetPlaceableMetaFile(filename
);
199 isEnhanced
= FileIsEnhanced(filename
);
201 enhmf
= GetEnhMetaFileW(filename
);
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
);
227 lstrcpyW(szCaption
, szView
);
229 SetWindowTextW(hMainWnd
, szCaption
);
232 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT uMessage
, WPARAM wparam
, LPARAM lparam
)
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
)
248 GetClientRect(hwnd
, &r
);
249 PlayEnhMetaFile(ps
.hdc
, enhmf
, &r
);
252 PlayMetaFile(ps
.hdc
, hmf
);
257 case WM_COMMAND
: /* message: command from application menu */
258 switch (LOWORD(wparam
))
262 WCHAR filename
[MAX_PATH
];
263 if (FileOpen(hwnd
, filename
, sizeof(filename
)/sizeof(WCHAR
)))
266 GetFileTitleW(filename
, szFileTitle
, sizeof(szFileTitle
)/sizeof(WCHAR
));
267 DoOpenFile(filename
);
268 UpdateWindowCaption();
273 case IDM_SET_EXT_TO_WIN
:
276 GetClientRect(hwnd
, &r
);
277 width
= r
.right
- r
.left
;
278 height
= r
.bottom
- r
.top
;
280 InvalidateRect( hwnd
, NULL
, TRUE
);
287 InvalidateRect( hwnd
, NULL
, TRUE
);
291 InvalidateRect( hwnd
, NULL
, TRUE
);
295 InvalidateRect( hwnd
, NULL
, TRUE
);
299 InvalidateRect( hwnd
, NULL
, TRUE
);
307 return DefWindowProcW(hwnd
, uMessage
, wparam
, lparam
);
311 case WM_DESTROY
: /* message: window being destroyed */
315 default: /* Passes it on if unprocessed */
316 return DefWindowProcW(hwnd
, uMessage
, wparam
, lparam
);
321 static BOOL
InitApplication(HINSTANCE hInstance
)
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
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 */
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 */
351 static BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
353 /* Save the instance handle in a global variable for later use */
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 */
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
);
379 static void HandleCommandLine(LPWSTR cmdline
)
381 /* skip white space */
382 while (*cmdline
== ' ') cmdline
++;
386 /* file name is passed on the command line */
387 if (cmdline
[0] == '"')
390 cmdline
[lstrlenW(cmdline
) - 1] = 0;
393 GetFileTitleW(cmdline
, szFileTitle
, sizeof(szFileTitle
)/sizeof(WCHAR
));
395 UpdateWindowCaption();
399 int APIENTRY
wWinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPWSTR lpCmdLine
, int nCmdShow
)
403 /* Other instances of app running? */
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
))
419 HandleCommandLine(lpCmdLine
);
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
);