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
)))
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
);
187 static void DoOpenFile(LPCWSTR filename
)
189 if (!filename
) return;
191 isAldus
= FileIsPlaceable(filename
);
193 hmf
= GetPlaceableMetaFile(filename
);
196 isEnhanced
= FileIsEnhanced(filename
);
198 enhmf
= GetEnhMetaFileW(filename
);
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
);
224 lstrcpyW(szCaption
, szView
);
226 SetWindowTextW(hMainWnd
, szCaption
);
229 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT uMessage
, WPARAM wparam
, LPARAM lparam
)
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
)
245 GetClientRect(hwnd
, &r
);
246 PlayEnhMetaFile(ps
.hdc
, enhmf
, &r
);
249 PlayMetaFile(ps
.hdc
, hmf
);
254 case WM_COMMAND
: /* message: command from application menu */
255 switch (LOWORD(wparam
))
259 WCHAR filename
[MAX_PATH
];
260 if (FileOpen(hwnd
, filename
, sizeof(filename
)/sizeof(WCHAR
)))
263 GetFileTitleW(filename
, szFileTitle
, sizeof(szFileTitle
));
264 DoOpenFile(filename
);
265 UpdateWindowCaption();
270 case IDM_SET_EXT_TO_WIN
:
273 GetClientRect(hwnd
, &r
);
274 width
= r
.right
- r
.left
;
275 height
= r
.bottom
- r
.top
;
277 InvalidateRect( hwnd
, NULL
, TRUE
);
284 InvalidateRect( hwnd
, NULL
, TRUE
);
288 InvalidateRect( hwnd
, NULL
, TRUE
);
292 InvalidateRect( hwnd
, NULL
, TRUE
);
296 InvalidateRect( hwnd
, NULL
, TRUE
);
304 return DefWindowProcW(hwnd
, uMessage
, wparam
, lparam
);
308 case WM_DESTROY
: /* message: window being destroyed */
312 default: /* Passes it on if unprocessed */
313 return DefWindowProcW(hwnd
, uMessage
, wparam
, lparam
);
318 static BOOL
InitApplication(HINSTANCE hInstance
)
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
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 */
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 */
348 static BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
350 /* Save the instance handle in a global variable for later use */
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 */
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
);
376 static void HandleCommandLine(LPWSTR cmdline
)
378 /* skip white space */
379 while (*cmdline
== ' ') cmdline
++;
383 /* file name is passed on the command line */
384 if (cmdline
[0] == '"')
387 cmdline
[lstrlenW(cmdline
) - 1] = 0;
390 GetFileTitleW(cmdline
, szFileTitle
, sizeof(szFileTitle
));
392 UpdateWindowCaption();
396 int APIENTRY
wWinMain(HINSTANCE hInstance
, HINSTANCE hPrevInstance
, LPWSTR lpCmdLine
, int nCmdShow
)
400 /* Other instances of app running? */
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
))
416 HandleCommandLine(lpCmdLine
);
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
);