DlgGameSelect.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /////////////////////////////////////////////////////////////////////////////
  2. // DlgGameSelect.cpp : implementation file
  3. //
  4. #include "stdafx.h"
  5. #include "AllSrvUI.h"
  6. #include "AllSrvUISheet.h"
  7. #include "DlgGameSelect.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CDlgGameSelect dialog
  15. /////////////////////////////////////////////////////////////////////////////
  16. // Message Map
  17. BEGIN_MESSAGE_MAP(CDlgGameSelect, CDialog)
  18. //{{AFX_MSG_MAP(CDlgGameSelect)
  19. ON_NOTIFY(LVN_DELETEITEM, IDC_GAME_LIST, OnDeleteItemGameList)
  20. ON_NOTIFY(LVN_ITEMCHANGED, IDC_GAME_LIST, OnItemChangedGameList)
  21. ON_NOTIFY(NM_DBLCLK, IDC_GAME_LIST, OnDblClickGameList)
  22. //}}AFX_MSG_MAP
  23. END_MESSAGE_MAP()
  24. /////////////////////////////////////////////////////////////////////////////
  25. // Construction
  26. CDlgGameSelect::CDlgGameSelect(CAllSrvUISheet* pSheet)
  27. : CDialog(CDlgGameSelect::IDD, pSheet)
  28. {
  29. //{{AFX_DATA_INIT(CDlgGameSelect)
  30. //}}AFX_DATA_INIT
  31. m_spGame = pSheet->GetGame();
  32. }
  33. /////////////////////////////////////////////////////////////////////////////
  34. // Overrides
  35. void CDlgGameSelect::DoDataExchange(CDataExchange* pDX)
  36. {
  37. // Perform default processing
  38. CDialog::DoDataExchange(pDX);
  39. //{{AFX_DATA_MAP(CDlgGameSelect)
  40. DDX_Control(pDX, IDC_GAME_LIST, m_listGames);
  41. //}}AFX_DATA_MAP
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // Implementation
  45. void CDlgGameSelect::PopulateGamesList()
  46. {
  47. CWaitCursor wait;
  48. LPCSTR pszContext = "retrieving collection of games on server";
  49. // Clear the contents of the list
  50. m_listGames.DeleteAllItems();
  51. // Get the collection of games on the server
  52. IAdminGamesPtr spGames;
  53. HRESULT hr = GetSheet()->GetServer()->get_Games(&spGames);
  54. if (FAILED(hr))
  55. {
  56. GetSheet()->HandleError(hr, pszContext, false);
  57. EndDialog(IDCANCEL);
  58. return;
  59. }
  60. // Get the enumerator object from the collection
  61. IUnknownPtr spEnumUnk;
  62. if (FAILED(hr = spGames->get__NewEnum(&spEnumUnk)))
  63. {
  64. GetSheet()->HandleError(hr, pszContext, false);
  65. EndDialog(IDCANCEL);
  66. return;
  67. }
  68. IEnumVARIANTPtr spEnum(spEnumUnk);
  69. if (NULL == spEnum)
  70. {
  71. GetSheet()->HandleError(E_NOINTERFACE, pszContext, false);
  72. EndDialog(IDCANCEL);
  73. return;
  74. }
  75. // Iterate through each game from the enumerator
  76. int iIndex = 0;
  77. CComVariant games[32];
  78. do
  79. {
  80. // Fetch the next block of games from the enumerator
  81. ULONG cFetched;
  82. if (FAILED(hr = spEnum->Next(sizeofArray(games), games, &cFetched)))
  83. {
  84. GetSheet()->HandleError(hr, pszContext, false);
  85. EndDialog(IDCANCEL);
  86. return;
  87. }
  88. // Add each game to the list
  89. for (ULONG i = 0; i < cFetched; ++i)
  90. {
  91. // Convert the VARIANT to IDispatch, if not already
  92. VARTYPE vt = V_VT(&games[i]);
  93. if (VT_DISPATCH != vt && VT_UNKNOWN != vt)
  94. {
  95. GetSheet()->HandleError(DISP_E_TYPEMISMATCH, pszContext, false);
  96. EndDialog(IDCANCEL);
  97. return;
  98. }
  99. // Ensure that the IDispatch supports IAdminGame
  100. IAdminGamePtr spGame(V_DISPATCH(&games[i]));
  101. if (NULL == spGame)
  102. {
  103. GetSheet()->HandleError(E_NOINTERFACE, pszContext, false);
  104. EndDialog(IDCANCEL);
  105. return;
  106. }
  107. // Clear the VARIANT in the array
  108. games[i].Clear();
  109. // Get the interesting properties of the Game
  110. CComBSTR bstrName;
  111. // Game Name
  112. spGame->get_Name(&bstrName);
  113. // Add the game to the list
  114. USES_CONVERSION;
  115. m_listGames.InsertItem(iIndex, OLE2CT(bstrName));
  116. m_listGames.SetItemData(iIndex, reinterpret_cast<DWORD>(spGame.Detach()));
  117. ++iIndex;
  118. }
  119. } while (S_FALSE != hr);
  120. }
  121. /////////////////////////////////////////////////////////////////////////////
  122. // Message Handlers
  123. BOOL CDlgGameSelect::OnInitDialog()
  124. {
  125. // Perform default processing
  126. CDialog::OnInitDialog();
  127. // Initialize the ListView control
  128. m_listGames.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_LABELTIP);
  129. m_listGames.InsertColumn(0, TEXT("Name"));
  130. m_listGames.SetColumnWidth(0, LVSCW_AUTOSIZE_USEHEADER);
  131. // TODO: Hook-up to receive game destruction events
  132. // Populate the list box with the available games
  133. PopulateGamesList();
  134. // Select the current game in the list
  135. LPARAM lp = reinterpret_cast<LPARAM>(static_cast<IAdminGame*>(m_spGame));
  136. LVFINDINFO lvfi = {LVFI_PARAM, NULL, lp};
  137. int iSel = m_listGames.FindItem(&lvfi);
  138. if (iSel > -1)
  139. m_listGames.SetItemState(iSel, LVIS_SELECTED, LVIS_SELECTED);
  140. // Return true to set the focus to the first tabstop control
  141. return true;
  142. }
  143. void CDlgGameSelect::OnDeleteItemGameList(NMHDR* pNMHDR, LRESULT* pResult)
  144. {
  145. // Parse the message parameters
  146. NM_LISTVIEW* pNMLV = (NM_LISTVIEW*)pNMHDR;
  147. // Reinterpret the item data
  148. IAdminGame* pGame = reinterpret_cast<IAdminGame*>(pNMLV->lParam);
  149. // Release the Game interface
  150. if (pGame)
  151. pGame->Release();
  152. // Clear the result
  153. *pResult = 0;
  154. }
  155. void CDlgGameSelect::OnItemChangedGameList(NMHDR* pNMHDR, LRESULT* pResult)
  156. {
  157. // Parse the message parameters
  158. NM_LISTVIEW* pNMLV = (NM_LISTVIEW*)pNMHDR;
  159. // Save the selected game, if this item is selected
  160. if (LVIS_SELECTED & pNMLV->uNewState)
  161. m_spGame = reinterpret_cast<IAdminGame*>(pNMLV->lParam);
  162. // Clear the result
  163. *pResult = 0;
  164. }
  165. void CDlgGameSelect::OnDblClickGameList(NMHDR* pNMHDR, LRESULT* pResult)
  166. {
  167. // Select the OK button
  168. OnOK();
  169. // Clear the result
  170. *pResult = 0;
  171. }