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.

544 lines
13 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 2001 **/
  4. /**********************************************************************/
  5. /*
  6. dlgroute.cpp
  7. Implementation of CDlgStaticRoutes, dialog to show the current static
  8. routes applied to this dialin client
  9. Implementation of CDlgAddRoute, dialog to create a new route to the list
  10. FILE HISTORY:
  11. */
  12. // DlgStaticRoutes.cpp : implementation file
  13. //
  14. #include "stdafx.h"
  15. #include "helper.h"
  16. #include "rasdial.h"
  17. #include "DlgRoute.h"
  18. #include "hlptable.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. #define MAX_ROUTES 256
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CDlgStaticRoutes dialog
  27. CDlgStaticRoutes::CDlgStaticRoutes(CStrArray& Routes, CWnd* pParent /*=NULL*/)
  28. : m_strArrayRoute(Routes), CDialog(CDlgStaticRoutes::IDD, pParent)
  29. {
  30. //{{AFX_DATA_INIT(CDlgStaticRoutes)
  31. // NOTE: the ClassWizard will add member initialization here
  32. //}}AFX_DATA_INIT
  33. m_pNewRoute = NULL;
  34. m_dwNextRouteID = 1;
  35. }
  36. CDlgStaticRoutes::~CDlgStaticRoutes()
  37. {
  38. int count = 0;
  39. CString* pString;
  40. if(m_pNewRoute)
  41. count = m_pNewRoute->GetSize();
  42. while(count --)
  43. {
  44. pString = m_pNewRoute->GetAt(0);
  45. m_pNewRoute->RemoveAt(0);
  46. delete pString;
  47. }
  48. delete m_pNewRoute;
  49. }
  50. void CDlgStaticRoutes::DoDataExchange(CDataExchange* pDX)
  51. {
  52. CDialog::DoDataExchange(pDX);
  53. //{{AFX_DATA_MAP(CDlgStaticRoutes)
  54. DDX_Control(pDX, IDC_LISTROUTES, m_ListRoutes);
  55. //}}AFX_DATA_MAP
  56. }
  57. BEGIN_MESSAGE_MAP(CDlgStaticRoutes, CDialog)
  58. //{{AFX_MSG_MAP(CDlgStaticRoutes)
  59. ON_BN_CLICKED(IDC_BUTTONDELETEROUTE, OnButtonDeleteRoute)
  60. ON_BN_CLICKED(IDC_BUTTONADDROUTE, OnButtonAddRoute)
  61. ON_NOTIFY(LVN_ITEMCHANGED, IDC_LISTROUTES, OnItemchangedListroutes)
  62. ON_WM_CONTEXTMENU()
  63. ON_WM_HELPINFO()
  64. //}}AFX_MSG_MAP
  65. END_MESSAGE_MAP()
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CDlgStaticRoutes message handlers
  68. int CDlgStaticRoutes::AllRouteEntry()
  69. {
  70. CStrArray* pRoutes;
  71. CString* pRouteString;
  72. pRoutes = m_pNewRoute;
  73. TRACE(_T("CDlgStaticRoutes::AllRouteEntry()\n"));
  74. if(!pRoutes) // no new yet
  75. pRoutes = &m_strArrayRoute;
  76. int count = pRoutes->GetSize();
  77. m_RouteIDs.RemoveAll();
  78. m_ListRoutes.DeleteAllItems();
  79. m_ListRoutes.SetItemCount(count);
  80. for(long i = 0; i < count; i++)
  81. {
  82. pRouteString = pRoutes->GetAt(i);
  83. ASSERT(pRouteString);
  84. TRACE(_T("Route: %d --%s-- \n"), i, *pRouteString);
  85. m_RouteIDs.Add(m_dwNextRouteID);
  86. AddRouteEntry(i, *pRouteString, m_dwNextRouteID++);
  87. }
  88. return count;
  89. }
  90. void CDlgStaticRoutes::AddRouteEntry(int i, CString& string, DWORD ID)
  91. {
  92. if(!string.GetLength()) return;
  93. CFramedRoute Route;
  94. CString strTemp;
  95. TRACE(_T("CDlgStaticRoutes::AddRouteEntry()\n"));
  96. Route.SetRoute(&string);
  97. // dest
  98. Route.GetDest(strTemp);
  99. i = m_ListRoutes.InsertItem(i, (LPTSTR)(LPCTSTR)strTemp);
  100. m_ListRoutes.SetItemData(i, ID);
  101. m_ListRoutes.SetItemText(i, 0, (LPTSTR)(LPCTSTR)strTemp);
  102. TRACE(_T("DEST: %s "), strTemp);
  103. // prefix length
  104. Route.GetMask(strTemp);
  105. m_ListRoutes.SetItemText(i, 1, (LPTSTR)(LPCTSTR)strTemp);
  106. TRACE(_T("MASK %s "), strTemp);
  107. // metric
  108. Route.GetMetric(strTemp);
  109. m_ListRoutes.SetItemText(i, 2, (LPTSTR)(LPCTSTR)strTemp);
  110. TRACE(_T("METRIC: %s \n"), strTemp);
  111. m_ListRoutes.SetItemState(i, LVIS_SELECTED|LVIS_FOCUSED, LVIS_SELECTED|LVIS_FOCUSED);
  112. m_ListRoutes.SetFocus();
  113. }
  114. BOOL CDlgStaticRoutes::OnInitDialog()
  115. {
  116. TRACE(_T("CDlgStaticRoutes::OnInitDialog()\r\n"));
  117. AFX_MANAGE_STATE(AfxGetStaticModuleState());
  118. CDialog::OnInitDialog();
  119. // Insert all the columns
  120. CString sDest;
  121. CString sPrefixLength;
  122. CString sMetric;
  123. try{
  124. if(sDest.LoadString(IDS_DESTINATION) && sPrefixLength.LoadString(IDS_MASK) && sMetric.LoadString(IDS_METRIC))
  125. {
  126. RECT rect;
  127. m_ListRoutes.GetClientRect(&rect);
  128. m_ListRoutes.InsertColumn(1, sDest, LVCFMT_LEFT, (rect.right - rect.left)* 3/8);
  129. m_ListRoutes.InsertColumn(2, sPrefixLength, LVCFMT_LEFT, (rect.right - rect.left) * 3/8);
  130. m_ListRoutes.InsertColumn(3, sMetric, LVCFMT_LEFT, (rect.right - rect.left) * 3/16);
  131. }
  132. // Insert all the items
  133. AllRouteEntry();
  134. m_ListRoutes.SetItemCount(MAX_ROUTES);
  135. }
  136. catch(CMemoryException&)
  137. {
  138. TRACEAfxMessageBox(IDS_OUTOFMEMORY);
  139. }
  140. ListView_SetExtendedListViewStyle(m_ListRoutes.m_hWnd, LVS_EX_FULLROWSELECT);
  141. GetDlgItem(IDC_BUTTONDELETEROUTE)->EnableWindow(m_ListRoutes.GetSelectedCount() != 0);
  142. return TRUE; // return TRUE unless you set the focus to a control
  143. // EXCEPTION: OCX Property Pages should return FALSE
  144. }
  145. void CDlgStaticRoutes::OnButtonDeleteRoute()
  146. {
  147. if(!m_pNewRoute)
  148. {
  149. m_pNewRoute = new CStrArray(m_strArrayRoute);
  150. if(!m_pNewRoute)
  151. {
  152. TRACEAfxMessageBox(IDS_OUTOFMEMORY);
  153. return;
  154. }
  155. }
  156. int count = m_pNewRoute->GetSize();
  157. DWORD id;
  158. int i;
  159. int total;
  160. CString* pString;
  161. while(count--)
  162. {
  163. if(m_ListRoutes.GetItemState(count, LVIS_SELECTED))
  164. {
  165. id = m_ListRoutes.GetItemData(count);
  166. total = m_RouteIDs.GetSize();
  167. for(i = 0; i < total; i++)
  168. if(m_RouteIDs[i] == id) break;
  169. ASSERT(i < total); // must exist
  170. m_RouteIDs.RemoveAt(i); // from ID array
  171. pString = m_pNewRoute->GetAt(i); // from string array
  172. m_pNewRoute->RemoveAt(i);
  173. delete pString;
  174. m_ListRoutes.DeleteItem(count);
  175. }
  176. }
  177. GetDlgItem(IDC_BUTTONDELETEROUTE)->EnableWindow(m_ListRoutes.GetSelectedCount() != 0);
  178. // change focus when there is nothing to delete
  179. if(m_ListRoutes.GetSelectedCount() == 0)
  180. GotoDlgCtrl(GetDlgItem(IDC_BUTTONADDROUTE));
  181. }
  182. void CDlgStaticRoutes::OnButtonAddRoute()
  183. {
  184. CString* pRouteStr = NULL;
  185. // it's fine not to catch, MFC function will catch it
  186. pRouteStr = new CString();
  187. CDlgAddRoute dlg(pRouteStr, this);
  188. if(dlg.DoModal()== IDOK && pRouteStr->GetLength())
  189. {
  190. if(!m_pNewRoute)
  191. {
  192. try{
  193. m_pNewRoute = new CStrArray(m_strArrayRoute);
  194. }
  195. catch(CMemoryException&)
  196. {
  197. delete pRouteStr;
  198. throw;
  199. }
  200. }
  201. m_RouteIDs.Add(m_dwNextRouteID);
  202. AddRouteEntry(m_pNewRoute->GetSize(), *pRouteStr, m_dwNextRouteID++);
  203. m_pNewRoute->Add(pRouteStr);
  204. }
  205. else
  206. delete pRouteStr;
  207. }
  208. void CDlgStaticRoutes::OnOK()
  209. {
  210. if(m_pNewRoute)
  211. {
  212. // clear the existing one
  213. int count = m_strArrayRoute.GetSize();
  214. CString* pString;
  215. while(count--)
  216. {
  217. pString = m_strArrayRoute.GetAt(0);
  218. m_strArrayRoute.RemoveAt(0);
  219. delete pString;
  220. }
  221. // copy over the new one
  222. count = m_pNewRoute->GetSize();
  223. while(count--)
  224. {
  225. pString = m_pNewRoute->GetAt(0);
  226. m_pNewRoute->RemoveAt(0);
  227. ASSERT(pString);
  228. m_strArrayRoute.Add(pString);
  229. }
  230. }
  231. CDialog::OnOK();
  232. }
  233. /////////////////////////////////////////////////////////////////////////////
  234. // CDlgAddRoute dialog
  235. CDlgAddRoute::CDlgAddRoute(CString* pStr, CWnd* pParent /*=NULL*/)
  236. : CDialog(CDlgAddRoute::IDD, pParent)
  237. {
  238. //{{AFX_DATA_INIT(CDlgAddRoute)
  239. m_nMetric = MIN_METRIC;
  240. //}}AFX_DATA_INIT
  241. m_dwDest = 0xffffffff;
  242. m_dwMask = 0xffffff00;
  243. m_pStr = pStr;
  244. m_bInited = false;
  245. }
  246. void CDlgAddRoute::DoDataExchange(CDataExchange* pDX)
  247. {
  248. CDialog::DoDataExchange(pDX);
  249. //{{AFX_DATA_MAP(CDlgAddRoute)
  250. DDX_Control(pDX, IDC_SPINMETRIC, m_SpinMetric);
  251. DDX_Text(pDX, IDC_EDITMETRIC, m_nMetric);
  252. DDV_MinMaxUInt(pDX, m_nMetric, MIN_METRIC, MAX_METRIC);
  253. //}}AFX_DATA_MAP
  254. if(pDX->m_bSaveAndValidate) // save data to this class
  255. {
  256. // ip adress control
  257. SendDlgItemMessage(IDC_EDITDEST, IPM_GETADDRESS, 0, (LPARAM)&m_dwDest);
  258. SendDlgItemMessage(IDC_EDITMASK, IPM_GETADDRESS, 0, (LPARAM)&m_dwMask);
  259. }
  260. else // put to dialog
  261. {
  262. // ip adress control
  263. if(m_bInited)
  264. {
  265. SendDlgItemMessage(IDC_EDITDEST, IPM_SETADDRESS, 0, m_dwDest);
  266. SendDlgItemMessage(IDC_EDITMASK, IPM_SETADDRESS, 0, m_dwMask);
  267. }
  268. else
  269. {
  270. SendDlgItemMessage(IDC_EDITDEST, IPM_CLEARADDRESS, 0, m_dwDest);
  271. SendDlgItemMessage(IDC_EDITMASK, IPM_CLEARADDRESS, 0, m_dwMask);
  272. }
  273. }
  274. }
  275. BEGIN_MESSAGE_MAP(CDlgAddRoute, CDialog)
  276. //{{AFX_MSG_MAP(CDlgAddRoute)
  277. ON_WM_HELPINFO()
  278. ON_WM_CONTEXTMENU()
  279. ON_NOTIFY(IPN_FIELDCHANGED, IDC_EDITMASK, OnFieldchangedEditmask)
  280. //}}AFX_MSG_MAP
  281. END_MESSAGE_MAP()
  282. /////////////////////////////////////////////////////////////////////////////
  283. // CDlgAddRoute message handlers
  284. void CDlgAddRoute::OnOK()
  285. {
  286. // TODO: Add extra validation here
  287. if (!UpdateData(TRUE))
  288. {
  289. TRACE0("UpdateData failed during dialog termination.\n");
  290. // the UpdateData routine will set focus to correct item
  291. return;
  292. }
  293. // check to see if mask is valid -- no HOLEs
  294. DWORD mask = m_dwMask;
  295. DWORD bit = 0x80000000;
  296. DWORD nPrefixLen = 0;
  297. UINT nErrorId = 0;
  298. // find the # of 1s
  299. while ( ( bit & mask ) )
  300. {
  301. nPrefixLen++;
  302. bit >>= 1;
  303. }
  304. if(nPrefixLen > 0 && nPrefixLen <= 32)
  305. {
  306. while(bit && ((bit & mask) == 0))
  307. bit >>= 1;
  308. if(bit) // all bit is tested
  309. nErrorId = IDS_INVALIDMASK;
  310. }
  311. else
  312. nErrorId = IDS_INVALIDMASK;
  313. if(nErrorId)
  314. {
  315. AfxMessageBox(IDS_INVALIDMASK);
  316. GetDlgItem(IDC_EDITMASK)->SetFocus();
  317. return;
  318. }
  319. // check if subnet address is correct / valid
  320. if((m_dwDest & m_dwMask) != m_dwDest)
  321. {
  322. CString strError, strError1;
  323. strError.LoadString(IDS_INVALIDADDR);
  324. WORD hi1, lo1;
  325. hi1 = HIWORD(m_dwDest); lo1 = LOWORD(m_dwDest);
  326. WORD hi2, lo2;
  327. hi2 = HIWORD(m_dwMask); lo2 = LOWORD(m_dwMask);
  328. WORD hi3, lo3;
  329. hi3 = HIWORD(m_dwMask & m_dwDest); lo3 = LOWORD(m_dwMask & m_dwDest);
  330. strError1.Format(strError, HIBYTE(hi1), LOBYTE(hi1), HIBYTE(lo1), LOBYTE(lo1),
  331. HIBYTE(hi2), LOBYTE(hi2), HIBYTE(lo2), LOBYTE(lo2),
  332. HIBYTE(hi3), LOBYTE(hi3), HIBYTE(lo3), LOBYTE(lo3),
  333. HIBYTE(hi3), LOBYTE(hi3), HIBYTE(lo3), LOBYTE(lo3));
  334. if(AfxMessageBox(strError1, MB_YESNO) == IDYES)
  335. {
  336. m_dwDest = (m_dwMask & m_dwDest);
  337. UpdateData(FALSE);
  338. }
  339. GetDlgItem(IDC_EDITDEST)->SetFocus();
  340. return;
  341. }
  342. // if everything is OK
  343. {
  344. WORD hi1, lo1;
  345. hi1 = HIWORD(m_dwDest); lo1 = LOWORD(m_dwDest);
  346. m_pStr->Format(_T("%-d.%-d.%d.%d/%-d 0.0.0.0 %-d"),
  347. HIBYTE(hi1), LOBYTE(hi1), HIBYTE(lo1), LOBYTE(lo1),
  348. nPrefixLen, m_nMetric);
  349. }
  350. EndDialog(IDOK);
  351. }
  352. BOOL CDlgStaticRoutes::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
  353. {
  354. // Trap Header control codes here
  355. HD_NOTIFY *phdn = (HD_NOTIFY *)lParam;
  356. BOOL bIsSet;
  357. BOOL bResult = SystemParametersInfo( SPI_GETDRAGFULLWINDOWS,
  358. 0, &bIsSet, 0 ); // if bIsSet == TRUE
  359. // NOTE: If the "show window contents while dragging" display
  360. // property is set multiple HDN_ITEMCHANGING and HDN_ITEMCHANGED
  361. // messages will be sent and the HDN_TRACK message will not be
  362. // sent. If this property is set the opposite will happen.
  363. // BOOL bResult = SystemParametersInfo( SPI_GETDRAGFULLWINDOWS,
  364. // 0, &bIsSet, 0 ); if bIsSet == TRUE "show window contents
  365. // while dragging" is set.
  366. switch( phdn->hdr.code )
  367. {
  368. // Trap the HDN_TRACK message
  369. case HDN_TRACKA:
  370. case HDN_TRACKW:
  371. // See note above
  372. TRACE(_T("CDlgStaticRoutes::OnNotify -- HDN_TRACK Trapped\n"));
  373. *pResult = 1;
  374. return( TRUE ); // return FALSE to continue tracking the divider
  375. break;
  376. // Trap the HDN_ITEMCHANGING message
  377. case HDN_ITEMCHANGINGA:
  378. case HDN_ITEMCHANGINGW:
  379. // See note above
  380. TRACE(_T("CDlgStaticRoutes::OnNotify -- HDN_ITEMCHANGING\n"));
  381. *pResult = 1;
  382. return( TRUE ); // return FALSE to allow changes
  383. break;
  384. }
  385. return CDialog::OnNotify(wParam, lParam, pResult);
  386. }
  387. BOOL CDlgAddRoute::OnInitDialog()
  388. {
  389. CDialog::OnInitDialog();
  390. int l, h;
  391. l = max(MIN_METRIC, UD_MINVAL);
  392. h = min(MAX_METRIC, UD_MAXVAL);
  393. m_SpinMetric.SetRange(l, h);
  394. m_bInited = true;
  395. return TRUE; // return TRUE unless you set the focus to a control
  396. // EXCEPTION: OCX Property Pages should return FALSE
  397. }
  398. void CDlgStaticRoutes::OnItemchangedListroutes(NMHDR* pNMHDR, LRESULT* pResult)
  399. {
  400. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  401. GetDlgItem(IDC_BUTTONDELETEROUTE)->EnableWindow(m_ListRoutes.GetSelectedCount() != 0);
  402. *pResult = 0;
  403. }
  404. BOOL CDlgAddRoute::OnHelpInfo(HELPINFO* pHelpInfo)
  405. {
  406. ::WinHelp ((HWND)pHelpInfo->hItemHandle,
  407. AfxGetApp()->m_pszHelpFilePath,
  408. HELP_WM_HELP,
  409. (DWORD_PTR)(LPVOID)g_aHelpIDs_IDD_ADDROUTE);
  410. return CDialog::OnHelpInfo(pHelpInfo);
  411. }
  412. void CDlgAddRoute::OnContextMenu(CWnd* pWnd, CPoint point)
  413. {
  414. ::WinHelp (pWnd->m_hWnd, AfxGetApp()->m_pszHelpFilePath,
  415. HELP_CONTEXTMENU, (DWORD_PTR)(LPVOID)g_aHelpIDs_IDD_ADDROUTE);
  416. }
  417. void CDlgStaticRoutes::OnContextMenu(CWnd* pWnd, CPoint point)
  418. {
  419. ::WinHelp (pWnd->m_hWnd, AfxGetApp()->m_pszHelpFilePath,
  420. HELP_CONTEXTMENU, (DWORD_PTR)(LPVOID)g_aHelpIDs_IDD_STATICROUTES);
  421. }
  422. BOOL CDlgStaticRoutes::OnHelpInfo(HELPINFO* pHelpInfo)
  423. {
  424. ::WinHelp ((HWND)pHelpInfo->hItemHandle,
  425. AfxGetApp()->m_pszHelpFilePath,
  426. HELP_WM_HELP,
  427. (DWORD_PTR)(LPVOID)g_aHelpIDs_IDD_STATICROUTES);
  428. return CDialog::OnHelpInfo(pHelpInfo);
  429. }
  430. void CDlgAddRoute::OnFieldchangedEditmask(NMHDR* pNMHDR, LRESULT* pResult)
  431. {
  432. // TODO: Add your control notification handler code here
  433. LPNMIPADDRESS pNMIpAdd = (LPNMIPADDRESS)pNMHDR;
  434. CWnd* pIPA = GetDlgItem(IDC_EDITMASK);
  435. BYTE F[4];
  436. DWORD address;
  437. pIPA->SendMessage(IPM_GETADDRESS, 0, (LPARAM)&address);
  438. F[0] = FIRST_IPADDRESS((LPARAM)address);
  439. F[1] = SECOND_IPADDRESS((LPARAM)address);
  440. F[2] = THIRD_IPADDRESS((LPARAM)address);
  441. F[3] = FOURTH_IPADDRESS((LPARAM)address);
  442. if(pNMIpAdd->iValue == 255)
  443. {
  444. for ( int i = 0; i < pNMIpAdd->iField; i++)
  445. {
  446. F[i] = 255;
  447. }
  448. address = MAKEIPADDRESS(F[0], F[1], F[2], F[3]);
  449. pIPA->SendMessage(IPM_SETADDRESS, 0, (LPARAM)address);
  450. }
  451. *pResult = 0;
  452. }