display required/available space
[swftools.git] / installer / installer.c
1 /* installer.c
2
3    Part of the swftools installer (Main program).
4    
5    Copyright (c) 2004 Matthias Kramm <kramm@quiss.org> 
6  
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
20
21 #include <windows.h>
22 #include <commctrl.h>
23 #include <commdlg.h>
24 #include <shlobj.h>
25 #include <prsht.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "installer.h"
29
30 #include "../config.h" //for swftools version
31
32 static int config_forAllUsers = 0;
33 static int config_createLinks = 0;
34 static int config_createStartmenu = 1;
35 static int config_createDesktop = 1;
36
37 static char path_startmenu[MAX_PATH] = "\0";
38 static char path_desktop[MAX_PATH] = "\0";
39 static char path_programfiles[MAX_PATH] = "\0";
40
41 extern char*crndata;
42 extern int crndata_len;
43 extern int crn_decompressed_size;
44
45 extern char*license_text;
46
47 static char*install_path = "c:\\swftools\\";
48 static char pathBuf[MAX_PATH];
49 static int do_abort = 0;
50
51 static char* pdf2swf_path;
52
53 static HBITMAP logo = 0;
54
55 static HINSTANCE me;
56
57 #define USER_SETMESSAGE 0x7f01
58
59 static void tball(HDC hdc, double px, double py, double sx, double sy)
60 {
61     MoveToEx(hdc, 75.25*sx+px, -32.50*sy+py, 0);
62     LineTo(hdc, 74.75*sx+px, -24.80*sy+py); LineTo(hdc, 74.95*sx+px, -3.45*sy+py); LineTo(hdc, 75.25*sx+px, -32.50*sy+py);
63     LineTo(hdc, 44.85*sx+px, -104.30*sy+py); LineTo(hdc, 74.75*sx+px, -24.80*sy+py); LineTo(hdc, 17.30*sx+px, -91.80*sy+py);
64     LineTo(hdc, 29.35*sx+px, -10.10*sy+py); LineTo(hdc, 74.75*sx+px, -24.80*sy+py); LineTo(hdc, 47.60*sx+px, 63.80*sy+py);
65     LineTo(hdc, 74.75*sx+px, 51.10*sy+py); LineTo(hdc, 74.95*sx+px, -3.45*sy+py);
66     MoveToEx(hdc, 74.75*sx+px, 51.10*sy+py, 0);
67     LineTo(hdc, 29.35*sx+px, 118.75*sy+py); LineTo(hdc, -45.20*sx+px, 155.35*sy+py);
68     LineTo(hdc, -127.95*sx+px, 145.10*sy+py); LineTo(hdc, -194.85*sx+px, 104.30*sy+py);
69     LineTo(hdc, -225.25*sx+px, 32.50*sy+py); LineTo(hdc, -224.75*sx+px, -51.10*sy+py);
70     LineTo(hdc, -179.35*sx+px, -118.75*sy+py); LineTo(hdc, -104.80*sx+px, -155.35*sy+py);
71     LineTo(hdc, -22.05*sx+px, -145.10*sy+py); LineTo(hdc, 44.85*sx+px, -104.30*sy+py);
72     LineTo(hdc, 17.30*sx+px, -91.80*sy+py); LineTo(hdc, -75.00*sx+px, -122.60*sy+py);
73     LineTo(hdc, -45.20*sx+px, -43.70*sy+py); LineTo(hdc, 17.30*sx+px, -91.80*sy+py);
74     LineTo(hdc, -22.05*sx+px, -145.10*sy+py); LineTo(hdc, -75.00*sx+px, -122.60*sy+py);
75     LineTo(hdc, -104.80*sx+px, -155.35*sy+py); LineTo(hdc, -167.30*sx+px, -107.25*sy+py);
76     LineTo(hdc, -75.00*sx+px, -122.60*sy+py); LineTo(hdc, -127.95*sx+px, -63.55*sy+py);
77     LineTo(hdc, -45.20*sx+px, -43.70*sy+py); LineTo(hdc, 29.35*sx+px, -10.10*sy+py);
78     LineTo(hdc, 47.60*sx+px, 63.80*sy+py); LineTo(hdc, -25.70*sx+px, 52.45*sy+py);
79     LineTo(hdc, -25.70*sx+px, 128.45*sy+py); LineTo(hdc, 47.60*sx+px, 63.80*sy+py);
80     LineTo(hdc, 29.35*sx+px, 118.75*sy+py); LineTo(hdc, -25.70*sx+px, 128.45*sy+py);
81     LineTo(hdc, -45.20*sx+px, 155.35*sy+py); LineTo(hdc, -118.85*sx+px, 143.85*sy+py);
82     LineTo(hdc, -127.95*sx+px, 145.10*sy+py);
83     MoveToEx(hdc, -179.35*sx+px, -118.75*sy+py, 0);
84     LineTo(hdc, -167.30*sx+px, -107.25*sy+py); LineTo(hdc, -127.95*sx+px, -63.55*sy+py);
85     LineTo(hdc, -118.85*sx+px, 21.65*sy+py); LineTo(hdc, -45.20*sx+px, -43.70*sy+py);
86     LineTo(hdc, -25.70*sx+px, 52.45*sy+py); LineTo(hdc, 29.35*sx+px, -10.10*sy+py);
87     MoveToEx(hdc, -225.25*sx+px, 32.50*sy+py, 0);
88     LineTo(hdc, -194.85*sx+px, -17.85*sy+py); LineTo(hdc, -224.75*sx+px, -51.10*sy+py);
89     LineTo(hdc, -167.30*sx+px, -107.25*sy+py); LineTo(hdc, -194.85*sx+px, -17.85*sy+py);
90     LineTo(hdc, -127.95*sx+px, -63.55*sy+py);
91     MoveToEx(hdc, -175.85*sx+px, 77.60*sy+py, 0);
92     LineTo(hdc, -96.35*sx+px, 99.55*sy+py); LineTo(hdc, -118.85*sx+px, 21.65*sy+py);
93     LineTo(hdc, -175.85*sx+px, 77.60*sy+py); LineTo(hdc, -225.25*sx+px, 32.50*sy+py);
94     MoveToEx(hdc, -118.85*sx+px, 143.85*sy+py, 0);
95     LineTo(hdc, -175.85*sx+px, 77.60*sy+py); LineTo(hdc, -194.85*sx+px, 104.30*sy+py);
96     LineTo(hdc, -118.85*sx+px, 143.85*sy+py); LineTo(hdc, -96.35*sx+px, 99.55*sy+py);
97     LineTo(hdc, -25.70*sx+px, 52.45*sy+py); LineTo(hdc, -118.85*sx+px, 21.65*sy+py);
98     LineTo(hdc, -194.85*sx+px, -17.85*sy+py); LineTo(hdc, -175.85*sx+px, 77.60*sy+py);
99     MoveToEx(hdc, -96.35*sx+px, 99.55*sy+py, 0);
100     LineTo(hdc, -25.70*sx+px, 128.45*sy+py); LineTo(hdc, -118.85*sx+px, 143.85*sy+py);
101 }
102
103 static char* concatPaths(const char*base, const char*add)
104 {
105     int l1 = strlen(base);
106     int l2 = strlen(add);
107     int pos = 0;
108     char*n = 0;
109     while(l1 && base[l1-1] == '\\')
110         l1--;
111     while(pos < l2 && add[pos] == '\\')
112         pos++;
113
114     n = (char*)malloc(l1 + (l2-pos) + 2);
115     memcpy(n,base,l1);
116     n[l1]='\\';
117     strcpy(&n[l1+1],&add[pos]);
118     return n;
119 }
120
121 static void handleTemplateFile(const char*filename)
122 {
123     FILE*fi = fopen(filename, "rb");
124     fseek(fi, SEEK_END, 0);
125     int len = ftell(fi);
126     fseek(fi, SEEK_SET, 0);
127     char*file = malloc(len);
128     fread(file, len, 1, fi);
129     fclose(fi);
130     int l = strlen(install_path);
131     fi = fopen(filename, "wb");
132     char*pos = file;
133     char*lastpos = file;
134     while(1) {
135         pos = strstr(pos, "%%PATH%%");
136         if(!pos) {
137             pos = &file[len];
138             break;
139         }
140         if(pos!=lastpos)
141             fwrite(lastpos, pos-lastpos, 1, fi);
142         fwrite(install_path, l, 1, fi);
143         pos+=8; // length of "%%PATH%%"
144         lastpos = pos;
145     }
146     fwrite(lastpos, pos-lastpos, 1, fi);
147     fclose(fi);
148     free(file);
149 }
150
151 static char* getRegistryEntry(char*path)
152 {
153     int res = 0;
154     HKEY key;
155     long rc;
156     long size = 0;
157     DWORD type;
158     char*buf;
159     rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS/* KEY_READ*/, &key);
160     if (rc != ERROR_SUCCESS) {
161         fprintf(stderr, "RegOpenKeyEx failed\n");
162         return 0;
163     }
164     rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
165     if(rc != ERROR_SUCCESS) {
166         fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
167         return 0;
168     }
169     buf = (char*)malloc(size+1);
170     rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
171     if(rc != ERROR_SUCCESS) {
172         fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
173         return 0;
174     }
175     if(type == REG_SZ || type == REG_EXPAND_SZ) {
176         while(size && buf[size-1] == '\0')
177           --size;
178         buf[size] = 0;
179         /* TODO: convert */
180         return buf;
181     } else if(type == REG_BINARY) {
182         return buf;
183     }
184 }
185
186 static int setRegistryEntry(char*key,char*value)
187 {
188     HKEY hkey1;
189     HKEY hkey2;
190     int ret1 = -1, ret2= -1;
191     ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
192     if(config_forAllUsers) {
193         ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
194     }
195
196     if(ret1 && ret2) {
197         fprintf(stderr, "registry: CreateKey %s failed\n", key);
198         return 0;
199     }
200     if(!ret1)
201         ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
202     if(!ret2)
203         ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
204     if(ret1 && ret2) {
205         fprintf(stderr, "registry: SetValue %s failed\n", key);
206         return 0;
207     }
208     return 1;
209 }
210
211 static HWND wnd_background = 0;
212 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
213 {
214     if(message == WM_CREATE) {
215         CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
216         if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
217             wnd_background = hwnd;
218         }
219     }
220     if(hwnd == wnd_background && message == WM_PAINT) {
221         HDC hdc;
222         PAINTSTRUCT ps;
223         RECT rc;
224         GetWindowRect(hwnd, &rc);
225         int width = rc.right - rc.left;
226         int height = rc.bottom - rc.top;
227
228         hdc = BeginPaint(hwnd, &ps);
229         SetBkMode(hdc, TRANSPARENT);
230
231         double s = width / 1282.0;
232         
233         HPEN pen0 = CreatePen(PS_SOLID, 2, RGB(0, 32, 128));
234         HPEN oldPen = (HPEN)SelectObject(hdc, pen0);
235         tball(hdc, 0.7*width,0.2*height,s,s);
236
237         HPEN pen1 = CreatePen(PS_SOLID, 2, RGB(0, 48, 128));
238         (HPEN)SelectObject(hdc, pen1);
239         tball(hdc, 0.233*width,0.3*height,s*2,s*2);
240         
241         HPEN pen2 = CreatePen(PS_SOLID, 2, RGB(0, 64, 128));
242         (HPEN)SelectObject(hdc, pen2);
243         tball(hdc, width, 1.2*height,s*3,-s*3);
244
245         SelectObject(hdc, oldPen); 
246         DeleteObject(pen1);
247         DeleteObject(pen2);
248         EndPaint(hwnd, &ps);
249         return 1;
250     }
251     return DefWindowProc(hwnd, message, wParam, lParam);
252 }
253
254 void processMessages()
255 {
256     MSG msg;
257     while(PeekMessage(&msg,NULL,0,0,0))
258     {
259         GetMessage(&msg, NULL, 0, 0);
260         TranslateMessage(&msg);
261         DispatchMessage(&msg);
262     }
263 }
264
265 int addRegistryEntries(char*install_dir)
266 {
267     int ret;
268     ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
269     if(!ret) return 0;
270     return 1;
271 }
272
273 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
274 {
275     WCHAR wszFilename[MAX_PATH];
276     IShellLink *ps1 = NULL;
277     IPersistFile *pPf = NULL;
278     HRESULT hr;
279     hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
280     if(FAILED(hr)) return 0;
281     hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
282     if(FAILED(hr)) return 0;
283     hr = ps1->lpVtbl->SetPath(ps1, path);
284     if(FAILED(hr)) return 0;
285     hr = ps1->lpVtbl->SetDescription(ps1, description);
286     
287     if (arguments) {
288         hr = ps1->lpVtbl->SetArguments(ps1, arguments);
289         if(FAILED(hr)) return 0;
290     }
291     if (iconpath) {
292         hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
293         if (FAILED(hr)) return 0;
294     }
295     if (workdir) {
296         hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
297         if (FAILED(hr)) return 0;
298     }
299     MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
300     hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
301     if(FAILED(hr)) {
302         return 0;
303     }
304     pPf->lpVtbl->Release(pPf);
305     ps1->lpVtbl->Release(ps1);
306     return 1;
307 }
308
309 static int CreateURL(const char*url, const char*path)
310 {
311     FILE*fi = fopen(path, "wb");
312     if(!fi)
313         return 0;
314     fprintf(fi, "[InternetShortcut]\r\n");
315     fprintf(fi, "URL=http://localhost:8081/\r\n");
316     fclose(fi);
317     return 1;
318 }
319
320
321 BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
322 {
323     LPNMHDR lpnm;
324         
325     HWND dialog = GetParent(hwnd);
326
327     if(message == WM_INITDIALOG) {
328         if(logo)
329             SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)logo);
330
331         RECT rc;
332         GetWindowRect(dialog, &rc);
333         int width = rc.right - rc.left;
334         int height = rc.bottom - rc.top;
335         MoveWindow(dialog, (GetSystemMetrics(SM_CXSCREEN) - width)/2, (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
336         return FALSE;
337      }
338
339     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
340         PropSheet_SetWizButtons(dialog, buttons);
341         return FALSE;
342     }
343     return FALSE;
344 }
345
346 BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
347     if(message == WM_INITDIALOG) {
348         SetDlgItemText(hwnd, IDC_LICENSE, license_text);
349     }
350     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
351 }
352 BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
353     if(message == WM_INITDIALOG) {
354         SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
355
356         config_forAllUsers = 0;
357         SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
358         SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
359     }
360     if(message == WM_COMMAND) {
361         if((wParam&0xffff) == IDC_BROWSE) {
362             BROWSEINFOA browse;
363             memset(&browse, 0, sizeof(browse));
364             browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
365             browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
366             memset(browse.pszDisplayName, 0, MAX_PATH);
367             browse.lpszTitle = "Select installation directory";
368             printf("Start browsing %s\n", browse.pszDisplayName);
369             browse.pidlRoot = SHBrowseForFolder(&browse);
370             printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
371             if(browse.pszDisplayName) {
372                 if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
373                     printf("Path is %s\n", browse.pszDisplayName);
374                     install_path = browse.pszDisplayName;
375                     int l = strlen(install_path);
376                     while(l && install_path[l-1]=='\\') {
377                         install_path[--l]=0;
378                     }   
379                 }
380             }
381             SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
382             return 0;
383
384         }
385         else if((wParam&0xffff) == IDC_ALLUSERS) {
386             config_forAllUsers = 1;
387             SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
388             SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
389         }
390         else if((wParam&0xffff) == IDC_CURRENTUSER) {
391             config_forAllUsers = 0;
392             SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
393             SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
394         }
395         else if((wParam&0xffff) == IDC_INSTALL_PATH) {
396             SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
397             if(pathBuf[0]) {
398                 install_path = pathBuf;
399                 int l = strlen(install_path);
400                 while(l && install_path[l-1]=='\\') {
401                     install_path[--l]=0;
402                 }   
403             }
404             return 0;
405         }
406     }
407     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
408 }
409 HWND statuswnd;
410 static int progress_pos = 0;
411 void PropertyArchiveStatus(int type, char*text)
412 {
413     if(text && text[0]=='[') return;
414     SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
415     int t;
416     /* There are usually 4 messages, and a range of 40 to fill, so 
417        step 9 times */
418     for(t=0;t<10;t++) {
419         SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, ++progress_pos, 0);
420         processMessages();
421         Sleep(30);
422     }
423     if(type<0) {
424         while(1) {
425             int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
426             if(ret==IDRETRY) continue;
427             else break;
428         }
429     }
430 }
431
432 void print_space(char*dest, char*msg, ULONGLONG size)
433 {
434     if(size < 1024)
435         sprintf(dest, "%s%d Bytes", msg, size);
436     else if(size < 1048576l)
437         sprintf(dest, "%s%.2f Kb", msg, size/1024.0);
438     else if(size < 1073741824l)
439         sprintf(dest, "%s%.2f Mb", msg, size/1048576.0);
440     else if(size < 1099511627776ll)
441         sprintf(dest, "%s%.2f Gb", msg, size/1073741824.0);
442     else
443         sprintf(dest, "%s%.2f Tb", msg, size/1125899906842624.0);
444 }
445
446 BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
447     HWND dialog = GetParent(hwnd);
448     if(message == WM_INITDIALOG) {
449         SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
450
451         char buf[256];
452         print_space(buf, "Space required: ", crn_decompressed_size);
453         SetDlgItemText(hwnd, IDC_SPACE1, buf);
454         ULARGE_INTEGER available,total,totalfree;
455         available.QuadPart=0;
456         total.QuadPart=0;
457         totalfree.QuadPart=0;
458         GetDiskFreeSpaceEx(install_path, &available, &total, &totalfree);
459         print_space(buf, "Space available: ", available.QuadPart);
460         SetDlgItemText(hwnd, IDC_SPACE2, buf);
461     }
462     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
463         SetDlgItemText(hwnd, IDC_SPACE1, "");
464         SetDlgItemText(hwnd, IDC_SPACE2, "");
465         PropSheet_SetWizButtons(dialog, 0);
466         SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0); //makes wine display a warning
467         SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
468         statuswnd = hwnd;
469         SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,40));
470         progress_pos = 0;
471         SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, progress_pos, 0);
472         int success = unpack_archive(crndata, crndata_len, install_path, PropertyArchiveStatus);
473         return 0;
474     }
475     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
476 }
477
478 static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
479
480 BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
481     if(message == WM_INITDIALOG) {
482         pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
483         FILE*fi = fopen(pdf2swf_path, "rb");
484         if(fi) {
485             config_createLinks = 1;
486             fclose(fi);
487         }
488         if(!config_createLinks) {
489             SendDlgItemMessage(hwnd, IDC_STARTMENU, SW_HIDE, 0, 0);
490             SendDlgItemMessage(hwnd, IDC_DESKTOP, SW_HIDE, 0, 0);
491         }
492
493         SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
494         SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createStartmenu, 0);
495     }
496     if(message == WM_COMMAND) {
497         if((wParam&0xffff) == IDC_STARTMENU) {
498             config_createStartmenu = SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_GETCHECK, 0, 0);
499             config_createStartmenu^=1;
500             SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
501             return 0;
502         }
503         if((wParam&0xffff) == IDC_DESKTOP) {
504             config_createDesktop = SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_GETCHECK, 0, 0);
505             config_createDesktop^=1;
506             SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
507             return 0;
508         }
509     }
510
511     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZFINISH)) {
512         if(!addRegistryEntries(install_path)) {
513             MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
514             return 1;
515         }
516
517         char mypath[MAX_PATH];
518         path_startmenu[0] = 0;
519         path_desktop[0] = 0;
520         if(config_forAllUsers) {
521             f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
522             f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_COMMON_PROGRAMS, 0);
523         }
524         /* get local program/desktop directory- this is both for forAllUsers=0 as well
525            as a fallback if the above didn't return any paths */
526         if(!path_startmenu[0]) {
527             f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
528         }
529         if(!path_desktop[0]) {
530             f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
531         }
532
533         if(config_createLinks) {
534             if(config_createDesktop && path_desktop[0]) {
535                 char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
536                 printf("Creating desktop link %s -> %s\n", linkName, pdf2swf_path);
537                 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
538                     MessageBox(0, "Couldn't create desktop shortcut", "Install.exe", MB_OK);
539                     return 1;
540                 }
541             }
542             if(config_createStartmenu && path_startmenu[0]) {
543                 char* group = concatPaths(path_startmenu, "pdf2swf");
544                 CreateDirectory(group, 0);
545                 char* linkName = concatPaths(group, "pdf2swf.lnk");
546                 printf("Creating %s -> %s\n", linkName, pdf2swf_path);
547                 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
548                     MessageBox(0, "Couldn't create start menu entry", "Install.exe", MB_OK);
549                     return 1;
550                 }
551             }
552         } else {
553             printf("not creating desktop/startmenu links\n");
554         }
555     }
556     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
557 }
558
559 #ifndef PSP_HIDEHEADER
560 #define PSP_HIDEHEADER  2048
561 #endif
562
563 typedef struct _wizardpage {
564     DLGPROC function;
565     int resource;
566 } wizardpage_t;
567
568 void runPropertySheet(HWND parent)
569 {
570     PROPSHEETHEADER sheet;
571
572     wizardpage_t wpage[5] = {
573         {PropertySheetFunc1, IDD_LICENSE},
574         {PropertySheetFunc2, IDD_INSTALLDIR},
575         {PropertySheetFunc3, IDD_PROGRESS},
576         {PropertySheetFunc4, IDD_FINISH},
577     };
578     int num = sizeof(wpage)/sizeof(wpage[0]);
579     HPROPSHEETPAGE pages[num];
580     int t;
581     for(t=0;t<num;t++) {
582         PROPSHEETPAGE page;
583         memset(&page, 0, sizeof(PROPSHEETPAGE));
584         page.dwSize = sizeof(PROPSHEETPAGE);
585         page.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
586         page.hInstance = me;
587         page.pfnDlgProc = wpage[t].function;
588         page.pszTemplate = MAKEINTRESOURCE(wpage[t].resource);
589         pages[t] = CreatePropertySheetPage(&page);
590     }
591
592     memset(&sheet, 0, sizeof(PROPSHEETHEADER));
593     sheet.dwSize = sizeof(PROPSHEETHEADER);
594     sheet.hInstance = me;
595     sheet.hwndParent = parent;
596     sheet.phpage = pages;
597     sheet.dwFlags = PSH_WIZARD;
598     sheet.nPages = num;
599     PropertySheet(&sheet);
600 }
601
602 int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
603 {
604     me = _me;
605     WNDCLASSEX wcl_background;
606     wcl_background.hInstance    = me;
607     wcl_background.lpfnWndProc  = WindowFunc;
608     wcl_background.lpszClassName= "SWFTools Install";
609     wcl_background.style        = CS_HREDRAW | CS_VREDRAW;
610     wcl_background.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
611     wcl_background.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
612     wcl_background.hCursor      = LoadCursor(NULL, IDC_ARROW);
613     wcl_background.lpszMenuName = NULL; //no menu
614     wcl_background.cbClsExtra   = 0;
615     wcl_background.cbWndExtra   = 0;
616     wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
617     wcl_background.cbSize       = sizeof(WNDCLASSEX);
618
619     HINSTANCE shell32 = LoadLibrary("shell32.dll");
620     if(!shell32) {
621         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
622         return 1;
623     }
624     f_SHGetSpecialFolderPath = (HRESULT (WINAPI*)(HWND,LPTSTR,int,BOOL))GetProcAddress(shell32, "SHGetSpecialFolderPathA");
625     if(!f_SHGetSpecialFolderPath) {
626         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
627         return 1;
628     }
629         
630     HRESULT coret = CoInitialize(NULL);
631     if(FAILED(coret)) {
632         MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
633         return 1;
634     }
635
636     path_programfiles[0] = 0;
637     f_SHGetSpecialFolderPath(NULL, path_programfiles, CSIDL_PROGRAM_FILES, 0);
638
639     if(!RegisterClassEx(&wcl_background)) {
640         MessageBox(0, "Could not register window background class", "Install.exe", MB_OK);
641         return 1;
642     }
643
644     HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
645                          0, 0, 0, 
646                          GetSystemMetrics(SM_CXFULLSCREEN),
647                          GetSystemMetrics(SM_CYFULLSCREEN),
648                          NULL, NULL, me, 
649                          (void*)"background");
650     
651     if(!background) {
652         MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
653         return 1;
654     }
655
656     ShowWindow(background, SW_SHOWMAXIMIZED);
657     SetForegroundWindow(background);
658     UpdateWindow(background);
659
660     logo = LoadBitmap(me, "SWFTOOLS");
661     
662     install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
663     if(!install_path || !install_path[0]) {
664         if(path_programfiles[0]) {
665             install_path = concatPaths(path_programfiles, "SWFTools");
666         } else {
667             install_path = "c:\\swftools\\";
668         }
669     }
670
671     CoInitialize(0);
672     InitCommonControls();
673
674     RECT r = {0,0,0,0};
675     GetWindowRect(background, &r);
676     int xx = 320, yy = 200;
677     if(r.right - r.left > 320)
678         xx = r.right - r.left;
679     if(r.right - r.left > 200)
680         yy = r.bottom - r.top;
681     
682     int ret = 0;
683     runPropertySheet(background);
684
685     return ret;
686 }
687