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.

661 lines
15 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows NT **/
  3. /** Copyright(c) Microsoft Corporation, 1991 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. DLGBINED
  7. Binary Editor dialog
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "dlgbined.h"
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CDlgBinEd dialog
  18. CDlgBinEd::CDlgBinEd
  19. (
  20. CDhcpOption * pdhcType,
  21. DHCP_OPTION_SCOPE_TYPE dhcScopeType,
  22. CWnd* pParent /*=NULL*/
  23. )
  24. : CBaseDialog(CDlgBinEd::IDD, pParent),
  25. m_p_type( pdhcType ),
  26. m_b_decimal( TRUE ),
  27. m_b_changed( FALSE ),
  28. m_option_type( dhcScopeType )
  29. {
  30. //{{AFX_DATA_INIT(CDlgBinEd)
  31. //}}AFX_DATA_INIT
  32. ASSERT( m_p_type != NULL ) ;
  33. }
  34. void
  35. CDlgBinEd::DoDataExchange
  36. (
  37. CDataExchange* pDX
  38. )
  39. {
  40. CBaseDialog::DoDataExchange(pDX);
  41. //{{AFX_DATA_MAP(CDlgBinEd)
  42. DDX_Control(pDX, IDC_RADIO_HEX, m_butn_hex);
  43. DDX_Control(pDX, IDC_RADIO_DECIMAL, m_butn_decimal);
  44. DDX_Control(pDX, IDC_STATIC_UNIT_SIZE, m_static_unit_size);
  45. DDX_Control(pDX, IDC_STATIC_OPTION_NAME, m_static_option_name);
  46. DDX_Control(pDX, IDC_STATIC_APPLICATION, m_static_application);
  47. DDX_Control(pDX, IDC_LIST_VALUES, m_list_values);
  48. DDX_Control(pDX, IDC_EDIT_VALUE, m_edit_value);
  49. DDX_Control(pDX, IDC_BUTN_DELETE, m_butn_delete);
  50. DDX_Control(pDX, IDC_BUTN_ADD, m_butn_add);
  51. DDX_Control(pDX, IDC_BUTN_UP, m_button_Up);
  52. DDX_Control(pDX, IDC_BUTN_DOWN, m_button_Down);
  53. //}}AFX_DATA_MAP
  54. }
  55. BEGIN_MESSAGE_MAP(CDlgBinEd, CBaseDialog)
  56. //{{AFX_MSG_MAP(CDlgBinEd)
  57. ON_BN_CLICKED(IDC_RADIO_DECIMAL, OnClickedRadioDecimal)
  58. ON_BN_CLICKED(IDC_RADIO_HEX, OnClickedRadioHex)
  59. ON_BN_CLICKED(IDC_BUTN_ADD, OnClickedButnAdd)
  60. ON_BN_CLICKED(IDC_BUTN_DELETE, OnClickedButnDelete)
  61. ON_BN_CLICKED(IDC_BUTN_DOWN, OnClickedButnDown)
  62. ON_BN_CLICKED(IDC_BUTN_UP, OnClickedButnUp)
  63. ON_LBN_SELCHANGE(IDC_LIST_VALUES, OnSelchangeListValues)
  64. ON_EN_CHANGE(IDC_EDIT_VALUE, OnChangeEditValue)
  65. //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CDlgBinEd message handlers
  69. BOOL
  70. CDlgBinEd::OnInitDialog()
  71. {
  72. CBaseDialog::OnInitDialog();
  73. CString str ;
  74. CString strNum ;
  75. DWORD err = 0 ;
  76. int cUnitSize = 1 ;
  77. int cStrId = m_option_type == DhcpDefaultOptions
  78. ? IDS_INFO_TITLE_DEFAULT_OPTIONS
  79. : (m_option_type == DhcpGlobalOptions
  80. ? IDS_INFO_TITLE_GLOBAL_OPTIONS
  81. : IDS_INFO_TITLE_SCOPE_OPTIONS ) ;
  82. switch ( m_p_type->QueryValue().QueryDataType() )
  83. {
  84. case DhcpWordOption:
  85. cUnitSize = 2 ;
  86. break ;
  87. case DhcpDWordOption:
  88. cUnitSize = 4 ;
  89. break ;
  90. case DhcpDWordDWordOption:
  91. cUnitSize = 8 ;
  92. break ;
  93. }
  94. CATCH_MEM_EXCEPTION
  95. {
  96. strNum.Format(_T("%d"), cUnitSize);
  97. m_static_unit_size.SetWindowText( strNum ) ;
  98. m_static_option_name.SetWindowText( m_p_type->QueryName() ) ;
  99. str.LoadString( cStrId ) ;
  100. m_static_application.SetWindowText( str ) ;
  101. //
  102. // Fill the internal list from the current value.
  103. //
  104. FillArray() ;
  105. Fill( 0, TRUE ) ;
  106. //
  107. // Set focus on the new value edit control.
  108. //
  109. m_edit_value.SetFocus() ;
  110. m_edit_value.SetModify( FALSE );
  111. //
  112. // Fiddle with the buttons.
  113. //
  114. HandleActivation() ;
  115. }
  116. END_MEM_EXCEPTION(err)
  117. if ( err )
  118. {
  119. ::DhcpMessageBox( err ) ;
  120. EndDialog(-1);
  121. }
  122. return FALSE; // return TRUE unless you set the focus to a control
  123. } // CDlgBinEd::OnInitDialog()
  124. //
  125. // Convert the existing values into an array for dialog manipualation
  126. //
  127. void
  128. CDlgBinEd::FillArray()
  129. {
  130. //
  131. // Fill the internal list from the current value.
  132. //
  133. INT cMax;
  134. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  135. switch ( optionType )
  136. {
  137. case DhcpBinaryDataOption :
  138. case DhcpEncapsulatedDataOption :
  139. cMax = (INT)m_p_type->QueryValue().QueryBinaryArray()->GetSize() ;
  140. break;
  141. default:
  142. cMax = (INT)m_p_type->QueryValue().QueryUpperBound() ;
  143. break;
  144. }
  145. for ( INT i = 0 ; i < cMax ; i++ )
  146. {
  147. if (optionType == DhcpDWordDWordOption)
  148. {
  149. m_dwdw_array.SetAtGrow( i, m_p_type->QueryValue().QueryDwordDword( i ) ) ;
  150. }
  151. else
  152. {
  153. m_dw_array.SetAtGrow( i, (DWORD) m_p_type->QueryValue().QueryNumber( i ) ) ;
  154. }
  155. }
  156. }
  157. BOOL
  158. CDlgBinEd :: ConvertValue
  159. (
  160. DWORD dwValue,
  161. CString & strValue
  162. )
  163. {
  164. const TCHAR * pszMaskHex = _T("%#x");
  165. const TCHAR * pszMaskDec = _T("%u");
  166. const TCHAR * pszMask = m_b_decimal
  167. ? pszMaskDec
  168. : pszMaskHex;
  169. TCHAR szNum [ STRING_LENGTH_MAX ] ;
  170. DWORD err = 0 ;
  171. CATCH_MEM_EXCEPTION
  172. {
  173. ::wsprintf( szNum, pszMask, dwValue ) ;
  174. strValue = szNum ;
  175. }
  176. END_MEM_EXCEPTION(err)
  177. if ( err )
  178. {
  179. ::DhcpMessageBox( err ) ;
  180. EndDialog( -1 ) ;
  181. }
  182. return err == 0 ;
  183. }
  184. BOOL
  185. CDlgBinEd :: ConvertValue
  186. (
  187. DWORD_DWORD dwdwValue,
  188. CString & strValue
  189. )
  190. {
  191. DWORD err = 0 ;
  192. CATCH_MEM_EXCEPTION
  193. {
  194. ::UtilConvertDwordDwordToString(&dwdwValue, &strValue, m_b_decimal);
  195. }
  196. END_MEM_EXCEPTION(err)
  197. if ( err )
  198. {
  199. ::DhcpMessageBox( err ) ;
  200. EndDialog( -1 ) ;
  201. }
  202. return err == 0 ;
  203. }
  204. //
  205. // Fill the list box
  206. //
  207. void
  208. CDlgBinEd::Fill
  209. (
  210. INT cFocus,
  211. BOOL bToggleRedraw
  212. )
  213. {
  214. if ( bToggleRedraw )
  215. {
  216. m_list_values.SetRedraw( FALSE ) ;
  217. }
  218. m_list_values.ResetContent() ;
  219. CString strValue ;
  220. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  221. INT cMax = (INT)((optionType == DhcpDWordDWordOption)
  222. ? m_dwdw_array.GetSize()
  223. : m_dw_array.GetSize());
  224. INT i ;
  225. DWORD_DWORD LongIntValue;
  226. DWORD LongValue;
  227. for ( i = 0 ; i < cMax ; i++ )
  228. {
  229. switch (optionType)
  230. {
  231. case DhcpDWordDWordOption:
  232. if ( ! ConvertValue( m_dwdw_array.GetAt( i ), strValue ) )
  233. {
  234. continue ;
  235. }
  236. break;
  237. default:
  238. if ( ! ConvertValue( m_dw_array.GetAt( i ), strValue ) )
  239. {
  240. continue ;
  241. }
  242. break;
  243. }
  244. m_list_values.AddString( strValue ) ;
  245. }
  246. if ( cFocus >= 0 && cFocus < cMax )
  247. {
  248. m_list_values.SetCurSel( cFocus ) ;
  249. }
  250. else {
  251. m_list_values.SetCurSel( 0 );
  252. }
  253. if ( bToggleRedraw )
  254. {
  255. m_list_values.SetRedraw( TRUE ) ;
  256. m_list_values.Invalidate() ;
  257. }
  258. // convert the value being edited
  259. m_edit_value.GetWindowText( strValue );
  260. if ( !strValue.IsEmpty()) {
  261. DWORD maxVal;
  262. if ( DhcpByteOption == optionType ) {
  263. maxVal = 0xff;
  264. }
  265. else if ( DhcpWordOption == optionType ) {
  266. maxVal = 0xffff;
  267. }
  268. else {
  269. maxVal = 0xffffffff;
  270. }
  271. if ( DhcpDWordDWordOption == optionType ) {
  272. UtilConvertStringToDwordDword( strValue, &LongIntValue );
  273. UtilConvertDwordDwordToString( &LongIntValue, &strValue,
  274. m_b_decimal );
  275. }
  276. else {
  277. FGetCtrlDWordValue( m_edit_value.GetSafeHwnd(), &LongValue,
  278. 0, maxVal );
  279. ConvertValue( LongValue, strValue );
  280. }
  281. m_edit_value.SetWindowText( strValue );
  282. } // if
  283. } // CDlgBinEd::Fill()
  284. //
  285. // Handle changes in the dialog
  286. //
  287. void
  288. CDlgBinEd::HandleActivation()
  289. {
  290. INT cSel = m_list_values.GetCurSel();
  291. INT cMax = (m_p_type->QueryValue().QueryDataType() == DhcpDWordDWordOption)
  292. ? (INT)m_dwdw_array.GetSize()
  293. : (INT)m_dw_array.GetSize() ;
  294. BOOL bModified = m_edit_value.GetWindowTextLength() > 0;
  295. m_button_Up.EnableWindow( cSel >= 1 ) ;
  296. m_button_Down.EnableWindow( cSel + 1 < cMax ) ;
  297. m_butn_add.EnableWindow( bModified ) ;
  298. m_butn_delete.EnableWindow( cSel >= 0 ) ;
  299. m_butn_hex.SetCheck( m_b_decimal == 0 ) ;
  300. m_butn_decimal.SetCheck( m_b_decimal > 0 ) ;
  301. // check if the focus in on a disabled control
  302. // If yes, put the focus back to list box
  303. if ( !::IsWindowEnabled( ::GetFocus())) {
  304. m_list_values.SetFocus();
  305. }
  306. } // CDlgBinEd::HandleActivation()
  307. void
  308. CDlgBinEd::OnClickedRadioDecimal()
  309. {
  310. m_b_decimal = TRUE ;
  311. Fill() ;
  312. HandleActivation() ;
  313. }
  314. void
  315. CDlgBinEd::OnClickedRadioHex()
  316. {
  317. m_b_decimal = FALSE ;
  318. Fill() ;
  319. HandleActivation() ;
  320. }
  321. void
  322. CDlgBinEd::OnClickedButnAdd()
  323. {
  324. INT cFocus = m_list_values.GetCurSel() ;
  325. DWORD dwValue;
  326. DWORD_DWORD dwdw;
  327. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  328. DWORD dwMask = 0xFFFFFFFF ;
  329. ASSERT(m_p_type);
  330. switch ( optionType )
  331. {
  332. case DhcpBinaryDataOption :
  333. case DhcpEncapsulatedDataOption :
  334. case DhcpByteOption:
  335. dwMask = 0xFF ;
  336. break ;
  337. case DhcpWordOption:
  338. dwMask = 0xFFFF ;
  339. break ;
  340. } // switch
  341. if (optionType == DhcpDWordDWordOption)
  342. {
  343. CString strValue;
  344. m_edit_value.GetWindowText(strValue);
  345. UtilConvertStringToDwordDword(strValue, &dwdw);
  346. }
  347. else
  348. {
  349. if (!FGetCtrlDWordValue(m_edit_value.m_hWnd, &dwValue, 0, dwMask))
  350. return;
  351. }
  352. DWORD err = 0 ;
  353. CATCH_MEM_EXCEPTION
  354. {
  355. if ( cFocus < 0 )
  356. {
  357. cFocus = 0 ;
  358. }
  359. (optionType == DhcpDWordDWordOption) ?
  360. m_dwdw_array.InsertAt( cFocus, dwdw ) : m_dw_array.InsertAt( cFocus, dwValue ) ;
  361. }
  362. END_MEM_EXCEPTION(err)
  363. if ( err )
  364. {
  365. ::DhcpMessageBox( err ) ;
  366. }
  367. else
  368. {
  369. m_b_changed = TRUE ;
  370. }
  371. //
  372. // Refill listbox, update controls. clear the edit control
  373. //
  374. m_edit_value.SetWindowText(_T(""));
  375. m_edit_value.SetFocus();
  376. Fill( cFocus ) ;
  377. HandleActivation() ;
  378. } // CDlgBinEd::OnClickedButnAdd()
  379. void
  380. CDlgBinEd::OnClickedButnDelete()
  381. {
  382. INT cFocus = m_list_values.GetCurSel() ;
  383. if ( cFocus < 0 )
  384. {
  385. return ;
  386. }
  387. DWORD err = 0 ;
  388. CString strValue ;
  389. DWORD dwValue;
  390. DWORD_DWORD dwdw;
  391. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  392. CATCH_MEM_EXCEPTION
  393. {
  394. if (optionType == DhcpDWordDWordOption)
  395. {
  396. dwdw = m_dwdw_array.GetAt( cFocus ) ;
  397. m_dwdw_array.RemoveAt( cFocus ) ;
  398. ConvertValue( dwdw, strValue ) ;
  399. }
  400. else
  401. {
  402. dwValue = m_dw_array.GetAt( cFocus ) ;
  403. m_dw_array.RemoveAt( cFocus ) ;
  404. ConvertValue( dwValue, strValue ) ;
  405. }
  406. }
  407. END_MEM_EXCEPTION(err)
  408. if ( err )
  409. {
  410. ::DhcpMessageBox( err ) ;
  411. EndDialog( -1 ) ;
  412. }
  413. else
  414. {
  415. m_b_changed = TRUE ;
  416. }
  417. cFocus--;
  418. if ( cFocus < 0 ) {
  419. cFocus = 0;
  420. }
  421. m_edit_value.SetWindowText( strValue ) ;
  422. m_edit_value.SetFocus();
  423. Fill( cFocus ) ;
  424. HandleActivation() ;
  425. }
  426. void
  427. CDlgBinEd::OnClickedButnDown()
  428. {
  429. INT cFocus = m_list_values.GetCurSel() ;
  430. INT cItems = m_list_values.GetCount() ;
  431. if ( cFocus < 0 || cFocus + 1 >= cItems )
  432. {
  433. return ;
  434. }
  435. DWORD dwValue ;
  436. DWORD err = 0 ;
  437. DWORD_DWORD dwdw;
  438. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  439. CATCH_MEM_EXCEPTION
  440. {
  441. if (optionType == DhcpDWordDWordOption)
  442. {
  443. dwdw = m_dwdw_array.GetAt( cFocus ) ;
  444. m_dwdw_array.RemoveAt( cFocus ) ;
  445. m_dwdw_array.InsertAt( cFocus + 1, dwdw ) ;
  446. }
  447. else
  448. {
  449. dwValue = m_dw_array.GetAt( cFocus ) ;
  450. m_dw_array.RemoveAt( cFocus ) ;
  451. m_dw_array.InsertAt( cFocus + 1, dwValue ) ;
  452. }
  453. }
  454. END_MEM_EXCEPTION(err)
  455. if ( err )
  456. {
  457. ::DhcpMessageBox( err ) ;
  458. }
  459. else
  460. {
  461. m_b_changed = TRUE ;
  462. }
  463. Fill( cFocus + 1 ) ;
  464. HandleActivation() ;
  465. }
  466. void
  467. CDlgBinEd::OnClickedButnUp()
  468. {
  469. INT cFocus = m_list_values.GetCurSel() ;
  470. INT cItems = m_list_values.GetCount() ;
  471. if ( cFocus <= 0 )
  472. {
  473. return ;
  474. }
  475. DWORD dwValue ;
  476. DWORD err = 0 ;
  477. DWORD_DWORD dwdw;
  478. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  479. CATCH_MEM_EXCEPTION
  480. {
  481. if (optionType == DhcpDWordDWordOption)
  482. {
  483. dwdw = m_dwdw_array.GetAt( cFocus ) ;
  484. m_dwdw_array.RemoveAt( cFocus ) ;
  485. m_dwdw_array.InsertAt( cFocus - 1, dwdw ) ;
  486. }
  487. else
  488. {
  489. dwValue = m_dw_array.GetAt( cFocus ) ;
  490. m_dw_array.RemoveAt( cFocus ) ;
  491. m_dw_array.InsertAt( cFocus - 1, dwValue ) ;
  492. }
  493. }
  494. END_MEM_EXCEPTION(err)
  495. if ( err )
  496. {
  497. ::DhcpMessageBox( err ) ;
  498. }
  499. else
  500. {
  501. m_b_changed = TRUE ;
  502. }
  503. Fill( cFocus - 1 ) ;
  504. HandleActivation() ;
  505. }
  506. void
  507. CDlgBinEd::OnSelchangeListValues()
  508. {
  509. HandleActivation() ;
  510. }
  511. void
  512. CDlgBinEd::OnOK()
  513. {
  514. DWORD err = 0 ;
  515. if ( m_b_changed )
  516. {
  517. CDhcpOptionValue & cValue = m_p_type->QueryValue() ;
  518. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  519. CATCH_MEM_EXCEPTION
  520. {
  521. if (optionType == DhcpDWordDWordOption)
  522. {
  523. cValue.SetUpperBound( (INT)m_dwdw_array.GetSize() ) ;
  524. for ( int i = 0 ; i < m_dwdw_array.GetSize() ; i++ )
  525. {
  526. cValue.SetDwordDword( m_dwdw_array.GetAt( i ), i ) ;
  527. }
  528. }
  529. else
  530. {
  531. cValue.SetUpperBound( (INT)m_dw_array.GetSize() ) ;
  532. for ( int i = 0 ; i < m_dw_array.GetSize() ; i++ )
  533. {
  534. cValue.SetNumber( m_dw_array.GetAt( i ), i ) ;
  535. }
  536. }
  537. }
  538. END_MEM_EXCEPTION( err ) ;
  539. if ( err )
  540. {
  541. ::DhcpMessageBox( err ) ;
  542. EndDialog( -1 ) ;
  543. }
  544. else
  545. {
  546. m_p_type->SetDirty() ;
  547. CBaseDialog::OnOK();
  548. }
  549. }
  550. else
  551. {
  552. OnCancel() ;
  553. }
  554. }
  555. void
  556. CDlgBinEd::OnCancel()
  557. {
  558. CBaseDialog::OnCancel();
  559. }
  560. void
  561. CDlgBinEd::OnChangeEditValue()
  562. {
  563. HandleActivation() ;
  564. }