gtkscrollable.c 9.81 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* gtkscrollable.c
 * Copyright (C) 2008 Tadej Borovšak <tadeboro@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21 22
 */

/**
 * SECTION:gtkscrollable
 * @Short_Description: An interface for scrollable widgets
 * @Title: GtkScrollable
 *
23
 * #GtkScrollable is an interface that is implemented by widgets with native
24 25
 * scrolling ability.
 *
26
 * To implement this interface you should override the
27 28
 * #GtkScrollable:hadjustment and #GtkScrollable:vadjustment properties.
 *
29 30
 * ## Creating a scrollable widget
 *
31
 * All scrollable widgets should do the following.
32
 *
33
 * - When a parent widget sets the scrollable child widget’s adjustments,
34
 *   the widget should populate the adjustments’
35 36 37 38 39 40 41 42 43 44 45
 *   #GtkAdjustment:lower, #GtkAdjustment:upper,
 *   #GtkAdjustment:step-increment, #GtkAdjustment:page-increment and
 *   #GtkAdjustment:page-size properties and connect to the
 *   #GtkAdjustment::value-changed signal.
 *
 * - Because its preferred size is the size for a fully expanded widget,
 *   the scrollable widget must be able to cope with underallocations.
 *   This means that it must accept any value passed to its
 *   #GtkWidgetClass.size_allocate() function.
 *
 * - When the parent allocates space to the scrollable child widget,
46
 *   the widget should update the adjustments’ properties with new values.
47 48 49
 *
 * - When any of the adjustments emits the #GtkAdjustment::value-changed signal,
 *   the scrollable widget should scroll its contents.
50 51 52 53 54
 */

#include "config.h"

#include "gtkscrollable.h"
55 56

#include "gtkadjustment.h"
57
#include "gtkprivate.h"
58
#include "gtktypebuiltins.h"
59 60 61 62 63 64 65 66 67 68 69 70
#include "gtkintl.h"

G_DEFINE_INTERFACE (GtkScrollable, gtk_scrollable, G_TYPE_OBJECT)

static void
gtk_scrollable_default_init (GtkScrollableInterface *iface)
{
  GParamSpec *pspec;

  /**
   * GtkScrollable:hadjustment:
   *
71 72
   * Horizontal #GtkAdjustment of the scrollable widget. This adjustment is
   * shared between the scrollable widget and its parent.
73 74 75 76 77 78
   *
   * Since: 3.0
   */
  pspec = g_param_spec_object ("hadjustment",
                               P_("Horizontal adjustment"),
                               P_("Horizontal adjustment that is shared "
79
                                  "between the scrollable widget and its "
80 81 82 83 84 85 86 87
                                  "controller"),
                               GTK_TYPE_ADJUSTMENT,
                               GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT);
  g_object_interface_install_property (iface, pspec);

  /**
   * GtkScrollable:vadjustment:
   *
88 89
   * Verical #GtkAdjustment of the scrollable widget. This adjustment is shared
   * between the scrollable widget and its parent.
90 91 92 93 94 95
   *
   * Since: 3.0
   */
  pspec = g_param_spec_object ("vadjustment",
                               P_("Vertical adjustment"),
                               P_("Vertical adjustment that is shared "
96
                                  "between the scrollable widget and its "
97 98 99 100
                                  "controller"),
                               GTK_TYPE_ADJUSTMENT,
                               GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT);
  g_object_interface_install_property (iface, pspec);
101 102 103 104

  /**
   * GtkScrollable:hscroll-policy:
   *
105 106
   * Determines whether horizontal scrolling should start once the scrollable
   * widget is allocated less than its minimum width or less than its natural width.
107 108 109 110 111 112 113 114
   *
   * Since: 3.0
   */
  pspec = g_param_spec_enum ("hscroll-policy",
			     P_("Horizontal Scrollable Policy"),
			     P_("How the size of the content should be determined"),
			     GTK_TYPE_SCROLLABLE_POLICY,
			     GTK_SCROLL_MINIMUM,
115
			     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
116 117 118 119 120
  g_object_interface_install_property (iface, pspec);

  /**
   * GtkScrollable:vscroll-policy:
   *
121 122
   * Determines whether vertical scrolling should start once the scrollable
   * widget is allocated less than its minimum height or less than its natural height.
123 124 125 126 127 128 129 130
   *
   * Since: 3.0
   */
  pspec = g_param_spec_enum ("vscroll-policy",
			     P_("Vertical Scrollable Policy"),
			     P_("How the size of the content should be determined"),
			     GTK_TYPE_SCROLLABLE_POLICY,
			     GTK_SCROLL_MINIMUM,
131
			     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
132
  g_object_interface_install_property (iface, pspec);
133 134 135 136 137 138
}

/**
 * gtk_scrollable_get_hadjustment:
 * @scrollable: a #GtkScrollable
 *
139
 * Retrieves the #GtkAdjustment used for horizontal scrolling.
140
 *
141
 * Returns: (transfer none): horizontal #GtkAdjustment.
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
 *
 * Since: 3.0
 **/
GtkAdjustment *
gtk_scrollable_get_hadjustment (GtkScrollable *scrollable)
{
  GtkAdjustment *adj = NULL;

  g_return_val_if_fail (GTK_IS_SCROLLABLE (scrollable), NULL);

  g_object_get (scrollable, "hadjustment", &adj, NULL);

  /* Horrid hack; g_object_get() returns a new reference but
   * that contradicts the memory management conventions
   * for accessors.
   */
  if (adj)
    g_object_unref (adj);

  return adj;
}

/**
 * gtk_scrollable_set_hadjustment:
 * @scrollable: a #GtkScrollable
 * @hadjustment: (allow-none): a #GtkAdjustment
 *
 * Sets the horizontal adjustment of the #GtkScrollable.
 *
 * Since: 3.0
 **/
void
gtk_scrollable_set_hadjustment (GtkScrollable *scrollable,
                                GtkAdjustment *hadjustment)
{
  g_return_if_fail (GTK_IS_SCROLLABLE (scrollable));
  g_return_if_fail (hadjustment == NULL || GTK_IS_ADJUSTMENT (hadjustment));

  g_object_set (scrollable, "hadjustment", hadjustment, NULL);
}

/**
 * gtk_scrollable_get_vadjustment:
 * @scrollable: a #GtkScrollable
 *
187
 * Retrieves the #GtkAdjustment used for vertical scrolling.
188
 *
189
 * Returns: (transfer none): vertical #GtkAdjustment.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
 *
 * Since: 3.0
 **/
GtkAdjustment *
gtk_scrollable_get_vadjustment (GtkScrollable *scrollable)
{
  GtkAdjustment *adj = NULL;

  g_return_val_if_fail (GTK_IS_SCROLLABLE (scrollable), NULL);

  g_object_get (scrollable, "vadjustment", &adj, NULL);

  /* Horrid hack; g_object_get() returns a new reference but
   * that contradicts the memory management conventions
   * for accessors.
   */
  if (adj)
    g_object_unref (adj);

  return adj;
}

/**
 * gtk_scrollable_set_vadjustment:
 * @scrollable: a #GtkScrollable
 * @vadjustment: (allow-none): a #GtkAdjustment
 *
 * Sets the vertical adjustment of the #GtkScrollable.
 *
 * Since: 3.0
 **/
void
gtk_scrollable_set_vadjustment (GtkScrollable *scrollable,
                                GtkAdjustment *vadjustment)
{
  g_return_if_fail (GTK_IS_SCROLLABLE (scrollable));
  g_return_if_fail (vadjustment == NULL || GTK_IS_ADJUSTMENT (vadjustment));

  g_object_set (scrollable, "vadjustment", vadjustment, NULL);
}
230 231 232 233 234 235 236 237


/**
 * gtk_scrollable_get_hscroll_policy:
 * @scrollable: a #GtkScrollable
 *
 * Gets the horizontal #GtkScrollablePolicy.
 *
238
 * Returns: The horizontal #GtkScrollablePolicy.
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
 *
 * Since: 3.0
 **/
GtkScrollablePolicy
gtk_scrollable_get_hscroll_policy (GtkScrollable *scrollable)
{
  GtkScrollablePolicy policy;

  g_return_val_if_fail (GTK_IS_SCROLLABLE (scrollable), GTK_SCROLL_MINIMUM);

  g_object_get (scrollable, "hscroll-policy", &policy, NULL);

  return policy;
}

/**
 * gtk_scrollable_set_hscroll_policy:
 * @scrollable: a #GtkScrollable
 * @policy: the horizontal #GtkScrollablePolicy
 *
259 260 261
 * Sets the #GtkScrollablePolicy to determine whether
 * horizontal scrolling should start below the minimum width or
 * below the natural width.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
 *
 * Since: 3.0
 **/
void
gtk_scrollable_set_hscroll_policy (GtkScrollable       *scrollable,
				   GtkScrollablePolicy  policy)
{
  g_return_if_fail (GTK_IS_SCROLLABLE (scrollable));

  g_object_set (scrollable, "hscroll-policy", policy, NULL);
}

/**
 * gtk_scrollable_get_vscroll_policy:
 * @scrollable: a #GtkScrollable
 *
 * Gets the vertical #GtkScrollablePolicy.
 *
280
 * Returns: The vertical #GtkScrollablePolicy.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
 *
 * Since: 3.0
 **/
GtkScrollablePolicy
gtk_scrollable_get_vscroll_policy (GtkScrollable *scrollable)
{
  GtkScrollablePolicy policy;

  g_return_val_if_fail (GTK_IS_SCROLLABLE (scrollable), GTK_SCROLL_MINIMUM);

  g_object_get (scrollable, "vscroll-policy", &policy, NULL);

  return policy;
}

/**
 * gtk_scrollable_set_vscroll_policy:
 * @scrollable: a #GtkScrollable
 * @policy: the vertical #GtkScrollablePolicy
 *
301 302 303
 * Sets the #GtkScrollablePolicy to determine whether
 * vertical scrolling should start below the minimum height or
 * below the natural height.
304 305 306 307 308 309 310 311 312 313 314
 *
 * Since: 3.0
 **/
void
gtk_scrollable_set_vscroll_policy (GtkScrollable       *scrollable,
				   GtkScrollablePolicy  policy)
{
  g_return_if_fail (GTK_IS_SCROLLABLE (scrollable));

  g_object_set (scrollable, "vscroll-policy", policy, NULL);
}
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

/**
 * gtk_scrollable_get_border:
 * @scrollable: a #GtkScrollable
 * @border: return location for the results
 *
 * Returns the size of a non-scrolling border around the
 * outside of the scrollable. An example for this would
 * be treeview headers. GTK+ can use this information to
 * display overlayed graphics, like the overshoot indication,
 * at the right position.
 *
 * Returns: %TRUE if @border has been set
 *
 * Since: 3.16
 */
gboolean
gtk_scrollable_get_border (GtkScrollable *scrollable,
                           GtkBorder     *border)
{
  g_return_val_if_fail (GTK_IS_SCROLLABLE (scrollable), FALSE);
  g_return_val_if_fail (border != NULL, FALSE);

  if (GTK_SCROLLABLE_GET_IFACE (scrollable)->get_border)
    return GTK_SCROLLABLE_GET_IFACE (scrollable)->get_border (scrollable, border);

  return FALSE;
}