MINOR: Tweak About box and traymenu pos
[openwide.git] / psDlg.c
blobeb1375ca1a2bdb26a7ff28808b7874c829b14094
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 /**
25 * @author Luke Hudson
26 * @licence GPL2
30 #include <shlwapi.h>
31 #include "openwidedll.h"
32 #include "openwideres.h"
33 #include "owutil.h"
34 #include "owSharedUtil.h"
36 #include "openwide_proto.h"
39 #define SETDLGRESULT(hw, x) SetWindowLong((hw), DWL_MSGRESULT, (LONG)(x))
40 static int gIdxLastSheet = 0;
43 static void setTabChanged(HWND hwnd, BOOL bChanged)
45 if( bChanged )
46 PropSheet_Changed(ghwPropSheet, hwnd);
47 else
48 PropSheet_UnChanged(ghwPropSheet, hwnd);
51 int savePrefsToRegistry(void)
53 HKEY hk = regCreateKey(HKEY_CURRENT_USER, OW_REGKEY_NAME);
54 if( hk )
56 regWriteDWORD(hk, NULL, sizeof(OWSharedData));
57 regWriteBinaryData(hk, "OWData", (byte *)gPowData, sizeof(OWSharedData));
58 regCloseKey(hk);
59 return 1;
61 return 0;
66 int initPrefs(HWND hwnd)
68 if(!initSharedMem(NULL))
69 return 0;
71 CheckDlgButton(hwnd, IDC_WSTARTUP, isStartupApp(hwnd));
72 CheckDlgButton(hwnd, IDC_LOG, BST_CHECKED);
74 if( !waitForMutex() )
75 return 0;
77 CheckDlgButton(hwnd, IDC_STARTMIN, gPowData->bStartMin);
78 CheckDlgButton(hwnd, IDC_SHOWICON, gPowData->bShowIcon);
80 releaseMutex();
82 /* if( bMinimize )
84 SendMessage(ghwMain, WM_SYSCOMMAND, SC_MINIMIZE, 0);
85 SetFocus(NULL);
86 EndTrayOperation();
88 else
90 SetFocus(GetDlgItem(hwnd, IDB_SETPLACEMENT));
91 ShowWindow(hwnd, SW_SHOWNORMAL);
92 }*/
93 return 1;
96 static void onPrefsTabCommand(HWND hwnd, WPARAM wp, LPARAM lp)
98 switch(LOWORD(wp))
100 case IDC_WSTARTUP:
101 case IDC_STARTMIN:
102 case IDC_SHOWICON:
103 setTabChanged(hwnd, HIWORD(wp)==BN_CLICKED);
104 break;
108 static BOOL CALLBACK wpPrefs(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
110 switch (uMsg)
112 case WM_INITDIALOG:
113 initPrefs(hwnd);
114 break;
115 case WM_COMMAND:
116 onPrefsTabCommand(hwnd, wp, lp);
117 break;
118 case WM_NOTIFY:
120 LPNMHDR pnh = (LPNMHDR)lp;
121 if( pnh->hwndFrom == ghwPropSheet )
123 switch(pnh->code)
125 case PSN_SETACTIVE:
126 SETDLGRESULT(hwnd, FALSE);
127 gIdxLastSheet = 1;
128 return FALSE;
129 case PSN_APPLY:
131 if(!waitForMutex())
133 //Warn("Couldn't get access to shared memory!");
134 SETDLGRESULT(hwnd, PSNRET_INVALID);
135 return FALSE;
138 gPowData->bStartMin = (IsDlgButtonChecked(hwnd, IDC_STARTMIN) == BST_CHECKED);
139 BOOL bShowIcon = gPowData->bShowIcon = (IsDlgButtonChecked(hwnd, IDC_SHOWICON) == BST_CHECKED);
140 BOOL bWinStart = IsDlgButtonChecked(hwnd, IDC_WSTARTUP) == BST_CHECKED;
142 savePrefsToRegistry();
144 releaseMutex();
146 setStartupApp(hwnd, bWinStart);
147 if( bShowIcon )
148 addTrayIcon(ghwMain);
149 else
150 remTrayIcon(ghwMain);
151 SETDLGRESULT(hwnd, PSNRET_NOERROR);
152 return TRUE;
154 break;
158 break;
160 case WM_DESTROY:
161 break;
162 case WM_CLOSE:
163 break;
166 return 0;
169 static int enumExcludes(HKEY hkRoot, const char *szKey, DWORD dwType, LPVOID pParm)
171 if( dwType == REG_DWORD )
172 SendMessage((HWND)pParm, LB_ADDSTRING, 0, (LPARAM)szKey);
173 return 1;
176 static int addExcludes2LB(HWND hwnd, UINT uID)
178 HWND hwLB = GetDlgItem(hwnd, uID);
179 if( !hwLB )
180 return 0;
181 HKEY hk = regOpenKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
182 if( hk )
184 regEnumValues(hk, enumExcludes, (LPVOID)hwLB);
185 regCloseKey(hk);
186 return 1;
188 return 0;
191 static int initExcludes(HWND hwnd)
193 return addExcludes2LB(hwnd, IDL_EXCLUDES);
196 static int addExclude(HWND hwnd)
198 const char *szEx = Prompt_File_Name(OPEN, hwnd, "Executable Files\0*.exe;*.com", "Choose program to exclude");
199 if( szEx )
201 // PathStripPath((char *)szEx);
202 int idx = SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_FINDSTRINGEXACT, -1, (LPARAM)szEx);
203 if( idx != LB_ERR )
204 Warn("This program is already excluded");
205 else
206 SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_ADDSTRING, 0, (LPARAM)szEx);
208 return 1;
211 static int rmvExclude(HWND hwnd)
213 int idx = SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_GETCURSEL, 0,0);
214 if( idx != LB_ERR )
215 SendDlgItemMessage(hwnd, IDL_EXCLUDES, LB_DELETESTRING, idx, 0);
216 return 1;
219 static int saveExcludes2Registry(HWND hwnd, UINT uID)
221 HWND hwLB = GetDlgItem(hwnd, uID);
222 if( !hwLB )
223 return 0;
224 // static char szKey[32];
225 regDeleteKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
226 HKEY hk = regCreateKey(HKEY_CURRENT_USER, OW_REGKEY_EXCLUDES_NAME);
227 if( hk )
229 int num = SendMessage(hwLB, LB_GETCOUNT,0,0);
230 if( num != LB_ERR )
232 for(int i=0; i < num; i++)
234 char *szEx = lbGetItemText(hwLB, i);
235 if( szEx )
237 regWriteDWORD(hk, szEx, 1);
238 free(szEx);
242 regCloseKey(hk);
243 return 1;
245 return 0;
248 static void onExTabCmd(HWND hwnd, WPARAM wp, LPARAM lp)
250 switch(LOWORD(wp))
252 case IDB_ADDEX:
253 addExclude(hwnd);
254 break;
255 case IDB_RMVEX:
256 rmvExclude(hwnd);
257 break;
261 static BOOL CALLBACK wpExcludes(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
263 switch (uMsg)
265 case WM_INITDIALOG:
266 initExcludes(hwnd);
267 break;
268 case WM_COMMAND:
269 onExTabCmd(hwnd, wp, lp);
270 break;
271 case WM_NOTIFY:
273 LPNMHDR pnh = (LPNMHDR)lp;
274 if( pnh->hwndFrom == ghwPropSheet )
276 switch(pnh->code)
278 case PSN_SETACTIVE:
279 SETDLGRESULT(hwnd, FALSE);
280 gIdxLastSheet = 1;
281 return FALSE;
282 case PSN_APPLY:
284 saveExcludes2Registry(hwnd, IDL_EXCLUDES);
285 SETDLGRESULT(hwnd, PSNRET_NOERROR);
286 return TRUE;
288 break;
292 break;
294 return 0;
299 static void onOSTabCommand(HWND hwnd, WPARAM wp, LPARAM lp)
301 switch(LOWORD(wp))
303 case IDCB_VIEW:
304 case IDCB_FOCUS:
305 if( HIWORD(wp) == CBN_SELCHANGE )
306 setTabChanged(hwnd, TRUE);
307 break;
308 case IDE_LEFT:
309 case IDE_TOP:
310 case IDE_WIDTH:
311 case IDE_HEIGHT:
312 if( HIWORD(wp) == EN_CHANGE )
313 setTabChanged(hwnd, TRUE);
314 break;
315 case IDB_QUIT:
316 DestroyWindow(hwnd);
317 break;
318 case IDB_SETPLACEMENT:
320 RECT r;
321 r.left = GetDlgItemInt(hwnd, IDE_LEFT, NULL, TRUE);
322 r.top = GetDlgItemInt(hwnd, IDE_TOP, NULL, TRUE);
323 r.right = max( GetDlgItemInt(hwnd, IDE_WIDTH, NULL, FALSE), 100 );
324 r.bottom = max( GetDlgItemInt(hwnd, IDE_HEIGHT, NULL, FALSE), 100 );
326 if( DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_PLACEMENT), hwnd, wpPlacement, (LPARAM)&r) )
328 SetDlgItemInt(hwnd, IDE_LEFT, r.left, TRUE);
329 SetDlgItemInt(hwnd, IDE_TOP, r.top, TRUE);
330 SetDlgItemInt(hwnd, IDE_WIDTH, r.right - r.left, TRUE);
331 SetDlgItemInt(hwnd, IDE_HEIGHT, r.bottom - r.top, TRUE);
332 setTabChanged(hwnd, TRUE);
335 break;
336 case IDB_TEST:
337 Prompt_File_Name(0, hwnd, NULL, NULL);
338 break;
342 void fillFocusCB(HWND hwnd, UINT uID)
344 HWND hwCB = GetDlgItem(hwnd, uID);
345 if(!hwCB)
346 return;
347 SendMessage(hwCB, CB_RESETCONTENT, 0,0);
348 cbAddString(hwCB, "Directory listing", F_DIRLIST);
349 cbAddString(hwCB, "File name", F_FNAME);
350 cbAddString(hwCB, "File type", F_FTYPE);
351 cbAddString(hwCB, "Places bar", F_PLACES);
352 cbAddString(hwCB, "Look in", F_LOOKIN);
356 void fillViewCB(HWND hwnd, UINT uID)
358 HWND hwCB = GetDlgItem(hwnd, uID);
359 if(!hwCB)
360 return;
361 SendMessage(hwCB, CB_RESETCONTENT, 0,0);
362 cbAddString(hwCB, "Large Icons", V_LGICONS);
363 if( isWinXP() )
364 cbAddString(hwCB, "Tiles", V_TILES);
365 else
366 cbAddString(hwCB, "Small Icons", V_SMICONS);
367 cbAddString(hwCB, "List", V_LIST);
368 cbAddString(hwCB, "Details", V_DETAILS);
369 cbAddString(hwCB, "Thumbnails", V_THUMBS);
372 void selectCBView(HWND hwnd, UINT uID, int iView)
374 HWND hwCB = GetDlgItem(hwnd, uID);
375 if(!hwCB)
376 return;
377 int nItems = SendMessage(hwCB, CB_GETCOUNT, 0,0);
378 for(int i=0; i < nItems; i++)
380 int ivItem = (int)SendMessage(hwCB, CB_GETITEMDATA, i, 0);
381 if( ivItem == iView)
383 SendMessage(hwCB, CB_SETCURSEL, i, 0);
384 break;
389 static int initOSTab(HWND hwnd)
391 CheckDlgButton(hwnd, IDC_WSTARTUP, isStartupApp(hwnd));
392 CheckDlgButton(hwnd, IDC_LOG, BST_CHECKED);
393 fillViewCB(hwnd, IDCB_VIEW);
394 fillFocusCB(hwnd, IDCB_FOCUS);
396 if(!waitForMutex())
397 return 0;
398 // bMinimize = gPowData->bStartMin;
400 SetDlgItemInt(hwnd, IDE_LEFT, gPowData->ptOrg.x, TRUE);
401 SetDlgItemInt(hwnd, IDE_TOP, gPowData->ptOrg.y, TRUE);
402 SetDlgItemInt(hwnd, IDE_WIDTH, gPowData->szDim.cx, FALSE);
403 SetDlgItemInt(hwnd, IDE_HEIGHT, gPowData->szDim.cy, FALSE);
405 selectCBView(hwnd, IDCB_VIEW, gPowData->iView);
406 SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_SETCURSEL, gPowData->iFocus, 0);
408 /// released shared data
409 releaseMutex();
410 return 1;
413 static BOOL CALLBACK wpOSDlgs(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
415 switch (uMsg)
417 case WM_INITDIALOG:
418 initOSTab(hwnd);
419 return FALSE;
420 case WM_TRAYICON:
421 switch(lp)
423 case WM_LBUTTONUP:
424 ShowWindow(hwnd, SW_SHOWNORMAL);
425 EnableWindow(hwnd, TRUE);
426 remTrayIcon(hwnd);
427 SetFocus(hwnd);
428 break;
430 break;
431 /* case WM_SYSCOMMAND:
432 if( wp == SC_MINIMIZE )
434 EnableWindow(hwnd, FALSE);
435 ShowWindow(hwnd, SW_HIDE);
436 addTrayIcon(hwnd);
437 EndTrayOperation();
439 else
440 return DefWindowProc(hwnd, uMsg, wp, lp);
441 break;*/
442 case WM_COMMAND:
443 onOSTabCommand(hwnd, wp, lp);
444 break;
445 case WM_NOTIFY:
447 LPNMHDR pnh = (LPNMHDR)lp;
448 if( pnh->hwndFrom == ghwPropSheet )
450 switch(pnh->code)
452 case PSN_SETACTIVE:
453 SETDLGRESULT(hwnd, FALSE);
454 gIdxLastSheet = 1;
455 return FALSE;
456 case PSN_APPLY:
458 if(!waitForMutex())
460 //Warn("Couldn't get access to shared memory!");
461 SETDLGRESULT(hwnd, PSNRET_INVALID);
462 return FALSE;
464 BOOL bOK;
465 int i = GetDlgItemInt(hwnd, IDE_LEFT, &bOK, TRUE);
466 if( bOK )
467 gPowData->ptOrg.x = i;
468 i = GetDlgItemInt(hwnd, IDE_TOP, &bOK, TRUE);
469 if( bOK )
470 gPowData->ptOrg.y = i;
471 i = GetDlgItemInt(hwnd, IDE_WIDTH, &bOK, TRUE);
472 if( bOK )
473 gPowData->szDim.cx = i;
474 i = GetDlgItemInt(hwnd, IDE_HEIGHT, &bOK, TRUE);
475 if( bOK )
476 gPowData->szDim.cy = i;
477 int idx = SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_GETCURSEL, 0, 0);
478 if( idx != CB_ERR )
480 int f = SendDlgItemMessage(hwnd, IDCB_FOCUS, CB_GETITEMDATA, idx, 0);
481 if( f >= 0 && f < F_MAX )
482 gPowData->iFocus = f;
484 idx = SendDlgItemMessage(hwnd, IDCB_VIEW, CB_GETCURSEL, 0, 0);
485 if( idx != CB_ERR )
487 int v = SendDlgItemMessage(hwnd, IDCB_VIEW, CB_GETITEMDATA, idx, 0);
488 if( v >= 0 && v < V_MAX )
489 gPowData->iView = v;
492 savePrefsToRegistry();
494 releaseMutex();
495 SETDLGRESULT(hwnd, PSNRET_NOERROR);
496 return TRUE;
498 break;
502 break;
504 return 0;
508 static BOOL CALLBACK wpApps(HWND hwnd, UINT uMsg, WPARAM wp, LPARAM lp)
510 switch (uMsg)
512 case WM_INITDIALOG:
513 break;
514 case WM_NOTIFY:
516 LPNMHDR pnh = (LPNMHDR)lp;
517 if( pnh->hwndFrom == ghwPropSheet )
519 switch(pnh->code)
521 case PSN_SETACTIVE:
522 SETDLGRESULT(hwnd, FALSE);
523 gIdxLastSheet = 1;
524 return FALSE;
525 case PSN_APPLY:
527 SETDLGRESULT(hwnd, PSNRET_NOERROR);
528 return TRUE;
530 break;
534 break;
536 return 0;
543 static void fillSheet(PROPSHEETPAGE * psp, int idDlg, DLGPROC pfnDlgProc)
545 memset(psp, 0, sizeof(PROPSHEETPAGE));
546 psp->dwSize = sizeof(PROPSHEETPAGE);
547 psp->dwFlags = PSP_DEFAULT; // | PSP_USEICONID;
548 psp->hInstance = ghInstance;
549 psp->pszTemplate = MAKEINTRESOURCE(idDlg);
550 //psp->pszIcon = (LPSTR) MAKEINTRESOURCE(IDI_DOC);
551 psp->pfnDlgProc = pfnDlgProc;
552 psp->lParam = 0;
558 int CALLBACK WINAPI initPropSheets(HWND hwnd, UINT msg, LPARAM lp)
560 //dbg("initPropSheets: %p, %d", hwnd, msg);
561 if (msg != PSCB_INITIALIZED)
562 return 0;
563 ghwPropSheet = hwnd;
564 PropSheet_SetCurSel(ghwPropSheet, gIdxLastSheet, gIdxLastSheet);
565 return 1;
569 HWND showDlg(HWND hwParent)
571 PROPSHEETHEADER psh;
572 PROPSHEETPAGE pages[4];
574 int k=0;
575 fillSheet(&pages[k++], IDD_OSDIALOGS, wpOSDlgs);
576 fillSheet(&pages[k++], IDD_EXCLUDES, wpExcludes);
577 // fillSheet(&pages[k++], IDD_APPS, wpApps);
578 fillSheet(&pages[k++], IDD_PREFS, wpPrefs);
580 memset(&psh, 0, sizeof(PROPSHEETHEADER));
581 psh.dwSize = sizeof(PROPSHEETHEADER);
582 psh.dwFlags = PSH_PROPSHEETPAGE | PSH_USEHICON | PSH_USECALLBACK | PSH_MODELESS; // |
583 // PSH_NOAPPLYNOW
584 // //
585 // |
586 // PSH_USECALLBACK;
587 psh.hwndParent = hwParent;
588 psh.hInstance = ghInstance;
589 psh.hIcon = ghIconSm;
590 psh.pszCaption = (LPSTR) "OpenWide Settings";
591 psh.nPages = k; //sizeof(pages) / sizeof(PROPSHEETPAGE);
592 psh.ppsp = (LPCPROPSHEETPAGE) pages;
593 psh.pfnCallback = initPropSheets;
594 psh.nStartPage = 0;
596 return (HWND)PropertySheet(&psh);