ieframe: Moved string and menu resources to ieframe.
[wine/testsucceed.git] / programs / view / view.c
blob917784d03671a296eac8511842a1331bf07b2729
1 /*
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
19 #include <windows.h>
20 #include <windowsx.h>
21 #include "resource.h"
23 #include <stdio.h>
25 static HINSTANCE hInst;
26 static char szAppName[5] = "View";
27 static char szTitle[80];
29 static HMETAFILE hmf;
30 static HENHMETAFILE enhmf;
31 static int deltax = 0, deltay = 0;
32 static int width = 0, height = 0;
33 static BOOL isAldus, isEnhanced;
35 #include "pshpack1.h"
36 typedef struct
38 DWORD key;
39 WORD hmf;
40 SMALL_RECT bbox;
41 WORD inch;
42 DWORD reserved;
43 WORD checksum;
44 } APMFILEHEADER;
45 #include "poppack.h"
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";
57 ofn.hwndOwner = hWnd;
58 ofn.lpstrFile = fn;
59 if( fnsz < 1 )
60 return FALSE;
61 *fn = 0;
62 return GetOpenFileName(&ofn);
65 static BOOL FileIsEnhanced( LPCSTR szFileName )
67 HFILE hInFile;
68 ENHMETAHEADER enh;
70 if( (hInFile = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR )
71 return FALSE;
73 if( _lread( hInFile, &enh, sizeof(ENHMETAHEADER) ) != sizeof(ENHMETAHEADER) )
75 _lclose( hInFile );
76 return FALSE;
78 _lclose( hInFile );
80 /* Is it enhanced? */
81 return (enh.dSignature == ENHMETA_SIGNATURE);
84 static BOOL FileIsPlaceable( LPCSTR szFileName )
86 HFILE hInFile;
87 APMFILEHEADER apmh;
89 if( (hInFile = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR )
90 return FALSE;
92 if( _lread( hInFile, &apmh, sizeof(APMFILEHEADER) )
93 != sizeof(APMFILEHEADER) )
95 _lclose( hInFile );
96 return FALSE;
98 _lclose( hInFile );
100 /* Is it placeable? */
101 return (apmh.key == APMHEADER_KEY);
104 static HMETAFILE GetPlaceableMetaFile( HWND hwnd, LPCSTR szFileName )
106 LPBYTE lpData;
107 METAHEADER mfHeader;
108 APMFILEHEADER APMHeader;
109 HFILE fh;
110 HMETAFILE hmf;
111 WORD checksum, *p;
112 HDC hdc;
113 int i;
115 if( (fh = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR ) return 0;
116 _llseek(fh, 0, 0);
117 if (!_lread(fh, &APMHeader, sizeof(APMFILEHEADER))) return 0;
118 _llseek(fh, sizeof(APMFILEHEADER), 0);
119 checksum = 0;
120 p = (WORD *) &APMHeader;
122 for(i=0; i<10; i++)
123 checksum ^= *p++;
124 if (checksum != APMHeader.checksum) {
125 char msg[128];
126 sprintf(msg, "Computed checksum %04x != stored checksum %04x\n",
127 checksum, APMHeader.checksum);
128 MessageBox(hwnd, msg, "Checksum failed", MB_OK);
129 return 0;
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)))
139 GlobalFree(lpData);
140 _lclose(fh);
141 return 0;
143 _lclose(fh);
145 if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData)))
146 return 0;
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); */
153 hdc = GetDC(hwnd);
154 width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch;
155 height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch;
156 ReleaseDC(hwnd, hdc);
158 deltax = 0;
159 deltay = 0 ;
160 return hmf;
164 static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
166 switch (uMessage)
168 case WM_PAINT:
170 PAINTSTRUCT ps;
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)
179 RECT r;
180 GetClientRect(hwnd, &r);
181 PlayEnhMetaFile(ps.hdc, enhmf, &r);
183 else if (hmf)
184 PlayMetaFile(ps.hdc, hmf);
185 EndPaint(hwnd, &ps);
187 break;
189 case WM_COMMAND: /* message: command from application menu */
190 switch (GET_WM_COMMAND_ID(wparam,lparam))
192 case IDM_OPEN:
194 char filename[MAX_PATH];
195 if (FileOpen(hwnd, filename, sizeof(filename))) {
196 isAldus = FileIsPlaceable(filename);
197 if (isAldus) {
198 hmf = GetPlaceableMetaFile(hwnd, filename);
199 } else {
200 RECT r;
201 isEnhanced = FileIsEnhanced(filename);
202 if (isEnhanced)
203 enhmf = GetEnhMetaFile(filename);
204 else
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 );
213 break;
215 case IDM_SET_EXT_TO_WIN:
217 RECT r;
218 GetClientRect(hwnd, &r);
219 width = r.right - r.left;
220 height = r.bottom - r.top;
221 deltax = deltay = 0;
222 InvalidateRect( hwnd, NULL, TRUE );
224 break;
227 case IDM_LEFT:
228 deltax += 100;
229 InvalidateRect( hwnd, NULL, TRUE );
230 break;
231 case IDM_RIGHT:
232 deltax -= 100;
233 InvalidateRect( hwnd, NULL, TRUE );
234 break;
235 case IDM_UP:
236 deltay += 100;
237 InvalidateRect( hwnd, NULL, TRUE );
238 break;
239 case IDM_DOWN:
240 deltay -= 100;
241 InvalidateRect( hwnd, NULL, TRUE );
242 break;
244 case IDM_EXIT:
245 DestroyWindow(hwnd);
246 break;
248 default:
249 return DefWindowProc(hwnd, uMessage, wparam, lparam);
251 break;
253 case WM_DESTROY: /* message: window being destroyed */
254 PostQuitMessage(0);
255 break;
257 default: /* Passes it on if unprocessed */
258 return DefWindowProc(hwnd, uMessage, wparam, lparam);
260 return 0;
263 static BOOL InitApplication(HINSTANCE hInstance)
265 WNDCLASSEX wc;
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
271 main window */
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))
294 return FALSE;
297 /* Call module specific initialization functions here */
299 return TRUE;
302 static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
304 HWND hwnd;
306 /* Save the instance handle in a global variable for later use */
307 hInst = hInstance;
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 */
317 hInstance,
318 NULL);
320 if (!hwnd)
321 return FALSE;
323 /* Call module specific instance initialization functions here */
325 /* show the window, and paint it for the first time */
326 ShowWindow(hwnd, nCmdShow);
327 UpdateWindow(hwnd);
329 return TRUE;
332 int APIENTRY WinMain(HINSTANCE hInstance,
333 HINSTANCE hPrevInstance,
334 LPSTR lpCmdLine,
335 int nCmdShow)
337 MSG msg;
338 HANDLE hAccelTable;
340 /* Other instances of app running? */
341 if (!hPrevInstance)
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))
353 return FALSE;
356 hAccelTable = LoadAccelerators(hInstance, szAppName);
358 /* Main loop */
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 */
375 return msg.wParam;