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";
44 extern char*license_text;
46 static char*install_path = "c:\\swftools\\";
47 static char pathBuf[1024];
48 static int do_abort = 0;
50 static char* pdf2swf_path;
56 #define USER_SETMESSAGE 0x7f01
58 static HWND wnd_background = 0;
59 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
61 if(message == WM_CREATE) {
62 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
63 if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
64 wnd_background = hwnd;
67 if(hwnd == wnd_background && message == WM_PAINT) {
71 GetWindowRect(hwnd, &rc);
72 int width = rc.right - rc.left;
73 int height = rc.bottom - rc.top;
75 hdc = BeginPaint(hwnd, &ps);
76 SetBkMode(hdc, TRANSPARENT);
78 HPEN pen = CreatePen(PS_SOLID, 2, RGB(32, 32, 128));
79 HPEN oldPen = (HPEN)SelectObject(hdc, pen);
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);
89 SelectObject(hdc, oldPen);
94 return DefWindowProc(hwnd, message, wParam, lParam);
97 void processMessages()
100 while(PeekMessage(&msg,NULL,0,0,0))
102 GetMessage(&msg, NULL, 0, 0);
103 TranslateMessage(&msg);
104 DispatchMessage(&msg);
108 int addRegistryEntries(char*install_dir)
111 ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
116 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
118 WCHAR wszFilename[MAX_PATH];
119 IShellLink *ps1 = NULL;
120 IPersistFile *pPf = NULL;
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);
131 hr = ps1->lpVtbl->SetArguments(ps1, arguments);
132 if(FAILED(hr)) return 0;
135 hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
136 if (FAILED(hr)) return 0;
139 hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
140 if (FAILED(hr)) return 0;
142 MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
143 hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
147 pPf->lpVtbl->Release(pPf);
148 ps1->lpVtbl->Release(ps1);
152 BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
156 HWND dialog = GetParent(hwnd);
158 if(message == WM_INITDIALOG) {
159 // create_bitmap(hwnd);
161 // SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap);
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);
170 if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
171 PropSheet_SetWizButtons(dialog, buttons);
177 BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
178 if(message == WM_INITDIALOG) {
179 SetDlgItemText(hwnd, IDC_LICENSE, license_text);
181 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
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);
187 if(message == WM_COMMAND) {
188 if((wParam&0xffff) == IDC_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;
204 SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
208 else if((wParam&0xffff) == IDC_INSTALL_PATH) {
209 SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
211 install_path = pathBuf;
216 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
219 static int progress_pos = 0;
220 void PropertyArchiveStatus(int type, char*text)
222 if(text && text[0]=='[') return;
223 SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
225 /* There are usually 6 messages, and a range of 54 to fill, so
228 SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, ++progress_pos, 0);
234 int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
235 if(ret==IDRETRY) continue;
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");
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...");
250 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,54));
252 SendDlgItemMessage(hwnd, IDC_PROGRESS, PBM_SETPOS, progress_pos, 0);
253 int success = unpack_archive(crndata, install_path, PropertyArchiveStatus);
256 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
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");
263 config_createLinks = 1;
266 if(!config_createLinks) {
267 SendDlgItemMessage(hwnd, IDC_STARTMENU, SW_HIDE, 0, 0);
268 SendDlgItemMessage(hwnd, IDC_DESKTOP, SW_HIDE, 0, 0);
271 SendDlgItemMessage(hwnd, IDC_STARTMENU, BM_SETCHECK, config_createStartmenu, 0);
272 SendDlgItemMessage(hwnd, IDC_DESKTOP, BM_SETCHECK, config_createStartmenu, 0);
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);
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);
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);
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);
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);
313 return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_FINISH);
316 #ifndef PSP_HIDEHEADER
317 #define PSP_HIDEHEADER 2048
320 typedef struct _wizardpage {
325 void runPropertySheet(HWND parent)
327 PROPSHEETHEADER sheet;
329 wizardpage_t wpage[5] = {
330 {PropertySheetFunc1, IDD_LICENSE},
331 {PropertySheetFunc2, IDD_INSTALLDIR},
332 {PropertySheetFunc3, IDD_PROGRESS},
333 {PropertySheetFunc4, IDD_FINISH},
335 int num = sizeof(wpage)/sizeof(wpage[0]);
336 HPROPSHEETPAGE pages[num];
340 memset(&page, 0, sizeof(PROPSHEETPAGE));
341 page.dwSize = sizeof(PROPSHEETPAGE);
342 page.dwFlags = PSP_DEFAULT|PSP_HIDEHEADER;
344 page.pfnDlgProc = wpage[t].function;
345 page.pszTemplate = MAKEINTRESOURCE(wpage[t].resource);
346 pages[t] = CreatePropertySheetPage(&page);
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;
356 PropertySheet(&sheet);
359 static HRESULT (WINAPI *f_SHGetSpecialFolderPath)(HWND hwnd, LPTSTR lpszPath, int nFolder, BOOL fCreate);
361 int WINAPI WinMain(HINSTANCE _me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
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);
378 HINSTANCE shell32 = LoadLibrary("shell32.dll");
380 MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
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);
389 HRESULT coret = CoInitialize(NULL);
391 MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
395 char mypath[MAX_PATH];
396 path_startmenu[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);
402 f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
403 f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
406 if(!RegisterClassEx(&wcl_background)) {
407 MessageBox(0, "Could not register window background class", "Install.exe", MB_OK);
411 HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
413 GetSystemMetrics(SM_CXFULLSCREEN),
414 GetSystemMetrics(SM_CYFULLSCREEN),
416 (void*)"background");
419 MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
423 ShowWindow(background, SW_SHOWMAXIMIZED);
424 SetForegroundWindow(background);
425 UpdateWindow(background);
427 logo = LoadBitmap(me, "SWFTOOLS");
429 install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
430 if(!install_path || !install_path[0])
431 install_path = "c:\\swftools\\";
434 InitCommonControls();
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;
445 runPropertySheet(background);