Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(682)

Side by Side Diff: chrome_frame/infobars/internal/subclassing_window.h

Issue 4766003: Preview CL for adding an Infobar facility to Google Chrome Frame.... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: '' Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CHROME_FRAME_INFOBARS_INTERNAL_SUBCLASSING_WINDOW_H_
6 #define CHROME_FRAME_INFOBARS_INTERNAL_SUBCLASSING_WINDOW_H_
7
8 #include <atlbase.h>
9 #include <atlcrack.h>
10 #include <atlwin.h>
11
12 #include "base/basictypes.h"
13 #include "base/logging.h"
14 #include "base/scoped_ptr.h"
15 #include "chrome_frame/utils.h"
16
17 // Implements behavior common to HostWindowManager and DisplacedWindowManager.
18 template<typename T> class SubclassingWindowWithDelegate
19 : public CWindowImpl<T> {
20 public:
21 // Allows clients to modify the dimensions of the displaced window.
22 // Through its destructor, allows clients to know when the subclassed window
23 // is destroyed.
24 class Delegate {
25 public:
26 // The delegate will be deleted when the subclassed window is destroyed.
27 virtual ~Delegate() {}
28
29 // Receives the natural dimensions of the displaced window. Upon return,
30 // rect should contain the adjusted dimensions (i.e., possibly reduced to
31 // accomodate an infobar).
32 virtual void AdjustDisplacedWindowDimensions(RECT* rect) = 0;
33 }; // class Delegate
34
35 SubclassingWindowWithDelegate() {}
36
37 // Returns true if the window is successfully subclassed, in which case this
38 // instance will take responsibility for its own destruction when the window
39 // is destroyed. If this method returns false, the caller should delete the
40 // instance immediately.
41 //
42 // Takes ownership of delegate in either case, deleting it when the window
43 // is destroyed (or immediately, in case of failure).
44 bool Initialize(HWND hwnd, Delegate* delegate) {
45 DCHECK(delegate != NULL);
46 DCHECK(delegate_ == NULL);
47 scoped_ptr<Delegate> new_delegate(delegate);
48
49 if (!::IsWindow(hwnd) || !SubclassWindow(hwnd)) {
50 PLOG(ERROR) << "Failed to subclass an HWND";
amit 2010/12/02 16:28:51 nit: it'll be nice if the error code is available
51 return false;
52 }
53
54 // Ensure we won't be unloaded while our window proc is attached to the tab
55 // window.
56 PinModule();
57
58 delegate_.swap(new_delegate);
59
60 return true;
61 }
62
63 // Returns the delegate associated with the specified window, if any.
64 static Delegate* GetDelegateForHwnd(HWND hwnd) {
65 LRESULT result = ::SendMessage(hwnd,
grt (UTC plus 2) 2010/12/02 16:27:52 nit: Can these lines be collapsed to reduce vertic
66 RegisterGetDelegateMessage(),
67 NULL,
68 NULL);
69 return reinterpret_cast<Delegate*>(result);
70 }
71
72 BEGIN_MSG_MAP_EX(SubclassingWindowWithDelegate)
73 MESSAGE_HANDLER(RegisterGetDelegateMessage(), OnGetDelegate)
74 MSG_WM_DESTROY(OnDestroy)
75 END_MSG_MAP()
76
77 // This instance is now free to be deleted.
78 virtual void OnFinalMessage(HWND hwnd) {
79 delete this;
80 }
81
82 protected:
83 scoped_ptr<Delegate>& delegate() { return delegate_; }
84
85 private:
86 // Registers a unique ID for our custom event.
87 static UINT RegisterGetDelegateMessage() {
88 static UINT message_id(
grt (UTC plus 2) 2010/12/02 16:27:52 This will register the same message id for all ins
89 RegisterWindowMessage(L"SubclassingWindowWithDelegate::OnGetDelegate"));
90 return message_id;
91 }
92
93 // The subclassed window has been destroyed. Delete the delegate. We will
94 // delete ourselves in OnFinalMessage.
95 void OnDestroy() {
96 delegate_.reset();
97 }
98
99 LRESULT OnGetDelegate(UINT message,
100 WPARAM wparam,
101 LPARAM lparam,
102 BOOL& handled) {
103 return reinterpret_cast<LRESULT>(delegate_.get());
104 }
105
106 scoped_ptr<Delegate> delegate_;
107 DISALLOW_COPY_AND_ASSIGN(SubclassingWindowWithDelegate);
108 }; // class SubclassingWindowWithDelegate
109
110 #endif // CHROME_FRAME_INFOBARS_INTERNAL_SUBCLASSING_WINDOW_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698