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/. */
7 * This file contains painting functions for each of the gtk2 widgets.
8 * Adapted from the gtkdrawing.c, and gtk+2.0 source.
12 #include <gdk/gdkprivate.h>
14 #include "gtkdrawing.h"
15 #include "mozilla/Assertions.h"
16 #include "mozilla/ScopeExit.h"
18 #include "WidgetStyleCache.h"
21 #include "WidgetUtilsGtk.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
;
38 #define ARROW_DOWN G_PI
39 #define ARROW_RIGHT G_PI_2
40 #define ARROW_LEFT (G_PI + G_PI_2)
43 // It's used for debugging only to compare Gecko widget style with
44 // the ones used by Gtk+ applications.
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
));
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
;
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
) {
76 gtk_style_context_get_margin(style
, gtk_style_context_get_state(style
),
79 *right
+= margin
.right
;
81 *bottom
+= margin
.bottom
;
84 static void moz_gtk_add_style_border(GtkStyleContext
* style
, gint
* left
,
85 gint
* top
, gint
* right
, gint
* bottom
) {
88 gtk_style_context_get_border(style
, gtk_style_context_get_state(style
),
92 *right
+= border
.right
;
94 *bottom
+= border
.bottom
;
97 static void moz_gtk_add_style_padding(GtkStyleContext
* style
, gint
* left
,
98 gint
* top
, gint
* right
, gint
* bottom
) {
101 gtk_style_context_get_padding(style
, gtk_style_context_get_state(style
),
104 *left
+= padding
.left
;
105 *right
+= padding
.right
;
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
;
152 stateFlags
= GTK_STATE_FLAG_INSENSITIVE
;
154 if (state
->depressed
|| state
->active
)
156 static_cast<GtkStateFlags
>(stateFlags
| GTK_STATE_FLAG_ACTIVE
);
159 static_cast<GtkStateFlags
>(stateFlags
| GTK_STATE_FLAG_PRELIGHT
);
162 static_cast<GtkStateFlags
>(stateFlags
| GTK_STATE_FLAG_FOCUSED
);
165 static_cast<GtkStateFlags
>(stateFlags
| GTK_STATE_FLAG_BACKDROP
);
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
;
181 checkbox_check_state
= GTK_STATE_FLAG_ACTIVE
;
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", ¬ebook_has_tab_gap
,
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 */
208 gint
moz_gtk_button_get_default_overflow(gint
* border_top
, gint
* border_left
,
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
);
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
,
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
);
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
);
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
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
),
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
,
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()) {
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
) {
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)
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
,
419 gtk_render_frame(decorationStyle
, cr
, rect
->x
, rect
->y
, rect
->width
,
421 return MOZ_GTK_SUCCESS
;
424 static gint
moz_gtk_button_paint(cairo_t
* cr
, const GdkRectangle
* rect
,
425 GtkWidgetState
* state
, GtkReliefStyle relief
,
427 GtkTextDirection direction
) {
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
);
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
);
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
) {
471 gtk_style_context_get_border(style
, state_flags
, &border
);
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
);
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
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
)));
507 return MOZ_GTK_UNKNOWN_WIDGET
;
509 cairo_surface_t
* surface
=
510 GetWidgetIconSurface(iconWidget
, state
->image_scale
);
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
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.
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
;
560 static_cast<GtkStateFlags
>(state_flags
| checkbox_check_state
);
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
;
582 gtk_render_option(style
, cr
, indicator_x
, indicator_y
, indicator_width
,
585 gtk_render_check(style
, cr
, indicator_x
, indicator_y
, indicator_width
,
590 gtk_render_option(style
, cr
, x
, y
, width
, height
);
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
;
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
),
612 gtk_style_context_get_padding(style
, gtk_style_context_get_state(style
),
615 inner_rect
->x
= rect
->x
+ border
.left
+ padding
.left
;
616 inner_rect
->y
= rect
->y
+ padding
.top
+ border
.top
;
618 MAX(1, rect
->width
- padding
.left
- padding
.right
- border
.left
* 2);
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;
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)) *
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
,
663 GtkStateFlags state_flags
= gtk_style_context_get_state(style
);
664 gtk_style_context_get(style
, state_flags
, "min-height", height
, "min-width",
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
) {
688 gtk_style_context_get_margin(style
, gtk_style_context_get_state(style
),
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
);
704 static void moz_gtk_draw_styled_frame(GtkStyleContext
* style
, cairo_t
* cr
,
705 const GdkRectangle
* aRect
,
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
);
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
;
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
,
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
,
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
;
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;
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
);
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
835 if (flags
== GTK_ORIENTATION_HORIZONTAL
) {
836 moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_HORIZONTAL
, &thumb_width
,
839 y
= rect
->y
+ (rect
->height
- thumb_height
) / 2;
841 moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_VERTICAL
, &thumb_height
,
843 x
= rect
->x
+ (rect
->width
- thumb_width
) / 2;
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
,
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
917 style
= GetStyleContext(MOZ_GTK_TEXT_VIEW_TEXT
, state
->image_scale
, direction
,
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
;
933 /* only handle disabled and normal states, otherwise the whole background
934 * area will be painted differently with other states */
936 state
->disabled
? GTK_STATE_FLAG_INSENSITIVE
: GTK_STATE_FLAG_NORMAL
;
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
;
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
);
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
,
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
);
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
;
1007 arrow_rect
.x
+= arrow_rect
.width
;
1009 gtk_render_frame(style
, cr
, arrow_rect
.x
, arrow_rect
.y
, separator_width
,
1012 if (direction
== GTK_TEXT_DIR_LTR
) {
1014 GtkStateFlags state_flags
= GetStateFlagsFromGtkWidgetState(state
);
1015 gtk_style_context_get_padding(style
, state_flags
, &padding
);
1016 arrow_rect
.x
-= padding
.left
;
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
;
1043 case GTK_ARROW_RIGHT
:
1044 arrow_angle
= ARROW_RIGHT
;
1046 case GTK_ARROW_DOWN
:
1047 arrow_angle
= ARROW_DOWN
;
1050 arrow_angle
= ARROW_UP
;
1053 if (arrow_type
== GTK_ARROW_NONE
) return MOZ_GTK_SUCCESS
;
1055 GtkWidget
* widget
= GetWidget(MOZ_GTK_BUTTON_ARROW
);
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
,
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:
1074 // Icon (not supported by Firefox)
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.
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
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
);
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
);
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.
1126 if (direction
== GTK_TEXT_DIR_RTL
) {
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
);
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.
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
;
1193 rect
->y
+= (dx
< travel
/ 2) ? dx
: travel
- dx
;
1194 rect
->height
= barSize
;
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 */
1212 gtk_style_context_get_border(style
, gtk_style_context_get_state(style
),
1214 if (border
.top
< 2) return 2; /* some themes don't set ythickness correctly */
1219 gint
moz_gtk_get_tab_thickness(WidgetNodeType aNodeType
) {
1220 GtkStyleContext
* style
= GetStyleContext(aNodeType
);
1221 int thickness
= moz_gtk_get_tab_thickness(style
);
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
));
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
,
1262 isBottomTab
? GTK_POS_TOP
: GTK_POS_BOTTOM
);
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 * ___________________________
1280 * ----------|. . . . . . . . . . . . . . .|----- top of tabpanel
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
;
1315 gap_loffset
= initial_gap
;
1318 GtkStyleContext
* panelStyle
=
1319 GetStyleContext(MOZ_GTK_TABPANELS
, state
->image_scale
, direction
);
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
,
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
);
1338 cairo_rectangle(cr
, backRect
.x
, backRect
.y
, backRect
.width
,
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
);
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
);
1363 cairo_rectangle(cr
, backRect
.x
, backRect
.y
, backRect
.width
,
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
);
1376 gtk_render_background(style
, cr
, tabRect
.x
, tabRect
.y
, tabRect
.width
,
1378 gtk_render_frame(style
, cr
, tabRect
.x
, tabRect
.y
, tabRect
.width
,
1382 if (state
->focused
) {
1383 /* Paint the focus ring */
1385 gtk_style_context_get_padding(style
, GetStateFlagsFromGtkWidgetState(state
),
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
,
1397 return MOZ_GTK_SUCCESS
;
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.
1419 cairo_rectangle(cr
, rect
->x
, rect
->y
, rect
->x
+ rect
->width
/ 2,
1420 rect
->y
+ rect
->height
);
1422 gtk_render_frame_gap(style
, cr
, rect
->x
, rect
->y
, rect
->width
, rect
->height
,
1423 GTK_POS_TOP
, rect
->width
- 1, rect
->width
);
1428 cairo_rectangle(cr
, rect
->x
+ rect
->width
/ 2, rect
->y
, rect
->x
+ rect
->width
,
1429 rect
->y
+ rect
->height
);
1431 gtk_render_frame_gap(style
, cr
, rect
->x
, rect
->y
, rect
->width
, rect
->height
,
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
) {
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
;
1456 case GTK_ARROW_RIGHT
:
1457 arrow_angle
= ARROW_RIGHT
;
1459 case GTK_ARROW_DOWN
:
1460 arrow_angle
= ARROW_DOWN
;
1463 arrow_angle
= ARROW_UP
;
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
,
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
) {
1503 GtkStyleContext
* style
;
1504 *left
= *top
= *right
= *bottom
= 0;
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
;
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
;
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
,
1575 moz_gtk_sanity_preferred_size(&arrow_req
);
1577 if (direction
== GTK_TEXT_DIR_RTL
)
1578 *left
+= separator_width
+ arrow_req
.width
;
1580 *right
+= separator_width
+ arrow_req
.width
;
1582 return MOZ_GTK_SUCCESS
;
1584 case MOZ_GTK_TABPANELS
:
1585 w
= GetWidget(MOZ_GTK_TABPANELS
);
1587 case MOZ_GTK_PROGRESSBAR
:
1588 w
= GetWidget(MOZ_GTK_PROGRESSBAR
);
1590 case MOZ_GTK_SPINBUTTON_ENTRY
:
1591 case MOZ_GTK_SPINBUTTON_UP
:
1592 case MOZ_GTK_SPINBUTTON_DOWN
:
1593 w
= GetWidget(MOZ_GTK_SPINBUTTON
);
1595 case MOZ_GTK_SCALE_HORIZONTAL
:
1596 case MOZ_GTK_SCALE_VERTICAL
:
1597 w
= GetWidget(widget
);
1600 w
= GetWidget(MOZ_GTK_FRAME
);
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
;
1645 g_warning("Unsupported widget type: %d", widget
);
1646 return MOZ_GTK_UNKNOWN_WIDGET
;
1648 /* TODO - we're still missing some widget implementations */
1650 moz_gtk_add_style_border(gtk_widget_get_style_context(w
), left
, top
, right
,
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) {
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
;
1679 *left
+= initial_gap
;
1684 gtk_style_context_get_margin(style
, gtk_style_context_get_state(style
),
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
),
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
) {
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
,
1715 switch (widgetType
) {
1716 case MOZ_GTK_DROPDOWN
:
1717 widget
= GetWidget(MOZ_GTK_COMBOBOX_ARROW
);
1720 widget
= GetWidget(MOZ_GTK_BUTTON_ARROW
);
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
;
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);
1743 *min_content_height
= 0;
1748 gtk_style_context_get_border(style
, gtk_style_context_get_state(style
),
1750 gtk_style_context_get_padding(style
, gtk_style_context_get_state(style
),
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;
1774 *scale_width
= thumb_height
+ trough_border
* 2;
1775 *scale_height
= thumb_length
+ trough_border
* 2;
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
,
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
);
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);
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
;
1829 case MOZ_GTK_CHECKBUTTON
:
1830 metrics
= &sCheckboxMetrics
;
1833 MOZ_CRASH("Unsupported widget type for getting metrics");
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
;
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
=
1881 * get_shadow_width() from gtkwindow.c is not public so we need
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};
1894 // Scale factor is applied later when decoration size is used for actual
1896 GtkStyleContext
* context
= GetStyleContext(MOZ_GTK_WINDOW_DECORATION
);
1898 /* Always sum border + 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
) {
1916 sGtkRenderBackgroundGetClip(context
, 0, 0, 0, 0, &clip
);
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
1927 if (!aPopupWindow
) {
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
) {
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
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
),
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
,
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
,
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
);
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
);
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
);
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
,
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
,
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
);
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 */
2060 return MOZ_GTK_SUCCESS
;