Spelling and case fixes.
[wine.git] / programs / view / view.c
blob0fa6e12d320446304108cb39622cfb4fe2badb86
1 /*
2 * Copyright 1998 Douglas Ridgway
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.
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.
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
19 #include <windows.h>
20 #include "resource.h"
23 #include <windowsx.h>
24 Wine doesn't have windowsx.h, so we use this
26 #define GET_WM_COMMAND_ID(wp,lp) LOWORD(wp)
28 /* Wine seems to need this */
29 #include <commdlg.h>
31 #include "globals.h"
32 #include <stdio.h>
34 BOOL FileIsPlaceable( LPCSTR szFileName );
35 HMETAFILE GetPlaceableMetaFile( HWND hwnd, LPCSTR szFileName );
37 #define FN_LENGTH 80
39 HMETAFILE hmf;
40 int deltax = 0, deltay = 0;
41 int width = 0, height = 0;
42 BOOL isAldus;
44 BOOL FileOpen(HWND hWnd, char *fn, int fnsz)
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);
60 LRESULT CALLBACK WndProc(HWND hwnd,
61 UINT uMessage,
62 WPARAM wparam,
63 LPARAM lparam)
65 switch (uMessage)
67 case WM_PAINT:
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);
78 break;
80 case WM_COMMAND: /* message: command from application menu */
81 switch (GET_WM_COMMAND_ID(wparam,lparam))
83 case IDM_HELLO:
84 MessageBox( hwnd , "Hello there world!", "Hello", MB_OK);
85 break;
87 case IDM_OPEN:
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;
101 InvalidateRect( hwnd, NULL, TRUE );
104 break;
106 case IDM_SET_EXT_TO_WIN:
108 RECT r;
109 GetClientRect(hwnd, &r);
110 width = r.right - r.left;
111 height = r.bottom - r.top;
112 InvalidateRect( hwnd, NULL, TRUE );
114 break;
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;
134 case IDM_EXIT:
135 DestroyWindow(hwnd);
136 break;
138 default:
139 return DefWindowProc(hwnd, uMessage, wparam, lparam);
141 break;
143 case WM_DESTROY: /* message: window being destroyed */
144 PostQuitMessage(0);
145 break;
147 default: /* Passes it on if unprocessed */
148 return DefWindowProc(hwnd, uMessage, wparam, lparam);
150 return 0;
153 BOOL FileIsPlaceable( LPCSTR szFileName )
155 HFILE hInFile;
156 APMFILEHEADER apmh;
158 if( (hInFile = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR )
159 return FALSE;
161 if( _lread( hInFile, &apmh, sizeof(APMFILEHEADER) )
162 != sizeof(APMFILEHEADER) )
164 _lclose( hInFile );
165 return FALSE;
167 _lclose( hInFile );
169 /* Is it placeable? */
170 return (apmh.key == APMHEADER_KEY);
173 HMETAFILE GetPlaceableMetaFile( HWND hwnd, LPCSTR szFileName )
175 LPSTR lpData;
176 METAHEADER mfHeader;
177 APMFILEHEADER APMHeader;
178 HFILE fh;
179 HMETAFILE hmf;
180 WORD checksum, *p;
181 int i;
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;
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;
200 if (!_lread(fh, (LPSTR)&mfHeader, sizeof(METAHEADER))) return 0;
202 if (!(lpData = (LPSTR) GlobalAlloc(GPTR, (mfHeader.mtSize * 2L)))) return 0;
204 _llseek(fh, sizeof(APMFILEHEADER), 0);
205 if (!_lread(fh, lpData, (UINT)(mfHeader.mtSize * 2L)))
207 GlobalFree((HGLOBAL)lpData);
208 _lclose(fh);
209 return 0;
211 _lclose(fh);
213 if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData)))
214 return 0;
217 width = APMHeader.bbox.Right - APMHeader.bbox.Left;
218 height = APMHeader.bbox.Bottom - APMHeader.bbox.Top;
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;
224 deltax = 0;
225 deltay = 0 ;
226 return hmf;