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