OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_WIN_SCOPED_HDC_H_ | 5 #ifndef BASE_WIN_SCOPED_HDC_H_ |
6 #define BASE_WIN_SCOPED_HDC_H_ | 6 #define BASE_WIN_SCOPED_HDC_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <windows.h> | 9 #include <windows.h> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 namespace win { | 15 namespace win { |
16 | 16 |
17 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | 17 // The ScopedGetDC and ScopedCreateDC classes manage the default GDI objects |
18 // GetDC. | 18 // that are initially selected into a DC. It helps you avoid the following |
Peter Kasting
2012/01/20 21:48:12
Nit: It helps -> They help
| |
19 class ScopedGetDC { | 19 // common mistake: |
20 public: | 20 // |
21 explicit ScopedGetDC(HWND hwnd) | 21 // HDC hdc = GetDC(NULL); |
22 : hwnd_(hwnd), | 22 // SelectObject(hdc, new_obj); |
23 hdc_(GetDC(hwnd)) { | 23 // .. drawing code here .. |
24 DCHECK(!hwnd_ || IsWindow(hwnd_)); | 24 // ReleaseDC(hdc); <--- error: the DC has a custom object still selected! |
25 DCHECK(hdc_); | 25 // |
26 } | 26 // This code should be: |
27 | 27 // |
28 ~ScopedGetDC() { | 28 // HDC hdc = GetDC(NULL); |
29 if (hdc_) | 29 // HGDIOBJ old_obj = SelectObject(hdc, new_obj); |
30 ReleaseDC(hwnd_, hdc_); | 30 // .. drawing code here .. |
31 } | 31 // SelectObject(hdc, old_obj); |
32 | 32 // ReleaseDC(hdc); <--- ok to release now. |
33 operator HDC() { return hdc_; } | 33 // |
34 | 34 // But why work so hard? use our handy classes: |
35 private: | 35 // |
36 HWND hwnd_; | 36 // ScopedGetDC dc(NULL); |
37 HDC hdc_; | 37 // dc.SelectObject(hdc, new_obj); |
38 | 38 // .. drawing here |
39 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); | 39 // .. when dc goes out of scope it will select the original object. |
Peter Kasting
2012/01/20 21:48:12
Nit: "...before being released."
| |
40 }; | 40 // |
41 | 41 template <typename Derived> |
42 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | 42 class ScopedDC { |
43 // CreateCompatibleDC, CreateDC and CreateIC. | 43 public: |
44 class ScopedCreateDC { | 44 ~ScopedDC() { |
45 public: | |
46 ScopedCreateDC() : hdc_(NULL) { } | |
47 explicit ScopedCreateDC(HDC h) : hdc_(h) { } | |
48 | |
49 ~ScopedCreateDC() { | |
50 Close(); | 45 Close(); |
51 } | 46 } |
52 | 47 |
53 HDC Get() { | 48 HDC Get() { |
54 return hdc_; | 49 return hdc_; |
55 } | 50 } |
56 | 51 |
57 void Set(HDC h) { | 52 operator HDC() { |
58 Close(); | 53 return hdc_; |
59 hdc_ = h; | |
60 } | 54 } |
61 | 55 |
62 operator HDC() { return hdc_; } | 56 void SelectBitmap(HBITMAP bitmap) { |
57 Select(bitmap, &bitmap_); | |
58 } | |
59 | |
60 void SelectFont(HFONT font) { | |
61 Select(font, &font_); | |
62 } | |
63 | |
64 void SelectBrush(HBRUSH brush) { | |
65 Select(brush, &brush_); | |
66 } | |
67 | |
68 void SelectPen(HPEN pen) { | |
69 Select(pen, &pen_); | |
70 } | |
71 | |
72 void SelectRegion(HRGN region) { | |
73 Select(region, ®ion); | |
74 } | |
75 | |
76 protected: | |
77 ScopedDC(HDC hdc) | |
78 : hdc_(hdc), bitmap_(0), font_(0), brush_(0), pen_(0), region_(0) { | |
79 } | |
80 | |
81 void Close() { | |
82 if (!hdc_) | |
83 return; | |
84 ResetObjects(); | |
85 static_cast<Derived*>(this)->DisposeDC(hdc_); | |
Peter Kasting
2012/01/20 21:48:12
Why use templates and static casts to do this inst
cpu_(ooo_6.6-7.5)
2012/01/20 23:50:59
Because the client code does not treat them polimo
| |
86 } | |
87 | |
88 void Reset(HDC hdc) { | |
89 Close(); | |
90 hdc_ = hdc; | |
91 } | |
63 | 92 |
64 private: | 93 private: |
65 void Close() { | 94 void ResetObjects() { |
66 #ifdef NOGDI | 95 if (bitmap_) { |
67 assert(false); | 96 SelectObject(hdc_, bitmap_); |
68 #else | 97 bitmap_ = 0; |
69 if (hdc_) | 98 } |
70 DeleteDC(hdc_); | 99 if (font_) { |
71 #endif // NOGDI | 100 SelectObject(hdc_, font_); |
101 font_ = 0; | |
102 } | |
103 if (brush_) { | |
104 SelectObject(hdc_, brush_); | |
105 brush_ = 0; | |
106 } | |
107 if (pen_) { | |
108 SelectObject(hdc_, pen_); | |
109 pen_ = 0; | |
110 } | |
111 if (region_) { | |
112 SelectObject(hdc_, region_); | |
113 region_ = 0; | |
114 } | |
115 } | |
116 | |
117 void Select(HGDIOBJ object, HGDIOBJ* holder) { | |
118 HGDIOBJ old = SelectObject(hdc_, object); | |
119 DCHECK(old); | |
120 if (!*holder) | |
121 *holder = old; | |
72 } | 122 } |
73 | 123 |
74 HDC hdc_; | 124 HDC hdc_; |
125 HGDIOBJ bitmap_; | |
126 HGDIOBJ font_; | |
127 HGDIOBJ brush_; | |
128 HGDIOBJ pen_; | |
129 HGDIOBJ region_; | |
130 }; | |
131 | |
132 // Creates and manages a HDC obtained by GetDC. | |
133 class ScopedGetDC : public ScopedDC<ScopedGetDC> { | |
134 public: | |
135 explicit ScopedGetDC(HWND hwnd) | |
136 : ScopedDC(GetDC(hwnd)), hwnd_(hwnd) { | |
137 } | |
138 | |
139 private: | |
140 friend class ScopedDC<ScopedGetDC>; | |
141 | |
142 void DisposeDC(HDC hdc) { | |
143 ReleaseDC(hwnd_, hdc); | |
144 } | |
145 | |
146 HWND hwnd_; | |
147 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); | |
148 }; | |
149 | |
150 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | |
151 // CreateCompatibleDC, CreateDC and CreateIC. | |
Peter Kasting
2012/01/20 21:48:12
The comments here and on ScopedGetDC are structure
cpu_(ooo_6.6-7.5)
2012/01/20 23:50:59
The null ctor and the set are used in our code, sp
| |
152 class ScopedCreateDC : public ScopedDC<ScopedCreateDC> { | |
153 public: | |
154 ScopedCreateDC() | |
155 : ScopedDC(0) { | |
156 } | |
157 | |
158 explicit ScopedCreateDC(HDC hdc) | |
159 : ScopedDC(hdc) { | |
160 } | |
161 | |
162 void Set(HDC hdc) { | |
163 Reset(hdc); | |
164 } | |
165 | |
166 private: | |
167 friend class ScopedDC<ScopedCreateDC>; | |
168 | |
169 void DisposeDC(HDC hdc) { | |
170 DeleteDC(hdc); | |
171 } | |
75 | 172 |
76 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); | 173 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); |
77 }; | 174 }; |
78 | 175 |
79 } // namespace win | 176 } // namespace win |
80 } // namespace base | 177 } // namespace base |
81 | 178 |
82 #endif // BASE_WIN_SCOPED_HDC_H_ | 179 #endif // BASE_WIN_SCOPED_HDC_H_ |
OLD | NEW |