Source code

Revision control

Other Tools

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/*
* This file contains painting functions for each of the gtk2 widgets.
* Adapted from the gtkdrawing.c, and gtk+2.0 source.
*/
#include <gtk/gtk.h>
#include <gdk/gdkprivate.h>
#include <string.h>
#include "gtkdrawing.h"
#include "mozilla/Assertions.h"
#include "mozilla/ScopeExit.h"
#include "prinrval.h"
#include "WidgetStyleCache.h"
#include "nsString.h"
#include "nsDebug.h"
#include <math.h>
#include <dlfcn.h>
static gboolean checkbox_check_state;
static gboolean notebook_has_tab_gap;
static ScrollbarGTKMetrics sScrollbarMetrics[2];
static ScrollbarGTKMetrics sActiveScrollbarMetrics[2];
static ToggleGTKMetrics sCheckboxMetrics;
static ToggleGTKMetrics sRadioMetrics;
static ToggleGTKMetrics sMenuRadioMetrics;
static ToggleGTKMetrics sMenuCheckboxMetrics;
static ToolbarGTKMetrics sToolbarMetrics;
static CSDWindowDecorationSize sToplevelWindowDecorationSize;
static CSDWindowDecorationSize sPopupWindowDecorationSize;
using mozilla::Span;
#define ARROW_UP 0
#define ARROW_DOWN G_PI
#define ARROW_RIGHT G_PI_2
#define ARROW_LEFT (G_PI + G_PI_2)
#if 0
// It's used for debugging only to compare Gecko widget style with
// the ones used by Gtk+ applications.
static void
style_path_print(GtkStyleContext *context)
{
const GtkWidgetPath* path = gtk_style_context_get_path(context);
static auto sGtkWidgetPathToStringPtr =
(char * (*)(const GtkWidgetPath *))
dlsym(RTLD_DEFAULT, "gtk_widget_path_to_string");
fprintf(stderr, "Style path:\n%s\n\n", sGtkWidgetPathToStringPtr(path));
}
#endif
static GtkBorder operator-(const GtkBorder& first, const GtkBorder& second) {
GtkBorder result;
result.left = first.left - second.left;
result.right = first.right - second.right;
result.top = first.top - second.top;
result.bottom = first.bottom - second.bottom;
return result;
}
static GtkBorder operator+(const GtkBorder& first, const GtkBorder& second) {
GtkBorder result;
result.left = first.left + second.left;
result.right = first.right + second.right;
result.top = first.top + second.top;
result.bottom = first.bottom + second.bottom;
return result;
}
static GtkBorder operator+=(GtkBorder& first, const GtkBorder& second) {
first.left += second.left;
first.right += second.right;
first.top += second.top;
first.bottom += second.bottom;
return first;
}
static gint moz_gtk_get_tab_thickness(GtkStyleContext* style);
static gint moz_gtk_menu_item_paint(WidgetNodeType widget, cairo_t* cr,
GdkRectangle* rect, GtkWidgetState* state,
GtkTextDirection direction);
static GtkBorder GetMarginBorderPadding(GtkStyleContext* aStyle);
static void Inset(GdkRectangle*, const GtkBorder&);
static void InsetByMargin(GdkRectangle*, GtkStyleContext* style);
static void moz_gtk_add_style_margin(GtkStyleContext* style, gint* left,
gint* top, gint* right, gint* bottom) {
GtkBorder margin;
gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
&margin);
*left += margin.left;
*right += margin.right;
*top += margin.top;
*bottom += margin.bottom;
}
static void moz_gtk_add_style_border(GtkStyleContext* style, gint* left,
gint* top, gint* right, gint* bottom) {
GtkBorder border;
gtk_style_context_get_border(style, gtk_style_context_get_state(style),
&border);
*left += border.left;
*right += border.right;
*top += border.top;
*bottom += border.bottom;
}
static void moz_gtk_add_style_padding(GtkStyleContext* style, gint* left,
gint* top, gint* right, gint* bottom) {
GtkBorder padding;
gtk_style_context_get_padding(style, gtk_style_context_get_state(style),
&padding);
*left += padding.left;
*right += padding.right;
*top += padding.top;
*bottom += padding.bottom;
}
static void moz_gtk_add_margin_border_padding(GtkStyleContext* style,
gint* left, gint* top,
gint* right, gint* bottom) {
moz_gtk_add_style_margin(style, left, top, right, bottom);
moz_gtk_add_style_border(style, left, top, right, bottom);
moz_gtk_add_style_padding(style, left, top, right, bottom);
}
static void moz_gtk_add_border_padding(GtkStyleContext* style, gint* left,
gint* top, gint* right, gint* bottom) {
moz_gtk_add_style_border(style, left, top, right, bottom);
moz_gtk_add_style_padding(style, left, top, right, bottom);
}
// In case there's an error in Gtk theme and preferred size is zero,
// return some sane values to pass mozilla automation tests.
// It should not happen in real-life.
#define MIN_WIDGET_SIZE 10
static void moz_gtk_sanity_preferred_size(GtkRequisition* requisition) {
if (requisition->width <= 0) {
requisition->width = MIN_WIDGET_SIZE;
}
if (requisition->height <= 0) {
requisition->height = MIN_WIDGET_SIZE;
}
}
// GetStateFlagsFromGtkWidgetState() can be safely used for the specific
// GtkWidgets that set both prelight and active flags. For other widgets,
// either the GtkStateFlags or Gecko's GtkWidgetState need to be carefully
// adjusted to match GTK behavior. Although GTK sets insensitive and focus
// flags in the generic GtkWidget base class, GTK adds prelight and active
// flags only to widgets that are expected to demonstrate prelight or active
// states. This contrasts with HTML where any element may have :active and
// :hover states, and so Gecko's GtkStateFlags do not necessarily map to GTK
// flags. Failure to restrict the flags in the same way as GTK can cause
// generic CSS selectors from some themes to unintentionally match elements
// that are not expected to change appearance on hover or mouse-down.
static GtkStateFlags GetStateFlagsFromGtkWidgetState(GtkWidgetState* state) {
GtkStateFlags stateFlags = GTK_STATE_FLAG_NORMAL;
if (state->disabled)
stateFlags = GTK_STATE_FLAG_INSENSITIVE;
else {
if (state->depressed || state->active)
stateFlags =
static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_ACTIVE);
if (state->inHover)
stateFlags =
static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_PRELIGHT);
if (state->focused)
stateFlags =
static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_FOCUSED);
if (state->backdrop)
stateFlags =
static_cast<GtkStateFlags>(stateFlags | GTK_STATE_FLAG_BACKDROP);
}
return stateFlags;
}
static GtkStateFlags GetStateFlagsFromGtkTabFlags(GtkTabFlags flags) {
return ((flags & MOZ_GTK_TAB_SELECTED) == 0) ? GTK_STATE_FLAG_NORMAL
: GTK_STATE_FLAG_ACTIVE;
}
gint moz_gtk_init() {
if (gtk_major_version > 3 ||
(gtk_major_version == 3 && gtk_minor_version >= 14))
checkbox_check_state = GTK_STATE_FLAG_CHECKED;
else
checkbox_check_state = GTK_STATE_FLAG_ACTIVE;
moz_gtk_refresh();
return MOZ_GTK_SUCCESS;
}
void moz_gtk_refresh() {
if (gtk_check_version(3, 20, 0) != nullptr) {
// Deprecated for Gtk >= 3.20+
GtkStyleContext* style = GetStyleContext(MOZ_GTK_TAB_TOP);
gtk_style_context_get_style(style, "has-tab-gap", &notebook_has_tab_gap,
NULL);
} else {
notebook_has_tab_gap = true;
}
sScrollbarMetrics[GTK_ORIENTATION_HORIZONTAL].initialized = false;
sScrollbarMetrics[GTK_ORIENTATION_VERTICAL].initialized = false;
sActiveScrollbarMetrics[GTK_ORIENTATION_HORIZONTAL].initialized = false;
sActiveScrollbarMetrics[GTK_ORIENTATION_VERTICAL].initialized = false;
sCheckboxMetrics.initialized = false;
sRadioMetrics.initialized = false;
sMenuCheckboxMetrics.initialized = false;
sMenuRadioMetrics.initialized = false;
sToolbarMetrics.initialized = false;
sToplevelWindowDecorationSize.initialized = false;
sPopupWindowDecorationSize.initialized = false;
/* This will destroy all of our widgets */
ResetWidgetCache();
}
static gint moz_gtk_get_focus_outline_size(GtkStyleContext* style,
gint* focus_h_width,
gint* focus_v_width) {
GtkBorder border;
gtk_style_context_get_border(style, gtk_style_context_get_state(style),
&border);
*focus_h_width = border.left;
*focus_v_width = border.top;
return MOZ_GTK_SUCCESS;
}
gint moz_gtk_get_focus_outline_size(gint* focus_h_width, gint* focus_v_width) {
GtkStyleContext* style = GetStyleContext(MOZ_GTK_ENTRY);
moz_gtk_get_focus_outline_size(style, focus_h_width, focus_v_width);
return MOZ_GTK_SUCCESS;
}
gint moz_gtk_menuitem_get_horizontal_padding(gint* horizontal_padding) {
GtkStyleContext* style = GetStyleContext(MOZ_GTK_MENUITEM);
gtk_style_context_get_style(style, "horizontal-padding", horizontal_padding,
nullptr);
return MOZ_GTK_SUCCESS;
}
gint moz_gtk_checkmenuitem_get_horizontal_padding(gint* horizontal_padding) {
GtkStyleContext* style = GetStyleContext(MOZ_GTK_CHECKMENUITEM);
gtk_style_context_get_style(style, "horizontal-padding", horizontal_padding,
nullptr);
return MOZ_GTK_SUCCESS;
}
gint moz_gtk_button_get_default_overflow(gint* border_top, gint* border_left,
gint* border_bottom,
gint* border_right) {
GtkBorder* default_outside_border;
GtkStyleContext* style = GetStyleContext(MOZ_GTK_BUTTON);
gtk_style_context_get_style(style, "default-outside-border",
&default_outside_border, NULL);
if (default_outside_border) {
*border_top = default_outside_border->top;
*border_left = default_outside_border->left;
*border_bottom = default_outside_border->bottom;
*border_right = default_outside_border->right;
gtk_border_free(default_outside_border);
} else {
*border_top = *border_left = *border_bottom = *border_right = 0;
}
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_button_get_default_border(gint* border_top,
gint* border_left,
gint* border_bottom,
gint* border_right) {
GtkBorder* default_border;
GtkStyleContext* style = GetStyleContext(MOZ_GTK_BUTTON);
gtk_style_context_get_style(style, "default-border", &default_border, NULL);
if (default_border) {
*border_top = default_border->top;
*border_left = default_border->left;
*border_bottom = default_border->bottom;
*border_right = default_border->right;
gtk_border_free(default_border);
} else {
/* see gtkbutton.c */
*border_top = *border_left = *border_bottom = *border_right = 1;
}
return MOZ_GTK_SUCCESS;
}
gint moz_gtk_splitter_get_metrics(gint orientation, gint* size) {
GtkStyleContext* style;
if (orientation == GTK_ORIENTATION_HORIZONTAL) {
style = GetStyleContext(MOZ_GTK_SPLITTER_HORIZONTAL);
} else {
style = GetStyleContext(MOZ_GTK_SPLITTER_VERTICAL);
}
gtk_style_context_get_style(style, "handle_size", size, NULL);
return MOZ_GTK_SUCCESS;
}
static void CalculateToolbarButtonMetrics(WidgetNodeType aAppearance,
ToolbarButtonGTKMetrics* aMetrics) {
gint iconWidth, iconHeight;
if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &iconWidth, &iconHeight)) {
NS_WARNING("Failed to get Gtk+ icon size for titlebar button!");
// Use some reasonable fallback size
iconWidth = 16;
iconHeight = 16;
}
GtkStyleContext* style = GetStyleContext(aAppearance);
gint width = 0, height = 0;
if (gtk_check_version(3, 20, 0) == nullptr) {
gtk_style_context_get(style, gtk_style_context_get_state(style),
"min-width", &width, "min-height", &height, NULL);
}
// Cover cases when min-width/min-height is not set, it's invalid
// or we're running on Gtk+ < 3.20.
if (width < iconWidth) width = iconWidth;
if (height < iconHeight) height = iconHeight;
gint left = 0, top = 0, right = 0, bottom = 0;
moz_gtk_add_border_padding(style, &left, &top, &right, &bottom);
// Button size is calculated as min-width/height + border/padding.
width += left + right;
height += top + bottom;
// Place icon at button center.
aMetrics->iconXPosition = (width - iconWidth) / 2;
aMetrics->iconYPosition = (height - iconHeight) / 2;
aMetrics->minSizeWithBorderMargin.width = width;
aMetrics->minSizeWithBorderMargin.height = height;
}
// We support LTR layout only here for now.
static void CalculateToolbarButtonSpacing(WidgetNodeType aAppearance,
ToolbarButtonGTKMetrics* aMetrics) {
GtkStyleContext* style = GetStyleContext(aAppearance);
gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
&aMetrics->buttonMargin);
// Get titlebar spacing, a default one is 6 pixels (gtk/gtkheaderbar.c)
gint buttonSpacing = 6;
g_object_get(GetWidget(MOZ_GTK_HEADER_BAR), "spacing", &buttonSpacing,
nullptr);
// We apply spacing as a margin equally to both adjacent buttons.
buttonSpacing /= 2;
if (!aMetrics->firstButton) {
aMetrics->buttonMargin.left += buttonSpacing;
}
if (!aMetrics->lastButton) {
aMetrics->buttonMargin.right += buttonSpacing;
}
aMetrics->minSizeWithBorderMargin.width +=
aMetrics->buttonMargin.right + aMetrics->buttonMargin.left;
aMetrics->minSizeWithBorderMargin.height +=
aMetrics->buttonMargin.top + aMetrics->buttonMargin.bottom;
}
size_t GetGtkHeaderBarButtonLayout(Span<ButtonLayout> aButtonLayout,
bool* aReversedButtonsPlacement) {
gchar* decorationLayoutSetting = nullptr;
GtkSettings* settings = gtk_settings_get_default();
g_object_get(settings, "gtk-decoration-layout", &decorationLayoutSetting,
nullptr);
auto free = mozilla::MakeScopeExit([&] { g_free(decorationLayoutSetting); });
// Use a default layout
const gchar* decorationLayout = "menu:minimize,maximize,close";
if (decorationLayoutSetting) {
decorationLayout = decorationLayoutSetting;
}
// "minimize,maximize,close:" layout means buttons are on the opposite
// titlebar side. close button is always there.
if (aReversedButtonsPlacement) {
const char* closeButton = strstr(decorationLayout, "close");
const char* separator = strchr(decorationLayout, ':');
*aReversedButtonsPlacement =
closeButton && separator && closeButton < separator;
}
// We check what position a button string is stored in decorationLayout.
//
// decorationLayout gets its value from the GNOME preference:
// org.gnome.desktop.vm.preferences.button-layout via the
// gtk-decoration-layout property.
//
// Documentation of the gtk-decoration-layout property can be found here:
if (aButtonLayout.IsEmpty()) {
return 0;
}
nsDependentCSubstring layout(decorationLayout, strlen(decorationLayout));
bool right = false;
size_t activeButtons = 0;
for (const auto& part : layout.Split(':')) {
for (const auto& button : part.Split(',')) {
if (button.EqualsLiteral("close")) {
aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_CLOSE,
right};
} else if (button.EqualsLiteral("minimize")) {
aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE,
right};
} else if (button.EqualsLiteral("maximize")) {
aButtonLayout[activeButtons++] = {MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE,
right};
}
if (activeButtons == aButtonLayout.Length()) {
return activeButtons;
}
}
right = true;
}
return activeButtons;
}
static void EnsureToolbarMetrics(void) {
if (!sToolbarMetrics.initialized) {
// Make sure we have clean cache after theme reset, etc.
memset(&sToolbarMetrics, 0, sizeof(sToolbarMetrics));
// Calculate titlebar button visibility and positions.
ButtonLayout aButtonLayout[TOOLBAR_BUTTONS];
size_t activeButtonNums =
GetGtkHeaderBarButtonLayout(mozilla::Span(aButtonLayout), nullptr);
for (size_t i = 0; i < activeButtonNums; i++) {
int buttonIndex =
(aButtonLayout[i].mType - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE);
ToolbarButtonGTKMetrics* metrics = sToolbarMetrics.button + buttonIndex;
metrics->visible = true;
// Mark first button
if (!i) {
metrics->firstButton = true;
}
// Mark last button.
if (i == (activeButtonNums - 1)) {
metrics->lastButton = true;
}
CalculateToolbarButtonMetrics(aButtonLayout[i].mType, metrics);
CalculateToolbarButtonSpacing(aButtonLayout[i].mType, metrics);
}
sToolbarMetrics.initialized = true;
}
}
const ToolbarButtonGTKMetrics* GetToolbarButtonMetrics(
WidgetNodeType aAppearance) {
EnsureToolbarMetrics();
int buttonIndex = (aAppearance - MOZ_GTK_HEADER_BAR_BUTTON_CLOSE);
NS_ASSERTION(buttonIndex >= 0 && buttonIndex <= TOOLBAR_BUTTONS,
"GetToolbarButtonMetrics(): Wrong titlebar button!");
return sToolbarMetrics.button + buttonIndex;
}
static gint moz_gtk_window_paint(cairo_t* cr, GdkRectangle* rect,
GtkTextDirection direction) {
GtkStyleContext* style = GetStyleContext(MOZ_GTK_WINDOW, direction);
gtk_style_context_save(style);
gtk_style_context_add_class(style, GTK_STYLE_CLASS_BACKGROUND);
gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
gtk_style_context_restore(style);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_button_paint(cairo_t* cr, const GdkRectangle* rect,
GtkWidgetState* state, GtkReliefStyle relief,
GtkWidget* widget,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
GtkStyleContext* style = gtk_widget_get_style_context(widget);
gint x = rect->x, y = rect->y, width = rect->width, height = rect->height;
gtk_widget_set_direction(widget, direction);
gtk_style_context_save(style);
StyleContextSetScale(style, state->scale);
gtk_style_context_set_state(style, state_flags);
if (state->isDefault && relief == GTK_RELIEF_NORMAL && !state->focused &&
!(state_flags & GTK_STATE_FLAG_PRELIGHT)) {
/* handle default borders both outside and inside the button */
gint default_top, default_left, default_bottom, default_right;
moz_gtk_button_get_default_overflow(&default_top, &default_left,
&default_bottom, &default_right);
x -= default_left;
y -= default_top;
width += default_left + default_right;
height += default_top + default_bottom;
gtk_render_background(style, cr, x, y, width, height);
gtk_render_frame(style, cr, x, y, width, height);
moz_gtk_button_get_default_border(&default_top, &default_left,
&default_bottom, &default_right);
x += default_left;
y += default_top;
width -= (default_left + default_right);
height -= (default_top + default_bottom);
} else if (relief != GTK_RELIEF_NONE || state->depressed ||
(state_flags & GTK_STATE_FLAG_PRELIGHT)) {
/* the following line can trigger an assertion (Crux theme)
file ../../gdk/gdkwindow.c: line 1846 (gdk_window_clear_area):
assertion `GDK_IS_WINDOW (window)' failed */
gtk_render_background(style, cr, x, y, width, height);
gtk_render_frame(style, cr, x, y, width, height);
}
if (state->focused) {
GtkBorder border;
gtk_style_context_get_border(style, state_flags, &border);
x += border.left;
y += border.top;
width -= (border.left + border.right);
height -= (border.top + border.bottom);
gtk_render_focus(style, cr, x, y, width, height);
}
gtk_style_context_restore(style);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_header_bar_button_paint(cairo_t* cr,
const GdkRectangle* aRect,
GtkWidgetState* state,
GtkReliefStyle relief,
WidgetNodeType aIconWidgetType,
GtkTextDirection direction) {
WidgetNodeType buttonWidgetType =
(aIconWidgetType == MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE)
? MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE
: aIconWidgetType;
GdkRectangle rect = *aRect;
// We need to inset our calculated margin because it also
// contains titlebar button spacing.
const ToolbarButtonGTKMetrics* metrics =
GetToolbarButtonMetrics(buttonWidgetType);
Inset(&rect, metrics->buttonMargin);
GtkWidget* buttonWidget = GetWidget(buttonWidgetType);
moz_gtk_button_paint(cr, &rect, state, relief, buttonWidget, direction);
GtkWidget* iconWidget =
gtk_bin_get_child(GTK_BIN(GetWidget(aIconWidgetType)));
cairo_surface_t* surface = GetWidgetIconSurface(iconWidget, state->scale);
if (surface) {
GtkStyleContext* style = gtk_widget_get_style_context(buttonWidget);
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
gtk_style_context_save(style);
StyleContextSetScale(style, state->scale);
gtk_style_context_set_state(style, state_flags);
const ToolbarButtonGTKMetrics* metrics =
GetToolbarButtonMetrics(buttonWidgetType);
/* This is available since Gtk+ 3.10 as well as GtkHeaderBar */
gtk_render_icon_surface(style, cr, surface, rect.x + metrics->iconXPosition,
rect.y + metrics->iconYPosition);
gtk_style_context_restore(style);
}
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_toggle_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state, gboolean selected,
gboolean inconsistent, gboolean isradio,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
gint x, y, width, height;
GtkStyleContext* style;
// We need to call this before GetStyleContext, because otherwise we would
// reset state flags
const ToggleGTKMetrics* metrics =
GetToggleMetrics(isradio ? MOZ_GTK_RADIOBUTTON : MOZ_GTK_CHECKBUTTON);
// Clamp the rect and paint it center aligned in the rect.
x = rect->x;
y = rect->y;
width = rect->width;
height = rect->height;
if (rect->width < rect->height) {
y = rect->y + (rect->height - rect->width) / 2;
height = rect->width;
}
if (rect->height < rect->width) {
x = rect->x + (rect->width - rect->height) / 2;
width = rect->height;
}
if (selected)
state_flags =
static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
if (inconsistent)
state_flags =
static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_INCONSISTENT);
style = GetStyleContext(isradio ? MOZ_GTK_RADIOBUTTON : MOZ_GTK_CHECKBUTTON,
state->scale, direction, state_flags);
if (gtk_check_version(3, 20, 0) == nullptr) {
gtk_render_background(style, cr, x, y, width, height);
gtk_render_frame(style, cr, x, y, width, height);
// Indicator is inset by the toggle's padding and border.
gint indicator_x = x + metrics->borderAndPadding.left;
gint indicator_y = y + metrics->borderAndPadding.top;
gint indicator_width = metrics->minSizeWithBorder.width -
metrics->borderAndPadding.left -
metrics->borderAndPadding.right;
gint indicator_height = metrics->minSizeWithBorder.height -
metrics->borderAndPadding.top -
metrics->borderAndPadding.bottom;
if (isradio) {
gtk_render_option(style, cr, indicator_x, indicator_y, indicator_width,
indicator_height);
} else {
gtk_render_check(style, cr, indicator_x, indicator_y, indicator_width,
indicator_height);
}
} else {
if (isradio) {
gtk_render_option(style, cr, x, y, width, height);
} else {
gtk_render_check(style, cr, x, y, width, height);
}
}
return MOZ_GTK_SUCCESS;
}
static gint calculate_button_inner_rect(GtkWidget* button,
const GdkRectangle* rect,
GdkRectangle* inner_rect,
GtkTextDirection direction) {
GtkStyleContext* style;
GtkBorder border;
GtkBorder padding = {0, 0, 0, 0};
style = gtk_widget_get_style_context(button);
/* This mirrors gtkbutton's child positioning */
gtk_style_context_get_border(style, gtk_style_context_get_state(style),
&border);
gtk_style_context_get_padding(style, gtk_style_context_get_state(style),
&padding);
inner_rect->x = rect->x + border.left + padding.left;
inner_rect->y = rect->y + padding.top + border.top;
inner_rect->width =
MAX(1, rect->width - padding.left - padding.right - border.left * 2);
inner_rect->height =
MAX(1, rect->height - padding.top - padding.bottom - border.top * 2);
return MOZ_GTK_SUCCESS;
}
static gint calculate_arrow_rect(GtkWidget* arrow, GdkRectangle* rect,
GdkRectangle* arrow_rect,
GtkTextDirection direction) {
/* defined in gtkarrow.c */
gfloat arrow_scaling = 0.7;
gfloat xalign, xpad;
gint extent;
gint mxpad, mypad;
gfloat mxalign, myalign;
GtkMisc* misc = GTK_MISC(arrow);
gtk_style_context_get_style(gtk_widget_get_style_context(arrow),
"arrow_scaling", &arrow_scaling, NULL);
gtk_misc_get_padding(misc, &mxpad, &mypad);
extent = MIN((rect->width - mxpad * 2), (rect->height - mypad * 2)) *
arrow_scaling;
gtk_misc_get_alignment(misc, &mxalign, &myalign);
xalign = direction == GTK_TEXT_DIR_LTR ? mxalign : 1.0 - mxalign;
xpad = mxpad + (rect->width - extent) * xalign;
arrow_rect->x = direction == GTK_TEXT_DIR_LTR ? floor(rect->x + xpad)
: ceil(rect->x + xpad);
arrow_rect->y = floor(rect->y + mypad + ((rect->height - extent) * myalign));
arrow_rect->width = arrow_rect->height = extent;
return MOZ_GTK_SUCCESS;
}
static MozGtkSize GetMinContentBox(GtkStyleContext* style) {
GtkStateFlags state_flags = gtk_style_context_get_state(style);
gint width, height;
gtk_style_context_get(style, state_flags, "min-width", &width, "min-height",
&height, nullptr);
return {width, height};
}
/**
* Get minimum widget size as sum of margin, padding, border and
* min-width/min-height.
*/
static void moz_gtk_get_widget_min_size(GtkStyleContext* style, int* width,
int* height) {
GtkStateFlags state_flags = gtk_style_context_get_state(style);
gtk_style_context_get(style, state_flags, "min-height", height, "min-width",
width, nullptr);
GtkBorder border, padding, margin;
gtk_style_context_get_border(style, state_flags, &border);
gtk_style_context_get_padding(style, state_flags, &padding);
gtk_style_context_get_margin(style, state_flags, &margin);
*width += border.left + border.right + margin.left + margin.right +
padding.left + padding.right;
*height += border.top + border.bottom + margin.top + margin.bottom +
padding.top + padding.bottom;
}
static MozGtkSize GetMinMarginBox(GtkStyleContext* style) {
gint width, height;
moz_gtk_get_widget_min_size(style, &width, &height);
return {width, height};
}
static void Inset(GdkRectangle* rect, const GtkBorder& aBorder) {
rect->x += aBorder.left;
rect->y += aBorder.top;
rect->width -= aBorder.left + aBorder.right;
rect->height -= aBorder.top + aBorder.bottom;
}
// Inset a rectangle by the margins specified in a style context.
static void InsetByMargin(GdkRectangle* rect, GtkStyleContext* style) {
GtkBorder margin;
gtk_style_context_get_margin(style, gtk_style_context_get_state(style),
&margin);
Inset(rect, margin);
}
// Inset a rectangle by the border and padding specified in a style context.
static void InsetByBorderPadding(GdkRectangle* rect, GtkStyleContext* style) {
GtkStateFlags state = gtk_style_context_get_state(style);
GtkBorder padding, border;
gtk_style_context_get_padding(style, state, &padding);
Inset(rect, padding);
gtk_style_context_get_border(style, state, &border);
Inset(rect, border);
}
static gint moz_gtk_scrollbar_button_paint(cairo_t* cr,
const GdkRectangle* aRect,
GtkWidgetState* state,
GtkScrollbarButtonFlags flags,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
GdkRectangle arrow_rect;
gdouble arrow_angle;
GtkStyleContext* style;
gint arrow_displacement_x, arrow_displacement_y;
GtkWidget* scrollbar = GetWidget(flags & MOZ_GTK_STEPPER_VERTICAL
? MOZ_GTK_SCROLLBAR_VERTICAL
: MOZ_GTK_SCROLLBAR_HORIZONTAL);
gtk_widget_set_direction(scrollbar, direction);
if (flags & MOZ_GTK_STEPPER_VERTICAL) {
arrow_angle = (flags & MOZ_GTK_STEPPER_DOWN) ? ARROW_DOWN : ARROW_UP;
} else {
arrow_angle = (flags & MOZ_GTK_STEPPER_DOWN) ? ARROW_RIGHT : ARROW_LEFT;
}
style = gtk_widget_get_style_context(scrollbar);
gtk_style_context_save(style);
gtk_style_context_add_class(style, GTK_STYLE_CLASS_BUTTON);
StyleContextSetScale(style, state->scale);
gtk_style_context_set_state(style, state_flags);
if (arrow_angle == ARROW_RIGHT) {
gtk_style_context_add_class(style, GTK_STYLE_CLASS_RIGHT);
} else if (arrow_angle == ARROW_DOWN) {
gtk_style_context_add_class(style, GTK_STYLE_CLASS_BOTTOM);
} else if (arrow_angle == ARROW_LEFT) {
gtk_style_context_add_class(style, GTK_STYLE_CLASS_LEFT);
} else {
gtk_style_context_add_class(style, GTK_STYLE_CLASS_TOP);
}
GdkRectangle rect = *aRect;
if (gtk_check_version(3, 20, 0) == nullptr) {
// The "trough-border" is not used since GTK 3.20. The stepper margin
// box occupies the full width of the "contents" gadget content box.
InsetByMargin(&rect, style);
} else {
// Scrollbar button has to be inset by trough_border because its DOM
// element is filling width of vertical scrollbar's track (or height
// in case of horizontal scrollbars).
GtkOrientation orientation = flags & MOZ_GTK_STEPPER_VERTICAL
? GTK_ORIENTATION_VERTICAL
: GTK_ORIENTATION_HORIZONTAL;
const ScrollbarGTKMetrics* metrics = GetScrollbarMetrics(orientation);
if (flags & MOZ_GTK_STEPPER_VERTICAL) {
rect.x += metrics->border.track.left;
rect.width = metrics->size.thumb.width;
} else {
rect.y += metrics->border.track.top;
rect.height = metrics->size.thumb.height;
}
}
gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
arrow_rect.width = rect.width / 2;
arrow_rect.height = rect.height / 2;
gfloat arrow_scaling;
gtk_style_context_get_style(style, "arrow-scaling", &arrow_scaling, NULL);
gdouble arrow_size = MIN(rect.width, rect.height) * arrow_scaling;
arrow_rect.x = rect.x + (rect.width - arrow_size) / 2;
arrow_rect.y = rect.y + (rect.height - arrow_size) / 2;
if (state_flags & GTK_STATE_FLAG_ACTIVE) {
gtk_style_context_get_style(style, "arrow-displacement-x",
&arrow_displacement_x, "arrow-displacement-y",
&arrow_displacement_y, NULL);
arrow_rect.x += arrow_displacement_x;
arrow_rect.y += arrow_displacement_y;
}
gtk_render_arrow(style, cr, arrow_angle, arrow_rect.x, arrow_rect.y,
arrow_size);
gtk_style_context_restore(style);
return MOZ_GTK_SUCCESS;
}
static void moz_gtk_update_scrollbar_style(GtkStyleContext* style,
WidgetNodeType widget,
GtkTextDirection direction) {
if (widget == MOZ_GTK_SCROLLBAR_HORIZONTAL) {
gtk_style_context_add_class(style, GTK_STYLE_CLASS_BOTTOM);
} else {
if (direction == GTK_TEXT_DIR_LTR) {
gtk_style_context_add_class(style, GTK_STYLE_CLASS_RIGHT);
gtk_style_context_remove_class(style, GTK_STYLE_CLASS_LEFT);
} else {
gtk_style_context_add_class(style, GTK_STYLE_CLASS_LEFT);
gtk_style_context_remove_class(style, GTK_STYLE_CLASS_RIGHT);
}
}
}
static void moz_gtk_draw_styled_frame(GtkStyleContext* style, cairo_t* cr,
const GdkRectangle* aRect,
bool drawFocus) {
GdkRectangle rect = *aRect;
InsetByMargin(&rect, style);
gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
if (drawFocus) {
gtk_render_focus(style, cr, rect.x, rect.y, rect.width, rect.height);
}
}
static gint moz_gtk_scrollbar_trough_paint(WidgetNodeType widget, cairo_t* cr,
const GdkRectangle* aRect,
GtkWidgetState* state,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
GdkRectangle rect = *aRect;
GtkStyleContext* style;
if (gtk_get_minor_version() >= 20) {
WidgetNodeType thumb = widget == MOZ_GTK_SCROLLBAR_TROUGH_VERTICAL
? MOZ_GTK_SCROLLBAR_THUMB_VERTICAL
: MOZ_GTK_SCROLLBAR_THUMB_HORIZONTAL;
MozGtkSize thumbSize = GetMinMarginBox(GetStyleContext(thumb));
style = GetStyleContext(widget, state->scale, direction, state_flags);
MozGtkSize trackSize = GetMinContentBox(style);
trackSize.Include(thumbSize);
trackSize += GetMarginBorderPadding(style);
// Gecko's trough |aRect| fills available breadth, but GTK's trough is
// centered in the contents_gadget. The centering here round left
// and up, like gtk_box_gadget_allocate_child().
if (widget == MOZ_GTK_SCROLLBAR_TROUGH_VERTICAL) {
rect.x += (rect.width - trackSize.width) / 2;
rect.width = trackSize.width;
} else {
rect.y += (rect.height - trackSize.height) / 2;
rect.height = trackSize.height;
}
} else {
style = GetStyleContext(widget, state->scale, direction, state_flags);
}
moz_gtk_draw_styled_frame(style, cr, &rect, state->focused);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_scrollbar_paint(WidgetNodeType widget, cairo_t* cr,
const GdkRectangle* rect,
GtkWidgetState* state,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
GtkStyleContext* style =
GetStyleContext(widget, state->scale, direction, state_flags);
moz_gtk_update_scrollbar_style(style, widget, direction);
moz_gtk_draw_styled_frame(style, cr, rect, state->focused);
style = GetStyleContext((widget == MOZ_GTK_SCROLLBAR_HORIZONTAL)
? MOZ_GTK_SCROLLBAR_CONTENTS_HORIZONTAL
: MOZ_GTK_SCROLLBAR_CONTENTS_VERTICAL,
state->scale, direction, state_flags);
moz_gtk_draw_styled_frame(style, cr, rect, state->focused);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_scrollbar_thumb_paint(WidgetNodeType widget, cairo_t* cr,
const GdkRectangle* aRect,
GtkWidgetState* state,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
GtkStyleContext* style =
GetStyleContext(widget, state->scale, direction, state_flags);
GtkOrientation orientation = (widget == MOZ_GTK_SCROLLBAR_THUMB_HORIZONTAL)
? GTK_ORIENTATION_HORIZONTAL
: GTK_ORIENTATION_VERTICAL;
GdkRectangle rect = *aRect;
const ScrollbarGTKMetrics* metrics =
(state->depressed || state->active || state->inHover)
? GetActiveScrollbarMetrics(orientation)
: GetScrollbarMetrics(orientation);
Inset(&rect, metrics->margin.thumb);
gtk_render_slider(style, cr, rect.x, rect.y, rect.width, rect.height,
orientation);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_inner_spin_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state,
GtkTextDirection direction) {
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_SPINBUTTON, state->scale, direction,
GetStateFlagsFromGtkWidgetState(state));
gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
/* hard code these values */
GdkRectangle arrow_rect;
arrow_rect.width = 6;
arrow_rect.height = 6;
// align spin to the left
arrow_rect.x = rect->x;
// up button
arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 - 3;
gtk_render_arrow(style, cr, ARROW_UP, arrow_rect.x, arrow_rect.y,
arrow_rect.width);
// down button
arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2 + 3;
gtk_render_arrow(style, cr, ARROW_DOWN, arrow_rect.x, arrow_rect.y,
arrow_rect.width);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_spin_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state,
GtkTextDirection direction) {
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_SPINBUTTON, state->scale, direction);
gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_spin_updown_paint(cairo_t* cr, GdkRectangle* rect,
gboolean isDown, GtkWidgetState* state,
GtkTextDirection direction) {
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_SPINBUTTON, state->scale, direction,
GetStateFlagsFromGtkWidgetState(state));
gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
/* hard code these values */
GdkRectangle arrow_rect;
arrow_rect.width = 6;
arrow_rect.height = 6;
arrow_rect.x = rect->x + (rect->width - arrow_rect.width) / 2;
arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2;
arrow_rect.y += isDown ? -1 : 1;
gtk_render_arrow(style, cr, isDown ? ARROW_DOWN : ARROW_UP, arrow_rect.x,
arrow_rect.y, arrow_rect.width);
return MOZ_GTK_SUCCESS;
}
/* See gtk_range_draw() for reference.
*/
static gint moz_gtk_scale_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state, GtkOrientation flags,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
gint x, y, width, height, min_width, min_height;
GtkStyleContext* style;
GtkBorder margin;
moz_gtk_get_scale_metrics(flags, &min_width, &min_height);
WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
? MOZ_GTK_SCALE_TROUGH_HORIZONTAL
: MOZ_GTK_SCALE_TROUGH_VERTICAL;
style = GetStyleContext(widget, state->scale, direction, state_flags);
gtk_style_context_get_margin(style, state_flags, &margin);
// Clamp the dimension perpendicular to the direction that the slider crosses
// to the minimum size.
if (flags == GTK_ORIENTATION_HORIZONTAL) {
width = rect->width - (margin.left + margin.right);
height = min_height - (margin.top + margin.bottom);
x = rect->x + margin.left;
y = rect->y + (rect->height - height) / 2;
} else {
width = min_width - (margin.left + margin.right);
height = rect->height - (margin.top + margin.bottom);
x = rect->x + (rect->width - width) / 2;
y = rect->y + margin.top;
}
gtk_render_background(style, cr, x, y, width, height);
gtk_render_frame(style, cr, x, y, width, height);
if (state->focused)
gtk_render_focus(style, cr, rect->x, rect->y, rect->width, rect->height);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_scale_thumb_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state,
GtkOrientation flags,
GtkTextDirection direction) {
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
GtkStyleContext* style;
gint thumb_width, thumb_height, x, y;
/* determine the thumb size, and position the thumb in the center in the
* opposite axis
*/
if (flags == GTK_ORIENTATION_HORIZONTAL) {
moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_HORIZONTAL, &thumb_width,
&thumb_height);
x = rect->x;
y = rect->y + (rect->height - thumb_height) / 2;
} else {
moz_gtk_get_scalethumb_metrics(GTK_ORIENTATION_VERTICAL, &thumb_height,
&thumb_width);
x = rect->x + (rect->width - thumb_width) / 2;
y = rect->y;
}
WidgetNodeType widget = (flags == GTK_ORIENTATION_HORIZONTAL)
? MOZ_GTK_SCALE_THUMB_HORIZONTAL
: MOZ_GTK_SCALE_THUMB_VERTICAL;
style = GetStyleContext(widget, state->scale, direction, state_flags);
gtk_render_slider(style, cr, x, y, thumb_width, thumb_height, flags);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_gripper_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state,
GtkTextDirection direction) {
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_GRIPPER, state->scale, direction,
GetStateFlagsFromGtkWidgetState(state));
gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height);
gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_hpaned_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state) {
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_HORIZONTAL, state->scale,
GTK_TEXT_DIR_LTR, GetStateFlagsFromGtkWidgetState(state));
gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_vpaned_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state) {
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_SPLITTER_SEPARATOR_VERTICAL, state->scale,
GTK_TEXT_DIR_LTR, GetStateFlagsFromGtkWidgetState(state));
gtk_render_handle(style, cr, rect->x, rect->y, rect->width, rect->height);
return MOZ_GTK_SUCCESS;
}
// See gtk_entry_draw() for reference.
static gint moz_gtk_entry_paint(cairo_t* cr, const GdkRectangle* aRect,
GtkWidgetState* state, GtkStyleContext* style,
WidgetNodeType widget) {
// StyleAppearance::FocusOutline
int draw_focus_outline_only = state->depressed;
GdkRectangle rect = *aRect;
if (draw_focus_outline_only) {
// Inflate the given 'rect' with the focus outline size.
gint h, v;
moz_gtk_get_focus_outline_size(style, &h, &v);
rect.x -= h;
rect.width += 2 * h;
rect.y -= v;
rect.height += 2 * v;
} else {
gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
}
// Paint the border, except for 'menulist-textfield' that isn't focused:
if (widget != MOZ_GTK_DROPDOWN_ENTRY || state->focused) {
gtk_render_frame(style, cr, rect.x, rect.y, rect.width, rect.height);
}
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_text_view_paint(cairo_t* cr, GdkRectangle* aRect,
GtkWidgetState* state,
GtkTextDirection direction) {
// GtkTextView and GtkScrolledWindow do not set active and prelight flags.
// The use of focus with MOZ_GTK_SCROLLED_WINDOW here is questionable
// because a parent widget will not have focus when its child GtkTextView
// has focus, but perhaps this may help identify a focused textarea with
// some themes as GtkTextView backgrounds do not typically render
// differently with focus.
GtkStateFlags state_flags = state->disabled ? GTK_STATE_FLAG_INSENSITIVE
: state->focused ? GTK_STATE_FLAG_FOCUSED
: GTK_STATE_FLAG_NORMAL;
GtkStyleContext* style_frame = GetStyleContext(
MOZ_GTK_SCROLLED_WINDOW, state->scale, direction, state_flags);
gtk_render_frame(style_frame, cr, aRect->x, aRect->y, aRect->width,
aRect->height);
GdkRectangle rect = *aRect;
InsetByBorderPadding(&rect, style_frame);
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_TEXT_VIEW, state->scale, direction, state_flags);
gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
// There is a separate "text" window, which usually provides the
// background behind the text. However, this is transparent in Ambiance
// for GTK 3.20, in which case the MOZ_GTK_TEXT_VIEW background is
// visible.
style = GetStyleContext(MOZ_GTK_TEXT_VIEW_TEXT, state->scale, direction,
state_flags);
gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_treeview_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state,
GtkTextDirection direction) {
gint xthickness, ythickness;
GtkStyleContext* style;
GtkStyleContext* style_tree;
GtkStateFlags state_flags;
GtkBorder border;
/* only handle disabled and normal states, otherwise the whole background
* area will be painted differently with other states */
state_flags =
state->disabled ? GTK_STATE_FLAG_INSENSITIVE : GTK_STATE_FLAG_NORMAL;
style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, state->scale, direction);
gtk_style_context_get_border(style, state_flags, &border);
xthickness = border.left;
ythickness = border.top;
style_tree = GetStyleContext(MOZ_GTK_TREEVIEW_VIEW, state->scale, direction);
gtk_render_background(style_tree, cr, rect->x + xthickness,
rect->y + ythickness, rect->width - 2 * xthickness,
rect->height - 2 * ythickness);
style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW, state->scale, direction);
gtk_render_frame(style, cr, rect->x, rect->y, rect->width, rect->height);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_tree_header_cell_paint(cairo_t* cr,
const GdkRectangle* aRect,
GtkWidgetState* state,
gboolean isSorted,
GtkTextDirection direction) {
moz_gtk_button_paint(cr, aRect, state, GTK_RELIEF_NORMAL,
GetWidget(MOZ_GTK_TREE_HEADER_CELL), direction);
return MOZ_GTK_SUCCESS;
}
static gint moz_gtk_tree_header_sort_arrow_paint(cairo_t* cr,
GdkRectangle* rect,
GtkWidgetState* state,
GtkArrowType arrow_type,
GtkTextDirection direction) {
GdkRectangle arrow_rect;
gdouble arrow_angle;
GtkStyleContext* style;
/* hard code these values */
arrow_rect.width = 11;
arrow_rect.height = 11;
arrow_rect.x = rect->x + (rect->width - arrow_rect.width) / 2;
arrow_rect.y = rect->y + (rect->height - arrow_rect.height) / 2;
style = GetStyleContext(MOZ_GTK_TREE_HEADER_SORTARROW, state->scale,
direction, GetStateFlagsFromGtkWidgetState(state));
switch (arrow_type) {
case GTK_ARROW_LEFT:
arrow_angle = ARROW_LEFT;
break;
case GTK_ARROW_RIGHT:
arrow_angle = ARROW_RIGHT;
break;
case GTK_ARROW_DOWN:
arrow_angle = ARROW_DOWN;
break;
default:
arrow_angle = ARROW_UP;
break;
}
if (arrow_type != GTK_ARROW_NONE)
gtk_render_arrow(style, cr, arrow_angle, arrow_rect.x, arrow_rect.y,
arrow_rect.width);
return MOZ_GTK_SUCCESS;
}
/* See gtk_expander_paint() for reference.
*/
static gint moz_gtk_treeview_expander_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state,
GtkExpanderStyle expander_state,
GtkTextDirection direction) {
/* Because the frame we get is of the entire treeview, we can't get the
* precise event state of one expander, thus rendering hover and active
* feedback useless. */
GtkStateFlags state_flags =
state->disabled ? GTK_STATE_FLAG_INSENSITIVE : GTK_STATE_FLAG_NORMAL;
if (state->inHover)
state_flags =
static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_PRELIGHT);
if (state->selected)
state_flags =
static_cast<GtkStateFlags>(state_flags | GTK_STATE_FLAG_SELECTED);
/* GTK_STATE_FLAG_ACTIVE controls expanded/colapsed state rendering
* in gtk_render_expander()
*/
if (expander_state == GTK_EXPANDER_EXPANDED)
state_flags =
static_cast<GtkStateFlags>(state_flags | checkbox_check_state);
else
state_flags =
static_cast<GtkStateFlags>(state_flags & ~(checkbox_check_state));
GtkStyleContext* style = GetStyleContext(
MOZ_GTK_TREEVIEW_EXPANDER, state->scale, direction, state_flags);
gtk_render_expander(style, cr, rect->x, rect->y, rect->width, rect->height);
return MOZ_GTK_SUCCESS;
}
/* See gtk_separator_draw() for reference.
*/
static gint moz_gtk_combo_box_paint(cairo_t* cr, const GdkRectangle* aRect,
GtkWidgetState* state,
GtkTextDirection direction) {
GdkRectangle arrow_rect, real_arrow_rect;
gint separator_width;
gboolean wide_separators;
GtkStyleContext* style;
GtkRequisition arrow_req;
GtkWidget* comboBoxButton = GetWidget(MOZ_GTK_COMBOBOX_BUTTON);
GtkWidget* comboBoxArrow = GetWidget(MOZ_GTK_COMBOBOX_ARROW);
/* Also sets the direction on gComboBoxButtonWidget, which is then
* inherited by the separator and arrow */
moz_gtk_button_paint(cr, aRect, state, GTK_RELIEF_NORMAL, comboBoxButton,
direction);
calculate_button_inner_rect(comboBoxButton, aRect, &arrow_rect, direction);
/* Now arrow_rect contains the inner rect ; we want to correct the width
* to what the arrow needs (see gtk_combo_box_size_allocate) */
gtk_widget_get_preferred_size(comboBoxArrow, NULL, &arrow_req);
moz_gtk_sanity_preferred_size(&arrow_req);
if (direction == GTK_TEXT_DIR_LTR)
arrow_rect.x += arrow_rect.width - arrow_req.width;
arrow_rect.width = arrow_req.width;
calculate_arrow_rect(comboBoxArrow, &arrow_rect, &real_arrow_rect, direction);
style = GetStyleContext(MOZ_GTK_COMBOBOX_ARROW, state->scale);
gtk_render_arrow(style, cr, ARROW_DOWN, real_arrow_rect.x, real_arrow_rect.y,
real_arrow_rect.width);
/* If there is no separator in the theme, there's nothing left to do. */
GtkWidget* widget = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR);
if (!widget) return MOZ_GTK_SUCCESS;
style = gtk_widget_get_style_context(widget);
StyleContextSetScale(style, state->scale);
gtk_style_context_get_style(style, "wide-separators", &wide_separators,
"separator-width", &separator_width, NULL);
if (wide_separators) {
if (direction == GTK_TEXT_DIR_LTR)
arrow_rect.x -= separator_width;
else
arrow_rect.x += arrow_rect.width;
gtk_render_frame(style, cr, arrow_rect.x, arrow_rect.y, separator_width,
arrow_rect.height);
} else {
if (direction == GTK_TEXT_DIR_LTR) {
GtkBorder padding;
GtkStateFlags state_flags = GetStateFlagsFromGtkWidgetState(state);
gtk_style_context_get_padding(style, state_flags, &padding);
arrow_rect.x -= padding.left;
} else
arrow_rect.x += arrow_rect.width;
gtk_render_line(style, cr, arrow_rect.x, arrow_rect.y, arrow_rect.x,
arrow_rect.y + arrow_rect.height);
}
return MOZ_GTK_SUCCESS;
}