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