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 */
30 #include "../config.h" //for swftools version
31 #include "../lib/os.h" //for registry functions
33 static int config_forAllUsers = 0;
34 static int config_createLinks = 0;
35 static int config_createStartmenu = 1;
36 static int config_createDesktop = 1;
41 static char*install_path = "c:\\swftools\\";
42 static char pathBuf[1024];
43 static int do_abort = 0;
45 static HWND wnd_params = 0;
46 static HWND wnd_progress = 0;
47 static HWND wnd_finish = 0;
48 static HWND wnd_background = 0;
52 #define USER_SETMESSAGE 0x7f01
54 struct progress_data {
83 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
85 //printf("%08x, %d %08x %08x\n", hwnd, message, wParam, lParam);
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
92 if(message == WM_CREATE) {
93 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
94 if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "params")) {
97 else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "progress")) {
100 else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "finish")) {
103 else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
104 wnd_background = hwnd;
109 return DefWindowProc(hwnd, message, wParam, lParam);
110 } else if(hwnd == wnd_progress) {
111 static struct progress_data data;
115 case USER_SETMESSAGE:
116 data.text3 = (char*)wParam;
117 SendMessage(data.wnd_text3, WM_SETTEXT, 0, (LPARAM)data.text3);
120 memset(&data, 0, sizeof(data));
121 data.text1 = "Installing SWFTools";
122 data.text2 = (char*)malloc(strlen(install_path)+250);
124 sprintf(data.text2, "to %s", install_path);
128 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
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;
135 data.bar_posx = (data.width -data.bar_width)/2;
139 data.hwndButton = CreateWindow (
142 WS_CHILD | WS_VISIBLE,
153 SendMessage(data.hwndButton, PBM_SETRANGE, 0, (LPARAM) MAKELONG(0,data.range));
154 SendMessage(data.hwndButton, PBM_SETSTEP, (WPARAM) data.step, 0);
158 if(data.pos+data.step < data.range) {
159 data.pos += data.step;
160 SendMessage(data.hwndButton, PBM_STEPIT, wParam, lParam);
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 */
170 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
171 GetTextMetrics(hdc, &tm);
172 ReleaseDC(hwnd, hdc);
174 hdc = BeginPaint (hwnd, &ps);
178 HDC memDc=CreateCompatibleDC(hdc);
179 SelectObject(memDc,logo);
180 BitBlt(hdc,0,0,406,93,memDc,0,0,SRCCOPY);
185 SetBkMode(hdc, TRANSPARENT);
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);
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],"...");
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);
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);
206 EndPaint (hwnd, &ps);
211 return DefWindowProc(hwnd, message, wParam, lParam);
213 return DefWindowProc(hwnd, message, wParam, lParam);
215 } else if(hwnd == wnd_params) {
216 static struct params_data data;
220 memset(&data, 0, sizeof(data));
221 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
223 GetClientRect (hwnd, &rc);
224 data.width = rc.right - rc.left;
225 data.height = rc.bottom - rc.top;
227 //EDITTEXT IDD_EDIT,68,8,72,12, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP
228 data.edit = CreateWindow (
231 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL,
234 (data.width-64)-32*2,
242 data.explore = CreateWindow (
245 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
256 data.installButton = CreateWindow (
259 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
270 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
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);
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);
296 if((wParam&0xffff) == 0x9999) {
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);
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;
318 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
320 } else if((wParam&0xffff) == 0xabcd) {
324 } else if((wParam&0xffff) == 0x1234) {
325 SendMessage(data.edit, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
327 install_path = pathBuf;
331 return DefWindowProc(hwnd, message, wParam, lParam);
345 return DefWindowProc(hwnd, message, wParam, lParam);
347 return DefWindowProc(hwnd, message, wParam, lParam);
349 } else if(hwnd == wnd_finish) {
350 static struct finish_data data;
356 "swftools has been installed into directory %s
359 [x] Create Desktop Shortcut
360 [x] Create Start Menu Entry
365 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
366 GetClientRect (hwnd, &rc);
367 data.width = rc.right - rc.left;
368 data.height = rc.bottom - rc.top;
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);
373 data.installButton = CreateWindow (
376 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
387 if(config_createLinks) {
388 data.check1 = CreateWindow (
390 "Create Desktop Shortcut",
391 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_CHECKBOX,
392 32, data.height - 96,
394 hwnd, (HMENU)0xabcf, cs->hInstance, NULL);
396 data.check2 = CreateWindow (
398 "Create Start Menu Entry",
399 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_CHECKBOX,
400 32, data.height - 64,
402 hwnd, (HMENU)0xabd0, cs->hInstance, NULL);
404 SendDlgItemMessage(hwnd, 0xabcf, BM_SETCHECK, config_createStartmenu, 0);
405 SendDlgItemMessage(hwnd, 0xabd0, BM_SETCHECK, config_createDesktop, 0);
414 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
415 GetTextMetrics(hdc, &tm);
416 ReleaseDC(hwnd, hdc);
417 hdc = BeginPaint (hwnd, &ps);
418 SetBkMode(hdc, TRANSPARENT);
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);
427 return DefWindowProc(hwnd, message, wParam, lParam);
430 if((wParam&0xffff) == 0xabce) {
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);
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);
449 free(data.text);data.text = 0;
455 return DefWindowProc(hwnd, message, wParam, lParam);
458 } else if(hwnd == wnd_background) {
465 hdc = BeginPaint(hwnd, &ps);
466 SetBkMode(hdc, TRANSPARENT);
468 HPEN pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0));
469 HPEN oldPen = (HPEN)SelectObject(hdc, pen);
471 MoveToEx(hdc, 10, 10, 0);
472 LineTo(hdc, 100, 100);
474 SelectObject(hdc, oldPen);
482 return DefWindowProc(hwnd, message, wParam, lParam);
485 void processMessages()
488 while(PeekMessage(&msg,NULL,0,0,0))
490 GetMessage(&msg, NULL, 0, 0);
491 TranslateMessage(&msg);
492 DispatchMessage(&msg);
496 static char*lastmessage = 0;
497 void myarchivestatus(int type, char*text)
499 if(text && text[0]=='[')
501 //printf("%s\n", text);
503 SendMessage(wnd_progress, USER_SETMESSAGE, (WPARAM)strdup(text), 0);
504 SendMessage(wnd_progress, WM_PAINT, 0, 0);
507 SendMessage(wnd_progress, PBM_STEPIT, 0, 0);
508 /* while we're here, we might also make ourselves useful */
510 /* we want the user to see what we're writing, right? */
516 int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
518 /* there is no MB_CANCEL, so, *sigh*, we have to display
519 the "retry" button. So pretend it's doing anything... */
528 int addRegistryEntries(char*install_dir)
531 ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
536 int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
538 WCHAR wszFilename[MAX_PATH];
539 IShellLink *ps1 = NULL;
540 IPersistFile *pPf = NULL;
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);
551 hr = ps1->lpVtbl->SetArguments(ps1, arguments);
552 if(FAILED(hr)) return 0;
555 hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
556 if (FAILED(hr)) return 0;
559 hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
560 if (FAILED(hr)) return 0;
562 MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
563 hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
567 pPf->lpVtbl->Release(pPf);
568 ps1->lpVtbl->Release(ps1);
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];
577 int WINAPI WinMain(HINSTANCE me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
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
590 wcl.hbrBackground= (HBRUSH)GetStockObject(LTGRAY_BRUSH);
591 wcl.cbSize = sizeof(WNDCLASSEX);
594 memcpy(&wcl_text, &wcl, sizeof(WNDCLASSEX));
595 wcl_text.lpszClassName= "TextClass";
596 wcl_text.hbrBackground = GetStockObject(HOLLOW_BRUSH);
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));
603 HINSTANCE shell32 = LoadLibrary("shell32.dll");
605 MessageBox(0, "Could not load shell32.dll", "Install.exe", MB_OK);
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);
614 HRESULT ret = CoInitialize(NULL);
616 MessageBox(0, "Could not initialize COM interface", "Install.exe", MB_OK);
620 char mypath[MAX_PATH];
621 path_startmenu[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);
627 f_SHGetSpecialFolderPath(NULL, path_desktop, CSIDL_DESKTOPDIRECTORY, 0);
628 f_SHGetSpecialFolderPath(NULL, path_startmenu, CSIDL_PROGRAMS, 0);
631 if(!RegisterClassEx(&wcl)) {
632 MessageBox(0, "Could not register window class", "Install.exe", MB_OK);
635 if(!RegisterClassEx(&wcl_background)) {
636 MessageBox(0, "Could not register window class 2", "Install.exe", MB_OK);
640 HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
642 GetSystemMetrics(SM_CXFULLSCREEN),
643 GetSystemMetrics(SM_CYFULLSCREEN),
645 (void*)"background");
648 MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
652 ShowWindow(background, SW_SHOWMAXIMIZED);
653 UpdateWindow(background);
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;
663 logo = LoadBitmap(me, "SWFTOOLS");
665 install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
666 if(!install_path || !install_path[0])
667 install_path = "c:\\swftools\\";
670 InitCommonControls();
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 */
682 me, /* This program instance */
683 (void*)"params" /* Creation parameters */
686 if(!installpath_window) {
687 MessageBox(background, "Could not create installation window", "Install.exe", MB_OK);
691 ShowWindow (wnd_params, nWinMode);
692 UpdateWindow (wnd_params);
697 GetMessage(&msg,NULL,0,0);
698 TranslateMessage(&msg);
699 DispatchMessage(&msg);
703 MessageBox(background, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
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);
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 */
723 me, /* This program instance */
724 (void*)"progress" /* Creation parameters */
726 ShowWindow (wnd_progress, nWinMode);
727 UpdateWindow (wnd_progress);
729 int success = unpack_archive(crndata, install_path, myarchivestatus);
731 DestroyWindow(wnd_progress);
736 MessageBox(background, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
740 char* pdf2swf_path = concatPaths(install_path, "pdf2swf_gui.exe");
741 FILE*fi = fopen(pdf2swf_path, "rb");
743 config_createLinks = 1;
747 int h = config_createLinks?200:140;
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 */
758 me, /* This program instance */
759 (void*)"finish" /* Creation parameters */
761 ShowWindow(wnd_finish, nWinMode);
762 UpdateWindow(wnd_finish);
767 MessageBox(0, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
771 if(!addRegistryEntries(install_path)) {
772 MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
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);
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);