1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /*
7  * This file contains painting functions for each of the gtk2 widgets.
8  * Adapted from the gtkdrawing.c, and gtk+2.0 source.
9  */
10 
11 #include <gtk/gtk.h>
12 #include <gdk/gdkprivate.h>
13 #include <string.h>
14 #include "gtkdrawing.h"
15 #include "mozilla/Assertions.h"
16 #include "prinrval.h"
17 #include "WidgetStyleCache.h"
18 #include "nsDebug.h"
19 
20 #include <math.h>
21 #include <dlfcn.h>
22 
23 static gboolean checkbox_check_state;
24 static gboolean notebook_has_tab_gap;
25 
26 static ScrollbarGTKMetrics sScrollbarMetrics[2];
27 static ScrollbarGTKMetrics sScrollbarMetricsActive[2];
28 static ToggleGTKMetrics sCheckboxMetrics;
29 static ToggleGTKMetrics sRadioMetrics;
30 static ToolbarGTKMetrics sToolbarMetrics;
31 
32 #define ARROW_UP 0
33 #define ARROW_DOWN G_PI
34 #define ARROW_RIGHT G_PI_2
35 #define ARROW_LEFT (G_PI + G_PI_2)
36 
37 #if !GTK_CHECK_VERSION(3, 14, 0)
38 #define GTK_STATE_FLAG_CHECKED (1 << 11)
39 #endif
40 
operator +=(GtkBorder & first,const GtkBorder & second)41 static GtkBorder operator+=(GtkBorder& first, const GtkBorder& second) {
42   first.left += second.left;
43   first.right += second.right;
44   first.top += second.top;
45   first.bottom += second.bottom;
46   return first;
47 }
48 
49 static gint moz_gtk_get_tab_thickness(GtkStyleContext* style);
50 
51 static gint moz_gtk_menu_item_paint(WidgetNodeType widget, cairo_t* cr,
52                                     GdkRectangle* rect, GtkWidgetState* state,
53                                     GtkTextDirection direction);
54 
55 static GtkBorder GetMarginBorderPadding(GtkStyleContext* aStyle);
56 
57 static void Inset(GdkRectangle* rect, const GtkBorder& aBorder);
58 
59 static void InsetByMargin(GdkRectangle* rect, GtkStyleContext* style);
60 
moz_gtk_add_style_margin(GtkStyleContext * style,gint * left,gint * top,gint * right,gint * bottom)61 static void moz_gtk_add_style_margin(GtkStyleContext* style, gint* left,
62                                      gint* top, gint* right, gint* bottom) {
63   GtkBorder margin;
64 
65   gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
66                                &margin);
67   *left += margin.left;
68   *right += margin.right;
69   *top += margin.top;
70   *bottom += margin.bottom;
71 }
72 
moz_gtk_add_style_border(GtkStyleContext * style,gint * left,gint * top,gint * right,gint * bottom)73 static void moz_gtk_add_style_border(GtkStyleContext* style, gint* left,
74                                      gint* top, gint* right, gint* bottom) {
75   GtkBorder border;
76 
77   gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
78 
79   *left += border.left;
80   *right += border.right;
81   *top += border.top;
82   *bottom += border.bottom;
83 }
84 
moz_gtk_add_style_padding(GtkStyleContext * style,gint * left,gint * top,gint * right,gint * bottom)85 static void moz_gtk_add_style_padding(GtkStyleContext* style, gint* left,
86                                       gint* top, gint* right, gint* bottom) {
87   GtkBorder padding;
88 
89   gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
90 
91   *left += padding.left;
92   *right += padding.right;
93   *top += padding.top;
94   *bottom += padding.bottom;
95 }
96 
moz_gtk_add_margin_border_padding(GtkStyleContext * style,gint * left,gint * top,gint * right,gint * bottom)97 static void moz_gtk_add_margin_border_padding(GtkStyleContext* style,
98                                               gint* left, gint* top,
99                                               gint* right, gint* bottom) {
100   moz_gtk_add_style_margin(style, left, top, right, bottom);
101   moz_gtk_add_style_border(style, left, top, right, bottom);
102   moz_gtk_add_style_padding(style, left, top, right, bottom);
103 }
104 
moz_gtk_add_border_padding(GtkStyleContext * style,gint * left,gint * top,gint * right,gint * bottom)105 static void moz_gtk_add_border_padding(GtkStyleContext* style, gint* left,
106                                        gint* top, gint* right, gint* bottom) {
107   moz_gtk_add_style_border(style, left, top, right, bottom);
108   moz_gtk_add_style_padding(style, left, top, right, bottom);
109 }
110 
111 // GetStateFlagsFromGtkWidgetState() can be safely used for the specific
112 // GtkWidgets that set both prelight and active flags.  For other widgets,
113 // either the GtkStateFlags or Gecko's GtkWidgetState need to be carefully
114 // adjusted to match GTK behavior.  Although GTK sets insensitive and focus
115 // flags in the generic GtkWidget base class, GTK adds prelight and active
116 // flags only to widgets that are expected to demonstrate prelight or active
117 // states.  This contrasts with HTML where any element may have :active and
118 // :hover states, and so Gecko's GtkStateFlags do not necessarily map to GTK
119 // flags.  Failure to restrict the flags in the same way as GTK can cause
120 // generic CSS selectors from some themes to unintentionally match elements
121 // that are not expected to change appearance on hover or mouse-down.
GetStateFlagsFromGtkWidgetState(GtkWidgetState * state)122 static GtkStateFlags GetStateFlagsFromGtkWidgetState(GtkWidgetState* state) {
123   GtkStateFlags stateFlags = GTK_STATE_FLAG_NORMAL;
124 
125   if (state->disabled)
126     stateFlags = GTK_STATE_FLAG_INSENSITIVE;
127   else {
128     if (state->depressed || state->active)
129       stateFlags =
130           static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_ACTIVE);
131     if (state->inHover)
132       stateFlags =
133           static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_PRELIGHT);
134     if (state->focused)
135       stateFlags =
136           static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_FOCUSED);
137   }
138 
139   return stateFlags;
140 }
141 
GetStateFlagsFromGtkTabFlags(GtkTabFlags flags)142 static GtkStateFlags GetStateFlagsFromGtkTabFlags(GtkTabFlags flags) {
143   return ((flags & MOZ_GTK_TAB_SELECTED) == 0) ? GTK_STATE_FLAG_NORMAL
144                                                : GTK_STATE_FLAG_ACTIVE;
145 }
146 
moz_gtk_init()147 gint moz_gtk_init() {
148   if (gtk_major_version > 3 ||
149       (gtk_major_version == 3 && gtk_minor_version >= 14))
150     checkbox_check_state = GTK_STATE_FLAG_CHECKED;
151   else
152     checkbox_check_state = GTK_STATE_FLAG_ACTIVE;
153 
154   moz_gtk_refresh();
155 
156   return MOZ_GTK_SUCCESS;
157 }
158 
moz_gtk_refresh()159 void moz_gtk_refresh() {
160   if (gtk_check_version(3, 12, 0) == nullptr &&
161       gtk_check_version(3, 20, 0) != nullptr) {
162     // Deprecated for Gtk >= 3.20+
163     GtkStyleContext* style = GetStyleContext(MOZ_GTK_TAB_TOP);
164     gtk_style_context_get_style(style, "has-tab-gap", &notebook_has_tab_gap,
165                                 NULL);
166   } else {
167     notebook_has_tab_gap = true;
168   }
169 
170   sScrollbarMetrics[GTK_ORIENTATION_HORIZONTAL].initialized = false;
171   sScrollbarMetrics[GTK_ORIENTATION_VERTICAL].initialized = false;
172   sScrollbarMetricsActive[GTK_ORIENTATION_HORIZONTAL].initialized = false;
173   sScrollbarMetricsActive[GTK_ORIENTATION_VERTICAL].initialized = false;
174   sCheckboxMetrics.initialized = false;
175   sRadioMetrics.initialized = false;
176   sToolbarMetrics.initialized = false;
177 
178   /* This will destroy all of our widgets */
179   ResetWidgetCache();
180 }
181 
moz_gtk_checkbox_get_metrics(gint * indicator_size,gint * indicator_spacing)182 gint moz_gtk_checkbox_get_metrics(gint* indicator_size,
183                                   gint* indicator_spacing) {
184   gtk_widget_style_get(GetWidget(MOZ_GTK_CHECKBUTTON_CONTAINER),
185                        "indicator_size", indicator_size, "indicator_spacing",
186                        indicator_spacing, NULL);
187   return MOZ_GTK_SUCCESS;
188 }
189 
moz_gtk_radio_get_metrics(gint * indicator_size,gint * indicator_spacing)190 gint moz_gtk_radio_get_metrics(gint* indicator_size, gint* indicator_spacing) {
191   gtk_widget_style_get(GetWidget(MOZ_GTK_RADIOBUTTON_CONTAINER),
192                        "indicator_size", indicator_size, "indicator_spacing",
193                        indicator_spacing, NULL);
194   return MOZ_GTK_SUCCESS;
195 }
196 
moz_gtk_get_focus_outline_size(GtkStyleContext * style,gint * focus_h_width,gint * focus_v_width)197 static gint moz_gtk_get_focus_outline_size(GtkStyleContext* style,
198                                            gint* focus_h_width,
199                                            gint* focus_v_width) {
200   GtkBorder border;
201   GtkBorder padding;
202   gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
203   gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
204   *focus_h_width = border.left + padding.left;
205   *focus_v_width = border.top + padding.top;
206   return MOZ_GTK_SUCCESS;
207 }
208 
moz_gtk_get_focus_outline_size(gint * focus_h_width,gint * focus_v_width)209 gint moz_gtk_get_focus_outline_size(gint* focus_h_width, gint* focus_v_width) {
210   GtkStyleContext* style = GetStyleContext(MOZ_GTK_ENTRY);
211   moz_gtk_get_focus_outline_size(style, focus_h_width, focus_v_width);
212   return MOZ_GTK_SUCCESS;
213 }
214 
moz_gtk_menuitem_get_horizontal_padding(gint * horizontal_padding)215 gint moz_gtk_menuitem_get_horizontal_padding(gint* horizontal_padding) {
216   GtkStyleContext* style = GetStyleContext(MOZ_GTK_MENUITEM);
217   gtk_style_context_get_style(style, "horizontal-padding", horizontal_padding,
218                               nullptr);
219   return MOZ_GTK_SUCCESS;
220 }
221 
moz_gtk_checkmenuitem_get_horizontal_padding(gint * horizontal_padding)222 gint moz_gtk_checkmenuitem_get_horizontal_padding(gint* horizontal_padding) {
223   GtkStyleContext* style = GetStyleContext(MOZ_GTK_CHECKMENUITEM);
224   gtk_style_context_get_style(style, "horizontal-padding", horizontal_padding,
225                               nullptr);
226   return MOZ_GTK_SUCCESS;
227 }
228 
moz_gtk_button_get_default_overflow(gint * border_top,gint * border_left,gint * border_bottom,gint * border_right)229 gint moz_gtk_button_get_default_overflow(gint* border_top, gint* border_left,
230                                          gint* border_bottom,
231                                          gint* border_right) {
232   GtkBorder* default_outside_border;
233 
234   GtkStyleContext* style = GetStyleContext(MOZ_GTK_BUTTON);
235   gtk_style_context_get_style(style, "default-outside-border",
236                               &default_outside_border, NULL);
237 
238   if (default_outside_border) {
239     *border_top = default_outside_border->top;
240     *border_left = default_outside_border->left;
241     *border_bottom = default_outside_border->bottom;
242     *border_right = default_outside_border->right;
243     gtk_border_free(default_outside_border);
244   } else {
245     *border_top = *border_left = *border_bottom = *border_right = 0;
246   }
247   return MOZ_GTK_SUCCESS;
248 }
249 
moz_gtk_button_get_default_border(gint * border_top,gint * border_left,gint * border_bottom,gint * border_right)250 static gint moz_gtk_button_get_default_border(gint* border_top,
251                                               gint* border_left,
252                                               gint* border_bottom,
253                                               gint* border_right) {
254   GtkBorder* default_border;
255 
256   GtkStyleContext* style = GetStyleContext(MOZ_GTK_BUTTON);
257   gtk_style_context_get_style(style, "default-border", &default_border, NULL);
258 
259   if (default_border) {
260     *border_top = default_border->top;
261     *border_left = default_border->left;
262     *border_bottom = default_border->bottom;
263     *border_right = default_border->right;
264     gtk_border_free(default_border);
265   } else {
266     /* see gtkbutton.c */
267     *border_top = *border_left = *border_bottom = *border_right = 1;
268   }
269   return MOZ_GTK_SUCCESS;
270 }
271 
moz_gtk_splitter_get_metrics(gint orientation,gint * size)272 gint moz_gtk_splitter_get_metrics(gint orientation, gint* size) {
273   GtkStyleContext* style;
274   if (orientation == GTK_ORIENTATION_HORIZONTAL) {
275     style = GetStyleContext(MOZ_GTK_SPLITTER_HORIZONTAL);
276   } else {
277     style = GetStyleContext(MOZ_GTK_SPLITTER_VERTICAL);
278   }
279   gtk_style_context_get_style(style, "handle_size", size, NULL);
280   return MOZ_GTK_SUCCESS;
281 }
282 
CalculateToolbarButtonMetrics(WidgetNodeType aWidgetType,ToolbarButtonGTKMetrics * aMetrics)283 static void CalculateToolbarButtonMetrics(WidgetNodeType aWidgetType,
284                                           ToolbarButtonGTKMetrics* aMetrics) {
285   gint iconWidth, iconHeight;
286   if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &iconWidth, &iconHeight)) {
287     NS_WARNING("Failed to get Gtk+ icon size for titlebar button!");
288 
289     // Use some reasonable fallback size
290     iconWidth = 16;
291     iconHeight = 16;
292   }
293 
294   GtkStyleContext* style = GetStyleContext(aWidgetType);
295   gint width = 0, height = 0;
296   if (gtk_check_version(3, 20, 0) == nullptr) {
297     gtk_style_context_get(style, gtk_style_context_get_state(style),
298                           "min-width", &width, "min-height", &height, NULL);
299   }
300 
301   // Cover cases when min-width/min-height is not set, it's invalid
302   // or we're running on Gtk+ < 3.20.
303   if (width < iconWidth) width = iconWidth;
304   if (height < iconHeight) height = iconHeight;
305 
306   gint left = 0, top = 0, right = 0, bottom = 0;
307   moz_gtk_add_border_padding(style, &left, &top, &right, &bottom);
308 
309   // Button size is calculated as min-width/height + border/padding.
310   width += left + right;
311   height += top + bottom;
312 
313   // Place icon at button center.
314   aMetrics->iconXPosition = (width - iconWidth) / 2;
315   aMetrics->iconYPosition = (height - iconHeight) / 2;
316 
317   aMetrics->minSizeWithBorderMargin.width = width;
318   aMetrics->minSizeWithBorderMargin.height = height;
319 }
320 
321 // We support LTR layout only here for now.
CalculateToolbarButtonSpacing(WidgetNodeType aWidgetType,ToolbarButtonGTKMetrics * aMetrics)322 static void CalculateToolbarButtonSpacing(WidgetNodeType aWidgetType,
323                                           ToolbarButtonGTKMetrics* aMetrics) {
324   GtkStyleContext* style = GetStyleContext(aWidgetType);
325   gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
326                                &aMetrics->buttonMargin);
327 
328   // Get titlebar spacing, a default one is 6 pixels (gtk/gtkheaderbar.c)
329   gint buttonSpacing = 6;
330   g_object_get(GetWidget(MOZ_GTK_HEADER_BAR), "spacing", &buttonSpacing,
331                nullptr);
332 
333   // We apply spacing as a margin equaly to both adjacent buttons.
334   buttonSpacing /= 2;
335 
336   if (!aMetrics->firstButton) {
337     aMetrics->buttonMargin.left += buttonSpacing;
338   }
339   if (!aMetrics->lastButton) {
340     aMetrics->buttonMargin.right += buttonSpacing;
341   }
342 
343   aMetrics->iconXPosition += aMetrics->buttonMargin.left;
344   aMetrics->iconYPosition += aMetrics->buttonMargin.top;
345 
346   aMetrics->minSizeWithBorderMargin.width +=
347       aMetrics->buttonMargin.right + aMetrics->buttonMargin.left;
348   aMetrics->minSizeWithBorderMargin.height +=
349       aMetrics->buttonMargin.top + aMetrics->buttonMargin.bottom;
350 }
351 
GetGtkHeaderBarButtonLayout(WidgetNodeType * aButtonLayout,int aMaxButtonNums)352 int GetGtkHeaderBarButtonLayout(WidgetNodeType* aButtonLayout,
353                                 int aMaxButtonNums) {
354   NS_ASSERTION(aMaxButtonNums >= TOOLBAR_BUTTONS,
355                "Requested number of buttons is higher than storage capacity!");
356 
357   static auto sGtkHeaderBarGetDecorationLayoutPtr =
358       (const gchar* (*)(GtkWidget*))dlsym(
359           RTLD_DEFAULT, "gtk_header_bar_get_decoration_layout");
360 
361   const gchar* decorationLayout = nullptr;
362   if (sGtkHeaderBarGetDecorationLayoutPtr) {
363     GtkWidget* headerBar = GetWidget(MOZ_GTK_HEADER_BAR);
364     decorationLayout = sGtkHeaderBarGetDecorationLayoutPtr(headerBar);
365     if (!decorationLayout) {
366       GtkSettings* settings =
367           gtk_settings_get_for_screen(gdk_screen_get_default());
368       g_object_get(settings, "gtk-decoration-layout", &decorationLayout,
369                    nullptr);
370     }
371   }
372 
373   // Use a default layout
374   if (!decorationLayout) {
375     decorationLayout = "minimize,maximize,close";
376   }
377 
378   // We support only default button order now:
379   // minimize/maximize/close
380   int activeButtonNums = 0;
381   if (strstr(decorationLayout, "minimize") != nullptr) {
382     aButtonLayout[activeButtonNums++] = MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE;
383   }
384   if (strstr(decorationLayout, "maximize") != nullptr) {
385     aButtonLayout[activeButtonNums++] = MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE;
386   }
387   if (strstr(decorationLayout, "close") != nullptr) {
388     aButtonLayout[activeButtonNums++] = MOZ_GTK_HEADER_BAR_BUTTON_CLOSE;
389   }
390 
391   return activeButtonNums;
392 }
393 
EnsureToolbarMetrics(void)394 static void EnsureToolbarMetrics(void) {
395   if (!sToolbarMetrics.initialized) {
396     // Make sure we have clean cache after theme reset, etc.
397     memset(&sToolbarMetrics, 0, sizeof(sToolbarMetrics));
398 
399     // We're running on old Gtk+ version. Leave the cache empty
400     // which means all buttons are disabled.
401     if (gtk_check_version(3, 10, 0) != nullptr) {
402       sToolbarMetrics.initialized = true;
403       return;
404     }
405 
406     // Calculate titlebar button visibility and positions.
407     WidgetNodeType aButtonLayout[TOOLBAR_BUTTONS];
408     int activeButtonNums =
409         GetGtkHeaderBarButtonLayout(aButtonLayout, TOOLBAR_BUTTONS);
410 
411     for (int i = 0; i < activeButtonNums; i++) {
412       int buttonIndex = (aButtonLayout[i] - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE);
413       ToolbarButtonGTKMetrics* metrics = sToolbarMetrics.button + buttonIndex;
414       metrics->visible = true;
415       // Mark first button
416       if (!i) {
417         metrics->firstButton = true;
418       }
419       // Mark last button.
420       if (i == (activeButtonNums - 1)) {
421         metrics->lastButton = true;
422       }
423 
424       CalculateToolbarButtonMetrics(aButtonLayout[i], metrics);
425       CalculateToolbarButtonSpacing(aButtonLayout[i], metrics);
426     }
427 
428     sToolbarMetrics.initialized = true;
429   }
430 }
431 
GetToolbarButtonMetrics(WidgetNodeType aWidgetType)432 const ToolbarButtonGTKMetrics* GetToolbarButtonMetrics(
433     WidgetNodeType aWidgetType) {
434   EnsureToolbarMetrics();
435 
436   int buttonIndex = (aWidgetType - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE);
437   NS_ASSERTION(buttonIndex >= 0 && buttonIndex <= TOOLBAR_BUTTONS,
438                "GetToolbarButtonMetrics(): Wrong titlebar button!");
439   return sToolbarMetrics.button + buttonIndex;
440 }
441 
moz_gtk_window_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)442 static gint moz_gtk_window_paint(cairo_t* cr, GdkRectangle* rect,
443                                  GtkTextDirection direction) {
444   GtkStyleContext* style = GetStyleContext(MOZ_GTK_WINDOW, direction);
445 
446   gtk_style_context_save(style);
447   gtk_style_context_add_class(style, GTK_STYLE_CLASS_BACKGROUND);
448   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
449   gtk_style_context_restore(style);
450 
451   return MOZ_GTK_SUCCESS;
452 }
453 
moz_gtk_button_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkReliefStyle relief,GtkWidget * widget,GtkTextDirection direction)454 static gint moz_gtk_button_paint(cairo_t* cr, GdkRectangle* rect,
455                                  GtkWidgetState* state, GtkReliefStyle relief,
456                                  GtkWidget* widget,
457                                  GtkTextDirection direction) {
458   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
459   GtkStyleContext* style = gtk_widget_get_style_context(widget);
460   gint x = rect->x, y = rect->y, width = rect->width, height = rect->height;
461 
462   gtk_widget_set_direction(widget, direction);
463 
464   gtk_style_context_save(style);
465   gtk_style_context_set_state(style, state_flags);
466 
467   if (state->isDefault && relief == GTK_RELIEF_NORMAL) {
468     /* handle default borders both outside and inside the button */
469     gint default_top, default_left, default_bottom, default_right;
470     moz_gtk_button_get_default_overflow(&default_top, &default_left,
471                                         &default_bottom, &default_right);
472     x -= default_left;
473     y -= default_top;
474     width += default_left + default_right;
475     height += default_top + default_bottom;
476     gtk_render_background(style, cr, x, y, width, height);
477     gtk_render_frame(style, cr, x, y, width, height);
478     moz_gtk_button_get_default_border(&default_top, &default_left,
479                                       &default_bottom, &default_right);
480     x += default_left;
481     y += default_top;
482     width -= (default_left + default_right);
483     height -= (default_top + default_bottom);
484   } else if (relief != GTK_RELIEF_NONE || state->depressed ||
485              (state_flags & GTK_STATE_FLAG_PRELIGHT)) {
486     /* the following line can trigger an assertion (Crux theme)
487        file ../../gdk/gdkwindow.c: line 1846 (gdk_window_clear_area):
488        assertion `GDK_IS_WINDOW (window)' failed */
489     gtk_render_background(style, cr, x, y, width, height);
490     gtk_render_frame(style, cr, x, y, width, height);
491   }
492 
493   if (state->focused) {
494     GtkBorder border;
495     gtk_style_context_get_border(style, state_flags, &border);
496     x += border.left;
497     y += border.top;
498     width -= (border.left + border.right);
499     height -= (border.top + border.bottom);
500     gtk_render_focus(style, cr, x, y, width, height);
501   }
502   gtk_style_context_restore(style);
503   return MOZ_GTK_SUCCESS;
504 }
505 
moz_gtk_header_bar_button_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkReliefStyle relief,WidgetNodeType aIconWidgetType,GtkTextDirection direction)506 static gint moz_gtk_header_bar_button_paint(cairo_t* cr, GdkRectangle* rect,
507                                             GtkWidgetState* state,
508                                             GtkReliefStyle relief,
509                                             WidgetNodeType aIconWidgetType,
510                                             GtkTextDirection direction) {
511   WidgetNodeType buttonWidgetType =
512       (aIconWidgetType == MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE)
513           ? MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE
514           : aIconWidgetType;
515 
516   // We need to inset our calculated margin because it also
517   // contains titlebar button spacing.
518   const ToolbarButtonGTKMetrics* metrics =
519       GetToolbarButtonMetrics(buttonWidgetType);
520   Inset(rect, metrics->buttonMargin);
521 
522   GtkWidget* buttonWidget = GetWidget(buttonWidgetType);
523   moz_gtk_button_paint(cr, rect, state, relief, buttonWidget, direction);
524 
525   GtkWidget* iconWidget =
526       gtk_bin_get_child(GTK_BIN(GetWidget(aIconWidgetType)));
527   cairo_surface_t* surface = GetWidgetIconSurface(iconWidget, state->scale);
528 
529   if (surface) {
530     GtkStyleContext* style = gtk_widget_get_style_context(buttonWidget);
531     GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
532 
533     gtk_style_context_save(style);
534     gtk_style_context_set_state(style, state_flags);
535 
536     const ToolbarButtonGTKMetrics* metrics =
537         GetToolbarButtonMetrics(buttonWidgetType);
538 
539     /* This is available since Gtk+ 3.10 as well as GtkHeaderBar */
540     static auto sGtkRenderIconSurfacePtr =
541         (void (*)(GtkStyleContext*, cairo_t*, cairo_surface_t*, gdouble,
542                   gdouble))dlsym(RTLD_DEFAULT, "gtk_render_icon_surface");
543 
544     sGtkRenderIconSurfacePtr(style, cr, surface, metrics->iconXPosition,
545                              metrics->iconYPosition);
546     gtk_style_context_restore(style);
547   }
548 
549   return MOZ_GTK_SUCCESS;
550 }
551 
moz_gtk_toggle_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,gboolean selected,gboolean inconsistent,gboolean isradio,GtkTextDirection direction)552 static gint moz_gtk_toggle_paint(cairo_t* cr, GdkRectangle* rect,
553                                  GtkWidgetState* state, gboolean selected,
554                                  gboolean inconsistent, gboolean isradio,
555                                  GtkTextDirection direction) {
556   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
557   gint x, y, width, height;
558   GtkStyleContext* style;
559 
560   const ToggleGTKMetrics* metrics = GetToggleMetrics(isradio);
561 
562   // Clamp the rect and paint it center aligned in the rect.
563   x = rect->x;
564   y = rect->y;
565   width = rect->width;
566   height = rect->height;
567 
568   if (rect->width < rect->height) {
569     y = rect->y + (rect->height - rect->width) / 2;
570     height = rect->width;
571   }
572 
573   if (rect->height < rect->width) {
574     x = rect->x + (rect->width - rect->height) / 2;
575     width = rect->height;
576   }
577 
578   if (selected)
579     state_flags =
580         static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
581 
582   if (inconsistent)
583     state_flags =
584         static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_INCONSISTENT);
585 
586   style = GetStyleContext(isradio ? MOZ_GTK_RADIOBUTTON : MOZ_GTK_CHECKBUTTON,
587                           direction, state_flags);
588 
589   if (gtk_check_version(3, 20, 0) == nullptr) {
590     gtk_render_background(style, cr, x, y, width, height);
591     gtk_render_frame(style, cr, x, y, width, height);
592     // Indicator is inset by the toggle's padding and border.
593     gint indicator_x = x + metrics->borderAndPadding.left;
594     gint indicator_y = y + metrics->borderAndPadding.top;
595     gint indicator_width = metrics->minSizeWithBorder.width -
596                            metrics->borderAndPadding.left -
597                            metrics->borderAndPadding.right;
598     gint indicator_height = metrics->minSizeWithBorder.height -
599                             metrics->borderAndPadding.top -
600                             metrics->borderAndPadding.bottom;
601     if (isradio) {
602       gtk_render_option(style, cr, indicator_x, indicator_y, indicator_width,
603                         indicator_height);
604     } else {
605       gtk_render_check(style, cr, indicator_x, indicator_y, indicator_width,
606                        indicator_height);
607     }
608   } else {
609     if (isradio) {
610       gtk_render_option(style, cr, x, y, width, height);
611     } else {
612       gtk_render_check(style, cr, x, y, width, height);
613     }
614   }
615 
616   return MOZ_GTK_SUCCESS;
617 }
618 
calculate_button_inner_rect(GtkWidget * button,GdkRectangle * rect,GdkRectangle * inner_rect,GtkTextDirection direction)619 static gint calculate_button_inner_rect(GtkWidget* button, GdkRectangle* rect,
620                                         GdkRectangle* inner_rect,
621                                         GtkTextDirection direction) {
622   GtkStyleContext* style;
623   GtkBorder border;
624   GtkBorder padding = {0, 0, 0, 0};
625 
626   style = gtk_widget_get_style_context(button);
627 
628   /* This mirrors gtkbutton's child positioning */
629   gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
630   gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
631 
632   inner_rect->x = rect->x + border.left + padding.left;
633   inner_rect->y = rect->y + padding.top + border.top;
634   inner_rect->width =
635       MAX(1, rect->width - padding.left - padding.right - border.left * 2);
636   inner_rect->height =
637       MAX(1, rect->height - padding.top - padding.bottom - border.top * 2);
638 
639   return MOZ_GTK_SUCCESS;
640 }
641 
calculate_arrow_rect(GtkWidget * arrow,GdkRectangle * rect,GdkRectangle * arrow_rect,GtkTextDirection direction)642 static gint calculate_arrow_rect(GtkWidget* arrow, GdkRectangle* rect,
643                                  GdkRectangle* arrow_rect,
644                                  GtkTextDirection direction) {
645   /* defined in gtkarrow.c */
646   gfloat arrow_scaling = 0.7;
647   gfloat xalign, xpad;
648   gint extent;
649   gint mxpad, mypad;
650   gfloat mxalign, myalign;
651   GtkMisc* misc = GTK_MISC(arrow);
652 
653   gtk_style_context_get_style(gtk_widget_get_style_context(arrow),
654                               "arrow_scaling", &arrow_scaling, NULL);
655 
656   gtk_misc_get_padding(misc, &mxpad, &mypad);
657   extent = MIN((rect->width - mxpad * 2), (rect->height - mypad * 2)) *
658            arrow_scaling;
659 
660   gtk_misc_get_alignment(misc, &mxalign, &myalign);
661 
662   xalign = direction == GTK_TEXT_DIR_LTR ? mxalign : 1.0 - mxalign;
663   xpad = mxpad + (rect->width - extent) * xalign;
664 
665   arrow_rect->x = direction == GTK_TEXT_DIR_LTR ? floor(rect->x + xpad)
666                                                 : ceil(rect->x + xpad);
667   arrow_rect->y = floor(rect->y + mypad + ((rect->height - extent) * myalign));
668 
669   arrow_rect->width = arrow_rect->height = extent;
670 
671   return MOZ_GTK_SUCCESS;
672 }
673 
GetMinContentBox(GtkStyleContext * style)674 static MozGtkSize GetMinContentBox(GtkStyleContext* style) {
675   GtkStateFlags state_flags = gtk_style_context_get_state(style);
676   gint width, height;
677   gtk_style_context_get(style, state_flags, "min-width", &width, "min-height",
678                         &height, nullptr);
679   return {width, height};
680 }
681 
682 /**
683  * Get minimum widget size as sum of margin, padding, border and
684  * min-width/min-height.
685  */
moz_gtk_get_widget_min_size(GtkStyleContext * style,int * width,int * height)686 static void moz_gtk_get_widget_min_size(GtkStyleContext* style, int* width,
687                                         int* height) {
688   GtkStateFlags state_flags = gtk_style_context_get_state(style);
689   gtk_style_context_get(style, state_flags, "min-height", height, "min-width",
690                         width, nullptr);
691 
692   GtkBorder border, padding, margin;
693   gtk_style_context_get_border(style, state_flags, &border);
694   gtk_style_context_get_padding(style, state_flags, &padding);
695   gtk_style_context_get_margin(style, state_flags, &margin);
696 
697   *width += border.left + border.right + margin.left + margin.right +
698             padding.left + padding.right;
699   *height += border.top + border.bottom + margin.top + margin.bottom +
700              padding.top + padding.bottom;
701 }
702 
GetMinMarginBox(GtkStyleContext * style)703 static MozGtkSize GetMinMarginBox(GtkStyleContext* style) {
704   gint width, height;
705   moz_gtk_get_widget_min_size(style, &width, &height);
706   return {width, height};
707 }
708 
Inset(GdkRectangle * rect,const GtkBorder & aBorder)709 static void Inset(GdkRectangle* rect, const GtkBorder& aBorder) {
710   MOZ_ASSERT(rect);
711   rect->x += aBorder.left;
712   rect->y += aBorder.top;
713   rect->width -= aBorder.left + aBorder.right;
714   rect->height -= aBorder.top + aBorder.bottom;
715 }
716 
717 // Inset a rectangle by the margins specified in a style context.
InsetByMargin(GdkRectangle * rect,GtkStyleContext * style)718 static void InsetByMargin(GdkRectangle* rect, GtkStyleContext* style) {
719   MOZ_ASSERT(rect);
720   GtkBorder margin;
721 
722   gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
723                                &margin);
724   Inset(rect, margin);
725 }
726 
727 // Inset a rectangle by the border and padding specified in a style context.
InsetByBorderPadding(GdkRectangle * rect,GtkStyleContext * style)728 static void InsetByBorderPadding(GdkRectangle* rect, GtkStyleContext* style) {
729   GtkStateFlags state = gtk_style_context_get_state(style);
730   GtkBorder padding, border;
731 
732   gtk_style_context_get_padding(style, state, &padding);
733   Inset(rect, padding);
734   gtk_style_context_get_border(style, state, &border);
735   Inset(rect, border);
736 }
737 
moz_gtk_scrollbar_button_paint(cairo_t * cr,const GdkRectangle * aRect,GtkWidgetState * state,GtkScrollbarButtonFlags flags,GtkTextDirection direction)738 static gint moz_gtk_scrollbar_button_paint(cairo_t* cr,
739                                            const GdkRectangle* aRect,
740                                            GtkWidgetState* state,
741                                            GtkScrollbarButtonFlags flags,
742                                            GtkTextDirection direction) {
743   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
744   GdkRectangle arrow_rect;
745   gdouble arrow_angle;
746   GtkStyleContext* style;
747   gint arrow_displacement_x, arrow_displacement_y;
748 
749   GtkWidget* scrollbar = GetWidget(flags & MOZ_GTK_STEPPER_VERTICAL
750                                        ? MOZ_GTK_SCROLLBAR_VERTICAL
751                                        : MOZ_GTK_SCROLLBAR_HORIZONTAL);
752 
753   gtk_widget_set_direction(scrollbar, direction);
754 
755   if (flags & MOZ_GTK_STEPPER_VERTICAL) {
756     arrow_angle = (flags & MOZ_GTK_STEPPER_DOWN) ? ARROW_DOWN : ARROW_UP;
757   } else {
758     arrow_angle = (flags & MOZ_GTK_STEPPER_DOWN) ? ARROW_RIGHT : ARROW_LEFT;
759   }
760 
761   style = gtk_widget_get_style_context(scrollbar);
762 
763   gtk_style_context_save(style);
764   gtk_style_context_add_class(style, GTK_STYLE_CLASS_BUTTON);
765   gtk_style_context_set_state(style, state_flags);
766   if (arrow_angle == ARROW_RIGHT) {
767     gtk_style_context_add_class(style, GTK_STYLE_CLASS_RIGHT);
768   } else if (arrow_angle == ARROW_DOWN) {
769     gtk_style_context_add_class(style, GTK_STYLE_CLASS_BOTTOM);
770   } else if (arrow_angle == ARROW_LEFT) {
771     gtk_style_context_add_class(style, GTK_STYLE_CLASS_LEFT);
772   } else {
773     gtk_style_context_add_class(style, GTK_STYLE_CLASS_TOP);
774   }
775 
776   GdkRectangle rect = *aRect;
777   if (gtk_check_version(3, 20, 0) == nullptr) {
778     // The "trough-border" is not used since GTK 3.20.  The stepper margin
779     // box occupies the full width of the "contents" gadget content box.
780     InsetByMargin(&rect, style);
781   } else {
782     // Scrollbar button has to be inset by trough_border because its DOM
783     // element is filling width of vertical scrollbar's track (or height
784     // in case of horizontal scrollbars).
785     GtkOrientation orientation = flags & MOZ_GTK_STEPPER_VERTICAL
786                                      ? GTK_ORIENTATION_VERTICAL
787                                      : GTK_ORIENTATION_HORIZONTAL;
788 
789     const ScrollbarGTKMetrics* metrics = GetScrollbarMetrics(orientation);
790     if (flags & MOZ_GTK_STEPPER_VERTICAL) {
791       rect.x += metrics->border.track.left;
792       rect.width = metrics->size.thumb.width;
793     } else {
794       rect.y += metrics->border.track.top;
795       rect.height = metrics->size.thumb.height;
796     }
797   }
798 
799   gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
800   gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
801 
802   arrow_rect.width = rect.width / 2;
803   arrow_rect.height = rect.height / 2;
804 
805   gfloat arrow_scaling;
806   gtk_style_context_get_style(style, "arrow-scaling", &arrow_scaling, NULL);
807 
808   gdouble arrow_size = MIN(rect.width, rect.height) * arrow_scaling;
809   arrow_rect.x = rect.x + (rect.width - arrow_size) / 2;
810   arrow_rect.y = rect.y + (rect.height - arrow_size) / 2;
811 
812   if (state_flags & GTK_STATE_FLAG_ACTIVE) {
813     gtk_style_context_get_style(style, "arrow-displacement-x",
814                                 &arrow_displacement_x, "arrow-displacement-y",
815                                 &arrow_displacement_y, NULL);
816 
817     arrow_rect.x += arrow_displacement_x;
818     arrow_rect.y += arrow_displacement_y;
819   }
820 
821   gtk_render_arrow(style, cr, arrow_angle, arrow_rect.x, arrow_rect.y,
822                    arrow_size);
823 
824   gtk_style_context_restore(style);
825 
826   return MOZ_GTK_SUCCESS;
827 }
828 
moz_gtk_update_scrollbar_style(GtkStyleContext * style,WidgetNodeType widget,GtkTextDirection direction)829 static void moz_gtk_update_scrollbar_style(GtkStyleContext* style,
830                                            WidgetNodeType widget,
831                                            GtkTextDirection direction) {
832   if (widget == MOZ_GTK_SCROLLBAR_HORIZONTAL) {
833     gtk_style_context_add_class(style, GTK_STYLE_CLASS_BOTTOM);
834   } else {
835     if (direction == GTK_TEXT_DIR_LTR) {
836       gtk_style_context_add_class(style, GTK_STYLE_CLASS_RIGHT);
837       gtk_style_context_remove_class(style, GTK_STYLE_CLASS_LEFT);
838     } else {
839       gtk_style_context_add_class(style, GTK_STYLE_CLASS_LEFT);
840       gtk_style_context_remove_class(style, GTK_STYLE_CLASS_RIGHT);
841     }
842   }
843 }
844 
moz_gtk_draw_styled_frame(GtkStyleContext * style,cairo_t * cr,const GdkRectangle * aRect,bool drawFocus)845 static void moz_gtk_draw_styled_frame(GtkStyleContext* style, cairo_t* cr,
846                                       const GdkRectangle* aRect,
847                                       bool drawFocus) {
848   GdkRectangle rect = *aRect;
849   if (gtk_check_version(3, 6, 0) == nullptr) {
850     InsetByMargin(&rect, style);
851   }
852   gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
853   gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
854   if (drawFocus) {
855     gtk_render_focus(style, cr, rect.x, rect.y, rect.width, rect.height);
856   }
857 }
858 
moz_gtk_scrollbar_trough_paint(WidgetNodeType widget,cairo_t * cr,const GdkRectangle * aRect,GtkWidgetState * state,GtkTextDirection direction)859 static gint moz_gtk_scrollbar_trough_paint(WidgetNodeType widget, cairo_t* cr,
860                                            const GdkRectangle* aRect,
861                                            GtkWidgetState* state,
862                                            GtkTextDirection direction) {
863   GdkRectangle rect = *aRect;
864   GtkStyleContext* style;
865 
866   if (gtk_get_minor_version() >= 20) {
867     WidgetNodeType thumb = widget == MOZ_GTK_SCROLLBAR_TROUGH_VERTICAL
868                                ? MOZ_GTK_SCROLLBAR_THUMB_VERTICAL
869                                : MOZ_GTK_SCROLLBAR_THUMB_HORIZONTAL;
870     MozGtkSize thumbSize = GetMinMarginBox(GetStyleContext(thumb));
871     style = GetStyleContext(widget, direction);
872     MozGtkSize trackSize = GetMinContentBox(style);
873     trackSize.Include(thumbSize);
874     trackSize += GetMarginBorderPadding(style);
875     // Gecko's trough |aRect| fills available breadth, but GTK's trough is
876     // centered in the contents_gadget.  The centering here round left
877     // and up, like gtk_box_gadget_allocate_child().
878     if (widget == MOZ_GTK_SCROLLBAR_TROUGH_VERTICAL) {
879       rect.x += (rect.width - trackSize.width) / 2;
880       rect.width = trackSize.width;
881     } else {
882       rect.y += (rect.height - trackSize.height) / 2;
883       rect.height = trackSize.height;
884     }
885   } else {
886     style = GetStyleContext(widget, direction);
887   }
888 
889   moz_gtk_draw_styled_frame(style, cr, &rect, state->focused);
890 
891   return MOZ_GTK_SUCCESS;
892 }
893 
moz_gtk_scrollbar_paint(WidgetNodeType widget,cairo_t * cr,const GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)894 static gint moz_gtk_scrollbar_paint(WidgetNodeType widget, cairo_t* cr,
895                                     const GdkRectangle* rect,
896                                     GtkWidgetState* state,
897                                     GtkTextDirection direction) {
898   GtkStyleContext* style = GetStyleContext(widget, direction);
899   moz_gtk_update_scrollbar_style(style, widget, direction);
900 
901   moz_gtk_draw_styled_frame(style, cr, rect, state->focused);
902 
903   style = GetStyleContext((widget == MOZ_GTK_SCROLLBAR_HORIZONTAL)
904                               ? MOZ_GTK_SCROLLBAR_CONTENTS_HORIZONTAL
905                               : MOZ_GTK_SCROLLBAR_CONTENTS_VERTICAL,
906                           direction);
907   moz_gtk_draw_styled_frame(style, cr, rect, state->focused);
908 
909   return MOZ_GTK_SUCCESS;
910 }
911 
moz_gtk_scrollbar_thumb_paint(WidgetNodeType widget,cairo_t * cr,const GdkRectangle * aRect,GtkWidgetState * state,GtkTextDirection direction)912 static gint moz_gtk_scrollbar_thumb_paint(WidgetNodeType widget, cairo_t* cr,
913                                           const GdkRectangle* aRect,
914                                           GtkWidgetState* state,
915                                           GtkTextDirection direction) {
916   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
917 
918   GdkRectangle rect = *aRect;
919   GtkStyleContext* style = GetStyleContext(widget, direction, state_flags);
920   InsetByMargin(&rect, style);
921 
922   gtk_render_slider(style, cr, rect.x, rect.y, rect.width, rect.height,
923                     (widget == MOZ_GTK_SCROLLBAR_THUMB_HORIZONTAL)
924                         ? GTK_ORIENTATION_HORIZONTAL
925                         : GTK_ORIENTATION_VERTICAL);
926 
927   return MOZ_GTK_SUCCESS;
928 }
929 
moz_gtk_inner_spin_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)930 static gint moz_gtk_inner_spin_paint(cairo_t* cr, GdkRectangle* rect,
931                                      GtkWidgetState* state,
932                                      GtkTextDirection direction) {
933   GtkStyleContext* style = GetStyleContext(
934       MOZ_GTK_SPINBUTTON, direction, GetStateFlagsFromGtkWidgetState(state));
935 
936   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
937   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
938 
939   /* hard code these values */
940   GdkRectangle arrow_rect;
941   arrow_rect.width = 6;
942   arrow_rect.height = 6;
943 
944   // align spin to the left
945   arrow_rect.x = rect->x;
946 
947   // up button
948   arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 - 3;
949   gtk_render_arrow(style, cr, ARROW_UP, arrow_rect.x, arrow_rect.y,
950                    arrow_rect.width);
951 
952   // down button
953   arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 + 3;
954   gtk_render_arrow(style, cr, ARROW_DOWN, arrow_rect.x, arrow_rect.y,
955                    arrow_rect.width);
956 
957   return MOZ_GTK_SUCCESS;
958 }
959 
moz_gtk_spin_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)960 static gint moz_gtk_spin_paint(cairo_t* cr, GdkRectangle* rect,
961                                GtkTextDirection direction) {
962   GtkStyleContext* style = GetStyleContext(MOZ_GTK_SPINBUTTON, direction);
963   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
964   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
965   return MOZ_GTK_SUCCESS;
966 }
967 
moz_gtk_spin_updown_paint(cairo_t * cr,GdkRectangle * rect,gboolean isDown,GtkWidgetState * state,GtkTextDirection direction)968 static gint moz_gtk_spin_updown_paint(cairo_t* cr, GdkRectangle* rect,
969                                       gboolean isDown, GtkWidgetState* state,
970                                       GtkTextDirection direction) {
971   GtkStyleContext* style = GetStyleContext(
972       MOZ_GTK_SPINBUTTON, direction, GetStateFlagsFromGtkWidgetState(state));
973 
974   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
975   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
976 
977   /* hard code these values */
978   GdkRectangle arrow_rect;
979   arrow_rect.width = 6;
980   arrow_rect.height = 6;
981   arrow_rect.x = rect->x + (rect->width - arrow_rect.width) / 2;
982   arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2;
983   arrow_rect.y += isDown ? -1 : 1;
984 
985   gtk_render_arrow(style, cr, isDown ? ARROW_DOWN : ARROW_UP, arrow_rect.x,
986                    arrow_rect.y, arrow_rect.width);
987 
988   return MOZ_GTK_SUCCESS;
989 }
990 
991 /* See gtk_range_draw() for reference.
992  */
moz_gtk_scale_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkOrientation flags,GtkTextDirection direction)993 static gint moz_gtk_scale_paint(cairo_t* cr, GdkRectangle* rect,
994                                 GtkWidgetState* state, GtkOrientation flags,
995                                 GtkTextDirection direction) {
996   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
997   gint x, y, width, height, min_width, min_height;
998   GtkStyleContext* style;
999   GtkBorder margin;
1000 
1001   moz_gtk_get_scale_metrics(flags, &min_width, &min_height);
1002 
1003   WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
1004                               ? MOZ_GTK_SCALE_TROUGH_HORIZONTAL
1005                               : MOZ_GTK_SCALE_TROUGH_VERTICAL;
1006   style = GetStyleContext(widget, direction, state_flags);
1007   gtk_style_context_get_margin(style, state_flags, &margin);
1008 
1009   // Clamp the dimension perpendicular to the direction that the slider crosses
1010   // to the minimum size.
1011   if (flags == GTK_ORIENTATION_HORIZONTAL) {
1012     width = rect->width - (margin.left + margin.right);
1013     height = min_height - (margin.top + margin.bottom);
1014     x = rect->x + margin.left;
1015     y = rect->y + (rect->height - height) / 2;
1016   } else {
1017     width = min_width - (margin.left + margin.right);
1018     height = rect->height - (margin.top + margin.bottom);
1019     x = rect->x + (rect->width - width) / 2;
1020     y = rect->y + margin.top;
1021   }
1022 
1023   gtk_render_background(style, cr, x, y, width, height);
1024   gtk_render_frame(style, cr, x, y, width, height);
1025 
1026   if (state->focused)
1027     gtk_render_focus(style, cr, rect->x, rect->y, rect->width, rect->height);
1028 
1029   return MOZ_GTK_SUCCESS;
1030 }
1031 
moz_gtk_scale_thumb_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkOrientation flags,GtkTextDirection direction)1032 static gint moz_gtk_scale_thumb_paint(cairo_t* cr, GdkRectangle* rect,
1033                                       GtkWidgetState* state,
1034                                       GtkOrientation flags,
1035                                       GtkTextDirection direction) {
1036   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1037   GtkStyleContext* style;
1038   gint thumb_width, thumb_height, x, y;
1039 
1040   /* determine the thumb size, and position the thumb in the center in the
1041    * opposite axis
1042    */
1043   if (flags == GTK_ORIENTATION_HORIZONTAL) {
1044     moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_HORIZONTAL, &thumb_width,
1045                                    &thumb_height);
1046     x = rect->x;
1047     y = rect->y + (rect->height - thumb_height) / 2;
1048   } else {
1049     moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_VERTICAL, &thumb_height,
1050                                    &thumb_width);
1051     x = rect->x + (rect->width - thumb_width) / 2;
1052     y = rect->y;
1053   }
1054 
1055   WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
1056                               ? MOZ_GTK_SCALE_THUMB_HORIZONTAL
1057                               : MOZ_GTK_SCALE_THUMB_VERTICAL;
1058   style = GetStyleContext(widget, direction, state_flags);
1059   gtk_render_slider(style, cr, x, y, thumb_width, thumb_height, flags);
1060 
1061   return MOZ_GTK_SUCCESS;
1062 }
1063 
moz_gtk_gripper_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)1064 static gint moz_gtk_gripper_paint(cairo_t* cr, GdkRectangle* rect,
1065                                   GtkWidgetState* state,
1066                                   GtkTextDirection direction) {
1067   GtkStyleContext* style = GetStyleContext(
1068       MOZ_GTK_GRIPPER, direction, GetStateFlagsFromGtkWidgetState(state));
1069   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1070   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1071   return MOZ_GTK_SUCCESS;
1072 }
1073 
moz_gtk_hpaned_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state)1074 static gint moz_gtk_hpaned_paint(cairo_t* cr, GdkRectangle* rect,
1075                                  GtkWidgetState* state) {
1076   GtkStyleContext* style =
1077       GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_HORIZONTAL, GTK_TEXT_DIR_LTR,
1078                       GetStateFlagsFromGtkWidgetState(state));
1079   gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
1080   return MOZ_GTK_SUCCESS;
1081 }
1082 
moz_gtk_vpaned_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state)1083 static gint moz_gtk_vpaned_paint(cairo_t* cr, GdkRectangle* rect,
1084                                  GtkWidgetState* state) {
1085   GtkStyleContext* style =
1086       GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_VERTICAL, GTK_TEXT_DIR_LTR,
1087                       GetStateFlagsFromGtkWidgetState(state));
1088   gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
1089   return MOZ_GTK_SUCCESS;
1090 }
1091 
1092 // See gtk_entry_draw() for reference.
moz_gtk_entry_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkStyleContext * style)1093 static gint moz_gtk_entry_paint(cairo_t* cr, GdkRectangle* rect,
1094                                 GtkWidgetState* state, GtkStyleContext* style) {
1095   gint x = rect->x, y = rect->y, width = rect->width, height = rect->height;
1096   int draw_focus_outline_only = state->depressed;  // NS_THEME_FOCUS_OUTLINE
1097 
1098   if (draw_focus_outline_only) {
1099     // Inflate the given 'rect' with the focus outline size.
1100     gint h, v;
1101     moz_gtk_get_focus_outline_size(style, &h, &v);
1102     rect->x -= h;
1103     rect->width += 2 * h;
1104     rect->y -= v;
1105     rect->height += 2 * v;
1106     width = rect->width;
1107     height = rect->height;
1108   } else {
1109     gtk_render_background(style, cr, x, y, width, height);
1110   }
1111   gtk_render_frame(style, cr, x, y, width, height);
1112 
1113   return MOZ_GTK_SUCCESS;
1114 }
1115 
moz_gtk_text_view_paint(cairo_t * cr,GdkRectangle * aRect,GtkWidgetState * state,GtkTextDirection direction)1116 static gint moz_gtk_text_view_paint(cairo_t* cr, GdkRectangle* aRect,
1117                                     GtkWidgetState* state,
1118                                     GtkTextDirection direction) {
1119   // GtkTextView and GtkScrolledWindow do not set active and prelight flags.
1120   // The use of focus with MOZ_GTK_SCROLLED_WINDOW here is questionable
1121   // because a parent widget will not have focus when its child GtkTextView
1122   // has focus, but perhaps this may help identify a focused textarea with
1123   // some themes as GtkTextView backgrounds do not typically render
1124   // differently with focus.
1125   GtkStateFlags state_flags =
1126       state->disabled
1127           ? GTK_STATE_FLAG_INSENSITIVE
1128           : state->focused ? GTK_STATE_FLAG_FOCUSED : GTK_STATE_FLAG_NORMAL;
1129 
1130   GtkStyleContext* style_frame =
1131       GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, direction, state_flags);
1132   gtk_render_frame(style_frame, cr, aRect->x, aRect->y, aRect->width,
1133                    aRect->height);
1134 
1135   GdkRectangle rect = *aRect;
1136   InsetByBorderPadding(&rect, style_frame);
1137 
1138   GtkStyleContext* style =
1139       GetStyleContext(MOZ_GTK_TEXT_VIEW, direction, state_flags);
1140   gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
1141   // There is a separate "text" window, which usually provides the
1142   // background behind the text.  However, this is transparent in Ambiance
1143   // for GTK 3.20, in which case the MOZ_GTK_TEXT_VIEW background is
1144   // visible.
1145   style = GetStyleContext(MOZ_GTK_TEXT_VIEW_TEXT, direction, state_flags);
1146   gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
1147 
1148   return MOZ_GTK_SUCCESS;
1149 }
1150 
moz_gtk_treeview_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)1151 static gint moz_gtk_treeview_paint(cairo_t* cr, GdkRectangle* rect,
1152                                    GtkWidgetState* state,
1153                                    GtkTextDirection direction) {
1154   gint xthickness, ythickness;
1155   GtkStyleContext* style;
1156   GtkStyleContext* style_tree;
1157   GtkStateFlags state_flags;
1158   GtkBorder border;
1159 
1160   /* only handle disabled and normal states, otherwise the whole background
1161    * area will be painted differently with other states */
1162   state_flags =
1163       state->disabled ? GTK_STATE_FLAG_INSENSITIVE : GTK_STATE_FLAG_NORMAL;
1164 
1165   style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, direction);
1166   gtk_style_context_get_border(style, state_flags, &border);
1167   xthickness = border.left;
1168   ythickness = border.top;
1169 
1170   style_tree = GetStyleContext(MOZ_GTK_TREEVIEW_VIEW, direction);
1171   gtk_render_background(style_tree, cr, rect->x + xthickness,
1172                         rect->y + ythickness, rect->width - 2 * xthickness,
1173                         rect->height - 2 * ythickness);
1174 
1175   style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, direction);
1176   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1177   return MOZ_GTK_SUCCESS;
1178 }
1179 
moz_gtk_tree_header_cell_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,gboolean isSorted,GtkTextDirection direction)1180 static gint moz_gtk_tree_header_cell_paint(cairo_t* cr, GdkRectangle* rect,
1181                                            GtkWidgetState* state,
1182                                            gboolean isSorted,
1183                                            GtkTextDirection direction) {
1184   moz_gtk_button_paint(cr, rect, state, GTK_RELIEF_NORMAL,
1185                        GetWidget(MOZ_GTK_TREE_HEADER_CELL), direction);
1186   return MOZ_GTK_SUCCESS;
1187 }
1188 
moz_gtk_tree_header_sort_arrow_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkArrowType arrow_type,GtkTextDirection direction)1189 static gint moz_gtk_tree_header_sort_arrow_paint(cairo_t* cr,
1190                                                  GdkRectangle* rect,
1191                                                  GtkWidgetState* state,
1192                                                  GtkArrowType arrow_type,
1193                                                  GtkTextDirection direction) {
1194   GdkRectangle arrow_rect;
1195   gdouble arrow_angle;
1196   GtkStyleContext* style;
1197 
1198   /* hard code these values */
1199   arrow_rect.width = 11;
1200   arrow_rect.height = 11;
1201   arrow_rect.x = rect->x + (rect->width - arrow_rect.width) / 2;
1202   arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2;
1203   style = GetStyleContext(MOZ_GTK_TREE_HEADER_SORTARROW, direction,
1204                           GetStateFlagsFromGtkWidgetState(state));
1205   switch (arrow_type) {
1206     case GTK_ARROW_LEFT:
1207       arrow_angle = ARROW_LEFT;
1208       break;
1209     case GTK_ARROW_RIGHT:
1210       arrow_angle = ARROW_RIGHT;
1211       break;
1212     case GTK_ARROW_DOWN:
1213       arrow_angle = ARROW_DOWN;
1214       break;
1215     default:
1216       arrow_angle = ARROW_UP;
1217       break;
1218   }
1219   if (arrow_type != GTK_ARROW_NONE)
1220     gtk_render_arrow(style, cr, arrow_angle, arrow_rect.x, arrow_rect.y,
1221                      arrow_rect.width);
1222   return MOZ_GTK_SUCCESS;
1223 }
1224 
1225 /* See gtk_expander_paint() for reference.
1226  */
moz_gtk_treeview_expander_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkExpanderStyle expander_state,GtkTextDirection direction)1227 static gint moz_gtk_treeview_expander_paint(cairo_t* cr, GdkRectangle* rect,
1228                                             GtkWidgetState* state,
1229                                             GtkExpanderStyle expander_state,
1230                                             GtkTextDirection direction) {
1231   /* Because the frame we get is of the entire treeview, we can't get the
1232    * precise event state of one expander, thus rendering hover and active
1233    * feedback useless. */
1234   GtkStateFlags state_flags =
1235       state->disabled ? GTK_STATE_FLAG_INSENSITIVE : GTK_STATE_FLAG_NORMAL;
1236 
1237   /* GTK_STATE_FLAG_ACTIVE controls expanded/colapsed state rendering
1238    * in gtk_render_expander()
1239    */
1240   if (expander_state == GTK_EXPANDER_EXPANDED)
1241     state_flags =
1242         static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
1243   else
1244     state_flags =
1245         static_cast<GtkStateFlags>(state_flags & ~(checkbox_check_state));
1246 
1247   GtkStyleContext* style =
1248       GetStyleContext(MOZ_GTK_TREEVIEW_EXPANDER, direction, state_flags);
1249   gtk_render_expander(style, cr, rect->x, rect->y, rect->width, rect->height);
1250 
1251   return MOZ_GTK_SUCCESS;
1252 }
1253 
1254 /* See gtk_separator_draw() for reference.
1255  */
moz_gtk_combo_box_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)1256 static gint moz_gtk_combo_box_paint(cairo_t* cr, GdkRectangle* rect,
1257                                     GtkWidgetState* state,
1258                                     GtkTextDirection direction) {
1259   GdkRectangle arrow_rect, real_arrow_rect;
1260   gint separator_width;
1261   gboolean wide_separators;
1262   GtkStyleContext* style;
1263   GtkRequisition arrow_req;
1264 
1265   GtkWidget* comboBoxButton = GetWidget(MOZ_GTK_COMBOBOX_BUTTON);
1266   GtkWidget* comboBoxArrow = GetWidget(MOZ_GTK_COMBOBOX_ARROW);
1267 
1268   /* Also sets the direction on gComboBoxButtonWidget, which is then
1269    * inherited by the separator and arrow */
1270   moz_gtk_button_paint(cr, rect, state, GTK_RELIEF_NORMAL, comboBoxButton,
1271                        direction);
1272 
1273   calculate_button_inner_rect(comboBoxButton, rect, &arrow_rect, direction);
1274   /* Now arrow_rect contains the inner rect ; we want to correct the width
1275    * to what the arrow needs (see gtk_combo_box_size_allocate) */
1276   gtk_widget_get_preferred_size(comboBoxArrow, NULL, &arrow_req);
1277 
1278   if (direction == GTK_TEXT_DIR_LTR)
1279     arrow_rect.x += arrow_rect.width - arrow_req.width;
1280   arrow_rect.width = arrow_req.width;
1281 
1282   calculate_arrow_rect(comboBoxArrow, &arrow_rect, &real_arrow_rect, direction);
1283 
1284   style = GetStyleContext(MOZ_GTK_COMBOBOX_ARROW);
1285   gtk_render_arrow(style, cr, ARROW_DOWN, real_arrow_rect.x, real_arrow_rect.y,
1286                    real_arrow_rect.width);
1287 
1288   /* If there is no separator in the theme, there's nothing left to do. */
1289   GtkWidget* widget = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR);
1290   if (!widget) return MOZ_GTK_SUCCESS;
1291   style = gtk_widget_get_style_context(widget);
1292   gtk_style_context_get_style(style, "wide-separators", &wide_separators,
1293                               "separator-width", &separator_width, NULL);
1294 
1295   if (wide_separators) {
1296     if (direction == GTK_TEXT_DIR_LTR)
1297       arrow_rect.x -= separator_width;
1298     else
1299       arrow_rect.x += arrow_rect.width;
1300 
1301     gtk_render_frame(style, cr, arrow_rect.x, arrow_rect.y, separator_width,
1302                      arrow_rect.height);
1303   } else {
1304     if (direction == GTK_TEXT_DIR_LTR) {
1305       GtkBorder padding;
1306       GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1307       gtk_style_context_get_padding(style, state_flags, &padding);
1308       arrow_rect.x -= padding.left;
1309     } else
1310       arrow_rect.x += arrow_rect.width;
1311 
1312     gtk_render_line(style, cr, arrow_rect.x, arrow_rect.y, arrow_rect.x,
1313                     arrow_rect.y + arrow_rect.height);
1314   }
1315   return MOZ_GTK_SUCCESS;
1316 }
1317 
moz_gtk_arrow_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkArrowType arrow_type,GtkTextDirection direction)1318 static gint moz_gtk_arrow_paint(cairo_t* cr, GdkRectangle* rect,
1319                                 GtkWidgetState* state, GtkArrowType arrow_type,
1320                                 GtkTextDirection direction) {
1321   GdkRectangle arrow_rect;
1322   gdouble arrow_angle;
1323 
1324   if (direction == GTK_TEXT_DIR_RTL) {
1325     if (arrow_type == GTK_ARROW_LEFT) {
1326       arrow_type = GTK_ARROW_RIGHT;
1327     } else if (arrow_type == GTK_ARROW_RIGHT) {
1328       arrow_type = GTK_ARROW_LEFT;
1329     }
1330   }
1331   switch (arrow_type) {
1332     case GTK_ARROW_LEFT:
1333       arrow_angle = ARROW_LEFT;
1334       break;
1335     case GTK_ARROW_RIGHT:
1336       arrow_angle = ARROW_RIGHT;
1337       break;
1338     case GTK_ARROW_DOWN:
1339       arrow_angle = ARROW_DOWN;
1340       break;
1341     default:
1342       arrow_angle = ARROW_UP;
1343       break;
1344   }
1345   if (arrow_type == GTK_ARROW_NONE) return MOZ_GTK_SUCCESS;
1346 
1347   calculate_arrow_rect(GetWidget(MOZ_GTK_BUTTON_ARROW), rect, &arrow_rect,
1348                        direction);
1349   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1350   GtkStyleContext* style =
1351       GetStyleContext(MOZ_GTK_BUTTON_ARROW, direction, state_flags);
1352   gtk_render_arrow(style, cr, arrow_angle, arrow_rect.x, arrow_rect.y,
1353                    arrow_rect.width);
1354   return MOZ_GTK_SUCCESS;
1355 }
1356 
moz_gtk_combo_box_entry_button_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,gboolean input_focus,GtkTextDirection direction)1357 static gint moz_gtk_combo_box_entry_button_paint(cairo_t* cr,
1358                                                  GdkRectangle* rect,
1359                                                  GtkWidgetState* state,
1360                                                  gboolean input_focus,
1361                                                  GtkTextDirection direction) {
1362   gint x_displacement, y_displacement;
1363   GdkRectangle arrow_rect, real_arrow_rect;
1364   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1365   GtkStyleContext* style;
1366 
1367   GtkWidget* comboBoxEntry = GetWidget(MOZ_GTK_COMBOBOX_ENTRY_BUTTON);
1368   moz_gtk_button_paint(cr, rect, state, GTK_RELIEF_NORMAL, comboBoxEntry,
1369                        direction);
1370   calculate_button_inner_rect(comboBoxEntry, rect, &arrow_rect, direction);
1371 
1372   if (state_flags & GTK_STATE_FLAG_ACTIVE) {
1373     style = gtk_widget_get_style_context(comboBoxEntry);
1374     gtk_style_context_get_style(style, "child-displacement-x", &x_displacement,
1375                                 "child-displacement-y", &y_displacement, NULL);
1376     arrow_rect.x += x_displacement;
1377     arrow_rect.y += y_displacement;
1378   }
1379 
1380   calculate_arrow_rect(GetWidget(MOZ_GTK_COMBOBOX_ENTRY_ARROW), &arrow_rect,
1381                        &real_arrow_rect, direction);
1382 
1383   style = GetStyleContext(MOZ_GTK_COMBOBOX_ENTRY_ARROW);
1384   gtk_render_arrow(style, cr, ARROW_DOWN, real_arrow_rect.x, real_arrow_rect.y,
1385                    real_arrow_rect.width);
1386   return MOZ_GTK_SUCCESS;
1387 }
1388 
moz_gtk_container_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,WidgetNodeType widget_type,GtkTextDirection direction)1389 static gint moz_gtk_container_paint(cairo_t* cr, GdkRectangle* rect,
1390                                     GtkWidgetState* state,
1391                                     WidgetNodeType widget_type,
1392                                     GtkTextDirection direction) {
1393   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1394   GtkStyleContext* style = GetStyleContext(widget_type, direction, state_flags);
1395   /* this is for drawing a prelight box */
1396   if (state_flags & GTK_STATE_FLAG_PRELIGHT) {
1397     gtk_render_background(style, cr, rect->x, rect->y, rect->width,
1398                           rect->height);
1399   }
1400 
1401   return MOZ_GTK_SUCCESS;
1402 }
1403 
moz_gtk_toggle_label_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,gboolean isradio,GtkTextDirection direction)1404 static gint moz_gtk_toggle_label_paint(cairo_t* cr, GdkRectangle* rect,
1405                                        GtkWidgetState* state, gboolean isradio,
1406                                        GtkTextDirection direction) {
1407   if (!state->focused) return MOZ_GTK_SUCCESS;
1408 
1409   GtkStyleContext* style = GetStyleContext(
1410       isradio ? MOZ_GTK_RADIOBUTTON_CONTAINER : MOZ_GTK_CHECKBUTTON_CONTAINER,
1411       direction, GetStateFlagsFromGtkWidgetState(state));
1412   gtk_render_focus(style, cr, rect->x, rect->y, rect->width, rect->height);
1413 
1414   return MOZ_GTK_SUCCESS;
1415 }
1416 
moz_gtk_toolbar_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1417 static gint moz_gtk_toolbar_paint(cairo_t* cr, GdkRectangle* rect,
1418                                   GtkTextDirection direction) {
1419   GtkStyleContext* style = GetStyleContext(MOZ_GTK_TOOLBAR, direction);
1420   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1421   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1422   return MOZ_GTK_SUCCESS;
1423 }
1424 
1425 /* See _gtk_toolbar_paint_space_line() for reference.
1426  */
moz_gtk_toolbar_separator_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1427 static gint moz_gtk_toolbar_separator_paint(cairo_t* cr, GdkRectangle* rect,
1428                                             GtkTextDirection direction) {
1429   gint separator_width;
1430   gint paint_width;
1431   gboolean wide_separators;
1432 
1433   /* Defined as constants in GTK+ 2.10.14 */
1434   const double start_fraction = 0.2;
1435   const double end_fraction = 0.8;
1436 
1437   GtkStyleContext* style = GetStyleContext(MOZ_GTK_TOOLBAR);
1438   gtk_style_context_get_style(style, "wide-separators", &wide_separators,
1439                               "separator-width", &separator_width, NULL);
1440 
1441   style = GetStyleContext(MOZ_GTK_TOOLBAR_SEPARATOR, direction);
1442   if (wide_separators) {
1443     if (separator_width > rect->width) separator_width = rect->width;
1444 
1445     gtk_render_frame(style, cr, rect->x + (rect->width - separator_width) / 2,
1446                      rect->y + rect->height * start_fraction, separator_width,
1447                      rect->height * (end_fraction - start_fraction));
1448   } else {
1449     GtkBorder padding;
1450     gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
1451 
1452     paint_width = padding.left;
1453     if (paint_width > rect->width) paint_width = rect->width;
1454 
1455     gtk_render_line(style, cr, rect->x + (rect->width - paint_width) / 2,
1456                     rect->y + rect->height * start_fraction,
1457                     rect->x + (rect->width - paint_width) / 2,
1458                     rect->y + rect->height * end_fraction);
1459   }
1460   return MOZ_GTK_SUCCESS;
1461 }
1462 
moz_gtk_tooltip_paint(cairo_t * cr,const GdkRectangle * aRect,GtkTextDirection direction)1463 static gint moz_gtk_tooltip_paint(cairo_t* cr, const GdkRectangle* aRect,
1464                                   GtkTextDirection direction) {
1465   // Tooltip widget is made in GTK3 as following tree:
1466   // Tooltip window
1467   //   Horizontal Box
1468   //     Icon (not supported by Firefox)
1469   //     Label
1470   // Each element can be fully styled by CSS of GTK theme.
1471   // We have to draw all elements with appropriate offset and right dimensions.
1472 
1473   // Tooltip drawing
1474   GtkStyleContext* style = GetStyleContext(MOZ_GTK_TOOLTIP, direction);
1475   GdkRectangle rect = *aRect;
1476   gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
1477   gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
1478 
1479   // Horizontal Box drawing
1480   //
1481   // The box element has hard-coded 6px margin-* GtkWidget properties, which
1482   // are added between the window dimensions and the CSS margin box of the
1483   // horizontal box.  The frame of the tooltip window is drawn in the
1484   // 6px margin.
1485   // For drawing Horizontal Box we have to inset drawing area by that 6px
1486   // plus its CSS margin.
1487   GtkStyleContext* boxStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX, direction);
1488 
1489   rect.x += 6;
1490   rect.y += 6;
1491   rect.width -= 12;
1492   rect.height -= 12;
1493 
1494   InsetByMargin(&rect, boxStyle);
1495   gtk_render_background(boxStyle, cr, rect.x, rect.y, rect.width, rect.height);
1496   gtk_render_frame(boxStyle, cr, rect.x, rect.y, rect.width, rect.height);
1497 
1498   // Label drawing
1499   InsetByBorderPadding(&rect, boxStyle);
1500 
1501   GtkStyleContext* labelStyle =
1502       GetStyleContext(MOZ_GTK_TOOLTIP_BOX_LABEL, direction);
1503   moz_gtk_draw_styled_frame(labelStyle, cr, &rect, false);
1504 
1505   return MOZ_GTK_SUCCESS;
1506 }
1507 
moz_gtk_resizer_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)1508 static gint moz_gtk_resizer_paint(cairo_t* cr, GdkRectangle* rect,
1509                                   GtkWidgetState* state,
1510                                   GtkTextDirection direction) {
1511   GtkStyleContext* style =
1512       GetStyleContext(MOZ_GTK_RESIZER, GTK_TEXT_DIR_LTR,
1513                       GetStateFlagsFromGtkWidgetState(state));
1514 
1515   // Workaround unico not respecting the text direction for resizers.
1516   // See bug 1174248.
1517   cairo_save(cr);
1518   if (direction == GTK_TEXT_DIR_RTL) {
1519     cairo_matrix_t mat;
1520     cairo_matrix_init_translate(&mat, 2 * rect->x + rect->width, 0);
1521     cairo_matrix_scale(&mat, -1, 1);
1522     cairo_transform(cr, &mat);
1523   }
1524 
1525   gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
1526   cairo_restore(cr);
1527 
1528   return MOZ_GTK_SUCCESS;
1529 }
1530 
moz_gtk_frame_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1531 static gint moz_gtk_frame_paint(cairo_t* cr, GdkRectangle* rect,
1532                                 GtkTextDirection direction) {
1533   GtkStyleContext* style = GetStyleContext(MOZ_GTK_FRAME, direction);
1534   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1535   return MOZ_GTK_SUCCESS;
1536 }
1537 
moz_gtk_progressbar_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1538 static gint moz_gtk_progressbar_paint(cairo_t* cr, GdkRectangle* rect,
1539                                       GtkTextDirection direction) {
1540   GtkStyleContext* style = GetStyleContext(MOZ_GTK_PROGRESS_TROUGH, direction);
1541   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1542   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1543 
1544   return MOZ_GTK_SUCCESS;
1545 }
1546 
moz_gtk_progress_chunk_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction,WidgetNodeType widget)1547 static gint moz_gtk_progress_chunk_paint(cairo_t* cr, GdkRectangle* rect,
1548                                          GtkTextDirection direction,
1549                                          WidgetNodeType widget) {
1550   GtkStyleContext* style = GetStyleContext(MOZ_GTK_PROGRESS_CHUNK, direction);
1551 
1552   if (widget == MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE ||
1553       widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE) {
1554     /**
1555      * The bar's size and the bar speed are set depending of the progress'
1556      * size. These could also be constant for all progress bars easily.
1557      */
1558     gboolean vertical =
1559         (widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE);
1560 
1561     /* The size of the dimension we are going to use for the animation. */
1562     const gint progressSize = vertical ? rect->height : rect->width;
1563 
1564     /* The bar is using a fifth of the element size, based on GtkProgressBar
1565      * activity-blocks property. */
1566     const gint barSize = MAX(1, progressSize / 5);
1567 
1568     /* Represents the travel that has to be done for a complete cycle. */
1569     const gint travel = 2 * (progressSize - barSize);
1570 
1571     /* period equals to travel / pixelsPerMillisecond
1572      * where pixelsPerMillisecond equals progressSize / 1000.0.
1573      * This is equivalent to 1600. */
1574     static const guint period = 1600;
1575     const gint t = PR_IntervalToMilliseconds(PR_IntervalNow()) % period;
1576     const gint dx = travel * t / period;
1577 
1578     if (vertical) {
1579       rect->y += (dx < travel / 2) ? dx : travel - dx;
1580       rect->height = barSize;
1581     } else {
1582       rect->x += (dx < travel / 2) ? dx : travel - dx;
1583       rect->width = barSize;
1584     }
1585   }
1586 
1587   // gtk_render_activity was used to render progress chunks on GTK versions
1588   // before 3.13.7, see bug 1173907.
1589   if (!gtk_check_version(3, 13, 7)) {
1590     gtk_render_background(style, cr, rect->x, rect->y, rect->width,
1591                           rect->height);
1592     gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1593   } else {
1594     gtk_render_activity(style, cr, rect->x, rect->y, rect->width, rect->height);
1595   }
1596 
1597   return MOZ_GTK_SUCCESS;
1598 }
1599 
moz_gtk_get_tab_thickness(GtkStyleContext * style)1600 static gint moz_gtk_get_tab_thickness(GtkStyleContext* style) {
1601   if (!notebook_has_tab_gap)
1602     return 0; /* tabs do not overdraw the tabpanel border with "no gap" style */
1603 
1604   GtkBorder border;
1605   gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
1606   if (border.top < 2) return 2; /* some themes don't set ythickness correctly */
1607 
1608   return border.top;
1609 }
1610 
moz_gtk_get_tab_thickness(WidgetNodeType aNodeType)1611 gint moz_gtk_get_tab_thickness(WidgetNodeType aNodeType) {
1612   GtkStyleContext* style = GetStyleContext(aNodeType);
1613   int thickness = moz_gtk_get_tab_thickness(style);
1614   return thickness;
1615 }
1616 
1617 /* actual small tabs */
moz_gtk_tab_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTabFlags flags,GtkTextDirection direction,WidgetNodeType widget)1618 static gint moz_gtk_tab_paint(cairo_t* cr, GdkRectangle* rect,
1619                               GtkWidgetState* state, GtkTabFlags flags,
1620                               GtkTextDirection direction,
1621                               WidgetNodeType widget) {
1622   /* When the tab isn't selected, we just draw a notebook extension.
1623    * When it is selected, we overwrite the adjacent border of the tabpanel
1624    * touching the tab with a pierced border (called "the gap") to make the
1625    * tab appear physically attached to the tabpanel; see details below. */
1626 
1627   GtkStyleContext* style;
1628   GdkRectangle tabRect;
1629   GdkRectangle focusRect;
1630   GdkRectangle backRect;
1631   int initial_gap = 0;
1632   bool isBottomTab = (widget == MOZ_GTK_TAB_BOTTOM);
1633 
1634   style =
1635       GetStyleContext(widget, direction, GetStateFlagsFromGtkTabFlags(flags));
1636   tabRect = *rect;
1637 
1638   if (flags & MOZ_GTK_TAB_FIRST) {
1639     gtk_style_context_get_style(style, "initial-gap", &initial_gap, NULL);
1640     tabRect.width -= initial_gap;
1641 
1642     if (direction != GTK_TEXT_DIR_RTL) {
1643       tabRect.x += initial_gap;
1644     }
1645   }
1646 
1647   focusRect = backRect = tabRect;
1648 
1649   if (notebook_has_tab_gap) {
1650     if ((flags & MOZ_GTK_TAB_SELECTED) == 0) {
1651       /* Only draw the tab */
1652       gtk_render_extension(style, cr, tabRect.x, tabRect.y, tabRect.width,
1653                            tabRect.height,
1654                            isBottomTab ? GTK_POS_TOP : GTK_POS_BOTTOM);
1655     } else {
1656       /* Draw the tab and the gap
1657        * We want the gap to be positioned exactly on the tabpanel top
1658        * border; since tabbox.css may set a negative margin so that the tab
1659        * frame rect already overlaps the tabpanel frame rect, we need to take
1660        * that into account when drawing. To that effect, nsNativeThemeGTK
1661        * passes us this negative margin (bmargin in the graphic below) in the
1662        * lowest bits of |flags|.  We use it to set gap_voffset, the distance
1663        * between the top of the gap and the bottom of the tab (resp. the
1664        * bottom of the gap and the top of the tab when we draw a bottom tab),
1665        * while ensuring that the gap always touches the border of the tab,
1666        * i.e. 0 <= gap_voffset <= gap_height, to avoid surprinsing results
1667        * with big negative or positive margins.
1668        * Here is a graphical explanation in the case of top tabs:
1669        *             ___________________________
1670        *            /                           \
1671        *           |            T A B            |
1672        * ----------|. . . . . . . . . . . . . . .|----- top of tabpanel
1673        *           :    ^       bmargin          :  ^
1674        *           :    | (-negative margin,     :  |
1675        *  bottom   :    v  passed in flags)      :  |       gap_height
1676        *    of  -> :.............................:  |    (the size of the
1677        * the tab   .       part of the gap       .  |  tabpanel top border)
1678        *           .      outside of the tab     .  v
1679        * ----------------------------------------------
1680        *
1681        * To draw the gap, we use gtk_render_frame_gap(), see comment in
1682        * moz_gtk_tabpanels_paint(). This gap is made 3 * gap_height tall,
1683        * which should suffice to ensure that the only visible border is the
1684        * pierced one.  If the tab is in the middle, we make the box_gap begin
1685        * a bit to the left of the tab and end a bit to the right, adjusting
1686        * the gap position so it still is under the tab, because we want the
1687        * rendering of a gap in the middle of a tabpanel.  This is the role of
1688        * the gints gap_{l,r}_offset. On the contrary, if the tab is the
1689        * first, we align the start border of the box_gap with the start
1690        * border of the tab (left if LTR, right if RTL), by setting the
1691        * appropriate offset to 0.*/
1692       gint gap_loffset, gap_roffset, gap_voffset, gap_height;
1693 
1694       /* Get height needed by the gap */
1695       gap_height = moz_gtk_get_tab_thickness(style);
1696 
1697       /* Extract gap_voffset from the first bits of flags */
1698       gap_voffset = flags & MOZ_GTK_TAB_MARGIN_MASK;
1699       if (gap_voffset > gap_height) gap_voffset = gap_height;
1700 
1701       /* Set gap_{l,r}_offset to appropriate values */
1702       gap_loffset = gap_roffset = 20; /* should be enough */
1703       if (flags & MOZ_GTK_TAB_FIRST) {
1704         if (direction == GTK_TEXT_DIR_RTL)
1705           gap_roffset = initial_gap;
1706         else
1707           gap_loffset = initial_gap;
1708       }
1709 
1710       GtkStyleContext* panelStyle =
1711           GetStyleContext(MOZ_GTK_TABPANELS, direction);
1712 
1713       if (isBottomTab) {
1714         /* Draw the tab on bottom */
1715         focusRect.y += gap_voffset;
1716         focusRect.height -= gap_voffset;
1717 
1718         gtk_render_extension(style, cr, tabRect.x, tabRect.y + gap_voffset,
1719                              tabRect.width, tabRect.height - gap_voffset,
1720                              GTK_POS_TOP);
1721 
1722         backRect.y += (gap_voffset - gap_height);
1723         backRect.height = gap_height;
1724 
1725         /* Draw the gap; erase with background color before painting in
1726          * case theme does not */
1727         gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
1728                               backRect.width, backRect.height);
1729         cairo_save(cr);
1730         cairo_rectangle(cr, backRect.x, backRect.y, backRect.width,
1731                         backRect.height);
1732         cairo_clip(cr);
1733 
1734         gtk_render_frame_gap(panelStyle, cr, tabRect.x - gap_loffset,
1735                              tabRect.y + gap_voffset - 3 * gap_height,
1736                              tabRect.width + gap_loffset + gap_roffset,
1737                              3 * gap_height, GTK_POS_BOTTOM, gap_loffset,
1738                              gap_loffset + tabRect.width);
1739         cairo_restore(cr);
1740       } else {
1741         /* Draw the tab on top */
1742         focusRect.height -= gap_voffset;
1743         gtk_render_extension(style, cr, tabRect.x, tabRect.y, tabRect.width,
1744                              tabRect.height - gap_voffset, GTK_POS_BOTTOM);
1745 
1746         backRect.y += (tabRect.height - gap_voffset);
1747         backRect.height = gap_height;
1748 
1749         /* Draw the gap; erase with background color before painting in
1750          * case theme does not */
1751         gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
1752                               backRect.width, backRect.height);
1753 
1754         cairo_save(cr);
1755         cairo_rectangle(cr, backRect.x, backRect.y, backRect.width,
1756                         backRect.height);
1757         cairo_clip(cr);
1758 
1759         gtk_render_frame_gap(panelStyle, cr, tabRect.x - gap_loffset,
1760                              tabRect.y + tabRect.height - gap_voffset,
1761                              tabRect.width + gap_loffset + gap_roffset,
1762                              3 * gap_height, GTK_POS_TOP, gap_loffset,
1763                              gap_loffset + tabRect.width);
1764         cairo_restore(cr);
1765       }
1766     }
1767   } else {
1768     gtk_render_background(style, cr, tabRect.x, tabRect.y, tabRect.width,
1769                           tabRect.height);
1770     gtk_render_frame(style, cr, tabRect.x, tabRect.y, tabRect.width,
1771                      tabRect.height);
1772   }
1773 
1774   if (state->focused) {
1775     /* Paint the focus ring */
1776     GtkBorder padding;
1777     gtk_style_context_get_padding(style, GetStateFlagsFromGtkWidgetState(state),
1778                                   &padding);
1779 
1780     focusRect.x += padding.left;
1781     focusRect.width -= (padding.left + padding.right);
1782     focusRect.y += padding.top;
1783     focusRect.height -= (padding.top + padding.bottom);
1784 
1785     gtk_render_focus(style, cr, focusRect.x, focusRect.y, focusRect.width,
1786                      focusRect.height);
1787   }
1788 
1789   return MOZ_GTK_SUCCESS;
1790 }
1791 
1792 /* tab area*/
moz_gtk_tabpanels_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1793 static gint moz_gtk_tabpanels_paint(cairo_t* cr, GdkRectangle* rect,
1794                                     GtkTextDirection direction) {
1795   GtkStyleContext* style = GetStyleContext(MOZ_GTK_TABPANELS, direction);
1796   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1797   /*
1798    * The gap size is not needed in moz_gtk_tabpanels_paint because
1799    * the gap will be painted with the foreground tab in moz_gtk_tab_paint.
1800    *
1801    * However, if moz_gtk_tabpanels_paint just uses gtk_render_frame(),
1802    * the theme will think that there are no tabs and may draw something
1803    * different.Hence the trick of using two clip regions, and drawing the
1804    * gap outside each clip region, to get the correct frame for
1805    * a tabpanel with tabs.
1806    */
1807   /* left side */
1808   cairo_save(cr);
1809   cairo_rectangle(cr, rect->x, rect->y, rect->x + rect->width / 2,
1810                   rect->y + rect->height);
1811   cairo_clip(cr);
1812   gtk_render_frame_gap(style, cr, rect->x, rect->y, rect->width, rect->height,
1813                        GTK_POS_TOP, rect->width - 1, rect->width);
1814   cairo_restore(cr);
1815 
1816   /* right side */
1817   cairo_save(cr);
1818   cairo_rectangle(cr, rect->x + rect->width / 2, rect->y, rect->x + rect->width,
1819                   rect->y + rect->height);
1820   cairo_clip(cr);
1821   gtk_render_frame_gap(style, cr, rect->x, rect->y, rect->width, rect->height,
1822                        GTK_POS_TOP, 0, 1);
1823   cairo_restore(cr);
1824 
1825   return MOZ_GTK_SUCCESS;
1826 }
1827 
moz_gtk_tab_scroll_arrow_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkArrowType arrow_type,GtkTextDirection direction)1828 static gint moz_gtk_tab_scroll_arrow_paint(cairo_t* cr, GdkRectangle* rect,
1829                                            GtkWidgetState* state,
1830                                            GtkArrowType arrow_type,
1831                                            GtkTextDirection direction) {
1832   GtkStyleContext* style;
1833   gdouble arrow_angle;
1834   gint arrow_size = MIN(rect->width, rect->height);
1835   gint x = rect->x + (rect->width - arrow_size) / 2;
1836   gint y = rect->y + (rect->height - arrow_size) / 2;
1837 
1838   if (direction == GTK_TEXT_DIR_RTL) {
1839     arrow_type =
1840         (arrow_type == GTK_ARROW_LEFT) ? GTK_ARROW_RIGHT : GTK_ARROW_LEFT;
1841   }
1842   switch (arrow_type) {
1843     case GTK_ARROW_LEFT:
1844       arrow_angle = ARROW_LEFT;
1845       break;
1846     case GTK_ARROW_RIGHT:
1847       arrow_angle = ARROW_RIGHT;
1848       break;
1849     case GTK_ARROW_DOWN:
1850       arrow_angle = ARROW_DOWN;
1851       break;
1852     default:
1853       arrow_angle = ARROW_UP;
1854       break;
1855   }
1856   if (arrow_type != GTK_ARROW_NONE) {
1857     style = GetStyleContext(MOZ_GTK_TAB_SCROLLARROW, direction,
1858                             GetStateFlagsFromGtkWidgetState(state));
1859     gtk_render_arrow(style, cr, arrow_angle, x, y, arrow_size);
1860   }
1861   return MOZ_GTK_SUCCESS;
1862 }
1863 
moz_gtk_menu_bar_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1864 static gint moz_gtk_menu_bar_paint(cairo_t* cr, GdkRectangle* rect,
1865                                    GtkTextDirection direction) {
1866   GtkStyleContext* style;
1867 
1868   GtkWidget* widget = GetWidget(MOZ_GTK_MENUBAR);
1869   gtk_widget_set_direction(widget, direction);
1870 
1871   style = gtk_widget_get_style_context(widget);
1872   gtk_style_context_save(style);
1873   gtk_style_context_add_class(style, GTK_STYLE_CLASS_MENUBAR);
1874   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1875   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1876   gtk_style_context_restore(style);
1877 
1878   return MOZ_GTK_SUCCESS;
1879 }
1880 
moz_gtk_menu_popup_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1881 static gint moz_gtk_menu_popup_paint(cairo_t* cr, GdkRectangle* rect,
1882                                      GtkTextDirection direction) {
1883   GtkStyleContext* style;
1884 
1885   GtkWidget* widget = GetWidget(MOZ_GTK_MENUPOPUP);
1886   gtk_widget_set_direction(widget, direction);
1887 
1888   // Draw a backing toplevel. This fixes themes that don't provide a menu
1889   // background, and depend on the GtkMenu's implementation window to provide
1890   // it.
1891   moz_gtk_window_paint(cr, rect, direction);
1892 
1893   style = gtk_widget_get_style_context(widget);
1894   gtk_style_context_save(style);
1895   gtk_style_context_add_class(style, GTK_STYLE_CLASS_MENU);
1896 
1897   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1898   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1899   gtk_style_context_restore(style);
1900 
1901   return MOZ_GTK_SUCCESS;
1902 }
1903 
1904 // See gtk_menu_item_draw() for reference.
moz_gtk_menu_separator_paint(cairo_t * cr,GdkRectangle * rect,GtkTextDirection direction)1905 static gint moz_gtk_menu_separator_paint(cairo_t* cr, GdkRectangle* rect,
1906                                          GtkTextDirection direction) {
1907   GtkWidgetState defaultState = {0};
1908   moz_gtk_menu_item_paint(MOZ_GTK_MENUSEPARATOR, cr, rect, &defaultState,
1909                           direction);
1910 
1911   if (gtk_get_minor_version() >= 20) return MOZ_GTK_SUCCESS;
1912 
1913   GtkStyleContext* style;
1914   gboolean wide_separators;
1915   gint separator_height;
1916   gint x, y, w;
1917   GtkBorder padding;
1918 
1919   style = GetStyleContext(MOZ_GTK_MENUSEPARATOR, direction);
1920   gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
1921 
1922   x = rect->x;
1923   y = rect->y;
1924   w = rect->width;
1925 
1926   gtk_style_context_save(style);
1927   gtk_style_context_add_class(style, GTK_STYLE_CLASS_SEPARATOR);
1928 
1929   gtk_style_context_get_style(style, "wide-separators", &wide_separators,
1930                               "separator-height", &separator_height, NULL);
1931 
1932   if (wide_separators) {
1933     gtk_render_frame(style, cr, x + padding.left, y + padding.top,
1934                      w - padding.left - padding.right, separator_height);
1935   } else {
1936     gtk_render_line(style, cr, x + padding.left, y + padding.top,
1937                     x + w - padding.right - 1, y + padding.top);
1938   }
1939 
1940   gtk_style_context_restore(style);
1941 
1942   return MOZ_GTK_SUCCESS;
1943 }
1944 
1945 // See gtk_menu_item_draw() for reference.
moz_gtk_menu_item_paint(WidgetNodeType widget,cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)1946 static gint moz_gtk_menu_item_paint(WidgetNodeType widget, cairo_t* cr,
1947                                     GdkRectangle* rect, GtkWidgetState* state,
1948                                     GtkTextDirection direction) {
1949   gint x, y, w, h;
1950   guint minorVersion = gtk_get_minor_version();
1951   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1952 
1953   // GTK versions prior to 3.8 render the background and frame only when not
1954   // a separator and in hover prelight.
1955   if (minorVersion < 8 && (widget == MOZ_GTK_MENUSEPARATOR ||
1956                            !(state_flags & GTK_STATE_FLAG_PRELIGHT)))
1957     return MOZ_GTK_SUCCESS;
1958 
1959   GtkStyleContext* style = GetStyleContext(widget, direction, state_flags);
1960 
1961   if (minorVersion < 6) {
1962     // GTK+ 3.4 saves the style context and adds the menubar class to
1963     // menubar children, but does each of these only when drawing, not
1964     // during layout.
1965     gtk_style_context_save(style);
1966     if (widget == MOZ_GTK_MENUBARITEM) {
1967       gtk_style_context_add_class(style, GTK_STYLE_CLASS_MENUBAR);
1968     }
1969   }
1970 
1971   x = rect->x;
1972   y = rect->y;
1973   w = rect->width;
1974   h = rect->height;
1975 
1976   gtk_render_background(style, cr, x, y, w, h);
1977   gtk_render_frame(style, cr, x, y, w, h);
1978 
1979   if (minorVersion < 6) {
1980     gtk_style_context_restore(style);
1981   }
1982 
1983   return MOZ_GTK_SUCCESS;
1984 }
1985 
moz_gtk_menu_arrow_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,GtkTextDirection direction)1986 static gint moz_gtk_menu_arrow_paint(cairo_t* cr, GdkRectangle* rect,
1987                                      GtkWidgetState* state,
1988                                      GtkTextDirection direction) {
1989   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1990   GtkStyleContext* style =
1991       GetStyleContext(MOZ_GTK_MENUITEM, direction, state_flags);
1992   gtk_render_arrow(style, cr,
1993                    (direction == GTK_TEXT_DIR_LTR) ? ARROW_RIGHT : ARROW_LEFT,
1994                    rect->x, rect->y, rect->width);
1995   return MOZ_GTK_SUCCESS;
1996 }
1997 
1998 // For reference, see gtk_check_menu_item_size_allocate() in GTK versions after
1999 // 3.20 and gtk_real_check_menu_item_draw_indicator() in earlier versions.
moz_gtk_check_menu_item_paint(WidgetNodeType widgetType,cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,gboolean checked,GtkTextDirection direction)2000 static gint moz_gtk_check_menu_item_paint(WidgetNodeType widgetType,
2001                                           cairo_t* cr, GdkRectangle* rect,
2002                                           GtkWidgetState* state,
2003                                           gboolean checked,
2004                                           GtkTextDirection direction) {
2005   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
2006   GtkStyleContext* style;
2007   gint indicator_size, horizontal_padding;
2008   gint x, y;
2009 
2010   moz_gtk_menu_item_paint(MOZ_GTK_MENUITEM, cr, rect, state, direction);
2011 
2012   if (checked) {
2013     state_flags =
2014         static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
2015   }
2016 
2017   bool pre_3_20 = gtk_get_minor_version() < 20;
2018   gint offset;
2019   style = GetStyleContext(widgetType, direction);
2020   gtk_style_context_get_style(style, "indicator-size", &indicator_size,
2021                               "horizontal-padding", &horizontal_padding, NULL);
2022   if (pre_3_20) {
2023     GtkBorder padding;
2024     gtk_style_context_get_padding(style, state_flags, &padding);
2025     offset = horizontal_padding + padding.left + 2;
2026   } else {
2027     GdkRectangle r = {0};
2028     InsetByMargin(&r, style);
2029     InsetByBorderPadding(&r, style);
2030     offset = r.x;
2031   }
2032 
2033   bool isRadio = (widgetType == MOZ_GTK_RADIOMENUITEM);
2034   WidgetNodeType indicatorType = isRadio ? MOZ_GTK_RADIOMENUITEM_INDICATOR
2035                                          : MOZ_GTK_CHECKMENUITEM_INDICATOR;
2036   style = GetStyleContext(indicatorType, direction, state_flags);
2037 
2038   if (direction == GTK_TEXT_DIR_RTL) {
2039     x = rect->width - indicator_size - offset;
2040   } else {
2041     x = rect->x + offset;
2042   }
2043   y = rect->y + (rect->height - indicator_size) / 2;
2044 
2045   if (!pre_3_20) {
2046     gtk_render_background(style, cr, x, y, indicator_size, indicator_size);
2047     gtk_render_frame(style, cr, x, y, indicator_size, indicator_size);
2048   }
2049 
2050   if (isRadio) {
2051     gtk_render_option(style, cr, x, y, indicator_size, indicator_size);
2052   } else {
2053     gtk_render_check(style, cr, x, y, indicator_size, indicator_size);
2054   }
2055 
2056   return MOZ_GTK_SUCCESS;
2057 }
2058 
moz_gtk_info_bar_paint(cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state)2059 static gint moz_gtk_info_bar_paint(cairo_t* cr, GdkRectangle* rect,
2060                                    GtkWidgetState* state) {
2061   GtkStyleContext* style =
2062       GetStyleContext(MOZ_GTK_INFO_BAR, GTK_TEXT_DIR_LTR,
2063                       GetStateFlagsFromGtkWidgetState(state));
2064   gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
2065   gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
2066 
2067   return MOZ_GTK_SUCCESS;
2068 }
2069 
moz_gtk_header_bar_paint(WidgetNodeType widgetType,cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state)2070 static gint moz_gtk_header_bar_paint(WidgetNodeType widgetType, cairo_t* cr,
2071                                      GdkRectangle* rect,
2072                                      GtkWidgetState* state) {
2073   GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
2074   GtkStyleContext* style =
2075       GetStyleContext(widgetType, GTK_TEXT_DIR_LTR, state_flags);
2076   InsetByMargin(rect, style);
2077 
2078 // Some themes (Adwaita for instance) draws bold dark line at
2079 // titlebar bottom. It does not fit well with Firefox tabs so
2080 // draw with some extent to make the titlebar bottom part invisible.
2081 #define TITLEBAR_EXTENT 4
2082   gtk_render_background(style, cr, rect->x, rect->y, rect->width,
2083                         rect->height + TITLEBAR_EXTENT);
2084   gtk_render_frame(style, cr, rect->x, rect->y, rect->width,
2085                    rect->height + TITLEBAR_EXTENT);
2086 
2087   return MOZ_GTK_SUCCESS;
2088 }
2089 
GetMarginBorderPadding(GtkStyleContext * aStyle)2090 static GtkBorder GetMarginBorderPadding(GtkStyleContext* aStyle) {
2091   gint left = 0, top = 0, right = 0, bottom = 0;
2092   moz_gtk_add_margin_border_padding(aStyle, &left, &top, &right, &bottom);
2093   // narrowing conversions to gint16:
2094   GtkBorder result;
2095   result.left = left;
2096   result.right = right;
2097   result.top = top;
2098   result.bottom = bottom;
2099   return result;
2100 }
2101 
moz_gtk_get_widget_border(WidgetNodeType widget,gint * left,gint * top,gint * right,gint * bottom,GtkTextDirection direction)2102 gint moz_gtk_get_widget_border(WidgetNodeType widget, gint* left, gint* top,
2103                                gint* right, gint* bottom,
2104                                // NOTE: callers depend on direction being used
2105                                // only for MOZ_GTK_DROPDOWN widgets.
2106                                GtkTextDirection direction) {
2107   GtkWidget* w;
2108   GtkStyleContext* style;
2109   *left = *top = *right = *bottom = 0;
2110 
2111   switch (widget) {
2112     case MOZ_GTK_BUTTON:
2113     case MOZ_GTK_TOOLBAR_BUTTON: {
2114       style = GetStyleContext(MOZ_GTK_BUTTON);
2115 
2116       *left = *top = *right = *bottom = gtk_container_get_border_width(
2117           GTK_CONTAINER(GetWidget(MOZ_GTK_BUTTON)));
2118 
2119       if (widget == MOZ_GTK_TOOLBAR_BUTTON) {
2120         gtk_style_context_save(style);
2121         gtk_style_context_add_class(style, "image-button");
2122       }
2123 
2124       moz_gtk_add_style_padding(style, left, top, right, bottom);
2125 
2126       if (widget == MOZ_GTK_TOOLBAR_BUTTON) gtk_style_context_restore(style);
2127 
2128       moz_gtk_add_style_border(style, left, top, right, bottom);
2129 
2130       return MOZ_GTK_SUCCESS;
2131     }
2132     case MOZ_GTK_ENTRY: {
2133       style = GetStyleContext(MOZ_GTK_ENTRY);
2134 
2135       // XXX: Subtract 1 pixel from the padding to account for the default
2136       // padding in forms.css. See bug 1187385.
2137       *left = *top = *right = *bottom = -1;
2138       moz_gtk_add_border_padding(style, left, top, right, bottom);
2139 
2140       return MOZ_GTK_SUCCESS;
2141     }
2142     case MOZ_GTK_TEXT_VIEW:
2143     case MOZ_GTK_TREEVIEW: {
2144       style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW);
2145       moz_gtk_add_style_border(style, left, top, right, bottom);
2146       return MOZ_GTK_SUCCESS;
2147     }
2148     case MOZ_GTK_TREE_HEADER_CELL: {
2149       /* A Tree Header in GTK is just a different styled button
2150        * It must be placed in a TreeView for getting the correct style
2151        * assigned.
2152        * That is why the following code is the same as for MOZ_GTK_BUTTON.
2153        * */
2154       *left = *top = *right = *bottom = gtk_container_get_border_width(
2155           GTK_CONTAINER(GetWidget(MOZ_GTK_TREE_HEADER_CELL)));
2156       style = GetStyleContext(MOZ_GTK_TREE_HEADER_CELL);
2157       moz_gtk_add_border_padding(style, left, top, right, bottom);
2158       return MOZ_GTK_SUCCESS;
2159     }
2160     case MOZ_GTK_TREE_HEADER_SORTARROW:
2161       w = GetWidget(MOZ_GTK_TREE_HEADER_SORTARROW);
2162       break;
2163     case MOZ_GTK_DROPDOWN_ENTRY:
2164       w = GetWidget(MOZ_GTK_COMBOBOX_ENTRY_TEXTAREA);
2165       break;
2166     case MOZ_GTK_DROPDOWN_ARROW:
2167       w = GetWidget(MOZ_GTK_COMBOBOX_ENTRY_BUTTON);
2168       break;
2169     case MOZ_GTK_DROPDOWN: {
2170       /* We need to account for the arrow on the dropdown, so text
2171        * doesn't come too close to the arrow, or in some cases spill
2172        * into the arrow. */
2173       gboolean wide_separators;
2174       gint separator_width;
2175       GtkRequisition arrow_req;
2176       GtkBorder border;
2177 
2178       *left = *top = *right = *bottom = gtk_container_get_border_width(
2179           GTK_CONTAINER(GetWidget(MOZ_GTK_COMBOBOX_BUTTON)));
2180       style = GetStyleContext(MOZ_GTK_COMBOBOX_BUTTON);
2181       moz_gtk_add_border_padding(style, left, top, right, bottom);
2182 
2183       /* If there is no separator, don't try to count its width. */
2184       separator_width = 0;
2185       GtkWidget* comboBoxSeparator = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR);
2186       if (comboBoxSeparator) {
2187         style = gtk_widget_get_style_context(comboBoxSeparator);
2188         gtk_style_context_get_style(style, "wide-separators", &wide_separators,
2189                                     "separator-width", &separator_width, NULL);
2190 
2191         if (!wide_separators) {
2192           gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
2193           separator_width = border.left;
2194         }
2195       }
2196 
2197       gtk_widget_get_preferred_size(GetWidget(MOZ_GTK_COMBOBOX_ARROW), NULL,
2198                                     &arrow_req);
2199 
2200       if (direction == GTK_TEXT_DIR_RTL)
2201         *left += separator_width + arrow_req.width;
2202       else
2203         *right += separator_width + arrow_req.width;
2204 
2205       return MOZ_GTK_SUCCESS;
2206     }
2207     case MOZ_GTK_TABPANELS:
2208       w = GetWidget(MOZ_GTK_TABPANELS);
2209       break;
2210     case MOZ_GTK_PROGRESSBAR:
2211       w = GetWidget(MOZ_GTK_PROGRESSBAR);
2212       break;
2213     case MOZ_GTK_SPINBUTTON_ENTRY:
2214     case MOZ_GTK_SPINBUTTON_UP:
2215     case MOZ_GTK_SPINBUTTON_DOWN:
2216       w = GetWidget(MOZ_GTK_SPINBUTTON);
2217       break;
2218     case MOZ_GTK_SCALE_HORIZONTAL:
2219     case MOZ_GTK_SCALE_VERTICAL:
2220       w = GetWidget(widget);
2221       break;
2222     case MOZ_GTK_FRAME:
2223       w = GetWidget(MOZ_GTK_FRAME);
2224       break;
2225     case MOZ_GTK_CHECKBUTTON_CONTAINER:
2226     case MOZ_GTK_RADIOBUTTON_CONTAINER: {
2227       w = GetWidget(widget);
2228       style = gtk_widget_get_style_context(w);
2229 
2230       *left = *top = *right = *bottom =
2231           gtk_container_get_border_width(GTK_CONTAINER(w));
2232       moz_gtk_add_border_padding(style, left, top, right, bottom);
2233       return MOZ_GTK_SUCCESS;
2234     }
2235     case MOZ_GTK_MENUPOPUP:
2236       w = GetWidget(MOZ_GTK_MENUPOPUP);
2237       break;
2238     case MOZ_GTK_MENUBARITEM:
2239     case MOZ_GTK_MENUITEM:
2240     case MOZ_GTK_CHECKMENUITEM:
2241     case MOZ_GTK_RADIOMENUITEM: {
2242       // Bug 1274143 for MOZ_GTK_MENUBARITEM
2243       WidgetNodeType type =
2244           widget == MOZ_GTK_MENUBARITEM ? MOZ_GTK_MENUITEM : widget;
2245       style = GetStyleContext(type);
2246 
2247       if (gtk_get_minor_version() < 20) {
2248         moz_gtk_add_style_padding(style, left, top, right, bottom);
2249       } else {
2250         moz_gtk_add_margin_border_padding(style, left, top, right, bottom);
2251       }
2252       return MOZ_GTK_SUCCESS;
2253     }
2254     case MOZ_GTK_INFO_BAR:
2255       w = GetWidget(MOZ_GTK_INFO_BAR);
2256       break;
2257     case MOZ_GTK_TOOLTIP: {
2258       // In GTK 3 there are 6 pixels of additional margin around the box.
2259       // See details there:
2260       // https://github.com/GNOME/gtk/blob/5ea69a136bd7e4970b3a800390e20314665aaed2/gtk/ui/gtktooltipwindow.ui#L11
2261       *left = *right = *top = *bottom = 6;
2262 
2263       // We also need to add margin/padding/borders from Tooltip content.
2264       // Tooltip contains horizontal box, where icon and label is put.
2265       // We ignore icon as long as we don't have support for it.
2266       GtkStyleContext* boxStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX);
2267       moz_gtk_add_margin_border_padding(boxStyle, left, top, right, bottom);
2268 
2269       GtkStyleContext* labelStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX_LABEL);
2270       moz_gtk_add_margin_border_padding(labelStyle, left, top, right, bottom);
2271 
2272       return MOZ_GTK_SUCCESS;
2273     }
2274     case MOZ_GTK_HEADER_BAR:
2275     case MOZ_GTK_HEADER_BAR_MAXIMIZED: {
2276       style = GetStyleContext(widget);
2277       moz_gtk_add_border_padding(style, left, top, right, bottom);
2278       *top = *bottom = 0;
2279       return MOZ_GTK_SUCCESS;
2280     }
2281     /* These widgets have no borders, since they are not containers. */
2282     case MOZ_GTK_CHECKBUTTON_LABEL:
2283     case MOZ_GTK_RADIOBUTTON_LABEL:
2284     case MOZ_GTK_SPLITTER_HORIZONTAL:
2285     case MOZ_GTK_SPLITTER_VERTICAL:
2286     case MOZ_GTK_CHECKBUTTON:
2287     case MOZ_GTK_RADIOBUTTON:
2288     case MOZ_GTK_SCROLLBAR_BUTTON:
2289     case MOZ_GTK_SCROLLBAR_THUMB_HORIZONTAL:
2290     case MOZ_GTK_SCROLLBAR_THUMB_VERTICAL:
2291     case MOZ_GTK_SCALE_THUMB_HORIZONTAL:
2292     case MOZ_GTK_SCALE_THUMB_VERTICAL:
2293     case MOZ_GTK_GRIPPER:
2294     case MOZ_GTK_PROGRESS_CHUNK:
2295     case MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE:
2296     case MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE:
2297     case MOZ_GTK_TREEVIEW_EXPANDER:
2298     case MOZ_GTK_TOOLBAR_SEPARATOR:
2299     case MOZ_GTK_MENUSEPARATOR:
2300     case MOZ_GTK_HEADER_BAR_BUTTON_CLOSE:
2301     case MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE:
2302     case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE:
2303     case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE:
2304     /* These widgets have no borders.*/
2305     case MOZ_GTK_INNER_SPIN_BUTTON:
2306     case MOZ_GTK_SPINBUTTON:
2307     case MOZ_GTK_WINDOW:
2308     case MOZ_GTK_RESIZER:
2309     case MOZ_GTK_MENUARROW:
2310     case MOZ_GTK_TOOLBARBUTTON_ARROW:
2311     case MOZ_GTK_TOOLBAR:
2312     case MOZ_GTK_MENUBAR:
2313     case MOZ_GTK_TAB_SCROLLARROW:
2314       return MOZ_GTK_SUCCESS;
2315     default:
2316       g_warning("Unsupported widget type: %d", widget);
2317       return MOZ_GTK_UNKNOWN_WIDGET;
2318   }
2319   /* TODO - we're still missing some widget implementations */
2320   if (w) {
2321     moz_gtk_add_style_border(gtk_widget_get_style_context(w), left, top, right,
2322                              bottom);
2323   }
2324   return MOZ_GTK_SUCCESS;
2325 }
2326 
moz_gtk_get_tab_border(gint * left,gint * top,gint * right,gint * bottom,GtkTextDirection direction,GtkTabFlags flags,WidgetNodeType widget)2327 gint moz_gtk_get_tab_border(gint* left, gint* top, gint* right, gint* bottom,
2328                             GtkTextDirection direction, GtkTabFlags flags,
2329                             WidgetNodeType widget) {
2330   GtkStyleContext* style =
2331       GetStyleContext(widget, direction, GetStateFlagsFromGtkTabFlags(flags));
2332 
2333   *left = *top = *right = *bottom = 0;
2334   moz_gtk_add_style_padding(style, left, top, right, bottom);
2335 
2336   // Gtk >= 3.20 does not use those styles
2337   if (gtk_check_version(3, 20, 0) != nullptr) {
2338     int tab_curvature;
2339 
2340     gtk_style_context_get_style(style, "tab-curvature", &tab_curvature, NULL);
2341     *left += tab_curvature;
2342     *right += tab_curvature;
2343 
2344     if (flags & MOZ_GTK_TAB_FIRST) {
2345       int initial_gap = 0;
2346       gtk_style_context_get_style(style, "initial-gap", &initial_gap, NULL);
2347       if (direction == GTK_TEXT_DIR_RTL)
2348         *right += initial_gap;
2349       else
2350         *left += initial_gap;
2351     }
2352   } else {
2353     GtkBorder margin;
2354 
2355     gtk_style_context_get_margin(style, GTK_STATE_FLAG_NORMAL, &margin);
2356     *left += margin.left;
2357     *right += margin.right;
2358 
2359     if (flags & MOZ_GTK_TAB_FIRST) {
2360       style = GetStyleContext(MOZ_GTK_NOTEBOOK_HEADER, direction);
2361       gtk_style_context_get_margin(style, GTK_STATE_FLAG_NORMAL, &margin);
2362       *left += margin.left;
2363       *right += margin.right;
2364     }
2365   }
2366 
2367   return MOZ_GTK_SUCCESS;
2368 }
2369 
moz_gtk_get_combo_box_entry_button_size(gint * width,gint * height)2370 gint moz_gtk_get_combo_box_entry_button_size(gint* width, gint* height) {
2371   /*
2372    * We get the requisition of the drop down button, which includes
2373    * all padding, border and focus line widths the button uses,
2374    * as well as the minimum arrow size and its padding
2375    * */
2376   GtkRequisition requisition;
2377 
2378   gtk_widget_get_preferred_size(GetWidget(MOZ_GTK_COMBOBOX_ENTRY_BUTTON), NULL,
2379                                 &requisition);
2380   *width = requisition.width;
2381   *height = requisition.height;
2382 
2383   return MOZ_GTK_SUCCESS;
2384 }
2385 
moz_gtk_get_tab_scroll_arrow_size(gint * width,gint * height)2386 gint moz_gtk_get_tab_scroll_arrow_size(gint* width, gint* height) {
2387   gint arrow_size;
2388 
2389   GtkStyleContext* style = GetStyleContext(MOZ_GTK_TABPANELS);
2390   gtk_style_context_get_style(style, "scroll-arrow-hlength", &arrow_size, NULL);
2391 
2392   *height = *width = arrow_size;
2393 
2394   return MOZ_GTK_SUCCESS;
2395 }
2396 
moz_gtk_get_arrow_size(WidgetNodeType widgetType,gint * width,gint * height)2397 void moz_gtk_get_arrow_size(WidgetNodeType widgetType, gint* width,
2398                             gint* height) {
2399   GtkWidget* widget;
2400   switch (widgetType) {
2401     case MOZ_GTK_DROPDOWN:
2402       widget = GetWidget(MOZ_GTK_COMBOBOX_ARROW);
2403       break;
2404     default:
2405       widget = GetWidget(MOZ_GTK_BUTTON_ARROW);
2406       break;
2407   }
2408 
2409   GtkRequisition requisition;
2410   gtk_widget_get_preferred_size(widget, NULL, &requisition);
2411   *width = requisition.width;
2412   *height = requisition.height;
2413 }
2414 
moz_gtk_get_toolbar_separator_width(gint * size)2415 gint moz_gtk_get_toolbar_separator_width(gint* size) {
2416   gboolean wide_separators;
2417   gint separator_width;
2418   GtkBorder border;
2419 
2420   GtkStyleContext* style = GetStyleContext(MOZ_GTK_TOOLBAR);
2421   gtk_style_context_get_style(style, "space-size", size, "wide-separators",
2422                               &wide_separators, "separator-width",
2423                               &separator_width, NULL);
2424   /* Just in case... */
2425   gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
2426   *size = MAX(*size, (wide_separators ? separator_width : border.left));
2427   return MOZ_GTK_SUCCESS;
2428 }
2429 
moz_gtk_get_expander_size(gint * size)2430 gint moz_gtk_get_expander_size(gint* size) {
2431   GtkStyleContext* style = GetStyleContext(MOZ_GTK_EXPANDER);
2432   gtk_style_context_get_style(style, "expander-size", size, NULL);
2433   return MOZ_GTK_SUCCESS;
2434 }
2435 
moz_gtk_get_treeview_expander_size(gint * size)2436 gint moz_gtk_get_treeview_expander_size(gint* size) {
2437   GtkStyleContext* style = GetStyleContext(MOZ_GTK_TREEVIEW);
2438   gtk_style_context_get_style(style, "expander-size", size, NULL);
2439   return MOZ_GTK_SUCCESS;
2440 }
2441 
2442 // See gtk_menu_item_draw() for reference.
moz_gtk_get_menu_separator_height(gint * size)2443 gint moz_gtk_get_menu_separator_height(gint* size) {
2444   gboolean wide_separators;
2445   gint separator_height;
2446   GtkBorder padding;
2447   GtkStyleContext* style = GetStyleContext(MOZ_GTK_MENUSEPARATOR);
2448   gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
2449 
2450   gtk_style_context_save(style);
2451   gtk_style_context_add_class(style, GTK_STYLE_CLASS_SEPARATOR);
2452 
2453   gtk_style_context_get_style(style, "wide-separators", &wide_separators,
2454                               "separator-height", &separator_height, NULL);
2455 
2456   gtk_style_context_restore(style);
2457 
2458   *size = padding.top + padding.bottom;
2459   *size += (wide_separators) ? separator_height : 1;
2460 
2461   return MOZ_GTK_SUCCESS;
2462 }
2463 
moz_gtk_get_entry_min_height(gint * height)2464 void moz_gtk_get_entry_min_height(gint* height) {
2465   GtkStyleContext* style = GetStyleContext(MOZ_GTK_ENTRY);
2466   if (!gtk_check_version(3, 20, 0)) {
2467     gtk_style_context_get(style, gtk_style_context_get_state(style),
2468                           "min-height", height, nullptr);
2469   } else {
2470     *height = 0;
2471   }
2472 
2473   GtkBorder border;
2474   GtkBorder padding;
2475   gtk_style_context_get_border(style, GTK_STATE_FLAG_NORMAL, &border);
2476   gtk_style_context_get_padding(style, GTK_STATE_FLAG_NORMAL, &padding);
2477 
2478   *height += (border.top + border.bottom + padding.top + padding.bottom);
2479 }
2480 
moz_gtk_get_scale_metrics(GtkOrientation orient,gint * scale_width,gint * scale_height)2481 void moz_gtk_get_scale_metrics(GtkOrientation orient, gint* scale_width,
2482                                gint* scale_height) {
2483   if (gtk_check_version(3, 20, 0) != nullptr) {
2484     WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
2485                                 ? MOZ_GTK_SCALE_HORIZONTAL
2486                                 : MOZ_GTK_SCALE_VERTICAL;
2487 
2488     gint thumb_length, thumb_height, trough_border;
2489     moz_gtk_get_scalethumb_metrics(orient, &thumb_length, &thumb_height);
2490 
2491     GtkStyleContext* style = GetStyleContext(widget);
2492     gtk_style_context_get_style(style, "trough-border", &trough_border, NULL);
2493 
2494     if (orient == GTK_ORIENTATION_HORIZONTAL) {
2495       *scale_width = thumb_length + trough_border * 2;
2496       *scale_height = thumb_height + trough_border * 2;
2497     } else {
2498       *scale_width = thumb_height + trough_border * 2;
2499       *scale_height = thumb_length + trough_border * 2;
2500     }
2501   } else {
2502     WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
2503                                 ? MOZ_GTK_SCALE_TROUGH_HORIZONTAL
2504                                 : MOZ_GTK_SCALE_TROUGH_VERTICAL;
2505     moz_gtk_get_widget_min_size(GetStyleContext(widget), scale_width,
2506                                 scale_height);
2507   }
2508 }
2509 
moz_gtk_get_scalethumb_metrics(GtkOrientation orient,gint * thumb_length,gint * thumb_height)2510 gint moz_gtk_get_scalethumb_metrics(GtkOrientation orient, gint* thumb_length,
2511                                     gint* thumb_height) {
2512   if (gtk_check_version(3, 20, 0) != nullptr) {
2513     WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
2514                                 ? MOZ_GTK_SCALE_HORIZONTAL
2515                                 : MOZ_GTK_SCALE_VERTICAL;
2516     GtkStyleContext* style = GetStyleContext(widget);
2517     gtk_style_context_get_style(style, "slider_length", thumb_length,
2518                                 "slider_width", thumb_height, NULL);
2519   } else {
2520     WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
2521                                 ? MOZ_GTK_SCALE_THUMB_HORIZONTAL
2522                                 : MOZ_GTK_SCALE_THUMB_VERTICAL;
2523     GtkStyleContext* style = GetStyleContext(widget);
2524 
2525     gint min_width, min_height;
2526     GtkStateFlags state = gtk_style_context_get_state(style);
2527     gtk_style_context_get(style, state, "min-width", &min_width, "min-height",
2528                           &min_height, nullptr);
2529     GtkBorder margin;
2530     gtk_style_context_get_margin(style, state, &margin);
2531     gint margin_width = margin.left + margin.right;
2532     gint margin_height = margin.top + margin.bottom;
2533 
2534     // Negative margin of slider element also determines its minimal size
2535     // so use bigger of those two values.
2536     if (min_width < -margin_width) min_width = -margin_width;
2537     if (min_height < -margin_height) min_height = -margin_height;
2538 
2539     *thumb_length = min_width;
2540     *thumb_height = min_height;
2541   }
2542 
2543   return MOZ_GTK_SUCCESS;
2544 }
2545 
SizeFromLengthAndBreadth(GtkOrientation aOrientation,gint aLength,gint aBreadth)2546 static MozGtkSize SizeFromLengthAndBreadth(GtkOrientation aOrientation,
2547                                            gint aLength, gint aBreadth) {
2548   return aOrientation == GTK_ORIENTATION_HORIZONTAL
2549              ? MozGtkSize({aLength, aBreadth})
2550              : MozGtkSize({aBreadth, aLength});
2551 }
2552 
GetToggleMetrics(bool isRadio)2553 const ToggleGTKMetrics* GetToggleMetrics(bool isRadio) {
2554   ToggleGTKMetrics* metrics;
2555   if (isRadio) {
2556     metrics = &sRadioMetrics;
2557   } else {
2558     metrics = &sCheckboxMetrics;
2559   }
2560   if (metrics->initialized) return metrics;
2561 
2562   metrics->initialized = true;
2563   if (gtk_check_version(3, 20, 0) == nullptr) {
2564     GtkStyleContext* style;
2565     if (isRadio) {
2566       style = GetStyleContext(MOZ_GTK_RADIOBUTTON);
2567     } else {
2568       style = GetStyleContext(MOZ_GTK_CHECKBUTTON);
2569     }
2570     GtkStateFlags state_flags = gtk_style_context_get_state(style);
2571     gtk_style_context_get(style, state_flags, "min-height",
2572                           &(metrics->minSizeWithBorder.height), "min-width",
2573                           &(metrics->minSizeWithBorder.width), nullptr);
2574     // Fallback to indicator size if min dimensions are zero
2575     if (metrics->minSizeWithBorder.height == 0 ||
2576         metrics->minSizeWithBorder.width == 0) {
2577       gint indicator_size;
2578       gtk_widget_style_get(GetWidget(MOZ_GTK_CHECKBUTTON_CONTAINER),
2579                            "indicator_size", &indicator_size, nullptr);
2580       if (metrics->minSizeWithBorder.height == 0) {
2581         metrics->minSizeWithBorder.height = indicator_size;
2582       }
2583       if (metrics->minSizeWithBorder.width == 0) {
2584         metrics->minSizeWithBorder.width = indicator_size;
2585       }
2586     }
2587 
2588     GtkBorder border, padding;
2589     gtk_style_context_get_border(style, state_flags, &border);
2590     gtk_style_context_get_padding(style, state_flags, &padding);
2591     metrics->borderAndPadding.left = border.left + padding.left;
2592     metrics->borderAndPadding.right = border.right + padding.right;
2593     metrics->borderAndPadding.top = border.top + padding.top;
2594     metrics->borderAndPadding.bottom = border.bottom + padding.bottom;
2595     metrics->minSizeWithBorder.width +=
2596         metrics->borderAndPadding.left + metrics->borderAndPadding.right;
2597     metrics->minSizeWithBorder.height +=
2598         metrics->borderAndPadding.top + metrics->borderAndPadding.bottom;
2599   } else {
2600     gint indicator_size, indicator_spacing;
2601     gtk_widget_style_get(GetWidget(MOZ_GTK_CHECKBUTTON_CONTAINER),
2602                          "indicator_size", &indicator_size, "indicator_spacing",
2603                          &indicator_spacing, nullptr);
2604     metrics->minSizeWithBorder.width = metrics->minSizeWithBorder.height =
2605         indicator_size;
2606   }
2607   return metrics;
2608 }
2609 
GetScrollbarMetrics(GtkOrientation aOrientation,bool aActive)2610 const ScrollbarGTKMetrics* GetScrollbarMetrics(GtkOrientation aOrientation,
2611                                                bool aActive) {
2612   auto metrics = aActive ? &sScrollbarMetricsActive[aOrientation]
2613                          : &sScrollbarMetrics[aOrientation];
2614   if (metrics->initialized) return metrics;
2615 
2616   metrics->initialized = true;
2617 
2618   WidgetNodeType scrollbar = aOrientation == GTK_ORIENTATION_HORIZONTAL
2619                                  ? MOZ_GTK_SCROLLBAR_HORIZONTAL
2620                                  : MOZ_GTK_SCROLLBAR_VERTICAL;
2621 
2622   gboolean backward, forward, secondary_backward, secondary_forward;
2623   GtkStyleContext* style = GetStyleContext(
2624       scrollbar, GTK_TEXT_DIR_NONE,
2625       aActive ? GTK_STATE_FLAG_PRELIGHT : GTK_STATE_FLAG_NORMAL);
2626   gtk_style_context_get_style(
2627       style, "has-backward-stepper", &backward, "has-forward-stepper", &forward,
2628       "has-secondary-backward-stepper", &secondary_backward,
2629       "has-secondary-forward-stepper", &secondary_forward, nullptr);
2630   bool hasButtons =
2631       backward || forward || secondary_backward || secondary_forward;
2632 
2633   if (gtk_get_minor_version() < 20) {
2634     gint slider_width, trough_border, stepper_size, min_slider_size;
2635 
2636     gtk_style_context_get_style(style, "slider-width", &slider_width,
2637                                 "trough-border", &trough_border, "stepper-size",
2638                                 &stepper_size, "min-slider-length",
2639                                 &min_slider_size, nullptr);
2640 
2641     metrics->size.thumb =
2642         SizeFromLengthAndBreadth(aOrientation, min_slider_size, slider_width);
2643     metrics->size.button =
2644         SizeFromLengthAndBreadth(aOrientation, stepper_size, slider_width);
2645     // overall scrollbar
2646     gint breadth = slider_width + 2 * trough_border;
2647     // Require room for the slider in the track if we don't have buttons.
2648     gint length = hasButtons ? 0 : min_slider_size + 2 * trough_border;
2649     metrics->size.scrollbar =
2650         SizeFromLengthAndBreadth(aOrientation, length, breadth);
2651 
2652     // Borders on the major axis are set on the outermost scrollbar
2653     // element to correctly position the buttons when
2654     // trough-under-steppers is true.
2655     // Borders on the minor axis are set on the track element so that it
2656     // receives mouse events, as in GTK.
2657     // Other borders have been zero-initialized.
2658     if (aOrientation == GTK_ORIENTATION_HORIZONTAL) {
2659       metrics->border.scrollbar.left = metrics->border.scrollbar.right =
2660           metrics->border.track.top = metrics->border.track.bottom =
2661               trough_border;
2662     } else {
2663       metrics->border.scrollbar.top = metrics->border.scrollbar.bottom =
2664           metrics->border.track.left = metrics->border.track.right =
2665               trough_border;
2666     }
2667 
2668     return metrics;
2669   }
2670 
2671   // GTK version > 3.20
2672   // scrollbar
2673   metrics->border.scrollbar = GetMarginBorderPadding(style);
2674 
2675   WidgetNodeType contents, track, thumb;
2676   if (aOrientation == GTK_ORIENTATION_HORIZONTAL) {
2677     contents = MOZ_GTK_SCROLLBAR_CONTENTS_HORIZONTAL;
2678     track = MOZ_GTK_SCROLLBAR_TROUGH_HORIZONTAL;
2679     thumb = MOZ_GTK_SCROLLBAR_THUMB_HORIZONTAL;
2680   } else {
2681     contents = MOZ_GTK_SCROLLBAR_CONTENTS_VERTICAL;
2682     track = MOZ_GTK_SCROLLBAR_TROUGH_VERTICAL;
2683     thumb = MOZ_GTK_SCROLLBAR_THUMB_VERTICAL;
2684   }
2685 
2686   /* GetStyleContext() sets GtkStateFlags to the latest widget name
2687    * in css selector string. When we call:
2688    *
2689    *     GetStyleContext(thumb, GTK_STATE_FLAG_PRELIGHT)
2690    *
2691    * we get:
2692    *
2693    *    "scrollbar contents trough slider:hover"
2694    *
2695    * Some themes (Ubuntu Ambiance) styles trough/thumb by scrollbar,
2696    * the Gtk+ css rule looks like:
2697    *
2698    *    "scrollbar:hover contents trough slider"
2699    *
2700    *  So we need to apply GtkStateFlags to each widgets in style path.
2701    */
2702 
2703   // thumb
2704   style = CreateStyleContextWithStates(
2705       thumb, GTK_TEXT_DIR_NONE,
2706       aActive ? GTK_STATE_FLAG_PRELIGHT : GTK_STATE_FLAG_NORMAL);
2707   metrics->size.thumb = GetMinMarginBox(style);
2708   g_object_unref(style);
2709 
2710   // track
2711   style = CreateStyleContextWithStates(
2712       track, GTK_TEXT_DIR_NONE,
2713       aActive ? GTK_STATE_FLAG_PRELIGHT : GTK_STATE_FLAG_NORMAL);
2714   metrics->border.track = GetMarginBorderPadding(style);
2715   MozGtkSize trackMinSize = GetMinContentBox(style) + metrics->border.track;
2716   MozGtkSize trackSizeForThumb = metrics->size.thumb + metrics->border.track;
2717   g_object_unref(style);
2718 
2719   // button
2720   if (hasButtons) {
2721     style = CreateStyleContextWithStates(
2722         MOZ_GTK_SCROLLBAR_BUTTON, GTK_TEXT_DIR_NONE,
2723         aActive ? GTK_STATE_FLAG_PRELIGHT : GTK_STATE_FLAG_NORMAL);
2724     metrics->size.button = GetMinMarginBox(style);
2725     g_object_unref(style);
2726   } else {
2727     metrics->size.button = {0, 0};
2728   }
2729   if (aOrientation == GTK_ORIENTATION_HORIZONTAL) {
2730     metrics->size.button.Rotate();
2731     // If the track is wider than necessary for the thumb, including when
2732     // the buttons will cause Gecko to expand the track to fill
2733     // available breadth, then add to the track border to prevent Gecko
2734     // from expanding the thumb to fill available breadth.
2735     gint extra = std::max(trackMinSize.height, metrics->size.button.height) -
2736                  trackSizeForThumb.height;
2737     if (extra > 0) {
2738       // If extra is odd, then the thumb is 0.5 pixels above
2739       // center as in gtk_range_compute_slider_position().
2740       metrics->border.track.top += extra / 2;
2741       metrics->border.track.bottom += extra - extra / 2;
2742       // Update size for change in border.
2743       trackSizeForThumb.height += extra;
2744     }
2745   } else {
2746     gint extra = std::max(trackMinSize.width, metrics->size.button.width) -
2747                  trackSizeForThumb.width;
2748     if (extra > 0) {
2749       // If extra is odd, then the thumb is 0.5 pixels to the left
2750       // of center as in gtk_range_compute_slider_position().
2751       metrics->border.track.left += extra / 2;
2752       metrics->border.track.right += extra - extra / 2;
2753       trackSizeForThumb.width += extra;
2754     }
2755   }
2756 
2757   style = CreateStyleContextWithStates(
2758       contents, GTK_TEXT_DIR_NONE,
2759       aActive ? GTK_STATE_FLAG_PRELIGHT : GTK_STATE_FLAG_NORMAL);
2760   GtkBorder contentsBorder = GetMarginBorderPadding(style);
2761   g_object_unref(style);
2762 
2763   metrics->size.scrollbar =
2764       trackSizeForThumb + contentsBorder + metrics->border.scrollbar;
2765 
2766   return metrics;
2767 }
2768 
2769 /*
2770  * get_shadow_width() from gtkwindow.c is not public so we need
2771  * to implement it.
2772  */
GetCSDDecorationSize(GtkWindow * aGtkWindow,GtkBorder * aDecorationSize)2773 bool GetCSDDecorationSize(GtkWindow* aGtkWindow, GtkBorder* aDecorationSize) {
2774   GtkStyleContext* context =
2775       gtk_widget_get_style_context(GTK_WIDGET(aGtkWindow));
2776   bool solidDecorations = gtk_style_context_has_class(context, "solid-csd");
2777   context = GetStyleContext(solidDecorations ? MOZ_GTK_WINDOW_DECORATION_SOLID
2778                                              : MOZ_GTK_WINDOW_DECORATION);
2779 
2780   /* Always sum border + padding */
2781   GtkBorder padding;
2782   GtkStateFlags state = gtk_style_context_get_state(context);
2783   gtk_style_context_get_border(context, state, aDecorationSize);
2784   gtk_style_context_get_padding(context, state, &padding);
2785   *aDecorationSize += padding;
2786 
2787   // Available on GTK 3.20+.
2788   static auto sGtkRenderBackgroundGetClip = (void (*)(
2789       GtkStyleContext*, gdouble, gdouble, gdouble, gdouble,
2790       GdkRectangle*))dlsym(RTLD_DEFAULT, "gtk_render_background_get_clip");
2791 
2792   GtkBorder margin;
2793   gtk_style_context_get_margin(context, state, &margin);
2794 
2795   GtkBorder extents = {0, 0, 0, 0};
2796   if (sGtkRenderBackgroundGetClip) {
2797     /* Get shadow extents but combine with style margin; use the bigger value.
2798      */
2799     GdkRectangle clip;
2800     sGtkRenderBackgroundGetClip(context, 0, 0, 0, 0, &clip);
2801 
2802     extents.top = -clip.y;
2803     extents.right = clip.width + clip.x;
2804     extents.bottom = clip.height + clip.y;
2805     extents.left = -clip.x;
2806 
2807     // Margin is used for resize grip size - it's not present on
2808     // popup windows.
2809     if (gtk_window_get_window_type(aGtkWindow) != GTK_WINDOW_POPUP) {
2810       extents.top = MAX(extents.top, margin.top);
2811       extents.right = MAX(extents.right, margin.right);
2812       extents.bottom = MAX(extents.bottom, margin.bottom);
2813       extents.left = MAX(extents.left, margin.left);
2814     }
2815   } else {
2816     /* If we can't get shadow extents use decoration-resize-handle instead
2817      * as a workaround. This is inspired by update_border_windows()
2818      * from gtkwindow.c although this is not 100% accurate as we emulate
2819      * the extents here.
2820      */
2821     gint handle;
2822     gtk_widget_style_get(GetWidget(MOZ_GTK_WINDOW), "decoration-resize-handle",
2823                          &handle, NULL);
2824 
2825     extents.top = handle + margin.top;
2826     extents.right = handle + margin.right;
2827     extents.bottom = handle + margin.bottom;
2828     extents.left = handle + margin.left;
2829   }
2830 
2831   *aDecorationSize += extents;
2832 
2833   return (sGtkRenderBackgroundGetClip != nullptr);
2834 }
2835 
2836 /* cairo_t *cr argument has to be a system-cairo. */
moz_gtk_widget_paint(WidgetNodeType widget,cairo_t * cr,GdkRectangle * rect,GtkWidgetState * state,gint flags,GtkTextDirection direction)2837 gint moz_gtk_widget_paint(WidgetNodeType widget, cairo_t* cr,
2838                           GdkRectangle* rect, GtkWidgetState* state, gint flags,
2839                           GtkTextDirection direction) {
2840   /* A workaround for https://bugzilla.gnome.org/show_bug.cgi?id=694086
2841    */
2842   cairo_new_path(cr);
2843 
2844   switch (widget) {
2845     case MOZ_GTK_BUTTON:
2846     case MOZ_GTK_TOOLBAR_BUTTON:
2847       if (state->depressed) {
2848         return moz_gtk_button_paint(cr, rect, state, (GtkReliefStyle)flags,
2849                                     GetWidget(MOZ_GTK_TOGGLE_BUTTON),
2850                                     direction);
2851       }
2852       return moz_gtk_button_paint(cr, rect, state, (GtkReliefStyle)flags,
2853                                   GetWidget(MOZ_GTK_BUTTON), direction);
2854       break;
2855     case MOZ_GTK_HEADER_BAR_BUTTON_CLOSE:
2856     case MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE:
2857     case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE:
2858     case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE:
2859       return moz_gtk_header_bar_button_paint(
2860           cr, rect, state, (GtkReliefStyle)flags, widget, direction);
2861       break;
2862     case MOZ_GTK_CHECKBUTTON:
2863     case MOZ_GTK_RADIOBUTTON:
2864       return moz_gtk_toggle_paint(cr, rect, state,
2865                                   !!(flags & MOZ_GTK_WIDGET_CHECKED),
2866                                   !!(flags & MOZ_GTK_WIDGET_INCONSISTENT),
2867                                   (widget == MOZ_GTK_RADIOBUTTON), direction);
2868       break;
2869     case MOZ_GTK_SCROLLBAR_BUTTON:
2870       return moz_gtk_scrollbar_button_paint(
2871           cr, rect, state, (GtkScrollbarButtonFlags)flags, direction);
2872       break;
2873     case MOZ_GTK_SCROLLBAR_HORIZONTAL:
2874     case MOZ_GTK_SCROLLBAR_VERTICAL:
2875       if (flags & MOZ_GTK_TRACK_OPAQUE) {
2876         GtkStyleContext* style = GetStyleContext(MOZ_GTK_WINDOW, direction);
2877         gtk_render_background(style, cr, rect->x, rect->y, rect->width,
2878                               rect->height);
2879       }
2880       if (gtk_check_version(3, 20, 0) == nullptr) {
2881         return moz_gtk_scrollbar_paint(widget, cr, rect, state, direction);
2882       } else {
2883         WidgetNodeType trough_widget = (widget == MOZ_GTK_SCROLLBAR_HORIZONTAL)
2884                                            ? MOZ_GTK_SCROLLBAR_TROUGH_HORIZONTAL
2885                                            : MOZ_GTK_SCROLLBAR_TROUGH_VERTICAL;
2886         return moz_gtk_scrollbar_trough_paint(trough_widget, cr, rect, state,
2887                                               direction);
2888       }
2889       break;
2890     case MOZ_GTK_SCROLLBAR_TROUGH_HORIZONTAL:
2891     case MOZ_GTK_SCROLLBAR_TROUGH_VERTICAL:
2892       if (gtk_check_version(3, 20, 0) == nullptr) {
2893         return moz_gtk_scrollbar_trough_paint(widget, cr, rect, state,
2894                                               direction);
2895       }
2896       break;
2897     case MOZ_GTK_SCROLLBAR_THUMB_HORIZONTAL:
2898     case MOZ_GTK_SCROLLBAR_THUMB_VERTICAL:
2899       return moz_gtk_scrollbar_thumb_paint(widget, cr, rect, state, direction);
2900       break;
2901     case MOZ_GTK_SCALE_HORIZONTAL:
2902     case MOZ_GTK_SCALE_VERTICAL:
2903       return moz_gtk_scale_paint(cr, rect, state, (GtkOrientation)flags,
2904                                  direction);
2905       break;
2906     case MOZ_GTK_SCALE_THUMB_HORIZONTAL:
2907     case MOZ_GTK_SCALE_THUMB_VERTICAL:
2908       return moz_gtk_scale_thumb_paint(cr, rect, state, (GtkOrientation)flags,
2909                                        direction);
2910       break;
2911     case MOZ_GTK_INNER_SPIN_BUTTON:
2912       return moz_gtk_inner_spin_paint(cr, rect, state, direction);
2913       break;
2914     case MOZ_GTK_SPINBUTTON:
2915       return moz_gtk_spin_paint(cr, rect, direction);
2916       break;
2917     case MOZ_GTK_SPINBUTTON_UP:
2918     case MOZ_GTK_SPINBUTTON_DOWN:
2919       return moz_gtk_spin_updown_paint(
2920           cr, rect, (widget == MOZ_GTK_SPINBUTTON_DOWN), state, direction);
2921       break;
2922     case MOZ_GTK_SPINBUTTON_ENTRY: {
2923       GtkStyleContext* style =
2924           GetStyleContext(MOZ_GTK_SPINBUTTON_ENTRY, direction,
2925                           GetStateFlagsFromGtkWidgetState(state));
2926       gint ret = moz_gtk_entry_paint(cr, rect, state, style);
2927       return ret;
2928     } break;
2929     case MOZ_GTK_GRIPPER:
2930       return moz_gtk_gripper_paint(cr, rect, state, direction);
2931       break;
2932     case MOZ_GTK_TREEVIEW:
2933       return moz_gtk_treeview_paint(cr, rect, state, direction);
2934       break;
2935     case MOZ_GTK_TREE_HEADER_CELL:
2936       return moz_gtk_tree_header_cell_paint(cr, rect, state, flags, direction);
2937       break;
2938     case MOZ_GTK_TREE_HEADER_SORTARROW:
2939       return moz_gtk_tree_header_sort_arrow_paint(
2940           cr, rect, state, (GtkArrowType)flags, direction);
2941       break;
2942     case MOZ_GTK_TREEVIEW_EXPANDER:
2943       return moz_gtk_treeview_expander_paint(
2944           cr, rect, state, (GtkExpanderStyle)flags, direction);
2945       break;
2946     case MOZ_GTK_ENTRY: {
2947       GtkStyleContext* style = GetStyleContext(
2948           MOZ_GTK_ENTRY, direction, GetStateFlagsFromGtkWidgetState(state));
2949       gint ret = moz_gtk_entry_paint(cr, rect, state, style);
2950       return ret;
2951     }
2952     case MOZ_GTK_TEXT_VIEW:
2953       return moz_gtk_text_view_paint(cr, rect, state, direction);
2954       break;
2955     case MOZ_GTK_DROPDOWN:
2956       return moz_gtk_combo_box_paint(cr, rect, state, direction);
2957       break;
2958     case MOZ_GTK_DROPDOWN_ARROW:
2959       return moz_gtk_combo_box_entry_button_paint(cr, rect, state, flags,
2960                                                   direction);
2961       break;
2962     case MOZ_GTK_DROPDOWN_ENTRY: {
2963       GtkStyleContext* style =
2964           GetStyleContext(MOZ_GTK_COMBOBOX_ENTRY_TEXTAREA, direction,
2965                           GetStateFlagsFromGtkWidgetState(state));
2966       gint ret = moz_gtk_entry_paint(cr, rect, state, style);
2967       return ret;
2968     } break;
2969     case MOZ_GTK_CHECKBUTTON_CONTAINER:
2970     case MOZ_GTK_RADIOBUTTON_CONTAINER:
2971       return moz_gtk_container_paint(cr, rect, state, widget, direction);
2972       break;
2973     case MOZ_GTK_CHECKBUTTON_LABEL:
2974     case MOZ_GTK_RADIOBUTTON_LABEL:
2975       return moz_gtk_toggle_label_paint(
2976           cr, rect, state, (widget == MOZ_GTK_RADIOBUTTON_LABEL), direction);
2977       break;
2978     case MOZ_GTK_TOOLBAR:
2979       return moz_gtk_toolbar_paint(cr, rect, direction);
2980       break;
2981     case MOZ_GTK_TOOLBAR_SEPARATOR:
2982       return moz_gtk_toolbar_separator_paint(cr, rect, direction);
2983       break;
2984     case MOZ_GTK_TOOLTIP:
2985       return moz_gtk_tooltip_paint(cr, rect, direction);
2986       break;
2987     case MOZ_GTK_FRAME:
2988       return moz_gtk_frame_paint(cr, rect, direction);
2989       break;
2990     case MOZ_GTK_RESIZER:
2991       return moz_gtk_resizer_paint(cr, rect, state, direction);
2992       break;
2993     case MOZ_GTK_PROGRESSBAR:
2994       return moz_gtk_progressbar_paint(cr, rect, direction);
2995       break;
2996     case MOZ_GTK_PROGRESS_CHUNK:
2997     case MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE:
2998     case MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE:
2999       return moz_gtk_progress_chunk_paint(cr, rect, direction, widget);
3000       break;
3001     case MOZ_GTK_TAB_TOP:
3002     case MOZ_GTK_TAB_BOTTOM:
3003       return moz_gtk_tab_paint(cr, rect, state, (GtkTabFlags)flags, direction,
3004                                widget);
3005       break;
3006     case MOZ_GTK_TABPANELS:
3007       return moz_gtk_tabpanels_paint(cr, rect, direction);
3008       break;
3009     case MOZ_GTK_TAB_SCROLLARROW:
3010       return moz_gtk_tab_scroll_arrow_paint(cr, rect, state,
3011                                             (GtkArrowType)flags, direction);
3012       break;
3013     case MOZ_GTK_MENUBAR:
3014       return moz_gtk_menu_bar_paint(cr, rect, direction);
3015       break;
3016     case MOZ_GTK_MENUPOPUP:
3017       return moz_gtk_menu_popup_paint(cr, rect, direction);
3018       break;
3019     case MOZ_GTK_MENUSEPARATOR:
3020       return moz_gtk_menu_separator_paint(cr, rect, direction);
3021       break;
3022     case MOZ_GTK_MENUBARITEM:
3023     case MOZ_GTK_MENUITEM:
3024       return moz_gtk_menu_item_paint(widget, cr, rect, state, direction);
3025       break;
3026     case MOZ_GTK_MENUARROW:
3027       return moz_gtk_menu_arrow_paint(cr, rect, state, direction);
3028       break;
3029     case MOZ_GTK_TOOLBARBUTTON_ARROW:
3030       return moz_gtk_arrow_paint(cr, rect, state, (GtkArrowType)flags,
3031                                  direction);
3032       break;
3033     case MOZ_GTK_CHECKMENUITEM:
3034     case MOZ_GTK_RADIOMENUITEM:
3035       return moz_gtk_check_menu_item_paint(widget, cr, rect, state,
3036                                            (gboolean)flags, direction);
3037       break;
3038     case MOZ_GTK_SPLITTER_HORIZONTAL:
3039       return moz_gtk_vpaned_paint(cr, rect, state);
3040       break;
3041     case MOZ_GTK_SPLITTER_VERTICAL:
3042       return moz_gtk_hpaned_paint(cr, rect, state);
3043       break;
3044     case MOZ_GTK_WINDOW:
3045       return moz_gtk_window_paint(cr, rect, direction);
3046       break;
3047     case MOZ_GTK_INFO_BAR:
3048       return moz_gtk_info_bar_paint(cr, rect, state);
3049       break;
3050     case MOZ_GTK_HEADER_BAR:
3051     case MOZ_GTK_HEADER_BAR_MAXIMIZED:
3052       return moz_gtk_header_bar_paint(widget, cr, rect, state);
3053       break;
3054     default:
3055       g_warning("Unknown widget type: %d", widget);
3056   }
3057 
3058   return MOZ_GTK_UNKNOWN_WIDGET;
3059 }
3060 
moz_gtk_shutdown()3061 gint moz_gtk_shutdown() {
3062   /* This will destroy all of our widgets */
3063   ResetWidgetCache();
3064 
3065   return MOZ_GTK_SUCCESS;
3066 }
3067