removed os.h include
[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[1024];
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 int setRegistryEntry(char*key,char*value)
121 {
122     HKEY hkey1;
123     HKEY hkey2;
124     int ret1 = -1, ret2= -1;
125     ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
126     if(config_forAllUsers) {
127         ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
128     }
129
130     if(ret1 && ret2) {
131         fprintf(stderr, "registry: CreateKey %s failed\n", key);
132         return 0;
133     }
134     if(!ret1)
135         ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
136     if(!ret2)
137         ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
138     if(ret1 && ret2) {
139         fprintf(stderr, "registry: SetValue %s failed\n", key);
140         return 0;
141     }
142     return 1;
143 }
144
145 static HWND wnd_background = 0;
146 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
147 {
148     if(message == WM_CREATE) {
149         CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
150         if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
151             wnd_background = hwnd;
152         }
153     }
154     if(hwnd == wnd_background && message == WM_PAINT) {
155         HDC hdc;
156         PAINTSTRUCT ps;
157         RECT rc;
158         GetWindowRect(hwnd, &rc);
159         int width = rc.right - rc.left;
160         int height = rc.bottom - rc.top;
161
162         hdc = BeginPaint(hwnd, &ps);
163         SetBkMode(hdc, TRANSPARENT);
164
165         double s = width / 1282.0;
166         
167         HPEN pen0 = CreatePen(PS_SOLID, 2, RGB(0, 32, 128));
168         HPEN oldPen = (HPEN)SelectObject(hdc, pen0);
169         tball(hdc, 0.7*width,0.2*height,s,s);
170
171         HPEN pen1 = CreatePen(PS_SOLID, 2, RGB(0, 48, 128));
172         (HPEN)SelectObject(hdc, pen1);
173         tball(hdc, 0.233*width,0.3*height,s*2,s*2);
174         
175         HPEN pen2 = CreatePen(PS_SOLID, 2, RGB(0, 64, 128));
176         (HPEN)SelectObject(hdc, pen2);
177         tball(hdc, width, 1.2*height,s*3,-s*3);
178
179         SelectObject(hdc, oldPen); 
180         DeleteObject(pen1);
181         DeleteObject(pen2);
182         EndPaint(hwnd, &ps);
183         return 1;
184     }
185     return DefWindowProc(hwnd, message, wParam, lParam);
186 }
187
188 void processMessages()
189 {
190     MSG msg;
191     while(PeekMessage(&msg,NULL,0,0,0))
192     {
193         GetMessage(&msg, NULL, 0, 0);
194         TranslateMessage(&msg);
195         DispatchMessage(&msg);
196     }
197 }
198
199 int addRegistryEntries(char*install_dir)
200 {
201     int ret;
202     ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
203     if(!ret) return 0;
204     return 1;
205 }
206
207 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
208 {
209     WCHAR wszFilename[MAX_PATH];
210     IShellLink *ps1 = NULL;
211     IPersistFile *pPf = NULL;
212     HRESULT hr;
213     hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
214     if(FAILED(hr)) return 0;
215     hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
216     if(FAILED(hr)) return 0;
217     hr = ps1->lpVtbl->SetPath(ps1, path);
218     if(FAILED(hr)) return 0;
219     hr = ps1->lpVtbl->SetDescription(ps1, description);
220     
221     if (arguments) {
222         hr = ps1->lpVtbl->SetArguments(ps1, arguments);
223         if(FAILED(hr)) return 0;
224     }
225     if (iconpath) {
226         hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
227         if (FAILED(hr)) return 0;
228     }
229     if (workdir) {
230         hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
231         if (FAILED(hr)) return 0;
232     }
233     MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
234     hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
235     if(FAILED(hr)) {
236         return 0;
237     }
238     pPf->lpVtbl->Release(pPf);
239     ps1->lpVtbl->Release(ps1);
240     return 1;
241 }
242
243 BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
244 {
245     LPNMHDR lpnm;
246         
247     HWND dialog = GetParent(hwnd);
248
249     if(message == WM_INITDIALOG) {
250         if(logo)
251             SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)logo);
252
253         RECT rc;
254         GetWindowRect(dialog, &rc);
255         int width = rc.right - rc.left;
256         int height = rc.bottom - rc.top;
257         MoveWindow(dialog, (GetSystemMetrics(SM_CXSCREEN) - width)/2, (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
258         return FALSE;
259      }
260
261     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
262         PropSheet_SetWizButtons(dialog, buttons);
263         return FALSE;
264     }
265     return FALSE;
266 }
267
268 BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
269     if(message == WM_INITDIALOG) {
270         SetDlgItemText(hwnd, IDC_LICENSE, license_text);
271     }
272     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
273 }
274 BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
275     if(message == WM_INITDIALOG) {
276         SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
277
278         config_forAllUsers = 0;
279         SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
280         SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
281     }
282     if(message == WM_COMMAND) {
283         if((wParam&0xffff) == IDC_BROWSE) {
284             BROWSEINFOA browse;
285             memset(&browse, 0, sizeof(browse));
286             browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
287             browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
288             memset(browse.pszDisplayName, 0, MAX_PATH);
289             browse.lpszTitle = "Select installation directory";
290             printf("Start browsing %s\n", browse.pszDisplayName);
291             browse.pidlRoot = SHBrowseForFolder(&browse);
292             printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
293             if(browse.pszDisplayName) {
294                 if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
295                     printf("Path is %s\n", browse.pszDisplayName);
296                     install_path = browse.pszDisplayName;
297                 }
298             }
299             SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
300             return 0;
301
302         }
303         else if((wParam&0xffff) == IDC_ALLUSERS) {
304             config_forAllUsers = 1;
305             SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
306             SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
307         }
308         else if((wParam&0xffff) == IDC_CURRENTUSER) {
309             config_forAllUsers = 0;
310             SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
311             SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
312         }
313         else if((wParam&0xffff) == IDC_INSTALL_PATH) {
314             SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
315             if(pathBuf[0]) {
316                 install_path = pathBuf;
317             }
318             return 0;
319         }
320     }
321     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
322 }
323 HWND statuswnd;
324 static int progress_pos = 0;
325 void PropertyArchiveStatus(int type, char*text)
326 {
327     if(text && text[0]=='[') return;
328     SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
329     int t;
330     /* There are usually 6 messages, and a range of 54 to fill, so 
331        step 9 times */
332     for(t=0;t<9;t++) {
333         SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, ++progress_pos, 0);
334         processMessages();
335         Sleep(30);
336     }
337     if(type<0) {
338         while(1) {
339             int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
340             if(ret==IDRETRY) continue;
341             else break;
342         }
343     }
344 }
345 BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
346     HWND dialog = GetParent(hwnd);
347     if(message == WM_INITDIALOG) {
348         SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
349     }
350     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
351         PropSheet_SetWizButtons(dialog, 0);
352         SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0); //makes wine display a warning
353         SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
354         statuswnd = hwnd;
355         SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,54));
356         progress_pos = 0;
357         SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, progress_pos, 0);
358         int success = unpack_archive(crndata, install_path, PropertyArchiveStatus);
359         return 0;
360     }
361     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
362 }
363
364 static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
365
366 BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
367     if(message == WM_INITDIALOG) {
368         pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
369         FILE*fi = fopen(pdf2swf_path, "rb");
370         if(fi) {
371             config_createLinks = 1;
372             fclose(fi);
373         }
374         if(!config_createLinks) {
375             SendDlgItemMessage(hwnd, IDC_STARTMENU, SW_HIDE, 0, 0);
376             SendDlgItemMessage(hwnd, IDC_DESKTOP, SW_HIDE, 0, 0);
377         }
378
379         SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
380         SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createStartmenu, 0);
381     }
382     if(message == WM_COMMAND) {
383         if((wParam&0xffff) == IDC_STARTMENU) {
384             config_createStartmenu = SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_GETCHECK, 0, 0);
385             config_createStartmenu^=1;
386             SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
387             return 0;
388         }
389         if((wParam&0xffff) == IDC_DESKTOP) {
390             config_createDesktop = SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_GETCHECK, 0, 0);
391             config_createDesktop^=1;
392             SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
393             return 0;
394         }
395     }
396
397     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZFINISH)) {
398         if(!addRegistryEntries(install_path)) {
399             MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
400             return 1;
401         }
402
403         char mypath[MAX_PATH];
404         path_startmenu[0] = 0;
405         path_desktop[0] = 0;
406         if(config_forAllUsers) {
407             f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
408             f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_COMMON_PROGRAMS, 0);
409         }
410         /* get local program/desktop directory- this is both for forAllUsers=0 as well
411            as a fallback if the above didn't return any paths */
412         if(!path_startmenu[0]) {
413             f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
414         }
415         if(!path_desktop[0]) {
416             f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
417         }
418
419         if(config_createLinks) {
420             if(config_createDesktop && path_desktop[0]) {
421                 char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
422                 printf("Creating desktop link %s -> %s\n", linkName, pdf2swf_path);
423                 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
424                     MessageBox(0, "Couldn't create desktop shortcut", "Install.exe", MB_OK);
425                     return 1;
426                 }
427             }
428             if(config_createStartmenu && path_startmenu[0]) {
429                 char* group = concatPaths(path_startmenu, "pdf2swf");
430                 CreateDirectory(group, 0);
431                 char* linkName = concatPaths(group, "pdf2swf.lnk");
432                 printf("Creating %s -> %s\n", linkName, pdf2swf_path);
433                 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
434                     MessageBox(0, "Couldn't create start menu entry", "Install.exe", MB_OK);
435                     return 1;
436                 }
437             }
438         } else {
439             printf("not creating desktop/startmenu links\n");
440         }
441     }
442     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
443 }
444
445 #ifndef PSP_HIDEHEADER
446 #define PSP_HIDEHEADER  2048
447 #endif
448
449 typedef struct _wizardpage {
450     DLGPROC function;
451     int resource;
452 } wizardpage_t;
453
454 void runPropertySheet(HWND parent)
455 {
456     PROPSHEETHEADER sheet;
457
458     wizardpage_t wpage[5] = {
459         {PropertySheetFunc1, IDD_LICENSE},
460         {PropertySheetFunc2, IDD_INSTALLDIR},
461         {PropertySheetFunc3, IDD_PROGRESS},
462         {PropertySheetFunc4, IDD_FINISH},
463     };
464     int num = sizeof(wpage)/sizeof(wpage[0]);
465     HPROPSHEETPAGE pages[num];
466     int t;
467     for(t=0;t<num;t++) {
468         PROPSHEETPAGE page;
469         memset(&page, 0, sizeof(PROPSHEETPAGE));
470         page.dwSize = sizeof(PROPSHEETPAGE);
471         page.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
472         page.hInstance = me;
473         page.pfnDlgProc = wpage[t].function;
474         page.pszTemplate = MAKEINTRESOURCE(wpage[t].resource);
475         pages[t] = CreatePropertySheetPage(&page);
476     }
477
478     memset(&sheet, 0, sizeof(PROPSHEETHEADER));
479     sheet.dwSize = sizeof(PROPSHEETHEADER);
480     sheet.hInstance = me;
481     sheet.hwndParent = parent;
482     sheet.phpage = pages;
483     sheet.dwFlags = PSH_WIZARD;
484     sheet.nPages = num;
485     PropertySheet(&sheet);
486 }
487
488 int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
489 {
490     me = _me;
491     WNDCLASSEX wcl_background;
492     wcl_background.hInstance    = me;
493     wcl_background.lpfnWndProc  = WindowFunc;
494     wcl_background.lpszClassName= "SWFTools Install";
495     wcl_background.style        = CS_HREDRAW | CS_VREDRAW;
496     wcl_background.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
497     wcl_background.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
498     wcl_background.hCursor      = LoadCursor(NULL, IDC_ARROW);
499     wcl_background.lpszMenuName = NULL; //no menu
500     wcl_background.cbClsExtra   = 0;
501     wcl_background.cbWndExtra   = 0;
502     wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
503     wcl_background.cbSize       = sizeof(WNDCLASSEX);
504
505     HINSTANCE shell32 = LoadLibrary("shell32.dll");
506     if(!shell32) {
507         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
508         return 1;
509     }
510     f_SHGetSpecialFolderPath = (HRESULT (WINAPI*)(HWND,LPTSTR,int,BOOL))GetProcAddress(shell32, "SHGetSpecialFolderPathA");
511     if(!f_SHGetSpecialFolderPath) {
512         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
513         return 1;
514     }
515         
516     HRESULT coret = CoInitialize(NULL);
517     if(FAILED(coret)) {
518         MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
519         return 1;
520     }
521
522     path_programfiles[0] = 0;
523     f_SHGetSpecialFolderPath(NULL, path_programfiles, CSIDL_PROGRAM_FILES, 0);
524
525     if(!RegisterClassEx(&wcl_background)) {
526         MessageBox(0, "Could not register window background class", "Install.exe", MB_OK);
527         return 1;
528     }
529
530     HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
531                          0, 0, 0, 
532                          GetSystemMetrics(SM_CXFULLSCREEN),
533                          GetSystemMetrics(SM_CYFULLSCREEN),
534                          NULL, NULL, me, 
535                          (void*)"background");
536     
537     if(!background) {
538         MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
539         return 1;
540     }
541
542     ShowWindow(background, SW_SHOWMAXIMIZED);
543     SetForegroundWindow(background);
544     UpdateWindow(background);
545
546     logo = LoadBitmap(me, "SWFTOOLS");
547     
548     install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
549     if(!install_path || !install_path[0]) {
550         if(path_programfiles[0]) {
551             install_path = concatPaths(path_programfiles, "SWFTools");
552         } else {
553             install_path = "c:\\swftools\\";
554         }
555     }
556
557     CoInitialize(0);
558     InitCommonControls();
559
560     RECT r = {0,0,0,0};
561     GetWindowRect(background, &r);
562     int xx = 320, yy = 200;
563     if(r.right - r.left > 320)
564         xx = r.right - r.left;
565     if(r.right - r.left > 200)
566         yy = r.bottom - r.top;
567     
568     int ret = 0;
569     runPropertySheet(background);
570
571     return ret;
572 }
573