wscript: Fix tests on wow64.
[wine] / programs / view / view.c
1 /*
2  * Copyright 1998 Douglas Ridgway
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <windows.h>
20 #include <windowsx.h>
21 #include "resource.h"
22
23 #include <stdio.h>
24
25 static HINSTANCE hInst;
26 static HWND hMainWnd;
27 static char szAppName[5] = "View";
28 static char szTitle[80];
29
30 static HMETAFILE hmf;
31 static HENHMETAFILE enhmf;
32 static int deltax = 0, deltay = 0;
33 static int width = 0, height = 0;
34 static BOOL isAldus, isEnhanced;
35
36 #include "pshpack1.h"
37 typedef struct
38 {
39         DWORD           key;
40         WORD            hmf;
41         SMALL_RECT      bbox;
42         WORD            inch;
43         DWORD           reserved;
44         WORD            checksum;
45 } APMFILEHEADER;
46 #include "poppack.h"
47
48 #define APMHEADER_KEY   0x9AC6CDD7l
49
50
51 static BOOL FileOpen(HWND hWnd, char *fn, int fnsz)
52 {
53   OPENFILENAME ofn = { sizeof(OPENFILENAME),
54                        0, 0, NULL, NULL, 0, 0, NULL,
55                        fnsz, NULL, 0, NULL, NULL, 
56                        OFN_SHOWHELP, 0, 0, NULL, 0, NULL };
57   ofn.lpstrFilter = "Metafiles\0*.wmf;*.emf\0";
58   ofn.hwndOwner = hWnd;
59   ofn.lpstrFile = fn;
60   if( fnsz < 1 )
61     return FALSE;
62   *fn = 0;
63   return GetOpenFileName(&ofn);
64 }
65
66 static BOOL FileIsEnhanced( LPCSTR szFileName )
67 {
68   HFILE hInFile;
69   ENHMETAHEADER enh;
70
71   if( (hInFile = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR )
72     return FALSE;
73
74   if( _lread( hInFile, &enh, sizeof(ENHMETAHEADER) ) != sizeof(ENHMETAHEADER) )
75     {
76       _lclose( hInFile );
77       return FALSE;
78     }
79   _lclose( hInFile );
80
81   /* Is it enhanced? */
82   return (enh.dSignature == ENHMETA_SIGNATURE);
83 }
84
85 static BOOL FileIsPlaceable( LPCSTR szFileName )
86 {
87   HFILE         hInFile;
88   APMFILEHEADER apmh;
89
90   if( (hInFile = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR )
91     return FALSE;
92
93   if( _lread( hInFile, &apmh, sizeof(APMFILEHEADER) )
94       != sizeof(APMFILEHEADER) )
95     {
96       _lclose( hInFile );
97       return FALSE;
98     }
99   _lclose( hInFile );
100
101   /* Is it placeable? */
102   return (apmh.key == APMHEADER_KEY);
103 }
104
105 static HMETAFILE GetPlaceableMetaFile( LPCSTR szFileName )
106 {
107   LPBYTE lpData;
108   METAHEADER mfHeader;
109   APMFILEHEADER APMHeader;
110   HFILE fh;
111   HMETAFILE hmf;
112   WORD checksum, *p;
113   HDC hdc;
114   int i;
115
116   if( (fh = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR ) return 0;
117   _llseek(fh, 0, 0);
118   if (!_lread(fh, &APMHeader, sizeof(APMFILEHEADER))) return 0;
119   _llseek(fh, sizeof(APMFILEHEADER), 0);
120   checksum = 0;
121   p = (WORD *) &APMHeader;
122
123   for(i=0; i<10; i++)
124     checksum ^= *p++;
125   if (checksum != APMHeader.checksum) {
126     char msg[128];
127     sprintf(msg, "Computed checksum %04x != stored checksum %04x\n",
128            checksum, APMHeader.checksum);
129         MessageBox(hMainWnd, msg, "Checksum failed", MB_OK);
130     return 0;
131   }
132
133   if (!_lread(fh, &mfHeader, sizeof(METAHEADER))) return 0;
134
135   if (!(lpData = GlobalAlloc(GPTR, (mfHeader.mtSize * 2L)))) return 0;
136
137   _llseek(fh, sizeof(APMFILEHEADER), 0);
138   if (!_lread(fh, lpData, (UINT)(mfHeader.mtSize * 2L)))
139   {
140     GlobalFree(lpData);
141     _lclose(fh);
142     return 0;
143   }
144   _lclose(fh);
145
146   if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData)))
147     return 0;
148
149
150   width = APMHeader.bbox.Right - APMHeader.bbox.Left;
151   height = APMHeader.bbox.Bottom - APMHeader.bbox.Top;
152
153   /*      printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch);  */
154   hdc = GetDC(hMainWnd);
155   width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch;
156   height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch;
157   ReleaseDC(hMainWnd, hdc);
158
159   deltax = 0;
160   deltay = 0 ;
161   return hmf;
162 }
163
164 static void DoOpenFile(LPCSTR filename)
165 {
166   if (!filename) return;
167
168   isAldus = FileIsPlaceable(filename);
169   if (isAldus) {
170     hmf = GetPlaceableMetaFile(filename);
171   } else {
172     RECT r;
173     isEnhanced = FileIsEnhanced(filename);
174     if (isEnhanced)
175        enhmf = GetEnhMetaFile(filename);
176     else
177        hmf = GetMetaFile(filename);
178     GetClientRect(hMainWnd, &r);
179     width = r.right - r.left;
180     height = r.bottom - r.top;
181   }
182   InvalidateRect( hMainWnd, NULL, TRUE );
183 }
184
185 static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
186 {
187   switch (uMessage)
188     {
189     case WM_PAINT:
190       {
191         PAINTSTRUCT ps;
192         BeginPaint(hwnd, &ps);
193         SetMapMode(ps.hdc, MM_ANISOTROPIC);
194         /* Set the window extent to a sane value in case the metafile doesn't */
195         SetWindowExtEx(ps.hdc, width, height, NULL);
196         SetViewportExtEx(ps.hdc, width, height, NULL);
197         SetViewportOrgEx(ps.hdc, deltax, deltay, NULL);
198        if (isEnhanced && enhmf)
199        {
200            RECT r;
201            GetClientRect(hwnd, &r);
202            PlayEnhMetaFile(ps.hdc, enhmf, &r);
203        }
204        else if (hmf)
205            PlayMetaFile(ps.hdc, hmf);
206         EndPaint(hwnd, &ps);
207       }
208       break;
209
210     case WM_COMMAND: /* message: command from application menu */
211       switch (GET_WM_COMMAND_ID(wparam,lparam))
212         {
213         case IDM_OPEN:
214           {
215             char filename[MAX_PATH];
216            if (FileOpen(hwnd, filename, sizeof(filename)))
217              DoOpenFile(filename);
218           }
219           break;
220
221         case IDM_SET_EXT_TO_WIN:
222           {
223             RECT r;
224             GetClientRect(hwnd, &r);
225             width = r.right - r.left;
226             height = r.bottom - r.top;
227             deltax = deltay = 0;
228             InvalidateRect( hwnd, NULL, TRUE );
229           }
230           break;
231
232
233         case IDM_LEFT:
234           deltax += 100;
235           InvalidateRect( hwnd, NULL, TRUE );
236           break;
237         case IDM_RIGHT:
238           deltax -= 100;
239           InvalidateRect( hwnd, NULL, TRUE );
240           break;
241         case IDM_UP:
242           deltay += 100;
243           InvalidateRect( hwnd, NULL, TRUE );
244           break;
245         case IDM_DOWN:
246           deltay -= 100;
247           InvalidateRect( hwnd, NULL, TRUE );
248           break;
249
250         case IDM_EXIT:
251           DestroyWindow(hwnd);
252           break;
253
254         default:
255           return DefWindowProc(hwnd, uMessage, wparam, lparam);
256         }
257       break;
258
259     case WM_DESTROY:  /* message: window being destroyed */
260       PostQuitMessage(0);
261       break;
262
263     default:          /* Passes it on if unprocessed */
264       return DefWindowProc(hwnd, uMessage, wparam, lparam);
265     }
266     return 0;
267 }
268
269 static BOOL InitApplication(HINSTANCE hInstance)
270 {
271   WNDCLASSEX wc;
272
273   /* Load the application description strings */
274   LoadString(hInstance, IDS_DESCRIPTION, szTitle, sizeof(szTitle));
275
276   /* Fill in window class structure with parameters that describe the
277      main window */
278
279   wc.cbSize = sizeof(WNDCLASSEX);
280   wc.style         = CS_HREDRAW | CS_VREDRAW;             /* Class style(s) */
281   wc.lpfnWndProc   = WndProc;                             /* Window Procedure */
282   wc.cbClsExtra    = 0;                          /* No per-class extra data */
283   wc.cbWndExtra    = 0;                         /* No per-window extra data */
284   wc.hInstance     = hInstance;                      /* Owner of this class */
285   wc.hIcon         = NULL;
286   wc.hIconSm       = NULL;
287   wc.hCursor       = LoadCursor(NULL, IDC_ARROW);                 /* Cursor */
288   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);           /* Default color */
289   wc.lpszMenuName  = szAppName;                       /* Menu name from .rc */
290   wc.lpszClassName = szAppName;                      /* Name to register as */
291
292   /* Register the window class and return FALSE if unsuccessful */
293
294   if (!RegisterClassEx(&wc))
295     {
296       if (!RegisterClass((LPWNDCLASS)&wc.style))
297       return FALSE;
298     }
299
300   /* Call module specific initialization functions here */
301
302   return TRUE;
303 }
304
305 static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
306 {
307     /* Save the instance handle in a global variable for later use */
308     hInst = hInstance;
309
310     /* Create main window */
311     hMainWnd = CreateWindow(szAppName,           /* See RegisterClass() call */
312                             szTitle,             /* window title */
313                             WS_OVERLAPPEDWINDOW, /* Window style */
314                             CW_USEDEFAULT, 0,    /* positioning */
315                             CW_USEDEFAULT, 0,    /* size */
316                             NULL,                /* Overlapped has no parent */
317                             NULL,                /* Use the window class menu */
318                             hInstance,
319                             NULL);
320
321     if (!hMainWnd)
322         return FALSE;
323
324     /* Call module specific instance initialization functions here */
325
326     /* show the window, and paint it for the first time */
327     ShowWindow(hMainWnd, nCmdShow);
328     UpdateWindow(hMainWnd);
329
330     return TRUE;
331 }
332
333 static void HandleCommandLine(LPSTR cmdline)
334 {
335     CHAR delimiter;
336
337     /* skip white space */
338     while (*cmdline == ' ') cmdline++;
339
340     /* skip executable name */
341     delimiter = (*cmdline == '"' ? '"' : ' ');
342
343     if (*cmdline == delimiter) cmdline++;
344     while (*cmdline && *cmdline != delimiter) cmdline++;
345     if (*cmdline == delimiter) cmdline++;
346
347     if (*cmdline)
348     {
349         /* file name is passed on the command line */
350         if (cmdline[0] == '"')
351         {
352             cmdline++;
353             cmdline[lstrlen(cmdline) - 1] = 0;
354         }
355         DoOpenFile(cmdline);
356     }
357 }
358
359 int APIENTRY WinMain(HINSTANCE hInstance,
360                      HINSTANCE hPrevInstance,
361                      LPSTR     lpCmdLine,
362                      int       nCmdShow)
363 {
364     MSG msg;
365     HANDLE hAccelTable;
366
367     /* Other instances of app running? */
368     if (!hPrevInstance)
369     {
370       /* stuff to be done once */
371       if (!InitApplication(hInstance))
372       {
373         return FALSE;      /* exit */
374       }
375     }
376
377     /* stuff to be done every time */
378     if (!InitInstance(hInstance, nCmdShow))
379     {
380       return FALSE;
381     }
382
383     HandleCommandLine(GetCommandLine());
384
385     hAccelTable = LoadAccelerators(hInstance, szAppName);
386
387     /* Main loop */
388     /* Acquire and dispatch messages until a WM_QUIT message is received */
389     while (GetMessage(&msg, NULL, 0, 0))
390     {
391       /* Add other Translation functions (for modeless dialogs
392       and/or MDI windows) here. */
393
394       if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
395         {
396             TranslateMessage(&msg);
397             DispatchMessage(&msg);
398         }
399     }
400
401     /* Add module specific instance free/delete functions here */
402
403     /* Returns the value from PostQuitMessage */
404     return msg.wParam;
405 }