/* -*- Mode: C++; tab-width: 2; 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.
*/
#if 0 // It's used for debugging only to compare Gecko widget style with // the ones used by Gtk+ applications. staticvoid
style_path_print(GtkStyleContext *context)
{ const GtkWidgetPath* path = gtk_style_context_get_path(context);
// 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 staticvoid 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);
}
// 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;
// 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) { constchar* closeButton = strstr(decorationLayout, "close"); constchar* 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: // https://developer.gnome.org/gtk3/stable/GtkSettings.html#GtkSettings--gtk-decoration-layout if (aButtonLayout.IsEmpty()) { return 0;
}
/* This is available since Gtk+ 3.10 as well as GtkHeaderBar */
gtk_render_icon_surface(style, cr, surface,
aRect->x + metrics->iconXPosition,
aRect->y + metrics->iconYPosition);
gtk_style_context_restore(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);
// Inset a rectangle by the margins specified in a style context. staticvoid 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. staticvoid InsetByBorderPadding(GdkRectangle* rect, GtkStyleContext* style) {
GtkStateFlags state = gtk_style_context_get_state(style);
GtkBorder padding, border;
/* 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;
}
// See gtk_entry_draw() for reference. static gint moz_gtk_entry_paint(cairo_t* cr, const GdkRectangle* aRect,
GtkWidgetState* state, GtkStyleContext* style,
WidgetNodeType widget) {
GdkRectangle rect = *aRect;
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;
GdkRectangle rect = *aRect;
InsetByBorderPadding(&rect, style_frame);
GtkStyleContext* style = GetStyleContext(
MOZ_GTK_TEXT_VIEW, state->image_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->image_scale, direction,
state_flags);
gtk_render_background(style, cr, rect.x, rect.y, rect.width, rect.height);
/* 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;
/* 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 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->image_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;
static gint moz_gtk_tooltip_paint(cairo_t* cr, const GdkRectangle* aRect,
GtkWidgetState* state,
GtkTextDirection direction) { // Tooltip widget is made in GTK3 as following tree: // Tooltip window // Horizontal Box // Icon (not supported by Firefox) // Label // Each element can be fully styled by CSS of GTK theme. // We have to draw all elements with appropriate offset and right dimensions.
// Horizontal Box drawing // // The box element has hard-coded 6px margin-* GtkWidget properties, which // are added between the window dimensions and the CSS margin box of the // horizontal box. The frame of the tooltip window is drawn in the // 6px margin. // For drawing Horizontal Box we have to inset drawing area by that 6px // plus its CSS margin.
GtkStyleContext* boxStyle =
GetStyleContext(MOZ_GTK_TOOLTIP_BOX, state->image_scale, direction);
// Workaround unico not respecting the text direction for resizers. // See bug 1174248.
cairo_save(cr); if (direction == GTK_TEXT_DIR_RTL) {
cairo_matrix_t mat;
cairo_matrix_init_translate(&mat, 2 * rect->x + rect->width, 0);
cairo_matrix_scale(&mat, -1, 1);
cairo_transform(cr, &mat);
}
if (widget == MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE ||
widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE) { /** * The bar's size and the bar speed are set depending of the progress' * size. These could also be constant for all progress bars easily.
*/
gboolean vertical =
(widget == MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE);
/* The size of the dimension we are going to use for the animation. */ const gint progressSize = vertical ? rect->height : rect->width;
/* The bar is using a fifth of the element size, based on GtkProgressBar
* activity-blocks property. */ const gint barSize = MAX(1, progressSize / 5);
/* Represents the travel that has to be done for a complete cycle. */ const gint travel = 2 * (progressSize - barSize);
/* period equals to travel / pixelsPerMillisecond * where pixelsPerMillisecond equals progressSize / 1000.0.
* This is equivalent to 1600. */ staticconst guint period = 1600; const gint t = PR_IntervalToMilliseconds(PR_IntervalNow()) % period; const gint dx = travel * t / period;
static gint moz_gtk_get_tab_thickness(GtkStyleContext* style) { if (!notebook_has_tab_gap) return 0; /* tabs do not overdraw the tabpanel border with "no gap" style */
GtkBorder border;
gtk_style_context_get_border(style, gtk_style_context_get_state(style),
&border); if (border.top < 2) return 2; /* some themes don't set ythickness correctly */
/* actual small tabs */ static gint moz_gtk_tab_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state, GtkTabFlags flags,
GtkTextDirection direction,
WidgetNodeType widget) { /* When the tab isn't selected, we just draw a notebook extension. * When it is selected, we overwrite the adjacent border of the tabpanel * touching the tab with a pierced border (called "the gap") to make the
* tab appear physically attached to the tabpanel; see details below. */
if (direction != GTK_TEXT_DIR_RTL) {
tabRect.x += initial_gap;
}
}
focusRect = backRect = tabRect;
if (notebook_has_tab_gap) { if ((flags & MOZ_GTK_TAB_SELECTED) == 0) { /* Only draw the tab */
gtk_render_extension(style, cr, tabRect.x, tabRect.y, tabRect.width,
tabRect.height,
isBottomTab ? GTK_POS_TOP : GTK_POS_BOTTOM);
} else { /* Draw the tab and the gap * We want the gap to be positioned exactly on the tabpanel top * border; since tabbox.css may set a negative margin so that the tab * frame rect already overlaps the tabpanel frame rect, we need to take * that into account when drawing. To that effect, nsNativeThemeGTK * passes us this negative margin (bmargin in the graphic below) in the * lowest bits of |flags|. We use it to set gap_voffset, the distance * between the top of the gap and the bottom of the tab (resp. the * bottom of the gap and the top of the tab when we draw a bottom tab), * while ensuring that the gap always touches the border of the tab, * i.e. 0 <= gap_voffset <= gap_height, to avoid surprinsing results * with big negative or positive margins. * Here is a graphical explanation in the case of top tabs: * ___________________________ * / \ * | T A B | * ----------|. . . . . . . . . . . . . . .|----- top of tabpanel * : ^ bmargin : ^ * : | (-negative margin, : | * bottom : v passed in flags) : | gap_height * of -> :.............................: | (the size of the * the tab . part of the gap . | tabpanel top border) * . outside of the tab . v * ---------------------------------------------- * * To draw the gap, we use gtk_render_frame_gap(), see comment in * moz_gtk_tabpanels_paint(). This gap is made 3 * gap_height tall, * which should suffice to ensure that the only visible border is the * pierced one. If the tab is in the middle, we make the box_gap begin * a bit to the left of the tab and end a bit to the right, adjusting * the gap position so it still is under the tab, because we want the * rendering of a gap in the middle of a tabpanel. This is the role of * the gints gap_{l,r}_offset. On the contrary, if the tab is the * first, we align the start border of the box_gap with the start * border of the tab (left if LTR, right if RTL), by setting the
* appropriate offset to 0.*/
gint gap_loffset, gap_roffset, gap_voffset, gap_height;
/* Get height needed by the gap */
gap_height = moz_gtk_get_tab_thickness(style);
/* Extract gap_voffset from the first bits of flags */
gap_voffset = flags & MOZ_GTK_TAB_MARGIN_MASK; if (gap_voffset > gap_height) gap_voffset = gap_height;
/* Set gap_{l,r}_offset to appropriate values */
gap_loffset = gap_roffset = 20; /* should be enough */ if (flags & MOZ_GTK_TAB_FIRST) { if (direction == GTK_TEXT_DIR_RTL)
gap_roffset = initial_gap; else
gap_loffset = initial_gap;
}
/* Draw the gap; erase with background color before painting in
* case theme does not */
gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
backRect.width, backRect.height);
cairo_save(cr);
cairo_rectangle(cr, backRect.x, backRect.y, backRect.width,
backRect.height);
cairo_clip(cr);
/* Draw the gap; erase with background color before painting in
* case theme does not */
gtk_render_background(panelStyle, cr, backRect.x, backRect.y,
backRect.width, backRect.height);
if (state->focused) { /* Paint the focus ring */
GtkBorder padding;
gtk_style_context_get_padding(style, GetStateFlagsFromGtkWidgetState(state),
&padding);
/* tab area*/ static gint moz_gtk_tabpanels_paint(cairo_t* cr, GdkRectangle* rect,
GtkWidgetState* state,
GtkTextDirection direction) {
GtkStyleContext* style =
GetStyleContext(MOZ_GTK_TABPANELS, state->image_scale, direction);
gtk_render_background(style, cr, rect->x, rect->y, rect->width, rect->height); /* * The gap size is not needed in moz_gtk_tabpanels_paint because * the gap will be painted with the foreground tab in moz_gtk_tab_paint. * * However, if moz_gtk_tabpanels_paint just uses gtk_render_frame(), * the theme will think that there are no tabs and may draw something * different.Hence the trick of using two clip regions, and drawing the * gap outside each clip region, to get the correct frame for * a tabpanel with tabs.
*/ /* left side */
cairo_save(cr);
cairo_rectangle(cr, rect->x, rect->y, rect->x + rect->width / 2,
rect->y + rect->height);
cairo_clip(cr);
gtk_render_frame_gap(style, cr, rect->x, rect->y, rect->width, rect->height,
GTK_POS_TOP, rect->width - 1, rect->width);
cairo_restore(cr);
// Some themes like Elementary's style the container of the headerbar rather // than the header bar itself. if (HeaderBarShouldDrawContainer(widgetType)) { auto containerType = widgetType == MOZ_GTK_HEADER_BAR
? MOZ_GTK_HEADERBAR_FIXED
: MOZ_GTK_HEADERBAR_FIXED_MAXIMIZED;
style = GetStyleContext(containerType, state->image_scale,
GTK_TEXT_DIR_NONE, state_flags);
}
return MOZ_GTK_SUCCESS;
} case MOZ_GTK_ENTRY: case MOZ_GTK_DROPDOWN_ENTRY: {
style = GetStyleContext(widget);
// XXX: Subtract 1 pixel from the padding to account for the default // padding in forms.css. See bug 1187385.
*left = *top = *right = *bottom = -1;
moz_gtk_add_border_padding(style, left, top, right, bottom);
return MOZ_GTK_SUCCESS;
} case MOZ_GTK_TEXT_VIEW: case MOZ_GTK_TREEVIEW: {
style = GetStyleContext(MOZ_GTK_SCROLLED_WINDOW);
moz_gtk_add_style_border(style, left, top, right, bottom); return MOZ_GTK_SUCCESS;
} case MOZ_GTK_DROPDOWN: { /* We need to account for the arrow on the dropdown, so text * doesn't come too close to the arrow, or in some cases spill
* into the arrow. */
gboolean wide_separators;
gint separator_width;
GtkRequisition arrow_req;
GtkBorder border;
/* If there is no separator, don't try to count its width. */
separator_width = 0;
GtkWidget* comboBoxSeparator = GetWidget(MOZ_GTK_COMBOBOX_SEPARATOR); if (comboBoxSeparator) {
style = gtk_widget_get_style_context(comboBoxSeparator);
gtk_style_context_get_style(style, "wide-separators", &wide_separators, "separator-width", &separator_width, NULL);
return MOZ_GTK_SUCCESS;
} case MOZ_GTK_TABPANELS:
w = GetWidget(MOZ_GTK_TABPANELS); break; case MOZ_GTK_PROGRESSBAR:
w = GetWidget(MOZ_GTK_PROGRESSBAR); break; case MOZ_GTK_SPINBUTTON_ENTRY: case MOZ_GTK_SPINBUTTON_UP: case MOZ_GTK_SPINBUTTON_DOWN:
w = GetWidget(MOZ_GTK_SPINBUTTON); break; case MOZ_GTK_SCALE_HORIZONTAL: case MOZ_GTK_SCALE_VERTICAL:
w = GetWidget(widget); break; case MOZ_GTK_FRAME:
w = GetWidget(MOZ_GTK_FRAME); break; case MOZ_GTK_TOOLTIP: { // In GTK 3 there are 6 pixels of additional margin around the box. // See details there: // https://github.com/GNOME/gtk/blob/5ea69a136bd7e4970b3a800390e20314665aaed2/gtk/ui/gtktooltipwindow.ui#L11
*left = *right = *top = *bottom = 6;
// We also need to add margin/padding/borders from Tooltip content. // Tooltip contains horizontal box, where icon and label is put. // We ignore icon as long as we don't have support for it.
GtkStyleContext* boxStyle = GetStyleContext(MOZ_GTK_TOOLTIP_BOX);
moz_gtk_add_margin_border_padding(boxStyle, left, top, right, bottom);
return MOZ_GTK_SUCCESS;
} /* These widgets have no borders, since they are not containers. */ case MOZ_GTK_SPLITTER_HORIZONTAL: case MOZ_GTK_SPLITTER_VERTICAL: case MOZ_GTK_CHECKBUTTON: case MOZ_GTK_RADIOBUTTON: case MOZ_GTK_SCALE_THUMB_HORIZONTAL: case MOZ_GTK_SCALE_THUMB_VERTICAL: case MOZ_GTK_PROGRESS_CHUNK: case MOZ_GTK_PROGRESS_CHUNK_INDETERMINATE: case MOZ_GTK_PROGRESS_CHUNK_VERTICAL_INDETERMINATE: case MOZ_GTK_HEADER_BAR: case MOZ_GTK_HEADER_BAR_MAXIMIZED: case MOZ_GTK_HEADER_BAR_BUTTON_CLOSE: case MOZ_GTK_HEADER_BAR_BUTTON_MINIMIZE: case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE: case MOZ_GTK_HEADER_BAR_BUTTON_MAXIMIZE_RESTORE: /* These widgets have no borders.*/ case MOZ_GTK_INNER_SPIN_BUTTON: case MOZ_GTK_SPINBUTTON: case MOZ_GTK_WINDOW_DECORATION: case MOZ_GTK_WINDOW_DECORATION_SOLID: case MOZ_GTK_RESIZER: case MOZ_GTK_TOOLBARBUTTON_ARROW: case MOZ_GTK_TAB_SCROLLARROW: return MOZ_GTK_SUCCESS; default:
g_warning("Unsupported widget type: %d", widget); return MOZ_GTK_UNKNOWN_WIDGET;
} /* TODO - we're still missing some widget implementations */ if (w) {
moz_gtk_add_style_border(gtk_widget_get_style_context(w), left, top, right,
bottom);
} return MOZ_GTK_SUCCESS;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.