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