| Left: | ||
| Right: | 
| OLD | NEW | 
|---|---|
| (Empty) | |
| 1 #include <cassert> | |
| 2 #include "NotificationWindow.h" | |
| 3 #include "../shared/Utils.h" | |
| 4 #include <algorithm> | |
| 5 #include <fstream> | |
| 6 #include "../shared/MsHTMLUtils.h" | |
| 7 | |
| 8 // it is taken from src/plugin/Resource.h | |
| 9 #define IDI_ICON_ENABLED 301 | |
| 10 | |
| 11 namespace { | |
| 12 // DIP = device independant pixels, as DPI is 96 | |
| 13 const uint32_t kWindowWidth = 400 /*DIP*/; | |
| 14 const uint32_t kWindowHeight = 120 /*DIP*/; | |
| 15 const uint32_t kIconSize = 64 /*DIP*/; | |
| 16 | |
| 17 // offsets from boundaries of working area of screen | |
| 18 const uint32_t kWindowMarginRight = 50 /*DIP*/; | |
| 19 const uint32_t kWindowMarginBottom = 20 /*DIP*/; | |
| 20 | |
| 21 std::vector<uint16_t> iconSizes = []()->std::vector<uint16_t> | |
| 22 { | |
| 23 std::vector<uint16_t> iconSizes; | |
| 24 iconSizes.emplace_back(16); | |
| 25 iconSizes.emplace_back(19); | |
| 26 iconSizes.emplace_back(48); | |
| 27 iconSizes.emplace_back(128); | |
| 28 iconSizes.emplace_back(256); | |
| 29 return iconSizes; | |
| 30 }(); | |
| 31 | |
| 32 class DCHandle | |
| 33 { | |
| 34 public: | |
| 35 explicit DCHandle(HDC hDC = nullptr) : m_hDC(hDC) | |
| 36 { | |
| 37 } | |
| 38 | |
| 39 ~DCHandle() | |
| 40 { | |
| 41 if(m_hDC != nullptr) | |
| 42 { | |
| 43 ::DeleteDC(m_hDC); | |
| 44 m_hDC = nullptr; | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 operator HDC() | |
| 49 { | |
| 50 return m_hDC; | |
| 51 } | |
| 52 | |
| 53 int GetDeviceCaps(int nIndex) const | |
| 54 { | |
| 55 ATLASSERT(m_hDC != nullptr); | |
| 56 return ::GetDeviceCaps(m_hDC, nIndex); | |
| 57 } | |
| 58 | |
| 59 HBITMAP SelectBitmap(HBITMAP value) | |
| 60 { | |
| 61 return static_cast<HBITMAP>(::SelectObject(m_hDC, value)); | |
| 62 } | |
| 63 private: | |
| 64 DCHandle(const DCHandle&); | |
| 65 DCHandle& operator=(const DCHandle&); | |
| 66 private: | |
| 67 HDC m_hDC; | |
| 68 }; | |
| 69 | |
| 70 std::wstring ReplaceMulti(std::wstring workingString, const std::wstring& plac eholder, const std::function<std::wstring()>& replacementGenerator) | |
| 71 { | |
| 72 std::wstring::size_type i = 0; | |
| 73 while ((i = workingString.find(placeholder, i)) != std::wstring::npos) | |
| 
 
Oleksandr
2015/06/26 23:05:18
How about putting everything in lowercase before t
 
sergei
2015/06/29 11:10:50
Done.
 
 | |
| 74 { | |
| 75 const std::wstring dst = replacementGenerator(); | |
| 76 workingString.replace(i, placeholder.length(), dst); | |
| 77 // tiny optimisation to skip already processed part of the string | |
| 78 i += dst.length(); | |
| 79 } | |
| 80 return workingString; | |
| 81 } | |
| 82 std::wstring ReplaceMulti(std::wstring workingString, const std::wstring& temp l, const std::wstring& replacement) | |
| 83 { | |
| 84 return ReplaceMulti(workingString, templ, [&replacement]()->std::wstring | |
| 85 { | |
| 86 return replacement; | |
| 87 }); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 NotificationWindow::NotificationWindow(const AdblockPlus::Notification& notifica tion, const std::wstring& htmlFileDir) | |
| 92 : m_dpi(96) | |
| 93 { | |
| 94 m_htmlPage = [](const std::wstring& filePath)->std::wstring | |
| 
 
Oleksandr
2015/06/26 23:05:18
Do we really need a lambda function here? Seems to
 
sergei
2015/06/29 11:10:50
Sorry, forgot to clean it. During the developing I
 
 | |
| 95 { | |
| 96 std::wifstream ifs(filePath); | |
| 97 assert(ifs.good() && "Cannot open NotificationWindow.html file"); | |
| 98 if (!ifs.good()) | |
| 99 { | |
| 100 throw std::runtime_error("Cannot read NotificationWindow.html"); | |
| 101 } | |
| 102 std::wstring fileContent; | |
| 103 fileContent.assign((std::istreambuf_iterator<wchar_t>(ifs)), std::istreambuf _iterator<wchar_t>()); | |
| 104 return fileContent; | |
| 105 }(htmlFileDir + L"NotificationWindow.html"); | |
| 106 | |
| 107 m_links = ToUtf16Strings(notification.GetLinks()); | |
| 108 auto body = ToUtf16String(notification.GetMessageString()); | |
| 109 uint32_t linkIDCounter = 0; | |
| 110 body = ReplaceMulti(body, L"<a>", [this, &linkIDCounter]()->std::wstring | |
| 111 { | |
| 112 return L"<a href=\"#\" data-linkID=\"" + std::to_wstring(linkIDCounter++) + L"\">"; | |
| 113 }); | |
| 114 assert(linkIDCounter == m_links.size() && "The amount of links in the text is different from the amount of provided links"); | |
| 115 m_htmlPage = ReplaceMulti(m_htmlPage, L"<!--Title-->", ToUtf16String(notificat ion.GetTitle())); | |
| 116 m_htmlPage = ReplaceMulti(m_htmlPage, L"<!--Body-->", body); | |
| 117 } | |
| 118 | |
| 119 NotificationWindow::~NotificationWindow() | |
| 120 { | |
| 121 } | |
| 122 | |
| 123 LRESULT NotificationWindow::OnCreate(const CREATESTRUCT* /*createStruct*/) { | |
| 124 m_bgColor.CreateSolidBrush(RGB(255, 255, 255)); | |
| 125 auto windowCoords = GetWindowCoordinates(); | |
| 126 MoveWindow(windowCoords.x, windowCoords.y, DPIAware(kWindowWidth), DPIAware(kW indowHeight)); | |
| 127 | |
| 128 m_icon.Create(m_hWnd, | |
| 129 DPIAware(CRect(CPoint(0, 0), CSize(kIconSize, kIconSize))), | |
| 130 nullptr, WS_CHILD | WS_VISIBLE | SS_BITMAP | SS_CENTERIMAGE); | |
| 131 LoadABPIcon(); | |
| 132 | |
| 133 m_axIE.Create(m_hWnd, DPIAware(CRect(CPoint(kIconSize, 0), CSize(kWindowWidth - kIconSize, kWindowHeight))), | |
| 
 
Oleksandr
2015/06/26 23:05:18
I think it would make sense to make the title to g
 
sergei
2015/06/29 11:10:50
I don't mind but before doing it I would like to s
 
 | |
| 134 L"", WS_CHILD | WS_VISIBLE, 0, kHTMLDocumentCtrlID); | |
| 135 m_axIE.CreateControl((L"mshtml:" + m_htmlPage).c_str()); | |
| 136 ATL::CComPtr<IAxWinAmbientDispatch> axWinAmbient; | |
| 137 if (SUCCEEDED(m_axIE.QueryHost(&axWinAmbient))) { | |
| 138 // disable web browser context menu | |
| 139 axWinAmbient->put_AllowContextMenu(VARIANT_FALSE); | |
| 140 // make web browser DPI aware, so the browser itself sets zoom level and | |
| 141 // cares about rendering (not zooming) in the proper size. | |
| 142 DWORD docFlags; | |
| 143 axWinAmbient->get_DocHostFlags(&docFlags); | |
| 144 docFlags |= DOCHOSTUIFLAG_DPI_AWARE; | |
| 145 // remove DOCHOSTUIFLAG_SCROLL_NO, so it's scrollable | |
| 146 docFlags &= ~DOCHOSTUIFLAG_SCROLL_NO; | |
| 147 axWinAmbient->put_DocHostFlags(docFlags); | |
| 148 } | |
| 149 // kHTMLDocumentCtrlID works here | |
| 150 AtlAdviseSinkMap(this, true); | |
| 151 | |
| 152 SetMsgHandled(false); | |
| 153 return 0; | |
| 154 } | |
| 155 | |
| 156 LRESULT NotificationWindow::OnCtlColor(UINT /*msg*/, WPARAM /*wParam*/, LPARAM l Param, BOOL& handled) | |
| 157 { | |
| 158 if (reinterpret_cast<HWND>(lParam) != m_icon) | |
| 159 { | |
| 160 handled = FALSE; | |
| 161 } | |
| 162 return reinterpret_cast<LRESULT>(static_cast<HBRUSH>(m_bgColor)); | |
| 163 } | |
| 164 | |
| 165 LRESULT NotificationWindow::OnClick(UINT /*msg*/, WPARAM /*wParam*/, LPARAM /*lP aram*/, BOOL& /*handled*/) | |
| 166 { | |
| 167 DestroyWindow(); | |
| 168 return 0; | |
| 169 } | |
| 170 | |
| 171 void NotificationWindow::OnSize(uint32_t wParam, CSize size) | |
| 172 { | |
| 173 if (m_axIE) | |
| 174 { | |
| 175 size.cx -= kIconSize; | |
| 176 CRect rect(CPoint(0, 0), size); | |
| 177 m_axIE.SetWindowPos(0, &rect, SWP_NOMOVE); | |
| 178 } | |
| 179 SetMsgHandled(false); | |
| 180 } | |
| 181 | |
| 182 void NotificationWindow::OnDestroy() | |
| 183 { | |
| 184 AtlAdviseSinkMap(this, false); | |
| 185 // and proceed as usual | |
| 186 SetMsgHandled(false); | |
| 187 } | |
| 188 | |
| 189 void __stdcall NotificationWindow::OnHTMLDocumentClick(IHTMLEventObj* eventObjec t) | |
| 190 { | |
| 191 // stop propagating the event since it's handled by us and should not cause an y other actions. | |
| 192 if (!eventObject) | |
| 193 return; | |
| 194 eventObject->put_cancelBubble(VARIANT_TRUE); | |
| 195 eventObject->put_returnValue(ATL::CComVariant(false)); | |
| 196 ATL::CComPtr<IHTMLElement> htmlElement; | |
| 197 if (FAILED(eventObject->get_srcElement(&htmlElement)) || !htmlElement) { | |
| 198 return; | |
| 199 } | |
| 200 ATL::CComBSTR tag; | |
| 201 htmlElement->get_tagName(&tag); | |
| 202 const wchar_t expectedTag[] = { L"a" }; | |
| 203 if (_wcsnicmp(tag, expectedTag, min(sizeof(expectedTag), tag.Length())) != 0) { | |
| 204 return; | |
| 205 } | |
| 206 auto classAttr = GetHtmlElementAttribute(*htmlElement, ATL::CComBSTR(L"class") ); | |
| 207 if (classAttr.attributeValue == L"closeButton") | |
| 208 { | |
| 209 PostMessage(WM_CLOSE); | |
| 210 return; | |
| 211 } | |
| 212 if (!linkClicked) | |
| 213 { | |
| 214 return; | |
| 215 } | |
| 216 auto linkIDAttr = GetHtmlElementAttribute(*htmlElement, ATL::CComBSTR(L"data-l inkID")); | |
| 217 uint32_t linkID = 0; | |
| 218 if (!linkIDAttr.attributeValue.empty() && (linkID = std::stoi(linkIDAttr.attri buteValue)) < m_links.size()) | |
| 219 { | |
| 220 linkClicked(m_links[linkID]); | |
| 221 PostMessage(WM_CLOSE); | |
| 222 } | |
| 223 } | |
| 224 | |
| 225 void __stdcall NotificationWindow::OnHTMLDocumentSelectStart(IHTMLEventObj* even tObject) | |
| 226 { | |
| 227 if (!eventObject) | |
| 228 return; | |
| 229 // disable selecting | |
| 230 eventObject->put_cancelBubble(VARIANT_TRUE); | |
| 231 eventObject->put_returnValue(ATL::CComVariant(false)); | |
| 232 } | |
| 233 | |
| 234 void NotificationWindow::OnFinalMessage(HWND) { | |
| 235 if (!!destroyed) { | |
| 236 destroyed(); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 void NotificationWindow::LoadABPIcon() | |
| 241 { | |
| 242 ScopedModule m_adblockPlusDLL; | |
| 243 if (!(m_adblockPlusDLL.Open(L"AdblockPlus32.dll", LOAD_LIBRARY_AS_DATAFILE) || | |
| 244 m_adblockPlusDLL.Open(L"AdblockPlus64.dll", LOAD_LIBRARY_AS_DATAFILE) || | |
| 245 // for debug | |
| 246 m_adblockPlusDLL.Open(L"AdblockPlus.dll", LOAD_LIBRARY_AS_DATAFILE))) | |
| 247 { | |
| 248 return; | |
| 249 } | |
| 250 auto iconSizeIterator = lower_bound(iconSizes.begin(), iconSizes.end(), DPIAwa re(kIconSize)); | |
| 251 if (iconSizeIterator == iconSizes.end()) | |
| 252 { | |
| 253 iconSizeIterator = iconSizes.rbegin().base(); | |
| 254 } | |
| 255 | |
| 256 auto hIcon = static_cast<HICON>(::LoadImageW(m_adblockPlusDLL, (L"#" + std::to _wstring(IDI_ICON_ENABLED)).c_str(), | |
| 257 IMAGE_ICON, *iconSizeIterator, *iconSizeIterator, LR_SHARED)); | |
| 258 if (hIcon == nullptr) | |
| 259 { | |
| 260 return; | |
| 261 } | |
| 262 | |
| 263 HDC screenDC = m_icon.GetDC(); | |
| 264 DCHandle tmpDC(::CreateCompatibleDC(screenDC)); | |
| 265 ScopedObjectHandle<HBITMAP> bitmap; | |
| 266 bitmap = CreateCompatibleBitmap(screenDC, DPIAware(kIconSize), DPIAware(kIconS ize)); | |
| 267 HBITMAP prevBitmap = tmpDC.SelectBitmap(bitmap); | |
| 268 RECT tmpRect = {0, 0, DPIAware(kIconSize), DPIAware(kIconSize)}; | |
| 269 FillRect(tmpDC, &tmpRect, m_bgColor); | |
| 270 ::DrawIconEx(tmpDC, 0, 0, hIcon, DPIAware(kIconSize), DPIAware(kIconSize), 0, nullptr, DI_NORMAL); | |
| 271 m_iconImg.Attach(bitmap.Detach()); | |
| 272 tmpDC.SelectBitmap(prevBitmap); | |
| 273 m_icon.SetBitmap(m_iconImg); | |
| 274 } | |
| 275 | |
| 276 POINT NotificationWindow::GetWindowCoordinates() { | |
| 277 HMONITOR primaryMonitor = MonitorFromWindow(m_hWnd, MONITOR_DEFAULTTOPRIMARY); | |
| 278 { | |
| 279 DCHandle hdc(GetDC()); | |
| 280 m_dpi = hdc.GetDeviceCaps(LOGPIXELSX); | |
| 281 } | |
| 282 MONITORINFO monitorInfo = {}; | |
| 283 monitorInfo.cbSize = sizeof(monitorInfo); | |
| 284 GetMonitorInfo(primaryMonitor, &monitorInfo); | |
| 285 int windowX = monitorInfo.rcWork.right - DPIAware(kWindowWidth + kWindowMargin Right); | |
| 286 int windowY = monitorInfo.rcWork.bottom - DPIAware(kWindowHeight + kWindowMarg inBottom); | |
| 287 POINT coords = {windowX, windowY}; | |
| 288 return coords; | |
| 289 } | |
| OLD | NEW |