OLD | NEW |
(Empty) | |
| 1 #pragma once |
| 2 #include <atlbase.h> |
| 3 #include <atlwin.h> |
| 4 #include <atlctl.h> |
| 5 #include <atlimage.h> |
| 6 #include <AdblockPlus/JsValue.h> |
| 7 #include <AdblockPlus/Notification.h> |
| 8 #include <functional> |
| 9 #include <MsHtmdid.h> |
| 10 |
| 11 class IconStaticControl : public ATL::CWindow |
| 12 { |
| 13 public: |
| 14 explicit IconStaticControl(HWND hWnd = nullptr) : ATL::CWindow(hWnd) |
| 15 { } |
| 16 |
| 17 IconStaticControl& operator=(HWND hWnd) |
| 18 { |
| 19 m_hWnd = hWnd; |
| 20 return *this; |
| 21 } |
| 22 |
| 23 HWND Create(HWND hWndParent, ATL::_U_RECT rect = nullptr, LPCTSTR szWindowName
= nullptr, |
| 24 DWORD dwStyle = 0, DWORD dwExStyle = 0, |
| 25 ATL::_U_MENUorID MenuOrID = nullptr, LPVOID lpCreateParam = nullptr) |
| 26 { |
| 27 return ATL::CWindow::Create(GetWndClassName(), hWndParent, rect.m_lpRect, sz
WindowName, dwStyle, dwExStyle, MenuOrID.m_hMenu, lpCreateParam); |
| 28 } |
| 29 |
| 30 static wchar_t* GetWndClassName() |
| 31 { |
| 32 return L"STATIC"; |
| 33 } |
| 34 |
| 35 void SetBitmap(HBITMAP hBitmap) |
| 36 { |
| 37 ATLASSERT(::IsWindow(m_hWnd)); |
| 38 ::SendMessage(m_hWnd, STM_SETIMAGE, IMAGE_BITMAP, reinterpret_cast<LPARAM>(h
Bitmap)); |
| 39 } |
| 40 }; |
| 41 |
| 42 template<typename T> |
| 43 class ScopedObjectHandle |
| 44 { |
| 45 public: |
| 46 explicit ScopedObjectHandle(T handle = nullptr) : m_handle(handle) |
| 47 { } |
| 48 |
| 49 ~ScopedObjectHandle() |
| 50 { |
| 51 if(m_handle != nullptr) |
| 52 { |
| 53 ::DeleteObject(m_handle); |
| 54 m_handle = nullptr; |
| 55 } |
| 56 } |
| 57 |
| 58 ScopedObjectHandle& operator=(T handle) |
| 59 { |
| 60 if(m_handle != nullptr && m_handle != handle) |
| 61 ::DeleteObject(m_handle); |
| 62 m_handle = handle; |
| 63 return *this; |
| 64 } |
| 65 |
| 66 T Detach() |
| 67 { |
| 68 T retValue = m_handle; |
| 69 m_handle = nullptr; |
| 70 return retValue; |
| 71 } |
| 72 |
| 73 operator T() |
| 74 { |
| 75 return m_handle; |
| 76 } |
| 77 |
| 78 operator bool() const |
| 79 { |
| 80 return m_handle == nullptr; |
| 81 } |
| 82 protected: |
| 83 T m_handle; |
| 84 private: |
| 85 ScopedObjectHandle(const ScopedObjectHandle&); |
| 86 ScopedObjectHandle& operator=(const ScopedObjectHandle&); |
| 87 }; |
| 88 |
| 89 class CFont : public ScopedObjectHandle<HFONT> |
| 90 { |
| 91 public: |
| 92 explicit CFont(HFONT hFont = nullptr) : ScopedObjectHandle(hFont) |
| 93 { |
| 94 } |
| 95 |
| 96 void CreateFontIndirect(const LOGFONT* logFontArg, uint32_t dpi) |
| 97 { |
| 98 ATLASSERT(m_handle == nullptr); |
| 99 LOGFONT logFont = *logFontArg; |
| 100 logFont.lfHeight = -MulDiv(dpi, logFont.lfHeight, 72); |
| 101 m_handle = ::CreateFontIndirect(&logFont); |
| 102 } |
| 103 }; |
| 104 |
| 105 class CBrush : public ScopedObjectHandle<HBRUSH> |
| 106 { |
| 107 public: |
| 108 explicit CBrush(HBRUSH brush = nullptr) : ScopedObjectHandle(brush) |
| 109 { |
| 110 } |
| 111 |
| 112 void CreateSolidBrush(COLORREF crColor) |
| 113 { |
| 114 ATLASSERT(m_handle == nullptr); |
| 115 m_handle = ::CreateSolidBrush(crColor); |
| 116 } |
| 117 }; |
| 118 |
| 119 class ScopedModule |
| 120 { |
| 121 public: |
| 122 ScopedModule() |
| 123 : m_hModule(nullptr) |
| 124 { |
| 125 } |
| 126 |
| 127 bool Open(const wchar_t* fileName, int flags) |
| 128 { |
| 129 m_hModule = LoadLibraryEx(fileName, nullptr, flags); |
| 130 return m_hModule != nullptr; |
| 131 } |
| 132 |
| 133 ~ScopedModule() |
| 134 { |
| 135 if (m_hModule != nullptr) |
| 136 { |
| 137 FreeLibrary(m_hModule); |
| 138 m_hModule = nullptr; |
| 139 } |
| 140 } |
| 141 operator HMODULE() |
| 142 { |
| 143 return m_hModule; |
| 144 } |
| 145 private: |
| 146 ScopedModule(const ScopedModule&); |
| 147 ScopedModule& operator=(const ScopedModule&); |
| 148 private: |
| 149 HMODULE m_hModule; |
| 150 }; |
| 151 |
| 152 typedef ATL::CWinTraits<WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_TOOL
WINDOW | WS_EX_TOPMOST> NotificationWindowStyles; |
| 153 enum |
| 154 { |
| 155 // ID of HTMLDocument ActiveX control, it's used for event binding. |
| 156 kHTMLDocumentCtrlID = 101 |
| 157 }; |
| 158 |
| 159 class NotificationWindow : public ATL::CWindowImpl<NotificationWindow, ATL::CWin
dow, NotificationWindowStyles> |
| 160 , ATL::IDispEventImpl<kHTMLDocumentCtrlID, NotificationWindow, &DIID_HTMLDocum
entEvents2, &LIBID_MSHTML, 4, 0> |
| 161 { |
| 162 public: |
| 163 explicit NotificationWindow(const AdblockPlus::Notification& notification, con
st std::wstring& htmlFileDir); |
| 164 ~NotificationWindow(); |
| 165 BEGIN_MSG_MAP(NotificationWindow) |
| 166 if (uMsg == WM_CREATE) |
| 167 { |
| 168 SetMsgHandled(TRUE); |
| 169 lResult = OnCreate(reinterpret_cast<CREATESTRUCT*>(lParam)); |
| 170 if(IsMsgHandled()) |
| 171 return TRUE; |
| 172 } |
| 173 MESSAGE_HANDLER(WM_LBUTTONUP, OnClick) |
| 174 MESSAGE_HANDLER(WM_RBUTTONUP, OnClick) |
| 175 MESSAGE_HANDLER(WM_CTLCOLORSTATIC, OnCtlColor) |
| 176 if (uMsg == WM_SIZE) |
| 177 { |
| 178 SetMsgHandled(TRUE); |
| 179 OnSize(static_cast<uint32_t>(wParam), CSize(GET_X_LPARAM(lParam), GET_Y_LP
ARAM(lParam))); |
| 180 lResult = 0; |
| 181 if(IsMsgHandled()) |
| 182 return TRUE; |
| 183 } |
| 184 if (uMsg == WM_DESTROY) |
| 185 { |
| 186 SetMsgHandled(TRUE); |
| 187 OnDestroy(); |
| 188 lResult = 0; |
| 189 if(IsMsgHandled()) |
| 190 return TRUE; |
| 191 } |
| 192 END_MSG_MAP() |
| 193 |
| 194 BEGIN_SINK_MAP(NotificationWindow) |
| 195 SINK_ENTRY_EX(kHTMLDocumentCtrlID, DIID_HTMLDocumentEvents2, DISPID_HTMLDOCU
MENTEVENTS2_ONCLICK, OnHTMLDocumentClick) |
| 196 SINK_ENTRY_EX(kHTMLDocumentCtrlID, DIID_HTMLDocumentEvents2, DISPID_HTMLDOCU
MENTEVENTS2_ONSELECTSTART, OnHTMLDocumentSelectStart) |
| 197 END_SINK_MAP() |
| 198 |
| 199 std::function<void()> destroyed; |
| 200 std::function<void(const std::wstring& url)> linkClicked; |
| 201 private: |
| 202 LRESULT OnCreate(const CREATESTRUCT* createStruct); |
| 203 LRESULT OnCtlColor(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled); |
| 204 LRESULT OnClick(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled); |
| 205 void OnGetMinMaxInfo(MINMAXINFO* minMaxInfo); |
| 206 void OnSize(uint32_t wParam, CSize size); |
| 207 void OnDestroy(); |
| 208 |
| 209 void __stdcall OnHTMLDocumentClick(IHTMLEventObj* pEvtObj); |
| 210 void __stdcall OnHTMLDocumentSelectStart(IHTMLEventObj* pEvtObj); |
| 211 |
| 212 void OnFinalMessage(HWND) override; |
| 213 void LoadABPIcon(); |
| 214 // returns {windowX, windowY} of top left corner on the monitor |
| 215 POINT GetWindowCoordinates(); |
| 216 |
| 217 uint32_t DPIAware(uint32_t value) const { |
| 218 return MulDiv(value, m_dpi, 96); |
| 219 } |
| 220 SIZE DPIAware(SIZE value) const { |
| 221 return CSize(DPIAware(value.cx), DPIAware(value.cy)); |
| 222 } |
| 223 RECT DPIAware(RECT value) const { |
| 224 return CRect(DPIAware(value.left), DPIAware(value.top), DPIAware(value.right
), DPIAware(value.bottom)); |
| 225 } |
| 226 private: |
| 227 uint32_t m_dpi; |
| 228 std::wstring m_htmlPage; |
| 229 CBrush m_bgColor; |
| 230 ATL::CAxWindow m_axIE; |
| 231 ATL::CImage m_iconImg; |
| 232 IconStaticControl m_icon; |
| 233 std::vector<std::wstring> m_links; |
| 234 }; |
OLD | NEW |