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