Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: src/engine/NotificationWindow.h

Issue 6505394822184960: Issue 1109 - Support notifications (Closed)
Left Patch Set: rebase, fix nullptrs, use COM InternetExplorer and close NotificationWindow when link is clicked Created June 25, 2015, 1:14 p.m.
Right Patch Set: fix obtaining of DPI value of content (NotificationWindow) of NotificationBorderWindow Created Aug. 18, 2015, 9:43 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 #pragma once 1 /*
Oleksandr 2015/06/26 23:05:18 It is not clear yet if we will ever need such a ri
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2015 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef NOTIFICATION_WINDOW_H
19 #define NOTIFICATION_WINDOW_H
20
2 #include <atlbase.h> 21 #include <atlbase.h>
3 #include <atlwin.h> 22 #include <atlwin.h>
4 #include <atlctl.h> 23 #include <atlctl.h>
5 #include <atlimage.h> 24 #include <atlimage.h>
6 #include <AdblockPlus/JsValue.h> 25 #include <AdblockPlus/JsValue.h>
7 #include <AdblockPlus/Notification.h> 26 #include <AdblockPlus/Notification.h>
8 #include <functional> 27 #include <functional>
9 #include <MsHtmdid.h> 28 #include <MsHtmdid.h>
10 29
11 class IconStaticControl : public ATL::CWindow 30 class IconStaticControl : public ATL::CWindow
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 { 98 {
80 return m_handle == nullptr; 99 return m_handle == nullptr;
81 } 100 }
82 protected: 101 protected:
83 T m_handle; 102 T m_handle;
84 private: 103 private:
85 ScopedObjectHandle(const ScopedObjectHandle&); 104 ScopedObjectHandle(const ScopedObjectHandle&);
86 ScopedObjectHandle& operator=(const ScopedObjectHandle&); 105 ScopedObjectHandle& operator=(const ScopedObjectHandle&);
87 }; 106 };
88 107
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> 108 class CBrush : public ScopedObjectHandle<HBRUSH>
106 { 109 {
107 public: 110 public:
108 explicit CBrush(HBRUSH brush = nullptr) : ScopedObjectHandle(brush) 111 explicit CBrush(HBRUSH brush = nullptr) : ScopedObjectHandle(brush)
109 { 112 {
110 } 113 }
111 114
112 void CreateSolidBrush(COLORREF crColor) 115 void CreateSolidBrush(COLORREF crColor)
113 { 116 {
114 ATLASSERT(m_handle == nullptr); 117 ATLASSERT(m_handle == nullptr);
(...skipping 27 matching lines...) Expand all
142 { 145 {
143 return m_hModule; 146 return m_hModule;
144 } 147 }
145 private: 148 private:
146 ScopedModule(const ScopedModule&); 149 ScopedModule(const ScopedModule&);
147 ScopedModule& operator=(const ScopedModule&); 150 ScopedModule& operator=(const ScopedModule&);
148 private: 151 private:
149 HMODULE m_hModule; 152 HMODULE m_hModule;
150 }; 153 };
151 154
152 typedef ATL::CWinTraits<WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_TOOL WINDOW | WS_EX_TOPMOST> NotificationWindowStyles; 155 class DpiAwareness {
156 public:
157 DpiAwareness() : m_dpi(96)
158 {
159 }
160 protected:
161 uint32_t DPIAware(uint32_t value) const {
162 return MulDiv(value, m_dpi, 96);
163 }
164 SIZE DPIAware(SIZE value) const {
165 return CSize(DPIAware(value.cx), DPIAware(value.cy));
166 }
167 RECT DPIAware(RECT value) const {
168 return CRect(DPIAware(value.left), DPIAware(value.top), DPIAware(value.right ), DPIAware(value.bottom));
169 }
170 uint32_t m_dpi;
171 };
172
153 enum 173 enum
154 { 174 {
155 // ID of HTMLDocument ActiveX control, it's used for event binding. 175 // ID of HTMLDocument ActiveX control, it's used for event binding.
156 kHTMLDocumentCtrlID = 101 176 kHTMLDocumentCtrlID = 101
157 }; 177 };
158 178
159 class NotificationWindow : public ATL::CWindowImpl<NotificationWindow, ATL::CWin dow, NotificationWindowStyles> 179 class NotificationWindow : public ATL::CWindowImpl<NotificationWindow>
160 , ATL::IDispEventImpl<kHTMLDocumentCtrlID, NotificationWindow, &DIID_HTMLDocum entEvents2, &LIBID_MSHTML, 4, 0> 180 , ATL::IDispEventImpl<kHTMLDocumentCtrlID, NotificationWindow, &DIID_HTMLDocum entEvents2, &LIBID_MSHTML, 4, 0>
181 , protected DpiAwareness
161 { 182 {
162 public: 183 public:
163 explicit NotificationWindow(const AdblockPlus::Notification& notification, con st std::wstring& htmlFileDir); 184 explicit NotificationWindow(const AdblockPlus::Notification& notification, con st std::wstring& htmlFileDir);
164 ~NotificationWindow(); 185 ~NotificationWindow();
165 BEGIN_MSG_MAP(NotificationWindow) 186 BEGIN_MSG_MAP(NotificationWindow)
166 if (uMsg == WM_CREATE) 187 if (uMsg == WM_CREATE)
167 { 188 {
168 SetMsgHandled(TRUE); 189 SetMsgHandled(TRUE);
169 lResult = OnCreate(reinterpret_cast<CREATESTRUCT*>(lParam)); 190 lResult = OnCreate(reinterpret_cast<CREATESTRUCT*>(lParam));
170 if(IsMsgHandled()) 191 if(IsMsgHandled())
(...skipping 18 matching lines...) Expand all
189 if(IsMsgHandled()) 210 if(IsMsgHandled())
190 return TRUE; 211 return TRUE;
191 } 212 }
192 END_MSG_MAP() 213 END_MSG_MAP()
193 214
194 BEGIN_SINK_MAP(NotificationWindow) 215 BEGIN_SINK_MAP(NotificationWindow)
195 SINK_ENTRY_EX(kHTMLDocumentCtrlID, DIID_HTMLDocumentEvents2, DISPID_HTMLDOCU MENTEVENTS2_ONCLICK, OnHTMLDocumentClick) 216 SINK_ENTRY_EX(kHTMLDocumentCtrlID, DIID_HTMLDocumentEvents2, DISPID_HTMLDOCU MENTEVENTS2_ONCLICK, OnHTMLDocumentClick)
196 SINK_ENTRY_EX(kHTMLDocumentCtrlID, DIID_HTMLDocumentEvents2, DISPID_HTMLDOCU MENTEVENTS2_ONSELECTSTART, OnHTMLDocumentSelectStart) 217 SINK_ENTRY_EX(kHTMLDocumentCtrlID, DIID_HTMLDocumentEvents2, DISPID_HTMLDOCU MENTEVENTS2_ONSELECTSTART, OnHTMLDocumentSelectStart)
197 END_SINK_MAP() 218 END_SINK_MAP()
198 219
199 std::function<void()> destroyed; 220 void SetOnClick(const std::function<void()>& callback)
200 std::function<void(const std::wstring& url)> linkClicked; 221 {
222 m_onClickCallback = callback;
223 }
224 void SetOnLinkClicked(const std::function<void(const std::wstring& url)>& call back)
225 {
226 m_onLinkClickedCallback = callback;
227 }
228 void SetOnClose(const std::function<void()>& callback)
229 {
230 m_onCloseCallback = callback;
231 }
201 private: 232 private:
202 LRESULT OnCreate(const CREATESTRUCT* createStruct); 233 LRESULT OnCreate(const CREATESTRUCT* createStruct);
203 LRESULT OnCtlColor(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled); 234 LRESULT OnCtlColor(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled);
204 LRESULT OnClick(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled); 235 LRESULT OnClick(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled);
205 void OnGetMinMaxInfo(MINMAXINFO* minMaxInfo);
206 void OnSize(uint32_t wParam, CSize size); 236 void OnSize(uint32_t wParam, CSize size);
207 void OnDestroy(); 237 void OnDestroy();
208 238
209 void __stdcall OnHTMLDocumentClick(IHTMLEventObj* pEvtObj); 239 void __stdcall OnHTMLDocumentClick(IHTMLEventObj* pEvtObj);
210 void __stdcall OnHTMLDocumentSelectStart(IHTMLEventObj* pEvtObj); 240 void __stdcall OnHTMLDocumentSelectStart(IHTMLEventObj* pEvtObj);
211 241
212 void OnFinalMessage(HWND) override;
213 void LoadABPIcon(); 242 void LoadABPIcon();
214 // returns {windowX, windowY} of top left corner on the monitor 243 private:
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; 244 std::wstring m_htmlPage;
229 CBrush m_bgColor; 245 CBrush m_bgColor;
230 ATL::CAxWindow m_axIE; 246 ATL::CAxWindow m_axIE;
231 ATL::CImage m_iconImg; 247 ATL::CImage m_iconImg;
232 IconStaticControl m_icon; 248 IconStaticControl m_icon;
233 std::vector<std::wstring> m_links; 249 std::vector<std::wstring> m_links;
234 }; 250 std::function<void()> m_onClickCallback;
251 std::function<void()> m_onCloseCallback;
252 std::function<void(const std::wstring& url)> m_onLinkClickedCallback;
253 };
254
255 typedef ATL::CWinTraits<WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, WS_EX_TOOL WINDOW | WS_EX_TOPMOST> NotificationBorderWindowStyles;
256 class NotificationBorderWindow : public ATL::CWindowImpl<NotificationBorderWindo w, ATL::CWindow, NotificationBorderWindowStyles>
257 , protected DpiAwareness
258 {
259 public:
260 DECLARE_WND_CLASS_EX(/*generate class name*/nullptr, CS_DROPSHADOW, WHITE_BRUS H);
261 NotificationBorderWindow(const AdblockPlus::Notification& notification, const std::wstring& htmlFileDir);
262 BEGIN_MSG_MAP(NotificationWindow)
263 if (uMsg == WM_CREATE)
264 {
265 SetMsgHandled(TRUE);
266 lResult = OnCreate(reinterpret_cast<CREATESTRUCT*>(lParam));
267 if(IsMsgHandled())
268 return TRUE;
269 }
270 MESSAGE_HANDLER(WM_LBUTTONUP, OnClick)
271 MESSAGE_HANDLER(WM_RBUTTONUP, OnClick)
272 if (uMsg == WM_SIZE)
273 {
274 SetMsgHandled(TRUE);
275 OnSize(static_cast<uint32_t>(wParam), CSize(GET_X_LPARAM(lParam), GET_Y_LP ARAM(lParam)));
276 lResult = 0;
277 if(IsMsgHandled())
278 return TRUE;
279 }
280 END_MSG_MAP()
281 void SetOnDestroyed(const std::function<void()>& callback)
282 {
283 m_onDestroyedCallback = callback;
284 }
285 void SetOnLinkClicked(const std::function<void(const std::wstring& url)>& call back)
286 {
287 m_content.SetOnLinkClicked(callback);
288 }
289 private:
290 LRESULT OnCreate(const CREATESTRUCT* createStruct);
291 void OnSize(uint32_t wParam, CSize size);
292 LRESULT OnClick(UINT msg, WPARAM wParam, LPARAM lParam, BOOL& handled);
293
294 void OnFinalMessage(HWND) override;
295
296 // returns {windowX, windowY} of top left corner on the monitor
297 POINT GetWindowCoordinates();
298 private:
299 // m_content is used as a holder of all children and we need it to have a bord er.
300 // It seems the most correct way to have a border to set WS_POPUPWINDOW style
301 // and paint the border in WM_NCPAINT but it simply does not work here.
302 NotificationWindow m_content;
303 std::function<void()> m_onDestroyedCallback;
304 };
305 #endif
LEFTRIGHT

Powered by Google App Engine
This is Rietveld