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