removed os.h include
[swftools.git] / installer / installer.c
index 5d977d9..ee1b3d0 100644 (file)
 #include <commctrl.h>
 #include <commdlg.h>
 #include <shlobj.h>
+#include <prsht.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include "installer.h"
 
 #include "depack.h"
 
 #include "../config.h" //for swftools version
 
+static int config_forAllUsers = 0;
+static int config_createLinks = 0;
+static int config_createStartmenu = 1;
+static int config_createDesktop = 1;
+
+static char path_startmenu[MAX_PATH] = "\0";
+static char path_desktop[MAX_PATH] = "\0";
+static char path_programfiles[MAX_PATH] = "\0";
+
 extern char*crndata;
+extern char*license_text;
 
 static char*install_path = "c:\\swftools\\";
 static char pathBuf[1024];
 static int do_abort = 0;
 
-static HWND wnd_progress = 0;
-static HWND wnd_params = 0;
+static char* pdf2swf_path;
+
+static HBITMAP logo = 0;
+
+static HINSTANCE me;
 
 #define USER_SETMESSAGE 0x7f01
 
-struct progress_data {
-    int width,height;
-    int bar_width;
-    int bar_height;
-    int bar_posx;
-    int bar_posy;
-    int pos,step,range;
-    char*text1;
-    char*text2;
-    char*text3;
-    HWND hwndButton;
-    HWND wnd_text3;
-};
-struct params_data {
-    int width,height;
-    int ok;
-    HWND installButton;
-    HWND edit;
-    HWND explore;
-};
+static void tball(HDC hdc, double px, double py, double sx, double sy)
+{
+    MoveToEx(hdc, 75.25*sx+px, -32.50*sy+py, 0);
+    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);
+    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);
+    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);
+    LineTo(hdc, 74.75*sx+px, 51.10*sy+py); LineTo(hdc, 74.95*sx+px, -3.45*sy+py);
+    MoveToEx(hdc, 74.75*sx+px, 51.10*sy+py, 0);
+    LineTo(hdc, 29.35*sx+px, 118.75*sy+py); LineTo(hdc, -45.20*sx+px, 155.35*sy+py);
+    LineTo(hdc, -127.95*sx+px, 145.10*sy+py); LineTo(hdc, -194.85*sx+px, 104.30*sy+py);
+    LineTo(hdc, -225.25*sx+px, 32.50*sy+py); LineTo(hdc, -224.75*sx+px, -51.10*sy+py);
+    LineTo(hdc, -179.35*sx+px, -118.75*sy+py); LineTo(hdc, -104.80*sx+px, -155.35*sy+py);
+    LineTo(hdc, -22.05*sx+px, -145.10*sy+py); LineTo(hdc, 44.85*sx+px, -104.30*sy+py);
+    LineTo(hdc, 17.30*sx+px, -91.80*sy+py); LineTo(hdc, -75.00*sx+px, -122.60*sy+py);
+    LineTo(hdc, -45.20*sx+px, -43.70*sy+py); LineTo(hdc, 17.30*sx+px, -91.80*sy+py);
+    LineTo(hdc, -22.05*sx+px, -145.10*sy+py); LineTo(hdc, -75.00*sx+px, -122.60*sy+py);
+    LineTo(hdc, -104.80*sx+px, -155.35*sy+py); LineTo(hdc, -167.30*sx+px, -107.25*sy+py);
+    LineTo(hdc, -75.00*sx+px, -122.60*sy+py); LineTo(hdc, -127.95*sx+px, -63.55*sy+py);
+    LineTo(hdc, -45.20*sx+px, -43.70*sy+py); LineTo(hdc, 29.35*sx+px, -10.10*sy+py);
+    LineTo(hdc, 47.60*sx+px, 63.80*sy+py); LineTo(hdc, -25.70*sx+px, 52.45*sy+py);
+    LineTo(hdc, -25.70*sx+px, 128.45*sy+py); LineTo(hdc, 47.60*sx+px, 63.80*sy+py);
+    LineTo(hdc, 29.35*sx+px, 118.75*sy+py); LineTo(hdc, -25.70*sx+px, 128.45*sy+py);
+    LineTo(hdc, -45.20*sx+px, 155.35*sy+py); LineTo(hdc, -118.85*sx+px, 143.85*sy+py);
+    LineTo(hdc, -127.95*sx+px, 145.10*sy+py);
+    MoveToEx(hdc, -179.35*sx+px, -118.75*sy+py, 0);
+    LineTo(hdc, -167.30*sx+px, -107.25*sy+py); LineTo(hdc, -127.95*sx+px, -63.55*sy+py);
+    LineTo(hdc, -118.85*sx+px, 21.65*sy+py); LineTo(hdc, -45.20*sx+px, -43.70*sy+py);
+    LineTo(hdc, -25.70*sx+px, 52.45*sy+py); LineTo(hdc, 29.35*sx+px, -10.10*sy+py);
+    MoveToEx(hdc, -225.25*sx+px, 32.50*sy+py, 0);
+    LineTo(hdc, -194.85*sx+px, -17.85*sy+py); LineTo(hdc, -224.75*sx+px, -51.10*sy+py);
+    LineTo(hdc, -167.30*sx+px, -107.25*sy+py); LineTo(hdc, -194.85*sx+px, -17.85*sy+py);
+    LineTo(hdc, -127.95*sx+px, -63.55*sy+py);
+    MoveToEx(hdc, -175.85*sx+px, 77.60*sy+py, 0);
+    LineTo(hdc, -96.35*sx+px, 99.55*sy+py); LineTo(hdc, -118.85*sx+px, 21.65*sy+py);
+    LineTo(hdc, -175.85*sx+px, 77.60*sy+py); LineTo(hdc, -225.25*sx+px, 32.50*sy+py);
+    MoveToEx(hdc, -118.85*sx+px, 143.85*sy+py, 0);
+    LineTo(hdc, -175.85*sx+px, 77.60*sy+py); LineTo(hdc, -194.85*sx+px, 104.30*sy+py);
+    LineTo(hdc, -118.85*sx+px, 143.85*sy+py); LineTo(hdc, -96.35*sx+px, 99.55*sy+py);
+    LineTo(hdc, -25.70*sx+px, 52.45*sy+py); LineTo(hdc, -118.85*sx+px, 21.65*sy+py);
+    LineTo(hdc, -194.85*sx+px, -17.85*sy+py); LineTo(hdc, -175.85*sx+px, 77.60*sy+py);
+    MoveToEx(hdc, -96.35*sx+px, 99.55*sy+py, 0);
+    LineTo(hdc, -25.70*sx+px, 128.45*sy+py); LineTo(hdc, -118.85*sx+px, 143.85*sy+py);
+}
+
+static char* concatPaths(const char*base, const char*add)
+{
+    int l1 = strlen(base);
+    int l2 = strlen(add);
+    int pos = 0;
+    char*n = 0;
+    while(l1 && base[l1-1] == '\\')
+       l1--;
+    while(pos < l2 && add[pos] == '\\')
+       pos++;
+
+    n = (char*)malloc(l1 + (l2-pos) + 2);
+    memcpy(n,base,l1);
+    n[l1]='\\';
+    strcpy(&n[l1+1],&add[pos]);
+    return n;
+}
 
+static int setRegistryEntry(char*key,char*value)
+{
+    HKEY hkey1;
+    HKEY hkey2;
+    int ret1 = -1, ret2= -1;
+    ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
+    if(config_forAllUsers) {
+       ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
+    }
+
+    if(ret1 && ret2) {
+       fprintf(stderr, "registry: CreateKey %s failed\n", key);
+       return 0;
+    }
+    if(!ret1)
+       ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
+    if(!ret2)
+       ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
+    if(ret1 && ret2) {
+       fprintf(stderr, "registry: SetValue %s failed\n", key);
+       return 0;
+    }
+    return 1;
+}
+
+static HWND wnd_background = 0;
 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
 {
-    printf("%08x, %d %08x %08x\n", hwnd, message, wParam, lParam);
-   
-    /* in order for the delegation below to also work for
-       WM_CREATE, we need to assign our window pointers *before* the
-       CreateWindow returns, because that's when the WM_CREATE event 
-       is sent  */
     if(message == WM_CREATE) {
        CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
-       if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "params")) {
-           wnd_params = hwnd;
-       }
-       if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "progress")) {
-           wnd_progress = hwnd;
+        if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
+           wnd_background = hwnd;
        }
     }
-
-    if(hwnd == 0) {
-       return DefWindowProc(hwnd, message, wParam, lParam);
-    } else if(hwnd == wnd_progress) {
-       static struct progress_data data;
-
-       switch(message)
-       {
-           case USER_SETMESSAGE:
-               data.text3 = (char*)wParam;
-               SendMessage(data.wnd_text3, WM_SETTEXT, 0, (LPARAM)data.text3);
-               return 0;
-           case WM_CREATE: {
-               memset(&data, 0, sizeof(data));
-               data.text1 = "Installing SWFTools";
-               data.text2 = (char*)malloc(strlen(install_path)+250);
-               sprintf(data.text2, "to %s", install_path);
-               data.pos = 0;
-               data.step = 1;
-
-               CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
-               RECT rc;
-               GetClientRect (hwnd, &rc);
-               data.width = rc.right - rc.left;
-               data.height = rc.bottom - rc.top;
-               data.bar_width = cs->cx - 17;
-               data.bar_height= 16;
-               data.bar_posx = (data.width -data.bar_width)/2;
-               data.bar_posy = 56;
-               data.range = 50;
-               data.hwndButton = CreateWindow (
-                       PROGRESS_CLASS,
-                       "Progress",
-                       WS_CHILD | WS_VISIBLE,
-                       data.bar_posx,
-                       data.bar_posy,
-                       data.bar_width, 
-                       data.bar_height,
-                       hwnd,  /* Parent */
-                       (HMENU)1,
-                       cs->hInstance,
-                       NULL
-                       );
-
-               data.wnd_text3 = CreateWindow (
-                       WC_EDIT,
-                       "text3",
-                       WS_CHILD | WS_VISIBLE | ES_READONLY | ES_CENTER,
-                       data.bar_posx,
-                       72,
-                       (rc.right - rc.left - data.bar_posx*2), 
-                       20,
-                       hwnd,  /* Parent */
-                       (HMENU)1,
-                       cs->hInstance,
-                       NULL
-                       );
-               SendMessage(data.wnd_text3, WM_SETTEXT, 0, (LPARAM)"");
-
-               SendMessage(data.hwndButton, PBM_SETRANGE, 0, (LPARAM) MAKELONG(0,data.range));
-               SendMessage(data.hwndButton, PBM_SETSTEP, (WPARAM) data.step, 0);
-               return 0;
-           }   
-           case PBM_STEPIT: {
-               if(data.pos+data.step < data.range) {
-                   data.pos += data.step;
-                   SendMessage(data.hwndButton, PBM_STEPIT, wParam, lParam);
-               }
-           }
-           case WM_PAINT: {
-               TEXTMETRIC    tm;
-               HDC           hdc;             /* A device context used for drawing */
-               PAINTSTRUCT   ps;              /* Also used during window drawing */
-               RECT          rc;              /* A rectangle used during drawing */
-                
-                hdc = GetDC(hwnd);
-                SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
-                GetTextMetrics(hdc, &tm);
-                ReleaseDC(hwnd, hdc);
-
-
-               hdc = BeginPaint (hwnd, &ps);
-
-               SetBkMode(hdc, TRANSPARENT);
-               
-               rc.top = 8; rc.left= 0; rc.right = data.width; rc.bottom = 24;
-               DrawText(hdc, data.text1, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
-
-               char buf[256];
-               char*text = data.text2;
-               if(tm.tmAveCharWidth * strlen(text) > data.width) {
-                   int chars = (data.width / tm.tmAveCharWidth)-8;
-                   if(chars>240) chars=240;
-                   strncpy(buf, text, chars);
-                   strcpy(&buf[chars],"...");
-                   text = buf;
-               }
-
-               rc.top = 32; rc.left= 0; rc.right = data.width; rc.bottom = 48;
-               DrawText(hdc, text, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
-
-               EndPaint (hwnd, &ps);
-               return 0;
-           }
-           case WM_DESTROY:
-               wnd_progress = 0;
-               return DefWindowProc(hwnd, message, wParam, lParam);
-           default:
-               return DefWindowProc(hwnd, message, wParam, lParam);
-       }
-    } else if(hwnd == wnd_params) {
-       static struct params_data data;
-       switch(message)
-       {
-           case WM_CREATE: {
-               memset(&data, 0, sizeof(data));
-               CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
-               RECT rc;
-               GetClientRect (hwnd, &rc);
-               data.width = rc.right - rc.left;
-               data.height = rc.bottom - rc.top;
-
-               //EDITTEXT IDD_EDIT,68,8,72,12, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER  | WS_TABSTOP
-               HWND text = CreateWindow(
-                       WC_STATIC,
-                       "Select Installation Directory:",
-                       WS_CHILD | WS_VISIBLE,
-                       32, 
-                       16,
-                       data.width-32*2, 
-                       20,
-                       hwnd,  /* Parent */
-                       (HMENU)1,
-                       cs->hInstance,
-                       NULL
-                       );
-
-               data.edit = CreateWindow (
-                       WC_EDIT,
-                       "EditPath",
-                       WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL,
-                       32, 
-                       48,
-                       (data.width-64)-32*2, 
-                       20,
-                       hwnd,  /* Parent */
-                       (HMENU)0x1234,
-                       cs->hInstance,
-                       NULL
-                       );
-               
-               data.explore = CreateWindow (
-                       WC_BUTTON,
-                       "Explore",
-                       WS_CHILD | WS_VISIBLE | WS_TABSTOP,
-                       data.width-32-64,
-                       48,
-                       64, 
-                       20,
-                       hwnd,  /* Parent */
-                       (HMENU)0x9999,
-                       cs->hInstance,
-                       NULL
-                       );
-               
-               data.installButton = CreateWindow (
-                       WC_BUTTON,
-                       "Install",
-                       WS_CHILD | WS_VISIBLE | WS_TABSTOP,
-                       (data.width - 80)/2,
-                       data.height - 32*2,
-                       80, 
-                       32,
-                       hwnd,  /* Parent */
-                       (HMENU)0xabcd,
-                       cs->hInstance,
-                       NULL
-                       );
-               
-               SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
-               return 0;
-           }   
-           case USER_SETMESSAGE: {
-               //install_path = (char*)lParam;
-               SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
-               printf("Setting path to %s\n", install_path);
-               return 0;
-           }
-           case WM_PAINT: {
-               return DefWindowProc(hwnd, message, wParam, lParam);
-           }
-           case WM_COMMAND: {
-               if((wParam&0xffff) == 0x9999) {
-                   BROWSEINFOA browse;
-                   memset(&browse, 0, sizeof(browse));
-                   browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
-                   browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
-                   memset(browse.pszDisplayName, 0, MAX_PATH);
-                   browse.lpszTitle = "Select installation directory";
-                   /*browse.pidlRoot = (ITEMIDLIST*)malloc(sizeof(ITEMIDLIST)*200);
-                   memset((void*)browse.pidlRoot, 0, sizeof(ITEMIDLIST)*200);*/
-                   printf("Start browsing %s\n", browse.pszDisplayName);
-                   //SHGetDesktopFolder
-                   //ParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
-                   //SHParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
-                   //SHBrowseForFolderA(&browse);
-                   browse.pidlRoot = SHBrowseForFolder(&browse);
-                   printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
-                   if(browse.pszDisplayName) {
-                       if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
-                           printf("Path is %s\n", browse.pszDisplayName);
-                           install_path = browse.pszDisplayName;
-                       }
-                   }
-                   SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
-                   return 0;
-               } else if((wParam&0xffff) == 0xabcd) {
-                   data.ok = 1;
-                   DestroyWindow(wnd_params);
-                   return 0;
-               } else if((wParam&0xffff) == 0x1234) {
-                   SendMessage(data.edit, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
-                   if(pathBuf[0]) {
-                       install_path = pathBuf;
-                       printf("Path edited: now \"%s\"\n", install_path);
-                   }
-                   return 0;
-               }
-               return DefWindowProc(hwnd, message, wParam, lParam);
-           }
-           case WM_KEYDOWN: {
-               if(wParam == 0x49) {
-                   DestroyWindow(wnd_params);
-               }
-               return 0;
-           }
-           case WM_DESTROY:
-               if(!data.ok) {
-                   do_abort = 1;
-                    PostQuitMessage (0);
-               }
-               wnd_params = 0;
-               return DefWindowProc(hwnd, message, wParam, lParam);
-           default:
-               return DefWindowProc(hwnd, message, wParam, lParam);
-       }
+    if(hwnd == wnd_background && message == WM_PAINT) {
+       HDC hdc;
+       PAINTSTRUCT ps;
+       RECT rc;
+        GetWindowRect(hwnd, &rc);
+       int width = rc.right - rc.left;
+       int height = rc.bottom - rc.top;
+
+       hdc = BeginPaint(hwnd, &ps);
+       SetBkMode(hdc, TRANSPARENT);
+
+        double s = width / 1282.0;
+       
+        HPEN pen0 = CreatePen(PS_SOLID, 2, RGB(0, 32, 128));
+       HPEN oldPen = (HPEN)SelectObject(hdc, pen0);
+        tball(hdc, 0.7*width,0.2*height,s,s);
+
+       HPEN pen1 = CreatePen(PS_SOLID, 2, RGB(0, 48, 128));
+       (HPEN)SelectObject(hdc, pen1);
+        tball(hdc, 0.233*width,0.3*height,s*2,s*2);
+       
+        HPEN pen2 = CreatePen(PS_SOLID, 2, RGB(0, 64, 128));
+       (HPEN)SelectObject(hdc, pen2);
+        tball(hdc, width, 1.2*height,s*3,-s*3);
+
+       SelectObject(hdc, oldPen); 
+       DeleteObject(pen1);
+       DeleteObject(pen2);
+       EndPaint(hwnd, &ps);
+       return 1;
     }
     return DefWindowProc(hwnd, message, wParam, lParam);
 }
@@ -339,164 +196,378 @@ void processMessages()
     }
 }
 
-static char*lastmessage = 0;
-void myarchivestatus(int type, char*text)
+int addRegistryEntries(char*install_dir)
 {
-    if(text && text[0]=='[')
-       return;
-    //printf("%s\n", text);
-                       
-    SendMessage(wnd_progress, USER_SETMESSAGE, (WPARAM)strdup(text), 0);
-    SendMessage(wnd_progress, WM_PAINT, 0, 0);
+    int ret;
+    ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
+    if(!ret) return 0;
+    return 1;
+}
+
+int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
+{
+    WCHAR wszFilename[MAX_PATH];
+    IShellLink *ps1 = NULL;
+    IPersistFile *pPf = NULL;
+    HRESULT hr;
+    hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
+    if(FAILED(hr)) return 0;
+    hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
+    if(FAILED(hr)) return 0;
+    hr = ps1->lpVtbl->SetPath(ps1, path);
+    if(FAILED(hr)) return 0;
+    hr = ps1->lpVtbl->SetDescription(ps1, description);
+    
+    if (arguments) {
+       hr = ps1->lpVtbl->SetArguments(ps1, arguments);
+       if(FAILED(hr)) return 0;
+    }
+    if (iconpath) {
+       hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
+       if (FAILED(hr)) return 0;
+    }
+    if (workdir) {
+       hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
+       if (FAILED(hr)) return 0;
+    }
+    MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
+    hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
+    if(FAILED(hr)) {
+       return 0;
+    }
+    pPf->lpVtbl->Release(pPf);
+    ps1->lpVtbl->Release(ps1);
+    return 1;
+}
+
+BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
+{
+    LPNMHDR lpnm;
+       
+    HWND dialog = GetParent(hwnd);
+
+    if(message == WM_INITDIALOG) {
+       if(logo)
+           SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)logo);
+
+        RECT rc;
+        GetWindowRect(dialog, &rc);
+       int width = rc.right - rc.left;
+       int height = rc.bottom - rc.top;
+        MoveWindow(dialog, (GetSystemMetrics(SM_CXSCREEN) - width)/2, (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
+       return FALSE;
+     }
+
+    if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
+       PropSheet_SetWizButtons(dialog, buttons);
+       return FALSE;
+    }
+    return FALSE;
+}
+
+BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+    if(message == WM_INITDIALOG) {
+       SetDlgItemText(hwnd, IDC_LICENSE, license_text);
+    }
+    return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
+}
+BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+    if(message == WM_INITDIALOG) {
+       SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
+
+        config_forAllUsers = 0;
+        SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
+        SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
+    }
+    if(message == WM_COMMAND) {
+       if((wParam&0xffff) == IDC_BROWSE) {
+           BROWSEINFOA browse;
+           memset(&browse, 0, sizeof(browse));
+           browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
+           browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
+           memset(browse.pszDisplayName, 0, MAX_PATH);
+           browse.lpszTitle = "Select installation directory";
+           printf("Start browsing %s\n", browse.pszDisplayName);
+           browse.pidlRoot = SHBrowseForFolder(&browse);
+           printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
+           if(browse.pszDisplayName) {
+               if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
+                   printf("Path is %s\n", browse.pszDisplayName);
+                   install_path = browse.pszDisplayName;
+               }
+           }
+           SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
+           return 0;
+
+       }
+       else if((wParam&0xffff) == IDC_ALLUSERS) {
+            config_forAllUsers = 1;
+            SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
+            SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
+        }
+       else if((wParam&0xffff) == IDC_CURRENTUSER) {
+            config_forAllUsers = 0;
+            SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
+            SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
+        }
+       else if((wParam&0xffff) == IDC_INSTALL_PATH) {
+           SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
+           if(pathBuf[0]) {
+               install_path = pathBuf;
+           }
+           return 0;
+       }
+    }
+    return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
+}
+HWND statuswnd;
+static int progress_pos = 0;
+void PropertyArchiveStatus(int type, char*text)
+{
+    if(text && text[0]=='[') return;
+    SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
     int t;
+    /* There are usually 6 messages, and a range of 54 to fill, so 
+       step 9 times */
     for(t=0;t<9;t++) {
-       SendMessage(wnd_progress, PBM_STEPIT, 0, 0);
-       /* while we're here, we might also make ourselves useful */
+       SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, ++progress_pos, 0);
        processMessages();
-       /* we want the user to see what we're writing, right? */
        Sleep(30);
     }
-
     if(type<0) {
        while(1) {
            int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
-           
-           /* there is no MB_CANCEL, so, *sigh*, we have to display
-              the "retry" button. So pretend it's doing anything... */
-           if(ret==IDRETRY)
-               continue;
-           else
-               break;
+           if(ret==IDRETRY) continue;
+           else break;
        }
     }
 }
-
-static int regEnter(char*key,char*value)
-{
-    HKEY hkey;
-    int ret = 0;
-    ret = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey);
-    if(ret != ERROR_SUCCESS) {
-       fprintf(stderr, "registry: CreateKey %s failed\n", key);
-       return 0;
+BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+    HWND dialog = GetParent(hwnd);
+    if(message == WM_INITDIALOG) {
+       SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
     }
-    ret = RegSetValue(hkey, NULL, REG_SZ, value, strlen(value)+1);
-    if(ret != ERROR_SUCCESS) {
-       fprintf(stderr, "registry: SetValue %s failed\n", key);
+    if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
+       PropSheet_SetWizButtons(dialog, 0);
+       SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0); //makes wine display a warning
+       SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
+       statuswnd = hwnd;
+       SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,54));
+       progress_pos = 0;
+       SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, progress_pos, 0);
+       int success = unpack_archive(crndata, install_path, PropertyArchiveStatus);
        return 0;
     }
-    return 1;
+    return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
 }
 
-int addRegistryEntries(char*install_dir)
+static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
+
+BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+    if(message == WM_INITDIALOG) {
+       pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
+       FILE*fi = fopen(pdf2swf_path, "rb");
+       if(fi) {
+           config_createLinks = 1;
+           fclose(fi);
+       }
+       if(!config_createLinks) {
+           SendDlgItemMessage(hwnd, IDC_STARTMENU, SW_HIDE, 0, 0);
+           SendDlgItemMessage(hwnd, IDC_DESKTOP, SW_HIDE, 0, 0);
+       }
+
+       SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
+       SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createStartmenu, 0);
+    }
+    if(message == WM_COMMAND) {
+       if((wParam&0xffff) == IDC_STARTMENU) {
+           config_createStartmenu = SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_GETCHECK, 0, 0);
+           config_createStartmenu^=1;
+           SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
+           return 0;
+       }
+       if((wParam&0xffff) == IDC_DESKTOP) {
+           config_createDesktop = SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_GETCHECK, 0, 0);
+           config_createDesktop^=1;
+           SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
+           return 0;
+       }
+    }
+
+    if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZFINISH)) {
+       if(!addRegistryEntries(install_path)) {
+           MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
+           return 1;
+       }
+
+        char mypath[MAX_PATH];
+        path_startmenu[0] = 0;
+        path_desktop[0] = 0;
+        if(config_forAllUsers) {
+            f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
+            f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_COMMON_PROGRAMS, 0);
+        }
+        /* get local program/desktop directory- this is both for forAllUsers=0 as well
+           as a fallback if the above didn't return any paths */
+        if(!path_startmenu[0]) {
+            f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
+        }
+        if(!path_desktop[0]) {
+            f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
+        }
+
+       if(config_createLinks) {
+           if(config_createDesktop && path_desktop[0]) {
+               char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
+                printf("Creating desktop link %s -> %s\n", linkName, pdf2swf_path);
+               if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
+                   MessageBox(0, "Couldn't create desktop shortcut", "Install.exe", MB_OK);
+                   return 1;
+               }
+           }
+           if(config_createStartmenu && path_startmenu[0]) {
+               char* group = concatPaths(path_startmenu, "pdf2swf");
+               CreateDirectory(group, 0);
+               char* linkName = concatPaths(group, "pdf2swf.lnk");
+                printf("Creating %s -> %s\n", linkName, pdf2swf_path);
+               if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
+                   MessageBox(0, "Couldn't create start menu entry", "Install.exe", MB_OK);
+                   return 1;
+               }
+           }
+       } else {
+            printf("not creating desktop/startmenu links\n");
+        }
+    }
+    return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
+}
+
+#ifndef PSP_HIDEHEADER
+#define PSP_HIDEHEADER 2048
+#endif
+
+typedef struct _wizardpage {
+    DLGPROC function;
+    int resource;
+} wizardpage_t;
+
+void runPropertySheet(HWND parent)
 {
-    int ret;
-    ret = regEnter("Software\\quiss.org\\swftools\\InstallPath", install_dir);
-    if(!ret) return 0;
-    return 1;
+    PROPSHEETHEADER sheet;
+
+    wizardpage_t wpage[5] = {
+       {PropertySheetFunc1, IDD_LICENSE},
+       {PropertySheetFunc2, IDD_INSTALLDIR},
+       {PropertySheetFunc3, IDD_PROGRESS},
+       {PropertySheetFunc4, IDD_FINISH},
+    };
+    int num = sizeof(wpage)/sizeof(wpage[0]);
+    HPROPSHEETPAGE pages[num];
+    int t;
+    for(t=0;t<num;t++) {
+       PROPSHEETPAGE page;
+       memset(&page, 0, sizeof(PROPSHEETPAGE));
+       page.dwSize = sizeof(PROPSHEETPAGE);
+       page.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
+       page.hInstance = me;
+       page.pfnDlgProc = wpage[t].function;
+       page.pszTemplate = MAKEINTRESOURCE(wpage[t].resource);
+       pages[t] = CreatePropertySheetPage(&page);
+    }
+
+    memset(&sheet, 0, sizeof(PROPSHEETHEADER));
+    sheet.dwSize = sizeof(PROPSHEETHEADER);
+    sheet.hInstance = me;
+    sheet.hwndParent = parent;
+    sheet.phpage = pages;
+    sheet.dwFlags = PSH_WIZARD;
+    sheet.nPages = num;
+    PropertySheet(&sheet);
 }
 
-int WINAPI WinMain(HINSTANCE me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
+int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
 {
-    WNDCLASSEX wcl;
-
-    wcl.hInstance    = me;
-    wcl.lpszClassName= "SWFTools-Installer";
-    wcl.lpfnWndProc  = WindowFunc;
-    wcl.style        = CS_HREDRAW | CS_VREDRAW;
-    wcl.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
-    wcl.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
-    wcl.hCursor      = LoadCursor(NULL, IDC_ARROW);
-    wcl.lpszMenuName = NULL; //no menu
-    wcl.cbClsExtra   = 0;
-    wcl.cbWndExtra   = 0;
-    //wcl.hbrBackground= (HBRUSH) GetStockObject(DKGRAY_BRUSH);
-    //wcl.hbrBackground= (HBRUSH) GetStockObject (WHITE_BRUSH);
-    wcl.hbrBackground= (HBRUSH) GetStockObject (LTGRAY_BRUSH);
-    //wcl.hbrBackground= (HBRUSH) GetStockObject (GRAY_BRUSH);
-    wcl.cbSize       = sizeof(WNDCLASSEX);
-
-    if(!RegisterClassEx (&wcl)) {
-       return 0;
+    me = _me;
+    WNDCLASSEX wcl_background;
+    wcl_background.hInstance    = me;
+    wcl_background.lpfnWndProc  = WindowFunc;
+    wcl_background.lpszClassName= "SWFTools Install";
+    wcl_background.style        = CS_HREDRAW | CS_VREDRAW;
+    wcl_background.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
+    wcl_background.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
+    wcl_background.hCursor      = LoadCursor(NULL, IDC_ARROW);
+    wcl_background.lpszMenuName = NULL; //no menu
+    wcl_background.cbClsExtra   = 0;
+    wcl_background.cbWndExtra   = 0;
+    wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
+    wcl_background.cbSize       = sizeof(WNDCLASSEX);
+
+    HINSTANCE shell32 = LoadLibrary("shell32.dll");
+    if(!shell32) {
+       MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
+       return 1;
+    }
+    f_SHGetSpecialFolderPath = (HRESULT (WINAPI*)(HWND,LPTSTR,int,BOOL))GetProcAddress(shell32, "SHGetSpecialFolderPathA");
+    if(!f_SHGetSpecialFolderPath) {
+       MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
+       return 1;
+    }
+       
+    HRESULT coret = CoInitialize(NULL);
+    if(FAILED(coret)) {
+       MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
+       return 1;
     }
 
-    CoInitialize(0);
-    InitCommonControls();
-   
-    CreateWindow (
-           wcl.lpszClassName,          /* Class name */
-           "SWFTools Installer",            /* Caption */
-           WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX),        /* Style */
-           CW_USEDEFAULT,              /* Initial x (use default) */
-           CW_USEDEFAULT,              /* Initial y (use default) */
-           320,                        /* Initial x size */
-           200,                        /* Initial y size */
-           NULL,                       /* No parent window */
-           NULL,                       /* No menu */
-           me,                         /* This program instance */
-           (void*)"params"             /* Creation parameters */
-           );
-
-    ShowWindow (wnd_params, nWinMode);
-    UpdateWindow (wnd_params);
-   
-    MSG msg;
-    while(wnd_params)
-    {
-       GetMessage(&msg,NULL,0,0);
-        TranslateMessage(&msg);
-        DispatchMessage(&msg);
+    path_programfiles[0] = 0;
+    f_SHGetSpecialFolderPath(NULL, path_programfiles, CSIDL_PROGRAM_FILES, 0);
+
+    if(!RegisterClassEx(&wcl_background)) {
+        MessageBox(0, "Could not register window background class", "Install.exe", MB_OK);
+        return 1;
     }
 
-    if(do_abort)
-       return 0;
-   
-    /*char buf[1024];
-    sprintf(buf, "Do you want me to install SWFTools into the directory %s now?", install_path);
-    int ret = MessageBox(0, buf, "SWFTools Install", MB_YESNO|MB_ICONQUESTION);
-    if(ret == IDNO)
-       return 0;*/
+    HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
+                        0, 0, 0, 
+                        GetSystemMetrics(SM_CXFULLSCREEN),
+                        GetSystemMetrics(SM_CYFULLSCREEN),
+                        NULL, NULL, me, 
+                        (void*)"background");
     
-    CreateWindow (
-           wcl.lpszClassName,          /* Class name */
-           "Installing...",            /* Caption */
-           WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX),        /* Style */
-           CW_USEDEFAULT,              /* Initial x (use default) */
-           CW_USEDEFAULT,              /* Initial y (use default) */
-           260,                        /* Initial x size */
-           128,                        /* Initial y size */
-           NULL,                       /* No parent window */
-           NULL,                       /* No menu */
-           me,                         /* This program instance */
-           (void*)"progress"           /* Creation parameters */
-           );
-    ShowWindow (wnd_progress, nWinMode);
-    UpdateWindow (wnd_progress);
-    
-    int success = unpack_archive(crndata, "C:\\swftools\\", myarchivestatus);
-   
-    DestroyWindow(wnd_progress);
+    if(!background) {
+       MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
+       return 1;
+    }
 
-    while(wnd_progress)
-       processMessages();
+    ShowWindow(background, SW_SHOWMAXIMIZED);
+    SetForegroundWindow(background);
+    UpdateWindow(background);
 
-    if(!addRegistryEntries(install_path)) {
-       success = 0;
-       MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
+    logo = LoadBitmap(me, "SWFTOOLS");
+    
+    install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
+    if(!install_path || !install_path[0]) {
+        if(path_programfiles[0]) {
+            install_path = concatPaths(path_programfiles, "SWFTools");
+        } else {
+           install_path = "c:\\swftools\\";
+        }
     }
 
-    if(success) {
-       char buf[1024];
-       sprintf(buf, "SWFTools Version %s has been installed into %s successfully", VERSION, install_path);
-       MessageBox(0, buf, "SWFTools Install", MB_OK|MB_ICONINFORMATION);
-    } else {
-       /* error will already have been notified by either myarchivestatus or some other
-          routine */
-       /*sprintf(buf, "Installation failed\nLast message: %s", lastmessage);
-       ret = MessageBox(0, buf, "SWFTools Install", MB_OK|MB_ICONERROR);*/
-    }
-    exit(0);
-}
+    CoInitialize(0);
+    InitCommonControls();
 
+    RECT r = {0,0,0,0};
+    GetWindowRect(background, &r);
+    int xx = 320, yy = 200;
+    if(r.right - r.left > 320)
+       xx = r.right - r.left;
+    if(r.right - r.left > 200)
+       yy = r.bottom - r.top;
+    
+    int ret = 0;
+    runPropertySheet(background);
 
+    return ret;
+}