Bug 1869647 - Mark hasStorageAccess.sub.https.window.html as intermittent after wpt...
[gecko.git] / widget / gtk / gtk3drawing.cpp
blob1fa8b9560694be36be413f3d097ed327fa6457f9
1 /* -*- Mode: C++; tab-width: 4; 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 iconWidth, iconHeight;
265 if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &iconWidth, &iconHeight)) {
266 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;
296 aMetrics->minSizeWithBorderMargin.width = width;
297 aMetrics->minSizeWithBorderMargin.height = height;
300 // We support LTR layout only here for now.
301 static void CalculateToolbarButtonSpacing(WidgetNodeType aAppearance,
302 ToolbarButtonGTKMetrics* aMetrics) {
303 GtkStyleContext* style = GetStyleContext(aAppearance);
304 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
305 &aMetrics->buttonMargin);
307 // Get titlebar spacing, a default one is 6 pixels (gtk/gtkheaderbar.c)
308 gint buttonSpacing = 6;
309 g_object_get(GetWidget(MOZ_GTK_HEADER_BAR), "spacing", &buttonSpacing,
310 nullptr);
312 // We apply spacing as a margin equally to both adjacent buttons.
313 buttonSpacing /= 2;
315 if (!aMetrics->firstButton) {
316 aMetrics->buttonMargin.left += buttonSpacing;
318 if (!aMetrics->lastButton) {
319 aMetrics->buttonMargin.right += buttonSpacing;
322 aMetrics->minSizeWithBorderMargin.width +=
323 aMetrics->buttonMargin.right + aMetrics->buttonMargin.left;
324 aMetrics->minSizeWithBorderMargin.height +=
325 aMetrics->buttonMargin.top + aMetrics->buttonMargin.bottom;
328 size_t GetGtkHeaderBarButtonLayout(Span<ButtonLayout> aButtonLayout,
329 bool* aReversedButtonsPlacement) {
330 gchar* decorationLayoutSetting = nullptr;
331 GtkSettings* settings = gtk_settings_get_default();
332 g_object_get(settings, "gtk-decoration-layout", &decorationLayoutSetting,
333 nullptr);
334 auto free = mozilla::MakeScopeExit([&] { g_free(decorationLayoutSetting); });
336 // Use a default layout
337 const gchar* decorationLayout = "menu:minimize,maximize,close";
338 if (decorationLayoutSetting) {
339 decorationLayout = decorationLayoutSetting;
342 // "minimize,maximize,close:" layout means buttons are on the opposite
343 // titlebar side. close button is always there.
344 if (aReversedButtonsPlacement) {
345 const char* closeButton = strstr(decorationLayout, "close");
346 const char* separator = strchr(decorationLayout, ':');
347 *aReversedButtonsPlacement =
348 closeButton && separator && closeButton < separator;
351 // We check what position a button string is stored in decorationLayout.
353 // decorationLayout gets its value from the GNOME preference:
354 // org.gnome.desktop.vm.preferences.button-layout via the
355 // gtk-decoration-layout property.
357 // Documentation of the gtk-decoration-layout property can be found here:
358 // https://developer.gnome.org/gtk3/stable/GtkSettings.html#GtkSettings--gtk-decoration-layout
359 if (aButtonLayout.IsEmpty()) {
360 return 0;
363 nsDependentCSubstring layout(decorationLayout, strlen(decorationLayout));
365 size_t activeButtons = 0;
366 for (const auto& part : layout.Split(':')) {
367 for (const auto& button : part.Split(',')) {
368 if (button.EqualsLiteral("close")) {
369 aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_CLOSE};
370 } else if (button.EqualsLiteral("minimize")) {
371 aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE};
372 } else if (button.EqualsLiteral("maximize")) {
373 aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE};
375 if (activeButtons == aButtonLayout.Length()) {
376 return activeButtons;
380 return activeButtons;
383 static void EnsureToolbarMetrics() {
384 if (sToolbarMetrics.initialized) {
385 return;
387 // Make sure we have clean cache after theme reset, etc.
388 memset(&sToolbarMetrics, 0, sizeof(sToolbarMetrics));
390 // Calculate titlebar button visibility and positions.
391 ButtonLayout aButtonLayout[TOOLBAR_BUTTONS];
392 size_t activeButtonNums =
393 GetGtkHeaderBarButtonLayout(Span(aButtonLayout), nullptr);
395 for (size_t i = 0; i < activeButtonNums; i++) {
396 int buttonIndex =
397 (aButtonLayout[i].mType - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE);
398 ToolbarButtonGTKMetrics* metrics = sToolbarMetrics.button + buttonIndex;
399 metrics->visible = true;
400 // Mark first button
401 if (!i) {
402 metrics->firstButton = true;
404 // Mark last button.
405 if (i == (activeButtonNums - 1)) {
406 metrics->lastButton = true;
409 CalculateToolbarButtonMetrics(aButtonLayout[i].mType, metrics);
410 CalculateToolbarButtonSpacing(aButtonLayout[i].mType, metrics);
413 sToolbarMetrics.initialized = true;
416 const ToolbarButtonGTKMetrics* GetToolbarButtonMetrics(
417 WidgetNodeType aAppearance) {
418 EnsureToolbarMetrics();
420 int buttonIndex = (aAppearance - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE);
421 NS_ASSERTION(buttonIndex >= 0 && buttonIndex <= TOOLBAR_BUTTONS,
422 "GetToolbarButtonMetrics(): Wrong titlebar button!");
423 return sToolbarMetrics.button + buttonIndex;
426 static gint moz_gtk_window_decoration_paint(cairo_t* cr,
427 const GdkRectangle* rect,
428 GtkWidgetState* state,
429 GtkTextDirection direction) {
430 if (mozilla::widget::GdkIsWaylandDisplay()) {
431 // Doesn't seem to be needed.
432 return MOZ_GTK_SUCCESS;
434 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
435 GtkStyleContext* windowStyle =
436 GetStyleContext(MOZ_GTK_HEADERBAR_WINDOW, state->image_scale);
437 const bool solidDecorations =
438 gtk_style_context_has_class(windowStyle, "solid-csd");
439 GtkStyleContext* decorationStyle =
440 GetStyleContext(solidDecorations ? MOZ_GTK_WINDOW_DECORATION_SOLID
441 : MOZ_GTK_WINDOW_DECORATION,
442 state->image_scale, GTK_TEXT_DIR_LTR, state_flags);
444 gtk_render_background(decorationStyle, cr, rect->x, rect->y, rect->width,
445 rect->height);
446 gtk_render_frame(decorationStyle, cr, rect->x, rect->y, rect->width,
447 rect->height);
448 return MOZ_GTK_SUCCESS;
451 static gint moz_gtk_button_paint(cairo_t* cr, const GdkRectangle* rect,
452 GtkWidgetState* state, GtkReliefStyle relief,
453 GtkWidget* widget,
454 GtkTextDirection direction) {
455 if (!widget) {
456 return MOZ_GTK_UNKNOWN_WIDGET;
459 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
460 GtkStyleContext* style = gtk_widget_get_style_context(widget);
461 gint x = rect->x, y = rect->y, width = rect->width, height = rect->height;
463 gtk_widget_set_direction(widget, direction);
465 gtk_style_context_save(style);
466 StyleContextSetScale(style, state->image_scale);
467 gtk_style_context_set_state(style, state_flags);
469 if (state->isDefault && relief == GTK_RELIEF_NORMAL && !state->focused &&
470 !(state_flags & GTK_STATE_FLAG_PRELIGHT)) {
471 /* handle default borders both outside and inside the button */
472 gint default_top, default_left, default_bottom, default_right;
473 moz_gtk_button_get_default_overflow(&default_top, &default_left,
474 &default_bottom, &default_right);
475 x -= default_left;
476 y -= default_top;
477 width += default_left + default_right;
478 height += default_top + default_bottom;
479 gtk_render_background(style, cr, x, y, width, height);
480 gtk_render_frame(style, cr, x, y, width, height);
481 moz_gtk_button_get_default_border(&default_top, &default_left,
482 &default_bottom, &default_right);
483 x += default_left;
484 y += default_top;
485 width -= (default_left + default_right);
486 height -= (default_top + default_bottom);
487 } else if (relief != GTK_RELIEF_NONE || state->depressed ||
488 (state_flags & GTK_STATE_FLAG_PRELIGHT)) {
489 /* the following line can trigger an assertion (Crux theme)
490 file ../../gdk/gdkwindow.c: line 1846 (gdk_window_clear_area):
491 assertion `GDK_IS_WINDOW (window)' failed */
492 gtk_render_background(style, cr, x, y, width, height);
493 gtk_render_frame(style, cr, x, y, width, height);
496 if (state->focused) {
497 GtkBorder border;
498 gtk_style_context_get_border(style, state_flags, &border);
499 x += border.left;
500 y += border.top;
501 width -= (border.left + border.right);
502 height -= (border.top + border.bottom);
503 gtk_render_focus(style, cr, x, y, width, height);
505 gtk_style_context_restore(style);
506 return MOZ_GTK_SUCCESS;
509 static gint moz_gtk_header_bar_button_paint(cairo_t* cr,
510 const GdkRectangle* aRect,
511 GtkWidgetState* state,
512 GtkReliefStyle relief,
513 WidgetNodeType aIconWidgetType,
514 GtkTextDirection direction) {
515 GdkRectangle rect = *aRect;
516 // We need to inset our calculated margin because it also
517 // contains titlebar button spacing.
518 const ToolbarButtonGTKMetrics* metrics = GetToolbarButtonMetrics(
519 aIconWidgetType == MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE
520 ? MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE
521 : aIconWidgetType);
522 Inset(&rect, metrics->buttonMargin);
524 GtkWidget* buttonWidget = GetWidget(aIconWidgetType);
525 if (!buttonWidget) {
526 return MOZ_GTK_UNKNOWN_WIDGET;
528 moz_gtk_button_paint(cr, &rect, state, relief, buttonWidget, direction);
530 GtkWidget* iconWidget =
531 gtk_bin_get_child(GTK_BIN(GetWidget(aIconWidgetType)));
532 if (!iconWidget) {
533 return MOZ_GTK_UNKNOWN_WIDGET;
535 cairo_surface_t* surface =
536 GetWidgetIconSurface(iconWidget, state->image_scale);
538 if (surface) {
539 GtkStyleContext* style = gtk_widget_get_style_context(buttonWidget);
540 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
542 gtk_style_context_save(style);
543 StyleContextSetScale(style, state->image_scale);
544 gtk_style_context_set_state(style, state_flags);
546 /* This is available since Gtk+ 3.10 as well as GtkHeaderBar */
547 gtk_render_icon_surface(style, cr, surface, rect.x + metrics->iconXPosition,
548 rect.y + metrics->iconYPosition);
549 gtk_style_context_restore(style);
552 return MOZ_GTK_SUCCESS;
555 static gint moz_gtk_toggle_paint(cairo_t* cr, GdkRectangle* rect,
556 GtkWidgetState* state, gboolean selected,
557 gboolean inconsistent, gboolean isradio,
558 GtkTextDirection direction) {
559 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
560 gint x, y, width, height;
561 GtkStyleContext* style;
563 // We need to call this before GetStyleContext, because otherwise we would
564 // reset state flags
565 const ToggleGTKMetrics* metrics =
566 GetToggleMetrics(isradio ? MOZ_GTK_RADIOBUTTON : MOZ_GTK_CHECKBUTTON);
567 // Clamp the rect and paint it center aligned in the rect.
568 x = rect->x;
569 y = rect->y;
570 width = rect->width;
571 height = rect->height;
573 if (rect->width < rect->height) {
574 y = rect->y + (rect->height - rect->width) / 2;
575 height = rect->width;
578 if (rect->height < rect->width) {
579 x = rect->x + (rect->width - rect->height) / 2;
580 width = rect->height;
583 if (selected)
584 state_flags =
585 static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
587 if (inconsistent)
588 state_flags =
589 static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_INCONSISTENT);
591 style = GetStyleContext(isradio ? MOZ_GTK_RADIOBUTTON : MOZ_GTK_CHECKBUTTON,
592 state->image_scale, direction, state_flags);
594 if (gtk_check_version(3, 20, 0) == nullptr) {
595 gtk_render_background(style, cr, x, y, width, height);
596 gtk_render_frame(style, cr, x, y, width, height);
597 // Indicator is inset by the toggle's padding and border.
598 gint indicator_x = x + metrics->borderAndPadding.left;
599 gint indicator_y = y + metrics->borderAndPadding.top;
600 gint indicator_width = metrics->minSizeWithBorder.width -
601 metrics->borderAndPadding.left -
602 metrics->borderAndPadding.right;
603 gint indicator_height = metrics->minSizeWithBorder.height -
604 metrics->borderAndPadding.top -
605 metrics->borderAndPadding.bottom;
606 if (isradio) {
607 gtk_render_option(style, cr, indicator_x, indicator_y, indicator_width,
608 indicator_height);
609 } else {
610 gtk_render_check(style, cr, indicator_x, indicator_y, indicator_width,
611 indicator_height);
613 } else {
614 if (isradio) {
615 gtk_render_option(style, cr, x, y, width, height);
616 } else {
617 gtk_render_check(style, cr, x, y, width, height);
621 return MOZ_GTK_SUCCESS;
624 static gint calculate_button_inner_rect(GtkWidget* button,
625 const GdkRectangle* rect,
626 GdkRectangle* inner_rect,
627 GtkTextDirection direction) {
628 GtkStyleContext* style;
629 GtkBorder border;
630 GtkBorder padding = {0, 0, 0, 0};
632 style = gtk_widget_get_style_context(button);
634 /* This mirrors gtkbutton's child positioning */
635 gtk_style_context_get_border(style, gtk_style_context_get_state(style),
636 &border);
637 gtk_style_context_get_padding(style, gtk_style_context_get_state(style),
638 &padding);
640 inner_rect->x = rect->x + border.left + padding.left;
641 inner_rect->y = rect->y + padding.top + border.top;
642 inner_rect->width =
643 MAX(1, rect->width - padding.left - padding.right - border.left * 2);
644 inner_rect->height =
645 MAX(1, rect->height - padding.top - padding.bottom - border.top * 2);
647 return MOZ_GTK_SUCCESS;
650 static gint calculate_arrow_rect(GtkWidget* arrow, GdkRectangle* rect,
651 GdkRectangle* arrow_rect,
652 GtkTextDirection direction) {
653 /* defined in gtkarrow.c */
654 gfloat arrow_scaling = 0.7;
655 gfloat xalign, xpad;
656 gint extent;
657 gint mxpad, mypad;
658 gfloat mxalign, myalign;
659 GtkMisc* misc = GTK_MISC(arrow);
661 gtk_style_context_get_style(gtk_widget_get_style_context(arrow),
662 "arrow_scaling", &arrow_scaling, NULL);
664 gtk_misc_get_padding(misc, &mxpad, &mypad);
665 extent = MIN((rect->width - mxpad * 2), (rect->height - mypad * 2)) *
666 arrow_scaling;
668 gtk_misc_get_alignment(misc, &mxalign, &myalign);
670 xalign = direction == GTK_TEXT_DIR_LTR ? mxalign : 1.0 - mxalign;
671 xpad = mxpad + (rect->width - extent) * xalign;
673 arrow_rect->x = direction == GTK_TEXT_DIR_LTR ? floor(rect->x + xpad)
674 : ceil(rect->x + xpad);
675 arrow_rect->y = floor(rect->y + mypad + ((rect->height - extent) * myalign));
677 arrow_rect->width = arrow_rect->height = extent;
679 return MOZ_GTK_SUCCESS;
683 * Get minimum widget size as sum of margin, padding, border and
684 * min-width/min-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);
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);
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;
703 static void Inset(GdkRectangle* rect, const GtkBorder& aBorder) {
704 rect->x += aBorder.left;
705 rect->y += aBorder.top;
706 rect->width -= aBorder.left + aBorder.right;
707 rect->height -= aBorder.top + aBorder.bottom;
710 // Inset a rectangle by the margins specified in a style context.
711 static void InsetByMargin(GdkRectangle* rect, GtkStyleContext* style) {
712 GtkBorder margin;
713 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
714 &margin);
715 Inset(rect, margin);
718 // Inset a rectangle by the border and padding specified in a style context.
719 static void InsetByBorderPadding(GdkRectangle* rect, GtkStyleContext* style) {
720 GtkStateFlags state = gtk_style_context_get_state(style);
721 GtkBorder padding, border;
723 gtk_style_context_get_padding(style, state, &padding);
724 Inset(rect, padding);
725 gtk_style_context_get_border(style, state, &border);
726 Inset(rect, border);
729 static void moz_gtk_draw_styled_frame(GtkStyleContext* style, cairo_t* cr,
730 const GdkRectangle* aRect,
731 bool drawFocus) {
732 GdkRectangle rect = *aRect;
734 InsetByMargin(&rect, style);
736 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
737 gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
738 if (drawFocus) {
739 gtk_render_focus(style, cr, rect.x, rect.y, rect.width, rect.height);
743 static gint moz_gtk_inner_spin_paint(cairo_t* cr, GdkRectangle* rect,
744 GtkWidgetState* state,
745 GtkTextDirection direction) {
746 GtkStyleContext* style =
747 GetStyleContext(MOZ_GTK_SPINBUTTON, state->image_scale, direction,
748 GetStateFlagsFromGtkWidgetState(state));
750 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
751 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
753 /* hard code these values */
754 GdkRectangle arrow_rect;
755 arrow_rect.width = 6;
756 arrow_rect.height = 6;
758 // align spin to the left
759 arrow_rect.x = rect->x;
761 // up button
762 arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 - 3;
763 gtk_render_arrow(style, cr, ARROW_UP, arrow_rect.x, arrow_rect.y,
764 arrow_rect.width);
766 // down button
767 arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 + 3;
768 gtk_render_arrow(style, cr, ARROW_DOWN, arrow_rect.x, arrow_rect.y,
769 arrow_rect.width);
771 return MOZ_GTK_SUCCESS;
774 static gint moz_gtk_spin_paint(cairo_t* cr, GdkRectangle* rect,
775 GtkWidgetState* state,
776 GtkTextDirection direction) {
777 GtkStyleContext* style =
778 GetStyleContext(MOZ_GTK_SPINBUTTON, state->image_scale, direction);
779 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
780 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
781 return MOZ_GTK_SUCCESS;
784 static gint moz_gtk_spin_updown_paint(cairo_t* cr, GdkRectangle* rect,
785 gboolean isDown, GtkWidgetState* state,
786 GtkTextDirection direction) {
787 GtkStyleContext* style =
788 GetStyleContext(MOZ_GTK_SPINBUTTON, state->image_scale, direction,
789 GetStateFlagsFromGtkWidgetState(state));
791 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
792 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
794 /* hard code these values */
795 GdkRectangle arrow_rect;
796 arrow_rect.width = 6;
797 arrow_rect.height = 6;
798 arrow_rect.x = rect->x + (rect->width - arrow_rect.width) / 2;
799 arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2;
800 arrow_rect.y += isDown ? -1 : 1;
802 gtk_render_arrow(style, cr, isDown ? ARROW_DOWN : ARROW_UP, arrow_rect.x,
803 arrow_rect.y, arrow_rect.width);
805 return MOZ_GTK_SUCCESS;
808 /* See gtk_range_draw() for reference.
810 static gint moz_gtk_scale_paint(cairo_t* cr, GdkRectangle* rect,
811 GtkWidgetState* state, GtkOrientation flags,
812 GtkTextDirection direction) {
813 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
814 gint x, y, width, height, min_width, min_height;
815 GtkStyleContext* style;
816 GtkBorder margin;
818 moz_gtk_get_scale_metrics(flags, &min_width, &min_height);
820 WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
821 ? MOZ_GTK_SCALE_TROUGH_HORIZONTAL
822 : MOZ_GTK_SCALE_TROUGH_VERTICAL;
823 style = GetStyleContext(widget, state->image_scale, direction, state_flags);
824 gtk_style_context_get_margin(style, state_flags, &margin);
826 // Clamp the dimension perpendicular to the direction that the slider crosses
827 // to the minimum size.
828 if (flags == GTK_ORIENTATION_HORIZONTAL) {
829 width = rect->width - (margin.left + margin.right);
830 height = min_height - (margin.top + margin.bottom);
831 x = rect->x + margin.left;
832 y = rect->y + (rect->height - height) / 2;
833 } else {
834 width = min_width - (margin.left + margin.right);
835 height = rect->height - (margin.top + margin.bottom);
836 x = rect->x + (rect->width - width) / 2;
837 y = rect->y + margin.top;
840 gtk_render_background(style, cr, x, y, width, height);
841 gtk_render_frame(style, cr, x, y, width, height);
843 if (state->focused)
844 gtk_render_focus(style, cr, rect->x, rect->y, rect->width, rect->height);
846 return MOZ_GTK_SUCCESS;
849 static gint moz_gtk_scale_thumb_paint(cairo_t* cr, GdkRectangle* rect,
850 GtkWidgetState* state,
851 GtkOrientation flags,
852 GtkTextDirection direction) {
853 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
854 GtkStyleContext* style;
855 gint thumb_width, thumb_height, x, y;
857 /* determine the thumb size, and position the thumb in the center in the
858 * opposite axis
860 if (flags == GTK_ORIENTATION_HORIZONTAL) {
861 moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_HORIZONTAL, &thumb_width,
862 &thumb_height);
863 x = rect->x;
864 y = rect->y + (rect->height - thumb_height) / 2;
865 } else {
866 moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_VERTICAL, &thumb_height,
867 &thumb_width);
868 x = rect->x + (rect->width - thumb_width) / 2;
869 y = rect->y;
872 WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
873 ? MOZ_GTK_SCALE_THUMB_HORIZONTAL
874 : MOZ_GTK_SCALE_THUMB_VERTICAL;
875 style = GetStyleContext(widget, state->image_scale, direction, state_flags);
876 gtk_render_slider(style, cr, x, y, thumb_width, thumb_height, flags);
878 return MOZ_GTK_SUCCESS;
881 static gint moz_gtk_hpaned_paint(cairo_t* cr, GdkRectangle* rect,
882 GtkWidgetState* state) {
883 GtkStyleContext* style =
884 GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_HORIZONTAL, state->image_scale,
885 GTK_TEXT_DIR_LTR, GetStateFlagsFromGtkWidgetState(state));
886 gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
887 return MOZ_GTK_SUCCESS;
890 static gint moz_gtk_vpaned_paint(cairo_t* cr, GdkRectangle* rect,
891 GtkWidgetState* state) {
892 GtkStyleContext* style =
893 GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_VERTICAL, state->image_scale,
894 GTK_TEXT_DIR_LTR, GetStateFlagsFromGtkWidgetState(state));
895 gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
896 return MOZ_GTK_SUCCESS;
899 // See gtk_entry_draw() for reference.
900 static gint moz_gtk_entry_paint(cairo_t* cr, const GdkRectangle* aRect,
901 GtkWidgetState* state, GtkStyleContext* style,
902 WidgetNodeType widget) {
903 GdkRectangle rect = *aRect;
904 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
906 // Paint the border, except for 'menulist-textfield' that isn't focused:
907 if (widget != MOZ_GTK_DROPDOWN_ENTRY || state->focused) {
908 gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
911 return MOZ_GTK_SUCCESS;
914 static gint moz_gtk_text_view_paint(cairo_t* cr, GdkRectangle* aRect,
915 GtkWidgetState* state,
916 GtkTextDirection direction) {
917 // GtkTextView and GtkScrolledWindow do not set active and prelight flags.
918 // The use of focus with MOZ_GTK_SCROLLED_WINDOW here is questionable
919 // because a parent widget will not have focus when its child GtkTextView
920 // has focus, but perhaps this may help identify a focused textarea with
921 // some themes as GtkTextView backgrounds do not typically render
922 // differently with focus.
923 GtkStateFlags state_flags = state->disabled ? GTK_STATE_FLAG_INSENSITIVE
924 : state->focused ? GTK_STATE_FLAG_FOCUSED
925 : GTK_STATE_FLAG_NORMAL;
927 GtkStyleContext* style_frame = GetStyleContext(
928 MOZ_GTK_SCROLLED_WINDOW, state->image_scale, direction, state_flags);
929 gtk_render_frame(style_frame, cr, aRect->x, aRect->y, aRect->width,
930 aRect->height);
932 GdkRectangle rect = *aRect;
933 InsetByBorderPadding(&rect, style_frame);
935 GtkStyleContext* style = GetStyleContext(
936 MOZ_GTK_TEXT_VIEW, state->image_scale, direction, state_flags);
937 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
938 // There is a separate "text" window, which usually provides the
939 // background behind the text. However, this is transparent in Ambiance
940 // for GTK 3.20, in which case the MOZ_GTK_TEXT_VIEW background is
941 // visible.
942 style = GetStyleContext(MOZ_GTK_TEXT_VIEW_TEXT, state->image_scale, direction,
943 state_flags);
944 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
946 return MOZ_GTK_SUCCESS;
949 static gint moz_gtk_treeview_paint(cairo_t* cr, GdkRectangle* rect,
950 GtkWidgetState* state,
951 GtkTextDirection direction) {
952 gint xthickness, ythickness;
953 GtkStyleContext* style;
954 GtkStyleContext* style_tree;
955 GtkStateFlags state_flags;
956 GtkBorder border;
958 /* only handle disabled and normal states, otherwise the whole background
959 * area will be painted differently with other states */
960 state_flags =
961 state->disabled ? GTK_STATE_FLAG_INSENSITIVE : GTK_STATE_FLAG_NORMAL;
963 style =
964 GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, state->image_scale, direction);
965 gtk_style_context_get_border(style, state_flags, &border);
966 xthickness = border.left;
967 ythickness = border.top;
969 style_tree =
970 GetStyleContext(MOZ_GTK_TREEVIEW_VIEW, state->image_scale, direction);
971 gtk_render_background(style_tree, cr, rect->x + xthickness,
972 rect->y + ythickness, rect->width - 2 * xthickness,
973 rect->height - 2 * ythickness);
975 style =
976 GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, state->image_scale, direction);
977 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
978 return MOZ_GTK_SUCCESS;
981 static gint moz_gtk_tree_header_cell_paint(cairo_t* cr,
982 const GdkRectangle* aRect,
983 GtkWidgetState* state,
984 gboolean isSorted,
985 GtkTextDirection direction) {
986 moz_gtk_button_paint(cr, aRect, state, GTK_RELIEF_NORMAL,
987 GetWidget(MOZ_GTK_TREE_HEADER_CELL), direction);
988 return MOZ_GTK_SUCCESS;
991 /* See gtk_expander_paint() for reference.
993 static gint moz_gtk_treeview_expander_paint(cairo_t* cr, GdkRectangle* rect,
994 GtkWidgetState* state,
995 GtkExpanderStyle expander_state,
996 GtkTextDirection direction) {
997 /* Because the frame we get is of the entire treeview, we can't get the
998 * precise event state of one expander, thus rendering hover and active
999 * feedback useless. */
1000 GtkStateFlags state_flags =
1001 state->disabled ? GTK_STATE_FLAG_INSENSITIVE : GTK_STATE_FLAG_NORMAL;
1003 if (state->inHover)
1004 state_flags =
1005 static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_PRELIGHT);
1006 if (state->selected)
1007 state_flags =
1008 static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_SELECTED);
1010 /* GTK_STATE_FLAG_ACTIVE controls expanded/colapsed state rendering
1011 * in gtk_render_expander()
1013 if (expander_state == GTK_EXPANDER_EXPANDED)
1014 state_flags =
1015 static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
1016 else
1017 state_flags =
1018 static_cast<GtkStateFlags>(state_flags & ~(checkbox_check_state));
1020 GtkStyleContext* style = GetStyleContext(
1021 MOZ_GTK_TREEVIEW_EXPANDER, state->image_scale, direction, state_flags);
1022 gtk_render_expander(style, cr, rect->x, rect->y, rect->width, rect->height);
1024 return MOZ_GTK_SUCCESS;
1027 /* See gtk_separator_draw() for reference.
1029 static gint moz_gtk_combo_box_paint(cairo_t* cr, const GdkRectangle* aRect,
1030 GtkWidgetState* state,
1031 GtkTextDirection direction) {
1032 GdkRectangle arrow_rect, real_arrow_rect;
1033 gint separator_width;
1034 gboolean wide_separators;
1035 GtkStyleContext* style;
1036 GtkRequisition arrow_req;
1038 GtkWidget* comboBoxButton = GetWidget(MOZ_GTK_COMBOBOX_BUTTON);
1039 GtkWidget* comboBoxArrow = GetWidget(MOZ_GTK_COMBOBOX_ARROW);
1040 if (!comboBoxButton || !comboBoxArrow) {
1041 return MOZ_GTK_UNKNOWN_WIDGET;
1044 /* Also sets the direction on gComboBoxButtonWidget, which is then
1045 * inherited by the separator and arrow */
1046 moz_gtk_button_paint(cr, aRect, state, GTK_RELIEF_NORMAL, comboBoxButton,
1047 direction);
1049 calculate_button_inner_rect(comboBoxButton, aRect, &arrow_rect, direction);
1050 /* Now arrow_rect contains the inner rect ; we want to correct the width
1051 * to what the arrow needs (see gtk_combo_box_size_allocate) */
1052 gtk_widget_get_preferred_size(comboBoxArrow, NULL, &arrow_req);
1053 moz_gtk_sanity_preferred_size(&arrow_req);
1055 if (direction == GTK_TEXT_DIR_LTR)
1056 arrow_rect.x += arrow_rect.width - arrow_req.width;
1057 arrow_rect.width = arrow_req.width;
1059 calculate_arrow_rect(comboBoxArrow, &arrow_rect, &real_arrow_rect, direction);
1061 style = GetStyleContext(MOZ_GTK_COMBOBOX_ARROW, state->image_scale);
1062 gtk_render_arrow(style, cr, ARROW_DOWN, real_arrow_rect.x, real_arrow_rect.y,
1063 real_arrow_rect.width);
1065 /* If there is no separator in the theme, there's nothing left to do. */
1066 GtkWidget* widget = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR);
1067 if (!widget) {
1068 return MOZ_GTK_SUCCESS;
1070 style = gtk_widget_get_style_context(widget);
1071 StyleContextSetScale(style, state->image_scale);
1072 gtk_style_context_get_style(style, "wide-separators", &wide_separators,
1073 "separator-width", &separator_width, NULL);
1075 if (wide_separators) {
1076 if (direction == GTK_TEXT_DIR_LTR)
1077 arrow_rect.x -= separator_width;
1078 else
1079 arrow_rect.x += arrow_rect.width;
1081 gtk_render_frame(style, cr, arrow_rect.x, arrow_rect.y, separator_width,
1082 arrow_rect.height);
1083 } else {
1084 if (direction == GTK_TEXT_DIR_LTR) {
1085 GtkBorder padding;
1086 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1087 gtk_style_context_get_padding(style, state_flags, &padding);
1088 arrow_rect.x -= padding.left;
1089 } else
1090 arrow_rect.x += arrow_rect.width;
1092 gtk_render_line(style, cr, arrow_rect.x, arrow_rect.y, arrow_rect.x,
1093 arrow_rect.y + arrow_rect.height);
1095 return MOZ_GTK_SUCCESS;
1098 static gint moz_gtk_arrow_paint(cairo_t* cr, GdkRectangle* rect,
1099 GtkWidgetState* state, GtkArrowType arrow_type,
1100 GtkTextDirection direction) {
1101 GdkRectangle arrow_rect;
1102 gdouble arrow_angle;
1104 if (direction == GTK_TEXT_DIR_RTL) {
1105 if (arrow_type == GTK_ARROW_LEFT) {
1106 arrow_type = GTK_ARROW_RIGHT;
1107 } else if (arrow_type == GTK_ARROW_RIGHT) {
1108 arrow_type = GTK_ARROW_LEFT;
1111 switch (arrow_type) {
1112 case GTK_ARROW_LEFT:
1113 arrow_angle = ARROW_LEFT;
1114 break;
1115 case GTK_ARROW_RIGHT:
1116 arrow_angle = ARROW_RIGHT;
1117 break;
1118 case GTK_ARROW_DOWN:
1119 arrow_angle = ARROW_DOWN;
1120 break;
1121 default:
1122 arrow_angle = ARROW_UP;
1123 break;
1125 if (arrow_type == GTK_ARROW_NONE) return MOZ_GTK_SUCCESS;
1127 GtkWidget* widget = GetWidget(MOZ_GTK_BUTTON_ARROW);
1128 if (!widget) {
1129 return MOZ_GTK_UNKNOWN_WIDGET;
1131 calculate_arrow_rect(widget, rect, &arrow_rect, direction);
1132 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1133 GtkStyleContext* style = GetStyleContext(
1134 MOZ_GTK_BUTTON_ARROW, state->image_scale, direction, state_flags);
1135 gtk_render_arrow(style, cr, arrow_angle, arrow_rect.x, arrow_rect.y,
1136 arrow_rect.width);
1137 return MOZ_GTK_SUCCESS;
1140 static gint moz_gtk_tooltip_paint(cairo_t* cr, const GdkRectangle* aRect,
1141 GtkWidgetState* state,
1142 GtkTextDirection direction) {
1143 // Tooltip widget is made in GTK3 as following tree:
1144 // Tooltip window
1145 // Horizontal Box
1146 // Icon (not supported by Firefox)
1147 // Label
1148 // Each element can be fully styled by CSS of GTK theme.
1149 // We have to draw all elements with appropriate offset and right dimensions.
1151 // Tooltip drawing
1152 GtkStyleContext* style =
1153 GetStyleContext(MOZ_GTK_TOOLTIP, state->image_scale, direction);
1154 GdkRectangle rect = *aRect;
1155 gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
1156 gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
1158 // Horizontal Box drawing
1160 // The box element has hard-coded 6px margin-* GtkWidget properties, which
1161 // are added between the window dimensions and the CSS margin box of the
1162 // horizontal box. The frame of the tooltip window is drawn in the
1163 // 6px margin.
1164 // For drawing Horizontal Box we have to inset drawing area by that 6px
1165 // plus its CSS margin.
1166 GtkStyleContext* boxStyle =
1167 GetStyleContext(MOZ_GTK_TOOLTIP_BOX, state->image_scale, direction);
1169 rect.x += 6;
1170 rect.y += 6;
1171 rect.width -= 12;
1172 rect.height -= 12;
1174 InsetByMargin(&rect, boxStyle);
1175 gtk_render_background(boxStyle, cr, rect.x, rect.y, rect.width, rect.height);
1176 gtk_render_frame(boxStyle, cr, rect.x, rect.y, rect.width, rect.height);
1178 // Label drawing
1179 InsetByBorderPadding(&rect, boxStyle);
1181 GtkStyleContext* labelStyle =
1182 GetStyleContext(MOZ_GTK_TOOLTIP_BOX_LABEL, state->image_scale, direction);
1183 moz_gtk_draw_styled_frame(labelStyle, cr, &rect, false);
1185 return MOZ_GTK_SUCCESS;
1188 static gint moz_gtk_resizer_paint(cairo_t* cr, GdkRectangle* rect,
1189 GtkWidgetState* state,
1190 GtkTextDirection direction) {
1191 GtkStyleContext* style =
1192 GetStyleContext(MOZ_GTK_RESIZER, state->image_scale, GTK_TEXT_DIR_LTR,
1193 GetStateFlagsFromGtkWidgetState(state));
1195 // Workaround unico not respecting the text direction for resizers.
1196 // See bug 1174248.
1197 cairo_save(cr);
1198 if (direction == GTK_TEXT_DIR_RTL) {
1199 cairo_matrix_t mat;
1200 cairo_matrix_init_translate(&mat, 2 * rect->x + rect->width, 0);
1201 cairo_matrix_scale(&mat, -1, 1);
1202 cairo_transform(cr, &mat);
1205 gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
1206 cairo_restore(cr);
1208 return MOZ_GTK_SUCCESS;
1211 static gint moz_gtk_frame_paint(cairo_t* cr, GdkRectangle* rect,
1212 GtkWidgetState* state,
1213 GtkTextDirection direction) {
1214 GtkStyleContext* style =
1215 GetStyleContext(MOZ_GTK_FRAME, state->image_scale, direction);
1216 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1217 return MOZ_GTK_SUCCESS;
1220 static gint moz_gtk_progressbar_paint(cairo_t* cr, GdkRectangle* rect,
1221 GtkWidgetState* state,
1222 GtkTextDirection direction) {
1223 GtkStyleContext* style =
1224 GetStyleContext(MOZ_GTK_PROGRESS_TROUGH, state->image_scale, direction);
1225 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1226 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1228 return MOZ_GTK_SUCCESS;
1231 static gint moz_gtk_progress_chunk_paint(cairo_t* cr, GdkRectangle* rect,
1232 GtkWidgetState* state,
1233 GtkTextDirection direction,
1234 WidgetNodeType widget) {
1235 GtkStyleContext* style =
1236 GetStyleContext(MOZ_GTK_PROGRESS_CHUNK, state->image_scale, direction);
1238 if (widget == MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE ||
1239 widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE) {
1241 * The bar's size and the bar speed are set depending of the progress'
1242 * size. These could also be constant for all progress bars easily.
1244 gboolean vertical =
1245 (widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE);
1247 /* The size of the dimension we are going to use for the animation. */
1248 const gint progressSize = vertical ? rect->height : rect->width;
1250 /* The bar is using a fifth of the element size, based on GtkProgressBar
1251 * activity-blocks property. */
1252 const gint barSize = MAX(1, progressSize / 5);
1254 /* Represents the travel that has to be done for a complete cycle. */
1255 const gint travel = 2 * (progressSize - barSize);
1257 /* period equals to travel / pixelsPerMillisecond
1258 * where pixelsPerMillisecond equals progressSize / 1000.0.
1259 * This is equivalent to 1600. */
1260 static const guint period = 1600;
1261 const gint t = PR_IntervalToMilliseconds(PR_IntervalNow()) % period;
1262 const gint dx = travel * t / period;
1264 if (vertical) {
1265 rect->y += (dx < travel / 2) ? dx : travel - dx;
1266 rect->height = barSize;
1267 } else {
1268 rect->x += (dx < travel / 2) ? dx : travel - dx;
1269 rect->width = barSize;
1273 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1274 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1276 return MOZ_GTK_SUCCESS;
1279 static gint moz_gtk_get_tab_thickness(GtkStyleContext* style) {
1280 if (!notebook_has_tab_gap)
1281 return 0; /* tabs do not overdraw the tabpanel border with "no gap" style */
1283 GtkBorder border;
1284 gtk_style_context_get_border(style, gtk_style_context_get_state(style),
1285 &border);
1286 if (border.top < 2) return 2; /* some themes don't set ythickness correctly */
1288 return border.top;
1291 gint moz_gtk_get_tab_thickness(WidgetNodeType aNodeType) {
1292 GtkStyleContext* style = GetStyleContext(aNodeType);
1293 int thickness = moz_gtk_get_tab_thickness(style);
1294 return thickness;
1297 /* actual small tabs */
1298 static gint moz_gtk_tab_paint(cairo_t* cr, GdkRectangle* rect,
1299 GtkWidgetState* state, GtkTabFlags flags,
1300 GtkTextDirection direction,
1301 WidgetNodeType widget) {
1302 /* When the tab isn't selected, we just draw a notebook extension.
1303 * When it is selected, we overwrite the adjacent border of the tabpanel
1304 * touching the tab with a pierced border (called "the gap") to make the
1305 * tab appear physically attached to the tabpanel; see details below. */
1307 GtkStyleContext* style;
1308 GdkRectangle tabRect;
1309 GdkRectangle focusRect;
1310 GdkRectangle backRect;
1311 int initial_gap = 0;
1312 bool isBottomTab = (widget == MOZ_GTK_TAB_BOTTOM);
1314 style = GetStyleContext(widget, state->image_scale, direction,
1315 GetStateFlagsFromGtkTabFlags(flags));
1316 tabRect = *rect;
1318 if (flags & MOZ_GTK_TAB_FIRST) {
1319 gtk_style_context_get_style(style, "initial-gap", &initial_gap, NULL);
1320 tabRect.width -= initial_gap;
1322 if (direction != GTK_TEXT_DIR_RTL) {
1323 tabRect.x += initial_gap;
1327 focusRect = backRect = tabRect;
1329 if (notebook_has_tab_gap) {
1330 if ((flags & MOZ_GTK_TAB_SELECTED) == 0) {
1331 /* Only draw the tab */
1332 gtk_render_extension(style, cr, tabRect.x, tabRect.y, tabRect.width,
1333 tabRect.height,
1334 isBottomTab ? GTK_POS_TOP : GTK_POS_BOTTOM);
1335 } else {
1336 /* Draw the tab and the gap
1337 * We want the gap to be positioned exactly on the tabpanel top
1338 * border; since tabbox.css may set a negative margin so that the tab
1339 * frame rect already overlaps the tabpanel frame rect, we need to take
1340 * that into account when drawing. To that effect, nsNativeThemeGTK
1341 * passes us this negative margin (bmargin in the graphic below) in the
1342 * lowest bits of |flags|. We use it to set gap_voffset, the distance
1343 * between the top of the gap and the bottom of the tab (resp. the
1344 * bottom of the gap and the top of the tab when we draw a bottom tab),
1345 * while ensuring that the gap always touches the border of the tab,
1346 * i.e. 0 <= gap_voffset <= gap_height, to avoid surprinsing results
1347 * with big negative or positive margins.
1348 * Here is a graphical explanation in the case of top tabs:
1349 * ___________________________
1350 * / \
1351 * | T A B |
1352 * ----------|. . . . . . . . . . . . . . .|----- top of tabpanel
1353 * : ^ bmargin : ^
1354 * : | (-negative margin, : |
1355 * bottom : v passed in flags) : | gap_height
1356 * of -> :.............................: | (the size of the
1357 * the tab . part of the gap . | tabpanel top border)
1358 * . outside of the tab . v
1359 * ----------------------------------------------
1361 * To draw the gap, we use gtk_render_frame_gap(), see comment in
1362 * moz_gtk_tabpanels_paint(). This gap is made 3 * gap_height tall,
1363 * which should suffice to ensure that the only visible border is the
1364 * pierced one. If the tab is in the middle, we make the box_gap begin
1365 * a bit to the left of the tab and end a bit to the right, adjusting
1366 * the gap position so it still is under the tab, because we want the
1367 * rendering of a gap in the middle of a tabpanel. This is the role of
1368 * the gints gap_{l,r}_offset. On the contrary, if the tab is the
1369 * first, we align the start border of the box_gap with the start
1370 * border of the tab (left if LTR, right if RTL), by setting the
1371 * appropriate offset to 0.*/
1372 gint gap_loffset, gap_roffset, gap_voffset, gap_height;
1374 /* Get height needed by the gap */
1375 gap_height = moz_gtk_get_tab_thickness(style);
1377 /* Extract gap_voffset from the first bits of flags */
1378 gap_voffset = flags & MOZ_GTK_TAB_MARGIN_MASK;
1379 if (gap_voffset > gap_height) gap_voffset = gap_height;
1381 /* Set gap_{l,r}_offset to appropriate values */
1382 gap_loffset = gap_roffset = 20; /* should be enough */
1383 if (flags & MOZ_GTK_TAB_FIRST) {
1384 if (direction == GTK_TEXT_DIR_RTL)
1385 gap_roffset = initial_gap;
1386 else
1387 gap_loffset = initial_gap;
1390 GtkStyleContext* panelStyle =
1391 GetStyleContext(MOZ_GTK_TABPANELS, state->image_scale, direction);
1393 if (isBottomTab) {
1394 /* Draw the tab on bottom */
1395 focusRect.y += gap_voffset;
1396 focusRect.height -= gap_voffset;
1398 gtk_render_extension(style, cr, tabRect.x, tabRect.y + gap_voffset,
1399 tabRect.width, tabRect.height - gap_voffset,
1400 GTK_POS_TOP);
1402 backRect.y += (gap_voffset - gap_height);
1403 backRect.height = gap_height;
1405 /* Draw the gap; erase with background color before painting in
1406 * case theme does not */
1407 gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
1408 backRect.width, backRect.height);
1409 cairo_save(cr);
1410 cairo_rectangle(cr, backRect.x, backRect.y, backRect.width,
1411 backRect.height);
1412 cairo_clip(cr);
1414 gtk_render_frame_gap(panelStyle, cr, tabRect.x - gap_loffset,
1415 tabRect.y + gap_voffset - 3 * gap_height,
1416 tabRect.width + gap_loffset + gap_roffset,
1417 3 * gap_height, GTK_POS_BOTTOM, gap_loffset,
1418 gap_loffset + tabRect.width);
1419 cairo_restore(cr);
1420 } else {
1421 /* Draw the tab on top */
1422 focusRect.height -= gap_voffset;
1423 gtk_render_extension(style, cr, tabRect.x, tabRect.y, tabRect.width,
1424 tabRect.height - gap_voffset, GTK_POS_BOTTOM);
1426 backRect.y += (tabRect.height - gap_voffset);
1427 backRect.height = gap_height;
1429 /* Draw the gap; erase with background color before painting in
1430 * case theme does not */
1431 gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
1432 backRect.width, backRect.height);
1434 cairo_save(cr);
1435 cairo_rectangle(cr, backRect.x, backRect.y, backRect.width,
1436 backRect.height);
1437 cairo_clip(cr);
1439 gtk_render_frame_gap(panelStyle, cr, tabRect.x - gap_loffset,
1440 tabRect.y + tabRect.height - gap_voffset,
1441 tabRect.width + gap_loffset + gap_roffset,
1442 3 * gap_height, GTK_POS_TOP, gap_loffset,
1443 gap_loffset + tabRect.width);
1444 cairo_restore(cr);
1447 } else {
1448 gtk_render_background(style, cr, tabRect.x, tabRect.y, tabRect.width,
1449 tabRect.height);
1450 gtk_render_frame(style, cr, tabRect.x, tabRect.y, tabRect.width,
1451 tabRect.height);
1454 if (state->focused) {
1455 /* Paint the focus ring */
1456 GtkBorder padding;
1457 gtk_style_context_get_padding(style, GetStateFlagsFromGtkWidgetState(state),
1458 &padding);
1460 focusRect.x += padding.left;
1461 focusRect.width -= (padding.left + padding.right);
1462 focusRect.y += padding.top;
1463 focusRect.height -= (padding.top + padding.bottom);
1465 gtk_render_focus(style, cr, focusRect.x, focusRect.y, focusRect.width,
1466 focusRect.height);
1469 return MOZ_GTK_SUCCESS;
1472 /* tab area*/
1473 static gint moz_gtk_tabpanels_paint(cairo_t* cr, GdkRectangle* rect,
1474 GtkWidgetState* state,
1475 GtkTextDirection direction) {
1476 GtkStyleContext* style =
1477 GetStyleContext(MOZ_GTK_TABPANELS, state->image_scale, direction);
1478 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1480 * The gap size is not needed in moz_gtk_tabpanels_paint because
1481 * the gap will be painted with the foreground tab in moz_gtk_tab_paint.
1483 * However, if moz_gtk_tabpanels_paint just uses gtk_render_frame(),
1484 * the theme will think that there are no tabs and may draw something
1485 * different.Hence the trick of using two clip regions, and drawing the
1486 * gap outside each clip region, to get the correct frame for
1487 * a tabpanel with tabs.
1489 /* left side */
1490 cairo_save(cr);
1491 cairo_rectangle(cr, rect->x, rect->y, rect->x + rect->width / 2,
1492 rect->y + rect->height);
1493 cairo_clip(cr);
1494 gtk_render_frame_gap(style, cr, rect->x, rect->y, rect->width, rect->height,
1495 GTK_POS_TOP, rect->width - 1, rect->width);
1496 cairo_restore(cr);
1498 /* right side */
1499 cairo_save(cr);
1500 cairo_rectangle(cr, rect->x + rect->width / 2, rect->y, rect->x + rect->width,
1501 rect->y + rect->height);
1502 cairo_clip(cr);
1503 gtk_render_frame_gap(style, cr, rect->x, rect->y, rect->width, rect->height,
1504 GTK_POS_TOP, 0, 1);
1505 cairo_restore(cr);
1507 return MOZ_GTK_SUCCESS;
1510 static gint moz_gtk_tab_scroll_arrow_paint(cairo_t* cr, GdkRectangle* rect,
1511 GtkWidgetState* state,
1512 GtkArrowType arrow_type,
1513 GtkTextDirection direction) {
1514 GtkStyleContext* style;
1515 gdouble arrow_angle;
1516 gint arrow_size = MIN(rect->width, rect->height);
1517 gint x = rect->x + (rect->width - arrow_size) / 2;
1518 gint y = rect->y + (rect->height - arrow_size) / 2;
1520 if (direction == GTK_TEXT_DIR_RTL) {
1521 arrow_type =
1522 (arrow_type == GTK_ARROW_LEFT) ? GTK_ARROW_RIGHT : GTK_ARROW_LEFT;
1524 switch (arrow_type) {
1525 case GTK_ARROW_LEFT:
1526 arrow_angle = ARROW_LEFT;
1527 break;
1528 case GTK_ARROW_RIGHT:
1529 arrow_angle = ARROW_RIGHT;
1530 break;
1531 case GTK_ARROW_DOWN:
1532 arrow_angle = ARROW_DOWN;
1533 break;
1534 default:
1535 arrow_angle = ARROW_UP;
1536 break;
1538 if (arrow_type != GTK_ARROW_NONE) {
1539 style = GetStyleContext(MOZ_GTK_TAB_SCROLLARROW, state->image_scale,
1540 direction, GetStateFlagsFromGtkWidgetState(state));
1541 gtk_render_arrow(style, cr, arrow_angle, x, y, arrow_size);
1543 return MOZ_GTK_SUCCESS;
1546 static gint moz_gtk_header_bar_paint(WidgetNodeType widgetType, cairo_t* cr,
1547 GdkRectangle* rect,
1548 GtkWidgetState* state) {
1549 GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
1550 GtkStyleContext* style = GetStyleContext(widgetType, state->image_scale,
1551 GTK_TEXT_DIR_NONE, state_flags);
1553 // Some themes like Elementary's style the container of the headerbar rather
1554 // than the header bar itself.
1555 if (HeaderBarShouldDrawContainer(widgetType)) {
1556 auto containerType = widgetType == MOZ_GTK_HEADER_BAR
1557 ? MOZ_GTK_HEADERBAR_FIXED
1558 : MOZ_GTK_HEADERBAR_FIXED_MAXIMIZED;
1559 style = GetStyleContext(containerType, state->image_scale,
1560 GTK_TEXT_DIR_NONE, state_flags);
1563 gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
1564 gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
1566 return MOZ_GTK_SUCCESS;
1569 gint moz_gtk_get_widget_border(WidgetNodeType widget, gint* left, gint* top,
1570 gint* right, gint* bottom,
1571 // NOTE: callers depend on direction being used
1572 // only for MOZ_GTK_DROPDOWN widgets.
1573 GtkTextDirection direction) {
1574 GtkWidget* w;
1575 GtkStyleContext* style;
1576 *left = *top = *right = *bottom = 0;
1578 switch (widget) {
1579 case MOZ_GTK_BUTTON:
1580 case MOZ_GTK_TOOLBAR_BUTTON: {
1581 style = GetStyleContext(MOZ_GTK_BUTTON);
1583 *left = *top = *right = *bottom = gtk_container_get_border_width(
1584 GTK_CONTAINER(GetWidget(MOZ_GTK_BUTTON)));
1586 if (widget == MOZ_GTK_TOOLBAR_BUTTON) {
1587 gtk_style_context_save(style);
1588 gtk_style_context_add_class(style, "image-button");
1591 moz_gtk_add_style_padding(style, left, top, right, bottom);
1593 if (widget == MOZ_GTK_TOOLBAR_BUTTON) gtk_style_context_restore(style);
1595 moz_gtk_add_style_border(style, left, top, right, bottom);
1597 return MOZ_GTK_SUCCESS;
1599 case MOZ_GTK_ENTRY:
1600 case MOZ_GTK_DROPDOWN_ENTRY: {
1601 style = GetStyleContext(widget);
1603 // XXX: Subtract 1 pixel from the padding to account for the default
1604 // padding in forms.css. See bug 1187385.
1605 *left = *top = *right = *bottom = -1;
1606 moz_gtk_add_border_padding(style, left, top, right, bottom);
1608 return MOZ_GTK_SUCCESS;
1610 case MOZ_GTK_TEXT_VIEW:
1611 case MOZ_GTK_TREEVIEW: {
1612 style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW);
1613 moz_gtk_add_style_border(style, left, top, right, bottom);
1614 return MOZ_GTK_SUCCESS;
1616 case MOZ_GTK_TREE_HEADER_CELL: {
1617 /* A Tree Header in GTK is just a different styled button
1618 * It must be placed in a TreeView for getting the correct style
1619 * assigned.
1620 * That is why the following code is the same as for MOZ_GTK_BUTTON.
1621 * */
1622 *left = *top = *right = *bottom = gtk_container_get_border_width(
1623 GTK_CONTAINER(GetWidget(MOZ_GTK_TREE_HEADER_CELL)));
1624 style = GetStyleContext(MOZ_GTK_TREE_HEADER_CELL);
1625 moz_gtk_add_border_padding(style, left, top, right, bottom);
1626 return MOZ_GTK_SUCCESS;
1628 case MOZ_GTK_DROPDOWN: {
1629 /* We need to account for the arrow on the dropdown, so text
1630 * doesn't come too close to the arrow, or in some cases spill
1631 * into the arrow. */
1632 gboolean wide_separators;
1633 gint separator_width;
1634 GtkRequisition arrow_req;
1635 GtkBorder border;
1637 *left = *top = *right = *bottom = gtk_container_get_border_width(
1638 GTK_CONTAINER(GetWidget(MOZ_GTK_COMBOBOX_BUTTON)));
1639 style = GetStyleContext(MOZ_GTK_COMBOBOX_BUTTON);
1640 moz_gtk_add_border_padding(style, left, top, right, bottom);
1642 /* If there is no separator, don't try to count its width. */
1643 separator_width = 0;
1644 GtkWidget* comboBoxSeparator = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR);
1645 if (comboBoxSeparator) {
1646 style = gtk_widget_get_style_context(comboBoxSeparator);
1647 gtk_style_context_get_style(style, "wide-separators", &wide_separators,
1648 "separator-width", &separator_width, NULL);
1650 if (!wide_separators) {
1651 gtk_style_context_get_border(
1652 style, gtk_style_context_get_state(style), &border);
1653 separator_width = border.left;
1657 gtk_widget_get_preferred_size(GetWidget(MOZ_GTK_COMBOBOX_ARROW), NULL,
1658 &arrow_req);
1659 moz_gtk_sanity_preferred_size(&arrow_req);
1661 if (direction == GTK_TEXT_DIR_RTL)
1662 *left += separator_width + arrow_req.width;
1663 else
1664 *right += separator_width + arrow_req.width;
1666 return MOZ_GTK_SUCCESS;
1668 case MOZ_GTK_TABPANELS:
1669 w = GetWidget(MOZ_GTK_TABPANELS);
1670 break;
1671 case MOZ_GTK_PROGRESSBAR:
1672 w = GetWidget(MOZ_GTK_PROGRESSBAR);
1673 break;
1674 case MOZ_GTK_SPINBUTTON_ENTRY:
1675 case MOZ_GTK_SPINBUTTON_UP:
1676 case MOZ_GTK_SPINBUTTON_DOWN:
1677 w = GetWidget(MOZ_GTK_SPINBUTTON);
1678 break;
1679 case MOZ_GTK_SCALE_HORIZONTAL:
1680 case MOZ_GTK_SCALE_VERTICAL:
1681 w = GetWidget(widget);
1682 break;
1683 case MOZ_GTK_FRAME:
1684 w = GetWidget(MOZ_GTK_FRAME);
1685 break;
1686 case MOZ_GTK_TOOLTIP: {
1687 // In GTK 3 there are 6 pixels of additional margin around the box.
1688 // See details there:
1689 // https://github.com/GNOME/gtk/blob/5ea69a136bd7e4970b3a800390e20314665aaed2/gtk/ui/gtktooltipwindow.ui#L11
1690 *left = *right = *top = *bottom = 6;
1692 // We also need to add margin/padding/borders from Tooltip content.
1693 // Tooltip contains horizontal box, where icon and label is put.
1694 // We ignore icon as long as we don't have support for it.
1695 GtkStyleContext* boxStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX);
1696 moz_gtk_add_margin_border_padding(boxStyle, left, top, right, bottom);
1698 GtkStyleContext* labelStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX_LABEL);
1699 moz_gtk_add_margin_border_padding(labelStyle, left, top, right, bottom);
1701 return MOZ_GTK_SUCCESS;
1703 case MOZ_GTK_HEADER_BAR_BUTTON_BOX: {
1704 style = GetStyleContext(MOZ_GTK_HEADER_BAR);
1705 moz_gtk_add_border_padding(style, left, top, right, bottom);
1706 *top = *bottom = 0;
1707 bool leftButtonsPlacement = false;
1708 GetGtkHeaderBarButtonLayout({}, &leftButtonsPlacement);
1709 if (direction == GTK_TEXT_DIR_RTL) {
1710 leftButtonsPlacement = !leftButtonsPlacement;
1712 if (leftButtonsPlacement) {
1713 *right = 0;
1714 } else {
1715 *left = 0;
1717 return MOZ_GTK_SUCCESS;
1719 /* These widgets have no borders, since they are not containers. */
1720 case MOZ_GTK_SPLITTER_HORIZONTAL:
1721 case MOZ_GTK_SPLITTER_VERTICAL:
1722 case MOZ_GTK_CHECKBUTTON:
1723 case MOZ_GTK_RADIOBUTTON:
1724 case MOZ_GTK_SCALE_THUMB_HORIZONTAL:
1725 case MOZ_GTK_SCALE_THUMB_VERTICAL:
1726 case MOZ_GTK_PROGRESS_CHUNK:
1727 case MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE:
1728 case MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE:
1729 case MOZ_GTK_TREEVIEW_EXPANDER:
1730 case MOZ_GTK_HEADER_BAR:
1731 case MOZ_GTK_HEADER_BAR_MAXIMIZED:
1732 case MOZ_GTK_HEADER_BAR_BUTTON_CLOSE:
1733 case MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE:
1734 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE:
1735 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE:
1736 /* These widgets have no borders.*/
1737 case MOZ_GTK_INNER_SPIN_BUTTON:
1738 case MOZ_GTK_SPINBUTTON:
1739 case MOZ_GTK_WINDOW_DECORATION:
1740 case MOZ_GTK_WINDOW_DECORATION_SOLID:
1741 case MOZ_GTK_RESIZER:
1742 case MOZ_GTK_TOOLBARBUTTON_ARROW:
1743 case MOZ_GTK_TAB_SCROLLARROW:
1744 return MOZ_GTK_SUCCESS;
1745 default:
1746 g_warning("Unsupported widget type: %d", widget);
1747 return MOZ_GTK_UNKNOWN_WIDGET;
1749 /* TODO - we're still missing some widget implementations */
1750 if (w) {
1751 moz_gtk_add_style_border(gtk_widget_get_style_context(w), left, top, right,
1752 bottom);
1754 return MOZ_GTK_SUCCESS;
1757 gint moz_gtk_get_tab_border(gint* left, gint* top, gint* right, gint* bottom,
1758 GtkTextDirection direction, GtkTabFlags flags,
1759 WidgetNodeType widget) {
1760 GtkStyleContext* style = GetStyleContext(widget, 1, direction,
1761 GetStateFlagsFromGtkTabFlags(flags));
1763 *left = *top = *right = *bottom = 0;
1764 moz_gtk_add_style_padding(style, left, top, right, bottom);
1766 // Gtk >= 3.20 does not use those styles
1767 if (gtk_check_version(3, 20, 0) != nullptr) {
1768 int tab_curvature;
1770 gtk_style_context_get_style(style, "tab-curvature", &tab_curvature, NULL);
1771 *left += tab_curvature;
1772 *right += tab_curvature;
1774 if (flags & MOZ_GTK_TAB_FIRST) {
1775 int initial_gap = 0;
1776 gtk_style_context_get_style(style, "initial-gap", &initial_gap, NULL);
1777 if (direction == GTK_TEXT_DIR_RTL)
1778 *right += initial_gap;
1779 else
1780 *left += initial_gap;
1782 } else {
1783 GtkBorder margin;
1785 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
1786 &margin);
1787 *left += margin.left;
1788 *right += margin.right;
1790 if (flags & MOZ_GTK_TAB_FIRST) {
1791 style = GetStyleContext(MOZ_GTK_NOTEBOOK_HEADER, direction);
1792 gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
1793 &margin);
1794 *left += margin.left;
1795 *right += margin.right;
1799 return MOZ_GTK_SUCCESS;
1802 gint moz_gtk_get_tab_scroll_arrow_size(gint* width, gint* height) {
1803 gint arrow_size;
1805 GtkStyleContext* style = GetStyleContext(MOZ_GTK_TABPANELS);
1806 gtk_style_context_get_style(style, "scroll-arrow-hlength", &arrow_size, NULL);
1808 *height = *width = arrow_size;
1810 return MOZ_GTK_SUCCESS;
1813 void moz_gtk_get_arrow_size(WidgetNodeType widgetType, gint* width,
1814 gint* height) {
1815 GtkWidget* widget;
1816 switch (widgetType) {
1817 case MOZ_GTK_DROPDOWN:
1818 widget = GetWidget(MOZ_GTK_COMBOBOX_ARROW);
1819 break;
1820 default:
1821 widget = GetWidget(MOZ_GTK_BUTTON_ARROW);
1822 break;
1824 if (widget) {
1825 GtkRequisition requisition;
1826 gtk_widget_get_preferred_size(widget, NULL, &requisition);
1827 moz_gtk_sanity_preferred_size(&requisition);
1829 *width = requisition.width;
1830 *height = requisition.height;
1831 } else {
1832 *width = 0;
1833 *height = 0;
1837 gint moz_gtk_get_expander_size(gint* size) {
1838 GtkStyleContext* style = GetStyleContext(MOZ_GTK_EXPANDER);
1839 gtk_style_context_get_style(style, "expander-size", size, NULL);
1840 return MOZ_GTK_SUCCESS;
1843 gint moz_gtk_get_treeview_expander_size(gint* size) {
1844 GtkStyleContext* style = GetStyleContext(MOZ_GTK_TREEVIEW);
1845 gtk_style_context_get_style(style, "expander-size", size, NULL);
1846 return MOZ_GTK_SUCCESS;
1849 void moz_gtk_get_entry_min_height(gint* min_content_height,
1850 gint* border_padding_height) {
1851 GtkStyleContext* style = GetStyleContext(MOZ_GTK_ENTRY);
1852 if (!gtk_check_version(3, 20, 0)) {
1853 gtk_style_context_get(style, gtk_style_context_get_state(style),
1854 "min-height", min_content_height, nullptr);
1855 } else {
1856 *min_content_height = 0;
1859 GtkBorder border;
1860 GtkBorder padding;
1861 gtk_style_context_get_border(style, gtk_style_context_get_state(style),
1862 &border);
1863 gtk_style_context_get_padding(style, gtk_style_context_get_state(style),
1864 &padding);
1866 *border_padding_height =
1867 (border.top + border.bottom + padding.top + padding.bottom);
1870 void moz_gtk_get_scale_metrics(GtkOrientation orient, gint* scale_width,
1871 gint* scale_height) {
1872 if (gtk_check_version(3, 20, 0) != nullptr) {
1873 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1874 ? MOZ_GTK_SCALE_HORIZONTAL
1875 : MOZ_GTK_SCALE_VERTICAL;
1877 gint thumb_length, thumb_height, trough_border;
1878 moz_gtk_get_scalethumb_metrics(orient, &thumb_length, &thumb_height);
1880 GtkStyleContext* style = GetStyleContext(widget);
1881 gtk_style_context_get_style(style, "trough-border", &trough_border, NULL);
1883 if (orient == GTK_ORIENTATION_HORIZONTAL) {
1884 *scale_width = thumb_length + trough_border * 2;
1885 *scale_height = thumb_height + trough_border * 2;
1886 } else {
1887 *scale_width = thumb_height + trough_border * 2;
1888 *scale_height = thumb_length + trough_border * 2;
1890 } else {
1891 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1892 ? MOZ_GTK_SCALE_TROUGH_HORIZONTAL
1893 : MOZ_GTK_SCALE_TROUGH_VERTICAL;
1894 moz_gtk_get_widget_min_size(GetStyleContext(widget), scale_width,
1895 scale_height);
1899 gint moz_gtk_get_scalethumb_metrics(GtkOrientation orient, gint* thumb_length,
1900 gint* thumb_height) {
1901 if (gtk_check_version(3, 20, 0) != nullptr) {
1902 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1903 ? MOZ_GTK_SCALE_HORIZONTAL
1904 : MOZ_GTK_SCALE_VERTICAL;
1905 GtkStyleContext* style = GetStyleContext(widget);
1906 gtk_style_context_get_style(style, "slider_length", thumb_length,
1907 "slider_width", thumb_height, NULL);
1908 } else {
1909 WidgetNodeType widget = (orient == GTK_ORIENTATION_HORIZONTAL)
1910 ? MOZ_GTK_SCALE_THUMB_HORIZONTAL
1911 : MOZ_GTK_SCALE_THUMB_VERTICAL;
1912 GtkStyleContext* style = GetStyleContext(widget);
1914 gint min_width, min_height;
1915 GtkStateFlags state = gtk_style_context_get_state(style);
1916 gtk_style_context_get(style, state, "min-width", &min_width, "min-height",
1917 &min_height, nullptr);
1918 GtkBorder margin;
1919 gtk_style_context_get_margin(style, state, &margin);
1920 gint margin_width = margin.left + margin.right;
1921 gint margin_height = margin.top + margin.bottom;
1923 // Negative margin of slider element also determines its minimal size
1924 // so use bigger of those two values.
1925 if (min_width < -margin_width) min_width = -margin_width;
1926 if (min_height < -margin_height) min_height = -margin_height;
1928 *thumb_length = min_width;
1929 *thumb_height = min_height;
1932 return MOZ_GTK_SUCCESS;
1935 const ToggleGTKMetrics* GetToggleMetrics(WidgetNodeType aWidgetType) {
1936 ToggleGTKMetrics* metrics;
1938 switch (aWidgetType) {
1939 case MOZ_GTK_RADIOBUTTON:
1940 metrics = &sRadioMetrics;
1941 break;
1942 case MOZ_GTK_CHECKBUTTON:
1943 metrics = &sCheckboxMetrics;
1944 break;
1945 default:
1946 MOZ_CRASH("Unsupported widget type for getting metrics");
1947 return nullptr;
1950 metrics->initialized = true;
1951 if (gtk_check_version(3, 20, 0) == nullptr) {
1952 GtkStyleContext* style = GetStyleContext(aWidgetType);
1953 GtkStateFlags state_flags = gtk_style_context_get_state(style);
1954 gtk_style_context_get(style, state_flags, "min-height",
1955 &(metrics->minSizeWithBorder.height), "min-width",
1956 &(metrics->minSizeWithBorder.width), nullptr);
1957 // Fallback to indicator size if min dimensions are zero
1958 if (metrics->minSizeWithBorder.height == 0 ||
1959 metrics->minSizeWithBorder.width == 0) {
1960 gint indicator_size = 0;
1961 gtk_widget_style_get(GetWidget(MOZ_GTK_CHECKBUTTON_CONTAINER),
1962 "indicator_size", &indicator_size, nullptr);
1963 if (metrics->minSizeWithBorder.height == 0) {
1964 metrics->minSizeWithBorder.height = indicator_size;
1966 if (metrics->minSizeWithBorder.width == 0) {
1967 metrics->minSizeWithBorder.width = indicator_size;
1971 GtkBorder border, padding;
1972 gtk_style_context_get_border(style, state_flags, &border);
1973 gtk_style_context_get_padding(style, state_flags, &padding);
1974 metrics->borderAndPadding.left = border.left + padding.left;
1975 metrics->borderAndPadding.right = border.right + padding.right;
1976 metrics->borderAndPadding.top = border.top + padding.top;
1977 metrics->borderAndPadding.bottom = border.bottom + padding.bottom;
1978 metrics->minSizeWithBorder.width +=
1979 metrics->borderAndPadding.left + metrics->borderAndPadding.right;
1980 metrics->minSizeWithBorder.height +=
1981 metrics->borderAndPadding.top + metrics->borderAndPadding.bottom;
1982 } else {
1983 gint indicator_size = 0, indicator_spacing = 0;
1984 gtk_widget_style_get(GetWidget(MOZ_GTK_CHECKBUTTON_CONTAINER),
1985 "indicator_size", &indicator_size, "indicator_spacing",
1986 &indicator_spacing, nullptr);
1987 metrics->minSizeWithBorder.width = metrics->minSizeWithBorder.height =
1988 indicator_size;
1990 return metrics;
1994 * get_shadow_width() from gtkwindow.c is not public so we need
1995 * to implement it.
1997 void InitWindowDecorationSize(CSDWindowDecorationSize* sWindowDecorationSize,
1998 bool aPopupWindow) {
1999 bool solidDecorations = gtk_style_context_has_class(
2000 GetStyleContext(MOZ_GTK_HEADERBAR_WINDOW, 1), "solid-csd");
2001 // solid-csd does not use frame extents, quit now.
2002 if (solidDecorations) {
2003 sWindowDecorationSize->decorationSize = {0, 0, 0, 0};
2004 return;
2007 // Scale factor is applied later when decoration size is used for actual
2008 // gtk windows.
2009 GtkStyleContext* context = GetStyleContext(MOZ_GTK_WINDOW_DECORATION);
2011 /* Always sum border + padding */
2012 GtkBorder padding;
2013 GtkStateFlags state = gtk_style_context_get_state(context);
2014 gtk_style_context_get_border(context, state,
2015 &sWindowDecorationSize->decorationSize);
2016 gtk_style_context_get_padding(context, state, &padding);
2017 sWindowDecorationSize->decorationSize += padding;
2019 // Available on GTK 3.20+.
2020 static auto sGtkRenderBackgroundGetClip = (void (*)(
2021 GtkStyleContext*, gdouble, gdouble, gdouble, gdouble,
2022 GdkRectangle*))dlsym(RTLD_DEFAULT, "gtk_render_background_get_clip");
2024 if (!sGtkRenderBackgroundGetClip) {
2025 return;
2028 GdkRectangle clip;
2029 sGtkRenderBackgroundGetClip(context, 0, 0, 0, 0, &clip);
2031 GtkBorder extents;
2032 extents.top = -clip.y;
2033 extents.right = clip.width + clip.x;
2034 extents.bottom = clip.height + clip.y;
2035 extents.left = -clip.x;
2037 // Get shadow extents but combine with style margin; use the bigger value.
2038 // Margin is used for resize grip size - it's not present on
2039 // popup windows.
2040 if (!aPopupWindow) {
2041 GtkBorder margin;
2042 gtk_style_context_get_margin(context, state, &margin);
2044 extents.top = MAX(extents.top, margin.top);
2045 extents.right = MAX(extents.right, margin.right);
2046 extents.bottom = MAX(extents.bottom, margin.bottom);
2047 extents.left = MAX(extents.left, margin.left);
2050 sWindowDecorationSize->decorationSize += extents;
2053 GtkBorder GetCSDDecorationSize(bool aIsPopup) {
2054 auto metrics =
2055 aIsPopup ? &sPopupWindowDecorationSize : &sToplevelWindowDecorationSize;
2056 if (!metrics->initialized) {
2057 InitWindowDecorationSize(metrics, aIsPopup);
2058 metrics->initialized = true;
2060 return metrics->decorationSize;
2063 /* cairo_t *cr argument has to be a system-cairo. */
2064 gint moz_gtk_widget_paint(WidgetNodeType widget, cairo_t* cr,
2065 GdkRectangle* rect, GtkWidgetState* state, gint flags,
2066 GtkTextDirection direction) {
2067 /* A workaround for https://bugzilla.gnome.org/show_bug.cgi?id=694086
2069 cairo_new_path(cr);
2071 switch (widget) {
2072 case MOZ_GTK_BUTTON:
2073 case MOZ_GTK_TOOLBAR_BUTTON:
2074 if (state->depressed) {
2075 return moz_gtk_button_paint(cr, rect, state, (GtkReliefStyle)flags,
2076 GetWidget(MOZ_GTK_TOGGLE_BUTTON),
2077 direction);
2079 return moz_gtk_button_paint(cr, rect, state, (GtkReliefStyle)flags,
2080 GetWidget(MOZ_GTK_BUTTON), direction);
2081 case MOZ_GTK_HEADER_BAR_BUTTON_CLOSE:
2082 case MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE:
2083 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE:
2084 case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE:
2085 return moz_gtk_header_bar_button_paint(
2086 cr, rect, state, (GtkReliefStyle)flags, widget, direction);
2087 case MOZ_GTK_CHECKBUTTON:
2088 case MOZ_GTK_RADIOBUTTON:
2089 return moz_gtk_toggle_paint(cr, rect, state,
2090 !!(flags & MOZ_GTK_WIDGET_CHECKED),
2091 !!(flags & MOZ_GTK_WIDGET_INCONSISTENT),
2092 (widget == MOZ_GTK_RADIOBUTTON), direction);
2093 case MOZ_GTK_SCALE_HORIZONTAL:
2094 case MOZ_GTK_SCALE_VERTICAL:
2095 return moz_gtk_scale_paint(cr, rect, state, (GtkOrientation)flags,
2096 direction);
2097 case MOZ_GTK_SCALE_THUMB_HORIZONTAL:
2098 case MOZ_GTK_SCALE_THUMB_VERTICAL:
2099 return moz_gtk_scale_thumb_paint(cr, rect, state, (GtkOrientation)flags,
2100 direction);
2101 case MOZ_GTK_INNER_SPIN_BUTTON:
2102 return moz_gtk_inner_spin_paint(cr, rect, state, direction);
2103 case MOZ_GTK_SPINBUTTON:
2104 return moz_gtk_spin_paint(cr, rect, state, direction);
2105 case MOZ_GTK_SPINBUTTON_UP:
2106 case MOZ_GTK_SPINBUTTON_DOWN:
2107 return moz_gtk_spin_updown_paint(
2108 cr, rect, (widget == MOZ_GTK_SPINBUTTON_DOWN), state, direction);
2109 case MOZ_GTK_SPINBUTTON_ENTRY: {
2110 GtkStyleContext* style =
2111 GetStyleContext(MOZ_GTK_SPINBUTTON_ENTRY, state->image_scale,
2112 direction, GetStateFlagsFromGtkWidgetState(state));
2113 return moz_gtk_entry_paint(cr, rect, state, style, widget);
2115 case MOZ_GTK_TREEVIEW:
2116 return moz_gtk_treeview_paint(cr, rect, state, direction);
2117 case MOZ_GTK_TREE_HEADER_CELL:
2118 return moz_gtk_tree_header_cell_paint(cr, rect, state, flags, direction);
2119 case MOZ_GTK_TREEVIEW_EXPANDER:
2120 return moz_gtk_treeview_expander_paint(
2121 cr, rect, state, (GtkExpanderStyle)flags, direction);
2122 case MOZ_GTK_ENTRY:
2123 case MOZ_GTK_DROPDOWN_ENTRY: {
2124 GtkStyleContext* style =
2125 GetStyleContext(widget, state->image_scale, direction,
2126 GetStateFlagsFromGtkWidgetState(state));
2127 gint ret = moz_gtk_entry_paint(cr, rect, state, style, widget);
2128 return ret;
2130 case MOZ_GTK_TEXT_VIEW:
2131 return moz_gtk_text_view_paint(cr, rect, state, direction);
2132 case MOZ_GTK_DROPDOWN:
2133 return moz_gtk_combo_box_paint(cr, rect, state, direction);
2134 case MOZ_GTK_TOOLTIP:
2135 return moz_gtk_tooltip_paint(cr, rect, state, direction);
2136 case MOZ_GTK_FRAME:
2137 return moz_gtk_frame_paint(cr, rect, state, direction);
2138 case MOZ_GTK_RESIZER:
2139 return moz_gtk_resizer_paint(cr, rect, state, direction);
2140 case MOZ_GTK_PROGRESSBAR:
2141 return moz_gtk_progressbar_paint(cr, rect, state, direction);
2142 case MOZ_GTK_PROGRESS_CHUNK:
2143 case MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE:
2144 case MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE:
2145 return moz_gtk_progress_chunk_paint(cr, rect, state, direction, widget);
2146 case MOZ_GTK_TAB_TOP:
2147 case MOZ_GTK_TAB_BOTTOM:
2148 return moz_gtk_tab_paint(cr, rect, state, (GtkTabFlags)flags, direction,
2149 widget);
2150 case MOZ_GTK_TABPANELS:
2151 return moz_gtk_tabpanels_paint(cr, rect, state, direction);
2152 case MOZ_GTK_TAB_SCROLLARROW:
2153 return moz_gtk_tab_scroll_arrow_paint(cr, rect, state,
2154 (GtkArrowType)flags, direction);
2155 case MOZ_GTK_TOOLBARBUTTON_ARROW:
2156 return moz_gtk_arrow_paint(cr, rect, state, (GtkArrowType)flags,
2157 direction);
2158 case MOZ_GTK_SPLITTER_HORIZONTAL:
2159 return moz_gtk_vpaned_paint(cr, rect, state);
2160 case MOZ_GTK_SPLITTER_VERTICAL:
2161 return moz_gtk_hpaned_paint(cr, rect, state);
2162 case MOZ_GTK_WINDOW_DECORATION:
2163 return moz_gtk_window_decoration_paint(cr, rect, state, direction);
2164 case MOZ_GTK_HEADER_BAR:
2165 case MOZ_GTK_HEADER_BAR_MAXIMIZED:
2166 return moz_gtk_header_bar_paint(widget, cr, rect, state);
2167 default:
2168 g_warning("Unknown widget type: %d", widget);
2171 return MOZ_GTK_UNKNOWN_WIDGET;
2174 gint moz_gtk_shutdown() {
2175 /* This will destroy all of our widgets */
2176 ResetWidgetCache();
2178 return MOZ_GTK_SUCCESS;