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_BORROWED_CONTEXT_H
8 #define _MOZILLA_GFX_BORROWED_CONTEXT_H
13 # include <X11/Xlib.h>
14 # include "X11UndefineNone.h"
22 /* This is a helper class that let's you borrow an Xlib drawable from
23 * a DrawTarget. This is used for drawing themed widgets.
25 * Callers should check the Xlib drawable after constructing the object
26 * to see if it succeeded. The DrawTarget should not be used while
27 * the drawable is borrowed. */
28 class BorrowedXlibDrawable
{
30 BorrowedXlibDrawable()
37 explicit BorrowedXlibDrawable(DrawTarget
* aDT
)
46 // We can optionally Init after construction in
47 // case we don't know what the DT will be at construction
49 bool Init(DrawTarget
* aDT
);
51 // The caller needs to call Finish if drawable is non-zero when
52 // they are done with the context. This is currently explicit
53 // instead of happening implicitly in the destructor to make
54 // what's happening in the caller more clear. It also
55 // let's you resume using the DrawTarget in the same scope.
58 ~BorrowedXlibDrawable() { MOZ_ASSERT(!mDrawable
); }
60 Display
* GetDisplay() const { return mDisplay
; }
61 Drawable
GetDrawable() const { return mDrawable
; }
62 Screen
* GetScreen() const { return mScreen
; }
63 Visual
* GetVisual() const { return mVisual
; }
64 IntSize
GetSize() const { return mSize
; }
65 Point
GetOffset() const { return mOffset
; }
79 /* This is a helper class that let's you borrow a CGContextRef from a
80 * DrawTargetCG. This is used for drawing themed widgets.
82 * Callers should check the cg member after constructing the object
83 * to see if it succeeded. The DrawTarget should not be used while
84 * the context is borrowed. */
85 class BorrowedCGContext
{
87 BorrowedCGContext() : cg(nullptr), mDT(nullptr) {}
89 explicit BorrowedCGContext(DrawTarget
* aDT
) : mDT(aDT
) {
90 MOZ_ASSERT(aDT
, "Caller should check for nullptr");
91 cg
= BorrowCGContextFromDrawTarget(aDT
);
94 // We can optionally Init after construction in
95 // case we don't know what the DT will be at construction
97 CGContextRef
Init(DrawTarget
* aDT
) {
98 MOZ_ASSERT(aDT
, "Caller should check for nullptr");
99 MOZ_ASSERT(!mDT
, "Can't initialize twice!");
101 cg
= BorrowCGContextFromDrawTarget(aDT
);
105 // The caller needs to call Finish if cg is non-null when
106 // they are done with the context. This is currently explicit
107 // instead of happening implicitly in the destructor to make
108 // what's happening in the caller more clear. It also
109 // let's you resume using the DrawTarget in the same scope.
112 ReturnCGContextToDrawTarget(mDT
, cg
);
117 ~BorrowedCGContext() { MOZ_ASSERT(!cg
); }
122 static CGContextRef
BorrowCGContextFromDrawTarget(DrawTarget
* aDT
);
123 static void ReturnCGContextToDrawTarget(DrawTarget
* aDT
, CGContextRef cg
);
129 } // namespace mozilla
131 #endif // _MOZILLA_GFX_BORROWED_CONTEXT_H