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