3 Part of the swftools installer (Main program).
5 Copyright (c) 2004 Matthias Kramm <kramm@quiss.org>
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.
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.
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 */
28 #include "installer.h"
32 #include "../config.h" //for swftools version
33 #include "../lib/os.h" //for registry functions
35 static int config_forAllUsers = 0;
36 static int config_createLinks = 0;
37 static int config_createStartmenu = 1;
38 static int config_createDesktop = 1;
40 static char path_startmenu[MAX_PATH] = "\0";
41 static char path_desktop[MAX_PATH] = "\0";
42 static char path_programfiles[MAX_PATH] = "\0";
45 extern char*license_text;
47 static char*install_path = "c:\\swftools\\";
48 static char pathBuf[1024];
49 static int do_abort = 0;
51 static char* pdf2swf_path;
53 static HBITMAP logo = 0;
57 #define USER_SETMESSAGE 0x7f01
59 void tball(HDC hdc, double px, double py, double sx, double sy)
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);
103 static HWND wnd_background = 0;
104 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
106 if(message == WM_CREATE) {
107 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
108 if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
109 wnd_background = hwnd;
112 if(hwnd == wnd_background && message == WM_PAINT) {
116 GetWindowRect(hwnd, &rc);
117 int width = rc.right - rc.left;
118 int height = rc.bottom - rc.top;
120 hdc = BeginPaint(hwnd, &ps);
121 SetBkMode(hdc, TRANSPARENT);
123 double s = width / 1282.0;
125 HPEN pen0 = CreatePen(PS_SOLID, 2, RGB(0, 32, 128));
126 HPEN oldPen = (HPEN)SelectObject(hdc, pen0);
127 tball(hdc, 0.7*width,0.2*height,s,s);
129 HPEN pen1 = CreatePen(PS_SOLID, 2, RGB(0, 48, 128));
130 (HPEN)SelectObject(hdc, pen1);
131 tball(hdc, 0.233*width,0.3*height,s*2,s*2);
133 HPEN pen2 = CreatePen(PS_SOLID, 2, RGB(0, 64, 128));
134 (HPEN)SelectObject(hdc, pen2);
135 tball(hdc, width, 1.2*height,s*3,-s*3);
137 SelectObject(hdc, oldPen);
143 return DefWindowProc(hwnd, message, wParam, lParam);
146 void processMessages()
149 while(PeekMessage(&msg,NULL,0,0,0))
151 GetMessage(&msg, NULL, 0, 0);
152 TranslateMessage(&msg);
153 DispatchMessage(&msg);
157 int addRegistryEntries(char*install_dir)
160 ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
165 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
167 WCHAR wszFilename[MAX_PATH];
168 IShellLink *ps1 = NULL;
169 IPersistFile *pPf = NULL;
171 hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
172 if(FAILED(hr)) return 0;
173 hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
174 if(FAILED(hr)) return 0;
175 hr = ps1->lpVtbl->SetPath(ps1, path);
176 if(FAILED(hr)) return 0;
177 hr = ps1->lpVtbl->SetDescription(ps1, description);
180 hr = ps1->lpVtbl->SetArguments(ps1, arguments);
181 if(FAILED(hr)) return 0;
184 hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
185 if (FAILED(hr)) return 0;
188 hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
189 if (FAILED(hr)) return 0;
191 MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
192 hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
196 pPf->lpVtbl->Release(pPf);
197 ps1->lpVtbl->Release(ps1);
201 BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
205 HWND dialog = GetParent(hwnd);
207 if(message == WM_INITDIALOG) {
209 SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)logo);
212 GetWindowRect(dialog, &rc);
213 int width = rc.right - rc.left;
214 int height = rc.bottom - rc.top;
215 MoveWindow(dialog, (GetSystemMetrics(SM_CXSCREEN) - width)/2, (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
219 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
220 PropSheet_SetWizButtons(dialog, buttons);
226 BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
227 if(message == WM_INITDIALOG) {
228 SetDlgItemText(hwnd, IDC_LICENSE, license_text);
230 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
232 BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
233 if(message == WM_INITDIALOG) {
234 SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
236 SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, 0, 0);
237 SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, 1, 0);
239 if(message == WM_COMMAND) {
240 if((wParam&0xffff) == IDC_BROWSE) {
242 memset(&browse, 0, sizeof(browse));
243 browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
244 browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
245 memset(browse.pszDisplayName, 0, MAX_PATH);
246 browse.lpszTitle = "Select installation directory";
247 printf("Start browsing %s\n", browse.pszDisplayName);
248 browse.pidlRoot = SHBrowseForFolder(&browse);
249 printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
250 if(browse.pszDisplayName) {
251 if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
252 printf("Path is %s\n", browse.pszDisplayName);
253 install_path = browse.pszDisplayName;
256 SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
260 else if((wParam&0xffff) == IDC_ALLUSERS) {
261 SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, 1, 0);
262 SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, 0, 0);
264 else if((wParam&0xffff) == IDC_CURRENTUSER) {
265 SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, 0, 0);
266 SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, 1, 0);
268 else if((wParam&0xffff) == IDC_INSTALL_PATH) {
269 SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
271 install_path = pathBuf;
276 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
279 static int progress_pos = 0;
280 void PropertyArchiveStatus(int type, char*text)
282 if(text && text[0]=='[') return;
283 SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
285 /* There are usually 6 messages, and a range of 54 to fill, so
288 SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, ++progress_pos, 0);
294 int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
295 if(ret==IDRETRY) continue;
300 BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
301 HWND dialog = GetParent(hwnd);
302 if(message == WM_INITDIALOG) {
303 SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
305 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZNEXT)) {
306 PropSheet_SetWizButtons(dialog, 0);
307 SendMessage(dialog, PSM_CANCELTOCLOSE, 0, 0); //makes wine display a warning
308 SetDlgItemText(hwnd, IDC_TITLE, "Installing files...");
310 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,54));
312 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, progress_pos, 0);
313 int success = unpack_archive(crndata, install_path, PropertyArchiveStatus);
316 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
319 static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
321 BOOL CALLBACK PropertySheetFunc4(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
322 if(message == WM_INITDIALOG) {
323 pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
324 FILE*fi = fopen(pdf2swf_path, "rb");
326 config_createLinks = 1;
329 if(!config_createLinks) {
330 SendDlgItemMessage(hwnd, IDC_STARTMENU, SW_HIDE, 0, 0);
331 SendDlgItemMessage(hwnd, IDC_DESKTOP, SW_HIDE, 0, 0);
334 SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
335 SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createStartmenu, 0);
337 if(message == WM_COMMAND) {
338 if((wParam&0xffff) == IDC_STARTMENU) {
339 config_createStartmenu = SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_GETCHECK, 0, 0);
340 config_createStartmenu^=1;
341 SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
344 if((wParam&0xffff) == IDC_DESKTOP) {
345 config_createDesktop = SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_GETCHECK, 0, 0);
346 config_createDesktop^=1;
347 SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createDesktop, 0);
352 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_WIZFINISH)) {
353 if(!addRegistryEntries(install_path)) {
354 MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
358 char mypath[MAX_PATH];
359 path_startmenu[0] = 0;
361 if(config_forAllUsers) {
362 f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_COMMON_DESKTOPDIRECTORY, 0);
363 f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_COMMON_PROGRAMS, 0);
365 /* get local program/desktop directory- this is both for forAllUsers=0 as well
366 as a fallback if the above didn't return any paths */
367 if(!path_startmenu[0]) {
368 f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
370 if(!path_desktop[0]) {
371 f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
374 if(config_createLinks) {
375 if(config_createDesktop && path_desktop[0]) {
376 char* linkName = concatPaths(path_desktop, "pdf2swf.lnk");
377 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
378 MessageBox(0, "Couldn't create desktop shortcut", "Install.exe", MB_OK);
382 if(config_createStartmenu && path_startmenu[0]) {
383 char* group = concatPaths(path_startmenu, "pdf2swf");
384 CreateDirectory(group, 0);
385 char* linkName = concatPaths(group, "pdf2swf.lnk");
386 if(!CreateShortcut(pdf2swf_path, "pdf2swf", linkName, 0, 0, 0, install_path)) {
387 MessageBox(0, "Couldn't create start menu entry", "Install.exe", MB_OK);
393 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
396 #ifndef PSP_HIDEHEADER
397 #define PSP_HIDEHEADER 2048
400 typedef struct _wizardpage {
405 void runPropertySheet(HWND parent)
407 PROPSHEETHEADER sheet;
409 wizardpage_t wpage[5] = {
410 {PropertySheetFunc1, IDD_LICENSE},
411 {PropertySheetFunc2, IDD_INSTALLDIR},
412 {PropertySheetFunc3, IDD_PROGRESS},
413 {PropertySheetFunc4, IDD_FINISH},
415 int num = sizeof(wpage)/sizeof(wpage[0]);
416 HPROPSHEETPAGE pages[num];
420 memset(&page, 0, sizeof(PROPSHEETPAGE));
421 page.dwSize = sizeof(PROPSHEETPAGE);
422 page.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
424 page.pfnDlgProc = wpage[t].function;
425 page.pszTemplate = MAKEINTRESOURCE(wpage[t].resource);
426 pages[t] = CreatePropertySheetPage(&page);
429 memset(&sheet, 0, sizeof(PROPSHEETHEADER));
430 sheet.dwSize = sizeof(PROPSHEETHEADER);
431 sheet.hInstance = me;
432 sheet.hwndParent = parent;
433 sheet.phpage = pages;
434 sheet.dwFlags = PSH_WIZARD;
436 PropertySheet(&sheet);
439 int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
442 WNDCLASSEX wcl_background;
443 wcl_background.hInstance = me;
444 wcl_background.lpfnWndProc = WindowFunc;
445 wcl_background.lpszClassName= "SWFTools Install";
446 wcl_background.style = CS_HREDRAW | CS_VREDRAW;
447 wcl_background.hIcon = LoadIcon(NULL, IDI_APPLICATION);
448 wcl_background.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
449 wcl_background.hCursor = LoadCursor(NULL, IDC_ARROW);
450 wcl_background.lpszMenuName = NULL; //no menu
451 wcl_background.cbClsExtra = 0;
452 wcl_background.cbWndExtra = 0;
453 wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
454 wcl_background.cbSize = sizeof(WNDCLASSEX);
456 HINSTANCE shell32 = LoadLibrary("shell32.dll");
458 MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
461 f_SHGetSpecialFolderPath = (HRESULT (WINAPI*)(HWND,LPTSTR,int,BOOL))GetProcAddress(shell32, "SHGetSpecialFolderPathA");
462 if(!f_SHGetSpecialFolderPath) {
463 MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
467 HRESULT coret = CoInitialize(NULL);
469 MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
473 path_programfiles[0] = 0;
474 f_SHGetSpecialFolderPath(NULL, path_programfiles, CSIDL_PROGRAM_FILES, 0);
476 if(!RegisterClassEx(&wcl_background)) {
477 MessageBox(0, "Could not register window background class", "Install.exe", MB_OK);
481 HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
483 GetSystemMetrics(SM_CXFULLSCREEN),
484 GetSystemMetrics(SM_CYFULLSCREEN),
486 (void*)"background");
489 MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
493 ShowWindow(background, SW_SHOWMAXIMIZED);
494 SetForegroundWindow(background);
495 UpdateWindow(background);
497 logo = LoadBitmap(me, "SWFTOOLS");
499 install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
501 if(!install_path || !install_path[0]) {
502 if(path_programfiles[0]) {
503 install_path = concatPaths(path_programfiles, "SWFTools");
505 install_path = "c:\\swftools\\";
510 InitCommonControls();
513 GetWindowRect(background, &r);
514 int xx = 320, yy = 200;
515 if(r.right - r.left > 320)
516 xx = r.right - r.left;
517 if(r.right - r.left > 200)
518 yy = r.bottom - r.top;
521 runPropertySheet(background);