no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / gfx / 2d / AutoHelpersWin.h
blob733f8a1e274cbbe23c9338679edebabb0f4bdc45
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_gfx_AutoHelpersWin_h
8 #define mozilla_gfx_AutoHelpersWin_h
10 #include <windows.h>
12 namespace mozilla {
13 namespace gfx {
15 // Get the global device context, and auto-release it on destruction.
16 class AutoDC {
17 public:
18 AutoDC() { mDC = ::GetDC(nullptr); }
20 ~AutoDC() { ::ReleaseDC(nullptr, mDC); }
22 HDC GetDC() { return mDC; }
24 private:
25 HDC mDC;
28 // Select a font into the given DC, and auto-restore.
29 class AutoSelectFont {
30 public:
31 AutoSelectFont(HDC aDC, LOGFONTW* aLogFont) : mOwnsFont(false) {
32 mFont = ::CreateFontIndirectW(aLogFont);
33 if (mFont) {
34 mOwnsFont = true;
35 mDC = aDC;
36 mOldFont = (HFONT)::SelectObject(aDC, mFont);
37 } else {
38 mOldFont = nullptr;
42 AutoSelectFont(HDC aDC, HFONT aFont) : mOwnsFont(false) {
43 mDC = aDC;
44 mFont = aFont;
45 mOldFont = (HFONT)::SelectObject(aDC, aFont);
48 ~AutoSelectFont() {
49 if (mOldFont) {
50 ::SelectObject(mDC, mOldFont);
51 if (mOwnsFont) {
52 ::DeleteObject(mFont);
57 bool IsValid() const { return mFont != nullptr; }
59 HFONT GetFont() const { return mFont; }
61 private:
62 HDC mDC;
63 HFONT mFont;
64 HFONT mOldFont;
65 bool mOwnsFont;
68 } // namespace gfx
69 } // namespace mozilla
71 #endif // mozilla_gfx_AutoHelpersWin_h