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
35 static char*install_path = "c:\\swftools\\";
36 static char pathBuf[1024];
37 static int do_abort = 0;
39 static HWND wnd_params = 0;
40 static HWND wnd_progress = 0;
41 static HWND wnd_finish = 0;
42 static HWND wnd_background = 0;
46 #define USER_SETMESSAGE 0x7f01
48 struct progress_data {
77 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
79 //printf("%08x, %d %08x %08x\n", hwnd, message, wParam, lParam);
81 /* in order for the delegation below to also work for
82 WM_CREATE, we need to assign our window pointers *before* the
83 CreateWindow returns, because that's when the WM_CREATE event
86 if(message == WM_CREATE) {
87 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
88 if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "params")) {
91 else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "progress")) {
94 else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "finish")) {
97 else if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "background")) {
98 wnd_background = hwnd;
103 return DefWindowProc(hwnd, message, wParam, lParam);
104 } else if(hwnd == wnd_progress) {
105 static struct progress_data data;
109 case USER_SETMESSAGE:
110 data.text3 = (char*)wParam;
111 SendMessage(data.wnd_text3, WM_SETTEXT, 0, (LPARAM)data.text3);
114 memset(&data, 0, sizeof(data));
115 data.text1 = "Installing SWFTools";
116 data.text2 = (char*)malloc(strlen(install_path)+250);
118 sprintf(data.text2, "to %s", install_path);
122 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
124 GetClientRect (hwnd, &rc);
125 data.width = rc.right - rc.left;
126 data.height = rc.bottom - rc.top;
127 data.bar_width = cs->cx - 17;
129 data.bar_posx = (data.width -data.bar_width)/2;
133 data.hwndButton = CreateWindow (
136 WS_CHILD | WS_VISIBLE,
147 SendMessage(data.hwndButton, PBM_SETRANGE, 0, (LPARAM) MAKELONG(0,data.range));
148 SendMessage(data.hwndButton, PBM_SETSTEP, (WPARAM) data.step, 0);
152 if(data.pos+data.step < data.range) {
153 data.pos += data.step;
154 SendMessage(data.hwndButton, PBM_STEPIT, wParam, lParam);
159 HDC hdc; /* A device context used for drawing */
160 PAINTSTRUCT ps; /* Also used during window drawing */
161 RECT rc; /* A rectangle used during drawing */
164 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
165 GetTextMetrics(hdc, &tm);
166 ReleaseDC(hwnd, hdc);
168 hdc = BeginPaint (hwnd, &ps);
172 HDC memDc=CreateCompatibleDC(hdc);
173 SelectObject(memDc,logo);
174 BitBlt(hdc,0,0,406,93,memDc,0,0,SRCCOPY);
179 SetBkMode(hdc, TRANSPARENT);
181 rc.top = 8; rc.left= 0; rc.right = data.width; rc.bottom = 24;
182 DrawText(hdc, data.text1, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
185 char*text = data.text2;
186 if(tm.tmAveCharWidth * strlen(text) > data.width) {
187 int chars = (data.width / tm.tmAveCharWidth)-8;
188 if(chars>240) chars=240;
189 strncpy(buf, text, chars);
190 strcpy(&buf[chars],"...");
194 rc.top = 32; rc.left= 0; rc.right = data.width; rc.bottom = 48;
195 DrawText(hdc, text, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
197 //rc.top = data.height-32; rc.left= 0; rc.right = data.width; rc.bottom = data.height;
198 //DrawText(hdc, data.text3, -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
200 EndPaint (hwnd, &ps);
205 return DefWindowProc(hwnd, message, wParam, lParam);
207 return DefWindowProc(hwnd, message, wParam, lParam);
209 } else if(hwnd == wnd_params) {
210 static struct params_data data;
214 memset(&data, 0, sizeof(data));
215 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
217 GetClientRect (hwnd, &rc);
218 data.width = rc.right - rc.left;
219 data.height = rc.bottom - rc.top;
221 //EDITTEXT IDD_EDIT,68,8,72,12, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP
222 data.edit = CreateWindow (
225 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL,
228 (data.width-64)-32*2,
236 data.explore = CreateWindow (
239 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
250 data.installButton = CreateWindow (
253 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
264 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
267 case USER_SETMESSAGE: {
268 //install_path = (char*)lParam;
269 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
270 printf("Setting path to %s\n", install_path);
279 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
280 GetTextMetrics(hdc, &tm);
281 ReleaseDC(hwnd, hdc);
282 hdc = BeginPaint (hwnd, &ps);
283 SetBkMode(hdc, TRANSPARENT);
284 rc.top = 32; rc.left= 16; rc.right = data.width-32*2; rc.bottom = 20;
285 DrawText(hdc, "Select Installation directory", -1, &rc, DT_SINGLELINE | DT_CENTER | DT_VCENTER);
286 EndPaint (hwnd, &ps);
290 if((wParam&0xffff) == 0x9999) {
292 memset(&browse, 0, sizeof(browse));
293 browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
294 browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
295 memset(browse.pszDisplayName, 0, MAX_PATH);
296 browse.lpszTitle = "Select installation directory";
297 /*browse.pidlRoot = (ITEMIDLIST*)malloc(sizeof(ITEMIDLIST)*200);
298 memset((void*)browse.pidlRoot, 0, sizeof(ITEMIDLIST)*200);*/
299 printf("Start browsing %s\n", browse.pszDisplayName);
301 //ParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
302 //SHParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
303 //SHBrowseForFolderA(&browse);
304 browse.pidlRoot = SHBrowseForFolder(&browse);
305 printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
306 if(browse.pszDisplayName) {
307 if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
308 printf("Path is %s\n", browse.pszDisplayName);
309 install_path = browse.pszDisplayName;
312 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
314 } else if((wParam&0xffff) == 0xabcd) {
318 } else if((wParam&0xffff) == 0x1234) {
319 SendMessage(data.edit, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
321 install_path = pathBuf;
325 return DefWindowProc(hwnd, message, wParam, lParam);
339 return DefWindowProc(hwnd, message, wParam, lParam);
341 return DefWindowProc(hwnd, message, wParam, lParam);
343 } else if(hwnd == wnd_finish) {
344 static struct finish_data data;
350 "swftools has been installed into directory %s
353 [x] Create Desktop Shortcut
354 [x] Create Start Menu Entry
359 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
360 GetClientRect (hwnd, &rc);
361 data.width = rc.right - rc.left;
362 data.height = rc.bottom - rc.top;
364 data.text = malloc(strlen(install_path)+256);
365 sprintf(data.text, "SWFTools has been installed into directory\r\n%s\r\nsucessfully.", install_path);
367 data.installButton = CreateWindow (
370 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
381 data.check1 = CreateWindow (
383 "Create Desktop Shortcut",
384 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_CHECKBOX,
385 32, data.height - 96,
387 hwnd, (HMENU)0xabcf, cs->hInstance, NULL);
389 data.check2 = CreateWindow (
391 "Create Start Menu Entry",
392 WS_CHILD | WS_VISIBLE | WS_TABSTOP | BS_CHECKBOX,
393 32, data.height - 64,
395 hwnd, (HMENU)0xabd0, cs->hInstance, NULL);
404 SelectObject(hdc, GetStockObject(DEFAULT_GUI_FONT));
405 GetTextMetrics(hdc, &tm);
406 ReleaseDC(hwnd, hdc);
407 hdc = BeginPaint (hwnd, &ps);
408 SetBkMode(hdc, TRANSPARENT);
411 rc.right = data.width;
412 rc.bottom = data.height-40-32;
413 DrawText(hdc, data.text, -1, &rc, DT_CENTER | DT_VCENTER);
414 EndPaint (hwnd, &ps);
417 return DefWindowProc(hwnd, message, wParam, lParam);
420 if((wParam&0xffff) == 0xabce) {
425 if((wParam&0xffff) == 0xabcf) {
428 if((wParam&0xffff) == 0xabd0) {
433 free(data.text);data.text = 0;
439 return DefWindowProc(hwnd, message, wParam, lParam);
442 } else if(hwnd == wnd_background) {
449 hdc = BeginPaint(hwnd, &ps);
450 SetBkMode(hdc, TRANSPARENT);
452 HPEN pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0));
453 HPEN oldPen = (HPEN)SelectObject(hdc, pen);
455 MoveToEx(hdc, 10, 10, 0);
456 LineTo(hdc, 100, 100);
458 SelectObject(hdc, oldPen);
466 return DefWindowProc(hwnd, message, wParam, lParam);
469 void processMessages()
472 while(PeekMessage(&msg,NULL,0,0,0))
474 GetMessage(&msg, NULL, 0, 0);
475 TranslateMessage(&msg);
476 DispatchMessage(&msg);
480 static char*lastmessage = 0;
481 void myarchivestatus(int type, char*text)
483 if(text && text[0]=='[')
485 //printf("%s\n", text);
487 SendMessage(wnd_progress, USER_SETMESSAGE, (WPARAM)strdup(text), 0);
488 SendMessage(wnd_progress, WM_PAINT, 0, 0);
491 SendMessage(wnd_progress, PBM_STEPIT, 0, 0);
492 /* while we're here, we might also make ourselves useful */
494 /* we want the user to see what we're writing, right? */
500 int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
502 /* there is no MB_CANCEL, so, *sigh*, we have to display
503 the "retry" button. So pretend it's doing anything... */
512 int addRegistryEntries(char*install_dir)
515 ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
520 int WINAPI WinMain(HINSTANCE me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
524 wcl.lpfnWndProc = WindowFunc;
525 wcl.lpszClassName= "SWFTools-Install";
526 wcl.style = CS_HREDRAW | CS_VREDRAW;
527 wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
528 wcl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
529 wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
530 wcl.lpszMenuName = NULL; //no menu
533 wcl.hbrBackground= (HBRUSH)GetStockObject(LTGRAY_BRUSH);
534 wcl.cbSize = sizeof(WNDCLASSEX);
537 memcpy(&wcl_text, &wcl, sizeof(WNDCLASSEX));
538 wcl_text.lpszClassName= "TextClass";
539 wcl_text.hbrBackground = GetStockObject(HOLLOW_BRUSH);
541 WNDCLASSEX wcl_background;
542 memcpy(&wcl_background, &wcl, sizeof(WNDCLASSEX));
543 wcl_background.lpszClassName= "SWFTools Installer";
544 wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
547 if(!RegisterClassEx(&wcl)) {
548 MessageBox(0, "Could not register window class", "Install.exe", MB_OK);
551 if(!RegisterClassEx(&wcl_background)) {
552 MessageBox(0, "Could not register window class 2", "Install.exe", MB_OK);
556 HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
558 GetSystemMetrics(SM_CXFULLSCREEN),
559 GetSystemMetrics(SM_CYFULLSCREEN),
561 (void*)"background");
564 MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
568 ShowWindow(background, SW_SHOWMAXIMIZED);
569 UpdateWindow(background);
572 GetWindowRect(background, &r);
573 int xx = 320, yy = 200;
574 if(r.right - r.left > 320)
575 xx = r.right - r.left;
576 if(r.right - r.left > 200)
577 yy = r.bottom - r.top;
579 logo = LoadBitmap(me, "SWFTOOLS");
581 install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
582 if(!install_path || !install_path[0])
583 install_path = "c:\\swftools\\";
586 InitCommonControls();
588 HWND installpath_window = CreateWindow(
589 wcl.lpszClassName, /* Class name */
590 "SWFTools Installer", /* Caption */
591 WS_CHILD | WS_CAPTION,
592 (xx-320)/2, /* Initial x */
593 (yy-200)/2, /* Initial y */
594 320, /* Initial x size */
595 200, /* Initial y size */
596 background, /* No parent window */
598 me, /* This program instance */
599 (void*)"params" /* Creation parameters */
602 if(!installpath_window) {
603 MessageBox(background, "Could not create installation window", "Install.exe", MB_OK);
607 ShowWindow (wnd_params, nWinMode);
608 UpdateWindow (wnd_params);
613 GetMessage(&msg,NULL,0,0);
614 TranslateMessage(&msg);
615 DispatchMessage(&msg);
619 MessageBox(background, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
624 sprintf(buf, "Do you want me to install SWFTools into the directory %s now?", install_path);
625 int ret = MessageBox(0, buf, "SWFTools Install", MB_YESNO|MB_ICONQUESTION);
630 wcl.lpszClassName, /* Class name */
631 "Installing...", /* Caption */
632 WS_CHILD | WS_CAPTION,
633 //WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX), /* Style */
634 (xx-260)/2, (yy-128)/2,
635 260, /* Initial x size */
636 128, /* Initial y size */
637 background, /* No parent window */
639 me, /* This program instance */
640 (void*)"progress" /* Creation parameters */
642 ShowWindow (wnd_progress, nWinMode);
643 UpdateWindow (wnd_progress);
645 int success = unpack_archive(crndata, install_path, myarchivestatus);
647 DestroyWindow(wnd_progress);
652 MessageBox(background, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
657 wcl.lpszClassName, /* Class name */
658 "Finished", /* Caption */
659 WS_CHILD | WS_CAPTION,
660 //WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX), /* Style */
661 (xx-320)/2, (yy-160)/2,
662 320, /* Initial x size */
663 200, /* Initial y size */
664 background, /* No parent window */
666 me, /* This program instance */
667 (void*)"finish" /* Creation parameters */
669 ShowWindow(wnd_finish, nWinMode);
670 UpdateWindow(wnd_finish);
675 MessageBox(0, "Aborting Installation", "Error", MB_OK|MB_ICONERROR);
679 if(!addRegistryEntries(install_path)) {
681 MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
686 //sprintf(buf, "SWFTools Version %s has been installed into %s successfully", VERSION, install_path);
687 //MessageBox(0, buf, "SWFTools Install", MB_OK|MB_ICONINFORMATION);
689 /* error will already have been notified by either myarchivestatus or some other
691 /*sprintf(buf, "Installation failed\nLast message: %s", lastmessage);
692 ret = MessageBox(0, buf, "SWFTools Install", MB_OK|MB_ICONERROR);*/