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 int deltax = 0, deltay = 0;
31 static int width = 0, height = 0;
46 #define APMHEADER_KEY 0x9AC6CDD7l
49 static BOOL FileOpen(HWND hWnd, char *fn, int fnsz)
51 OPENFILENAME ofn = { sizeof(OPENFILENAME),
52 0, 0, NULL, NULL, 0, 0, NULL,
53 fnsz, NULL, 0, NULL, NULL,
54 OFN_SHOWHELP, 0, 0, NULL, 0, NULL };
55 ofn.lpstrFilter = "Metafiles\0*.wmf\0";
61 return GetOpenFileName(&ofn);
64 static BOOL FileIsPlaceable( LPCSTR szFileName )
69 if( (hInFile = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR )
72 if( _lread( hInFile, &apmh, sizeof(APMFILEHEADER) )
73 != sizeof(APMFILEHEADER) )
80 /* Is it placeable? */
81 return (apmh.key == APMHEADER_KEY);
84 static HMETAFILE GetPlaceableMetaFile( HWND hwnd, LPCSTR szFileName )
88 APMFILEHEADER APMHeader;
95 if( (fh = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR ) return 0;
97 if (!_lread(fh, &APMHeader, sizeof(APMFILEHEADER))) return 0;
98 _llseek(fh, sizeof(APMFILEHEADER), 0);
100 p = (WORD *) &APMHeader;
104 if (checksum != APMHeader.checksum) {
106 sprintf(msg, "Computed checksum %04x != stored checksum %04x\n",
107 checksum, APMHeader.checksum);
108 MessageBox(hwnd, msg, "Checksum failed", MB_OK);
112 if (!_lread(fh, &mfHeader, sizeof(METAHEADER))) return 0;
114 if (!(lpData = GlobalAlloc(GPTR, (mfHeader.mtSize * 2L)))) return 0;
116 _llseek(fh, sizeof(APMFILEHEADER), 0);
117 if (!_lread(fh, lpData, (UINT)(mfHeader.mtSize * 2L)))
125 if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData)))
129 width = APMHeader.bbox.Right - APMHeader.bbox.Left;
130 height = APMHeader.bbox.Bottom - APMHeader.bbox.Top;
132 /* printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch); */
134 width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch;
135 height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch;
136 ReleaseDC(hwnd, hdc);
144 static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
151 BeginPaint(hwnd, &ps);
152 SetMapMode(ps.hdc, MM_ANISOTROPIC);
153 /* Set the window extent to a sane value in case the metafile doesn't */
154 SetWindowExtEx(ps.hdc, width, height, NULL);
155 SetViewportExtEx(ps.hdc, width, height, NULL);
156 SetViewportOrgEx(ps.hdc, deltax, deltay, NULL);
157 if(hmf) PlayMetaFile(ps.hdc, hmf);
162 case WM_COMMAND: /* message: command from application menu */
163 switch (GET_WM_COMMAND_ID(wparam,lparam))
167 char filename[MAX_PATH];
168 if (FileOpen(hwnd, filename, sizeof(filename))) {
169 isAldus = FileIsPlaceable(filename);
171 hmf = GetPlaceableMetaFile(hwnd, filename);
174 hmf = GetMetaFile(filename);
175 GetClientRect(hwnd, &r);
176 width = r.right - r.left;
177 height = r.bottom - r.top;
179 InvalidateRect( hwnd, NULL, TRUE );
184 case IDM_SET_EXT_TO_WIN:
187 GetClientRect(hwnd, &r);
188 width = r.right - r.left;
189 height = r.bottom - r.top;
191 InvalidateRect( hwnd, NULL, TRUE );
198 InvalidateRect( hwnd, NULL, TRUE );
202 InvalidateRect( hwnd, NULL, TRUE );
206 InvalidateRect( hwnd, NULL, TRUE );
210 InvalidateRect( hwnd, NULL, TRUE );
218 return DefWindowProc(hwnd, uMessage, wparam, lparam);
222 case WM_DESTROY: /* message: window being destroyed */
226 default: /* Passes it on if unprocessed */
227 return DefWindowProc(hwnd, uMessage, wparam, lparam);
232 static BOOL InitApplication(HINSTANCE hInstance)
236 /* Load the application description strings */
237 LoadString(hInstance, IDS_DESCRIPTION, szTitle, sizeof(szTitle));
239 /* Fill in window class structure with parameters that describe the
242 wc.cbSize = sizeof(WNDCLASSEX);
244 /* Load small icon image */
245 wc.hIconSm = LoadImage(hInstance, MAKEINTRESOURCEA(IDI_APPICON), IMAGE_ICON, 16, 16, 0);
247 wc.style = CS_HREDRAW | CS_VREDRAW; /* Class style(s) */
248 wc.lpfnWndProc = WndProc; /* Window Procedure */
249 wc.cbClsExtra = 0; /* No per-class extra data */
250 wc.cbWndExtra = 0; /* No per-window extra data */
251 wc.hInstance = hInstance; /* Owner of this class */
252 wc.hIcon = LoadIcon(hInstance, szAppName); /* Icon name from .rc */
253 wc.hCursor = LoadCursor(NULL, IDC_ARROW); /* Cursor */
254 wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); /* Default color */
255 wc.lpszMenuName = szAppName; /* Menu name from .rc */
256 wc.lpszClassName = szAppName; /* Name to register as */
258 /* Register the window class and return FALSE if unsuccessful */
260 if (!RegisterClassEx(&wc))
262 if (!RegisterClass((LPWNDCLASS)&wc.style))
266 /* Call module specific initialization functions here */
271 static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
275 /* Save the instance handle in a global variable for later use */
278 /* Create main window */
279 hwnd = CreateWindow(szAppName, /* See RegisterClass() call */
280 szTitle, /* window title */
281 WS_OVERLAPPEDWINDOW, /* Window style */
282 CW_USEDEFAULT, 0, /* positioning */
283 CW_USEDEFAULT, 0, /* size */
284 NULL, /* Overlapped has no parent */
285 NULL, /* Use the window class menu */
292 /* Call module specific instance initialization functions here */
294 /* show the window, and paint it for the first time */
295 ShowWindow(hwnd, nCmdShow);
301 int APIENTRY WinMain(HINSTANCE hInstance,
302 HINSTANCE hPrevInstance,
309 /* Other instances of app running? */
312 /* stuff to be done once */
313 if (!InitApplication(hInstance))
315 return FALSE; /* exit */
319 /* stuff to be done every time */
320 if (!InitInstance(hInstance, nCmdShow))
325 hAccelTable = LoadAccelerators(hInstance, szAppName);
328 /* Acquire and dispatch messages until a WM_QUIT message is received */
329 while (GetMessage(&msg, NULL, 0, 0))
331 /* Add other Translation functions (for modeless dialogs
332 and/or MDI windows) here. */
334 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
336 TranslateMessage(&msg);
337 DispatchMessage(&msg);
341 /* Add module specific instance free/delete functions here */
343 /* Returns the value from PostQuitMessage */