browserWndEnum.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "main.h"
  2. #include "./browserWndEnum.h"
  3. #include "./browserWndRecord.h"
  4. #include "./browserView.h"
  5. #include "./browserPopup.h"
  6. #include "./ifc_omservice.h"
  7. OmBrowserWndEnumerator::OmBrowserWndEnumerator(const GUID *windowTypeFilter, const UINT *serviceIdFilter, OmBrowserWndRecord * const *windowList, size_t windowListSize)
  8. : ref(1), index(0), list(NULL), size(0), filterId(FALSE), filterType(FALSE)
  9. {
  10. if (NULL != serviceIdFilter)
  11. {
  12. serviceId = *serviceIdFilter;
  13. filterId = TRUE;
  14. }
  15. else
  16. serviceId = 0;
  17. if (NULL != windowTypeFilter)
  18. {
  19. windowType = *windowTypeFilter;
  20. filterType = TRUE;
  21. }
  22. else
  23. windowType = GUID_NULL;
  24. if (NULL != windowList && 0 != windowListSize)
  25. {
  26. list = (OmBrowserWndRecord**)calloc(windowListSize, sizeof(OmBrowserWndRecord*));
  27. if (NULL != list)
  28. {
  29. for (size_t i = 0; i < windowListSize; i++)
  30. {
  31. list[i] = windowList[i];
  32. list[i]->AddRef();
  33. }
  34. size = windowListSize;
  35. }
  36. }
  37. }
  38. OmBrowserWndEnumerator::~OmBrowserWndEnumerator()
  39. {
  40. if (NULL != list)
  41. {
  42. for (size_t i = 0; i < size; i++)
  43. {
  44. list[i]->Release();
  45. }
  46. free(list);
  47. }
  48. }
  49. HRESULT OmBrowserWndEnumerator::CreateInstance(const GUID *windowTypeFilter, const UINT *serviceIdFilter, OmBrowserWndRecord * const* windowList, size_t windowListSize, OmBrowserWndEnumerator **instance)
  50. {
  51. if (NULL == instance) return E_POINTER;
  52. *instance = new OmBrowserWndEnumerator(windowTypeFilter, serviceIdFilter, windowList, windowListSize);
  53. if (NULL == *instance) return E_OUTOFMEMORY;
  54. return S_OK;
  55. }
  56. size_t OmBrowserWndEnumerator::AddRef()
  57. {
  58. return InterlockedIncrement((LONG*)&ref);
  59. }
  60. size_t OmBrowserWndEnumerator::Release()
  61. {
  62. if (0 == ref)
  63. return ref;
  64. LONG r = InterlockedDecrement((LONG*)&ref);
  65. if (0 == r)
  66. delete(this);
  67. return r;
  68. }
  69. int OmBrowserWndEnumerator::QueryInterface(GUID interface_guid, void **object)
  70. {
  71. if (NULL == object) return E_POINTER;
  72. if (IsEqualIID(interface_guid, IFC_OmBrowserWindowEnumerator))
  73. *object = static_cast<ifc_ombrowserwndenum*>(this);
  74. else
  75. {
  76. *object = NULL;
  77. return E_NOINTERFACE;
  78. }
  79. if (NULL == *object)
  80. return E_UNEXPECTED;
  81. AddRef();
  82. return S_OK;
  83. }
  84. HRESULT OmBrowserWndEnumerator::Next(ULONG listSize, HWND *elementList, ULONG *elementCount)
  85. {
  86. if (NULL == elementList || 0 == listSize) return E_INVALIDARG;
  87. if (index >= size)
  88. {
  89. if (NULL != elementCount) *elementCount = 0;
  90. return S_FALSE;
  91. }
  92. ULONG count = 0;
  93. for (;index < size && count < listSize; index++)
  94. {
  95. OmBrowserWndRecord *r = list[index];
  96. if (FALSE != filterType)
  97. {
  98. if (S_OK != r->IsEqualType(&windowType))
  99. continue;
  100. }
  101. if (FALSE != filterId)
  102. {
  103. BOOL passOk = FALSE;
  104. if (S_OK == r->IsEqualType(&WTID_BrowserView) ||
  105. S_OK == r->IsEqualType(&WTID_BrowserPopup))
  106. {
  107. ifc_omservice *service;
  108. if (FALSE != SendMessage(r->GetHwnd(), NBCM_GETSERVICE, 0, (LPARAM)&service) && NULL != service)
  109. {
  110. if (serviceId == service->GetId())
  111. passOk = TRUE;
  112. service->Release();
  113. }
  114. }
  115. if (FALSE == passOk)
  116. continue;
  117. }
  118. elementList[count] = r->GetHwnd();
  119. count++;
  120. }
  121. if (NULL != elementCount) *elementCount = count;
  122. return (count == listSize) ? S_OK : S_FALSE;
  123. }
  124. HRESULT OmBrowserWndEnumerator::Reset(void)
  125. {
  126. index = 0;
  127. return S_OK;
  128. }
  129. HRESULT OmBrowserWndEnumerator::Skip(ULONG elementCount)
  130. {
  131. index += elementCount;
  132. if (index >= size)
  133. {
  134. index = (size - 1);
  135. return S_FALSE;
  136. }
  137. return S_OK;
  138. }
  139. #define CBCLASS OmBrowserWndEnumerator
  140. START_DISPATCH;
  141. CB(ADDREF, AddRef)
  142. CB(RELEASE, Release)
  143. CB(QUERYINTERFACE, QueryInterface)
  144. CB(API_NEXT, Next)
  145. CB(API_RESET, Reset)
  146. CB(API_SKIP, Skip)
  147. END_DISPATCH;
  148. #undef CBCLASS