* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
#include "gtkviewport.h"
+#include "gtkadjustment.h"
#include "gtkintl.h"
#include "gtkmarshalers.h"
+#include "gtkprivate.h"
#include "gtkscrollable.h"
#include "gtktypebuiltins.h"
-#include "gtkprivate.h"
/**
* The #GtkViewport widget acts as an adaptor class, implementing
* scrollability for child widgets that lack their own scrolling
* capabilities. Use #GtkViewport to scroll child widgets such as
- * #GtkTable, #GtkBox, and so on.
+ * #GtkGrid, #GtkBox, and so on.
*
* If a widget has native scrolling abilities, such as #GtkTextView,
- * #GtkTreeView or #GtkIconview, it can be added to a #GtkScrolledWindow
+ * #GtkTreeView or #GtkIconView, it can be added to a #GtkScrolledWindow
* with gtk_container_add(). If a widget does not, you must first add the
* widget to a #GtkViewport, then add the viewport to the scrolled window.
- * The convenience function gtk_scrolled_window_add_with_viewport() does
- * exactly this, so you can ignore the presence of the viewport.
+ * gtk_container_add() does this automatically if a child that does not
+ * implement #GtkScrollable is added to a #GtkScrolledWindow, so you can
+ * ignore the presence of the viewport.
*
* The #GtkViewport will start scrolling content only if allocated less
* than the child widget's minimum size in a given orientation.
};
-static void gtk_viewport_finalize (GObject *object);
static void gtk_viewport_set_property (GObject *object,
guint prop_id,
const GValue *value,
GtkAllocation *allocation);
static void gtk_viewport_adjustment_value_changed (GtkAdjustment *adjustment,
gpointer data);
-static void gtk_viewport_style_set (GtkWidget *widget,
- GtkStyle *previous_style);
+static void gtk_viewport_style_updated (GtkWidget *widget);
static void gtk_viewport_get_preferred_width (GtkWidget *widget,
gint *minimum_size,
gint *minimum_size,
gint *natural_size);
+static void viewport_set_adjustment (GtkViewport *viewport,
+ GtkOrientation orientation,
+ GtkAdjustment *adjustment);
G_DEFINE_TYPE_WITH_CODE (GtkViewport, gtk_viewport, GTK_TYPE_BIN,
G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
widget_class = (GtkWidgetClass*) class;
container_class = (GtkContainerClass*) class;
- gobject_class->finalize = gtk_viewport_finalize;
gobject_class->set_property = gtk_viewport_set_property;
gobject_class->get_property = gtk_viewport_get_property;
widget_class->unrealize = gtk_viewport_unrealize;
widget_class->draw = gtk_viewport_draw;
widget_class->size_allocate = gtk_viewport_size_allocate;
- widget_class->style_set = gtk_viewport_style_set;
+ widget_class->style_updated = gtk_viewport_style_updated;
widget_class->get_preferred_width = gtk_viewport_get_preferred_width;
widget_class->get_preferred_height = gtk_viewport_get_preferred_height;
+ gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_VIEWPORT);
+
container_class->add = gtk_viewport_add;
/* GtkScrollable implementation */
switch (prop_id)
{
case PROP_HADJUSTMENT:
- gtk_viewport_set_hadjustment (viewport, g_value_get_object (value));
+ viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, g_value_get_object (value));
break;
case PROP_VADJUSTMENT:
- gtk_viewport_set_vadjustment (viewport, g_value_get_object (value));
+ viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, g_value_get_object (value));
break;
case PROP_HSCROLL_POLICY:
viewport->priv->hscroll_policy = g_value_get_enum (value);
gtk_widget_set_has_window (GTK_WIDGET (viewport), TRUE);
gtk_widget_set_redraw_on_allocate (GTK_WIDGET (viewport), FALSE);
- gtk_container_set_resize_mode (GTK_CONTAINER (viewport), GTK_RESIZE_QUEUE);
priv->shadow_type = GTK_SHADOW_IN;
priv->view_window = NULL;
priv->bin_window = NULL;
priv->hadjustment = NULL;
priv->vadjustment = NULL;
+
+ viewport_set_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL, NULL);
+ viewport_set_adjustment (viewport, GTK_ORIENTATION_VERTICAL, NULL);
}
/**
* gtk_viewport_new:
- * @hadjustment: horizontal adjustment.
- * @vadjustment: vertical adjustment.
- * @returns: a new #GtkViewport.
+ * @hadjustment: (allow-none): horizontal adjustment
+ * @vadjustment: (allow-none): vertical adjustment
*
- * Creates a new #GtkViewport with the given adjustments.
+ * Creates a new #GtkViewport with the given adjustments, or with default
+ * adjustments if none are given.
*
- **/
+ * Returns: a new #GtkViewport
+ */
GtkWidget*
gtk_viewport_new (GtkAdjustment *hadjustment,
GtkAdjustment *vadjustment)
}
}
-static void
-gtk_viewport_finalize (GObject *object)
-{
- GtkViewport *viewport = GTK_VIEWPORT (object);
-
- viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_HORIZONTAL);
- viewport_disconnect_adjustment (viewport, GTK_ORIENTATION_VERTICAL);
-
- G_OBJECT_CLASS (gtk_viewport_parent_class)->finalize (object);
-}
-
static void
gtk_viewport_destroy (GtkWidget *widget)
{
GtkAllocation *view_allocation)
{
GtkViewportPrivate *priv = viewport->priv;
- GtkStyle *style;
GtkWidget *widget = GTK_WIDGET (viewport);
GtkAllocation allocation;
+ GtkStyleContext *context;
+ GtkStateFlags state;
+ GtkBorder padding, border;
guint border_width;
gtk_widget_get_allocation (widget, &allocation);
view_allocation->x = 0;
view_allocation->y = 0;
+ context = gtk_widget_get_style_context (widget);
+ 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);
+
if (priv->shadow_type != GTK_SHADOW_NONE)
{
- style = gtk_widget_get_style (widget);
- view_allocation->x = style->xthickness;
- view_allocation->y = style->ythickness;
+ view_allocation->x = border.left;
+ view_allocation->y = border.top;
}
- view_allocation->width = MAX (1, allocation.width - view_allocation->x * 2 - border_width * 2);
- view_allocation->height = MAX (1, allocation.height - view_allocation->y * 2 - border_width * 2);
+ view_allocation->x += padding.left;
+ view_allocation->y += padding.top;
+ view_allocation->width = MAX (1, allocation.width - padding.left - padding.right - border_width * 2);
+ view_allocation->height = MAX (1, allocation.height - padding.top - padding.bottom - border_width * 2);
+
+ if (priv->shadow_type != GTK_SHADOW_NONE)
+ {
+ view_allocation->width = MAX (1, view_allocation->width - border.left - border.right);
+ view_allocation->height = MAX (1, view_allocation->height - border.top - border.bottom);
+ }
}
/**
GtkAdjustment*
gtk_viewport_get_hadjustment (GtkViewport *viewport)
{
- GtkViewportPrivate *priv;
-
g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
- priv = viewport->priv;
-
- if (!priv->hadjustment)
- gtk_viewport_set_hadjustment (viewport, NULL);
-
- return priv->hadjustment;
+ return viewport->priv->hadjustment;
}
/**
GtkAdjustment*
gtk_viewport_get_vadjustment (GtkViewport *viewport)
{
- GtkViewportPrivate *priv;
-
g_return_val_if_fail (GTK_IS_VIEWPORT (viewport), NULL);
- priv = viewport->priv;
-
- if (!priv->vadjustment)
- gtk_viewport_set_vadjustment (viewport, NULL);
-
- return priv->vadjustment;
+ return viewport->priv->vadjustment;
}
static void
{
GtkBin *bin = GTK_BIN (viewport);
GtkAllocation view_allocation;
- GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
+ GtkAdjustment *hadjustment = viewport->priv->hadjustment;
GtkWidget *child;
gdouble upper, value;
{
GtkBin *bin = GTK_BIN (viewport);
GtkAllocation view_allocation;
- GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
+ GtkAdjustment *vadjustment = viewport->priv->vadjustment;
GtkWidget *child;
gdouble upper;
GtkViewport *viewport = GTK_VIEWPORT (widget);
GtkViewportPrivate *priv = viewport->priv;
GtkBin *bin = GTK_BIN (widget);
- GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
- GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
+ GtkAdjustment *hadjustment = priv->hadjustment;
+ GtkAdjustment *vadjustment = priv->vadjustment;
GtkAllocation allocation;
GtkAllocation view_allocation;
- GtkStyle *style;
+ GtkStyleContext *context;
GtkWidget *child;
GdkWindow *window;
GdkWindowAttr attributes;
attributes.visual = gtk_widget_get_visual (widget);
event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
- /* We select on button_press_mask so that button 4-5 scrolls are trapped.
- */
- attributes.event_mask = event_mask | GDK_BUTTON_PRESS_MASK;
+
+ attributes.event_mask = event_mask | GDK_SCROLL_MASK | GDK_TOUCH_MASK | GDK_SMOOTH_SCROLL_MASK;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
window = gdk_window_new (gtk_widget_get_parent_window (widget),
&attributes, attributes_mask);
gtk_widget_set_window (widget, window);
- gdk_window_set_user_data (window, viewport);
+ gtk_widget_register_window (widget, window);
viewport_get_view_allocation (viewport, &view_allocation);
priv->view_window = gdk_window_new (window,
&attributes, attributes_mask);
- gdk_window_set_user_data (priv->view_window, viewport);
+ gtk_widget_register_window (widget, priv->view_window);
- attributes.x = - hadjustment->value;
- attributes.y = - vadjustment->value;
- attributes.width = hadjustment->upper;
- attributes.height = vadjustment->upper;
+ attributes.x = - gtk_adjustment_get_value (hadjustment);
+ attributes.y = - gtk_adjustment_get_value (vadjustment);
+ attributes.width = gtk_adjustment_get_upper (hadjustment);
+ attributes.height = gtk_adjustment_get_upper (vadjustment);
attributes.event_mask = event_mask;
priv->bin_window = gdk_window_new (priv->view_window, &attributes, attributes_mask);
- gdk_window_set_user_data (priv->bin_window, viewport);
+ gtk_widget_register_window (widget, priv->bin_window);
child = gtk_bin_get_child (bin);
if (child)
gtk_widget_set_parent_window (child, priv->bin_window);
- gtk_widget_style_attach (widget);
- style = gtk_widget_get_style (widget);
- gtk_style_set_background (style, window, GTK_STATE_NORMAL);
- gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
+ context = gtk_widget_get_style_context (widget);
+ gtk_style_context_set_background (context, window);
+ gtk_style_context_set_background (context, priv->bin_window);
gdk_window_show (priv->bin_window);
gdk_window_show (priv->view_window);
GtkViewport *viewport = GTK_VIEWPORT (widget);
GtkViewportPrivate *priv = viewport->priv;
- gdk_window_set_user_data (priv->view_window, NULL);
+ gtk_widget_unregister_window (widget, priv->view_window);
gdk_window_destroy (priv->view_window);
priv->view_window = NULL;
- gdk_window_set_user_data (priv->bin_window, NULL);
+ gtk_widget_unregister_window (widget, priv->bin_window);
gdk_window_destroy (priv->bin_window);
priv->bin_window = NULL;
{
GtkViewport *viewport = GTK_VIEWPORT (widget);
GtkViewportPrivate *priv = viewport->priv;
+ GtkStyleContext *context;
int x, y;
- if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)))
+ context = gtk_widget_get_style_context (widget);
+
+ if (gtk_cairo_should_draw_window (cr, gtk_widget_get_window (widget)) &&
+ priv->shadow_type != GTK_SHADOW_NONE)
{
- gtk_paint_shadow (gtk_widget_get_style (widget),
- cr,
- GTK_STATE_NORMAL, priv->shadow_type,
- widget, "viewport",
- 0, 0,
+ gtk_style_context_save (context);
+ gtk_style_context_add_class (context, GTK_STYLE_CLASS_FRAME);
+
+ gtk_render_frame (context, cr, 0, 0,
gdk_window_get_width (gtk_widget_get_window (widget)),
gdk_window_get_height (gtk_widget_get_window (widget)));
+
+ gtk_style_context_restore (context);
}
if (gtk_cairo_should_draw_window (cr, priv->view_window))
if (gtk_cairo_should_draw_window (cr, priv->bin_window))
{
gdk_window_get_position (priv->bin_window, &x, &y);
- gtk_paint_flat_box (gtk_widget_get_style (widget), cr,
- GTK_STATE_NORMAL, GTK_SHADOW_NONE,
- widget, "viewportbin",
- x, y,
- gdk_window_get_width (priv->bin_window),
- gdk_window_get_height (priv->bin_window));
+ gtk_render_background (context, cr, x, y,
+ gdk_window_get_width (priv->bin_window),
+ gdk_window_get_height (priv->bin_window));
GTK_WIDGET_CLASS (gtk_viewport_parent_class)->draw (widget, cr);
}
GtkViewportPrivate *priv = viewport->priv;
GtkBin *bin = GTK_BIN (widget);
guint border_width;
- GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
- GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
+ GtkAdjustment *hadjustment = priv->hadjustment;
+ GtkAdjustment *vadjustment = priv->vadjustment;
GtkAllocation child_allocation;
GtkWidget *child;
child_allocation.x = 0;
child_allocation.y = 0;
- child_allocation.width = hadjustment->upper;
- child_allocation.height = vadjustment->upper;
+ child_allocation.width = gtk_adjustment_get_upper (hadjustment);
+ child_allocation.height = gtk_adjustment_get_upper (vadjustment);
if (gtk_widget_get_realized (widget))
{
GtkAllocation view_allocation;
view_allocation.width,
view_allocation.height);
gdk_window_move_resize (priv->bin_window,
- - hadjustment->value,
- - vadjustment->value,
+ - gtk_adjustment_get_value (hadjustment),
+ - gtk_adjustment_get_value (vadjustment),
child_allocation.width,
child_allocation.height);
}
if (child && gtk_widget_get_visible (child) &&
gtk_widget_get_realized (GTK_WIDGET (viewport)))
{
- GtkAdjustment *hadjustment = gtk_viewport_get_hadjustment (viewport);
- GtkAdjustment *vadjustment = gtk_viewport_get_vadjustment (viewport);
+ GtkAdjustment *hadjustment = priv->hadjustment;
+ GtkAdjustment *vadjustment = priv->vadjustment;
gint old_x, old_y;
gint new_x, new_y;
gdk_window_get_position (priv->bin_window, &old_x, &old_y);
- new_x = - hadjustment->value;
- new_y = - vadjustment->value;
+ new_x = - gtk_adjustment_get_value (hadjustment);
+ new_y = - gtk_adjustment_get_value (vadjustment);
if (new_x != old_x || new_y != old_y)
- {
- gdk_window_move (priv->bin_window, new_x, new_y);
- gdk_window_process_updates (priv->bin_window, TRUE);
- }
+ gdk_window_move (priv->bin_window, new_x, new_y);
}
}
static void
-gtk_viewport_style_set (GtkWidget *widget,
- GtkStyle *previous_style)
+gtk_viewport_style_updated (GtkWidget *widget)
{
+ GTK_WIDGET_CLASS (gtk_viewport_parent_class)->style_updated (widget);
+
if (gtk_widget_get_realized (widget) &&
gtk_widget_get_has_window (widget))
{
- GtkStyle *style;
- GtkViewport *viewport = GTK_VIEWPORT (widget);
+ GtkStyleContext *context;
+ GtkViewport *viewport = GTK_VIEWPORT (widget);
GtkViewportPrivate *priv = viewport->priv;
- style = gtk_widget_get_style (widget);
- gtk_style_set_background (style, priv->bin_window, GTK_STATE_NORMAL);
- gtk_style_set_background (style,
- gtk_widget_get_window (widget),
- gtk_widget_get_state (widget));
+ context = gtk_widget_get_style_context (widget);
+ gtk_style_context_set_background (context, priv->bin_window);
+ gtk_style_context_set_background (context, gtk_widget_get_window (widget));
}
}
{
GtkViewport *viewport = GTK_VIEWPORT (widget);
GtkViewportPrivate *priv = viewport->priv;
- GtkStyle *style;
+ GtkStyleContext *context;
+ GtkStateFlags state;
+ GtkBorder padding, border;
GtkWidget *child;
gint child_min, child_nat;
gint minimum, natural;
*/
minimum = gtk_container_get_border_width (GTK_CONTAINER (widget));
+ context = gtk_widget_get_style_context (GTK_WIDGET (widget));
+ state = gtk_widget_get_state_flags (GTK_WIDGET (widget));
+ gtk_style_context_get_padding (context, state, &padding);
+
if (priv->shadow_type != GTK_SHADOW_NONE)
{
- style = gtk_widget_get_style (GTK_WIDGET (widget));
+ gtk_style_context_get_border (context, state, &border);
+
if (orientation == GTK_ORIENTATION_HORIZONTAL)
- minimum += 2 * style->xthickness;
+ minimum += border.left + border.right;
else
- minimum += 2 * style->ythickness;
+ minimum += border.top + border.bottom;
}
+ if (orientation == GTK_ORIENTATION_HORIZONTAL)
+ minimum += padding.left + padding.right;
+ else
+ minimum += padding.top + padding.bottom;
+
natural = minimum;
if (child && gtk_widget_get_visible (child))