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.

254 lines
7.4 KiB

  1. //
  2. //browsedlg.cpp: browse for servers dialog
  3. //
  4. #include "stdafx.h"
  5. #define TRC_GROUP TRC_GROUP_UI
  6. #define TRC_FILE "browsesrv"
  7. #include <atrcapi.h>
  8. #include "browsedlg.h"
  9. #include "resource.h"
  10. #include "wuiids.h"
  11. CBrowseDlg* CBrowseDlg::m_pThis = NULL;
  12. CBrowseDlg::CBrowseDlg(HWND hWndOwner, HINSTANCE hInst) : m_hWnd(hWndOwner), m_hInst(hInst)
  13. {
  14. m_pThis = this;
  15. _pBrowseSrvCtl = NULL;
  16. _tcscpy( m_szServer, _T(""));
  17. }
  18. CBrowseDlg::~CBrowseDlg()
  19. {
  20. }
  21. int
  22. CBrowseDlg::DoModal()
  23. {
  24. int retVal;
  25. //
  26. // Init owner draw servers list box
  27. //
  28. _pBrowseSrvCtl = new CBrowseServersCtl(m_hInst);
  29. if(!_pBrowseSrvCtl)
  30. {
  31. return 0;
  32. }
  33. _pBrowseSrvCtl->AddRef();
  34. retVal = DialogBox( m_hInst,MAKEINTRESOURCE(IDD_DIALOG_BROWSESERVERS),
  35. m_hWnd, StaticDlgProc);
  36. //Object self deletes when refcount reaches 0
  37. //done so object is still around if list box population thread is still running
  38. _pBrowseSrvCtl->Release();
  39. return retVal;
  40. }
  41. INT_PTR CALLBACK CBrowseDlg::StaticDlgProc(HWND hDlg,UINT uMsg, WPARAM wParam, LPARAM lParam)
  42. {
  43. //
  44. // need access to class variables so redirect to non-static version of callback
  45. //
  46. return m_pThis->DlgProc(hDlg,uMsg,wParam,lParam);
  47. }
  48. INT_PTR
  49. CBrowseDlg::DlgProc(HWND hwndDlg,UINT uMsg, WPARAM wParam, LPARAM lParam)
  50. {
  51. DC_BEGIN_FN("DlgProc");
  52. BOOL rc = FALSE;
  53. static ServerListItem *plbi = NULL;
  54. static HANDLE hThread = NULL;
  55. static DCUINT DomainCount = 0;
  56. switch (uMsg)
  57. {
  58. case WM_INITDIALOG:
  59. {
  60. //Set the parent dialog handle of the browse for servers listbox
  61. _pBrowseSrvCtl->SetDialogHandle( hwndDlg);
  62. _pBrowseSrvCtl->Init( hwndDlg );
  63. if(hwndDlg)
  64. {
  65. DWORD dwResult = 0, dwThreadId;
  66. LPVOID lpMsgBuf = NULL;
  67. _bLBPopulated = FALSE;
  68. //create an event to signal the worker thread
  69. //auto reset and initial state is nonsignalled
  70. _hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  71. if(!_hEvent)
  72. {
  73. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  74. FORMAT_MESSAGE_FROM_SYSTEM,
  75. NULL,
  76. GetLastError(),
  77. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  78. (LPTSTR) & lpMsgBuf, 0, NULL);
  79. TRC_ERR((TB, _T("CreateEvent returned %s"), lpMsgBuf));
  80. }
  81. else
  82. {
  83. //Set the event handle for notification
  84. //the _BrowseSrvListBox will CloseHandle the event when it is done
  85. _pBrowseSrvCtl->SetEventHandle(_hEvent);
  86. //
  87. // Need to set the wait cursor on the UI thread
  88. // dismiss it in the LB_POPULATE message handler
  89. //
  90. SetCursor(LoadCursor(NULL, IDC_WAIT));
  91. /* Create a worker thread to do the browsing for servers */
  92. hThread = CreateThread(NULL, 0,
  93. &CBrowseServersCtl::UIStaticPopListBoxThread,
  94. _pBrowseSrvCtl, 0, &dwThreadId);
  95. }
  96. if(lpMsgBuf)
  97. {
  98. LocalFree(lpMsgBuf);
  99. }
  100. if(NULL == hThread)
  101. {
  102. // Since the CreateThread failed, populate the list box directly
  103. _pBrowseSrvCtl->LoadLibraries();
  104. plbi = _pBrowseSrvCtl->PopulateListBox(hwndDlg, &DomainCount);
  105. }
  106. else
  107. {
  108. //
  109. // Add a reference to the list box object for the new thread
  110. // so the object doesn't get deleted before the thread is done
  111. // the Release() is in the function called on this new thread
  112. //
  113. _pBrowseSrvCtl->AddRef();
  114. CloseHandle(hThread);
  115. }
  116. }
  117. rc = TRUE;
  118. }
  119. break;
  120. //Notification from server list control
  121. case UI_LB_POPULATE_START:
  122. {
  123. //Set a cursor for the wait state
  124. SetCursor(LoadCursor(NULL, IDC_WAIT));
  125. }
  126. break;
  127. //Notification from server list control
  128. case UI_LB_POPULATE_END:
  129. {
  130. _bLBPopulated = TRUE;
  131. SetCursor(LoadCursor(NULL, IDC_ARROW));
  132. }
  133. break;
  134. case WM_CLOSE:
  135. {
  136. EndDialog(hwndDlg, IDCANCEL);
  137. }
  138. break;
  139. case WM_NOTIFY:
  140. {
  141. //
  142. // Don't forward tree view notifications
  143. // untill the async enumeration thread has finished
  144. // populating (_bLBPopulated is set)
  145. //
  146. if(UI_IDC_SERVERS_TREE == wParam &&
  147. _bLBPopulated)
  148. {
  149. LPNMHDR pnmh = (LPNMHDR) lParam;
  150. if(pnmh)
  151. {
  152. if(pnmh->code == NM_DBLCLK)
  153. {
  154. //
  155. // If the current selection is a server
  156. // then we're done
  157. //
  158. if(_pBrowseSrvCtl->GetServer( m_szServer,
  159. SIZECHAR(m_szServer) ))
  160. {
  161. EndDialog( hwndDlg, IDOK );
  162. rc = TRUE;
  163. }
  164. else
  165. {
  166. _tcscpy( m_szServer, _T(""));
  167. }
  168. }
  169. }
  170. return _pBrowseSrvCtl->OnNotify( hwndDlg, wParam, lParam );
  171. }
  172. }
  173. break;
  174. case WM_COMMAND:
  175. {
  176. switch(DC_GET_WM_COMMAND_ID(wParam))
  177. {
  178. case IDCANCEL:
  179. {
  180. EndDialog(hwndDlg, IDCANCEL);
  181. rc = TRUE;
  182. }
  183. break;
  184. case IDOK:
  185. {
  186. if(_pBrowseSrvCtl->GetServer( m_szServer,
  187. SIZECHAR(m_szServer) ))
  188. {
  189. EndDialog(hwndDlg, IDOK);
  190. }
  191. else
  192. {
  193. EndDialog(hwndDlg, IDCANCEL);
  194. }
  195. rc = TRUE;
  196. }
  197. break;
  198. }
  199. }
  200. break;
  201. case WM_DESTROY:
  202. {
  203. /* Since we are in WM_DESTROY signal to the worker thread to discontinue. */
  204. if(_hEvent)
  205. {
  206. SetEvent(_hEvent);
  207. }
  208. rc = FALSE;
  209. }
  210. break;
  211. }
  212. return(rc);
  213. }