Bug 1899500 - Implement explicit resource management in Baseline compiler. r=arai
[gecko.git] / widget / gtk / gtk3drawing.cpp
blobc67716b90ef8f9bdac604c9e4ec4819ad177dc73
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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/. */
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 */
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 "mozilla/ScopeExit.h"
17 #include "prinrval.h"
18 #include "WidgetStyleCache.h"
19 #include "nsString.h"
20 #include "nsDebug.h"
21 #include "WidgetUtilsGtk.h"
23 #include <math.h>
24 #include <dlfcn.h>
26 static gboolean checkbox_check_state;
27 static gboolean notebook_has_tab_gap;
29 static ToggleGTKMetrics sCheckboxMetrics;
30 static ToggleGTKMetrics sRadioMetrics;
31 static ToolbarGTKMetrics sToolbarMetrics;
32 static CSDWindowDecorationSize sToplevelWindowDecorationSize;
33 static CSDWindowDecorationSize sPopupWindowDecorationSize;
35 using mozilla::Span;
37 #define ARROW_UP 0
38 #define ARROW_DOWN G_PI
39 #define ARROW_RIGHT G_PI_2
40 #define ARROW_LEFT (G_PI + G_PI_2)
42 #if 0
43 // It's used for debugging only to compare Gecko widget style with
44 // the ones used by Gtk+ applications.
45 static void
46 style_path_print(GtkStyleContext *context)
48 const GtkWidgetPath* path = gtk_style_context_get_path(context);
50 static auto sGtkWidgetPathToStringPtr =
51 (char * (*)(const GtkWidgetPath *))
52 dlsym(RTLD_DEFAULT, "gtk_widget_path_to_string");
54 fprintf(stderr, "Style path:\n%s\n\n", sGtkWidgetPathToStringPtr(path));
56 #endif
58 static GtkBorder operator+=(GtkBorder& first, const GtkBorder& second) {
59 first.left += second.left;
60 first.right += second.right;
61 first.top += second.top;
62 first.bottom += second.bottom;
63 return first;
66 static gint moz_gtk_get_tab_thickness(GtkStyleContext* style);
68 static void Inset(GdkRectangle*, const GtkBorder&);
70 static void InsetByMargin(GdkRectangle*, GtkStyleContext* style);
72 static void moz_gtk_add_style_margin(GtkStyleContext* style, gint* left,
73 gint* top, gint* right, gint* bottom) {
74 GtkBorder margin;
76 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
77 &margin);
78 *left += margin.left;
79 *right += margin.right;
80 *top += margin.top;
81 *bottom += margin.bottom;
84 static void moz_gtk_add_style_border(GtkStyleContext* style, gint* left,
85 gint* top, gint* right, gint* bottom) {
86 GtkBorder border;
88 gtk_style_context_get_border(style, gtk_style_context_get_state(style),
89 &border);
91 *left += border.left;
92 *right += border.right;
93 *top += border.top;
94 *bottom += border.bottom;
97 static void moz_gtk_add_style_padding(GtkStyleContext* style, gint* left,
98 gint* top, gint* right, gint* bottom) {
99 GtkBorder padding;
101 gtk_style_context_get_padding(style, gtk_style_context_get_state(style),
102 &padding);
104 *left += padding.left;
105 *right += padding.right;
106 *top += padding.top;
107 *bottom += padding.bottom;
110 static void moz_gtk_add_margin_border_padding(GtkStyleContext* style,
111 gint* left, gint* top,
112 gint* right, gint* bottom) {
113 moz_gtk_add_style_margin(style, left, top, right, bottom);
114 moz_gtk_add_style_border(style, left, top, right, bottom);
115 moz_gtk_add_style_padding(style, left, top, right, bottom);
118 static void moz_gtk_add_border_padding(GtkStyleContext* style, gint* left,
119 gint* top, gint* right, gint* bottom) {
120 moz_gtk_add_style_border(style, left, top, right, bottom);
121 moz_gtk_add_style_padding(style, left, top, right, bottom);
124 // In case there's an error in Gtk theme and preferred size is zero,
125 // return some sane values to pass mozilla automation tests.
126 // It should not happen in real-life.
127 #define MIN_WIDGET_SIZE 10
128 static void moz_gtk_sanity_preferred_size(GtkRequisition* requisition) {
129 if (requisition->width <= 0) {
130 requisition->width = MIN_WIDGET_SIZE;
132 if (requisition->height <= 0) {
133 requisition->height = MIN_WIDGET_SIZE;
137 // GetStateFlagsFromGtkWidgetState() can be safely used for the specific
138 // GtkWidgets that set both prelight and active flags. For other widgets,
139 // either the GtkStateFlags or Gecko's GtkWidgetState need to be carefully
140 // adjusted to match GTK behavior. Although GTK sets insensitive and focus
141 // flags in the generic GtkWidget base class, GTK adds prelight and active
142 // flags only to widgets that are expected to demonstrate prelight or active
143 // states. This contrasts with HTML where any element may have :active and
144 // :hover states, and so Gecko's GtkStateFlags do not necessarily map to GTK
145 // flags. Failure to restrict the flags in the same way as GTK can cause
146 // generic CSS selectors from some themes to unintentionally match elements
147 // that are not expected to change appearance on hover or mouse-down.
148 static GtkStateFlags GetStateFlagsFromGtkWidgetState(GtkWidgetState* state) {
149 GtkStateFlags stateFlags = GTK_STATE_FLAG_NORMAL;
151 if (state->disabled)
152 stateFlags = GTK_STATE_FLAG_INSENSITIVE;
153 else {
154 if (state->depressed || state->active)
155 stateFlags =
156 static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_ACTIVE);
157 if (state->inHover)
158 stateFlags =
159 static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_PRELIGHT);
160 if (state->focused)
161 stateFlags =
162 static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_FOCUSED);
163 if (state->backdrop)
164 stateFlags =
165 static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_BACKDROP);
168 return stateFlags;
171 static GtkStateFlags GetStateFlagsFromGtkTabFlags(GtkTabFlags flags) {
172 return ((flags & MOZ_GTK_TAB_SELECTED) == 0) ? GTK_STATE_FLAG_NORMAL
173 : GTK_STATE_FLAG_ACTIVE;
176 gint moz_gtk_init() {
177 if (gtk_major_version > 3 ||
178 (gtk_major_version == 3 && gtk_minor_version >= 14))
179 checkbox_check_state = GTK_STATE_FLAG_CHECKED;
180 else
181 checkbox_check_state = GTK_STATE_FLAG_ACTIVE;
183 moz_gtk_refresh();
185 return MOZ_GTK_SUCCESS;
188 void moz_gtk_refresh() {
189 if (gtk_check_version(3, 20, 0) != nullptr) {
190 // Deprecated for Gtk >= 3.20+
191 GtkStyleContext* style = GetStyleContext(MOZ_GTK_TAB_TOP);
192 gtk_style_context_get_style(style, "has-tab-gap", &notebook_has_tab_gap,
193 NULL);
194 } else {
195 notebook_has_tab_gap = true;
198 sCheckboxMetrics.initialized = false;
199 sRadioMetrics.initialized = false;
200 sToolbarMetrics.initialized = false;
201 sToplevelWindowDecorationSize.initialized = false;
202 sPopupWindowDecorationSize.initialized = false;
204 /* This will destroy all of our widgets */
205 ResetWidgetCache();
208 gint moz_gtk_button_get_default_overflow(gint* border_top, gint* border_left,
209 gint* border_bottom,
210 gint* border_right) {
211 GtkBorder* default_outside_border;
213 GtkStyleContext* style = GetStyleContext(MOZ_GTK_BUTTON);
214 gtk_style_context_get_style(style, "default-outside-border",
215 &default_outside_border, NULL);
217 if (default_outside_border) {
218 *border_top = default_outside_border->top;
219 *border_left = default_outside_border->left;
220 *border_bottom = default_outside_border->bottom;
221 *border_right = default_outside_border->right;
222 gtk_border_free(default_outside_border);
223 } else {
224 *border_top = *border_left = *border_bottom = *border_right = 0;
226 return MOZ_GTK_SUCCESS;
229 static gint moz_gtk_button_get_default_border(gint* border_top,
230 gint* border_left,
231 gint* border_bottom,
232 gint* border_right) {
233 GtkBorder* default_border;
235 GtkStyleContext* style = GetStyleContext(MOZ_GTK_BUTTON);
236 gtk_style_context_get_style(style, "default-border", &default_border, NULL);
238 if (default_border) {
239 *border_top = default_border->top;
240 *border_left = default_border->left;
241 *border_bottom = default_border->bottom;
242 *border_right = default_border->right;
243 gtk_border_free(default_border);
244 } else {
245 /* see gtkbutton.c */
246 *border_top = *border_left = *border_bottom = *border_right = 1;
248 return MOZ_GTK_SUCCESS;
251 gint moz_gtk_splitter_get_metrics(gint orientation, gint* size) {
252 GtkStyleContext* style;
253 if (orientation == GTK_ORIENTATION_HORIZONTAL) {
254 style = GetStyleContext(MOZ_GTK_SPLITTER_HORIZONTAL);
255 } else {
256 style = GetStyleContext(MOZ_GTK_SPLITTER_VERTICAL);
258 gtk_style_context_get_style(style, "handle_size", size, NULL);
259 return MOZ_GTK_SUCCESS;
262 static void CalculateToolbarButtonMetrics(WidgetNodeType aAppearance,
263 ToolbarButtonGTKMetrics* aMetrics,
264 gint* aMaxInlineMargin) {
265 gint iconWidth, iconHeight;
266 if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &iconWidth, &iconHeight)) {
267 NS_WARNING("Failed to get Gtk+ icon size for titlebar button!");
268 // Use some reasonable fallback size
269 iconWidth = 16;
270 iconHeight = 16;
273 GtkStyleContext* style = GetStyleContext(aAppearance);
274 gint width = 0, height = 0;
275 if (!gtk_check_version(3, 20, 0)) {
276 gtk_style_context_get(style, gtk_style_context_get_state(style),
277 "min-width", &width, "min-height", &height, NULL);
280 // Cover cases when min-width/min-height is not set, it's invalid
281 // or we're running on Gtk+ < 3.20.
282 if (width < iconWidth) width = iconWidth;
283 if (height < iconHeight) height = iconHeight;
285 gint left = 0, top = 0, right = 0, bottom = 0;
286 moz_gtk_add_border_padding(style, &left, &top, &right, &bottom);
288 // Button size is calculated as min-width/height + border/padding.
289 width += left + right;
290 height += top + bottom;
292 // Place icon at button center.
293 aMetrics->iconXPosition = (width - iconWidth) / 2;
294 aMetrics->iconYPosition = (height - iconHeight) / 2;
295 aMetrics->minSizeWithBorder = {width, height};
297 GtkBorder margin = {0};
298 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
299 &margin);
300 *aMaxInlineMargin = std::max(*aMaxInlineMargin, margin.left + margin.right);
303 size_t GetGtkHeaderBarButtonLayout(Span<ButtonLayout> aButtonLayout,
304 bool* aReversedButtonsPlacement) {
305 gchar* decorationLayoutSetting = nullptr;
306 GtkSettings* settings = gtk_settings_get_default();
307 g_object_get(settings, "gtk-decoration-layout", &decorationLayoutSetting,
308 nullptr);
309 auto free = mozilla::MakeScopeExit([&] { g_free(decorationLayoutSetting); });
311 // Use a default layout
312 const gchar* decorationLayout = "menu:minimize,maximize,close";
313 if (decorationLayoutSetting) {
314 decorationLayout = decorationLayoutSetting;
317 // "minimize,maximize,close:" layout means buttons are on the opposite
318 // titlebar side. close button is always there.
319 if (aReversedButtonsPlacement) {
320 const char* closeButton = strstr(decorationLayout, "close");
321 const char* separator = strchr(decorationLayout, ':');
322 *aReversedButtonsPlacement =
323 closeButton && separator && closeButton < separator;
326 // We check what position a button string is stored in decorationLayout.
328 // decorationLayout gets its value from the GNOME preference:
329 // org.gnome.desktop.vm.preferences.button-layout via the
330 // gtk-decoration-layout property.
332 // Documentation of the gtk-decoration-layout property can be found here:
333 // https://developer.gnome.org/gtk3/stable/GtkSettings.html#GtkSettings--gtk-decoration-layout
334 if (aButtonLayout.IsEmpty()) {
335 return 0;
338 nsDependentCSubstring layout(decorationLayout, strlen(decorationLayout));
340 size_t activeButtons = 0;
341 for (const auto& part : layout.Split(':')) {
342 for (const auto& button : part.Split(',')) {
343 if (button.EqualsLiteral("close")) {
344 aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_CLOSE};
345 } else if (button.EqualsLiteral("minimize")) {
346 aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE};
347 } else if (button.EqualsLiteral("maximize")) {
348 aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE};
350 if (activeButtons == aButtonLayout.Length()) {
351 return activeButtons;
355 return activeButtons;
358 static void EnsureToolbarMetrics() {
359 if (sToolbarMetrics.initialized) {
360 return;
362 sToolbarMetrics = {};
364 // Calculate titlebar button visibility and positions.
365 ButtonLayout buttonLayout[TOOLBAR_BUTTONS];
366 size_t activeButtonNums =
367 GetGtkHeaderBarButtonLayout(Span(buttonLayout), nullptr);
369 for (const auto& layout : Span(buttonLayout, activeButtonNums)) {
370 int buttonIndex = layout.mType - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE;
371 ToolbarButtonGTKMetrics* metrics = &sToolbarMetrics.button[buttonIndex];
372 CalculateToolbarButtonMetrics(layout.mType, metrics,
373 &sToolbarMetrics.inlineSpacing);
376 // Account for the spacing property in the header bar.
377 // Default to 6 pixels (gtk/gtkheaderbar.c)
378 gint spacing = 6;
379 g_object_get(GetWidget(MOZ_GTK_HEADER_BAR), "spacing", &spacing, nullptr);
380 sToolbarMetrics.inlineSpacing += spacing;
381 sToolbarMetrics.initialized = true;
384 const ToolbarButtonGTKMetrics* GetToolbarButtonMetrics(
385 WidgetNodeType aAppearance) {
386 EnsureToolbarMetrics();
388 int buttonIndex = (aAppearance - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE);
389 NS_ASSERTION(buttonIndex >= 0 && buttonIndex <= TOOLBAR_BUTTONS,
390 "GetToolbarButtonMetrics(): Wrong titlebar button!");
391 return sToolbarMetrics.button + buttonIndex;
394 gint moz_gtk_get_titlebar_button_spacing() {
395 EnsureToolbarMetrics();
396 return sToolbarMetrics.inlineSpacing;
399 static gint moz_gtk_window_decoration_paint(cairo_t* cr,
400 const GdkRectangle* rect,
401 GtkWidgetState* state,
402 GtkTextDirection direction) {
403 if (mozilla::widget::GdkIsWaylandDisplay()) {
404 // Doesn't seem to be needed.
405 return MOZ_GTK_SUCCESS;
407 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
408 GtkStyleContext* windowStyle =
409 GetStyleContext(MOZ_GTK_HEADERBAR_WINDOW, state->image_scale);
410 const bool solidDecorations =
411 gtk_style_context_has_class(windowStyle, "solid-csd");
412 GtkStyleContext* decorationStyle =
413 GetStyleContext(solidDecorations ? MOZ_GTK_WINDOW_DECORATION_SOLID
414 : MOZ_GTK_WINDOW_DECORATION,
415 state->image_scale, GTK_TEXT_DIR_LTR, state_flags);
417 gtk_render_background(decorationStyle, cr, rect->x, rect->y, rect->width,
418 rect->height);
419 gtk_render_frame(decorationStyle, cr, rect->x, rect->y, rect->width,
420 rect->height);
421 return MOZ_GTK_SUCCESS;
424 static gint moz_gtk_button_paint(cairo_t* cr, const GdkRectangle* rect,
425 GtkWidgetState* state, GtkReliefStyle relief,
426 GtkWidget* widget,
427 GtkTextDirection direction) {
428 if (!widget) {
429 return MOZ_GTK_UNKNOWN_WIDGET;
432 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
433 GtkStyleContext* style = gtk_widget_get_style_context(widget);
434 gint x = rect->x, y = rect->y, width = rect->width, height = rect->height;
436 gtk_widget_set_direction(widget, direction);
438 gtk_style_context_save(style);
439 StyleContextSetScale(style, state->image_scale);
440 gtk_style_context_set_state(style, state_flags);
442 if (state->isDefault && relief == GTK_RELIEF_NORMAL && !state->focused &&
443 !(state_flags & GTK_STATE_FLAG_PRELIGHT)) {
444 /* handle default borders both outside and inside the button */
445 gint default_top, default_left, default_bottom, default_right;
446 moz_gtk_button_get_default_overflow(&default_top, &default_left,
447 &default_bottom, &default_right);
448 x -= default_left;
449 y -= default_top;
450 width += default_left + default_right;
451 height += default_top + default_bottom;
452 gtk_render_background(style, cr, x, y, width, height);
453 gtk_render_frame(style, cr, x, y, width, height);
454 moz_gtk_button_get_default_border(&default_top, &default_left,
455 &default_bottom, &default_right);
456 x += default_left;
457 y += default_top;
458 width -= (default_left + default_right);
459 height -= (default_top + default_bottom);
460 } else if (relief != GTK_RELIEF_NONE || state->depressed ||
461 (state_flags & GTK_STATE_FLAG_PRELIGHT)) {
462 /* the following line can trigger an assertion (Crux theme)
463 file ../../gdk/gdkwindow.c: line 1846 (gdk_window_clear_area):
464 assertion `GDK_IS_WINDOW (window)' failed */
465 gtk_render_background(style, cr, x, y, width, height);
466 gtk_render_frame(style, cr, x, y, width, height);
469 if (state->focused) {
470 GtkBorder border;
471 gtk_style_context_get_border(style, state_flags, &border);
472 x += border.left;
473 y += border.top;
474 width -= (border.left + border.right);
475 height -= (border.top + border.bottom);
476 gtk_render_focus(style, cr, x, y, width, height);
478 gtk_style_context_restore(style);
479 return MOZ_GTK_SUCCESS;
482 static gint moz_gtk_header_bar_button_paint(cairo_t* cr, GdkRectangle* aRect,
483 GtkWidgetState* state,
484 GtkReliefStyle relief,
485 WidgetNodeType aIconWidgetType,
486 GtkTextDirection direction) {
487 GtkWidget* buttonWidget = GetWidget(aIconWidgetType);
488 if (!buttonWidget) {
489 return MOZ_GTK_UNKNOWN_WIDGET;
492 const ToolbarButtonGTKMetrics* metrics = GetToolbarButtonMetrics(
493 aIconWidgetType == MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE
494 ? MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE
495 : aIconWidgetType);
496 // Vertically center and clamp the rect to the desired size.
497 if (aRect->height > metrics->minSizeWithBorder.height) {
498 gint diff = aRect->height - metrics->minSizeWithBorder.height;
499 aRect->y += diff / 2;
500 aRect->height = metrics->minSizeWithBorder.height;
502 moz_gtk_button_paint(cr, aRect, state, relief, buttonWidget, direction);
504 GtkWidget* iconWidget =
505 gtk_bin_get_child(GTK_BIN(GetWidget(aIconWidgetType)));
506 if (!iconWidget) {
507 return MOZ_GTK_UNKNOWN_WIDGET;
509 cairo_surface_t* surface =
510 GetWidgetIconSurface(iconWidget, state->image_scale);
512 if (surface) {
513 GtkStyleContext* style = gtk_widget_get_style_context(buttonWidget);
514 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
516 gtk_style_context_save(style);
517 StyleContextSetScale(style, state->image_scale);
518 gtk_style_context_set_state(style, state_flags);
520 /* This is available since Gtk+ 3.10 as well as GtkHeaderBar */
521 gtk_render_icon_surface(style, cr, surface,
522 aRect->x + metrics->iconXPosition,
523 aRect->y + metrics->iconYPosition);
524 gtk_style_context_restore(style);
527 return MOZ_GTK_SUCCESS;
530 static gint moz_gtk_toggle_paint(cairo_t* cr, GdkRectangle* rect,
531 GtkWidgetState* state, gboolean selected,
532 gboolean inconsistent, gboolean isradio,
533 GtkTextDirection direction) {
534 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
535 gint x, y, width, height;
536 GtkStyleContext* style;
538 // We need to call this before GetStyleContext, because otherwise we would
539 // reset state flags
540 const ToggleGTKMetrics* metrics =
541 GetToggleMetrics(isradio ? MOZ_GTK_RADIOBUTTON : MOZ_GTK_CHECKBUTTON);
542 // Clamp the rect and paint it center aligned in the rect.
543 x = rect->x;
544 y = rect->y;
545 width = rect->width;
546 height = rect->height;
548 if (rect->width < rect->height) {
549 y = rect->y + (rect->height - rect->width) / 2;
550 height = rect->width;
553 if (rect->height < rect->width) {
554 x = rect->x + (rect->width - rect->height) / 2;
555 width = rect->height;
558 if (selected)
559 state_flags =
560 static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
562 if (inconsistent)
563 state_flags =
564 static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_INCONSISTENT);
566 style = GetStyleContext(isradio ? MOZ_GTK_RADIOBUTTON : MOZ_GTK_CHECKBUTTON,
567 state->image_scale, direction, state_flags);
569 if (gtk_check_version(3, 20, 0) == nullptr) {
570 gtk_render_background(style, cr, x, y, width, height);
571 gtk_render_frame(style, cr, x, y, width, height);
572 // Indicator is inset by the toggle's padding and border.
573 gint indicator_x = x + metrics->borderAndPadding.left;
574 gint indicator_y = y + metrics->borderAndPadding.top;
575 gint indicator_width = metrics->minSizeWithBorder.width -
576 metrics->borderAndPadding.left -
577 metrics->borderAndPadding.right;
578 gint indicator_height = metrics->minSizeWithBorder.height -
579 metrics->borderAndPadding.top -
580 metrics->borderAndPadding.bottom;
581 if (isradio) {
582 gtk_render_option(style, cr, indicator_x, indicator_y, indicator_width,
583 indicator_height);
584 } else {
585 gtk_render_check(style, cr, indicator_x, indicator_y, indicator_width,
586 indicator_height);
588 } else {
589 if (isradio) {
590 gtk_render_option(style, cr, x, y, width, height);
591 } else {
592 gtk_render_check(style, cr, x, y, width, height);
596 return MOZ_GTK_SUCCESS;
599 static gint calculate_button_inner_rect(GtkWidget* button,
600 const GdkRectangle* rect,
601 GdkRectangle* inner_rect,
602 GtkTextDirection direction) {
603 GtkStyleContext* style;
604 GtkBorder border;
605 GtkBorder padding = {0, 0, 0, 0};
607 style = gtk_widget_get_style_context(button);
609 /* This mirrors gtkbutton's child positioning */
610 gtk_style_context_get_border(style, gtk_style_context_get_state(style),
611 &border);
612 gtk_style_context_get_padding(style, gtk_style_context_get_state(style),
613 &padding);
615 inner_rect->x = rect->x + border.left + padding.left;
616 inner_rect->y = rect->y + padding.top + border.top;
617 inner_rect->width =
618 MAX(1, rect->width - padding.left - padding.right - border.left * 2);
619 inner_rect->height =
620 MAX(1, rect->height - padding.top - padding.bottom - border.top * 2);
622 return MOZ_GTK_SUCCESS;
625 static gint calculate_arrow_rect(GtkWidget* arrow, GdkRectangle* rect,
626 GdkRectangle* arrow_rect,
627 GtkTextDirection direction) {
628 /* defined in gtkarrow.c */
629 gfloat arrow_scaling = 0.7;
630 gfloat xalign, xpad;
631 gint extent;
632 gint mxpad, mypad;
633 gfloat mxalign, myalign;
634 GtkMisc* misc = GTK_MISC(arrow);
636 gtk_style_context_get_style(gtk_widget_get_style_context(arrow),
637 "arrow_scaling", &arrow_scaling, NULL);
639 gtk_misc_get_padding(misc, &mxpad, &mypad);
640 extent = MIN((rect->width - mxpad * 2), (rect->height - mypad * 2)) *
641 arrow_scaling;
643 gtk_misc_get_alignment(misc, &mxalign, &myalign);
645 xalign = direction == GTK_TEXT_DIR_LTR ? mxalign : 1.0 - mxalign;
646 xpad = mxpad + (rect->width - extent) * xalign;
648 arrow_rect->x = direction == GTK_TEXT_DIR_LTR ? floor(rect->x + xpad)
649 : ceil(rect->x + xpad);
650 arrow_rect->y = floor(rect->y + mypad + ((rect->height - extent) * myalign));
652 arrow_rect->width = arrow_rect->height = extent;
654 return MOZ_GTK_SUCCESS;
658 * Get minimum widget size as sum of margin, padding, border and
659 * min-width/min-height.
661 static void moz_gtk_get_widget_min_size(GtkStyleContext* style, int* width,
662 int* height) {
663 GtkStateFlags state_flags = gtk_style_context_get_state(style);
664 gtk_style_context_get(style, state_flags, "min-height", height, "min-width",
665 width, nullptr);
667 GtkBorder border, padding, margin;
668 gtk_style_context_get_border(style, state_flags, &border);
669 gtk_style_context_get_padding(style, state_flags, &padding);
670 gtk_style_context_get_margin(style, state_flags, &margin);
672 *width += border.left + border.right + margin.left + margin.right +
673 padding.left + padding.right;
674 *height += border.top + border.bottom + margin.top + margin.bottom +
675 padding.top + padding.bottom;
678 static void Inset(GdkRectangle* rect, const GtkBorder& aBorder) {
679 rect->x += aBorder.left;
680 rect->y += aBorder.top;
681 rect->width -= aBorder.left + aBorder.right;
682 rect->height -= aBorder.top + aBorder.bottom;
685 // Inset a rectangle by the margins specified in a style context.
686 static void InsetByMargin(GdkRectangle* rect, GtkStyleContext* style) {
687 GtkBorder margin;
688 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
689 &margin);
690 Inset(rect, margin);
693 // Inset a rectangle by the border and padding specified in a style context.
694 static void InsetByBorderPadding(GdkRectangle* rect, GtkStyleContext* style) {
695 GtkStateFlags state = gtk_style_context_get_state(style);
696 GtkBorder padding, border;
698 gtk_style_context_get_padding(style, state, &padding);
699 Inset(rect, padding);
700 gtk_style_context_get_border(style, state, &border);
701 Inset(rect, border);
704 static void moz_gtk_draw_styled_frame(GtkStyleContext* style, cairo_t* cr,
705 const GdkRectangle* aRect,
706 bool drawFocus) {
707 GdkRectangle rect = *aRect;
709 InsetByMargin(&rect, style);
711 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
712 gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
713 if (drawFocus) {
714 gtk_render_focus(style, cr, rect.x, rect.y, rect.width, rect.height);
718 static gint moz_gtk_inner_spin_paint(cairo_t* cr, GdkRectangle* rect,
719 GtkWidgetState* state,
720 GtkTextDirection direction) {
721 GtkStyleContext* style =
722 GetStyleContext(MOZ_GTK_SPINBUTTON, state->image_scale, direction,
723 GetStateFlagsFromGtkWidgetState(state));
725 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
726 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
728 /* hard code these values */
729 GdkRectangle arrow_rect;
730 arrow_rect.width = 6;
731 arrow_rect.height = 6;
733 // align spin to the left
734 arrow_rect.x = rect->x;
736 // up button
737 arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 - 3;
738 gtk_render_arrow(style, cr, ARROW_UP, arrow_rect.x, arrow_rect.y,
739 arrow_rect.width);
741 // down button
742 arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 + 3;
743 gtk_render_arrow(style, cr, ARROW_DOWN, arrow_rect.x, arrow_rect.y,
744 arrow_rect.width);
746 return MOZ_GTK_SUCCESS;
749 static gint moz_gtk_spin_paint(cairo_t* cr, GdkRectangle* rect,
750 GtkWidgetState* state,
751 GtkTextDirection direction) {
752 GtkStyleContext* style =
753 GetStyleContext(MOZ_GTK_SPINBUTTON, state->image_scale, direction);
754 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
755 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
756 return MOZ_GTK_SUCCESS;
759 static gint moz_gtk_spin_updown_paint(cairo_t* cr, GdkRectangle* rect,
760 gboolean isDown, GtkWidgetState* state,
761 GtkTextDirection direction) {
762 GtkStyleContext* style =
763 GetStyleContext(MOZ_GTK_SPINBUTTON, state->image_scale, direction,
764 GetStateFlagsFromGtkWidgetState(state));
766 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
767 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
769 /* hard code these values */
770 GdkRectangle arrow_rect;
771 arrow_rect.width = 6;
772 arrow_rect.height = 6;
773 arrow_rect.x = rect->x + (rect->width - arrow_rect.width) / 2;
774 arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2;
775 arrow_rect.y += isDown ? -1 : 1;
777 gtk_render_arrow(style, cr, isDown ? ARROW_DOWN : ARROW_UP, arrow_rect.x,
778 arrow_rect.y, arrow_rect.width);
780 return MOZ_GTK_SUCCESS;
783 /* See gtk_range_draw() for reference.
785 static gint moz_gtk_scale_paint(cairo_t* cr, GdkRectangle* rect,
786 GtkWidgetState* state, GtkOrientation flags,
787 GtkTextDirection direction) {
788 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
789 gint x, y, width, height, min_width, min_height;
790 GtkStyleContext* style;
791 GtkBorder margin;
793 moz_gtk_get_scale_metrics(flags, &min_width, &min_height);
795 WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
796 ? MOZ_GTK_SCALE_TROUGH_HORIZONTAL
797 : MOZ_GTK_SCALE_TROUGH_VERTICAL;
798 style = GetStyleContext(widget, state->image_scale, direction, state_flags);
799 gtk_style_context_get_margin(style, state_flags, &margin);
801 // Clamp the dimension perpendicular to the direction that the slider crosses
802 // to the minimum size.
803 if (flags == GTK_ORIENTATION_HORIZONTAL) {
804 width = rect->width - (margin.left + margin.right);
805 height = min_height - (margin.top + margin.bottom);
806 x = rect->x + margin.left;
807 y = rect->y + (rect->height - height) / 2;
808 } else {
809 width = min_width - (margin.left + margin.right);
810 height = rect->height - (margin.top + margin.bottom);
811 x = rect->x + (rect->width - width) / 2;
812 y = rect->y + margin.top;
815 gtk_render_background(style, cr, x, y, width, height);
816 gtk_render_frame(style, cr, x, y, width, height);
818 if (state->focused)
819 gtk_render_focus(style, cr, rect->x, rect->y, rect->width, rect->height);
821 return MOZ_GTK_SUCCESS;
824 static gint moz_gtk_scale_thumb_paint(cairo_t* cr, GdkRectangle* rect,
825 GtkWidgetState* state,
826 GtkOrientation flags,
827 GtkTextDirection direction) {
828 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
829 GtkStyleContext* style;
830 gint thumb_width, thumb_height, x, y;
832 /* determine the thumb size, and position the thumb in the center in the
833 * opposite axis
835 if (flags == GTK_ORIENTATION_HORIZONTAL) {
836 moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_HORIZONTAL, &thumb_width,
837 &thumb_height);
838 x = rect->x;
839 y = rect->y + (rect->height - thumb_height) / 2;
840 } else {
841 moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_VERTICAL, &thumb_height,
842 &thumb_width);
843 x = rect->x + (rect->width - thumb_width) / 2;
844 y = rect->y;
847 WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
848 ? MOZ_GTK_SCALE_THUMB_HORIZONTAL
849 : MOZ_GTK_SCALE_THUMB_VERTICAL;
850 style = GetStyleContext(widget, state->image_scale, direction, state_flags);
851 gtk_render_slider(style, cr, x, y, thumb_width, thumb_height, flags);
853 return MOZ_GTK_SUCCESS;
856 static gint moz_gtk_hpaned_paint(cairo_t* cr, GdkRectangle* rect,
857 GtkWidgetState* state) {
858 GtkStyleContext* style =
859 GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_HORIZONTAL, state->image_scale,
860 GTK_TEXT_DIR_LTR, GetStateFlagsFromGtkWidgetState(state));
861 gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
862 return MOZ_GTK_SUCCESS;
865 static gint moz_gtk_vpaned_paint(cairo_t* cr, GdkRectangle* rect,
866 GtkWidgetState* state) {
867 GtkStyleContext* style =
868 GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_VERTICAL, state->image_scale,
869 GTK_TEXT_DIR_LTR, GetStateFlagsFromGtkWidgetState(state));
870 gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
871 return MOZ_GTK_SUCCESS;
874 // See gtk_entry_draw() for reference.
875 static gint moz_gtk_entry_paint(cairo_t* cr, const GdkRectangle* aRect,
876 GtkWidgetState* state, GtkStyleContext* style,
877 WidgetNodeType widget) {
878 GdkRectangle rect = *aRect;
879 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
881 // Paint the border, except for 'menulist-textfield' that isn't focused:
882 if (widget != MOZ_GTK_DROPDOWN_ENTRY || state->focused) {
883 gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
886 return MOZ_GTK_SUCCESS;
889 static gint moz_gtk_text_view_paint(cairo_t* cr, GdkRectangle* aRect,
890 GtkWidgetState* state,
891 GtkTextDirection direction) {
892 // GtkTextView and GtkScrolledWindow do not set active and prelight flags.
893 // The use of focus with MOZ_GTK_SCROLLED_WINDOW here is questionable
894 // because a parent widget will not have focus when its child GtkTextView
895 // has focus, but perhaps this may help identify a focused textarea with
896 // some themes as GtkTextView backgrounds do not typically render
897 // differently with focus.
898 GtkStateFlags state_flags = state->disabled ? GTK_STATE_FLAG_INSENSITIVE
899 : state->focused ? GTK_STATE_FLAG_FOCUSED
900 : GTK_STATE_FLAG_NORMAL;
902 GtkStyleContext* style_frame = GetStyleContext(
903 MOZ_GTK_SCROLLED_WINDOW, state->image_scale, direction, state_flags);
904 gtk_render_frame(style_frame, cr, aRect->x, aRect->y, aRect->width,
905 aRect->height);
907 GdkRectangle rect = *aRect;
908 InsetByBorderPadding(&rect, style_frame);
910 GtkStyleContext* style = GetStyleContext(
911 MOZ_GTK_TEXT_VIEW, state->image_scale, direction, state_flags);
912 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
913 // There is a separate "text" window, which usually provides the
914 // background behind the text. However, this is transparent in Ambiance
915 // for GTK 3.20, in which case the MOZ_GTK_TEXT_VIEW background is
916 // visible.
917 style = GetStyleContext(MOZ_GTK_TEXT_VIEW_TEXT, state->image_scale, direction,
918 state_flags);
919 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
921 return MOZ_GTK_SUCCESS;
924 static gint moz_gtk_treeview_paint(cairo_t* cr, GdkRectangle* rect,
925 GtkWidgetState* state,
926 GtkTextDirection direction) {
927 gint xthickness, ythickness;
928 GtkStyleContext* style;
929 GtkStyleContext* style_tree;
930 GtkStateFlags state_flags;
931 GtkBorder border;
933 /* only handle disabled and normal states, otherwise the whole background
934 * area will be painted differently with other states */
935 state_flags =
936 state->disabled ? GTK_STATE_FLAG_INSENSITIVE : GTK_STATE_FLAG_NORMAL;
938 style =
939 GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, state->image_scale, direction);
940 gtk_style_context_get_border(style, state_flags, &border);
941 xthickness = border.left;
942 ythickness = border.top;
944 style_tree =
945 GetStyleContext(MOZ_GTK_TREEVIEW_VIEW, state->image_scale, direction);
946 gtk_render_background(style_tree, cr, rect->x + xthickness,
947 rect->y + ythickness, rect->width - 2 * xthickness,
948 rect->height - 2 * ythickness);
950 style =
951 GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, state->image_scale, direction);
952 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
953 return MOZ_GTK_SUCCESS;
956 /* See gtk_separator_draw() for reference. */
957 static gint moz_gtk_combo_box_paint(cairo_t* cr, const GdkRectangle* aRect,
958 GtkWidgetState* state,
959 GtkTextDirection direction) {
960 GdkRectangle arrow_rect, real_arrow_rect;
961 gint separator_width;
962 gboolean wide_separators;
963 GtkStyleContext* style;
964 GtkRequisition arrow_req;
966 GtkWidget* comboBoxButton = GetWidget(MOZ_GTK_COMBOBOX_BUTTON);
967 GtkWidget* comboBoxArrow = GetWidget(MOZ_GTK_COMBOBOX_ARROW);
968 if (!comboBoxButton || !comboBoxArrow) {
969 return MOZ_GTK_UNKNOWN_WIDGET;
972 /* Also sets the direction on gComboBoxButtonWidget, which is then
973 * inherited by the separator and arrow */
974 moz_gtk_button_paint(cr, aRect, state, GTK_RELIEF_NORMAL, comboBoxButton,
975 direction);
977 calculate_button_inner_rect(comboBoxButton, aRect, &arrow_rect, direction);
978 /* Now arrow_rect contains the inner rect ; we want to correct the width
979 * to what the arrow needs (see gtk_combo_box_size_allocate) */
980 gtk_widget_get_preferred_size(comboBoxArrow, NULL, &arrow_req);
981 moz_gtk_sanity_preferred_size(&arrow_req);
983 if (direction == GTK_TEXT_DIR_LTR)
984 arrow_rect.x += arrow_rect.width - arrow_req.width;
985 arrow_rect.width = arrow_req.width;
987 calculate_arrow_rect(comboBoxArrow, &arrow_rect, &real_arrow_rect, direction);
989 style = GetStyleContext(MOZ_GTK_COMBOBOX_ARROW, state->image_scale);
990 gtk_render_arrow(style, cr, ARROW_DOWN, real_arrow_rect.x, real_arrow_rect.y,
991 real_arrow_rect.width);
993 /* If there is no separator in the theme, there's nothing left to do. */
994 GtkWidget* widget = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR);
995 if (!widget) {
996 return MOZ_GTK_SUCCESS;
998 style = gtk_widget_get_style_context(widget);
999 StyleContextSetScale(style, state->image_scale);
1000 gtk_style_context_get_style(style, "wide-separators", &wide_separators,
1001 "separator-width", &separator_width, NULL);
1003 if (wide_separators) {
1004 if (direction == GTK_TEXT_DIR_LTR)
1005 arrow_rect.x -= separator_width;
1006 else
1007 arrow_rect.x += arrow_rect.width;
1009 gtk_render_frame(style, cr, arrow_rect.x, arrow_rect.y, separator_width,
1010 arrow_rect.height);
1011 } else {
1012 if (direction == GTK_TEXT_DIR_LTR) {
1013 GtkBorder padding;
1014 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1015 gtk_style_context_get_padding(style, state_flags, &padding);
1016 arrow_rect.x -= padding.left;
1017 } else
1018 arrow_rect.x += arrow_rect.width;
1020 gtk_render_line(style, cr, arrow_rect.x, arrow_rect.y, arrow_rect.x,
1021 arrow_rect.y + arrow_rect.height);
1023 return MOZ_GTK_SUCCESS;
1026 static gint moz_gtk_arrow_paint(cairo_t* cr, GdkRectangle* rect,
1027 GtkWidgetState* state, GtkArrowType arrow_type,
1028 GtkTextDirection direction) {
1029 GdkRectangle arrow_rect;
1030 gdouble arrow_angle;
1032 if (direction == GTK_TEXT_DIR_RTL) {
1033 if (arrow_type == GTK_ARROW_LEFT) {
1034 arrow_type = GTK_ARROW_RIGHT;
1035 } else if (arrow_type == GTK_ARROW_RIGHT) {
1036 arrow_type = GTK_ARROW_LEFT;
1039 switch (arrow_type) {
1040 case GTK_ARROW_LEFT:
1041 arrow_angle = ARROW_LEFT;
1042 break;
1043 case GTK_ARROW_RIGHT:
1044 arrow_angle = ARROW_RIGHT;
1045 break;
1046 case GTK_ARROW_DOWN:
1047 arrow_angle = ARROW_DOWN;
1048 break;
1049 default:
1050 arrow_angle = ARROW_UP;
1051 break;
1053 if (arrow_type == GTK_ARROW_NONE) return MOZ_GTK_SUCCESS;
1055 GtkWidget* widget = GetWidget(MOZ_GTK_BUTTON_ARROW);
1056 if (!widget) {
1057 return MOZ_GTK_UNKNOWN_WIDGET;
1059 calculate_arrow_rect(widget, rect, &arrow_rect, direction);
1060 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1061 GtkStyleContext* style = GetStyleContext(
1062 MOZ_GTK_BUTTON_ARROW, state->image_scale, direction, state_flags);
1063 gtk_render_arrow(style, cr, arrow_angle, arrow_rect.x, arrow_rect.y,
1064 arrow_rect.width);
1065 return MOZ_GTK_SUCCESS;
1068 static gint moz_gtk_tooltip_paint(cairo_t* cr, const GdkRectangle* aRect,
1069 GtkWidgetState* state,
1070 GtkTextDirection direction) {
1071 // Tooltip widget is made in GTK3 as following tree:
1072 // Tooltip window
1073 // Horizontal Box
1074 // Icon (not supported by Firefox)
1075 // Label
1076 // Each element can be fully styled by CSS of GTK theme.
1077 // We have to draw all elements with appropriate offset and right dimensions.
1079 // Tooltip drawing
1080 GtkStyleContext* style =
1081 GetStyleContext(MOZ_GTK_TOOLTIP, state->image_scale, direction);
1082 GdkRectangle rect = *aRect;
1083 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
1084 gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
1086 // Horizontal Box drawing
1088 // The box element has hard-coded 6px margin-* GtkWidget properties, which
1089 // are added between the window dimensions and the CSS margin box of the
1090 // horizontal box. The frame of the tooltip window is drawn in the
1091 // 6px margin.
1092 // For drawing Horizontal Box we have to inset drawing area by that 6px
1093 // plus its CSS margin.
1094 GtkStyleContext* boxStyle =
1095 GetStyleContext(MOZ_GTK_TOOLTIP_BOX, state->image_scale, direction);
1097 rect.x += 6;
1098 rect.y += 6;
1099 rect.width -= 12;
1100 rect.height -= 12;
1102 InsetByMargin(&rect, boxStyle);
1103 gtk_render_background(boxStyle, cr, rect.x, rect.y, rect.width, rect.height);
1104 gtk_render_frame(boxStyle, cr, rect.x, rect.y, rect.width, rect.height);
1106 // Label drawing
1107 InsetByBorderPadding(&rect, boxStyle);
1109 GtkStyleContext* labelStyle =
1110 GetStyleContext(MOZ_GTK_TOOLTIP_BOX_LABEL, state->image_scale, direction);
1111 moz_gtk_draw_styled_frame(labelStyle, cr, &rect, false);
1113 return MOZ_GTK_SUCCESS;
1116 static gint moz_gtk_resizer_paint(cairo_t* cr, GdkRectangle* rect,
1117 GtkWidgetState* state,
1118 GtkTextDirection direction) {
1119 GtkStyleContext* style =
1120 GetStyleContext(MOZ_GTK_RESIZER, state->image_scale, GTK_TEXT_DIR_LTR,
1121 GetStateFlagsFromGtkWidgetState(state));
1123 // Workaround unico not respecting the text direction for resizers.
1124 // See bug 1174248.
1125 cairo_save(cr);
1126 if (direction == GTK_TEXT_DIR_RTL) {
1127 cairo_matrix_t mat;
1128 cairo_matrix_init_translate(&mat, 2 * rect->x + rect->width, 0);
1129 cairo_matrix_scale(&mat, -1, 1);
1130 cairo_transform(cr, &mat);
1133 gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
1134 cairo_restore(cr);
1136 return MOZ_GTK_SUCCESS;
1139 static gint moz_gtk_frame_paint(cairo_t* cr, GdkRectangle* rect,
1140 GtkWidgetState* state,
1141 GtkTextDirection direction) {
1142 GtkStyleContext* style =
1143 GetStyleContext(MOZ_GTK_FRAME, state->image_scale, direction);
1144 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1145 return MOZ_GTK_SUCCESS;
1148 static gint moz_gtk_progressbar_paint(cairo_t* cr, GdkRectangle* rect,
1149 GtkWidgetState* state,
1150 GtkTextDirection direction) {
1151 GtkStyleContext* style =
1152 GetStyleContext(MOZ_GTK_PROGRESS_TROUGH, state->image_scale, direction);
1153 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1154 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1156 return MOZ_GTK_SUCCESS;
1159 static gint moz_gtk_progress_chunk_paint(cairo_t* cr, GdkRectangle* rect,
1160 GtkWidgetState* state,
1161 GtkTextDirection direction,
1162 WidgetNodeType widget) {
1163 GtkStyleContext* style =
1164 GetStyleContext(MOZ_GTK_PROGRESS_CHUNK, state->image_scale, direction);
1166 if (widget == MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE ||
1167 widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE) {
1169 * The bar's size and the bar speed are set depending of the progress'
1170 * size. These could also be constant for all progress bars easily.
1172 gboolean vertical =
1173 (widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE);
1175 /* The size of the dimension we are going to use for the animation. */
1176 const gint progressSize = vertical ? rect->height : rect->width;
1178 /* The bar is using a fifth of the element size, based on GtkProgressBar
1179 * activity-blocks property. */
1180 const gint barSize = MAX(1, progressSize / 5);
1182 /* Represents the travel that has to be done for a complete cycle. */
1183 const gint travel = 2 * (progressSize - barSize);
1185 /* period equals to travel / pixelsPerMillisecond
1186 * where pixelsPerMillisecond equals progressSize / 1000.0.
1187 * This is equivalent to 1600. */
1188 static const guint period = 1600;
1189 const gint t = PR_IntervalToMilliseconds(PR_IntervalNow()) % period;
1190 const gint dx = travel * t / period;
1192 if (vertical) {
1193 rect->y += (dx < travel / 2) ? dx : travel - dx;
1194 rect->height = barSize;
1195 } else {
1196 rect->x += (dx < travel / 2) ? dx : travel - dx;
1197 rect->width = barSize;
1201 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1202 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1204 return MOZ_GTK_SUCCESS;
1207 static gint moz_gtk_get_tab_thickness(GtkStyleContext* style) {
1208 if (!notebook_has_tab_gap)
1209 return 0; /* tabs do not overdraw the tabpanel border with "no gap" style */
1211 GtkBorder border;
1212 gtk_style_context_get_border(style, gtk_style_context_get_state(style),
1213 &border);
1214 if (border.top < 2) return 2; /* some themes don't set ythickness correctly */
1216 return border.top;
1219 gint moz_gtk_get_tab_thickness(WidgetNodeType aNodeType) {
1220 GtkStyleContext* style = GetStyleContext(aNodeType);
1221 int thickness = moz_gtk_get_tab_thickness(style);
1222 return thickness;
1225 /* actual small tabs */
1226 static gint moz_gtk_tab_paint(cairo_t* cr, GdkRectangle* rect,
1227 GtkWidgetState* state, GtkTabFlags flags,
1228 GtkTextDirection direction,
1229 WidgetNodeType widget) {
1230 /* When the tab isn't selected, we just draw a notebook extension.
1231 * When it is selected, we overwrite the adjacent border of the tabpanel
1232 * touching the tab with a pierced border (called "the gap") to make the
1233 * tab appear physically attached to the tabpanel; see details below. */
1235 GtkStyleContext* style;
1236 GdkRectangle tabRect;
1237 GdkRectangle focusRect;
1238 GdkRectangle backRect;
1239 int initial_gap = 0;
1240 bool isBottomTab = (widget == MOZ_GTK_TAB_BOTTOM);
1242 style = GetStyleContext(widget, state->image_scale, direction,
1243 GetStateFlagsFromGtkTabFlags(flags));
1244 tabRect = *rect;
1246 if (flags & MOZ_GTK_TAB_FIRST) {
1247 gtk_style_context_get_style(style, "initial-gap", &initial_gap, NULL);
1248 tabRect.width -= initial_gap;
1250 if (direction != GTK_TEXT_DIR_RTL) {
1251 tabRect.x += initial_gap;
1255 focusRect = backRect = tabRect;
1257 if (notebook_has_tab_gap) {
1258 if ((flags & MOZ_GTK_TAB_SELECTED) == 0) {
1259 /* Only draw the tab */
1260 gtk_render_extension(style, cr, tabRect.x, tabRect.y, tabRect.width,
1261 tabRect.height,
1262 isBottomTab ? GTK_POS_TOP : GTK_POS_BOTTOM);
1263 } else {
1264 /* Draw the tab and the gap
1265 * We want the gap to be positioned exactly on the tabpanel top
1266 * border; since tabbox.css may set a negative margin so that the tab
1267 * frame rect already overlaps the tabpanel frame rect, we need to take
1268 * that into account when drawing. To that effect, nsNativeThemeGTK
1269 * passes us this negative margin (bmargin in the graphic below) in the
1270 * lowest bits of |flags|. We use it to set gap_voffset, the distance
1271 * between the top of the gap and the bottom of the tab (resp. the
1272 * bottom of the gap and the top of the tab when we draw a bottom tab),
1273 * while ensuring that the gap always touches the border of the tab,
1274 * i.e. 0 <= gap_voffset <= gap_height, to avoid surprinsing results
1275 * with big negative or positive margins.
1276 * Here is a graphical explanation in the case of top tabs:
1277 * ___________________________
1278 * / \
1279 * | T A B |
1280 * ----------|. . . . . . . . . . . . . . .|----- top of tabpanel
1281 * : ^ bmargin : ^
1282 * : | (-negative margin, : |
1283 * bottom : v passed in flags) : | gap_height
1284 * of -> :.............................: | (the size of the
1285 * the tab . part of the gap . | tabpanel top border)
1286 * . outside of the tab . v
1287 * ----------------------------------------------
1289 * To draw the gap, we use gtk_render_frame_gap(), see comment in
1290 * moz_gtk_tabpanels_paint(). This gap is made 3 * gap_height tall,
1291 * which should suffice to ensure that the only visible border is the
1292 * pierced one. If the tab is in the middle, we make the box_gap begin
1293 * a bit to the left of the tab and end a bit to the right, adjusting
1294 * the gap position so it still is under the tab, because we want the
1295 * rendering of a gap in the middle of a tabpanel. This is the role of
1296 * the gints gap_{l,r}_offset. On the contrary, if the tab is the
1297 * first, we align the start border of the box_gap with the start
1298 * border of the tab (left if LTR, right if RTL), by setting the
1299 * appropriate offset to 0.*/
1300 gint gap_loffset, gap_roffset, gap_voffset, gap_height;
1302 /* Get height needed by the gap */
1303 gap_height = moz_gtk_get_tab_thickness(style);
1305 /* Extract gap_voffset from the first bits of flags */
1306 gap_voffset = flags & MOZ_GTK_TAB_MARGIN_MASK;
1307 if (gap_voffset > gap_height) gap_voffset = gap_height;
1309 /* Set gap_{l,r}_offset to appropriate values */
1310 gap_loffset = gap_roffset = 20; /* should be enough */
1311 if (flags & MOZ_GTK_TAB_FIRST) {
1312 if (direction == GTK_TEXT_DIR_RTL)
1313 gap_roffset = initial_gap;
1314 else
1315 gap_loffset = initial_gap;
1318 GtkStyleContext* panelStyle =
1319 GetStyleContext(MOZ_GTK_TABPANELS, state->image_scale, direction);
1321 if (isBottomTab) {
1322 /* Draw the tab on bottom */
1323 focusRect.y += gap_voffset;
1324 focusRect.height -= gap_voffset;
1326 gtk_render_extension(style, cr, tabRect.x, tabRect.y + gap_voffset,
1327 tabRect.width, tabRect.height - gap_voffset,
1328 GTK_POS_TOP);
1330 backRect.y += (gap_voffset - gap_height);
1331 backRect.height = gap_height;
1333 /* Draw the gap; erase with background color before painting in
1334 * case theme does not */
1335 gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
1336 backRect.width, backRect.height);
1337 cairo_save(cr);
1338 cairo_rectangle(cr, backRect.x, backRect.y, backRect.width,
1339 backRect.height);
1340 cairo_clip(cr);
1342 gtk_render_frame_gap(panelStyle, cr, tabRect.x - gap_loffset,
1343 tabRect.y + gap_voffset - 3 * gap_height,
1344 tabRect.width + gap_loffset + gap_roffset,
1345 3 * gap_height, GTK_POS_BOTTOM, gap_loffset,
1346 gap_loffset + tabRect.width);
1347 cairo_restore(cr);
1348 } else {
1349 /* Draw the tab on top */
1350 focusRect.height -= gap_voffset;
1351 gtk_render_extension(style, cr, tabRect.x, tabRect.y, tabRect.width,
1352 tabRect.height - gap_voffset, GTK_POS_BOTTOM);
1354 backRect.y += (tabRect.height - gap_voffset);
1355 backRect.height = gap_height;
1357 /* Draw the gap; erase with background color before painting in
1358 * case theme does not */
1359 gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
1360 backRect.width, backRect.height);
1362 cairo_save(cr);
1363 cairo_rectangle(cr, backRect.x, backRect.y, backRect.width,
1364 backRect.height);
1365 cairo_clip(cr);
1367 gtk_render_frame_gap(panelStyle, cr, tabRect.x - gap_loffset,
1368 tabRect.y + tabRect.height - gap_voffset,
1369 tabRect.width + gap_loffset + gap_roffset,
1370 3 * gap_height, GTK_POS_TOP, gap_loffset,
1371 gap_loffset + tabRect.width);
1372 cairo_restore(cr);
1375 } else {
1376 gtk_render_background(style, cr, tabRect.x, tabRect.y, tabRect.width,
1377 tabRect.height);
1378 gtk_render_frame(style, cr, tabRect.x, tabRect.y, tabRect.width,
1379 tabRect.height);
1382 if (state->focused) {
1383 /* Paint the focus ring */
1384 GtkBorder padding;
1385 gtk_style_context_get_padding(style, GetStateFlagsFromGtkWidgetState(state),
1386 &padding);
1388 focusRect.x += padding.left;
1389 focusRect.width -= (padding.left + padding.right);
1390 focusRect.y += padding.top;
1391 focusRect.height -= (padding.top + padding.bottom);
1393 gtk_render_focus(style, cr, focusRect.x, focusRect.y, focusRect.width,
1394 focusRect.height);
1397 return MOZ_GTK_SUCCESS;
1400 /* tab area*/
1401 static gint moz_gtk_tabpanels_paint(cairo_t* cr, GdkRectangle* rect,
1402 GtkWidgetState* state,
1403 GtkTextDirection direction) {
1404 GtkStyleContext* style =
1405 GetStyleContext(MOZ_GTK_TABPANELS, state->image_scale, direction);
1406 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1408 * The gap size is not needed in moz_gtk_tabpanels_paint because
1409 * the gap will be painted with the foreground tab in moz_gtk_tab_paint.
1411 * However, if moz_gtk_tabpanels_paint just uses gtk_render_frame(),
1412 * the theme will think that there are no tabs and may draw something
1413 * different.Hence the trick of using two clip regions, and drawing the
1414 * gap outside each clip region, to get the correct frame for
1415 * a tabpanel with tabs.
1417 /* left side */
1418 cairo_save(cr);
1419 cairo_rectangle(cr, rect->x, rect->y, rect->x + rect->width / 2,
1420 rect->y + rect->height);
1421 cairo_clip(cr);
1422 gtk_render_frame_gap(style, cr, rect->x, rect->y, rect->width, rect->height,
1423 GTK_POS_TOP, rect->width - 1, rect->width);
1424 cairo_restore(cr);
1426 /* right side */
1427 cairo_save(cr);
1428 cairo_rectangle(cr, rect->x + rect->width / 2, rect->y, rect->x + rect->width,
1429 rect->y + rect->height);
1430 cairo_clip(cr);
1431 gtk_render_frame_gap(style, cr, rect->x, rect->y, rect->width, rect->height,
1432 GTK_POS_TOP, 0, 1);
1433 cairo_restore(cr);
1435 return MOZ_GTK_SUCCESS;
1438 static gint moz_gtk_tab_scroll_arrow_paint(cairo_t* cr, GdkRectangle* rect,
1439 GtkWidgetState* state,
1440 GtkArrowType arrow_type,
1441 GtkTextDirection direction) {
1442 GtkStyleContext* style;
1443 gdouble arrow_angle;
1444 gint arrow_size = MIN(rect->width, rect->height);
1445 gint x = rect->x + (rect->width - arrow_size) / 2;
1446 gint y = rect->y + (rect->height - arrow_size) / 2;
1448 if (direction == GTK_TEXT_DIR_RTL) {
1449 arrow_type =
1450 (arrow_type == GTK_ARROW_LEFT) ? GTK_ARROW_RIGHT : GTK_ARROW_LEFT;
1452 switch (arrow_type) {
1453 case GTK_ARROW_LEFT:
1454 arrow_angle = ARROW_LEFT;
1455 break;
1456 case GTK_ARROW_RIGHT:
1457 arrow_angle = ARROW_RIGHT;
1458 break;
1459 case GTK_ARROW_DOWN:
1460 arrow_angle = ARROW_DOWN;
1461 break;
1462 default:
1463 arrow_angle = ARROW_UP;
1464 break;
1466 if (arrow_type != GTK_ARROW_NONE) {
1467 style = GetStyleContext(MOZ_GTK_TAB_SCROLLARROW, state->image_scale,
1468 direction, GetStateFlagsFromGtkWidgetState(state));
1469 gtk_render_arrow(style, cr, arrow_angle, x, y, arrow_size);
1471 return MOZ_GTK_SUCCESS;
1474 static gint moz_gtk_header_bar_paint(WidgetNodeType widgetType, cairo_t* cr,
1475 GdkRectangle* rect,
1476 GtkWidgetState* state) {
1477 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1478 GtkStyleContext* style = GetStyleContext(widgetType, state->image_scale,
1479 GTK_TEXT_DIR_NONE, state_flags);
1481 // Some themes like Elementary's style the container of the headerbar rather
1482 // than the header bar itself.
1483 if (HeaderBarShouldDrawContainer(widgetType)) {
1484 auto containerType = widgetType == MOZ_GTK_HEADER_BAR
1485 ? MOZ_GTK_HEADERBAR_FIXED
1486 : MOZ_GTK_HEADERBAR_FIXED_MAXIMIZED;
1487 style = GetStyleContext(containerType, state->image_scale,
1488 GTK_TEXT_DIR_NONE, state_flags);
1491 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1492 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1494 return MOZ_GTK_SUCCESS;
1497 gint moz_gtk_get_widget_border(WidgetNodeType widget, gint* left, gint* top,
1498 gint* right, gint* bottom,
1499 // NOTE: callers depend on direction being used
1500 // only for MOZ_GTK_DROPDOWN widgets.
1501 GtkTextDirection direction) {
1502 GtkWidget* w;
1503 GtkStyleContext* style;
1504 *left = *top = *right = *bottom = 0;
1506 switch (widget) {
1507 case MOZ_GTK_BUTTON:
1508 case MOZ_GTK_TOOLBAR_BUTTON: {
1509 style = GetStyleContext(MOZ_GTK_BUTTON);
1511 *left = *top = *right = *bottom = gtk_container_get_border_width(
1512 GTK_CONTAINER(GetWidget(MOZ_GTK_BUTTON)));
1514 if (widget == MOZ_GTK_TOOLBAR_BUTTON) {
1515 gtk_style_context_save(style);
1516 gtk_style_context_add_class(style, "image-button");
1519 moz_gtk_add_style_padding(style, left, top, right, bottom);
1521 if (widget == MOZ_GTK_TOOLBAR_BUTTON) gtk_style_context_restore(style);
1523 moz_gtk_add_style_border(style, left, top, right, bottom);
1525 return MOZ_GTK_SUCCESS;
1527 case MOZ_GTK_ENTRY:
1528 case MOZ_GTK_DROPDOWN_ENTRY: {
1529 style = GetStyleContext(widget);
1531 // XXX: Subtract 1 pixel from the padding to account for the default
1532 // padding in forms.css. See bug 1187385.
1533 *left = *top = *right = *bottom = -1;
1534 moz_gtk_add_border_padding(style, left, top, right, bottom);
1536 return MOZ_GTK_SUCCESS;
1538 case MOZ_GTK_TEXT_VIEW:
1539 case MOZ_GTK_TREEVIEW: {
1540 style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW);
1541 moz_gtk_add_style_border(style, left, top, right, bottom);
1542 return MOZ_GTK_SUCCESS;
1544 case MOZ_GTK_DROPDOWN: {
1545 /* We need to account for the arrow on the dropdown, so text
1546 * doesn't come too close to the arrow, or in some cases spill
1547 * into the arrow. */
1548 gboolean wide_separators;
1549 gint separator_width;
1550 GtkRequisition arrow_req;
1551 GtkBorder border;
1553 *left = *top = *right = *bottom = gtk_container_get_border_width(
1554 GTK_CONTAINER(GetWidget(MOZ_GTK_COMBOBOX_BUTTON)));
1555 style = GetStyleContext(MOZ_GTK_COMBOBOX_BUTTON);
1556 moz_gtk_add_border_padding(style, left, top, right, bottom);
1558 /* If there is no separator, don't try to count its width. */
1559 separator_width = 0;
1560 GtkWidget* comboBoxSeparator = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR);
1561 if (comboBoxSeparator) {
1562 style = gtk_widget_get_style_context(comboBoxSeparator);
1563 gtk_style_context_get_style(style, "wide-separators", &wide_separators,
1564 "separator-width", &separator_width, NULL);
1566 if (!wide_separators) {
1567 gtk_style_context_get_border(
1568 style, gtk_style_context_get_state(style), &border);
1569 separator_width = border.left;
1573 gtk_widget_get_preferred_size(GetWidget(MOZ_GTK_COMBOBOX_ARROW), NULL,
1574 &arrow_req);
1575 moz_gtk_sanity_preferred_size(&arrow_req);
1577 if (direction == GTK_TEXT_DIR_RTL)
1578 *left += separator_width + arrow_req.width;
1579 else
1580 *right += separator_width + arrow_req.width;
1582 return MOZ_GTK_SUCCESS;
1584 case MOZ_GTK_TABPANELS:
1585 w = GetWidget(MOZ_GTK_TABPANELS);
1586 break;
1587 case MOZ_GTK_PROGRESSBAR:
1588 w = GetWidget(MOZ_GTK_PROGRESSBAR);
1589 break;
1590 case MOZ_GTK_SPINBUTTON_ENTRY:
1591 case MOZ_GTK_SPINBUTTON_UP:
1592 case MOZ_GTK_SPINBUTTON_DOWN:
1593 w = GetWidget(MOZ_GTK_SPINBUTTON);
1594 break;
1595 case MOZ_GTK_SCALE_HORIZONTAL:
1596 case MOZ_GTK_SCALE_VERTICAL:
1597 w = GetWidget(widget);
1598 break;
1599 case MOZ_GTK_FRAME:
1600 w = GetWidget(MOZ_GTK_FRAME);
1601 break;
1602 case MOZ_GTK_TOOLTIP: {
1603 // In GTK 3 there are 6 pixels of additional margin around the box.
1604 // See details there:
1605 // https://github.com/GNOME/gtk/blob/5ea69a136bd7e4970b3a800390e20314665aaed2/gtk/ui/gtktooltipwindow.ui#L11
1606 *left = *right = *top = *bottom = 6;
1608 // We also need to add margin/padding/borders from Tooltip content.
1609 // Tooltip contains horizontal box, where icon and label is put.
1610 // We ignore icon as long as we don't have support for it.
1611 GtkStyleContext* boxStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX);
1612 moz_gtk_add_margin_border_padding(boxStyle, left, top, right, bottom);
1614 GtkStyleContext* labelStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX_LABEL);
1615 moz_gtk_add_margin_border_padding(labelStyle, left, top, right, bottom);
1617 return MOZ_GTK_SUCCESS;
1619 /* These widgets have no borders, since they are not containers. */
1620 case MOZ_GTK_SPLITTER_HORIZONTAL:
1621 case MOZ_GTK_SPLITTER_VERTICAL:
1622 case MOZ_GTK_CHECKBUTTON:
1623 case MOZ_GTK_RADIOBUTTON:
1624 case MOZ_GTK_SCALE_THUMB_HORIZONTAL:
1625 case MOZ_GTK_SCALE_THUMB_VERTICAL:
1626 case MOZ_GTK_PROGRESS_CHUNK:
1627 case MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE:
1628 case MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE:
1629 case MOZ_GTK_HEADER_BAR:
1630 case MOZ_GTK_HEADER_BAR_MAXIMIZED:
1631 case MOZ_GTK_HEADER_BAR_BUTTON_CLOSE:
1632 case MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE:
1633 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE:
1634 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE:
1635 /* These widgets have no borders.*/
1636 case MOZ_GTK_INNER_SPIN_BUTTON:
1637 case MOZ_GTK_SPINBUTTON:
1638 case MOZ_GTK_WINDOW_DECORATION:
1639 case MOZ_GTK_WINDOW_DECORATION_SOLID:
1640 case MOZ_GTK_RESIZER:
1641 case MOZ_GTK_TOOLBARBUTTON_ARROW:
1642 case MOZ_GTK_TAB_SCROLLARROW:
1643 return MOZ_GTK_SUCCESS;
1644 default:
1645 g_warning("Unsupported widget type: %d", widget);
1646 return MOZ_GTK_UNKNOWN_WIDGET;
1648 /* TODO - we're still missing some widget implementations */
1649 if (w) {
1650 moz_gtk_add_style_border(gtk_widget_get_style_context(w), left, top, right,
1651 bottom);
1653 return MOZ_GTK_SUCCESS;
1656 gint moz_gtk_get_tab_border(gint* left, gint* top, gint* right, gint* bottom,
1657 GtkTextDirection direction, GtkTabFlags flags,
1658 WidgetNodeType widget) {
1659 GtkStyleContext* style = GetStyleContext(widget, 1, direction,
1660 GetStateFlagsFromGtkTabFlags(flags));
1662 *left = *top = *right = *bottom = 0;
1663 moz_gtk_add_style_padding(style, left, top, right, bottom);
1665 // Gtk >= 3.20 does not use those styles
1666 if (gtk_check_version(3, 20, 0) != nullptr) {
1667 int tab_curvature;
1669 gtk_style_context_get_style(style, "tab-curvature", &tab_curvature, NULL);
1670 *left += tab_curvature;
1671 *right += tab_curvature;
1673 if (flags & MOZ_GTK_TAB_FIRST) {
1674 int initial_gap = 0;
1675 gtk_style_context_get_style(style, "initial-gap", &initial_gap, NULL);
1676 if (direction == GTK_TEXT_DIR_RTL)
1677 *right += initial_gap;
1678 else
1679 *left += initial_gap;
1681 } else {
1682 GtkBorder margin;
1684 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
1685 &margin);
1686 *left += margin.left;
1687 *right += margin.right;
1689 if (flags & MOZ_GTK_TAB_FIRST) {
1690 style = GetStyleContext(MOZ_GTK_NOTEBOOK_HEADER, direction);
1691 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
1692 &margin);
1693 *left += margin.left;
1694 *right += margin.right;
1698 return MOZ_GTK_SUCCESS;
1701 gint moz_gtk_get_tab_scroll_arrow_size(gint* width, gint* height) {
1702 gint arrow_size;
1704 GtkStyleContext* style = GetStyleContext(MOZ_GTK_TABPANELS);
1705 gtk_style_context_get_style(style, "scroll-arrow-hlength", &arrow_size, NULL);
1707 *height = *width = arrow_size;
1709 return MOZ_GTK_SUCCESS;
1712 void moz_gtk_get_arrow_size(WidgetNodeType widgetType, gint* width,
1713 gint* height) {
1714 GtkWidget* widget;
1715 switch (widgetType) {
1716 case MOZ_GTK_DROPDOWN:
1717 widget = GetWidget(MOZ_GTK_COMBOBOX_ARROW);
1718 break;
1719 default:
1720 widget = GetWidget(MOZ_GTK_BUTTON_ARROW);
1721 break;
1723 if (widget) {
1724 GtkRequisition requisition;
1725 gtk_widget_get_preferred_size(widget, NULL, &requisition);
1726 moz_gtk_sanity_preferred_size(&requisition);
1728 *width = requisition.width;
1729 *height = requisition.height;
1730 } else {
1731 *width = 0;
1732 *height = 0;
1736 void moz_gtk_get_entry_min_height(gint* min_content_height,
1737 gint* border_padding_height) {
1738 GtkStyleContext* style = GetStyleContext(MOZ_GTK_ENTRY);
1739 if (!gtk_check_version(3, 20, 0)) {
1740 gtk_style_context_get(style, gtk_style_context_get_state(style),
1741 "min-height", min_content_height, nullptr);
1742 } else {
1743 *min_content_height = 0;
1746 GtkBorder border;
1747 GtkBorder padding;
1748 gtk_style_context_get_border(style, gtk_style_context_get_state(style),
1749 &border);
1750 gtk_style_context_get_padding(style, gtk_style_context_get_state(style),
1751 &padding);
1753 *border_padding_height =
1754 (border.top + border.bottom + padding.top + padding.bottom);
1757 void moz_gtk_get_scale_metrics(GtkOrientation orient, gint* scale_width,
1758 gint* scale_height) {
1759 if (gtk_check_version(3, 20, 0) != nullptr) {
1760 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1761 ? MOZ_GTK_SCALE_HORIZONTAL
1762 : MOZ_GTK_SCALE_VERTICAL;
1764 gint thumb_length, thumb_height, trough_border;
1765 moz_gtk_get_scalethumb_metrics(orient, &thumb_length, &thumb_height);
1767 GtkStyleContext* style = GetStyleContext(widget);
1768 gtk_style_context_get_style(style, "trough-border", &trough_border, NULL);
1770 if (orient == GTK_ORIENTATION_HORIZONTAL) {
1771 *scale_width = thumb_length + trough_border * 2;
1772 *scale_height = thumb_height + trough_border * 2;
1773 } else {
1774 *scale_width = thumb_height + trough_border * 2;
1775 *scale_height = thumb_length + trough_border * 2;
1777 } else {
1778 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1779 ? MOZ_GTK_SCALE_TROUGH_HORIZONTAL
1780 : MOZ_GTK_SCALE_TROUGH_VERTICAL;
1781 moz_gtk_get_widget_min_size(GetStyleContext(widget), scale_width,
1782 scale_height);
1786 gint moz_gtk_get_scalethumb_metrics(GtkOrientation orient, gint* thumb_length,
1787 gint* thumb_height) {
1788 if (gtk_check_version(3, 20, 0) != nullptr) {
1789 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1790 ? MOZ_GTK_SCALE_HORIZONTAL
1791 : MOZ_GTK_SCALE_VERTICAL;
1792 GtkStyleContext* style = GetStyleContext(widget);
1793 gtk_style_context_get_style(style, "slider_length", thumb_length,
1794 "slider_width", thumb_height, NULL);
1795 } else {
1796 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1797 ? MOZ_GTK_SCALE_THUMB_HORIZONTAL
1798 : MOZ_GTK_SCALE_THUMB_VERTICAL;
1799 GtkStyleContext* style = GetStyleContext(widget);
1801 gint min_width, min_height;
1802 GtkStateFlags state = gtk_style_context_get_state(style);
1803 gtk_style_context_get(style, state, "min-width", &min_width, "min-height",
1804 &min_height, nullptr);
1805 GtkBorder margin;
1806 gtk_style_context_get_margin(style, state, &margin);
1807 gint margin_width = margin.left + margin.right;
1808 gint margin_height = margin.top + margin.bottom;
1810 // Negative margin of slider element also determines its minimal size
1811 // so use bigger of those two values.
1812 if (min_width < -margin_width) min_width = -margin_width;
1813 if (min_height < -margin_height) min_height = -margin_height;
1815 *thumb_length = min_width;
1816 *thumb_height = min_height;
1819 return MOZ_GTK_SUCCESS;
1822 const ToggleGTKMetrics* GetToggleMetrics(WidgetNodeType aWidgetType) {
1823 ToggleGTKMetrics* metrics;
1825 switch (aWidgetType) {
1826 case MOZ_GTK_RADIOBUTTON:
1827 metrics = &sRadioMetrics;
1828 break;
1829 case MOZ_GTK_CHECKBUTTON:
1830 metrics = &sCheckboxMetrics;
1831 break;
1832 default:
1833 MOZ_CRASH("Unsupported widget type for getting metrics");
1834 return nullptr;
1837 metrics->initialized = true;
1838 if (gtk_check_version(3, 20, 0) == nullptr) {
1839 GtkStyleContext* style = GetStyleContext(aWidgetType);
1840 GtkStateFlags state_flags = gtk_style_context_get_state(style);
1841 gtk_style_context_get(style, state_flags, "min-height",
1842 &(metrics->minSizeWithBorder.height), "min-width",
1843 &(metrics->minSizeWithBorder.width), nullptr);
1844 // Fallback to indicator size if min dimensions are zero
1845 if (metrics->minSizeWithBorder.height == 0 ||
1846 metrics->minSizeWithBorder.width == 0) {
1847 gint indicator_size = 0;
1848 gtk_widget_style_get(GetWidget(MOZ_GTK_CHECKBUTTON_CONTAINER),
1849 "indicator_size", &indicator_size, nullptr);
1850 if (metrics->minSizeWithBorder.height == 0) {
1851 metrics->minSizeWithBorder.height = indicator_size;
1853 if (metrics->minSizeWithBorder.width == 0) {
1854 metrics->minSizeWithBorder.width = indicator_size;
1858 GtkBorder border, padding;
1859 gtk_style_context_get_border(style, state_flags, &border);
1860 gtk_style_context_get_padding(style, state_flags, &padding);
1861 metrics->borderAndPadding.left = border.left + padding.left;
1862 metrics->borderAndPadding.right = border.right + padding.right;
1863 metrics->borderAndPadding.top = border.top + padding.top;
1864 metrics->borderAndPadding.bottom = border.bottom + padding.bottom;
1865 metrics->minSizeWithBorder.width +=
1866 metrics->borderAndPadding.left + metrics->borderAndPadding.right;
1867 metrics->minSizeWithBorder.height +=
1868 metrics->borderAndPadding.top + metrics->borderAndPadding.bottom;
1869 } else {
1870 gint indicator_size = 0, indicator_spacing = 0;
1871 gtk_widget_style_get(GetWidget(MOZ_GTK_CHECKBUTTON_CONTAINER),
1872 "indicator_size", &indicator_size, "indicator_spacing",
1873 &indicator_spacing, nullptr);
1874 metrics->minSizeWithBorder.width = metrics->minSizeWithBorder.height =
1875 indicator_size;
1877 return metrics;
1881 * get_shadow_width() from gtkwindow.c is not public so we need
1882 * to implement it.
1884 void InitWindowDecorationSize(CSDWindowDecorationSize* sWindowDecorationSize,
1885 bool aPopupWindow) {
1886 bool solidDecorations = gtk_style_context_has_class(
1887 GetStyleContext(MOZ_GTK_HEADERBAR_WINDOW, 1), "solid-csd");
1888 // solid-csd does not use frame extents, quit now.
1889 if (solidDecorations) {
1890 sWindowDecorationSize->decorationSize = {0, 0, 0, 0};
1891 return;
1894 // Scale factor is applied later when decoration size is used for actual
1895 // gtk windows.
1896 GtkStyleContext* context = GetStyleContext(MOZ_GTK_WINDOW_DECORATION);
1898 /* Always sum border + padding */
1899 GtkBorder padding;
1900 GtkStateFlags state = gtk_style_context_get_state(context);
1901 gtk_style_context_get_border(context, state,
1902 &sWindowDecorationSize->decorationSize);
1903 gtk_style_context_get_padding(context, state, &padding);
1904 sWindowDecorationSize->decorationSize += padding;
1906 // Available on GTK 3.20+.
1907 static auto sGtkRenderBackgroundGetClip = (void (*)(
1908 GtkStyleContext*, gdouble, gdouble, gdouble, gdouble,
1909 GdkRectangle*))dlsym(RTLD_DEFAULT, "gtk_render_background_get_clip");
1911 if (!sGtkRenderBackgroundGetClip) {
1912 return;
1915 GdkRectangle clip;
1916 sGtkRenderBackgroundGetClip(context, 0, 0, 0, 0, &clip);
1918 GtkBorder extents;
1919 extents.top = -clip.y;
1920 extents.right = clip.width + clip.x;
1921 extents.bottom = clip.height + clip.y;
1922 extents.left = -clip.x;
1924 // Get shadow extents but combine with style margin; use the bigger value.
1925 // Margin is used for resize grip size - it's not present on
1926 // popup windows.
1927 if (!aPopupWindow) {
1928 GtkBorder margin;
1929 gtk_style_context_get_margin(context, state, &margin);
1931 extents.top = MAX(extents.top, margin.top);
1932 extents.right = MAX(extents.right, margin.right);
1933 extents.bottom = MAX(extents.bottom, margin.bottom);
1934 extents.left = MAX(extents.left, margin.left);
1937 sWindowDecorationSize->decorationSize += extents;
1940 GtkBorder GetCSDDecorationSize(bool aIsPopup) {
1941 auto metrics =
1942 aIsPopup ? &sPopupWindowDecorationSize : &sToplevelWindowDecorationSize;
1943 if (!metrics->initialized) {
1944 InitWindowDecorationSize(metrics, aIsPopup);
1945 metrics->initialized = true;
1947 return metrics->decorationSize;
1950 /* cairo_t *cr argument has to be a system-cairo. */
1951 gint moz_gtk_widget_paint(WidgetNodeType widget, cairo_t* cr,
1952 GdkRectangle* rect, GtkWidgetState* state, gint flags,
1953 GtkTextDirection direction) {
1954 /* A workaround for https://bugzilla.gnome.org/show_bug.cgi?id=694086
1956 cairo_new_path(cr);
1958 switch (widget) {
1959 case MOZ_GTK_BUTTON:
1960 case MOZ_GTK_TOOLBAR_BUTTON:
1961 if (state->depressed) {
1962 return moz_gtk_button_paint(cr, rect, state, (GtkReliefStyle)flags,
1963 GetWidget(MOZ_GTK_TOGGLE_BUTTON),
1964 direction);
1966 return moz_gtk_button_paint(cr, rect, state, (GtkReliefStyle)flags,
1967 GetWidget(MOZ_GTK_BUTTON), direction);
1968 case MOZ_GTK_HEADER_BAR_BUTTON_CLOSE:
1969 case MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE:
1970 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE:
1971 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE:
1972 return moz_gtk_header_bar_button_paint(
1973 cr, rect, state, (GtkReliefStyle)flags, widget, direction);
1974 case MOZ_GTK_CHECKBUTTON:
1975 case MOZ_GTK_RADIOBUTTON:
1976 return moz_gtk_toggle_paint(cr, rect, state,
1977 !!(flags & MOZ_GTK_WIDGET_CHECKED),
1978 !!(flags & MOZ_GTK_WIDGET_INCONSISTENT),
1979 (widget == MOZ_GTK_RADIOBUTTON), direction);
1980 case MOZ_GTK_SCALE_HORIZONTAL:
1981 case MOZ_GTK_SCALE_VERTICAL:
1982 return moz_gtk_scale_paint(cr, rect, state, (GtkOrientation)flags,
1983 direction);
1984 case MOZ_GTK_SCALE_THUMB_HORIZONTAL:
1985 case MOZ_GTK_SCALE_THUMB_VERTICAL:
1986 return moz_gtk_scale_thumb_paint(cr, rect, state, (GtkOrientation)flags,
1987 direction);
1988 case MOZ_GTK_INNER_SPIN_BUTTON:
1989 return moz_gtk_inner_spin_paint(cr, rect, state, direction);
1990 case MOZ_GTK_SPINBUTTON:
1991 return moz_gtk_spin_paint(cr, rect, state, direction);
1992 case MOZ_GTK_SPINBUTTON_UP:
1993 case MOZ_GTK_SPINBUTTON_DOWN:
1994 return moz_gtk_spin_updown_paint(
1995 cr, rect, (widget == MOZ_GTK_SPINBUTTON_DOWN), state, direction);
1996 case MOZ_GTK_SPINBUTTON_ENTRY: {
1997 GtkStyleContext* style =
1998 GetStyleContext(MOZ_GTK_SPINBUTTON_ENTRY, state->image_scale,
1999 direction, GetStateFlagsFromGtkWidgetState(state));
2000 return moz_gtk_entry_paint(cr, rect, state, style, widget);
2002 case MOZ_GTK_TREEVIEW:
2003 return moz_gtk_treeview_paint(cr, rect, state, direction);
2004 case MOZ_GTK_ENTRY:
2005 case MOZ_GTK_DROPDOWN_ENTRY: {
2006 GtkStyleContext* style =
2007 GetStyleContext(widget, state->image_scale, direction,
2008 GetStateFlagsFromGtkWidgetState(state));
2009 gint ret = moz_gtk_entry_paint(cr, rect, state, style, widget);
2010 return ret;
2012 case MOZ_GTK_TEXT_VIEW:
2013 return moz_gtk_text_view_paint(cr, rect, state, direction);
2014 case MOZ_GTK_DROPDOWN:
2015 return moz_gtk_combo_box_paint(cr, rect, state, direction);
2016 case MOZ_GTK_TOOLTIP:
2017 return moz_gtk_tooltip_paint(cr, rect, state, direction);
2018 case MOZ_GTK_FRAME:
2019 return moz_gtk_frame_paint(cr, rect, state, direction);
2020 case MOZ_GTK_RESIZER:
2021 return moz_gtk_resizer_paint(cr, rect, state, direction);
2022 case MOZ_GTK_PROGRESSBAR:
2023 return moz_gtk_progressbar_paint(cr, rect, state, direction);
2024 case MOZ_GTK_PROGRESS_CHUNK:
2025 case MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE:
2026 case MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE:
2027 return moz_gtk_progress_chunk_paint(cr, rect, state, direction, widget);
2028 case MOZ_GTK_TAB_TOP:
2029 case MOZ_GTK_TAB_BOTTOM:
2030 return moz_gtk_tab_paint(cr, rect, state, (GtkTabFlags)flags, direction,
2031 widget);
2032 case MOZ_GTK_TABPANELS:
2033 return moz_gtk_tabpanels_paint(cr, rect, state, direction);
2034 case MOZ_GTK_TAB_SCROLLARROW:
2035 return moz_gtk_tab_scroll_arrow_paint(cr, rect, state,
2036 (GtkArrowType)flags, direction);
2037 case MOZ_GTK_TOOLBARBUTTON_ARROW:
2038 return moz_gtk_arrow_paint(cr, rect, state, (GtkArrowType)flags,
2039 direction);
2040 case MOZ_GTK_SPLITTER_HORIZONTAL:
2041 return moz_gtk_vpaned_paint(cr, rect, state);
2042 case MOZ_GTK_SPLITTER_VERTICAL:
2043 return moz_gtk_hpaned_paint(cr, rect, state);
2044 case MOZ_GTK_WINDOW_DECORATION:
2045 return moz_gtk_window_decoration_paint(cr, rect, state, direction);
2046 case MOZ_GTK_HEADER_BAR:
2047 case MOZ_GTK_HEADER_BAR_MAXIMIZED:
2048 return moz_gtk_header_bar_paint(widget, cr, rect, state);
2049 default:
2050 g_warning("Unknown widget type: %d", widget);
2053 return MOZ_GTK_UNKNOWN_WIDGET;
2056 gint moz_gtk_shutdown() {
2057 /* This will destroy all of our widgets */
2058 ResetWidgetCache();
2060 return MOZ_GTK_SUCCESS;