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;
45 #define USER_SETMESSAGE 0x7f01
47 struct progress_data {
73 LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
75 //printf("%08x, %d %08x %08x\n", hwnd, message, wParam, lParam);
77 /* in order for the delegation below to also work for
78 WM_CREATE, we need to assign our window pointers *before* the
79 CreateWindow returns, because that's when the WM_CREATE event
81 if(message == WM_CREATE) {
82 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
83 if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "params")) {
86 if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "progress")) {
89 if(cs->lpCreateParams && !strcmp((char*)cs->lpCreateParams, "finish")) {
95 return DefWindowProc(hwnd, message, wParam, lParam);
96 } else if(hwnd == wnd_progress) {
97 static struct progress_data data;
101 case USER_SETMESSAGE:
102 data.text3 = (char*)wParam;
103 SendMessage(data.wnd_text3, WM_SETTEXT, 0, (LPARAM)data.text3);
106 memset(&data, 0, sizeof(data));
107 data.text1 = "Installing SWFTools";
108 data.text2 = (char*)malloc(strlen(install_path)+250);
109 sprintf(data.text2, "to %s", install_path);
113 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
115 GetClientRect (hwnd, &rc);
116 data.width = rc.right - rc.left;
117 data.height = rc.bottom - rc.top;
118 data.bar_width = cs->cx - 17;
120 data.bar_posx = (data.width -data.bar_width)/2;
124 data.hwndButton = CreateWindow (
127 WS_CHILD | WS_VISIBLE,
138 data.wnd_text3 = CreateWindow (
141 WS_CHILD | WS_VISIBLE,
144 (rc.right - rc.left - data.bar_posx*2),
151 SendMessage(data.wnd_text3, WM_SETTEXT, 0, (LPARAM)"");
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(SYSTEM_FIXED_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 EndPaint (hwnd, &ps);
208 return DefWindowProc(hwnd, message, wParam, lParam);
210 return DefWindowProc(hwnd, message, wParam, lParam);
212 } else if(hwnd == wnd_params) {
213 static struct params_data data;
217 memset(&data, 0, sizeof(data));
218 CREATESTRUCT*cs = ((LPCREATESTRUCT)lParam);
220 GetClientRect (hwnd, &rc);
221 data.width = rc.right - rc.left;
222 data.height = rc.bottom - rc.top;
224 //EDITTEXT IDD_EDIT,68,8,72,12, ES_LEFT | ES_AUTOHSCROLL | WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP
225 HWND text = CreateWindow(
227 "Select Installation Directory:",
228 WS_CHILD | WS_VISIBLE,
239 data.edit = CreateWindow (
242 WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_AUTOHSCROLL,
245 (data.width-64)-32*2,
253 data.explore = CreateWindow (
256 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
267 data.installButton = CreateWindow (
270 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
281 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
284 case USER_SETMESSAGE: {
285 //install_path = (char*)lParam;
286 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
287 printf("Setting path to %s\n", install_path);
291 return DefWindowProc(hwnd, message, wParam, lParam);
294 if((wParam&0xffff) == 0x9999) {
296 memset(&browse, 0, sizeof(browse));
297 browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
298 browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
299 memset(browse.pszDisplayName, 0, MAX_PATH);
300 browse.lpszTitle = "Select installation directory";
301 /*browse.pidlRoot = (ITEMIDLIST*)malloc(sizeof(ITEMIDLIST)*200);
302 memset((void*)browse.pidlRoot, 0, sizeof(ITEMIDLIST)*200);*/
303 printf("Start browsing %s\n", browse.pszDisplayName);
305 //ParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
306 //SHParseDisplayName(install_path,0,&browse.pidlRoot,0,0);
307 //SHBrowseForFolderA(&browse);
308 browse.pidlRoot = SHBrowseForFolder(&browse);
309 printf("Browsing returns %s / %08x\n", browse.pszDisplayName, browse.pidlRoot);
310 if(browse.pszDisplayName) {
311 if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
312 printf("Path is %s\n", browse.pszDisplayName);
313 install_path = browse.pszDisplayName;
316 SendMessage(data.edit, WM_SETTEXT, 0, (LPARAM)install_path);
318 } else if((wParam&0xffff) == 0xabcd) {
320 DestroyWindow(wnd_params);
322 } else if((wParam&0xffff) == 0x1234) {
323 SendMessage(data.edit, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
325 install_path = pathBuf;
326 printf("Path edited: now \"%s\"\n", install_path);
330 return DefWindowProc(hwnd, message, wParam, lParam);
334 DestroyWindow(wnd_params);
344 return DefWindowProc(hwnd, message, wParam, lParam);
346 return DefWindowProc(hwnd, message, wParam, lParam);
348 } else if(hwnd == wnd_finish) {
349 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.installButton = CreateWindow (
373 WS_CHILD | WS_VISIBLE | WS_TABSTOP,
386 return DefWindowProc(hwnd, message, wParam, lParam);
389 void processMessages()
392 while(PeekMessage(&msg,NULL,0,0,0))
394 GetMessage(&msg, NULL, 0, 0);
395 TranslateMessage(&msg);
396 DispatchMessage(&msg);
400 static char*lastmessage = 0;
401 void myarchivestatus(int type, char*text)
403 if(text && text[0]=='[')
405 //printf("%s\n", text);
407 SendMessage(wnd_progress, USER_SETMESSAGE, (WPARAM)strdup(text), 0);
408 SendMessage(wnd_progress, WM_PAINT, 0, 0);
411 SendMessage(wnd_progress, PBM_STEPIT, 0, 0);
412 /* while we're here, we might also make ourselves useful */
414 /* we want the user to see what we're writing, right? */
420 int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
422 /* there is no MB_CANCEL, so, *sigh*, we have to display
423 the "retry" button. So pretend it's doing anything... */
432 int addRegistryEntries(char*install_dir)
435 ret = setRegistryEntry("Software\\quiss.org\\swftools\\InstallPath", install_dir);
440 int WINAPI WinMain(HINSTANCE me,HINSTANCE hPrevInst,LPSTR lpszArgs, int nWinMode)
444 wcl.lpfnWndProc = WindowFunc;
445 wcl.lpszClassName= "SWFTools-Install";
446 wcl.style = CS_HREDRAW | CS_VREDRAW;
447 wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
448 wcl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
449 wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
450 wcl.lpszMenuName = NULL; //no menu
453 wcl.hbrBackground= (HBRUSH) GetStockObject(LTGRAY_BRUSH);
454 wcl.cbSize = sizeof(WNDCLASSEX);
456 WNDCLASSEX wcl_background;
457 memcpy(&wcl_background, &wcl, sizeof(WNDCLASSEX));
458 wcl_background.lpszClassName= "SWFTools Installer";
459 wcl_background.hbrBackground= CreateSolidBrush(RGB(0, 0, 128));
461 if(!RegisterClassEx(&wcl)) {
462 MessageBox(0, "Could not register window class", "Install.exe", MB_OK);
465 if(!RegisterClassEx(&wcl_background)) {
466 MessageBox(0, "Could not register window class 2", "Install.exe", MB_OK);
470 HWND background = CreateWindow(wcl_background.lpszClassName, "Setup SWFTools",
472 GetSystemMetrics(SM_CXFULLSCREEN),
473 GetSystemMetrics(SM_CYFULLSCREEN),
474 NULL, NULL, me, NULL);
477 MessageBox(0, "Could not create installation background window", "Install.exe", MB_OK);
481 ShowWindow(background, SW_SHOWMAXIMIZED);
482 UpdateWindow(background);
485 GetWindowRect(background, &r);
486 int xx = 320, yy = 200;
487 if(r.right - r.left > 320)
488 xx = r.right - r.left;
489 if(r.right - r.left > 200)
490 yy = r.bottom - r.top;
492 logo = LoadBitmap(me, "SWFTOOLS");
494 install_path = getRegistryEntry("Software\\quiss.org\\swftools\\InstallPath");
495 if(!install_path || !install_path[0])
496 install_path = "c:\\swftools\\";
499 InitCommonControls();
501 HWND installpath_window = CreateWindow(
502 wcl.lpszClassName, /* Class name */
503 "SWFTools Installer", /* Caption */
504 WS_CHILD | WS_CAPTION,
505 (xx-320)/2, /* Initial x */
506 (yy-200)/2, /* Initial y */
507 320, /* Initial x size */
508 200, /* Initial y size */
509 background, /* No parent window */
511 me, /* This program instance */
512 (void*)"params" /* Creation parameters */
515 if(!installpath_window) {
516 MessageBox(0, "Could not create installation window", "Install.exe", MB_OK);
520 ShowWindow (wnd_params, nWinMode);
521 UpdateWindow (wnd_params);
526 GetMessage(&msg,NULL,0,0);
527 TranslateMessage(&msg);
528 DispatchMessage(&msg);
535 sprintf(buf, "Do you want me to install SWFTools into the directory %s now?", install_path);
536 int ret = MessageBox(0, buf, "SWFTools Install", MB_YESNO|MB_ICONQUESTION);
541 wcl.lpszClassName, /* Class name */
542 "Installing...", /* Caption */
543 WS_CHILD | WS_CAPTION,
544 //WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX), /* Style */
545 (xx-260)/2, (yy-128)/2,
546 260, /* Initial x size */
547 128, /* Initial y size */
548 background, /* No parent window */
550 me, /* This program instance */
551 (void*)"progress" /* Creation parameters */
553 ShowWindow (wnd_progress, nWinMode);
554 UpdateWindow (wnd_progress);
556 int success = unpack_archive(crndata, install_path, myarchivestatus);
558 DestroyWindow(wnd_progress);
564 wcl.lpszClassName, /* Class name */
565 "Finished", /* Caption */
566 WS_CHILD | WS_CAPTION,
567 //WS_OVERLAPPEDWINDOW&(~WS_SIZEBOX), /* Style */
568 (xx-320)/2, (yy-160)/2,
569 320, /* Initial x size */
570 160, /* Initial y size */
571 background, /* No parent window */
573 me, /* This program instance */
574 (void*)"finish" /* Creation parameters */
576 ShowWindow(wnd_finish, nWinMode);
577 UpdateWindow(wnd_finish);
583 if(!addRegistryEntries(install_path)) {
585 MessageBox(0, "Couldn't create Registry Entries", "SWFTools Install", MB_OK|MB_ICONERROR);
590 sprintf(buf, "SWFTools Version %s has been installed into %s successfully", VERSION, install_path);
591 MessageBox(0, buf, "SWFTools Install", MB_OK|MB_ICONINFORMATION);
593 /* error will already have been notified by either myarchivestatus or some other
595 /*sprintf(buf, "Installation failed\nLast message: %s", lastmessage);
596 ret = MessageBox(0, buf, "SWFTools Install", MB_OK|MB_ICONERROR);*/