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