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

Side by Side Diff: base/win/scoped_hdc.h

Issue 9212020: Make scoped dc objects smarter. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 8 years, 11 months 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
« no previous file with comments | « base/base.gyp ('k') | base/win/scoped_hdc_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/compiler_specific.h"
12 #include "base/logging.h" 13 #include "base/logging.h"
13 14
14 namespace base { 15 namespace base {
15 namespace win { 16 namespace win {
16 17
17 // Like ScopedHandle but for HDC. Only use this on HDCs returned from 18 // The ScopedGetDC and ScopedCreateDC classes manage the default GDI objects
18 // GetDC. 19 // that are initially selected into a DC. They help you avoid the following
19 class ScopedGetDC { 20 // common mistake:
21 //
22 // HDC hdc = GetDC(NULL);
23 // SelectObject(hdc, new_bitmap);
24 // .. drawing code here ..
25 // ReleaseDC(hdc); <--- error: the DC has a custom object still selected!
26 //
27 // This code should be:
28 //
29 // HDC hdc = GetDC(NULL);
30 // HGDIOBJ old_obj = SelectObject(hdc, new_bitmap);
31 // .. drawing code here ..
32 // SelectObject(hdc, old_obj);
33 // ReleaseDC(hdc); <--- ok to release now.
34 //
35 // But why work so hard? Use our handy classes:
36 //
37 // ScopedGetDC dc(NULL);
38 // dc.SelectBitmap(hdc, new_bitmap);
39 // .. drawing here
40 // .. when dc goes out of scope it will select the original object before
41 // .. being released.
42 //
43 class ScopedDC {
44 public:
45 virtual ~ScopedDC() {}
46
47 virtual void DisposeDC(HDC hdc) = 0;
48
49 HDC Get() {
50 return hdc_;
51 }
52
53 operator HDC() {
54 return hdc_;
55 }
56
57 void SelectBitmap(HBITMAP bitmap) {
58 Select(bitmap, &bitmap_);
59 }
60
61 void SelectFont(HFONT font) {
62 Select(font, &font_);
63 }
64
65 void SelectBrush(HBRUSH brush) {
66 Select(brush, &brush_);
67 }
68
69 void SelectPen(HPEN pen) {
70 Select(pen, &pen_);
71 }
72
73 void SelectRegion(HRGN region) {
74 Select(region, &region_);
75 }
76
77 protected:
78 ScopedDC(HDC hdc)
79 : hdc_(hdc), bitmap_(0), font_(0), brush_(0), pen_(0), region_(0) {
80 }
81
82 void Close() {
83 if (!hdc_)
84 return;
85 ResetObjects();
86 DisposeDC(hdc_);
87 }
88
89 void Reset(HDC hdc) {
Peter Kasting 2012/01/24 21:03:26 Nit: We can eliminate this and inline it into the
90 Close();
91 hdc_ = hdc;
92 }
93
94 private:
95 void ResetObjects() {
96 if (bitmap_) {
97 SelectObject(hdc_, bitmap_);
98 bitmap_ = 0;
99 }
100 if (font_) {
101 SelectObject(hdc_, font_);
102 font_ = 0;
103 }
104 if (brush_) {
105 SelectObject(hdc_, brush_);
106 brush_ = 0;
107 }
108 if (pen_) {
109 SelectObject(hdc_, pen_);
110 pen_ = 0;
111 }
112 if (region_) {
113 SelectObject(hdc_, region_);
114 region_ = 0;
115 }
116 }
117
118 void Select(HGDIOBJ object, HGDIOBJ* holder) {
119 HGDIOBJ old = SelectObject(hdc_, object);
120 DCHECK(old);
121 // We only want to store the first |old| object.
122 if (!*holder)
123 *holder = old;
124 }
125
126 HDC hdc_;
127 HGDIOBJ bitmap_;
128 HGDIOBJ font_;
129 HGDIOBJ brush_;
130 HGDIOBJ pen_;
131 HGDIOBJ region_;
132 };
133
134 // Creates and manages a HDC obtained by GetDC.
135 class ScopedGetDC : public ScopedDC {
20 public: 136 public:
21 explicit ScopedGetDC(HWND hwnd) 137 explicit ScopedGetDC(HWND hwnd)
22 : hwnd_(hwnd), 138 : ScopedDC(GetDC(hwnd)), hwnd_(hwnd) {
23 hdc_(GetDC(hwnd)) {
24 DCHECK(!hwnd_ || IsWindow(hwnd_));
25 DCHECK(hdc_);
26 } 139 }
27 140
28 ~ScopedGetDC() { 141 virtual ~ScopedGetDC() {
29 if (hdc_) 142 Close();
30 ReleaseDC(hwnd_, hdc_);
31 } 143 }
32 144
33 operator HDC() { return hdc_; } 145 private:
146 virtual void DisposeDC(HDC hdc) OVERRIDE {
147 ReleaseDC(hwnd_, hdc);
148 }
34 149
35 private:
36 HWND hwnd_; 150 HWND hwnd_;
37 HDC hdc_;
38
39 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); 151 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC);
40 }; 152 };
41 153
42 // Like ScopedHandle but for HDC. Only use this on HDCs returned from 154 // Like ScopedHandle but for HDC. Only use this on HDCs returned from
43 // CreateCompatibleDC, CreateDC and CreateIC. 155 // CreateCompatibleDC, CreateDC and CreateIC.
44 class ScopedCreateDC { 156 class ScopedCreateDC : public ScopedDC {
45 public: 157 public:
46 ScopedCreateDC() : hdc_(NULL) { } 158 ScopedCreateDC()
47 explicit ScopedCreateDC(HDC h) : hdc_(h) { } 159 : ScopedDC(0) {
160 }
48 161
49 ~ScopedCreateDC() { 162 explicit ScopedCreateDC(HDC hdc)
163 : ScopedDC(hdc) {
164 }
165
166 virtual ~ScopedCreateDC() {
50 Close(); 167 Close();
51 } 168 }
52 169
53 HDC Get() { 170 void Set(HDC hdc) {
54 return hdc_; 171 Reset(hdc);
55 } 172 }
56 173
57 void Set(HDC h) { 174 private:
58 Close(); 175 virtual void DisposeDC(HDC hdc) OVERRIDE {
59 hdc_ = h; 176 DeleteDC(hdc);
60 } 177 }
61 178
62 operator HDC() { return hdc_; }
63
64 private:
65 void Close() {
66 #ifdef NOGDI
67 assert(false);
68 #else
69 if (hdc_)
70 DeleteDC(hdc_);
71 #endif // NOGDI
72 }
73
74 HDC hdc_;
75
76 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); 179 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC);
77 }; 180 };
78 181
79 } // namespace win 182 } // namespace win
80 } // namespace base 183 } // namespace base
81 184
82 #endif // BASE_WIN_SCOPED_HDC_H_ 185 #endif // BASE_WIN_SCOPED_HDC_H_
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/win/scoped_hdc_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698