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
;
26 static char szAppName
[5] = "View";
27 static char szTitle
[80];
30 static HENHMETAFILE enhmf
;
31 static int deltax
= 0, deltay
= 0;
32 static int width
= 0, height
= 0;
33 static BOOL isAldus
, isEnhanced
;
47 #define APMHEADER_KEY 0x9AC6CDD7l
50 static BOOL
FileOpen(HWND hWnd
, char *fn
, int fnsz
)
52 OPENFILENAME ofn
= { sizeof(OPENFILENAME
),
53 0, 0, NULL
, NULL
, 0, 0, NULL
,
54 fnsz
, NULL
, 0, NULL
, NULL
,
55 OFN_SHOWHELP
, 0, 0, NULL
, 0, NULL
};
56 ofn
.lpstrFilter
= "Metafiles\0*.wmf;*.emf\0";
62 return GetOpenFileName(&ofn
);
65 static BOOL
FileIsEnhanced( LPCSTR szFileName
)
70 if( (hInFile
= _lopen( szFileName
, OF_READ
) ) == HFILE_ERROR
)
73 if( _lread( hInFile
, &enh
, sizeof(ENHMETAHEADER
) ) != sizeof(ENHMETAHEADER
) )
81 return (enh
.dSignature
== ENHMETA_SIGNATURE
);
84 static BOOL
FileIsPlaceable( LPCSTR szFileName
)
89 if( (hInFile
= _lopen( szFileName
, OF_READ
) ) == HFILE_ERROR
)
92 if( _lread( hInFile
, &apmh
, sizeof(APMFILEHEADER
) )
93 != sizeof(APMFILEHEADER
) )
100 /* Is it placeable? */
101 return (apmh
.key
== APMHEADER_KEY
);
104 static HMETAFILE
GetPlaceableMetaFile( HWND hwnd
, LPCSTR szFileName
)
108 APMFILEHEADER APMHeader
;
115 if( (fh
= _lopen( szFileName
, OF_READ
) ) == HFILE_ERROR
) return 0;
117 if (!_lread(fh
, &APMHeader
, sizeof(APMFILEHEADER
))) return 0;
118 _llseek(fh
, sizeof(APMFILEHEADER
), 0);
120 p
= (WORD
*) &APMHeader
;
124 if (checksum
!= APMHeader
.checksum
) {
126 sprintf(msg
, "Computed checksum %04x != stored checksum %04x\n",
127 checksum
, APMHeader
.checksum
);
128 MessageBox(hwnd
, msg
, "Checksum failed", MB_OK
);
132 if (!_lread(fh
, &mfHeader
, sizeof(METAHEADER
))) return 0;
134 if (!(lpData
= GlobalAlloc(GPTR
, (mfHeader
.mtSize
* 2L)))) return 0;
136 _llseek(fh
, sizeof(APMFILEHEADER
), 0);
137 if (!_lread(fh
, lpData
, (UINT
)(mfHeader
.mtSize
* 2L)))
145 if (!(hmf
= SetMetaFileBitsEx(mfHeader
.mtSize
*2, lpData
)))
149 width
= APMHeader
.bbox
.Right
- APMHeader
.bbox
.Left
;
150 height
= APMHeader
.bbox
.Bottom
- APMHeader
.bbox
.Top
;
152 /* printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch); */
154 width
= width
* GetDeviceCaps(hdc
, LOGPIXELSX
)/APMHeader
.inch
;
155 height
= height
* GetDeviceCaps(hdc
,LOGPIXELSY
)/APMHeader
.inch
;
156 ReleaseDC(hwnd
, hdc
);
164 static LRESULT CALLBACK
WndProc(HWND hwnd
, UINT uMessage
, WPARAM wparam
, LPARAM lparam
)
171 BeginPaint(hwnd
, &ps
);
172 SetMapMode(ps
.hdc
, MM_ANISOTROPIC
);
173 /* Set the window extent to a sane value in case the metafile doesn't */
174 SetWindowExtEx(ps
.hdc
, width
, height
, NULL
);
175 SetViewportExtEx(ps
.hdc
, width
, height
, NULL
);
176 SetViewportOrgEx(ps
.hdc
, deltax
, deltay
, NULL
);
177 if (isEnhanced
&& enhmf
)
180 GetClientRect(hwnd
, &r
);
181 PlayEnhMetaFile(ps
.hdc
, enhmf
, &r
);
184 PlayMetaFile(ps
.hdc
, hmf
);
189 case WM_COMMAND
: /* message: command from application menu */
190 switch (GET_WM_COMMAND_ID(wparam
,lparam
))
194 char filename
[MAX_PATH
];
195 if (FileOpen(hwnd
, filename
, sizeof(filename
))) {
196 isAldus
= FileIsPlaceable(filename
);
198 hmf
= GetPlaceableMetaFile(hwnd
, filename
);
201 isEnhanced
= FileIsEnhanced(filename
);
203 enhmf
= GetEnhMetaFile(filename
);
205 hmf
= GetMetaFile(filename
);
206 GetClientRect(hwnd
, &r
);
207 width
= r
.right
- r
.left
;
208 height
= r
.bottom
- r
.top
;
210 InvalidateRect( hwnd
, NULL
, TRUE
);
215 case IDM_SET_EXT_TO_WIN
:
218 GetClientRect(hwnd
, &r
);
219 width
= r
.right
- r
.left
;
220 height
= r
.bottom
- r
.top
;
222 InvalidateRect( hwnd
, NULL
, TRUE
);
229 InvalidateRect( hwnd
, NULL
, TRUE
);
233 InvalidateRect( hwnd
, NULL
, TRUE
);
237 InvalidateRect( hwnd
, NULL
, TRUE
);
241 InvalidateRect( hwnd
, NULL
, TRUE
);
249 return DefWindowProc(hwnd
, uMessage
, wparam
, lparam
);
253 case WM_DESTROY
: /* message: window being destroyed */
257 default: /* Passes it on if unprocessed */
258 return DefWindowProc(hwnd
, uMessage
, wparam
, lparam
);
263 static BOOL
InitApplication(HINSTANCE hInstance
)
267 /* Load the application description strings */
268 LoadString(hInstance
, IDS_DESCRIPTION
, szTitle
, sizeof(szTitle
));
270 /* Fill in window class structure with parameters that describe the
273 wc
.cbSize
= sizeof(WNDCLASSEX
);
275 /* Load small icon image */
276 wc
.hIconSm
= LoadImage(hInstance
, MAKEINTRESOURCEA(IDI_APPICON
), IMAGE_ICON
, 16, 16, 0);
278 wc
.style
= CS_HREDRAW
| CS_VREDRAW
; /* Class style(s) */
279 wc
.lpfnWndProc
= WndProc
; /* Window Procedure */
280 wc
.cbClsExtra
= 0; /* No per-class extra data */
281 wc
.cbWndExtra
= 0; /* No per-window extra data */
282 wc
.hInstance
= hInstance
; /* Owner of this class */
283 wc
.hIcon
= LoadIcon(hInstance
, szAppName
); /* Icon name from .rc */
284 wc
.hCursor
= LoadCursor(NULL
, IDC_ARROW
); /* Cursor */
285 wc
.hbrBackground
= (HBRUSH
)(COLOR_WINDOW
+ 1); /* Default color */
286 wc
.lpszMenuName
= szAppName
; /* Menu name from .rc */
287 wc
.lpszClassName
= szAppName
; /* Name to register as */
289 /* Register the window class and return FALSE if unsuccessful */
291 if (!RegisterClassEx(&wc
))
293 if (!RegisterClass((LPWNDCLASS
)&wc
.style
))
297 /* Call module specific initialization functions here */
302 static BOOL
InitInstance(HINSTANCE hInstance
, int nCmdShow
)
306 /* Save the instance handle in a global variable for later use */
309 /* Create main window */
310 hwnd
= CreateWindow(szAppName
, /* See RegisterClass() call */
311 szTitle
, /* window title */
312 WS_OVERLAPPEDWINDOW
, /* Window style */
313 CW_USEDEFAULT
, 0, /* positioning */
314 CW_USEDEFAULT
, 0, /* size */
315 NULL
, /* Overlapped has no parent */
316 NULL
, /* Use the window class menu */
323 /* Call module specific instance initialization functions here */
325 /* show the window, and paint it for the first time */
326 ShowWindow(hwnd
, nCmdShow
);
332 int APIENTRY
WinMain(HINSTANCE hInstance
,
333 HINSTANCE hPrevInstance
,
340 /* Other instances of app running? */
343 /* stuff to be done once */
344 if (!InitApplication(hInstance
))
346 return FALSE
; /* exit */
350 /* stuff to be done every time */
351 if (!InitInstance(hInstance
, nCmdShow
))
356 hAccelTable
= LoadAccelerators(hInstance
, szAppName
);
359 /* Acquire and dispatch messages until a WM_QUIT message is received */
360 while (GetMessage(&msg
, NULL
, 0, 0))
362 /* Add other Translation functions (for modeless dialogs
363 and/or MDI windows) here. */
365 if (!TranslateAccelerator(msg
.hwnd
, hAccelTable
, &msg
))
367 TranslateMessage(&msg
);
368 DispatchMessage(&msg
);
372 /* Add module specific instance free/delete functions here */
374 /* Returns the value from PostQuitMessage */