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.

375 lines
9.1 KiB

  1. // BindsDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "resource.h"
  5. #include "KeyObjs.h"
  6. #include "wrapmb.h"
  7. #include "cmnkey.h"
  8. #include "mdkey.h"
  9. #include "ListRow.h"
  10. #include "BindsDlg.h"
  11. #include "EdtBindD.h"
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17. #define COL_RAW 0
  18. #define COL_IP 1
  19. #define COL_PORT 2
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CBindingsDlg dialog
  22. CBindingsDlg::CBindingsDlg(WCHAR* pszw, CWnd* pParent /*=NULL*/)
  23. : CDialog(CBindingsDlg::IDD, pParent),
  24. m_pszwMachineName( pszw )
  25. {
  26. //{{AFX_DATA_INIT(CBindingsDlg)
  27. // NOTE: the ClassWizard will add member initialization here
  28. //}}AFX_DATA_INIT
  29. }
  30. void CBindingsDlg::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CBindingsDlg)
  34. DDX_Control(pDX, IDC_DELETE, m_cbutton_delete);
  35. DDX_Control(pDX, IDC_EDIT, m_cbutton_edit);
  36. DDX_Control(pDX, IDC_LIST, m_clistctrl_list);
  37. //}}AFX_DATA_MAP
  38. }
  39. BEGIN_MESSAGE_MAP(CBindingsDlg, CDialog)
  40. //{{AFX_MSG_MAP(CBindingsDlg)
  41. ON_BN_CLICKED(IDC_ADD, OnAdd)
  42. ON_BN_CLICKED(IDC_DELETE, OnDelete)
  43. ON_BN_CLICKED(IDC_EDIT, OnEdit)
  44. ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST, OnItemchangedList)
  45. ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList)
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. //---------------------------------------------------------------------------
  49. BOOL CBindingsDlg::OnInitDialog()
  50. {
  51. // call the parental oninitdialog
  52. BOOL f = CDialog::OnInitDialog();
  53. // set up the columns
  54. FInitList();
  55. // fill in the bindings
  56. FillInBindings();
  57. // set the initial states of the buttons
  58. EnableButtons();
  59. // return the answer
  60. return f;
  61. }
  62. //-----------------------------------------------------------------
  63. BOOL CBindingsDlg::FInitList()
  64. {
  65. CString sz;
  66. int i;
  67. // setup the raw field
  68. sz.LoadString( IDS_IP_ADDRESS );
  69. i = m_clistctrl_list.InsertColumn( COL_RAW, " ", LVCFMT_LEFT, 0 );
  70. // setup the alias field
  71. sz.LoadString( IDS_IP_ADDRESS );
  72. i = m_clistctrl_list.InsertColumn( COL_IP, sz, LVCFMT_LEFT, 118 );
  73. // setup the directory field
  74. sz.LoadString( IDS_PORT_NUMBER );
  75. i = m_clistctrl_list.InsertColumn( COL_PORT, sz, LVCFMT_LEFT, 118);
  76. return TRUE;
  77. }
  78. //--------------------------------------------------------
  79. // This provides us another opportunity to parse the
  80. void CBindingsDlg::AddBinding( CString sz )
  81. {
  82. DWORD i;
  83. BOOL f;
  84. // add the binding string to the dislpay list
  85. i = m_clistctrl_list.InsertItem( 0, " " );
  86. // update the display portion
  87. UpdateBindingDisplay( i, sz );
  88. // update the raw portion
  89. m_clistctrl_list.SetItemText( i, COL_RAW, sz );
  90. }
  91. //--------------------------------------------------------
  92. // This provides us another opportunity to parse the
  93. void CBindingsDlg::UpdateBindingDisplay( DWORD iItem, CString szBinding )
  94. {
  95. CString szIP;
  96. CString szPort;
  97. int iColon;
  98. // prepare the localized string
  99. CString szDefault;
  100. szDefault.LoadString( IDS_DEFAULT );
  101. // check for the default
  102. if ( szBinding == MDNAME_DEFAULT )
  103. {
  104. szIP.Empty();
  105. szIP.Empty();
  106. }
  107. else
  108. {
  109. // the first thing we are going to do is seperate the IP and PORT into seperate strings
  110. // look for the : and seperate
  111. iColon = szBinding.Find(':');
  112. // if we got the colon, we can seperate easy
  113. if ( iColon >= 0 )
  114. {
  115. szIP = szBinding.Left(iColon);
  116. szPort = szBinding.Right( szBinding.GetLength() - iColon - 1);
  117. }
  118. // we did not get the colon, so it is one or the other, look for a '.' to get the IP
  119. else
  120. {
  121. if ( szBinding.Find('.') >= 0 )
  122. szIP = szBinding;
  123. else
  124. szPort = szBinding;
  125. }
  126. }
  127. // if there any wildcards, show the right thing
  128. if ( szIP.IsEmpty() )
  129. szIP.LoadString( IDS_ANY_UNASSIGNED );
  130. if ( szPort.IsEmpty() )
  131. szPort.LoadString( IDS_ANY_UNASSIGNED );
  132. // set the strings into the columns
  133. m_clistctrl_list.SetItemText( iItem, COL_IP, szIP );
  134. m_clistctrl_list.SetItemText( iItem, COL_PORT, szPort );
  135. }
  136. //--------------------------------------------------------
  137. // fill in the bindings into the list
  138. void CBindingsDlg::FillInBindings()
  139. {
  140. // just load the binding list from the key into the list. The only
  141. // exception is that the non-localized "default" key indicator should
  142. // be displayed as the localed "Default" from the resources
  143. // get the number of strings that we will be dealing with here.
  144. DWORD nStrings = m_pKey->m_rgbszBindings.GetSize();
  145. // loop the strings and add them to the display list
  146. for ( DWORD i = 0; i < nStrings; i++ )
  147. {
  148. CString sz = m_pKey->m_rgbszBindings[i];
  149. // add the binding string to the dislpay list
  150. AddBinding( sz );
  151. }
  152. }
  153. //--------------------------------------------------------
  154. // enable the buttons as appropriate
  155. void CBindingsDlg::EnableButtons()
  156. {
  157. // if there is an item selected in the list, then enable
  158. // the edit and delete buttons. Otherwise, disable them
  159. if ( m_clistctrl_list.GetSelectedCount() >= 1 )
  160. {
  161. // there are items selected
  162. m_cbutton_edit.EnableWindow( TRUE );
  163. m_cbutton_delete.EnableWindow( TRUE );
  164. }
  165. else
  166. {
  167. // nope. Nothing selected
  168. m_cbutton_edit.EnableWindow( FALSE );
  169. m_cbutton_delete.EnableWindow( FALSE );
  170. }
  171. }
  172. //--------------------------------------------------------
  173. BOOL CBindingsDlg::FEdit( CString &sz )
  174. {
  175. BOOL fAnswer = FALSE;
  176. CEditBindingDlg dlg( m_pszwMachineName );
  177. // prepare the dialog
  178. dlg.m_pBindingsDlg = this;
  179. dlg.m_szBinding = sz;
  180. // run the dialog and return if the answer is OK
  181. if (dlg.DoModal() == IDOK)
  182. {
  183. fAnswer = TRUE;
  184. sz = dlg.m_szBinding;
  185. }
  186. // return the answer
  187. return fAnswer;
  188. }
  189. //--------------------------------------------------------
  190. // check the local list to see if a binding is already there
  191. BOOL CBindingsDlg::FContainsBinding( CString sz )
  192. {
  193. DWORD iItem, cItems;
  194. CString szTest;
  195. // scan all the items in the list looking for a match
  196. cItems = m_clistctrl_list.GetItemCount();
  197. for ( iItem = 0; iItem < cItems; iItem++ )
  198. {
  199. szTest = m_clistctrl_list.GetItemText(iItem, COL_RAW);
  200. if ( sz == szTest )
  201. return TRUE;
  202. }
  203. // we did not find the binding
  204. return FALSE;
  205. }
  206. /////////////////////////////////////////////////////////////////////////////
  207. // CBindingsDlg message handlers
  208. //--------------------------------------------------------
  209. void CBindingsDlg::OnAdd()
  210. {
  211. CString szNew;
  212. // default the new binding to be the default
  213. szNew = MDNAME_DEFAULT;
  214. // edit the new string. if the answer is OK, then add it to the list
  215. if ( FEdit(szNew) )
  216. {
  217. // add the binding string to the dislpay list
  218. AddBinding( szNew );
  219. }
  220. // enable the buttons as appropriate
  221. EnableButtons();
  222. }
  223. //--------------------------------------------------------
  224. void CBindingsDlg::OnDelete()
  225. {
  226. int i = GetSelectedIndex();
  227. // delete the item from the display list
  228. if ( i >= 0 )
  229. m_clistctrl_list.DeleteItem ( GetSelectedIndex() );
  230. // enable the buttons as appropriate
  231. EnableButtons();
  232. }
  233. //--------------------------------------------------------
  234. // edit the selected item
  235. void CBindingsDlg::OnEdit()
  236. {
  237. int i = GetSelectedIndex();
  238. // delete the item from the display list
  239. if ( i >= 0 )
  240. {
  241. // get the existing binding text
  242. CString sz = m_clistctrl_list.GetItemText(i, COL_RAW);
  243. // edit the text
  244. if ( FEdit(sz) )
  245. {
  246. // if the edit worked, place the text back into the object
  247. m_clistctrl_list.SetItemText(i, COL_RAW, sz);
  248. // update the display portion
  249. UpdateBindingDisplay( i, sz );
  250. }
  251. }
  252. }
  253. //--------------------------------------------------------
  254. void CBindingsDlg::OnItemchangedList(NMHDR* pNMHDR, LRESULT* pResult)
  255. {
  256. NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
  257. *pResult = 0;
  258. // enable the buttons as appropriate
  259. EnableButtons();
  260. }
  261. //--------------------------------------------------------
  262. void CBindingsDlg::OnOK()
  263. {
  264. DWORD iItem, cItems;
  265. CString sz;
  266. // all the validation work of the bindings is done in the binding edit
  267. // dialog, not here. So we can just blurt out the bindings back into
  268. // key's binding list
  269. // clear the list
  270. m_pKey->m_rgbszBindings.RemoveAll();
  271. // blurt out the bindings back into the key and set the bindings changed
  272. // flag for the key
  273. cItems = m_clistctrl_list.GetItemCount();
  274. for ( iItem = 0; iItem < cItems; iItem++ )
  275. {
  276. // get the text for the binding
  277. sz = m_clistctrl_list.GetItemText(iItem, COL_RAW);
  278. // if it is the default string, add the non-localized version
  279. if ( sz == MDNAME_DEFAULT )
  280. m_pKey->m_rgbszBindings.Add( MDNAME_DEFAULT );
  281. else
  282. // otherwise, add the string directly
  283. m_pKey->m_rgbszBindings.Add( (LPCSTR)sz );
  284. }
  285. // we also need to remove bindings in other keys that this one has taken over
  286. cItems = rgbRemovals.GetSize();
  287. for ( iItem = 0; iItem < cItems; iItem++ )
  288. {
  289. // remove the binding
  290. rgbRemovals[iItem]->RemoveBinding();
  291. // delete the objects as we go
  292. delete rgbRemovals[iItem];
  293. }
  294. rgbRemovals.RemoveAll();
  295. // tell the key to update the bindings
  296. m_pKey->m_fUpdateBindings = TRUE;
  297. // call the parent to close the dialog
  298. CDialog::OnOK();
  299. }
  300. //--------------------------------------------------------
  301. void CBindingsDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult)
  302. {
  303. // if something in the list was double clicked, edit it
  304. if ( m_clistctrl_list.GetSelectedCount() == 1 )
  305. OnEdit();
  306. *pResult = 0;
  307. }