michael@0: /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef _MOZILLA_GFX_BORROWED_CONTEXT_H michael@0: #define _MOZILLA_GFX_BORROWED_CONTEXT_H michael@0: michael@0: #include "2D.h" michael@0: michael@0: struct _cairo; michael@0: typedef struct _cairo cairo_t; michael@0: michael@0: namespace mozilla { michael@0: michael@0: namespace gfx { michael@0: michael@0: /* This is a helper class that let's you borrow a cairo_t from a michael@0: * DrawTargetCairo. This is used for drawing themed widgets. michael@0: * michael@0: * Callers should check the cr member after constructing the object michael@0: * to see if it succeeded. The DrawTarget should not be used while michael@0: * the context is borrowed. */ michael@0: class BorrowedCairoContext michael@0: { michael@0: public: michael@0: BorrowedCairoContext() michael@0: : mCairo(nullptr) michael@0: , mDT(nullptr) michael@0: { } michael@0: michael@0: BorrowedCairoContext(DrawTarget *aDT) michael@0: : mDT(aDT) michael@0: { michael@0: mCairo = BorrowCairoContextFromDrawTarget(aDT); michael@0: } michael@0: michael@0: // We can optionally Init after construction in michael@0: // case we don't know what the DT will be at construction michael@0: // time. michael@0: cairo_t *Init(DrawTarget *aDT) michael@0: { michael@0: MOZ_ASSERT(!mDT, "Can't initialize twice!"); michael@0: mDT = aDT; michael@0: return mCairo = BorrowCairoContextFromDrawTarget(aDT); michael@0: } michael@0: michael@0: // The caller needs to call Finish if cr is non-null when michael@0: // they are done with the context. This is currently explicit michael@0: // instead of happening implicitly in the destructor to make michael@0: // what's happening in the caller more clear. It also michael@0: // let's you resume using the DrawTarget in the same scope. michael@0: void Finish() michael@0: { michael@0: if (mCairo) { michael@0: ReturnCairoContextToDrawTarget(mDT, mCairo); michael@0: mCairo = nullptr; michael@0: } michael@0: } michael@0: michael@0: ~BorrowedCairoContext() { michael@0: MOZ_ASSERT(!mCairo); michael@0: } michael@0: michael@0: cairo_t *mCairo; michael@0: private: michael@0: static cairo_t* BorrowCairoContextFromDrawTarget(DrawTarget *aDT); michael@0: static void ReturnCairoContextToDrawTarget(DrawTarget *aDT, cairo_t *aCairo); michael@0: DrawTarget *mDT; michael@0: }; michael@0: michael@0: #ifdef XP_MACOSX michael@0: /* This is a helper class that let's you borrow a CGContextRef from a michael@0: * DrawTargetCG. This is used for drawing themed widgets. michael@0: * michael@0: * Callers should check the cg member after constructing the object michael@0: * to see if it succeeded. The DrawTarget should not be used while michael@0: * the context is borrowed. */ michael@0: class BorrowedCGContext michael@0: { michael@0: public: michael@0: BorrowedCGContext() michael@0: : cg(nullptr) michael@0: , mDT(nullptr) michael@0: { } michael@0: michael@0: BorrowedCGContext(DrawTarget *aDT) michael@0: : mDT(aDT) michael@0: { michael@0: cg = BorrowCGContextFromDrawTarget(aDT); michael@0: } michael@0: michael@0: // We can optionally Init after construction in michael@0: // case we don't know what the DT will be at construction michael@0: // time. michael@0: CGContextRef Init(DrawTarget *aDT) michael@0: { michael@0: MOZ_ASSERT(!mDT, "Can't initialize twice!"); michael@0: mDT = aDT; michael@0: cg = BorrowCGContextFromDrawTarget(aDT); michael@0: return cg; michael@0: } michael@0: michael@0: // The caller needs to call Finish if cg is non-null when michael@0: // they are done with the context. This is currently explicit michael@0: // instead of happening implicitly in the destructor to make michael@0: // what's happening in the caller more clear. It also michael@0: // let's you resume using the DrawTarget in the same scope. michael@0: void Finish() michael@0: { michael@0: if (cg) { michael@0: ReturnCGContextToDrawTarget(mDT, cg); michael@0: cg = nullptr; michael@0: } michael@0: } michael@0: michael@0: ~BorrowedCGContext() { michael@0: MOZ_ASSERT(!cg); michael@0: } michael@0: michael@0: CGContextRef cg; michael@0: private: michael@0: static CGContextRef BorrowCGContextFromDrawTarget(DrawTarget *aDT); michael@0: static void ReturnCGContextToDrawTarget(DrawTarget *aDT, CGContextRef cg); michael@0: DrawTarget *mDT; michael@0: }; michael@0: #endif michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif // _MOZILLA_GFX_BORROWED_CONTEXT_H