Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1292 lines
30 KiB

  1. /*******************************************************************************
  2. *
  3. * Copyright 1999 American Power Conversion, All Rights Reserved
  4. *
  5. * TITLE: UPSSELECT.C
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: SteveT
  10. *
  11. * DATE: 07 June, 1999
  12. *
  13. * DESCRIPTION: This file contains all of the functions that support the
  14. * UPS Type Selection dialog.
  15. *******************************************************************************/
  16. #include "upstab.h"
  17. //#include "..\powercfg.h"
  18. #include "..\pwrresid.h"
  19. #include "..\PwrMn_cs.h"
  20. /*
  21. * forward declarations
  22. */
  23. void initUPSSelectDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  24. void updateVendorList(HWND hDlg) ;
  25. void updateModelList(HWND hDlg) ;
  26. void updatePortList(HWND hDlg) ;
  27. void handleVendorList(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) ;
  28. void handleModelList(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  29. void handlePortList(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) ;
  30. void setServiceData(HWND hDlg);
  31. void configPortList(HWND hDlg);
  32. void configModelList(HWND hDlg);
  33. void configFinishButton(HWND hDlg);
  34. BOOL processUPSSelectCtrls(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  35. /*
  36. * local allocations
  37. */
  38. static TCHAR g_szCurrentVendor[MAX_PATH] = _T("");
  39. static TCHAR g_szCurrentModel[MAX_PATH] = _T("");
  40. static TCHAR g_szCurrentPort[MAX_PATH] = _T("");
  41. static TCHAR g_szCurrentServiceDLL[MAX_PATH]= _T("");
  42. static DWORD g_dwCurrentOptions = UPS_DEFAULT_SIGMASK;
  43. static DWORD g_dwCurrentCustomOptions = UPS_DEFAULT_SIGMASK;
  44. static struct _customData g_CustomData = { g_szCurrentPort, &g_dwCurrentCustomOptions};
  45. static TCHAR g_szNoUPSVendor[MAX_PATH] = _T("");
  46. static TCHAR g_szOtherUPSVendor[MAX_PATH] = _T("");
  47. static TCHAR g_szCustomUPSModel[MAX_PATH] = _T("");
  48. static TCHAR g_szCOMPortPrefix[MAX_PATH] = _T("");
  49. static const DWORD g_UPSSelectHelpIDs[] =
  50. {
  51. IDC_VENDOR_TEXT,idh_select_manufacturer,
  52. IDC_VENDOR_LIST,idh_select_manufacturer,
  53. IDC_MODEL_TEXT,idh_select_model,
  54. IDC_MODEL_LIST,idh_select_model,
  55. IDC_PORT_TEXT,idh_on_port,
  56. IDC_PORT_LIST,idh_on_port,
  57. IDB_SELECT_FINISH,idh_finish,
  58. IDB_SELECT_NEXT,idh_next,
  59. 0,0
  60. };
  61. /*
  62. * define the possible "states" of the controls.
  63. * these states are set whenever a control is altered,
  64. * and are generally used to speed up processing by
  65. * basing decisions on states rather than having to
  66. * continually query controls to find out what they're
  67. * displaying.
  68. */
  69. static enum _vendorStates {eVendorUnknown, eVendorSelected, eVendorGeneric, eVendorNone} g_vendorState;
  70. static enum _modelStates {eModelUnknown, eModelSelected, eModelCustom} g_modelState;
  71. static enum _portStates {ePortUnknown, ePortSelected} g_portState;
  72. static enum _finishStates {eFinish, eNext} g_finishButtonState;
  73. /*
  74. * Load all used values here
  75. */
  76. void getUPSConfigValues()
  77. {
  78. GetUPSConfigVendor(g_szCurrentVendor, MAX_PATH);
  79. GetUPSConfigModel(g_szCurrentModel, MAX_PATH);
  80. GetUPSConfigPort(g_szCurrentPort, MAX_PATH);
  81. GetUPSConfigOptions( &g_dwCurrentOptions);
  82. GetUPSConfigCustomOptions( &g_dwCurrentCustomOptions);
  83. GetUPSConfigServiceDLL(g_szCurrentServiceDLL, MAX_PATH);
  84. }
  85. /*
  86. * Save all used values here
  87. */
  88. void setUPSConfigValues()
  89. {
  90. SetUPSConfigVendor( g_szCurrentVendor);
  91. SetUPSConfigModel( g_szCurrentModel);
  92. SetUPSConfigPort( g_szCurrentPort);
  93. SetUPSConfigOptions( g_dwCurrentOptions);
  94. SetUPSConfigCustomOptions( g_dwCurrentCustomOptions);
  95. SetUPSConfigServiceDLL( g_szCurrentServiceDLL);
  96. AddActiveDataState(SERVICE_DATA_CHANGE);
  97. EnableApplyButton();
  98. }
  99. /*
  100. * BOOL CALLBACK UPSSelectDlgProc (HWND hDlg,
  101. * UINT uMsg,
  102. * WPARAM wParam,
  103. * LPARAM lParam);
  104. *
  105. * Description: This is a standard DialogProc associated with the UPS select dialog
  106. *
  107. * Additional Information: See help on DialogProc
  108. *
  109. * Parameters:
  110. *
  111. * HWND hDlg :- Handle to dialog box
  112. *
  113. * UINT uMsg :- message ID
  114. *
  115. * WPARAM wParam :- Specifies additional message-specific information.
  116. *
  117. * LPARAM lParam :- Specifies additional message-specific information.
  118. *
  119. * Return Value: Except in response to the WM_INITDIALOG message, the dialog
  120. * box procedure should return nonzero if it processes the
  121. * message, and zero if it does not.
  122. */
  123. INT_PTR CALLBACK UPSSelectDlgProc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  124. {
  125. BOOL bRes = TRUE;
  126. switch (uMsg)
  127. {
  128. case WM_INITDIALOG:
  129. {
  130. initUPSSelectDlg(hDlg,uMsg,wParam,lParam);
  131. break;
  132. }
  133. case WM_CLOSE:
  134. {
  135. EndDialog( hDlg, IDCANCEL);
  136. break;
  137. }
  138. case WM_COMMAND:
  139. {
  140. bRes = processUPSSelectCtrls( hDlg, uMsg, wParam, lParam);
  141. break;
  142. }
  143. case WM_HELP: //F1 or question box
  144. {
  145. WinHelp(((LPHELPINFO)lParam)->hItemHandle,
  146. PWRMANHLP,
  147. HELP_WM_HELP,
  148. (ULONG_PTR)(LPTSTR)g_UPSSelectHelpIDs);
  149. break;
  150. }
  151. case WM_CONTEXTMENU: // right mouse click help
  152. {
  153. WinHelp((HWND)wParam,
  154. PWRMANHLP,
  155. HELP_CONTEXTMENU,
  156. (ULONG_PTR)(LPTSTR)g_UPSSelectHelpIDs);
  157. break;
  158. }
  159. default:
  160. {
  161. bRes = FALSE;
  162. break;
  163. }
  164. }
  165. return bRes;
  166. }
  167. /*
  168. * BOOL processUPSSelectCtrls (HWND hDlg,
  169. * UINT uMsg,
  170. * WPARAM wParam,
  171. * LPARAM lParam);
  172. *
  173. * Description: processes WM_COMMAND messages for the UPS select dialog
  174. *
  175. * Additional Information:
  176. *
  177. * Parameters:
  178. *
  179. * HWND hDlg :- Handle to dialog box
  180. *
  181. * UINT uMsg :- message ID
  182. *
  183. * WPARAM wParam :- Specifies additional message-specific information.
  184. *
  185. * LPARAM lParam :- Specifies additional message-specific information.
  186. *
  187. * Return Value: returns TRUE unless the control specified is unknown
  188. */
  189. BOOL processUPSSelectCtrls(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  190. {
  191. BOOL bRes = TRUE;
  192. switch (LOWORD(wParam))
  193. {
  194. case IDB_SELECT_FINISH:
  195. {
  196. setServiceData(hDlg);
  197. setUPSConfigValues();
  198. EndDialog(hDlg,wParam);
  199. break;
  200. }
  201. case IDB_SELECT_NEXT:
  202. {
  203. /*
  204. * the user has selected Custom...
  205. */
  206. INT_PTR iCustomRes;
  207. /*
  208. * bring up the custom config dialog
  209. */
  210. // ShowWindow(hDlg,SW_HIDE);
  211. iCustomRes = DialogBoxParam(
  212. GetUPSModuleHandle(),
  213. MAKEINTRESOURCE(IDD_UPSCUSTOM),
  214. hDlg,
  215. UPSCustomDlgProc,
  216. (LPARAM)&g_CustomData);
  217. switch (iCustomRes)
  218. {
  219. case IDB_CUSTOM_FINISH:
  220. {
  221. /*
  222. * Save Custom signal values
  223. */
  224. setServiceData(hDlg);
  225. setUPSConfigValues();
  226. EndDialog(hDlg,wParam);
  227. break;
  228. }
  229. case IDCANCEL: // the escape key
  230. {
  231. EndDialog(hDlg,wParam);
  232. break;
  233. }
  234. case IDB_CUSTOM_BACK:
  235. {
  236. ShowWindow(hDlg,SW_SHOW);
  237. break;
  238. }
  239. }
  240. break;
  241. }
  242. case IDCANCEL: // the escape key
  243. {
  244. EndDialog(hDlg,wParam);
  245. break;
  246. }
  247. case IDC_VENDOR_LIST:
  248. {
  249. handleVendorList(hDlg,uMsg,wParam,lParam);
  250. break;
  251. }
  252. case IDC_MODEL_LIST:
  253. {
  254. handleModelList(hDlg,uMsg,wParam,lParam);
  255. break;
  256. }
  257. case IDC_PORT_LIST:
  258. {
  259. handlePortList(hDlg,uMsg,wParam,lParam);
  260. break;
  261. }
  262. default:
  263. {
  264. bRes = FALSE;
  265. break;
  266. }
  267. }
  268. return bRes;
  269. }
  270. /*
  271. * void initUPSSelectDlg (HWND hDlg,
  272. * UINT uMsg,
  273. * WPARAM wParam,
  274. * LPARAM lParam);
  275. *
  276. * Description: initializes global data and controls for UPS select dialog
  277. *
  278. * Additional Information:
  279. *
  280. * Parameters:
  281. *
  282. * HWND hDlg :- Handle to dialog box
  283. *
  284. * UINT uMsg :- message ID
  285. *
  286. * WPARAM wParam :- Specifies additional message-specific information.
  287. *
  288. * LPARAM lParam :- Specifies additional message-specific information.
  289. *
  290. * Return Value: none
  291. */
  292. void initUPSSelectDlg(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  293. {
  294. /*
  295. * load registry settings
  296. */
  297. getUPSConfigValues();
  298. /*
  299. * load string resources
  300. */
  301. LoadString( GetUPSModuleHandle(),
  302. IDS_NO_UPS_VENDOR,
  303. g_szNoUPSVendor,
  304. sizeof(g_szNoUPSVendor)/sizeof(TCHAR));
  305. LoadString( GetUPSModuleHandle(),
  306. IDS_OTHER_UPS_VENDOR,
  307. g_szOtherUPSVendor,
  308. sizeof(g_szOtherUPSVendor)/sizeof(TCHAR));
  309. LoadString( GetUPSModuleHandle(),
  310. IDS_CUSTOM_UPS_MODEL,
  311. g_szCustomUPSModel,
  312. sizeof(g_szCustomUPSModel)/sizeof(TCHAR));
  313. LoadString( GetUPSModuleHandle(),
  314. IDS_COM_PORT_PREFIX,
  315. g_szCOMPortPrefix,
  316. sizeof(g_szCOMPortPrefix)/sizeof(TCHAR));
  317. /*
  318. * disable the Finish button, just in case its not disabled by default
  319. */
  320. //EnableWindow( GetDlgItem( hDlg, IDB_SELECT_FINISH), FALSE);
  321. /*
  322. * init the list controls
  323. */
  324. updateVendorList( hDlg);
  325. updateModelList( hDlg);
  326. updatePortList( hDlg);
  327. configFinishButton(hDlg);
  328. }
  329. /*
  330. * void updateVendorList (HWND hDlg);
  331. *
  332. * Description: updates the vendor list control in UPS select dialog
  333. *
  334. * Additional Information:
  335. *
  336. * Parameters:
  337. *
  338. * HWND hDlg :- Handle to dialog box
  339. *
  340. * Return Value: none
  341. */
  342. void updateVendorList(HWND hDlg)
  343. {
  344. HKEY hkResult;
  345. LRESULT lListRes;
  346. g_vendorState = eVendorUnknown;
  347. /*
  348. * clear and disable the list
  349. */
  350. lListRes = SendDlgItemMessage( hDlg, IDC_VENDOR_LIST, CB_RESETCONTENT,0,0);
  351. EnableWindow( GetDlgItem( hDlg, IDC_VENDOR_LIST), FALSE);
  352. EnableWindow( GetDlgItem( hDlg, IDC_VENDOR_TEXT), FALSE);
  353. // Add "None" as the first item in the list
  354. lListRes = SendDlgItemMessage( hDlg,
  355. IDC_VENDOR_LIST,
  356. CB_ADDSTRING,
  357. 0,
  358. (LPARAM)g_szNoUPSVendor);
  359. /*
  360. * Build the rest of the Vendor list from the registry
  361. */
  362. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  363. UPS_SERVICE_ROOT,
  364. 0,
  365. KEY_ENUMERATE_SUB_KEYS,
  366. &hkResult))
  367. {
  368. DWORD dwIndex = 0;
  369. LONG lRes = ERROR_SUCCESS;
  370. TCHAR szKeyName[MAX_PATH] = _T("");
  371. DWORD dwKeyLen;
  372. FILETIME ftLast;
  373. while (ERROR_SUCCESS == lRes)
  374. {
  375. dwKeyLen = sizeof(szKeyName)/sizeof(TCHAR);
  376. lRes = RegEnumKeyEx(hkResult,
  377. dwIndex,
  378. szKeyName, // address of buffer for subkey name
  379. &dwKeyLen, // address for size of subkey buffer
  380. NULL, // reserved
  381. NULL, // address of buffer for class string
  382. NULL, // address for size of class buffer
  383. &ftLast); // address for time key last written to
  384. // The "NoUPS" and "OtherUPS" options are added manually
  385. // before and after spinning through the registry
  386. // (unlike mfg names, these string require localization).
  387. // In RC2 registry keys were created with these values (English only).
  388. // To avoid duplicate entries in the combo, if the registry key name
  389. // matches the "NoUPS" or "OtherUPS" strings then skip over it.
  390. if( (ERROR_SUCCESS == lRes) &&
  391. (0 != _tcsicmp( szKeyName, g_szNoUPSVendor)) &&
  392. (0 != _tcsicmp( szKeyName, g_szOtherUPSVendor)) )
  393. {
  394. lListRes = SendDlgItemMessage( hDlg,
  395. IDC_VENDOR_LIST,
  396. CB_ADDSTRING,
  397. 0,
  398. (LPARAM)szKeyName);
  399. }
  400. dwIndex++;
  401. }
  402. RegCloseKey(hkResult);
  403. }
  404. // Add the "Generic" vendor at the end of the list
  405. lListRes = SendDlgItemMessage( hDlg,
  406. IDC_VENDOR_LIST,
  407. CB_ADDSTRING,
  408. 0,
  409. (LPARAM)g_szOtherUPSVendor);
  410. EnableWindow( GetDlgItem( hDlg, IDC_VENDOR_LIST), TRUE);
  411. EnableWindow( GetDlgItem( hDlg, IDC_VENDOR_TEXT), TRUE);
  412. // Now find the current vendor in the combo box...
  413. //
  414. lListRes = SendDlgItemMessage(hDlg,
  415. IDC_VENDOR_LIST,
  416. CB_FINDSTRINGEXACT,
  417. -1,
  418. (LPARAM)g_szCurrentVendor);
  419. // ... and select it.
  420. if( CB_ERR != lListRes )
  421. {
  422. lListRes = SendDlgItemMessage( hDlg,
  423. IDC_VENDOR_LIST,
  424. CB_SETCURSEL,
  425. lListRes,
  426. 0);
  427. }
  428. // Set the vendor state
  429. //
  430. if (0 == _tcsicmp( g_szCurrentVendor, g_szNoUPSVendor))
  431. {
  432. g_vendorState = eVendorNone;
  433. }
  434. else
  435. {
  436. if (0 == _tcsicmp( g_szCurrentVendor, g_szOtherUPSVendor))
  437. {
  438. g_vendorState = eVendorGeneric;
  439. }
  440. else
  441. {
  442. g_vendorState = eVendorSelected;
  443. }
  444. }
  445. }
  446. /*
  447. * void updateModelList (HWND hDlg);
  448. *
  449. * Description: updates the UPS model list control in UPS select dialog
  450. *
  451. * Additional Information:
  452. *
  453. * Parameters:
  454. *
  455. * HWND hDlg :- Handle to dialog box
  456. *
  457. * Return Value: none
  458. */
  459. void updateModelList(HWND hDlg)
  460. {
  461. LRESULT lListRes;
  462. g_modelState = eModelUnknown;
  463. /*
  464. * clear and disable the list
  465. */
  466. lListRes = SendDlgItemMessage( hDlg, IDC_MODEL_LIST, LB_RESETCONTENT,0,0);
  467. configModelList( hDlg);
  468. /*
  469. * load the list, but only if:
  470. * 1) the current vendor is valid and not NONE
  471. */
  472. if (eVendorGeneric == g_vendorState)
  473. {
  474. lListRes = SendDlgItemMessage( hDlg,
  475. IDC_MODEL_LIST,
  476. LB_ADDSTRING,
  477. 0,
  478. (LPARAM)g_szCustomUPSModel);
  479. }
  480. if (eVendorSelected == g_vendorState)
  481. {
  482. HKEY hkResult;
  483. TCHAR szVendorKey[MAX_PATH] = _T("");
  484. wsprintf(szVendorKey,_T("%s\\%s"),UPS_SERVICE_ROOT,g_szCurrentVendor);
  485. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  486. szVendorKey,
  487. 0,
  488. KEY_QUERY_VALUE,
  489. &hkResult))
  490. {
  491. DWORD dwIndex = 0;
  492. LONG lRes = ERROR_SUCCESS;
  493. TCHAR szValueName[MAX_PATH] = _T("");
  494. DWORD dwValueLen;
  495. while (ERROR_SUCCESS == lRes)
  496. {
  497. dwValueLen = sizeof(szValueName)/sizeof(TCHAR);
  498. lRes = RegEnumValue(hkResult,
  499. dwIndex,
  500. szValueName, // address of buffer for subkey name
  501. &dwValueLen, // address for size of subkey buffer
  502. NULL, // reserved
  503. NULL, // address of buffer for type code
  504. NULL, // address of buffer for value data
  505. NULL); // address for size of data buffer
  506. if (ERROR_SUCCESS == lRes)
  507. {
  508. lListRes = SendDlgItemMessage( hDlg,
  509. IDC_MODEL_LIST,
  510. LB_ADDSTRING,
  511. 0,
  512. (LPARAM)szValueName);
  513. }
  514. dwIndex++;
  515. }
  516. RegCloseKey(hkResult);
  517. }
  518. // Now find the current model in the list box...
  519. //
  520. lListRes = SendDlgItemMessage(hDlg,
  521. IDC_VENDOR_LIST,
  522. LB_FINDSTRINGEXACT,
  523. -1,
  524. (LPARAM)g_szCurrentModel);
  525. // ... and select it.
  526. if( CB_ERR != lListRes )
  527. {
  528. lListRes = SendDlgItemMessage( hDlg,
  529. IDC_VENDOR_LIST,
  530. LB_SETCURSEL,
  531. lListRes,
  532. 0);
  533. }
  534. }
  535. /*
  536. * set the model state
  537. */
  538. if (0 == _tcsicmp( g_szCurrentModel, g_szCustomUPSModel))
  539. {
  540. g_modelState = eModelCustom;
  541. }
  542. else
  543. {
  544. g_modelState = eModelSelected;
  545. }
  546. configModelList( hDlg);
  547. }
  548. /*
  549. * void updatePortList (HWND hDlg);
  550. *
  551. * Description: updates the UPS port list control in UPS select dialog
  552. *
  553. * Additional Information:
  554. *
  555. * Parameters:
  556. *
  557. * HWND hDlg :- Handle to dialog box
  558. *
  559. * Return Value: none
  560. */
  561. void updatePortList(HWND hDlg)
  562. {
  563. HKEY hkResult;
  564. LPTSTR lpColon;
  565. g_portState = ePortUnknown;
  566. /*
  567. * disable the list
  568. */
  569. configPortList( hDlg);
  570. /*
  571. * remove possible trailing colon from COM port setting
  572. */
  573. if (NULL != (lpColon = _tcschr( g_szCurrentPort, (TCHAR)':')))
  574. {
  575. *lpColon = (TCHAR)'\0';
  576. }
  577. /*
  578. * (re)building the port list, its ok to wipe it out
  579. */
  580. SendDlgItemMessage( hDlg, IDC_PORT_LIST, CB_RESETCONTENT,0,0);
  581. if (ERROR_SUCCESS == RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  582. UPS_PORT_ROOT,
  583. 0,
  584. KEY_QUERY_VALUE,
  585. &hkResult))
  586. {
  587. DWORD dwIndex = 0;
  588. LONG lRes = ERROR_SUCCESS;
  589. TCHAR szPortValue[MAX_PATH] = _T("");
  590. TCHAR szPortData[MAX_PATH] = _T("");
  591. DWORD dwPortLen;
  592. while (ERROR_SUCCESS == lRes)
  593. {
  594. dwPortLen = sizeof(szPortValue)/sizeof(TCHAR);
  595. lRes = RegEnumValue(hkResult,
  596. dwIndex,
  597. szPortValue,// address of buffer for subkey name
  598. &dwPortLen, // address for size of subkey buffer
  599. NULL, // reserved
  600. NULL, // address of buffer for type code
  601. NULL, // address of buffer for value data
  602. NULL); // address for size of data buffer
  603. if (ERROR_SUCCESS == lRes)
  604. {
  605. DWORD dwValueLen;
  606. DWORD dwValueType;
  607. // Once we have the szPortValue we need to get its data.
  608. // This is what we'll put in the combobox.
  609. dwValueLen = sizeof(szPortData)/sizeof(TCHAR);
  610. lRes = RegQueryValueEx(
  611. hkResult, // handle to key to query
  612. szPortValue, // address of name of value to query
  613. NULL, // reserved
  614. &dwValueType, // address of buffer for value type
  615. (LPBYTE)szPortData, // address of data buffer
  616. &dwValueLen // address of data buffer size
  617. );
  618. if (ERROR_SUCCESS == lRes)
  619. {
  620. LONG_PTR listRes;
  621. // Add szPortData to the combobox
  622. listRes = SendDlgItemMessage( hDlg,
  623. IDC_PORT_LIST,
  624. CB_ADDSTRING,
  625. 0,
  626. (LPARAM)szPortData);
  627. /*
  628. * this item matches the currentPort, so select it.
  629. */
  630. if (0 ==_tcsicmp( szPortData, g_szCurrentPort) &&
  631. CB_ERR != listRes &&
  632. CB_ERRSPACE != listRes)
  633. {
  634. if( CB_ERR != SendDlgItemMessage( hDlg,
  635. IDC_PORT_LIST,
  636. CB_SETCURSEL,
  637. listRes,0) )
  638. {
  639. // the combobox is enabled based on whether or not
  640. // a port is selected so if the CB_SETCURSEL call
  641. // is successful, we should set the port state accordingly
  642. g_portState = ePortSelected;
  643. }
  644. }
  645. } // RegQueryValueEx
  646. } // RegEnumValue
  647. dwIndex++;
  648. } // while loop
  649. // If I haven't already set the current selection then
  650. // default to the 0th item in the combobox and set the
  651. // g_portState to indicate that we have a selection
  652. if( g_portState != ePortSelected )
  653. {
  654. if( CB_ERR != SendDlgItemMessage( hDlg,
  655. IDC_PORT_LIST,
  656. CB_SETCURSEL,
  657. 0,0) )
  658. {
  659. // the combobox is enabled based on whether or not
  660. // a port is selected so if the CB_SETCURSEL call
  661. // is successful, we should set the port state accordingly
  662. g_portState = ePortSelected;
  663. }
  664. }
  665. RegCloseKey(hkResult);
  666. }
  667. configPortList( hDlg);
  668. }
  669. /*
  670. * void handleVendorList (HWND hDlg,
  671. * UINT uMsg,
  672. * WPARAM wParam,
  673. * LPARAM lParam);
  674. *
  675. * Description: handles messages specific to the Vendor List control
  676. *
  677. * Additional Information:
  678. *
  679. * Parameters:
  680. *
  681. * HWND hDlg :- Handle to dialog box
  682. *
  683. * UINT uMsg :- message ID
  684. *
  685. * WPARAM wParam :- Specifies additional message-specific information.
  686. *
  687. * LPARAM lParam :- Specifies additional message-specific information.
  688. *
  689. * Return Value: none
  690. */
  691. void handleVendorList(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  692. {
  693. switch (HIWORD(wParam))
  694. {
  695. case CBN_SELCHANGE://CBN_CLOSEUP:
  696. {
  697. LONG_PTR lVendorRes;
  698. TCHAR szVendorName[MAX_PATH] = _T("");
  699. /*
  700. * Get the user's selection
  701. */
  702. lVendorRes = SendDlgItemMessage( hDlg,
  703. IDC_VENDOR_LIST,
  704. CB_GETCURSEL,
  705. 0,0);
  706. if (CB_ERR != lVendorRes)
  707. {
  708. lVendorRes = SendDlgItemMessage( hDlg,
  709. IDC_VENDOR_LIST,
  710. CB_GETLBTEXT,
  711. lVendorRes,
  712. (LPARAM)szVendorName);
  713. if (CB_ERR != lVendorRes)
  714. {
  715. /*
  716. * if its different from the current value, affect a change
  717. */
  718. _tcscpy(g_szCurrentVendor, szVendorName);
  719. /*
  720. * set new vendor state
  721. */
  722. if (0 == _tcsicmp(szVendorName,g_szNoUPSVendor))
  723. {
  724. g_vendorState = eVendorNone;
  725. }
  726. else
  727. {
  728. if (0 == _tcsicmp(szVendorName,g_szOtherUPSVendor))
  729. {
  730. g_vendorState = eVendorGeneric;
  731. }
  732. else
  733. {
  734. g_vendorState = eVendorSelected;
  735. }
  736. }
  737. /*
  738. * force user to select a new model
  739. */
  740. _tcscpy(g_szCurrentModel, _T(""));
  741. /*
  742. * disable the model and port lists
  743. * forcing the user to reselect them
  744. */
  745. updateModelList(hDlg);
  746. configPortList( hDlg);
  747. }
  748. }
  749. configFinishButton( hDlg);
  750. break;
  751. }
  752. }
  753. }
  754. /*
  755. * void handleModelList (HWND hDlg,
  756. * UINT uMsg,
  757. * WPARAM wParam,
  758. * LPARAM lParam);
  759. *
  760. * Description: handles messages specific to the Model list control
  761. *
  762. * Additional Information:
  763. *
  764. * Parameters:
  765. *
  766. * HWND hDlg :- Handle to dialog box
  767. *
  768. * UINT uMsg :- message ID
  769. *
  770. * WPARAM wParam :- Specifies additional message-specific information.
  771. *
  772. * LPARAM lParam :- Specifies additional message-specific information.
  773. *
  774. * Return Value: none
  775. */
  776. void handleModelList(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  777. {
  778. LONG_PTR lModelRes;
  779. switch (HIWORD(wParam))
  780. {
  781. case LBN_SETFOCUS:
  782. {
  783. /*
  784. * focus has come into the list, make sure
  785. * selected item is visible
  786. */
  787. lModelRes = SendDlgItemMessage( hDlg,
  788. IDC_MODEL_LIST,
  789. LB_GETCURSEL,
  790. 0,0);
  791. if (LB_ERR != lModelRes)
  792. {
  793. lModelRes = SendDlgItemMessage( hDlg,
  794. IDC_MODEL_LIST,
  795. LB_SETTOPINDEX,
  796. lModelRes,
  797. 0);
  798. }
  799. break;
  800. }
  801. case LBN_DBLCLK:
  802. case LBN_SELCHANGE:
  803. {
  804. TCHAR szModelName[MAX_PATH] = _T("");
  805. /*
  806. * get the user selection
  807. */
  808. lModelRes = SendDlgItemMessage( hDlg,
  809. IDC_MODEL_LIST,
  810. LB_GETCURSEL,
  811. 0,0);
  812. if (LB_ERR != lModelRes)
  813. {
  814. lModelRes = SendDlgItemMessage( hDlg,
  815. IDC_MODEL_LIST,
  816. LB_GETTEXT,
  817. lModelRes,
  818. (LPARAM)szModelName);
  819. if (LB_ERR != lModelRes)
  820. {
  821. _tcscpy( g_szCurrentModel, szModelName);
  822. /*
  823. * set new model state
  824. */
  825. if (0==_tcsicmp( szModelName,g_szCustomUPSModel))
  826. {
  827. g_modelState = eModelCustom;
  828. }
  829. else
  830. {
  831. g_modelState = eModelSelected;
  832. }
  833. /*
  834. * enable port selection in the event that
  835. * changing Vendors disabled the port selector
  836. */
  837. configPortList( hDlg);
  838. }
  839. }
  840. configFinishButton( hDlg);
  841. break;
  842. }
  843. }
  844. }
  845. /*
  846. * void handlePortList (HWND hDlg,
  847. * UINT uMsg,
  848. * WPARAM wParam,
  849. * LPARAM lParam);
  850. *
  851. * Description: handles messages specific to the Port list control
  852. *
  853. * Additional Information:
  854. *
  855. * Parameters:
  856. *
  857. * HWND hDlg :- Handle to dialog box
  858. *
  859. * UINT uMsg :- message ID
  860. *
  861. * WPARAM wParam :- Specifies additional message-specific information.
  862. *
  863. * LPARAM lParam :- Specifies additional message-specific information.
  864. *
  865. * Return Value: none
  866. */
  867. void handlePortList(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  868. {
  869. switch (HIWORD(wParam))
  870. {
  871. case CBN_SELCHANGE: //CBN_CLOSEUP:
  872. {
  873. LONG_PTR lPortRes;
  874. TCHAR szPortName[MAX_PATH] = _T("");
  875. /*
  876. * get the user selection
  877. */
  878. lPortRes = SendDlgItemMessage( hDlg,
  879. IDC_PORT_LIST,
  880. CB_GETCURSEL,
  881. 0,0);
  882. if (CB_ERR != lPortRes)
  883. {
  884. lPortRes = SendDlgItemMessage( hDlg,
  885. IDC_PORT_LIST,
  886. CB_GETLBTEXT,
  887. lPortRes,
  888. (LPARAM)szPortName);
  889. if (CB_ERR != lPortRes)
  890. {
  891. _tcscpy( g_szCurrentPort, szPortName);
  892. /*
  893. * set port state
  894. */
  895. g_portState = ePortSelected;
  896. }
  897. }
  898. configFinishButton( hDlg);
  899. break;
  900. }
  901. }
  902. }
  903. /*
  904. * void setServiceData (HWND hDlg);
  905. *
  906. * Description: utility to configure the registry entries that
  907. * contain info used by the UPS service
  908. *
  909. * Additional Information:
  910. *
  911. * Parameters:
  912. *
  913. * HWND hDlg :- Handle to dialog box
  914. *
  915. * Return Value: none
  916. */
  917. void setServiceData(HWND hDlg)
  918. {
  919. TCHAR szModelEntry[MAX_PATH] = _T("");
  920. DWORD dwTmpOpts = 0;
  921. LPTSTR lpTmpDLL = _T("");
  922. /*
  923. * check that a vendor and model have been selected. If None
  924. * were selected, that is considered a bad model.. and this
  925. * check will fail, however, Custom/Generic is a valid selection
  926. */
  927. if ((eModelSelected == g_modelState &&
  928. eVendorSelected == g_vendorState)||
  929. (eModelCustom == g_modelState &&
  930. eVendorGeneric == g_vendorState))
  931. {
  932. /*
  933. * If the custom UPS model is selected, don't bother
  934. * reading the registry entry. Custom can only be simple
  935. * signalling and the options come from the custom dialog
  936. */
  937. if (eModelCustom == g_modelState)
  938. {
  939. dwTmpOpts = g_dwCurrentCustomOptions;
  940. }
  941. else
  942. {
  943. HKEY hkResult;
  944. TCHAR szVendorKey[MAX_PATH] = _T("");
  945. wsprintf( szVendorKey, _T("%s\\%s"), UPS_SERVICE_ROOT, g_szCurrentVendor);
  946. if (ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE,
  947. szVendorKey,
  948. 0,
  949. KEY_QUERY_VALUE,
  950. &hkResult))
  951. {
  952. LONG lRes;
  953. DWORD dwValueLen;
  954. DWORD dwValueType;
  955. dwValueLen = sizeof(szModelEntry); //BYTES HERE, NOT CHARS
  956. lRes = RegQueryValueEx(
  957. hkResult, // handle to key to query
  958. g_szCurrentModel, // address of name of value to query
  959. NULL, // reserved
  960. &dwValueType, // address of buffer for value type
  961. (LPBYTE)szModelEntry, // address of data buffer
  962. &dwValueLen // address of data buffer size
  963. );
  964. if ((ERROR_SUCCESS == lRes) &&
  965. (0 != dwValueLen))
  966. {
  967. LPTSTR lpColon = NULL;
  968. /*
  969. * seperate the DLL from the options data
  970. */
  971. if (NULL != (lpColon = _tcschr( szModelEntry, (TCHAR)';')))
  972. {
  973. *lpColon = (TCHAR)'\0';
  974. lpColon++;
  975. lpTmpDLL = lpColon;
  976. }
  977. /*
  978. * convert options data from string to int
  979. * WARNING:
  980. * wscanf doesn't work
  981. * _tscanf doesn't work
  982. * swscanf does
  983. * _stscanf does
  984. */
  985. if (swscanf( szModelEntry, _T("%x"), &dwTmpOpts) == 0) {
  986. // options could not be converted properly, set back to previous value
  987. dwTmpOpts = g_dwCurrentCustomOptions;
  988. }
  989. }
  990. RegCloseKey( hkResult);
  991. }
  992. }
  993. }
  994. /*
  995. * Move values into the registry
  996. */
  997. g_dwCurrentOptions = dwTmpOpts;
  998. _tcscpy(g_szCurrentServiceDLL, lpTmpDLL);
  999. }
  1000. /*
  1001. * void configPortList (HWND hDlg);
  1002. *
  1003. * Description: utility to configure port selection list ctrl
  1004. *
  1005. * Additional Information:
  1006. *
  1007. * Parameters:
  1008. *
  1009. * HWND hDlg :- Handle to dialog box
  1010. *
  1011. * Return Value: none
  1012. */
  1013. void configPortList(HWND hDlg)
  1014. {
  1015. /*
  1016. * enable the Port selection list, if
  1017. * 1) there is a valid vendor/model selection
  1018. * this includes Custom/Generic but NOT (none)
  1019. * 2) there is a port or some type specified
  1020. */
  1021. if (((eModelSelected == g_modelState &&
  1022. eVendorSelected == g_vendorState)||
  1023. (eModelCustom == g_modelState &&
  1024. eVendorGeneric == g_vendorState)) &&
  1025. ePortUnknown != g_portState)
  1026. {
  1027. EnableWindow( GetDlgItem( hDlg, IDC_PORT_LIST), TRUE);
  1028. EnableWindow( GetDlgItem( hDlg, IDC_PORT_TEXT), TRUE);
  1029. }
  1030. else
  1031. {
  1032. EnableWindow( GetDlgItem( hDlg, IDC_PORT_LIST), FALSE);
  1033. EnableWindow( GetDlgItem( hDlg, IDC_PORT_TEXT), FALSE);
  1034. }
  1035. }
  1036. /*
  1037. * void configModelList (HWND hDlg);
  1038. *
  1039. * Description: utility to configure model selection list ctrl
  1040. *
  1041. * Additional Information:
  1042. *
  1043. * Parameters:
  1044. *
  1045. * HWND hDlg :- Handle to dialog box
  1046. *
  1047. * Return Value: none
  1048. */
  1049. void configModelList(HWND hDlg)
  1050. {
  1051. LRESULT lModelRes;
  1052. /*
  1053. * to enable the model list, there has to
  1054. * be a valid or generic vendor selection (NOT none)
  1055. */
  1056. if ((eVendorGeneric == g_vendorState ||
  1057. eVendorSelected == g_vendorState))
  1058. {
  1059. EnableWindow( GetDlgItem( hDlg, IDC_MODEL_LIST), TRUE);
  1060. EnableWindow( GetDlgItem( hDlg, IDC_MODEL_TEXT), TRUE);
  1061. /*
  1062. * make sure the selected item is visible
  1063. */
  1064. lModelRes = SendDlgItemMessage( hDlg,
  1065. IDC_MODEL_LIST,
  1066. LB_GETCURSEL,
  1067. 0,0);
  1068. if (LB_ERR != lModelRes)
  1069. {
  1070. SendDlgItemMessage( hDlg,
  1071. IDC_MODEL_LIST,
  1072. LB_SETTOPINDEX,
  1073. lModelRes,0);
  1074. }
  1075. }
  1076. else
  1077. {
  1078. EnableWindow( GetDlgItem( hDlg, IDC_MODEL_LIST), FALSE);
  1079. EnableWindow( GetDlgItem( hDlg, IDC_MODEL_TEXT), FALSE);
  1080. }
  1081. }
  1082. /*
  1083. * void configFinishButton (HWND hDlg);
  1084. *
  1085. * Description: utility to configure the Finish button ctrl
  1086. *
  1087. * Additional Information:
  1088. *
  1089. * Parameters:
  1090. *
  1091. * HWND hDlg :- Handle to dialog box
  1092. *
  1093. * Return Value: none
  1094. */
  1095. void configFinishButton(HWND hDlg)
  1096. {
  1097. BOOL bFinState = FALSE;
  1098. g_finishButtonState = eFinish; // default
  1099. /*
  1100. * enable Finish if vendor is NONE
  1101. */
  1102. if (eVendorNone == g_vendorState)
  1103. {
  1104. bFinState = TRUE;
  1105. }
  1106. else
  1107. {
  1108. /*
  1109. * If all other fields are valid, enable the button
  1110. */
  1111. if (eVendorUnknown != g_vendorState &&
  1112. eModelUnknown != g_modelState &&
  1113. ePortUnknown != g_portState)
  1114. {
  1115. bFinState = TRUE;
  1116. /*
  1117. * if the vendor/model is other/custom, the text of
  1118. * the button is Next
  1119. */
  1120. if (eVendorGeneric == g_vendorState &&
  1121. eModelCustom == g_modelState)
  1122. {
  1123. g_finishButtonState = eNext;
  1124. }
  1125. }
  1126. }
  1127. /*
  1128. * toggle between the finish and next buttons
  1129. */
  1130. SendDlgItemMessage( hDlg,
  1131. IDB_SELECT_FINISH,
  1132. BM_SETSTYLE,
  1133. eFinish == g_finishButtonState ? BS_DEFPUSHBUTTON:BS_PUSHBUTTON,
  1134. (LPARAM) TRUE);
  1135. //
  1136. // Set the NExt Button's style
  1137. //
  1138. SendDlgItemMessage( hDlg,
  1139. IDB_SELECT_NEXT,
  1140. BM_SETSTYLE,
  1141. eNext == g_finishButtonState ? BS_DEFPUSHBUTTON:BS_PUSHBUTTON,
  1142. (LPARAM) TRUE);
  1143. //
  1144. // Set the default push button's control ID.
  1145. //
  1146. SendMessage ( hDlg,
  1147. DM_SETDEFID,
  1148. eFinish == g_finishButtonState ? IDB_SELECT_FINISH: IDB_SELECT_NEXT,
  1149. 0L);
  1150. ShowWindow( GetDlgItem( hDlg, IDB_SELECT_FINISH),
  1151. (eFinish == g_finishButtonState ? SW_SHOW : SW_HIDE));
  1152. ShowWindow( GetDlgItem( hDlg, IDB_SELECT_NEXT),
  1153. (eFinish == g_finishButtonState ? SW_HIDE : SW_SHOW));
  1154. EnableWindow( GetDlgItem( hDlg, eFinish == g_finishButtonState ?
  1155. IDB_SELECT_FINISH : IDB_SELECT_NEXT), bFinState);
  1156. }