tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

AutoHelpersWin.h (1611B)


      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/. */
      6 
      7 #ifndef mozilla_gfx_AutoHelpersWin_h
      8 #define mozilla_gfx_AutoHelpersWin_h
      9 
     10 #include <windows.h>
     11 
     12 namespace mozilla {
     13 namespace gfx {
     14 
     15 // Get the global device context, and auto-release it on destruction.
     16 class AutoDC {
     17 public:
     18  AutoDC() { mDC = ::GetDC(nullptr); }
     19 
     20  ~AutoDC() { ::ReleaseDC(nullptr, mDC); }
     21 
     22  HDC GetDC() { return mDC; }
     23 
     24 private:
     25  HDC mDC;
     26 };
     27 
     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;
     39    }
     40  }
     41 
     42  AutoSelectFont(HDC aDC, HFONT aFont) : mOwnsFont(false) {
     43    mDC = aDC;
     44    mFont = aFont;
     45    mOldFont = (HFONT)::SelectObject(aDC, aFont);
     46  }
     47 
     48  ~AutoSelectFont() {
     49    if (mOldFont) {
     50      ::SelectObject(mDC, mOldFont);
     51      if (mOwnsFont) {
     52        ::DeleteObject(mFont);
     53      }
     54    }
     55  }
     56 
     57  bool IsValid() const { return mFont != nullptr; }
     58 
     59  HFONT GetFont() const { return mFont; }
     60 
     61 private:
     62  HDC mDC;
     63  HFONT mFont;
     64  HFONT mOldFont;
     65  bool mOwnsFont;
     66 };
     67 
     68 }  // namespace gfx
     69 }  // namespace mozilla
     70 
     71 #endif  // mozilla_gfx_AutoHelpersWin_h