#include "config.h"
-#include <math.h>
+#include "gtkscrolledwindow.h"
+#include "gtkadjustment.h"
#include "gtkbindings.h"
-#include "gtkmarshalers.h"
-#include "gtkscrollable.h"
-#include "gtkscrollbar.h"
-#include "gtkscrolledwindow.h"
-#include "gtkwindow.h"
-#include "gtkviewport.h"
#include "gtkdnd.h"
+#include "gtkintl.h"
#include "gtkmain.h"
+#include "gtkmarshalers.h"
#include "gtkprivate.h"
+#include "gtkscrollable.h"
+#include "gtkscrollbar.h"
#include "gtktypebuiltins.h"
-#include "gtkintl.h"
#include "gtkviewport.h"
#include "gtkwidgetprivate.h"
+#include "gtkwindow.h"
#include "a11y/gtkscrolledwindowaccessible.h"
+#include <math.h>
+
/**
* SECTION:gtkscrolledwindow
* @Short_description: Adds scrollbars to its child widget
* If a widget has native scrolling abilities, it can be added to the
* #GtkScrolledWindow with gtk_container_add(). If a widget does not, you
* must first add the widget to a #GtkViewport, then add the #GtkViewport
- * to the scrolled window. The convenience function
- * gtk_scrolled_window_add_with_viewport() does exactly this, so you can
+ * to the scrolled window. gtk_container_add() will do this for you for
+ * widgets that don't implement #GtkScrollable natively, so you can
* ignore the presence of the viewport.
*
* The position of the scrollbars is controlled by the scroll
#define TOUCH_BYPASS_CAPTURED_THRESHOLD 30
/* Kinetic scrolling */
-#define FRAME_INTERVAL (1000 / 60)
#define MAX_OVERSHOOT_DISTANCE 50
#define FRICTION_DECELERATION 0.003
#define OVERSHOOT_INVERSE_ACCELERATION 0.003
static void gtk_scrolled_window_unrealize (GtkWidget *widget);
static void gtk_scrolled_window_map (GtkWidget *widget);
static void gtk_scrolled_window_unmap (GtkWidget *widget);
+
static void gtk_scrolled_window_grab_notify (GtkWidget *widget,
gboolean was_grabbed);
gboolean allow_overshooting,
gboolean snap_to_border);
+static void gtk_scrolled_window_cancel_deceleration (GtkScrolledWindow *scrolled_window);
+
static guint signals[LAST_SIGNAL] = {0};
G_DEFINE_TYPE (GtkScrolledWindow, gtk_scrolled_window, GTK_TYPE_BIN)
cairo_t *cr)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
+ GtkWidget *widget = GTK_WIDGET (scrolled_window);
GtkAllocation wid_allocation, hscr_allocation, vscr_allocation;
GtkStyleContext *context;
GdkRectangle junction_rect;
- gboolean is_rtl;
+ gboolean is_rtl, scrollbars_within_bevel;
- is_rtl = gtk_widget_get_direction (GTK_WIDGET (scrolled_window)) == GTK_TEXT_DIR_RTL;
- gtk_widget_get_allocation (GTK_WIDGET (scrolled_window), &wid_allocation);
+ is_rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
+ gtk_widget_get_allocation (widget, &wid_allocation);
gtk_widget_get_allocation (GTK_WIDGET (priv->hscrollbar), &hscr_allocation);
gtk_widget_get_allocation (GTK_WIDGET (priv->vscrollbar), &vscr_allocation);
- junction_rect.x = 0;
- junction_rect.y = 0;
+ gtk_widget_style_get (widget,
+ "scrollbars-within-bevel", &scrollbars_within_bevel,
+ NULL);
+ context = gtk_widget_get_style_context (widget);
+
+ if (scrollbars_within_bevel &&
+ priv->shadow_type != GTK_SHADOW_NONE)
+ {
+ GtkStateFlags state;
+ GtkBorder padding, border;
+
+ state = gtk_widget_get_state_flags (widget);
+
+ gtk_style_context_save (context);
+ gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
+ gtk_style_context_get_padding (context, state, &padding);
+ gtk_style_context_get_border (context, state, &border);
+ gtk_style_context_restore (context);
+
+ junction_rect.x = padding.left + border.left;
+ junction_rect.y = padding.top + border.top;
+ }
+ else
+ {
+ junction_rect.x = 0;
+ junction_rect.y = 0;
+ }
+
junction_rect.width = vscr_allocation.width;
junction_rect.height = hscr_allocation.height;
(!is_rtl &&
(priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
- junction_rect.x = hscr_allocation.width;
+ junction_rect.x += hscr_allocation.width;
if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
- junction_rect.y = vscr_allocation.height;
+ junction_rect.y += vscr_allocation.height;
- context = gtk_widget_get_style_context (GTK_WIDGET (scrolled_window));
gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_SCROLLBARS_JUNCTION);
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
GtkAllocation relative_allocation;
- cairo_pattern_t *pattern = NULL;
GtkStyleContext *context;
- GtkWidget *child;
context = gtk_widget_get_style_context (widget);
gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
- /* Use child's background if possible */
- child = gtk_bin_get_child (GTK_BIN (widget));
-
- if (child && gtk_widget_get_has_window (child))
- pattern = gdk_window_get_background_pattern (gtk_widget_get_window (child));
-
- if (pattern &&
- cairo_pattern_get_type (pattern) == CAIRO_PATTERN_TYPE_SOLID)
- {
- cairo_set_source (cr, pattern);
-
- cairo_rectangle (cr, relative_allocation.x, relative_allocation.y,
- relative_allocation.width, relative_allocation.height);
- cairo_fill (cr);
- }
- else
- gtk_render_background (context, cr,
- relative_allocation.x, relative_allocation.y,
- relative_allocation.width, relative_allocation.height);
+ gtk_render_background (context, cr,
+ 0, 0,
+ gtk_widget_get_allocated_width (widget), gtk_widget_get_allocated_height (widget));
if (priv->hscrollbar_visible &&
priv->vscrollbar_visible)
gtk_widget_get_preferred_height (priv->hscrollbar, &sb_height, NULL);
gtk_widget_get_preferred_width (priv->vscrollbar, &sb_width, NULL);
- /* Subtract some things from our available allocation size */
+ gtk_widget_get_allocation (widget, &widget_allocation);
+
allocation->x = 0;
allocation->y = 0;
+ allocation->width = widget_allocation.width;
+ allocation->height = widget_allocation.height;
+ /* Subtract some things from our available allocation size */
if (priv->shadow_type != GTK_SHADOW_NONE)
{
GtkStyleContext *context;
allocation->x += padding.left + border.left;
allocation->y += padding.top + border.top;
+ allocation->width = MAX (1, allocation->width - (padding.left + border.left + padding.right + border.right));
+ allocation->height = MAX (1, allocation->height - (padding.top + border.top + padding.bottom + border.bottom));
gtk_style_context_restore (context);
}
- gtk_widget_get_allocation (widget, &widget_allocation);
- allocation->width = MAX (1, (gint) widget_allocation.width - allocation->x * 2);
- allocation->height = MAX (1, (gint) widget_allocation.height - allocation->y * 2);
-
if (priv->vscrollbar_visible)
{
gboolean is_rtl;
{
gint child_scroll_width;
gint child_scroll_height;
+ GtkScrollablePolicy hscroll_policy;
+ GtkScrollablePolicy vscroll_policy;
gboolean previous_hvis;
gboolean previous_vvis;
guint count = 0;
+ hscroll_policy = GTK_IS_SCROLLABLE (child)
+ ? gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child))
+ : GTK_SCROLL_MINIMUM;
+ vscroll_policy = GTK_IS_SCROLLABLE (child)
+ ? gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child))
+ : GTK_SCROLL_MINIMUM;
+
/* Determine scrollbar visibility first via hfw apis */
if (gtk_widget_get_request_mode (child) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
{
- if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
+ if (hscroll_policy == GTK_SCROLL_MINIMUM)
gtk_widget_get_preferred_width (child, &child_scroll_width, NULL);
else
gtk_widget_get_preferred_width (child, NULL, &child_scroll_width);
{
/* First try without a vertical scrollbar if the content will fit the height
* given the extra width of the scrollbar */
- if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
+ if (vscroll_policy == GTK_SCROLL_MINIMUM)
gtk_widget_get_preferred_height_for_width (child,
MAX (allocation->width, child_scroll_width),
&child_scroll_height, NULL);
}
else /* GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT */
{
- if (gtk_scrollable_get_vscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
+ if (vscroll_policy == GTK_SCROLL_MINIMUM)
gtk_widget_get_preferred_height (child, &child_scroll_height, NULL);
else
gtk_widget_get_preferred_height (child, NULL, &child_scroll_height);
{
/* First try without a horizontal scrollbar if the content will fit the width
* given the extra height of the scrollbar */
- if (gtk_scrollable_get_hscroll_policy (GTK_SCROLLABLE (child)) == GTK_SCROLL_MINIMUM)
+ if (hscroll_policy == GTK_SCROLL_MINIMUM)
gtk_widget_get_preferred_width_for_height (child,
MAX (allocation->height, child_scroll_height),
&child_scroll_width, NULL);
gtk_scrolled_window_relative_allocation (widget, &relative_allocation);
}
+ gtk_widget_set_child_visible (priv->hscrollbar, priv->hscrollbar_visible);
if (priv->hscrollbar_visible)
{
- if (!gtk_widget_get_visible (priv->hscrollbar))
- gtk_widget_show (priv->hscrollbar);
-
child_allocation.x = relative_allocation.x;
if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
child_allocation.y = (relative_allocation.y +
relative_allocation.height +
- sb_spacing +
- (priv->shadow_type == GTK_SHADOW_NONE ?
- 0 : padding.top + border.top));
+ sb_spacing);
else
- child_allocation.y = 0;
+ child_allocation.y = relative_allocation.y - sb_spacing - sb_height;
child_allocation.width = relative_allocation.width;
child_allocation.height = sb_height;
{
child_allocation.x -= padding.left + border.left;
child_allocation.width += padding.left + padding.right + border.left + border.right;
- }
- else if (GTK_CORNER_TOP_RIGHT == priv->real_window_placement ||
- GTK_CORNER_TOP_LEFT == priv->real_window_placement)
- {
- child_allocation.y -= padding.top + border.top;
- }
- else
- {
- child_allocation.y += padding.top + border.top;
+
+ if (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
+ priv->real_window_placement == GTK_CORNER_TOP_RIGHT)
+ child_allocation.y += padding.bottom + border.bottom;
+ else
+ child_allocation.y -= padding.top + border.top;
}
}
gtk_widget_size_allocate (priv->hscrollbar, &child_allocation);
}
- else if (gtk_widget_get_visible (priv->hscrollbar))
- gtk_widget_hide (priv->hscrollbar);
+ gtk_widget_set_child_visible (priv->vscrollbar, priv->vscrollbar_visible);
if (priv->vscrollbar_visible)
{
- if (!gtk_widget_get_visible (priv->vscrollbar))
- gtk_widget_show (priv->vscrollbar);
-
if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
(priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
child_allocation.x = (relative_allocation.x +
relative_allocation.width +
- sb_spacing +
- (priv->shadow_type == GTK_SHADOW_NONE ?
- 0 : padding.left + border.left));
+ sb_spacing);
else
- child_allocation.x = 0;
+ child_allocation.x = relative_allocation.x - sb_spacing - sb_width;
child_allocation.y = relative_allocation.y;
child_allocation.width = sb_width;
{
child_allocation.y -= padding.top + border.top;
child_allocation.height += padding.top + padding.bottom + border.top + border.bottom;
+
+ if ((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL &&
+ (priv->real_window_placement == GTK_CORNER_TOP_RIGHT ||
+ priv->real_window_placement == GTK_CORNER_BOTTOM_RIGHT)) ||
+ (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR &&
+ (priv->real_window_placement == GTK_CORNER_TOP_LEFT ||
+ priv->real_window_placement == GTK_CORNER_BOTTOM_LEFT)))
+ child_allocation.x += padding.right + border.right;
+ else
+ child_allocation.x -= padding.left + border.left;
}
- else if (GTK_CORNER_BOTTOM_LEFT == priv->real_window_placement ||
- GTK_CORNER_TOP_LEFT == priv->real_window_placement)
- {
- child_allocation.x -= padding.left + border.left;
- }
- else
- {
- child_allocation.x += padding.left + border.left;
- }
- }
+ }
gtk_widget_size_allocate (priv->vscrollbar, &child_allocation);
}
- else if (gtk_widget_get_visible (priv->vscrollbar))
- gtk_widget_hide (priv->vscrollbar);
_gtk_scrolled_window_allocate_overshoot_window (scrolled_window);
}
{
GtkScrolledWindowPrivate *priv;
GtkScrolledWindow *scrolled_window;
- GtkWidget *range;
+ gboolean handled = FALSE;
+ gdouble delta_x;
+ gdouble delta_y;
+ gdouble delta;
g_return_val_if_fail (GTK_IS_SCROLLED_WINDOW (widget), FALSE);
- g_return_val_if_fail (event != NULL, FALSE);
+ g_return_val_if_fail (event != NULL, FALSE);
scrolled_window = GTK_SCROLLED_WINDOW (widget);
priv = scrolled_window->priv;
- if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
- range = priv->vscrollbar;
- else
- range = priv->hscrollbar;
+ if (gdk_event_get_scroll_deltas ((GdkEvent *) event, &delta_x, &delta_y))
+ {
+ if (delta_x != 0.0 && priv->hscrollbar &&
+ gtk_widget_get_visible (priv->hscrollbar))
+ {
+ GtkAdjustment *adj;
+ gdouble new_value;
+ gdouble page_size;
+ gdouble scroll_unit;
+
+ adj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
+ page_size = gtk_adjustment_get_page_size (adj);
+ scroll_unit = pow (page_size, 2.0 / 3.0);
- if (range && gtk_widget_get_visible (range))
+ new_value = CLAMP (gtk_adjustment_get_value (adj) + delta_x * scroll_unit,
+ gtk_adjustment_get_lower (adj),
+ gtk_adjustment_get_upper (adj) -
+ gtk_adjustment_get_page_size (adj));
+
+ gtk_adjustment_set_value (adj, new_value);
+
+ handled = TRUE;
+ }
+
+ if (delta_y != 0.0 && priv->vscrollbar &&
+ gtk_widget_get_visible (priv->vscrollbar))
+ {
+ GtkAdjustment *adj;
+ gdouble new_value;
+ gdouble page_size;
+ gdouble scroll_unit;
+
+ adj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
+ page_size = gtk_adjustment_get_page_size (adj);
+ scroll_unit = pow (page_size, 2.0 / 3.0);
+
+ new_value = CLAMP (gtk_adjustment_get_value (adj) + delta_y * scroll_unit,
+ gtk_adjustment_get_lower (adj),
+ gtk_adjustment_get_upper (adj) -
+ gtk_adjustment_get_page_size (adj));
+
+ gtk_adjustment_set_value (adj, new_value);
+
+ handled = TRUE;
+ }
+ }
+ else
{
- GtkAdjustment *adjustment = gtk_range_get_adjustment (GTK_RANGE (range));
- gdouble delta;
+ GtkWidget *range;
- delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event->direction);
- gtk_adjustment_set_value (adjustment, gtk_adjustment_get_value (adjustment) + delta);
+ if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
+ range = priv->vscrollbar;
+ else
+ range = priv->hscrollbar;
- return TRUE;
+ if (range && gtk_widget_get_visible (range))
+ {
+ GtkAdjustment *adj = gtk_range_get_adjustment (GTK_RANGE (range));
+ gdouble new_value;
+
+ delta = _gtk_range_get_wheel_delta (GTK_RANGE (range), event);
+
+ new_value = CLAMP (gtk_adjustment_get_value (adj) + delta,
+ gtk_adjustment_get_lower (adj),
+ gtk_adjustment_get_upper (adj) -
+ gtk_adjustment_get_page_size (adj));
+
+ gtk_adjustment_set_value (adj, new_value);
+
+ handled = TRUE;
+ }
}
- return FALSE;
+ return handled;
}
static gboolean
}
static gboolean
-scrolled_window_deceleration_cb (gpointer user_data)
+scrolled_window_deceleration_cb (GtkWidget *widdget,
+ GdkFrameClock *frame_clock,
+ gpointer user_data)
{
KineticScrollData *data = user_data;
GtkScrolledWindow *scrolled_window = data->scrolled_window;
_gtk_scrolled_window_get_overshoot (scrolled_window,
&old_overshoot_x, &old_overshoot_y);
- current_time = g_get_monotonic_time ();
+ current_time = gdk_frame_clock_get_frame_time (frame_clock);
elapsed = (current_time - data->last_deceleration_time) / 1000;
data->last_deceleration_time = current_time;
_gtk_scrolled_window_allocate_overshoot_window (scrolled_window);
}
- if (overshoot_x != 0 || overshoot_y != 0 ||
- data->x_velocity != 0 || data->y_velocity != 0)
- return TRUE;
- else
- {
- priv->deceleration_id = 0;
- return FALSE;
- }
+ if (overshoot_x == 0 && overshoot_y == 0 &&
+ data->x_velocity == 0 && data->y_velocity == 0)
+ gtk_scrolled_window_cancel_deceleration (scrolled_window);
+
+ return G_SOURCE_CONTINUE;
}
static void
if (priv->deceleration_id)
{
- g_source_remove (priv->deceleration_id);
+ gtk_widget_remove_tick_callback (GTK_WIDGET (scrolled_window),
+ priv->deceleration_id);
priv->deceleration_id = 0;
}
}
gtk_scrolled_window_start_deceleration (GtkScrolledWindow *scrolled_window)
{
GtkScrolledWindowPrivate *priv = scrolled_window->priv;
+ GdkFrameClock *frame_clock;
KineticScrollData *data;
gdouble angle;
+ frame_clock = gtk_widget_get_frame_clock (GTK_WIDGET (scrolled_window));
+
data = g_new0 (KineticScrollData, 1);
data->scrolled_window = scrolled_window;
- data->last_deceleration_time = g_get_monotonic_time ();
+ data->last_deceleration_time = gdk_frame_clock_get_frame_time (frame_clock);
data->x_velocity = priv->x_velocity;
data->y_velocity = priv->y_velocity;
data->vel_sine = sin (angle);
scrolled_window->priv->deceleration_id =
- gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT,
- FRAME_INTERVAL,
+ gtk_widget_add_tick_callback (GTK_WIDGET (scrolled_window),
scrolled_window_deceleration_cb,
- data, (GDestroyNotify) g_free);
+ data,
+ (GDestroyNotify) g_free);
}
static gboolean
gboolean retval = FALSE;
GtkScrolledWindowPrivate *priv = GTK_SCROLLED_WINDOW (widget)->priv;
+ if (gdk_window_get_window_type (event->any.window) == GDK_WINDOW_TEMP)
+ return FALSE;
+
switch (event->type)
{
case GDK_TOUCH_BEGIN:
GtkScrolledWindowPrivate *priv;
GtkScrolledWindow *scrolled_window;
GtkBin *bin;
- GtkWidget *child_widget;
+ GtkWidget *child_widget, *scrollable_child;
GtkAdjustment *hadj, *vadj;
bin = GTK_BIN (container);
scrolled_window = GTK_SCROLLED_WINDOW (container);
priv = scrolled_window->priv;
+ if (GTK_IS_SCROLLABLE (child))
+ {
+ scrollable_child = child;
+ }
+ else
+ {
+ scrollable_child = gtk_viewport_new (NULL, NULL);
+ gtk_widget_show (scrollable_child);
+ gtk_container_add (GTK_CONTAINER (scrollable_child), child);
+ }
+
if (gtk_widget_get_realized (GTK_WIDGET (bin)))
- gtk_widget_set_parent_window (child, priv->overshoot_window);
+ gtk_widget_set_parent_window (scrollable_child, priv->overshoot_window);
- _gtk_bin_set_child (bin, child);
- gtk_widget_set_parent (child, GTK_WIDGET (bin));
+ _gtk_bin_set_child (bin, scrollable_child);
+ gtk_widget_set_parent (scrollable_child, GTK_WIDGET (bin));
hadj = gtk_range_get_adjustment (GTK_RANGE (priv->hscrollbar));
vadj = gtk_range_get_adjustment (GTK_RANGE (priv->vscrollbar));
- if (GTK_IS_SCROLLABLE (child))
- g_object_set (child, "hadjustment", hadj, "vadjustment", vadj, NULL);
- else
- g_warning ("gtk_scrolled_window_add(): cannot add non scrollable widget "
- "use gtk_scrolled_window_add_with_viewport() instead");
+ g_object_set (scrollable_child, "hadjustment", hadj, "vadjustment", vadj, NULL);
}
static void
*
* A widget supports scrolling natively if it implements the
* #GtkScrollable interface.
+ *
+ * Deprecated: 3.8: gtk_container_add() will now automatically add
+ * a #GtkViewport if the child doesn't implement #GtkScrollable.
*/
void
gtk_scrolled_window_add_with_viewport (GtkScrolledWindow *scrolled_window,
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.visual = gtk_widget_get_visual (widget);
attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK |
- GDK_BUTTON_MOTION_MASK | GDK_TOUCH_MASK;
+ GDK_BUTTON_MOTION_MASK | GDK_TOUCH_MASK | GDK_EXPOSURE_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
scrolled_window->priv->overshoot_window =
gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask);
-
- gdk_window_set_user_data (scrolled_window->priv->overshoot_window, widget);
+ gtk_widget_register_window (widget, scrolled_window->priv->overshoot_window);
child_widget = gtk_bin_get_child (GTK_BIN (widget));
{
GtkScrolledWindow *scrolled_window = GTK_SCROLLED_WINDOW (widget);
- gdk_window_set_user_data (scrolled_window->priv->overshoot_window, NULL);
+ gtk_widget_unregister_window (widget, scrolled_window->priv->overshoot_window);
gdk_window_destroy (scrolled_window->priv->overshoot_window);
scrolled_window->priv->overshoot_window = NULL;