OLD | NEW |
| (Empty) |
1 // Copyright 2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 // The scoped_* classes in this file help to make GDI settings easier. The | |
17 // constructors of these classes sets/selects provided new value into the | |
18 // context (HDC) and stores the original value in a class member. The | |
19 // destructors restore the context to its original state by setting the stored | |
20 // value back. | |
21 // | |
22 // Example usage: | |
23 // { | |
24 // // Selects the font into hdc. | |
25 // scoped_select_object select_font(hdc, ::GetStockObject(DEFAULT_GUI_FONT)); | |
26 // | |
27 // // Changes the bk mode in hdc. | |
28 // scoped_set_bk_mode set_bk_mode(hdc, TRANSPARENT); | |
29 // | |
30 // // Changes the bk color in hdc. | |
31 // scoped_set_bk_color set_bk_color(hdc, kBackgroundColor); | |
32 // | |
33 // /* Do painting operations with hdc here */ | |
34 // ... | |
35 // | |
36 // } // scoped_* goes out of scope, restoring hdc back to its original state. | |
37 // | |
38 | |
39 #ifndef OMAHA_UI_SCOPED_GDI_H_ | |
40 #define OMAHA_UI_SCOPED_GDI_H_ | |
41 | |
42 #include <wingdi.h> | |
43 #include "omaha/base/scoped_any.h" | |
44 | |
45 namespace omaha { | |
46 | |
47 template<typename C, typename T, class set_policy, class invalid_type> | |
48 class scoped_context_set { | |
49 public: | |
50 scoped_context_set(C context, T new_value) : context_(context) { | |
51 original_value_ = set_policy::set(context, new_value); | |
52 } | |
53 | |
54 ~scoped_context_set() { | |
55 invalid_type invalid_value; | |
56 if (original_value_ != invalid_value) { | |
57 set_policy::set(context_, original_value_); | |
58 } | |
59 } | |
60 | |
61 private: | |
62 T original_value_; | |
63 C context_; | |
64 }; | |
65 | |
66 template<typename Fn, Fn Pfn> | |
67 class set_function { | |
68 public: | |
69 template<typename C, typename T> | |
70 static T set(C context, T value) { | |
71 return Pfn(context, value); | |
72 } | |
73 }; | |
74 | |
75 typedef set_function<HGDIOBJ (__stdcall *)(HDC hdc, HGDIOBJ gdiobj), // NOLINT | |
76 ::SelectObject> select_object; | |
77 typedef set_function<int (__stdcall *)(HDC hdc, int mode), // NOLINT | |
78 ::SetBkMode> set_bk_mode; | |
79 typedef set_function<COLORREF (__stdcall *)(HDC hdc, COLORREF mode), // NOLINT | |
80 ::SetBkColor> set_bk_color; | |
81 | |
82 typedef value_const<HGDIOBJ, NULL> invalid_gdi_obj_value; | |
83 typedef value_const<int, 0> invalid_bk_mode_value; | |
84 typedef value_const<COLORREF, CLR_INVALID> invalid_bk_color_value; | |
85 | |
86 typedef scoped_context_set<HDC, | |
87 HGDIOBJ, | |
88 select_object, | |
89 invalid_gdi_obj_value> ScopedSelectObject; | |
90 typedef scoped_context_set<HDC, | |
91 int, | |
92 set_bk_mode, | |
93 invalid_bk_mode_value> ScopedSetBkMode; | |
94 typedef scoped_context_set<HDC, | |
95 COLORREF, | |
96 set_bk_color, | |
97 invalid_bk_color_value> ScopedSetBkColor; | |
98 | |
99 } // namespace omaha | |
100 | |
101 #endif // OMAHA_UI_SCOPED_GDI_H_ | |
OLD | NEW |