OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/win/scoped_hdc.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 namespace base { | |
10 namespace win { | |
11 | |
12 ScopedDC::ScopedDC(HDC hdc) | |
13 : hdc_(hdc), | |
14 bitmap_(0), | |
15 font_(0), | |
16 brush_(0), | |
17 pen_(0), | |
18 region_(0) { | |
19 } | |
20 | |
21 ScopedDC::~ScopedDC() {} | |
22 | |
23 void ScopedDC::SelectBitmap(HBITMAP bitmap) { | |
24 Select(bitmap, &bitmap_); | |
25 } | |
26 | |
27 void ScopedDC::SelectFont(HFONT font) { | |
28 Select(font, &font_); | |
29 } | |
30 | |
31 void ScopedDC::SelectBrush(HBRUSH brush) { | |
32 Select(brush, &brush_); | |
33 } | |
34 | |
35 void ScopedDC::SelectPen(HPEN pen) { | |
36 Select(pen, &pen_); | |
37 } | |
38 | |
39 void ScopedDC::SelectRegion(HRGN region) { | |
40 Select(region, ®ion_); | |
41 } | |
42 | |
43 void ScopedDC::Close() { | |
44 if (!hdc_) | |
45 return; | |
46 ResetObjects(); | |
47 DisposeDC(hdc_); | |
48 } | |
49 | |
50 void ScopedDC::Reset(HDC hdc) { | |
51 Close(); | |
52 hdc_ = hdc; | |
53 } | |
54 | |
55 void ScopedDC::ResetObjects() { | |
56 if (bitmap_) { | |
57 SelectObject(hdc_, bitmap_); | |
58 bitmap_ = 0; | |
59 } | |
60 if (font_) { | |
61 SelectObject(hdc_, font_); | |
62 font_ = 0; | |
63 } | |
64 if (brush_) { | |
65 SelectObject(hdc_, brush_); | |
66 brush_ = 0; | |
67 } | |
68 if (pen_) { | |
69 SelectObject(hdc_, pen_); | |
70 pen_ = 0; | |
71 } | |
72 if (region_) { | |
73 SelectObject(hdc_, region_); | |
74 region_ = 0; | |
75 } | |
76 } | |
77 | |
78 void ScopedDC::Select(HGDIOBJ object, HGDIOBJ* holder) { | |
79 HGDIOBJ old = SelectObject(hdc_, object); | |
80 DCHECK(old); | |
81 // We only want to store the first |old| object. | |
82 if (!*holder) | |
83 *holder = old; | |
84 } | |
85 | |
86 ScopedGetDC::ScopedGetDC(HWND hwnd) : ScopedDC(GetDC(hwnd)), hwnd_(hwnd) { | |
87 } | |
88 | |
89 ScopedGetDC::~ScopedGetDC() { | |
90 Close(); | |
91 } | |
92 | |
93 void ScopedGetDC::DisposeDC(HDC hdc) { | |
94 ReleaseDC(hwnd_, hdc); | |
95 } | |
96 | |
97 ScopedCreateDC::ScopedCreateDC() : ScopedDC(0) { | |
98 } | |
99 | |
100 ScopedCreateDC::ScopedCreateDC(HDC hdc) : ScopedDC(hdc) { | |
101 } | |
102 | |
103 ScopedCreateDC::~ScopedCreateDC() { | |
104 Close(); | |
105 } | |
106 | |
107 void ScopedCreateDC::Set(HDC hdc) { | |
108 Reset(hdc); | |
109 } | |
110 | |
111 void ScopedCreateDC::DisposeDC(HDC hdc) { | |
112 DeleteDC(hdc); | |
113 } | |
114 | |
115 } // namespace win | |
116 } // namespace base | |
OLD | NEW |