added start menu + desktop link creation code
[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 <stdio.h>
26 #include <stdlib.h>
27
28 #include "depack.h"
29
30 #include "../config.h" //for swftools version
31 #include "../lib/os.h" //for registry functions
32
33 static int config_forAllUsers = 0;
34 static int config_createLinks = 0;
35 static int config_createStartmenu = 1;
36 static int config_createDesktop = 1;
37
38
39 extern char*crndata;
40
41 static char*install_path = "c:\\swftools\\";
42 static char pathBuf[1024];
43 static int do_abort = 0;
44
45 static HWND wnd_params = 0;
46 static HWND wnd_progress = 0;
47 static HWND wnd_finish = 0;
48 static HWND wnd_background = 0;
49
50 static HBITMAP logo;
51
52 #define USER_SETMESSAGE 0x7f01
53
54 struct progress_data {
55     int width,height;
56     int bar_width;
57     int bar_height;
58     int bar_posx;
59     int bar_posy;
60     int pos,step,range;
61     char*text1;
62     char*text2;
63     char*text3;
64     HWND hwndButton;
65     HWND wnd_text3;
66 };
67 struct params_data {
68     int width,height;
69     int ok;
70     HWND installButton;
71     HWND edit;
72     HWND explore;
73 };
74 struct finish_data {
75     int width,height;
76     int ok;
77     char*text;
78     HWND installButton;
79     HWND check1;
80     HWND check2;
81 };
82
83 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
84 {
85     //printf("%08x, %d %08x %08x\n", hwnd, message, wParam, lParam);
86    
87     /* in order for the delegation below to also work for
88        WM_CREATE, we need to assign our window pointers *before* the
89        CreateWindow returns, because that's when the WM_CREATE event 
90        is sent  */
91
92     if(message == WM_CREATE) {
93         CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
94         if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "params")) {
95             wnd_params = hwnd;
96         }
97         else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "progress")) {
98             wnd_progress = hwnd;
99         }
100         else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "finish")) {
101             wnd_finish = hwnd;
102         }
103         else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
104             wnd_background = hwnd;
105         }
106     }
107
108     if(hwnd == 0) {
109         return DefWindowProc(hwnd, message, wParam, lParam);
110     } else if(hwnd == wnd_progress) {
111         static struct progress_data data;
112
113         switch(message)
114         {
115             case USER_SETMESSAGE:
116                 data.text3 = (char*)wParam;
117                 SendMessage(data.wnd_text3, WM_SETTEXT, 0, (LPARAM)data.text3);
118                 return 0;
119             case WM_CREATE: {
120                 memset(&data, 0, sizeof(data));
121                 data.text1 = "Installing SWFTools";
122                 data.text2 = (char*)malloc(strlen(install_path)+250);
123                 data.text3 = "";
124                 sprintf(data.text2, "to %s", install_path);
125                 data.pos = 0;
126                 data.step = 1;
127
128                 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
129                 RECT rc;
130                 GetClientRect (hwnd, &rc);
131                 data.width = rc.right - rc.left;
132                 data.height = rc.bottom - rc.top;
133                 data.bar_width = cs->cx - 17;
134                 data.bar_height= 16;
135                 data.bar_posx = (data.width -data.bar_width)/2;
136                 data.bar_posy = 56;
137                 data.range = 50;
138
139                 data.hwndButton = CreateWindow (
140                         PROGRESS_CLASS,
141                         "Progress",
142                         WS_CHILD | WS_VISIBLE,
143                         data.bar_posx,
144                         data.bar_posy,
145                         data.bar_width, 
146                         data.bar_height,
147                         hwnd,  /* Parent */
148                         0,
149                         cs->hInstance,
150                         NULL
151                         );
152
153                 SendMessage(data.hwndButton, PBM_SETRANGE, 0, (LPARAM) MAKELONG(0,data.range));
154                 SendMessage(data.hwndButton, PBM_SETSTEP, (WPARAM) data.step, 0);
155                 return 0;
156             }   
157             case PBM_STEPIT: {
158                 if(data.pos+data.step < data.range) {
159                     data.pos += data.step;
160                     SendMessage(data.hwndButton, PBM_STEPIT, wParam, lParam);
161                 }
162             }
163             case WM_PAINT: {
164                 TEXTMETRIC    tm;
165                 HDC           hdc;             /* A device context used for drawing */
166                 PAINTSTRUCT   ps;              /* Also used during window drawing */
167                 RECT          rc;              /* A rectangle used during drawing */
168                 
169                 hdc = GetDC(hwnd);
170                 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
171                 GetTextMetrics(hdc, &tm);
172                 ReleaseDC(hwnd, hdc);
173
174                 hdc = BeginPaint (hwnd, &ps);
175
176                 /*
177                 // draw logo 
178                 HDC memDc=CreateCompatibleDC(hdc);
179                 SelectObject(memDc,logo);
180                 BitBlt(hdc,0,0,406,93,memDc,0,0,SRCCOPY);
181                 DeleteDC(memDc);
182                 // /
183                 */
184
185                 SetBkMode(hdc, TRANSPARENT);
186                 
187                 rc.top = 8; rc.left= 0; rc.right = data.width; rc.bottom = 24;
188                 DrawText(hdc, data.text1, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
189
190                 char buf[256];
191                 char*text = data.text2;
192                 if(tm.tmAveCharWidth * strlen(text) > data.width) {
193                     int chars = (data.width / tm.tmAveCharWidth)-8;
194                     if(chars>240) chars=240;
195                     strncpy(buf, text, chars);
196                     strcpy(&buf[chars],"...");
197                     text = buf;
198                 }
199
200                 rc.top = 32; rc.left= 0; rc.right = data.width; rc.bottom = 48;
201                 DrawText(hdc, text, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
202                 
203                 //rc.top = data.height-32; rc.left= 0; rc.right = data.width; rc.bottom = data.height;
204                 //DrawText(hdc, data.text3, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
205
206                 EndPaint (hwnd, &ps);
207                 return 0;
208             }
209             case WM_DESTROY:
210                 wnd_progress = 0;
211                 return DefWindowProc(hwnd, message, wParam, lParam);
212             default:
213                 return DefWindowProc(hwnd, message, wParam, lParam);
214         }
215     } else if(hwnd == wnd_params) {
216         static struct params_data data;
217         switch(message)
218         {
219             case WM_CREATE: {
220                 memset(&data, 0, sizeof(data));
221                 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
222                 RECT rc;
223                 GetClientRect (hwnd, &rc);
224                 data.width = rc.right - rc.left;
225                 data.height = rc.bottom - rc.top;
226
227                 //EDITTEXT IDD_EDIT,68,8,72,12, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER  | WS_TABSTOP
228                 data.edit = CreateWindow (
229                         WC_EDIT,
230                         "EditPath",
231                         WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL,
232                         32, 
233                         48,
234                         (data.width-64)-32*2, 
235                         20,
236                         hwnd,  /* Parent */
237                         (HMENU)0x1234,
238                         cs->hInstance,
239                         NULL
240                         );
241                 
242                 data.explore = CreateWindow (
243                         WC_BUTTON,
244                         "Browse",
245                         WS_CHILD | WS_VISIBLE | WS_TABSTOP,
246                         data.width-32-64,
247                         48,
248                         64, 
249                         20,
250                         hwnd,  /* Parent */
251                         (HMENU)0x9999,
252                         cs->hInstance,
253                         NULL
254                         );
255                 
256                 data.installButton = CreateWindow (
257                         WC_BUTTON,
258                         "Install",
259                         WS_CHILD | WS_VISIBLE | WS_TABSTOP,
260                         (data.width - 80)/2,
261                         data.height - 32*2,
262                         80, 
263                         32,
264                         hwnd,  /* Parent */
265                         (HMENU)0xabcd,
266                         cs->hInstance,
267                         NULL
268                         );
269                 
270                 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
271                 return 0;
272             }   
273             case USER_SETMESSAGE: {
274                 //install_path = (char*)lParam;
275                 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
276                 printf("Setting path to %s\n", install_path);
277                 return 0;
278             }
279             case WM_PAINT: {
280                 TEXTMETRIC tm;
281                 HDC hdc;
282                 PAINTSTRUCT ps;
283                 RECT rc;
284                 hdc = GetDC(hwnd);
285                 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
286                 GetTextMetrics(hdc, &tm);
287                 ReleaseDC(hwnd, hdc);
288                 hdc = BeginPaint (hwnd, &ps);
289                 SetBkMode(hdc, TRANSPARENT);
290                 rc.top = 32; rc.left= 16; rc.right = data.width-32*2; rc.bottom = 20;
291                 DrawText(hdc, "Select Installation directory", -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
292                 EndPaint (hwnd, &ps);
293                 return 0;
294             }
295             case WM_COMMAND: {
296                 if((wParam&0xffff) == 0x9999) {
297                     BROWSEINFOA browse;
298                     memset(&browse, 0, sizeof(browse));
299                     browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
300                     browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
301                     memset(browse.pszDisplayName, 0, MAX_PATH);
302                     browse.lpszTitle = "Select installation directory";
303                     /*browse.pidlRoot = (ITEMIDLIST*)malloc(sizeof(ITEMIDLIST)*200);
304                     memset((void*)browse.pidlRoot, 0, sizeof(ITEMIDLIST)*200);*/
305                     printf("Start browsing %s\n", browse.pszDisplayName);
306                     //SHGetDesktopFolder
307                     //ParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
308                     //SHParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
309                     //SHBrowseForFolderA(&browse);
310                     browse.pidlRoot = SHBrowseForFolder(&browse);
311                     printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
312                     if(browse.pszDisplayName) {
313                         if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
314                             printf("Path is %s\n", browse.pszDisplayName);
315                             install_path = browse.pszDisplayName;
316                         }
317                     }
318                     SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
319                     return 0;
320                 } else if((wParam&0xffff) == 0xabcd) {
321                     data.ok = 1;
322                     DestroyWindow(hwnd);
323                     return 0;
324                 } else if((wParam&0xffff) == 0x1234) {
325                     SendMessage(data.edit, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
326                     if(pathBuf[0]) {
327                         install_path = pathBuf;
328                     }
329                     return 0;
330                 }
331                 return DefWindowProc(hwnd, message, wParam, lParam);
332             }
333             case WM_KEYDOWN: {
334                 if(wParam == 0x49) {
335                     DestroyWindow(hwnd);
336                 }
337                 return 0;
338             }
339             case WM_DESTROY:
340                 if(!data.ok) {
341                     do_abort = 1;
342                     PostQuitMessage (0);
343                 }
344                 wnd_params = 0;
345                 return DefWindowProc(hwnd, message, wParam, lParam);
346             default:
347                 return DefWindowProc(hwnd, message, wParam, lParam);
348         }
349     } else if(hwnd == wnd_finish) {
350         static struct finish_data data;
351         switch(message)
352         {
353             case WM_CREATE: {
354                 /* TODO:
355
356                    "swftools has been installed into directory %s
357                    successfully"
358
359                    [x] Create Desktop Shortcut
360                    [x] Create Start Menu Entry
361
362                 */
363
364                 RECT rc;
365                 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
366                 GetClientRect (hwnd, &rc);
367                 data.width = rc.right - rc.left;
368                 data.height = rc.bottom - rc.top;
369
370                 data.text = malloc(strlen(install_path)+256);
371                 sprintf(data.text, "SWFTools has been installed into directory\r\n%s\r\nsucessfully.", install_path);
372                 
373                 data.installButton = CreateWindow (
374                         WC_BUTTON,
375                         "Finish",
376                         WS_CHILD | WS_VISIBLE | WS_TABSTOP,
377                         (data.width - 80)/2,
378                         data.height - 32,
379                         80, 
380                         32,
381                         hwnd, 
382                         (HMENU)0xabce,
383                         cs->hInstance,
384                         NULL
385                         );
386         
387                 if(config_createLinks) {
388                     data.check1 = CreateWindow (
389                             WC_BUTTON,
390                             "Create Desktop Shortcut",
391                             WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_CHECKBOX,
392                             32, data.height - 96, 
393                             data.width-32*2, 32, 
394                             hwnd, (HMENU)0xabcf, cs->hInstance, NULL);
395                     
396                     data.check2 = CreateWindow (
397                             WC_BUTTON,
398                             "Create Start Menu Entry",
399                             WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_CHECKBOX,
400                             32, data.height - 64, 
401                             data.width-32*2, 32, 
402                             hwnd, (HMENU)0xabd0, cs->hInstance, NULL);
403                     
404                     SendDlgItemMessage(hwnd, 0xabcf, BM_SETCHECK, config_createStartmenu, 0);
405                     SendDlgItemMessage(hwnd, 0xabd0, BM_SETCHECK, config_createDesktop, 0);
406                 }
407             }
408             case WM_PAINT: {
409                 TEXTMETRIC tm;
410                 HDC hdc;
411                 PAINTSTRUCT ps;
412                 RECT rc;
413                 hdc = GetDC(hwnd);
414                 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
415                 GetTextMetrics(hdc, &tm);
416                 ReleaseDC(hwnd, hdc);
417                 hdc = BeginPaint (hwnd, &ps);
418                 SetBkMode(hdc, TRANSPARENT);
419                 rc.left = 0; 
420                 rc.top = 10;
421                 rc.right = data.width; 
422                 rc.bottom = data.height-40-32;
423                 DrawText(hdc, data.text, -1, &rc, DT_CENTER | DT_VCENTER);
424                 EndPaint (hwnd, &ps);
425                 return 0;
426
427                 return DefWindowProc(hwnd, message, wParam, lParam);
428             }
429             case WM_COMMAND: {
430                 if((wParam&0xffff) == 0xabce) {
431                     data.ok = 1;
432                     DestroyWindow(hwnd);
433                     return 0;
434                 }
435                 if((wParam&0xffff) == 0xabcf) {
436                     config_createDesktop = SendDlgItemMessage(hwnd, 0xabcf, BM_GETCHECK, 0, 0);
437                     config_createDesktop^=1;
438                     SendDlgItemMessage(hwnd, 0xabcf, BM_SETCHECK, config_createDesktop, 0);
439                     return 0;
440                 }
441                 if((wParam&0xffff) == 0xabd0) {
442                     config_createStartmenu = SendDlgItemMessage(hwnd, 0xabd0, BM_GETCHECK, 0, 0);
443                     config_createStartmenu^=1;
444                     SendDlgItemMessage(hwnd, 0xabd0, BM_SETCHECK, config_createStartmenu, 0);
445                     return 0;
446                 }
447             }
448             case WM_DESTROY: {
449                 free(data.text);data.text = 0;
450                 if(!data.ok) {
451                     do_abort = 1;
452                     PostQuitMessage(0);
453                 }
454                 wnd_finish = 0;
455                 return DefWindowProc(hwnd, message, wParam, lParam);
456             }
457         }
458     } else if(hwnd == wnd_background) {
459         switch(message)
460         {
461             case WM_PAINT: {
462                 HDC hdc;
463                 PAINTSTRUCT ps;
464                 RECT rc;
465                 hdc = BeginPaint(hwnd, &ps);
466                 SetBkMode(hdc, TRANSPARENT);
467
468                 HPEN pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0));    
469                 HPEN oldPen = (HPEN)SelectObject(hdc, pen);
470
471                 MoveToEx(hdc, 10, 10, 0);
472                 LineTo(hdc, 100, 100);
473
474                 SelectObject(hdc, oldPen); 
475                 DeleteObject(pen);
476
477                 EndPaint(hwnd, &ps);
478                 return 1;
479             }
480         }
481     }
482     return DefWindowProc(hwnd, message, wParam, lParam);
483 }
484
485 void processMessages()
486 {
487     MSG msg;
488     while(PeekMessage(&msg,NULL,0,0,0))
489     {
490         GetMessage(&msg, NULL, 0, 0);
491         TranslateMessage(&msg);
492         DispatchMessage(&msg);
493     }
494 }
495
496 static char*lastmessage = 0;
497 void myarchivestatus(int type, char*text)
498 {
499     if(text && text[0]=='[')
500         return;
501     //printf("%s\n", text);
502                         
503     SendMessage(wnd_progress, USER_SETMESSAGE, (WPARAM)strdup(text), 0);
504     SendMessage(wnd_progress, WM_PAINT, 0, 0);
505     int t;
506     for(t=0;t<9;t++) {
507         SendMessage(wnd_progress, PBM_STEPIT, 0, 0);
508         /* while we're here, we might also make ourselves useful */
509         processMessages();
510         /* we want the user to see what we're writing, right? */
511         Sleep(30);
512     }
513
514     if(type<0) {
515         while(1) {
516             int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
517             
518             /* there is no MB_CANCEL, so, *sigh*, we have to display
519                the "retry" button. So pretend it's doing anything... */
520             if(ret==IDRETRY)
521                 continue;
522             else
523                 break;
524         }
525     }
526 }
527
528 int addRegistryEntries(char*install_dir)
529 {
530     int ret;
531     ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
532     if(!ret) return 0;
533     return 1;
534 }
535
536 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
537 {
538     WCHAR wszFilename[MAX_PATH];
539     IShellLink *ps1 = NULL;
540     IPersistFile *pPf = NULL;
541     HRESULT hr;
542     hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
543     if(FAILED(hr)) return 0;
544     hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
545     if(FAILED(hr)) return 0;
546     hr = ps1->lpVtbl->SetPath(ps1, path);
547     if(FAILED(hr)) return 0;
548     hr = ps1->lpVtbl->SetDescription(ps1, description);
549     
550     if (arguments) {
551         hr = ps1->lpVtbl->SetArguments(ps1, arguments);
552         if(FAILED(hr)) return 0;
553     }
554     if (iconpath) {
555         hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
556         if (FAILED(hr)) return 0;
557     }
558     if (workdir) {
559         hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
560         if (FAILED(hr)) return 0;
561     }
562     MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
563     hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
564     if(FAILED(hr)) {
565         return 0;
566     }
567     pPf->lpVtbl->Release(pPf);
568     ps1->lpVtbl->Release(ps1);
569     return 1;
570 }
571
572
573 static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
574 static char path_startmenu[MAX_PATH];
575 static char path_desktop[MAX_PATH];
576
577 int WINAPI WinMain(HINSTANCE me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
578 {
579     WNDCLASSEX wcl;
580     wcl.hInstance    = me;
581     wcl.lpfnWndProc  = WindowFunc;
582     wcl.lpszClassName= "SWFTools-Install";
583     wcl.style        = CS_HREDRAW | CS_VREDRAW;
584     wcl.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
585     wcl.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
586     wcl.hCursor      = LoadCursor(NULL, IDC_ARROW);
587     wcl.lpszMenuName = NULL; //no menu
588     wcl.cbClsExtra   = 0;
589     wcl.cbWndExtra   = 0;
590     wcl.hbrBackground= (HBRUSH)GetStockObject(LTGRAY_BRUSH);
591     wcl.cbSize       = sizeof(WNDCLASSEX);
592
593     WNDCLASSEX wcl_text;
594     memcpy(&wcl_text, &wcl, sizeof(WNDCLASSEX));
595     wcl_text.lpszClassName= "TextClass";
596     wcl_text.hbrBackground = GetStockObject(HOLLOW_BRUSH);
597
598     WNDCLASSEX wcl_background;
599     memcpy(&wcl_background, &wcl, sizeof(WNDCLASSEX));
600     wcl_background.lpszClassName= "SWFTools Installer";
601     wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
602   
603     HINSTANCE shell32 = LoadLibrary("shell32.dll");
604     if(!shell32) {
605         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
606         return 1;
607     }
608     f_SHGetSpecialFolderPath = (HRESULT (WINAPI*)(HWND,LPTSTR,int,BOOL))GetProcAddress(shell32, "SHGetSpecialFolderPathA");
609     if(!f_SHGetSpecialFolderPath) {
610         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
611         return 1;
612     }
613         
614     HRESULT ret = CoInitialize(NULL);
615     if(FAILED(ret)) {
616         MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
617         return 1;
618     }
619
620     char mypath[MAX_PATH];
621     path_startmenu[0] = 0;
622     path_desktop[0] = 0;
623     if(config_forAllUsers) {
624         f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
625         f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_COMMON_PROGRAMS, 0);
626     } else {
627         f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
628         f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
629     }
630
631     if(!RegisterClassEx(&wcl)) {
632         MessageBox(0, "Could not register window class", "Install.exe", MB_OK);
633         return 1;
634     }
635     if(!RegisterClassEx(&wcl_background)) {
636         MessageBox(0, "Could not register window class 2", "Install.exe", MB_OK);
637         return 1;
638     }
639
640     HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
641                          0, 0, 0, 
642                          GetSystemMetrics(SM_CXFULLSCREEN),
643                          GetSystemMetrics(SM_CYFULLSCREEN),
644                          NULL, NULL, me, 
645                          (void*)"background");
646     
647     if(!background) {
648         MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
649         return 1;
650     }
651
652     ShowWindow(background, SW_SHOWMAXIMIZED);
653     UpdateWindow(background);
654
655     RECT r = {0,0,0,0};
656     GetWindowRect(background, &r);
657     int xx = 320, yy = 200;
658     if(r.right - r.left > 320)
659         xx = r.right - r.left;
660     if(r.right - r.left > 200)
661         yy = r.bottom - r.top;
662     
663     logo = LoadBitmap(me, "SWFTOOLS");
664     
665     install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
666     if(!install_path || !install_path[0])
667         install_path = "c:\\swftools\\";
668
669     CoInitialize(0);
670     InitCommonControls();
671     
672     HWND installpath_window = CreateWindow(
673             wcl.lpszClassName,          /* Class name */
674             "SWFTools Installer",       /* Caption */
675             WS_CHILD | WS_CAPTION,
676             (xx-320)/2,                 /* Initial x  */
677             (yy-200)/2,                 /* Initial y  */
678             320,                        /* Initial x size */
679             200,                        /* Initial y size */
680             background,                       /* No parent window */
681             NULL,                       /* No menu */
682             me,                         /* This program instance */
683             (void*)"params"             /* Creation parameters */
684             );
685
686     if(!installpath_window) {
687         MessageBox(background, "Could not create installation window", "Install.exe", MB_OK);
688         return 1;
689     }
690
691     ShowWindow (wnd_params, nWinMode);
692     UpdateWindow (wnd_params);
693    
694     MSG msg;
695     while(wnd_params)
696     {
697         GetMessage(&msg,NULL,0,0);
698         TranslateMessage(&msg);
699         DispatchMessage(&msg);
700     }
701
702     if(do_abort) {
703         MessageBox(background, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
704         return 1;
705     }
706    
707     /*char buf[1024];
708     sprintf(buf, "Do you want me to install SWFTools into the directory %s now?", install_path);
709     int ret = MessageBox(0, buf, "SWFTools Install", MB_YESNO|MB_ICONQUESTION);
710     if(ret == IDNO)
711         return 0;*/
712     
713     CreateWindow (
714             wcl.lpszClassName,          /* Class name */
715             "Installing...",            /* Caption */
716             WS_CHILD | WS_CAPTION,
717             //WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX),        /* Style */
718             (xx-260)/2, (yy-128)/2,
719             260,                        /* Initial x size */
720             128,                        /* Initial y size */
721             background,                 /* No parent window */
722             NULL,                       /* No menu */
723             me,                         /* This program instance */
724             (void*)"progress"           /* Creation parameters */
725             );
726     ShowWindow (wnd_progress, nWinMode);
727     UpdateWindow (wnd_progress);
728     
729     int success = unpack_archive(crndata, install_path, myarchivestatus);
730    
731     DestroyWindow(wnd_progress);
732
733     while(wnd_progress)
734         processMessages();
735     if(do_abort) {
736         MessageBox(background, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
737         return 1;
738     }
739     
740     char* pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
741     FILE*fi = fopen(pdf2swf_path, "rb");
742     if(fi) {
743         config_createLinks = 1;
744         fclose(fi);
745     }
746
747     int h = config_createLinks?200:140;
748     CreateWindow (
749             wcl.lpszClassName,          /* Class name */
750             "Finished",                 /* Caption */
751             WS_CHILD | WS_CAPTION,
752             //WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX),        /* Style */
753             (xx-320)/2, (yy-h)/2,
754             320,                        /* Initial x size */
755             h,                        /* Initial y size */
756             background,                 /* No parent window */
757             NULL,                       /* No menu */
758             me,                         /* This program instance */
759             (void*)"finish"             /* Creation parameters */
760             );
761     ShowWindow(wnd_finish, nWinMode);
762     UpdateWindow(wnd_finish);
763
764     while(wnd_finish)
765         processMessages();
766     if(do_abort) {
767         MessageBox(0, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
768         return 1;
769     }
770
771     if(!addRegistryEntries(install_path)) {
772         MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
773         return 1;
774     }
775
776     if(config_createLinks) {
777         if(config_createDesktop && path_desktop[0]) {
778             char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
779             if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
780                 MessageBox(0, "Couldn't create desktop shortcut", "Install.exe", MB_OK);
781                 return 1;
782             }
783         }
784         if(config_createStartmenu && path_startmenu[0]) {
785             char* group = concatPaths(path_startmenu, "pdf2swf");
786             CreateDirectory(group, 0);
787             char* linkName = concatPaths(group, "pdf2swf.lnk");
788             if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
789                 MessageBox(0, "Couldn't create start menu entry", "Install.exe", MB_OK);
790                 return 1;
791             }
792         }
793     }
794     exit(0);
795 }
796