switched to resource dialogs and property sheets
[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
43 extern char*crndata;
44 extern char*license_text;
45
46 static char*install_path = "c:\\swftools\\";
47 static char pathBuf[1024];
48 static int do_abort = 0;
49
50 static char* pdf2swf_path;
51
52 static HBITMAP logo;
53
54 static HINSTANCE me;
55
56 #define USER_SETMESSAGE 0x7f01
57
58 static HWND wnd_background = 0;
59 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
60 {
61     if(message == WM_CREATE) {
62         CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
63         if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
64             wnd_background = hwnd;
65         }
66     }
67     if(hwnd == wnd_background && message == WM_PAINT) {
68         HDC hdc;
69         PAINTSTRUCT ps;
70         RECT rc;
71         GetWindowRect(hwnd, &rc);
72         int width = rc.right - rc.left;
73         int height = rc.bottom - rc.top;
74
75         hdc = BeginPaint(hwnd, &ps);
76         SetBkMode(hdc, TRANSPARENT);
77
78         HPEN pen = CreatePen(PS_SOLID, 2, RGB(32, 32, 128));
79         HPEN oldPen = (HPEN)SelectObject(hdc, pen);
80
81         int t;
82         for(t=0;t<20;t++) {
83             MoveToEx(hdc, t*t, 0, 0);
84             LineTo(hdc, t*t, height);
85             MoveToEx(hdc, 0, t*t, 0);
86             LineTo(hdc, width, t*t);
87         }
88
89         SelectObject(hdc, oldPen); 
90         DeleteObject(pen);
91         EndPaint(hwnd, &ps);
92         return 1;
93     }
94     return DefWindowProc(hwnd, message, wParam, lParam);
95 }
96
97 void processMessages()
98 {
99     MSG msg;
100     while(PeekMessage(&msg,NULL,0,0,0))
101     {
102         GetMessage(&msg, NULL, 0, 0);
103         TranslateMessage(&msg);
104         DispatchMessage(&msg);
105     }
106 }
107
108 int addRegistryEntries(char*install_dir)
109 {
110     int ret;
111     ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
112     if(!ret) return 0;
113     return 1;
114 }
115
116 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
117 {
118     WCHAR wszFilename[MAX_PATH];
119     IShellLink *ps1 = NULL;
120     IPersistFile *pPf = NULL;
121     HRESULT hr;
122     hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
123     if(FAILED(hr)) return 0;
124     hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
125     if(FAILED(hr)) return 0;
126     hr = ps1->lpVtbl->SetPath(ps1, path);
127     if(FAILED(hr)) return 0;
128     hr = ps1->lpVtbl->SetDescription(ps1, description);
129     
130     if (arguments) {
131         hr = ps1->lpVtbl->SetArguments(ps1, arguments);
132         if(FAILED(hr)) return 0;
133     }
134     if (iconpath) {
135         hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
136         if (FAILED(hr)) return 0;
137     }
138     if (workdir) {
139         hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
140         if (FAILED(hr)) return 0;
141     }
142     MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
143     hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
144     if(FAILED(hr)) {
145         return 0;
146     }
147     pPf->lpVtbl->Release(pPf);
148     ps1->lpVtbl->Release(ps1);
149     return 1;
150 }
151
152 BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
153 {
154     LPNMHDR lpnm;
155         
156     HWND dialog = GetParent(hwnd);
157
158     if(message == WM_INITDIALOG) {
159         //    create_bitmap(hwnd);
160         //    if(hBitmap)
161         //          SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
162         RECT rc;
163         GetWindowRect(dialog, &rc);
164         int width = rc.right - rc.left;
165         int height = rc.bottom - rc.top;
166         MoveWindow(dialog, (GetSystemMetrics(SM_CXSCREEN) - width)/2, (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
167         return FALSE;
168      }
169
170     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
171         PropSheet_SetWizButtons(dialog, buttons);
172         return FALSE;
173     }
174     return FALSE;
175 }
176
177 BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
178     if(message == WM_INITDIALOG) {
179         SetDlgItemText(hwnd, IDC_LICENSE, license_text);
180     }
181     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
182 }
183 BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
184     if(message == WM_INITDIALOG) {
185         SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
186     }
187     if(message == WM_COMMAND) {
188         if((wParam&0xffff) == IDC_BROWSE) {
189             BROWSEINFOA browse;
190             memset(&browse, 0, sizeof(browse));
191             browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
192             browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
193             memset(browse.pszDisplayName, 0, MAX_PATH);
194             browse.lpszTitle = "Select installation directory";
195             printf("Start browsing %s\n", browse.pszDisplayName);
196             browse.pidlRoot = SHBrowseForFolder(&browse);
197             printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
198             if(browse.pszDisplayName) {
199                 if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
200                     printf("Path is %s\n", browse.pszDisplayName);
201                     install_path = browse.pszDisplayName;
202                 }
203             }
204             SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
205             return 0;
206
207         }
208         else if((wParam&0xffff) == IDC_INSTALL_PATH) {
209             SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
210             if(pathBuf[0]) {
211                 install_path = pathBuf;
212             }
213             return 0;
214         }
215     }
216     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
217 }
218 HWND statuswnd;
219 static int progress_pos = 0;
220 void PropertyArchiveStatus(int type, char*text)
221 {
222     if(text && text[0]=='[') return;
223     SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
224     int t;
225     /* There are usually 6 messages, and a range of 54 to fill, so 
226        step 9 times */
227     for(t=0;t<9;t++) {
228         SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, ++progress_pos, 0);
229         processMessages();
230         Sleep(30);
231     }
232     if(type<0) {
233         while(1) {
234             int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
235             if(ret==IDRETRY) continue;
236             else break;
237         }
238     }
239 }
240 BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
241     HWND dialog = GetParent(hwnd);
242     if(message == WM_INITDIALOG) {
243         SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
244     }
245     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
246         PropSheet_SetWizButtons(dialog, 0);
247         SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0); //makes wine display a warning
248         SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
249         statuswnd = hwnd;
250         SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,54));
251         progress_pos = 0;
252         SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, progress_pos, 0);
253         int success = unpack_archive(crndata, install_path, PropertyArchiveStatus);
254         return 0;
255     }
256     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
257 }
258 BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
259     if(message == WM_INITDIALOG) {
260         pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
261         FILE*fi = fopen(pdf2swf_path, "rb");
262         if(fi) {
263             config_createLinks = 1;
264             fclose(fi);
265         }
266         if(!config_createLinks) {
267             SendDlgItemMessage(hwnd, IDC_STARTMENU, SW_HIDE, 0, 0);
268             SendDlgItemMessage(hwnd, IDC_DESKTOP, SW_HIDE, 0, 0);
269         }
270
271         SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
272         SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createStartmenu, 0);
273     }
274     if(message == WM_COMMAND) {
275         if((wParam&0xffff) == IDC_STARTMENU) {
276             config_createStartmenu = SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_GETCHECK, 0, 0);
277             config_createStartmenu^=1;
278             SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
279             return 0;
280         }
281         if((wParam&0xffff) == IDC_DESKTOP) {
282             config_createDesktop = SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_GETCHECK, 0, 0);
283             config_createDesktop^=1;
284             SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
285             return 0;
286         }
287     }
288
289     if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZFINISH)) {
290         if(!addRegistryEntries(install_path)) {
291             MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
292             return 1;
293         }
294         if(config_createLinks) {
295             if(config_createDesktop && path_desktop[0]) {
296                 char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
297                 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
298                     MessageBox(0, "Couldn't create desktop shortcut", "Install.exe", MB_OK);
299                     return 1;
300                 }
301             }
302             if(config_createStartmenu && path_startmenu[0]) {
303                 char* group = concatPaths(path_startmenu, "pdf2swf");
304                 CreateDirectory(group, 0);
305                 char* linkName = concatPaths(group, "pdf2swf.lnk");
306                 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
307                     MessageBox(0, "Couldn't create start menu entry", "Install.exe", MB_OK);
308                     return 1;
309                 }
310             }
311         }
312     }
313     return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
314 }
315
316 #ifndef PSP_HIDEHEADER
317 #define PSP_HIDEHEADER  2048
318 #endif
319
320 typedef struct _wizardpage {
321     DLGPROC function;
322     int resource;
323 } wizardpage_t;
324
325 void runPropertySheet(HWND parent)
326 {
327     PROPSHEETHEADER sheet;
328
329     wizardpage_t wpage[5] = {
330         {PropertySheetFunc1, IDD_LICENSE},
331         {PropertySheetFunc2, IDD_INSTALLDIR},
332         {PropertySheetFunc3, IDD_PROGRESS},
333         {PropertySheetFunc4, IDD_FINISH},
334     };
335     int num = sizeof(wpage)/sizeof(wpage[0]);
336     HPROPSHEETPAGE pages[num];
337     int t;
338     for(t=0;t<num;t++) {
339         PROPSHEETPAGE page;
340         memset(&page, 0, sizeof(PROPSHEETPAGE));
341         page.dwSize = sizeof(PROPSHEETPAGE);
342         page.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
343         page.hInstance = me;
344         page.pfnDlgProc = wpage[t].function;
345         page.pszTemplate = MAKEINTRESOURCE(wpage[t].resource);
346         pages[t] = CreatePropertySheetPage(&page);
347     }
348
349     memset(&sheet, 0, sizeof(PROPSHEETHEADER));
350     sheet.dwSize = sizeof(PROPSHEETHEADER);
351     sheet.hInstance = me;
352     sheet.hwndParent = parent;
353     sheet.phpage = pages;
354     sheet.dwFlags = PSH_WIZARD;
355     sheet.nPages = num;
356     PropertySheet(&sheet);
357 }
358
359 static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
360
361 int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
362 {
363     me = _me;
364     WNDCLASSEX wcl_background;
365     wcl_background.hInstance    = me;
366     wcl_background.lpfnWndProc  = WindowFunc;
367     wcl_background.lpszClassName= "SWFTools Install";
368     wcl_background.style        = CS_HREDRAW | CS_VREDRAW;
369     wcl_background.hIcon        = LoadIcon(NULL, IDI_APPLICATION);
370     wcl_background.hIconSm      = LoadIcon(NULL, IDI_APPLICATION);
371     wcl_background.hCursor      = LoadCursor(NULL, IDC_ARROW);
372     wcl_background.lpszMenuName = NULL; //no menu
373     wcl_background.cbClsExtra   = 0;
374     wcl_background.cbWndExtra   = 0;
375     wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
376     wcl_background.cbSize       = sizeof(WNDCLASSEX);
377   
378     HINSTANCE shell32 = LoadLibrary("shell32.dll");
379     if(!shell32) {
380         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
381         return 1;
382     }
383     f_SHGetSpecialFolderPath = (HRESULT (WINAPI*)(HWND,LPTSTR,int,BOOL))GetProcAddress(shell32, "SHGetSpecialFolderPathA");
384     if(!f_SHGetSpecialFolderPath) {
385         MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
386         return 1;
387     }
388         
389     HRESULT coret = CoInitialize(NULL);
390     if(FAILED(coret)) {
391         MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
392         return 1;
393     }
394
395     char mypath[MAX_PATH];
396     path_startmenu[0] = 0;
397     path_desktop[0] = 0;
398     if(config_forAllUsers) {
399         f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
400         f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_COMMON_PROGRAMS, 0);
401     } else {
402         f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
403         f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
404     }
405
406     if(!RegisterClassEx(&wcl_background)) {
407         MessageBox(0, "Could not register window background class", "Install.exe", MB_OK);
408         return 1;
409     }
410
411     HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
412                          0, 0, 0, 
413                          GetSystemMetrics(SM_CXFULLSCREEN),
414                          GetSystemMetrics(SM_CYFULLSCREEN),
415                          NULL, NULL, me, 
416                          (void*)"background");
417     
418     if(!background) {
419         MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
420         return 1;
421     }
422
423     ShowWindow(background, SW_SHOWMAXIMIZED);
424     SetForegroundWindow(background);
425     UpdateWindow(background);
426
427     logo = LoadBitmap(me, "SWFTOOLS");
428     
429     install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
430     if(!install_path || !install_path[0])
431         install_path = "c:\\swftools\\";
432
433     CoInitialize(0);
434     InitCommonControls();
435
436     RECT r = {0,0,0,0};
437     GetWindowRect(background, &r);
438     int xx = 320, yy = 200;
439     if(r.right - r.left > 320)
440         xx = r.right - r.left;
441     if(r.right - r.left > 200)
442         yy = r.bottom - r.top;
443     
444     int ret = 0;
445     runPropertySheet(background);
446
447     return ret;
448 }
449