Removed Pro2 comments
[openwide.git] / psDlg.c
blob2e74dd6e986210944885e653ed87f4c4ba20ecbe
1 /*
2 * Openwide -- control Windows common dialog
3 *
4 * Copyright (c) 2000 Luke Hudson
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 #include <windows.h>
24 #include <shlwapi.h>
25 #include "openwidedll.h"
26 #include "openwideres.h"
27 #include "owutil.h"
28 #include "owSharedUtil.h"
30 #include "openwide_proto.h"
33 #define SETDLGRESULT(hw, x) SetWindowLong((hw), DWL_MSGRESULT, (LONG)(x))
34 static int gIdxLastSheet = 0;
37 static void setTabChanged(HWND hwnd, BOOL bChanged)
39 if( bChanged )
40 PropSheet_Changed(ghwPropSheet, hwnd);
41 else
42 PropSheet_UnChanged(ghwPropSheet, hwnd);
45 int savePrefsToRegistry(void)
47 HKEY hk = regCreateKey(HKEY_CURRENT_USER, OW_REGKEY_NAME);
48 if( hk )
50 regWriteDWORD(hk, NULL, sizeof(OWSharedData));
51 regWriteBinaryData(hk, "OWData", (byte *)gPowData, sizeof(OWSharedData));
52 regCloseKey(hk);
53 return 1;
55 return 0;
60 int initPrefs(HWND hwnd)
62 if(!initSharedMem(NULL))
63 return 0;
65 CheckDlgButton(hwnd, IDC_WSTARTUP, isStartupApp(hwnd));
66 CheckDlgButton(hwnd, IDC_LOG, BST_CHECKED);
68 if( !waitForMutex() )
69 return 0;
71 CheckDlgButton(hwnd, IDC_STARTMIN, gPowData->bStartMin);
72 CheckDlgButton(hwnd, IDC_SHOWICON, gPowData->bShowIcon);
74 releaseMutex();
76 /* if( bMinimize )
78 SendMessage(ghwMain, WM_SYSCOMMAND, SC_MINIMIZE, 0);
79 SetFocus(NULL);
80 EndTrayOperation();
82 else
84 SetFocus(GetDlgItem(hwnd, IDB_SETPLACEMENT));
85 ShowWindow(hwnd, SW_SHOWNORMAL);
86 }*/
87 return 1;
90 static void onPrefsTabCommand(HWND hwnd, WPARAM wp, LPARAM lp)
92 switch(LOWORD(wp))
94 case IDC_WSTARTUP:
95 case IDC_STARTMIN:
96 case IDC_SHOWICON:
97 setTabChanged(hwnd, HIWORD(wp)==BN_CLICKED);
98 break;
102 static BOOL CALLBACK wpPrefs(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
104 switch (uMsg)
106 case WM_INITDIALOG:
107 initPrefs(hwnd);
108 break;
109 case WM_COMMAND:
110 onPrefsTabCommand(hwnd, wp, lp);
111 break;
112 case WM_NOTIFY:
114 LPNMHDR pnh = (LPNMHDR)lp;
115 if( pnh->hwndFrom == ghwPropSheet )
117 switch(pnh->code)
119 case PSN_SETACTIVE:
120 SETDLGRESULT(hwnd, FALSE);
121 gIdxLastSheet = 1;
122 return FALSE;
123 case PSN_APPLY:
125 if(!waitForMutex())
127 //Warn("Couldn't get access to shared memory!");
128 SETDLGRESULT(hwnd, PSNRET_INVALID);
129 return FALSE;
132 gPowData->bStartMin = (IsDlgButtonChecked(hwnd, IDC_STARTMIN) == BST_CHECKED);
133 BOOL bShowIcon = gPowData->bShowIcon = (IsDlgButtonChecked(hwnd, IDC_SHOWICON) == BST_CHECKED);
134 BOOL bWinStart = IsDlgButtonChecked(hwnd, IDC_WSTARTUP) == BST_CHECKED;
136 savePrefsToRegistry();
138 releaseMutex();
140 setStartupApp(hwnd, bWinStart);
141 if( bShowIcon )
142 addTrayIcon(ghwMain);
143 else
144 remTrayIcon(ghwMain);
145 SETDLGRESULT(hwnd, PSNRET_NOERROR);
146 return TRUE;
148 break;
152 break;
154 case WM_DESTROY:
155 break;
156 case WM_CLOSE:
157 break;
160 return 0;
163 static int enumExcludes(HKEY hkRoot, const char *szKey, DWORD dwType, LPVOID pParm)
165 if( dwType == REG_DWORD )
166 SendMessage((HWND)pParm, LB_ADDSTRING, 0, (LPARAM)szKey);
167 return 1;
170 static int addExcludes2LB(HWND hwnd, UINT uID)
172 HWND hwLB = GetDlgItem(hwnd, uID);
173 if( !hwLB )
174 return 0;
175 HKEY hk = regOpenKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
176 if( hk )
178 regEnumValues(hk, enumExcludes, (LPVOID)hwLB);
179 regCloseKey(hk);
180 return 1;
182 return 0;
185 static int initExcludes(HWND hwnd)
187 return addExcludes2LB(hwnd, IDL_EXCLUDES);
190 static int addExclude(HWND hwnd)
192 const char *szEx = Prompt_File_Name(OPEN, hwnd, "Executable Files\0*.exe;*.com", "Choose program to exclude");
193 if( szEx )
195 // PathStripPath((char *)szEx);
196 int idx = SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_FINDSTRINGEXACT, -1, (LPARAM)szEx);
197 if( idx != LB_ERR )
198 Warn("This program is already excluded");
199 else
200 SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_ADDSTRING, 0, (LPARAM)szEx);
202 return 1;
205 static int rmvExclude(HWND hwnd)
207 int idx = SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_GETCURSEL, 0,0);
208 if( idx != LB_ERR )
209 SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_DELETESTRING, idx, 0);
210 return 1;
213 static int saveExcludes2Registry(HWND hwnd, UINT uID)
215 HWND hwLB = GetDlgItem(hwnd, uID);
216 if( !hwLB )
217 return 0;
218 // static char szKey[32];
219 regDeleteKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
220 HKEY hk = regCreateKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
221 if( hk )
223 int num = SendMessage(hwLB, LB_GETCOUNT,0,0);
224 if( num != LB_ERR )
226 for(int i=0; i < num; i++)
228 char *szEx = lbGetItemText(hwLB, i);
229 if( szEx )
231 regWriteDWORD(hk, szEx, 1);
232 free(szEx);
236 regCloseKey(hk);
237 return 1;
239 return 0;
242 static void onExTabCmd(HWND hwnd, WPARAM wp, LPARAM lp)
244 switch(LOWORD(wp))
246 case IDB_ADDEX:
247 addExclude(hwnd);
248 break;
249 case IDB_RMVEX:
250 rmvExclude(hwnd);
251 break;
255 static BOOL CALLBACK wpExcludes(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
257 switch (uMsg)
259 case WM_INITDIALOG:
260 initExcludes(hwnd);
261 break;
262 case WM_COMMAND:
263 onExTabCmd(hwnd, wp, lp);
264 break;
265 case WM_NOTIFY:
267 LPNMHDR pnh = (LPNMHDR)lp;
268 if( pnh->hwndFrom == ghwPropSheet )
270 switch(pnh->code)
272 case PSN_SETACTIVE:
273 SETDLGRESULT(hwnd, FALSE);
274 gIdxLastSheet = 1;
275 return FALSE;
276 case PSN_APPLY:
278 saveExcludes2Registry(hwnd, IDL_EXCLUDES);
279 SETDLGRESULT(hwnd, PSNRET_NOERROR);
280 return TRUE;
282 break;
286 break;
288 return 0;
293 static void onOSTabCommand(HWND hwnd, WPARAM wp, LPARAM lp)
295 switch(LOWORD(wp))
297 case IDCB_VIEW:
298 case IDCB_FOCUS:
299 if( HIWORD(wp) == CBN_SELCHANGE )
300 setTabChanged(hwnd, TRUE);
301 break;
302 case IDE_LEFT:
303 case IDE_TOP:
304 case IDE_WIDTH:
305 case IDE_HEIGHT:
306 if( HIWORD(wp) == EN_CHANGE )
307 setTabChanged(hwnd, TRUE);
308 break;
309 case IDB_QUIT:
310 DestroyWindow(hwnd);
311 break;
312 case IDB_SETPLACEMENT:
314 RECT r;
315 r.left = GetDlgItemInt(hwnd, IDE_LEFT, NULL, TRUE);
316 r.top = GetDlgItemInt(hwnd, IDE_TOP, NULL, TRUE);
317 r.right = max( GetDlgItemInt(hwnd, IDE_WIDTH, NULL, FALSE), 100 );
318 r.bottom = max( GetDlgItemInt(hwnd, IDE_HEIGHT, NULL, FALSE), 100 );
320 if( DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_PLACEMENT), hwnd, wpPlacement, (LPARAM)&r) )
322 SetDlgItemInt(hwnd, IDE_LEFT, r.left, TRUE);
323 SetDlgItemInt(hwnd, IDE_TOP, r.top, TRUE);
324 SetDlgItemInt(hwnd, IDE_WIDTH, r.right - r.left, TRUE);
325 SetDlgItemInt(hwnd, IDE_HEIGHT, r.bottom - r.top, TRUE);
326 setTabChanged(hwnd, TRUE);
329 break;
330 case IDB_TEST:
331 Prompt_File_Name(0, hwnd, NULL, NULL);
332 break;
336 void fillFocusCB(HWND hwnd, UINT uID)
338 HWND hwCB = GetDlgItem(hwnd, uID);
339 if(!hwCB)
340 return;
341 SendMessage(hwCB, CB_RESETCONTENT, 0,0);
342 cbAddString(hwCB, "Directory listing", F_DIRLIST);
343 cbAddString(hwCB, "File name", F_FNAME);
344 cbAddString(hwCB, "File type", F_FTYPE);
345 cbAddString(hwCB, "Places bar", F_PLACES);
346 cbAddString(hwCB, "Look in", F_LOOKIN);
350 void fillViewCB(HWND hwnd, UINT uID)
352 HWND hwCB = GetDlgItem(hwnd, uID);
353 if(!hwCB)
354 return;
355 SendMessage(hwCB, CB_RESETCONTENT, 0,0);
356 cbAddString(hwCB, "Large Icons", V_LGICONS);
357 if( isWinXP() )
358 cbAddString(hwCB, "Tiles", V_TILES);
359 else
360 cbAddString(hwCB, "Small Icons", V_SMICONS);
361 cbAddString(hwCB, "List", V_LIST);
362 cbAddString(hwCB, "Details", V_DETAILS);
363 cbAddString(hwCB, "Thumbnails", V_THUMBS);
366 void selectCBView(HWND hwnd, UINT uID, int iView)
368 HWND hwCB = GetDlgItem(hwnd, uID);
369 if(!hwCB)
370 return;
371 int nItems = SendMessage(hwCB, CB_GETCOUNT, 0,0);
372 for(int i=0; i < nItems; i++)
374 int ivItem = (int)SendMessage(hwCB, CB_GETITEMDATA, i, 0);
375 if( ivItem == iView)
377 SendMessage(hwCB, CB_SETCURSEL, i, 0);
378 break;
383 static int initOSTab(HWND hwnd)
385 CheckDlgButton(hwnd, IDC_WSTARTUP, isStartupApp(hwnd));
386 CheckDlgButton(hwnd, IDC_LOG, BST_CHECKED);
387 fillViewCB(hwnd, IDCB_VIEW);
388 fillFocusCB(hwnd, IDCB_FOCUS);
390 if(!waitForMutex())
391 return 0;
392 // bMinimize = gPowData->bStartMin;
394 SetDlgItemInt(hwnd, IDE_LEFT, gPowData->ptOrg.x, TRUE);
395 SetDlgItemInt(hwnd, IDE_TOP, gPowData->ptOrg.y, TRUE);
396 SetDlgItemInt(hwnd, IDE_WIDTH, gPowData->szDim.cx, FALSE);
397 SetDlgItemInt(hwnd, IDE_HEIGHT, gPowData->szDim.cy, FALSE);
399 selectCBView(hwnd, IDCB_VIEW, gPowData->iView);
400 SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_SETCURSEL, gPowData->iFocus, 0);
402 /// released shared data
403 releaseMutex();
404 return 1;
407 static BOOL CALLBACK wpOSDlgs(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
409 switch (uMsg)
411 case WM_INITDIALOG:
412 initOSTab(hwnd);
413 return FALSE;
414 case WM_TRAYICON:
415 switch(lp)
417 case WM_LBUTTONUP:
418 ShowWindow(hwnd, SW_SHOWNORMAL);
419 EnableWindow(hwnd, TRUE);
420 remTrayIcon(hwnd);
421 SetFocus(hwnd);
422 break;
424 break;
425 /* case WM_SYSCOMMAND:
426 if( wp == SC_MINIMIZE )
428 EnableWindow(hwnd, FALSE);
429 ShowWindow(hwnd, SW_HIDE);
430 addTrayIcon(hwnd);
431 EndTrayOperation();
433 else
434 return DefWindowProc(hwnd, uMsg, wp, lp);
435 break;*/
436 case WM_COMMAND:
437 onOSTabCommand(hwnd, wp, lp);
438 break;
439 case WM_NOTIFY:
441 LPNMHDR pnh = (LPNMHDR)lp;
442 if( pnh->hwndFrom == ghwPropSheet )
444 switch(pnh->code)
446 case PSN_SETACTIVE:
447 SETDLGRESULT(hwnd, FALSE);
448 gIdxLastSheet = 1;
449 return FALSE;
450 case PSN_APPLY:
452 if(!waitForMutex())
454 //Warn("Couldn't get access to shared memory!");
455 SETDLGRESULT(hwnd, PSNRET_INVALID);
456 return FALSE;
458 BOOL bOK;
459 int i = GetDlgItemInt(hwnd, IDE_LEFT, &bOK, TRUE);
460 if( bOK )
461 gPowData->ptOrg.x = i;
462 i = GetDlgItemInt(hwnd, IDE_TOP, &bOK, TRUE);
463 if( bOK )
464 gPowData->ptOrg.y = i;
465 i = GetDlgItemInt(hwnd, IDE_WIDTH, &bOK, TRUE);
466 if( bOK )
467 gPowData->szDim.cx = i;
468 i = GetDlgItemInt(hwnd, IDE_HEIGHT, &bOK, TRUE);
469 if( bOK )
470 gPowData->szDim.cy = i;
471 int idx = SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_GETCURSEL, 0, 0);
472 if( idx != CB_ERR )
474 int f = SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_GETITEMDATA, idx, 0);
475 if( f >= 0 && f < F_MAX )
476 gPowData->iFocus = f;
478 idx = SendDlgItemMessage(hwnd, IDCB_VIEW, CB_GETCURSEL, 0, 0);
479 if( idx != CB_ERR )
481 int v = SendDlgItemMessage(hwnd, IDCB_VIEW, CB_GETITEMDATA, idx, 0);
482 if( v >= 0 && v < V_MAX )
483 gPowData->iView = v;
486 savePrefsToRegistry();
488 releaseMutex();
489 SETDLGRESULT(hwnd, PSNRET_NOERROR);
490 return TRUE;
492 break;
496 break;
498 return 0;
502 static BOOL CALLBACK wpApps(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
504 switch (uMsg)
506 case WM_INITDIALOG:
507 break;
508 case WM_NOTIFY:
510 LPNMHDR pnh = (LPNMHDR)lp;
511 if( pnh->hwndFrom == ghwPropSheet )
513 switch(pnh->code)
515 case PSN_SETACTIVE:
516 SETDLGRESULT(hwnd, FALSE);
517 gIdxLastSheet = 1;
518 return FALSE;
519 case PSN_APPLY:
521 SETDLGRESULT(hwnd, PSNRET_NOERROR);
522 return TRUE;
524 break;
528 break;
530 return 0;
537 static void fillSheet(PROPSHEETPAGE * psp, int idDlg, DLGPROC pfnDlgProc)
539 memset(psp, 0, sizeof(PROPSHEETPAGE));
540 psp->dwSize = sizeof(PROPSHEETPAGE);
541 psp->dwFlags = PSP_DEFAULT; // | PSP_USEICONID;
542 psp->hInstance = ghInstance;
543 psp->pszTemplate = MAKEINTRESOURCE(idDlg);
544 //psp->pszIcon = (LPSTR) MAKEINTRESOURCE(IDI_DOC);
545 psp->pfnDlgProc = pfnDlgProc;
546 psp->lParam = 0;
552 int CALLBACK WINAPI initPropSheets(HWND hwnd, UINT msg, LPARAM lp)
554 //dbg("initPropSheets: %p, %d", hwnd, msg);
555 if (msg != PSCB_INITIALIZED)
556 return 0;
557 ghwPropSheet = hwnd;
558 PropSheet_SetCurSel(ghwPropSheet, gIdxLastSheet, gIdxLastSheet);
559 return 1;
563 HWND showDlg(HWND hwParent)
565 PROPSHEETHEADER psh;
566 PROPSHEETPAGE pages[4];
568 int k=0;
569 fillSheet(&pages[k++], IDD_OSDIALOGS, wpOSDlgs);
570 fillSheet(&pages[k++], IDD_EXCLUDES, wpExcludes);
571 // fillSheet(&pages[k++], IDD_APPS, wpApps);
572 fillSheet(&pages[k++], IDD_PREFS, wpPrefs);
574 memset(&psh, 0, sizeof(PROPSHEETHEADER));
575 psh.dwSize = sizeof(PROPSHEETHEADER);
576 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEHICON | PSH_USECALLBACK | PSH_MODELESS; // |
577 // PSH_NOAPPLYNOW
578 // //
579 // |
580 // PSH_USECALLBACK;
581 psh.hwndParent = hwParent;
582 psh.hInstance = ghInstance;
583 psh.hIcon = ghIconSm;
584 psh.pszCaption = (LPSTR) "OpenWide Settings";
585 psh.nPages = k; //sizeof(pages) / sizeof(PROPSHEETPAGE);
586 psh.ppsp = (LPCPROPSHEETPAGE) pages;
587 psh.pfnCallback = initPropSheets;
588 psh.nStartPage = 0;
590 return (HWND)PropertySheet(&psh);