server: Report a default DACL for registry keys.
[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 <commdlg.h>
21 #include "resource.h"
22
23 #include <stdio.h>
24
25 static HINSTANCE hInst;
26 static HWND hMainWnd;
27 static WCHAR szAppName[5] = {'V','i','e','w',0};
28 static WCHAR szTitle[MAX_PATH];
29 static WCHAR szFileTitle[MAX_PATH];
30
31 static HMETAFILE hmf;
32 static HENHMETAFILE enhmf;
33 static int deltax = 0, deltay = 0;
34 static int width = 0, height = 0;
35 static BOOL isAldus, isEnhanced;
36
37 #include "pshpack1.h"
38 typedef struct
39 {
40         DWORD           key;
41         WORD            hmf;
42         SMALL_RECT      bbox;
43         WORD            inch;
44         DWORD           reserved;
45         WORD            checksum;
46 } APMFILEHEADER;
47 #include "poppack.h"
48
49 #define APMHEADER_KEY   0x9AC6CDD7l
50
51
52 static BOOL FileOpen(HWND hWnd, WCHAR *fn, int fnsz)
53 {
54   static const WCHAR filter[] = {'M','e','t','a','f','i','l','e','s','\0','*','.','w','m','f',';','*','.','e','m','f','\0',0};
55   OPENFILENAMEW ofn = { sizeof(OPENFILENAMEW),
56                         0, 0, NULL, NULL, 0, 0, NULL,
57                         fnsz, NULL, 0, NULL, NULL,
58                         OFN_SHOWHELP, 0, 0, NULL, 0, NULL };
59   ofn.lpstrFilter = filter;
60   ofn.hwndOwner = hWnd;
61   ofn.lpstrFile = fn;
62   if( fnsz < 1 )
63     return FALSE;
64   *fn = 0;
65   return GetOpenFileNameW(&ofn);
66 }
67
68 static BOOL FileIsEnhanced( LPCWSTR szFileName )
69 {
70   ENHMETAHEADER enh;
71   HANDLE handle;
72   DWORD size;
73
74   handle = CreateFileW( szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
75                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
76   if (handle == INVALID_HANDLE_VALUE)
77     return FALSE;
78
79   if (!ReadFile( handle, &enh, sizeof(ENHMETAHEADER), &size, NULL ) || size != sizeof(ENHMETAHEADER) )
80   {
81       CloseHandle( handle );
82       return FALSE;
83   }
84   CloseHandle( handle );
85
86   /* Is it enhanced? */
87   return (enh.dSignature == ENHMETA_SIGNATURE);
88 }
89
90 static BOOL FileIsPlaceable( LPCWSTR szFileName )
91 {
92   APMFILEHEADER apmh;
93   HANDLE handle;
94   DWORD size;
95
96   handle = CreateFileW( szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
97                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
98   if (handle == INVALID_HANDLE_VALUE)
99     return FALSE;
100
101   if (!ReadFile( handle, &apmh, sizeof(APMFILEHEADER), &size, NULL ) || size != sizeof(APMFILEHEADER))
102   {
103       CloseHandle( handle );
104       return FALSE;
105   }
106   CloseHandle( handle );
107
108   /* Is it placeable? */
109   return (apmh.key == APMHEADER_KEY);
110 }
111
112 static HMETAFILE GetPlaceableMetaFile( LPCWSTR szFileName )
113 {
114   LPBYTE lpData;
115   METAHEADER mfHeader;
116   APMFILEHEADER APMHeader;
117   HANDLE handle;
118   DWORD size;
119   HMETAFILE hmf;
120   WORD checksum, *p;
121   HDC hdc;
122   int i;
123
124   handle = CreateFileW( szFileName, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
125                         NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
126   if (handle == INVALID_HANDLE_VALUE)
127     return 0;
128
129   if (!ReadFile( handle, &APMHeader, sizeof(APMFILEHEADER), &size, NULL ) || size != sizeof(APMFILEHEADER))
130   {
131       CloseHandle( handle );
132       return 0;
133   }
134   checksum = 0;
135   p = (WORD *) &APMHeader;
136
137   for(i=0; i<10; i++)
138     checksum ^= *p++;
139   if (checksum != APMHeader.checksum) {
140     char msg[128];
141     sprintf(msg, "Computed checksum %04x != stored checksum %04x\n",
142            checksum, APMHeader.checksum);
143     MessageBoxA(hMainWnd, msg, "Checksum failed", MB_OK);
144     CloseHandle( handle );
145     return 0;
146   }
147
148   if (!ReadFile( handle, &mfHeader, sizeof(METAHEADER), &size, NULL) || size != sizeof(METAHEADER))
149   {
150       CloseHandle( handle );
151       return 0;
152   }
153
154   if (!(lpData = GlobalAlloc(GPTR, (mfHeader.mtSize * 2L))))
155   {
156       CloseHandle( handle );
157       return 0;
158   }
159
160   SetFilePointer( handle, sizeof(APMFILEHEADER), NULL, FILE_BEGIN );
161   if (!ReadFile(handle, lpData, mfHeader.mtSize * 2, &size, NULL ) || size != mfHeader.mtSize * 2)
162   {
163     GlobalFree(lpData);
164     CloseHandle( handle );
165     return 0;
166   }
167   CloseHandle( handle );
168
169   if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData)))
170     return 0;
171
172
173   width = APMHeader.bbox.Right - APMHeader.bbox.Left;
174   height = APMHeader.bbox.Bottom - APMHeader.bbox.Top;
175
176   /*      printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch);  */
177   hdc = GetDC(hMainWnd);
178   width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch;
179   height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch;
180   ReleaseDC(hMainWnd, hdc);
181
182   deltax = 0;
183   deltay = 0 ;
184   return hmf;
185 }
186
187 static void DoOpenFile(LPCWSTR filename)
188 {
189   if (!filename) return;
190
191   isAldus = FileIsPlaceable(filename);
192   if (isAldus) {
193     hmf = GetPlaceableMetaFile(filename);
194   } else {
195     RECT r;
196     isEnhanced = FileIsEnhanced(filename);
197     if (isEnhanced)
198        enhmf = GetEnhMetaFileW(filename);
199     else
200        hmf = GetMetaFileW(filename);
201     GetClientRect(hMainWnd, &r);
202     width = r.right - r.left;
203     height = r.bottom - r.top;
204   }
205   InvalidateRect( hMainWnd, NULL, TRUE );
206 }
207
208 static void UpdateWindowCaption(void)
209 {
210   WCHAR szCaption[MAX_PATH];
211   WCHAR szView[MAX_PATH];
212   static const WCHAR hyphenW[] = { ' ','-',' ',0 };
213
214   LoadStringW(hInst, IDS_DESCRIPTION, szView, sizeof(szView)/sizeof(WCHAR));
215
216   if (szFileTitle[0] != '\0')
217   {
218     lstrcpyW(szCaption, szFileTitle);
219     LoadStringW(hInst, IDS_DESCRIPTION, szView, sizeof(szView)/sizeof(WCHAR));
220     lstrcatW(szCaption, hyphenW);
221     lstrcatW(szCaption, szView);
222   }
223   else
224     lstrcpyW(szCaption, szView);
225
226   SetWindowTextW(hMainWnd, szCaption);
227 }
228
229 static LRESULT CALLBACK WndProc(HWND hwnd, UINT uMessage, WPARAM wparam, LPARAM lparam)
230 {
231   switch (uMessage)
232     {
233     case WM_PAINT:
234       {
235         PAINTSTRUCT ps;
236         BeginPaint(hwnd, &ps);
237         SetMapMode(ps.hdc, MM_ANISOTROPIC);
238         /* Set the window extent to a sane value in case the metafile doesn't */
239         SetWindowExtEx(ps.hdc, width, height, NULL);
240         SetViewportExtEx(ps.hdc, width, height, NULL);
241         SetViewportOrgEx(ps.hdc, deltax, deltay, NULL);
242        if (isEnhanced && enhmf)
243        {
244            RECT r;
245            GetClientRect(hwnd, &r);
246            PlayEnhMetaFile(ps.hdc, enhmf, &r);
247        }
248        else if (hmf)
249            PlayMetaFile(ps.hdc, hmf);
250         EndPaint(hwnd, &ps);
251       }
252       break;
253
254     case WM_COMMAND: /* message: command from application menu */
255         switch (LOWORD(wparam))
256         {
257         case IDM_OPEN:
258           {
259               WCHAR filename[MAX_PATH];
260               if (FileOpen(hwnd, filename, sizeof(filename)/sizeof(WCHAR)))
261               {
262                   szFileTitle[0] = 0;
263                   GetFileTitleW(filename, szFileTitle, sizeof(szFileTitle)/sizeof(WCHAR));
264                   DoOpenFile(filename);
265                   UpdateWindowCaption();
266               }
267           }
268           break;
269
270         case IDM_SET_EXT_TO_WIN:
271           {
272             RECT r;
273             GetClientRect(hwnd, &r);
274             width = r.right - r.left;
275             height = r.bottom - r.top;
276             deltax = deltay = 0;
277             InvalidateRect( hwnd, NULL, TRUE );
278           }
279           break;
280
281
282         case IDM_LEFT:
283           deltax += 100;
284           InvalidateRect( hwnd, NULL, TRUE );
285           break;
286         case IDM_RIGHT:
287           deltax -= 100;
288           InvalidateRect( hwnd, NULL, TRUE );
289           break;
290         case IDM_UP:
291           deltay += 100;
292           InvalidateRect( hwnd, NULL, TRUE );
293           break;
294         case IDM_DOWN:
295           deltay -= 100;
296           InvalidateRect( hwnd, NULL, TRUE );
297           break;
298
299         case IDM_EXIT:
300           DestroyWindow(hwnd);
301           break;
302
303         default:
304           return DefWindowProcW(hwnd, uMessage, wparam, lparam);
305         }
306       break;
307
308     case WM_DESTROY:  /* message: window being destroyed */
309       PostQuitMessage(0);
310       break;
311
312     default:          /* Passes it on if unprocessed */
313       return DefWindowProcW(hwnd, uMessage, wparam, lparam);
314     }
315     return 0;
316 }
317
318 static BOOL InitApplication(HINSTANCE hInstance)
319 {
320   WNDCLASSEXW wc;
321
322   /* Load the application description strings */
323   LoadStringW(hInstance, IDS_DESCRIPTION, szTitle, sizeof(szTitle)/sizeof(WCHAR));
324
325   /* Fill in window class structure with parameters that describe the
326      main window */
327
328   wc.cbSize        = sizeof(WNDCLASSEXW);
329   wc.style         = CS_HREDRAW | CS_VREDRAW;             /* Class style(s) */
330   wc.lpfnWndProc   = WndProc;                             /* Window Procedure */
331   wc.cbClsExtra    = 0;                          /* No per-class extra data */
332   wc.cbWndExtra    = 0;                         /* No per-window extra data */
333   wc.hInstance     = hInstance;                      /* Owner of this class */
334   wc.hIcon         = NULL;
335   wc.hIconSm       = NULL;
336   wc.hCursor       = LoadCursorW(NULL, (LPWSTR)IDC_ARROW);
337   wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);           /* Default color */
338   wc.lpszMenuName  = szAppName;                       /* Menu name from .rc */
339   wc.lpszClassName = szAppName;                      /* Name to register as */
340
341   if (!RegisterClassExW(&wc)) return FALSE;
342
343   /* Call module specific initialization functions here */
344
345   return TRUE;
346 }
347
348 static BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
349 {
350     /* Save the instance handle in a global variable for later use */
351     hInst = hInstance;
352
353     /* Create main window */
354     hMainWnd = CreateWindowW(szAppName,          /* See RegisterClass() call */
355                             szTitle,             /* window title */
356                             WS_OVERLAPPEDWINDOW, /* Window style */
357                             CW_USEDEFAULT, 0,    /* positioning */
358                             CW_USEDEFAULT, 0,    /* size */
359                             NULL,                /* Overlapped has no parent */
360                             NULL,                /* Use the window class menu */
361                             hInstance,
362                             NULL);
363
364     if (!hMainWnd)
365         return FALSE;
366
367     /* Call module specific instance initialization functions here */
368
369     /* show the window, and paint it for the first time */
370     ShowWindow(hMainWnd, nCmdShow);
371     UpdateWindow(hMainWnd);
372
373     return TRUE;
374 }
375
376 static void HandleCommandLine(LPWSTR cmdline)
377 {
378     /* skip white space */
379     while (*cmdline == ' ') cmdline++;
380
381     if (*cmdline)
382     {
383         /* file name is passed on the command line */
384         if (cmdline[0] == '"')
385         {
386             cmdline++;
387             cmdline[lstrlenW(cmdline) - 1] = 0;
388         }
389         szFileTitle[0] = 0;
390         GetFileTitleW(cmdline, szFileTitle, sizeof(szFileTitle)/sizeof(WCHAR));
391         DoOpenFile(cmdline);
392         UpdateWindowCaption();
393     }
394 }
395
396 int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
397 {
398     MSG msg;
399
400     /* Other instances of app running? */
401     if (!hPrevInstance)
402     {
403       /* stuff to be done once */
404       if (!InitApplication(hInstance))
405       {
406         return FALSE;      /* exit */
407       }
408     }
409
410     /* stuff to be done every time */
411     if (!InitInstance(hInstance, nCmdShow))
412     {
413       return FALSE;
414     }
415
416     HandleCommandLine(lpCmdLine);
417
418     /* Main loop */
419     /* Acquire and dispatch messages until a WM_QUIT message is received */
420     while (GetMessageW(&msg, NULL, 0, 0))
421     {
422         TranslateMessage(&msg);
423         DispatchMessageW(&msg);
424     }
425
426     return msg.wParam;
427 }