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 /* service providing platform-specific native rendering for widgets */
8 
9 #ifndef nsITheme_h_
10 #define nsITheme_h_
11 
12 #include "mozilla/AlreadyAddRefed.h"
13 #include "nsISupports.h"
14 #include "nsID.h"
15 #include "nscore.h"
16 #include "Units.h"
17 
18 struct nsRect;
19 class gfxContext;
20 class nsAttrValue;
21 class nsPresContext;
22 class nsDeviceContext;
23 class nsIFrame;
24 class nsAtom;
25 class nsIWidget;
26 
27 namespace mozilla {
28 class ComputedStyle;
29 enum class StyleAppearance : uint8_t;
30 enum class StyleScrollbarWidth : uint8_t;
31 namespace layers {
32 class StackingContextHelper;
33 class RenderRootStateManager;
34 }  // namespace layers
35 namespace widget {
36 class Theme;
37 }  // namespace widget
38 namespace wr {
39 class DisplayListBuilder;
40 class IpcResourceUpdateQueue;
41 }  // namespace wr
42 }  // namespace mozilla
43 
44 // IID for the nsITheme interface
45 // {7329f760-08cb-450f-8225-dae729096dec}
46 #define NS_ITHEME_IID                                \
47   {                                                  \
48     0x7329f760, 0x08cb, 0x450f, {                    \
49       0x82, 0x25, 0xda, 0xe7, 0x29, 0x09, 0x6d, 0xec \
50     }                                                \
51   }
52 
53 /**
54  * nsITheme is a service that provides platform-specific native
55  * rendering for widgets.  In other words, it provides the necessary
56  * operations to draw a rendering object (an nsIFrame) as a native
57  * widget.
58  *
59  * All the methods on nsITheme take a rendering context or device
60  * context, a frame (the rendering object), and a widget type (one of
61  * the constants in nsThemeConstants.h).
62  */
63 class nsITheme : public nsISupports {
64  protected:
65   using LayoutDeviceIntMargin = mozilla::LayoutDeviceIntMargin;
66   using LayoutDeviceIntSize = mozilla::LayoutDeviceIntSize;
67   using LayoutDeviceIntCoord = mozilla::LayoutDeviceIntCoord;
68   using StyleAppearance = mozilla::StyleAppearance;
69   using StyleScrollbarWidth = mozilla::StyleScrollbarWidth;
70   using ComputedStyle = mozilla::ComputedStyle;
71 
72  public:
73   NS_DECLARE_STATIC_IID_ACCESSOR(NS_ITHEME_IID)
74 
75   /**
76    * Draw the actual theme background.
77    * @param aContext the context to draw into
78    * @param aFrame the frame for the widget that we're drawing
79    * @param aWidgetType the -moz-appearance value to draw
80    * @param aRect the rectangle defining the area occupied by the widget
81    * @param aDirtyRect the rectangle that needs to be drawn
82    * @param DrawOverflow whether outlines, shadows and other such overflowing
83    *        things should be drawn. Honoring this creates better results for
84    *        box-shadow, though it's not a hard requirement.
85    */
86   enum class DrawOverflow { No, Yes };
87   NS_IMETHOD DrawWidgetBackground(gfxContext* aContext, nsIFrame* aFrame,
88                                   StyleAppearance aWidgetType,
89                                   const nsRect& aRect, const nsRect& aDirtyRect,
90                                   DrawOverflow = DrawOverflow::Yes) = 0;
91 
92   /**
93    * Create WebRender commands for the theme background.
94    * @return true if the theme knows how to create WebRender commands for the
95    *         given widget type, false if DrawWidgetBackground need sto be called
96    *         instead.
97    */
CreateWebRenderCommandsForWidget(mozilla::wr::DisplayListBuilder & aBuilder,mozilla::wr::IpcResourceUpdateQueue & aResources,const mozilla::layers::StackingContextHelper & aSc,mozilla::layers::RenderRootStateManager * aManager,nsIFrame * aFrame,StyleAppearance aWidgetType,const nsRect & aRect)98   virtual bool CreateWebRenderCommandsForWidget(
99       mozilla::wr::DisplayListBuilder& aBuilder,
100       mozilla::wr::IpcResourceUpdateQueue& aResources,
101       const mozilla::layers::StackingContextHelper& aSc,
102       mozilla::layers::RenderRootStateManager* aManager, nsIFrame* aFrame,
103       StyleAppearance aWidgetType, const nsRect& aRect) {
104     return false;
105   }
106 
107   /**
108    * Returns the minimum widths of a scrollbar for a given style, that is, the
109    * minimum width for a vertical scrollbar, and the minimum height of a
110    * horizontal scrollbar.
111    */
112   enum class Overlay { No, Yes };
113   struct ScrollbarSizes {
114     LayoutDeviceIntCoord mVertical;
115     LayoutDeviceIntCoord mHorizontal;
116   };
117   virtual ScrollbarSizes GetScrollbarSizes(nsPresContext*, StyleScrollbarWidth,
118                                            Overlay) = 0;
119 
120   /**
121    * Return the border for the widget, in device pixels.
122    */
123   [[nodiscard]] virtual LayoutDeviceIntMargin GetWidgetBorder(
124       nsDeviceContext* aContext, nsIFrame* aFrame,
125       StyleAppearance aWidgetType) = 0;
126 
127   /**
128    * This method can return false to indicate that the CSS padding
129    * value should be used.  Otherwise, it will fill in aResult with the
130    * computed padding, in pixels, and return true.
131    *
132    * XXXldb This ought to be required to return true for non-containers
133    * so that we don't let specified padding that has no effect change
134    * the computed padding and potentially the size.
135    */
136   virtual bool GetWidgetPadding(nsDeviceContext* aContext, nsIFrame* aFrame,
137                                 StyleAppearance aWidgetType,
138                                 LayoutDeviceIntMargin* aResult) = 0;
139 
140   /**
141    * On entry, *aResult is positioned at 0,0 and sized to the new size
142    * of aFrame (aFrame->GetSize() may be stale and should not be used).
143    * This method can return false to indicate that no special
144    * overflow area is required by the native widget. Otherwise it will
145    * fill in aResult with the desired overflow area, in appunits, relative
146    * to the frame origin, and return true.
147    *
148    * This overflow area is used to determine what area needs to be
149    * repainted when the widget changes.  However, it does not affect the
150    * widget's size or what area is reachable by scrollbars.  (In other
151    * words, in layout terms, it affects ink overflow but not
152    * scrollable overflow.)
153    */
GetWidgetOverflow(nsDeviceContext * aContext,nsIFrame * aFrame,StyleAppearance aWidgetType,nsRect * aOverflowRect)154   virtual bool GetWidgetOverflow(nsDeviceContext* aContext, nsIFrame* aFrame,
155                                  StyleAppearance aWidgetType,
156                                  /*INOUT*/ nsRect* aOverflowRect) {
157     return false;
158   }
159 
160   /**
161    * Get the preferred content-box size of a checkbox / radio button, in app
162    * units.  Historically 9px.
163    */
GetCheckboxRadioPrefSize()164   virtual nscoord GetCheckboxRadioPrefSize() {
165     return mozilla::CSSPixel::ToAppUnits(9);
166   }
167 
168   /**
169    * Get the minimum border-box size of a widget, in *pixels* (in
170    * |aResult|).  If |aIsOverridable| is set to true, this size is a
171    * minimum size; if false, this size is the only valid size for the
172    * widget.
173    */
174   NS_IMETHOD GetMinimumWidgetSize(nsPresContext* aPresContext, nsIFrame* aFrame,
175                                   StyleAppearance aWidgetType,
176                                   mozilla::LayoutDeviceIntSize* aResult,
177                                   bool* aIsOverridable) = 0;
178 
179   enum Transparency { eOpaque = 0, eTransparent, eUnknownTransparency };
180 
181   /**
182    * Returns what we know about the transparency of the widget.
183    */
GetWidgetTransparency(nsIFrame * aFrame,StyleAppearance aWidgetType)184   virtual Transparency GetWidgetTransparency(nsIFrame* aFrame,
185                                              StyleAppearance aWidgetType) {
186     return eUnknownTransparency;
187   }
188 
189   /**
190    * Sets |*aShouldRepaint| to indicate whether an attribute or content state
191    * change should trigger a repaint.  Call with null |aAttribute| (and
192    * null |aOldValue|) for content state changes.
193    */
194   NS_IMETHOD WidgetStateChanged(nsIFrame* aFrame, StyleAppearance aWidgetType,
195                                 nsAtom* aAttribute, bool* aShouldRepaint,
196                                 const nsAttrValue* aOldValue) = 0;
197 
198   NS_IMETHOD ThemeChanged() = 0;
199 
WidgetAppearanceDependsOnWindowFocus(StyleAppearance aWidgetType)200   virtual bool WidgetAppearanceDependsOnWindowFocus(
201       StyleAppearance aWidgetType) {
202     return false;
203   }
204 
205   /**
206    * ThemeGeometryType values are used for describing themed nsIFrames in
207    * calls to nsIWidget::UpdateThemeGeometries. We don't simply pass the
208    * -moz-appearance value ("widget type") of the frame because the widget may
209    * want to treat different frames with the same -moz-appearance differently
210    * based on other properties of the frame. So we give the theme a first look
211    * at the frame in nsITheme::ThemeGeometryTypeForWidget and pass the
212    * returned ThemeGeometryType along to the widget.
213    * Each theme backend defines the ThemeGeometryType values it needs in its
214    * own nsITheme subclass. eThemeGeometryTypeUnknown is the only value that's
215    * shared between backends.
216    */
217   typedef uint8_t ThemeGeometryType;
218   enum { eThemeGeometryTypeUnknown = 0 };
219 
220   /**
221    * Returns the theme geometry type that should be used in the ThemeGeometry
222    * array that's passed to the widget using nsIWidget::UpdateThemeGeometries.
223    * A return value of eThemeGeometryTypeUnknown means that this frame will
224    * not be included in the ThemeGeometry array.
225    */
ThemeGeometryTypeForWidget(nsIFrame * aFrame,StyleAppearance aWidgetType)226   virtual ThemeGeometryType ThemeGeometryTypeForWidget(
227       nsIFrame* aFrame, StyleAppearance aWidgetType) {
228     return eThemeGeometryTypeUnknown;
229   }
230 
231   /**
232    * Can the nsITheme implementation handle this widget?
233    */
234   virtual bool ThemeSupportsWidget(nsPresContext* aPresContext,
235                                    nsIFrame* aFrame,
236                                    StyleAppearance aWidgetType) = 0;
237 
238   virtual bool WidgetIsContainer(StyleAppearance aWidgetType) = 0;
239 
240   /**
241    * Does the nsITheme implementation draw its own focus ring for this widget?
242    */
243   virtual bool ThemeDrawsFocusForWidget(nsIFrame*, StyleAppearance) = 0;
244 
245   /**
246    * Whether we want an inner focus ring for buttons and such.
247    *
248    * Usually, we don't want it if we have our own focus indicators, but windows
249    * is special, because it wants it even though focus also alters the border
250    * color and such.
251    */
ThemeWantsButtonInnerFocusRing(nsIFrame * aFrame,StyleAppearance aAppearance)252   virtual bool ThemeWantsButtonInnerFocusRing(nsIFrame* aFrame,
253                                               StyleAppearance aAppearance) {
254     return !ThemeDrawsFocusForWidget(aFrame, aAppearance);
255   }
256 
257   /**
258    * Should we insert a dropmarker inside of combobox button?
259    */
260   virtual bool ThemeNeedsComboboxDropmarker() = 0;
261 
262   virtual bool ThemeSupportsScrollbarButtons() = 0;
263 };
264 
265 NS_DEFINE_STATIC_IID_ACCESSOR(nsITheme, NS_ITHEME_IID)
266 
267 // Singleton accessor functions, these should never return null.
268 //
269 // Do not use directly, use nsPresContext::Theme instead.
270 extern already_AddRefed<nsITheme> do_GetNativeThemeDoNotUseDirectly();
271 extern already_AddRefed<nsITheme> do_GetBasicNativeThemeDoNotUseDirectly();
272 extern already_AddRefed<nsITheme> do_GetRDMThemeDoNotUseDirectly();
273 
274 // Native theme creation function, these should never return null.
275 extern already_AddRefed<mozilla::widget::Theme>
276 do_CreateNativeThemeDoNotUseDirectly();
277 
278 #endif
279