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.

428 lines
9.8 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1995 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. dhcpipar.cpp
  7. IP Array dialog
  8. FILE HISTORY:
  9. */
  10. #include "stdafx.h"
  11. #include "dlgiparr.h"
  12. #ifdef _DEBUG
  13. #undef THIS_FILE
  14. static char BASED_CODE THIS_FILE[] = __FILE__;
  15. #endif
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CDhcpIpArrayDlg dialog
  18. CDhcpIpArrayDlg::CDhcpIpArrayDlg(
  19. CDhcpOption * pdhcType,
  20. DHCP_OPTION_SCOPE_TYPE dhcScopeType,
  21. CWnd* pParent /*=NULL*/
  22. )
  23. : CBaseDialog(CDhcpIpArrayDlg::IDD, pParent),
  24. m_p_type( pdhcType ),
  25. m_option_type( dhcScopeType )
  26. {
  27. //{{AFX_DATA_INIT(CDhcpIpArrayDlg)
  28. //}}AFX_DATA_INIT
  29. ASSERT( m_p_type != NULL ) ;
  30. }
  31. void
  32. CDhcpIpArrayDlg::DoDataExchange(
  33. CDataExchange* pDX
  34. )
  35. {
  36. CBaseDialog::DoDataExchange(pDX);
  37. //{{AFX_DATA_MAP(CDhcpIpArrayDlg)
  38. DDX_Control(pDX, IDC_BUTN_RESOLVE, m_butn_resolve);
  39. DDX_Control(pDX, IDC_STATIC_OPTION_NAME, m_static_option_name);
  40. DDX_Control(pDX, IDC_STATIC_APPLICATION, m_static_application);
  41. DDX_Control(pDX, IDC_LIST_IP_ADDRS, m_list_ip_addrs);
  42. DDX_Control(pDX, IDC_EDIT_SERVER_NAME, m_edit_server);
  43. DDX_Control(pDX, IDC_BUTN_ADD, m_butn_add);
  44. DDX_Control(pDX, IDC_BUTN_DELETE, m_butn_delete);
  45. DDX_Control(pDX, IDC_BUTN_UP, m_button_Up);
  46. DDX_Control(pDX, IDC_BUTN_DOWN, m_button_Down);
  47. //}}AFX_DATA_MAP
  48. // The IP address custom control
  49. DDX_Control(pDX, IDC_IPADDR_NEW, m_ipa_new );
  50. }
  51. BEGIN_MESSAGE_MAP(CDhcpIpArrayDlg, CBaseDialog)
  52. //{{AFX_MSG_MAP(CDhcpIpArrayDlg)
  53. ON_BN_CLICKED(IDC_BUTN_ADD, OnClickedButnAdd)
  54. ON_BN_CLICKED(IDC_BUTN_DELETE, OnClickedButnDelete)
  55. ON_BN_CLICKED(IDC_BUTN_DOWN, OnClickedButnDown)
  56. ON_BN_CLICKED(IDC_BUTN_UP, OnClickedButnUp)
  57. ON_BN_CLICKED(IDC_HELP, OnClickedHelp)
  58. ON_LBN_SELCHANGE(IDC_LIST_IP_ADDRS, OnSelchangeListIpAddrs)
  59. ON_EN_CHANGE(IDC_EDIT_SERVER_NAME, OnChangeEditServerName)
  60. ON_BN_CLICKED(IDC_BUTN_RESOLVE, OnClickedButnResolve)
  61. ON_COMMAND(EN_SETFOCUS, OnSetFocusEditIpAddr)
  62. //}}AFX_MSG_MAP
  63. END_MESSAGE_MAP()
  64. /////////////////////////////////////////////////////////////////////////////
  65. // CDhcpIpArrayDlg message handlers
  66. BOOL
  67. CDhcpIpArrayDlg::OnInitDialog()
  68. {
  69. CBaseDialog::OnInitDialog();
  70. DWORD err = 0 ;
  71. int cStrId = m_option_type == DhcpDefaultOptions
  72. ? IDS_INFO_TITLE_DEFAULT_OPTIONS
  73. : (m_option_type == DhcpGlobalOptions
  74. ? IDS_INFO_TITLE_GLOBAL_OPTIONS
  75. : IDS_INFO_TITLE_SCOPE_OPTIONS ) ;
  76. CString str ;
  77. CATCH_MEM_EXCEPTION
  78. {
  79. m_static_option_name.SetWindowText( m_p_type->QueryName() ) ;
  80. str.LoadString( cStrId ) ;
  81. m_static_application.SetWindowText( str ) ;
  82. //
  83. // Fill the internal list from the current value.
  84. //
  85. INT cMax = m_p_type->QueryValue().QueryUpperBound() ;
  86. for (int i = 0; i < cMax; i++)
  87. {
  88. if (m_p_type->QueryValue().QueryIpAddr(i))
  89. {
  90. m_dw_array.Add((DWORD) m_p_type->QueryValue().QueryIpAddr( i ) ) ;
  91. }
  92. }
  93. //
  94. // Fill the list box without toggling redraw
  95. //
  96. Fill( 0, FALSE ) ;
  97. //
  98. // Set focus on the new IP address control.
  99. //
  100. m_ipa_new.SetFocus() ;
  101. m_ipa_new.SetModify( FALSE ) ;
  102. m_edit_server.SetModify( FALSE ) ;
  103. //
  104. // Set proper button states.
  105. //
  106. HandleActivation() ;
  107. }
  108. END_MEM_EXCEPTION( err ) ;
  109. if ( err )
  110. {
  111. ::DhcpMessageBox( err ) ;
  112. EndDialog(-1);
  113. }
  114. return FALSE ;
  115. }
  116. void
  117. CDhcpIpArrayDlg :: Fill (
  118. INT cFocus,
  119. BOOL bToggleRedraw
  120. )
  121. {
  122. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  123. CString strIp;
  124. if ( bToggleRedraw )
  125. {
  126. m_list_ip_addrs.SetRedraw( FALSE ) ;
  127. }
  128. m_list_ip_addrs.ResetContent() ;
  129. if (m_dw_array.GetSize())
  130. {
  131. for ( INT i = 0 ; i < m_dw_array.GetSize() ; i++ )
  132. {
  133. ASSERT(m_dw_array.GetAt(i));
  134. ::UtilCvtIpAddrToWstr( m_dw_array.GetAt(i), &strIp ) ;
  135. m_list_ip_addrs.AddString( strIp ) ;
  136. }
  137. }
  138. else
  139. {
  140. // Set the string to "<None>" iff the list is empty
  141. strIp.LoadString(IDS_INFO_FORMAT_IP_NONE);
  142. m_list_ip_addrs.AddString( strIp ) ;
  143. }
  144. if ( cFocus >= 0 )
  145. {
  146. m_list_ip_addrs.SetCurSel( cFocus ) ;
  147. }
  148. if ( bToggleRedraw )
  149. {
  150. m_list_ip_addrs.SetRedraw( TRUE ) ;
  151. m_list_ip_addrs.Invalidate() ;
  152. }
  153. }
  154. void
  155. CDhcpIpArrayDlg :: HandleActivation ()
  156. {
  157. INT cItems = m_list_ip_addrs.GetCount() ;
  158. INT cFocus = m_list_ip_addrs.GetCurSel() ;
  159. m_button_Up.EnableWindow(cFocus > 0) ;
  160. m_button_Down.EnableWindow(cFocus < cItems - 1) ;
  161. m_butn_delete.EnableWindow(m_dw_array.GetSize()>0) ;
  162. m_butn_resolve.EnableWindow( m_edit_server.GetModify() ) ;
  163. // check if the focus in on a disabled control
  164. // If yes, put the focus back to list box
  165. if ( !::IsWindowEnabled( ::GetFocus())) {
  166. m_list_ip_addrs.SetFocus();
  167. }
  168. } // CDhcpIpArrayDlg::HandleActivation()
  169. void
  170. CDhcpIpArrayDlg::OnOK()
  171. {
  172. INT cItems = (INT)m_dw_array.GetSize() ;
  173. DWORD err = 0 ;
  174. // Check for special case standard options 21 and 33
  175. if (( !m_p_type->IsVendor()) &&
  176. (( 21 == m_p_type->QueryId()) ||
  177. ( 33 == m_p_type->QueryId())) &&
  178. ( 0 != cItems % 2 )) {
  179. ::DhcpMessageBox( IDS_ERR_OPTION_ADDR_PAIRS );
  180. return;
  181. } // if
  182. CATCH_MEM_EXCEPTION
  183. {
  184. CDhcpOptionValue * pdhcValue = & m_p_type->QueryValue() ;
  185. pdhcValue->SetUpperBound( cItems ) ;
  186. for ( INT i = 0 ; i < cItems ; i++ )
  187. {
  188. pdhcValue->SetIpAddr( m_dw_array.GetAt(i), i ) ;
  189. }
  190. pdhcValue->SetIpAddr(0, i); // 0.0.0.0 IP Terminator
  191. m_p_type->SetDirty() ;
  192. }
  193. END_MEM_EXCEPTION(err)
  194. if ( err )
  195. {
  196. ::DhcpMessageBox( err ) ;
  197. OnCancel() ;
  198. }
  199. else
  200. {
  201. CBaseDialog::OnOK();
  202. }
  203. }
  204. void
  205. CDhcpIpArrayDlg::OnCancel()
  206. {
  207. CBaseDialog::OnCancel();
  208. }
  209. void
  210. CDhcpIpArrayDlg::OnClickedButnAdd()
  211. {
  212. INT cFocus = m_list_ip_addrs.GetCurSel() ;
  213. DWORD dhipa ;
  214. m_ipa_new.GetAddress( & dhipa ) ;
  215. // Empty IP address
  216. if (!dhipa)
  217. return;
  218. DWORD err = 0 ;
  219. CATCH_MEM_EXCEPTION
  220. {
  221. if ( cFocus < 0 )
  222. {
  223. cFocus = 0 ;
  224. }
  225. m_dw_array.InsertAt( cFocus, dhipa ) ;
  226. }
  227. END_MEM_EXCEPTION(err)
  228. if ( err )
  229. {
  230. ::DhcpMessageBox( err ) ;
  231. }
  232. //
  233. // Refill listbox, update controls.
  234. //
  235. m_ipa_new.ClearAddress();
  236. m_ipa_new.SetFocus();
  237. Fill( cFocus ) ;
  238. HandleActivation() ;
  239. }
  240. void
  241. CDhcpIpArrayDlg::OnClickedButnDelete()
  242. {
  243. INT cFocus = m_list_ip_addrs.GetCurSel() ;
  244. if ( cFocus < 0)
  245. {
  246. return ;
  247. }
  248. DHCP_IP_ADDRESS dhipa = m_dw_array.GetAt( cFocus ) ;
  249. if (!dhipa)
  250. return;
  251. m_dw_array.RemoveAt( cFocus ) ;
  252. m_ipa_new.SetAddress( dhipa ) ;
  253. m_ipa_new.SetFocus();
  254. Fill( cFocus ) ;
  255. HandleActivation() ;
  256. }
  257. void
  258. CDhcpIpArrayDlg::OnClickedButnDown()
  259. {
  260. INT cFocus = m_list_ip_addrs.GetCurSel() ;
  261. INT cItems = m_list_ip_addrs.GetCount() ;
  262. if ( cFocus < 0 || cFocus + 1 >= cItems )
  263. {
  264. return ;
  265. }
  266. DHCP_IP_ADDRESS dhipa ;
  267. DWORD err = 0 ;
  268. CATCH_MEM_EXCEPTION
  269. {
  270. dhipa = m_dw_array.GetAt( cFocus ) ;
  271. m_dw_array.RemoveAt( cFocus ) ;
  272. m_dw_array.InsertAt( cFocus + 1, dhipa ) ;
  273. }
  274. END_MEM_EXCEPTION(err)
  275. if ( err )
  276. {
  277. ::DhcpMessageBox( err ) ;
  278. }
  279. Fill( cFocus + 1 ) ;
  280. HandleActivation() ;
  281. }
  282. void
  283. CDhcpIpArrayDlg::OnClickedButnUp()
  284. {
  285. INT cFocus = m_list_ip_addrs.GetCurSel() ;
  286. INT cItems = m_list_ip_addrs.GetCount() ;
  287. if ( cFocus <= 0 )
  288. {
  289. return ;
  290. }
  291. DHCP_IP_ADDRESS dhipa ;
  292. DWORD err = 0 ;
  293. CATCH_MEM_EXCEPTION
  294. {
  295. dhipa = m_dw_array.GetAt( cFocus ) ;
  296. m_dw_array.RemoveAt( cFocus ) ;
  297. m_dw_array.InsertAt( cFocus - 1, dhipa ) ;
  298. }
  299. END_MEM_EXCEPTION(err)
  300. if ( err )
  301. {
  302. ::DhcpMessageBox( err ) ;
  303. }
  304. Fill( cFocus - 1 ) ;
  305. HandleActivation() ;
  306. }
  307. void
  308. CDhcpIpArrayDlg::OnClickedHelp()
  309. {
  310. }
  311. void
  312. CDhcpIpArrayDlg::OnSetFocusEditIpAddr()
  313. {
  314. // REVIEW t-danmo
  315. // Add code to change the default push button
  316. }
  317. void
  318. CDhcpIpArrayDlg::OnSelchangeListIpAddrs()
  319. {
  320. HandleActivation() ;
  321. }
  322. void
  323. CDhcpIpArrayDlg::OnChangeEditServerName()
  324. {
  325. HandleActivation() ;
  326. }
  327. void
  328. CDhcpIpArrayDlg::OnClickedButnResolve()
  329. {
  330. CString strHost ;
  331. DHCP_IP_ADDRESS dhipa ;
  332. DWORD err = 0 ;
  333. CATCH_MEM_EXCEPTION
  334. {
  335. m_edit_server.GetWindowText( strHost ) ;
  336. }
  337. END_MEM_EXCEPTION( err ) ;
  338. if ( err == 0 )
  339. {
  340. if ( strHost.GetLength() == 0 )
  341. {
  342. err = IDS_ERR_BAD_HOST_NAME ;
  343. }
  344. else
  345. {
  346. err = ::UtilGetHostAddress( strHost, & dhipa ) ;
  347. }
  348. if ( err == 0 )
  349. {
  350. m_ipa_new.SetAddress( dhipa ) ;
  351. }
  352. }
  353. if ( err )
  354. {
  355. ::DhcpMessageBox( err ) ;
  356. }
  357. }