Removed +x perm
[openwide.git] / psDlg.c
blob72482a5ddc631c5e75e1f3b08a47b3fa39f0e36e
1 #include <windows.h>
2 /**
3 /**
4 * @author Luke Hudson
5 * @licence GPL2
6 */
8 * @author Luke Hudson
9 * @licence GPL2
12 #include <shlwapi.h>
13 #include "openwidedll.h"
14 #include "openwideres.h"
15 #include "owutil.h"
16 #include "owSharedUtil.h"
18 #include "openwide_proto.h"
21 #define SETDLGRESULT(hw, x) SetWindowLong((hw), DWL_MSGRESULT, (LONG)(x))
22 static int gIdxLastSheet = 0;
25 static void setTabChanged(HWND hwnd, BOOL bChanged)
27 if( bChanged )
28 PropSheet_Changed(ghwPropSheet, hwnd);
29 else
30 PropSheet_UnChanged(ghwPropSheet, hwnd);
33 int savePrefsToRegistry(void)
35 HKEY hk = regCreateKey(HKEY_CURRENT_USER, OW_REGKEY_NAME);
36 if( hk )
38 regWriteDWORD(hk, NULL, sizeof(OWSharedData));
39 regWriteBinaryData(hk, "OWData", (byte *)gPowData, sizeof(OWSharedData));
40 regCloseKey(hk);
41 return 1;
43 return 0;
48 int initPrefs(HWND hwnd)
50 if(!initSharedMem(NULL))
51 return 0;
53 CheckDlgButton(hwnd, IDC_WSTARTUP, isStartupApp(hwnd));
54 CheckDlgButton(hwnd, IDC_LOG, BST_CHECKED);
56 if( !waitForMutex() )
57 return 0;
59 CheckDlgButton(hwnd, IDC_STARTMIN, gPowData->bStartMin);
60 CheckDlgButton(hwnd, IDC_SHOWICON, gPowData->bShowIcon);
62 releaseMutex();
64 /* if( bMinimize )
66 SendMessage(ghwMain, WM_SYSCOMMAND, SC_MINIMIZE, 0);
67 SetFocus(NULL);
68 EndTrayOperation();
70 else
72 SetFocus(GetDlgItem(hwnd, IDB_SETPLACEMENT));
73 ShowWindow(hwnd, SW_SHOWNORMAL);
74 }*/
75 return 1;
78 static void onPrefsTabCommand(HWND hwnd, WPARAM wp, LPARAM lp)
80 switch(LOWORD(wp))
82 case IDC_WSTARTUP:
83 case IDC_STARTMIN:
84 case IDC_SHOWICON:
85 setTabChanged(hwnd, HIWORD(wp)==BN_CLICKED);
86 break;
90 static BOOL CALLBACK wpPrefs(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
92 switch (uMsg)
94 case WM_INITDIALOG:
95 initPrefs(hwnd);
96 break;
97 case WM_COMMAND:
98 onPrefsTabCommand(hwnd, wp, lp);
99 break;
100 case WM_NOTIFY:
102 LPNMHDR pnh = (LPNMHDR)lp;
103 if( pnh->hwndFrom == ghwPropSheet )
105 switch(pnh->code)
107 case PSN_SETACTIVE:
108 SETDLGRESULT(hwnd, FALSE);
109 gIdxLastSheet = 1;
110 return FALSE;
111 case PSN_APPLY:
113 if(!waitForMutex())
115 //Warn("Couldn't get access to shared memory!");
116 SETDLGRESULT(hwnd, PSNRET_INVALID);
117 return FALSE;
120 gPowData->bStartMin = (IsDlgButtonChecked(hwnd, IDC_STARTMIN) == BST_CHECKED);
121 BOOL bShowIcon = gPowData->bShowIcon = (IsDlgButtonChecked(hwnd, IDC_SHOWICON) == BST_CHECKED);
122 BOOL bWinStart = IsDlgButtonChecked(hwnd, IDC_WSTARTUP) == BST_CHECKED;
124 savePrefsToRegistry();
126 releaseMutex();
128 setStartupApp(hwnd, bWinStart);
129 if( bShowIcon )
130 addTrayIcon(ghwMain);
131 else
132 remTrayIcon(ghwMain);
133 SETDLGRESULT(hwnd, PSNRET_NOERROR);
134 return TRUE;
136 break;
140 break;
142 case WM_DESTROY:
143 break;
144 case WM_CLOSE:
145 break;
148 return 0;
151 static int enumExcludes(HKEY hkRoot, const char *szKey, DWORD dwType, LPVOID pParm)
153 if( dwType == REG_DWORD )
154 SendMessage((HWND)pParm, LB_ADDSTRING, 0, (LPARAM)szKey);
155 return 1;
158 static int addExcludes2LB(HWND hwnd, UINT uID)
160 HWND hwLB = GetDlgItem(hwnd, uID);
161 if( !hwLB )
162 return 0;
163 HKEY hk = regOpenKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
164 if( hk )
166 regEnumValues(hk, enumExcludes, (LPVOID)hwLB);
167 regCloseKey(hk);
168 return 1;
170 return 0;
173 static int initExcludes(HWND hwnd)
175 return addExcludes2LB(hwnd, IDL_EXCLUDES);
178 static int addExclude(HWND hwnd)
180 const char *szEx = Prompt_File_Name(OPEN, hwnd, "Executable Files\0*.exe;*.com", "Choose program to exclude");
181 if( szEx )
183 // PathStripPath((char *)szEx);
184 int idx = SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_FINDSTRINGEXACT, -1, (LPARAM)szEx);
185 if( idx != LB_ERR )
186 Warn("This program is already excluded");
187 else
188 SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_ADDSTRING, 0, (LPARAM)szEx);
190 return 1;
193 static int rmvExclude(HWND hwnd)
195 int idx = SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_GETCURSEL, 0,0);
196 if( idx != LB_ERR )
197 SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_DELETESTRING, idx, 0);
198 return 1;
201 static int saveExcludes2Registry(HWND hwnd, UINT uID)
203 HWND hwLB = GetDlgItem(hwnd, uID);
204 if( !hwLB )
205 return 0;
206 static char szKey[32];
207 regDeleteKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
208 HKEY hk = regCreateKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
209 if( hk )
211 int num = SendMessage(hwLB, LB_GETCOUNT,0,0);
212 if( num != LB_ERR )
214 for(int i=0; i < num; i++)
216 char *szEx = lbGetItemText(hwLB, i);
217 if( szEx )
219 regWriteDWORD(hk, szEx, 1);
220 free(szEx);
224 regCloseKey(hk);
225 return 1;
227 return 0;
230 static void onExTabCmd(HWND hwnd, WPARAM wp, LPARAM lp)
232 switch(LOWORD(wp))
234 case IDB_ADDEX:
235 addExclude(hwnd);
236 break;
237 case IDB_RMVEX:
238 rmvExclude(hwnd);
239 break;
243 static BOOL CALLBACK wpExcludes(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
245 switch (uMsg)
247 case WM_INITDIALOG:
248 initExcludes(hwnd);
249 break;
250 case WM_COMMAND:
251 onExTabCmd(hwnd, wp, lp);
252 break;
253 case WM_NOTIFY:
255 LPNMHDR pnh = (LPNMHDR)lp;
256 if( pnh->hwndFrom == ghwPropSheet )
258 switch(pnh->code)
260 case PSN_SETACTIVE:
261 SETDLGRESULT(hwnd, FALSE);
262 gIdxLastSheet = 1;
263 return FALSE;
264 case PSN_APPLY:
266 saveExcludes2Registry(hwnd, IDL_EXCLUDES);
267 SETDLGRESULT(hwnd, PSNRET_NOERROR);
268 return TRUE;
270 break;
274 break;
276 return 0;
281 static void onOSTabCommand(HWND hwnd, WPARAM wp, LPARAM lp)
283 switch(LOWORD(wp))
285 case IDCB_VIEW:
286 case IDCB_FOCUS:
287 if( HIWORD(wp) == CBN_SELCHANGE )
288 setTabChanged(hwnd, TRUE);
289 break;
290 case IDE_LEFT:
291 case IDE_TOP:
292 case IDE_WIDTH:
293 case IDE_HEIGHT:
294 if( HIWORD(wp) == EN_CHANGE )
295 setTabChanged(hwnd, TRUE);
296 break;
297 case IDB_QUIT:
298 DestroyWindow(hwnd);
299 break;
300 case IDB_SETPLACEMENT:
302 RECT r;
303 r.left = GetDlgItemInt(hwnd, IDE_LEFT, NULL, TRUE);
304 r.top = GetDlgItemInt(hwnd, IDE_TOP, NULL, TRUE);
305 r.right = max( GetDlgItemInt(hwnd, IDE_WIDTH, NULL, FALSE), 100 );
306 r.bottom = max( GetDlgItemInt(hwnd, IDE_HEIGHT, NULL, FALSE), 100 );
308 if( DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_PLACEMENT), hwnd, wpPlacement, (LPARAM)&r) )
310 SetDlgItemInt(hwnd, IDE_LEFT, r.left, TRUE);
311 SetDlgItemInt(hwnd, IDE_TOP, r.top, TRUE);
312 SetDlgItemInt(hwnd, IDE_WIDTH, r.right - r.left, TRUE);
313 SetDlgItemInt(hwnd, IDE_HEIGHT, r.bottom - r.top, TRUE);
314 setTabChanged(hwnd, TRUE);
317 break;
318 case IDB_TEST:
319 Prompt_File_Name(0, hwnd, NULL, NULL);
320 break;
324 void fillFocusCB(HWND hwnd, UINT uID)
326 HWND hwCB = GetDlgItem(hwnd, uID);
327 if(!hwCB)
328 return;
329 SendMessage(hwCB, CB_RESETCONTENT, 0,0);
330 cbAddString(hwCB, "Directory listing", F_DIRLIST);
331 cbAddString(hwCB, "File name", F_FNAME);
332 cbAddString(hwCB, "File type", F_FTYPE);
333 cbAddString(hwCB, "Places bar", F_PLACES);
334 cbAddString(hwCB, "Look in", F_LOOKIN);
338 void fillViewCB(HWND hwnd, UINT uID)
340 HWND hwCB = GetDlgItem(hwnd, uID);
341 if(!hwCB)
342 return;
343 SendMessage(hwCB, CB_RESETCONTENT, 0,0);
344 cbAddString(hwCB, "Large Icons", V_LGICONS);
345 if( isWinXP() )
346 cbAddString(hwCB, "Tiles", V_TILES);
347 else
348 cbAddString(hwCB, "Small Icons", V_SMICONS);
349 cbAddString(hwCB, "List", V_LIST);
350 cbAddString(hwCB, "Details", V_DETAILS);
351 cbAddString(hwCB, "Thumbnails", V_THUMBS);
354 void selectCBView(HWND hwnd, UINT uID, int iView)
356 HWND hwCB = GetDlgItem(hwnd, uID);
357 if(!hwCB)
358 return;
359 int nItems = SendMessage(hwCB, CB_GETCOUNT, 0,0);
360 for(int i=0; i < nItems; i++)
362 int ivItem = (int)SendMessage(hwCB, CB_GETITEMDATA, i, 0);
363 if( ivItem == iView)
365 SendMessage(hwCB, CB_SETCURSEL, i, 0);
366 break;
371 static int initOSTab(HWND hwnd)
373 CheckDlgButton(hwnd, IDC_WSTARTUP, isStartupApp(hwnd));
374 CheckDlgButton(hwnd, IDC_LOG, BST_CHECKED);
375 fillViewCB(hwnd, IDCB_VIEW);
376 fillFocusCB(hwnd, IDCB_FOCUS);
378 if(!waitForMutex())
379 return 0;
380 // bMinimize = gPowData->bStartMin;
382 SetDlgItemInt(hwnd, IDE_LEFT, gPowData->ptOrg.x, TRUE);
383 SetDlgItemInt(hwnd, IDE_TOP, gPowData->ptOrg.y, TRUE);
384 SetDlgItemInt(hwnd, IDE_WIDTH, gPowData->szDim.cx, FALSE);
385 SetDlgItemInt(hwnd, IDE_HEIGHT, gPowData->szDim.cy, FALSE);
387 selectCBView(hwnd, IDCB_VIEW, gPowData->iView);
388 SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_SETCURSEL, gPowData->iFocus, 0);
390 /// released shared data
391 releaseMutex();
392 return 1;
395 static BOOL CALLBACK wpOSDlgs(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
397 switch (uMsg)
399 case WM_INITDIALOG:
400 initOSTab(hwnd);
401 return FALSE;
402 case WM_TRAYICON:
403 switch(lp)
405 case WM_LBUTTONUP:
406 ShowWindow(hwnd, SW_SHOWNORMAL);
407 EnableWindow(hwnd, TRUE);
408 remTrayIcon(hwnd);
409 SetFocus(hwnd);
410 break;
412 break;
413 /* case WM_SYSCOMMAND:
414 if( wp == SC_MINIMIZE )
416 EnableWindow(hwnd, FALSE);
417 ShowWindow(hwnd, SW_HIDE);
418 addTrayIcon(hwnd);
419 EndTrayOperation();
421 else
422 return DefWindowProc(hwnd, uMsg, wp, lp);
423 break;*/
424 case WM_COMMAND:
425 onOSTabCommand(hwnd, wp, lp);
426 break;
427 case WM_NOTIFY:
429 LPNMHDR pnh = (LPNMHDR)lp;
430 if( pnh->hwndFrom == ghwPropSheet )
432 switch(pnh->code)
434 case PSN_SETACTIVE:
435 SETDLGRESULT(hwnd, FALSE);
436 gIdxLastSheet = 1;
437 return FALSE;
438 case PSN_APPLY:
440 if(!waitForMutex())
442 //Warn("Couldn't get access to shared memory!");
443 SETDLGRESULT(hwnd, PSNRET_INVALID);
444 return FALSE;
446 BOOL bOK;
447 int i = GetDlgItemInt(hwnd, IDE_LEFT, &bOK, TRUE);
448 if( bOK )
449 gPowData->ptOrg.x = i;
450 i = GetDlgItemInt(hwnd, IDE_TOP, &bOK, TRUE);
451 if( bOK )
452 gPowData->ptOrg.y = i;
453 i = GetDlgItemInt(hwnd, IDE_WIDTH, &bOK, TRUE);
454 if( bOK )
455 gPowData->szDim.cx = i;
456 i = GetDlgItemInt(hwnd, IDE_HEIGHT, &bOK, TRUE);
457 if( bOK )
458 gPowData->szDim.cy = i;
459 int idx = SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_GETCURSEL, 0, 0);
460 if( idx != CB_ERR )
462 int f = SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_GETITEMDATA, idx, 0);
463 if( f >= 0 && f < F_MAX )
464 gPowData->iFocus = f;
466 idx = SendDlgItemMessage(hwnd, IDCB_VIEW, CB_GETCURSEL, 0, 0);
467 if( idx != CB_ERR )
469 int v = SendDlgItemMessage(hwnd, IDCB_VIEW, CB_GETITEMDATA, idx, 0);
470 if( v >= 0 && v < V_MAX )
471 gPowData->iView = v;
474 savePrefsToRegistry();
476 releaseMutex();
477 SETDLGRESULT(hwnd, PSNRET_NOERROR);
478 return TRUE;
480 break;
484 break;
486 return 0;
490 static BOOL CALLBACK wpApps(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
492 switch (uMsg)
494 case WM_INITDIALOG:
495 break;
496 case WM_NOTIFY:
498 LPNMHDR pnh = (LPNMHDR)lp;
499 if( pnh->hwndFrom == ghwPropSheet )
501 switch(pnh->code)
503 case PSN_SETACTIVE:
504 SETDLGRESULT(hwnd, FALSE);
505 gIdxLastSheet = 1;
506 return FALSE;
507 case PSN_APPLY:
509 SETDLGRESULT(hwnd, PSNRET_NOERROR);
510 return TRUE;
512 break;
516 break;
518 return 0;
525 static void fillSheet(PROPSHEETPAGE * psp, int idDlg, DLGPROC pfnDlgProc)
527 memset(psp, 0, sizeof(PROPSHEETPAGE));
528 psp->dwSize = sizeof(PROPSHEETPAGE);
529 psp->dwFlags = PSP_DEFAULT; // | PSP_USEICONID;
530 psp->hInstance = ghInstance;
531 psp->pszTemplate = MAKEINTRESOURCE(idDlg);
532 //psp->pszIcon = (LPSTR) MAKEINTRESOURCE(IDI_DOC);
533 psp->pfnDlgProc = pfnDlgProc;
534 psp->lParam = 0;
540 int CALLBACK WINAPI initPropSheets(HWND hwnd, UINT msg, LPARAM lp)
542 //dbg("initPropSheets: %p, %d", hwnd, msg);
543 if (msg != PSCB_INITIALIZED)
544 return 0;
545 ghwPropSheet = hwnd;
546 PropSheet_SetCurSel(ghwPropSheet, gIdxLastSheet, gIdxLastSheet);
547 return 1;
551 HWND showDlg(HWND hwParent)
553 PROPSHEETHEADER psh;
554 PROPSHEETPAGE pages[4];
556 int k=0;
557 fillSheet(&pages[k++], IDD_OSDIALOGS, wpOSDlgs);
558 fillSheet(&pages[k++], IDD_EXCLUDES, wpExcludes);
559 // fillSheet(&pages[k++], IDD_APPS, wpApps);
560 fillSheet(&pages[k++], IDD_PREFS, wpPrefs);
562 memset(&psh, 0, sizeof(PROPSHEETHEADER));
563 psh.dwSize = sizeof(PROPSHEETHEADER);
564 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEHICON | PSH_USECALLBACK | PSH_MODELESS; // |
565 // PSH_NOAPPLYNOW
566 // //
567 // |
568 // PSH_USECALLBACK;
569 psh.hwndParent = hwParent;
570 psh.hInstance = ghInstance;
571 psh.hIcon = ghIconSm;
572 psh.pszCaption = (LPSTR) "OpenWide Settings";
573 psh.nPages = k; //sizeof(pages) / sizeof(PROPSHEETPAGE);
574 psh.ppsp = (LPCPROPSHEETPAGE) pages;
575 psh.pfnCallback = initPropSheets;
576 psh.nStartPage = 0;
578 return (HWND)PropertySheet(&psh);