Chromium Code Reviews| 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/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() { | |
|
Peter Kasting
2012/02/08 00:38:49
Nit: Seems like this should be "get()" since it's
cpu_(ooo_6.6-7.5)
2012/02/10 19:41:55
Done.
| |
| 50 return hdc_; | |
| 51 } | |
| 52 | |
| 53 void SelectBitmap(HBITMAP bitmap) { | |
|
Peter Kasting
2012/02/08 00:38:49
Nit: Can we move these and the other non-unix_hack
| |
| 54 Select(bitmap, &bitmap_); | |
| 55 } | |
| 56 | |
| 57 void SelectFont(HFONT font) { | |
| 58 Select(font, &font_); | |
| 59 } | |
| 60 | |
| 61 void SelectBrush(HBRUSH brush) { | |
| 62 Select(brush, &brush_); | |
| 63 } | |
| 64 | |
| 65 void SelectPen(HPEN pen) { | |
| 66 Select(pen, &pen_); | |
| 67 } | |
| 68 | |
| 69 void SelectRegion(HRGN region) { | |
| 70 Select(region, ®ion_); | |
| 71 } | |
| 72 | |
| 73 protected: | |
| 74 ScopedDC(HDC hdc) | |
| 75 : hdc_(hdc), bitmap_(0), font_(0), brush_(0), pen_(0), region_(0) { | |
|
Peter Kasting
2012/02/08 00:38:49
Nit: Indent 4 (4 places); 1 member per line since
cpu_(ooo_6.6-7.5)
2012/02/10 19:41:55
Done.
| |
| 76 } | |
| 77 | |
| 78 void Close() { | |
| 79 if (!hdc_) | |
| 80 return; | |
| 81 ResetObjects(); | |
| 82 DisposeDC(hdc_); | |
| 83 } | |
| 84 | |
| 85 void Reset(HDC hdc) { | |
| 86 Close(); | |
| 87 hdc_ = hdc; | |
| 88 } | |
| 89 | |
| 90 private: | |
| 91 void ResetObjects() { | |
| 92 if (bitmap_) { | |
| 93 SelectObject(hdc_, bitmap_); | |
| 94 bitmap_ = 0; | |
| 95 } | |
| 96 if (font_) { | |
| 97 SelectObject(hdc_, font_); | |
| 98 font_ = 0; | |
| 99 } | |
| 100 if (brush_) { | |
| 101 SelectObject(hdc_, brush_); | |
| 102 brush_ = 0; | |
| 103 } | |
| 104 if (pen_) { | |
| 105 SelectObject(hdc_, pen_); | |
| 106 pen_ = 0; | |
| 107 } | |
| 108 if (region_) { | |
| 109 SelectObject(hdc_, region_); | |
| 110 region_ = 0; | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void Select(HGDIOBJ object, HGDIOBJ* holder) { | |
| 115 HGDIOBJ old = SelectObject(hdc_, object); | |
| 116 DCHECK(old); | |
| 117 // We only want to store the first |old| object. | |
| 118 if (!*holder) | |
| 119 *holder = old; | |
| 120 } | |
| 121 | |
| 122 HDC hdc_; | |
| 123 HGDIOBJ bitmap_; | |
| 124 HGDIOBJ font_; | |
| 125 HGDIOBJ brush_; | |
| 126 HGDIOBJ pen_; | |
| 127 HGDIOBJ region_; | |
| 128 }; | |
| 129 | |
| 130 // Creates and manages a HDC obtained by GetDC. | |
|
Derek Bruening
2012/02/08 15:13:19
nit: "an HDC" (unless you pronounce it "huhd-kuh"
cpu_(ooo_6.6-7.5)
2012/02/10 19:41:55
Done.
| |
| 131 class ScopedGetDC : public ScopedDC { | |
| 20 public: | 132 public: |
| 21 explicit ScopedGetDC(HWND hwnd) | 133 explicit ScopedGetDC(HWND hwnd) |
| 22 : hwnd_(hwnd), | 134 : ScopedDC(GetDC(hwnd)), hwnd_(hwnd) { |
| 23 hdc_(GetDC(hwnd)) { | |
| 24 DCHECK(!hwnd_ || IsWindow(hwnd_)); | |
| 25 DCHECK(hdc_); | |
| 26 } | 135 } |
| 27 | 136 |
| 28 ~ScopedGetDC() { | 137 virtual ~ScopedGetDC() { |
| 29 if (hdc_) | 138 Close(); |
| 30 ReleaseDC(hwnd_, hdc_); | |
| 31 } | 139 } |
| 32 | 140 |
| 33 operator HDC() { return hdc_; } | 141 private: |
| 142 virtual void DisposeDC(HDC hdc) OVERRIDE { | |
| 143 ReleaseDC(hwnd_, hdc); | |
| 144 } | |
| 34 | 145 |
| 35 private: | |
| 36 HWND hwnd_; | 146 HWND hwnd_; |
| 37 HDC hdc_; | |
| 38 | |
| 39 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); | 147 DISALLOW_COPY_AND_ASSIGN(ScopedGetDC); |
| 40 }; | 148 }; |
| 41 | 149 |
| 42 // Like ScopedHandle but for HDC. Only use this on HDCs returned from | 150 // Like ScopedHandle but for HDC. Only use this on HDCs returned from |
| 43 // CreateCompatibleDC, CreateDC and CreateIC. | 151 // CreateCompatibleDC, CreateDC and CreateIC. |
| 44 class ScopedCreateDC { | 152 class ScopedCreateDC : public ScopedDC { |
| 45 public: | 153 public: |
| 46 ScopedCreateDC() : hdc_(NULL) { } | 154 ScopedCreateDC() |
| 47 explicit ScopedCreateDC(HDC h) : hdc_(h) { } | 155 : ScopedDC(0) { |
| 156 } | |
| 48 | 157 |
| 49 ~ScopedCreateDC() { | 158 explicit ScopedCreateDC(HDC hdc) |
| 159 : ScopedDC(hdc) { | |
| 160 } | |
| 161 | |
| 162 virtual ~ScopedCreateDC() { | |
| 50 Close(); | 163 Close(); |
| 51 } | 164 } |
| 52 | 165 |
| 53 HDC Get() { | 166 void Set(HDC hdc) { |
| 54 return hdc_; | 167 Reset(hdc); |
| 55 } | 168 } |
| 56 | 169 |
| 57 void Set(HDC h) { | 170 private: |
| 58 Close(); | 171 virtual void DisposeDC(HDC hdc) OVERRIDE { |
| 59 hdc_ = h; | 172 DeleteDC(hdc); |
| 60 } | 173 } |
| 61 | 174 |
| 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); | 175 DISALLOW_COPY_AND_ASSIGN(ScopedCreateDC); |
| 77 }; | 176 }; |
| 78 | 177 |
| 79 } // namespace win | 178 } // namespace win |
| 80 } // namespace base | 179 } // namespace base |
| 81 | 180 |
| 82 #endif // BASE_WIN_SCOPED_HDC_H_ | 181 #endif // BASE_WIN_SCOPED_HDC_H_ |
| OLD | NEW |