Source code of Windows XP (NT5)
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.

611 lines
13 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. }
  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("0x%x");
  165. const TCHAR * pszMaskDec = _T("%ld");
  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) ? m_dwdw_array.GetSize() : m_dw_array.GetSize());
  222. INT i ;
  223. for ( i = 0 ; i < cMax ; i++ )
  224. {
  225. switch (optionType)
  226. {
  227. case DhcpDWordDWordOption:
  228. if ( ! ConvertValue( m_dwdw_array.GetAt( i ), strValue ) )
  229. {
  230. continue ;
  231. }
  232. break;
  233. default:
  234. if ( ! ConvertValue( m_dw_array.GetAt( i ), strValue ) )
  235. {
  236. continue ;
  237. }
  238. break;
  239. }
  240. m_list_values.AddString( strValue ) ;
  241. }
  242. if ( cFocus >= 0 && cFocus < cMax )
  243. {
  244. m_list_values.SetCurSel( cFocus ) ;
  245. }
  246. if ( bToggleRedraw )
  247. {
  248. m_list_values.SetRedraw( TRUE ) ;
  249. m_list_values.Invalidate() ;
  250. }
  251. }
  252. //
  253. // Handle changes in the dialog
  254. //
  255. void
  256. CDlgBinEd::HandleActivation()
  257. {
  258. INT cSel = m_list_values.GetCurSel();
  259. INT cMax = (m_p_type->QueryValue().QueryDataType() == DhcpDWordDWordOption) ? (INT)m_dwdw_array.GetSize() : (INT)m_dw_array.GetSize() ;
  260. BOOL bModified = m_edit_value.GetWindowTextLength() > 0;
  261. m_button_Up.EnableWindow( cSel >= 1 ) ;
  262. m_button_Down.EnableWindow( cSel + 1 < cMax ) ;
  263. m_butn_add.EnableWindow( bModified ) ;
  264. m_butn_delete.EnableWindow( cSel >= 0 ) ;
  265. m_butn_hex.SetCheck( m_b_decimal == 0 ) ;
  266. m_butn_decimal.SetCheck( m_b_decimal > 0 ) ;
  267. }
  268. void
  269. CDlgBinEd::OnClickedRadioDecimal()
  270. {
  271. m_b_decimal = TRUE ;
  272. Fill() ;
  273. HandleActivation() ;
  274. }
  275. void
  276. CDlgBinEd::OnClickedRadioHex()
  277. {
  278. m_b_decimal = FALSE ;
  279. Fill() ;
  280. HandleActivation() ;
  281. }
  282. void
  283. CDlgBinEd::OnClickedButnAdd()
  284. {
  285. INT cFocus = m_list_values.GetCurSel() ;
  286. DWORD dwValue;
  287. DWORD_DWORD dwdw;
  288. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  289. DWORD dwMask = 0xFFFFFFFF ;
  290. ASSERT(m_p_type);
  291. switch ( optionType )
  292. {
  293. case DhcpBinaryDataOption :
  294. case DhcpEncapsulatedDataOption :
  295. case DhcpByteOption:
  296. dwMask = 0xFF ;
  297. break ;
  298. case DhcpWordOption:
  299. dwMask = 0xFFFF ;
  300. break ;
  301. } // switch
  302. if (optionType == DhcpDWordDWordOption)
  303. {
  304. CString strValue;
  305. m_edit_value.GetWindowText(strValue);
  306. UtilConvertStringToDwordDword(strValue, &dwdw);
  307. }
  308. else
  309. {
  310. if (!FGetCtrlDWordValue(m_edit_value.m_hWnd, &dwValue, 0, dwMask))
  311. return;
  312. }
  313. DWORD err = 0 ;
  314. CATCH_MEM_EXCEPTION
  315. {
  316. if ( cFocus < 0 )
  317. {
  318. cFocus = 0 ;
  319. }
  320. (optionType == DhcpDWordDWordOption) ?
  321. m_dwdw_array.InsertAt( cFocus, dwdw ) : m_dw_array.InsertAt( cFocus, dwValue ) ;
  322. }
  323. END_MEM_EXCEPTION(err)
  324. if ( err )
  325. {
  326. ::DhcpMessageBox( err ) ;
  327. }
  328. else
  329. {
  330. m_b_changed = TRUE ;
  331. }
  332. //
  333. // Refill listbox, update controls. clear the edit control
  334. //
  335. m_edit_value.SetWindowText(_T(""));
  336. Fill( cFocus ) ;
  337. HandleActivation() ;
  338. }
  339. void
  340. CDlgBinEd::OnClickedButnDelete()
  341. {
  342. INT cFocus = m_list_values.GetCurSel() ;
  343. if ( cFocus < 0 )
  344. {
  345. return ;
  346. }
  347. DWORD err = 0 ;
  348. CString strValue ;
  349. DWORD dwValue;
  350. DWORD_DWORD dwdw;
  351. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  352. CATCH_MEM_EXCEPTION
  353. {
  354. if (optionType == DhcpDWordDWordOption)
  355. {
  356. dwdw = m_dwdw_array.GetAt( cFocus ) ;
  357. m_dwdw_array.RemoveAt( cFocus ) ;
  358. ConvertValue( dwdw, strValue ) ;
  359. }
  360. else
  361. {
  362. dwValue = m_dw_array.GetAt( cFocus ) ;
  363. m_dw_array.RemoveAt( cFocus ) ;
  364. ConvertValue( dwValue, strValue ) ;
  365. }
  366. }
  367. END_MEM_EXCEPTION(err)
  368. if ( err )
  369. {
  370. ::DhcpMessageBox( err ) ;
  371. EndDialog( -1 ) ;
  372. }
  373. else
  374. {
  375. m_b_changed = TRUE ;
  376. }
  377. m_edit_value.SetWindowText( strValue ) ;
  378. m_edit_value.SetFocus();
  379. Fill( cFocus ) ;
  380. HandleActivation() ;
  381. }
  382. void
  383. CDlgBinEd::OnClickedButnDown()
  384. {
  385. INT cFocus = m_list_values.GetCurSel() ;
  386. INT cItems = m_list_values.GetCount() ;
  387. if ( cFocus < 0 || cFocus + 1 >= cItems )
  388. {
  389. return ;
  390. }
  391. DWORD dwValue ;
  392. DWORD err = 0 ;
  393. DWORD_DWORD dwdw;
  394. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  395. CATCH_MEM_EXCEPTION
  396. {
  397. if (optionType == DhcpDWordDWordOption)
  398. {
  399. dwdw = m_dwdw_array.GetAt( cFocus ) ;
  400. m_dwdw_array.RemoveAt( cFocus ) ;
  401. m_dwdw_array.InsertAt( cFocus + 1, dwdw ) ;
  402. }
  403. else
  404. {
  405. dwValue = m_dw_array.GetAt( cFocus ) ;
  406. m_dw_array.RemoveAt( cFocus ) ;
  407. m_dw_array.InsertAt( cFocus + 1, dwValue ) ;
  408. }
  409. }
  410. END_MEM_EXCEPTION(err)
  411. if ( err )
  412. {
  413. ::DhcpMessageBox( err ) ;
  414. }
  415. else
  416. {
  417. m_b_changed = TRUE ;
  418. }
  419. Fill( cFocus + 1 ) ;
  420. HandleActivation() ;
  421. }
  422. void
  423. CDlgBinEd::OnClickedButnUp()
  424. {
  425. INT cFocus = m_list_values.GetCurSel() ;
  426. INT cItems = m_list_values.GetCount() ;
  427. if ( cFocus <= 0 )
  428. {
  429. return ;
  430. }
  431. DWORD dwValue ;
  432. DWORD err = 0 ;
  433. DWORD_DWORD dwdw;
  434. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  435. CATCH_MEM_EXCEPTION
  436. {
  437. if (optionType == DhcpDWordDWordOption)
  438. {
  439. dwdw = m_dwdw_array.GetAt( cFocus ) ;
  440. m_dwdw_array.RemoveAt( cFocus ) ;
  441. m_dwdw_array.InsertAt( cFocus - 1, dwdw ) ;
  442. }
  443. else
  444. {
  445. dwValue = m_dw_array.GetAt( cFocus ) ;
  446. m_dw_array.RemoveAt( cFocus ) ;
  447. m_dw_array.InsertAt( cFocus - 1, dwValue ) ;
  448. }
  449. }
  450. END_MEM_EXCEPTION(err)
  451. if ( err )
  452. {
  453. ::DhcpMessageBox( err ) ;
  454. }
  455. else
  456. {
  457. m_b_changed = TRUE ;
  458. }
  459. Fill( cFocus - 1 ) ;
  460. HandleActivation() ;
  461. }
  462. void
  463. CDlgBinEd::OnSelchangeListValues()
  464. {
  465. HandleActivation() ;
  466. }
  467. void
  468. CDlgBinEd::OnOK()
  469. {
  470. DWORD err = 0 ;
  471. if ( m_b_changed )
  472. {
  473. CDhcpOptionValue & cValue = m_p_type->QueryValue() ;
  474. DHCP_OPTION_DATA_TYPE optionType = m_p_type->QueryValue().QueryDataType();
  475. CATCH_MEM_EXCEPTION
  476. {
  477. if (optionType == DhcpDWordDWordOption)
  478. {
  479. cValue.SetUpperBound( (INT)m_dwdw_array.GetSize() ) ;
  480. for ( int i = 0 ; i < m_dwdw_array.GetSize() ; i++ )
  481. {
  482. cValue.SetDwordDword( m_dwdw_array.GetAt( i ), i ) ;
  483. }
  484. }
  485. else
  486. {
  487. cValue.SetUpperBound( (INT)m_dw_array.GetSize() ) ;
  488. for ( int i = 0 ; i < m_dw_array.GetSize() ; i++ )
  489. {
  490. cValue.SetNumber( m_dw_array.GetAt( i ), i ) ;
  491. }
  492. }
  493. }
  494. END_MEM_EXCEPTION( err ) ;
  495. if ( err )
  496. {
  497. ::DhcpMessageBox( err ) ;
  498. EndDialog( -1 ) ;
  499. }
  500. else
  501. {
  502. m_p_type->SetDirty() ;
  503. CBaseDialog::OnOK();
  504. }
  505. }
  506. else
  507. {
  508. OnCancel() ;
  509. }
  510. }
  511. void
  512. CDlgBinEd::OnCancel()
  513. {
  514. CBaseDialog::OnCancel();
  515. }
  516. void
  517. CDlgBinEd::OnChangeEditValue()
  518. {
  519. HandleActivation() ;
  520. }