wordpad: Remove unused parameter for preview_command().
[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 "globals.h"
24 #include <stdio.h>
25
26 BOOL FileIsPlaceable( LPCSTR szFileName );
27 HMETAFILE GetPlaceableMetaFile( HWND hwnd, LPCSTR szFileName );
28
29 HMETAFILE hmf;
30 int deltax = 0, deltay = 0;
31 int width = 0, height = 0;
32 BOOL isAldus;
33
34 static BOOL FileOpen(HWND hWnd, char *fn, int fnsz)
35 {
36   OPENFILENAME ofn = { sizeof(OPENFILENAME),
37                        0, 0, NULL, NULL, 0, 0, NULL,
38                        fnsz, NULL, 0, NULL, NULL, 
39                        OFN_SHOWHELP, 0, 0, NULL, 0, NULL };
40   ofn.lpstrFilter = "Metafiles\0*.wmf\0";
41   ofn.hwndOwner = hWnd;
42   ofn.lpstrFile = fn;
43   if( fnsz < 1 )
44     return FALSE;
45   *fn = 0;
46   return GetOpenFileName(&ofn);
47 }
48
49
50 LRESULT CALLBACK WndProc(HWND hwnd,
51                          UINT uMessage,
52                          WPARAM wparam,
53                          LPARAM lparam)
54 {
55   switch (uMessage)
56     {
57     case WM_PAINT:
58       {
59         PAINTSTRUCT ps;
60         BeginPaint(hwnd, &ps);
61         SetMapMode(ps.hdc, MM_ANISOTROPIC);
62         /* Set the window extent to a sane value in case the metafile doesn't */
63         SetWindowExtEx(ps.hdc, width, height, NULL);
64         SetViewportExtEx(ps.hdc, width, height, NULL);
65         SetViewportOrgEx(ps.hdc, deltax, deltay, NULL);
66         if(hmf) PlayMetaFile(ps.hdc, hmf);
67         EndPaint(hwnd, &ps);
68       }
69       break;
70
71     case WM_COMMAND: /* message: command from application menu */
72       switch (GET_WM_COMMAND_ID(wparam,lparam))
73         {
74         case IDM_OPEN:
75           {
76             char filename[MAX_PATH];
77             if (FileOpen(hwnd, filename, sizeof(filename))) {
78               isAldus = FileIsPlaceable(filename);
79               if (isAldus) {
80                 hmf = GetPlaceableMetaFile(hwnd, filename);
81               } else {
82                 RECT r;
83                 hmf = GetMetaFile(filename);
84                 GetClientRect(hwnd, &r);
85                 width = r.right - r.left;
86                 height = r.bottom - r.top;
87               }
88               InvalidateRect( hwnd, NULL, TRUE );
89             }
90           }
91           break;
92
93         case IDM_SET_EXT_TO_WIN:
94           {
95             RECT r;
96             GetClientRect(hwnd, &r);
97             width = r.right - r.left;
98             height = r.bottom - r.top;
99             deltax = deltay = 0;
100             InvalidateRect( hwnd, NULL, TRUE );
101           }
102           break;
103
104
105         case IDM_LEFT:
106           deltax += 100;
107           InvalidateRect( hwnd, NULL, TRUE );
108           break;
109         case IDM_RIGHT:
110           deltax -= 100;
111           InvalidateRect( hwnd, NULL, TRUE );
112           break;
113         case IDM_UP:
114           deltay += 100;
115           InvalidateRect( hwnd, NULL, TRUE );
116           break;
117         case IDM_DOWN:
118           deltay -= 100;
119           InvalidateRect( hwnd, NULL, TRUE );
120           break;
121
122         case IDM_EXIT:
123           DestroyWindow(hwnd);
124           break;
125
126         default:
127           return DefWindowProc(hwnd, uMessage, wparam, lparam);
128         }
129       break;
130
131     case WM_DESTROY:  /* message: window being destroyed */
132       PostQuitMessage(0);
133       break;
134
135     default:          /* Passes it on if unprocessed */
136       return DefWindowProc(hwnd, uMessage, wparam, lparam);
137     }
138     return 0;
139 }
140
141 BOOL FileIsPlaceable( LPCSTR szFileName )
142 {
143   HFILE         hInFile;
144   APMFILEHEADER apmh;
145
146   if( (hInFile = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR )
147     return FALSE;
148
149   if( _lread( hInFile, &apmh, sizeof(APMFILEHEADER) )
150       != sizeof(APMFILEHEADER) )
151     {
152       _lclose( hInFile );
153       return FALSE;
154     }
155   _lclose( hInFile );
156
157   /* Is it placeable? */
158   return (apmh.key == APMHEADER_KEY);
159 }
160
161 HMETAFILE GetPlaceableMetaFile( HWND hwnd, LPCSTR szFileName )
162 {
163   LPBYTE lpData;
164   METAHEADER mfHeader;
165   APMFILEHEADER APMHeader;
166   HFILE fh;
167   HMETAFILE hmf;
168   WORD checksum, *p;
169   HDC hdc;
170   int i;
171
172   if( (fh = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR ) return 0;
173   _llseek(fh, 0, 0);
174   if (!_lread(fh, (LPSTR)&APMHeader, sizeof(APMFILEHEADER))) return 0;
175   _llseek(fh, sizeof(APMFILEHEADER), 0);
176   checksum = 0;
177   p = (WORD *) &APMHeader;
178
179   for(i=0; i<10; i++)
180     checksum ^= *p++;
181   if (checksum != APMHeader.checksum) {
182     char msg[128];
183     sprintf(msg, "Computed checksum %04x != stored checksum %04x\n",
184            checksum, APMHeader.checksum);
185         MessageBox(hwnd, msg, "Checksum failed", MB_OK);
186     return 0;
187   }
188
189   if (!_lread(fh, (LPSTR)&mfHeader, sizeof(METAHEADER))) return 0;
190
191   if (!(lpData = GlobalAlloc(GPTR, (mfHeader.mtSize * 2L)))) return 0;
192
193   _llseek(fh, sizeof(APMFILEHEADER), 0);
194   if (!_lread(fh, lpData, (UINT)(mfHeader.mtSize * 2L)))
195   {
196     GlobalFree((HGLOBAL)lpData);
197     _lclose(fh);
198     return 0;
199   }
200   _lclose(fh);
201
202   if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData)))
203     return 0;
204
205
206   width = APMHeader.bbox.Right - APMHeader.bbox.Left;
207   height = APMHeader.bbox.Bottom - APMHeader.bbox.Top;
208
209   /*      printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch);  */
210   hdc = GetDC(hwnd);
211   width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch;
212   height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch;
213   ReleaseDC(hwnd, hdc);
214
215   deltax = 0;
216   deltay = 0 ;
217   return hmf;
218 }